Runtime tests: the copy-in span test asserts the ordering its doc states, on the GPU's counter

every_device_leaves_a_copy_in_out_of_the_gpu_span_it_goes_ahead_of says "a start written
before the copy-in ends puts the upload inside it". It checked that with a bar on span
lengths, the command buffer's span times 4 under its copy-in's, and that bar measured the
card's load.

In the forced two-copy view it was red in three of three Intel suite runs:
- the Iris Xe in round 1b908464ea-1126-1752 (m3-max-75798): copy-in 12,017 us, span
  34,975 us;
- the UHD 630 on Windows in the same round (m3-max-75776): copy-in 29,668 us, span
  142,758 us;
- the Iris Xe again, from a stamp-printing probe build of the whole suite (m3-max-75836).

Two printed runs on the Iris show the start after the copy-in's end both times:
- alone (m3-max-75815), it passed: the command buffer started 53 ticks (2.8 us) after
  its copy-in's end, and its span was 3 us against an 8.2 ms copy-in;
- inside the whole suite (m3-max-75836), it failed: the starts came 53 and 58 ticks
  (2.8 and 3.0 us) after the copy-ins' ends, while the spans stretched to 37 and 50 ms.
The 52.083 ns ticks come from the probe branch steelbrain/irisspan-probe, which never lands.

This is neither an adjustment nor a skip. The cause is shown in those two runs, and the
test now asserts the claim its own doc states:
- The device keeps one test-only record of each pair of timestamps the completion thread
  reads, a Metal command buffer's or the runtime's own, in retirement order, in raw ticks
  (Core::stamped, times::Stamped). It replaces tick_spans, which held only Metal command
  buffers' durations. The calibration test reads its durations from the same record.
- The test clears the record before did_modify_range. It then asserts that the record
  holds the copy-in's pair and then the command buffer's, both read, and that the command
  buffer's start tick is at or after the copy-in's end tick (starts_after, on Ticks::nanos,
  across the counter's wrap). The span-length bar is gone.
- "Both read" replaces "the copy-in's span > 0", which contradicted a gap the runtime
  README's known gaps already state: on Mesa's Apple-GPU driver, an upload or a copy-in's
  span is 0. Equal stamps pass the ordering.
- Ticks::nanos becomes crate-visible for the comparison. Product behaviour is unchanged:
  every new item is under cfg(test).

The negative control is a_start_before_the_copy_in_s_end_fails_the_ordering. It is a unit
check of the comparison, since no device writes the start early: a start 1 tick before the
end fails, and a start after it, at it, or past the counter's wrap holds.
This commit is contained in:
Anees Iqbal committed 2026-11-26 18:33:08 -08:00
1 parent cc89c80bdc
commit 0ae2000a78
5 files changed
+89 -21

No files matched your search

+10 -1
View File
@@ -755,7 +755,8 @@ pub(crate) fn run(core: Arc<Core>) {
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 own = false;
lock(&core.stamped).push(times::Stamped { own, start, end });
}
if let Some((start, end)) = stamps {
// Retirees come in submission order: the first of a held group's command
@@ -793,6 +794,14 @@ pub(crate) fn run(core: Arc<Core>) {
// Its GPU span joins those the later ones' spans lie under: a command buffer's as
// placed, an upload's or a copy-in's by the calibration as it stands.
let own = retiree.retire(&core, completion);
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
if let Some((start, end)) = own {
lock(&core.stamped).push(times::Stamped {
own: true,
start,
end,
});
}
let own = own
.zip(calibration.as_ref())
.map(|((start, end), calibration)| {
+5 -4
View File
@@ -315,10 +315,11 @@ pub(crate) struct Core {
/// 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)).
/// For the device tests: each pair of timestamps the completion thread read, a Metal command
/// buffer's or the runtime's own (an upload's or a copy-in's), in the order it retired them,
/// in raw ticks ([`Stamped`](crate::times::Stamped)).
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) tick_spans: Mutex<Vec<u64>>,
pub(crate) stamped: Mutex<Vec<crate::times::Stamped>>,
/// For the device tests: where set, the making of the staging region meets the test at
/// the barrier twice before it allocates, once parked and once released.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
@@ -876,7 +877,7 @@ impl Device {
#[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(),
stamped: 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)))]
@@ -1667,7 +1667,7 @@ pub(crate) mod on_device {
thread::sleep(Duration::from_nanos(
crate::times::CALIBRATION_PERIOD * 3 / 2,
));
lock(&core.tick_spans).clear();
lock(&core.stamped).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)>>>;
@@ -1692,7 +1692,10 @@ pub(crate) mod on_device {
finished(name, command_buffer);
}
let ends = lock(&ends).clone();
let spans = lock(&core.tick_spans).clone();
let spans: Vec<u64> = (lock(&core.stamped).iter())
.filter(|pair| !pair.own)
.map(|pair| core.ticks.span(pair.start, pair.end))
.collect();
assert_eq!(
(ends.len(), spans.len()),
(8, 8),
@@ -3,8 +3,40 @@
//! `gpu_start_time..gpu_end_time`, as a Mac reports it (an upload sits between a command buffer's
//! scheduling and its GPU start there).
use crate::times::{Stamped, Ticks};
/// Whether `command_buffer`'s pair starts at or after `copy_in`'s ends, on the GPU's counter and
/// across its wrap ([`Ticks::nanos`]): equal stamps count as after.
pub(crate) fn starts_after(ticks: &Ticks, copy_in: Stamped, command_buffer: Stamped) -> bool {
ticks.nanos(copy_in.end, command_buffer.start) >= 0.0
}
/// [`starts_after`] holds for a start after the copy-in's end, at it, and past the counter's wrap,
/// and fails for a start before it, the case the device test exists to catch.
#[test]
fn a_start_before_the_copy_in_s_end_fails_the_ordering() {
let ticks = Ticks::new(52.083_332, 36);
let top = (1u64 << 36) - 1;
let pair = |own, start, end| Stamped { own, start, end };
let copy_in = pair(true, 900, 1000);
assert!(starts_after(&ticks, copy_in, pair(false, 1053, 1100)));
assert!(starts_after(&ticks, copy_in, pair(false, 1000, 1100)));
assert!(!starts_after(&ticks, copy_in, pair(false, 999, 1100)));
assert!(starts_after(
&ticks,
pair(true, top - 20, top - 10),
pair(false, 5, 9)
));
assert!(!starts_after(
&ticks,
pair(true, 0, 5),
pair(false, top - 10, top - 5)
));
}
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
pub(crate) mod on_device {
use crate::device::lock;
use crate::tests::command::on_device::{
each_device, each_device_in_both_forms, finished, Arena,
};
@@ -90,9 +122,13 @@ pub(crate) mod on_device {
/// A Managed buffer's copy-in is outside the GPU span of the command buffer it goes ahead of:
/// a command buffer of one small dispatch over a Managed buffer whose [`NAMED`] bytes were
/// named before its commit spans under a quarter of the copy-in's own span
/// (`Metrics::runtime_command_buffer_gpu_nanos`). A start written before the copy-in ends
/// puts the upload inside it. Only where the Managed buffer has a device copy.
/// named before its commit starts, on the GPU's own counter, at or after its copy-in ends
/// ([`starts_after`](super::starts_after), over the pairs the completion thread read,
/// `Core::stamped`). A start written before the copy-in ends puts the upload inside it. The
/// spans' lengths are not compared: beside other work on the GPU a short command buffer's span
/// stretches past its copy-in's while its start stays after the copy-in's end. Equal stamps
/// pass, as a driver that samples at encoder boundaries gives them (the runtime README's known
/// gaps, "Mesa's Apple-GPU driver"). Only where the Managed buffer has a device copy.
#[test]
fn every_device_leaves_a_copy_in_out_of_the_gpu_span_it_goes_ahead_of() {
each_device_in_both_forms("copy-in outside the span", |name, _, device| {
@@ -100,7 +136,7 @@ pub(crate) mod on_device {
println!("copy-in outside the span: {name}: unified, no copy-in");
return;
}
let counters = device.counters();
let core = device.core();
let cpu = (device.take_block(MemoryClass::Shared, NAMED))
.unwrap_or_else(|error| panic!("{name}: {error}"));
let managed = (new_managed(&device, cpu.id, 0, NAMED, 0))
@@ -111,23 +147,30 @@ pub(crate) mod on_device {
let warm = long(&queue, &state, &managed, 1, 64);
warm.commit().expect("a commit");
finished(name, &warm);
let before = counters.metrics().runtime_command_buffer_gpu_nanos;
lock(&core.stamped).clear();
(managed.did_modify_range(0, NAMED)).unwrap_or_else(|error| panic!("{name}: {error}"));
let short = long(&queue, &state, &managed, 1, 64);
short.commit().expect("a commit");
finished(name, &short);
assert_eq!(short.error(), None, "{name}");
let copy = counters.metrics().runtime_command_buffer_gpu_nanos - before;
let span = short.gpu_end_time().saturating_sub(short.gpu_start_time());
let pairs = lock(&core.stamped).clone();
assert_eq!(
pairs.iter().map(|pair| pair.own).collect::<Vec<_>>(),
[true, false],
"{name}: the copy-in's pair, then the command buffer's, both read: {pairs:?}"
);
let (copy_in, command_buffer) = (pairs[0], pairs[1]);
let after = core.ticks.nanos(copy_in.end, command_buffer.start);
println!(
"copy-in outside the span: {name}: copy-in {} us, command buffer {} us",
copy / 1000,
span / 1000
"copy-in outside the span: {name}: copy-in {} us, command buffer {} us, its start \
{after} ns after the copy-in's end ({pairs:?})",
core.ticks.span(copy_in.start, copy_in.end) / 1000,
core.ticks.span(command_buffer.start, command_buffer.end) / 1000,
);
assert!(copy > 0, "{name}: the copy-in's span");
assert!(
span.saturating_mul(4) < copy,
"{name}: the command buffer's span {span} ns holds its copy-in's {copy} ns"
super::starts_after(&core.ticks, copy_in, command_buffer),
"{name}: the command buffer starts {} ns before its copy-in ends",
-after
);
drop((warm, short, managed, queue));
(device.release_block(cpu)).unwrap_or_else(|error| panic!("{name}: {error}"));
+13 -1
View File
@@ -146,7 +146,7 @@ impl Ticks {
/// 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.
fn nanos(&self, from: u64, to: u64) -> f64 {
pub(crate) fn nanos(&self, from: u64, to: u64) -> f64 {
let ahead = to.wrapping_sub(from) & self.mask;
if ahead <= self.mask / 2 {
ahead as f64 * self.period
@@ -156,6 +156,18 @@ impl Ticks {
}
}
/// For the tests: one pair of timestamps the completion thread read as it retired their command
/// buffer, in the GPU's raw ticks ([`Ticks`]).
#[cfg(test)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Stamped {
/// Whether the pair is the runtime's own command buffer's (an upload or a Managed buffer's
/// copy-in) rather than a Metal command buffer's.
pub(crate) own: bool,
pub(crate) start: u64,
pub(crate) end: u64,
}
/// One reading of the GPU's clock and the host's together (`vkGetCalibratedTimestampsEXT`): the
/// device's tick and the host nanoseconds of the same moment.
#[derive(Clone, Copy)]