diff --git a/crates/metal2vulkan-runtime/src/complete.rs b/crates/metal2vulkan-runtime/src/complete.rs index 6e13f3e80..22392edc8 100644 --- a/crates/metal2vulkan-runtime/src/complete.rs +++ b/crates/metal2vulkan-runtime/src/complete.rs @@ -755,7 +755,8 @@ pub(crate) fn run(core: Arc) { 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) { // 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)| { diff --git a/crates/metal2vulkan-runtime/src/device.rs b/crates/metal2vulkan-runtime/src/device.rs index f69141f7b..8edf1f272 100644 --- a/crates/metal2vulkan-runtime/src/device.rs +++ b/crates/metal2vulkan-runtime/src/device.rs @@ -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>, + pub(crate) stamped: Mutex>, /// 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)))] diff --git a/crates/metal2vulkan-runtime/src/tests/command.rs b/crates/metal2vulkan-runtime/src/tests/command.rs index 15ed7a064..806c957ad 100644 --- a/crates/metal2vulkan-runtime/src/tests/command.rs +++ b/crates/metal2vulkan-runtime/src/tests/command.rs @@ -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>>; @@ -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 = (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), diff --git a/crates/metal2vulkan-runtime/src/tests/gpu_span.rs b/crates/metal2vulkan-runtime/src/tests/gpu_span.rs index e2ee50b9d..10802f078 100644 --- a/crates/metal2vulkan-runtime/src/tests/gpu_span.rs +++ b/crates/metal2vulkan-runtime/src/tests/gpu_span.rs @@ -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::>(), + [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}")); diff --git a/crates/metal2vulkan-runtime/src/times.rs b/crates/metal2vulkan-runtime/src/times.rs index 2d703c9cd..76b03c722 100644 --- a/crates/metal2vulkan-runtime/src/times.rs +++ b/crates/metal2vulkan-runtime/src/times.rs @@ -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)]