From c303cdf71d11fba14326da79e2dcd0a068f4d501 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 8 Nov 2026 10:02:41 -0800 Subject: [PATCH] memory_frees: a buffer's own block is kept for the next of its size; the dedicated free with the budget at 0 The test of a dedicated block's free now sets the heaps' budget of kept empty blocks to 0 through a test-only stand-in on Memory, as the free it counts happens only past that budget. A new test checks the kept path: a released 128 MiB Private buffer frees nothing, its block is kept empty, and the next buffer of its size takes it with no vkAllocateMemory. (cherry picked from commit fcc48ace45b8d96a55e283dafbcaa2ef927d41a7) --- crates/metal2vulkan-runtime/src/memory.rs | 9 ++++ .../src/tests/memory_frees.rs | 53 ++++++++++++++++--- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index dd41cd7b3..eb2bd3e84 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -1368,6 +1368,9 @@ pub(super) struct Memory { /// the lock released where the driver's import runs. #[cfg(test)] pub(crate) import_hold: Option>, + /// A test's stand-in for every heap's budget of kept empty blocks ([`Memory::kept_budget`]). + #[cfg(test)] + pub(crate) kept_budget_stand_in: Option, /// Blocks of images are allocated with `VK_MEMORY_ALLOCATE_ZERO_INITIALIZE_BIT_EXT` /// ([`Memory::zero_image_blocks`]). zero_images: bool, @@ -1440,6 +1443,8 @@ impl Memory { host_copy_blocks: HostCopyBlocks::default(), #[cfg(test)] import_hold: None, + #[cfg(test)] + kept_budget_stand_in: None, } } @@ -1507,6 +1512,10 @@ impl Memory { /// of the heap, at most [`KEPT_EMPTY_BYTES`]. Its pools' first [`KEPT_EMPTY_BLOCKS`] empty /// pooled blocks are kept whatever it says, and count against it. fn kept_budget(&self, heap: usize) -> u64 { + #[cfg(test)] + if let Some(budget) = self.kept_budget_stand_in { + return budget; + } let size = self.heaps.get(heap).map_or(0, |&(size, _)| size); (size / KEPT_EMPTY_SHARE).min(KEPT_EMPTY_BYTES) } diff --git a/crates/metal2vulkan-runtime/src/tests/memory_frees.rs b/crates/metal2vulkan-runtime/src/tests/memory_frees.rs index 6f22b694f..5d9e8b78d 100644 --- a/crates/metal2vulkan-runtime/src/tests/memory_frees.rs +++ b/crates/metal2vulkan-runtime/src/tests/memory_frees.rs @@ -3,10 +3,11 @@ //! object's pages as it frees it charges that page-table update to the free, so what a workload //! frees is what sets the cost and the cause says which of those frees a change could remove. //! -//! A buffer with a block of its own is counted as a dedicated block; an import of the caller's -//! host memory, which is a dedicated block too, is counted apart as the import it is; and the -//! blocks counted are never more than the process's `vkFreeMemory` calls, so no block is counted -//! twice or counted that was never freed. +//! A buffer with a block of its own keeps it for the next buffer of its size where its heap's +//! budget of empty blocks spares it, and is counted as a dedicated block where it does not; an +//! import of the caller's host memory, which is a dedicated block too, is counted apart as the +//! import it is; and the blocks counted are never more than the process's `vkFreeMemory` calls, +//! so no block is counted twice or counted that was never freed. //! //! Device tests: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, //! `m2v_cpu_icd`), on the kind of device the cfg selects. @@ -45,12 +46,14 @@ fn frees(device: &Device) -> (MemoryFrees, u64) { (counts.memory_frees, counts.object_churn.vk_memory_freed) } -/// A buffer with a block of its own, released, frees one block, counted as a dedicated block: -/// one resource going away with memory of its own, which only a shorter lifetime removes. +/// A buffer with a block of its own, released where its heap keeps no empty block (the budget's +/// test stand-in at 0), frees one block, counted as a dedicated block: one resource going away +/// with memory of its own. #[test] fn a_buffer_with_a_block_of_its_own_frees_a_dedicated_block() { let scratch = Scratch::new("dedicated"); let device = device(&scratch.0); + crate::device::lock(&device.shared().memory).kept_budget_stand_in = Some(0); let (before, _) = frees(&device); let buffer = device .new_buffer(OWN_BLOCK, ResourceOptions::private()) @@ -79,6 +82,44 @@ fn a_buffer_with_a_block_of_its_own_frees_a_dedicated_block() { ); } +/// A buffer with a block of its own, released where its heap's budget spares the block, frees +/// none: the block is kept, and the next buffer of its size takes it with no allocation. +#[test] +fn a_buffer_with_a_block_of_its_own_keeps_it_for_the_next_of_its_size() { + let scratch = Scratch::new("kept"); + let device = device(&scratch.0); + let buffer = device + .new_buffer(OWN_BLOCK, ResourceOptions::private()) + .unwrap(); + let (before, _) = frees(&device); + let counts = device.work_counts(); + drop(buffer); + let (after, _) = frees(&device); + assert_eq!( + after.blocks, before.blocks, + "the released buffer's block was kept" + ); + let kept = device.work_counts(); + assert_eq!( + kept.empty_memory_blocks_live, + counts.empty_memory_blocks_live + 1, + "the block is kept empty" + ); + let again = device + .new_buffer(OWN_BLOCK, ResourceOptions::private()) + .unwrap(); + let taken = device.work_counts(); + assert_eq!( + taken.object_churn.vk_memory_allocations, kept.object_churn.vk_memory_allocations, + "the next buffer of its size took the kept block" + ); + assert_eq!( + taken.empty_memory_blocks_live, + counts.empty_memory_blocks_live + ); + drop(again); +} + /// An import of the caller's host memory is a dedicated block too, and is counted as the import /// it is rather than as a dedicated block: the two have different cures, so the breakdown keeps /// them apart.