0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Arm架构下的Synchronization概述和案例分析

安芯教育科技 来源:安芯教育科技 2023-05-11 14:45 次阅读

本文选自极术专栏“Arm服务器”,文章将带你了解Arm架构下的Synchronization专业知识。

一、简介

随着近年来Arm服务器的应用越来越广泛,越来越多的云厂商开始提供基于Arm架构的云实例,越来越多的开发人员正在为Arm平台编写软件。

Synchronization是软件迁移和优化过程中的热门话题。基于Arm架构的服务器通常具有比其他架构更多的CPU内核,对Synchronization的深入理解显得更为重要。

Arm和X86 CPU之间最显著的区别之一是它们的内存模型:Arm架构具有与x86架构的TSO(Total Store Order)模型不同的弱内存模型。不同的内存模型可能会导致程序在一种架构上运行良好,但在另一种架构上会遇到性能问题或错误。Arm服务器更宽松的内存模型允许更多的编译器和硬件优化以提高系统性能,但代价是它更难理解并且可能更容易编写错误代码。

我们创作此文档是为了分享有关Arm架构的Synchronization专业知识,可以帮助其他架构的开发人员在Arm系统上进行开发。

二、Armv8-A架构上的Synchronization方法

本文档首先介绍了Armv8-A架构上的Synchronization相关知识,包括原子操作、Arm内存顺序和数据访问屏障指令。

2.1 原子操作

锁的实现要求原子访问,Arm架构定义了两种类型的原子访问:

Load exclusive and store exclusive

Atomic operation, which is introduced in armv8.1-a large system extension (LSE)

2.1.1 Exclusive load and store

LDREX/LDXR - The load exclusive instruction performs a load from an addressed memory location, the PE (e.g. the CPU) also marks the physical address being accessed as an exclusive access. The exclusive access mark is checked by store exclusive instructions.

STREX/STXR - The store exclusive instruction tries to a value from a register to memory if the PE (e.g. the CPU) has exclusive access to the memory address, and returns a status value of 0 if the store was successful, or of 1 if no store was performed.

2.1.2 LSE Atomic operation

LDXR/STXR使用了try and test机制,LSE不一样,它直接强制原子访问,主要有如下指令:

Compare and Swap instructions, CAS, and CASP. These instructions perform a read from memory and compare it against the value held in the first register. If the comparison is equal, the value in the second register is written to memory. If the write is performed, the read and write occur atomically such that no other modification of the memory location can take place between the read and write.

Atomic memory operation instructions, LD, and ST, whereis one of ADD, CLR, EOR, SET, SMAX, SMIN, UMAX, and UMIN. Each instruction atomically loads a value from memory, performs an operation on the values, and stores the result back to memory. The LDinstructions save the originally read value in the destination register of the instruction.

Swap instruction, SWP. This instruction atomically reads a location from memory into a register and writes back a different supplied value back to the same memory location.

2.2 Arm内存顺序

Arm架构定义了一种弱内存模型,内存访问可能不会按照代码顺序:

f3f40e8c-efc5-11ed-90ce-dac502259ad0.png

2.3 Arm数据访问屏障指令

Arm架构定义了屏障指令来保证内存访问的顺序。

DMB– Data Memory Barrier
Explicit memory accesses before the DMB are observed before any explicit access after the DMB

Does not guarantee when the operations happen, just guarantee the order

LDR X0, [X1] ;Must be seen by memory system before STR

DMB SY

ADD X2, #1 ; May be executed before or after memory system sees LDR

STR X3, [X4] ;Must be seen by memory system after LDR

DSB– Data Synchronization Barrier
A DSB is more restrictive than a DMB

Use a DSB when necessary, but do not overuse them

No instruction after a DSB will execute until:

All explicit memory accesses before the DSB in program order have completed

Any outstanding cache/TLB/branch predictor operations complete

DC ISW ; Operation must have completed before DSB can complete

STR X0, [X1] ; Access must have completed before DSB can complete

DSB SY

ADD X2, X2, #3 ;Cannot be executed until DSB completes

DMB和DSB是双向栅栏,对两个方向都限制,Armv8-a也设计了一种单向栅栏:load-acquire和store-release机制,只在一个方向上做限制。

Load-Acquire (LDAR)

All accesses after the LDAR are observed by memory system after the LDAR.

Accesses before the LDAR are not affected.

f412ad92-efc5-11ed-90ce-dac502259ad0.png

Store-Release (STLR)

All accesses before the STLR are observed by memory system before the STLR.

Accesses after the STLR are not affected.

f424103c-efc5-11ed-90ce-dac502259ad0.png

三、C++内存模型

有了语言层面的内存模型,对于大多数情况,开发者不需要去写依赖于具体架构的汇编代码,而只需要借助于良好设计的语言层面的内存模型来编写高质量代码,不必担心架构差异。


C++ memory model:
https://en.cppreference.com/w/cpp/header/atomic

f439ac08-efc5-11ed-90ce-dac502259ad0.png

我们做了一个C++内存模型与Armv8-A实现之间的映射:

f4531328-efc5-11ed-90ce-dac502259ad0.png

四、总结

在白皮书中,为帮助读者更好地理解,我们选取了三个典型案例进行深入分析。由于与Synchronization相关的编程非常复杂,因此我们必须仔细权衡其正确性和性能。

我们建议首先使用较重的屏障指令保证逻辑的正确性,然后通过移除一些冗余屏障或在必要时切换到较轻的屏障来继续提高性能。对Arm内存模型和相关指令的深入理解,是对实现准确和高性能的Synchronization编程非常有必要的。

在附录部分,我们还介绍了内存模型工具(The litmus test suite),它可以帮助理解内存模型并在各种架构上验证程序。

关于以上内容更完整的讲解,请参考“Arm架构下的Synchronization概述和案例分析白皮书”。

审核编辑:汤梓红

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • ARM
    ARM
    +关注

    关注

    134

    文章

    8653

    浏览量

    361844
  • 内核
    +关注

    关注

    3

    文章

    1309

    浏览量

    39862
  • cpu
    cpu
    +关注

    关注

    68

    文章

    10449

    浏览量

    206579
  • 服务器
    +关注

    关注

    12

    文章

    8124

    浏览量

    82538
  • 编译器
    +关注

    关注

    1

    文章

    1577

    浏览量

    48626

原文标题:Arm架构下的Synchronization概述和案例分析白皮书|附下载

文章出处:【微信号:Ithingedu,微信公众号:安芯教育科技】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    ARM架构和X86架构终端特性对比

    我们就ARM架构的系统与X86架构系统的特性进行一个系统分析,方便用户在选择系统时进行理性、合理的比价分析
    发表于 11-30 11:19 5086次阅读

    ARM架构是什么

    从单片机转到ARM,主要需要学习ARM架构ARM相比单片机多了一些外设和总线。在仅仅是裸奔的情况,如果熟悉了
    发表于 07-01 09:23

    ARMARM的架构ARM架构的区别

    目录文章目录目录ARMARM 的架构x86 架构ARM 架构的区别ARM 的技术实现ARMARM
    发表于 07-16 06:43

    ARM芯片架构

    ARM芯片ARM 历史ARM架构 (Advanced RISC Machine)64/32位架构32位
    发表于 07-28 08:16

    ARM架构

    ARM架构ARM架构如图所示,ARM公司只提供内核技术,而其他外设则为芯片商设计并使用,ARM
    发表于 08-04 06:18

    介绍一ARM架构

    微型计算机(PC计算机)来说,就是降低中央处理器的频率和规格,降低存储器空间,这样的改变,使之广泛的运用在各种仪器等电子产品中当前,X86和ARM架构是公认的在商业化进程中表现最优秀的两大架构。之前
    发表于 11-25 08:51

    ARM在嵌入式开发中的概述

    是对ARM在嵌入式开发中的概述。一、ARM是什么?ARM是全球领先的半导体知识产权 (IP) 提供商。全世界超过95%的智能手机和平板电脑都采用AR
    发表于 12-14 08:34

    CMSIS软件架构概述

    目录CMSIS软件架构库文件说明CMSIS软件架构CMSIS概述     CMSIS软件架构由四层:用户应用层、操作系统及中间件接口层、CMSIS层和硬件层     由三部分构成核内外
    发表于 12-22 07:34

    介绍Armv8-A架构上的Synchronization相关知识

    1、Arm架构Synchronization概述和案例分析
    发表于 07-06 17:19

    ARM架构同步概述及案例分析

    本白皮书的目的是分享有关ARM架构的同步知识。 本文档的目标读者是从事ARM®架构同步工作的人员。 [警告]当我们处理锁定优化时,我们必须非常小心正确性。 同步导致的错误通常很难找出根
    发表于 08-21 07:51

    Essential synchronization tech

    Essential synchronization technologies in PXI:Many test and measurement applications
    发表于 07-23 22:43 9次下载

    什么叫arm架构_X86架构ARM架构有什么区别

    本文首先介绍了arm架构的概念,其次介绍了ARM架构图与ARM的技术实现,最后介绍了X86架构
    发表于 04-24 08:45 8.6w次阅读
    什么叫<b class='flag-5'>arm</b><b class='flag-5'>架构</b>_X86<b class='flag-5'>架构</b>与<b class='flag-5'>ARM</b><b class='flag-5'>架构</b>有什么区别

    ARM7TDMI 调试架构分析

    设计中,微处理器内核不能直接从芯片外围访问,这增加了调试系统的问题。本应用笔记描述了 ARM7TDMI 调试架构如何克服这个问题以及使用这种方法的优势。 ARM 调试架构——
    的头像 发表于 06-18 16:42 2175次阅读
    <b class='flag-5'>ARM</b>7TDMI 调试<b class='flag-5'>架构</b><b class='flag-5'>分析</b>

    Arm架构下的Synchronization概述和案例分析

    DMB和DSB是双向栅栏,对两个方向都限制,Armv8-a也设计了一种单向栅栏:load-acquire和store-release机制,只在一个方向上做限制。
    发表于 07-07 09:19 916次阅读

    Arm架构科普解读 Arm架构的底层逻辑和Arm架构的顶层设计

    本文主要探讨了 Arm 架构的底层逻辑,介绍了Arm 架构的顶层设计;以处理器核心架构为基础,以系统架构
    的头像 发表于 02-06 05:33 4989次阅读
    <b class='flag-5'>Arm</b><b class='flag-5'>架构</b>科普解读  <b class='flag-5'>Arm</b><b class='flag-5'>架构</b>的底层逻辑和<b class='flag-5'>Arm</b><b class='flag-5'>架构</b>的顶层设计