From 1149c040810f882dc58db3ac14e158b61e846bbf Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 08:12:43 -0800 Subject: [PATCH] rt test: a quiesce writes a grown pipeline cache blob with no drop, after a creation it waits for (red) The blob is written only at the reaper's looks and at the device's drop (Caches::close). A consumer whose process ends with _exit after Device::quiesce, as the README's Quiesce and exit says it does, keeps none of the pipelines made since the last look's write: quiesce stops the reaper, which ends with no blob write. The tests: a cache grown with its look skipped under a creation held open; a quiesce on another thread, the creation ending once the reaper's stop is asked; then the blob holds the cache, the drop writes nothing more, and a device seeded from it quiesces with no growth and writes nothing. And with the creation held past a 300 ms quiesce's deadline, the last write is skipped and counted in Metrics::pipeline_cache_writes_skipped, and the drop writes the grown cache. Expected red today: no blob after the quiesce; no skip counted. --- crates/metal2vulkan-runtime/src/reaper.rs | 6 + .../src/tests/translate.rs | 156 ++++++++++++++++++ 2 files changed, 162 insertions(+) diff --git a/crates/metal2vulkan-runtime/src/reaper.rs b/crates/metal2vulkan-runtime/src/reaper.rs index 56c41832d..ea73d85bd 100644 --- a/crates/metal2vulkan-runtime/src/reaper.rs +++ b/crates/metal2vulkan-runtime/src/reaper.rs @@ -138,6 +138,12 @@ impl Reaper { self.changed.notify_one(); } + /// For the device tests: whether the reaper was told to end. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) fn stopping(&self) -> bool { + lock(&self.state).stop + } + /// Tells the reaper to end once its queue is empty. pub(crate) fn stop(&self) { lock(&self.state).stop = true; diff --git a/crates/metal2vulkan-runtime/src/tests/translate.rs b/crates/metal2vulkan-runtime/src/tests/translate.rs index 3d8b8b20d..ccd25ee5d 100644 --- a/crates/metal2vulkan-runtime/src/tests/translate.rs +++ b/crates/metal2vulkan-runtime/src/tests/translate.rs @@ -3032,6 +3032,162 @@ attributes #1 = { convergent nounwind willreturn } } } + /// The cache grown and no look at it written: the reaper's look waits until a creation is + /// held open, and is then skipped. Returns the kernel's state, and the creation held open. + fn grown_unwritten<'a>( + name: &str, + device: &Device, + core: &'a crate::device::Core, + kernel: &ComputePipelineDescriptor, + ) -> (ComputePipelineState, crate::cache::Creation<'a>) { + let (caches, counters) = (&core.caches, device.counters()); + caches.look_hold.hold(); + let made = state(name, device, kernel); + within("the look", || caches.look_hold.waiting()); + let open = crate::cache::creation(core); + let skipped = counters.metrics().pipeline_cache_looks_skipped; + caches.look_hold.release(); + within("the look skipped", || { + counters.metrics().pipeline_cache_looks_skipped > skipped + }); + (made, open) + } + + /// The blob's last modification time, which a write, a file renamed over it, changes. + fn blob_time(blob: &Path) -> Option { + std::fs::metadata(blob) + .and_then(|meta| meta.modified()) + .ok() + } + + /// A quiesce writes the blob where the cache grew since the last write, with no drop, once a + /// creation still running ends: the reaper's look at the grown cache is skipped while a + /// creation is held open, so no look wrote it; the quiesce, on another thread, stops the + /// reaper, whose last write waits for that creation, which ends once the stop is asked. Once + /// the quiesce returns, the blob holds the cache. The device's drop after it writes nothing + /// more; a device after it, seeded from that blob, makes the same kernel, and its quiesce, + /// with no growth, writes nothing. + #[test] + fn every_device_writes_its_grown_blob_at_a_quiesce_with_no_drop() { + let Some(kernel) = descriptor("a1_bool_from_float") else { + return; + }; + let instance = crate::tests::validation::instance(&[]); + let devices = devices_of_the_cfgs_kind(&instance); + assert!(!devices.is_empty(), "no device of this cfg's kind"); + for (name, physical) in &devices { + let dir = + std::env::temp_dir().join(format!("m2v-rt-quiesce-blob-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let device = on_directory(name, physical, &dir); + if !keeps_a_blob(name, &device) { + drop(device); + let _ = std::fs::remove_dir_all(&dir); + continue; + } + let core = std::sync::Arc::clone(device.core()); + let counters = device.counters(); + let blob = core.caches.blob().expect("a cache directory").to_path_buf(); + let (made, open) = grown_unwritten(name, &device, &core, &kernel); + assert!(!blob.exists(), "{name}: no blob before the quiesce"); + let quiesced = std::thread::scope(|scope| { + let quiesce = scope.spawn(|| device.quiesce(std::time::Duration::from_secs(8))); + within("the reaper's stop", || core.reaper.stopping()); + drop(open); + quiesce.join().expect("the quiesce") + }); + assert!(quiesced, "{name}: the quiesce answered true"); + let wrote = std::fs::read(&blob).expect("the blob the quiesce wrote"); + assert!(wrote.len() > 24, "{name}: the blob holds the cache"); + let time = blob_time(&blob); + drop(made); + drop(device); + drop(core); + within("the teardown", || counters.gauges() == Gauges::default()); + assert_eq!( + blob_time(&blob), + time, + "{name}: the drop wrote nothing more" + ); + assert_eq!(std::fs::read(&blob).expect("the blob"), wrote, "{name}"); + let next = on_directory(name, physical, &dir); + let counters = next.counters(); + within("the seed", || next.core().caches.is_seeded()); + let again = state(name, &next, &kernel); + assert!( + next.quiesce(std::time::Duration::from_secs(8)), + "{name}: the next device's quiesce answered true" + ); + assert_eq!( + next.core().caches.looks_and_writes().1, + 0, + "{name}: a quiesce with no growth writes nothing" + ); + drop(again); + drop(next); + within("the teardown", || counters.gauges() == Gauges::default()); + assert_eq!( + blob_time(&blob), + time, + "{name}: the blob as the first quiesce left it" + ); + let _ = std::fs::remove_dir_all(&dir); + println!( + "quiesce blob: {name}: a blob of {} bytes written at the quiesce, after the \ + creation it waited for; nothing more at the drop, nor at a quiesce with no growth", + wrote.len() + ); + } + } + + /// A creation still running at a quiesce's deadline: the reaper's last write is skipped and + /// counted once (`Metrics::pipeline_cache_writes_skipped`), and nothing is written; the drop + /// then writes the grown cache. + #[test] + fn every_device_skips_its_last_blob_write_under_a_creation_past_the_quiesce_s_deadline() { + let Some(kernel) = descriptor("a1_sign_and_division") else { + return; + }; + let instance = crate::tests::validation::instance(&[]); + let devices = devices_of_the_cfgs_kind(&instance); + assert!(!devices.is_empty(), "no device of this cfg's kind"); + for (name, physical) in &devices { + let dir = + std::env::temp_dir().join(format!("m2v-rt-quiesce-skip-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let device = on_directory(name, physical, &dir); + if !keeps_a_blob(name, &device) { + drop(device); + let _ = std::fs::remove_dir_all(&dir); + continue; + } + let core = std::sync::Arc::clone(device.core()); + let counters = device.counters(); + let blob = core.caches.blob().expect("a cache directory").to_path_buf(); + let (made, open) = grown_unwritten(name, &device, &core, &kernel); + let skipped = counters.metrics().pipeline_cache_writes_skipped; + let _ = device.quiesce(std::time::Duration::from_millis(300)); + // The reaper tries until the deadline, so it counts the skip as it ends alone at or + // past it, while the quiesce, bounded by the same deadline, may already have returned. + within("the skip", || { + counters.metrics().pipeline_cache_writes_skipped > skipped + }); + assert_eq!( + counters.metrics().pipeline_cache_writes_skipped, + skipped + 1, + "{name}: the last write skipped past the deadline" + ); + assert!(!blob.exists(), "{name}: nothing written"); + drop(open); + drop(made); + drop(device); + drop(core); + within("the teardown", || counters.gauges() == Gauges::default()); + assert!(blob.exists(), "{name}: the drop wrote the grown cache"); + let _ = std::fs::remove_dir_all(&dir); + } + } + /// A state's lowerings go to the store on the reaper: with the reaper's writes held, the /// creation returns and nothing is written. Once the queue is full, the next state's lowerings /// are dropped and counted, never waited for. Let go, the reaper writes what was queued: a