The MESI protocol is an invalidate-based cache coherence protocol, and is one of the most common protocols that support write-back caches. It is also known as the Illinois protocol due to its development at the University of Illinois at Urbana-Champaign. Write back caches can save considerable bandwidth generally wasted on a write through cache. There is always a dirty state present in write-back caches that indicates that the data in the cache is different from that in the main memory. The Illinois Protocol requires a cache-to-cache transfer on a miss if the block resides in another cache. This protocol reduces the number of main memory transactions with respect to the MSI protocol. This marks a significant improvement in performance.
States The letters in the acronym MESI represent four exclusive states that a cache line can be marked with (encoded using two additional bits):
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 the 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. Invalid (I) Indicates that this cache line is invalid (unused).
For any given pair of caches, the permitted states of a given cache line are as follows: M E S I M E S I When the block is marked M (modified) or E (exclusive), the copies of the block in other Caches are marked as I (Invalid).
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, which monitor all the bus transactions.
Following are the different types 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. 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. 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.
Explanation:
Each Cache block has its own 4 state finite-state machine (refer image 1.1). The State transitions and the responses at a particular state with respect to different inputs are shown in Table1.1 and Table 1.2 +Table 1.1 State Transitions and response to various Processor OperationsInitial StateOperationResponseInvalid(I)PrRd Issue BusRd to the bus other Caches see BusRd and check if they have a valid copy, inform sending cache State transition to (S)Shared, if other Caches have valid copy. State transition to (E)Exclusive, if none (must ensure all others have reported). If other Caches have copy, one of them sends value, else fetch from Main MemoryPrWr Issue BusRdX signal on the bus State transition to (M)Modified in the requestor Cache. If other Caches have copy, they send value, otherwise fetch from Main Memory If other Caches have copy, they see BusRdX signal and invalidate their copies. Write into Cache block modifies the value.Exclusive(E)PrRd No bus transactions generated State remains the same. Read to the block is a Cache HitPrWr No bus transaction generated State transition from Exclusive to (M)Modified Write to the block is a Cache HitShared(S)PrRd No bus transactions generated State remains the same. Read to the block is a Cache Hit.PrWr Issues BusUpgr signal on the bus. State transition to (M)Modified. other Caches see BusUpgr and mark their copies of the block as (I)Invalid.Modified(M)PrRd No bus transactions generated State remains the same. Read to the block is a Cache hitPrWr No bus transactions generated State remains the same. Write to the block is a Cache hit. +Table 1.2 State Transitions and response to various Bus OperationsInitial StateOperationResponseInvalid(I)BusRd No State change. Signal Ignored.BusRdX/BusUpgr No State change. Signal IgnoredExclusive(E)BusRd Transition to Shared (Since it implies a read taking place in other cache). Put FlushOpt on bus together with contents of block.BusRdX Transition to Invalid. Put FlushOpt on Bus, together with the data from now-invalidated block. Shared(S)BusRd No State change (other cache performed read on this block, so still shared). May put FlushOpt on bus together with contents of block (design choice, which cache with Shared state does this).BusRdX/BusUpgr Transition to Invalid (cache that sent BuxRdX/BusUpgr becomes Modified) May put FlushOpt on bus together with contents of block (design choice, which cache with Shared state does this)Modified(M)BusRd Transition to (S)Shared. Put FlushOpt on Bus with data. Received by sender of BusRd and Memory Controller, which writes to Main memory.BusRdX Transition to (I)Invalid. Put FlushOpt on Bus with data. Received by sender of BusRdx and Memory Controller, which writes to Main memory.
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).
A cache that holds a line in the Modified state must snoop (intercept) all attempted reads (from all the other caches in the system) of the corresponding main memory location and insert the data that it holds. This can be done by forcing the read to back off (i.e. retry later), then writing the data to main memory and changing the cache line to the Shared state. It can also be done by sending data from Modified cache to the cache performing the read. Note, snooping only required for read misses (protocol ensures that Modified cannot exist if any other cache can perform a read hit).
A cache that holds a line in the Shared state must listen for invalidate or request-for-ownership broadcasts from other caches, and discard the line (by moving it into Invalid state) on a match.
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. Other caches do not broadcast notices when they discard cache lines, and this cache could not use such notifications without maintaining a count of the number of shared copies.
In that sense the Exclusive state is an opportunistic optimization: If the CPU wants to modify a cache line in state S, a bus transaction is necessary to invalidate all other cached copies. State E enables modifying a cache line with no bus transaction.
Illustration of MESI protocol operations
For example, let us assume that the following stream of read/write references. All the references are to the same location and the digit refers to the processor issuing the reference.
The stream is : R1, W1, R3, W3, R1, R3, R2.
Initially it is assumed that all the caches are empty. +Table 1.3 An example of how MESI works All operations to same cache block
(Example: "R3" means read block by processor 3)Local RequestP1P2P3Generated Bus RequestData Supplier0Initially-----1R1E--BusRdMem2W1M----3R3S-SBusRdP1's Cache4W3I-MBusUpgr-5R1 S-SBusRdP3's Cache6R3S-S--7R2SSSBusRd P1/P3's Cache Note: The term snooping referred to below is a protocol for maintaining cache coherency in symmetric multiprocessing environments. All the caches on the bus monitor (snoop) the bus if they have a copy of the block of data that is requested on the bus.
Step 1: As the cache is initially empty, so the main memory provides P1 with the block and it becomes exclusive state. Step 2: As the block is already present in the cache and in an exclusive state so it directly modifies that without any bus instruction. The block is now in a modified state. Step 3: In this step, a BusRd is posted on the bus and the snooper on P1 senses this. It then flushes the data and changes its state to shared. The block on P3 also changes its state to shared as it has received data from another cache. The data is also written back to the main memory. Step 4: Here a BusUpgr is posted on the bus and the snooper on P1 senses this and invalidates the block as it is going to be modified by another cache. P3 then changes its block state to modified. Step 5: As the current state is invalid, thus it will post a BusRd on the bus. The snooper at P3 will sense this and so will flush the data out. The state of both the blocks on P1 and P3 will become shared now. Notice that this is when even the main memory will be updated with the previously modified data. Step 6: There is a hit in the cache and it is in the shared state so no bus request is made here. Step 7: There is cache miss on P2 and a BusRd is posted. The snooper on P1 and P3 sense this and both will attempt a flush. Whichever gets access of the bus first will do that operation.
Read For Ownership A Read For Ownership (RFO) is an operation in cache coherency protocols that combines a read and an invalidate broadcast. The operation is issued by a processor trying to write into a cache line that is in the shared (S) or invalid (I) states of the MESI protocol. The operation causes all other caches to set the state of such a line to I. A read for ownership transaction is a read operation with intent to write to that memory address. Therefore, this operation is exclusive. It brings data to the cache and invalidates all other processor caches that hold this memory line. This is termed "BusRdX" in tables above.
Memory Barriers