S4 · Lesson 51 · Coherence simulator project
MESI caches on a snooping bus, with traffic counts, data values and invariant checks
Coherence protocols are small state machines, but they're easy to get subtly wrong: one missing transition and, once in a million accesses, a core reads a stale value. Protocol designers guard against that the way verification engineers do (S3.25): they write a model of the protocol and drive it with random traffic while checking invariants after every step. In this project you'll build that model, MESI caches on a snooping bus carrying real data values, count the bus traffic it generates, and run it against a testbench that checks the single-writer invariant and that every read returns the most recent write, then extend it to MSI and compare.
Predict
Four cores each read and then write their own private line. How many bus transactions does MESI need, and MSI?
MESI: 4. Each read miss finds no other copy and loads the line in E, and the write moves E to M silently. MSI: 8. Without an E state, each read loads S, and the write needs a BusUpgr to invalidate copies that don't exist. You'll check both counts with your simulator in step 3.
Name it
The model
- n cores, each with a cache that holds any number of lines (no evictions), each line in state M, E, S or I (absent means I).
- A trace is a list of
(core, op, line)with op'R'or'W'. The write at trace position t stores the value t + 1, so every write is distinguishable; memory starts at 0 everywhere. - Each step is atomic on the bus: the requesting cache decides its transaction, every other cache snoops it, then the request completes.
The protocol tables
on_processor(state, op, shared) returns the new state and the bus transaction (or None), where shared says whether any other cache holds a valid copy:
| State | Read | Write |
|---|---|---|
| I | BusRd → E (S if shared) | BusRdX → M |
| S | → S | BusUpgr → M |
| E | → E | → M, silently |
| M | → M | → M |
on_snoop(state, bus) returns the new state and whether to flush (supply the data and update memory):
| State | BusRd | BusRdX or BusUpgr |
|---|---|---|
| M | → S, flush | → I, flush (BusRdX) |
| E | → S | → I |
| S | → S | → I |
| I | → I | → I |
The simulator
For each trace step: look up the requester's state and whether others share the line; get its new state and transaction; if there's a transaction, count it and apply on_snoop to every other cache (counting flushes, and copying flushed data to memory); on BusRd or BusRdX the requester then takes its data from memory; finally set its state, and either store the new value (write) or record the value it read. run(trace, n, protocol) returns {'counts': ..., 'reads': [...], 'states': [...]}.
The invariants the tests check after every prefix of random traces
- Single writer: if any cache holds a line in M or E, no other cache holds it at all.
- Data value: every read returns the value of the latest write to that line in trace order.
Worked example
Trace the counts and read values for (0, W, 0), (1, R, 0), (1, W, 0), (0, R, 0) on two caches.
Show the solution
- Core 0 writes. I → M with a BusRdX; it holds value 1.
- Core 1 reads. A BusRd: core 0 flushes (M → S, memory = 1), and core 1 gets S and reads 1.
- Core 1 writes. A BusUpgr: core 0 invalidates, and core 1 holds value 3 in M.
- Core 0 reads. A BusRd: core 1 flushes (memory = 3) and drops to S, and core 0 reads 3.
Answer the counts are BusRd 2, BusRdX 1, BusUpgr 1 and Flush 2, and the reads return [1, 3].
Worked example
Two cores alternately write the same line k times each. How many transactions per write, and what does this say about false sharing?
Show the solution
- Each write after the first. It finds the line in M in the other cache, so it needs a BusRdX that forces a flush and an invalidation.
- Count. That's two bus events per write, about 4k for 2k writes, however little data actually changes.
- False sharing. If the two cores were writing different words of that line, all of that traffic would be false sharing, which separate lines would remove entirely.
Answer two bus events per write (a BusRdX and the flush it forces), about 4k for 2k writes; if the cores write different words of the line, all of it is false sharing.
Check
Step 1. Write on_processor and on_snoop, the MESI protocol tables.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write run(trace, n): a snooping bus of n MESI caches, with traffic counts and real data values.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Make protocol='MSI' work: no E state, so every read miss lands in S.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why do protocol designers test coherence with random traces and invariants instead of a list of hand-written cases?
A coherence protocol's states multiply across caches and lines, and its bugs usually need an unlucky combination: a particular state in one cache, a request from another, then a third access in a certain order. Hand-written cases cover the situations a designer thought of, which are exactly the ones least likely to hide a bug. Random traces reach combinations nobody listed, and invariants such as "a modified copy is the only copy" and "a read returns the latest write" define correctness for every possible trace at once, so the checker doesn't need to know the right answer in advance. Running thousands of random traces and checking after every step finds the rare sequence that breaks the protocol, and the failing trace can then be shrunk to a short example.
Practice
Keep practicing
3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.