rt: a wait for a shared event's value is Scheduled once the value is reached
Before, rt reported a command buffer as Scheduled, and ran its scheduled
handlers, as soon as its last segment joined a submission, even when that
segment waited for a shared event's value whose signal was submitted and still
running. Metal does not. Apple's MTLCommandBuffer.h defines
MTLCommandBufferStatusScheduled as "All dependencies have been resolved and
the command buffer has been scheduled for execution", and Metal applies that
to shared-event waits only. The recording event-wait-behind-a-running-signal
(9e698176bc, M3 Max, five runs alike) shows it. A command buffer is committed
on a second queue behind one whose long fills run before its signals. While
the signaller still runs (it reads Scheduled, and its companion signal has not
landed):
- order/event (MTLEvent): the waiter reads Scheduled, handlers ["scheduled"].
- order/shared-event (MTLSharedEvent): the waiter reads Committed, handlers
ran nothing, signaledValue 0.
The rule here follows the event's kind, a fact of the Metal type the consumer
made (newSharedEvent), never an identity. Applying it to both kinds would turn
the passing MTLEvent row into a divergence.
What changes is only when rt reports Scheduled. When it submits does not
change: the waiter still goes to the driver once its signal is submitted, so
the GPU chains the two with no CPU round trip.
- EventInner carries shared (set by new_shared_event) and a count of the held
waits on it (holds).
- When a command buffer's last segment joins a submission, the submit thread
collects its waits on a shared event whose value the host does not yet see
reached (floor or observed): Pending::held. With any, it neither schedules
the command buffer nor runs its scheduled handlers. A presenting command
buffer with any is not scheduled at submit either.
- The completion thread keeps those command buffers with their scheduled
handlers. Each round, after it observes the watched events and the host's
sets, it schedules each one whose waits are reached, or that retires in this
round, before any retirement. So Scheduled always comes before Completed,
including when the waiter completes in the same batch as its signaller. One
that retires with an error keeps the failed-submission rule: its scheduled
handlers run as it retires.
- A host set wakes the completion thread while holds > 0.
Tests:
- tests::command::every_device_schedules_a_wait_for_a_shared_event_once_its_
value_is_reached. A plain event's wait (signal pretended as submitted) reads
Scheduled with its handler at submission. A shared event's wait reads
Committed with no handler after its submission is counted, and is scheduled
by the host's set while its GPU wait still holds it. A third, signalled only
while the completion thread is held and the queue is idle, is scheduled in
the round that retires it, its scheduled handler before its completed one.
Without the change, both shared waiters read Scheduled at submission.
- every_device_ends_its_work_at_a_loss_and_a_new_device_opens: its command
buffer in flight waits for a shared event's value, so it now stays Committed
while submitted. The test waits for its submission instead of its Scheduled.
Docs: CommandBufferStatus::Scheduled, add_scheduled_handler,
Device::new_shared_event and the runtime README's submission and handler
paragraphs.
This commit is contained in:
1 parent
c9637b0d08
commit
deca5649b3
6 files changed
+297
-25
No files matched your search
@@ -853,14 +853,22 @@ made, with no lock, and never rewritten for layouts.
|
||||
command buffer makes before its wait. Other queues go on. `Scheduled` means the last segment is in
|
||||
a submission: passed to `vkQueueSubmit2`, or in a group held; a command buffer that presents a
|
||||
drawable is never held, and is scheduled once its submission has passed `vkQueueSubmit2` (see
|
||||
"Presenting"). A command buffer whose segments hold no commands, no event wait and no event
|
||||
signal takes no submission: the submit thread schedules it as it reaches it, and it completes
|
||||
"Presenting"). One that waits for a shared event's value not yet reached when its last segment
|
||||
joins a submission stays `Committed` until the value is: the completion thread schedules it, and
|
||||
runs its scheduled handlers, once it sees the signal land or the host set the value, and before
|
||||
it completes where its own completion comes first. Apple defines `Scheduled` as "All dependencies
|
||||
have been resolved and the command buffer has been scheduled for execution", and Metal applies
|
||||
it so to shared-event waits only: recorded on the M3 Max, a command buffer behind a signal still
|
||||
running reads `Committed` with no handler where it waits for an `MTLSharedEvent`, and `Scheduled`
|
||||
with its scheduled handler run where it waits for an `MTLEvent`, as here. A command buffer whose
|
||||
segments hold no commands, no event wait and no event signal takes no submission: the submit thread schedules it as it reaches it, and it completes
|
||||
with its queue's command buffer before it, in flight, or at once where that one has completed. Where that one is in a group held, it joins the group, which
|
||||
goes at once, so its completion waits only on submitted work.
|
||||
- A command buffer's scheduled handlers run on the submit thread, once it is scheduled and before
|
||||
its submission goes to the driver (one that presents a drawable: after, before the completion
|
||||
thread learns of it; one that ends without a submission: once it ends, before it retires), with
|
||||
no lock of the runtime's held. In the handler its status is `Scheduled` and its kernel times are
|
||||
thread learns of it; one that ends without a submission: once it ends, before it retires; one
|
||||
held for a shared event's value: on the completion thread, once it is reached), with no lock of
|
||||
the runtime's held. In the handler its status is `Scheduled` and its kernel times are
|
||||
set, and it runs before the command buffer's completed handlers. The queue's
|
||||
later submissions wait while it runs, and 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
|
||||
|
||||
@@ -61,7 +61,12 @@ pub enum CommandBufferStatus {
|
||||
/// `vkQueueSubmit2`, or in a group its queue holds while three of its submissions run, which
|
||||
/// goes with no other call. A command buffer that presents a drawable
|
||||
/// ([`CommandBuffer::present_drawable`]) is never held: it is scheduled once its submission
|
||||
/// has passed `vkQueueSubmit2`.
|
||||
/// has passed `vkQueueSubmit2`. One that waits for a [`SharedEvent`](crate::SharedEvent)'s
|
||||
/// value not yet reached when its last segment joins a submission is scheduled once the value
|
||||
/// is, as the completion thread sees the signal or the host's set, and always before it
|
||||
/// completes. Apple defines the status as "All dependencies have been resolved and the command
|
||||
/// buffer has been scheduled for execution"; Metal applies that to a shared event's wait only
|
||||
/// ([`Device::new_shared_event`](crate::Device::new_shared_event)).
|
||||
Scheduled,
|
||||
/// `MTLCommandBufferStatusCompleted`.
|
||||
Completed,
|
||||
@@ -277,6 +282,9 @@ pub(crate) struct Pending {
|
||||
pub(crate) handlers: Handlers,
|
||||
/// It took a pair of timestamp queries.
|
||||
pub(crate) timed: bool,
|
||||
/// Its waits for a shared event's value not reached when its last segment joined a
|
||||
/// submission: it is scheduled once they are (`crate::complete`), not as it joins.
|
||||
pub(crate) held: Vec<(Arc<EventInner>, u64)>,
|
||||
}
|
||||
|
||||
impl Pending {
|
||||
@@ -479,7 +487,10 @@ impl CommandBuffer {
|
||||
/// before its submission goes to the driver, or for one that ends without running once it
|
||||
/// ends, before it retires. For one that presents a drawable
|
||||
/// ([`present_drawable`](Self::present_drawable)) it runs once its submission has passed
|
||||
/// `vkQueueSubmit2`, and is not yet in flight for the completion thread. In the handler the command buffer's status is
|
||||
/// `vkQueueSubmit2`, and is not yet in flight for the completion thread. For one that waits
|
||||
/// for a shared event's value not yet reached as it is submitted
|
||||
/// ([`CommandBufferStatus::Scheduled`]) it runs on the completion thread once the value is,
|
||||
/// before the command buffer's completed handlers. In the handler the command buffer's status is
|
||||
/// [`CommandBufferStatus::Scheduled`] and its kernel times are set, and it runs before the
|
||||
/// command buffer's completed handlers. Its queue's later submissions wait while it runs, and
|
||||
/// it may run while another command buffer's completed handler runs on the completion thread.
|
||||
@@ -605,6 +616,7 @@ impl CommandBuffer {
|
||||
gpu,
|
||||
handlers,
|
||||
timed: stamps.is_some(),
|
||||
held: Vec::new(),
|
||||
};
|
||||
state.status = CommandBufferStatus::Committed;
|
||||
self.inner.changed.notify_all();
|
||||
@@ -1359,7 +1371,9 @@ pub(crate) fn not_in_scheduled_handler() -> Result<(), Error> {
|
||||
|
||||
/// On the submit thread, with no lock of the runtime's held: runs the scheduled handlers of a
|
||||
/// command buffer scheduled, before its submission goes to the driver (after, for one that
|
||||
/// presents a drawable) and before it is handed on to the completion thread.
|
||||
/// presents a drawable) and before it is handed on to the completion thread. On the completion
|
||||
/// thread for one held at `Committed` for a shared event's value (`Pending::held`), before it
|
||||
/// retires.
|
||||
pub(crate) fn run_scheduled(inner: Arc<CbInner>, handlers: Vec<Handler>) {
|
||||
/// Clears the mark as the handlers end, a panic among them too.
|
||||
struct Running;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
//! The completion thread: it retires command buffers and uploads as the GPU completes them, runs
|
||||
//! their completed handlers, follows the event signals it submitted and runs listeners, and ends the work in
|
||||
//! flight at a loss.
|
||||
//! their completed handlers, follows the event signals it submitted and runs listeners, schedules
|
||||
//! the command buffers held at `Committed` for a shared event's value once it is reached, and ends
|
||||
//! the work in flight at a loss.
|
||||
|
||||
use crate::command::{self, CbInner, CommandBufferError, Gpu, Handlers, Pending};
|
||||
use crate::command::{self, CbInner, CommandBufferError, Gpu, Handler, Handlers, Pending};
|
||||
use crate::counters::Metric;
|
||||
use crate::device::{lock, Core};
|
||||
use crate::error::Error;
|
||||
@@ -38,8 +39,11 @@ struct DoneState {
|
||||
ended: Vec<Retiree>,
|
||||
/// Events with submitted signals the completion thread has not yet seen.
|
||||
watched: Vec<Arc<EventInner>>,
|
||||
/// Events whose listeners a host value may reach.
|
||||
/// Events whose listeners, or whose held command buffers' waits, a host value may reach.
|
||||
fire: Vec<Arc<EventInner>>,
|
||||
/// Submitted command buffers held at `Committed` for a shared event's value
|
||||
/// (`Pending::held`), each scheduled once its waits are reached, or as it retires.
|
||||
held: Vec<Held>,
|
||||
waiting: Waiting,
|
||||
/// The wake semaphore's last value.
|
||||
woken: u64,
|
||||
@@ -64,6 +68,35 @@ struct Retiree {
|
||||
item: Item,
|
||||
}
|
||||
|
||||
/// A command buffer held at `Committed` for a shared event's value: its waits and its scheduled
|
||||
/// handlers.
|
||||
struct Held {
|
||||
cb: Arc<CbInner>,
|
||||
waits: Vec<(Arc<EventInner>, u64)>,
|
||||
scheduled: Vec<Handler>,
|
||||
}
|
||||
|
||||
impl Held {
|
||||
/// Whether the value the host sees reaches each of its waits.
|
||||
fn reached(&self) -> bool {
|
||||
(self.waits.iter()).all(|(event, value)| event.reached(*value))
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes out of `held` the command buffers to schedule now: those whose waits are reached, and
|
||||
/// those that retire in `retire`, which are scheduled, where they ran, before they complete. Each
|
||||
/// event's count of held waits falls with them.
|
||||
fn due(held: &mut Vec<Held>, retire: &[Retiree]) -> Vec<Held> {
|
||||
let due: Vec<Held> = (held.extract_if(.., |entry| {
|
||||
entry.reached() || retire.iter().any(|retiree| retiree.is(&entry.cb))
|
||||
}))
|
||||
.collect();
|
||||
for (event, _) in due.iter().flat_map(|entry| &entry.waits) {
|
||||
lock(&event.state).holds -= 1;
|
||||
}
|
||||
due
|
||||
}
|
||||
|
||||
enum Item {
|
||||
Command {
|
||||
cb: Arc<CbInner>,
|
||||
@@ -130,6 +163,7 @@ impl Done {
|
||||
ended: Vec::new(),
|
||||
watched: Vec::new(),
|
||||
fire: Vec::new(),
|
||||
held: Vec::new(),
|
||||
waiting: Waiting::Not,
|
||||
woken: 0,
|
||||
stop: false,
|
||||
@@ -195,7 +229,7 @@ impl Done {
|
||||
watch: Vec<Arc<EventInner>>,
|
||||
) {
|
||||
let mut state = self.lock();
|
||||
let flight_only = watch.is_empty();
|
||||
let mut flight_only = watch.is_empty();
|
||||
if let Some(upload) = upload {
|
||||
state.flight.push_back(Retiree {
|
||||
value,
|
||||
@@ -204,7 +238,24 @@ impl Done {
|
||||
}
|
||||
for (cb, pending, error) in finished {
|
||||
let written = pending.stamps_written();
|
||||
let Pending { gpu, handlers, .. } = pending;
|
||||
let Pending {
|
||||
gpu,
|
||||
mut handlers,
|
||||
held,
|
||||
..
|
||||
} = pending;
|
||||
// Held at `Committed`: the thread looks at its waits from its next round.
|
||||
if !held.is_empty() {
|
||||
for (event, _) in &held {
|
||||
lock(&event.state).holds += 1;
|
||||
}
|
||||
state.held.push(Held {
|
||||
cb: Arc::clone(&cb),
|
||||
waits: held,
|
||||
scheduled: std::mem::take(&mut handlers.scheduled),
|
||||
});
|
||||
flight_only = false;
|
||||
}
|
||||
state.flight.push_back(Retiree {
|
||||
value,
|
||||
item: Item::Command {
|
||||
@@ -256,7 +307,8 @@ impl Done {
|
||||
self.wake(&mut state, &core.device, false);
|
||||
}
|
||||
|
||||
/// A host value of `event` may reach some of its listeners.
|
||||
/// A host value of `event` may reach some of its listeners, or the waits of command buffers
|
||||
/// held for it.
|
||||
pub(crate) fn fire(&self, core: &Core, event: Arc<EventInner>) {
|
||||
let mut state = self.lock();
|
||||
state.fire.push(event);
|
||||
@@ -314,6 +366,24 @@ impl Retiree {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether it is `cb`.
|
||||
fn is(&self, cb: &Arc<CbInner>) -> bool {
|
||||
matches!(&self.item, Item::Command { cb: own, .. } if Arc::ptr_eq(own, cb))
|
||||
}
|
||||
|
||||
/// Where it is `cb` and ends with an error: its handlers, which its scheduled handlers join.
|
||||
fn failed(&mut self, cb: &Arc<CbInner>) -> Option<&mut Handlers> {
|
||||
match &mut self.item {
|
||||
Item::Command {
|
||||
cb: own,
|
||||
handlers,
|
||||
error: Some(_),
|
||||
..
|
||||
} if Arc::ptr_eq(own, cb) => Some(handlers),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// In flight at a loss.
|
||||
fn lost(mut self) -> Retiree {
|
||||
if let Item::Command { error, .. } = &mut self.item {
|
||||
@@ -453,7 +523,8 @@ pub(crate) fn run(core: Arc<Core>) {
|
||||
for event in &fired {
|
||||
event.observe(None, &mut fire);
|
||||
}
|
||||
if newly_lost || !retire.is_empty() || !fire.is_empty() {
|
||||
let due = due(&mut state.held, &retire);
|
||||
if newly_lost || !retire.is_empty() || !fire.is_empty() || !due.is_empty() {
|
||||
state.waiting = Waiting::Not;
|
||||
drop(state);
|
||||
if newly_lost {
|
||||
@@ -463,6 +534,20 @@ pub(crate) fn run(core: Arc<Core>) {
|
||||
// A batch kept while submissions were in flight may go.
|
||||
core.work.completed();
|
||||
}
|
||||
// Each held command buffer whose waits are reached, or that retires now, is scheduled
|
||||
// before any retires, so before it completes. One that retires with an error is not:
|
||||
// its scheduled handlers run as it retires, as a failed submission's do.
|
||||
for Held { cb, scheduled, .. } in due {
|
||||
match retire.iter_mut().find_map(|retiree| retiree.failed(&cb)) {
|
||||
Some(handlers) => handlers.scheduled = scheduled,
|
||||
None => {
|
||||
cb.scheduled(times::host_time());
|
||||
if !scheduled.is_empty() {
|
||||
command::run_scheduled(cb, scheduled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Handlers and listeners run with no lock held.
|
||||
// Each completed command buffer's timestamps are placed on the host clock by the
|
||||
// last calibration, read again first where the period has passed since it, and moved
|
||||
|
||||
@@ -37,6 +37,10 @@ pub(crate) type Listener = Box<dyn FnOnce(u64) + Send>;
|
||||
/// The event itself: what command buffers and the completion thread hold.
|
||||
pub(crate) struct EventInner {
|
||||
pub(crate) semaphore: vk::Semaphore,
|
||||
/// Made by `newSharedEvent`: a command buffer that waits for a value of it not yet reached
|
||||
/// when it is submitted is scheduled once the value is (`crate::complete`), as Metal holds it
|
||||
/// at `Committed` until then.
|
||||
pub(crate) shared: bool,
|
||||
core: Arc<Core>,
|
||||
pub(crate) state: Mutex<EventState>,
|
||||
/// Signalled whenever the value the host sees rises, and at a loss.
|
||||
@@ -54,6 +58,9 @@ pub(crate) struct EventState {
|
||||
pub(crate) pending: VecDeque<u64>,
|
||||
/// In the completion thread's watch list: while signals are pending.
|
||||
pub(crate) watched: bool,
|
||||
/// The waits of submitted command buffers held at `Committed` for a value of it
|
||||
/// (`crate::complete`): while there are any, a host set wakes the completion thread.
|
||||
pub(crate) holds: usize,
|
||||
listeners: Vec<(u64, Listener)>,
|
||||
/// The device was lost while signals were pending: they never come.
|
||||
lost: bool,
|
||||
@@ -64,12 +71,18 @@ impl Device {
|
||||
///
|
||||
/// Err: [`Error::Vulkan`] where the semaphore cannot be made.
|
||||
pub fn new_event(&self) -> Result<Event, Error> {
|
||||
self.event(false)
|
||||
}
|
||||
|
||||
/// An event, a shared one where `shared`.
|
||||
fn event(&self, shared: bool) -> Result<Event, Error> {
|
||||
let core = self.core();
|
||||
let semaphore = timeline(&core.device)?;
|
||||
core.counters.rise(Gauge::Events);
|
||||
Ok(Event {
|
||||
inner: Arc::new(EventInner {
|
||||
semaphore,
|
||||
shared,
|
||||
core: Arc::clone(core),
|
||||
state: Mutex::new(EventState {
|
||||
submitted: 0,
|
||||
@@ -77,6 +90,7 @@ impl Device {
|
||||
observed: 0,
|
||||
pending: VecDeque::new(),
|
||||
watched: false,
|
||||
holds: 0,
|
||||
listeners: Vec::new(),
|
||||
lost: false,
|
||||
}),
|
||||
@@ -88,10 +102,21 @@ impl Device {
|
||||
|
||||
/// `newSharedEvent`.
|
||||
///
|
||||
/// A command buffer that waits for a value of it is `Scheduled`, with its scheduled handlers
|
||||
/// run, once that value is reached, never while the signal it waits for is still running on
|
||||
/// the GPU; a wait for an [`Event`]'s value is scheduled as its command buffer is submitted.
|
||||
/// Apple defines `MTLCommandBufferStatusScheduled` as "All dependencies have been resolved and
|
||||
/// the command buffer has been scheduled for execution", and Metal applies it so to
|
||||
/// shared-event waits only. Recorded on the M3 Max (`event-wait-behind-a-running-signal`, five
|
||||
/// runs alike), a command buffer committed on another queue behind one whose signal still runs
|
||||
/// (that one `Scheduled`, its signal not landed) reads `Scheduled` with its scheduled handler
|
||||
/// run where it waits for an `MTLEvent`, and `Committed` with no handler run and the value 0
|
||||
/// where it waits for an `MTLSharedEvent`.
|
||||
///
|
||||
/// Err: [`Error::Vulkan`] where the semaphore cannot be made.
|
||||
pub fn new_shared_event(&self) -> Result<SharedEvent, Error> {
|
||||
Ok(SharedEvent {
|
||||
event: self.new_event()?,
|
||||
event: self.event(true)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -140,7 +165,7 @@ impl SharedEvent {
|
||||
}
|
||||
state.floor = value;
|
||||
inner.changed.notify_all();
|
||||
state.listeners.iter().any(|(at, _)| *at <= value)
|
||||
state.holds > 0 || state.listeners.iter().any(|(at, _)| *at <= value)
|
||||
};
|
||||
let core = &inner.core;
|
||||
core.work.poke();
|
||||
@@ -233,7 +258,15 @@ impl SharedEvent {
|
||||
/// on the semaphore, which the test then signals from the host.
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
pub(crate) fn pretend_submitted_for_test(&self, value: u64) {
|
||||
let mut state = lock(&self.event.inner.state);
|
||||
self.event.pretend_submitted_for_test(value);
|
||||
}
|
||||
}
|
||||
|
||||
impl Event {
|
||||
/// As [`SharedEvent::pretend_submitted_for_test`].
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
pub(crate) fn pretend_submitted_for_test(&self, value: u64) {
|
||||
let mut state = lock(&self.inner.state);
|
||||
state.submitted = state.submitted.max(value);
|
||||
}
|
||||
}
|
||||
@@ -285,6 +318,13 @@ impl EventInner {
|
||||
self.changed.notify_all();
|
||||
}
|
||||
|
||||
/// Whether the value the host sees, its floor or the semaphore's value the completion thread
|
||||
/// has seen, reaches `value`.
|
||||
pub(crate) fn reached(&self, value: u64) -> bool {
|
||||
let state = lock(&self.state);
|
||||
state.observed.max(state.floor) >= value
|
||||
}
|
||||
|
||||
/// The value the completion thread waits for next: the lowest pending signal.
|
||||
pub(crate) fn next_pending(&self) -> Option<u64> {
|
||||
lock(&self.state).pending.front().copied()
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
|
||||
use crate::buffer::{Buffer, DeviceCopy};
|
||||
use crate::capture::{self, Changes, Chunk, CopyIn, Modified, Named, CAPTURE_LEN};
|
||||
use crate::command::{self, CbInner, CommandBufferError, Handler, Pending, Segment};
|
||||
use crate::command::{self, CbInner, CommandBufferError, Gpu, Handler, Pending, Segment};
|
||||
use crate::complete;
|
||||
use crate::counters::{Counters, Gauge, Metric};
|
||||
use crate::device::{lock, Core};
|
||||
@@ -1446,7 +1446,9 @@ 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`.
|
||||
/// `vkQueueSubmit2`, and one that waits for a shared event's value not yet reached, which the
|
||||
/// completion thread schedules once it is ([`held`]); one ended with nothing held goes to
|
||||
/// `ended`.
|
||||
fn step(&mut self, now: u64, joined: &mut Vec<Joined>, ended: &mut Vec<Ended>) -> Step {
|
||||
let Some(index) = self.next_ready() else {
|
||||
return Step::Idle;
|
||||
@@ -1623,7 +1625,8 @@ impl WorkState {
|
||||
else {
|
||||
return Step::Staged;
|
||||
};
|
||||
if !pending.gpu.presents() {
|
||||
pending.held = held(&pending.gpu);
|
||||
if !pending.gpu.presents() && pending.held.is_empty() {
|
||||
cb.scheduled(now);
|
||||
let scheduled = std::mem::take(&mut pending.handlers.scheduled);
|
||||
if !scheduled.is_empty() {
|
||||
@@ -1763,6 +1766,20 @@ impl Fifo {
|
||||
}
|
||||
}
|
||||
|
||||
/// The waits of `gpu`'s segments for a shared event's value the host does not yet see reached,
|
||||
/// as its last segment joins a submission: its command buffer stays `Committed` until the
|
||||
/// completion thread sees each reached (`Pending::held`). Metal holds a command buffer that waits
|
||||
/// for an `MTLSharedEvent`'s value at `Committed`, with no scheduled handler run, while the
|
||||
/// signal it waits for runs, and schedules one that waits for an `MTLEvent`'s, so a plain event's
|
||||
/// wait holds nothing here.
|
||||
fn held(gpu: &Gpu) -> Vec<(Arc<EventInner>, u64)> {
|
||||
(gpu.segments.iter())
|
||||
.flat_map(|segment| &segment.waits)
|
||||
.filter(|(event, value)| event.shared && !event.reached(*value))
|
||||
.map(|(event, value)| (Arc::clone(event), *value))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Whether every wait of `segment` is met by the host's value or by a submitted signal.
|
||||
fn ready(segment: &Segment) -> bool {
|
||||
segment.waits.iter().all(|(event, value)| {
|
||||
@@ -2127,10 +2144,11 @@ fn submit(core: &Core, value: u64, upload: Option<Upload>, batch: Batch) {
|
||||
// signals the consumer's semaphores has passed `vkQueueSubmit2`: a present made from here,
|
||||
// its own scheduled handlers' included, waits on a submitted signal. They run before the
|
||||
// completion thread learns of it below, so before its completed handlers. One whose
|
||||
// submission failed is not scheduled; its scheduled handlers run as it retires.
|
||||
// submission failed is not scheduled; its scheduled handlers run as it retires. One held
|
||||
// for a shared event's value is scheduled by the completion thread ([`held`]).
|
||||
let now = times::host_time();
|
||||
for (cb, pending, error) in &mut finished {
|
||||
if error.is_none() && pending.gpu.presents() {
|
||||
if error.is_none() && pending.gpu.presents() && pending.held.is_empty() {
|
||||
cb.scheduled(now);
|
||||
let scheduled = std::mem::take(&mut pending.handlers.scheduled);
|
||||
if !scheduled.is_empty() {
|
||||
|
||||
@@ -889,6 +889,111 @@ pub(crate) mod on_device {
|
||||
});
|
||||
}
|
||||
|
||||
/// A command buffer whose wait for a shared event's value is met only by a submitted signal
|
||||
/// not yet run (one the test makes as if submitted, then gives from the host) is submitted and
|
||||
/// stays `Committed`, no scheduled handler run, until the value is reached, and is scheduled
|
||||
/// before it completes: by the host's set of the value, while its GPU wait still holds it, and
|
||||
/// where the value arrives only as it completes (the completion thread held meanwhile), in
|
||||
/// the round that retires it. A wait for a plain event's value submitted so is scheduled as
|
||||
/// it is submitted. Metal answers so (`event-wait-behind-a-running-signal`, recorded on the M3
|
||||
/// Max): `Committed` with no handler for an `MTLSharedEvent`, `Scheduled` with its scheduled
|
||||
/// handler run for an `MTLEvent`.
|
||||
#[test]
|
||||
fn every_device_schedules_a_wait_for_a_shared_event_once_its_value_is_reached() {
|
||||
each_device("shared_wait_scheduled", |name, _, device| {
|
||||
let mut arena = Arena::new(&device);
|
||||
let (a, b, c) = (arena.buffer(), arena.buffer(), arena.buffer());
|
||||
let queue = queue(&device);
|
||||
let log = Log::default();
|
||||
let signal = |semaphore: vk::Semaphore| {
|
||||
let signal = vk::SemaphoreSignalInfo::default()
|
||||
.semaphore(semaphore)
|
||||
.value(1);
|
||||
// SAFETY: the semaphore is the device's, and 1 is above its value.
|
||||
unsafe { device.raw().device.signal_semaphore(&signal) }.expect("a host signal");
|
||||
};
|
||||
// Commits a command buffer that waits for `event` at 1 and fills `buffer`, and
|
||||
// returns it once its submission is counted.
|
||||
let waiting = |label: &str, event: &crate::Event, buffer: &Buffer, word| {
|
||||
let command_buffer = queue.command_buffer().expect("a command buffer");
|
||||
(command_buffer.encode_wait_for_event(event, 1)).expect("a wait");
|
||||
fill(&command_buffer, buffer, word);
|
||||
log.handlers(label, &command_buffer);
|
||||
let before = device.counters().metrics().submissions;
|
||||
command_buffer.commit().expect("a commit");
|
||||
within(name, || device.counters().metrics().submissions > before);
|
||||
command_buffer
|
||||
};
|
||||
|
||||
// A plain event's wait: scheduled as it is submitted.
|
||||
let plain = device.new_event().expect("an event");
|
||||
plain.pretend_submitted_for_test(1);
|
||||
let on_plain = waiting("plain", &plain, &a, 1);
|
||||
assert_eq!(on_plain.status(), CommandBufferStatus::Scheduled, "{name}");
|
||||
assert_eq!(log.lines(), ["plain scheduled Scheduled"], "{name}");
|
||||
signal(plain.inner().semaphore);
|
||||
finished(name, &on_plain);
|
||||
|
||||
// A shared event's: held at `Committed` until the host sets the value, then scheduled
|
||||
// while its GPU wait, on the semaphore, still holds it.
|
||||
let shared = shared_event(&device);
|
||||
shared.pretend_submitted_for_test(1);
|
||||
let on_set = waiting("set", shared.as_event(), &b, 2);
|
||||
assert_eq!(
|
||||
on_set.status(),
|
||||
CommandBufferStatus::Committed,
|
||||
"{name}: held"
|
||||
);
|
||||
assert_eq!(log.lines().len(), 2, "{name}: no scheduled handler");
|
||||
shared.set_signaled_value(1);
|
||||
(on_set.wait_until_scheduled()).expect("a wait outside a scheduled handler");
|
||||
assert_eq!(on_set.status(), CommandBufferStatus::Scheduled, "{name}");
|
||||
assert_eq!(log.lines()[2..], ["set scheduled Scheduled"], "{name}");
|
||||
signal(shared.as_event().inner().semaphore);
|
||||
finished(name, &on_set);
|
||||
|
||||
// A shared event's value that arrives only as the command buffer completes: the
|
||||
// completion thread, held meanwhile, schedules it in the round that retires it.
|
||||
let late = shared_event(&device);
|
||||
late.pretend_submitted_for_test(1);
|
||||
let core = device.core();
|
||||
let hold = core.hold_completions.write().expect("the hold");
|
||||
let on_late = waiting("late", late.as_event(), &c, 3);
|
||||
assert_eq!(
|
||||
on_late.status(),
|
||||
CommandBufferStatus::Committed,
|
||||
"{name}: held"
|
||||
);
|
||||
signal(late.as_event().inner().semaphore);
|
||||
{
|
||||
let queue = lock(&core.queues.runtime);
|
||||
// SAFETY: the queue is held for the call.
|
||||
unsafe { core.device.queue_wait_idle(*queue) }.expect("an idle queue");
|
||||
}
|
||||
assert_eq!(
|
||||
on_late.status(),
|
||||
CommandBufferStatus::Committed,
|
||||
"{name}: ran, held"
|
||||
);
|
||||
drop(hold);
|
||||
finished(name, &on_late);
|
||||
assert_eq!(
|
||||
log.lines(),
|
||||
[
|
||||
"plain scheduled Scheduled",
|
||||
"plain completed Completed None",
|
||||
"set scheduled Scheduled",
|
||||
"set completed Completed None",
|
||||
"late scheduled Scheduled",
|
||||
"late completed Completed None",
|
||||
],
|
||||
"{name}"
|
||||
);
|
||||
assert_eq!((word(&a), word(&b), word(&c)), (1, 2, 3), "{name}");
|
||||
drop((a, b, c, arena, plain, shared, late, queue, device));
|
||||
});
|
||||
}
|
||||
|
||||
/// A listener runs when the GPU reaches its value in the middle of a command buffer, at once
|
||||
/// for a value already reached, and for a host value.
|
||||
#[test]
|
||||
@@ -2638,12 +2743,14 @@ pub(crate) mod on_device {
|
||||
in_flight
|
||||
.encode_signal_event(g.as_event(), 1)
|
||||
.expect("a signal");
|
||||
let before = device.counters().metrics().submissions;
|
||||
in_flight.commit().expect("a commit");
|
||||
let held = other.command_buffer().expect("a command buffer");
|
||||
held.encode_wait_for_event(f.as_event(), 1).expect("a wait");
|
||||
held.commit().expect("a commit");
|
||||
within("the scheduling", || {
|
||||
in_flight.status() == CommandBufferStatus::Scheduled
|
||||
// Submitted, and `Committed` while the value it waits for is not reached.
|
||||
within("the submission", || {
|
||||
device.counters().metrics().submissions > before
|
||||
});
|
||||
let waiting = thread::scope(|scope| {
|
||||
let waiter = scope.spawn(|| {
|
||||
|
||||
Reference in new issue
Block a user