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)
This commit is contained in:
Anees Iqbal committed 2026-11-09 17:52:06 -08:00
1 parent d3e0663439
commit 52b4685ffc
3 files changed
+77 -10

No files matched your search

+29 -10
View File
@@ -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<u64> {
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<Item = &PoolBlock<Block>> {
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<Placement, Error> {
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;
@@ -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<Texture> = 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
@@ -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<String> = (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}");