From ebfc68ebb795843032fbdc64406fbbffaeda20e7 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 18 Nov 2026 16:26:52 -0800 Subject: [PATCH] rt: a timestamp pair read as not available after its submission signalled is counted, and every play and suite leg reports the count The read without the wait (f6906db61e) reports no times for a pair that is not available. Two things leave a pair that way once its submission signalled: a submission the device cancelled at a reset, which signals having run nothing, and a driver that makes timestamps available only after the signal (MoltenVK 1.4.2 on an M3 Max did, for most command buffers, which is why the read waited; MoltenVK is below the floor now). The second would zero GPU times with no other sign, so each such read is counted: Metrics::stamps_not_available, through Pair::times, the one counted read that CbInner::stamps makes. Reported where the matrix records it: - rt's device tests: `M2V-MATRIX metric /stamps_not_available ` from each device the device loop opens (tests::command::on_device::on_each_device); - the harness: `M2V-MATRIX metric harness-/device/stamps_not_available `, from the new Device::totals and Session::totals, before the session finishes (empty for the old runtime); - the replay player: the `stamps_not_available` counter in each frame's executed counters. The readback test now reads through Pair::times on counters of its own and checks that the read counts one, so the device's record stays that of its real command buffers. The specification read (Vulkan 1.4.366, sections 7.4 to 7.4.4, 23.2, 23.5 and the memory model's system-synchronizes-with): - "A query is made available by the operation of ... vkCmdWriteTimestamp2" (23.2); - the signal's first synchronization scope "includes every command in the same batch that the signal operation is defined in, and all commands that occur earlier in submission order" (vkQueueSubmit2); - "signal operations happening before host operations that wait on them" (memory model, the note under System-Synchronizes-With). So a pair is available to a read made after a host wait (vkWaitSemaphores) that saw its submission's value. The completion thread also retires values it learns from vkGetSemaphoreCounterValue alone (after a wait that returned for an event, for its wake or for an earlier value), and for that command the specification states no ordering: only that "the value returned by this command may immediately be out of date". The count covers both. Public surface: Metrics::stamps_not_available (api_signatures.tsv; signatures.py --check run here and clean). Not yet compiled or run. --- .../metal2vulkan-harness/src/bin/harness.rs | 3 ++ crates/metal2vulkan-harness/src/lib.rs | 9 +++++- crates/metal2vulkan-harness/src/old.rs | 5 +++ crates/metal2vulkan-harness/src/rt.rs | 7 +++++ crates/metal2vulkan-harness/src/run.rs | 7 +++++ crates/metal2vulkan-harness/src/tests.rs | 8 +++++ crates/metal2vulkan-rt/README.md | 5 ++- crates/metal2vulkan-rt/src/command.rs | 10 +++--- crates/metal2vulkan-rt/src/counters.rs | 15 ++++++++- crates/metal2vulkan-rt/src/readback.rs | 31 ++++++++++++------- crates/metal2vulkan-rt/src/tests/command.rs | 3 ++ crates/metal2vulkan-rt/src/tests/readback.rs | 20 +++++++----- crates/metal2vulkan-rt/src/times.rs | 25 ++++++++++++++- .../src/replay/new_runtime/mod.rs | 1 + crates/metal2vulkan/tests/api_signatures.tsv | 1 + 15 files changed, 122 insertions(+), 28 deletions(-) diff --git a/crates/metal2vulkan-harness/src/bin/harness.rs b/crates/metal2vulkan-harness/src/bin/harness.rs index 12b4fb925..ecd52989f 100644 --- a/crates/metal2vulkan-harness/src/bin/harness.rs +++ b/crates/metal2vulkan-harness/src/bin/harness.rs @@ -387,6 +387,9 @@ fn run_all(args: &Args) -> Result { } } for (runtime, session) in sessions { + for (total, value) in session.totals() { + println!("M2V-MATRIX metric harness-{runtime}/device/{total} {value}"); + } if let Some(gauges) = session.finish() { let list: Vec = (gauges.iter()) .map(|(gauge, value)| format!("{gauge} {value}")) diff --git a/crates/metal2vulkan-harness/src/lib.rs b/crates/metal2vulkan-harness/src/lib.rs index 941898024..28c1415f6 100644 --- a/crates/metal2vulkan-harness/src/lib.rs +++ b/crates/metal2vulkan-harness/src/lib.rs @@ -133,7 +133,9 @@ //! M2V-MATRIX metric harness-//_per_run //! ``` //! -//! and once every workload has run, where the runtime's gauges outlive its device, +//! and once every workload has run, the runtime's totals over the device's life +//! ([`Device::totals`]), `M2V-MATRIX metric harness-/device/ `, and where the +//! runtime's gauges outlive its device, //! `M2V-MATRIX gauge harness-/device/@drop ` read after the device dropped. //! A workload that fails prints `M2V-FAILED harness-/` beside its `FAILED` //! line, for the runtime it failed on ([`Failed`]), or for each runtime where it names none. @@ -245,6 +247,11 @@ pub trait Device: Sized { /// they end with it. No Metal counterpart. fn gauges_after_drop(&self) -> Option; + /// The runtime's counts over the device's whole life so far, by name, that the matrix holds + /// at 0: what went wrong without failing a call. Empty where it keeps none. No Metal + /// counterpart. + fn totals(&self) -> Vec<(&'static str, u64)>; + /// The OS ids of the threads the runtime started for the device and runs now, by its own /// account; empty where it gives none. The harness counts these and its own thread as the /// runtime's work, apart from the threads a driver starts for itself. No Metal counterpart. diff --git a/crates/metal2vulkan-harness/src/old.rs b/crates/metal2vulkan-harness/src/old.rs index f16e4ea0f..23b6cfc8a 100644 --- a/crates/metal2vulkan-harness/src/old.rs +++ b/crates/metal2vulkan-harness/src/old.rs @@ -58,6 +58,11 @@ impl crate::Device for runtime::Device { None } + /// None: the old runtime keeps no such count. + fn totals(&self) -> Vec<(&'static str, u64)> { + Vec::new() + } + /// None: the old runtime gives no account of its threads. fn threads(&self) -> Vec { Vec::new() diff --git a/crates/metal2vulkan-harness/src/rt.rs b/crates/metal2vulkan-harness/src/rt.rs index ddd5f9047..53555fb63 100644 --- a/crates/metal2vulkan-harness/src/rt.rs +++ b/crates/metal2vulkan-harness/src/rt.rs @@ -430,6 +430,13 @@ impl crate::Device for Device { Some(Box::new(move || named(counters.gauges()))) } + /// `Metrics::stamps_not_available`: command buffers whose GPU times read none though their + /// submission signalled. + fn totals(&self) -> Vec<(&'static str, u64)> { + let metrics = self.device.counters().metrics(); + vec![("stamps_not_available", metrics.stamps_not_available)] + } + /// `Metrics::pipelines_created`. fn pipelines_created(&self) -> u64 { self.device.counters().metrics().pipelines_created diff --git a/crates/metal2vulkan-harness/src/run.rs b/crates/metal2vulkan-harness/src/run.rs index ca3160d81..8cf8c9ccd 100644 --- a/crates/metal2vulkan-harness/src/run.rs +++ b/crates/metal2vulkan-harness/src/run.rs @@ -404,6 +404,9 @@ pub trait Session { /// gauges outlive the device. fn finish(self: Box) -> Option>; + /// The device's [`Device::totals`]. + fn totals(&self) -> Vec<(&'static str, u64)>; + /// The device's [`Device::pipelines_created`]. fn pipelines_created(&self) -> u64; @@ -1263,6 +1266,10 @@ impl Session for Harness { reader.map(|read| read()) } + fn totals(&self) -> Vec<(&'static str, u64)> { + self.device.totals() + } + fn pipelines_created(&self) -> u64 { self.device.pipelines_created() } diff --git a/crates/metal2vulkan-harness/src/tests.rs b/crates/metal2vulkan-harness/src/tests.rs index 8a692ca92..a30ecf8ce 100644 --- a/crates/metal2vulkan-harness/src/tests.rs +++ b/crates/metal2vulkan-harness/src/tests.rs @@ -947,6 +947,10 @@ impl Device for HostDevice { None } + fn totals(&self) -> Vec<(&'static str, u64)> { + Vec::new() + } + fn threads(&self) -> Vec { Vec::new() } @@ -1152,6 +1156,10 @@ impl Session for Answers { None } + fn totals(&self) -> Vec<(&'static str, u64)> { + Vec::new() + } + fn pipelines_created(&self) -> u64 { 0 } diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index a2271399a..9090907cd 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -822,7 +822,10 @@ that touch blocks first. - A command buffer that finds every pair of the device's timestamp pools taken, where the device then refuses a new pool, runs untimed: its `gpu_start_time` and `gpu_end_time` are 0, and `Metrics::untimed_command_buffers` counts it. Once the device refuses a pool it asks for none - again. Vulkan states no most queries a pool may hold or most pools a device may make; the + again. A command buffer whose pair reads as not available once its submission signalled has + times of 0 too, and `Metrics::stamps_not_available` counts it: a submission the device + cancelled at a reset, or a driver that makes timestamps available only after the signal. The + device tests, the harness and the replay player report the count. Vulkan states no most queries a pool may hold or most pools a device may make; the runtime's 4,096 a pool is the smallest met: Mesa's Apple-GPU driver makes each pool a Metal counter heap, of at most 4,096 entries and at most 32 live in a process, so there at most 65,536 command buffers of a process are timed at once, fewer where other devices of the diff --git a/crates/metal2vulkan-rt/src/command.rs b/crates/metal2vulkan-rt/src/command.rs index 82e2e9ecb..800993ebf 100644 --- a/crates/metal2vulkan-rt/src/command.rs +++ b/crates/metal2vulkan-rt/src/command.rs @@ -13,7 +13,6 @@ use crate::memory::Block; use crate::over::{self, Overs}; use crate::pipeline::ComputePipelineState; use crate::queue::QueueInner; -use crate::readback; use crate::record::{self, Reach, Recorder}; use crate::render::RenderPipelineState; use crate::ring::{Placed, Writes}; @@ -1226,12 +1225,13 @@ impl CbInner { lock(&self.state).ended = Some(error); } - /// Its start and end timestamps, once it completed having written both. + /// Its start and end timestamps, once its submission signalled having recorded both: none + /// where it took no pair, or where its pair reads as not available ([`Pair::times`]). pub(crate) fn stamps(&self, core: &Core) -> Option<(u64, u64)> { let pair = lock(&self.state).stamps?; - // SAFETY: the pair's pool holds its two queries, which only this command buffer's - // retirement resets, after this read. - unsafe { readback::timestamps(&core.device, pair.pool, pair.first) } + // SAFETY: the pair's pool is the device's, and only this command buffer's retirement + // resets its two queries, after this read. + unsafe { pair.times(&core.device, &core.counters) } } } diff --git a/crates/metal2vulkan-rt/src/counters.rs b/crates/metal2vulkan-rt/src/counters.rs index 13f0914c0..ce87d1051 100644 --- a/crates/metal2vulkan-rt/src/counters.rs +++ b/crates/metal2vulkan-rt/src/counters.rs @@ -81,10 +81,12 @@ pub(crate) enum Metric { UntimedCommandBuffers, /// [`Metrics::sampler_element_writes`]. SamplerElementWrites, + /// [`Metrics::stamps_not_available`]. + StampsNotAvailable, } /// The number of [`Metric`]s. -const METRICS: usize = Metric::SamplerElementWrites as usize + 1; +const METRICS: usize = Metric::StampsNotAvailable as usize + 1; /// Every [`Metric`], in its order: each at its own index. const METRIC_ORDER: [Metric; METRICS] = [ @@ -116,6 +118,7 @@ const METRIC_ORDER: [Metric; METRICS] = [ Metric::CaptureOverflows, Metric::UntimedCommandBuffers, Metric::SamplerElementWrites, + Metric::StampsNotAvailable, ]; // Each metric of the order is at its own index. @@ -274,6 +277,7 @@ struct Atomics { capture_overflows: AtomicU64, untimed_command_buffers: AtomicU64, sampler_element_writes: AtomicU64, + stamps_not_available: AtomicU64, capture_overflow_bytes: AtomicU64, capture_copy_nanos: AtomicU64, capture_wait_nanos: AtomicU64, @@ -372,6 +376,7 @@ impl Counters { Metric::CaptureOverflows => &a.capture_overflows, Metric::UntimedCommandBuffers => &a.untimed_command_buffers, Metric::SamplerElementWrites => &a.sampler_element_writes, + Metric::StampsNotAvailable => &a.stamps_not_available, } } @@ -669,6 +674,7 @@ impl Counters { capture_overflows: a.capture_overflows.load(Relaxed), untimed_command_buffers: a.untimed_command_buffers.load(Relaxed), sampler_element_writes: a.sampler_element_writes.load(Relaxed), + stamps_not_available: a.stamps_not_available.load(Relaxed), capture_overflow_bytes: a.capture_overflow_bytes.load(Relaxed), capture_copy_nanos: a.capture_copy_nanos.load(Relaxed), capture_wait_nanos: a.capture_wait_nanos.load(Relaxed), @@ -956,6 +962,13 @@ pub struct Metrics { /// differs from what the index's element holds, so an equal state made at the index of one /// released, while work that used it is pending, writes none. pub sampler_element_writes: u64, + /// Command buffers whose submission the completion thread saw signal its progress value while + /// their timestamp pair read as not available, so their GPU start and end times stay 0 + /// ([`CommandBuffer::gpu_start_time`](crate::CommandBuffer::gpu_start_time)). A driver whose + /// timestamps become available only after the signal it orders them before counts every such + /// command buffer here; a submission the device cancelled at a reset, which signals having run + /// nothing, counts too. + pub stamps_not_available: u64, } /// A thread's slot in [`Gauges::threads`], given back as it drops. diff --git a/crates/metal2vulkan-rt/src/readback.rs b/crates/metal2vulkan-rt/src/readback.rs index 86002db40..fcee0fcbf 100644 --- a/crates/metal2vulkan-rt/src/readback.rs +++ b/crates/metal2vulkan-rt/src/readback.rs @@ -9,18 +9,25 @@ use ash::vk; /// A completed command buffer's two timestamps, from its pair in the device's pools; None where /// the driver does not report both available, so the command buffer's GPU times stay 0. /// -/// The read does not wait (no `VK_QUERY_RESULT_WAIT_BIT`). The caller reads a pair only once the -/// completion thread has seen its submission's progress value. The signal's first synchronization -/// scope "includes every command in the same batch that the signal operation is defined in, and -/// all commands that occur earlier in submission order" (`vkQueueSubmit2`, all commands' stages); -/// the host wait that sees the value happens after the signal ("signal operations happening -/// before host operations that wait on them"); and "a query is made available by the operation of -/// ... `vkCmdWriteTimestamp2`". So both queries of a submission that ran are available, and a -/// read without the wait reports them. A submission the device cancelled at a reset may still -/// signal its value having run nothing: its queries stay unavailable ("`vkGetQueryPoolResults` -/// may return `VK_NOT_READY` if there are queries in the unavailable state"), and the read reports -/// no times at once, where a waiting read runs until the driver gives up (RADV: twice the -/// kernel's 60 s compute timeout). +/// The read does not wait (no `VK_QUERY_RESULT_WAIT_BIT`). A submission the device cancelled at a +/// reset may still signal its progress value having run nothing, its queries never made +/// available: a waiting read of them runs until the driver gives up (RADV: twice the kernel's +/// 60 s compute timeout), and the read without the wait reports no times at once +/// ("`vkGetQueryPoolResults` may return `VK_NOT_READY` if there are queries in the unavailable +/// state"). +/// +/// The caller reads a pair only once the completion thread has seen its submission's progress +/// value. The signal's first synchronization scope "includes every command in the same batch that +/// the signal operation is defined in, and all commands that occur earlier in submission order" +/// (`vkQueueSubmit2`), "a query is made available by the operation of ... `vkCmdWriteTimestamp2`", +/// and a host wait that sees the value happens after the signal ("signal operations happening +/// before host operations that wait on them"): the pair of a submission that ran is available at +/// the read. The completion thread also learns values from `vkGetSemaphoreCounterValue`, for which +/// the specification states no such ordering. A driver that makes timestamps available only +/// after the signal (MoltenVK 1.4.2 on an M3 Max did, for most command buffers; it is below the +/// floor) would read none here and leave the GPU times 0 with no other sign, so the caller counts +/// each pair that reads as not available (`Metrics::stamps_not_available`), and every play and +/// suite leg reports the count. /// /// Bound: the values only decide that command buffer's `GPUStartTime`, through arithmetic defined /// for every value that gives a time no later than its `GPUEndTime`. diff --git a/crates/metal2vulkan-rt/src/tests/command.rs b/crates/metal2vulkan-rt/src/tests/command.rs index a401448f2..f285798ce 100644 --- a/crates/metal2vulkan-rt/src/tests/command.rs +++ b/crates/metal2vulkan-rt/src/tests/command.rs @@ -85,6 +85,9 @@ pub(crate) mod on_device { body(name, instance, device); within("the teardown", || counters.gauges() == Gauges::default()); print_gauges_at_drop(test, counters.gauges()); + // The matrix holds this at 0 (`readback::timestamps`). + let unavailable = counters.metrics().stamps_not_available; + println!("M2V-MATRIX metric {test}/stamps_not_available {unavailable}"); } } diff --git a/crates/metal2vulkan-rt/src/tests/readback.rs b/crates/metal2vulkan-rt/src/tests/readback.rs index fafc568ed..025b33060 100644 --- a/crates/metal2vulkan-rt/src/tests/readback.rs +++ b/crates/metal2vulkan-rt/src/tests/readback.rs @@ -110,14 +110,17 @@ fn the_readback_scan_finds_each_form_and_passes_prose() { } /// A timestamp pair no submission wrote, as one the device cancelled at a reset leaves it (its -/// progress value signalled, its commands never run): the read reports no times, and returns at -/// once rather than waiting for values that never come. RADV's waiting read of such a query spins -/// for twice the kernel's 60 s compute timeout before it reports the device lost; the bound here -/// turns that wait into a failure. On lavapipe a query no command wrote reads as not ready with or -/// without the wait flag, so there the test passes either way. +/// progress value signalled, its commands never run): the read reports no times, returns at once +/// rather than waiting for values that never come, and is counted +/// (`Metrics::stamps_not_available`, here on counters of the test's own, so the device's record +/// for the matrix stays that of its real command buffers). RADV's waiting read of such a query spins for twice the +/// kernel's 60 s compute timeout before it reports the device lost; the bound here turns that wait +/// into a failure. On lavapipe a query no command wrote reads as not ready with or without the +/// wait flag, so there only the count tells the forms apart. #[cfg(any(m2v_gpu, m2v_cpu_icd))] #[test] fn a_pair_no_submission_wrote_reads_as_no_times_at_once() { + use crate::counters::Counters; use crate::tests::command::on_device::{each_device, PATIENCE}; use std::sync::{mpsc, Arc}; each_device("readback_unwritten_pair", |name, _, device| { @@ -125,11 +128,12 @@ fn a_pair_no_submission_wrote_reads_as_no_times_at_once() { let pair = (core.stamps.take(&core.device, &core.counters)).expect("a pair"); let (send, receive) = mpsc::channel(); let reader = Arc::clone(&core); + let counters = Counters::new(); + let counting = counters.clone(); let thread = std::thread::spawn(move || { // SAFETY: the pair is the device's, both its queries reset, and no other thread resets // or reads them. - let read = - unsafe { crate::readback::timestamps(&reader.device, pair.pool, pair.first) }; + let read = unsafe { pair.times(&reader.device, &counting) }; let _ = send.send(read); }); let read = (receive.recv_timeout(PATIENCE)).unwrap_or_else(|_| { @@ -137,6 +141,8 @@ fn a_pair_no_submission_wrote_reads_as_no_times_at_once() { }); thread.join().expect("the reader"); assert_eq!(read, None, "{name}: times from a pair no submission wrote"); + let counted = counters.metrics().stamps_not_available; + assert_eq!(counted, 1, "{name}: the pairs counted as not available"); core.stamps.give_back(pair); }); } diff --git a/crates/metal2vulkan-rt/src/times.rs b/crates/metal2vulkan-rt/src/times.rs index 8811881b6..a3467de99 100644 --- a/crates/metal2vulkan-rt/src/times.rs +++ b/crates/metal2vulkan-rt/src/times.rs @@ -10,8 +10,9 @@ //! the last. The kernel times are the host time at commit and when the submit call that passed the //! last segment returned. -use crate::counters::{Counters, Gauge}; +use crate::counters::{Counters, Gauge, Metric}; use crate::device::{lock, Core}; +use crate::readback; use ash::vk; use std::sync::{Mutex, PoisonError}; @@ -195,6 +196,28 @@ pub(crate) struct Pair { pub(crate) first: u32, } +impl Pair { + /// Its two timestamps, read once its command buffer's submission signalled: none where they + /// read as not available ([`readback::timestamps`]), each such read counted + /// (`Metrics::stamps_not_available`). + /// + /// # Safety + /// + /// The pair's pool is `device`'s, and no other thread resets its two queries while it reads. + pub(crate) unsafe fn times( + self, + device: &ash::Device, + counters: &Counters, + ) -> Option<(u64, u64)> { + // SAFETY: by the caller. + let read = unsafe { readback::timestamps(device, self.pool, self.first) }; + if read.is_none() { + counters.count(Metric::StampsNotAvailable); + } + read + } +} + /// The device's timestamp pools, which every queue's command buffers take their pairs from: made /// on demand, [`POOL_QUERIES`] queries each, kept until the device's teardown, so their count is /// the most command buffers ever timed at once over 2,048. A pair given back is taken again before diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs index f20e86713..d48a9f7d1 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs @@ -2136,6 +2136,7 @@ impl Backend for NewRuntime { ("capture_overflow_bytes", metrics.capture_overflow_bytes), ("capture_copy_nanos", metrics.capture_copy_nanos), ("capture_wait_nanos", metrics.capture_wait_nanos), + ("stamps_not_available", metrics.stamps_not_available), ("skipped_encoders", self.skips.encoders), ("skipped_objects", self.skips.objects), ("skipped_render_states", self.skips.render_states), diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 6955ced16..8c930b8b9 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2376,6 +2376,7 @@ field metal2vulkan-rt::Metrics.pushes_held pub pushes_held: u64 field metal2vulkan-rt::Metrics.ring_writes pub ring_writes: u64 field metal2vulkan-rt::Metrics.sampler_element_writes pub sampler_element_writes: u64 field metal2vulkan-rt::Metrics.staging_sleeps pub staging_sleeps: u64 +field metal2vulkan-rt::Metrics.stamps_not_available pub stamps_not_available: u64 field metal2vulkan-rt::Metrics.submissions pub submissions: u64 field metal2vulkan-rt::Metrics.submissions_during_frees pub submissions_during_frees: u64 field metal2vulkan-rt::Metrics.submit_infos pub submit_infos: u64