If the background

In order to solve the problem of speed mismatch between CPU and memory, modern computer systems have introduced Cache to improve performance. The original processors were single-core, and the Cache had to deal with the problem of whether the data in the Cache was consistent with the data in memory.

  • The current Cache behavior is considered Clean if the data in the Cache is consistent with the data in the corresponding memory row.
  • The current Cache behavior is considered Dirty when the data in the Cache does not correspond to the corresponding row.

Dirty or CleanTwo states are sufficient to represent the state of the Cache in a single-core system, but as the processor progresses to multi-core, each CPU core has its own corresponding Cache, in memory
The same row may be cached by multiple Cache blocks, and in a different state, where simply Dirty and Clean are not enough to deal with the consistency issues that arise.
This led to the introduction of the MESI Conformance Protocol.


Introduction of agreement

The MESI protocol divides the data in the Cache into four states:

  • Modified: The data in the current Cache row is Dirty and is only Modified in the current CPU’s Cache. At this point, the data for this Cache row is different from the data in any other Cache and from the data for that row in memory.
  • EXCLUSIVE: indicates that the current Cache row is valid and does not exist in other CPUs’ caches; And the current Cache row data is the same as the data in memory.
  • Shared: The row is Shared between multiple CPUs, and the data in the Cache is identical to the data in memory.
  • Invalid: Indicates that data in the current Cache row is Invalid;

Exclusive state

Core 0 has a Cache of (x=3) that corresponds to memory. Core1 and Core2 do not Cache this data. At this point, the Cache row corresponding to Core 0 is in an Exclusive state.

Shared state

Core0/1/2 (x=3) is Shared, and the Cache rows corresponding to Core0/1/2 (x=3) are Shared.

Modified and Invalid states

Core 0 has the latest x-value (x=5), and both Core 1/2 and memory have an invalid x-value (x=3). The Cache behavior of Core 0 is considered Modified, and the Cache behavior of Core 1/2 is considered Invalid.

MESI state toggle

The state transitions in the MESI protocol are shown in the figure. Each Cache controller decides how to make the state jump based on the read and write operations of its own Core and the read and write operations of other cores.

This Local the Read/Write refers to Cache the corresponding Core to the current data Read and Write operations, Remote Read/Write refers to other Core to the current data Read and Write operations.

The jump for each state is analyzed below.

MESI state jump diagram

Modified state jump

  • Local Read: Fetch data from current Cache, state unchanged, M
  • Local Write: Write to the current Cache with the same state as M
  • Remote Read: Write this data to memory so that the rest of the Core can retrieve the latest data and change the state to S.
  • Remote Write: The data is written to memory, and then the other Core modifies the data, changing the state to I.

Exclusive state jump

  • Local Read: Fetches data from current Cache, state unchanged, E
  • Local Write: Writes data to the current Cache with state M.
  • Remote Read: The rest of the Core reads this data from memory, becomes a shared state, and changes its state to S.
  • Remote Write: The current Cache row is invalid, and the state is changed to I.

    Shared state jump

  • Local Read: Fetches data from current Cache, state unchanged, S
  • Local Write: Write to the current Cache with a state of “M”, and Write to “I”.
  • Remote Read: Has no effect on the current Cache.
  • Remote Write: The data was modified, the current row is invalid, and the state is changed to I.

Invalid state jump

  • Local Read: ** If the data is not cached by any other Cache, Read it from memory and the state is changed to E; If one of the other caches has this item in state “M”, then the current Cache will fetch the same item from memory, and the corresponding row will be changed to “S**”. If another Cache has this item in state S or E, and the current Cache fetches this item from memory, the corresponding rows are the same, and they all become S.
  • Local Write: read data in memory, modify data in Cache, change state to M; If any other Cache has this item in state M, then update the data to memory first. If other caches have this data, then those Cache rows become I.
  • Remote Read: Has no effect on the current Cache.
  • Remote Write: Has no effect on the current Cache.