Repository navigation
jitv2: per-CPU page generation counters remove the per-store shared RMW - #943
Samueltgg12 wants to merge 1 commit into
Conversation
A jitv2 build paid ~10% on every guest store from a shared gen[phys >> 12].fetch_add(1, Release), which also ping-ponged the cache line between guest CPUs and compile workers. Each 4KB page now has 16 counters: slot 0 for bus/DMA/monitor writes, slots 1..8 for the guest CPUs. A CPU store bumps only its own slot with a plain Release store (no atomic RMW, single writer); PhysicalCodePage::current_gen() takes the max of all slots with Acquire, so any CPU's or a device's write is observed before code is published. CodePage::gen_slots bounds the read (16 for RAM, 1 for ROM/MMIO); NEVER_COMPILABLE_GEN is an array. ppMem GEN_RATIO 512 -> 32 (16 counters per page), GEN_WINDOW_SIZE 128MB; all window map/scrub/mark sites and the JIT inline store's gen index scaled by GEN_SLOTS; MipsCore::cpu_id selects the slot. bump_gen_all bumps every slot; install_jit_mem_ptrs stamps each cache's CPU id. SMP-safe: single-writer slots + Release/Acquire + max over all CPUs. cargo check (jitv2, +rex-jit/gr4-jit/gr2-jit/wgpu/gputrace) clean; ppmem 51 passed; jitv2 523 passed. Not yet guest-measured.
|
PhysicalCodePage::current_gen() reads all gen_slots counters with Acquire and returns their max. Any CPU's or a device's write is therefore observed before a code snapshot publishes. Acquire pairs with the store's Release so the compile sees the preceding data bytes. this seems very much incorrect. |
|
@techomancer looks good but it fails atleast 28 tests. fix it and change whatever you want... the nutlb PR probably didn't cause more tests to fail tho... also fix it and change whatever you want in it... |
|
here is more critique from the LLM, this is pretty bad, provable correctness problems (see 1.) and most likely serious performance issues too. 1. Taking the max misses invalidations, even with one CPU. Each slot counts only its own writer's stores, so the slots drift apart. Example: page P has CPU 0's slot at 100 and CPU 1's slot at 5. P compiles with 2. CPUs still share a cache line for the counters, so there is no SMP win. The counters are interleaved per page ( 3. It slows down dispatch. 4. Broken tests and a latent bug.
If someone wants to try the per-store gen cost again, it needs to start from a guest measurement that shows the cost exists. Any new layout needs a per-writer plane ( |
|
ok, i will do a remodelling then. |
|
i dont think remodeling will help, it doesnt seem worth pursuing. |
Summary
Follow-up to the dispatch-gate shared-write work. Removes the ~10% per-store generation tax in jitv2 builds, keeping SMP correctness.
A jitv2 build with no code compiling was ~10% slower than a plain
tcachebuild. The tax wasgen[phys >> 12].fetch_add(1, Release)on a shared per-pageAtomicU64, bumped on every guest store (interpreter cache, tcache, JIT inline store), on all guest CPUs plus compile workers touching the same cache lines.Design
GEN_SLOTS = 16counters: slot 0 = external (bus writes with no guest CPU: DMA, device, monitor), slots 1..=8 = one per guest CPU (MAX_CPUS = 8).load(Relaxed) + store(Release)— single writer, no atomic RMW, no shared-line ping-pong. Device/DMA writes keepfetch_add(Release)on slot 0.PhysicalCodePage::current_gen()reads allgen_slotscounters withAcquireand returns their max. Any CPU's or a device's write is therefore observed before a code snapshot publishes. Acquire pairs with the store's Release so the compile sees the preceding data bytes.CodePage::gen_slotsbounds the read: 16 for RAM (CodeBank::page), 1 for ROM/MMIO (CodeKey::flat_page);NEVER_COMPILABLE_GENis an array of 16. No out-of-bounds read on single-counter devices.bump_gen_all()(restore/power-on) bumps every slot of every page so all CPUs see the invalidation.GEN_RATIO = 4096 / 16 / 8 = 32,GEN_WINDOW_SIZE = 128 MB; all window map/scrub/mark sites and the JIT inline store index are scaled byGEN_SLOTS;MipsCore::cpu_idselects the slot.install_jit_mem_ptrsstamps each cache's CPU id.atomic_genis no longer used for the generation bump (still used byemit_sync).SMP safety
Per-CPU slots are single-writer (one guest CPU thread per
cpu_id), so no data race. Cross-thread visibility is Release-store / Acquire-load;current_gen()'s max means a write by CPU 3 is seen by a compile triggered on CPU 0. Device writes on slot 0 are included. No per-page counter is reset by a CPU store. Safe for 4-vCPU Fuel/Tezro; please verify on the Tezro.Verification
cargo check -p iris --features jitv2clean; also withrex-jit,gr4-jit,gr2-jit,wgpu,gputrace.cargo test -p iris --lib --features jitv2 ppmem::→ 51 passed;jitv2::→ 523 passed. Not yet guest-measured — please runiris-bench/gltest --benchand compare jitv2 gate-off vs atcache-no-jitv2 build, plus an SMP IRIX boot.Ranked follow-ups (not here)
current_gen()now issues up to 16 loads on any dispatch that asksis_runnable(); this is a new cost to measure against the removed RMW. Store-heavy guests should win, dispatch-heavy ones may lose. If needed, read onlyMAX_CPUS + 1 = 9slots (keep 16 in the window).tc_bump_genSMP RMW-vs-stale-code dilemma is resolved here by single-writer slots + max-read.clanker/jitv2/per-cpu-page-generation.md+clanker/INDEX.md; CHANGELOG### jitv2 memory.