From 5904807b3b5e063150078932c7c9e7bf6a356c3c Mon Sep 17 00:00:00 2001 From: James Braid Date: Thu, 19 Nov 2026 03:12:16 -0700 Subject: [PATCH] jitv2: stop the idle compile worker polling every 200us handle_request_deferred counts a compile whose range is not sealed yet in `pending`. When a later compile's seal attempt released that range, publish_all published it but `pending` stayed up, and the idle sweeps only subtract what they seal themselves. With nothing left in the seal queue `pending` never reached 0, so the worker kept the 200us publish backstop instead of the 20ms idle one. Subtract the entries a finalize releases other than the compile's own. At the Indy PROM menu the worker sat at pending=63 with an empty seal queue. Voluntary context switches of the compile thread over 10 s, on a loaded host, built-in PROM, headless: Indy 3770/s -> 49/s, O2 3763/s -> 49/s. The thread made 2 clock_gettime calls per wakeup. Fixes: 6d70615c7 ("multi threaded jit compiler") --- src/cpu/jitv2/comp.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/cpu/jitv2/comp.rs b/src/cpu/jitv2/comp.rs index c0309f2da..8de0b1890 100644 --- a/src/cpu/jitv2/comp.rs +++ b/src/cpu/jitv2/comp.rs @@ -836,9 +836,13 @@ pub fn handle_request_deferred( jit_fn: None, }; let sealed = codegen.finalize_batch_nonforced(func_id, publish); + // Entries this seal released were counted when they queued; uncount + // them, or `pending` never returns to 0 and the worker polls at 200us. + let own = codegen.finalized_fn_is_among(&sealed); + *pending = pending.saturating_sub(sealed.len() - own as usize); if codegen.last_finalize_failed() { page.mark_finalize_failed(); - } else if !codegen.finalized_fn_is_among(&sealed) { + } else if !own { // Own range still unsealed. `sealed` may be non-empty all the // same (this allocation vacated the previous host page and // released what waited there), so test for our own function, @@ -1333,6 +1337,31 @@ mod tests { assert!(pages.iter().all(|p| p.is_runnable(4)), "the forced sweep must leave every page published"); } + #[test] + fn handle_request_deferred_uncounts_entries_a_later_compile_released() { + // Entries released by another compile's seal attempt were counted + // when they queued. If they stay counted, the worker polls every + // 200us for good although nothing is left to publish. + let _stable = crate::cpu::jitv2::opcode_support::table_stable(); + let bus: Arc = Arc::new(AddiuDevice); + const N: usize = 64; + let counters: Vec = (0..N).map(|_| AtomicU64::new(0)).collect(); + let mut pages: Vec = counters.iter().enumerate() + .map(|(i, c)| PhysicalCodePage::new(i as u32, c as *const AtomicU64)) + .collect(); + let mut analyzer = Analyzer::new(); + let mut codegen = Codegen::new(); + let mut pending: PendingCount = 0; + for i in 0..N { + pages[i].mark_requested(4); + let req = CompileRequest { page: &mut pages[i] as *mut PhysicalCodePage, compiled_for_fr1: true, cpu: 0 }; + handle_request_deferred_for_test(&req, &bus, &mut analyzer, &mut codegen, &mut pending); + } + assert!(pending <= 1, "only the compile still queued may stay counted, got {pending}"); + force_publish_pending(&mut codegen, &mut pending); + assert_eq!(pending, 0); + } + /// One thread stores into a page through `bus` (each `write32` bumps the /// page's generation after its bytes, as every RAM device does) while /// others take compile snapshots of it with `snapshot_page`, as compile