diff --git a/d3d12/crates/leonida-gpu/src/timeline/record.rs b/d3d12/crates/leonida-gpu/src/timeline/record.rs index 272ffc5e09..027ec0a7df 100644 --- a/d3d12/crates/leonida-gpu/src/timeline/record.rs +++ b/d3d12/crates/leonida-gpu/src/timeline/record.rs @@ -668,6 +668,12 @@ impl RollingBuffer { dropped_contention, } } + + #[cfg(test)] + fn has_event(&self, pred: impl Fn(&Event) -> bool) -> bool { + let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner()); + inner.events.iter().rev().any(pred) + } } /// One title-side `frame_ends` boundary in QPC ticks. @@ -2211,18 +2217,18 @@ mod tests { /// Under parallel `cargo test`, unrelated test threads may momentarily hold /// `ROLLING.inner`, causing an intentional non-blocking contention drop on a - /// single global producer call. Retry our synthetic producer call when the - /// contention counter advances so global-routing tests stay deterministic - /// without weakening the non-blocking producer path. - fn emit_global_without_contention(mut emit: impl FnMut()) { + /// single global producer call. Retry our synthetic producer call until the + /// matching event is retained in `ROLLING` (instead of polling the + /// process-wide `dropped_contention` counter that other threads advance). + fn emit_global_until(pred: impl Fn(&Event) -> bool, mut emit: impl FnMut()) { for _ in 0..64 { - let drops_before = ROLLING.dropped_contention.load(Ordering::Acquire); emit(); - if ROLLING.dropped_contention.load(Ordering::Acquire) == drops_before { + if ROLLING.has_event(&pred) { return; } std::thread::yield_now(); } + panic!("synthetic stall event was not retained in ROLLING after 64 attempts"); } #[test] @@ -2244,18 +2250,9 @@ mod tests { }; ROLLING.try_push(frame_event(start_mark)); - // 1. Span routed through global stall mode (spin > 50 us using QPC ticks, no sleep). - emit_global_without_contention(|| { - let _s = span("synthetic stall span"); - let until = ticks() + frequency() / 10_000; // 100 us - while ticks() < until { - std::hint::spin_loop(); - } - }); - - // 2. Real wrapped wait + wake across threads using a condvar & barrier. + // 1. Real wrapped wait + wake across threads using a condvar & barrier. let mut waker_tid = 0; - emit_global_without_contention(|| { + for attempt in 0..64 { let pair = Arc::new((Mutex::new(false), Condvar::new())); let waiting_ready = Arc::new(Barrier::new(2)); let worker = { @@ -2281,22 +2278,47 @@ mod tests { } } waker_tid = worker.join().unwrap(); - }); + if ROLLING.has_event(|e| e.kind == Kind::Wake && e.thread == waker_tid) + && ROLLING.has_event(|e| e.kind == Kind::Wait && e.b == 0) + { + break; + } + assert!(attempt < 63, "synthetic wake/wait events were not retained in ROLLING"); + std::thread::yield_now(); + } + + // 2. Span routed through global stall mode (spin > 50 us using QPC ticks, no sleep). + emit_global_until( + |e| e.kind == Kind::Span && e.name == "synthetic stall span", + || { + let _s = span("synthetic stall span"); + let until = ticks() + frequency() / 10_000; // 100 us + while ticks() < until { + std::hint::spin_loop(); + } + }, + ); // 3. CPU-side submission carrying fence ID 777 and a fence-linked wait. - emit_global_without_contention(|| { - let send_t0 = ticks(); - sent(send_t0, 777, None); - }); - emit_global_without_contention(|| { - let _card = card_wait(Some(777)); - let wait_t0 = ticks(); - let until = wait_t0 + frequency() / 10_000; // 100 us - while ticks() < until { - std::hint::spin_loop(); - } - waited("WaitForSingleObject", 0xfeed, wait_t0); - }); + emit_global_until( + |e| e.kind == Kind::Sent && e.a == 777 && e.b == 0, + || { + let send_t0 = ticks(); + sent(send_t0, 777, None); + }, + ); + emit_global_until( + |e| e.kind == Kind::Wait && e.a == 0xfeed && e.b == 777, + || { + let _card = card_wait(Some(777)); + let wait_t0 = ticks(); + let until = wait_t0 + frequency() / 10_000; // 100 us + while ticks() < until { + std::hint::spin_loop(); + } + waited("WaitForSingleObject", 0xfeed, wait_t0); + }, + ); let end_tick = ticks().max(start_tick + 1); let end_mark = PresentMark { @@ -2412,17 +2434,23 @@ mod tests { } // Produce a span and CPU submission through real global routing. - emit_global_without_contention(|| { - let _s = span("background writer span"); - let until = ticks() + frequency() / 10_000; - while ticks() < until { - std::hint::spin_loop(); - } - }); - emit_global_without_contention(|| { - let send_t0 = ticks(); - sent(send_t0, 512, None); - }); + emit_global_until( + |e| e.kind == Kind::Span && e.name == "background writer span", + || { + let _s = span("background writer span"); + let until = ticks() + frequency() / 10_000; + while ticks() < until { + std::hint::spin_loop(); + } + }, + ); + emit_global_until( + |e| e.kind == Kind::Sent && e.a == 512 && e.b == 0, + || { + let send_t0 = ticks(); + sent(send_t0, 512, None); + }, + ); let end_tick = ticks().max(start_tick + 200); let mut handle: Option> = None;