MESI
States
Modified (M)
The cache line is present only in the current cache, and is dirty - it has been modified (M state) from the value in main memory. The cache is required to write the data back to main memory at some time in the future, before permitting any other read of the (no longer valid) main memory state. The write-back changes the line to the Shared state(S).
Exclusive (E)
The cache line is present only in the current cache, but is clean - it matches main memory. It may be changed to the Shared state at any time, in response to a read request. Alternatively, it may be changed to the Modified state when writing to it.
Shared (S)
Indicates that this cache line may be stored in other caches of the machine and is clean - it matches the main memory. The line may be discarded (changed to the Invalid state) at any time.
注: "may be stored in other cahches" 表示该cache line为shared时,可能仅被一个master缓存。
Invalid (I)
Indicates that this cache line is invalid (unused).
Operation
The MESI protocol is defined by a finite-state machine that transitions from one state to another based on 2 stimuli.
The first stimulus is the processor specific Read and Write request. For example: A processor P1 has a Block X in its Cache, and there is a request from the processor to read or write from that block.
The second stimulus is given through the bus connecting the processors. In particular the "Bus side requests" come from other processors that don't have the cache block or the updated data in their Cache. The bus requests are monitored with the help of Snoopers,[4] which monitor all the bus transactions.
Following are the different type of Processor requests and Bus side requests:
Processor Requests to Cache include the following operations:
- PrRd: The processor requests to read a Cache block.
- PrWr: The processor requests to write a Cache block
Bus side requests are the following:
- BusRd: Snooped request that indicates there is a read request to a Cache block requested by another processor
- BusRdX: Snooped request that indicates there is a write request to a Cache block requested by another processor that doesn't already have the block. (注:snoop当前processor,获取并invalid该cache line copy)
- BusUpgr: Snooped request that indicates that there is a write request to a Cache block requested by another processor that already has that cache block residing in its own cache. (注:snooop当前processor,invalid该cache line copy)
- Flush: Snooped request that indicates that an entire cache block is written back to the main memory by another processor.
- FlushOpt: Snooped request that indicates that an entire cache block is posted on the bus in order to supply it to another processor (Cache to Cache transfers).
(Such Cache to Cache transfers can reduce the read miss latency if the latency to bring the block from the main memory is more than from Cache to Cache transfers, which is generally the case in bus based systems.)
Snooping Operation: In a snooping system, all caches on a bus monitor all the transactions on that bus. Every cache has a copy of the sharing status of every block of physical memory it has stored. The state of the block is changed according to the State Diagram of the protocol used. (Refer image above for MESI state diagram). The bus has snoopers on both sides:
- Snooper towards the Processor/Cache side.
- The snooping function on the memory side is done by the Memory controller.
- 注:总线上snooper通常只存在一处
Initial State | Operation | Response |
---|---|---|
Invalid(I) | PrRd |
|
PrWr |
| |
Exclusive(E) | PrRd |
|
PrWr |
| |
Shared(S) | PrRd |
|
PrWr |
| |
Modified(M) | PrRd |
|
PrWr |
|
Initial State | Operation | Response |
---|---|---|
Invalid(I) | BusRd |
|
BusRdX/BusUpgr |
| |
Exclusive(E) | BusRd |
|
BusRdX |
| |
Shared(S) | BusRd |
|
BusRdX/BusUpgr |
| |
Modified(M) | BusRd |
|
BusRdX |
|
1,The Modified and Exclusive states are always precise: i.e. they match the true cache line ownership situation in the system. The Shared state may be imprecise: if another cache discards a Shared line, this cache may become the sole owner of that cache line, but it will not be promoted to Exclusive state.
2,A write may only be performed freely if the cache line is in the Modified or Exclusive state. If it is in the Shared state, all other cached copies must be invalidated first. This is typically done by a broadcast operation known as Request For Ownership (RFO).
3,两个shared状态的cache,同时对同一cacheline发起BusUpgr;或两个invalid状态的cache,同时对同一cacheline发起BusRdX;详细处理过程? 应该是交由interconnect仲裁处理!interconnect如何响应被拒绝的chache(cpu)?
4,MESI协议状态转换在线网站: VivioJS MESI help (tcd.ie)
MOESI
In computing, MOESI is a full cache coherency protocol that encompasses all of the possible states commonly used in other protocols. In addition to the four common MESI protocol states, there is a fifth "Owned" state representing data that is both modified and shared. This avoids the need to write modified data back to main memory before sharing it. While the data must still be written back eventually, the write-back may be deferred.
In order for this to be possible, direct cache-to-cache transfers of data must be possible, so a cache with the data in the modified state can supply that data to another reader without transferring it to memory.
(注:MESI也可以实现不回写至memory,直接交由另一cache操作后,回写至memory;区别在于当前cache交出去后,需invalid snooped master)
Modified
This cache has the only valid copy of the cache line, and has made changes to that copy. The cached copy may be further modified freely.
Owned
This line is one of several copies in the system. This cache does not have permission to modify the copy but the line is modified (dirty) relative to main memory, and this cache has the exclusive responsibility for ensuring main memory is eventually updated. The cache line may be changed to the Modified state after invalidating all shared copies, or changed to the Shared state by writing the modifications back to main memory. Owned cache lines must respond to a snoop request with data, to ensure the stale copy in main memory is not used.
注:Owned应为Modified状态的cache line copy,被其他cache读取后,转换得到的状态。
Exclusive
This cache has the only copy of the line, but the line is clean (unmodified). It may be written to, changing to the Modified state.
Shared
This line is one of several copies in the system. This cache does not have permission to modify the copy. Unlike the MESI protocol, a shared cache line may be dirty with respect to memory; if it is, one cache has a copy in the Owned state, and that cache is responsible for eventually updating main memory. If no cache holds the line in the Owned state, the memory copy is up to date. The cache line may not be written, but must be changed to the Exclusive or Modified state first, by invalidating all other cached copies. (If the cache line was Owned before, the invalidate response will indicate this, and the state will become Modified, so the obligation to eventually write the data back to memory is not forgotten.) It may also be discarded (changed to the Invalid state) at any time.
注:shared状态的cache line与owner状态的cache line一致,clean或dirty。
Invalid
This block is not valid; it must be fetched to satisfy any attempted access.
This protocol, a more elaborate version of the simpler MESI protocol, avoids the need to write a dirty cache line back to main memory when another processor tries to read it. Instead, the Owned state allows a processor to supply the modified data directly to the other processor. This is beneficial when the communication between two CPUs is significantly better than to main memory. An example would be multi-core CPUs with per-core L2 caches.
While MOESI can quickly share dirty cache lines from cache, it may struggle to quickly share clean lines from cache. If a cache line is clean with respect to memory and in the shared state, then there is no obvious single candidate cache to respond to a read request, so it is normal to let the read request be filled from memory. (This is solved by the MESIF protocol, which may be combined with MOESI to make MOESIF.)
注:系统当前只有shared状态的cache line,没有owner,无法确定由谁来响应读数据。不合理,MESI都能解决,MOESI也不应存在该问题。且即使如此总线也能解决!
If a processor wishes to write to an Owned cache line, it must notify the other processors which are sharing that cache line. The standard implementation simply tells them to invalidate their copies, moving its own copy to the Modified state when this is complete, but alternatively it may use a write-through policy, telling them to update their copies with the new contents. This is a partial write-through which does not go as far as main memory; the processor's own copy remains in the Owned state.
ARMv8-缓存一致性(cache coherency)解决方案:MOESI protocol_moesi协议-CSDN博客
上述链接所示的MOESI状态图,是不完整的实现方案;不含 Shared -> Owned、不含Owned -> Shared 两条转换路径。