diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 3a8fffc36..373fc0759 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -135,6 +135,7 @@ mod real_size_renders; mod recorders; mod reflection_answers; mod refusal_basis; +mod releases_done; mod render; mod resolve; mod ring; diff --git a/crates/metal2vulkan-runtime/src/tests/releases_done.rs b/crates/metal2vulkan-runtime/src/tests/releases_done.rs new file mode 100644 index 000000000..3e20404bc --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/releases_done.rs @@ -0,0 +1,154 @@ +//! [`Gauges::releases_waiting`](crate::Gauges::releases_waiting) counts a release until it is +//! done, under a device cfg on every device of the cfg's kind: an object the reaper thread is +//! given counts until the reaper has freed it, and a host import given back counts until the +//! block-maker thread has freed it and run its release. Each thread is held while the test reads: +//! the reaper in its look at the pipeline cache, the block-maker parked before its first work. So +//! a read of no release waiting with no command buffer live, which the harness's churn rows and +//! the replay player's settled reads take for every release done, comes after the frees. + +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod on_device { + use crate::memory::MakerTest; + use crate::tests::command::on_device::{within, Release as Unpark}; + use crate::tests::devices_of_the_cfgs_kind; + use crate::tests::import::on_device::{free, host_memory, releasing}; + use crate::{ + Device, DeviceOptions, IndexClass, PixelFormat, StorageMode, TextureDescriptor, TextureType, + }; + use std::sync::atomic::Ordering; + use std::sync::{Arc, Barrier}; + + /// The reaper held in its look at the pipeline cache until this drops, so a failed assertion + /// lets it go on and the device's teardown does not wait for it. + struct LookHeld<'a>(&'a crate::cache::Hold); + + impl Drop for LookHeld<'_> { + fn drop(&mut self) { + self.0.release(); + } + } + + /// A texture view dropped with nothing in flight and no release waiting goes to the reaper + /// thread at once. Held in its look at the pipeline cache, the reaper has not freed it: the + /// textures gauge still counts the view, and its release counts as waiting. Let go, the + /// reaper frees it, and the view and its release leave the gauges together. + #[test] + fn every_device_counts_a_view_the_reaper_has_not_freed_as_a_release_waiting() { + 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 device = (Device::new(physical, DeviceOptions::default())) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let counters = device.counters(); + let _grant = (device.grant_indices(IndexClass::Texture, 1, 2)) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let mut d = + TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 64, 64); + d.storage_mode = StorageMode::Private; + let texture = + (device.new_texture(&d, Some(1))).unwrap_or_else(|error| panic!("{name}: {error}")); + let view = texture.new_texture_view( + PixelFormat::RGBA8Unorm, + TextureType::Type2D, + 0..1, + 0..1, + Some(2), + ); + let view = view.unwrap_or_else(|error| panic!("{name}: a view: {error}")); + // A command buffer completed after them carries and retires what their making left + // in flight. + let queue = device.new_command_queue(1).expect("a queue"); + let command_buffer = queue.command_buffer().expect("a command buffer"); + command_buffer.commit().expect("a commit"); + command_buffer.wait_until_completed().expect("a wait"); + drop(command_buffer); + within("nothing in flight or waiting", || { + let gauges = counters.gauges(); + gauges.command_buffers == 0 && gauges.releases_waiting == 0 + }); + let caches = &device.core().caches; + caches.look_hold.hold(); + let held = LookHeld(&caches.look_hold); + // A creation's end has the reaper look at the cache once creation is quiet. + drop(crate::cache::creation(device.core())); + within("the reaper's look", || caches.look_hold.waiting()); + let before = counters.gauges(); + drop(view); + let after = counters.gauges(); + assert_eq!( + (after.textures, after.releases_waiting), + (before.textures, before.releases_waiting + 1), + "{name}: a view the reaper has not freed (textures, releases waiting)" + ); + drop(held); + within("the reaper's free", || { + let gauges = counters.gauges(); + gauges.textures + 1 == before.textures + && gauges.releases_waiting == before.releases_waiting + }); + println!( + "releases_done: {name}: a view queued for the reaper counted as waiting until freed" + ); + drop((texture, queue, device)); + } + } + + /// A host import given back goes to the block-maker thread to free. Parked before its first + /// work, the thread has not freed it: the imports gauge still counts it, its release has not + /// run, and it counts as waiting. Let go, the thread frees it and runs the release, and the + /// import and its release leave the gauges together. A device that imports no host memory + /// says so. + #[test] + fn every_device_counts_an_import_the_block_maker_has_not_freed_as_a_release_waiting() { + 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 park = Arc::new(Barrier::new(2)); + let options = DeviceOptions { + maker: MakerTest::Park(Arc::clone(&park)), + ..DeviceOptions::default() + }; + let device = + Device::new(physical, options).unwrap_or_else(|error| panic!("{name}: {error}")); + let counters = device.counters(); + // Parked before its first work. + park.wait(); + let mut unpark = Unpark::of(&park); + let Some(&(_, alignment)) = device.core().imports.host.as_ref() else { + unpark.now(); + println!("releases_done: {name}: imports no host memory"); + continue; + }; + let len = (64u64 << 10).next_multiple_of(alignment); + let (pointer, mapping) = host_memory(len, alignment); + let (release, released) = releasing(); + let before = counters.gauges(); + // SAFETY: the memory is the test's, mapped until it unmaps it after the release. + let (block, _) = unsafe { device.import_host_memory(pointer, len, release) } + .unwrap_or_else(|error| panic!("{name}: an import: {error}")); + drop(block); + let after = counters.gauges(); + assert!( + !released.load(Ordering::SeqCst), + "{name}: the import's release ran with the block-maker thread parked" + ); + assert_eq!( + (after.imports, after.releases_waiting), + (before.imports + 1, before.releases_waiting + 1), + "{name}: an import the block-maker thread has not freed (imports, releases waiting)" + ); + unpark.now(); + within("the block-maker's free", || { + let gauges = counters.gauges(); + released.load(Ordering::SeqCst) + && gauges.imports == before.imports + && gauges.releases_waiting == before.releases_waiting + }); + free(mapping); + println!("releases_done: {name}: an import given back counted as waiting until freed"); + drop(device); + } + } +}