From 1eaae45ea6a769c1c939922e6143a764ca13d108 Mon Sep 17 00:00:00 2001 From: Nick233x <361370614+Nick233x@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:08:06 +0100 Subject: [PATCH] feat(gpu): report how long threads are held by host GPU work, by what ran it A title thread that submits, signals or waits on a queue, calls a fence or presents runs the held work that may run then (crate::hold), the host submissions with their preparation, GPU wait and copy-back included. It goes on with its frame only after that. The 5 s frame report now gives, per frame and per trigger, the milliseconds spent waiting for the queues' lock and running the work, so a world run shows how much of a frame the title spends held there rather than on its own work: the figure that says whether submitting without waiting would pay. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/host.rs | 13 +++++ crates/victory-gpu/src/queue.rs | 94 +++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 5d7ad469..d9a58a5a 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -4301,6 +4301,19 @@ impl Meter { crate::texture::ALIAS_COPIES.swap(0, Ordering::Relaxed) as f64 / report_frames, crate::twin::COPIES.swap(0, Ordering::Relaxed) as f64 / report_frames, ); + // How long the threads that ran held work were held by it: a + // title thread waits there instead of going on with its frame. + let ms = |us: u64| us as f64 / 1e3 / report_frames; + let held: Vec = crate::queue::WHY_NAMES + .iter() + .zip(crate::queue::take_blocked()) + .map(|(why, [lock, run])| format!("{why} {:.1} + {:.1}", ms(lock), ms(run))) + .collect(); + tracing::info!( + "threads held by host GPU work per frame, ms waiting for the queues' lock + \ + running it: {}", + held.join(", ") + ); #[cfg(feature = "diagnostics")] { if let Some([(local, budget), (system, system_budget)]) = video_memory() { diff --git a/crates/victory-gpu/src/queue.rs b/crates/victory-gpu/src/queue.rs index 7a71c08d..75fb5a28 100644 --- a/crates/victory-gpu/src/queue.rs +++ b/crates/victory-gpu/src/queue.rs @@ -120,7 +120,7 @@ extern "system" fn execute(this: This, count: u32, lists: *const This) { if crate::host::host().is_some() { // SAFETY: the title passes `count` lists. let lists = unsafe { std::slice::from_raw_parts(lists, count as usize) }; - let mut held = held(); + let mut held = held_for(Why::Submission); for &l in lists { for segment in crate::list::segments(l) { let waits = segment.waits(); @@ -164,10 +164,61 @@ fn held() -> std::sync::MutexGuard<'static, Holding> { HELD.lock().unwrap_or_else(|e| e.into_inner()) } +/// Microseconds the threads that ran held work spent on it, by what ran it +/// ([`Why`], in its order): `[0]` waiting for the queues' lock, `[1]` in +/// the pump, the host submissions included. The thread that called is held +/// for both: a title thread for all but [`Why::Timer`], which is ours. The +/// frame report divides them by its frames ([`take_blocked`]). +#[cfg(windows)] +static BLOCKED_US: [[AtomicU64; 2]; 6] = [const { [const { AtomicU64::new(0) }; 2] }; 6]; + +/// [`held`] for a caller that runs held work for `why`, its wait for the +/// lock counted in [`BLOCKED_US`]. +#[cfg(windows)] +fn held_for(why: Why) -> std::sync::MutexGuard<'static, Holding> { + lock_timed(&HELD, &BLOCKED_US[why as usize][0]) +} + +/// Locks `lock`, adding the microseconds it waited to `waited`. +#[cfg(any(windows, test))] +fn lock_timed<'a, T>( + lock: &'a std::sync::Mutex, + waited: &AtomicU64, +) -> std::sync::MutexGuard<'a, T> { + let asked = std::time::Instant::now(); + let guard = lock.lock().unwrap_or_else(|e| e.into_inner()); + waited.fetch_add(asked.elapsed().as_micros() as u64, Ordering::Relaxed); + guard +} + +/// [`Why`]'s variants in their order, as the frame report names them: the +/// title's submissions, its queues' Signals and Waits, its CPU fence calls, +/// its presents, and our timer thread. +#[cfg(windows)] +pub(crate) const WHY_NAMES: [&str; 6] = [ + "title submissions", + "queue signals", + "queue waits", + "fence calls", + "presents", + "our timer", +]; +#[cfg(windows)] +const _: () = assert!(Why::Timer as usize + 1 == WHY_NAMES.len()); + +/// [`BLOCKED_US`] since the last call, cleared: for each [`Why`], the +/// microseconds waiting for the queues' lock and in the pump. +#[cfg(windows)] +pub(crate) fn take_blocked() -> [[u64; 2]; 6] { + BLOCKED_US + .each_ref() + .map(|w| w.each_ref().map(|a| a.swap(0, Ordering::Relaxed))) +} + /// Runs held work that may run now (`why` for the frame line). pub(crate) fn pump_held(why: Why) { #[cfg(windows)] - pump(&mut held(), why); + pump(&mut held_for(why), why); #[cfg(not(windows))] let _ = why; } @@ -190,8 +241,17 @@ pub(crate) fn pump_held_if_free(why: Why) { #[cfg(windows)] const VALVE: std::time::Duration = std::time::Duration::from_secs(10); +/// Runs what held work may run now (`why`), its time counted in +/// [`BLOCKED_US`]. #[cfg(windows)] fn pump(held: &mut Holding, why: Why) { + let entered = std::time::Instant::now(); + pump_work(held, why); + BLOCKED_US[why as usize][1].fetch_add(entered.elapsed().as_micros() as u64, Ordering::Relaxed); +} + +#[cfg(windows)] +fn pump_work(held: &mut Holding, why: Why) { if held.is_empty() { return; } @@ -382,7 +442,7 @@ extern "system" fn signal(this: This, fence: This, value: u64) -> HRESULT { } #[cfg(windows)] { - let mut held = held(); + let mut held = held_for(Why::Signal); if held.holds_work(this as usize) { crate::fence::keep(fence); held.push(this as usize, Item::Signal(fence as usize, value)); @@ -409,7 +469,7 @@ extern "system" fn wait(this: This, fence: This, value: u64) -> HRESULT { } #[cfg(windows)] if crate::host::host().is_some() && crate::fence::value(fence) < value { - let mut held = held(); + let mut held = held_for(Why::Wait); held.push(this as usize, Item::FenceWait(fence as usize, value)); pump(&mut held, Why::Wait); } @@ -881,6 +941,32 @@ mod tests { use super::*; use crate::object::slot; + /// A lock another thread holds is waited for, and the wait is counted + /// in microseconds; a free lock adds next to nothing. + #[test] + fn a_timed_lock_counts_its_wait() { + let lock = std::sync::Mutex::new(()); + let waited = AtomicU64::new(0); + drop(lock_timed(&lock, &waited)); + assert!(waited.load(Ordering::Relaxed) < 5_000, "a free lock"); + let (taken, take) = std::sync::mpsc::channel(); + std::thread::scope(|s| { + s.spawn(|| { + let _held = lock.lock().unwrap(); + taken.send(()).unwrap(); + std::thread::sleep(std::time::Duration::from_millis(30)); + }); + take.recv().unwrap(); + let before = waited.load(Ordering::Relaxed); + drop(lock_timed(&lock, &waited)); + let counted = waited.load(Ordering::Relaxed) - before; + assert!( + counted >= 20_000, + "waited {counted} us for a lock held 30 ms" + ); + }); + } + /// Which plane gets shown: the one `VICTORY_PRESENT_PLANE` names when /// it names one, else the plane whose first resource has the most /// pixels. It only weighs the areas it is handed, so it runs on every