A latency profile of the memory hierarchy on Apple Silicon.
It measures what a hot path pays for each dependent load
at every cache level, as the number of busy cores grows, and when two cores
write to the same line. It then recovers the cache geometry from those curves
and checks it against what sysctl reports.
The core instrument is a pointer chase over a Sattolo shuffle. Each load's address is the result of the previous load, so there is no memory level parallelism, no prefetching and no out of order overlap to hide the cost.
Latencies are measured in nanoseconds. Cycle counts assume the nominal P core clock of ~4.4 GHz.
-
Per core hot state has a hard budget of 128 KB. Inside it, a dependent load takes 0.68 ns, ~3 cycles. At 152 KB it's already 4.2 ns, a 6x step, and from 256 KB it settles at 6 ns, ~27 cycles and 9x. Size order books, symbol tables and per instrument state against that measured edge, not the spec sheet.
-
L2 is budgeted per cluster, not per core. Five busy cores share one 16 MB L2. At 4 MB per thread, 5 threads pay 1.8 -> 1.9x and 10 threads 2.4 -> 3.3x what a single thread pays across three runs; at 8 MB per thread even 2 threads pay 1.7 -> 2.2x. Under random access the usable capacity is 8 -> 11 MB, not 16, and a thread per core design has to divide that number.
-
State owned by different writers needs to sit at least 64 B apart on this chip. Two cores updating counters 8 -> 32 B apart pay 3x per update; at 64 B or more they don't interact. Padding to 128 B also clears the L2 line.
-
A DRAM miss costs 105 ns, ~460 cycles, which is more than a hundred L1 hits. Data that falls out of L2 first lands on the system level cache shelf at 16 MB -> 64 MB (20 -> 89 ns).
-
Stores are cheap everywhere, loads are not. A random store costs 0.68 ns from 4 KB to 1 MB and 4.3 ns at DRAM; the same working sets cost 0.67 and 105 ns per dependent load. Write-heavy paths are bandwidth problems, read paths are latency problems, and the two need different layouts.
The chip has 10 P cores in two clusters of 5, with each cluster sharing a 16 MB
L2, 128 KB of L1d per core, and 16 KB pages. The numbers below come from one
run of ./build/cache_bench, taking the minimum over trials, with threads
steered onto P cores through a QoS class. Take them on a quiet machine. With
another benchmark running alongside, every level came out ~10 percent slower,
the 32 MB point went from 34 to 100 ns because the system cache was gone, and
the L2 pick wandered between 4 and 13.5 MB across five runs. L1d and the 64 B
boundary did not move.
| quantity | detected | next point measured | sysctl |
note |
|---|---|---|---|---|
| L1d capacity | 128 KB | 152 KB | 128 KB | exact |
| L2 capacity | 8 MB | 10 MB | 16 MB | 8 -> 11 MB across runs, see "Why L2 reads low" |
| coherence granularity | 64 B | 128 B (hw.cachelinesize) |
see "64 or 128" |
| level | latency per dependent load (ns) |
|---|---|
| L1d hit (4 KB -> 128 KB) | 0.68 |
| L2 hit (256 KB -> 8 MB) | 6.0 -> 8.0 |
| system level cache (16 MB -> 64 MB) | 20 -> 89 |
| DRAM (128 MB and up) | 103 -> 105 |
| random store, L1 through L2 | 0.69 |
| random store, DRAM | 4.3 |
Latency is flat at 0.68 ns through 128 KB, jumps 6x at 152 KB, and stays flat
again to 8 MB. From there it climbs through 10 MB (11.9 ns), 14 MB (17.3 ns)
and 16 MB (20.2 ns) up to DRAM latency. The red dotted lines mark what the
detector picked and the grey dashed lines mark sysctl.
Each thread chases its own private chain. While every thread's working set fits in L1, all thread counts overlap, because L1 is private. Per thread latency degrades once N times the working set exceeds the cluster's L2. At 4 MB per thread, a single thread is still in cache at 7.2 ns, while 5 threads see 13.9 ns and 10 threads see 23.9 ns. At 8 MB per thread even 2 threads have left L2 (16.3 ns against 9.2). By 64 MB per thread everyone is in DRAM and the curves converge.
Two threads each fetch_add their own counter, placed sep bytes apart. At 8,
16 and 32 B the counters share a coherence unit, so every increment waits for
ownership and costs 5.0 -> 5.4 ns. From 64 B on they stop interfering and sit
at 1.59 ns, flat out to 1 KB. The drop happens at 64 B, not at the 128 B that
hw.cachelinesize reports.
This is a random chase over a fixed number of slots spaced stride bytes
apart. It's the textbook line size experiment, but with random ordering it
can't see the line size. The footprint grows with the stride, so the curve
really shows L2, then the system cache, then DRAM being exceeded in turn. It
stays in as a footprint sweep, and the line size comes from the false sharing
test instead.
To settle whether that unit is 64 B or the full 128 B line, a second run fixes
writer A at byte 64 of a 128 B aligned line and scans writer B across every 8 B
offset from 0 to 248, both doing fetch_add:
| offset of writer B | ns per increment |
|---|---|
| 0 to 56 | 1.59 |
| 64 to 120 | 5.2 to 6.4 |
| 128 to 248 | 1.59 |
B at 0 to 56 sits in the same 128 B line as A and does not interfere at all.
Interference is confined to the 64 B block that holds A, and stops again at
128. The unit the cores fight over is a 64 B block aligned at 64, inside the
128 B line sysctl reports.
Random stores into a working set of S bytes, one store per node in a shuffled order read from a precomputed index, so the index reads are sequential and prefetched and the timed loop is the stores. Reported as ns per store, which is throughput, not latency: a store never blocks the core the way a dependent load does, it goes into the store buffer and retires when the line arrives.
| working set | ns per random store | ns per dependent load |
|---|---|---|
| 4 KB to 1 MB | 0.69 | 0.68 to 6.1 |
| 2 MB to 16 MB | 0.88 to 0.94 | 6.1 to 19 |
| 32 MB | 1.7 | 40 |
| 64 MB to 256 MB | 4.2 to 4.4 | 80 to 105 |
The L1 edge does not exist for stores: a random store into L2 costs the same as one into L1 because the store buffer absorbs the write-allocate miss. Even at DRAM a store is 25x cheaper than a load (4.3 against 105 ns). If a store miss takes as long as a load miss, Little's law puts about 24 misses in flight (105 / 4.3); the true number is lower if store misses are cheaper, and either way only bandwidth is left to pay for.
Where the floor and the 2 MB step come from, probed one variable at a time
(probes/store_cost_probe.cpp):
| probe | result | why |
|---|---|---|
| random lines vs one repeated line, 16 KB | 0.67 vs 0.67 ns | the floor is the index-dependent store, about 3 cycles, not the cache |
| sequential stores, no index load | 0.28 ns | removing the index load removes the floor |
| 1 MB of data, index array 32 KB to 4 MB | 0.69 to 0.77 ns | the index stream is not the 2 MB step |
| 8192 lines over 64 to 1024 pages | 0.69 ns flat | pages are not the step either |
| 4K, 8K, 12K, 16K, 24K, 32K lines | 0.69, 0.69, 0.72, 0.87, 0.86, 0.85 ns | the step follows the count of distinct lines, between 12K and 16K |
| read chase, same sizes | steps at 3 to 4 MB, not 2 MB | a store-path structure, not the L2 itself |
| one line per page, 2048 vs 4096 pages | 0.68 vs 1.34 ns | second-level TLB reach is about 2048 pages, 32 MB |
| all lines in one cache set | 2.2 to 4.2 ns | set conflicts serialise misses even when the data is tiny |
The 2 MB step is a store side effect with about 2 MB of reach per core: it follows the count of lines written, not pages, not the index, not the L2 read path. Whether that reach is counted in lines or in bytes was not tested, the step is 20 -> 25 percent and varies between runs, and naming the structure would need hardware counters. The earlier version of this experiment added the store to the load chain and subtracted the read latency; it read zero everywhere, which is the same fact seen from the other side: stores only become visible when the store buffer itself is the bottleneck.
The chain touches every line of its working set exactly once per cycle, in random order, and macOS hands out physical pages at random. The L2 is set associative, and the set index bits above the 16 KB page offset come from the physical page number. That makes the number of lines landing in any one set a binomial draw rather than a constant. At 8 MB the mean is half the ways and almost no set overflows. At 16 MB the mean equals the number of ways and ~half the sets overflow, which is why latency at exactly 16 MB is already 2.5x the L2 plateau.
So the transition is smeared across 8 MB -> 16 MB by construction. The detector reports the last point before the steepest step inside it, which lands at 8 -> 11 MB from run to run. Page colour aware allocation would sharpen it. In practice, budget 60 -> 70% of nominal L2 for any randomly accessed structure.
hw.cachelinesize says 128 B, which is the L2 line. The false sharing cliff
says two cores stop interfering once their counters are 64 B apart, so the
unit the coherence protocol moves between cores is 64 B. Both numbers are
right; they describe different levels. The chase nodes are 128 B aligned,
which also makes them 64 B aligned, so the measurements aren't affected. 64 B
separation is enough on this chip, and 128 B is the portable choice.
In the order main calls them.
Measurement measure_working_set(i64 bytes) and SweepResult cache_size_detection()
allocate bytes / 128 nodes, each alignas(128) so that one node is one line,
and link them into a single random cycle with a Sattolo shuffle. They walk the
cycle once untimed to pay for page faults and cold misses, then time
p = p->next for 10M -> 50M accesses and keep the minimum of 5 trials.
Interference can only make a run slower, so the minimum is the estimate closest
to the hardware. The sweep runs 4 KB -> 256 MB, doubling each step.
void refine_size_sweep(SweepResult& r) finds the cliffs in the doubling sweep
and measures three geometric midpoints inside each one, which places every
boundary to within ~a quarter of an octave.
SweepResult cache_line_size_detection(const AppleSystemInfo&) holds the slot
count at 4 x L2 / 128 and doubles the stride over 4 B -> 2 KB, chasing a
Sattolo successor table baked into the slots. Because the slot count is fixed,
the footprint grows with the stride, and that's what the curve shows.
SweepResult cache_write_throughput()
Fills S / 128 nodes, shuffles their indices with sattolo_cycle, warms with
one untimed pass, then times total_accesses(count) random stores through that
index and keeps the minimum of 5 trials. Nothing in the loop depends on a
store, so the number is ns per store at full store-buffer overlap.
SweepResult cache_contention(const AppleSystemInfo&) runs T threads for T in
{1, 2, cores per L2, all P cores}. Each thread builds a private chain of S
bytes, for S over 64 KB -> 64 MB, and requests the user interactive QoS class
so the scheduler places it on a P core. Each of the 3 timed trials starts
behind a spin barrier so the timing windows overlap. It reports the mean over
threads of each thread's minimum.
SweepResult cache_false_sharing() has two threads fetch_add their own
std::atomic<u64> at offsets 0 and sep inside a line aligned buffer, with
sep doubling over 8 B -> 1 KB. Trials are barrier aligned, and it takes the
minimum of 5 and the mean over the two writers. It uses fetch_add rather than
a plain store because the store buffer hides plain stores completely.
Detected detect_geometry(const SweepResult& size, const SweepResult& false_sharing)
is a pure function over the curves. On the power of two points, the L1 cliff is
the first doubling step with a ratio of at least 1.4, and the L2 cliff is the
first such step after the L1 cliff ends. Inside each cliff, the boundary is the
last point before the steepest refined step, and the point after it is reported
too. The coherence unit is the separation just after the largest drop in the
false sharing curve. It's unit tested on synthetic curves and asserted against
the live machine in the slow suite.
The measured binary is built with -O2 -g and without NDEBUG, so the
asserts are live. The run the numbers above come from was made before a stale
Release build type was cleared from the build directory, so its asserts were
compiled out; the timed loops contain no asserts, so no number changes.
If they aren't installed yet:
brew install cmake googletest
python3 -m pip install matplotlib pandasBuild:
cmake -S . -B build && cmake --build buildBuild with -O2, which is what the CMake file sets. A full run under -O3
was compared sweep by sweep against -O2 and every measured latency came out
the same within run to run noise, because the timed loops are dependency
chains that no optimisation level can shorten; -O3 only saved 8 s of untimed
setup, and -O2 keeps the timed loops closer to what is written. If your
build directory was ever configured with a build type, clear it once with
cmake -S . -B build -DCMAKE_BUILD_TYPE= so NDEBUG is not defined and the
asserts stay live.
Run every sweep from the repo root (~1.5 mins):
./build/cache_benchRun a single sweep. The names are size, line, write, contention,
false_sharing and false_sharing_scan:
./build/cache_bench false_sharingRender every CSV to docs/*.png:
python3 plot.py --save docsShow one plot in a window:
python3 plot.py contention.csvUnit tests (~2 s):
ctest --test-dir build -LE slow --output-on-failureFull sweeps under GoogleTest (~2 mins):
ctest --test-dir build -L slow --output-on-failure- Apple Silicon only.
node.hrefuses to compile anywhere else. - macOS has no hard core pinning, so the scheduler decides how threads spread across the two P clusters. The 5 thread contention numbers can straddle clusters.
sysctldoesn't expose the SLC size. It's reported elsewhere as 128 MB, but this tool doesn't confirm that.- E cores aren't measured.
- The 2 MB store step is characterised but its hardware cause is not identified.





