Hardware basics and java memory model

cache

  1. Now the processing power of the processor far exceeds the access rate of the main memory. The time required for a read or write operation of the main memory is enough for the processor to execute hundreds of instructions. In order to bridge the gap between the processing power of the processor and the main memory , a cache is introduced between the processor and main memory.

    Cache is a kind of storage unit whose read rate is much higher than that of main memory, but the capacity is much smaller than that of main memory. Each processor has its own cache.

  2. In the cache, it is equivalent to storing a copy for each variable of the access program, the variable name is equivalent to the memory address, and the variable value is equivalent to the data stored in the corresponding memory space, but the cache does not contain all the data at all times. A copy of the variable.

  3. The cache is equivalent to a hash table (zipper method) implemented in hardware. Its key is a memory address, and its value is a copy of the data or data to be written into memory. The structure of the cache is roughly as follows:

    The cache is a classic zipper hash table structure. The cache contains several buckets, and each bucket contains several cache entries, and a cache entry can be divided into: Tag Data-Blockand Flagthree parts, which Tagare used to distinguish the data in which On a data entry, it Flagis used to identify the state of the data entry. Data-BlockAlso called a data row, it is used to store data read from main memory or data to be written to main memory. A data row may contain the values ​​of several variables.

    When the processor is ready to read a piece of data, the processor "decodes" the corresponding memory address to obtain three values:

    • index - used to determine the bucket number

    • tag - used to identify the cache entry

    • offset - the offset in the entry used to determine where the data resides

    If the data is found in the cache according to the memory address, it is called a cache hit, otherwise the processor will go to the main memory to find the data.

  4. 现在处理器一般会具有多个层次的高速缓存,相应的是 一级缓存(L1 Cache) 二级缓存(L2 Cache) 三级缓存(L3 Cache)等,一般一级缓存会被集成在处理器内核中,因此其访问速度非常高,一般情况下以及缓存的读取会在2-4个处理器时钟循环内完成,其中一部分用于存储指令(L1i),一部分用于存储数据(L1d),一般越靠近处理器核心的高速缓存,存取速率越高,制造成本越高,容量越小.

    在linux系统可以使用lscpu命令查看缓存层次

缓存一致性协议

  1. MESI(Modified-Exclusive-Shared-Invalid)是一种广为使用的缓存一致性协议,X86处理器的缓存一致性协议就是基于MESI协议的,它对于访问的控制类似于读写锁,即对于同一地址的读操作时并发的,对于同一地址的写操作是独占的.

    MESI协议将缓存条目的状态分为四种: Modified Exclusive Shared Invalid

    • Invalid(无效的,记为I): 表示相应缓存行中不包含任何内存地址所对应的数据副本,该状态是缓存条目的初始状态.

    • Shared(共享的,记为S): 表示相应的缓存行包含相应的内存地址所对应的数据副本,并且,其它处理器上的高速缓存中也可能有相同的内存地址对应的数据副本.所以一个缓存条目的状态是Shared,并且,如果其它处理器上也存在与这个给处理器相同的tag的缓存条目,那么这些缓存条目的状态也为Shared.处于这个状态的缓存条目,其缓存行中包含的数据是与主内存中包含的数据是一致的.

    • Exclusive(独占的,记为E): 表示相应缓存行包含相应的内存地址对应的数据副本,并且,缓存行以独占的方式保留了内存地址所对应的数据副本,即,其它处理器中不存在这个内存地址所对应的数据副本,处于这个状态的缓存条目,其缓存行中包含的数据与主内存中包含的数据是一致的.

    • Modified(更改过的,记为M): 表示相应缓存行包含相应的内存地址对应的更新结果,并且,由于MESI协议中规定任意时刻只能有一个处理器对同一个内存地址的数据做更改,所以其它处理器不存在这个内存地址所对应的数据副本,处于这个给状态的缓存条目,其缓存行中包含的数据是与主内存中包含的数据是不一致的.

    MESI协议在这四种状态的基础上定义一组消息用于协调各个处理器之间的读写操作,比照HTTP协议,我们可以将MESI中的消息分为:请求消息 - 相应消息,处理器在执行内存的读写操作时会向总线发送相应的消息,其它处理器会拦截总线中的消息并在一定条件下往总线中回复相应的响应消息.

    消息名 消息类型 描述
    Read 请求 通知其它处理器、主内存当前处理器准备读取某个数据.该消息中包含准备读取消息的内存地址.
    Read Response 响应 该消息包含被请求读取的数据,可能来自于主内存也可能来自于其它拦截到读取消息的其他处理器.
    Invalidate 请求 通知其它处理器将各自高速缓存中的对应内存地址的缓存条目的状态置为I,即,通知这些处理器删除指定内存地址所对应的数据副本.
    Invalidate Acknowledge 响应 接收到Invalidate消息的处理器必须回复该消息,以表示它在高速缓存中删除了对应的数据副本.
    Read Invalidate 请求 该消息是由Read消息和Invalidate消息组成的复合消息,用于通知其它处理器,当前处理器准备更新一个数据(Read-Modify-Write),并请求器它处理器删除指定内存地址的对应的数据副本,收到这个消息的的处理器必须回复 Read Response消息和Invalidate Acknowledge消息
    WriteBack 请求 该消息包含需要羞辱内存的数据信息和对应的内存地址
  2. MESI读操作流程

    假设内存地址A上的数据DATA是处理器-1和处理器-2可能共享的数据。

    下面讨论在处理器-1上读取数据DATA的实现。处理器-1会根据地址A找到对应的缓存条目,并读取该缓存条目的Tag和Flag值(缓存条目状态)。为讨论方便,这里我们不讨论Tag值的匹配问题。处理器-1找到的缓存条目的状态如果为M、E或者S,那么该处理器可以直接从相应的缓存行中读取地址A所对应的数据,而无须往总线中发送任何消息。处理器-1找到的缓存条目的状态如果为I,则说明该处理器的高速缓存中并不包含DATA的有效副本数据,此时处理器-1需要往总线发送Read消息以读取地址A对应的数据,而其他处理器处理器-2(或者主内存)则需要回复Read Response以提供相应的数据。

    处理器-1接收到Read Response消息时,会将其中携带的数据(包含数据DATA的数据块)存入相应的缓存行并将相应缓存条目的状态更新为S。处理器-1接收到的Read Response消息可能来自主内存也可能来自其他处理器(处理器-2)。处理器-2会嗅探总线中由其他处理器发送的消息。处理器-2嗅探到Read消息的时候,会从该消息中取出待读取的内存地址,并根据该地址在其高速缓存中查找对应的缓存条目。如果处理器-2找到的缓存条目的状态不为I,则说明该处理器的高速缓存中有待读取数据的副本,此时处理器-2会构造相应的Read Response消息并将相应缓存行所存储的整块数据(而不仅仅是处理器-1所请求的数据DATA)“塞入”该消息。如果处理器-2找到的相应缓存条目的状态为M,那么处理器-2可能在往总线发送Read Response消息前将相应缓存行中的数据写入主内存。处理器-2往总线发送Read Response之后,相应缓存条目的状态会被更新为S。如果处理器-2找到的高速缓存条目的状态为I,那么处理器-1所接收到的Read Response消息就来自主内存。可见,在处理器-1读取内存的时候,即便处理器-2对相应的内存数据进行了更新且这种更新还停留在处理器-2的高速缓存中而造成高速缓存与主内存中的数据不一致,在MESI消息的协调下这种不一致也并不会导致处理器-1读取到一个过时的旧值。

  3. MESI写操作流程

    任何一个处理器执行内存写操作时必须拥有相应数据的所有权。在执行内存写操作时,处理器-1会先根据内存地址A找到相应的缓存条目。处理器-1所找到的缓存条目的状态若为E或者M,则说明该处理器已经拥有相应数据的所有权,此时该处理器可以直接将数据写入相应的缓存行并将相应缓存条目的状态更新为M。处理器-1所找到的缓存条目的状态如果不为E、M,则该处理器需要往总线发送Invalidate消息以获得数据的所有权。其他处理器接收到Invalidate消息后会将其高速缓存中相应的缓存条目状态更新为I(相当于删除相应的副本数据)并回复Invalidate Acknowledge消息。发送Invalidate消息的处理器(即内存写操作的执行处理器),必须在接收到其他所有处理器所回复的所有Invalidate Acknowledge消息之后再将数据更新到相应的缓存行之中。

    处理器-1所找到的缓存条目的状态若为S,则说明处理器-2上的高速缓存可能也保留了地址A对应的数据副本,此时处理器-1需要往总线发送Invalidate消息。处理器-1在接收到其他所有处理器所回复的Invalidate Acknowledge消息之后会将相应的缓存条目的状态更新为E,此时处理器-1获得了地址A上数据的所有权。接着,处理器-1便可以将数据写入相应的缓存行,并将相应的缓存条目的状态更新为M。处理器-1所找到的缓存条目的状态若为I,则表示该处理器不包含地址A对应的有效副本数据,此时处理器-1需要往总线发送Read Invalidate消息。处理器-1在接收到Read Response消息以及其他所有处理器所回复的Invalidate Acknowledge消息之后,会将相应缓存条目的状态更新为E,这表示该处理器已经获得相应数据的所有权。接着,处理器-1便可以往相应的缓存行中写入数据了并将相应缓存条目的状态更新为M。其他处理器在接收到Invalidate消息或者Read Invalidate消息之后,必须根据消息中包含的内存地址在该处理器的高速缓存中查找相应的高速缓存条目。若处理器-2所找到的高速缓存条目的状态不为I,那么处理器-2必须将相应缓存条目的状态更新为I,以删除相应的副本数据并给总线回复Invalidate Acknowledge消息。可见,Invalidate消息和Invalidate Acknowledge消息使得针对同一个内存地址的写操作在任意一个时刻只能由一个处理器执行,从而避免了多个处理器同时更新同一数据可能导致的数据不一致问题。

写缓冲器和无效化队列

  1. MESI协议解决了缓存一致性问题,但是,当一个处理器在进行写操作时,必须等待其它处理器将对应的数据副本删除后并回复Invalidate Acknowledge/Read Response消息才能将数据写入高速缓存,为了解决性能问题,便引入了写缓冲器和无效化队列.

  2. 写缓冲器时处理器内部一个比高速缓存容量还小的私有存储部件,每个处理器都有自己的写缓冲器,写缓冲器内部可能包含多个条目,一个处理器不能读取另一个处理器的写缓冲器.

  3. 当处理器在进行写操作时,如果对应的缓存条目为I(或者S),处理器会先将写操作相关的数据(包含数据和待操作的内存地址)存入写缓冲器的相关条目当中,并发送Read Invalidate(Invalidate)消息,若其它所有处理器对应的条目状态全部也为I,那么,就发生了所谓的"写未命中",即Read请求会进行主内存读操作,这样是开销比较大的,所以处理器在收到主内存返回的相应内存地址对应的数据,将其写入写缓冲器后,就不再等待其它处理器回复Read Response /Invalidate Acknowledge消息而是继续执行其它指令,等到所有处理器的Read Response /Invalidate Acknowledge消息返回成功,那么写缓冲区再将数据写入高速缓存.

  4. 处理器在收到Invalidate消息后并不删除指定缓存条目中的数据,而是先将消息存入到无效化队列,然后回复Invalidate Acknowledge消息,以减少处理器的等待时间,然后再从无效化队列中读取消息删除相关数据,某些处理器并不拥有无效化队列(比如X86处理器)

存储转发

处理器在进行读操作的时候,由于对应内存地址的变量可能刚写完,还没有从写缓冲器同步到高速缓存中去,所以在处理读的时候,会先去写缓冲器中读取是否存在相应的条目,如果没有就去高速缓存中读取,但是一个处理器并不能读取其它处理器的写缓冲区.

内存重排序

写缓冲器和无效化队列都可能导致内存重排序.

  1. 写缓冲器可能导致StoreLoad重排序

    Processor 0 Processor 1
    X=1; //S1 Y=1; //S3
    r1=Y; //L2
    r2=X; //L4

    如上表X、Y均为共享变量其初始值均为0,r1、r2为局部变量

    当Processor 0执行到L2的时候,如果S3的操作的结果还处于写缓冲器之中,那么L2读取到Y的值还是初始值0,同样当Processor 1执行L4的时候S1的操作结果也还处于写缓冲器之中,那么r2读取到X的值也为初始值 0,对于此时的Processor 1看来S1是没有发生的,即Processor的执行顺序为L2→S1这就是所谓的StoreLoad重排序.

  2. 写缓冲器可能导致StoreStore重排序

    Processor 0 Processor 1
    data=1; //S1
    ready=true; //S2
    while(! ready) continue; //L3
    print(data); //L4

    如上表data和ready是共享变量,初始值为0和false,在执行之前它们在Processor 0处理器所对应的缓存条目状态分别为S(或者I)和E,在Processor 1上对应的缓存条目为S和I

    1. 当执行到S1时,Processor 0会先把S1的操作结果存到写缓冲器中,然后向其它处理器发出Invalidate请求....

    2. ready由于时Processor 0独享的数据,所以S2的结果被直接存入到了高速缓存当中

    3. L3通过缓存一致性协议成功读取到了ready的新值true

    4. L4由于S1的操作结果还没有从写缓冲器同步至高速缓存,所以读取的的data值还是一个旧值为0

    就Processor 1感知到的顺序在Processor 0中ready的值已经改变,data还是初始值,即S2→S1,这就是StoreStore重排序.

  3. 无效化队列造成LoadLoad重排序

    Processor 0 Processor 1
    data=1; //S1
    ready=true; //S2
    while(! ready) continue; //L3
    print(data); //L4

    如上表data和ready是共享变量,初始值为0和false,在执行之前它们在Processor 0处理器所对应的缓存条目状态分别为S和E,在Processor 1上对应的缓存条目为S和I

    1. S1是对data变量做修改,Processor 0会先将S1的操作结果放入写缓冲器,然后向总线发送携有data内存地址信息的Invalidate请求,Processor 1拦截到这个请求后,回复一个Invalidate Acknowledge消息,然后,把请求放入无效化队列

    2. S2中ready是Processor 0独占的,所以Processor 0直接将ready修改为true存到高速缓存中

    3. L3通过缓存一致性协议从Processor 0中同步了ready的值,while条件为false进入L4

    4. L4中可能出现这种情况,由于1中的Invalidate请求还在Processor 1的无效化队列当中,此时L4还能直接从其高速缓存中读取data的值,但是此时data=0,还是初始值

    就Processor 0感知到Processor 1中的L4读取的data是一个旧值,即执行顺序为L4→L3,这就是LoadLoad重排序

可见性问题

可见性问题是由写缓冲器和无效化队列造成的,而解决可见性问题的方法就是在共享变量的读写时加入内存屏障.

存储屏障 → 会将写缓冲器中的内容冲刷到高速缓存,以免最新的数据不能够被其它处理器读到.

加载屏障 → 会将无效化队列中的请求执行,以免所在处理器读到的是一个旧值.

基本内存屏障

  1. 处理器支持哪种内存重排序(LoadLoad重排序,LoadStore重排序,StoreLoad重排序,StoreStore重排序)就会提供能够禁止相应重排序的指令,这种指令被称作内存屏障(LoadLoad屏障,LoadStore屏障,StoreLoad屏障,StoreStore屏障).

  2. 屏障的具体作用是禁止指令之间的重排序,例如LoadStore屏障就是禁止这个屏障之前的读指令与这个屏障之后的写指令重排序.

  3. LoadLoad屏障 → 解决LoadLoad重排序(上文提到过是由无效化队列造成的)

    所以LoadLoad屏障的实现原理就是通过清空无效化队列中的Invalidate消息来删除高速缓存中的无效副本来保证不发生重排序的.

    加载屏障的实现方式

  4. StoreStore屏障 → 解决StoreStore重排序

    通过对写缓冲器中的条目进行标记来实现的,通过来判断条目的提交顺序,如果处理器在进行写操作的过程中发现写缓冲器中的条目存在标记现象,那么即使这个写操作对应的高速缓存中的数据的条目状态为E或M,也会将写的数据存入写缓冲器而不是高速缓存.这样就会保障屏障之前的写操作肯定会在屏障之后的操作前面提交至高速缓存

  5. StoreLoad屏障 → 很多处理器的基本屏障

    StoreLoad屏障能够代替其它任何屏障的作用,它的主要操作是冲刷写缓冲器缓存 + 清空无效化队列,所以StoreLoad的屏障的开销也是最大的.

同步机制和内存屏障

  1. 获取屏障(Acquire Barrier)&释放屏障(Release Barrier)两种屏障是由基础屏障组成的复合屏障

    获取屏障 →LoadLoad屏障和LoadStore屏障组合而成,它能阻止屏障前的任何读操作与屏障后的读写操作发生重排序.

    释放屏障→LoadStore屏障和StoreStore屏障组合而成,它能阻止屏障后的任何写操作和屏障前的读写操作发生重排序,

  2. volatile与存储屏障

    volatile关键字写操作的屏障使用方式

    volatile关键字读操作的屏障使用方式

    然后实际情况在X86处理器(常用的pc机,英特尔处理器 & amd处理器→其新推出锐龙的zen架构其实也属于X86)下只支持StoreLoad重排序(LoadLoad这种重排序根本不会发生),所以在X86处理器中,LoadLoad屏障、LoadStore屏障、StoreStore屏障都是空指令.

  3. Synchronized-related storage barriers: the same as volatile, they are basically composed of several basic barriers, but synchronized modifies code blocks, so it does not distinguish between read and write conditions, and requires more barriers, so the overhead is greater, but in the end Only StoreLoad barriers exist in X86 processors.

    The virtual machine inserts a load barrier before the start of the critical section after the MonitorEnter instruction to ensure that updates to shared variables by other threads can be synchronized to the cache of the processor where the thread is located. At the same time, a storage barrier is also inserted after the MonitorExit instruction , to ensure that changes to shared variables in the code of the critical section can be synchronized in time.

    The virtual machine inserts an acquire barrier after the MonitorEnter instruction and a release barrier before the MonitorExit instruction.

  4. final memory barrier rules

    The final reordering rules require the compiler to insert a StoreStore barrier after the final field is written and before the constructor returns.

    The reordering rules for reading final fields require the compiler to insert a LoadLoad barrier before operations that read final fields.

    Since x86 processors do not reorder write-to-write operations, the StoreStore barrier required to write final fields is omitted on x86 processors. Also, since x86 processors do not reorder operations that have indirect dependencies, the LoadLoad barrier required to read final fields is also omitted on x86 processors. That is to say, on x86 processors, reads and writes of final fields do not insert any memory barriers!

Virtual machine optimization of memory barriers

These optimizations include omission, merging, etc. For example, for two consecutive volatile write operations, the virtual machine will only add a StoreLoad barrier after the last write operation. The implementation of each MonitorExit by the X86 processor has the effect of StoreLoad, so There is no need to add a StoreLoad barrier behind it.

Guess you like

Origin juejin.im/post/7086301569032912932