Many Cores and Caches: What Changes
Everything this module did rests on one fact: with one core and no cache, a load returns what the program last stored. Add a second core and caches, and that stops being simple. On the lab models of module S9, this page shows what changes for the reference model. It shows how a cache subsystem is checked.
You Will Be Able To
- Explain why a second core and caches break per-core lockstep of loads.
- Tell true sharing from false sharing, and say what each costs.
- Compare agent-driven and model-driven checking of a cache subsystem.
- Say why ap_soc has neither, and what that leaves out of this track.
- Lesson
- V5.7
- Time
- 22 min
- Runs on
- Your browser
- Check
- 5 questions
Core A stores 11 to a word. Core B loads the same word one cycle later and gets 0. Is that a bug? It depends on which store the bus put first, and no model of one core can say.
- Processor
- Bus
- Memory
- Data
- Both read
A and B read the line: both copies are Shared, and agree with memory.
- B writes
B must remove A's copy first: an invalidate on the bus. B's copy is now Modified; memory is old.
- A reads again
A misses; B answers with its changed copy and writes it back. A must see B's value, not memory's old one.
Read the diagram as text
- Core A and its cache: a copy of the line
- Core B and its cache: a copy of the line
- Coherent bus: invalidate, read miss, write back
- Memory: may be out of date
- Core A and its cache → Coherent bus: commands
- Core B and its cache → Coherent bus: commands
- Coherent bus → Memory: reads and write-backs
The cache lab of S9.3 (MSI, two caches, one line of two bytes). ap_soc has neither a second core nor a cache.
Before You Start
Read More Than One Core and Cache Coherence: MSI and MESI first, and Step Compare Against End-of-Test Memory Compare.
| Question | |
|---|---|
| 1 | What does a second core change for a model in lockstep? |
| 2 | What must a cache subsystem guarantee, and how is that checked? |
| 3 | What is false sharing, and why does it cost? |
| 4 | When are bus agents the better stimulus, and when are core models? |
| 5 | Why does ap_soc have neither, and what does that leave out? |
The Idea: A Load Stops Being Predictable
With one core and plain memory, a load returns the last value the program stored there. The lockstep model of this module relies on that: memory is its own, and only devices come from the record. With two cores, the last store to a word may be the other core's, and which one was last depends on timing.
Caches add copies. Each core may hold the line in its own cache, and only a coherence protocol keeps the copies in agreement. The rule a checker needs is simple to state. Every read returns the value of the last write to that word, in the order the interconnect chose.
So the checking splits. Each core can still be checked in lockstep, with shared loads treated like device reads, taken from the record. Coherence itself gets a checker of its own: a model of memory fed with the writes in bus order, judging every read.
New Words
| Word | Meaning |
|---|---|
| Hart | A hardware thread that runs instructions: here, one core. |
| Cache line | The block a cache copies at once; in the S9.3 lab, two bytes. |
| Coherence | All copies of a line agree: a read sees the last write in one agreed order. |
| True sharing | Two cores use the same bytes of a line. |
| False sharing | Two cores use different bytes of the same line, and the line moves between them anyway. |
| Agent-driven | Bus agents in the cores' seats make the traffic; a reference model of memory checks it. |
| Model-driven | Instruction-level models of the cores run programs; memory images and checkers check the result. |
The Smallest Example: Check Four Reads by Hand
Here are the steps of the S9.3 lab's shared run. Keep one flat memory on paper: a write sets a word; a read must return what the paper says.
| Step | Recorded |
|---|---|
| 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 |
Recorded by the S9.3 lab on Icarus. got: the value a read returned.
A reads 0, B reads 0, B writes x = 11. Then A reads: the paper says 11, and A got the line 0011 (y = 00, x = 11) from B's cache. Every read agrees with the flat memory, although memory itself held 0 until B wrote its line back.
The Rules
Rule 1: Every read returns the last write in bus order.
A checker keeps one flat memory, updated in the order the interconnect serialised the writes.
Rule 2: Shared loads come from the record.
Per-core lockstep still works, if a load from shared memory is treated like a device read.
Rule 3: Memory is right only after a flush.
An end-of-test image compare needs every changed line written back first.
Rule 4: Aim with agents, confirm with models.
Agents put two cores on one line at once; models show what real code does.
Rule 5: Count the line, not the byte.
Cost follows lines: false sharing moves a line on every write.
Sources for These Rules
MEHTA-DV chapter 17: a cache subsystem checked with UVM agents in the processors' seats. Chapter 18: the same subsystem with instruction-level models of the processors and a final image of external memory. The book weighs the two and recommends agents first, then models.
The labs: content/modules/v3/soc/soc-s93-shared.mjs (two caches, MSI) and soc-s91-labs.mjs (two harts and a wake-up). The bus order of writes is the lab's own.
ap_rv32 has no cache, and Zifencei is left out with it: docs/soc/ap_rv32-blueprint.md, section 10. ap_soc v1.5 has one core: plan §3.2.
Now Run It: The Two-Cache Lab, Checked by a Model
Neither lab below is ap_soc. The S9.3 lab is a small model of two caches kept coherent by MSI. Its one line holds two bytes. The S9.1 lab has two hart bus-functional models, on a dual-hart option of the chip. A bus-functional model is bench code that drives the bus the way a processor would.
The slice at the end of this page is a model-driven checker of the S9.3 lab. It reads the recorded steps and checks each read against a flat memory: stale reads: 0.
s1 A write x=01 bus=write-miss from=memory wb=- A=M B=I mem=0000
s2 B write y=02 bus=write-miss from=A wb=A A=I B=M mem=0001
s3 A write x=03 bus=write-miss from=B wb=B A=M B=I mem=0201
s4 B write y=04 bus=write-miss from=A wb=A A=I B=M mem=0203
bus commands=4 flushes=3Four writes to two different bytes took 4 bus commands, and every write found the line in the other cache. With x and y in two lines, the padded run took 1: rule 5. No byte was shared; the line was.
hart1 id=1: parks in wfi
hart0 id=0: boot hart, runs start-up code
e9 hart=0 op=store addr=02000004 wdata=00000001 result=00000000 resv=00 msip=00
e11 hart=1 op=store addr=02000004 wdata=00000000 result=00000000 resv=00 msip=10Two harts already change the core's pin checks. Each has its own hart ID, its own software line and its own wake-up. A check per hart, not per chip.
Why It Is Built This Way
Agents are the better stimulus for a cache subsystem. They place two transactions on one line in the same cycle, again and again. A program on a model cannot aim that well. The book's advice follows. Use agents to find most of the bugs, then run models of the cores for the cases real code makes.
Models are the better check of meaning. A program's result has one right answer whatever the interleaving. So an end-of-test image after a flush catches what agents never thought to send. Its weakness is the one the step and memory compares showed: late, and silent on masked faults.
ap_soc has one core and no cache on purpose. This track teaches the chip-level method on a chip a learner can hold in one head, and runs every test in seconds. So the per-core lockstep of this module is exact, and coherence appears only in the S9 labs and here. A chip with several cores would need the checkers of this page in its bench.
The Trap: The Lockstep That Broke on Shared Memory
A team adds a second core and keeps one model per core, each owning its copy of memory. The first program that shares a flag fails: core B's model loads its own stale copy while the chip loaded A's store. The bench reports a core bug in a correct core.
Shared memory must come from the record, like a device, and coherence must have a checker of its own. The worksheet counts what sharing costs and chooses a method for each job.
At Scale
This section looks ahead. This track's chip has one core.
Clusters
Application processors have clusters of cores with shared caches; checkers watch every coherent port.
Memory models
RISC-V's weak memory order (RVWMO) allows some reorderings; checkers then check a read against every order the model allows, not one.
Coherent interconnects
ACE and CHI carry the protocol between clusters (module S9); verification IP checks each transaction against the protocol.
Try It
Run the slice: it checks each recorded read of the cache lab against a flat memory.
Which read would a cache that missed the invalidate get wrong?
The predict question gives it such a read.
A reads word 0: got 0, model says 0 B reads word 0: got 0, model says 0 A reads word 0: got 11, model says 11 stale reads: 0Check Yourself
Common Mistakes
Wrong Give each core's model its own copy of shared memory.
The other core's stores never reach it, so a correct chip fails.
Fix Take shared loads from the record; check coherence separately.
Wrong Compare memory before the caches are flushed.
A Modified line holds the newest value; memory is legally old.
Fix Flush, then compare images.
Wrong Think data not shared costs nothing.
Two variables in one line move together on every write.
Fix Place data that cores write apart in separate lines.
Remember
- With several cores, a load's value depends on the order of writes, not on one program.
- Per-core lockstep survives if shared loads come from the record; coherence needs its own checker.
- Agents aim at the corners; models show what real code does; both are needed.
- ap_soc has one core and no cache, so this module's lockstep is exact; the S9 labs show what changes.