rt: a take finds a block given back before the block-maker thread has kept it

The create_64MiB regression (matrix 61f2799088: next_take 7-27x old on RADV, NVIDIA and the
Iris). Each create in the harness's loop takes a 64 MiB Shared block, which takes an
allocation of its own, and gives it back; the next create takes again straight away. A take
looked only at the kept blocks, and the block-maker thread keeps a given-back block only when it
gets to the queue. While the thread was inside a driver call (a 16 MiB vkAllocateMemory or a
vkFreeMemory, 3-70 ms on the NVIDIA cards), the take found nothing, allocated a fresh 64 MiB
on the guest thread (fresh pages, their first touch in the memset), and the block it missed
left its allocation idle and over the window's peak, to be freed: a free that kept the thread
busy for the next take. Shown by the new test at the parent of this change
(every_device_takes_a_long_block_given_back_again_at_once, draft at 30cc1a0b69): on the RTX
3090 (m3-max-45044) rounds 3-19 each allocated one and freed one; on the RX 550 (45043) round
16 did; lavapipe (45042) none.

- Pool::take, after the kept blocks, takes a block from the give-back queue that the thread
  would keep (not spilled) and of the length asked: its allocation stays in use throughout, so
  no bookkeeping changes but the reuse count and the window's note.
- The block-maker thread holds the lock from popping a kept block off the queue to pushing it
  on the kept blocks (it dropped it in between, with nothing to do for a kept block), so a take
  finds the block in one or the other.
- The test: 20 takes of a 64 MiB Shared block, each given a zeroed buffer and given back, each
  take straight after the give-back before: only the first allocates, 19 reuse, none is freed.
This commit is contained in:
Anees Iqbal committed 2026-11-18 01:04:47 -08:00
1 parent 4d803e2a33
commit c15cebf4d1
2 files changed
+48 -30

No files matched your search

+18 -5
View File
@@ -631,8 +631,9 @@ impl Pool {
Ok((len, base_align(len, requirements.alignment, self.granule)))
}
/// Takes a block of `class` of at least `len` bytes for `core`: a kept block of its length,
/// with its number, buffer, address and mapping; else a free range, the first that fits in the
/// Takes a block of `class` of at least `len` bytes for `core`: a kept block of its length, or
/// one given back that the block-maker thread would keep and has not kept yet, with its
/// number, buffer, address and mapping; else a free range, the first that fits in the
/// order the allocations were made, with a new number and buffer; else a range of an
/// allocation made here, of [`ALLOCATION_LEN`] or the block's length where that is longer.
/// Where the class keeps blocks of other lengths, that take also asks the block-maker thread to
@@ -668,6 +669,17 @@ impl Pool {
self.changed.notify_all();
return Ok(memory);
}
// A block given back that the block-maker thread has not kept yet, which it would
// keep: taken as it is, its allocation in use throughout. The thread can be inside a
// driver's allocation or free for milliseconds, and a take that allocated beside the
// block would leave its allocation idle and over the window's peak, to be freed.
let returned = (state.returned.iter())
.rposition(|memory| memory.class == class && memory.len == len && !memory.spilled);
if let Some(memory) = returned.and_then(|at| state.returned.remove(at)) {
self.counters.count(Metric::BlocksReused);
state.note(class, self.clock.seconds());
return Ok(memory);
}
if let Some(carve) = state.fit(class, len, align) {
let id = number(&mut state)?;
drop(state);
@@ -1055,15 +1067,16 @@ pub(crate) fn run(core: Arc<Core>) {
continue;
}
if let Some(memory) = state.returned.pop_front() {
drop(state);
// A block of a spilled allocation is not kept: its bytes go back to its allocation,
// and a spilled allocation that empties is the idle rule's next to free, so the next
// tries device-local memory again.
// tries device-local memory again. A kept block stays under the lock from the queue to
// the kept blocks, so a take finds it in one or the other.
let kept = !memory.spilled;
if !kept {
drop(state);
retire(&core, &memory);
state = pool.lock();
}
state = pool.lock();
let class = memory.class;
// The block's allocation was in use until now, however long ago it was taken.
state.note(class, pool.clock.seconds());
+30 -25
View File
@@ -1050,16 +1050,11 @@ mod on_device {
}
}
/// Idle allocations follow the window's peak down: four Shared blocks of 16 MiB taken beside
/// a small one, each with an allocation of its own, and given back are kept, their
/// allocations idle, while the most bytes in use over the last 10 seconds hold them, through a
/// Private take and give-back and the small block's give-back 5 seconds on. Once the clock has
/// moved 11 seconds, the small block's allocation is the reserve that keeps 8 MiB free, and the
/// window holds one allocation more than the bytes in use: the three oldest idle allocations
/// are freed, one at a time, after the hook of the block kept in each, on the block-maker
/// thread. Five seconds later, with no take or give-back, the window's peak falls to nothing
/// and the fourth is freed; the reserve stays. Nothing commits, so the device is quiet
/// throughout.
/// A long block given back is taken again at once by the next take of its length: a Shared
/// block of 64 MiB, which takes an allocation of its own, is taken, given a zeroed buffer, and
/// given back 20 times in a row, each take straight after the give-back before. Whether the
/// block-maker thread has kept the block yet or not (it may be inside a driver's allocation or
/// free), the take finds it: only the first take allocates, and nothing is freed.
#[test]
fn every_device_takes_a_long_block_given_back_again_at_once() {
const LEN: u64 = 64 << 20;
@@ -1070,35 +1065,45 @@ mod on_device {
let hook = Arc::new(Hook::default());
let device = device_with(&hook, physical, name);
let counters = device.counters();
let before = counters.metrics();
let mut made = Vec::new();
for round in 0..20 {
let before = counters.metrics();
let allocations = counters.metrics().consumer_allocations;
let block = (device.take_block(MemoryClass::Shared, LEN))
.unwrap_or_else(|error| panic!("{name}: round {round}: {error}"));
let buffer = (device.new_buffer(block.id, 0, LEN, Some(0)))
.unwrap_or_else(|error| panic!("{name}: round {round}: {error}"));
drop(buffer);
device.release_block(block).expect("a release");
let after = counters.metrics();
println!(
"long block: {name}: round {round}: consumer allocations +{}, made +{}, \
reused +{}, freed +{}; gauges {:?}",
after.consumer_allocations - before.consumer_allocations,
after.blocks_made - before.blocks_made,
after.blocks_reused - before.blocks_reused,
after.allocations_freed - before.allocations_freed,
counters.gauges()
);
made.push(after.consumer_allocations - before.consumer_allocations);
made.push(counters.metrics().consumer_allocations - allocations);
}
let after = counters.metrics();
assert_eq!(
made.iter().skip(1).sum::<u64>(),
0,
"{name}: allocations a take made, by round: {made:?}"
(made[0], made.iter().skip(1).sum::<u64>()),
(1, 0),
"{name}: allocations each take made: {made:?}"
);
assert_eq!(
(
after.blocks_reused - before.blocks_reused,
after.allocations_freed - before.allocations_freed
),
(19, 0),
"{name}: takes of the block given back, and allocations freed"
);
}
}
/// Idle allocations follow the window's peak down: four Shared blocks of 16 MiB taken beside
/// a small one, each with an allocation of its own, and given back are kept, their
/// allocations idle, while the most bytes in use over the last 10 seconds hold them, through a
/// Private take and give-back and the small block's give-back 5 seconds on. Once the clock has
/// moved 11 seconds, the small block's allocation is the reserve that keeps 8 MiB free, and the
/// window holds one allocation more than the bytes in use: the three oldest idle allocations
/// are freed, one at a time, after the hook of the block kept in each, on the block-maker
/// thread. Five seconds later, with no take or give-back, the window's peak falls to nothing
/// and the fourth is freed; the reserve stays. Nothing commits, so the device is quiet
/// throughout.
#[test]
fn every_device_frees_idle_allocations_as_the_window_s_peak_falls() {
let instance = Instance::new(&[]).expect("an instance at the floor");