MESI

Every core sees every write, and the bus bills you for it

L215 MINSYSTEMS & LOW-LEVEL
PREDICT

Each core has a private cache, kept coherent by messages on a shared bus. Core 0 holds line L in the Exclusive state: no other cache has a copy. It writes one byte of L. What goes on the bus?

Each core has a private cache. When one core writes, the others must not keep reading their old copy, so the caches talk over a bus. MESI tags every cache line as Modified, Exclusive, Shared or Invalid. A read miss asks the bus; a write to a Shared line first invalidates every other copy; a Modified line is flushed when someone else reads it. A line a core holds in Exclusive or Modified is written with no bus traffic at all. The bus does not know what a variable is. It knows lines: 64 bytes on most x86 and many ARM cores, 128 on Apple M-series.

▶ SIMULATION
CORE 0
L0: Invalid · x = —, y = —
L1: Invalid · z = —
last: —
BUS / MEMORY
mem: x = 42, y = 7 (L0) · z = 0 (L1)
bus transactions: 0 · line bounces: 0
cache hits (no bus): 0
bus: —
CORE 1
L0: Invalid · x = —, y = —
L1: Invalid · z = —
last: —
✓ a line in E or M is the only copy✓ every read returns the newest write
SCHEDULE
01[Core 0]Core 0: read x (miss → BusRd → Exclusive)
02[Core 1]Core 1: read x (miss → BusRd, Core 0 snoops → both Shared)
03[Core 0]Core 0: write x = 99 (Shared → invalidate Core 1 → Modified)
04[Core 1]Core 1: read x (miss → Core 0 flushes 99 → both Shared)
Start of coherent write scenario. 4 steps.
MESI state transitions (PrRd/PrWr = this core; BusRd/BusRdX = snooped from the bus)

Invalid   + PrRd   → BusRd   → Exclusive  (no other copy)
                             → Shared     (another cache has it)
Invalid   + PrWr   → BusRdX  → Modified   (others invalidated)

Exclusive + PrRd   → hit     → Exclusive
Exclusive + PrWr   → hit     → Modified   (SILENT: no bus traffic)
Exclusive + BusRd  → supply  → Shared
Exclusive + BusRdX →         → Invalid

Shared    + PrRd   → hit     → Shared
Shared    + PrWr   → BusUpgr → Modified   (invalidate every other copy)
Shared    + BusRd  →         → Shared
Shared    + BusRdX →         → Invalid

Modified  + PrRd   → hit     → Modified
Modified  + PrWr   → hit     → Modified
Modified  + BusRd  → flush   → Shared     (write back, then share)
Modified  + BusRdX → flush   → Invalid    (write back, then drop)

Invariant: at most one cache holds a line in E or M, and then no other cache holds it at all.

Watch the two invariants. WITHOUT COHERENCE breaks the single-owner invariant the moment Core 0 writes without invalidating, and the freshness invariant on Core 1’s next read. FALSE SHARING keeps both green while the bus counter climbs by one per write.

BREAK IT NOT YET

Core 0 only ever writes x. Core 1 only ever writes y. They never read each other's variable. Order the six writes so the bus carries at least 6 transactions.

GOAL: all six writes run and bus transactions >= 6

FIX IT NOT YET

This transition table has two wrong rows. One is unsafe: a core can read a stale value. One is slow: it puts a transaction on the bus that the protocol does not need. Fix both. The grader runs two caches through five programs and checks every read against the newest write.

Mental model:

the unit of coherence is the line, not the variable. A write is free while you own the line and costs a bus round trip whenever someone else holds a copy. Two cores that write the same line, even to different bytes, pay that round trip on every write. Coherence keeps the values right; it does nothing to keep them cheap.

IN PRODUCTION

Per-thread counters packed in one array, a spinlock next to the data it protects, hot fields of two threads in one struct. Symptoms are scaling curves that bend the wrong way: more cores, less throughput, and a profiler that blames a plain store. Look for alignas(64), padding fields and @Contended.

IN INTERVIEWS

“What is false sharing and how do you fix it?” Say that the cache line is the unit, that each write invalidates the other core's copy, and that padding hot variables onto separate lines removes the traffic. Then name the invariant MESI protects: at most one Modified copy, every other copy Invalid.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: VIRTUAL MEMORY →