diff --git a/crates/metal2vulkan-runtime/src/gpu_pools.rs b/crates/metal2vulkan-runtime/src/gpu_pools.rs index 24b44ada6..48109d199 100644 --- a/crates/metal2vulkan-runtime/src/gpu_pools.rs +++ b/crates/metal2vulkan-runtime/src/gpu_pools.rs @@ -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 diff --git a/crates/metal2vulkan-validation/src/runtime_lane/index_pools.rs b/crates/metal2vulkan-validation/src/runtime_lane/index_pools.rs index 4cb8027dd..8c4169687 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/index_pools.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/index_pools.rs @@ -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>> = 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>> = 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); } diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 6ec80fdac..2c8e5cdb2 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -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