Multi-core programming--Cache consistency Protocol Mesi

Source: Internet
Author: User

Mesi protocol

In the MP system, each CPU has its own cache, the consistency between caches is very vulnerable, so the cache consistency protocol comes into being. Mesi is a very broad cache consistency protocol.

Mesi There are four states of "modified", "exclusive", "shared" and "invalid", and the protocol can apply these four states in a specified cache. Therefore, the Protocol maintains a two-bit state "tag" in each cache line, which is attached to the cache line's physical address or data.

    • The cache line in the "modified" state is due to the recent memory storage of the corresponding CPU. and the corresponding memory ensures that it does not appear in the cache of other CPUs. The figure below is somewhat misleading and can be seen as a invalid state. These pictures are the author of the "Big Liar", which is directly stolen here.
    • The "exclusive" state is very similar to the "modified" state, except that the cache line has not been modified by the corresponding CPU, which means that the data in the cache row and the data in memory are up-to-date.
    • Cache rows in the "shared" state may be replicated to at least one other CPU cache, so that data cannot be stored to the cache line when no other CPU is licensed.
    • The line in the "invalid" state is empty, in other words, it does not hold any valid data.
Mesi message

According to several graphs, the various states of the MESI protocol are very easy to understand. But in fact, the core of the Mesi protocol is MESI messages and state transitions. "Is parallel programming hard, ..." is to separate the message and state transfer, I think it is not particularly intuitive, so I put these two mixed together. If CPUs is on a single shared bus, the MESI protocol typically has the following messages.

  • READ: When the CPU does not find the physical address required in its own cache, it sends a "read" message that includes the physical address that the cache line needs to read.

  • Read Response: As the name implies, the "read Response" message is a reply to the "read" message. The "Read Response" message is provided by memory or other CPU caches. If the other cache requests a data that is in the "modified" state, the local cache must provide a "Read Response" message. It is easy to understand that other CPUs are requesting data from the local cache, and this data has not been flushed to memory, so the other CPUs must be told the latest value of the data. When a "read Response" message is received, the cached state of the data is changed from "invalid" to "share" or "exclusive", depending on whether the provider of read Response is memory or other CPU cache.

  • Invalidate: The "Invalidate" message contains the physical address of the cache line to invalidate. Other caches must remove the corresponding data from their cache and respond to this message. When the CPU is going to write to a variable that is in the read-only state (share), it needs to send a "invalid" message. Because a variable is cached by multiple CPUs, overwriting a single CPU causes the cache to be inconsistent, so you must tell the other CPUs before writing that the values you cache are going to be out of date soon. The CPU that receives the "invalidate" message will invalidate the corresponding data in the local cache.

  • Invalidate acknowledge: A CPU that receives a "Invalidate" message must respond to a "Invalidate acknowledge" message after removing the specified data. The message is to tell the provider of the "invalidate" message "I already know you're going to change this data and I'm quitting using my own cache copy!" ”

  • Read Invalidate: "read Invalidate" message contains the physical address to cache line reads. Also indicates that other caches are removing data. Therefore, it contains a "read" and a "invalidate". "Read Invalidate" also requires "read response" and "invalidate acknowledge" message set.
    The "Read Invalidate" message is sent at two times: the first is that the CPU is doing atomic read and write operations on one data, but that data is not in the cache of the local CPU, and there may be copies of that data in other CPU caches. So it needs to send a "read Invalidate" message, which not only needs to read the latest value of the data, but also invalidates other CPU caches (it will overwrite the data immediately).

  • Writeback: The "Writeback" message contains the address and data to write back to memory. This message allows the cache to free up space by swapping out "modified" state data when necessary. When the message is sent, the CPU flushes the data in the local cache into memory, which is the share state (read-only), and it needs to tell the other CPUs, "I'm not using these cached data anymore."

Mesi State Transitions

With the previous study, Mesi's state transfer is not difficult to understand. The state transition diagram for the MESI protocol is as follows:

Where A-l is the state transfer path. With regard to the full state transfer path, I am not prepared to document it in detail, as described in detail in the are parallel programming hard .... Here I have a few notes that I don't think are easy to understand.

    • Transition (E): This path leads from "share" state to "Modified" loading. This state transition occurs when the CPU executes an atomic read-write instruction (such as Test_and_set or EXCHG). In essence, there is no transfer of a state from S directly to M. The micro-instruction logic of the atomic read-write instruction is still read and write before it is written, it needs to invalid other CPU buffers before writing, so it starts from S to E, then overwrites, and the state shifts from E to M. However, the difference between the atomic read-write instruction and the Load+store is that it is atomic, cannot be segmented, and the entire instruction execution process is transactional and must be completed at once. Therefore, there will be such a state transition in the MESI protocol.

    • Transition (h): H is a state transfer path from S to E. There are two times when this state transfer occurs. The first one is: The CPU is going to write to a data immediately, it first invalidate the other CPU cache, so it enters the E state (no other CPU cache does not exist in the data). Another time is to receive writeback messages sent by all other CPUs that share some data, that is, the local cache is the only cache, and it is exclusive. What needs to be emphasized is--to rewrite a data, to "own" it, that is, in the S state is not able to rewrite the data.

      In "in-depth understanding of parallel programming V1.0", I think the translation of Transition (h) is wrong. At the same time I have found a lot of other errors, in the future blog post will be written in succession.

      Blog:intheworld

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Multi-core programming--Cache consistency Protocol Mesi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.