Checked-head GPU wait test: hold the signal on the GPU behind a gate the test opens, not behind a long fill

runtime_lane::event_round_trips::a_checked_head_waits_for_another_queue_s_signal_on_the_gpu failed
once in pin 32's lane at 39b45dd232 on the RTX 5080 (two lanes sharing the GPU; 1 of 32 runs, 0 of
10 beside one neighbour): Grouped, scheduler_gpu_waits rose by 0 over 3 rounds. The test assumed
the signalling command buffer's 64 MiB fill, about 70 us there, still ran when the scheduler's
thread considered the checked head; a loaded host's scheduler came after the signal landed, and
then rightly waited for nothing on the GPU.

The signalling command buffer now waits first for a plain event's signal the test reserved
(Event::reserve_vk_signal) and gives from the host (GpuGate, as submission_checks' InFlight does).
A plain event's wait for a reserved signal goes to its VkQueue at once (event.rs, interop.rs), so
the command buffer is submitted and its shared event signal is a submitted signal still in
flight: the checked head's landing wait names it, and the scheduler's thread waits for it on the
GPU (scheduler/mod.rs, gpu_waits; a shared event wait the host must meet is held on the host
instead, which is why the gate is a plain event's). The test waits until scheduler_gpu_waits
rose, checks that the head's check has not run and the event still reads 0, then opens the gate;
the check then reads 1. No fill length or timing is assumed: the signal cannot land before the
test opens the gate. On MoltenVK, which keeps the condition variable, the gate opens after 2 ms
and no GPU wait is counted, as before. The 64 MiB buffer is gone.

To be run on the RTX 5080 beside a lane part and on the 780M or XTX, and with the scheduler's GPU
wait removed (gpu_waits answering false), where it must fail.

(cherry picked from commit 69d6101aa9d44e9349e92e0721b66fef8d40b676)
This commit is contained in:
Anees Iqbal committed 2026-11-09 15:57:01 -08:00
1 parent ea02af47a4
commit 74474d2e23
1 file changed
+109 -28
@@ -19,21 +19,23 @@
//! calibrated timestamps. Beside them: the scheduler thread's sleeps, those it slept waiting on the
//! GPU itself, and its wakings by completions, per command buffer.
//!
//! Checked beside it: that thread waits for such a signal on the GPU itself, and the check still
//! runs only once the signal landed; and a host signal wakes it from that wait, whether of the
//! event it waits for there or of another head's (no head waits for the GPU's signal once the
//! host met its wait).
//! Checked beside it: that thread waits for such a signal on the GPU itself, the signal held on the
//! GPU behind a gate the test opens once the thread waits there, and the check still runs only
//! once the signal landed; and a host signal wakes it from that wait, whether of the event it
//! waits for there or of another head's (no head waits for the GPU's signal once the host met its
//! wait).
use super::{
layer_reported_nothing_at_all, loader, open_device, validated_device_with, Messages,
ScratchCache, WAIT,
};
use crate::gpu::TEST_DEVICE;
use ash::vk;
use metal2vulkan::device::CompletionPolicy;
use metal2vulkan::vk::DriverId;
use metal2vulkan_runtime::{
Buffer, CommandBuffer, CommandQueue, DeviceOptions, EventRef, ResourceOptions,
ShaderCacheOptions,
ShaderCacheOptions, VkEventSignal,
};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
@@ -209,14 +211,74 @@ fn checked_event_round_trips() {
}
}
/// A plain event's signal the test reserved (`Event::reserve_vk_signal`) and gives from the host
/// ([`open`](Self::open)), which a command buffer's wait for it holds on the GPU: such a wait goes
/// to its `VkQueue` at once, so the command buffer is submitted and its signals are in flight
/// until the test opens the gate, however fast the GPU and however loaded the host. Given on drop
/// where not yet, so no command buffer waits for good after a failed assertion.
struct GpuGate {
device: metal2vulkan_runtime::Device,
event: metal2vulkan_runtime::Event,
counter: u64,
opened: bool,
}
impl GpuGate {
/// A fresh event of `device`, its first signal reserved.
fn new(device: &metal2vulkan_runtime::Device) -> GpuGate {
let event = device.new_event().unwrap();
// SAFETY: the reservation's one signal is given once, from the host, by `open` (or the
// drop): a fresh event's first, so after nothing.
let reserved = unsafe { event.reserve_vk_signal(1) }.unwrap();
let VkEventSignal::Counter {
counter,
after: None,
} = reserved
else {
panic!("a fresh event's first signal reserved as {reserved:?}");
};
GpuGate {
device: device.clone(),
event,
counter,
opened: false,
}
}
/// Give the reserved signal from the host, once: the work waiting for it runs on.
fn open(&mut self) {
if self.opened {
return;
}
self.opened = true;
let info = vk::SemaphoreSignalInfo::default()
// SAFETY: the semaphore the event holds while it lives, signalled only as reserved.
.semaphore(unsafe { self.event.vk_semaphore() })
.value(self.counter);
// SAFETY: the reservation's one signal operation, from the host, of a semaphore of the
// live device; none of the device's objects is destroyed.
unsafe { self.device.raw().device.signal_semaphore(&info) }.unwrap();
}
}
impl Drop for GpuGate {
fn drop(&mut self) {
self.open();
}
}
/// Where the one head waits for an event value another queue's submitted command buffer signals,
/// the scheduler's thread waits for it on the GPU itself (`scheduler/mod.rs`; not on MoltenVK,
/// which keeps the condition variable), and the head's check, run as the signal landed, reads it.
/// The signalling command buffer waits at a [`GpuGate`] before its fill and its signal: it is
/// submitted, its signal in flight and held on the GPU, while the test waits for the scheduler's
/// thread to wait on the GPU (`WorkCounts::scheduler_gpu_waits`), the check not yet run and the
/// event still 0, and only then opens the gate. So the GPU wait is met however late the
/// scheduler's thread comes, where before a 64 MiB fill held the signal for some 70 us of an RTX
/// 5080's time, which a loaded host's scheduler could come after. On MoltenVK the test opens the
/// gate after 2 ms instead.
#[test]
fn a_checked_head_waits_for_another_queue_s_signal_on_the_gpu() {
/// The signalling command buffer's fill: long enough that its signal lands after the pass
/// that submitted it.
const LONG: u64 = 64 << 20;
let messages = Messages::default();
for completion in [CompletionPolicy::Exact, CompletionPolicy::Grouped] {
let cache = ScratchCache::new().unwrap();
@@ -229,22 +291,21 @@ fn a_checked_head_waits_for_another_queue_s_signal_on_the_gpu() {
device.new_command_queue().unwrap(),
device.new_command_queue().unwrap(),
];
let long = device.new_buffer(LONG, ResourceOptions::private()).unwrap();
let short = device.new_buffer(FILL, ResourceOptions::private()).unwrap();
let buffers = [
device.new_buffer(FILL, ResourceOptions::private()).unwrap(),
device.new_buffer(FILL, ResourceOptions::private()).unwrap(),
];
let before = device.work_counts();
for round in 0..3_u8 {
let (event, gate) = (
device.new_shared_event().unwrap(),
device.new_shared_event().unwrap(),
);
// Behind a gate the host raises once both are committed, so the scheduler holds the
// checked one, waiting for the signal, while the signalling one runs.
let what = format!("{completion:?} round {round}");
let event = device.new_shared_event().unwrap();
let mut gate = GpuGate::new(&device);
let signalling = queues[0].command_buffer().unwrap();
signalling
.encode_wait_for_event(EventRef::from(&gate), 1)
.encode_wait_for_event(EventRef::from(&gate.event), 1)
.unwrap();
let mut blit = signalling.blit_command_encoder().unwrap();
blit.fill_buffer(&long, 0..LONG, round).unwrap();
blit.fill_buffer(&buffers[0], 0..FILL, round).unwrap();
blit.end_encoding().unwrap();
signalling
.encode_signal_event(EventRef::from(&event), 1)
@@ -265,32 +326,52 @@ fn a_checked_head_waits_for_another_queue_s_signal_on_the_gpu() {
.encode_wait_for_event(EventRef::from(&event), 1)
.unwrap();
let mut blit = checked.blit_command_encoder().unwrap();
blit.fill_buffer(&short, 0..FILL, round).unwrap();
blit.fill_buffer(&buffers[1], 0..FILL, round).unwrap();
blit.end_encoding().unwrap();
let waits = device.work_counts().scheduler_gpu_waits;
signalling.commit().unwrap();
checked.commit().unwrap();
gate.set_signaled_value(1);
// The scheduler's thread holds the checked one, waiting on the GPU for the signal the
// gate holds.
if moltenvk {
std::thread::sleep(Duration::from_millis(2));
} else {
let start = Instant::now();
while device.work_counts().scheduler_gpu_waits == waits {
assert!(
start.elapsed() < WAIT,
"{what}: the scheduler never waited for the signal on the GPU"
);
std::thread::sleep(Duration::from_micros(100));
}
}
assert_eq!(
*seen.lock().unwrap(),
None,
"{what}: checked before the gate"
);
assert_eq!(
event.signaled_value(),
0,
"{what}: signalled before the gate"
);
gate.open();
for command_buffer in [&signalling, &checked] {
command_buffer.wait_until_completed_for(WAIT).unwrap();
assert_eq!(command_buffer.error(), None, "{completion:?} round {round}");
assert_eq!(command_buffer.error(), None, "{what}");
}
assert_eq!(
*seen.lock().unwrap(),
Some(1),
"{completion:?} round {round}: the check ran before the signal landed"
"{what}: the check ran before the signal landed"
);
}
let waits = device.work_counts().scheduler_gpu_waits - before.scheduler_gpu_waits;
if moltenvk {
let waits = device.work_counts().scheduler_gpu_waits - before.scheduler_gpu_waits;
assert_eq!(
waits, 0,
"{completion:?}: MoltenVK keeps the condition variable"
);
} else {
assert!(
waits > 0,
"{completion:?}: the scheduler never waited for the signal on the GPU"
);
}
}
layer_reported_nothing_at_all(&messages);