From 9612f79c1d48471e9a3cff13a3f5dddc3fb3ce26 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 9 Nov 2026 16:54:40 -0800 Subject: [PATCH] Blocks made ahead: a pool's first block is made alone A call that waits for a pool's block where the pool has no making started its thread with the rest of the pool's block's worth queued behind the call's block (`Memory::wait_for`). For a pool of small blocks (16 MiB, four to a block's worth) that queued three more behind the pool's first block, for a pool that had not grown and that a placement or two left within that block. Those waited unused and went after KEPT_EMPTY_FOR (`Memory::sweep_stale`): on a Radeon 780M, at RT 1991, each small-blocks arm of the reclaim bench made 9 blocks of 16 MiB (144 MiB) ahead that were freed unused in its pressed phase (blocks_ahead_unused 9, the stale frees 9; job 12560), where RT 1987 made none so. The rest is now queued only where the pool keeps blocks ahead at all (`Memory::growing`: it grew within KEPT_EMPTY_FOR or is near its next block), the rule the trigger (`ready_if_growing`) and the exportable pools (`exported_growing`) already follow, now one function. A pool's later growth keeps its block's worth as before. Test: tests::blocks_ahead::a_pool_s_first_block_is_made_alone (a 64 KiB buffer and a 256 KiB texture on a device of small blocks; every pool that keeps blocks ahead grows or is near its next block, the host copies' pool aside). Without this change the RX 550's four pools that took one block each kept 48 MiB ahead and neither grew nor neared their next block; with it, none kept any (job 12585). Measured on a Radeon 780M (job 12586, the small-blocks arms of the reclaim bench, run in both orders, identical in both), per pressed phase, blocks made (MiB) and made ahead and freed unused, before and after this change: - Images one a frame: 73 (1168 MiB) with 9 (144 MiB) unused, then 67 (1072 MiB) with 3 (48 MiB). - Mixed sizes: 45 (720 MiB) with 9 unused, then 39 (624 MiB) with 3. - Bursts: 73 (1168 MiB) with 9 unused, then 67 (1072 MiB) with 3. Quiet phases are unchanged (64, 36 and 64 blocks, none unused), as are the waits. The 3 blocks left unused a device are another pool's (not one that took one block only), whose cause is not yet identified; RT 1987 made none unused, with one small block ahead a pool. Work counts expected to move: blocks_ahead_unused and pooled_memory_blocks_made/freed down by three for each kept pool that takes one small block only; none on a device of 64 MiB blocks, whose block's worth is one block. (cherry picked from commit d5fd0dc672497227cc0519be958ed08bd9bc18b6) --- crates/metal2vulkan-runtime/src/memory.rs | 55 ++++++++++++------- .../metal2vulkan-runtime/src/memory/ready.rs | 4 +- .../src/tests/blocks_ahead.rs | 34 ++++++++++++ 3 files changed, 72 insertions(+), 21 deletions(-) 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