diff --git a/crates/metal2vulkan-runtime/src/hazard.rs b/crates/metal2vulkan-runtime/src/hazard.rs index 0c2be803d..a140f044a 100644 --- a/crates/metal2vulkan-runtime/src/hazard.rs +++ b/crates/metal2vulkan-runtime/src/hazard.rs @@ -141,6 +141,12 @@ impl Hazards { self.read_wild } + /// For the device tests: whether the sets hold a table of marks. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) fn holds_table(&self) -> bool { + self.marks.capacity() + self.own.capacity() > 0 + } + /// Starts both sets again, at a barrier. pub(crate) fn clear(&mut self) { if self.generation == u32::MAX { diff --git a/crates/metal2vulkan-runtime/src/record.rs b/crates/metal2vulkan-runtime/src/record.rs index 8041f1c0a..7f3cfba55 100644 --- a/crates/metal2vulkan-runtime/src/record.rs +++ b/crates/metal2vulkan-runtime/src/record.rs @@ -11,7 +11,12 @@ use ash::vk; use std::sync::Mutex; /// The most idle pools the device keeps; one given back past them is destroyed. -const IDLE_MAX: usize = 64; +pub(crate) const IDLE_MAX: usize = 64; + +/// For the device tests: the idle pools that keep their memory through the reset that idles them; +/// today every idle pool keeps it. +#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] +pub(crate) const WARM: usize = 48; /// A command pool and the command buffers allocated from it so far, the first `used` of them /// recording or recorded, and the hazard sets of the Metal command buffer recording with it, @@ -223,6 +228,16 @@ impl Recorders { } } + /// For the device tests: the idle recorders whose pools kept their memory at the reset that + /// idled them, and those whose pools released it, each with how many of them hold a hazard + /// table. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) fn idle(&self) -> [(usize, usize); 2] { + let idle = lock(&self.idle); + let tables = idle.iter().filter(|r| r.hazards.holds_table()).count(); + [(idle.len(), tables), (0, 0)] + } + /// At teardown: destroys every idle recorder. pub(crate) fn destroy_all(&mut self, device: &ash::Device) { let idle = self diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 5f10f28e0..e38f6c7b0 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -128,6 +128,7 @@ mod readback; pub(crate) mod real_size; mod real_size_overs; mod real_size_renders; +mod recorders; mod refusal_basis; mod render; mod resolve; diff --git a/crates/metal2vulkan-runtime/src/tests/recorders.rs b/crates/metal2vulkan-runtime/src/tests/recorders.rs new file mode 100644 index 000000000..96622f6c6 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/recorders.rs @@ -0,0 +1,79 @@ +//! The device's idle recorders (`crate::record`), on every device of the cfg's kind: a burst of +//! recorders past `IDLE_MAX`, each with a command buffer recorded and a hazard table, given back, +//! leaves `WARM` idle that kept their pools' memory and their tables, and the rest up to +//! `IDLE_MAX` idle with both released; the reaper destroys those past `IDLE_MAX`. Recorders taken +//! within the warm ones are taken from them and come back among them, and the released ones record +//! again. + +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod on_device { + use crate::device::Core; + use crate::hazard::Access; + use crate::record::{self, Recorder, IDLE_MAX, WARM}; + use crate::tests::command::on_device::each_device; + + /// A resource id each recorder marks, so its hazard table holds 4,096 marks. + const ID: u32 = 4095; + + /// `count` recorders, each with a command buffer recorded and ended, and [`ID`] marked. + fn recorded(core: &Core, count: usize) -> Vec { + (0..count) + .map(|_| { + let mut recorder = core.recorders.take(&core.device).expect("a recorder"); + let buffer = recorder.next(&core.device).expect("a command buffer"); + recorder.hazards.mark(ID, Access::Write); + record::end(&core.device, buffer, false, &[], None, &core.counters) + .expect("the command buffer ended"); + recorder + }) + .collect() + } + + fn give_back(core: &Core, recorders: Vec) { + for recorder in recorders { + core.recorders.give_back(core, recorder); + } + } + + /// A burst of [`IDLE_MAX`] and 8 recorders, given back: the first [`WARM`] kept their memory and + /// tables, the rest up to `IDLE_MAX` released both. Taking the warm ones again leaves the + /// released ones idle, and giving them back makes them warm again; taking every idle one + /// records into the released ones too, and their give-back leaves the same split. + #[test] + fn a_burst_s_recorders_past_the_warm_ones_release_their_memory() { + each_device("recorders_burst", |name, _, device| { + let core = device.core(); + core.pool.await_internals(); + let cold = IDLE_MAX - WARM; + give_back(core, recorded(core, IDLE_MAX + 8)); + assert_eq!( + core.recorders.idle(), + [(WARM, WARM), (cold, 0)], + "{name}: the burst given back (kept, with tables; released, with tables)" + ); + let warm = recorded(core, WARM); + assert_eq!( + core.recorders.idle(), + [(0, 0), (cold, 0)], + "{name}: the warm ones taken" + ); + give_back(core, warm); + assert_eq!( + core.recorders.idle(), + [(WARM, WARM), (cold, 0)], + "{name}: the warm ones given back" + ); + give_back(core, recorded(core, IDLE_MAX)); + assert_eq!( + core.recorders.idle(), + [(WARM, WARM), (cold, 0)], + "{name}: every idle one taken and given back" + ); + println!( + "recorders burst: {name}: {} given back, {WARM} idle kept their memory, {cold} \ + released it", + IDLE_MAX + 8 + ); + }); + } +}