Read the kept-block counts once the blocks made ahead are made

What: tests::memory_frees::a_buffer_with_a_block_of_its_own_keeps_it_for_the_next_of_its_size
failed once in 22 runtime-suite runs on the Iris Xe (ms01), "left 5, right 4". The device's
first creation call (the test's first new_buffer) starts blocks made ahead on a thread of their
own (memory/ready.rs), each a vkAllocateMemory that the device's counts hold, and the test
reads object_churn.vk_memory_allocations before and after the next buffer of its size to say
that buffer allocated nothing. Under load a making still pending at the first reading landed
between the two: "the next buffer of its size took the kept block", left 5, right 4. The test
now waits until no block is being made ahead (Memory::readying_ended, as tests::zeroed_start
does) before its readings.

Verified on the Iris Xe (ms01, Mesa 26.2.6, --cfg m2v_gpu, jobs m3-max-13493 and 13525):
- Not reproduced as committed: 0 of 40 alone and 0 of 568 beside a runtime-suite run. A scratch
  probe of the counts at each reading showed readying_ended false at all three, and
  vk_memory_allocations at 5 in a run alone against 4 beside the suite: a making ahead still
  pending through the whole test.
- Negative control, a scratch copy of the old test with that pending making let land between
  the two readings (a wait for readying_ended there): failed 6 of 40 alone and 145 of 222
  beside the suite, every time with this commit's message, "the next buffer of its size took
  the kept block", left 5, right 4 (allocations 4 at the first reading, 5 at the second).
- This test: 0 of 40 alone, 0 of 222 beside the suite (2029 of 2029 passed).

(cherry picked from commit 448c3ce0a20d02a00d4156c5f5d481abc6425d13)
This commit is contained in:
Anees Iqbal committed 2026-11-10 03:16:30 -08:00
1 parent ab281552db
commit 7c5a6901aa
1 file changed
+24 -2
@@ -4,7 +4,8 @@
//! 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 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
//! budget of empty blocks spares it (read once no block is being made ahead, below), 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.
@@ -16,6 +17,10 @@ use super::freed_memory::{device, unused, Host, Runs};
use crate::work_counts::MemoryFrees;
use crate::{Device, ResourceOptions};
use std::sync::Arc;
use std::time::{Duration, Instant};
/// The longest the blocks made ahead may take to be made.
const WAIT: Duration = Duration::from_secs(60);
/// A scratch directory for a device's shader cache, removed when dropped.
struct Scratch(std::path::PathBuf);
@@ -40,6 +45,21 @@ impl Drop for Scratch {
/// (`recycle.rs`'s `BUFFERS`): its release frees its block there and then.
const OWN_BLOCK: u64 = 128 << 20;
/// Wait until no block is being made ahead for `device` (`memory/ready.rs`): the device's first
/// creation call starts makings on a thread of their own, each a `vkAllocateMemory` the device's
/// counts hold, and under load one landed between two readings of a test that counts its own
/// allocations (the Iris Xe, once in 22 runs of the runtime suite).
fn made_ahead(device: &Device) {
let at = Instant::now();
while !crate::device::lock(&device.shared().memory).readying_ended() {
assert!(
at.elapsed() < WAIT,
"a block made ahead still making after {WAIT:?}"
);
std::thread::yield_now();
}
}
/// What `device` has freed so far, by cause, and the process's `vkFreeMemory` calls.
fn frees(device: &Device) -> (MemoryFrees, u64) {
let counts = device.work_counts();
@@ -83,7 +103,8 @@ 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.
/// none: the block is kept, and the next buffer of its size takes it with no allocation. Read once
/// the blocks the first creation call started making ahead are made ([`made_ahead`]).
#[test]
fn a_buffer_with_a_block_of_its_own_keeps_it_for_the_next_of_its_size() {
let scratch = Scratch::new("kept");
@@ -91,6 +112,7 @@ fn a_buffer_with_a_block_of_its_own_keeps_it_for_the_next_of_its_size() {
let buffer = device
.new_buffer(OWN_BLOCK, ResourceOptions::private())
.unwrap();
made_ahead(&device);
let (before, _) = frees(&device);
let counts = device.work_counts();
drop(buffer);