Memory: a pool of images' blocks are 64 MiB, as every pool's

930e3aa49e made a pooled block of images a sixteenth larger than every other
pool's (68 MiB, IMAGE_BLOCK_SLACK), so that four images of a quarter block
fit in one past the driver's metadata. On the consumer's 780M scroll gate
those blocks were pin 36's regression against pin 30. Pin 36 with images'
blocks at BLOCK_BYTES (probe e674b9c87d) matched pin 30 over 4 + 4 boots in
ABBA BAAB order (um870 Hawk Point 780M, x86 macOS 12.7.6 guest, page scroll,
job m3-max-13952): rAF gaps of 100 ms or more 8 against 8, of 50 ms or more
22 against 24, presents 29.0/s against 29.3/s; its ready thread made 72 blocks
of 64 MiB a boot and none of 68 MiB.

Every pooled block is now Memory::block_bytes: IMAGE_BLOCK_SLACK,
Memory::image_block_bytes, BlockMaker::image_block_bytes and the two
pooled_bytes lookups that existed only to tell images apart go; their callers
read block_bytes.

tests::blocks_ahead: the packing test now holds that a pool of images' blocks
are BLOCK_BYTES (fails at 68 MiB) and that textures pack as many to a block
as the driver's own placed bytes and alignment fit (three 2048x2048 RGBA8 on
RADV and ANV, four on lavapipe), instead of four to a block.

docs/FAST-PATHS.md: the NVIDIA, blocks-ahead and RADV plain-blocks rows say
64 MiB for images too, with the scroll A/B above.

Owed on development: RUNTIME_VERSION and the pin (block sizes change what a
run allocates), and the cost records of the two cases whose
device_memory_allocated read 71303168 (shared-creator-extended-range-samples,
shared-creator-padded-format-storage) at the next whole-set re-bless.
This commit is contained in:
Anees Iqbal committed 2026-11-11 01:16:55 -08:00
1 parent 812c30a980
commit 653060d6ee
3 files changed
+47 -67

No files matched your search

+8 -43
View File
@@ -5,8 +5,7 @@
//! made outside a heap, and later a Metal heap (step 7: a heap is one block, its resources placed
//! at offsets).
//! - **Pooling.** Resources share blocks of [`BLOCK_BYTES`], so `maxMemoryAllocationCount` (4096
//! on many devices) stays out of reach; a block of images holds a sixteenth besides
//! ([`IMAGE_BLOCK_SLACK`]), so that four images of a quarter of a block fit in one. A request
//! on many devices) stays out of reach; a block of images is one of the same size. A request
//! larger than half a block takes a dedicated block of its own size.
//! - **Kept for reuse.** A block that empties is kept for the next placements rather than freed:
//! a pooled one while fewer than [`KEPT_EMPTY_BLOCKS`] empty ones are kept in its pool, and
@@ -171,15 +170,6 @@ mod ready;
/// The size of a pooled block, at most the device's `maxMemoryAllocationSize`.
pub(super) const BLOCK_BYTES: u64 = 64 << 20;
/// What a pooled block of images holds past [`BLOCK_BYTES`], as a fraction of it: a sixteenth
/// ([`Memory::pooled_bytes`]). A driver's image takes more than its texels: RADV on a Radeon 780M
/// and Iris Xe's driver place a `Private` `RGBA8Unorm` texture of 1024 x 1024 (4 MiB of texels)
/// in 4 MiB and 16 KiB, and one of 2048 x 2048 (16 MiB) in 16 MiB and 64 KiB, at an alignment of
/// 64 KiB, so that a block of 64 MiB held three of the latter, not four. With a sixteenth
/// besides, a block holds four images of a quarter of a block whose metadata and alignment take
/// up to 1 MiB each.
pub(super) const IMAGE_BLOCK_SLACK: u64 = 16;
/// The empty pooled blocks a pool keeps for its next placements whatever its heap's budget
/// ([`Pool::release`], [`Memory::kept_budget`]). In a pool the encodings place in, one of them is
/// mostly the block [`keep_room`] made for room: with one kept, every other block that emptied was
@@ -1921,9 +1911,6 @@ pub(super) struct Memory {
/// The buffers' pools by memory type, then the images' ([`Memory::pool`]).
pools: Vec<Pool<Block>>,
block_bytes: u64,
/// A pooled block of images' size: [`Memory::block_bytes`] and its [`IMAGE_BLOCK_SLACK`],
/// at most the device's largest allocation.
image_block_bytes: u64,
/// Blocks are allocated with `VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT`: the device enabled
/// `bufferDeviceAddress`, so buffers carry device addresses.
device_address: bool,
@@ -2167,8 +2154,6 @@ impl Memory {
.collect(),
types,
block_bytes: BLOCK_BYTES.min(max_allocation.max(1)),
image_block_bytes: (BLOCK_BYTES + BLOCK_BYTES / IMAGE_BLOCK_SLACK)
.min(max_allocation.max(1)),
device_address,
address_types: None,
fd_address_types: None,
@@ -2664,7 +2649,7 @@ impl Memory {
/// 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 bytes = self.block_bytes;
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)
@@ -2950,15 +2935,6 @@ impl Memory {
pool.add_as(size, dedicated, block, used, zeroed)
}
/// The bytes of a pooled block made for `pool`: [`Memory::block_bytes`], and a block of
/// images' [`Memory::image_block_bytes`].
fn pooled_bytes(&self, pool: (Holds, u32)) -> u64 {
match pool.0 {
Holds::Images => self.image_block_bytes,
_ => self.block_bytes,
}
}
/// The bytes of each live pooled block that holds `holds`, of every memory type, for the
/// crate's device tests.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
@@ -3026,7 +3002,6 @@ impl Memory {
.fold(0, |bits, (index, _)| bits | 1 << index);
BlockMaker {
block_bytes: self.block_bytes,
image_block_bytes: self.image_block_bytes,
host_visible,
fd_types: self.fd_buffer_types.unwrap_or(0),
page_types: self.page_buffer_types.unwrap_or(0),
@@ -3272,7 +3247,7 @@ impl Memory {
while at < self.readying.len() {
let (holds, type_index) = self.readying[at].pool();
let started = self.readying[at].started();
let bytes = self.pooled_bytes((holds, type_index));
let bytes = self.block_bytes;
let pool = self.pool(holds, type_index);
let room = ahead_room(bytes);
let since = pool.grew(None).map_or(started, |grew| grew.max(started));
@@ -3647,8 +3622,6 @@ impl Memory {
struct BlockMaker {
/// A pooled block's size ([`BLOCK_BYTES`], or the device's largest allocation).
block_bytes: u64,
/// As [`Memory`]'s.
image_block_bytes: u64,
/// The host-visible memory types, as bits: a block of one is mapped whole.
host_visible: u32,
/// The memory types whose [`Holds::FdBuffers`] blocks are allocated exportable, as bits.
@@ -3668,14 +3641,6 @@ struct BlockMaker {
}
impl BlockMaker {
/// The bytes of a pooled block made for `pool` ([`Memory::pooled_bytes`]).
fn pooled_bytes(&self, pool: (Holds, u32)) -> u64 {
match pool.0 {
Holds::Images => self.image_block_bytes,
_ => self.block_bytes,
}
}
/// A new block of `size` bytes of memory type `type_index` that holds `holds`, with a buffer
/// of addresses where `addressed` gives the counts of such buffers made (its memory then as
/// long as that buffer requires, if longer), allocated zero-initialized where it holds images
@@ -4624,7 +4589,7 @@ fn place(
let pool = (holds, type_index);
let block_size = match dedicated {
true => size,
false => maker.pooled_bytes(pool),
false => maker.block_bytes,
};
if fit == Fit::Pooled {
let mut guard = lock(memory);
@@ -4789,7 +4754,7 @@ pub(super) fn keep_room(
.filter(|&&(holds, _)| holds != own && holds != Holds::FdBuffers && !page(holds));
for &pool in first.chain(rest) {
let addressed = pool.0.buffers().then_some(&**counts);
let bytes = maker.pooled_bytes(pool);
let bytes = maker.block_bytes;
keep_pool_room(memory, device, (&maker, addressed), (pool, bytes));
ready_if_growing(&mut lock(memory), pool);
}
@@ -4859,7 +4824,7 @@ pub(super) fn ahead_room(bytes: u64) -> u64 {
/// ([`Memory::lacking`]).
fn ready_if_growing(memory: &mut Memory, pool: (Holds, u32)) {
if memory.growing(pool) {
let bytes = memory.pooled_bytes(pool);
let bytes = memory.block_bytes;
memory.keep_ahead(pool, bytes);
}
}
@@ -4870,7 +4835,7 @@ fn ready_if_growing(memory: &mut Memory, pool: (Holds, u32)) {
/// there and then ([`ready_if_growing`]), not at the next creation call's [`keep_room`], which
/// may need the block at once. A smaller placement leaves that to the next creation call.
fn ready_after_placing(memory: &mut Memory, pool: (Holds, u32), size: u64) {
let bytes = memory.pooled_bytes(pool);
let bytes = memory.block_bytes;
if size >= bytes - ahead_room(bytes) && memory.makes_ahead(pool) {
ready_if_growing(memory, pool);
}
@@ -4886,7 +4851,7 @@ fn ready_ahead(memory: &Mutex<Memory>, device: &ash::Device, copies: impl FnOnce
let Some(pool) = memory.host_copy_pool(device, copies) else {
return;
};
let bytes = memory.pooled_bytes(pool);
let bytes = memory.block_bytes;
if memory.pool(pool.0, pool.1).has_room(bytes / 2) {
return;
}
@@ -20,7 +20,7 @@
use super::freed_memory::device;
use crate::device::queue_quiet::QueueQuiet;
use crate::memory::Holds;
use crate::memory::{Holds, BLOCK_BYTES};
use crate::{Buffer, Device, Error, ResourceOptions, Texture, TextureDescriptor, WorkCounts};
use metal2vulkan::metal::{PixelFormat, TextureUsage};
use std::time::{Duration, Instant};
@@ -259,15 +259,14 @@ fn a_pool_s_first_block_is_made_alone() {
drop(buffer.unwrap());
}
/// The textures of a pool of images pack as many to a block as their texels' bytes fill: four
/// `RGBA8Unorm` textures of 2048 x 2048 (16 MiB of texels each, a quarter of `memory::BLOCK_BYTES`)
/// to a block, whatever metadata the driver's image carries past its texels within the block's
/// sixteenth besides (`memory::IMAGE_BLOCK_SLACK`): RADV on a Radeon 780M and Iris Xe's driver
/// take 16 MiB and 64 KiB for each at an alignment of 64 KiB, three to a block of 64 MiB. 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.
/// A pool of images' blocks are [`BLOCK_BYTES`], as every pool's, and its textures pack as many to
/// a block as the driver's placed bytes fit: `RGBA8Unorm` textures of 2048 x 2048 (16 MiB of
/// texels each, a quarter of a block), which RADV on a Radeon 780M and Iris Xe's driver place in
/// 16 MiB and 64 KiB at an alignment of 64 KiB, three to a block, and lavapipe in 16 MiB, four.
/// 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 a_block_of_images_holds_four_textures_of_a_quarter_block_each() {
fn a_block_of_images_holds_as_many_textures_as_their_placed_bytes_fit() {
let scratch = Scratch::new("pack");
let device = device(&scratch.0);
let start = device.work_counts();
@@ -290,14 +289,28 @@ fn a_block_of_images_holds_four_textures_of_a_quarter_block_each() {
device.name(),
);
assert!(
used <= PACKED / 4,
"{PACKED} textures of a quarter of a block lie in {used} blocks, not {}",
PACKED / 4
sizes.iter().all(|&size| size == BLOCK_BYTES),
"pooled blocks of images of {sizes:?} bytes, not {BLOCK_BYTES} as every pool's"
);
// The textures' placement: the largest the run asked for, as many times as it made them.
let Some((&(bytes, align), _)) = (placements.iter())
.filter(|&(_, &count)| count >= PACKED as u64)
.max_by_key(|&(&(bytes, _), _)| bytes)
else {
panic!("no placement asked for {PACKED} times among {placements:?}");
};
let stride = bytes.div_ceil(align) * align;
let per_block = ((BLOCK_BYTES - bytes) / stride + 1) as usize;
let fit = PACKED.div_ceil(per_block);
assert!(
used <= fit,
"{PACKED} textures placed in {bytes} bytes at {align} lie in {used} blocks, not the {fit} \
that {per_block} to a block of {BLOCK_BYTES} bytes fill"
);
}
/// The textures `a_block_of_images_holds_four_textures_of_a_quarter_block_each` makes, and their
/// side.
/// The textures `a_block_of_images_holds_as_many_textures_as_their_placed_bytes_fit` makes, and
/// their side.
const PACKED: usize = 8;
const PACKED_SIDE: u64 = 2048;
+12 -10
View File
@@ -966,7 +966,7 @@ driver's. Each is open work.
for each command buffer. Metal's command buffers wait for no such making. Not announced.
- **On NVIDIA, a command buffer submitted while the runtime makes or frees a memory block completes
as that call ends; emptied blocks are kept so that a returning working set makes none again.**
The runtime's memory blocks (`memory.rs`: pooled blocks of 64 MiB, 68 MiB for images (the row on
The runtime's memory blocks (`memory.rs`: pooled blocks of 64 MiB, images' included (the row on
plain blocks below), blocks of their own for placements past half of 64 MiB, and the blocks
exportable buffers share) are made with
`vkAllocateMemory` and freed with `vkFreeMemory`, which hold every submission on NVIDIA's driver
@@ -1000,8 +1000,8 @@ driver's. Each is open work.
its making at once rather than at the next creation call. A creation call that needs one while
its making still waits for that end makes it at once and waits for it, as for a block made in
place, and one that finds none made waits for the ready thread to make it first. A pool near its
next block keeps that block at rest for as long as it stays so, 64 MiB a pool at most (68 MiB
for images), counted against the kept empty blocks' budget. Metal's command buffers wait for no
next block keeps that block at rest for as long as it stays so, 64 MiB a pool at most, counted
against the kept empty blocks' budget. Metal's command buffers wait for no
allocation. Not announced.
- **With unified memory, `Private` memory lies in the system heap's types before the device-local
ones, its buffers' blocks there allocated exportable but for buffers that declare their padding
@@ -1079,13 +1079,15 @@ driver's. Each is open work.
buffer where the device's buffers lie in plain blocks; not the runtime's own pages, which it
imports) shares the reservation of the process's buffer objects on RADV, and its
`vkAllocateMemory` holds every submission of the process while the kernel fills its pages (the
row above). Pooled blocks are 64 MiB on every device, and a block of images 68 MiB: a
sixteenth besides (`IMAGE_BLOCK_SLACK`), since a
driver's image takes more than its texels (RADV on a Radeon 780M and ANV on an Iris Xe place a
`Private` `RGBA8Unorm` texture of 1024x1024 in 4 MiB and 16 KiB, and one of 2048x2048 in 16 MiB
and 64 KiB, at an alignment of 64 KiB), so that four images of a quarter of a block fit in one:
a block of 64 MiB held three such 2048x2048 textures on both, one of 68 MiB holds four
(`tests::blocks_ahead`). On a Radeon 780M (RADV PHOENIX, Mesa
row above). Pooled blocks are 64 MiB on every device, images' included. A driver's image takes
more than its texels (RADV on a Radeon 780M and ANV on an Iris Xe place a `Private`
`RGBA8Unorm` texture of 1024x1024 in 4 MiB and 16 KiB, and one of 2048x2048 in 16 MiB and
64 KiB, at an alignment of 64 KiB), so a block of images holds three such 2048x2048 textures
there, not four (`tests::blocks_ahead`). Blocks of images a sixteenth larger (68 MiB), which
hold four, dropped more frames in a macOS guest's page scroll on a Hawk Point 780M; with
64 MiB blocks the same build matched the build before the larger blocks, over 4 + 4 boots in
ABBA BAAB order: 8 rAF gaps of 100 ms or more against 8, 22 of 50 ms or more against 24, and
29.0 presents a second against 29.3. On a Radeon 780M (RADV PHOENIX, Mesa
26.2.6), the kernel's page pools emptied before each run (through debugfs, the measurement's
method alone: with freed pages in them a block of 16 to 64 MiB took 0.1 to 0.3 ms), a `Private`
texture made every 16.7 ms beside a command buffer committed about every millisecond