diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index 9cbc9c6b4..456278a53 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -3734,7 +3734,9 @@ impl CommandBuffer { /// `kernelStartTime`: the host time, in seconds of [`host_time`](super::host_time)'s clock, /// at which the device's scheduler began submitting the command buffer's work to Vulkan; 0 - /// before. Like Metal's, the read waits for no submission in progress and no GPU work. + /// before, and until its [`kernel_end_time`](Self::kernel_end_time) is known: as Metal's, no + /// read shows a start without an end. Like Metal's, the read waits for no submission in + /// progress and no GPU work. pub fn kernel_start_time(&self) -> f64 { gpu_time::seconds(self.0.kernel.start()) } @@ -5583,7 +5585,7 @@ fn submission_error(device: &Device, error: vk::Result) -> CommandBufferError { /// timestamps are checked against it (`gpu_time.rs`, `GpuClock::span`). fn life(kernel: &KernelTimes, times: &Times) -> Life { Life { - kernel_start: kernel.start(), + kernel_start: kernel.began(), completed: times.completed, } } diff --git a/crates/metal2vulkan-runtime/src/command_buffer/recovery.rs b/crates/metal2vulkan-runtime/src/command_buffer/recovery.rs index e9ba323b7..03542395a 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/recovery.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/recovery.rs @@ -98,7 +98,7 @@ pub(super) fn injected(error: CommandBufferError) -> Option { /// The first recovery of `buffer`'s device since its first submission (the module docs). pub(super) fn recovered(buffer: &CommandBufferShared) -> Option { - let since = buffer.kernel.start(); + let since = buffer.kernel.began(); buffer.device.shared().recoveries.since(since) } diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs index af713240f..6f8e39a10 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs @@ -547,7 +547,7 @@ impl CommandBufferShared { let completed = shared.queues[queue].completed_value(); (shared.counts).batch_submitted_behind(scheduler.values[queue].saturating_sub(completed)); // One a hang's thread made `Scheduled` early began then (`hang.rs`). - if index == 0 && self.kernel.start() == 0 { + if index == 0 && self.kernel.began() == 0 { self.kernel.begin(); } let slot = state @@ -1231,7 +1231,7 @@ impl CommandBufferShared { ) -> Submission { let device = &self.device; let queue = self.queue.vk_queue(); - if index == 0 && self.kernel.start() == 0 { + if index == 0 && self.kernel.began() == 0 { self.kernel.begin(); } // What it holds until it completes, as a submitted batch takes it. diff --git a/crates/metal2vulkan-runtime/src/gpu_time.rs b/crates/metal2vulkan-runtime/src/gpu_time.rs index e3157b7ed..993d8671f 100644 --- a/crates/metal2vulkan-runtime/src/gpu_time.rs +++ b/crates/metal2vulkan-runtime/src/gpu_time.rs @@ -109,7 +109,9 @@ //! buffer's first batch and finished submitting its last. They are kept apart from the command //! buffer's state ([`KernelTimes`]), which the scheduler holds while it records the batch's //! own command buffers and submits it: a read waits for no submission in -//! progress and for no GPU work, as Metal's reads wait for none. +//! progress and for no GPU work, as Metal's reads wait for none. The start shows only once the +//! end is known, so no read sees a start without an end, as none of Metal's does (measured on +//! the M3 Max). //! - **No encoder, no timestamps**. A command buffer that encoded nothing and waits for no event or //! semaphore takes no slot and writes no timestamps. Its GPU start and end are both where its //! queue reached it: its kernel end time, or the GPU end of the queue's command buffer before it @@ -933,7 +935,8 @@ unsafe fn calibration( } /// A command buffer's kernel times, in nanoseconds of the host's clock, 0 until known: apart -/// from its state, so a read waits for no submission in progress (the module docs). +/// from its state, so a read waits for no submission in progress (the module docs). The start is +/// shown with the end alone ([`start`](Self::start)), as Metal shows them. #[derive(Debug, Default)] pub(crate) struct KernelTimes { /// The scheduler began submitting its first batch. @@ -948,13 +951,29 @@ impl KernelTimes { self.start.store(host_nanos(), Ordering::Release); } - /// The scheduler finished submitting the last batch at host time `at`. + /// The scheduler finished submitting the last batch at host time `at`, after it began + /// ([`begin`](Self::begin)): the start, stored before, is shown from now on. pub(crate) fn end_at(&self, at: u64) { self.end.store(at, Ordering::Release); } - /// When the scheduler began submitting the first batch; 0 before. + /// When the scheduler began submitting the first batch, as `kernelStartTime` shows it: 0 + /// until the end is known too, so that no read sees a start without an end, in either order + /// of the two reads (a start shown means a later read of the end sees it, and an end seen + /// means the start is shown). Metal's reads never see one (measured on the M3 Max under macOS + /// 27.0.1: in 7 to 8.5 million reads of each command buffer's pair while it was committed, + /// never a start without an end). The end's acquire load orders the start's after it: the + /// start was stored before the end ([`end_at`](Self::end_at)). pub(crate) fn start(&self) -> u64 { + match self.end.load(Ordering::Acquire) { + 0 => 0, + _ => self.began(), + } + } + + /// When the scheduler began submitting the first batch, whether or not it finished: the + /// scheduler's own reads; 0 before. + pub(crate) fn began(&self) -> u64 { self.start.load(Ordering::Acquire) } diff --git a/crates/metal2vulkan-runtime/src/tests/gpu_time.rs b/crates/metal2vulkan-runtime/src/tests/gpu_time.rs index a739f076f..ba61c5b0a 100644 --- a/crates/metal2vulkan-runtime/src/tests/gpu_time.rs +++ b/crates/metal2vulkan-runtime/src/tests/gpu_time.rs @@ -4,11 +4,12 @@ //! drift allow set aside for the completion; the pair, the //! reading of the least deviation; the host time domain a calibration reads; the extension's //! preferred form; and the device's timestamp pools, their slots held by the command buffers in -//! flight, made as those need them up to the most. +//! flight, made as those need them up to the most; and the kernel times, the start shown with the +//! end alone. use crate::gpu_time::{ - host_domain, host_time, least_deviation, Extension, GpuClock, Life, Pair, Placement, Pool, - Pools, Reading, DRIFT_PPM, PLAIN, PREFIXED, SLOTS, + host_domain, host_time, least_deviation, Extension, GpuClock, KernelTimes, Life, Pair, + Placement, Pool, Pools, Reading, DRIFT_PPM, PLAIN, PREFIXED, SLOTS, }; use ash::vk; use std::collections::BTreeSet; @@ -421,3 +422,27 @@ fn a_refused_pool_ends_the_making_and_scarce_pools_are_one() { assert_eq!(scarce.slots(), SLOTS as usize); drop(held); } + +/// A command buffer's kernel start shows only once its end is known (`kernelStartTime`), as no +/// read of Metal's sees a start without an end: while the scheduler submits, between the first +/// batch's start and the last batch's end, the start reads 0 to `kernel_start_time` and the +/// scheduler's own read (`began`) has it; once the end is stored both show. +#[test] +fn a_kernel_start_shows_with_its_end_alone() { + let kernel = KernelTimes::default(); + assert_eq!((kernel.start(), kernel.end(), kernel.began()), (0, 0, 0)); + kernel.begin(); + let began = kernel.began(); + assert_ne!(began, 0, "the scheduler's own read of the start"); + assert_eq!( + (kernel.start(), kernel.end()), + (0, 0), + "a start shown before its end" + ); + kernel.end_at(began + 7); + // Either order of the two reads sees both. + let (end, start) = (kernel.end(), kernel.start()); + assert_eq!((start, end), (began, began + 7)); + let (start, end) = (kernel.start(), kernel.end()); + assert_eq!((start, end), (began, began + 7)); +}