diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs index 9589afd03..bb7cb86ca 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -655,7 +655,7 @@ pub(super) struct DeviceShared { pub(super) pipeline_pool: crate::pipeline::pool::Pool, /// The thread that lets go of what a released state's pool pieces gave back as they ended /// (`pipeline/reaper.rs`). - pub(super) reaper: crate::pipeline::reaper::Reaper, + pub(super) reaper: Arc, /// The trails its threads open between a signal and their last let-go of what they held for /// the work, which a command buffer's waiters wait for (`trails.rs`). pub(crate) trails: Arc, diff --git a/crates/metal2vulkan-runtime/src/exit_guard.rs b/crates/metal2vulkan-runtime/src/exit_guard.rs index 78c2b3e0d..f2044c238 100644 --- a/crates/metal2vulkan-runtime/src/exit_guard.rs +++ b/crates/metal2vulkan-runtime/src/exit_guard.rs @@ -1167,10 +1167,9 @@ where /// the scheduler's (`scheduler/`), each `VkQueue`'s waiter, which makes the completion turns /// (`command_buffer/completion.rs`), every thread of the handler /// pool (`contexts/`), the device's lowering thread (`render/lowering_worker.rs`), and a predicated loop's check's (`command_buffer/rerun.rs`), -/// whose work the program's command buffers wait for. A thread one -/// of them starts (the reaper a pipeline state's last release -/// starts on a handler thread, a program's thread made in a handler) does not take either: it -/// starts at the default class on macOS ([`spawn`]), and with no floor on Linux. +/// whose work the program's command buffers wait for. A thread one of them starts (a program's +/// thread made in a handler) does not take either: it starts at the default class on macOS +/// ([`spawn`]), and with no floor on Linux. pub(crate) fn spawn_user_interactive( builder: std::thread::Builder, f: F, @@ -1300,14 +1299,13 @@ fn run_at_user_interactive() { // before. // // Reset on fork: a thread this one starts begins with no floor (`uclamp_fork`). Without it, - // every thread started from one of these would keep the floor for its life: the reaper a - // pipeline state's last release starts on a handler thread, the notices thread, a pipeline - // pool's or a compile's thread started by a program's call in a handler, a program's own - // thread made there. The kernel's reset also starts such a thread at `SCHED_OTHER` where this - // one runs at a real-time policy, and at nice 0 where this one's nice is negative - // (`sched_fork`); `SCHED_BATCH`, `SCHED_IDLE` and a nice of 0 or more carry over as before. A - // thread without `CAP_SYS_NICE` cannot clear the setting again, the program's code in a - // handler included. + // every thread started from one of these would keep the floor for its life: the notices + // thread, a pipeline pool's or a compile's thread started by a program's call in a handler, a + // program's own thread made there. The kernel's reset also starts such a thread at + // `SCHED_OTHER` where this one runs at a real-time policy, and at nice 0 where this one's nice + // is negative (`sched_fork`); `SCHED_BATCH`, `SCHED_IDLE` and a nice of 0 or more carry over as + // before. A thread without `CAP_SYS_NICE` cannot clear the setting again, the program's code + // in a handler included. // // Host-wide: the first call with a utilization clamp since the host booted turns the kernel's // clamp accounting on (`static_branch_enable(&sched_uclamp_used)` in `uclamp_validate`) for diff --git a/crates/metal2vulkan-runtime/src/pipeline/piece_holds.rs b/crates/metal2vulkan-runtime/src/pipeline/piece_holds.rs index d6d79407e..dc4d075c3 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/piece_holds.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/piece_holds.rs @@ -20,14 +20,15 @@ //! releasing thread. //! - **A release never waits** (the coordinator's rule: Metal's release never blocks, and a //! making takes 20 to 50 s on NVIDIA). The state's release lets go of what was kept and given -//! back at once; a piece running then gives back to the device's reaper (`reaper.rs`), which -//! the release started, and its thread lets go of it. Where the host cannot start that thread, -//! the piece lets go of it itself. A piece that panics drops what it took on its thread. +//! back at once, and starts no thread; a piece running then gives back to the device's reaper +//! (`reaper.rs`), which the piece starts unless it runs, and its thread lets go of it. Where +//! the host cannot start that thread, the piece lets go of it itself. A piece that panics drops +//! what it took on its thread. //! - **With its trail.** A piece that settled its making before it gives back gives back with the //! trail it opened before the settle (`trails.rs`): it closes once the state keeps what was //! given back, or once the reaper (or the piece, without one) let go of it. -use super::reaper::{Reaper, Reaps}; +use super::reaper::Reaper; use crate::device::lock; use crate::trails::Trail; use std::collections::HashMap; @@ -42,8 +43,8 @@ struct Holds { /// The state let go: no piece takes what was kept any more. closed: bool, /// Where the pieces running as the state let go give back: the device's reaper, which the - /// release started; `None` before, and where the host could not start it. - reaps: Option, + /// first of them starts; `None` before. + reaper: Option>, /// The leases not ended. running: usize, next: u64, @@ -69,7 +70,7 @@ impl PieceHolds { Arc::new(PieceHolds { state: Mutex::new(Holds { closed: false, - reaps: None, + reaper: None, running: 0, next: 0, waiting: HashMap::new(), @@ -96,12 +97,13 @@ impl PieceHolds { } /// The state's release, returning at once: no piece takes what was kept any more; a piece - /// running gives back to `reaper`, started here; everything kept is let go of on this thread. - pub(crate) fn close(&self, reaper: &Reaper) { + /// running gives back to `reaper`, which it starts ([`Lease::give_back`]), never this + /// release; everything kept is let go of on this thread. + pub(crate) fn close(&self, reaper: &Arc) { let mut state = lock(&self.state); state.closed = true; if state.running > 0 { - state.reaps = reaper.start(); + state.reaper = Some(Arc::clone(reaper)); } state.waiting.clear(); state.given_back.clear(); @@ -133,8 +135,8 @@ struct Reaped { impl Lease { /// Give back what remains of what the piece took, and end the lease: kept by the state until /// a hand-over's thread or the state's release lets go of it, or, once the state let go, - /// handed to the reaper. `trail`, the piece's where it settled its making before, closes - /// once the state keeps it, or once it was let go of. + /// handed to the reaper, whose thread it starts unless it runs. `trail`, the piece's where it + /// settled its making before, closes once the state keeps it, or once it was let go of. pub(crate) fn give_back(self, held: T, trail: Option) { let mut state = lock(&self.holds.state); if !state.closed { @@ -143,14 +145,17 @@ impl Lease { drop(trail); return; } - match &state.reaps { + // The reaper's thread starts here, outside the lock, which the release takes: a start + // can wait (`reaper.rs`). + let reaper = state.reaper.clone(); + drop(state); + match reaper.and_then(|reaper| reaper.start()) { Some(reaps) => reaps.reap(Box::new(Reaped { _held: held, _trail: trail, })), - // No reaper thread: let go of it here, outside the lock, then of the trail. + // No reaper thread: let go of it here, then of the trail. None => { - drop(state); drop(held); drop(trail); } @@ -196,7 +201,7 @@ mod tests { #[test] fn what_a_piece_gave_back_goes_on_the_next_hand_over_s_thread() { let (noted, dropped) = mpsc::channel(); - let reaper = Reaper::default(); + let reaper = Arc::new(Reaper::default()); let holds = PieceHolds::new(); let keeping = holds.keep(Noted(noted.clone())); exit_guard::spawn(Builder::new(), move || { @@ -222,7 +227,7 @@ mod tests { #[test] fn a_piece_of_a_released_state_takes_nothing() { let (noted, dropped) = mpsc::channel(); - let reaper = Reaper::default(); + let reaper = Arc::new(Reaper::default()); let holds = PieceHolds::new(); let keeping = holds.keep(Noted(noted)); holds.close(&reaper); @@ -235,12 +240,13 @@ mod tests { ); } - /// The state's release returns while a piece runs, and what the piece gives back as it - /// ends goes on the reaper's thread, never on the piece's. + /// The state's release returns while a piece runs, and starts no thread (a start can wait, + /// `reaper.rs`); what the piece gives back as it ends goes on the reaper's thread, which the + /// piece started, never on the piece's. #[test] fn the_state_s_release_returns_while_a_piece_runs_and_the_reaper_lets_go_of_it() { let (noted, dropped) = mpsc::channel(); - let reaper = Reaper::default(); + let reaper = Arc::new(Reaper::default()); let holds = PieceHolds::new(); let keeping = holds.keep(Noted(noted)); let (started, running) = mpsc::channel(); @@ -258,8 +264,10 @@ mod tests { dropped.try_recv().is_err(), "the release let go of the running piece's" ); + assert!(!reaper.runs(), "the release started the reaper's thread"); end.send(()).unwrap(); piece.join().unwrap(); + assert!(reaper.runs(), "the piece did not start the reaper's thread"); assert_eq!(dropped.recv_timeout(WAIT).unwrap(), "metal2vulkan-reaper"); reaper.stop(); } @@ -278,7 +286,7 @@ mod tests { } let trails = Arc::new(Trails::default()); let (noted, dropped) = mpsc::channel(); - let reaper = Reaper::default(); + let reaper = Arc::new(Reaper::default()); let holds = PieceHolds::new(); let keeping = holds.keep(Counted(Arc::clone(&trails), noted)); let (started, running) = mpsc::channel(); diff --git a/crates/metal2vulkan-runtime/src/pipeline/reaper.rs b/crates/metal2vulkan-runtime/src/pipeline/reaper.rs index a71082347..baccfd369 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/reaper.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/reaper.rs @@ -2,9 +2,13 @@ //! of what a released render state's pool pieces give back as they end, so the state's release //! returns at once and a pool thread is never the last holder of the device. //! -//! - **Started by a release.** A state released while a piece of its own is running starts the -//! thread ([`Reaper::start`], on the releasing thread), unless it runs; until then the device -//! has none. +//! - **Started by a released state's piece.** A piece of a state released while it ran starts the +//! thread as it gives back ([`Reaper::start`], on the pool's thread), unless it runs; until then +//! the device has none. The release only notes the reaper (`piece_holds.rs`), as a thread's +//! start can wait: on an Intel Iris Xe (Mesa 26.2.6) beside the runtime suite, 20 of 8,655 +//! releases that started the thread took 16 to 32 ms, and in the 8 timed by part the start took +//! all of it but 0.1 ms, the releasing thread asleep (one voluntary switch, at most 1.2 ms on a +//! CPU and 0.7 ms waiting for one). //! - **What a piece gave back.** Such a piece, as it ends, hands what it took ([`Reaps::reap`]), //! with the trail it opened before settling its making (`trails.rs`), which closes after it; //! the thread lets go of each, in turn, outside any section of the exit guard: each release of a @@ -47,6 +51,12 @@ struct Queue { } impl Reaper { + /// Whether its thread was started and not yet joined. + #[cfg(test)] + pub(crate) fn runs(&self) -> bool { + lock(&self.thread).is_some() + } + /// Start the thread unless it runs, and where the pieces hand what they give back; `None` /// where the host cannot start a thread. pub(crate) fn start(&self) -> Option {