From 52b4685ffce97d35ef182dc929646cbde52e4aa0 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 9 Nov 2026 17:51:33 -0800 Subject: [PATCH] Small blocks: a packing test, and each image's bytes and alignment as placed A device test that makes 16 RGBA8Unorm textures of 1024 x 1024 (4 MiB of texels each) on a device whose plain blocks are 16 MiB and counts the pooled blocks of images that hold one, asserting four textures to a block; and a test-side record of each image placement's bytes and alignment as the driver asked them (Memory::image_placements), which the test and the reclaim bench print. On a Radeon 780M the bench's 192 such textures took 64 blocks of 16 MiB (1024 MiB), three to a block, against 13 of 64 MiB (832 MiB), fifteen to a block (job 12560): the images carry bytes past their texels. Here the test fails as it should: 6 blocks of 16 MiB hold the 16 textures on the 780M (each placed in 4,210,688 bytes at 65,536, job 12602) and on the RX 550 (4,210,712 at 65,536, job 12590); 4 on lavapipe (4,194,304 at 64). The fix follows. (cherry picked from commit 62b0f67dda0b1cd722bcde3a4ba459d01613c544) --- crates/metal2vulkan-runtime/src/memory.rs | 39 ++++++++++++++----- .../src/tests/blocks_ahead.rs | 38 ++++++++++++++++++ .../src/tests/reclaim_submit_bench.rs | 10 +++++ 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index 025aee692..0f8de4fdf 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -2028,6 +2028,10 @@ pub(super) struct Memory { /// The bytes of the blocks made ahead and freed unused ([`AheadBlocks::unused`]). #[cfg(all(test, m2v_gpu, target_os = "linux"))] unused_bytes: u64, + /// The images placed ([`place`]), by their bytes and alignment as the driver asked them, since + /// a test last cleared it. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) image_placements: std::collections::BTreeMap<(u64, u64), u64>, /// Blocks of images are allocated with `VK_MEMORY_ALLOCATE_ZERO_INITIALIZE_BIT_EXT` /// ([`Memory::zero_image_blocks`]). zero_images: bool, @@ -2219,6 +2223,8 @@ impl Memory { in_place_bytes: std::collections::BTreeMap::new(), #[cfg(all(test, m2v_gpu, target_os = "linux"))] unused_bytes: 0, + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + image_placements: std::collections::BTreeMap::new(), } } @@ -3016,17 +3022,26 @@ impl Memory { /// crate's device tests. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] pub(super) fn pooled_block_sizes(&self, holds: Holds) -> Vec { + self.pooled_blocks(holds).map(|block| block.size).collect() + } + + /// How many live pooled blocks that hold `holds`, of every memory type, hold a placement, for + /// the crate's device tests. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(super) fn pooled_blocks_in_use(&self, holds: Holds) -> usize { + self.pooled_blocks(holds) + .filter(|block| !block.empty()) + .count() + } + + /// The live pooled blocks that hold `holds`, of every memory type. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + fn pooled_blocks(&self, holds: Holds) -> impl Iterator> { let count = self.types.len() as u32; - (0..count) - .flat_map(|type_index| { - let pool = self.pool_ref(holds, type_index); - pool.blocks - .iter() - .flatten() - .filter(|block| !block.dedicated) - }) - .map(|block| block.size) - .collect() + (0..count).flat_map(move |type_index| { + let pool = self.pool_ref(holds, type_index); + (pool.blocks.iter().flatten()).filter(|block| !block.dedicated) + }) } /// For each pool of `holds` that keeps blocks made ahead, its memory type's index and the bytes @@ -4667,6 +4682,10 @@ fn place( ) -> Result { let size = requirements.size.max(1); let align = requirements.alignment.max(1); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + if holds == Holds::Images { + *(lock(memory).image_placements.entry((size, align))).or_default() += 1; + } let (candidates, overflow, maker) = { let memory = lock(memory); let bits = requirements.memory_type_bits; diff --git a/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs b/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs index 7c442ff97..debe87da3 100644 --- a/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs +++ b/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs @@ -341,6 +341,44 @@ fn a_pool_of_small_blocks_keeps_a_block_s_bytes_ahead_and_no_creation_call_makes ); } +/// The textures of a pool of small blocks pack as many to a block as their texels' bytes fill: +/// four `RGBA8Unorm` textures of 1024 x 1024 (4 MiB of texels each, a quarter of a 16 MiB block) +/// to a block, whatever metadata the driver's image carries past its texels. The run makes +/// [`PACKED`] of them, one at a time, and counts the pool's blocks of images, printing each +/// image's bytes and alignment as the driver asked them. +#[test] +fn small_blocks_hold_four_textures_of_a_quarter_block_each() { + let scratch = Scratch::new("pack"); + let device = small_blocks_device(&scratch); + let start = device.work_counts(); + let mut held: Vec = Vec::new(); + for _ in 0..PACKED { + texture(&device, (TEXTURE_SIDE, TEXTURE_SIDE), &mut held); + } + let counts = device.work_counts(); + let made = (counts.image_blocks_made_ahead + counts.image_blocks_made_in_place) + - (start.image_blocks_made_ahead + start.image_blocks_made_in_place); + let memory = crate::device::lock(&device.shared().memory); + let used = memory.pooled_blocks_in_use(Holds::Images); + let sizes = memory.pooled_block_sizes(Holds::Images); + let placements = memory.image_placements.clone(); + drop(memory); + println!( + "BLOCKS-AHEAD {}: {PACKED} textures of 1024 x 1024 in pooled blocks of {sizes:?} bytes, \ + {made} added since the first texture: {used} hold a texture; images placed by bytes and \ + alignment {placements:?}", + device.name(), + ); + assert!( + used <= PACKED / 4, + "{PACKED} textures of a quarter of a small block lie in {used} blocks, not {}", + PACKED / 4 + ); +} + +/// The textures `small_blocks_hold_four_textures_of_a_quarter_block_each` makes. +const PACKED: usize = 16; + /// A pool of small blocks whose placements larger than its blocks, each taking a block of its /// own size (`memory::pooled_block_bytes`), are recent keeps a block of that size made ahead /// too, which the calls that need one of the pool's own blocks leave to them: the first such diff --git a/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs b/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs index 055c77ac5..a3a33ed65 100644 --- a/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs +++ b/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs @@ -599,6 +599,7 @@ fn phase( let mut memory = crate::device::lock(&device.shared().memory); memory.longest_making = Duration::ZERO; memory.in_place_bytes.clear(); + memory.image_placements.clear(); memory.turnover_bytes() }; let before = (vmstat(), device.work_counts()); @@ -631,6 +632,11 @@ fn phase( memory.turnover_bytes(), ) }; + let placements: Vec = (crate::device::lock(&device.shared().memory) + .image_placements + .iter()) + .map(|(&(bytes, align), count)| format!("{count} of {bytes} bytes at {align}")) + .collect(); let over = |times: &[(Duration, f64)], bound: f64| { (times.iter()) .filter(|&&(took, _)| took.as_secs_f64() * 1e3 >= bound) @@ -702,6 +708,10 @@ fn phase( frees.kept_stale - earlier_frees.kept_stale, frees.kept_for_room - earlier_frees.kept_for_room, ); + eprintln!( + "RECLAIM-SUBMIT {arm}, {name}: images placed by bytes and alignment [{}]", + placements.join(", ") + ); eprintln!("RECLAIM-SUBMIT {arm}, {name}: slowest submissions {slow}"); eprintln!("RECLAIM-SUBMIT {arm}, {name}: slowest turnarounds {slow_turns}"); eprintln!("RECLAIM-SUBMIT {arm}, {name}: slowest makings {slow_makes}");