index_pools: count a completed handler's lag from its submission's return too

runtime_lane::index_pools::a_completed_handler_waits_for_no_pool_block_made_meanwhile
failed on the RTX 5080 (round 5ce05e1908, lane ln07): the handler ran 37.2 ms
after its command buffer completed, during a 50.5 ms making of the other
queue's index pool block.

What it waited on is the driver, not the completion path. A probe on the
5080 (NVIDIA 615.71.09; 40 rounds of the lane's draw, with and without the
validation layer, completion trace, b5080 entry poolwait-probe1) found:
- the making is nearly all vkAllocateMemory of the 254.7 MiB block, 3.1 to
  35.7 ms beside another process's GPU work;
- in 25 rounds the scheduler thread's vkQueueSubmit of the first command
  buffer was called during that allocation and returned 0.01 to 0.25 ms after
  it, twice after the GPU had ended the batch (23.9 ms into a 29.4 ms making;
  0.3 ms into a 3.7 ms one: the failure's shape). The runtime watches a batch
  from its submission's return on, so it completed as the making ended;
- in the other 15 the submission returned before the allocation began, and
  the handler ran during the allocation in all 15;
- in all 40, the handler ran 0.07 to 1.44 ms after the later of the GPU end
  and the submission's return;
- raw, another thread's vkWaitSemaphores (no wait), vkGetSemaphoreCounterValue
  and vkGetFenceStatus took at most 0.03 ms during allocations of that size
  (1.9 to 213.6 ms): host waits are not held, submissions are.

So the claim is now exact: the lag counts from the later of the GPU end and
the kernel end time (the last vkQueueSubmit's return), and a round tells only
where both fall in the making's first half. One round told about one time in
four on the 5080, so the test runs 12, each with a queue of its own, and
prints each. The driver's hold of the submission is a known gap
(docs/FAST-PATHS.md), and gpu_pools.rs says what a making costs there.

(cherry picked from commit 438b4ec928dc1d44d2003a20537fea77a768d731)
This commit is contained in:
Anees Iqbal committed 2026-11-08 08:23:36 -08:00
1 parent 9ac1abfcd1
commit 9024d9ee5f
3 files changed
+91 -43

No files matched your search

+5 -1
View File
@@ -40,7 +40,11 @@
//! took 24 to 29 ms to make on the M3 Max (MoltenVK 1.4.1, macOS 27.0.1), 35 to 39 ms while the
//! GPU ran, all of it MoltenVK's commit of the block to its device's residency set inside
//! `vkBindBufferMemory`, under the lock its `vkDestroyBuffer` and `vkFreeMemory` take too, and
//! wired its 1 GiB of host memory whatever the GPU wrote of it.
//! wired its 1 GiB of host memory whatever the GPU wrote of it. A block of 254.7 MiB took 3.1 to
//! 35.7 ms on the RTX 5080 Laptop GPU (NVIDIA 615.71.09, Linux) beside another process's GPU
//! work, nearly all of it in `vkAllocateMemory`, and a `vkQueueSubmit` another thread called
//! meanwhile returned only once that did, so another queue's command buffer submitted during a
//! making completes as the making ends (`docs/FAST-PATHS.md`, known gaps).
//! - **Raw.** A block is a `VkBuffer` bound to private memory, outside the Metal address space,
//! not a `Buffer`, which would hold the device from the device's own blocks (as the mipmap
//! scratch's, `mipmap_scratch.rs`). The device releases the blocks left as it is destroyed. A
@@ -18,6 +18,17 @@
//! and pools go after its handlers. Before, its uploads went first, and on MoltenVK their
//! `vkDestroyBuffer` waited for the lock that making holds, so the handler ran as the making
//! ended, 20 to 27 ms after the command buffer completed.
//! - The handler's time counts from the later of its command buffer's GPU end and its last
//! submission's return (`kernel_end_time`): the runtime watches a batch for its completion from
//! its `vkQueueSubmit`'s return on, and a driver may hold that return for the making. On the
//! RTX 5080 Laptop GPU (NVIDIA 615.71.09, Linux) a `vkQueueSubmit` the scheduler's thread
//! called while the making's `vkAllocateMemory` ran returned 0.01 to 0.25 ms after that did, in
//! 25 of 40 rounds of a probe (allocations of a 254.7 MiB block, 3.1 to 35.7 ms, beside another
//! process's GPU work), twice after the GPU had ended the batch (23.9 ms into a 29.4 ms making,
//! and 0.3 ms into a 3.7 ms one); another thread's `vkWaitSemaphores`, semaphore reads and fence
//! reads took at most 0.03 ms during raw allocations of that size (1.9 to 213.6 ms). In the other 15 rounds the submission
//! returned before the allocation began and the handler ran during it; in all 40 the handler
//! ran 0.07 to 1.44 ms after the later of the two.
use super::{
buffer_with, layer_reported_nothing_at_all, validated_device_with, Messages, ScratchCache, WAIT,
@@ -42,6 +53,11 @@ const ROUNDS: usize = 6;
/// ends (`gpu_pools::QUIET`).
const QUIET: Duration = Duration::from_secs(2);
/// The rounds of a command buffer completing while another queue makes its block. A round tells
/// only where its submission returned before the making's allocation took the driver (the module
/// docs): about one round in four on the RTX 5080 under the validation layer.
const MAKING_ROUNDS: usize = 12;
/// The shortest making of a block that tells a handler that waited for it from one that did not
/// (on the M3 Max a block took 24 to 39 ms; a handler that waits for nothing runs within a
/// millisecond of its command buffer's completion).
@@ -229,9 +245,11 @@ fn a_pool_block_no_command_buffer_held_goes_after_the_quiet_period() {
}
/// A command buffer's completed handler runs while another queue's first command buffer makes
/// that queue's block on this thread: where the making took long enough to tell and the command
/// buffer completed in its first half, the handler ran less than half the making after the
/// completion (the module docs).
/// that queue's block on this thread, round after round, each with a queue of its own: where the
/// making took long enough to tell and the command buffer both completed and returned from its
/// submission in its first half, the handler ran less than half the making after the later of the
/// two (the module docs). A submission the driver holds to the making's end tells nothing of the
/// handler.
#[test]
fn a_completed_handler_waits_for_no_pool_block_made_meanwhile() {
let cache = ScratchCache::new().unwrap();
@@ -239,49 +257,63 @@ fn a_completed_handler_waits_for_no_pool_block_made_meanwhile() {
{
let lane = Lane::new(&messages, &cache);
let queue = lane.queue();
let other = lane.queue();
// The pipeline's first draw, which makes the first queue's block.
lane.drawn(&queue);
let first = lane.encoded(&queue);
let ran: Arc<Mutex<Option<f64>>> = Arc::default();
let log = Arc::clone(&ran);
first
.add_completed_handler(Box::new(move |_| {
*log.lock().unwrap() = Some(host_time());
}))
.unwrap();
let (made, _) = lane.blocks();
first.commit().unwrap();
let start = host_time();
let second = lane.encoded(&other);
let encoded = host_time();
assert_eq!(
lane.blocks().0 - made,
1,
"the second queue's command buffer made a block"
);
second.commit().unwrap();
completed(&first);
completed(&second);
let handled = ran.lock().unwrap().expect("the completed handler ran");
let seen = first.gpu_end_time().unwrap();
let making = encoded - start;
let lag = handled - seen;
eprintln!(
"making {:.3} ms, completion {:+.3} ms into it, handler {:.3} ms after the completion",
making * 1e3,
(seen - start) * 1e3,
lag * 1e3
);
if making >= MAKING_TELLS && seen < start + making / 2.0 {
assert!(
lag < making / 2.0,
"the completed handler ran {:.3} ms after its command buffer completed, during a \
making of {:.3} ms",
lag * 1e3,
making * 1e3
let mut told = 0;
for round in 0..MAKING_ROUNDS {
let other = lane.queue();
let first = lane.encoded(&queue);
let ran: Arc<Mutex<Option<f64>>> = Arc::default();
let log = Arc::clone(&ran);
first
.add_completed_handler(Box::new(move |_| {
*log.lock().unwrap() = Some(host_time());
}))
.unwrap();
let (made, _) = lane.blocks();
first.commit().unwrap();
let start = host_time();
let second = lane.encoded(&other);
let encoded = host_time();
assert_eq!(
lane.blocks().0 - made,
1,
"round {round}: the second queue's command buffer made a block"
);
second.commit().unwrap();
completed(&first);
completed(&second);
let handled = ran.lock().unwrap().expect("the completed handler ran");
let seen = first.gpu_end_time().unwrap();
// Its last batch's `vkQueueSubmit` returned: from then on its queue's waiter watches
// for its completion.
let submitted = first.kernel_end_time();
let reached = seen.max(submitted);
let making = encoded - start;
let lag = handled - reached;
let tells = making >= MAKING_TELLS && reached < start + making / 2.0;
told += usize::from(tells);
eprintln!(
"round {round}: making {:.3} ms, completion {:+.3} ms and submission's return \
{:+.3} ms into it, handler {:.3} ms after both{}",
making * 1e3,
(seen - start) * 1e3,
(submitted - start) * 1e3,
lag * 1e3,
if tells { "" } else { " (tells nothing)" }
);
if tells {
assert!(
lag < making / 2.0,
"round {round}: the completed handler ran {:.3} ms after its command buffer \
completed and its submission returned, during a making of {:.3} ms",
lag * 1e3,
making * 1e3
);
}
drop((first, second, other));
}
eprintln!("{told} of {MAKING_ROUNDS} rounds told");
}
layer_reported_nothing_at_all(&messages);
}
+12
View File
@@ -893,6 +893,18 @@ driver's. Each is open work.
GPU, once per cold cache; states that share the module then take 13 to 16 ms, and a warm cache
2 ms. The module inlines a few sampling helpers five or six times each; keeping one copy of each
would cut it. It is not built.
- **On NVIDIA, a command buffer submitted while the runtime makes a queue's GPU pool block
completes as the making ends.** A queue's first lease of an index or mesh records pool makes its
block as the command buffer encodes (`gpu_pools.rs`), a sixty-fourth of the device-local heap.
On the RTX 5080 Laptop GPU (NVIDIA 615.71.09, Linux) a block of 254.7 MiB took 3.1 to 35.7 ms
beside another process's GPU work, nearly all of it in `vkAllocateMemory`, and a
`vkQueueSubmit` the scheduler's thread called meanwhile returned only 0.01 to 0.25 ms after
that did (25 of 40 rounds of a probe), twice after the GPU had already ended the batch. The
runtime watches a batch for its completion from its submission's return on, so another queue's
command buffer submitted then completes, and runs its completed handlers, as the making ends,
and the submissions behind it on the scheduler's thread wait as long; one whose submission
returned before the allocation began completed during it (15 of 15). Metal's command buffers
wait for no such making. Not announced.
- **Making and releasing an event on NVIDIA take 0.1 to 0.6 ms of the calling thread.**
`new_event` and `new_shared_event` each make one timeline semaphore (`vkCreateSemaphore`,
`event.rs`), and an event's release destroys it where it goes. On the RTX 5080 Laptop GPU both