rt: the device makes its first block of each class before it returns

The block-maker thread made the pool's first Shared and Private blocks after Device::new
returned, so their allocations ran beside the consumer's first calls. On NVIDIA's Linux driver
(620.70.06), a vkAllocateMemory of host-visible memory holds the device's other calls,
vkCreateComputePipelines and vkQueueSubmit2 among them, until it returns: a 256 MiB Shared
block's allocation took 39 to 56 ms on an RTX 3090, and a pipeline state created right after
Device::new waited 47 to 61 ms behind it (2.4 to 3.1 ms once the block was made in
Device::new; scratch probe f3c15f7ca3, job m3-max-43980).

Device::new now makes READY (one) block of each class on its own thread before it starts the
block-maker thread, which keeps one of each ready after that as before. Memory held is the
same; Device::new takes the two allocations and their zero fills longer.

Test: every_device_holds_a_block_of_each_class_when_it_is_made: as Device::new returns each
class holds a ready block and the blocks gauge counts them, and the block-maker thread makes
none in the 200 ms after.
This commit is contained in:
Anees Iqbal committed 2026-11-17 20:10:27 -08:00
1 parent d2fa098f0c
commit 5c9ca33c48
4 files changed
+75 -10

No files matched your search

+5 -2
View File
@@ -85,8 +85,11 @@ in its memory.
block's life) memory, as a `BlockInfo`: its number, class, length, device address and, for
Shared, its host address. One `VkBuffer` spans each block, with every buffer usage and a device
address. Nothing in a block is exportable.
- Up to `BLOCK_LEN` (256 MiB) a block is pooled. The block-maker thread keeps one block of each
class ready, made off every caller's path, so a take waits only when none is ready. Past
- Up to `BLOCK_LEN` (256 MiB) a block is pooled. `Device::new` makes one block of each class
before it returns, and the block-maker thread keeps one of each class ready after that, made off
every caller's path, so a take waits only when none is ready. No block is so made beside the
consumer's first calls: on an RTX 3090 (NVIDIA's Linux driver), a pipeline state's creation right
after `Device::new` waited 47 to 61 ms behind the allocation of the first Shared block's memory. Past
`BLOCK_LEN` a block is dedicated: the length rounded up to 64 KiB, made in the call, up to the
device's `maxMemoryAllocationSize` and `maxBufferSize` (`Limit::BlockLen` past them).
- A Private allocation takes the device-local Private type. Where that allocation itself fails
+7 -6
View File
@@ -267,9 +267,9 @@ impl Device {
/// of the consumer's extensions ([`DeviceOptions::extensions`]) the device offers with the
/// features the consumer needs, and chooses its memory types on it. It takes one queue of the floor's queue
/// family for the runtime and, where that family has a second queue, a queue for presenting;
/// where it has one, presenting shares the runtime's queue and its lock. It starts the
/// block-maker thread, which makes one block of each memory class ready, the submit thread and
/// the completion thread.
/// where it has one, presenting shares the runtime's queue and its lock. It makes one block of
/// each memory class ready, then starts the block-maker thread, which keeps one of each ready
/// after that, the submit thread and the completion thread.
///
/// Err: [`Error::BelowFloor`] with every item the device lacks; [`Error::Vulkan`] where a
/// Vulkan call fails; [`Limit::MemoryAllocations`](crate::Limit::MemoryAllocations) at a
@@ -476,6 +476,7 @@ impl Device {
};
// A thread that does not start drops the device, which stops those that did.
let core = &device.shared.core;
memory::stock(core);
let reaper = spawn("m2v-reaper", core, Role::Reaper, reaper::run)?;
lock(&device.shared.threads).reaper = Some(reaper);
let blocks = spawn("m2v-blocks", core, Role::Blocks, memory::run)?;
@@ -547,9 +548,9 @@ impl Device {
/// No Metal counterpart: takes a block of `class` memory at least `len` bytes long, for the
/// consumer to place buffers in.
///
/// For `len` up to [`BLOCK_LEN`] it is a pooled block. The block-maker thread makes pooled
/// blocks ahead of need, off every caller's path, so this call waits only when no block of
/// the class is ready. Past [`BLOCK_LEN`] it is a dedicated block, made in this call.
/// For `len` up to [`BLOCK_LEN`] it is a pooled block. The device's making makes one of each
/// class, and the block-maker thread makes the next ones ahead of need, off every caller's
/// path, so this call waits only when no block of the class is ready. Past [`BLOCK_LEN`] it is a dedicated block, made in this call.
///
/// Every block reads zero, as Metal's new buffers do, whether new or taken again: a Shared
/// block is zeroed through its mapping, which also makes each page resident before the block
+25 -1
View File
@@ -16,7 +16,8 @@ use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
/// this, and a dedicated one past it.
pub const BLOCK_LEN: u64 = 256 << 20;
/// Blocks of each class the block-maker thread keeps ready.
/// Blocks of each class the device's making makes ([`stock`]) and the block-maker thread keeps
/// ready after it.
const READY: usize = 1;
/// The most idle blocks of a class the pool keeps. A block released past them is freed.
@@ -359,6 +360,29 @@ fn number(state: &mut State) -> Result<BlockId, Error> {
Ok(BlockId(id))
}
/// In the device's making, before the block-maker thread starts: makes [`READY`] blocks of each
/// class, so the consumer's first block of each class is ready as the device is, and no block is
/// made beside the consumer's first calls. A driver may hold the device's other calls while it
/// allocates a block's memory: on an RTX 3090 (NVIDIA's Linux driver), a pipeline state's creation
/// right after the device's making waited 47 to 61 ms behind the block-maker thread's first Shared
/// block, against 2.4 to 3.1 ms once the block was made here. A class whose block cannot be made
/// keeps why for its next taker, as the block-maker thread does.
pub(crate) fn stock(core: &Core) {
for class in MemoryClass::ALL {
for _ in 0..READY {
let made = (core.pool.number()).and_then(|id| make(core, class, BLOCK_LEN, id, true));
let mut state = core.pool.lock();
match made {
Ok(memory) => state.idle[class.index()].push(memory),
Err(error) => {
state.failed[class.index()] = Some(error);
break;
}
}
}
}
}
/// The block-maker thread, until the device's teardown stops it. Off every caller's path, it
/// zeroes each pooled block given back and keeps it while its class has fewer than [`IDLE_MAX`]
/// idle, and frees the rest through the hook; then it makes blocks until each class has
+38 -1
View File
@@ -1,5 +1,6 @@
//! Memory blocks and buffers: the plan of a zero fill and a dedicated block's length; and, under a
//! device cfg, on every device of the cfg's kind, that blocks are handed out zeroed and buffers
//! device cfg, on every device of the cfg's kind, that the device holds a block of each class as it
//! is made, that blocks are handed out zeroed and buffers
//! view them, that a block is freed through the consumer's hook only once nothing uses it, that
//! the pool keeps two idle blocks of a class and frees the rest, and that teardown frees every
//! block with the gauges at zero.
@@ -577,6 +578,42 @@ mod on_device {
}
}
/// The device's making makes one block of each class before it returns: each class holds a
/// ready block as `Device::new` returns, made before the block-maker thread started, which
/// then makes none, each class having its ready block.
#[test]
fn every_device_holds_a_block_of_each_class_when_it_is_made() {
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 device = Device::new(physical, DeviceOptions::default())
.unwrap_or_else(|error| panic!("{name}: {error}"));
let core = device.core();
let counters = device.counters();
let made = counters.metrics().blocks_made;
assert_eq!(
(
core.pool.idle(MemoryClass::Private),
core.pool.idle(MemoryClass::Shared)
),
(1, 1),
"{name}: the ready blocks as the device returns"
);
assert_eq!(
counters.gauges().blocks,
ZERO_SOURCE + 2,
"{name}: the zero source and the two ready blocks"
);
thread::sleep(Duration::from_millis(200));
assert_eq!(
counters.metrics().blocks_made,
made,
"{name}: the block-maker thread made a block"
);
}
}
/// Past two idle blocks of a class, the pool frees what is given back through the hook; a
/// block it keeps is zeroed and handed out again with its number, address and mapping, with no
/// block made; and a Private block taken again reads zero too.