Runtime: held work goes only by room, teardown and send-through, never because a completion round runs
The completion thread set Done::busy and then woke the submit thread (complete.rs, at each round), and a pass that read busy held nothing: every held group and every command buffer held for room went to the VkQueue at once. The wake that announces a completion is sent right after busy is set, so whether room held was a race between a round's length and a thread wake: on the 3090 it mostly held, on the Iris and on the XTX's short arm the backlog flooded past room (A/B jobs m3-max-64198, 64241: B's p90 two to three of A's command buffers). The planner's ruling (A): busy goes. Done::busy, its two stores, the read in pass and the parameter through next_ready, waits_for_room, hold's callers, step, Fifo::goes and unheld are deleted; held work goes when a completion leaves room, at teardown, or on a send-through (a host wait, send_now, a fence's updater). `completed` is still raised before the submit thread's mark is read, so a completion is never missed. While a round runs it sees no completion, so held work waits for the round's end: a completed handler or listener longer than the work in flight leaves the GPU idle for the rest, where Metal would go on. The README records that known gap beside the scheduled-handler one and drops "a slow completed handler holds no later submission". Test: every_device_holds_work_for_room_while_a_completed_handler_runs parks the completion thread in a completed handler, commits three on a queue with room for two, and finds the third held in a group, two submissions sent, once a later commit on another queue is scheduled. With busy it went at once (3 in flight). The waits for !busy in the two_queues and behind_busy helpers and the upload test's trace are gone with it.
This commit is contained in:
1 parent
ba859221ff
commit
3f2be1ea4d
6 files changed
+100
-64
No files matched your search
@@ -1711,10 +1711,7 @@ made, with no lock, and never rewritten for layouts.
|
||||
its scheduled handlers run: `Metrics::held_for_room`), and so are its queue's later command
|
||||
buffers. The held work goes when a completion leaves room (the completion thread wakes the
|
||||
submit thread as it retires; no commit or
|
||||
wait is needed), while the completion thread runs a round of retirements, completed handlers and
|
||||
listeners (it sees no completion until the round ends: a group is held only while the completion
|
||||
thread waits on the GPU, so a slow completed handler holds no later submission), when the host
|
||||
is to wait for one of them (`wait_until_completed`, or
|
||||
wait is needed), when the host is to wait for one of them (`wait_until_completed`, or
|
||||
`CommandQueue::send_now`, the hint for a wait the runtime does not see), or at teardown. Waiting
|
||||
for a place in the queue sends nothing. A segment that signals an event ends its group, which goes
|
||||
at once as it is staged, and so does a presenting command buffer's last (see "Presenting"); so a
|
||||
@@ -1793,8 +1790,11 @@ made, with no lock, and never rewritten for layouts.
|
||||
handlers invoked. Every queue's later submissions wait while it runs, those of command buffers
|
||||
held in a group or for room included, so a scheduled handler that blocks outside the runtime
|
||||
until such a command buffer completes never returns, where Metal, running it on a thread of its
|
||||
own, would go on: a known gap. It may run while another command buffer's completed handler
|
||||
runs on the completion thread. A scheduled handler may read the command buffer's status
|
||||
own, would go on: a known gap. While the completion thread runs a round it sees no completion,
|
||||
so held work waits for the round's end: a completed handler or listener that runs longer than
|
||||
the work in flight leaves the GPU idle for the rest, where Metal, running it on a thread of its
|
||||
own, would go on: a known gap. A scheduled handler may run while another command buffer's
|
||||
completed handler runs on the completion thread. A scheduled handler may read the command buffer's status
|
||||
and times, enqueue command buffers, make objects that take no upload, and do the consumer's own
|
||||
work. Each call that may wait for the submit thread is `Refusal::InScheduledHandler` in it, on
|
||||
every device: `commit`, the waits (`wait_until_scheduled`, `wait_until_completed`,
|
||||
@@ -1806,7 +1806,7 @@ made, with no lock, and never rewritten for layouts.
|
||||
submission's in commit order: the status, the blocks they kept, their recorders and their place
|
||||
in the queue, then the completed handlers. Completed handlers and listeners run on it, in
|
||||
turn, so a slow completed handler delays the later command buffers' retirement, status and
|
||||
handlers, in that order, though not their submission (above). A wait in one for what that thread has not yet done is `Refusal::WaitOnOwnThread`, and one
|
||||
handlers, in that order, and the held work their completions would send (above). A wait in one for what that thread has not yet done is `Refusal::WaitOnOwnThread`, and one
|
||||
already met answers at once: `wait_until_completed` for a command buffer not yet completed (its
|
||||
own handler's command buffer has), `wait_until_scheduled` for one not yet scheduled,
|
||||
`wait_until_signaled_value` for a value not yet reached (a timed wait too: it is refused, never
|
||||
|
||||
@@ -13,7 +13,7 @@ use crate::submit::{Ended, Finished, Upload};
|
||||
use crate::times::{self, Calibration, OwnEnd, Spans};
|
||||
use ash::vk;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -31,10 +31,6 @@ pub(crate) struct Done {
|
||||
wake: vk::Semaphore,
|
||||
/// The highest progress value the completion thread has seen reached.
|
||||
completed: AtomicU64,
|
||||
/// The completion thread runs a round of retirements, completed handlers and listeners, from
|
||||
/// the observation that began it: it sees no completion until the round ends, so the submit
|
||||
/// thread holds no group meanwhile (`crate::submit`).
|
||||
busy: AtomicBool,
|
||||
}
|
||||
|
||||
struct DoneState {
|
||||
@@ -158,7 +154,6 @@ impl Done {
|
||||
progress,
|
||||
wake,
|
||||
completed: AtomicU64::new(0),
|
||||
busy: AtomicBool::new(false),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -184,11 +179,6 @@ impl Done {
|
||||
self.completed.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
/// Whether the completion thread runs a round of retirements, handlers and listeners.
|
||||
pub(crate) fn busy(&self) -> bool {
|
||||
self.busy.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
/// Wakes the completion thread where it waits. With `flight_only` (the change is only more
|
||||
/// work in flight), a thread that already waits for the progress semaphore is left to wait.
|
||||
fn wake(&self, state: &mut DoneState, device: &ash::Device, flight_only: bool) {
|
||||
@@ -563,10 +553,9 @@ pub(crate) fn run(core: Arc<Core>) {
|
||||
if let Some(loss) = newly_lost {
|
||||
core.work.lose(fault::report(&core, loss));
|
||||
}
|
||||
// Set before the submit thread's mark is read (`Work::completed`): a group it keeps
|
||||
// while submissions were in flight goes, a completion having left fewer of them, or
|
||||
// this round, which sees no completion, running while it is held.
|
||||
done.busy.store(true, Ordering::SeqCst);
|
||||
// `completed` was raised before the submit thread's mark is read (`Work::completed`):
|
||||
// what it holds goes as the completion left room. The round sees no completion until
|
||||
// it ends, so held work waits for its end.
|
||||
core.work.completed();
|
||||
core.pool.retired();
|
||||
// Handlers and listeners run with no lock held.
|
||||
@@ -636,7 +625,6 @@ pub(crate) fn run(core: Arc<Core>) {
|
||||
listener(value);
|
||||
}
|
||||
drop(fired);
|
||||
done.busy.store(false, Ordering::SeqCst);
|
||||
state = done.lock();
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -9,9 +9,8 @@
|
||||
//! be in the group, in commit order with every queue's, once its waits are met
|
||||
//! ([`WorkState::hold`]), while other queues' go on. Only its `vkQueueSubmit2` waits for room.
|
||||
//! The group goes when a completion leaves room (the completion thread wakes the thread as
|
||||
//! it retires), while the completion thread runs a round of retirements, completed handlers and
|
||||
//! listeners (it sees no completion until the round ends, so a group is held only while it waits
|
||||
//! on the GPU), when the host is to wait for one of them or one waiting behind it
|
||||
//! it retires; a round of it sees no completion until it ends, and sends nothing), when the host
|
||||
//! is to wait for one of them or one waiting behind it
|
||||
//! ([`Work::send`]), when a command buffer of another queue that waits on a fence one of them
|
||||
//! updates is to go (`crate::fence`), or at teardown. The
|
||||
//! driver runs a submission's command buffers as one kernel job, which a job timeout ends as a
|
||||
@@ -1486,11 +1485,11 @@ impl WorkState {
|
||||
/// Else, of the committed heads that are ended or whose next segment is ready, and of the
|
||||
/// first command buffers not yet scheduled behind heads that wait, ended or with every
|
||||
/// segment's waits met, the earliest committed.
|
||||
fn next_ready(&self, busy: bool) -> Option<(usize, bool)> {
|
||||
fn next_ready(&self) -> Option<(usize, bool)> {
|
||||
let mut held: Option<(u64, usize)> = None;
|
||||
let mut best: Option<(usize, bool, u64)> = None;
|
||||
for (index, fifo) in self.fifos.iter().enumerate() {
|
||||
let waits = self.waits_for_room(fifo, busy);
|
||||
let waits = self.waits_for_room(fifo);
|
||||
if let (false, Some(at)) = (waits, fifo.entries.front().and_then(|head| head.held)) {
|
||||
held = held.filter(|&(first, _)| first < at).or(Some((at, index)));
|
||||
continue;
|
||||
@@ -1529,7 +1528,7 @@ impl WorkState {
|
||||
/// ([`Batch::takes`]) or would go at its signal. With no group held, one that signals an event
|
||||
/// in any segment, which would send its group at once, waits where its queue has no room for
|
||||
/// it. The command buffers behind it wait with it.
|
||||
fn waits_for_room(&self, fifo: &Fifo, busy: bool) -> bool {
|
||||
fn waits_for_room(&self, fifo: &Fifo) -> bool {
|
||||
let Some(head) = fifo.entries.front() else {
|
||||
return false;
|
||||
};
|
||||
@@ -1547,11 +1546,11 @@ impl WorkState {
|
||||
Some(group) => {
|
||||
!group.leans.conflicts(&segment.overs)
|
||||
&& (signals || !group.takes(run, fifo.queue.retired()))
|
||||
&& !fifo.goes(self.stop, busy)
|
||||
&& !fifo.goes(self.stop)
|
||||
}
|
||||
None => {
|
||||
let room = room(&fifo.in_flight, run.unwrap_or(u64::MAX));
|
||||
signals && !(self.stop || busy || fifo.send_through >= head.place || room)
|
||||
signals && !(self.stop || fifo.send_through >= head.place || room)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1630,19 +1629,16 @@ impl WorkState {
|
||||
/// ended command buffer behind its queue's work. A command buffer whose last segment joins a
|
||||
/// group is scheduled at `now`, its scheduled handlers going to `joined`, but one that
|
||||
/// presents a drawable, which `submit` schedules once its submission has passed
|
||||
/// `vkQueueSubmit2`; one ended with nothing held goes to `ended`. With `busy` (the completion
|
||||
/// thread runs a round of retirements and handlers, `Done::busy`) a group goes as each
|
||||
/// command buffer joins it, as no completion would send it until the round ends. Behind a
|
||||
/// head that waits for room, the next command buffer is scheduled instead ([`hold`](Self::hold)).
|
||||
/// `vkQueueSubmit2`; one ended with nothing held goes to `ended`. Behind a head that waits
|
||||
/// for room, the next command buffer is scheduled instead ([`hold`](Self::hold)).
|
||||
fn step(
|
||||
&mut self,
|
||||
now: u64,
|
||||
busy: bool,
|
||||
counters: &Counters,
|
||||
joined: &mut Vec<Joined>,
|
||||
ended: &mut Vec<Ended>,
|
||||
) -> Step {
|
||||
let Some((index, waits)) = self.next_ready(busy) else {
|
||||
let Some((index, waits)) = self.next_ready() else {
|
||||
return Step::Idle;
|
||||
};
|
||||
if waits {
|
||||
@@ -1800,7 +1796,7 @@ impl WorkState {
|
||||
// A command buffer held for room behind it joins it or, not taken, sends it first: with
|
||||
// room the two would have been staged together.
|
||||
let held_behind = (fifo.entries.front()).is_some_and(|entry| entry.held.is_some());
|
||||
match signals || (fifo.goes(stop, busy) && !held_behind) {
|
||||
match signals || (fifo.goes(stop) && !held_behind) {
|
||||
true => Step::Goes(index),
|
||||
false => Step::Staged,
|
||||
}
|
||||
@@ -1877,8 +1873,8 @@ impl WorkState {
|
||||
}
|
||||
|
||||
/// The first FIFO whose group goes at the end of a pass ([`Fifo::goes`]).
|
||||
fn unheld(&self, busy: bool) -> Option<usize> {
|
||||
(self.fifos.iter()).position(|fifo| fifo.goes(self.stop, busy))
|
||||
fn unheld(&self) -> Option<usize> {
|
||||
(self.fifos.iter()).position(|fifo| fifo.goes(self.stop))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1898,12 +1894,13 @@ impl Fifo {
|
||||
(self.entries.front()).and_then(|entry| entry.pending.as_ref())
|
||||
}
|
||||
|
||||
/// Whether it holds a group that goes now: at teardown (`stop`), while the completion thread
|
||||
/// runs a round of retirements and handlers (`busy`), where a host wait is to come for one of
|
||||
/// its command buffers or one behind it, or with room ([`room`]).
|
||||
fn goes(&self, stop: bool, busy: bool) -> bool {
|
||||
/// Whether it holds a group that goes now: at teardown (`stop`), where a host wait is to come
|
||||
/// for one of its command buffers or one behind it, or with room ([`room`]). A round of the
|
||||
/// completion thread sends none: it holds what it saw complete when it began, so held work
|
||||
/// waits for the round to end.
|
||||
fn goes(&self, stop: bool) -> bool {
|
||||
self.group.as_ref().is_some_and(|group| {
|
||||
stop || busy || self.send_through >= group.first || room(&self.in_flight, group.run)
|
||||
stop || self.send_through >= group.first || room(&self.in_flight, group.run)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2098,19 +2095,17 @@ pub(crate) fn run(core: Arc<Core>) {
|
||||
/// goes as it closes, then each group its queue no longer holds, then an upload to flush alone.
|
||||
fn pass<'a>(core: &'a Core, mut state: MutexGuard<'a, WorkState>) -> MutexGuard<'a, WorkState> {
|
||||
let work = &core.work;
|
||||
// Marked before the completed value and the completion thread's busy mark are read: a
|
||||
// completion the read misses, or a round of the completion thread's begun after its read,
|
||||
// sees the mark and wakes the thread.
|
||||
// Marked before the completed value is read: a completion the read misses sees the mark and
|
||||
// wakes the thread.
|
||||
work.keeping.store(true, Ordering::SeqCst);
|
||||
state.settle(core.done.completed());
|
||||
let mut joined: Vec<Joined> = Vec::new();
|
||||
let mut ended: Vec<Ended> = Vec::new();
|
||||
let mut sent = false;
|
||||
loop {
|
||||
let busy = core.done.busy();
|
||||
let now = times::host_time();
|
||||
let index = match state.step(now, busy, &core.counters, &mut joined, &mut ended) {
|
||||
Step::Idle => match state.unheld(busy) {
|
||||
let index = match state.step(now, &core.counters, &mut joined, &mut ended) {
|
||||
Step::Idle => match state.unheld() {
|
||||
Some(index) => index,
|
||||
None => {
|
||||
state.mark_holds(now);
|
||||
|
||||
@@ -1759,9 +1759,7 @@ pub(crate) mod on_device {
|
||||
/// body opens the busy work ([`Busy::open`]) and the completion thread sees a busy one
|
||||
/// complete, and a held group goes no earlier than that. The busy work is opened after `body`
|
||||
/// where it did not, and waited for. First a command buffer of the queue with no commands of
|
||||
/// its own runs and retires, and the completion thread waits on the GPU again
|
||||
/// (`Done::busy`), so a commit in `body` is held rather than sent as that round ends. Then
|
||||
/// the queue's expected GPU run is set to [`BODY_RUN`], the test's own input, so the command
|
||||
/// its own runs and retires. Then the queue's expected GPU run is set to [`BODY_RUN`], the test's own input, so the command
|
||||
/// buffers held behind the busy work join one group (`submit::GROUP_RUN`) however long that
|
||||
/// command buffer's run measured on a loaded host. A group sent after the busy work opens is
|
||||
/// sent at the run its retirements leave, which a loaded host can raise past the bound: a
|
||||
@@ -1776,7 +1774,6 @@ pub(crate) mod on_device {
|
||||
(first.record_for_test(&[], |_, _| {})).expect("a command buffer's commands");
|
||||
first.commit().expect("a commit");
|
||||
finished(name, &first);
|
||||
within(name, || !device.core().done.busy());
|
||||
first.queue().set_run_for_test(BODY_RUN);
|
||||
let before = device.counters().metrics().submissions;
|
||||
let seen = device.core().done.completed();
|
||||
@@ -1868,7 +1865,6 @@ pub(crate) mod on_device {
|
||||
finished(name, &first);
|
||||
first.queue().set_run_for_test(run);
|
||||
}
|
||||
within(name, || !device.core().done.busy());
|
||||
let busy = Busy::new(device);
|
||||
let scheduled = Arc::new(AtomicUsize::new(0));
|
||||
let logged = |queue: &CommandQueue, number: u32| {
|
||||
@@ -1985,6 +1981,65 @@ pub(crate) mod on_device {
|
||||
});
|
||||
}
|
||||
|
||||
/// Held work waits for room while the completion thread runs a round: with a completed handler
|
||||
/// parked, A1 and A2 go, room for two, and A3 is held in a group, though A1 and A2 may have
|
||||
/// ended on the GPU, until the round ends and the completion thread sees them complete. An
|
||||
/// empty command buffer committed after A3 on another queue, scheduled, shows the submit
|
||||
/// thread has looked at A3.
|
||||
#[test]
|
||||
fn every_device_holds_work_for_room_while_a_completed_handler_runs() {
|
||||
each_device("command_room_in_a_round", |name, _, device| {
|
||||
let mut arena = Arena::new(&device);
|
||||
let buffer = arena.buffer();
|
||||
let (a, parked_queue, other) = (queue(&device), queue(&device), queue(&device));
|
||||
let probe = a.command_buffer().expect("a command buffer");
|
||||
probe.queue().set_run_for_test(LONG_RUN);
|
||||
drop(probe);
|
||||
let park = Arc::new(std::sync::Barrier::new(2));
|
||||
let round = parked_queue.command_buffer().expect("a command buffer");
|
||||
(round.record_for_test(&[], |_, _| {})).expect("a command buffer's commands");
|
||||
let parked = Arc::clone(&park);
|
||||
(round.add_completed_handler(move |_| {
|
||||
parked.wait();
|
||||
parked.wait();
|
||||
}))
|
||||
.expect("a handler");
|
||||
round.commit().expect("a commit");
|
||||
park.wait();
|
||||
let mut release = Release::of(&park);
|
||||
let before = device.counters().metrics().submissions;
|
||||
let held: Vec<CommandBuffer> =
|
||||
(1..=3).map(|word| queue_fill(&a, &buffer, word)).collect();
|
||||
let looked = other.command_buffer().expect("a command buffer");
|
||||
looked.commit().expect("a commit");
|
||||
within(name, || looked.status() != CommandBufferStatus::Committed);
|
||||
assert_eq!(
|
||||
(
|
||||
device.core().work.in_flight(held[0].queue()),
|
||||
device.counters().metrics().submissions - before,
|
||||
),
|
||||
((2, true), 2),
|
||||
"{name}: A3 held while the round runs"
|
||||
);
|
||||
release.now();
|
||||
for command_buffer in held.iter().chain([&round]) {
|
||||
finished(name, command_buffer);
|
||||
}
|
||||
assert_eq!(word(&buffer), 3, "{name}");
|
||||
drop((
|
||||
looked,
|
||||
held,
|
||||
round,
|
||||
buffer,
|
||||
arena,
|
||||
a,
|
||||
parked_queue,
|
||||
other,
|
||||
device,
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
/// So does one whose signal is in a later segment: each of A1 to A5 waits after its work for
|
||||
/// a value another event already holds, which ends its first segment (`encodeWaitForEvent`
|
||||
/// splits a segment with commands), then signals; A3 to A5 wait for room whole.
|
||||
|
||||
@@ -342,9 +342,9 @@ mod on_device {
|
||||
/// A wait on a fence no encoder updated waits for nothing, while queue A holds in a group an
|
||||
/// update of another fence: on queue B, a command buffer with work after the wait goes in one
|
||||
/// submission of its own, A's group staying held, and on a third queue an encoder that only
|
||||
/// waits on it completes at once. The submissions are counted first: a command buffer with
|
||||
/// nothing for the GPU retires in a round of the completion thread, and a group held goes
|
||||
/// during such a round (`crate::submit`), so none runs while they are counted.
|
||||
/// waits on it completes at once. The submissions are counted first, with no round of the
|
||||
/// completion thread run since the wait's commit: a command buffer with nothing for the GPU
|
||||
/// retires in one.
|
||||
#[test]
|
||||
fn every_device_runs_a_wait_on_a_fence_never_updated_at_once() {
|
||||
each_device("fence_never_updated", |name, _, device| {
|
||||
@@ -388,7 +388,7 @@ mod on_device {
|
||||
"{name}: after the wait's commit, the submissions: all of them; B's, the \
|
||||
wait's; A's group's, and whether A still holds it; the runtime's own uploads \
|
||||
sent alone; the runtime's own command buffers in them (uploads, copy-ins); \
|
||||
and the completion thread's rounds, during which a group held goes"
|
||||
and the completion thread's rounds"
|
||||
);
|
||||
let empty = c.command_buffer().expect("a command buffer");
|
||||
let mut compute =
|
||||
|
||||
@@ -623,15 +623,13 @@ mod on_device {
|
||||
let mut points = Vec::new();
|
||||
let mut held = Vec::new();
|
||||
let mut run = None;
|
||||
// Each step's submissions counted, progress seen and the completion thread's round,
|
||||
// which a failed check names.
|
||||
// Each step's submissions counted and progress seen, which a failed check names.
|
||||
let mut trace = Vec::new();
|
||||
let mut step = |label: &str| {
|
||||
trace.push(format!(
|
||||
"{label}: {} sent, {} seen, busy {}",
|
||||
"{label}: {} sent, {} seen",
|
||||
device.counters().metrics().submissions,
|
||||
core.done.completed(),
|
||||
core.done.busy()
|
||||
))
|
||||
};
|
||||
behind_busy(name, &device, &queue, |busy, _, _| {
|
||||
|
||||
Reference in new issue
Block a user