From 7ba0cd9fe6c0301fdc2d59ebb84477e15e991ea4 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 15 Nov 2026 05:18:59 -0800 Subject: [PATCH] RUNTIME 2343: GPU times placed by a calibration still beyond 20 us are announced and counted A calibration pair is read again while the smallest deviation so far states more than 20 us (`gpu_time::SETTLED`), four readings at most (`least_deviation`). Where the least of four still stated more (93 and 125 us on the RX 7900 XTX under full parallel suites: a reading's deviation holds the time its thread spent off the CPU), the runtime placed the command buffer's GPU times by it, inside the command buffer's life, with up to that error, and said nothing. Owner rule 2: a near-equivalent is recorded and announced. `GpuClock::span` now answers `Placement::Loose { deviation }` where the pair places both times inside the life and its deviation exceeds `SETTLED` (a pair outside the life is still set aside as before). `Placement::read` gives the notice for every placement the first read of a command buffer's GPU times announces, counting a loose one: - new kind `Degradation::LooselyCalibratedGpuTimes`, `loosely-calibrated-gpu-times`, class Precision, announced once per device at the read (the site `uncalibrated-gpu-times` uses), its reason stating the deviation; not `uncalibrated-gpu-times`, whose cost says the times are placed by the completion, where these are the pair's, the duration exact; - new `WorkCounts::loosely_calibrated_gpu_times`: command buffers whose times a read found placed so, counted at the read that announces, so the count is nonzero exactly where the kind was announced. Tests: `tests::gpu_time::a_pair_still_beyond_the_settled_deviation_is_ announced_once_and_counted` feeds `least_deviation` a fake source of four readings above 20 us (Loose, the kind with the deviation in its reason, each read counted, announced once) and settled ones (Iris Xe's 165 then 7.6 us; 20 us exactly: Calibrated, nothing), and a loose pair outside the life (Disagreed, the uncalibrated kind, not counted). The lane's `gpu_times` expects the kind exactly where counted, and holds the count nonzero exactly where `calibration_deviation_most` exceeds the slack (no disagreement); `read_kinds` adds it where counted (mesh_draws), `disagreeable_kinds` allows it (first_use); a site const `LOOSE_GPU_TIMES` takes it. The conformance cases that read GPU times (`command-buffer-facts`, `empty-command-buffers`) allow its notice: the host's load decides it, and every value still compares exactly. docs/API.md's catalogue lists the kind; api_signatures.tsv blessed. --- .../src/cases/command_buffer_facts.rs | 22 +++-- .../src/cases/empty_command_buffers.rs | 9 +- .../examples/host_ab/runtime.rs | 4 + .../src/command_buffer/mod.rs | 40 +++----- .../metal2vulkan-runtime/src/degradation.rs | 11 +++ crates/metal2vulkan-runtime/src/gpu_time.rs | 63 +++++++++++- .../src/tests/gpu_time.rs | 97 ++++++++++++++++++- .../metal2vulkan-runtime/src/work_counts.rs | 22 +++++ .../src/runtime_lane/degrade_notices.rs | 10 +- .../src/runtime_lane/first_use.rs | 3 +- .../src/runtime_lane/gpu_times.rs | 75 +++++++++----- crates/metal2vulkan/src/cache/mod.rs | 2 +- crates/metal2vulkan/tests/api_signatures.tsv | 2 + docs/API.md | 8 ++ 14 files changed, 295 insertions(+), 73 deletions(-) diff --git a/crates/metal2vulkan-conformance/src/cases/command_buffer_facts.rs b/crates/metal2vulkan-conformance/src/cases/command_buffer_facts.rs index 4e3d98d57..71a545624 100644 --- a/crates/metal2vulkan-conformance/src/cases/command_buffer_facts.rs +++ b/crates/metal2vulkan-conformance/src/cases/command_buffer_facts.rs @@ -53,9 +53,15 @@ pub const COMMAND_BUFFER_FACTS: &str = "command-buffer-facts"; /// placed their times as it does on an uncalibrated device (`gpu_time.rs`), and every value of /// the case still compared exactly. The Radeon 780M (RADV PHOENIX) in the same machine agreed /// with its calibration in the same runs (0) and announced nothing. -const GPU_TIME_NOTICES: [Degradation; 2] = [ +/// +/// `LooselyCalibratedGpuTimes` is here because a loaded host makes it: a calibration pair whose +/// readings each state the time the reading thread spent off the CPU stays beyond 20 us after +/// the runtime's four readings (the RX 7900 XTX stated 93 and 125 us in full parallel suites), +/// and its times, still inside the command buffer's life, are announced as such (`gpu_time.rs`). +const GPU_TIME_NOTICES: [Degradation; 3] = [ Degradation::GpuTimesFromHost, Degradation::UncalibratedGpuTimes, + Degradation::LooselyCalibratedGpuTimes, ]; /// A case over one command buffer's own answers. @@ -110,8 +116,10 @@ impl Case for CommandBufferFactsCase { // driver's own timestamps wait for every queue's (MoltenVK on Apple's GPUs). // `UncalibratedGpuTimes`: it places them without calibrated timestamps where the device // offers none, or where the device's calibration disagrees with the command buffer's life - // (the RX 7900 XTX, `GPU_TIME_NOTICES`). The orderings this case compares do not depend on - // either, which is why the case can take the notice and still compare every value exactly. + // (the RX 7900 XTX, `GPU_TIME_NOTICES`). `LooselyCalibratedGpuTimes`: it places them by a + // calibration a loaded host left beyond 20 us. The orderings this case compares do not + // depend on any of them, which is why the case can take the notice and still compare every + // value exactly. // Every row reads its answer back through a `Shared` buffer, so on a device without // unified memory the runtime announces `shared-in-system-memory` // (`crate::replay::shared_memory_notice` holds the whole reason). It declares the @@ -148,10 +156,10 @@ impl Case for CommandBufferFactsCase { mod tests { use super::*; - /// The case states its inputs, names itself once, and declares the two notices the runtime can - /// announce over the GPU times, the venue's own memory notice, and nothing else. + /// The case states its inputs, names itself once, and declares the three notices the runtime + /// can announce over the GPU times, the venue's own memory notice, and nothing else. /// - /// A fourth notice slipping in would be a value the case took for a reason nobody wrote down, + /// A fifth notice slipping in would be a value the case took for a reason nobody wrote down, /// which is what this count is for. #[test] fn the_case_states_its_inputs_and_only_the_gpu_time_notices() { @@ -161,7 +169,7 @@ mod tests { assert_eq!(case.name(), COMMAND_BUFFER_FACTS); assert!(!case.inputs().expect("the inputs").is_empty()); let allowed = case.allowances(); - // The two GPU-time notices, and the one a card without unified memory announces for the + // The three GPU-time notices, and the one a card without unified memory announces for the // `Shared` buffer the case reads its answer back through. assert_eq!(allowed.len(), GPU_TIME_NOTICES.len() + 1); // And the case records more than one run, because two of its booleans read a clock. diff --git a/crates/metal2vulkan-conformance/src/cases/empty_command_buffers.rs b/crates/metal2vulkan-conformance/src/cases/empty_command_buffers.rs index 2fabb0129..e469d09ca 100644 --- a/crates/metal2vulkan-conformance/src/cases/empty_command_buffers.rs +++ b/crates/metal2vulkan-conformance/src/cases/empty_command_buffers.rs @@ -74,10 +74,13 @@ impl Case for EmptyCommandBuffersCase { // fill outside its life (`WorkCounts::calibration_disagreements` 1 in each of three `cost` // runs); the Radeon 780M in the same machine agreed with its calibration (0). The orderings // this case compares hold for every source, so every value still compares exactly; the - // notice site alone takes either notice. + // notice site alone takes each notice. `LooselyCalibratedGpuTimes`: it places them by a + // calibration pair a loaded host left beyond 20 us after its four readings, inside the + // command buffer's life (`gpu_time.rs`). vec![ notice_allowance(Degradation::GpuTimesFromHost.code()), notice_allowance(Degradation::UncalibratedGpuTimes.code()), + notice_allowance(Degradation::LooselyCalibratedGpuTimes.code()), ] } @@ -95,7 +98,7 @@ impl Case for EmptyCommandBuffersCase { mod tests { use super::*; - /// The case states its inputs, names itself once, takes the two GPU-time notices alone, and + /// The case states its inputs, names itself once, takes the three GPU-time notices alone, and /// records more than one run. #[test] fn the_case_states_its_inputs_and_only_the_gpu_time_notices() { @@ -104,7 +107,7 @@ mod tests { let case = &cases[0]; assert_eq!(case.name(), EMPTY_COMMAND_BUFFERS); assert!(!case.inputs().expect("the inputs").is_empty()); - assert_eq!(case.allowances().len(), 2); + assert_eq!(case.allowances().len(), 3); assert!(case.record_runs() > 1); } } diff --git a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs index 15c11e0dd..2d765a33a 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs @@ -1523,6 +1523,10 @@ impl Gpu for Runtime { ("calibration_readings", c.calibration_readings), ("calibration_deviation_most", c.calibration_deviation_most), ("calibration_disagreements", c.calibration_disagreements), + ( + "loosely_calibrated_gpu_times", + c.loosely_calibrated_gpu_times, + ), ("storage_forms_taken", c.storage_forms_taken), ("addressable_buffers_made", c.addressable_buffers_made), ("mipmaps_filtered", c.mipmaps_filtered), diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index b27e3a36c..ee07fb76a 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -4692,11 +4692,14 @@ impl CommandBuffer { /// /// On a device with calibrated timestamps (`VK_KHR_calibrated_timestamps` or /// `VK_EXT_calibrated_timestamps`, enabled where listed) it is Metal's answer, within the - /// calibration's deviation. Without, it is the end below less the GPU's duration, which - /// stays exact, and the device announces `Degradation::UncalibratedGpuTimes` once; so is it - /// where the calibration would place the times outside the command buffer's life, from its - /// first submission to its completion, by more than its deviation and the clocks' drift (a - /// calibration that disagrees with the device's own timestamps, `gpu_time.rs`). A device + /// calibration's deviation; where that deviation is still above 20 us once the calibration was + /// read again (four readings at most: a loaded host states more), the device announces + /// `Degradation::LooselyCalibratedGpuTimes` once. Without, it is the end below less the GPU's + /// duration, which stays exact, and the device announces `Degradation::UncalibratedGpuTimes` + /// once; so is it where the calibration would place the times outside the command buffer's + /// life, from its first submission to its completion, by more than its deviation and the + /// clocks' drift (a calibration that disagrees with the device's own timestamps, + /// `gpu_time.rs`). A device /// whose queue family writes no timestamps refuses it (`Unsupported`, `Feature::GpuTimes`). /// So does a command buffer committed while others held every slot of the device's /// timestamp pools and no pool more could be made (2048 on MoltenVK; elsewhere only where @@ -4843,30 +4846,9 @@ impl CommandBuffer { ); state.times.gpu = Some(times); drop(state); - let reason = match placement { - Placement::Calibrated => return Ok(times), - Placement::Uncalibrated if clock.calibration.is_some() => { - "the device's calibrated timestamps could not be read as it completed".to_owned() - } - Placement::Uncalibrated => { - "the device calibrates its timestamps against no host clock the runtime reads" - .to_owned() - } - Placement::Disagreed { by, deviation } => format!( - "the device's calibration placed them {by} ns outside the command buffer's life, \ - between its first submission and its completion, where it stated a deviation of \ - {deviation} ns: its timestamps count on another clock than its calibrated one" - ), - }; - device.degrade(Degradation::UncalibratedGpuTimes, || { - ( - "a command buffer's GPU times".to_owned(), - format!( - "{reason} ({}-bit timestamps of {} ns)", - clock.valid_bits, clock.period - ), - ) - }); + if let Some((kind, reason)) = placement.read(clock, &shared.counts) { + device.degrade(kind, || ("a command buffer's GPU times".to_owned(), reason)); + } Ok(times) } diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 15a667f47..84df60c0b 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -763,6 +763,17 @@ degradations! { UncalibratedGpuTimes = "uncalibrated-gpu-times", Precision, "the end is the host time the runtime saw the command buffer complete, late by that \ latency, and the start the GPU's exact duration before it"; + /// A command buffer's GPU times on a device whose calibration pair, taken as the command + /// buffer completed, stated a deviation above 20 us though the runtime read it again, + /// four readings at most, and placed them inside the command buffer's life by the + /// reading of the least deviation (`gpu_time.rs`). A reading's deviation holds the time + /// its thread spent off the CPU between its two values, so a loaded host states more: an + /// RX 7900 XTX (RADV) under full parallel test suites stated 93 and 125 us after four + /// readings. + LooselyCalibratedGpuTimes = "loosely-calibrated-gpu-times", Precision, + "each GPU time may stand from the moment the GPU wrote it by up to the deviation its \ + reason states, more than the 20 us a settled calibration reading keeps to; the \ + duration between them stays exact"; /// A command buffer's GPU times on a device whose timestamp pools had no slot free at /// the command buffer's commit, and which could make no pool more: every slot was held /// by a command buffer in flight (`gpu_time.rs`). diff --git a/crates/metal2vulkan-runtime/src/gpu_time.rs b/crates/metal2vulkan-runtime/src/gpu_time.rs index 769438ee1..2c37ffb9e 100644 --- a/crates/metal2vulkan-runtime/src/gpu_time.rs +++ b/crates/metal2vulkan-runtime/src/gpu_time.rs @@ -87,6 +87,13 @@ //! us over 200 readings). The reading after it stated 7.6 us and agreed with the next ones within //! 0.4 us. A thread preempted between a reading's two values states the time it lost: on an RX //! 7900 XTX (RADV) under a full parallel suite, two readings in turn stated about 70 us each. +//! - **Announced where the least of them states more.** A pair whose least deviation is still +//! above [`SETTLED`] places the times (inside the command buffer's life, below) within that +//! deviation of the moments the GPU wrote them, the duration between them exact: the RX 7900 XTX +//! under full parallel suites stated 93 and 125 us after four readings. The first read of such a +//! command buffer's GPU times counts it (`WorkCounts::loosely_calibrated_gpu_times`), and the +//! device announces it once (`Degradation::LooselyCalibratedGpuTimes`), the deviation in the +//! notice ([`Placement::read`]). //! - **Checked against the command buffer's life.** A time the pair places is the GPU's, and the //! GPU ran the work between the first batch's submission (the kernel start) and the moment the //! completion turn saw it complete. A pair that places the start before that start, or the @@ -191,6 +198,7 @@ //! moves a time by nanoseconds to microseconds, within what the calibration states for itself on //! the devices measured. +use super::degradation::Degradation; use super::device::lock; use super::encoder_times::{EncoderTimes, Timed}; use super::exit_guard::{self, Call}; @@ -365,6 +373,10 @@ pub(crate) struct Life { pub(crate) enum Placement { /// By the device's calibration pair. Calibrated, + /// By the device's calibration pair, inside its life, where the pair stated a `deviation` + /// above [`SETTLED`] though it was read again: each time lies within that many nanoseconds + /// of the moment the GPU wrote it (the module docs). + Loose { deviation: u64 }, /// By its completion: the device had no pair as it completed. Uncalibrated, /// By its completion: the pair placed a time outside its life by `by` nanoseconds, more than @@ -372,6 +384,47 @@ pub(crate) enum Placement { Disagreed { by: u64, deviation: u64 }, } +impl Placement { + /// What the first read of a command buffer's GPU times placed so announces on a device of + /// `clock`: the kind and the notice's reason, `None` for Metal's answer (the module docs). + /// A loose placement is counted in `counts` (`WorkCounts::loosely_calibrated_gpu_times`). + pub(crate) fn read(self, clock: &GpuClock, counts: &Counters) -> Option<(Degradation, String)> { + let (kind, reason) = match self { + Placement::Calibrated => return None, + Placement::Loose { deviation } => { + counts.loosely_calibrated(); + let reason = format!( + "the device's calibration pair stated a deviation of {deviation} ns, the least \ + of its readings ({READINGS} at most), more than the {SETTLED} ns a settled \ + reading keeps to: each time lies within that much of the moment the GPU \ + wrote it" + ); + (Degradation::LooselyCalibratedGpuTimes, reason) + } + Placement::Uncalibrated if clock.calibration.is_some() => ( + Degradation::UncalibratedGpuTimes, + "the device's calibrated timestamps could not be read as it completed".to_owned(), + ), + Placement::Uncalibrated => ( + Degradation::UncalibratedGpuTimes, + "the device calibrates its timestamps against no host clock the runtime reads" + .to_owned(), + ), + Placement::Disagreed { by, deviation } => ( + Degradation::UncalibratedGpuTimes, + format!( + "the device's calibration placed them {by} ns outside the command buffer's \ + life, between its first submission and its completion, where it stated a \ + deviation of {deviation} ns: its timestamps count on another clock than its \ + calibrated one" + ), + ), + }; + let stamps = format!("{}-bit timestamps of {} ns", clock.valid_bits, clock.period); + Some((kind, format!("{reason} ({stamps})"))) + } +} + /// Of the readings `read` gives, the one of the smallest deviation, and how many `read` was /// called for: one more while the latest halved the deviation the one before it stated, or while /// the smallest so far states more than [`SETTLED`], [`READINGS`] at most (the module docs). @@ -428,8 +481,9 @@ impl GpuClock { /// The host times, in nanoseconds of the host's clock, of a command buffer's timestamps /// `start` and `end`, and how they were placed: by `reading`'s pair where they lie inside the /// command buffer's `life` within the reading's deviation and the clocks' drift since it - /// ([`DRIFT_PPM`]); else, and without a reading, ending where `life` ends, the host time its - /// completion was seen, and starting their duration before (the module docs). + /// ([`DRIFT_PPM`]), loosely where that deviation is above [`SETTLED`]; else, and without a + /// reading, ending where `life` ends, the host time its completion was seen, and starting + /// their duration before (the module docs). pub(crate) fn span( &self, reading: Option, @@ -457,7 +511,10 @@ impl GpuClock { let by = early.max(late); return (by_completion, Placement::Disagreed { by, deviation }); } - (placed, Placement::Calibrated) + match deviation > SETTLED { + true => (placed, Placement::Loose { deviation }), + false => (placed, Placement::Calibrated), + } } /// The nanoseconds from timestamp `start` to `end`. diff --git a/crates/metal2vulkan-runtime/src/tests/gpu_time.rs b/crates/metal2vulkan-runtime/src/tests/gpu_time.rs index 31a4ff958..c815adf3f 100644 --- a/crates/metal2vulkan-runtime/src/tests/gpu_time.rs +++ b/crates/metal2vulkan-runtime/src/tests/gpu_time.rs @@ -2,15 +2,19 @@ //! calibration pair or by the completion, across a wrap of the timestamps' bits; a pair that //! places them outside the command buffer's life by more than its deviation and the clocks' //! drift allow set aside for the completion; the pair, the -//! reading of the least deviation; the host time domain a calibration reads; the extension's +//! reading of the least deviation, announced and counted where it still states more than 20 us; +//! the host time domain a calibration reads; the extension's //! preferred form; the device's timestamp pools, their slots held by the command buffers in //! flight, made as those need them up to the most; and the kernel times, the start shown with the //! end alone. +use crate::degradation::Announced; use crate::gpu_time::{ host_domain, host_time, least_deviation, Extension, GpuClock, KernelTimes, Life, Pair, - Placement, Pool, Pools, Reading, DRIFT_PPM, PLAIN, PREFIXED, SLOTS, + Placement, Pool, Pools, Reading, DRIFT_PPM, PLAIN, PREFIXED, READINGS, SLOTS, }; +use crate::work_counts::Counters; +use crate::Degradation; use ash::vk; use std::collections::BTreeSet; use std::ffi::CString; @@ -235,6 +239,95 @@ fn a_pair_that_places_times_outside_the_life_is_set_aside() { )); } +/// A pair whose readings each state more than `SETTLED` (20 us) is read four times, and the least +/// places the times inside the command buffer's life, loosely: the duration exact, each time +/// within that deviation of the GPU's. The first read of each command buffer placed so counts it +/// (`WorkCounts::loosely_calibrated_gpu_times`) and gives `loosely-calibrated-gpu-times`, the +/// deviation in its reason, which the device announces once. A pair that settles at 20 us or +/// below (the Iris Xe's first reading after an idle GPU, 165 us, then 7.6 us) is Metal's answer: +/// nothing counted or announced. A loose pair that places a time outside the life is set aside, +/// as any pair that disagrees. +#[test] +fn a_pair_still_beyond_the_settled_deviation_is_announced_once_and_counted() { + let clock = clock(64, 1.0); + // A fake calibration whose readings state `deviations` in turn, ticks counting host + // nanoseconds. + let reading_of = |deviations: &[u64]| { + let mut readings = deviations.iter().map(|&deviation| Reading { + pair: Pair { + ticks: 1_000_000, + host: 1_000_000, + }, + deviation, + }); + least_deviation(|| readings.next()).expect("a first reading") + }; + let (start, end, inside) = (2_000_100, 2_001_100, life(2_000_000, 2_002_000)); + let counts = Counters::default(); + let announced = Announced::default(); + // An RX 7900 XTX's under full parallel suites: the least of four, 93.4 us; no fifth read. + let (loose, readings) = reading_of(&[124_900, 93_400, 110_000, 99_000, 1_000]); + assert_eq!((readings, loose.deviation), (READINGS, 93_400)); + let (times, placement) = clock.span(Some(loose), start, end, inside); + assert_eq!(times, (start, end)); + assert_eq!(placement, Placement::Loose { deviation: 93_400 }); + let first_announced: Vec = (0..2) + .map(|_| { + let (kind, reason) = placement.read(&clock, &counts).expect("a notice"); + assert_eq!(kind, Degradation::LooselyCalibratedGpuTimes); + assert!(reason.contains("a deviation of 93400 ns"), "{reason}"); + assert!(reason.ends_with("(64-bit timestamps of 1 ns)"), "{reason}"); + announced.now(kind) + }) + .collect(); + assert_eq!(first_announced, [true, false], "announced once"); + assert_eq!( + counts.loosely_calibrated_gpu_times(), + 2, + "each read counted" + ); + + // Settled: the first reading after an idle GPU, then a settled one and one that halves + // nothing; and one at 20 us exactly. + for (deviations, calls, least) in [ + (&[165_000, 7_600, 7_600][..], 3, 7_600), + (&[30_000, 20_000][..], 2, 20_000), + ] { + let (settled, readings) = reading_of(deviations); + assert_eq!((readings, settled.deviation), (calls, least)); + let (times, placement) = clock.span(Some(settled), start, end, inside); + assert_eq!((times, placement), ((start, end), Placement::Calibrated)); + assert_eq!(placement.read(&clock, &counts), None); + } + assert_eq!( + counts.loosely_calibrated_gpu_times(), + 2, + "nothing counted when settled" + ); + + // Loose and a second off: set aside for the completion, the uncalibrated kind, not counted. + let (times, placement) = clock.span( + Some(loose), + start + 1_000_000_000, + end + 1_000_000_000, + inside, + ); + assert_eq!(times, (2_001_000, 2_002_000)); + assert!( + matches!( + placement, + Placement::Disagreed { + deviation: 93_400, + .. + } + ), + "{placement:?}" + ); + let (kind, _) = placement.read(&clock, &counts).expect("a notice"); + assert_eq!(kind, Degradation::UncalibratedGpuTimes); + assert_eq!(counts.loosely_calibrated_gpu_times(), 2); +} + #[cfg(unix)] #[test] fn calibration_reads_the_devices_domain_and_a_monotonic_host_one() { diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 47e042627..f4bbac396 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -108,6 +108,7 @@ pub(crate) struct Counters { calibration_readings: AtomicU64, calibration_deviation_most: AtomicU64, calibration_disagreements: AtomicU64, + loosely_calibrated_gpu_times: AtomicU64, storage_forms_taken: AtomicU64, addressable_buffers_made: AtomicU64, mipmaps_filtered: AtomicU64, @@ -2652,6 +2653,20 @@ impl Counters { .fetch_add(1, Ordering::Relaxed); } + /// The first read of a command buffer's GPU times found them placed by a calibration pair + /// that stated a deviation above 20 us (`gpu_time.rs`, `Placement::read`). + pub(crate) fn loosely_calibrated(&self) { + self.loosely_calibrated_gpu_times + .fetch_add(1, Ordering::Relaxed); + } + + /// The command buffers counted by [`loosely_calibrated`](Self::loosely_calibrated) so far, as + /// [`Device::work_counts`] reads them. + #[cfg(test)] + pub(crate) fn loosely_calibrated_gpu_times(&self) -> u64 { + self.loosely_calibrated_gpu_times.load(Ordering::Relaxed) + } + /// A texture took its storage form (`texture/storage_form.rs`). pub(crate) fn storage_form_taken(&self) { self.storage_forms_taken.fetch_add(1, Ordering::Relaxed); @@ -6442,6 +6457,11 @@ pub struct WorkCounts { /// pair's stated deviation and the clocks' drift allow: a pair that disagrees with the /// device's own timestamps. Each was placed by its completion, as on an uncalibrated device. pub calibration_disagreements: u64, + /// Command buffers whose GPU times, at their first read, were placed by a calibration pair + /// that stated a deviation above 20 us, the least of its readings (four at most): inside + /// their life, each time within that deviation of the moment the GPU wrote it. The device + /// announces `loosely-calibrated-gpu-times` at the first. + pub loosely_calibrated_gpu_times: u64, /// Textures that took their storage form: a fetched render target's image made again with /// `STORAGE`, once per texture (`texture/storage_form.rs`). Counted by the time the /// committing command buffer is scheduled, and by the time a command buffer made meanwhile @@ -11317,6 +11337,8 @@ impl Device { calibration_deviation_most: (counters.calibration_deviation_most) .load(Ordering::Relaxed), calibration_disagreements: (counters.calibration_disagreements).load(Ordering::Relaxed), + loosely_calibrated_gpu_times: (counters.loosely_calibrated_gpu_times) + .load(Ordering::Relaxed), storage_forms_taken: counters.storage_forms_taken.load(Ordering::Relaxed), addressable_buffers_made: counters.addressable_buffers_made.load(Ordering::Relaxed), mipmaps_filtered: counters.mipmaps_filtered.load(Ordering::Relaxed), diff --git a/crates/metal2vulkan-validation/src/runtime_lane/degrade_notices.rs b/crates/metal2vulkan-validation/src/runtime_lane/degrade_notices.rs index d1cdab161..869d30383 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/degrade_notices.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/degrade_notices.rs @@ -21,7 +21,7 @@ //! ([`PLANES`]), textures over the caller's images //! ([`WRAPPED`]), the shader-side stencil ([`SHADOW`]), tile dispatches ([`TILE_DISPATCHES`]), //! fragment imageblocks ([`FRAGMENT_IMAGEBLOCKS`]), command buffers' GPU times ([`GPU_TIMES`], -//! [`UNTIMED`], [`FROM_HOST`]), color fills packed where fills ignore the color +//! [`LOOSE_GPU_TIMES`], [`UNTIMED`], [`FROM_HOST`]), color fills packed where fills ignore the color //! ([`UNPACKED_FILL`]), texture buffers' fills reading bytes past their pattern's copies in //! `texture_buffer_fills` ([`TEXTURE_BUFFER_FILL`]), draws of points no state sizes in //! `topology_classes` ([`UNSIZED_POINTS`]), triangles in fill mode `Points` whose vertex function @@ -348,6 +348,13 @@ pub(super) const PRIVATE_PROPERTIES: [Degradation; 1] = [Degradation::PrivatePro /// whose measurement reads GPU times (`gpu_times::read_kinds`). pub(super) const GPU_TIMES: [Degradation; 1] = [Degradation::UncalibratedGpuTimes]; +/// The kind `gpu_times` takes on a venue whose device calibrates its timestamps, where a +/// calibration pair that placed a command buffer's GPU times read stated a deviation above 20 us +/// though the runtime read it again (`WorkCounts::loosely_calibrated_gpu_times`): the host's load +/// decides it, not the work, so a test reading GPU times on such a device may meet it +/// (`gpu_times::read_kinds`, `gpu_times::disagreeable_kinds`). +pub(super) const LOOSE_GPU_TIMES: [Degradation; 1] = [Degradation::LooselyCalibratedGpuTimes]; + /// The kind `gpu_times` takes on every venue that writes timestamps, with more command buffers in /// flight than the device's timestamp pools have slots. pub(super) const UNTIMED: [Degradation; 1] = [Degradation::UntimedCommandBuffer]; @@ -509,6 +516,7 @@ fn every_kind_has_a_site_a_lane_test_takes() { &TILE_DISPATCHES, &FRAGMENT_IMAGEBLOCKS, &GPU_TIMES, + &LOOSE_GPU_TIMES, &UNTIMED, &UNPACKED_FILL, &TEXTURE_BUFFER_FILL, diff --git a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs index 9c0cccfd7..bd1fd4b8f 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs @@ -1219,7 +1219,8 @@ fn neutrals_and_sparse_textures_wait_for_no_gpu_work() { // there (a format class the function reads as data) decide the notes their stages keep by // such values, which a making does not announce: each is allowed. // A calibrating device may yet place one of these command buffers' GPU times outside its - // life, which it announces at the read (`gpu_times::disagreeable_kinds`). + // life, or by a pair a loaded host left beyond 20 us, which it announces at the read + // (`gpu_times::disagreeable_kinds`). let mut allowed = disagreeable_kinds(device); for (_, case, _, _) in &cases { kinds.extend(case_making_kinds(device, case)); diff --git a/crates/metal2vulkan-validation/src/runtime_lane/gpu_times.rs b/crates/metal2vulkan-validation/src/runtime_lane/gpu_times.rs index 6dd940c10..656e9f0b4 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/gpu_times.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/gpu_times.rs @@ -12,9 +12,11 @@ //! time are printed: report only. //! //! The created device enables calibrated timestamps where listed: where it can calibrate (the -//! device's own domain and a monotonic host one), it announces nothing, and the calibration pairs -//! that placed the times stated deviations within [`SLACK`], each the least of two to -//! [`READINGS`] readings, unless a pair read all [`READINGS`] (a loaded host states more); +//! device's own domain and a monotonic host one), the calibration pairs that placed the times +//! stated deviations within [`SLACK`], each the least of two to [`READINGS`] readings, and it +//! announces nothing; or a pair read all [`READINGS`] and still stated more (a loaded host states +//! more), and it announces `Degradation::LooselyCalibratedGpuTimes` once, exactly where it +//! counted a command buffer read so (`WorkCounts::loosely_calibrated_gpu_times`, printed); //! elsewhere it announces `Degradation::UncalibratedGpuTimes` once. So does a calibrating //! device whose pairs placed a command buffer's times outside its life //! (`WorkCounts::calibration_disagreements`, printed): its timestamps count on another clock than @@ -66,7 +68,7 @@ //! none). use super::commit_work::orders_by_ends; -use super::degrade_notices::{assert_once, counts, FROM_HOST, GPU_TIMES, UNTIMED}; +use super::degrade_notices::{assert_once, counts, FROM_HOST, GPU_TIMES, LOOSE_GPU_TIMES, UNTIMED}; use super::presenting::Adopted; use super::{ at_plan, layer_reported_nothing_at_all, validated_device, validated_device_with, Messages, @@ -243,36 +245,44 @@ fn starts_in_the_first_encoder(device: &Device) -> bool { && !facts.contains(&DriverFact::SemaphoreOperationsStallSubmits) } -/// The kind a read of a timed command buffer's GPU times announces on `device`, made with the +/// The kinds reads of timed command buffers' GPU times announced on `device`, made with the /// default options, as `command_buffers_are_timed_in_the_hosts_time_base` checks: [`FROM_HOST`]'s /// where its driver answers them from the host, [`GPU_TIMES`]' where it writes timestamps it does /// not calibrate or its calibration disagreed with them so far -/// (`WorkCounts::calibration_disagreements`), none elsewhere (a calibrating device, or one that +/// (`WorkCounts::calibration_disagreements`), and [`LOOSE_GPU_TIMES`]' where a read so far found +/// them placed by a pair that stated more than 20 us (`WorkCounts::loosely_calibrated_gpu_times`, +/// counted at the read that announces it); none elsewhere (a calibrating device, or one that /// writes no timestamps and refuses the read). pub(super) fn read_kinds(device: &Device) -> BTreeSet { if from_host_by_driver(device) { return BTreeSet::from(FROM_HOST); } let venue = Venue::of(device); - let disagreed = device.work_counts().calibration_disagreements > 0; - match venue.bits > 0 && (!venue.calibrates() || disagreed) { - true => BTreeSet::from(GPU_TIMES), - false => BTreeSet::new(), + let counts = device.work_counts(); + let mut kinds = BTreeSet::new(); + if venue.bits > 0 && (!venue.calibrates() || counts.calibration_disagreements > 0) { + kinds.extend(GPU_TIMES); } + if counts.loosely_calibrated_gpu_times > 0 { + kinds.extend(LOOSE_GPU_TIMES); + } + kinds } -/// The kind a read of a timed command buffer's GPU times may announce on `device` once, or not, -/// where [`read_kinds`] does not already ask for it: a device that writes timestamps and +/// The kinds a read of a timed command buffer's GPU times may announce on `device` once, or not, +/// where [`read_kinds`] does not already ask for them: a device that writes timestamps and /// calibrates them can still place one command buffer's times outside its life, which the -/// runtime finds at the read (`WorkCounts::calibration_disagreements`) and announces then. A test +/// runtime finds at the read (`WorkCounts::calibration_disagreements`) and announces then +/// ([`GPU_TIMES`]), or place them by a pair that stated more than 20 us after its readings, as a +/// loaded host makes it (`WorkCounts::loosely_calibrated_gpu_times`, [`LOOSE_GPU_TIMES`]). A test /// whose own work reads GPU times cannot know in advance whether one of its command buffers -/// disagrees, so it allows the kind rather than requiring it (the RX 7900 XTX under RADV +/// meets either, so it allows the kinds rather than requiring them (the RX 7900 XTX under RADV /// disagreed in `first_use`'s busy work and not in the test beside it). pub(super) fn disagreeable_kinds(device: &Device) -> BTreeSet { let venue = Venue::of(device); let may = !from_host_by_driver(device) && venue.bits > 0 && venue.calibrates(); match may { - true => BTreeSet::from(GPU_TIMES), + true => BTreeSet::from([GPU_TIMES[0], LOOSE_GPU_TIMES[0]]), false => BTreeSet::new(), } } @@ -390,13 +400,10 @@ fn command_buffers_are_timed_in_the_hosts_time_base() { assert_once(¬ices, &FROM_HOST); assert_eq!(counts(¬ices).len(), 1, "{:?}", counts(¬ices)); } else if venue.calibrates() { - match device.work_counts().calibration_disagreements { - 0 => assert_eq!(counts(¬ices), Default::default(), "a calibrated device"), - _ => { - assert_once(¬ices, &GPU_TIMES); - assert_eq!(counts(¬ices).len(), 1, "{:?}", counts(¬ices)); - } - } + // Nothing on a calibrated device but a disagreement, or a pair still beyond the + // slack after its readings, each where counted. + let read: Vec = read_kinds(&device).into_iter().collect(); + assert_once(¬ices, &read); calibrated_within_slack(&device); } else { assert_once(¬ices, &GPU_TIMES); @@ -416,21 +423,32 @@ fn command_buffers_are_timed_in_the_hosts_time_base() { /// (RADV, Mesa 26.2.6) the least of four readings stated 37.5 us with this test alone on a host at /// load average 13.7 on 16 threads, and 93.4 and 124.9 us in whole suites, every placed time /// still inside its command buffer's life within it (`WorkCounts::calibration_disagreements`). +/// Where none disagreed, some command buffer was read as placed loosely +/// (`WorkCounts::loosely_calibrated_gpu_times`, announced) exactly where a pair stated more than +/// [`SLACK`]: every command buffer that took a pair here had its times read. fn calibrated_within_slack(device: &Device) { let counts = device.work_counts(); - let (pairs, readings, most, disagreed) = ( + let (pairs, readings, most, disagreed, loose) = ( counts.calibrations, counts.calibration_readings, counts.calibration_deviation_most, counts.calibration_disagreements, + counts.loosely_calibrated_gpu_times, ); eprintln!( "runtime gpu times: {}: {pairs} calibration pairs of {readings} readings, the largest \ deviation {:.1} us; {disagreed} command buffers whose pair placed their times outside \ - their life, timed by their completion", + their life, timed by their completion; {loose} read as placed by a pair beyond the slack", device.name(), most as f64 / 1e3 ); + if disagreed == 0 { + assert_eq!( + loose > 0, + most > (SLACK * 1e9).round() as u64, + "{loose} command buffers read loosely, the largest deviation {most} ns" + ); + } assert!(pairs > 0, "no calibration pair taken"); assert!( (2 * pairs..=READINGS * pairs).contains(&readings), @@ -919,7 +937,12 @@ fn command_buffers_past_a_pool_s_slots_are_timed_by_pools_made_as_needed() { ); let notices = messages.take_notices(); if scarce { - assert_once(¬ices, &UNTIMED); + // A pair still beyond the slack after its readings, where one was, beside it. + let mut kinds = UNTIMED.to_vec(); + if device.work_counts().loosely_calibrated_gpu_times > 0 { + kinds.extend(LOOSE_GPU_TIMES); + } + assert_once(¬ices, &kinds); let named = format!("{POOL_SLOTS} timestamp slots"); assert!( notices.iter().any(|notice| notice.text.contains(&named)), @@ -927,7 +950,7 @@ fn command_buffers_past_a_pool_s_slots_are_timed_by_pools_made_as_needed() { ); } else { // What reading GPU times announces there, after the reads: nothing on a device - // whose calibration agreed with its timestamps. + // whose calibration agreed with its timestamps and settled. let read: Vec = read_kinds(&device).into_iter().collect(); assert_once(¬ices, &read); } diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index e2825c3eb..326c3afbf 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2342; +pub const RUNTIME_VERSION: u32 = 2343; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 97ef31dac..cb4c9392a 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -3275,6 +3275,7 @@ field metal2vulkan-runtime::WorkCounts.linked_use_questions_asked pub linked_use field metal2vulkan-runtime::WorkCounts.live_tables_uploaded pub live_tables_uploaded: u64 field metal2vulkan-runtime::WorkCounts.long_queue_settles pub long_queue_settles: u64 field metal2vulkan-runtime::WorkCounts.longest_stall_nanos pub longest_stall_nanos: u64 +field metal2vulkan-runtime::WorkCounts.loosely_calibrated_gpu_times pub loosely_calibrated_gpu_times: u64 field metal2vulkan-runtime::WorkCounts.lost_programs_released pub lost_programs_released: u64 field metal2vulkan-runtime::WorkCounts.lost_textures_released pub lost_textures_released: u64 field metal2vulkan-runtime::WorkCounts.makings_held_for_compile_memory pub makings_held_for_compile_memory: u64 @@ -5984,6 +5985,7 @@ macro-member metal2vulkan-runtime::Degradation::LinearAttachmentCopies LinearAtt macro-member metal2vulkan-runtime::Degradation::LinesAtNearestWidth LinesAtNearestWidth = "lines-at-nearest-width", Precision, macro-member metal2vulkan-runtime::Degradation::LinkedSamplerNumbering LinkedSamplerNumbering = "linked-sampler-numbering"; macro-member metal2vulkan-runtime::Degradation::LoopedSampleOutputs LoopedSampleOutputs = "looped-sample-outputs"; +macro-member metal2vulkan-runtime::Degradation::LooselyCalibratedGpuTimes LooselyCalibratedGpuTimes = "loosely-calibrated-gpu-times", Precision, macro-member metal2vulkan-runtime::Degradation::MemorylessBacked MemorylessBacked = "memoryless-backed", Performance, macro-member metal2vulkan-runtime::Degradation::MemorylessTextureBuffer MemorylessTextureBuffer = "memoryless-texture-buffer", Performance, macro-member metal2vulkan-runtime::Degradation::MisplacedNotice MisplacedNotice = "misplaced-notice", Performance, diff --git a/docs/API.md b/docs/API.md index 25ba3ec4d..d68664d5a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2794,6 +2794,14 @@ bound. by more than its stated deviation and the clocks' drift allow. Cost: the end is the host time the runtime saw the command buffer complete, late by that latency, and the start the GPU's exact duration before it. +- `metal2vulkan-degrade-loosely-calibrated-gpu-times` (precision). `LooselyCalibratedGpuTimes`: A + command buffer's GPU times on a device whose calibration, taken as the command buffer completed, + stated a deviation above 20 us though the runtime read it again (four readings at most), and + placed them inside the command buffer's life by the reading of the least deviation, which the + notice's reason states. A reading's deviation holds the time its thread spent off the CPU, so a + loaded host states more. Cost: each GPU time may stand from the moment the GPU wrote it by up to + the deviation its reason states, more than the 20 us a settled calibration reading keeps to; the + duration between them stays exact. - `metal2vulkan-degrade-untimed-command-buffer` (precision). `UntimedCommandBuffer`: A command buffer's GPU times on a device whose timestamp pools had no slot free at the command buffer's commit, and which could make no pool more: every slot was held by a command buffer in flight.