What is the CPU cache

First, what is the CPU cache

1. CPU cache of origin

As we all know, CPU is the brains of the computer, which is responsible for execution of the program instructions, and responsible for the data stored in memory, including the program's own data. Many years ago, the frequency of the CPU and memory bus frequency on the same level. Only memory access speed slower than the register. However, the situation in the 1990s was broken. Greatly enhance the frequency of the CPU, but the performance of the memory bus and the memory chip frequency has not been improved in proportion. Not because of not making faster memory, just because too expensive. If you want to reach the current CPU memory that speed, then its cost is probably more expensive Shanghao several orders of magnitude. Therefore, the operation speed than the memory read and write speeds much faster CPU, so CPU will take a long time to wait for the arrival of data or to write data into memory. So, in order to solve the CPU speed and memory access speed does not match the contradictions , appeared CPU cache.

2. CPU cache concept

CPU cache memory located between the CPU and temporary data switch, which is smaller than the memory capacity but more memory much faster than the switching speed. Usually CPU cache directly integrated on separate chips or motherboard bus interconnected with the CPU chip .

To simplify the communication with the memory, a cache controller for the data block, rather than byte operations. It is actually a set of cache called a cache line of a fixed size (Cache Line) data blocks, the line is typically 64bytes.

3. CPU cache significance

CPU repeats the processing often requires the same data, the same instruction is repeatedly executed, if this part of the data, CPU instructions can be found in the CPU cache, the CPU does not need to re-read data, instructions from a memory or a hard disk, thereby reducing the overall response time. Therefore, the cache satisfies the following two meanings locality principle :

  • Temporal locality (Temporal Locality) : If an item is being accessed, then it is likely to be accessed again in the near future.
  • Spatial locality (Spatial Locality) : If a memory location is referenced, then his position will be referenced in the vicinity of the future.

Two, three CPU cache

1. CPU of level three cache

With the development of multi-core CPU, CPU cache is typically divided into three levels: L1, L2, L3. The smaller the closer the level of CPU, so the speed is faster, but also represents the smaller capacity. L1 is the closest to the CPU, which is the minimum capacity (for example: 32K), fastest, each nucleus has a L1 cache, there are actually two L1 cache on each core L1 cache, a data memory for L1d Cache (Data Cache), for storing an instruction L1i Cache (instruction Cache). Larger L2 cache (e.g.: 256K), slower speed, the core has a separate L2 cache each general; L3 cache buffer is three largest one (e.g. 3MB), but also the slowest level, the same nuclear shared between a CPU socket L3 cache.

The following are three cache processing speed reference table:

From the CPU to It takes about CPU cycles It takes about a time (in ns)
register 1 cycle  
L1 Cache ~3-4 cycles ~0.5-1 ns
L2 Cache ~10-20 cycles ~3-7 ns
L3 Cache ~40-45 cycles ~15 ns
Cross-slot transmission   ~20 ns
RAM ~120-240 cycles ~60-120ns

The figure is a schematic view of three cache of CPU Intel Core i5-4285U:

 

CPU L3 cache
CPU L3 cache

 

Like database cache, the data will first look for the fastest time to get data cache, if the cache is not hit (Cache miss) then down to find one, until the three-level cache can not find that to be the only memory the data. Miss time and time again, the representative data consumer taking longer time.

2. The calculation process executed with the CPU cache

  1. Programs and data are loaded into the main memory
  2. The instructions and data are loaded into the CPU cache
  3. CPU instruction execution, the results written to cache
  4. Data cache write-back to main memory

目前流行的多级缓存结构如下图:

 

Multi-level cache architecture
多级缓存结构

 

三、CPU缓存一致性协议(MESI)

MESIModified Exclusive Shared Or Invalid)(也称为伊利诺斯协议,是因为该协议由伊利诺斯州立大学提出的)是一种广泛使用的支持写回策略的缓存一致性协议。为了保证多个CPU缓存中共享数据的一致性,定义了缓存行(Cache Line)的四种状态,而CPU对缓存行的四种操作可能会产生不一致的状态,因此缓存控制器监听到本地操作和远程操作的时候,需要对地址一致的缓存行的状态进行一致性修改,从而保证数据在多个缓存之间保持一致性。

1. MESI协议中的状态

CPU中每个缓存行(Caceh line)使用4种状态进行标记,使用2bit来表示:

状态 描述 监听任务 状态转换
M 修改 (Modified) 该Cache line有效,数据被修改了,和内存中的数据不一致,数据只存在于本Cache中。 缓存行必须时刻监听所有试图读该缓存行相对就主存的操作,这种操作必须在缓存将该缓存行写回主存并将状态变成S(共享)状态之前被延迟执行。 当被写回主存之后,该缓存行的状态会变成独享(exclusive)状态。
E 独享、互斥 (Exclusive) 该Cache line有效,数据和内存中的数据一致,数据只存在于本Cache中。 缓存行也必须监听其它缓存读主存中该缓存行的操作,一旦有这种操作,该缓存行需要变成S(共享)状态。 当CPU修改该缓存行中内容时,该状态可以变成Modified状态
S 共享 (Shared) 该Cache line有效,数据和内存中的数据一致,数据存在于很多Cache中。 缓存行也必须监听其它缓存使该缓存行无效或者独享该缓存行的请求,并将该缓存行变成无效(Invalid)。 当有一个CPU修改该缓存行时,其它CPU中该缓存行可以被作废(变成无效状态 Invalid)。
I 无效 (Invalid) 该Cache line无效。

注意
对于M和E状态而言总是精确的,他们在和该缓存行的真正状态是一致的,而S状态可能是非一致的。如果一个缓存将处于S状态的缓存行作废了,而另一个缓存实际上可能已经独享了该缓存行,但是该缓存却不会将该缓存行升迁为E状态,这是因为其它缓存不会广播他们作废掉该缓存行的通知,同样由于缓存并没有保存该缓存行的copy的数量,因此(即使有这种通知)也没有办法确定自己是否已经独享了该缓存行。

从上面的意义看来E状态是一种投机性的优化:如果一个CPU想修改一个处于S状态的缓存行,总线事务需要将所有该缓存行的copy变成invalid状态,而修改E状态的缓存不需要使用总线事务。

MESI状态转换图:

 

MESI state transition diagram
MESI状态转换图

 

下图表示了当一个缓存行(Cache line)的调整的状态的时候,另外一个缓存行(Cache line)需要调整的状态。

状态 M E S I
M × × ×
E × × ×
S × ×
I

举个示例:

假设cache 1 中有一个变量x = 0的 Cache line 处于S状态(共享)。
那么其他拥有x变量的 cache 2、cache 3 等x的 Cache line调整为S状态(共享)或者调整为I状态(无效)。

2. 多核缓存协同操作

(1) 内存变量

假设有三个CPU A、B、C,对应三个缓存分别是cache a、b、c。在主内存中定义了x的引用值为0。

 

Memory variable
内存变量

 

(2) 单核读取

执行流程是:

  • CPU A发出了一条指令,从主内存中读取x
  • 从主内存通过 bus 读取到 CPU A 的缓存中(远端读取 Remote read),这时该 Cache line 修改为 E 状态(独享)。

 

Mononuclear read
单核读取

 

(3) 双核读取

执行流程是:

  • CPU A发出了一条指令,从主内存中读取x
  • CPU A从主内存通过bus读取到 cache a 中并将该 Cache line 设置为E状态。
  • CPU B发出了一条指令,从主内存中读取x
  • CPU B试图从主内存中读取x时,CPU A检测到了地址冲突。这时CPU A对相关数据做出响应。此时x存储于 cache a 和 cache b 中,x在 chche a 和 cache b 中都被设置为S状态(共享)。

 

Dual-core reading
双核读取

 

(4) 修改数据

执行流程是:

  • CPU A 计算完成后发指令需要修改x.
  • CPU A 将x设置为M状态(修改)并通知缓存了x的 CPU B, CPU B 将本地 cache b 中的x设置为I状态(无效)
  • CPU A 对x进行赋值。

 

change the data
修改数据

 

(5) 同步数据

那么执行流程是:

  • CPU B 发出了要读取x的指令。
  • CPU B 通知CPU A,CPU A将修改后的数据同步到主内存时cache a 修改为E(独享)
  • CPU A同步CPU B的x,将cache a和同步后cache b中的x设置为S状态(共享)。

 

Synchronous Data
同步数据

 

3. CPU 存储模型简介

MESI协议为了保证多个 CPU cache 中共享数据的一致性,定义了 Cache line 的四种状态,而 CPU 对 cache 的4种操作可能会产生不一致状态,因此 cache 控制器监听到本地操作和远程操作的时候,需要对地址一致的 Cache line 状态做出一定的修改,从而保证数据在多个cache之间流转的一致性。

但是,缓存的一致性消息传递是要时间的,这就使得状态切换会有更多的延迟。某些状态的切换需要特殊的处理,可能会阻塞处理器。这些都将会导致各种各样的稳定性和性能问题。比如你需要修改本地缓存中的一条信息,那么你必须将I(无效)状态通知到其他拥有该缓存数据的CPU缓存中,并且等待确认。等待确认的过程会阻塞处理器,这会降低处理器的性能。因为这个等待远远比一个指令的执行时间长的多。所以,为了为了避免这种阻塞导致时间的浪费,引入了存储缓存(Store Buffer)和无效队列(Invalidate Queue)。

(1) 存储缓存

在没有存储缓存时,CPU 要写入一个量,有以下情况:

  • 量不在该 CPU 缓存中,则需要发送 Read Invalidate 信号,再等待此信号返回,之后再写入量到缓存中。
  • 量在该 CPU 缓存中,如果该量的状态是 Exclusive 则直接更改。而如果是 Shared 则需要发送 Invalidate 消息让其它 CPU 感知到这一更改后再更改。

这些情况中,很有可能会触发该 CPU 与其它 CPU 进行通讯,接着需要等待它们回复。这会浪费大量的时钟周期!为了提高效率,可以使用异步的方式去处理:先将值写入到一个 Buffer 中,再发送通讯的信号,等到信号被响应,再应用到 cache 中。并且此 Buffer 能够接受该 CPU 读值。这个 Buffer 就是 Store Buffer。而不须要等待对某个量的赋值指令的完成才继续执行下一条指令,直接去 Store Buffer 中读该量的值,这种优化叫Store Forwarding

(2) 无效队列

同理,解决了主动发送信号端的效率问题,那么,接受端 CPU 接受到 Invalidate 信号后如果立即采取相应行动(去其它 CPU 同步值),再返回响应信号,则时钟周期也太长了,此处也可优化。接受端 CPU 接受到信号后不是立即采取行动,而是将 Invalidate 信号插入到一个队列 Queue 中,立即作出响应。等到合适的时机,再去处理这个 Queue 中的 Invalidate 信号,并作相应处理。这个 Queue 就是Invalidate Queue

四、乱序执行

乱序执行(out-of-orderexecution:是指CPU允许将多条指令不按程序规定的顺序分开发送给各相应电路单元处理的技术。这样将根据各电路单元的状态和各指令能否提前执行的具体情况分析后,将能提前执行的指令立即发送给相应电路。

这好比请A、B、C三个名人为晚会题写横幅“春节联欢晚会”六个大字,每人各写两个字。如果这时在一张大纸上按顺序由A写好”春节”后再交给B写”联欢”,然后再由C写”晚会”,那么这样在A写的时候,B和C必须等待,而在B写的时候C仍然要等待而A已经没事了。

但如果采用三个人分别用三张纸同时写的做法, 那么B和C都不必须等待就可以同时各写各的了,甚至C和B还可以比A先写好也没关系(就象乱序执行),但当他们都写完后就必须重新在横幅上(自然可以由别人做,就象CPU中乱序执行后的重新排列单元)按”春节联欢晚会”的顺序排好才能挂出去。

所以,CPU 为什么会有乱序执行优化?本质原因是CPU为了效率,将长费时的操作“异步”执行,排在后面的指令不等前面的指令执行完毕就开始执行后面的指令。而且允许排在前面的长费时指令后于排在后面的指令执行完。

CPU 执行乱序主要有以下几种:

  • 写写乱序(store store)a=1;b=2; -> b=2;a=1;
  • Write Read out of order (the Load Store) :a=1;load(b); -> load(b);a=1;
  • Read scrambled (Load Load) :load(a);load(b); -> load(b);load(a);
  • Read scrambled (Load Store) :load(a);b=2; -> b=2;load(a);

In summary, out of order execution of the CPU processors optimization has been made to improve the computing speed optimized original order of the code violation .


Reference article:

Guess you like

Origin www.cnblogs.com/xuanbjut/p/11608991.html