Cache Coherence: MSI and MESI
Two harts, two caches, one shared byte. Without rules, one hart keeps reading a value that the other has already changed. Three states for each cache line are enough to stop that, and a fourth makes it cheaper.
You Will Be Able To
- Explain how two caches can hold two different values for one block.
- Follow one line through the states I, S and M, and name the command that each access puts on the bus.
- Say what the state E saves, and why two variables in one block can slow two harts down.
- Lesson
- S9.3
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
Hart B stores 11 to a shared flag. A moment later hart A reads 00. Nothing crashed, and every test with one hart had passed.
Hart A read its own copy, and nobody had told that copy that it was old.
Before You Start
You need Memory and Caches in One Picture: a cache, a cache line, a hit, a miss and the tag. This page builds on every one of them.
You need More Than One Core: two harts that share one memory.
It helps to have read Atomic Operations: The Lost Update and LR/SC: two harts and one word, with no cache in between.
| Question | |
|---|---|
| 1 | How can two caches hold two different values for one block? |
| 2 | What must happen to the other copies before a hart writes? |
| 3 | One cache holds the only changed copy, and the other misses. Who supplies the block? |
| 4 | What does the state E save, and when? |
| 5 | Both harts read a block, then hart A writes it and hart B reads it. What does the lab print last? |
The Idea: Every Copy Must Know When It Is Old
A cache keeps copies of memory close to its hart. With one hart, a copy is never older than what the hart last wrote. Give a second hart a cache of its own, and one piece of memory can have two copies.
A cache copies memory in pieces. One piece is a block, and a line holds one block. Now hart B writes a block that both caches hold. Its own copy is new. The copy in cache A is a stale copy. It still holds the old value, and hart A will read it as if nothing had happened.
A memory system is coherent when this cannot be seen. A hart reads back what it last wrote. A write by one hart is seen by the others soon after. And all harts see the writes to one place in the same order.
The classic way to get there has one rule: before a hart writes a block, every other copy is removed. For that, the caches must talk. They share a bus, and every cache watches each command that crosses it. Watching is called snooping. Each line also needs to remember what it knows about the other copies. Three states are enough.
New Words
| Word | Meaning |
|---|---|
| Block | The piece of memory that one cache line holds a copy of. The lab has one block, named X. |
| Stale copy | A copy that still holds an old value after the block was changed somewhere else. |
| Coherent | No hart can ever read a stale copy. The rules that make it so are a coherence protocol. |
| Snooping | A cache watches the commands of the other caches on a shared bus, and reacts to those that name a block it holds. |
| Clean copy | A copy that equals memory. |
| Dirty copy | A copy that was changed, so memory is old. |
| Line in I | I for invalid: the line holds no copy of the block. |
| Line in S | S for shared: the line holds a clean copy. Other caches may hold one too. |
| Line in M | M for modified: the line holds the only copy, and it is dirty. |
| MSI | The coherence protocol with these three states. |
| Read miss, write miss | Commands on the bus: "I need this block to read it", and "I need it to write it, so drop yours". |
| Invalidate | A command on the bus: "I have the block already. Drop your copy." |
| Flush | A cache sends its dirty block out. The cache that asked takes it, and so does memory. |
| Evict | A cache gives a line up, to make room. A dirty block is written to memory first. |
| Line in E | E for exclusive: the line holds the only copy, and it is clean. |
| MESI | MSI with the state E added. |
One Picture: A Bus That Both Caches Watch
- Processor
- Memory
- Bus
- Two copies
Both harts have read block X. Each cache holds a copy, and both copies equal memory.
- One copy goes old
Hart B writes X in its own cache. With no rule, cache A still holds the old value, and hart A reads it.
- Tell the others first
So cache B puts a command on the bus before the write. Cache A sees it and drops its copy.
- The holder answers
Hart A reads X again and misses. Memory is old, so cache B sends the block. Memory takes a copy as well.
Read the diagram as text
- Hart A: loads and stores
- Cache A: one line with a copy of block X
- Shared bus: carries one command at a time, and both caches watch it
- Cache B: one line with a copy of block X
- Hart B: loads and stores
- Memory: holds every block, sometimes an old version
- Hart A → Cache A: read, write
- Hart B → Cache B: read, write
- Cache A → Shared bus: command
- Cache B → Shared bus: command
- Shared bus → Memory: block
Two caches on one bus. A cache that watches the commands of the others is said to snoop.
The Smallest Example, Worked by Hand
No simulator yet. One block, X, holds 00 in memory. Both lines start in state I. Follow four accesses, and write down the state of each line after every one.
| Step | Access | Command on the bus | Who supplies the block | Line A | Line B | Memory |
|---|---|---|---|---|---|---|
| 1 | A reads | Read miss | Memory | S | I | 00 |
| 2 | B reads | Read miss | Memory | S | S | 00 |
| 3 | B writes 11 | Invalidate | Nobody: B has it | I | M | 00 |
| 4 | A reads, gets 11 | Read miss | Cache B | S | S | 11 |
Read step 3. B holds a copy already, so it needs no data. It needs the other copy gone. One invalidate does that, and line A goes to I. Memory still holds 00. That is allowed, because B holds the block in M and knows that memory is old.
Read step 4. A misses. If memory answered, A would get 00: the stale value of the story at the top. So cache B, which sees the read miss on the bus, sends its block. Memory takes it too, and both lines end in S.
The Rules
Rule 1: Before a hart writes a block, every other copy is removed.
So at most one cache holds a dirty copy, and no other copy exists beside it.
Rule 2: A line is in one of three states: I, S or M.
I: no copy. S: a clean copy, maybe not the only one. M: the only copy, and dirty.
Rule 3: A cache that needs something puts one command on the bus.
A read miss, a write miss, or an invalidate. A hit in M needs none.
Rule 4: Every cache snoops every command.
It acts when the command names a block that it holds.
Rule 5: A cache that holds the block in M answers in place of memory.
It flushes the block. Memory would give the old value.
Rule 6: A fourth state, E, marks a clean copy that is known to be the only one.
A write from E goes to M with no command.
| Event | In I | In S | In M |
|---|---|---|---|
| My hart reads | Read miss, go to S | Hit, stay S | Hit, stay M |
| My hart writes | Write miss, go to M | Invalidate, go to M | Hit, stay M |
| I evict the block | Nothing to do | Drop it, go to I | Write it to memory, go to I |
| Seen on the bus: read miss | Nothing | Stay S | Send the block, go to S |
| Seen on the bus: write miss | Nothing | Go to I | Send the block, go to I |
| Seen on the bus: invalidate | Nothing | Go to I | Cannot happen |
The first three rows are what any cache does. The last three are the snooping half.
The cell "cannot happen" is a promise of rule 1. An invalidate comes from a cache that holds S. Nobody holds S while someone holds M.
Sources for These Rules
The three conditions of coherence, snooping, removing the other copies before a write, and false sharing: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 5.10.
The table of three states, the holder of a dirty copy answering in place of memory, and the state E: the same book, section 5.12, Figures e5.12.9 to e5.12.11.
The codes of the states, the two bytes of the block and the wire with which a cache says "I hold it too" are choices of our lab.
Now Run It
The lab at the end of this page has two caches with one line each. A test bench plays the bus and memory. It runs eight accesses to block X. The first four are the hand example.
s1 A read got=0000 bus=read-miss from=memory wb=- A=S B=I mem=0000
s2 B read got=0000 bus=read-miss from=memory wb=- A=S B=S mem=0000
s3 B write x=11 bus=invalidate from=- wb=- A=I B=M mem=0000
s4 A read got=0011 bus=read-miss from=B wb=B A=S B=S mem=0011
s5 A write x=22 bus=invalidate from=- wb=- A=M B=I mem=0011
s6 A write x=33 bus=- from=- wb=- A=M B=I mem=0011
s7 B write y=44 bus=write-miss from=A wb=A A=I B=M mem=0033
s8 B evict bus=- from=- wb=B A=I B=I mem=4433
bus commands=6 flushes=3Rule 1 at work, in row s3. B writes while it holds S. The bus shows an invalidate, A goes to I, and memory still holds 0000.
Rule 5, in row s4. A misses, and the block comes from B. Memory and A both get 0011.
Rule 3, in row s6. A writes while it holds M, and the bus stays quiet. In all, 8 accesses put 6 commands on the bus.
- Cycle 0
A misses and takes the block from memory, in state S.
- Cycle 2
B holds the block in S, so its write sends only an invalidate. A goes to I.
- Cycle 3
B holds the only new copy and sends it. Memory and A both get 0011.
- Cycle 6
The write miss of B makes A send 0033 and drop its copy.
- Cycle 7
B evicts its changed line. Only now does memory hold 4433.
Recorded from the lab below.
Now rule 6. Much data is used by one hart alone: it reads a block and then writes it. With three states that costs two commands.
s1 A read got=0000 bus=read-miss from=memory wb=- A=S B=I mem=0000
s2 A write x=55 bus=invalidate from=- wb=- A=M B=I mem=0000
bus commands=2 flushes=0Look at s2. The bus carries an invalidate, and line B was in I all the time. The command removed nothing. Line A was in S, and S only says "maybe someone else has it".
s1 A read got=0000 bus=read-miss from=memory wb=- A=E B=I mem=0000
s2 A write x=55 bus=- from=- wb=- A=M B=I mem=0000
bus commands=1 flushes=0With four states the read ends in E, and the write puts nothing on the bus: one command in place of two.
This lab leaves things out. ap_soc has one hart and no data cache: both caches are models. Each has one line, so nothing is ever evicted to make room. The line with four states is not shown, because you build it in the practice.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Remove the other copies before a write | Send the new value to every copy on each write | One command, and later writes are free. A hart that reads the block again must miss. |
| The dirty holder answers a miss | Write to memory on every store | Stores stay fast. Memory can be old, so every miss must ask the caches first. |
| A state E | Three states only | No command for data that one hart uses alone. Each cache must say, on a read miss, whether it holds the block. |
The Trap: Two Counters in One Block
Two harts each kept a counter of their own. No hart ever touched the counter of the other. Yet the program ran slower on two harts than on one.
The two counters sat in the same block. Coherence works on whole blocks, not on variables. Each write by one hart took the whole block away from the other. In the lab, four such writes end with "bus commands=4 flushes=3": every write is a write miss. With the second counter in a block of its own, the two writes of hart A end with "bus commands=1 flushes=0".
This is false sharing. The fix is in the layout of the data, because the hardware sees only blocks.
The second trap is to read S as proof that another copy exists. A cache may drop a clean copy and tell nobody. S means "maybe", and only E means "I am alone".
At Scale
This section looks ahead. Two caches on one bus is the textbook case.
Every command costs a lookup
Each command makes every cache compare its tags, while its own hart wants to use them too.
One bus stops being enough
With many caches, a block in the middle keeps a list of who may hold each block. Then only those caches are asked.
Nothing happens in one step
Here an access is finished before the next begins. On a real network a command can arrive while a cache waits for its own answer.
A real block
A real line holds about 64 bytes. False sharing is then two variables anywhere inside those 64 bytes.
Follow a read across a larger chip.
Try It
Run the lab. Find the step in which memory first holds 0011. Which cache sent it?
Set SCRIPT to 2 and then to 3. Compare the two last lines.
In msi_line, find the branch for a read miss seen on the bus, and delete its flush. Run script 0 again. What does A get in step 4?
Names Printed by the Lab
| Name | Meaning |
|---|---|
| s1, s2, ... | One access each. An access is finished before the next begins. |
| A, B | The hart that made the access, and at the end of the row the state of the line in each cache. |
| got | What a read returned. Block X holds two bytes, named y and x, printed as y and then x. |
| bus | The command that the access put on the bus. A dash means none. |
| from | Who supplied the block: memory, or the other cache. |
| wb | The cache that wrote the block to memory in this step, if any. |
| mem | What memory holds for block X after the step. |
s1 A read got=0000 bus=read-miss from=memory wb=- A=S B=I mem=0000 s2 B read got=0000 bus=read-miss from=memory wb=- A=S B=S mem=0000 s3 B write x=11 bus=invalidate from=- wb=- A=I B=M mem=0000 s4 A read got=0011 bus=read-miss from=B wb=B A=S B=S mem=0011 s5 A write x=22 bus=invalidate from=- wb=- A=M B=I mem=0011 s6 A write x=33 bus=- from=- wb=- A=M B=I mem=0011 s7 B write y=44 bus=write-miss from=A wb=A A=I B=M mem=0033 s8 B evict bus=- from=- wb=B A=I B=I mem=4433 bus commands=6 flushes=3Read included file: msi_line.sv
// One line of a write-back, write-invalidate MSI cache, as in Patterson and
// Hennessy, Computer Organization and Design (ARM edition), section 5.12,
// Figures e5.12.9 and e5.12.10. This is a teaching model for S9.3: ap_soc v1.0
// has no data cache, so nothing in the chip instantiates it yet.
//
// The controller is pure next-state logic. The state register lives with the
// caller, so one module serves every line and every cache in the lab.
module msi_line (
input logic [1:0] state, // I = 0, S = 1, M = 3 (2 is left for MESI's E)
input logic [2:0] op, // what happened to this block in this step
output logic [1:0] next,
output logic [1:0] bus_cmd, // what this cache places on the snoop bus
output logic flush // this cache sends the block out (to the bus and memory)
);
localparam logic [1:0] I = 2'd0, S = 2'd1, M = 2'd3;
// Requests from this cache's own processor ...
localparam logic [2:0] PR_RD = 3'd1, PR_WR = 3'd2, EVICT = 3'd3;
// ... and commands snooped from the other caches on the bus.
localparam logic [2:0] SN_RD = 3'd4, SN_WRMISS = 3'd5, SN_INV = 3'd6;
localparam logic [1:0] NONE = 2'd0, RD_MISS = 2'd1, WR_MISS = 2'd2, INVALIDATE = 2'd3;
always_comb begin
next = state;
bus_cmd = NONE;
flush = 1'b0;
case (op)
PR_RD:
if (state == I) begin // read miss: fetch a readable copy
next = S;
bus_cmd = RD_MISS;
end
PR_WR:
if (state == I) begin // write miss: fetch the block and own it
next = M;
bus_cmd = WR_MISS;
end else if (state == S) begin // upgrade: other copies must go first
next = M;
bus_cmd = INVALIDATE;
end
EVICT: begin // replacement: only a dirty block is written back
flush = state == M;
next = I;
end
SN_RD:
if (state == M) begin // another cache reads our dirty block:
flush = 1'b1; // supply it, memory takes a copy,
next = S; // and we keep a shared copy
end
SN_WRMISS: begin // another cache wants to write:
flush = state == M; // hand over dirty data, then drop the copy
next = I;
end
SN_INV:
if (state == S) next = I;
default: ;
endcase
end
endmodule
Check Yourself
Common Mistakes
Wrong Read S as proof that another copy exists.
A cache can drop a clean copy and tell nobody.
Fix Read S as "maybe shared" and E as "known to be alone".
Wrong Let memory answer every read miss.
A copy in M somewhere else is newer than memory.
Fix Let the holder of the dirty copy send it first.
Wrong Put data of two harts in one block.
Every write takes the whole block from the other cache.
Fix Give data that different harts write a block of its own.
Remember
- Two caches can hold two copies of one block, and a write makes the other copy stale.
- Before a write, every other copy is removed. Three states and three commands do that.
- The holder of a dirty copy answers a miss in place of memory.
- The state E saves a command for private data. Two variables in one block cost commands for no reason.