Repository navigation
Size the copy cell's footprint, not one of its two buffers - #10
Conversation
A copy cell walks a source and a destination, so its footprint is two buffers. Every level planner sized ONE buffer at the level's per-thread window, which put twice the working set through the cache the row is named after: 100% of L1d, 100% of L2, 150% of L3. A streaming footprint 1.5x a cache measures DRAM, because each line is evicted just before it is reused, and the grid printed it under 'L3 Cache' (13.8 GB/s against 213.9 read on a 25 MiB part; 74.7 GB/s in this README's own grid against 329.9 read). The cell's own note printed 'x 2 buffers, 37.5M in total' beside '1.50x L3' and oversized_note still did not fire, because it was handed one buffer's size. Also fixes the L3 thread loop arguing 'footprint >= 1 x L2' while the floor enforced 2 x L2, which let the floor lift the aggregate above the window in silence; it now sheds to the floor it enforces, and says which gave way when both cannot be satisfied. l1/l2/l3 take the buffer count and size the footprint; for_level_buffers is the entry point. The memory row keeps two full-size buffers on purpose (no residency claim) and says so. PLAN.md's 'per-thread buffer' column, which is where the bug was authorised, now says 'working set'. The C reference had the same conflation (it doubles in its notes but not in its sizing) and now applies the same rule, so the gate compares like with like: L1 copy moved from -54.5% to within a few percent of the reference, L3 copy from +10.3% to -5.6%, and cells over the gate from 6 to 3 of 16. --dry-run prints the copy split. Measured on this host, three interleaved pairings old against new: L1 copy +1.2%, L2 copy +47.1%, L3 copy +125.0%; every read and write cell within its noise, because their plans did not change.
The CI failure on the previous commit was this script, not the change: it exited 2 with 'no assembly produced'. Investigating that turned out to be worse than a broken CI step. `--emit=asm` is a rustc flag, so cargo's freshness check does not know about it. When the crate is already built -- a second run in the same tree, or a restored CI rust-cache, which is every CI run -- cargo prints 'Finished in 0.03s', rustc never runs, and no assembly is written. The script looked in $OUT/release/deps, which a current toolchain does not write to at all: rustc puts the file under $OUT/build/.../out now. So the only files that path could ever match were leftovers, and a run with leftovers passed while scanning a file this run did not produce. A run without them failed, loudly, for a reason that had nothing to do with the commit. That is a gate which approves by reading history. It has been doing it while I was making changes to kernels in earlier rounds. Fix: touch the crate so rustc really runs, search the tree cargo writes, and accept only a file newer than this build, so 'no assembly' can never again mean 'an old one will do'. Verified from a clean tree and warm, twice in a row each time; 23 kernel labels, 0 copies outside our asm.
Second commit: the asm gate was approving on historyThe CI failure on the first commit here was not this change. It was
It now touches the crate so rustc really runs, searches the tree cargo actually writes, Pushed as 5f677c0. It is unrelated to the copy sizing except that the copy PR is what made CI disagree with itself. |
The copy cell was twice the cache it claimed to measure
What was wrong
A copy cell walks two buffers — a source and a destination. Every level planner sized
one buffer at the level's per-thread window (
0.5 × L1d,0.5 × L2,0.75 × L3 ÷ cores in the L3 domain), so a copy cell's footprint was twice the windowit was planned for: 100% of L1d, 100% of L2, and 150% of L3.
A streaming footprint 1.5 × a cache does not measure a smaller version of that cache. It
measures DRAM, because with near-LRU replacement each line is evicted just before it is
reused — overshoot is a cliff, not a slope. The grid printed the result under
L3 Cache.The plan machinery knew the pair existed:
total_bytesmultiplied bybuffers_per_threadfor progress, and the buffer note printed "× 2 buffers". Butoversized_notewas handed the size of a single buffer, so the one warning that shouldhave fired never could.
plan_for's comment said "the plan already accounts for that".It did not.
The evidence
1. The tool's own metadata, on a Supermicro X9 box (E5-2680 v2, 10 cores, 25 MiB
L3, one NUMA node, SSE2 path) — the first machine with a small enough L3 per core to
notice:
13.8 GB/s against a memory copy of 11.2 GB/s on the same run.
2. A falsifiable prediction. The pair's aggregate is
2 × 0.75 × L3whatever thethread count, because the per-thread share is
0.75 × L3 ÷ threads. Capacity says thecell is flat in threads; a kernel defect or a threading problem says it is not. Measured
on that box:
Flat against a neighbour that scaled 11×.
3. The README's own machine (Ryzen 5 PRO 5650G, 16 MiB L3) says the same: the copy
cell's footprint is 1.50 × L3 and it printed 75.0 GB/s beside a read of 353.9 GB/s. That
74.7 figure was in the README's headline grid.
4. The traffic arithmetic closes. Copy moves about three bytes of DRAM traffic per
counted byte (read source, fetch destination for ownership, write back): memory copy
11.2 × 3 = 33.6 GB/s, against a measured 33.8 GB/s for non-temporal write. The "L3" copy
cell's 13.8 × 3 = 41 GB/s sits at the 39.1 GB/s DRAM read ceiling. It was not
partly cache-resident; it was not an L3 cell.
The second bug, found in the same function
The L3 thread-shrink loop stopped at
footprint ≥ 1 × L2while the floor enforced2 × L2. The loop therefore kept a thread count whose share was then lifted by thefloor, silently, above the window the level was sized for. On a fixture with 8 cores, a
16 MiB L3 and 2 MiB L2s it settled on 6 threads × 4 MiB = 24 MiB through a 16 MiB
cache for a plain read cell. The loop now argues about the same quantity it enforces,
and takes 3 threads × 4 MiB = 12 MiB = the window, with no note required. Where a part
is so small that no footprint both fits the window and clears the floor, the plan says
which of the two gave way instead of printing a number that quietly means something
else.
The fix
Size the footprint — buffers × bytes — and divide into per-buffer sizes on the way
out.
l1,l2andl3now takebuffers;for_level_buffersis the entry point andfor_levelis the one-buffer case. Halving after the planner would have left the floorand the thread loop where they were, which is the wrong place: the cache sees the pair,
so the floor (
2 × L2) is a footprint floor — two buffers of one L2 each still both fitin L2.
The memory row keeps two full-size buffers, deliberately: it makes no claim to fit
anywhere, it is sized to clear the whole L3 four times over, and halving it would buy
nothing but a smaller resident set. Its derivation says the copy cell holds two.
The note that says
part of this cell is memory trafficsaid too little. A cell whosefootprint overshoots is memory-bound; that is what it says now.
A copy plan whose footprint fits gets no note, because there is nothing to apologise
for.
--dry-rungained a copy line per row, so the plan can be read before it is paidfor:
What it does to the numbers
Pre-fix binary against this one, three interleaved pairings (arms alternating, not
grouped), same host,
--trials 5:The read and write cells' plans did not change, so their −4%…+2% spread is this box's
noise floor — which makes the copy jumps 30 to 100 times the drift they are sitting in.
The L1 pair filled L1d completely and cost 1%; the L2 pair cost 47%; the L3 pair cost
125%. The damage scales with how much the level is shared, which is what a capacity
effect should do.
Why the gate did not catch this
reference/is an independent C implementation with no shared code, and its gate is10% per cell. It could not catch this because both sides sized a copy cell one buffer
at a time: a gate between two implementations that share a rule cannot test the rule.
L3 copy sat inside the gate on this host for three pairings running (+6.5%, −1.8%,
+4.2%) while both programs measured DRAM. What caught it was a machine nobody had run
the tool on.
The reference now applies the same rule (it already multiplied by two in its notes,
just not in its sizing). Re-pairing on this host afterwards: L1 copy −1.6 to −7.3%, L2
copy −11 to −14.4%, L3 copy −4.2 to −5.6%, and cells over the gate 3 of 16 in each of
three pairings against 6 in a pre-fix pairing taken for this change. The two programs
agreeing better is how a shared mistake becomes visible afterwards; it is no way to find
one.
Notes for review
buffer_bytes_per_threadof a copy cell is now half what it was, andcomparewillshow a large delta against pre-fix JSONs. That is a real measurement difference, and
the derivation string says why.
the level's whole window. There is a test for exactly that, because the first version
of this patch halved it and the test caught it.
PLAN.mdsaid "Per-thread buffer" in its sizing table, which is where the bug wasauthorised. It now says "working set" and states the pair rule.
window and the floor are both footprint quantities), so no row in the grid silently
changes thread count under the copy column.
both implementations, so every load/store pair shares bits 11:0 and stalls on Intel's
4K-aliasing rule. L1 copy is where that would show, and it would be a second,
separate effect on the same cell. It needs its own before/after, not a ride-along.
inline) before patching. It confirmed the capacity reading, proposed sizing the
footprint inside the planner rather than halving afterwards — which is what landed —
and found the loop/floor mismatch above. Its suggestion to "re-run at 6 threads" was
wrong: the aggregate is thread-invariant, which is the basis of test 2 instead.