rt: a command buffer's GPU times are never later than the completion seen, and keep their span
The completion thread placed a command buffer's two timestamps on the host clock by the last calibration and nothing bounded them: on the RX 550 a GPU end time fell after the host time its own completed handler read. A calibration pairs the GPU's tick with a host time that Mesa's drivers read after it (vk_common_GetCalibratedTimestampsKHR reads the domains in the order asked, the device's first, bracketed by the host clock), so every time placed by it runs late by the time between the two reads, and nothing held the end before the completion. Now the thread reads host_time() when the wait says the completion is reached, and placing moves the pair together: where the placed end would fall after that time, the calibration's host time moves earlier by the excess, for this command buffer and every later one placed by it, then both times are placed with it. So the end is never later than the completion seen, end - start is the timestamps' own span (Ticks::span, which the end is now placed by: start + span), and later command buffers keep their order against this one. The calibration's host time is late, and every completion bounds it from above, so the move takes it toward the moment of the tick and never past it. A device test (every_device_places_gpu_times_by_the_completion_seen_and_keeps_their_span) reads each calibration a second late through a test hook on Core (calibration_lateness, added in Calibration::read), lets the first completion calibrate again, and asserts for eight command buffers that start <= end <= the host time in its completed handler, and that end - start is the span of its two timestamps (recorded by the thread under the test cfg, Core::tick_spans). The gpu_start_time and gpu_end_time docs and the README's times paragraph say the times are placed through the calibrated timestamps and never later than the completion observed.
This commit is contained in:
1 parent
f8909e682d
commit
4197923467
6 files changed
+138
-22
No files matched your search
@@ -884,12 +884,19 @@ made, with no lock, and never rewritten for layouts.
|
||||
ends, read once it completes with no wait, and placed on the host clock by the device's last
|
||||
calibration: one `vkGetCalibratedTimestampsEXT` call reading the GPU's clock and the host's
|
||||
together, made as the device starts and again at a completion once 100 ms have passed since
|
||||
the last, never for each command buffer. A command buffer that took no submission writes no
|
||||
the last, never for each command buffer. Neither is ever later than the host time the
|
||||
completion thread observed the command buffer complete: where the calibration would place the
|
||||
end after it, the calibration's host time moves earlier by the excess, for that command buffer
|
||||
and every later one, so the two times move together, `gpu_end_time - gpu_start_time` stays the
|
||||
two timestamps' own span, and later command buffers keep their order. Mesa's drivers read the
|
||||
two clocks in turn, in the order asked, the GPU's first, so a calibration's host time trails
|
||||
its GPU tick by the time between the reads, and each completion bounds it from above. A command
|
||||
buffer that took no submission writes no
|
||||
query: its `gpu_start_time` and `gpu_end_time` are one time, never 0: the latest `gpu_end_time`
|
||||
of its queue's command buffers before it, or its `kernel_end_time` where that is later, the shape Metal
|
||||
reports for a command buffer that does no work. A command buffer's GPU times so fall between
|
||||
`host_time()` before its commit and after its completion, within the calibration's deviation
|
||||
and the clocks' drift over 100 ms. The two queries are a pair from the device's timestamp
|
||||
`host_time()` before its commit, within the calibration's deviation and the clocks' drift over
|
||||
100 ms, and `host_time()` in its completed handlers. The two queries are a pair from the device's timestamp
|
||||
pools, which a command buffer takes as it records its first commands and gives back as they are
|
||||
read at its completion, or as it is dropped uncommitted. A pool holds 4,096 queries, the pairs
|
||||
of 2,048 command buffers; the device makes one only once every pair of its pools is taken, and
|
||||
|
||||
@@ -745,19 +745,21 @@ impl CommandBuffer {
|
||||
/// seconds: the timestamp written where its first commands began, placed on the host clock by
|
||||
/// the device's last calibration of its clock against the host's
|
||||
/// (`vkGetCalibratedTimestampsEXT`, read as the device starts and again at a completion once
|
||||
/// 100 ms have passed since the last). 0 until it completes, and for one that ended with an
|
||||
/// error. One whose segments hold no commands, no event wait and no event signal writes no
|
||||
/// timestamp: its start and end are one time, the latest GPU end of its queue's command
|
||||
/// buffers before it, or its kernel end where that is later, as Metal reports for a command
|
||||
/// buffer that does no work.
|
||||
/// 100 ms have passed since the last), and never later than the completion the runtime
|
||||
/// observed. 0 until it completes, and for one that ended with an error. One whose segments
|
||||
/// hold no commands, no event wait and no event signal writes no timestamp: its start and end
|
||||
/// are one time, the latest GPU end of its queue's command buffers before it, or its kernel
|
||||
/// end where that is later, as Metal reports for a command buffer that does no work.
|
||||
pub fn gpu_start_time(&self) -> u64 {
|
||||
self.state().times.gpu_start
|
||||
}
|
||||
|
||||
/// `GPUEndTime`, in nanoseconds of [`host_time`](crate::host_time) where Metal gives seconds:
|
||||
/// the timestamp written where its last segment ended, placed on the host clock as
|
||||
/// [`gpu_start_time`](Self::gpu_start_time) is. 0 until it completes, and for one that ended
|
||||
/// with an error; its start time for one that writes no timestamp.
|
||||
/// the timestamp written where its last segment ended, placed on the host clock through the
|
||||
/// device's calibrated timestamps as [`gpu_start_time`](Self::gpu_start_time) is, and never
|
||||
/// later than the completion the runtime observed; their difference is the two timestamps'
|
||||
/// own span. 0 until it completes, and for one that ended with an error; its start time for
|
||||
/// one that writes no timestamp.
|
||||
pub fn gpu_end_time(&self) -> u64 {
|
||||
self.state().times.gpu_end
|
||||
}
|
||||
|
||||
@@ -372,6 +372,9 @@ pub(crate) fn run(core: Arc<Core>) {
|
||||
let fired = std::mem::take(&mut state.fire);
|
||||
let mut fire: Vec<(Listener, u64)> = Vec::new();
|
||||
let mut newly_lost = false;
|
||||
// The host time the thread saw this round's completions, which no GPU time of theirs
|
||||
// passes (`Calibration::place`).
|
||||
let mut seen_at = None;
|
||||
if state.lost {
|
||||
// The signals pending never come: the host waits for them end first.
|
||||
for event in state.watched.drain(..) {
|
||||
@@ -414,6 +417,7 @@ pub(crate) fn run(core: Arc<Core>) {
|
||||
};
|
||||
match seen {
|
||||
Ok(Seen::Reached(reached)) => {
|
||||
seen_at = Some(times::host_time());
|
||||
behind = None;
|
||||
// Before the submit thread's mark is read (`Work::completed`).
|
||||
done.completed.fetch_max(reached, Ordering::SeqCst);
|
||||
@@ -461,7 +465,9 @@ pub(crate) fn run(core: Arc<Core>) {
|
||||
}
|
||||
// Handlers and listeners run with no lock held.
|
||||
// Each completed command buffer's timestamps are placed on the host clock by the
|
||||
// last calibration, read again first where the period has passed since it.
|
||||
// last calibration, read again first where the period has passed since it, and moved
|
||||
// earlier where it would place an end after the host time the thread saw it complete.
|
||||
let seen = seen_at.unwrap_or_else(times::host_time);
|
||||
let stamps: Vec<Option<(u64, u64)>> =
|
||||
retire.iter().map(|retiree| retiree.stamps(&core)).collect();
|
||||
if stamps.iter().any(Option::is_some)
|
||||
@@ -472,9 +478,13 @@ pub(crate) fn run(core: Arc<Core>) {
|
||||
calibration = Calibration::read(&core).or(calibration);
|
||||
}
|
||||
for (retiree, stamps) in retire.into_iter().zip(stamps) {
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
if let Some((start, end)) = stamps {
|
||||
lock(&core.tick_spans).push(core.ticks.span(start, end));
|
||||
}
|
||||
let times = stamps
|
||||
.zip(calibration)
|
||||
.map(|((start, end), calibration)| calibration.place(&core.ticks, start, end));
|
||||
.zip(calibration.as_mut())
|
||||
.map(|(stamps, calibration)| calibration.place(&core.ticks, stamps, seen));
|
||||
retiree.retire(&core, times);
|
||||
}
|
||||
for (listener, value) in fire {
|
||||
|
||||
@@ -183,6 +183,14 @@ pub(crate) struct Core {
|
||||
/// of device memory, before the driver is called.
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
pub(crate) fail_private: std::sync::atomic::AtomicU32,
|
||||
/// For the device tests: nanoseconds added to the host time of each calibration read, as a
|
||||
/// calibration whose host clock is read that long after its GPU tick.
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
pub(crate) calibration_lateness: std::sync::atomic::AtomicU64,
|
||||
/// For the device tests: the span of each command buffer's two timestamps the completion
|
||||
/// thread placed, in the order it placed them ([`Ticks::span`](crate::times::Ticks::span)).
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
pub(crate) tick_spans: Mutex<Vec<u64>>,
|
||||
/// For the device tests: where set, the making of a staging or capture ring meets the test at
|
||||
/// the barrier twice before it allocates, once parked and once released.
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
@@ -536,6 +544,10 @@ impl Device {
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
fail_private: std::sync::atomic::AtomicU32::new(0),
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
calibration_lateness: std::sync::atomic::AtomicU64::new(0),
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
tick_spans: Mutex::default(),
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
park_region: Mutex::new(None),
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
hold_completions: std::sync::RwLock::new(()),
|
||||
|
||||
@@ -1006,6 +1006,64 @@ pub(crate) mod on_device {
|
||||
});
|
||||
}
|
||||
|
||||
/// A command buffer's GPU times are never later than the completion the runtime saw, by
|
||||
/// however much a calibration's host time trails its GPU tick, and stay apart by their
|
||||
/// timestamps' own span: here each calibration read a second late, whose placed times would
|
||||
/// fall after the completed handler ran.
|
||||
#[test]
|
||||
fn every_device_places_gpu_times_by_the_completion_seen_and_keeps_their_span() {
|
||||
each_device("command_times_late", |name, _, device| {
|
||||
let mut arena = Arena::new(&device);
|
||||
let buffer = arena.buffer();
|
||||
let queue = device.new_command_queue(4).expect("a queue");
|
||||
let core = device.core();
|
||||
core.calibration_lateness
|
||||
.store(1_000_000_000, Ordering::Relaxed);
|
||||
// The first completion calibrates again, a period after the device's own reading.
|
||||
thread::sleep(Duration::from_nanos(
|
||||
crate::times::CALIBRATION_PERIOD * 3 / 2,
|
||||
));
|
||||
lock(&core.tick_spans).clear();
|
||||
// Each command buffer's GPU start and end, and the host time its completed handler
|
||||
// read.
|
||||
type Ends = Arc<Mutex<Vec<(u64, u64, u64)>>>;
|
||||
let ends: Ends = Arc::default();
|
||||
let mut committed = Vec::new();
|
||||
let mut before = Vec::new();
|
||||
for index in 0..8 {
|
||||
let command_buffer = queue.command_buffer().expect("a command buffer");
|
||||
fill(&command_buffer, &buffer, index);
|
||||
let ends = Arc::clone(&ends);
|
||||
command_buffer
|
||||
.add_completed_handler(move |cb| {
|
||||
let at = host_time();
|
||||
lock(&ends).push((cb.gpu_start_time(), cb.gpu_end_time(), at))
|
||||
})
|
||||
.expect("a completed handler");
|
||||
before.push(host_time());
|
||||
command_buffer.commit().expect("a commit");
|
||||
committed.push(command_buffer);
|
||||
}
|
||||
for command_buffer in &committed {
|
||||
finished(name, command_buffer);
|
||||
}
|
||||
let ends = lock(&ends).clone();
|
||||
let spans = lock(&core.tick_spans).clone();
|
||||
assert_eq!(
|
||||
(ends.len(), spans.len()),
|
||||
(8, 8),
|
||||
"{name}: every one placed"
|
||||
);
|
||||
for (((start, end, handled), span), before) in ends.iter().zip(&spans).zip(&before) {
|
||||
assert!(
|
||||
before <= start && start <= end && end <= handled,
|
||||
"{name}: {before} {start} {end} {handled}"
|
||||
);
|
||||
assert_eq!(end - start, *span, "{name}: the span of {start} {end}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The GPU's clock is calibrated against the host's as the device starts, then at a
|
||||
/// completion once 100 ms have passed since the last calibration, never for each command
|
||||
/// buffer: none while the device idles, and for a burst of 32 command buffers one at its first
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
//! The GPU times are the two timestamps placed on the host clock by the device's last calibration
|
||||
//! (`VK_EXT_calibrated_timestamps`, one reading of the GPU's clock and the host's together): one
|
||||
//! as the device starts, then one at a completion once [`CALIBRATION_PERIOD`] has passed since
|
||||
//! the last. The kernel times are the host time at commit and when the submit call that passed the
|
||||
//! last segment returned.
|
||||
//! the last, the calibration moved earlier where it would place a command buffer's end after the
|
||||
//! host time the completion thread saw it complete. The kernel times are the host time at commit
|
||||
//! and when the submit call that passed the last segment returned.
|
||||
|
||||
use crate::counters::{Counters, Gauge, Metric};
|
||||
use crate::device::{lock, Core};
|
||||
@@ -132,6 +133,11 @@ impl Ticks {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whole nanoseconds from timestamp `start` to `end`, 0 where `end` is the earlier.
|
||||
pub(crate) fn span(&self, start: u64, end: u64) -> u64 {
|
||||
self.nanos(start, end) as u64
|
||||
}
|
||||
|
||||
/// Nanoseconds from timestamp `from` to `to`, negative where `to` is the earlier, within the
|
||||
/// valid bits: exact across the counter's wrap for two timestamps less than half its range
|
||||
/// apart.
|
||||
@@ -164,9 +170,15 @@ impl Calibration {
|
||||
let (values, _) = unsafe { core.calibrated.get_calibrated_timestamps(&infos) }.ok()?;
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
core.counters.calibrated();
|
||||
let host = host_nanos(values[1])?;
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
let host = host
|
||||
+ core
|
||||
.calibration_lateness
|
||||
.load(std::sync::atomic::Ordering::Relaxed);
|
||||
Some(Calibration {
|
||||
tick: values[0],
|
||||
host: host_nanos(values[1])?,
|
||||
host,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -175,11 +187,26 @@ impl Calibration {
|
||||
self.host
|
||||
}
|
||||
|
||||
/// The host times of a command buffer's two timestamps. A float-to-integer cast saturates at
|
||||
/// 0.
|
||||
pub(crate) fn place(&self, ticks: &Ticks, start: u64, end: u64) -> (u64, u64) {
|
||||
let at = |tick: u64| (self.host as f64 + ticks.nanos(self.tick, tick)) as u64;
|
||||
(at(start), at(end))
|
||||
/// The host times of a command buffer's two timestamps, apart by the ticks' own span
|
||||
/// ([`Ticks::span`]). Where its end would fall after `seen`, the host time the completion
|
||||
/// thread saw it complete, the calibration's host time moves earlier by that excess first,
|
||||
/// for this placement and every later one, so the two move together and later command
|
||||
/// buffers keep their order against this one. A calibration's host time is late where the
|
||||
/// driver reads it after the GPU's tick (Mesa's drivers read the domains in turn, in the
|
||||
/// order asked, the GPU's first), and each completion bounds it from above, so the move only
|
||||
/// takes it toward the moment of the tick. A float-to-integer cast saturates at 0.
|
||||
pub(crate) fn place(
|
||||
&mut self,
|
||||
ticks: &Ticks,
|
||||
(start, end): (u64, u64),
|
||||
seen: u64,
|
||||
) -> (u64, u64) {
|
||||
let span = ticks.span(start, end);
|
||||
let start = (self.host as f64 + ticks.nanos(self.tick, start)) as u64;
|
||||
let excess = (start + span).saturating_sub(seen);
|
||||
self.host = self.host.saturating_sub(excess);
|
||||
let start = start.saturating_sub(excess);
|
||||
(start, start + span)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user