diff --git a/crates/metal2vulkan-runtime/src/device/submit_stalls.rs b/crates/metal2vulkan-runtime/src/device/submit_stalls.rs index 597530dce..1df6cab92 100644 --- a/crates/metal2vulkan-runtime/src/device/submit_stalls.rs +++ b/crates/metal2vulkan-runtime/src/device/submit_stalls.rs @@ -1,29 +1,34 @@ //! A driver's stall in one of every so many signalling submissions to a `VkQueue`, taken while //! the device's scheduler is idle. //! -//! **The driver.** NVIDIA's proprietary driver spends 2.2 to 3.7 ms in one `vkQueueSubmit` of -//! every 1024 that signal a fence or a semaphore on a `VkQueue`, whatever the call submits -//! (615.71.09, `driverVersion` 0x99d1c240, on an RTX 5080 Laptop GPU under Linux; 8192 calls a -//! form, timed one by one): one command buffer and a timeline signal, no command buffer and a -//! timeline signal, a fence alone, `vkQueueSubmit2`, and four signalling submit infos in one call -//! each stall once in 1024 calls, 100 us apart or back to back; a call that signals nothing never -//! stalls. The count is the `VkQueue`'s: two queues of one device, two devices of one process and -//! two processes each stall once in 1024 of their own calls; `vkQueueWaitIdle` counts as one. The -//! other calls take 1.6 us (p50). Vulkan states no such cost. Each batch the runtime submits -//! signals its queue's timeline, so one command buffer in 1024 waited there, and every one -//! committed behind it with it: `host_ab present` at 120 Hz (25 command buffers a frame) held a -//! submission 2.3 to 5.1 ms every 341.6 ms, in 30 of 1200 frames. +//! **The driver.** NVIDIA's proprietary driver spends 2 to 4 ms (1.97 to 4.06 ms measured) in one +//! `vkQueueSubmit` of every 1024 that signal a fence or a semaphore on a `VkQueue`, whatever the +//! call submits (615.71.09, `driverVersion` 0x99d1c240, on an RTX 5080 Laptop GPU under Linux; +//! 8192 calls a form, timed one by one): one command buffer and a timeline signal, no command +//! buffer and a timeline signal, a fence alone, `vkQueueSubmit2`, and four signalling submit infos +//! in one call each stall once in 1024 calls, 100 us apart or back to back; a call that signals +//! nothing never stalls. The count is the `VkQueue`'s: two queues of one device, two devices of +//! one process and two processes each stall once in 1024 of their own calls; `vkQueueWaitIdle` +//! counts as one. The other calls take 1.6 us (p50), though a few elsewhere take a millisecond or +//! more (a device's 62nd submission 10 ms, its 162nd 1.2 to 1.4 ms). Vulkan states no such cost. +//! Each batch the runtime submits signals its queue's timeline, so one command buffer in 1024 +//! waited there, and every one committed behind it with it: `host_ab present` at 120 Hz (25 +//! command buffers a frame) held a submission 2.3 to 5.1 ms every 341.6 ms, in 30 of 1200 frames. //! //! **Taken idle** ([`Period`], [`IdleStalls`]), where the driver has the cost fact //! (`DriverFact::SignallingSubmitsStallEvery1024`, the translator's `device/driver_facts.rs`): //! //! - **Learnt per queue.** Each signalling call the runtime makes under a queue's lock and times //! (a batch's, a fence's with no work, the device's own submission, an empty one of the plan's) -//! is counted; one that took [`STALL_NANOS`] or more is the driver's stall, and the count starts -//! again from it. A use of the queue the runtime cannot count (the program's, a sparse binding, -//! a wait for its idle) forgets the count until the next stall, as does a due stall that did not -//! come: a release without the stall submits no empty submission after its first miss, and none -//! before a stall was seen. +//! is counted; one that took [`STALL_NANOS`] or more is taken as the driver's stall, and the +//! count starts again from it, unless the place is confirmed and the call is not where the +//! count puts the next stall (within [`MARGIN`] calls before it): there it is another of the +//! driver's costs. The place is confirmed once a stall came where the count put it, so a long +//! call elsewhere before then (a device's first submissions) gives way to the next stall, and +//! one after leaves the place. A use of the queue the runtime cannot count (the program's, a +//! sparse binding, a wait for its idle) forgets the count until the next stall, as does a due +//! stall that did not come: a release without the stall submits no empty submission after its +//! first miss, and none before a stall was seen. //! - **Taken while idle.** Once the scheduler's thread has had nothing to submit for //! [`IDLE_NANOS`] and a queue's next stall is due within its next burst of work (the most //! signalling calls it made between two such idles over its last [`BURSTS`], plus [`MARGIN`]), @@ -50,9 +55,11 @@ use std::sync::{Arc, Mutex}; /// The signalling calls to a `VkQueue` from one stall of the driver's to the next. pub(crate) const PERIOD: u32 = 1024; -/// A signalling call that took this long or longer, in nanoseconds, is the driver's stall: the -/// stalls measured took 2.2 ms or more, and no other signalling call 1.6 ms. -pub(crate) const STALL_NANOS: u64 = 2_000_000; +/// A signalling call that took this long or longer, in nanoseconds, is the driver's stall where +/// the count puts it, or where the queue's place is not confirmed ([`Period::called`]): the +/// stalls measured took 1.97 ms or more (3 of 15 under 2 ms in the device test on the RTX 5080), +/// and other signalling calls up to 1.4 ms, a device's 62nd 10 ms. +pub(crate) const STALL_NANOS: u64 = 1_000_000; /// How long the scheduler's thread has had nothing to submit before it takes a stall due, in /// nanoseconds: it begins a gap between bursts of work (a frame's), not one between two commits @@ -102,6 +109,9 @@ pub(crate) struct Period { /// The signalling calls since the last stall seen, each counted: `None` before one is seen, /// once a use was not counted, or once a stall due did not come. since: Option, + /// The place `since` counts from is confirmed: that stall came where the count before it put + /// the stall. A long call elsewhere then is another of the driver's costs, and leaves it. + confirmed: bool, /// The signalling calls of work in the burst under way. burst: u32, /// The last bursts' calls, a ring. @@ -118,6 +128,7 @@ impl Period { /// A use of the queue the runtime cannot count: no stall is due until one is seen. pub(crate) fn forget(&mut self) { self.since = None; + self.confirmed = false; self.idle_stall = false; } @@ -130,10 +141,16 @@ impl Period { } /// A signalling call of `caller`'s that took `nanos` and ended at host time `end`, in - /// nanoseconds: whether it was the driver's stall. A call of work [`IDLE_NANOS`] or more - /// after the one before begins a burst. + /// nanoseconds: whether it was the driver's stall, a call of [`STALL_NANOS`] or more where + /// the count puts the next stall (the [`PERIOD`]th call since the last, or up to [`MARGIN`] + /// calls before it), which confirms the place, or anywhere while the place is not confirmed. + /// A call of work [`IDLE_NANOS`] or more after the one before begins a burst. pub(crate) fn called(&mut self, caller: Caller, nanos: u64, end: u64) -> bool { - let stalled = nanos >= STALL_NANOS; + let expected = (self.since).is_some_and(|since| since + 1 + MARGIN >= PERIOD); + let stalled = nanos >= STALL_NANOS && (expected || !self.confirmed); + if stalled { + self.confirmed = expected; + } if caller == Caller::Work { let gap = end.saturating_sub(nanos).saturating_sub(self.last_end); if gap >= IDLE_NANOS { @@ -146,7 +163,10 @@ impl Period { self.since = match (stalled, self.since) { (true, _) => Some(0), // The stall due did not come: the count is wrong, or the driver stalls no more. - (false, Some(since)) if since + 1 >= PERIOD => None, + (false, Some(since)) if since + 1 >= PERIOD => { + self.confirmed = false; + None + } (false, since) => since.map(|since| since + 1), }; stalled diff --git a/crates/metal2vulkan-runtime/src/tests/submit_stalls.rs b/crates/metal2vulkan-runtime/src/tests/submit_stalls.rs index 09e91989b..a028feadd 100644 --- a/crates/metal2vulkan-runtime/src/tests/submit_stalls.rs +++ b/crates/metal2vulkan-runtime/src/tests/submit_stalls.rs @@ -10,11 +10,15 @@ const STALL: u64 = 2_300_000; const CALL: u64 = 2_000; /// A driver whose every [`PERIOD`]th signalling call to the queue stalls, its first at call -/// `first`, and every `early`th period `by` calls early; or none, where `stalls` is false. +/// `first`, and every `early`th period `by` calls early; or none, where `stalls` is false. Its +/// stalls take `stall`, and the calls of `costs` (each a call's number and its time) take long +/// elsewhere. struct Driver { calls: u64, first: u64, stalls: bool, + stall: u64, + costs: Vec<(u64, u64)>, /// The call that last stalled, and the periods so far. last: Option, periods: u64, @@ -27,14 +31,16 @@ impl Driver { calls: 0, first, stalls, + stall: STALL, + costs: Vec::new(), last: None, periods: 0, early: None, } } - /// One signalling call: its time. - fn call(&mut self) -> u64 { + /// One signalling call: its time, and whether it stalled. + fn call(&mut self) -> (u64, bool) { self.calls += 1; let period = match self.early { Some((every, by)) if (self.periods + 1).is_multiple_of(every) => u64::from(PERIOD) - by, @@ -46,22 +52,25 @@ impl Driver { Some(last) => self.calls - last >= period, }; if !stalls { - return CALL; + let cost = self.costs.iter().find(|(call, _)| *call == self.calls); + return (cost.map_or(CALL, |(_, took)| *took), false); } if self.last.is_some() { self.periods += 1; } self.last = Some(self.calls); - STALL + (self.stall, true) } } -/// What a run of frames did: the stalls on work, those taken idle, and the empty calls made. +/// What a run of frames did: the stalls on work, those taken idle, and the empty calls made, as +/// the plan counts them; and the driver's stalls that fell on work. #[derive(Debug, Default, PartialEq, Eq)] struct Run { on_work: u32, idle: u32, empty: u32, + driver_on_work: u32, } /// `frames` frames of `per_frame` submissions 15 us apart, each frame followed by an idle of 8 ms @@ -70,16 +79,17 @@ fn frames(driver: &mut Driver, frames: u32, per_frame: u32, take: bool) -> Run { let (mut period, mut run, mut now) = (Period::default(), Run::default(), 0); for _ in 0..frames { for _ in 0..per_frame { - let took = driver.call(); + let (took, stalled) = driver.call(); now += took + 15_000; if period.called(Caller::Work, took, now) { run.on_work += 1; } + run.driver_on_work += u32::from(stalled); } now += 8_000_000; if take && period.may_be_due() { for _ in 0..period.due_idle() { - let took = driver.call(); + let (took, _) = driver.call(); now += took; run.empty += 1; if period.called(Caller::Empty, took, now) { @@ -123,6 +133,56 @@ fn a_stall_that_comes_early_is_taken_idle() { assert_eq!(u64::from(with.idle), driver.periods, "{with:?}"); } +/// As on the RTX 5080 (615.71.09) in the device test: stalls of 1.97 ms, the device's 62nd +/// submission 10 ms and its 162nd 1.3 ms before the first stall, and a call of 1.26 ms well +/// before a later one. The long calls before the first stall give way to it; the one after it is +/// no stall; every stall after the first is taken idle, each by no more empty calls than a burst +/// and the margin. +#[test] +fn long_calls_elsewhere_and_short_stalls_spend_no_empty_call() { + let mut driver = Driver::new(997, true); + driver.stall = 1_970_000; + driver.costs = vec![(62, 10_000_000), (162, 1_300_000), (5013, 1_260_000)]; + let with = frames(&mut driver, 200, 25, true); + assert_eq!(driver.periods, 4, "{with:?}"); + assert_eq!(with.driver_on_work, 1, "{with:?}"); + assert_eq!(with.idle, 4, "{with:?}"); + assert!(with.empty <= with.idle * (25 + MARGIN), "{with:?}"); +} + +/// Once a stall came where the count put it, a long call elsewhere is no stall and leaves the +/// place: the next stall is still due where the count puts it. +#[test] +fn a_long_call_away_from_a_confirmed_place_leaves_it() { + let mut period = Period::default(); + let mut now = 0; + let mut call = |period: &mut Period, took: u64| { + now += took + 15_000; + period.called(Caller::Work, took, now) + }; + assert!(call(&mut period, STALL)); + for _ in 0..PERIOD - 1 { + assert!(!call(&mut period, CALL)); + } + // Where the count put it: the place is confirmed. + assert!(call(&mut period, STALL)); + for _ in 0..500 { + assert!(!call(&mut period, CALL)); + } + assert!( + !call(&mut period, 10_000_000), + "a long call away from the place" + ); + for _ in 0..PERIOD - 502 { + assert!(!call(&mut period, CALL)); + } + assert!(period.may_be_due()); + assert!( + call(&mut period, STALL_NANOS), + "the stall where the count put it" + ); +} + /// A driver that never stalls gets no empty call: no place is learnt. #[test] fn no_stall_seen_takes_none() { diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 2c5596471..19d1b4992 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -3472,11 +3472,14 @@ pub struct WorkCounts { /// (NVIDIA's; `device/submit_stalls.rs`): the calls left before the stall, where it was due /// within the queue's next burst of work. pub idle_submits: u64, - /// Of those, the ones that took the stall: 2.2 ms or more each on the RTX 5080 measured. + /// Of those, the ones that took the stall: 1.97 ms or more each on the RTX 5080 measured. pub submit_stalls_idle: u64, - /// Signalling submissions of work that took 2 ms or more in the driver (on a driver with - /// the stall above, the stall where none was taken idle: before the first was seen, or after - /// a use of the queue the runtime cannot count). + /// Signalling submissions of work the runtime took for the driver's stall: 1 ms or more in + /// the driver, where the count of the queue's calls puts the stall, or anywhere before that + /// place is confirmed (`device/submit_stalls.rs`). On a driver with the stall above, the stall + /// where none was taken idle (before the first was seen, or after a use of the queue the + /// runtime cannot count), and before the place is confirmed, another long call of the + /// driver's too (a device's first batches). pub submit_stalls_on_work: u64, /// Work that came while a stall taken idle ran: a submission that waited 10 us or more for /// its queue's lock behind it, and a commit that reached the scheduler meanwhile. diff --git a/crates/metal2vulkan-validation/runtime-sources.sha256 b/crates/metal2vulkan-validation/runtime-sources.sha256 index 81f89dbc5..e8d309f88 100644 --- a/crates/metal2vulkan-validation/runtime-sources.sha256 +++ b/crates/metal2vulkan-validation/runtime-sources.sha256 @@ -1 +1 @@ -58fdd6a92ba611d4f24c6fd335171b10897cca8b7fe704a6eeb1b3927133fe15 +e3d7d54968425c51fd01119a8ed84179f883e0b06d6028f3ac05e320a8f5aae4 diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index f455b6636..3ec51aeb2 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -93,7 +93,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 478; /// 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 = 1696; +pub const RUNTIME_VERSION: u32 = 1697; /// 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/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index ec4ceb819..6a55ec39d 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -83,7 +83,7 @@ known. The rest of the differences are docs/API.md section 9's precision notes. | Float simdgroup reductions sum in the device's association order. | Apple's order is not modelled. | `ReductionOrder` (`reduction-order`) | | A vertex or fragment function keeps more than 16 KiB a thread of private memory, up to the 192 KiB Metal holds, on a device whose Function storage was not measured: it runs on the driver's scratch, which past what the driver holds answers wrongly or hangs the GPU with no error. | Vulkan states no amount of Function storage; only a measurement of the device does. No other form is exact: a fragment function's helper invocations may not write the memory it would move to, and a vertex function's, a compute pre-pass, is not built. | `FunctionStorageAssumed` (`function-storage-assumed`) | | A render pipeline of more color attachments than the device draws at once (the lesser of `maxColorAttachments` and `maxFragmentOutputAttachments`, four at Vulkan's floor; Metal takes eight) draws each draw once per group of that many, each group in a render pass of its own over a twin of the pass's depth and stencil. Every variant's vertex stage is one module, `Position` invariant, and every fragment variant's `FragDepth`, `SampleMask` and `FragStencilRefEXT` are `Invariant`; groups past the first may differ at discard or depth edges if the driver compiles the variants differently. | The device holds no more attachments in one subpass. Vulkan's invariance rules make only identical pipelines repeat exactly, and the groups' fragment modules and attachments differ. | `ColorAttachmentGroups` (`color-attachment-groups`) | -| On NVIDIA's driver (615.71.09 on an RTX 5080 Laptop GPU) one `vkQueueSubmit` in every 1024 that signal on a `VkQueue` takes 2.2 to 3.7 ms in the driver, which holds the command buffer whose batch it falls on, and the ones committed behind it, from the GPU that long. | The cost is the driver's, and every batch signals its completion. The scheduler's thread takes the stall while idle, by empty submissions, once one was seen (`DriverFact::SignallingSubmitsStallEvery1024`, `device/submit_stalls.rs`): it falls on work only before then, after a use of the queue the runtime cannot count, or on a queue that never idles 1 ms. Whether the driver's stall also holds other threads' semaphore calls is unsettled: a probe polling the timeline from a second thread during 10 stalls saw 5 of its 9 slow calls (up to 7.1 ms) overlap one in one run, and none of its 3 in another; in `host_ab present` the completion handlers of command buffers that completed during a stall taken idle ran 126 us (p50, 341 at most) after their GPU end, as elsewhere. | None (timing only; `WorkCounts::submit_stalls_on_work`, `WorkCounts::submit_stalls_idle`) | +| On NVIDIA's driver (615.71.09 on an RTX 5080 Laptop GPU) one `vkQueueSubmit` in every 1024 that signal on a `VkQueue` takes 2 to 4 ms in the driver (1.97 to 4.06 ms measured), which holds the command buffer whose batch it falls on, and the ones committed behind it, from the GPU that long. | The cost is the driver's, and every batch signals its completion. The scheduler's thread takes the stall while idle, by empty submissions, once one was seen (`DriverFact::SignallingSubmitsStallEvery1024`, `device/submit_stalls.rs`; a call of 1 ms or more, and once one came where the count put it, only there): it falls on work only before then, after a use of the queue the runtime cannot count, or on a queue that never idles 1 ms. Whether the driver's stall also holds other threads' semaphore calls is unsettled: a probe polling the timeline from a second thread during 10 stalls saw 5 of its 9 slow calls (up to 7.1 ms) overlap one in one run, and none of its 3 in another; in `host_ab present` the completion handlers of command buffers that completed during a stall taken idle ran 126 us (p50, 341 at most) after their GPU end, as elsewhere. | None (timing only; `WorkCounts::submit_stalls_on_work`, `WorkCounts::submit_stalls_idle`) | | Command buffers that share no resource run one after another, where Metal overlaps them while the GPU has room. | The runtime orders nothing between them and runs at each driver's floor for one completion signal per command buffer; a signal shared by several would hold an earlier one's completion for the later ones' work. | None (timing only) | | On MoltenVK a light queue's command buffer waits for the latest one another queue committed, where Metal waits for none: MoltenVK fences every encoder of every queue device-wide when argument buffers and a residency set are on. | The runtime needs argument buffers, and MoltenVK has no setting for its residency set; `runtime_lane::queue_storms` judges the runtime against Metal under those fences. | None (timing only) | | A `Counting` visibility result of a draw writing more than one color attachment, on ANV (Mesa 26.2.4): Metal's count multiplied by the colors written, not divided back. `Boolean` results stay exact. | The driver counts each sample once per color output (`RuntimeDriverDefect::OcclusionCountPerColorOutput`), no documented Vulkan form counts it once, and a refusal would withhold the Mac2 and Metal3 families. | `VisibilityCountPerColor` (`visibility-count-per-color`) |