From 10cb2efa054b67cdeb0bbb018866933a56a42c53 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 5 Nov 2026 15:52:32 -0800 Subject: [PATCH] Trace a commit's way to its submission in the latency lane The runtime lane timed commit -> watched as one span: 140 to 165 us at the median on the RTX 5080 against 5 us for vkQueueSubmit. The trace now marks the steps between, in device-test builds only: the commit posted to the scheduler's inbox, the scheduler's thread woken by it and waking, the commit taken in by a pass, and the driver's submission called and returned (relay.rs submit_logged, which every batch's submission goes through). The lane commits a third shape, the completion-seen handler alone with commit_and_submit, between the other two, and prints each shape's commit call return and the split of its way to the watch. A measurement change only (an ignored test and test-only trace points). (cherry picked from commit 60905d60311824b51aaa077bd7baaa7227b095da) --- .../src/command_buffer/completion.rs | 11 ++ .../metal2vulkan-runtime/src/device/relay.rs | 4 + .../metal2vulkan-runtime/src/scheduler/mod.rs | 16 ++ .../src/tests/completion_latency.rs | 146 ++++++++++++++---- 4 files changed, 149 insertions(+), 28 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs index ac94cc844..137c56bf7 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs @@ -1252,6 +1252,9 @@ pub(crate) mod trace { Finished { queue: usize, value: u64, seen: u64 }, /// The turn that finished the batches reached step `what` of `finish`. Step { what: &'static str, at: u64 }, + /// A commit's way to its submission reached step `what`: posted to the scheduler, its + /// thread woken, taken in by a pass, the driver's submission called and returned. + Submitting { what: &'static str, at: u64 }, } static ON: AtomicBool = AtomicBool::new(false); @@ -1277,6 +1280,14 @@ pub(crate) mod trace { }); } + /// Keep the commit's step `what` on its way to its submission, now. + pub(crate) fn submitting(what: &'static str) { + record(|| Traced::Submitting { + what, + at: crate::gpu_time::host_nanos(), + }); + } + /// Keep the step `step` makes, while a test asks. pub(super) fn record(step: impl FnOnce() -> Traced) { if ON.load(Ordering::Relaxed) { diff --git a/crates/metal2vulkan-runtime/src/device/relay.rs b/crates/metal2vulkan-runtime/src/device/relay.rs index e333f0a44..7514d420a 100644 --- a/crates/metal2vulkan-runtime/src/device/relay.rs +++ b/crates/metal2vulkan-runtime/src/device/relay.rs @@ -400,9 +400,13 @@ impl super::DeviceShared { if let Some((fence, flights)) = logged { flights.push(fence, By::Batch(Arc::clone(failed))); } + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + crate::command_buffer::completion::trace::submitting("submit called"); let entered = crate::gpu_time::host_nanos(); let submitted = f(vk_queue); let end = crate::gpu_time::host_nanos(); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + crate::command_buffer::completion::trace::submitting("submit returned"); let nanos = end.saturating_sub(entered); (self.counts).batch_submitted_in(nanos); if submitted.is_ok() && period.called(Caller::Work, nanos, end) { diff --git a/crates/metal2vulkan-runtime/src/scheduler/mod.rs b/crates/metal2vulkan-runtime/src/scheduler/mod.rs index 7ec5f11d2..30d9eeaea 100644 --- a/crates/metal2vulkan-runtime/src/scheduler/mod.rs +++ b/crates/metal2vulkan-runtime/src/scheduler/mod.rs @@ -179,6 +179,8 @@ pub(super) mod left; pub(super) mod order; mod steps; +#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] +use super::command_buffer::completion::trace; use super::command_buffer::gate::Hold; use super::command_buffer::kept_barriers::KeptBarriers; use super::command_buffer::BatchLists; @@ -850,10 +852,16 @@ impl Scheduler { buffer.device().shared().counts.commit_handed_off(); if hand == Hand::Pass { shared.inbox.post_quietly(committed); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + trace::submitting("posted"); return; } if shared.inbox.post(committed) { + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + trace::submitting("posted"); shared.wake_thread(Woken::Commit, shared.nudge.get()); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + trace::submitting("woke the thread"); } } @@ -1215,6 +1223,8 @@ fn run(shared: &Arc) { shared.sleeps.fetch_add(1, Ordering::Relaxed); if let Some(nudge) = on_gpu { state = wait_for_gpu(shared, (state, sleep), nudge, &mut gpu, (slice, short)); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + trace::submitting("thread woke"); continue; } sleep.asleep = true; @@ -1236,6 +1246,8 @@ fn run(shared: &Arc) { }; sleep.asleep = false; drop(sleep); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + trace::submitting("thread woke"); if timed_out && !short { shared.wakings[Woken::Slice as usize].fetch_add(1, Ordering::Relaxed); } @@ -1402,6 +1414,10 @@ struct Passed { /// ([`SchedulerShared::took_in`]). fn take_in(state: &mut State, commits: &mut Vec, spent: &mut Spent) -> usize { let taken = commits.len(); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + if taken > 0 { + trace::submitting("taken in"); + } for Committed { buffer, queue, diff --git a/crates/metal2vulkan-runtime/src/tests/completion_latency.rs b/crates/metal2vulkan-runtime/src/tests/completion_latency.rs index 596c559d9..5f4eecc41 100644 --- a/crates/metal2vulkan-runtime/src/tests/completion_latency.rs +++ b/crates/metal2vulkan-runtime/src/tests/completion_latency.rs @@ -30,13 +30,15 @@ //! submission is the one the runtime's from the commit compares with. //! - **The runtime** (`runtime_completion_latency`): a device of the runtime's, one queue, each //! command buffer a blit encoder's `fill_buffer` with a completion-seen handler and a completed -//! handler that note the host time (the consumer's shape), and in turn with them every other -//! one with the completion-seen handler alone, committed and not waited for. From the GPU end -//! (`gpu_end_time`, on a device that calibrates its timestamps) to the completion-seen handler; -//! and the runtime's steps between, from its queue waiter's trace -//! (`command_buffer::completion::trace`): the batch watched, the driver's wait entered and -//! returned, the completion turn's `seen` (the command buffer's completion time) and the -//! handlers. +//! handler that note the host time, committed with `commit` (the consumer's shape), and in turn +//! with them one with the completion-seen handler alone, so committed, and one so committed with +//! `commit_and_submit`; none waited for. From the GPU end (`gpu_end_time`, on a device that +//! calibrates its timestamps) to the completion-seen handler; and the runtime's steps between, +//! from its trace (`command_buffer::completion::trace`): on the commit's way to the +//! `VkQueue`, the commit posted to the scheduler, its thread woken by the commit and waking, +//! the commit taken in by a pass, the driver's submission called and returned; then the batch +//! watched, the driver's wait entered and returned, the completion turn's `seen` (the command +//! buffer's completion time) and the handlers. //! //! Each line also counts the waits entered after the GPU end: there, the time to the wait's return //! holds the wake of the thread that waits, not the driver's wake alone. @@ -747,10 +749,11 @@ impl Drop for Scratch { } /// One of the runtime's command buffers as its handlers saw it: host nanoseconds of its commit -/// call, its completion-seen handler and its completed handler. +/// call and the call's return, its completion-seen handler and its completed handler. struct Committed { command_buffer: CommandBuffer, commit: u64, + committed: u64, seen: Arc>>, completed: Arc>>, } @@ -802,29 +805,51 @@ fn runtime_completion_latency() { } let committed = commit_timed(&fill, &mut next); let times = times_of(calibrated, &committed); - for (completed_handler, shape) in [ - (true, "seen and completed handlers"), - (false, "seen handler alone"), - ] { - let times: Vec<&Times> = (times.iter()) - .filter(|t| t.completed.is_some() == completed_handler) - .collect(); - print_runtime(&name, shape, calibrated, ×); + for (index, shape) in SHAPES.iter().enumerate() { + let times: Vec<&Times> = (times.iter()).skip(index).step_by(SHAPES.len()).collect(); + print_runtime(&name, shape.name, calibrated, ×); } } -/// [`RUNS`] fills `fill` makes of each shape, in turn: each with a completion-seen handler, and -/// every other one first with a completed handler too, so the shapes meet the same host; committed -/// [`GAP`] apart from `next` on and waited for at the end, with the queue waiters' steps traced -/// (`trace`). +/// One shape of the runtime's command buffers (the module docs). +struct Shape { + name: &'static str, + /// It has a completed handler besides its completion-seen handler. + completed_handler: bool, + /// It is committed with `commit_and_submit`, else with `commit`. + and_submit: bool, +} + +/// The shapes, committed in turn. +const SHAPES: [Shape; 3] = [ + Shape { + name: "seen and completed handlers", + completed_handler: true, + and_submit: false, + }, + Shape { + name: "seen handler alone", + completed_handler: false, + and_submit: false, + }, + Shape { + name: "seen handler alone, commit_and_submit", + completed_handler: false, + and_submit: true, + }, +]; + +/// [`RUNS`] fills `fill` makes of each of the [`SHAPES`], in turn, so the shapes meet the same +/// host; committed [`GAP`] apart from `next` on and waited for at the end, with the runtime's +/// steps traced (`trace`). fn commit_timed( fill: &dyn Fn(u8) -> CommandBuffer, next: &mut Instant, ) -> (Vec, Vec) { trace::start(); - let mut committed = Vec::with_capacity(2 * RUNS); - for run in 0..2 * RUNS { - let completed_handler = run % 2 == 0; + let mut committed = Vec::with_capacity(SHAPES.len() * RUNS); + for run in 0..SHAPES.len() * RUNS { + let shape = &SHAPES[run % SHAPES.len()]; *next += GAP; let command_buffer = fill(run as u8); let seen = Arc::new(Mutex::new(None)); @@ -835,7 +860,7 @@ fn commit_timed( *noted.lock().unwrap() = Some(host_nanos()); })) .unwrap(); - if completed_handler { + if shape.completed_handler { let noted = Arc::clone(&completed); command_buffer .add_completed_handler(Box::new(move |_: &CommandBuffer| { @@ -844,10 +869,15 @@ fn commit_timed( .unwrap(); } let commit = host_nanos(); - command_buffer.commit().unwrap(); + match shape.and_submit { + true => command_buffer.commit_and_submit().unwrap(), + false => command_buffer.commit().unwrap(), + } + let returned = host_nanos(); committed.push(Committed { command_buffer, commit, + committed: returned, seen, completed, }); @@ -865,6 +895,11 @@ fn commit_timed( /// end where the device calibrates its timestamps. struct Times { commit: u64, + /// The commit call returned. + committed: u64, + /// The steps on the commit's way to its submission (`trace::submitting`), in order, between + /// its commit call and its batch's watch. + submitting: Vec<(&'static str, u64)>, watched: u64, entered: u64, returned: u64, @@ -929,9 +964,20 @@ fn times_of(calibrated: bool, (committed, steps): &(Vec, Vec) let (entered, returned) = waited(queue, value, turn).expect("a wait before the turn"); let gpu_end = calibrated .then(|| (each.command_buffer.gpu_end_time().unwrap() * 1e9).round() as u64); + let watched = watched(queue, value).expect("its batch watched"); + let submitting = (steps.iter()) + .filter_map(|step| match *step { + Traced::Submitting { what, at } if (each.commit..=watched).contains(&at) => { + Some((what, at)) + } + _ => None, + }) + .collect(); Times { commit: each.commit, - watched: watched(queue, value).expect("its batch watched"), + committed: each.committed, + submitting, + watched, entered, returned, turn, @@ -974,9 +1020,11 @@ fn print_runtime(name: &str, shape: &str, calibrated: bool, times: &[&Times]) { }; eprintln!( "COMPLETION-LATENCY runtime, {shape} ({name}): {} fills of {BYTES} bytes {GAP:?} apart, \ - between the other shape's: {ends}commit -> watched {} us; watched -> wait entered {} us; wait returned -> \ - turn {} us; turn -> seen handler {} us; {completed}commit -> seen handler {} us", + between the other shapes': {ends}commit -> commit returned {} us; commit -> watched {} us; \ + watched -> wait entered {} us; wait returned -> turn {} us; turn -> seen handler {} us; \ + {completed}commit -> seen handler {} us", times.len(), + of(&|t| us(t.commit, t.committed)), of(&|t| us(t.commit, t.watched)), of(&|t| us(t.watched, t.entered)), of(&|t| us(t.returned, t.turn)), @@ -1020,4 +1068,46 @@ fn print_runtime(name: &str, shape: &str, calibrated: bool, times: &[&Times]) { } } eprintln!("COMPLETION-LATENCY runtime turn, {shape} ({name}){split}"); + // The commit's way to its submission, each step from the one before it that the command + // buffer marked (the commit call first), and the last to its batch's watch: the first of each + // name after the commit call. Each over the command buffers that marked it. + const WAY: [&str; 6] = [ + "posted", + "woke the thread", + "thread woke", + "taken in", + "submit called", + "submit returned", + ]; + let mut way = String::new(); + for step in 0..=WAY.len() { + let to_name = WAY.get(step).copied().unwrap_or("watched"); + let spans: Vec = (times.iter()) + .filter_map(|t| { + let at = |name: &str| { + (t.submitting.iter()) + .find(|&&(what, _)| what == name) + .map(|&(_, at)| at) + }; + let to = match WAY.get(step) { + None => t.watched, + Some(name) => at(name)?, + }; + // The latest step before it that this command buffer marked, else its commit. + let from = (WAY[..step].iter().rev()) + .find_map(|name| at(name)) + .unwrap_or(t.commit); + Some(us(from, to)) + }) + .collect(); + match spans.is_empty() { + true => way.push_str(&format!("; -> {to_name} none")), + false => way.push_str(&format!( + "; -> {to_name} {} us ({} commits)", + line(spans.clone()), + spans.len() + )), + } + } + eprintln!("COMPLETION-LATENCY runtime commit, {shape} ({name}){way}"); }