CPU modification of memory variables is to read the memory data into the CPU cache, and then by the CPU to do the operation, after the completion of the operation to continue to write to memory
In a single-core CPU, this is completely fine, but in multicore CPUs, each CPU core has its own independent cache
When you access the same memory address at the same time, the memory value is copied to the cache of multiple CPUs
At this point, if you modify the value in the cache, the data will be inconsistent, when written to memory, the data in memory will not reach the expected value
In order to solve this problem, the early CPU, the use of bus lock method, a CPU to memory operation, the bus lock, until the operation is completed and then unlock
The bus contains a lot of device control, lock will greatly reduce the speed of resource processing, so Intel proposed the MESI protocol
Intel has been introducing the MESI protocol since the Pentium, and many CPUs are now using variants of the Protocol
In Mesi, a cache is called row, a copy of the same memory address in different CPU caches , and their Row is the same
There are 4 states of row, and they are
State |
Describe |
M (Modified) |
This line of data is valid, the data is modified, and the data in memory is inconsistent, the data only exists in this cache. |
E (Exclusive) |
This line of data is valid, the data is consistent with the data in memory, and the data exists only in this cache. |
S (Shared) |
This line of data is valid, the data is consistent with the data in memory, and the data exists in many caches. |
I (Invalid) |
This row of data is invalid. |
Exclusive Monopoly
M Modify State
Each CPU listens to its own cache row while listening to the other CPU's cache row
Set to E (Exclusive) state when only one CPU has a memory copy
Set to the S (Shared) state when the second CPU reads a memory copy
Set to I (Invalid) state when other CPUs modify the memory copy
When the current CPU modifies the memory copy, it is set to the M (Modified) state
Each CPU Cache row has a status of its own
The migration process between the Mesi states is as follows:
Current status |
Event |
Behavior |
Next state |
I (Invalid) |
Local Read |
If the other cache does not have this data, this cache takes data from memory, the cache line status becomes E; If the other cache has this data, and the status is M, the data will be updated to memory, the cache and then fetch data from memory, the cache line status of 2 cache is changed to S; If the other cache has this data and the status is S or E, this cache takes data from memory and the cache line status of these caches becomes s |
e/s |
Local Write |
Fetch data from memory, modify it in cache, change status to M; If the other cache has this data and the status is M, the data should be updated to memory first; If the other cache has this data, the cache line status of the other cache becomes I |
M |
Remote Read |
Since it's invalid, the operation of the other nuclei has nothing to do with it. |
I |
Remote Write |
Since it's invalid, the operation of the other nuclei has nothing to do with it. |
I |
E (Exclusive) |
Local Read |
Fetch data from cache, state unchanged |
E |
Local Write |
Modify the data in the cache, the status becomes m |
M |
Remote Read |
Data and other cores are shared, the state becomes S |
S |
Remote Write |
The data is modified, this cache line can no longer be used, the status becomes I |
I |
S (Shared) |
Local Read |
Fetch data from cache, state unchanged |
S |
Local Write |
Modify the data in the cache, the status becomes M, Other kernel-shared cache line status becomes I |
M |
Remote Read |
The state does not change |
S |
Remote Write |
The data is modified, this cache line can no longer be used, the status becomes I |
I |
M (Modified) |
Local Read |
Fetch data from cache, state unchanged |
M |
Local Write |
Modify the data in the cache with the same status |
M |
Remote Read |
This line of data is written into memory so that other nuclear energy is used to the latest data, the state becomes S |
S |
Remote Write |
This line of data is written into memory so that other nuclear energy is used to the latest data, as other cores modify the line of data, The state becomes I |
I |
Multithreaded programming, CPU is if you troubleshoot multi-threaded memory access problems