diff --git a/crates/metal2vulkan-runtime/src/tests/memory.rs b/crates/metal2vulkan-runtime/src/tests/memory.rs index 95a9ac922..3de04c986 100644 --- a/crates/metal2vulkan-runtime/src/tests/memory.rs +++ b/crates/metal2vulkan-runtime/src/tests/memory.rs @@ -5,7 +5,8 @@ //! that blocks are handed out and buffers view them, zeroed from their `zero_from` (a Private //! buffer's fill ahead of a copy into it in the next command buffer), that the consumer's hook //! maps each allocation before any block of it goes out and a take whose allocation it does not -//! map is refused, that a block given back is a free range at once, its bytes as written, the +//! map is refused, that a block-maker thread ended by a panic of that hook or of the unmap hook +//! leaves nothing waiting for it, that a block given back is a free range at once, its bytes as written, the //! first fit of a later take, that each idle allocation is freed at its idle age, under steady //! commits too, that seeded takes and releases of mixed lengths all return, that a Private range //! is zeroed when the consumer asks, that the runtime zeroes what it is asked over bytes written @@ -112,8 +113,10 @@ fn the_idle_clock_is_due_at_the_idle_age() { #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod on_device { use crate::device::lock; + use crate::memory::Internal; use crate::memory::IDLE_AGE_NANOS; - use crate::tests::command::on_device::finished; + use crate::ring::{Writes, CHUNK, SPARE}; + use crate::tests::command::on_device::{finished, PATIENCE}; use crate::tests::compute::on_device::{add, WORDS}; use crate::tests::devices_of_the_cfgs_kind; use crate::{ @@ -124,7 +127,7 @@ mod on_device { }; use ash::vk; use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; - use std::sync::{Arc, Mutex}; + use std::sync::{mpsc, Arc, Mutex}; use std::thread; use std::time::{Duration, Instant}; @@ -139,12 +142,30 @@ mod on_device { /// The consumer's hooks as a test sees them: each allocation mapped, as the runtime described /// it and the thread the map ran on, and each unmap, the thread it ran on and the /// allocation's first byte read through the mapping the consumer was told of. While `refuse` - /// is set, it maps nothing. + /// is set, it maps nothing. Where `panic` is armed, the armed hook's next run on the + /// block-maker thread panics there ([`Hook::panic_if_armed`]). #[derive(Default)] struct Hook { mapped: Mutex>, unmapped: Mutex>, refuse: AtomicBool, + panic: Mutex>, + /// The allocation the armed hook panicked over. + panicked_over: Mutex>, + } + + /// Which of the consumer's hooks panics on the block-maker thread. + #[derive(Clone, Copy, PartialEq, Eq, Debug)] + enum Hooked { + Map, + Unmap, + } + + /// A panic armed in one hook: it tells the test it is inside, then waits for the test's word. + struct Armed { + hooked: Hooked, + inside: mpsc::Sender<()>, + word: mpsc::Receiver<()>, } #[derive(Clone, Debug)] @@ -168,6 +189,7 @@ mod on_device { impl BlockMappings for Hook { fn map(&self, allocation: &AllocationInfo) -> bool { + self.panic_if_armed(Hooked::Map, allocation.id); if self.refuse.load(Relaxed) { return false; } @@ -184,6 +206,7 @@ mod on_device { } fn unmap(&self, allocation: AllocationId) { + self.panic_if_armed(Hooked::Unmap, allocation); let contents = (lock(&self.mapped).iter()) .find(|mapped| mapped.allocation == allocation) .and_then(|mapped| mapped.contents); @@ -200,6 +223,28 @@ mod on_device { } impl Hook { + /// On the block-maker thread, where the panic is armed for `hooked`: notes `allocation` + /// as the one panicked over, tells the test it is inside, waits for the test's word + /// within the tests' patience, and panics. + fn panic_if_armed(&self, hooked: Hooked, allocation: AllocationId) { + if thread::current().name() != Some("m2v-blocks") { + return; + } + let armed = { + let mut slot = lock(&self.panic); + match slot.as_ref().is_some_and(|armed| armed.hooked == hooked) { + true => slot.take(), + false => None, + } + }; + if let Some(armed) = armed { + *lock(&self.panicked_over) = Some(allocation); + let _ = armed.inside.send(()); + let _ = armed.word.recv_timeout(PATIENCE); + panic!("the consumer's {hooked:?} hook panics on the block-maker thread"); + } + } + fn mapped(&self) -> Vec { lock(&self.mapped).clone() } @@ -1132,6 +1177,138 @@ mod on_device { } } + /// The block-maker thread ended by a panic of the consumer's map hook, run for the allocation + /// a take made ahead of need, leaves nothing waiting ([`ended_by_a_panic_of`]). + #[test] + fn every_device_whose_block_maker_thread_ends_by_a_map_hook_s_panic_leaves_nothing_waiting() { + ended_by_a_panic_of(Hooked::Map); + } + + /// The block-maker thread ended by a panic of the consumer's unmap hook, run before the free + /// of an allocation idle past the idle age, leaves nothing waiting ([`ended_by_a_panic_of`]). + #[test] + fn every_device_whose_block_maker_thread_ends_by_an_unmap_hook_s_panic_leaves_nothing_waiting() + { + ended_by_a_panic_of(Hooked::Unmap); + } + + /// The block-maker thread ended by a panic of the consumer's `hooked` hook answers what was + /// asked of it as a stop does, and nothing waits for it. The hook holds the thread while + /// takes of the parameter ring on a thread of their own use its free chunks and ask the + /// thread for the next block, then panics. The takes go on within the tests' patience, each + /// block past the free chunks made on their thread; the pool is stopped, so a take of a block + /// is `Error::Quiesced` and an internal asked now is the asking call's to make; the allocation + /// the hook panicked over is freed (at once where its map panicked, at teardown where its + /// unmap did, with no second unmap), so teardown leaves every gauge at zero, and every other + /// allocation mapped is unmapped once. + fn ended_by_a_panic_of(hooked: Hooked) { + const MIB: u64 = 1 << 20; + 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 hook = Arc::new(Hook::default()); + let device = device_with(&hook, physical, name); + let core = device.core(); + let counters = device.counters(); + let (inside_sender, inside) = mpsc::channel(); + let (word, heard) = mpsc::channel(); + *lock(&hook.panic) = Some(Armed { + hooked, + inside: inside_sender, + word: heard, + }); + let held = match hooked { + // A take that breaks the class's only wholly free unit: the block-maker thread + // makes the next allocation ahead of need, and its map holds the thread. + Hooked::Map => Some( + (device.take_block(MemoryClass::Shared, 12 * MIB)) + .unwrap_or_else(|error| panic!("{name}: a block of 12 MiB: {error}")), + ), + // A whole unit taken, then given back once the next is made ahead: the + // block-maker thread frees it at the idle age, and its unmap holds the thread. + Hooked::Unmap => { + let unit = (device + .take_block(MemoryClass::Shared, MemoryClass::Shared.block_len())) + .unwrap_or_else(|error| panic!("{name}: a whole unit: {error}")); + wait_until("the unit made ahead", || { + core.pool.stocked(MemoryClass::Shared) + }); + device.release_block(unit).expect("a release"); + None + } + }; + (inside.recv_timeout(PATIENCE)).unwrap_or_else(|_| { + panic!("{name}: the {hooked:?} hook on the block-maker thread") + }); + // Takes of the ring past every chunk it holds free, on a thread of their own, which + // the test does not join: one left waiting fails the test, not hangs it. + let (done, answer) = mpsc::channel(); + let taker = Arc::clone(core); + thread::spawn(move || { + let bytes = vec![7u8; CHUNK as usize]; + let mut writes = Writes::default(); + let written = + (0..4 * SPARE).try_for_each(|_| writes.write(&taker, &bytes).map(drop)); + writes.give_back(&taker); + let _ = done.send(written); + }); + wait_until("the ring's ask of the block-maker thread", || { + core.ring.asked() + }); + // The hook panics, the ring's ask outstanding. + word.send(()).expect("the hook waits for the word"); + let written = answer.recv_timeout(PATIENCE).unwrap_or_else(|_| { + panic!( + "{name}: the ring's takes still wait {PATIENCE:?} after the block-maker \ + thread ended in the {hooked:?} hook" + ) + }); + written.unwrap_or_else(|error| panic!("{name}: a ring write: {error}")); + assert!( + core.pool.stopped(), + "{name}: the pool stopped as the thread ended" + ); + match device.take_block(MemoryClass::Shared, 64 << 10) { + Err(Error::Quiesced) => {} + other => panic!( + "{name}: a take after the block-maker thread ended: {:?}", + other.map(|block| block.id) + ), + } + assert!( + !core.pool.ask(Internal::StagingRegion), + "{name}: an internal asked of the ended thread" + ); + core.pool.await_made(Internal::StagingRegion); + drop(held); + drop(device); + assert_eq!( + counters.gauges(), + Gauges::default(), + "{name}: gauges after teardown" + ); + let over = (*lock(&hook.panicked_over)).expect("the allocation panicked over"); + let mut unmapped: Vec = + hook.unmapped().iter().map(|u| u.allocation).collect(); + unmapped.sort_unstable(); + let mut mapped: Vec = (hook.mapped().iter()) + .map(|m| m.allocation) + .filter(|&allocation| allocation != over) + .collect(); + mapped.sort_unstable(); + assert_eq!( + unmapped, mapped, + "{name}: each allocation mapped unmapped once, none the {hooked:?} hook panicked over" + ); + println!( + "memory: {name}: the block-maker thread ended by the {hooked:?} hook's panic; the \ + ring's takes went on, a take refused as quiesced; metrics {:?}", + counters.metrics() + ); + } + } + /// A long block given back is a free range of its long allocation at once, which the next take /// of its length lands in: a Shared block of 64 MiB, which takes an allocation of its own, is /// taken, given a zeroed buffer, and given back 20 times in a row, each take straight after