RUNTIME 2503: A command buffer that waited for another queue's completes after it behind handler-free items too
What: a command buffer's transition to Completed that waited on the GPU for another queue's command buffer X now waits for X's transition to reach its completed handlers wherever nothing before X's transition in X's queue's context may block: no item there, nor what a thread runs there, runs a scheduled or completed handler of the program's or waits for a submission handler (Run::may_block; completion-seen handlers never block). Before, it waited only where X's transition was first in its context or already running, so behind an earlier command buffer's handler-free transition still queued, or run by a thread the host kept off the CPU, it went on and the program's completed handler of the waiter could read X short of Completed. Behind an item that may block it still goes on, as Metal's does (a_blocked_handler_of_the_signaller_s_queue_holds_back_the_signaller_and_not_the_waiter). How: each item posted to a context takes a place (a counter over all lines); a line keeps the places of its items that may block and whether what a thread runs of it may. Follow keeps its transition's place (an AtomicU64 store where it kept an AtomicBool) in place of the command buffer's address, so Lines::leads is one look at the line's first item and first place that may block: no walk of the line (the old lead check walked it where a thread ran the line), no new lock. Cost: a u64 per queued item, a u64 per queued item that may block, a deque push and pop for those. Item::finishes, which only the walk used, is gone. The validation test a_shared_event_wait_for_a_kept_group_s_signal_ends_at_the_next_completion_turn now checks what the program can rely on: X reads Completed in Y's completed handler, and Y's GPU work began after X's ended where the device's own timestamps place both. It no longer compares the two completed handlers' start instants, which Metal does not order either (an M3 Max under macOS 27.0.1 started the waiter's first in up to 6% of rounds with a plain event, 0.1% with a shared one). The contexts tests name items by their places, so Ends carries no name (353257e382). Verified on lavapipe (msi, m3-max-38523): clippy (cpu_icd, runtime and validation, all targets and features) clean; runtime tests 63/63 (tests::contexts with the new an_item_leads_its_line_behind_what_may_not_block, the three completion_order follow tests, unshared_hold, queue_turns, submission_turns, scheduler_held_wakes); validation grouped_submissions 16/16. A scratch diagnostic of the same shape, 5 rounds under each of Fair and CommitOrder: X read Completed in Y's handler in 10 of 10 (before the fix, m3-max-38464: X still Scheduled in 2 of 10). m3-max-38524: workspace clippy (all targets, all features), clippy with --cfg m2v_gpu, workspace rustdoc with -D warnings: all clean.
This commit is contained in:
1 parent
99778b04c0
commit
946abfc913
10 files changed
+376
-203
No files matched your search
@@ -12,28 +12,38 @@
|
||||
//! completed handlers (`EventTimeline::signallers`). A batch of another queue's command buffer
|
||||
//! that waits for one of those signals takes it as it is submitted, so its command buffer knows
|
||||
//! whom it completes after.
|
||||
//! - **Only where the signaller's transition leads its context.** Metal runs each queue's
|
||||
//! transitions in their own context: while one of a queue's handlers blocks, another queue's
|
||||
//! transitions go on (`contexts/mod.rs`). So a transition waits for the signaller's only where
|
||||
//! the signaller's transition was posted to its context and no item there runs before it
|
||||
//! ([`Follow::leads`]). An earlier command buffer of the signaller's queue whose waiters returned
|
||||
//! runs nothing there any more, however long the host keeps the thread that ran it off the CPU:
|
||||
//! a context is done with an item before the item's hold of its command buffer goes, which lets
|
||||
//! those waiters return (`contexts/mod.rs`). That transition runs no code of the program's that
|
||||
//! may block before its completed handlers, so the wait ends once a thread takes it and runs its
|
||||
//! completion-seen handlers, and it holds no thread meanwhile: a gate in the waiting
|
||||
//! transition's context (`contexts/lines.rs`). Behind any other item, the waiting transition
|
||||
//! goes on, as Metal's does. The completion turns take the signaller's batch before the waiting one's
|
||||
//! - **Only behind what blocks nothing.** Metal runs each queue's transitions in their own
|
||||
//! context: while one of a queue's handlers blocks, another queue's transitions go on
|
||||
//! (`contexts/mod.rs`). So a transition waits for the signaller's only where the signaller's
|
||||
//! transition was posted to its context and nothing there before it may block
|
||||
//! ([`Follow::leads`]): no item before it, nor what a thread runs there, runs a handler of the
|
||||
//! program's but completion-seen handlers, which never block, or waits for one
|
||||
//! (`Run::may_block`). An earlier command buffer's transition with no scheduled, submission or
|
||||
//! completed handler left ends without the program, however long the host keeps the thread
|
||||
//! that runs it off the CPU, so the waiting transition waits behind it: a program whose command
|
||||
//! buffer waited on the GPU for another queue's never sees it complete while that one reads
|
||||
//! short of `Completed` only because a host thread was slow. Its release of what it retained
|
||||
//! may still run a deallocator of the program's (`program_objects.rs`), which the waiting
|
||||
//! transition waits behind too. An earlier command buffer of the signaller's queue whose waiters
|
||||
//! returned runs nothing there any more: a context is done with an item before the item's hold
|
||||
//! of its command buffer goes, which lets those waiters return (`contexts/mod.rs`). The
|
||||
//! signaller's transition runs no code of the program's that may block before its completed
|
||||
//! handlers, so the wait ends once a thread takes it and runs its completion-seen handlers, and
|
||||
//! it holds no thread meanwhile: a gate in the waiting transition's context
|
||||
//! (`contexts/lines.rs`). Behind an item that may block, the waiting transition goes on, as
|
||||
//! Metal's does. The completion turns take the signaller's batch before the waiting one's
|
||||
//! (`completion.rs`), so the signaller's transition is posted by the time the waiting one's is.
|
||||
//! - **Nothing of the signaller is held.** A [`Follow`] names its command buffer by address alone,
|
||||
//! to pick out its transition among its context's items: the waiter that completes the follower
|
||||
//! never holds the signaller's last reference, so never the device's.
|
||||
//! Whether it leads is one look at its context's first item and first item that may block
|
||||
//! (`Lines::leads`), however many items the context holds.
|
||||
//! - **Nothing of the signaller is held.** A [`Follow`] names its command buffer's transition by
|
||||
//! its place in its context alone: the waiter that completes the follower never holds the
|
||||
//! signaller's last reference, so never the device's.
|
||||
|
||||
use super::super::contexts::Contexts;
|
||||
use super::super::device::lock;
|
||||
use super::super::event::EventShared;
|
||||
use super::Item;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
/// The command buffer whose last batch's GPU signals another queue's command buffers may wait for,
|
||||
@@ -42,15 +52,12 @@ use std::sync::{Arc, Mutex};
|
||||
pub(crate) struct Follow {
|
||||
/// Its queue's identity: its context's.
|
||||
queue: u64,
|
||||
/// Its address, which picks out its transition to `Completed` or `Error` among its context's
|
||||
/// items (`Item::finishes`).
|
||||
buffer: usize,
|
||||
/// The gates of the transitions posted behind its own, each a context and a gate's token,
|
||||
/// until its own reaches its completed handlers; `None` from then on.
|
||||
gates: Mutex<Option<Vec<(u64, u64)>>>,
|
||||
/// Its transition to `Completed` or `Error` was posted to its context, or ran
|
||||
/// ([`Follow::posted`]): before then no item its context runs is it.
|
||||
posted: AtomicBool,
|
||||
/// The place its transition to `Completed` or `Error` took in its context as it was posted
|
||||
/// ([`Follow::posted`], `Contexts::post`); 0 before then, and where it ran without a post.
|
||||
at: AtomicU64,
|
||||
}
|
||||
|
||||
/// What a command buffer's transition to `Completed` or `Error` lets go as it reaches its completed
|
||||
@@ -63,13 +70,12 @@ pub(super) struct Release {
|
||||
}
|
||||
|
||||
impl Follow {
|
||||
/// The follow of the command buffer at `buffer`, of the queue `queue`.
|
||||
pub(super) fn new(queue: u64, buffer: usize) -> Follow {
|
||||
/// The follow of a command buffer of the queue `queue`.
|
||||
pub(super) fn new(queue: u64) -> Follow {
|
||||
Follow {
|
||||
queue,
|
||||
buffer,
|
||||
gates: Mutex::new(Some(Vec::new())),
|
||||
posted: AtomicBool::new(false),
|
||||
at: AtomicU64::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,22 +84,23 @@ impl Follow {
|
||||
self.queue
|
||||
}
|
||||
|
||||
/// Its transition to `Completed` or `Error` was posted to its context, or ran there.
|
||||
pub(super) fn posted(&self) {
|
||||
self.posted.store(true, Ordering::Release);
|
||||
/// Its transition to `Completed` or `Error` was posted to its context, at the place `at`.
|
||||
pub(super) fn posted(&self, at: u64) {
|
||||
self.at.store(at, Ordering::Release);
|
||||
}
|
||||
|
||||
/// Whether its transition to `Completed` or `Error` has not reached its completed handlers and
|
||||
/// leads its context: it is there, and no item there runs before it.
|
||||
/// leads its context: it was posted there, and nothing there before it may block.
|
||||
pub(super) fn leads(&self, contexts: &Contexts<Item>) -> bool {
|
||||
let gates = lock(&self.gates);
|
||||
gates.is_some() && self.leads_its_context(contexts)
|
||||
}
|
||||
|
||||
/// Whether its transition leads its context, where it has not reached its completed handlers.
|
||||
/// Whether its transition leads its context, where it has not reached its completed handlers,
|
||||
/// so is not done there.
|
||||
fn leads_its_context(&self, contexts: &Contexts<Item>) -> bool {
|
||||
let posted = self.posted.load(Ordering::Acquire);
|
||||
contexts.leads(self.queue, |item| item.finishes(self.buffer), posted)
|
||||
let at = self.at.load(Ordering::Acquire);
|
||||
at > 0 && contexts.leads(self.queue, at)
|
||||
}
|
||||
|
||||
/// Close `gate` of a transition of `queue`'s context until its own transition reaches its
|
||||
|
||||
@@ -6726,22 +6726,24 @@ impl CommandBufferShared {
|
||||
/// queue's context: behind a gate its submission handlers open where they have not run
|
||||
/// ([`gate_for_handlers`](Self::gate_for_handlers)), plainly otherwise, with one look for a
|
||||
/// thread to run it either way. The caller keeps no other reference: once the transition
|
||||
/// ran, the command buffer may go, with its device, on the handler thread.
|
||||
/// ran, the command buffer may go, with its device, on the handler thread. Its place there
|
||||
/// (`Contexts::post`).
|
||||
fn post_finished_transition(
|
||||
contexts: &Contexts<Item>,
|
||||
buffer: Hold<CommandBufferShared>,
|
||||
result: Result<(), CommandBufferError>,
|
||||
) {
|
||||
) -> u64 {
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
let serves = contexts::serves_here();
|
||||
let (gate, queue) = (buffer.gate_for_handlers(contexts), buffer.queue.id());
|
||||
let item = Item::Transition(Transition::finished(buffer, result));
|
||||
match gate {
|
||||
let at = match gate {
|
||||
Some(gate) => contexts.post_gated(queue, item, gate),
|
||||
None => contexts.post(queue, item),
|
||||
}
|
||||
};
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
completion::trace::finished_posted(gate.is_some(), contexts::serves_here() - serves);
|
||||
at
|
||||
}
|
||||
|
||||
/// Its batch `index` failed at its submission (a host step's recording or the `vkQueueSubmit`
|
||||
@@ -6880,10 +6882,13 @@ impl CommandBufferShared {
|
||||
to: To::Scheduled,
|
||||
plain,
|
||||
where_seen: plain,
|
||||
may_block: !plain,
|
||||
});
|
||||
match shared.contexts.run_alone(self.queue.id(), item) {
|
||||
Ok(()) => shared.counts.scheduled_where_submitted(),
|
||||
Err(item) => shared.contexts.post(self.queue.id(), item),
|
||||
Err(item) => {
|
||||
shared.contexts.post(self.queue.id(), item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6960,9 +6965,9 @@ impl CommandBufferShared {
|
||||
) {
|
||||
let contexts = buffer.device.shared().contexts.clone();
|
||||
let own = (lock(&buffer.state).release.as_ref()).map(|release| Arc::clone(&release.follow));
|
||||
CommandBufferShared::post_finished_transition(&contexts, buffer, result);
|
||||
let at = CommandBufferShared::post_finished_transition(&contexts, buffer, result);
|
||||
if let Some(own) = own {
|
||||
own.posted();
|
||||
own.posted(at);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6978,11 +6983,11 @@ impl CommandBufferShared {
|
||||
/// [`post_finished`](Self::post_finished) posts it.
|
||||
///
|
||||
/// Where the transition of a command buffer of another queue whose GPU signal it waited for has
|
||||
/// not run up to its completed handlers and leads its context (no item there runs before it),
|
||||
/// its own is posted behind a gate that transition opens there (`Follow::follow`), its place in
|
||||
/// its own context kept: Metal's completion follows the GPU's (`completion.rs`, "After what it
|
||||
/// waited for"). Behind any other item there, Metal runs the other queue's transitions on, and
|
||||
/// so does this.
|
||||
/// not run up to its completed handlers and leads its context (nothing there before it may
|
||||
/// block, `Run::may_block`), its own is posted behind a gate that transition opens there
|
||||
/// (`Follow::follow`), its place in its own context kept: Metal's completion follows the GPU's
|
||||
/// (`completion.rs`, "After what it waited for"). Behind an item there that may block, Metal
|
||||
/// runs the other queue's transitions on, and so does this.
|
||||
///
|
||||
/// The waiter takes no handle of the device here, and lets each transition's hold go short of
|
||||
/// its command buffer's last reference ([`Transition::run`]): the device's last handle never
|
||||
@@ -7002,8 +7007,8 @@ impl CommandBufferShared {
|
||||
(signallers, own)
|
||||
};
|
||||
// Its own transition is posted, or runs, below: another queue's command buffer that waited
|
||||
// for its signal may follow it from then on.
|
||||
let posted = || own.iter().for_each(|own| own.posted());
|
||||
// for its signal may follow it once it was posted, at its place.
|
||||
let posted = |at| own.iter().for_each(|own| own.posted(at));
|
||||
if !signallers.is_empty() {
|
||||
// Closed by this thread until the item is posted behind it.
|
||||
let gate = contexts.gate();
|
||||
@@ -7016,8 +7021,7 @@ impl CommandBufferShared {
|
||||
}
|
||||
buffer.finish_after_handlers(&contexts, gate);
|
||||
let item = Item::Transition(Transition::finished(buffer, result));
|
||||
contexts.post_behind(queue, item, gate);
|
||||
posted();
|
||||
posted(contexts.post_behind(queue, item, gate));
|
||||
contexts.open(queue, gate);
|
||||
return;
|
||||
}
|
||||
@@ -7038,8 +7042,9 @@ impl CommandBufferShared {
|
||||
(seen_first(&state), state.scheduled_owed)
|
||||
};
|
||||
if owed {
|
||||
CommandBufferShared::post_finished_transition(&contexts, buffer, result);
|
||||
posted();
|
||||
posted(CommandBufferShared::post_finished_transition(
|
||||
&contexts, buffer, result,
|
||||
));
|
||||
return;
|
||||
}
|
||||
let item = match serving && own.is_none() && seen_first {
|
||||
@@ -7061,8 +7066,7 @@ impl CommandBufferShared {
|
||||
}
|
||||
None => item,
|
||||
};
|
||||
contexts.post(queue, item.whole());
|
||||
posted();
|
||||
posted(contexts.post(queue, item.whole()));
|
||||
}
|
||||
|
||||
/// Finish the command buffer with `result` (or the failure of a batch): take the device's
|
||||
@@ -7514,6 +7518,11 @@ pub(crate) struct Transition {
|
||||
/// ([`CommandBuffer::add_completion_seen_handler`]): plain, a transition to `Completed`
|
||||
/// with no completed handler, or one that runs to its completion-seen handlers ([`To::Seen`]).
|
||||
where_seen: bool,
|
||||
/// It may block a thread that runs it, or wait for the program before it runs
|
||||
/// (`Run::may_block`): it runs a scheduled or completed handler of its command buffer's, or
|
||||
/// waits for a submission handler. Another queue's transition waits behind one that does
|
||||
/// neither (`follow.rs`).
|
||||
may_block: bool,
|
||||
}
|
||||
|
||||
/// The status a transition goes to.
|
||||
@@ -7543,18 +7552,22 @@ pub(crate) struct Rest {
|
||||
impl Transition {
|
||||
/// The transition of the command buffer `buffer` holds to `Completed` or `Error`, with
|
||||
/// `result`: it runs where seen where it has no completed handler, and is plain where it has
|
||||
/// no completion-seen handler either (none is added after the commit).
|
||||
/// no completion-seen handler either (none is added after the commit). It may block where it
|
||||
/// has a completed handler, or a scheduled or submission handler not run yet, whose step it
|
||||
/// may run or wait for ([`run_scheduled`](CommandBufferShared::run_scheduled)).
|
||||
fn finished(buffer: Hold<CommandBufferShared>, result: Result<(), CommandBufferError>) -> Self {
|
||||
let (where_seen, plain) = {
|
||||
let (where_seen, plain, may_block) = {
|
||||
let state = lock(&buffer.state);
|
||||
let where_seen = state.completed.is_empty();
|
||||
(where_seen, where_seen && state.seen.is_empty())
|
||||
let may_block = !(where_seen && plain_transition(&state));
|
||||
(where_seen, where_seen && state.seen.is_empty(), may_block)
|
||||
};
|
||||
Transition {
|
||||
buffer,
|
||||
to: To::Finished(result),
|
||||
plain,
|
||||
where_seen,
|
||||
may_block,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7572,6 +7585,7 @@ impl Transition {
|
||||
to: To::Seen(result),
|
||||
plain: false,
|
||||
where_seen: true,
|
||||
may_block: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7601,6 +7615,7 @@ impl Transition {
|
||||
to: To::Rest(rest),
|
||||
plain: false,
|
||||
where_seen: false,
|
||||
may_block: true,
|
||||
});
|
||||
(buffer.device.shared().contexts).post(buffer.queue.id(), rest);
|
||||
}
|
||||
@@ -7675,24 +7690,11 @@ impl Item {
|
||||
to: To::Finished(result),
|
||||
plain: false,
|
||||
where_seen: false,
|
||||
may_block: true,
|
||||
}),
|
||||
item => item,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether it is the transition to `Completed` or `Error` of the command buffer at the address
|
||||
/// `buffer` (`follow.rs`). A command buffer whose transition runs to its completion-seen
|
||||
/// handlers where seen ([`To::Seen`]) has no [`Follow`]: no other queue's command buffer
|
||||
/// looks for its transition.
|
||||
fn finishes(&self, buffer: usize) -> bool {
|
||||
match self {
|
||||
Item::Transition(transition) => {
|
||||
let at = Arc::as_ptr(&transition.buffer) as usize;
|
||||
matches!(transition.to, To::Finished(_)) && at == buffer
|
||||
}
|
||||
Item::Release(_) | Item::Submissions(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Run for Item {
|
||||
@@ -7725,6 +7727,13 @@ impl Run for Item {
|
||||
Item::Release(_) | Item::Submissions(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn may_block(&self) -> bool {
|
||||
match self {
|
||||
Item::Transition(transition) => transition.may_block,
|
||||
Item::Release(_) | Item::Submissions(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a queue's waiter runs a command buffer's transition to `Completed` or `Error` to its
|
||||
|
||||
@@ -1689,8 +1689,7 @@ impl CommandBufferShared {
|
||||
// A last batch's signals name its command buffer, which another queue's command buffer
|
||||
// that waits for one completes after; its transition to `Completed` lets them go
|
||||
// (`follow.rs`).
|
||||
let follow = (last && !kept.is_empty())
|
||||
.then(|| Arc::new(Follow::new(self.queue.id(), Arc::as_ptr(self) as usize)));
|
||||
let follow = (last && !kept.is_empty()).then(|| Arc::new(Follow::new(self.queue.id())));
|
||||
for (event, signal, value) in &kept {
|
||||
event.keep(
|
||||
*signal,
|
||||
|
||||
@@ -10,6 +10,12 @@
|
||||
//! **Gates.** An item may be pushed behind a gate ([`Lines::push_behind`]), which holds it, and its
|
||||
//! line's later items, until every closing of the gate is opened again ([`Lines::close_new`],
|
||||
//! [`Lines::close`], [`Lines::open`]): it keeps its place in its line meanwhile.
|
||||
//!
|
||||
//! **Places.** Each item pushed takes the next place, whatever its line, so places rise along a
|
||||
//! line. A line keeps the places of its items that may block a thread that runs them (the
|
||||
//! `may_block` of [`Lines::new`]), and whether what a thread runs of it may: whether an item
|
||||
//! leads its line ([`Lines::leads`]) is a look at the line's first item and first such place,
|
||||
//! however many items the line holds.
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
@@ -22,13 +28,32 @@ pub(crate) struct Lines<I> {
|
||||
ready: VecDeque<u64>,
|
||||
/// The gates not yet open, by token: how many closings each still holds.
|
||||
closed: HashMap<u64, u32>,
|
||||
/// The places taken so far: the last item pushed took this one, the first 1.
|
||||
placed: u64,
|
||||
/// Whether an item may block a thread that runs it ([`Lines::new`]).
|
||||
may_block: fn(&I) -> bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Line<I> {
|
||||
items: VecDeque<Entry<I>>,
|
||||
/// The places of the items it holds that may block, in order.
|
||||
blocking: VecDeque<u64>,
|
||||
/// A thread runs the line's first item.
|
||||
running: bool,
|
||||
/// What that thread runs may block: false where no thread runs the line.
|
||||
running_blocks: bool,
|
||||
}
|
||||
|
||||
impl<I> Line<I> {
|
||||
fn new(running: bool, running_blocks: bool) -> Line<I> {
|
||||
Line {
|
||||
items: VecDeque::new(),
|
||||
blocking: VecDeque::new(),
|
||||
running,
|
||||
running_blocks,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -36,22 +61,26 @@ struct Entry<I> {
|
||||
item: I,
|
||||
/// The gate it waits behind, if any ([`Lines::push_behind`]).
|
||||
gate: Option<u64>,
|
||||
/// Its place ([`Lines::push`]).
|
||||
place: u64,
|
||||
}
|
||||
|
||||
impl<I> Default for Lines<I> {
|
||||
fn default() -> Lines<I> {
|
||||
impl<I> Lines<I> {
|
||||
/// Lines in which `may_block` tells whether an item may block a thread that runs it: what
|
||||
/// [`Lines::leads`] looks past.
|
||||
pub(crate) fn new(may_block: fn(&I) -> bool) -> Lines<I> {
|
||||
Lines {
|
||||
lines: HashMap::new(),
|
||||
ready: VecDeque::new(),
|
||||
closed: HashMap::new(),
|
||||
placed: 0,
|
||||
may_block,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Lines<I> {
|
||||
/// Append `item` to `queue`'s line.
|
||||
pub(crate) fn push(&mut self, queue: u64, item: I) {
|
||||
self.push_entry(queue, Entry { item, gate: None });
|
||||
/// Append `item` to `queue`'s line: its place.
|
||||
pub(crate) fn push(&mut self, queue: u64, item: I) -> u64 {
|
||||
self.push_entry(queue, item, None)
|
||||
}
|
||||
|
||||
/// Close the new gate `token` once ([`Lines::push_behind`]).
|
||||
@@ -60,22 +89,26 @@ impl<I> Lines<I> {
|
||||
}
|
||||
|
||||
/// Append `item` to `queue`'s line behind the gate `token` ([`Lines::close_new`]): it runs once
|
||||
/// every closing of the gate is opened ([`Lines::open`]).
|
||||
pub(crate) fn push_behind(&mut self, queue: u64, item: I, token: u64) {
|
||||
let gate = Some(token);
|
||||
self.push_entry(queue, Entry { item, gate });
|
||||
/// every closing of the gate is opened ([`Lines::open`]). Its place.
|
||||
pub(crate) fn push_behind(&mut self, queue: u64, item: I, token: u64) -> u64 {
|
||||
self.push_entry(queue, item, Some(token))
|
||||
}
|
||||
|
||||
fn push_entry(&mut self, queue: u64, entry: Entry<I>) {
|
||||
fn push_entry(&mut self, queue: u64, item: I, gate: Option<u64>) -> u64 {
|
||||
self.placed += 1;
|
||||
let place = self.placed;
|
||||
let blocks = (self.may_block)(&item);
|
||||
let entry = Entry { item, gate, place };
|
||||
let open = self.open_entry(&entry);
|
||||
let line = self.lines.entry(queue).or_insert_with(|| Line {
|
||||
items: VecDeque::new(),
|
||||
running: false,
|
||||
});
|
||||
let line = (self.lines.entry(queue)).or_insert_with(|| Line::new(false, false));
|
||||
if line.items.is_empty() && !line.running && open {
|
||||
self.ready.push_back(queue);
|
||||
}
|
||||
if blocks {
|
||||
line.blocking.push_back(place);
|
||||
}
|
||||
line.items.push_back(entry);
|
||||
place
|
||||
}
|
||||
|
||||
/// Whether `entry` waits behind no gate that is still closed.
|
||||
@@ -126,6 +159,10 @@ impl<I> Lines<I> {
|
||||
let line = self.lines.get_mut(&queue)?;
|
||||
line.running = true;
|
||||
let entry = line.items.pop_front()?;
|
||||
line.running_blocks = line.blocking.front() == Some(&entry.place);
|
||||
if line.running_blocks {
|
||||
line.blocking.pop_front();
|
||||
}
|
||||
Some((queue, entry.item))
|
||||
}
|
||||
|
||||
@@ -136,6 +173,7 @@ impl<I> Lines<I> {
|
||||
return;
|
||||
};
|
||||
line.running = false;
|
||||
line.running_blocks = false;
|
||||
let Some(first) = line.items.front() else {
|
||||
self.lines.remove(&queue);
|
||||
return;
|
||||
@@ -150,12 +188,13 @@ impl<I> Lines<I> {
|
||||
|
||||
/// Take `queue`'s line for the calling thread, where no thread runs it and `plain` holds for
|
||||
/// each item it holds, none behind a closed gate: those items, in order, which the caller
|
||||
/// runs before anything it runs in the line itself; the line is running until
|
||||
/// [`Lines::done`]. `None` otherwise, changing nothing.
|
||||
/// runs before anything it runs in the line itself (which may block where `blocks`); the
|
||||
/// line is running until [`Lines::done`]. `None` otherwise, changing nothing.
|
||||
pub(crate) fn take_plain(
|
||||
&mut self,
|
||||
queue: u64,
|
||||
plain: impl Fn(&I) -> bool,
|
||||
blocks: bool,
|
||||
) -> Option<VecDeque<I>> {
|
||||
if let Some(line) = self.lines.get(&queue) {
|
||||
let runs = |entry: &Entry<I>| plain(&entry.item) && self.open_entry(entry);
|
||||
@@ -163,49 +202,48 @@ impl<I> Lines<I> {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let items: VecDeque<I> = (self.lines.remove(&queue)).map_or_else(VecDeque::new, |line| {
|
||||
line.items.into_iter().map(|entry| entry.item).collect()
|
||||
});
|
||||
let (items, blocking) = match self.lines.remove(&queue) {
|
||||
Some(line) => {
|
||||
let items: VecDeque<I> = line.items.into_iter().map(|entry| entry.item).collect();
|
||||
(items, !line.blocking.is_empty())
|
||||
}
|
||||
None => (VecDeque::new(), false),
|
||||
};
|
||||
if !items.is_empty() {
|
||||
// A line that holds an item and runs on no thread is ready, once.
|
||||
self.ready.retain(|ready| *ready != queue);
|
||||
}
|
||||
self.lines.insert(
|
||||
queue,
|
||||
Line {
|
||||
items: VecDeque::new(),
|
||||
running: true,
|
||||
},
|
||||
);
|
||||
self.lines
|
||||
.insert(queue, Line::new(true, blocking || blocks));
|
||||
Some(items)
|
||||
}
|
||||
|
||||
/// Take `queue`'s line for the calling thread where it holds no item and no thread runs it:
|
||||
/// the line is running until [`Lines::done`]. False otherwise, changing nothing.
|
||||
pub(crate) fn take_idle(&mut self, queue: u64) -> bool {
|
||||
/// Take `queue`'s line for the calling thread where it holds no item and no thread runs it,
|
||||
/// for what may block where `blocks`: the line is running until [`Lines::done`]. False
|
||||
/// otherwise, changing nothing.
|
||||
pub(crate) fn take_idle(&mut self, queue: u64, blocks: bool) -> bool {
|
||||
if self.lines.contains_key(&queue) {
|
||||
return false;
|
||||
}
|
||||
self.lines.insert(
|
||||
queue,
|
||||
Line {
|
||||
items: VecDeque::new(),
|
||||
running: true,
|
||||
},
|
||||
);
|
||||
self.lines.insert(queue, Line::new(true, blocks));
|
||||
true
|
||||
}
|
||||
|
||||
/// Whether the item `is` picks out leads `queue`'s line: it is the first item and no thread
|
||||
/// runs the line; or, where the caller knows it was pushed (`posted`) and is not done, a thread
|
||||
/// runs the line and no item left in it is it, so the thread runs it.
|
||||
pub(crate) fn leads(&self, queue: u64, is: impl Fn(&I) -> bool, posted: bool) -> bool {
|
||||
/// Whether the item placed at `at` in `queue`'s line ([`Lines::push`]), which the caller knows
|
||||
/// is not done, leads the line: where it waits there, no item before it may block, nor what a
|
||||
/// thread runs of the line; where it no longer waits there, a thread runs it. One look at the
|
||||
/// line's first item and first place that may block, however many items the line holds.
|
||||
pub(crate) fn leads(&self, queue: u64, at: u64) -> bool {
|
||||
let Some(line) = self.lines.get(&queue) else {
|
||||
return false;
|
||||
};
|
||||
match line.running {
|
||||
true => posted && !line.items.iter().any(|entry| is(&entry.item)),
|
||||
false => line.items.front().is_some_and(|entry| is(&entry.item)),
|
||||
let waits = line.items.front().is_some_and(|first| first.place <= at);
|
||||
match waits {
|
||||
true => {
|
||||
let before = line.blocking.front().is_some_and(|&place| place < at);
|
||||
!line.running_blocks && !before
|
||||
}
|
||||
false => line.running,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,9 @@
|
||||
//! no queue's handlers. A transition to `Completed` of a command buffer that waited on the GPU
|
||||
//! for another queue's command buffer may be posted behind a gate ([`Contexts::gate`],
|
||||
//! [`Contexts::post_behind`]), its place in its context kept, until that command buffer's
|
||||
//! transition, which leads its own context ([`Contexts::leads`]), reaches its handlers
|
||||
//! (`command_buffer/follow.rs`): a gate holds no thread.
|
||||
//! transition, which nothing that may block precedes in its own context ([`Contexts::leads`],
|
||||
//! [`Run::may_block`]), reaches its handlers (`command_buffer/follow.rs`): a gate holds no
|
||||
//! thread.
|
||||
//! - **Done with an item before what it held goes.** The thread that ran an item marks its context
|
||||
//! done with it, and only then lets go of what the item still holds ([`Run::Held`]): a
|
||||
//! transition's hold of its command buffer, whose going lets that command buffer's waiters
|
||||
@@ -135,6 +136,15 @@ pub(crate) trait Run: Send + 'static {
|
||||
fn runs_where_seen(&self) -> bool {
|
||||
self.plain()
|
||||
}
|
||||
|
||||
/// Whether the item may block a thread that runs it, or wait for the program before it runs:
|
||||
/// it runs a handler of the program's other than its completion-seen handlers, which never
|
||||
/// block, or waits for one (a scheduled, submission or completed handler). Every item may by
|
||||
/// default. A transition of another queue waits behind one that may not
|
||||
/// (`command_buffer/follow.rs`).
|
||||
fn may_block(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// One device's handler threads. A clone is another handle of the same pool, which holds nothing
|
||||
@@ -225,7 +235,7 @@ impl<I: Run> Contexts<I> {
|
||||
pub(crate) fn start(limit: usize) -> Result<Contexts<I>, Error> {
|
||||
let shared = Arc::new(Shared {
|
||||
state: Mutex::new(State {
|
||||
lines: Lines::default(),
|
||||
lines: Lines::new(|item: &I| item.may_block()),
|
||||
gates: 0,
|
||||
free: 1,
|
||||
parked: 0,
|
||||
@@ -259,11 +269,12 @@ impl<I: Run> Contexts<I> {
|
||||
}
|
||||
|
||||
/// Append `item` to the context of logical queue `queue`; start a thread when a context is
|
||||
/// ready that no free thread will take, below the cap.
|
||||
pub(crate) fn post(&self, queue: u64, item: I) {
|
||||
/// ready that no free thread will take, below the cap. Its place there ([`Lines::push`]).
|
||||
pub(crate) fn post(&self, queue: u64, item: I) -> u64 {
|
||||
let mut state = lock(&self.shared.state);
|
||||
state.lines.push(queue, item);
|
||||
let place = state.lines.push(queue, item);
|
||||
self.serve(state);
|
||||
place
|
||||
}
|
||||
|
||||
/// Run `item` in the context of logical queue `queue` on this thread, after the items that
|
||||
@@ -276,7 +287,11 @@ impl<I: Run> Contexts<I> {
|
||||
if !may_run(&item) {
|
||||
return Err(item);
|
||||
}
|
||||
let Some(before) = lock(&self.shared.state).lines.take_plain(queue, may_run) else {
|
||||
let blocks = item.may_block();
|
||||
let taken = lock(&self.shared.state)
|
||||
.lines
|
||||
.take_plain(queue, may_run, blocks);
|
||||
let Some(before) = taken else {
|
||||
return Err(item);
|
||||
};
|
||||
let earlier: Vec<I::Held> = before.into_iter().map(Run::run).collect();
|
||||
@@ -301,22 +316,24 @@ impl<I: Run> Contexts<I> {
|
||||
|
||||
/// Append `item` to the context of logical queue `queue` behind the gate `token`
|
||||
/// ([`Contexts::gate`]): the item keeps its place in its context, and it and the items after it
|
||||
/// run once the gate is open.
|
||||
pub(crate) fn post_behind(&self, queue: u64, item: I, token: u64) {
|
||||
/// run once the gate is open. Its place there ([`Lines::push`]).
|
||||
pub(crate) fn post_behind(&self, queue: u64, item: I, token: u64) -> u64 {
|
||||
let mut state = lock(&self.shared.state);
|
||||
state.lines.push_behind(queue, item, token);
|
||||
let place = state.lines.push_behind(queue, item, token);
|
||||
self.serve(state);
|
||||
place
|
||||
}
|
||||
|
||||
/// Append `item` to the context of logical queue `queue` behind the gate `token`
|
||||
/// ([`Contexts::gate`]) and open the caller's closing of it, under one lock: one look for a
|
||||
/// thread to run it ([`post_behind`](Self::post_behind) and [`open`](Self::open) make one
|
||||
/// each). It runs in its turn once the gate's other closings opened.
|
||||
pub(crate) fn post_gated(&self, queue: u64, item: I, token: u64) {
|
||||
/// each). It runs in its turn once the gate's other closings opened. Its place there.
|
||||
pub(crate) fn post_gated(&self, queue: u64, item: I, token: u64) -> u64 {
|
||||
let mut state = lock(&self.shared.state);
|
||||
state.lines.push_behind(queue, item, token);
|
||||
let place = state.lines.push_behind(queue, item, token);
|
||||
state.lines.open(queue, token);
|
||||
self.serve(state);
|
||||
place
|
||||
}
|
||||
|
||||
/// Close the gate `token` once more, where it is not open yet: whether it was not.
|
||||
@@ -332,10 +349,11 @@ impl<I: Run> Contexts<I> {
|
||||
self.serve(state);
|
||||
}
|
||||
|
||||
/// Whether the item `is` picks out leads the context of `queue`: no item of the context runs
|
||||
/// before it ([`Lines::leads`]; `posted` where the caller knows it was posted there).
|
||||
pub(crate) fn leads(&self, queue: u64, is: impl Fn(&I) -> bool, posted: bool) -> bool {
|
||||
lock(&self.shared.state).lines.leads(queue, is, posted)
|
||||
/// Whether the item posted at the place `at` in the context of `queue`, which the caller knows
|
||||
/// is not done, leads it: nothing there that may block runs before it ([`Lines::leads`],
|
||||
/// [`Run::may_block`]).
|
||||
pub(crate) fn leads(&self, queue: u64, at: u64) -> bool {
|
||||
lock(&self.shared.state).lines.leads(queue, at)
|
||||
}
|
||||
|
||||
/// Hold the context of logical queue `queue`, which holds no item and runs on no thread (a
|
||||
@@ -343,7 +361,8 @@ impl<I: Run> Contexts<I> {
|
||||
/// there waits until [`release`](Self::release). False where it held an item or ran, changing
|
||||
/// nothing.
|
||||
pub(crate) fn hold(&self, queue: u64) -> bool {
|
||||
lock(&self.shared.state).lines.take_idle(queue)
|
||||
// What it waits for may run the program's handlers.
|
||||
lock(&self.shared.state).lines.take_idle(queue, true)
|
||||
}
|
||||
|
||||
/// Let the context [`hold`](Self::hold) held run what was posted there meanwhile, in order.
|
||||
@@ -358,7 +377,8 @@ impl<I: Run> Contexts<I> {
|
||||
/// this thread meanwhile, so the items posted to it wait for `item`, then run on the pool's
|
||||
/// threads. Else give `item` back, changing nothing.
|
||||
pub(crate) fn run_alone(&self, queue: u64, item: I) -> Result<(), I> {
|
||||
if !item.plain() || !lock(&self.shared.state).lines.take_idle(queue) {
|
||||
let blocks = item.may_block();
|
||||
if !item.plain() || !lock(&self.shared.state).lines.take_idle(queue, blocks) {
|
||||
return Err(item);
|
||||
}
|
||||
let held = item.run();
|
||||
|
||||
@@ -328,7 +328,8 @@ fn a_handler_thread_held_off_the_cpu_after_a_wait_returned_keeps_the_order() {
|
||||
/// behind it, as Metal's does; B, which waited on the GPU for A's signal, still completes and runs
|
||||
/// its completion-seen handler, A short of `Completed`: Metal runs another queue's transitions
|
||||
/// while one queue's handler blocks, so B's waits for A's only where A's leads its context
|
||||
/// (`WorkCounts::command_buffers_completed_after_another_queue`), never behind another item.
|
||||
/// (`WorkCounts::command_buffers_completed_after_another_queue`), never behind an item that may
|
||||
/// block.
|
||||
#[test]
|
||||
fn a_blocked_handler_of_the_signaller_s_queue_holds_back_the_signaller_and_not_the_waiter() {
|
||||
let device = device();
|
||||
|
||||
@@ -13,9 +13,14 @@ use std::time::{Duration, Instant};
|
||||
/// The longest any step here may take.
|
||||
const WAIT: Duration = Duration::from_secs(20);
|
||||
|
||||
/// Lines whose every item may block.
|
||||
fn blocking_lines<I>() -> Lines<I> {
|
||||
Lines::new(|_| true)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_line_runs_one_item_at_a_time_in_order_and_others_go_on() {
|
||||
let mut lines = Lines::default();
|
||||
let mut lines = blocking_lines();
|
||||
lines.push(1, "a1");
|
||||
lines.push(1, "a2");
|
||||
lines.push(2, "b1");
|
||||
@@ -42,18 +47,18 @@ fn a_line_runs_one_item_at_a_time_in_order_and_others_go_on() {
|
||||
#[test]
|
||||
fn a_line_is_taken_whole_where_it_runs_nowhere_and_holds_only_plain_items() {
|
||||
let plain = |item: &&str| !item.ends_with('!');
|
||||
let mut lines = Lines::default();
|
||||
let mut lines = blocking_lines();
|
||||
// An idle line: taken, with nothing before.
|
||||
assert_eq!(lines.take_plain(1, plain), Some(VecDeque::new()));
|
||||
assert_eq!(lines.take_plain(1, plain, false), Some(VecDeque::new()));
|
||||
lines.push(1, "a1");
|
||||
assert!(!lines.is_ready());
|
||||
assert_eq!(lines.take_plain(1, plain), None);
|
||||
assert_eq!(lines.take_plain(1, plain, false), None);
|
||||
lines.done(1);
|
||||
// A line holding plain items, on no thread: taken with them, in order.
|
||||
lines.push(1, "a2");
|
||||
assert_eq!(lines.ready(), 1);
|
||||
assert_eq!(
|
||||
lines.take_plain(1, plain),
|
||||
lines.take_plain(1, plain, false),
|
||||
Some(VecDeque::from(["a1", "a2"]))
|
||||
);
|
||||
assert!(!lines.is_ready());
|
||||
@@ -62,10 +67,10 @@ fn a_line_is_taken_whole_where_it_runs_nowhere_and_holds_only_plain_items() {
|
||||
// A line holding an item that is not plain stays as it is.
|
||||
lines.push(2, "b1!");
|
||||
lines.push(2, "b2");
|
||||
assert_eq!(lines.take_plain(2, plain), None);
|
||||
assert_eq!(lines.take_plain(2, plain, false), None);
|
||||
assert_eq!(lines.take(), Some((2, "b1!")));
|
||||
// So does a line a thread runs.
|
||||
assert_eq!(lines.take_plain(2, plain), None);
|
||||
assert_eq!(lines.take_plain(2, plain, false), None);
|
||||
lines.done(2);
|
||||
assert_eq!(lines.take(), Some((2, "b2")));
|
||||
lines.done(2);
|
||||
@@ -76,25 +81,25 @@ fn a_line_is_taken_whole_where_it_runs_nowhere_and_holds_only_plain_items() {
|
||||
/// items pushed meanwhile wait for its end.
|
||||
#[test]
|
||||
fn an_idle_line_alone_is_taken_for_the_calling_thread() {
|
||||
let mut lines = Lines::default();
|
||||
assert!(lines.take_idle(1));
|
||||
let mut lines = blocking_lines();
|
||||
assert!(lines.take_idle(1, false));
|
||||
lines.push(1, "a1");
|
||||
assert!(!lines.is_ready());
|
||||
assert!(!lines.take_idle(1));
|
||||
assert!(!lines.take_idle(1, false));
|
||||
lines.done(1);
|
||||
// A line holding an item, plain or not, stays as it is.
|
||||
assert_eq!(lines.ready(), 1);
|
||||
assert!(!lines.take_idle(1));
|
||||
assert!(!lines.take_idle(1, false));
|
||||
assert_eq!(lines.take(), Some((1, "a1")));
|
||||
// So does a line a thread runs.
|
||||
assert!(!lines.take_idle(1));
|
||||
assert!(!lines.take_idle(1, false));
|
||||
lines.done(1);
|
||||
assert!(lines.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ready_lines_are_taken_in_the_order_they_became_ready() {
|
||||
let mut lines = Lines::default();
|
||||
let mut lines = blocking_lines();
|
||||
for queue in [3, 1, 2] {
|
||||
lines.push(queue, queue);
|
||||
}
|
||||
@@ -108,7 +113,7 @@ fn ready_lines_are_taken_in_the_order_they_became_ready() {
|
||||
#[test]
|
||||
fn an_item_behind_a_gate_holds_its_line_until_every_closing_is_opened() {
|
||||
let plain = |_: &&str| true;
|
||||
let mut lines = Lines::default();
|
||||
let mut lines = blocking_lines();
|
||||
lines.push(1, "a1");
|
||||
lines.close_new(7);
|
||||
assert!(lines.close(7), "closed again while closed");
|
||||
@@ -121,7 +126,7 @@ fn an_item_behind_a_gate_holds_its_line_until_every_closing_is_opened() {
|
||||
assert_eq!(lines.take(), Some((2, "b1")));
|
||||
lines.done(2);
|
||||
assert_eq!(lines.take(), None);
|
||||
assert_eq!(lines.take_plain(1, plain), None);
|
||||
assert_eq!(lines.take_plain(1, plain, false), None);
|
||||
lines.open(1, 7);
|
||||
assert_eq!(lines.take(), None, "one closing is left");
|
||||
lines.open(1, 7);
|
||||
@@ -148,28 +153,104 @@ fn an_item_behind_a_gate_holds_its_line_until_every_closing_is_opened() {
|
||||
assert!(lines.is_empty());
|
||||
}
|
||||
|
||||
/// An item leads its line where it is the first and no thread runs the line, or where it was
|
||||
/// pushed, a thread runs the line and no item left is it (the thread runs it); one not pushed yet
|
||||
/// leads no running line.
|
||||
/// An item leads its line where it is the first, run by a thread or not, or, where no item that
|
||||
/// may block was pushed there before it, at any place; behind an item that may block, it leads
|
||||
/// only once that ran. Each item takes the next place, whatever its line.
|
||||
#[test]
|
||||
fn an_item_leads_its_line_with_nothing_before_it() {
|
||||
let mut lines = Lines::default();
|
||||
let leads =
|
||||
|lines: &Lines<&str>, name: &str, posted| lines.leads(1, |item| *item == name, posted);
|
||||
assert!(!leads(&lines, "a2", true), "no line");
|
||||
lines.push(1, "a1");
|
||||
assert!(leads(&lines, "a1", true));
|
||||
let mut lines = blocking_lines();
|
||||
assert!(!lines.leads(1, 1), "no line");
|
||||
let a1 = lines.push(1, "a1");
|
||||
assert_eq!(a1, 1);
|
||||
assert!(lines.leads(1, a1));
|
||||
assert_eq!(lines.take(), Some((1, "a1")));
|
||||
// a1 runs: it leads; a2, not pushed yet, does not.
|
||||
assert!(leads(&lines, "a1", true));
|
||||
assert!(!leads(&lines, "a2", false));
|
||||
lines.push(1, "a2");
|
||||
assert!(!leads(&lines, "a2", true), "a2 waits behind a1");
|
||||
// a1 runs: it leads.
|
||||
assert!(lines.leads(1, a1));
|
||||
let b1 = lines.push(2, "b1");
|
||||
let a2 = lines.push(1, "a2");
|
||||
assert_eq!((b1, a2), (2, 3));
|
||||
assert!(!lines.leads(1, a2), "a2 waits behind a1, which may block");
|
||||
lines.done(1);
|
||||
assert!(leads(&lines, "a2", true));
|
||||
assert!(lines.leads(1, a2));
|
||||
assert_eq!(lines.take(), Some((2, "b1")));
|
||||
assert_eq!(lines.take(), Some((1, "a2")));
|
||||
assert!(leads(&lines, "a2", true));
|
||||
assert!(lines.leads(1, a2));
|
||||
lines.done(1);
|
||||
lines.done(2);
|
||||
assert!(lines.is_empty());
|
||||
}
|
||||
|
||||
/// An item leads its line behind items that may not block, waiting there or run by a thread,
|
||||
/// however many: not behind one that may, waiting or run, before it; one that may after it
|
||||
/// changes nothing. A line taken whole or held idle for what may not block is led past too.
|
||||
#[test]
|
||||
fn an_item_leads_its_line_behind_what_may_not_block() {
|
||||
let mut lines: Lines<&str> = Lines::new(|item| item.ends_with('!'));
|
||||
lines.push(1, "a1");
|
||||
let a2 = lines.push(1, "a2");
|
||||
let a3 = lines.push(1, "a3!");
|
||||
let a4 = lines.push(1, "a4");
|
||||
let a5 = lines.push(1, "a5!");
|
||||
assert!(lines.leads(1, a2), "behind a1, which may not block");
|
||||
assert!(lines.leads(1, a3), "itself may block: led past a1 and a2");
|
||||
assert!(!lines.leads(1, a4), "behind a3, which may block");
|
||||
assert_eq!(lines.take(), Some((1, "a1")));
|
||||
// a1 runs.
|
||||
assert!(lines.leads(1, a2) && lines.leads(1, a3));
|
||||
lines.done(1);
|
||||
assert_eq!(lines.take(), Some((1, "a2")));
|
||||
lines.done(1);
|
||||
assert_eq!(lines.take(), Some((1, "a3!")));
|
||||
// a3 runs, and may block: a4 waits behind it.
|
||||
assert!(!lines.leads(1, a4));
|
||||
lines.done(1);
|
||||
assert!(lines.leads(1, a4), "a5 after it changes nothing");
|
||||
assert!(lines.leads(1, a5), "behind a4, which may not block");
|
||||
assert_eq!(lines.take(), Some((1, "a4")));
|
||||
assert!(lines.leads(1, a5), "behind a4 running, which may not block");
|
||||
lines.done(1);
|
||||
assert_eq!(lines.take(), Some((1, "a5!")));
|
||||
lines.done(1);
|
||||
assert!(lines.is_empty());
|
||||
// A line taken whole for what may not block, and for what may.
|
||||
let plain = |_: &&str| true;
|
||||
lines.push(2, "b1");
|
||||
assert_eq!(
|
||||
lines.take_plain(2, plain, false),
|
||||
Some(VecDeque::from(["b1"]))
|
||||
);
|
||||
let b2 = lines.push(2, "b2");
|
||||
assert!(lines.leads(2, b2), "the thread runs what may not block");
|
||||
lines.done(2);
|
||||
lines.push(2, "b3!");
|
||||
assert_eq!(
|
||||
lines.take_plain(2, plain, false),
|
||||
Some(VecDeque::from(["b2", "b3!"]))
|
||||
);
|
||||
let b4 = lines.push(2, "b4");
|
||||
assert!(!lines.leads(2, b4), "the thread runs b3, which may block");
|
||||
lines.done(2);
|
||||
assert_eq!(lines.take(), Some((2, "b4")));
|
||||
lines.done(2);
|
||||
assert_eq!(lines.take_plain(2, plain, true), Some(VecDeque::new()));
|
||||
let b5 = lines.push(2, "b5");
|
||||
assert!(!lines.leads(2, b5), "the thread runs what may block");
|
||||
lines.done(2);
|
||||
assert_eq!(lines.take(), Some((2, "b5")));
|
||||
lines.done(2);
|
||||
// A line held idle, for what may not block and for what may.
|
||||
for blocks in [false, true] {
|
||||
assert!(lines.take_idle(3, blocks));
|
||||
let c = lines.push(3, "c");
|
||||
assert_eq!(
|
||||
lines.leads(3, c),
|
||||
!blocks,
|
||||
"held for what may block: {blocks}"
|
||||
);
|
||||
lines.done(3);
|
||||
assert_eq!(lines.take(), Some((3, "c")));
|
||||
lines.done(3);
|
||||
}
|
||||
assert!(lines.is_empty());
|
||||
}
|
||||
|
||||
@@ -417,7 +498,6 @@ fn a_plain_item_runs_alone_only_where_its_context_is_idle() {
|
||||
/// waits for the test to go on, as a transition's hold opens its command buffer's gate as it goes
|
||||
/// (`Run::Held`). Plain, so a thread that runs its context itself may run it.
|
||||
struct Ends {
|
||||
name: &'static str,
|
||||
told: Option<Tell>,
|
||||
}
|
||||
|
||||
@@ -452,14 +532,14 @@ impl Run for Ends {
|
||||
}
|
||||
|
||||
/// An [`Ends`] that tells, with the test's ends of its channels: told as it ends, and the go-on.
|
||||
fn telling(name: &'static str) -> (Ends, mpsc::Receiver<()>, mpsc::Sender<()>) {
|
||||
fn telling() -> (Ends, mpsc::Receiver<()>, mpsc::Sender<()>) {
|
||||
let (ended, ends) = mpsc::channel();
|
||||
let (go_on, goes_on) = mpsc::channel();
|
||||
let told = Some(Tell {
|
||||
ended,
|
||||
go_on: goes_on,
|
||||
});
|
||||
(Ends { name, told }, ends, go_on)
|
||||
(Ends { told }, ends, go_on)
|
||||
}
|
||||
|
||||
/// An item's context is done with it before what it still holds goes (`Run::Held`), on the
|
||||
@@ -472,20 +552,18 @@ fn telling(name: &'static str) -> (Ends, mpsc::Receiver<()>, mpsc::Sender<()>) {
|
||||
#[test]
|
||||
fn an_item_s_context_is_done_with_it_before_what_it_held_goes() {
|
||||
let contexts: Contexts<Ends> = Contexts::start(1).unwrap();
|
||||
let leads =
|
||||
|queue, name: &'static str| contexts.leads(queue, |item: &Ends| item.name == name, true);
|
||||
let quiet = |name| Ends { name, told: None };
|
||||
let (first, ends, go_on) = telling("first");
|
||||
let quiet = || Ends { told: None };
|
||||
let (first, ends, go_on) = telling();
|
||||
contexts.post(1, first);
|
||||
ends.recv_timeout(WAIT).unwrap();
|
||||
contexts.post(1, quiet("next"));
|
||||
let next = contexts.post(1, quiet());
|
||||
assert!(
|
||||
leads(1, "next"),
|
||||
contexts.leads(1, next),
|
||||
"the pool's thread holds the context still"
|
||||
);
|
||||
for (queue, alone) in [(2, false), (3, true)] {
|
||||
let (item, ends, item_goes_on) = telling("here");
|
||||
let (earlier, earlier_ends, earlier_goes_on) = telling("earlier");
|
||||
let (item, ends, item_goes_on) = telling();
|
||||
let (earlier, earlier_ends, earlier_goes_on) = telling();
|
||||
let here = {
|
||||
let contexts = contexts.clone();
|
||||
std::thread::spawn(move || match alone {
|
||||
@@ -501,9 +579,9 @@ fn an_item_s_context_is_done_with_it_before_what_it_held_goes() {
|
||||
earlier_goes_on.send(()).unwrap();
|
||||
}
|
||||
ends.recv_timeout(WAIT).unwrap();
|
||||
contexts.post(queue, quiet("next"));
|
||||
let next = contexts.post(queue, quiet());
|
||||
assert!(
|
||||
leads(queue, "next"),
|
||||
contexts.leads(queue, next),
|
||||
"the thread that ran it holds the context still (alone: {alone})"
|
||||
);
|
||||
item_goes_on.send(()).unwrap();
|
||||
|
||||
@@ -6863,7 +6863,8 @@ pub struct WorkCounts {
|
||||
pub batches_held_for_another_queue: u64,
|
||||
/// Command buffers whose transition to `Completed` waited, its place in its queue's context
|
||||
/// kept, for the transition of another queue's command buffer whose GPU signal it waited for
|
||||
/// to reach its handlers, where that transition led its own context
|
||||
/// to reach its handlers, where nothing that may block (a handler of the program's other than
|
||||
/// its completion-seen handlers) came before that transition in its own context
|
||||
/// (`CommandBufferShared::finish_here`): Metal's completion follows the GPU's.
|
||||
pub command_buffers_completed_after_another_queue: u64,
|
||||
/// Completion-seen handlers run (`CommandBuffer::add_completion_seen_handler`), each as its
|
||||
|
||||
@@ -1704,8 +1704,11 @@ struct KeptSignalWait {
|
||||
/// Whether `X`'s group went while `B` still ran (at `A`'s completion turn); `None` where no
|
||||
/// group went before `B` completed and the wait gave up.
|
||||
sent_while_b_ran: Option<bool>,
|
||||
/// From `X`'s completed handler to `Y`'s.
|
||||
y_after_x: Option<std::time::Duration>,
|
||||
/// `X`'s status as `Y`'s completed handler ran; `None` where it never ran.
|
||||
x_at_y: Option<CommandBufferStatus>,
|
||||
/// `Y`'s GPU start less `X`'s GPU end, in seconds, where the device's own timestamps placed
|
||||
/// both (it announced neither `UncalibratedGpuTimes` nor `GpuTimesFromHost`).
|
||||
gpu_gap: Option<f64>,
|
||||
/// The groups from `Y`'s take-in by what sent them ([`group_ends`]).
|
||||
ends: [u64; 9],
|
||||
}
|
||||
@@ -1765,16 +1768,6 @@ fn behind_a_kept_signal(stretch: usize) -> KeptSignalWait {
|
||||
blit.end_encoding().unwrap();
|
||||
command_buffer
|
||||
};
|
||||
let completed_at = |command_buffer: &CommandBuffer| {
|
||||
let at = Arc::new(Mutex::new(None));
|
||||
let seen = Arc::clone(&at);
|
||||
command_buffer
|
||||
.add_completed_handler(Box::new(move |_: &CommandBuffer| {
|
||||
*seen.lock().unwrap() = Some(std::time::Instant::now());
|
||||
}))
|
||||
.unwrap();
|
||||
at
|
||||
};
|
||||
// `A`, then `B` behind it, each submitted as it is taken in: two submissions in flight.
|
||||
let running = long();
|
||||
running.commit().unwrap();
|
||||
@@ -1790,7 +1783,10 @@ fn behind_a_kept_signal(stretch: usize) -> KeptSignalWait {
|
||||
blit.fill_buffer(&buffers[0], 0..BYTES, 1).unwrap();
|
||||
blit.end_encoding().unwrap();
|
||||
x.encode_signal_event(EventRef::from(&event), 1).unwrap();
|
||||
let x_completed = completed_at(&x);
|
||||
// A completed handler, which may block: `X`'s transition runs on its queue's handler
|
||||
// thread, which `Y`'s waits for.
|
||||
x.add_completed_handler(Box::new(|_: &CommandBuffer| {}))
|
||||
.unwrap();
|
||||
x.commit().unwrap();
|
||||
taken_in(&device);
|
||||
x.wait_until_scheduled().unwrap();
|
||||
@@ -1801,7 +1797,14 @@ fn behind_a_kept_signal(stretch: usize) -> KeptSignalWait {
|
||||
let mut blit = y.blit_command_encoder().unwrap();
|
||||
blit.fill_buffer(&buffers[1], 0..BYTES, 2).unwrap();
|
||||
blit.end_encoding().unwrap();
|
||||
let y_completed = completed_at(&y);
|
||||
let x_at_y: Arc<Mutex<Option<CommandBufferStatus>>> = Arc::default();
|
||||
{
|
||||
let (x, seen) = (x.clone(), Arc::clone(&x_at_y));
|
||||
y.add_completed_handler(Box::new(move |_: &CommandBuffer| {
|
||||
*seen.lock().unwrap() = Some(x.status());
|
||||
}))
|
||||
.unwrap();
|
||||
}
|
||||
y.commit().unwrap();
|
||||
taken_in(&device);
|
||||
let taken = device.work_counts();
|
||||
@@ -1838,9 +1841,13 @@ fn behind_a_kept_signal(stretch: usize) -> KeptSignalWait {
|
||||
.count();
|
||||
assert_eq!(wrong, 0, "buffer {at}: bytes off its fill");
|
||||
}
|
||||
let y_after_x = (*y_completed.lock().unwrap())
|
||||
.zip(*x_completed.lock().unwrap())
|
||||
.and_then(|(y, x)| y.checked_duration_since(x));
|
||||
// Read before the notices: a device announces how it times command buffers as it first
|
||||
// times one.
|
||||
let times = (x.gpu_end_time().ok()).zip(y.gpu_start_time().ok());
|
||||
let timed = !messages.announced(Degradation::UncalibratedGpuTimes)
|
||||
&& !messages.announced(Degradation::GpuTimesFromHost);
|
||||
// A wait returns once the command buffer's completed handlers ran.
|
||||
let x_at_y = *x_at_y.lock().unwrap();
|
||||
KeptSignalWait {
|
||||
device: device.name().to_owned(),
|
||||
merging,
|
||||
@@ -1849,7 +1856,8 @@ fn behind_a_kept_signal(stretch: usize) -> KeptSignalWait {
|
||||
kept,
|
||||
a_ran,
|
||||
sent_while_b_ran,
|
||||
y_after_x,
|
||||
x_at_y,
|
||||
gpu_gap: (times.filter(|_| timed)).map(|(x_end, y_start)| y_start - x_end),
|
||||
ends: group_ends(&taken, &device.work_counts()),
|
||||
}
|
||||
};
|
||||
@@ -1886,9 +1894,21 @@ fn a_shared_event_wait_for_a_kept_group_s_signal_ends_at_the_next_completion_tur
|
||||
stretch *= 2;
|
||||
eprintln!("GROUPED-KEPT-SIGNAL: A ended before Y was taken in: A {stretch} times as long");
|
||||
};
|
||||
// `Y` completes after `X`, whose signal its work waited for: `X` reads `Completed` in `Y`'s
|
||||
// completed handler, the runtime's host threads however slow (its `command_buffer/follow.rs`).
|
||||
// Which of the two handlers starts first is not checked: they run on two threads, in no
|
||||
// guaranteed order, as on Metal, where an M3 Max under macOS 27.0.1 started the waiter's
|
||||
// completed handler first in up to 6% of rounds with a plain event and 0.1% with a shared one
|
||||
// (a probe of the same shape: a fill signalling an event, another queue's fill waiting for
|
||||
// it, 2000 rounds a row).
|
||||
assert_eq!(
|
||||
seen.x_at_y,
|
||||
Some(CommandBufferStatus::Completed),
|
||||
"Y completed while X, whose signal it waits for, read short of Completed: {seen:?}"
|
||||
);
|
||||
assert!(
|
||||
seen.y_after_x.is_some(),
|
||||
"Y completed before X, whose signal it waits for: {seen:?}"
|
||||
seen.gpu_gap.is_none_or(|gap| gap >= 0.0),
|
||||
"Y's GPU work began before X's, whose signal it waits for, ended: {seen:?}"
|
||||
);
|
||||
if !seen.grouping {
|
||||
assert_eq!(
|
||||
|
||||
Vendored
+1
-1
@@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 525;
|
||||
/// changes no behaviour, and then updates that hash alone with the commit saying why, or changes
|
||||
/// behaviour and also bumps this version. The commit that bumps it says what changed; this comment
|
||||
/// keeps no history.
|
||||
pub const RUNTIME_VERSION: u32 = 2502;
|
||||
pub const RUNTIME_VERSION: u32 = 2503;
|
||||
|
||||
/// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex,
|
||||
/// of the manifest, the build script, and exactly the files the crate roots' module tree compiles
|
||||
|
||||
Reference in new issue
Block a user