A released state's release starts no thread; the piece giving back starts the reaper
On an Intel Iris Xe (ANV, Mesa 26.2.6) beside the runtime suite, 20 of 8,655 releases of a render or compute state whose pool piece was still running took 16 to 32 ms: the release started the device's reaper thread on the releasing thread, and in the 8 runs timed by part the thread start took all of it but 0.1 ms, the releasing thread asleep (one voluntary switch). Metal's release never blocks, and teardown_threads' release bound failed there (12 of 4,530 and 8 of 4,125 loaded runs). The release now only notes the device's reaper (an Arc) in the state's holds; the first piece that gives back after the release starts the reaper's thread, on the pool's thread and outside the holds' lock, and hands it what it gave back. Where no thread can be started the piece lets go of it itself, as before. The reaper's thread still starts at the default class (exit_guard::spawn). Test: piece_holds' release-while-a-piece-runs test asserts the release started no thread and the piece did. Iris A/B under load: see the land commit. (Fix written by exitcrash on steelbrain/iris-flakes-wip 78fcea640e, unrun there.) (cherry picked from commit b6aa117ac559d7b819d885f200d54a66424df61b)
This commit is contained in:
1 parent
7265c23bbd
commit
7dabd402c0
4 files changed
+53
-37
No files matched your search
@@ -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<crate::pipeline::reaper::Reaper>,
|
||||
/// 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<crate::trails::Trails>,
|
||||
|
||||
@@ -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<F, T>(
|
||||
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
|
||||
|
||||
@@ -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<T> {
|
||||
/// 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<Reaps>,
|
||||
/// first of them starts; `None` before.
|
||||
reaper: Option<Arc<Reaper>>,
|
||||
/// The leases not ended.
|
||||
running: usize,
|
||||
next: u64,
|
||||
@@ -69,7 +70,7 @@ impl<T: Send + 'static> PieceHolds<T> {
|
||||
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<T: Send + 'static> PieceHolds<T> {
|
||||
}
|
||||
|
||||
/// 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<Reaper>) {
|
||||
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<T> {
|
||||
impl<T: Send + 'static> Lease<T> {
|
||||
/// 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<Trail>) {
|
||||
let mut state = lock(&self.holds.state);
|
||||
if !state.closed {
|
||||
@@ -143,14 +145,17 @@ impl<T: Send + 'static> Lease<T> {
|
||||
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();
|
||||
|
||||
@@ -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<Reaps> {
|
||||
|
||||
Reference in new issue
Block a user