rt: the block-maker thread makes a class's next pooled block whatever dedicated blocks it keeps idle

Since given-back dedicated blocks are kept idle (208887e13a), a class's
idle list holds pooled and dedicated blocks, but the block-maker thread
still judged a class ready by the idle list's length. Pool::take hands
out only pooled blocks, so once a class kept READY or more dedicated
blocks and no pooled one, the maker slept while a pooled take waited for
it forever: replay's play of a recording stalled in Device::take_block
(job m3-max-44321, gdb stacks from the replay lane).

The maker now counts only the class's idle pooled blocks (State::ready),
as does the tests' Pool::idle, which waits for the ready ones. Take,
keep (the ready block is the latest pooled one), take_held and remaining
already told the two apart.

Test: every_device_makes_pooled_blocks_while_it_keeps_dedicated_ones
keeps a dedicated Shared block idle beside the ready block, then takes
two pooled blocks, each under a 30 s timeout; without the fix the second
take waits forever.
This commit is contained in:
Anees Iqbal committed 2026-11-17 21:44:43 -08:00
1 parent 363088e6c4
commit 8f4d6a1066
2 files changed
+75 -7

No files matched your search

+19 -7
View File
@@ -303,11 +303,11 @@ impl Pool {
lock(&self.state)
}
/// The zeroed blocks of `class` no one holds: the tests wait for the block-maker thread to
/// have made its ready ones, and with them its fills' submissions.
/// The zeroed pooled blocks of `class` no one holds: the tests wait for the block-maker thread
/// to have made its ready ones, and with them its fills' submissions.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn idle(&self, class: MemoryClass) -> usize {
self.lock().idle[class.index()].len()
self.lock().ready(class)
}
fn wait<'a>(&self, state: MutexGuard<'a, State>) -> MutexGuard<'a, State> {
@@ -446,6 +446,17 @@ impl Pool {
}
}
impl State {
/// The idle pooled blocks of `class`: those [`Pool::take`] hands out. The idle dedicated
/// blocks beside them serve only [`Pool::take_held`].
fn ready(&self, class: MemoryClass) -> usize {
self.idle[class.index()]
.iter()
.filter(|memory| memory.pooled)
.count()
}
}
fn number(state: &mut State) -> Result<BlockId, Error> {
let id = state.next_id;
state.next_id = id.checked_add(1).ok_or(Error::Limit(Limit::BlockIds))?;
@@ -484,7 +495,8 @@ pub(crate) fn stock(core: &Core) {
/// zeroes each block given back, pooled or dedicated, and keeps it as its class's latest idle
/// block, freeing through the hook the oldest kept blocks past the class's
/// [`MemoryClass::idle_bound`], the ready block aside (and at once a block longer than the bound,
/// or spilled); then it makes pooled blocks until each class has [`READY`] idle.
/// or spilled); then it makes pooled blocks until each class has [`READY`] pooled ones idle,
/// whatever dedicated ones it keeps.
pub(crate) fn run(core: Arc<Core>) {
let pool = &core.pool;
let mut state = pool.lock();
@@ -512,9 +524,9 @@ pub(crate) fn run(core: Arc<Core>) {
state = pool.lock();
continue;
}
let wanted = MemoryClass::ALL.into_iter().find(|class| {
state.idle[class.index()].len() < READY && state.failed[class.index()].is_none()
});
let wanted = MemoryClass::ALL
.into_iter()
.find(|class| state.ready(*class) < READY && state.failed[class.index()].is_none());
if let Some(class) = wanted {
let id = number(&mut state);
drop(state);
@@ -911,6 +911,62 @@ mod on_device {
);
}
}
/// A take of a pooled block while the class keeps only dedicated blocks idle: the block-maker
/// thread makes the class's next pooled block whatever dedicated blocks it keeps, so the take
/// returns. A Shared block of twice the pooled length is given back and kept beside the ready
/// block; the first pooled take takes the ready block, and the second waits only for the
/// block-maker thread to make the next.
#[test]
fn every_device_makes_pooled_blocks_while_it_keeps_dedicated_ones() {
let instance = Instance::new(&[]).expect("an instance at the floor");
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 counters = device.counters();
let shared_len = MemoryClass::Shared.block_len();
let dedicated = (device.take_block(MemoryClass::Shared, 2 * shared_len))
.unwrap_or_else(|error| panic!("{name}: a dedicated block: {error}"));
device.release_block(dedicated.id).expect("a release");
wait_until("the dedicated block kept beside the ready one", || {
counters.gauges().shared_idle_bytes == 3 * shared_len
});
let mut handed = vec![dedicated.id];
for take in 0..2 {
let (sender, receiver) = std::sync::mpsc::channel();
let taker = device.clone();
thread::spawn(move || {
let taken = taker.take_block(MemoryClass::Shared, 1);
drop(taker);
let _ = sender.send(taken);
});
let block = (receiver.recv_timeout(Duration::from_secs(30)))
.unwrap_or_else(|_| panic!("{name}: pooled take {take} waited 30 s"))
.unwrap_or_else(|error| panic!("{name}: pooled take {take}: {error}"));
assert_eq!(block.len, shared_len, "{name}: a pooled block");
assert_ne!(
block.id, dedicated.id,
"{name}: the dedicated block as pooled"
);
handed.push(block.id);
}
assert_eq!(counters.metrics().dedicated_blocks_reused, 0, "{name}");
drop(device);
assert_eq!(
counters.gauges(),
Gauges::default(),
"{name}: gauges after teardown"
);
handed.sort_unstable();
assert_each_unmapped_once(name, &hook, &handed);
println!(
"memory: {name}: two pooled takes beside a kept dedicated block; metrics {:?}",
counters.metrics()
);
}
}
/// With the device's `maxMemoryAllocationCount` answered as the zero source's, the two ready
/// blocks' and one more (`Device::open`), the count is reached once a texture takes the ready
/// Private block and the pool makes it again. Past it, a texture larger than a block, which has