From 42ddd3f9662374046b07fb83ff9be133c9785c1b Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 18 Nov 2026 18:10:50 -0800 Subject: [PATCH] rt: a single-object allocation is never the reserve What: aging() counts an idle allocation as its class's reserve only where it is a unit allocation, of 16 MiB (ALLOCATION_LEN) or less. A long allocation, one a take longer than the unit made for itself, is always weighed by the idle rule and freed at its idle age. Why: the reserve is the newest idle allocations with free bytes that keep the class's free bytes at 8 MiB (MAKE_BELOW), so the block-maker thread does not free what it would make again. Once a long block's bytes were given back to its allocation (an unkeep: a take that no free range fits asks the kept blocks of other lengths for their bytes), that allocation was idle with free bytes and the newest: it became the reserve and was never freed, holding its whole length (64 MiB in the test, hundreds of MiB in a play) for the device's life, while the older unit allocation was freed instead. Test: every_device_frees_a_long_allocation_whose_block_s_bytes_are_free_at_its_idle_age. 1. A Shared block of 64 MiB is taken and given back, and a take of 20 MiB makes its bytes a free range of their allocation (the unkeep). 2. The 20 MiB block is given back too, and the test waits twice the idle age. 3. Only the class's unit allocation stays, as the reserve: blocks, Shared idle bytes, Shared reserve bytes and Shared idle age are (zero source + 2, 16 MiB, 16 MiB, 0). Verified: - Check m3-max-49062 at this tree (trial commit ad1321382c, the same tree on the same parent): fmt, clippy in every configuration, Rustdoc and the suites, green. - Negative control m3-max-49102 (the fix reverted, the test kept) fails as intended. The reserve holds the 64 MiB allocation: left (3, 67108864, 67108864, 0) against right (3, 16777216, 16777216, 0). - Device legs at ad1321382c, memory::on_device, green: - lavapipe 49100 - RTX 2080 49105 - RX 7900 XTX 49111 - M3 (KosmicKrisp) 49104 - RTX 3090 49103 and 49147 - Iris 49134 - UHD 630 (Windows) 49118 - RX 550 49106 The 780M's leg, 49181, is queued. - r3 played on the 2080 to frame 1137 (m3-max-49135): the players before (a47e4abe53) and after (this tree) both end with Shared and Private idle bytes 0 and reserve bytes 0. Neither reserves more than 16 MiB on any frame, so r3 does not show the bug. u3's play on the 780M follows as evidence after the landing. --- crates/metal2vulkan-rt/src/memory.rs | 16 +++-- crates/metal2vulkan-rt/src/tests/memory.rs | 73 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/crates/metal2vulkan-rt/src/memory.rs b/crates/metal2vulkan-rt/src/memory.rs index 375cb078a..a3e7b2a7f 100644 --- a/crates/metal2vulkan-rt/src/memory.rs +++ b/crates/metal2vulkan-rt/src/memory.rs @@ -347,8 +347,9 @@ struct Allocation { /// wait, for those bytes, and look for a free range once more. It waits only while the block-maker /// thread works for it, by one predicate both read: [`State::needs_maker`]. /// -/// The idle rule never frees an allocation that the class's free bytes need to stay at or above -/// [`MAKE_BELOW`], so it does not free what the block-maker thread would make again. +/// The idle rule never frees a unit allocation, one of [`ALLOCATION_LEN`], that the class's free +/// bytes need to stay at or above [`MAKE_BELOW`], so it does not free what the block-maker thread +/// would make again. An allocation a longer take made for itself is never kept so. /// /// The idle rule runs on the block-maker thread as a block is taken or given back, and again when /// the oldest idle allocation reaches [`IDLE_AGE_NANOS`]: it frees each idle allocation of that @@ -900,9 +901,12 @@ impl State { } /// The class's idle allocations the idle rule weighs, as when each became idle and its memory, - /// the oldest first, and the bytes of its reserve. The newest idle allocations with free bytes - /// that the class's free bytes need to reach [`MAKE_BELOW`] are its reserve, made ahead of - /// need: the rule neither weighs nor frees them. + /// the oldest first, and the bytes of its reserve. The newest idle unit allocations, of + /// [`ALLOCATION_LEN`], with free bytes that the class's free bytes need to reach [`MAKE_BELOW`] + /// are its reserve, made ahead of need: the rule neither weighs nor frees them. An allocation + /// a take longer than the unit made for itself is never the reserve, even once its block's + /// bytes are free ranges: the rule frees it at its idle age, so it is not held for the + /// device's life. fn aging(&self, class: MemoryClass) -> (Vec<(u64, vk::DeviceMemory)>, u64) { let allocations = &self.allocations[class.index()]; let mut free: u64 = (allocations.iter()) @@ -924,7 +928,7 @@ impl State { idle.sort_unstable_by_key(|&(since, ..)| std::cmp::Reverse(since)); let mut reserved = 0; idle.retain(|&(_, _, bytes, len)| { - let reserve = free < MAKE_BELOW && bytes > 0; + let reserve = free < MAKE_BELOW && bytes > 0 && len <= ALLOCATION_LEN; if reserve { free += bytes; reserved += len; diff --git a/crates/metal2vulkan-rt/src/tests/memory.rs b/crates/metal2vulkan-rt/src/tests/memory.rs index eaf8c9dae..21c06d113 100644 --- a/crates/metal2vulkan-rt/src/tests/memory.rs +++ b/crates/metal2vulkan-rt/src/tests/memory.rs @@ -1247,6 +1247,79 @@ mod on_device { } } + /// An allocation a take longer than the unit made for itself is never the reserve: a Shared + /// block of 64 MiB, given back and kept, has its bytes given back to its allocation by a take + /// of 20 MiB that no free range fits (an unkeep), so that allocation is idle with free bytes, + /// newer than the class's first one. With the 20 MiB block given back too and nothing else + /// live, both long allocations are freed at their idle age, and the class keeps its unit + /// allocation as its reserve, unweighed. Nothing commits. + #[test] + fn every_device_frees_a_long_allocation_whose_block_s_bytes_are_free_at_its_idle_age() { + const MIB: u64 = 1 << 20; + let instance = crate::tests::validation::instance(&[]); + let devices = devices_of_the_cfgs_kind(&instance); + assert!(!devices.is_empty(), "no device of this cfg's kind"); + for (name, physical) in &devices { + let hook = Arc::new(Hook::default()); + let device = device_with(&hook, physical, name); + let core = device.core(); + let counters = device.counters(); + let unit = MemoryClass::Shared.block_len(); + let long = (device.take_block(MemoryClass::Shared, 64 * MIB)) + .unwrap_or_else(|error| panic!("{name}: a block of 64 MiB: {error}")); + let mut handed = vec![long.id]; + device.release_block(long).expect("a release"); + wait_until("the long block kept", || { + core.pool.kept(MemoryClass::Shared) == 1 + }); + // No free range fits 20 MiB: the take allocates, and asks for the kept block's bytes. + let other = (device.take_block(MemoryClass::Shared, 20 * MIB)) + .unwrap_or_else(|error| panic!("{name}: a block of 20 MiB: {error}")); + handed.push(other.id); + assert!( + core.pool.frees().is_empty(), + "{name}: the long allocation was freed before the take: the test, not the \ + runtime, was late" + ); + wait_until("the long block's bytes given back", || { + core.pool.kept(MemoryClass::Shared) == 0 && hook.unmapped().len() == 1 + }); + device.release_block(other).expect("a release"); + wait_until("the two long allocations freed", || { + core.pool.frees().len() == 2 && !core.pool.freeing() + }); + thread::sleep(Duration::from_nanos(2 * IDLE_AGE_NANOS)); + let gauges = counters.gauges(); + assert_eq!( + ( + gauges.blocks, + gauges.shared_idle_bytes, + gauges.shared_reserve_bytes, + gauges.shared_idle_age_nanos + ), + (ZERO_SOURCE + 2, unit, unit, 0), + "{name}: one unit allocation of each class, the Shared one the reserve" + ); + assert_eq!( + core.pool.frees().len(), + 2, + "{name}: no third free: the reserve stays" + ); + drop(device); + assert_eq!( + counters.gauges(), + Gauges::default(), + "{name}: gauges after teardown" + ); + assert_each_unmapped_once(name, &hook, &handed); + println!( + "memory: {name}: a long allocation with free bytes freed at its idle age; \ + metrics {:?}", + counters.metrics() + ); + } + } + /// The bucket of [`Metrics::idle_reuses_by_age`](crate::Metrics::idle_reuses_by_age) an idle /// time of `nanos` counts in: the runtime's upper bounds, 50, 100, 250, 500, 1000 and /// 2000 ms.