Show a command buffer's kernel start only once its end is known
The scheduler stored a command buffer's kernel start as it began submitting the first batch and the end as it finished the last, each in its own atomic, so `kernel_start_time` answered a start while `kernel_end_time` still answered 0 for as long as the submission took. Metal never shows that: a Swift probe on the M3 Max under macOS 27.0.1 read each command buffer's pair 7 to 8.5 million times while it was committed and always saw both or neither. `KernelTimes::start`, which `kernel_start_time` reads, now answers 0 until the end is stored: it loads the end first (acquire) and the start only where the end is set. The start is stored before the end on every path (the first batch's taking, a hang's early scheduling), so a start shown means a later read of the end sees it, and an end seen means the start shows, in either order of the two reads. The cost is one more atomic load per `kernel_start_time`. The scheduler's own reads of the start (whether to begin, a hang's recovery window, the GPU times' life check) take the stored value through the new `KernelTimes::began`. Test: `tests::gpu_time::a_kernel_start_shows_with_its_end_alone` checks that after `begin` and before `end_at` the start reads 0 while `began` has it, and that once the end is stored both reads, in either order, see both. The branch report carries its runs and its negative control (the start read raw, as before: the test fails). (cherry picked from commit 86701a2e00b0518e7eac3beb4740222edd3982bf)
This commit is contained in:
1 parent
c989f853d0
commit
8bc4fef06d
5 files changed
+58
-12
No files matched your search
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ pub(super) fn injected(error: CommandBufferError) -> Option<Recovery> {
|
||||
|
||||
/// The first recovery of `buffer`'s device since its first submission (the module docs).
|
||||
pub(super) fn recovered(buffer: &CommandBufferShared) -> Option<Recovery> {
|
||||
let since = buffer.kernel.start();
|
||||
let since = buffer.kernel.began();
|
||||
buffer.device.shared().recoveries.since(since)
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
Reference in new issue
Block a user