Tests: ten piece tests; the README's queue section on pieces and the device's room; two expectations at the device's room

The pieces tests (src/tests/pieces.rs), each on every device of the cfg's kind, a kernel's rate the test's own input:
1. six dispatches at 3 ms a threadgroup are six pieces (five cuts, six submissions), at 1.5 ms three;
2. another queue's fill goes between two pieces, counted once in Metrics::pieces_passed;
3. another queue's read of what a cut command buffer writes goes after its last piece, held once
   (Metrics::shared_holds), and reads every step;
4. the queue's own next command buffer reads every step of its cut one;
5. a fence wait on another queue goes after the last piece of the update, held once;
6. three dispatches of a kernel of unknown rate and one of another are two pieces, each piece's
   run sets its kernel's rate, and a dispatch past the bound is not cut (a second after it is);
7. a blit encoder after a piece past the bound cuts, one after a piece within it does not;
8. a cut command buffer is scheduled, its handler run, with two of four pieces submitted;
9. an abandon keeps a scheduled cut command buffer: every piece runs;
10. the Iris shape: five queues each submitted once and hold a backlog of two cut command buffers
    of four pieces, the device takes two pieces, and a sixth queue, idle, commits a fill: the
    pieces submitted ahead of it are counted and asserted to be 2.

Two expectations change with the device's room, each an "at once" become "when the device has room":
- command.rs every_device_submits_a_write_on_an_idle_queue_before_another_queues_later_read. Old: the
  writer submitted at once (submissions > before), then the reader (> before + 1), while the busy
  queue's three are in flight. New: each held in a group for the device's room, nothing in flight
  (in_flight (0, true) for both), the writer going first by commit order. The order assertion, the
  read sees the write, is unchanged.
- fence.rs, renamed every_device_runs_a_wait_on_a_fence_never_updated_at_once to
  every_device_runs_a_wait_on_a_fence_never_updated_when_the_device_has_room. Old: after the wait's
  commit (all, b_sent, a_sent, a_held, alone, own, rounds) = (1, 1, 0, true, 0, 0, 0). New:
  (all, b_sent, b_held, a_sent, a_held, alone, own, rounds) = (0, 0, true, 0, true, 0, 0, 0): B's
  group waits for the device's room behind A's three busy submissions. The third queue's empty
  wait still completes at once, and the wait completes once the busy work opens.
No order assertion is weakened.

Docs: the runtime README's "Queues, command buffers and events" states the device's room at 4 ms,
the rank (the queue that waited longest; an idle queue's commit first; a backlog waits for as
long as other queues that commit only when idle keep the room taken, Metal promising no order
between queues, with no aging rule), the pieces and their cut points, the shared-resource rule
and the GPU times of a cut command buffer. Its known gaps: what the pieces do not cut (a dispatch
past 4 ms, a render pass, a kernel not yet measured, a piece that writes a Managed buffer or owes
copies over a texture), and the conservative shared rule, against the 5300M (job m3-max-68137)
and the UHD 630 (job m3-max-68129); the job-timeout gap now says the cut is only between
dispatches and encoders. Metrics::cuts and the CommandBuffer type doc state the same-kernel rule.
refusal_surface.py --check passes.
This commit is contained in:
Anees Iqbal committed 2026-11-22 11:11:23 -08:00
1 parent f997dfa7d3
commit 544f21b48f
7 files changed
+576 -53

No files matched your search

+51 -17
View File
@@ -1811,14 +1811,22 @@ made, with no lock, and never rewritten for layouts.
each upload that holds a released object. `Metrics::held_releases` and `held_release_bytes`
count every release they held, and `held_release_starts` those that were the first since they
came to hold the object, each a submission it started. While
a queue has room on the device's `VkQueue`, each of its command buffers goes at once, alone. It
has room while fewer than two of its submissions are not yet seen complete, and while fewer than
three are whose expected GPU runs (below) behind the oldest, with the next's, sum to 16 ms or
less; a run not yet known counts as past 16 ms. So a queue whose run is not yet known (none of
its command buffers has retired with its GPU times) has room for two submissions, and its third
command buffer waits for one seen complete. A queue keeps on the `VkQueue` no more than about 16
ms of work behind what runs, and one behind it always, and a command buffer of another queue
committed later waits behind no more of it. Without room, the ones committed meanwhile are held
the device has room on its `VkQueue`, a command buffer goes at once, alone. It has room while
fewer than two of its submissions, of every queue, are not yet seen complete, and while fewer
than three are whose expected GPU runs (below) behind the oldest, with the next's, sum to 4 ms
or less; a run not yet known counts as past 4 ms. So while a queue's run is not yet known (none
of its command buffers has retired with its GPU times), its command buffer goes only where fewer
than two submissions are on the device. The device keeps on the `VkQueue` no more than about
4 ms of work behind what runs, and one behind it always, whatever the queues, and a command
buffer committed later waits behind no more of it: behind a running piece of at most 4 ms
(below), such a wait stays within 16 ms. The room goes first to the queue that waited longest: by
its turn, its last submission, or none where its command buffer was committed with nothing of
its queue queued or in flight, then in commit order. So a command buffer of an idle queue goes
at the device's next room, behind what runs and the one behind it, ahead of every queue's
backlog, and a backlog whose last piece just completed passes no queue idle longer. A queue's
backlog waits for as long as other queues that commit only when idle keep the device's room
taken: Metal promises no order between queues, and the runtime keeps no aging rule for it.
Without room, the ones committed meanwhile are held
in one group of at most sixteen, each scheduled as it joins; one the group does not take is held
too, not yet in a group, scheduled as it would be in one (its waits met, in its queue's order,
its scheduled handlers run: `Metrics::held_for_room`), and so are its queue's later command
@@ -1836,18 +1844,34 @@ made, with no lock, and never rewritten for layouts.
expected GPU run: a queue's expected run follows its command buffers'
own GPU times as each retires (a longer one raises it at once, a shorter one halves the way toward
it), and a command buffer joins a group only while the group's expected runs, summed, stay within
16 ms. One whose queue has no run yet, or that would take the sum past the bound, goes alone, the
4 ms. One whose queue has no run yet, or that would take the sum past the bound, goes alone, the
group held going first. The bound is tested again as the group goes, with the expected run as it
then stands: a group past it goes member by member, one `vkQueueSubmit2` each, only the last
signalling. At most four command buffers join a group while none of the queue's retires, since
they join on an expected run no completion has updated: in
a replay stream on an RTX 3090, the most GPU time over four consecutive command buffers of a queue
was 385.1 ms, within a quarter of amdgpu's default job timeout of 2,000 ms, and over sixteen
1,536.6 ms. The thread holds a segment whose event waits are not met, and its queue's later
1,536.6 ms. A command buffer's compute work goes to the `VkQueue` in pieces, each one
submission that waits for the device's room as a command buffer does: the runtime cuts it
before a compute dispatch that would take the piece's expected GPU run past 4 ms, between the
dispatches of a kernel with no measured rate yet, which share a piece of their own, and any
other, and where a compute or blit encoder opens after a full piece, never before or inside a
render pass (`Metrics::cuts`). A dispatch's expected run is its kernel's rate, the GPU run of
one threadgroup as the pieces that ran the kernel measured with their own timestamps, times its
threadgroups; an indirect dispatch, whose threadgroups only the GPU reads, counts as one. So a
command buffer of another queue committed while one runs goes between two of its pieces
(`Metrics::pieces_passed`), unless it holds a buffer or texture the cut one holds (by the ids
the hazards track them by, a heap's resources by the heap's), waits on a fence the cut one
updates, or either reaches memory outside its resources: then it goes after the last piece
(`Metrics::shared_holds`). The cut command buffer is scheduled as its first piece is submitted,
where none of its later pieces waits for an event, and its GPU times run from its first piece's
start to its last piece's end, the runs of what went between its pieces included, as on the UHD
630 under macOS 26.7.1 a running command buffer's end moved by the run of one that started
beside it (job m3-max-68129). The thread holds a segment whose event waits are not met, and its queue's later
command buffers behind it, while the segments before it run, so the host can see a signal the
command buffer makes before its wait. Other queues go on. Each queue groups on its own count:
a command buffer of a queue with nothing running is submitted at once, ahead of every command
buffer committed after it on any queue, while one its queue holds, in a group or for room, may
a command buffer of a queue with nothing queued or running is submitted at the device's next
room, ahead of every command buffer committed after it on any queue, while one its queue holds, in a group or for room, may
be passed by another queue's command buffer committed after it, which only a fence or an event
then orders after it. Metal's hazard tracking orders such a pair against work already submitted, and no
more. Recorded on an M3 Max with macOS 27.0.1, with tracked buffers and no fence, a blit fill on
@@ -1927,8 +1951,8 @@ made, with no lock, and never rewritten for layouts.
`CommandQueue::command_buffer` on a queue whose maximum count are made and not yet completed,
which a scheduled handler answers the same way. No wait of the runtime's hangs by construction.
A command buffer of a group is seen complete with the group's last: up to the GPU time of the
fifteen after it late. Only a queue without room holds a group; one whose
submissions have completed sends its next command buffer alone. Without a wait or a hint, the lateness is bounded only while the queue has no room,
fifteen after it late. Only a queue that finds the device without room holds a group; one that
finds room sends its next command buffer alone. Without a wait or a hint, the lateness is bounded only while the queue has no room,
and the completion that leaves room sends the group (the measured lateness is under
"Known gaps"). A command buffer ended while its queue holds a group joins it, and retires in its
place; the group goes at once.
@@ -2085,14 +2109,24 @@ while the driver's exit handler waited on a lock, with every runtime thread alre
The call's time grows with the new block it is the first to use: 2.1 and 2.5 ms with a 1 MiB
block, 9.1 ms with 64 MiB and 28.4 ms with 256 MiB (job m3-max-52745); the probe's later
submissions spent at most 0.18 ms each.
- A command buffer of another queue waits for the whole of what the pieces do not cut: a
dispatch whose own expected run passes 4 ms, a render pass and the encoders after it, the
dispatches of a kernel whose rate is not yet measured, and the commands of a piece that writes
a Managed buffer or while copies over a texture are owed. It waits behind every piece of a cut
command buffer that holds one of its buffers or textures, a read of a read included. Metal
starts it beside the running command buffer: on a Radeon Pro 5300M another queue's command
buffer started 0.21 to 0.40 ms after its commit beside a running one (job m3-max-68137), and on
an Intel UHD 630 under macOS 26.7.1 one of a queue at a lower priority number started about
0.4 ms after its commit, mid-way through a running 60 ms command buffer, whose own run did not
change (job m3-max-68129). The runtime starts it at the next piece's boundary.
- On NVIDIA each submission pays the driver's latency per submission. The runtime submits each
command buffer as soon as it can, so short command buffers committed back to back reach the GPU
in several submissions: on an RTX 2080 (harness row `frame_5cb`: five short command buffers a
run, two runs in flight) a run takes about 2.5 submissions, and each submission boundary leaves
the GPU idle for about 20-25 µs.
- One command buffer whose own GPU run passes the driver's job timeout still ends there: the
runtime does not cut a command buffer's work into shorter jobs, and joins none to it (the
grouping bound above). Linux's amdgpu ends a job past its timeout, 2 s by default (Linux 7.3.3),
- One dispatch, render pass or blit encoder whose own GPU run passes the driver's job timeout
still ends there: the runtime cuts a command buffer's compute work only between dispatches and
where an encoder opens (the pieces above), and joins none to it (the grouping bound above). Linux's amdgpu ends a job past its timeout, 2 s by default (Linux 7.3.3),
and resets the ring, where Metal runs the command buffer within its own watchdog's longer bound.
- A command buffer whose timestamps are not available when its submission signals reports no GPU
times: its `gpu_start_time` and `gpu_end_time` are 0, and `Metrics::stamps_not_available`
+5 -4
View File
@@ -37,11 +37,12 @@ use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
/// part waits for can be signalled by the part before it. Each segment's commands end with one
/// barrier that makes their writes visible to every later command and to the host. Its compute
/// work is cut into pieces too, before a dispatch that would take a piece's expected GPU run past
/// 4 ms and where a compute or blit encoder opens after a full piece, never inside a render pass
/// 4 ms, between the dispatches of a kernel with no measured rate yet and any other, and where a
/// compute or blit encoder opens after a full piece, never before or inside a render pass
/// (`crate::piece`): each piece is a submission of its own, and a command buffer of another queue
/// committed meanwhile goes between two pieces unless it holds a buffer or texture the cut one
/// holds. It is scheduled as its first piece is submitted where its later pieces wait for no
/// event.
/// committed meanwhile goes between two pieces unless it shares a buffer, a texture or a fence
/// with the cut one. It is scheduled as its first piece is submitted where its later pieces wait
/// for no event.
///
/// Its GPU times come from two timestamps, written where its first commands begin and as the last
/// command of its last segment, after the copies back of the images its commands wrote and its end
+6 -5
View File
@@ -2633,11 +2633,12 @@ pub struct Metrics {
/// as they began count here too, and their time in
/// [`runtime_command_buffer_gpu_nanos`](Self::runtime_command_buffer_gpu_nanos) is lost.
pub untimed_command_buffers: u64,
/// No Metal counterpart: the cuts that ended a piece of a command buffer, before a compute
/// dispatch that would take the piece's expected GPU run past 4 ms, or that follows one of a
/// kernel with no measured rate yet, or where a compute or blit encoder opens after a full
/// piece. Each piece is a submission of its own, so a command buffer of another queue
/// committed meanwhile goes between two pieces ([`CommandBuffer`](crate::CommandBuffer)).
/// No Metal counterpart: the cuts that ended a piece of a command buffer: before a compute
/// dispatch that would take the piece's expected GPU run past 4 ms; between the dispatches of
/// a kernel with no measured rate yet, which share a piece of their own, and any other; and
/// where a compute or blit encoder opens after a full piece. Each piece is a submission of
/// its own, so a command buffer of another queue committed meanwhile goes between two pieces
/// ([`CommandBuffer`](crate::CommandBuffer)).
pub cuts: u64,
/// Submissions of a queue's command buffers that went to the `VkQueue` while a cut command
/// buffer of another queue had pieces not yet submitted: they run before those pieces.
@@ -1802,15 +1802,15 @@ pub(crate) mod on_device {
}
}
/// A write on a queue with nothing running is submitted at once, so a command buffer of
/// another queue committed after it reads the write, with no fence or event between them:
/// every queue's submissions go to the device's one `VkQueue`, each segment ending in a barrier
/// that reaches the commands after it. A third queue's busy work holds the GPU, so neither
/// runs before both are submitted, and puts as many submissions in flight across the device
/// as a queue keeps before it groups: the writer's queue groups on its own count alone.
/// Metal's hazard tracking orders the same pair of tracked buffers where the writer's queue
/// has nothing ahead of it, and not where its queue's own work is ahead of it (the README's
/// "Queues, command buffers and events").
/// A write on a queue with nothing running is submitted when the device has room, before a
/// command buffer of another idle queue committed after it, so that one reads the write, with
/// no fence or event between them: every queue's submissions go to the device's one
/// `VkQueue`, each segment ending in a barrier that reaches the commands after it, and of
/// idle queues the device's room goes in commit order (`WorkState::rank`). A third queue's
/// busy work holds the GPU and fills the device's room, so both wait, scheduled, and neither
/// runs before both are submitted. Metal's hazard tracking orders the same pair of tracked
/// buffers where the writer's queue has nothing ahead of it, and not where its queue's own
/// work is ahead of it (the README's "Queues, command buffers and events").
#[test]
fn every_device_submits_a_write_on_an_idle_queue_before_another_queues_later_read() {
each_device("command_cross_queue_order", |name, _, device| {
@@ -1819,16 +1819,23 @@ pub(crate) mod on_device {
let (busy_queue, writer_queue, reader_queue) =
(queue(&device), queue(&device), queue(&device));
behind_busy(name, &device, &busy_queue, |busy, _, _| {
let sent = || device.counters().metrics().submissions;
let before = sent();
let writer = queue_fill(&writer_queue, &written, 0x5a5a_5a5a);
within(name, || sent() > before);
within(name, || writer.status() != CommandBufferStatus::Committed);
let reader = reader_queue.command_buffer().expect("a command buffer");
let mut blit = reader.blit_command_encoder().expect("an encoder");
(blit.copy_from_buffer_to_buffer(&written, 0, &read, 0, 256)).expect("a copy");
blit.end_encoding();
reader.commit().expect("a commit");
within(name, || sent() > before + 1);
within(name, || reader.status() != CommandBufferStatus::Committed);
let work = &device.core().work;
assert_eq!(
(
work.in_flight(writer.queue()),
work.in_flight(reader.queue())
),
((0, true), (0, true)),
"{name}: each held in a group for the device's room, nothing in flight"
);
busy.open();
finished(name, &reader);
finished(name, &writer);
+17 -14
View File
@@ -339,14 +339,15 @@ 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, with no round of the
/// completion thread run since the wait's commit: a command buffer with nothing for the GPU
/// retires in one.
/// A wait on a fence no encoder updated waits for nothing but the device's room, while queue A
/// holds in a group an update of another fence: on queue B, a command buffer with work after
/// the wait joins a group of its own, held while A's busy work fills the device's room, A's
/// group staying held too, and both go when the device has room; on a third queue an encoder
/// that only 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() {
fn every_device_runs_a_wait_on_a_fence_never_updated_when_the_device_has_room() {
each_device("fence_never_updated", |name, _, device| {
let (written, copy) = written_and_copy(&device);
let updated = device.new_fence();
@@ -378,17 +379,19 @@ mod on_device {
let all = after.submissions - before.submissions;
let (a_sent, a_held) = work.in_flight(updater.queue());
let a_sent = a_sent.saturating_sub(IN_FLIGHT) as u64;
let b_sent = work.in_flight(waiter.queue()).0 as u64;
let (b_sent, b_held) = work.in_flight(waiter.queue());
let b_sent = b_sent as u64;
let alone = all.saturating_sub(a_sent + b_sent);
let own = after.runtime_command_buffers - before.runtime_command_buffers;
let rounds = device.core().pool.rounds() - rounds;
assert_eq!(
(all, b_sent, a_sent, a_held, alone, own, rounds),
(1, 1, 0, true, 0, 0, 0),
"{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"
(all, b_sent, b_held, a_sent, a_held, alone, own, rounds),
(0, 0, true, 0, true, 0, 0, 0),
"{name}: after the wait's commit, the submissions: all of them; B's, and \
whether B holds the wait's group for the device's room; 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"
);
let empty = c.command_buffer().expect("a command buffer");
let mut compute =
@@ -74,6 +74,7 @@ mod over_aliases;
mod over_causes;
mod pass;
mod patch;
mod pieces;
mod pixel_format_info;
mod pixel_interlock;
mod places;
@@ -0,0 +1,476 @@
//! Pieces, under a device cfg on every device of the cfg's kind: a command buffer's compute work
//! goes to the device's `VkQueue` in pieces, each expected to run for at most `GROUP_RUN`
//! (`crate::piece`), each a submission that waits for the device's room. A kernel's rate here is
//! the test's own input (`Rate::set_for_test`), set before each recording, so where a piece ends
//! does not depend on how fast the device runs. Behind the busy gate (`Busy::new`) nothing
//! submitted completes until the test opens it, so the device holds the two pieces its room
//! takes and the rest wait: a command buffer of another queue goes between two pieces, one that
//! shares a resource or a fence with a cut command buffer goes after its last, the queue's own
//! next waits for every piece, a cut command buffer is scheduled at its first piece and kept by an
//! abandon, and an idle queue's commit waits for no more than the two pieces on the device
//! behind five queues' backlogs. Without the gate: the cuts counted, a kernel of unknown rate in
//! a piece of its own whose run sets its rate, a dispatch past the bound uncut, and a blit encoder
//! after a full piece cut.
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
mod on_device {
use crate::tests::command::on_device::{
each_device, fill, finished, shared_event, within, word, Arena, Busy, BODY_RUN,
};
use crate::tests::compute::on_device::{add, step};
use crate::{
Buffer, CommandBuffer, CommandBufferStatus, CommandQueue, ComputeCommandEncoder,
ComputePipelineState, Device, DispatchType,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
/// A kernel's rate of 3 ms a threadgroup, in picoseconds: one dispatch of one threadgroup
/// fits a piece of 4 ms (`GROUP_RUN`), two do not, and the device's room takes two such
/// pieces, not three (`submit::room`).
const RATE: u64 = 3_000_000_000;
fn queue(device: &Device) -> CommandQueue {
device.new_command_queue(64).expect("a queue")
}
/// A compute encoder of `count` dispatches of `state` (`x = x * 2 + 1`), each of one
/// threadgroup, over `buffer`, left open.
fn steps(
command_buffer: &CommandBuffer,
state: &ComputePipelineState,
buffer: &Buffer,
count: u32,
) -> ComputeCommandEncoder {
let mut encoder =
(command_buffer.compute_command_encoder(DispatchType::Serial)).expect("an encoder");
encoder.set_compute_pipeline_state(state).expect("a state");
encoder.set_buffer(Some(buffer), 0, 0).expect("a buffer");
for _ in 0..count {
(encoder.dispatch_threadgroups([1, 1, 1], [64, 1, 1])).expect("a dispatch");
}
encoder
}
/// A command buffer of `queue` with `count` steps over `buffer`, `state`'s rate set to
/// `rate` first, committed.
fn cut(
queue: &CommandQueue,
state: &ComputePipelineState,
rate: u64,
buffer: &Buffer,
count: u32,
) -> CommandBuffer {
state.inner.compiled.rate.set_for_test(rate);
let command_buffer = queue.command_buffer().expect("a command buffer");
steps(&command_buffer, state, buffer, count).end_encoding();
command_buffer.commit().expect("a commit");
command_buffer
}
/// The word `count` steps leave in a word that held 0.
fn stepped(count: u32) -> u32 {
(1 << count) - 1
}
/// Of the queues' submissions so far, in order, which were `of`'s queue's.
fn order(device: &Device, of: &CommandBuffer) -> Vec<bool> {
device.core().work.submitted_by(of.queue())
}
/// The queues' submissions so far.
fn sent(device: &Device, queue: &CommandQueue) -> usize {
order(device, &queue.command_buffer().expect("a command buffer")).len()
}
/// Waits for `command_buffer` to complete, as Metal's would, with no error.
fn completed(name: &str, command_buffer: &CommandBuffer) {
finished(name, command_buffer);
assert_eq!(command_buffer.error(), None, "{name}");
}
/// The cuts: at 3 ms a threadgroup six dispatches go in six pieces, five cuts, each its own
/// submission; at 1.5 ms, two to a piece, three pieces and two cuts. Every step lands.
#[test]
fn every_device_cuts_a_command_buffer_into_pieces_of_the_bound() {
each_device("pieces_cuts", |name, _, device| {
let mut arena = Arena::new(&device);
let state = step(&device);
let queue = queue(&device);
for (rate, pieces) in [(RATE, 6), (RATE / 2, 3)] {
let buffer = arena.buffer();
let (from, cuts) = (sent(&device, &queue), device.counters().metrics().cuts);
let command_buffer = cut(&queue, &state, rate, &buffer, 6);
completed(name, &command_buffer);
assert_eq!(
(
device.counters().metrics().cuts - cuts,
order(&device, &command_buffer).len() - from,
word(&buffer),
),
(pieces - 1, pieces as usize, stepped(6)),
"{name}: at {rate} ps a threadgroup, the cuts, the submissions and the word"
);
}
});
}
/// A command buffer of another queue goes between two pieces: A's command buffer of four
/// pieces has two on the device, the room's two, and B's fill, committed to an idle queue,
/// goes before A's third, counted once (`Metrics::pieces_passed`). Every step and the fill
/// land.
#[test]
fn every_device_runs_another_queues_command_buffer_between_two_pieces() {
each_device("pieces_passed", |name, _, device| {
let mut arena = Arena::new(&device);
let (stepped_buffer, filled) = (arena.buffer(), arena.buffer());
let state = step(&device);
let (a, b) = (queue(&device), queue(&device));
let busy = Busy::new(&device);
let from = sent(&device, &a);
let pieces = cut(&a, &state, RATE, &stepped_buffer, 4);
within(name, || order(&device, &pieces).len() == from + 2);
let passed = device.counters().metrics().pieces_passed;
let passer = b.command_buffer().expect("a command buffer");
fill(&passer, &filled, 7);
passer.commit().expect("a commit");
within(name, || passer.status() != CommandBufferStatus::Committed);
busy.open();
completed(name, &pieces);
completed(name, &passer);
assert_eq!(
order(&device, &passer)[from..],
[false, false, true, false, false],
"{name}: A's two pieces, B's fill, A's last two"
);
assert_eq!(
(
device.counters().metrics().pieces_passed - passed,
word(&stepped_buffer),
word(&filled),
),
(1, stepped(4), 7),
"{name}: the passes, the steps and the fill"
);
});
}
/// A command buffer of another queue that reads what a cut command buffer committed before
/// it writes goes after its last piece, held once (`Metrics::shared_holds`), though its queue
/// is idle, and reads every step.
#[test]
fn every_device_runs_a_read_of_a_cut_command_buffers_write_after_its_last_piece() {
each_device("pieces_shared", |name, _, device| {
let mut arena = Arena::new(&device);
let (written, read) = (arena.buffer(), arena.buffer());
let state = step(&device);
let (a, b) = (queue(&device), queue(&device));
let busy = Busy::new(&device);
let from = sent(&device, &a);
let pieces = cut(&a, &state, RATE, &written, 4);
within(name, || order(&device, &pieces).len() == from + 2);
let holds = device.counters().metrics().shared_holds;
let reader = b.command_buffer().expect("a command buffer");
let mut blit = reader.blit_command_encoder().expect("an encoder");
(blit.copy_from_buffer_to_buffer(&written, 0, &read, 0, 256)).expect("a copy");
blit.end_encoding();
reader.commit().expect("a commit");
within(name, || {
device.counters().metrics().shared_holds == holds + 1
});
busy.open();
completed(name, &pieces);
completed(name, &reader);
assert_eq!(
order(&device, &reader)[from..],
[false, false, false, false, true],
"{name}: A's four pieces, then B's read"
);
assert_eq!(word(&read), stepped(4), "{name}: the read sees every step");
});
}
/// A queue's next command buffer waits for every piece of its cut one: its copy reads every
/// step.
#[test]
fn every_device_runs_a_queues_next_command_buffer_after_every_piece() {
each_device("pieces_own_order", |name, _, device| {
let mut arena = Arena::new(&device);
let (written, read) = (arena.buffer(), arena.buffer());
let state = step(&device);
let a = queue(&device);
let busy = Busy::new(&device);
let from = sent(&device, &a);
let pieces = cut(&a, &state, RATE, &written, 4);
within(name, || order(&device, &pieces).len() == from + 2);
let next = a.command_buffer().expect("a command buffer");
let mut blit = next.blit_command_encoder().expect("an encoder");
(blit.copy_from_buffer_to_buffer(&written, 0, &read, 0, 256)).expect("a copy");
blit.end_encoding();
next.commit().expect("a commit");
busy.open();
completed(name, &pieces);
completed(name, &next);
assert_eq!(
(order(&device, &next).len() - from, word(&read)),
(5, stepped(4)),
"{name}: four pieces and the copy, which sees every step"
);
});
}
/// A wait on another queue for a fence a cut command buffer updates after its last
/// dispatch goes after that last piece, held once (`Metrics::shared_holds`), though it
/// shares no buffer with it.
#[test]
fn every_device_runs_a_fence_wait_after_the_last_piece_of_its_update() {
each_device("pieces_fence", |name, _, device| {
let mut arena = Arena::new(&device);
let (written, filled) = (arena.buffer(), arena.buffer());
let state = step(&device);
let fence = device.new_fence();
let (a, b) = (queue(&device), queue(&device));
let busy = Busy::new(&device);
state.inner.compiled.rate.set_for_test(RATE);
let updater = a.command_buffer().expect("a command buffer");
let mut encoder = steps(&updater, &state, &written, 4);
encoder.update_fence(&fence).expect("an update");
encoder.end_encoding();
let from = order(&device, &updater).len();
updater.commit().expect("a commit");
within(name, || order(&device, &updater).len() == from + 2);
let holds = device.counters().metrics().shared_holds;
let waiter = b.command_buffer().expect("a command buffer");
let mut blit = waiter.blit_command_encoder().expect("an encoder");
blit.wait_for_fence(&fence).expect("a wait");
(blit.fill_buffer(&filled, 0..4, 9)).expect("a fill");
blit.end_encoding();
waiter.commit().expect("a commit");
within(name, || {
device.counters().metrics().shared_holds == holds + 1
});
busy.open();
completed(name, &updater);
completed(name, &waiter);
assert_eq!(
(order(&device, &waiter)[from..].to_vec(), word(&filled)),
(vec![false, false, false, false, true], 0x0909_0909),
"{name}: A's four pieces, then B's wait and fill"
);
});
}
/// The dispatches of a kernel of unknown rate share a piece of their own: three of one such
/// kernel and then one of another make two pieces, one cut, and each piece's run sets its
/// kernel's rate. A dispatch past the bound is not cut, and a second after it is.
#[test]
fn every_device_puts_a_kernel_of_unknown_rate_in_a_piece_of_its_own() {
each_device("pieces_unknown_rate", |name, _, device| {
let mut arena = Arena::new(&device);
let (x, a, b, o) = (
arena.buffer(),
arena.buffer(),
arena.buffer(),
arena.buffer(),
);
let (stepping, adding) = (step(&device), add(&device));
let rate = |state: &ComputePipelineState| state.inner.compiled.rate.expected(1);
assert_eq!((rate(&stepping), rate(&adding)), (None, None), "{name}");
let queue = queue(&device);
let cuts = || device.counters().metrics().cuts;
let before = cuts();
let command_buffer = queue.command_buffer().expect("a command buffer");
let mut encoder = steps(&command_buffer, &stepping, &x, 3);
encoder
.set_compute_pipeline_state(&adding)
.expect("a state");
for (slot, buffer) in [&a, &b, &o].into_iter().enumerate() {
(encoder.set_buffer(Some(buffer), 0, slot as u32)).expect("a buffer");
}
(encoder.dispatch_threadgroups([1, 1, 1], [64, 1, 1])).expect("a dispatch");
encoder.end_encoding();
command_buffer.commit().expect("a commit");
completed(name, &command_buffer);
assert_eq!(
cuts() - before,
1,
"{name}: the steps' piece, then the add's"
);
assert!(
rate(&stepping).is_some() && rate(&adding).is_some(),
"{name}: each kernel's rate set by its piece's run"
);
for (count, cut_before) in [(1, 0), (2, 1)] {
let before = cuts();
completed(name, &cut(&queue, &stepping, 5 * RATE, &x, count));
assert_eq!(cuts() - before, cut_before, "{name}: {count} of 15 ms");
}
assert_eq!(word(&x), stepped(6), "{name}");
});
}
/// A blit encoder that opens after a piece past the bound begins the next piece, and one
/// after a piece within it does not.
#[test]
fn every_device_cuts_before_a_blit_encoder_after_a_full_piece() {
each_device("pieces_blit", |name, _, device| {
let mut arena = Arena::new(&device);
let (x, filled) = (arena.buffer(), arena.buffer());
let state = step(&device);
let queue = queue(&device);
for (rate, cuts) in [(5 * RATE / 3, 1), (RATE / 3, 0)] {
state.inner.compiled.rate.set_for_test(rate);
let before = device.counters().metrics().cuts;
let command_buffer = queue.command_buffer().expect("a command buffer");
steps(&command_buffer, &state, &x, 1).end_encoding();
let mut blit = command_buffer.blit_command_encoder().expect("an encoder");
(blit.fill_buffer(&filled, 0..4, 5)).expect("a fill");
blit.end_encoding();
command_buffer.commit().expect("a commit");
completed(name, &command_buffer);
assert_eq!(
device.counters().metrics().cuts - before,
cuts,
"{name}: after a piece of {rate} ps"
);
}
assert_eq!(
(word(&x), word(&filled)),
(stepped(2), 0x0505_0505),
"{name}"
);
});
}
/// A cut command buffer is scheduled as its first piece is staged, its scheduled handler
/// run, while two of its four pieces wait for the device's room.
#[test]
fn every_device_schedules_a_cut_command_buffer_at_its_first_piece() {
each_device("pieces_scheduled", |name, _, device| {
let mut arena = Arena::new(&device);
let x = arena.buffer();
let state = step(&device);
let queue = queue(&device);
let busy = Busy::new(&device);
state.inner.compiled.rate.set_for_test(RATE);
let command_buffer = queue.command_buffer().expect("a command buffer");
steps(&command_buffer, &state, &x, 4).end_encoding();
let scheduled = Arc::new(AtomicUsize::new(0));
let counted = Arc::clone(&scheduled);
(command_buffer.add_scheduled_handler(move |_| {
counted.fetch_add(1, Ordering::SeqCst);
}))
.expect("a handler");
let from = order(&device, &command_buffer).len();
command_buffer.commit().expect("a commit");
within(name, || order(&device, &command_buffer).len() == from + 2);
within(name, || scheduled.load(Ordering::SeqCst) == 1);
assert_eq!(
(
command_buffer.status(),
order(&device, &command_buffer).len() - from
),
(CommandBufferStatus::Scheduled, 2),
"{name}: scheduled, with two pieces submitted and two waiting for room"
);
busy.open();
completed(name, &command_buffer);
assert_eq!(word(&x), stepped(4), "{name}");
});
}
/// An abandon of a queue whose cut command buffer is scheduled with pieces still waiting for
/// room ends nothing: every piece runs, and the command buffer completes.
#[test]
fn every_device_keeps_a_scheduled_cut_command_buffer_through_an_abandon() {
each_device("pieces_abandon", |name, _, device| {
let mut arena = Arena::new(&device);
let x = arena.buffer();
let state = step(&device);
let queue = queue(&device);
let busy = Busy::new(&device);
let command_buffer = cut(&queue, &state, RATE, &x, 4);
within(name, || {
command_buffer.status() == CommandBufferStatus::Scheduled
});
queue.abandon();
busy.open();
completed(name, &command_buffer);
assert_eq!(word(&x), stepped(4), "{name}: every piece ran");
});
}
/// The shape of five queues with backlogs and a sixth, idle, committing one command buffer.
/// Each of the five submits a fill, the first piece of a cut command buffer whose next waits
/// for an event's value, and holds behind it a second of four pieces. The value set, the
/// device takes two pieces, its room, and the sixth queue, which last submitted before all of
/// them and expects a run of 2 ms, too long to go behind two pieces of 3 ms, commits a fill.
/// It goes at the device's next room, before any backlog's next piece, though the three queues
/// with nothing in flight hold command buffers committed before it: the pieces submitted
/// ahead of it are the two on the device when it committed. Every step lands.
#[test]
fn every_device_runs_an_idle_queues_commit_behind_two_pieces_of_five_backlogs() {
each_device("pieces_backlogs", |name, _, device| {
let mut arena = Arena::new(&device);
let state = step(&device);
let event = shared_event(&device);
let queues: Vec<CommandQueue> = (0..5).map(|_| queue(&device)).collect();
let buffers: Vec<Buffer> = (0..5).map(|_| arena.buffer()).collect();
let (idle, filled) = (queue(&device), arena.buffer());
let warm = idle.command_buffer().expect("a command buffer");
fill(&warm, &filled, 0);
warm.commit().expect("a commit");
completed(name, &warm);
warm.queue().set_run_for_test(2 * BODY_RUN);
state.inner.compiled.rate.set_for_test(RATE);
let base = order(&device, &warm).len();
let mut backlogs = Vec::new();
for (queue, buffer) in queues.iter().zip(&buffers) {
let head = queue.command_buffer().expect("a command buffer");
fill(&head, buffer, 0);
(head.encode_wait_for_event(event.as_event(), 1)).expect("a wait");
steps(&head, &state, buffer, 4).end_encoding();
head.commit().expect("a commit");
let next = queue.command_buffer().expect("a command buffer");
steps(&next, &state, buffer, 4).end_encoding();
next.commit().expect("a commit");
backlogs.extend([head, next]);
}
let work = &device.core().work;
within(name, || {
order(&device, &warm).len() == base + 5
&& (backlogs.iter()).all(|backlog| work.in_flight(backlog.queue()).0 == 0)
});
let busy = Busy::new(&device);
let from = base + 5;
event.set_signaled_value(1);
within(name, || order(&device, &warm).len() == from + 2);
let late = idle.command_buffer().expect("a command buffer");
fill(&late, &filled, 7);
late.commit().expect("a commit");
within(name, || late.status() != CommandBufferStatus::Committed);
busy.open();
completed(name, &late);
for command_buffer in &backlogs {
completed(name, command_buffer);
}
let submitted = order(&device, &late);
let ahead = submitted[from..].iter().position(|&late| late);
println!("{name}: {ahead:?} pieces submitted ahead of the idle queue's fill");
assert_eq!(
ahead,
Some(2),
"{name}: the pieces submitted ahead of the fill"
);
assert_eq!(
submitted.len() - from,
41,
"{name}: forty pieces and the fill"
);
for buffer in &buffers {
assert_eq!(word(buffer), stepped(8), "{name}");
}
assert_eq!(word(&filled), 7, "{name}");
});
}
}