Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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), or I (Invalid). A copy in M, E, or S is 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 messageMeaning
BusRda read miss: the core asks for a copy of the line to read
BusRdXthe article's read-for-ownership: the core asks for the data and exclusive ownership, so every other copy is invalidated
BusUpgran invalidate with no data: the core already holds a current Shared copy and only needs the other copies gone
nonethe 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 cores cores, named C0 through C<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 a or cnt. 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.
CommandMeaningPrints
Ck READ xCore k loads from line xa report line
Ck WRITE xCore k stores to line xa report line
Ck EVICT xCore k drops its copy of line x (Part 2)a report line
STATSReport 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> is BusRd, BusRdX, BusUpgr, or none.
  • Then one entry, , Cj X->Y, for every core whose copy of the line changed state, from old state X to new state Y.
  • An entry for a copy that left M ends with writeback.
  • 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 writeback says C0 missed, broadcast BusRd, and loaded the line Shared, while C1 wrote 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:

CommandRequesting core's stateBus messageRequesting core's new state
READM, E, or Snoneunchanged (a hit)
READIBusRdE if no other core holds a valid copy, otherwise S
WRITEMnoneM
WRITEEnoneM
WRITESBusUpgrM
WRITEIBusRdXM
  • 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 E copy 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 a BusUpgr.

Every other core snoops the message and updates its own copy of the line:

Bus messageOther core's stateOther core's new state
BusRdMS, with a writeback
BusRdES
BusRdSunchanged
BusRdXMI, with a writeback
BusRdXE or SI
BusUpgrSI
  1. An I copy never changes when another core's message arrives.
  2. After every command the article's invariant holds for each line: either any number of cores hold it S and the rest I, or at most one core holds it M or E and the rest I.
  3. Lines are independent, so a command changes states for its own line only.

Limits

  • At most 104 commands.
  • cores is between 1 and 8, and every core number is below cores.
  • 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.

  1. A Modified copy is written back as it leaves. An Exclusive or Shared copy is dropped without one.
  2. If core k holds no valid copy, nothing changes and the report is just Ck EVICT x: none.
  3. 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.
  4. Once every valid copy of a line has been evicted, the next READ finds no other valid copy and takes the line E again.
  5. A core can be left as the only S holder without knowing it. Its next WRITE still sends BusUpgr, 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, and c count the bus messages of each kind.
  • invalidations counts copies that changed from a valid state to I because of another core's BusRdX or BusUpgr. An eviction is not an invalidation.
  • writebacks counts every writeback, whether a snooped BusRd, a snooped BusRdX, 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.

  1. After n comes the body: one or more triples Ck READ x or Ck WRITE x. The loop runs the whole body in order n times.
  2. An increment of a counter, an atomic fetch_add included, is one WRITE, because the core must own the line before it can change it.
  3. The operations inside a loop print nothing.
  4. The loop's events still count toward later STATS lines, and the states it leaves behind stay in place for the commands that follow.
  5. 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.

Example
Input
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
Output
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
Explanation

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.