diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index 2ef0b08ff..29e6dbcc2 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -2659,16 +2659,18 @@ impl Memory { let pools = (0..types).flat_map(|type_index| { [Holds::Exports, Holds::DmaBufs].map(|holds| (holds, type_index)) }); - pools - .filter(|&(holds, type_index)| { - let bytes = self.pooled_bytes((holds, type_index)); - let pool = self.pool(holds, type_index); - let grew = pool - .grew(None) - .is_some_and(|at| at.elapsed() < KEPT_EMPTY_FOR); - grew || pool.near_next(bytes) - }) - .collect() + pools.filter(|&pool| self.growing(pool)).collect() + } + + /// Whether `pool` keeps blocks made ahead now (`memory/ready.rs`): it grew within + /// [`KEPT_EMPTY_FOR`] (a pooled block was added while it held another), or, grown or not, is + /// near its next block ([`Pool::near_next`]). A pool's first block is no growth: a pool that + /// takes one block and stays within it keeps none ahead. + fn growing(&mut self, pool: (Holds, u32)) -> bool { + let bytes = self.pooled_bytes(pool); + let pool = self.pool(pool.0, pool.1); + let grew = (pool.grew(None)).is_some_and(|at| at.elapsed() < KEPT_EMPTY_FOR); + grew || pool.near_next(bytes) } /// The blocks `pool` keeps made ahead (`memory/ready.rs`), its blocks of `bytes`: as many of @@ -2764,9 +2766,13 @@ impl Memory { /// A call's wait for a block of `least` bytes or more made ahead for `pool`, its blocks of /// `bytes` (`memory/ready.rs`; within [`need`](Self::need)), where none is made: the block - /// being made or still to make, or - /// else one queued first for the call, its thread started where it has none, the rest of the - /// pool's blocks kept ahead after it. `None` where the pool host copies place in reads a + /// being made or still to make, or else one queued first for the call, its thread started + /// where it has none, the rest of the pool's blocks kept ahead after it where the pool keeps + /// them ([`growing`](Self::growing)). A pool's first block is made alone: the rest of a + /// block's worth of small blocks behind it (three of 16 MiB) waited unused for a pool that + /// stayed within that block, and went after [`KEPT_EMPTY_FOR`], to be made again + /// (`tests::reclaim_submit_bench`'s small-blocks arms on a Radeon 780M: nine such blocks a + /// device). `None` where the pool host copies place in reads a /// block's pages now, as a page's first touch may wait behind the program's GPU work, before /// [`keep_room`] gave the makings what they are made through, or where no thread starts. fn wait_for(&mut self, pool: (Holds, u32), bytes: u64, least: u64) -> Option { @@ -2783,7 +2789,10 @@ impl Memory { } } None => { - let rest = self.lacking(pool, bytes, &[least]); + let rest = match self.growing(pool) { + true => self.lacking(pool, bytes, &[least]), + false => Vec::new(), + }; let sizes = std::iter::once(least).chain(rest).collect(); let (readying, waiter) = ready::Readying::start(making, pool, sizes, Some(need))?; self.readying.push(readying); @@ -3033,6 +3042,15 @@ impl Memory { .collect() } + /// Every pool that keeps blocks made ahead, with the bytes of its blocks made ahead and not + /// yet added and of those still to make (`memory/ready.rs`), for the crate's device tests. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(super) fn ahead_by_pool(&self) -> Vec<((Holds, u32), u64)> { + (self.readying.iter()) + .map(|ready| (ready.pool(), ready.outstanding_bytes())) + .collect() + } + /// The pool host copies place in, where it was found and the device copies color textures on /// the host ([`Memory::host_copy_pool`]), for the crate's device tests. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] @@ -4911,13 +4929,10 @@ pub(super) fn ahead_room(bytes: u64) -> u64 { /// ([`Pool::near_next`]): those it lacks, its own blocks counted in bytes with its room /// ([`Memory::lacking`]). fn ready_if_growing(memory: &mut Memory, pool: (Holds, u32)) { - let bytes = memory.pooled_bytes(pool); - let pool_of = memory.pool(pool.0, pool.1); - let grew = (pool_of.grew(None)).is_some_and(|at| at.elapsed() < KEPT_EMPTY_FOR); - if !grew && !pool_of.near_next(bytes) { - return; + if memory.growing(pool) { + let bytes = memory.pooled_bytes(pool); + memory.keep_ahead(pool, bytes); } - memory.keep_ahead(pool, bytes); } /// After a placement of `size` bytes in `pool`'s blocks ([`place`]): where the pool's blocks are diff --git a/crates/metal2vulkan-runtime/src/memory/ready.rs b/crates/metal2vulkan-runtime/src/memory/ready.rs index cf88a9ea5..fb34e1374 100644 --- a/crates/metal2vulkan-runtime/src/memory/ready.rs +++ b/crates/metal2vulkan-runtime/src/memory/ready.rs @@ -77,7 +77,9 @@ //! add the pool's smallest block made ahead of the size they need. Where none is made they wait //! for the one being made or still to make, or else for one the pool's thread makes first for //! them, the thread started where the pool has none, the rest of its block's worth made after -//! it: no two threads make blocks for the same pool, none is made only to be freed, and a call +//! it where the pool grows (`Memory::growing`; a pool's first block is made alone, as the rest +//! behind it waited unused where the pool stayed within it): no two threads make blocks for the +//! same pool, none is made only to be freed, and a call //! makes no block of a pool whose blocks are made ahead where a making can serve it. A making //! still waiting for a burst's end when a call comes allocates at once, so that the call waits //! for the allocation alone, never for the program's own submissions to pause; while a call diff --git a/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs b/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs index c92642398..809f59ad2 100644 --- a/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs +++ b/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs @@ -248,6 +248,40 @@ fn a_growing_pool_of_exportable_buffers_gets_its_new_blocks_made_ahead() { ); } +/// A pool's first block is made alone: a pool that takes one block and stays within it keeps no +/// block ahead (`Memory::growing`). The rest of a block's worth of small blocks once queued behind +/// each such pool's first block (three of 16 MiB) waited unused and went after `KEPT_EMPTY_FOR`: +/// on a Radeon 780M, nine blocks of 16 MiB a device in `tests::reclaim_submit_bench`'s small-blocks +/// arms, made and freed for nothing. The pool host copies place in keeps its own blocks ahead +/// from the device's first creation call, by design, and is left out. +#[test] +fn a_pool_s_first_block_is_made_alone() { + let scratch = Scratch::new("first-alone"); + let device = small_blocks_device(&scratch); + let buffer = device.new_buffer(64 << 10, ResourceOptions::private()); + let mut held: Vec = Vec::new(); + texture(&device, (256, 256), &mut held); + makings_ended(&device); + let memory = crate::device::lock(&device.shared().memory); + let host_copies = memory.host_copies_pool(); + let all = memory.ahead_by_pool(); + drop(memory); + println!( + "BLOCKS-AHEAD {}: after a 64 KiB buffer and a 256 KiB texture, kept ahead by pool {:?} \ + bytes; host copies' pool {host_copies:?}", + device.name(), + all, + ); + let kept: Vec<_> = (all.into_iter()) + .filter(|&(pool, bytes)| bytes > 0 && Some(pool) != host_copies) + .collect(); + assert!( + kept.is_empty(), + "pools that took one block kept blocks ahead: {kept:?} bytes" + ); + drop(buffer.unwrap()); +} + /// A pool whose blocks of plain memory are made small after a slow making /// (`Memory::plain_block_made`) keeps as many of them made ahead as one block of /// `memory::BLOCK_BYTES` holds, the bytes it kept ahead before they were made small, and no