diff --git a/crates/metal2vulkan-runtime/src/allocations.rs b/crates/metal2vulkan-runtime/src/allocations.rs index 96083eace..6703d465d 100644 --- a/crates/metal2vulkan-runtime/src/allocations.rs +++ b/crates/metal2vulkan-runtime/src/allocations.rs @@ -35,7 +35,7 @@ //! released, `host_ram.rs` its chunks' with theirs), and allocates nothing, so the wait ends. use crate::free_thread::FreeThread; -use crate::step_aside::Activity; +use crate::step_aside::{Activity, OwnCall}; use ash::vk; use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::{Arc, Condvar, Mutex, OnceLock, PoisonError}; @@ -361,12 +361,16 @@ pub(crate) fn freed(allocations: Option<&Allocations>) { /// An allocation or free of the device of `allocations` in progress on this thread, counted for /// its checks to step aside for (`step_aside.rs`) until this goes: none where the device is not -/// registered or this is a check's thread. -pub(crate) struct Busy<'a>(Option<&'a Allocations>); +/// registered or this is a check's thread, whose own call on a registered device is owed to its +/// next step instead ([`OwnCall`]). +pub(crate) struct Busy<'a>(Option<&'a Allocations>, Option); impl<'a> Busy<'a> { pub(crate) fn of(allocations: Option<&'a Allocations>) -> Busy<'a> { - Busy(allocations.filter(|allocations| allocations.activity.begin())) + Busy( + allocations.filter(|allocations| allocations.activity.begin()), + allocations.and_then(|_| OwnCall::begin()), + ) } } @@ -375,6 +379,8 @@ impl Drop for Busy<'_> { if let Some(allocations) = self.0 { allocations.activity.end(); } + // A check's own call ends: its length is owed now. + drop(self.1.take()); } } diff --git a/crates/metal2vulkan-runtime/src/step_aside.rs b/crates/metal2vulkan-runtime/src/step_aside.rs index a35f9378c..0d1b429f4 100644 --- a/crates/metal2vulkan-runtime/src/step_aside.rs +++ b/crates/metal2vulkan-runtime/src/step_aside.rs @@ -1,7 +1,8 @@ //! The device's checks step aside for its other work: before each step that allocates or frees //! memory, maps or unmaps pages, or submits, a check's thread (`submit_check.rs`, -//! `queue_switch_check.rs`, `in_place_check.rs`) waits until no allocation or free of the -//! device's is in progress on another thread and none ended in the last [`QUIET`]. +//! `queue_switch_check.rs`, `in_place_check.rs`) first waits as long as its own allocations and +//! frees since its last step took, then until no allocation or free of the device's is in +//! progress on another thread and none ended in the last [`QUIET`]. //! //! - **Why.** A device whose checks have no record runs them on threads of their own as it is //! made (the submission checks) or at the first use that asks (the others), beside the program's @@ -19,6 +20,20 @@ //! threads' work for them (the guest RAM's imports ahead, the free thread's frees). A making //! that allocates nothing (a buffer in a block already allocated) is not seen, nor waits on a //! check in the driver. +//! - **A check's own calls give way** ([`OwnCall`]). A check's allocation or free of memory at +//! the same two doors may hold the driver against the device's other threads for as long as it +//! runs, so its length is owed: the thread's next step waits as long first. On an RTX 3090 +//! (NVIDIA 620.70.06, Linux), each `vkFreeMemory` of a host-pointer import held back every +//! other thread's `vkQueueSubmit` while it ran. A probe outside the runtime made and freed 32 +//! imports of 512 KiB in turn beside one fill submitted 4,800 times a second: each free took +//! 0.52 to 0.80 ms (p50) with the GPU otherwise idle, and 0.82 to 0.90 ms beside 30 ms command +//! buffers on another queue. The fills' latency went from 0.12 to 3.9 and 8.3 ms (p50) alone, and +//! from 0.74 to 11.4 and 12.7 ms beside the long work. The submission checks' import tax +//! (`submit_check.rs`) freed its imports back to back, so the scheduler's thread got a +//! submission in only between two frees, and a compositor's command buffers beside long work +//! waited up to 21 ms from commit to submission. Paced so, the device's other threads' calls go +//! between the check's own, whose allocations and frees hold the driver at most half the time +//! they run; a call of microseconds is owed microseconds. //! - **Never for a waiter.** A step steps aside for no one while a program's call waits for the //! check's verdicts (`SubmitChecks::wait`, `InPlaceChecks::wait`), so such a wait is the check's //! own run alone. @@ -64,6 +79,9 @@ thread_local! { static CHECKS: Cell = const { Cell::new(false) }; /// Its makings, releases and submissions are counted as the checks' ([`Counted::Apart`]). static APART: Cell = const { Cell::new(false) }; + /// How long its own allocations and frees took since its last step, owed to its next + /// ([`OwnCall`]). + static OWED: Cell = const { Cell::new(Duration::ZERO) }; } /// Whose counts a check's thread's makings, releases and submissions are. @@ -95,6 +113,25 @@ pub(crate) fn counted_apart() -> bool { APART.with(Cell::get) } +/// A check's own allocation or free of memory in progress on its thread, at one of the two doors +/// (`allocations::Busy`): as it ends, its length is owed to the thread's next step (the module +/// docs). +pub(crate) struct OwnCall(Instant); + +impl OwnCall { + /// One beginning now, where this is a check's thread. + pub(crate) fn begin() -> Option { + on_a_check_s_thread().then(|| OwnCall(Instant::now())) + } +} + +impl Drop for OwnCall { + fn drop(&mut self) { + let took = self.0.elapsed(); + OWED.with(|owed| owed.set(owed.get().saturating_add(took))); + } +} + /// One device's allocations and frees in progress on threads not its checks', and when the last /// ended. pub(crate) struct Activity { @@ -147,12 +184,20 @@ impl Activity { QUIET.saturating_sub(since).min(SLICE) } - /// Wait, on a check's thread, until a step may go on ([`Activity::left`]), `stop` says to - /// go on now (the check stops, or a call waits for its verdicts) or it waited - /// [`MOST_ASIDE`]: the time it waited, `None` where it went on at once. + /// Wait, on a check's thread, as long as its own calls since its last step took + /// ([`OwnCall`]), then until a step may go on ([`Activity::left`]), `stop` says to go on now + /// (the check stops, or a call waits for its verdicts) or it waited [`MOST_ASIDE`]: the time + /// it waited, `None` where it went on at once. pub(crate) fn step_aside(&self, stop: impl Fn() -> bool) -> Option { let began = Instant::now(); + let owed = OWED + .with(|owed| owed.replace(Duration::ZERO)) + .min(MOST_ASIDE); let mut waited = false; + if !owed.is_zero() && !stop() { + std::thread::sleep(owed); + waited = true; + } loop { let left = self.left(); let past = began.elapsed(); @@ -203,6 +248,33 @@ mod tests { assert_eq!(activity.step_aside(|| true), None, "told to go on"); } + /// A check's own call is owed to its next step once, which waits as long first; a step told + /// to go on pays nothing. The test's own thread is marked, as below. + #[test] + fn a_check_s_own_call_is_owed_to_its_next_step() { + let activity = Activity::new(); + mark_a_check_s_thread(Counted::Apart); + let call = Duration::from_millis(5); + { + let _call = OwnCall::begin().expect("a check's thread"); + std::thread::sleep(call); + } + let waited = activity.step_aside(|| false).expect("it waited"); + assert!(waited >= call, "waited {waited:?}"); + assert_eq!(activity.step_aside(|| false), None, "owed once"); + { + let _call = OwnCall::begin().expect("a check's thread"); + std::thread::sleep(call); + } + assert_eq!(activity.step_aside(|| true), None, "told to go on"); + assert_eq!(activity.step_aside(|| false), None, "nothing left owed"); + } + + #[test] + fn a_call_on_another_thread_is_owed_nothing() { + assert!(OwnCall::begin().is_none()); + } + /// The test's own thread is marked, for the rest of its life: each test has a thread of its /// own, and threads start only through the exit guard (`tests::exit_guard`). #[test] diff --git a/crates/metal2vulkan-runtime/src/submit_check.rs b/crates/metal2vulkan-runtime/src/submit_check.rs index e0dbf74d4..300ccb7b1 100644 --- a/crates/metal2vulkan-runtime/src/submit_check.rs +++ b/crates/metal2vulkan-runtime/src/submit_check.rs @@ -134,7 +134,11 @@ //! (`step_aside.rs`): it goes on once none is in progress and none ended for //! `step_aside::QUIET`, at once while a call waits for its verdicts (`ANSWER_WAIT`, //! `Device::settle_submit_checks`). Beside a program's burst of makings its verdicts so come -//! after the burst. +//! after the burst. Each step first waits as long as the thread's own allocations and frees +//! since the last one took (`step_aside::OwnCall`): NVIDIA's driver holds back every other +//! thread's `vkQueueSubmit` while it frees a pointer import, so the import tax's 32 frees in a +//! row held the device's submissions for their whole run, and a compositor's command buffers +//! beside long work waited up to 21 ms. Paced, the device's submissions go between them. //! - **Its thread** holds nothing of the device, as the queue-switch check's does //! (`queue_switch_check.rs`): only the parts of it the checks work with ([`Parts`]), never a //! handle, so the device's last handle never goes on it. It reads the stop between its steps diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 6e3938632..fa2bc62dc 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -4762,8 +4762,9 @@ pub struct DeviceCheckWork { /// uses of a queue (`WorkCounts::queue_submissions` leaves them out); the submission checks' /// are `WorkCounts::submit_check_calls`. pub queue_submissions: u64, - /// Steps of the device's checks that waited for the device's allocations and frees on other - /// threads before they went on (`step_aside.rs`). + /// Steps of the device's checks that waited before they went on: as long as their own + /// allocations and frees since their last step took, then for the device's allocations and + /// frees on other threads (`step_aside.rs`). pub steps_aside: u64, /// The nanoseconds those steps waited, in all. pub aside_nanos: u64,