MESI Coherence Simulator
Prerequisites: Cache Coherency and MESI, False Sharing, The CPU Cache Hierarchy
Cache Coherency and MESI followed one 64-byte line while two cores took turns incrementing a counter on it, and each write flipped the other core's copy to Invalid.
Turn that trace into a program. Simulate the private caches of a small multicore CPU, apply MESI to every load and store, and report the coherence traffic each one causes.
- States: a core's copy of a line is
M(Modified),E(Exclusive),S(Shared), orI(Invalid). A copy inM,E, orSis valid. - Bus: the caches share a bus. A core that needs something from the other caches broadcasts one request on it.
- Snoop: every other cache checks its own copy of the requested line and changes state if the request requires it.
- Writeback: a Modified copy holds the only current data, because memory still has the old value. Whenever a copy leaves
M, the core first copies the line back to memory, the write-back the article says a Modified copy owes.
| Bus message | Meaning |
|---|---|
BusRd | a read miss: the core asks for a copy of the line to read |
BusRdX | the article's read-for-ownership: the core asks for the data and exclusive ownership, so every other copy is invalidated |
BusUpgr | an invalidate with no data: the core already holds a current Shared copy and only needs the other copies gone |
none | the core's own copy can serve the load or store, so nothing is sent |
What to implement
process_commands(cores, commands)returns the printed lines, in command order.- The machine has
corescores, namedC0throughC<cores-1>. - Each core has a private cache with unlimited capacity, so a line leaves a cache only when a command evicts it.
- Each cache line is named by a short lowercase identifier such as
aorcnt. The simulator sees lines, never addresses. - Every line starts Invalid in every core.
- On stdin, the first line is
cores, the second is the number of commands, and each remaining line is one command. - There are three parts. One solution must pass all ten tests.
| Command | Meaning | Prints |
|---|---|---|
Ck READ x | Core k loads from line x | a report line |
Ck WRITE x | Core k stores to line x | a report line |
Ck EVICT x | Core k drops its copy of line x (Part 2) | a report line |
STATS | Report the event counters (Part 2) | STATS: and the counters |
LOOP n ... | Repeat a sequence of loads and stores n times (Part 3) | the command, : , and the counters the loop added |
Report line
A READ, WRITE, or EVICT prints:
<command>: <message>, Cj X->Y, Cj X->Y writeback, ...
<command>is the command exactly as given.<message>isBusRd,BusRdX,BusUpgr, ornone.- Then one entry,
, Cj X->Y, for every core whose copy of the line changed state, from old stateXto new stateY. - An entry for a copy that left
Mends withwriteback. - Entries go in increasing core number, the requesting core included. Cores whose state did not change are left out.
- Example:
C0 READ a: BusRd, C0 I->S, C1 M->S writebacksaysC0missed, broadcastBusRd, and loaded the line Shared, whileC1wrote its Modified copy back and dropped to Shared.
Part 1: Loads and stores
A command first looks at the requesting core's own copy of the line:
| Command | Requesting core's state | Bus message | Requesting core's new state |
|---|---|---|---|
READ | M, E, or S | none | unchanged (a hit) |
READ | I | BusRd | E if no other core holds a valid copy, otherwise S |
WRITE | M | none | M |
WRITE | E | none | M |
WRITE | S | BusUpgr | M |
WRITE | I | BusRdX | M |
- The second row is the exclusive-on-first-read rule: a core that misses and finds no other valid copy holds the only copy, so it takes the line Exclusive.
- That pays off on the fourth row: an
Ecopy is known to be the only one, so writing it needs no bus message. A Shared copy has no such guarantee, so writing it costs aBusUpgr.
Every other core snoops the message and updates its own copy of the line:
| Bus message | Other core's state | Other core's new state |
|---|---|---|
BusRd | M | S, with a writeback |
BusRd | E | S |
BusRd | S | unchanged |
BusRdX | M | I, with a writeback |
BusRdX | E or S | I |
BusUpgr | S | I |
- An
Icopy never changes when another core's message arrives. - After every command the article's invariant holds for each line: either any number of cores hold it
Sand the restI, or at most one core holds itMorEand the restI. - Lines are independent, so a command changes states for its own line only.
Limits
- At most 104 commands.
coresis between 1 and 8, and every core number is belowcores.- Line names are 1 to 8 lowercase letters or digits.
- The tokens of a command are separated by single spaces.
Tests 1–4 cover this part. They use only READ and WRITE.
Part 2: Evictions and counters
Ck EVICT x removes core k's copy of x, so its state becomes I.
- A Modified copy is written back as it leaves. An Exclusive or Shared copy is dropped without one.
- If core
kholds no valid copy, nothing changes and the report is justCk EVICT x: none. - An eviction never sends a bus message, so its report always shows
none. No other core learns of it, and their states stay as they were. - Once every valid copy of a line has been evicted, the next
READfinds no other valid copy and takes the lineEagain. - A core can be left as the only
Sholder without knowing it. Its nextWRITEstill sendsBusUpgr, which then invalidates nothing.
Output of STATS:
STATS: BusRd=a BusRdX=b BusUpgr=c invalidations=i writebacks=w
- Counted from the first command, including commands run inside a
LOOP. a,b, andccount the bus messages of each kind.invalidationscounts copies that changed from a valid state toIbecause of another core'sBusRdXorBusUpgr. An eviction is not an invalidation.writebackscounts every writeback, whether a snoopedBusRd, a snoopedBusRdX, or an eviction caused it.
Tests 5–7 cover this part.
Part 3: Counter loops
The article's demo ran the same number of fetch_add calls on one thread and then split across two, and the split version was slower. LOOP n Ck OP x Cj OP y ... replays that kind of workload.
- After
ncomes the body: one or more triplesCk READ xorCk WRITE x. The loop runs the whole body in orderntimes. - An increment of a counter, an atomic
fetch_addincluded, is oneWRITE, because the core must own the line before it can change it. - The operations inside a loop print nothing.
- The loop's events still count toward later
STATSlines, and the states it leaves behind stay in place for the commands that follow. - The simulator works on lines, not variables. Two counters packed into one 64-byte line are one line here, so two cores writing them behave exactly like two cores writing one shared counter: false sharing as the hardware sees it. Padding each counter onto its own line gives each one a different line name.
Output: one line, the command exactly as given, : , and the counters in the STATS format, holding only what this loop added.
LOOP n ...: BusRd=a BusRdX=b BusUpgr=c invalidations=i writebacks=w
Limits: a LOOP repeats at most 104 times, and its body has at most 8 triples. Across all commands the simulator performs at most 2 × 105 loads, stores, and evictions, so simulating every one is fast enough.
Tests 8–10 cover this part.
2 11 C0 READ a C0 READ a C1 READ a C1 WRITE a C1 WRITE a C0 READ a C0 WRITE a C1 WRITE a C1 READ b C1 WRITE b C0 READ b
C0 READ a: BusRd, C0 I->E C0 READ a: none C1 READ a: BusRd, C0 E->S, C1 I->S C1 WRITE a: BusUpgr, C0 S->I, C1 S->M C1 WRITE a: none C0 READ a: BusRd, C0 I->S, C1 M->S writeback C0 WRITE a: BusUpgr, C0 S->M, C1 S->I C1 WRITE a: BusRdX, C0 M->I writeback, C1 I->M C1 READ b: BusRd, C1 I->E C1 WRITE b: none, C1 E->M C0 READ b: BusRd, C0 I->S, C1 M->S writeback
The first line is the number of cores and the second is the number of commands.
C0's first read misses and finds no other copy, so it takes line a Exclusive, and its second read is a hit. C1's read then demotes C0 to Shared and loads its own Shared copy.
C1's write starts from Shared, so it needs only a BusUpgr, which invalidates C0. When C0 reads again, C1's Modified copy is written back and both cores end Shared.
C0's write invalidates C1 with a BusUpgr. C1's next write starts from Invalid, so it sends a BusRdX that takes the line from C0's Modified copy.
Line b shows the silent upgrade: C1 reads it Exclusive and writes it with no bus message.