diff --git a/crates/metal2vulkan-runtime/src/tests/memory_frees.rs b/crates/metal2vulkan-runtime/src/tests/memory_frees.rs index 5d9e8b78d..ee7b960d2 100644 --- a/crates/metal2vulkan-runtime/src/tests/memory_frees.rs +++ b/crates/metal2vulkan-runtime/src/tests/memory_frees.rs @@ -4,7 +4,8 @@ //! frees is what sets the cost and the cause says which of those frees a change could remove. //! //! A buffer with a block of its own keeps it for the next buffer of its size where its heap's -//! budget of empty blocks spares it, and is counted as a dedicated block where it does not; an +//! budget of empty blocks spares it (read once no block is being made ahead, below), and is +//! counted as a dedicated block where it does not; an //! import of the caller's host memory, which is a dedicated block too, is counted apart as the //! import it is; and the blocks counted are never more than the process's `vkFreeMemory` calls, //! so no block is counted twice or counted that was never freed. @@ -16,6 +17,10 @@ use super::freed_memory::{device, unused, Host, Runs}; use crate::work_counts::MemoryFrees; use crate::{Device, ResourceOptions}; use std::sync::Arc; +use std::time::{Duration, Instant}; + +/// The longest the blocks made ahead may take to be made. +const WAIT: Duration = Duration::from_secs(60); /// A scratch directory for a device's shader cache, removed when dropped. struct Scratch(std::path::PathBuf); @@ -40,6 +45,21 @@ impl Drop for Scratch { /// (`recycle.rs`'s `BUFFERS`): its release frees its block there and then. const OWN_BLOCK: u64 = 128 << 20; +/// Wait until no block is being made ahead for `device` (`memory/ready.rs`): the device's first +/// creation call starts makings on a thread of their own, each a `vkAllocateMemory` the device's +/// counts hold, and under load one landed between two readings of a test that counts its own +/// allocations (the Iris Xe, once in 22 runs of the runtime suite). +fn made_ahead(device: &Device) { + let at = Instant::now(); + while !crate::device::lock(&device.shared().memory).readying_ended() { + assert!( + at.elapsed() < WAIT, + "a block made ahead still making after {WAIT:?}" + ); + std::thread::yield_now(); + } +} + /// What `device` has freed so far, by cause, and the process's `vkFreeMemory` calls. fn frees(device: &Device) -> (MemoryFrees, u64) { let counts = device.work_counts(); @@ -83,7 +103,8 @@ fn a_buffer_with_a_block_of_its_own_frees_a_dedicated_block() { } /// A buffer with a block of its own, released where its heap's budget spares the block, frees -/// none: the block is kept, and the next buffer of its size takes it with no allocation. +/// none: the block is kept, and the next buffer of its size takes it with no allocation. Read once +/// the blocks the first creation call started making ahead are made ([`made_ahead`]). #[test] fn a_buffer_with_a_block_of_its_own_keeps_it_for_the_next_of_its_size() { let scratch = Scratch::new("kept"); @@ -91,6 +112,7 @@ fn a_buffer_with_a_block_of_its_own_keeps_it_for_the_next_of_its_size() { let buffer = device .new_buffer(OWN_BLOCK, ResourceOptions::private()) .unwrap(); + made_ahead(&device); let (before, _) = frees(&device); let counts = device.work_counts(); drop(buffer);