diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index 32a23960d..1cfbee1ab 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -139,14 +139,14 @@ //! //! - **Blocks made ahead** (`memory/ready.rs`). Each pool the encodings and commits place their //! own resources in ([`keep_room`]) and each pool of exportable buffers ([`allocate_exported`]) -//! gets a block made off the program's thread while it grows, and the pool a color texture's -//! host-transfer image places in ([`Memory::host_copy_pool`]) gets its blocks so: each made -//! right after a burst of the device's submissions ended, since a block's `vkAllocateMemory` -//! holds the device's submissions while it runs on RADV and on NVIDIA's driver, then every page -//! of its mapping read once before anything is placed in it, so that no host copy's first touch -//! faults on memory whose kernel work waits behind the program's queued GPU work. A call that -//! needs room takes the pool's block made ahead, or waits for its making, rather than make a -//! block of its own. +//! gets a block made off the program's thread while it grows or is near its next block, and the +//! pool a color texture's host-transfer image places in ([`Memory::host_copy_pool`]) gets its +//! blocks so: each made right after a burst of the device's submissions ended, since a block's +//! `vkAllocateMemory` holds the device's submissions while it runs on RADV and on NVIDIA's +//! driver, then every page of its mapping read once before anything is placed in it, so that no +//! host copy's first touch faults on memory whose kernel work waits behind the program's queued +//! GPU work. A call that needs room takes the pool's block made ahead, or waits for its making, +//! rather than make a block of its own. use super::buffer::usage; use super::device::lock; @@ -1244,17 +1244,28 @@ impl Pool { } /// When it last grew: a pooled block was added to it while it held another, at `now` where - /// `adding` names one being added. Blocks made ahead are made for a pool that grew - /// (`memory/ready.rs`). + /// `adding` names one being added. Blocks made ahead are made for a pool that grew, and for + /// one near its next block ([`Pool::near_next`], `memory/ready.rs`). pub(super) fn grew(&mut self, adding: Option) -> Option { if let Some(now) = adding { - if self.blocks.iter().flatten().any(|block| !block.dedicated) { + if self.holds_pooled() { self.grew = Some(now); } } self.grew } + /// Whether it holds a pooled block. + fn holds_pooled(&self) -> bool { + self.blocks.iter().flatten().any(|block| !block.dedicated) + } + + /// Whether it holds a pooled block, its blocks of `bytes`, and lacks room for [`ahead_room`] + /// of one: near its next block, which is made ahead (`memory/ready.rs`). + fn near_next(&mut self, bytes: u64) -> bool { + self.holds_pooled() && !self.has_room(ahead_room(bytes)) + } + /// Every block's payload, emptying the pool. pub(super) fn drain(&mut self) -> impl Iterator + '_ { self.room = None; @@ -2558,8 +2569,8 @@ impl Memory { } /// The pools of exportable buffers ([`Holds::Exports`], [`Holds::DmaBufs`]) that grew within - /// [`KEPT_EMPTY_FOR`] (`allocate_exported`'s pooled placements): those whose blocks are made - /// ahead while they grow, besides [`Memory::kept`]'s. + /// [`KEPT_EMPTY_FOR`] (`allocate_exported`'s pooled placements), or are near their next block + /// ([`Pool::near_next`]): those whose blocks are made ahead, besides [`Memory::kept`]'s. fn exported_growing(&mut self) -> Vec<(Holds, u32)> { let types = self.types.len() as u32; let pools = (0..types).flat_map(|type_index| { @@ -2567,8 +2578,12 @@ impl Memory { }); pools .filter(|&(holds, type_index)| { - let grew = self.pool(holds, type_index).grew(None); - grew.is_some_and(|at| at.elapsed() < KEPT_EMPTY_FOR) + let bytes = self.pooled_bytes((holds, type_index)); + let pool = self.pool(holds, type_index); + let grew = pool + .grew(None) + .is_some_and(|at| at.elapsed() < KEPT_EMPTY_FOR); + grew || pool.near_next(bytes) }) .collect() } @@ -3071,12 +3086,13 @@ impl Memory { } } // A block made ahead for a pool that stopped growing that long ago, where the pool has - // room for half a block (`memory/ready.rs`). + // room for three quarters of a block, so that it is no longer near its next block + // ([`ahead_room`], `memory/ready.rs`). let mut at = 0; while at < self.readying.len() { let (holds, type_index) = self.readying[at].pool(); let started = self.readying[at].started(); - let room = self.pooled_bytes((holds, type_index)) / 2; + let room = ahead_room(self.pooled_bytes((holds, type_index))); let pool = self.pool(holds, type_index); let since = pool.grew(None).map_or(started, |grew| grew.max(started)); let unused = now.saturating_duration_since(since) >= KEPT_EMPTY_FOR @@ -4587,10 +4603,18 @@ pub(super) fn keep_room( } } +/// The longest free range below which a pool whose blocks are made ahead gets its next block made +/// ahead, whether or not it grew lately, of a pool of blocks of `bytes` (`memory/ready.rs`): three +/// quarters of a block, a quarter of a block before the half block [`keep_room`] keeps. +pub(super) fn ahead_room(bytes: u64) -> u64 { + bytes - bytes / 4 +} + /// Start making a block ahead for `pool` (`memory/ready.rs`), through `functions`, made once a /// burst of `quiet`'s queue uses ended, with a buffer of addresses counted in `counts` where its -/// blocks carry one, where the pool grew within [`KEPT_EMPTY_FOR`], lacks room for a whole block -/// and holds no block made ahead. +/// blocks carry one, where the pool holds no block made ahead and grew within [`KEPT_EMPTY_FOR`] +/// lacking room for a whole block, or, grown or not, is near its next block +/// ([`Pool::near_next`]). fn ready_if_growing( memory: &mut Memory, pool: (Holds, u32), @@ -4601,7 +4625,8 @@ fn ready_if_growing( let ahead = memory.readying_at(pool).is_some(); let pool_of = memory.pool(pool.0, pool.1); let grew = (pool_of.grew(None)).is_some_and(|at| at.elapsed() < KEPT_EMPTY_FOR); - if !grew || pool_of.has_room(bytes) || ahead { + let near = pool_of.near_next(bytes); + if !(grew || near) || pool_of.has_room(bytes) || ahead { return; } let addressed = pool.0.buffers().then(|| Arc::clone(counts)); diff --git a/crates/metal2vulkan-runtime/src/memory/ready.rs b/crates/metal2vulkan-runtime/src/memory/ready.rs index 2b6a24c92..563e6be43 100644 --- a/crates/metal2vulkan-runtime/src/memory/ready.rs +++ b/crates/metal2vulkan-runtime/src/memory/ready.rs @@ -32,15 +32,23 @@ //! pool the encodings and commits place in once the pool grew (a second or later pooled block //! was added to it within `KEPT_EMPTY_FOR`) and lacks room for a whole block, so that a growth //! like a guest's scroll on the 780M (from 20 to 63 live blocks of 64 MiB in about 20 s, about 4 -//! a second) finds a block made when it needs one; and for the host copies' pool once that -//! lacks room for half a block, the first by the device's first creation call, before the -//! program queued work. A pool of exportable buffers, which the creation calls themselves place -//! in, gets its block started the same way once it grew, so that the creation call that finds -//! no room there takes a block made ahead rather than make one on the program's thread (on a -//! Radeon 780M a creation call that made such a 64 MiB block itself took 10.0 to 15.0 ms in -//! three of four phases of `tests::reclaim_submit_bench`, quiet and fragmented, and none took -//! over 0.3 ms with the blocks made ahead); none of its blocks is made by a creation call that -//! places nothing there. +//! a second) finds a block made when it needs one; and, grown or not, once the pool is near its +//! next block (`Pool::near_next`: it holds a pooled block and lacks room for three quarters of +//! one, `memory::ahead_room`, a quarter of a block before the half block `keep_room` keeps), so +//! that its first growth, and the first after a pause, finds one made too, where a guest's +//! scroll on a Radeon 780M made its buffers' pool's first growth in place, on the guest's +//! thread (`tests::reclaim_submit_bench`'s scroll arm: of each phase's 8 blocks, the first was +//! made in place in three runs out of three, quiet and fragmented, and none is now). Such a +//! pool keeps that block at rest for as long as it stays near its next one: one block at most a +//! pool, none for a pool with room for three quarters of a block. And for the +//! host copies' pool once that lacks room for half a block, the first by the device's first +//! creation call, before the program queued work. A pool of exportable buffers, which the +//! creation calls themselves place in, gets its block started the same way once it grew or is +//! near its next block, so that the creation call that finds no room there takes a block made +//! ahead rather than make one on the program's thread (on a Radeon 780M a creation call that +//! made such a 64 MiB block itself took 10.0 to 15.0 ms in three of four phases of +//! `tests::reclaim_submit_bench`, quiet and fragmented, and none took over 0.3 ms with the +//! blocks made ahead); none of its blocks is made by a creation call that places nothing there. //! - **Added where room is needed.** A creation call that finds such a pool without room for //! half a block (`memory::keep_room`), and a placement that finds no room (`memory::place`), //! add the pool's block made ahead once its making ended. Where the making is under way they @@ -60,7 +68,7 @@ //! - **Bounded.** One making a pool at most, on a thread of its own that ends with the block. //! While it waits to be added, its bytes count against its heap's budget of kept empty blocks //! (`Memory::kept_budget`); one whose pool stopped growing for `KEPT_EMPTY_FOR`, the pool -//! having room for half a block, goes (`Memory::sweep_stale`). The device's teardown stops a +//! having room for three quarters of a block, goes (`Memory::sweep_stale`). The device's teardown stops a //! making that has not begun, joins every one and frees its block. Its Vulkan calls run in a //! section of the exit guard (`exit_guard.rs`): once the process exits, no block is made. //! - **Counted** (`WorkCounts`): the blocks made ahead and added, by kind diff --git a/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs b/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs index e0d935c7b..95e3a2a6c 100644 --- a/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs +++ b/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs @@ -1,8 +1,8 @@ //! A pool the runtime's encodings and commits place their own resources in gets its blocks made -//! ahead, off the program's thread, while it grows (`memory/ready.rs`): once the pool grew, each -//! creation call that needs a new block there finds one made ahead and makes none itself, so no -//! `vkAllocateMemory` of a block runs on the program's thread, where on RADV and on NVIDIA's -//! driver it held the device's submissions beside it. +//! ahead, off the program's thread, while it grows or is near its next block (`memory/ready.rs`): +//! from its first growth on, each creation call that needs a new block there finds one made ahead +//! and makes none itself, so no `vkAllocateMemory` of a block runs on the program's thread, where +//! on RADV and on NVIDIA's driver it held the device's submissions beside it. //! //! The first run makes `Private` buffers of a guest's scroll, eight to a block, and waits for every //! making to end between two creations: what it holds is where the blocks are made, not how soon @@ -91,7 +91,7 @@ fn make(device: &Device, held: &mut Vec) -> WorkCounts { } #[test] -fn a_growing_pool_s_new_blocks_are_made_ahead_and_none_in_place() { +fn a_growing_pool_s_new_blocks_its_first_growth_s_among_them_are_made_ahead_and_none_in_place() { let scratch = Scratch::new("growth"); let device = device(&scratch.0); // The pools the runtime keeps room in are known before the first creation: those of blocks @@ -100,7 +100,7 @@ fn a_growing_pool_s_new_blocks_are_made_ahead_and_none_in_place() { let _settled = device.settle_submit_checks(Duration::from_secs(60)); let mut held = Vec::new(); // The first creation makes each pool's first block; the buffers' pool's first growth past it - // is made in place, as nothing grew before it. Other pools' blocks are not counted. + // is made ahead, the pool near its next block before it. Other pools' blocks are not counted. let start = make(&device, &mut held); let mut counts = start; while grown(&start, &counts) == 0 { @@ -113,14 +113,19 @@ fn a_growing_pool_s_new_blocks_are_made_ahead_and_none_in_place() { let in_place = counts.buffer_blocks_made_in_place - before.buffer_blocks_made_in_place; let ahead = counts.buffer_blocks_made_ahead - before.buffer_blocks_made_ahead; let waited_for = counts.blocks_ahead_waited_for - before.blocks_ahead_waited_for; + let first_in_place = before.buffer_blocks_made_in_place - start.buffer_blocks_made_in_place; + let first_ahead = before.buffer_blocks_made_ahead - start.buffer_blocks_made_ahead; println!( "BLOCKS-AHEAD {}: {} buffers of {BUFFER_BYTES} bytes; over the buffers' pool's last {} \ blocks, {ahead} made ahead, {in_place} in place, {waited_for} waited for; its first \ - growth made {} in place", + growth made {first_ahead} ahead, {first_in_place} in place", device.name(), held.len(), grown(&before, &counts), - before.buffer_blocks_made_in_place - start.buffer_blocks_made_in_place, + ); + assert_eq!( + first_in_place, 0, + "the pool's first growth was made by a creation call itself" ); assert_eq!( in_place, 0, diff --git a/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs b/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs index 658a5cbb3..3972b564b 100644 --- a/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs +++ b/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs @@ -38,6 +38,12 @@ //! holds its verdicts knows them as it is made, where the first starts no check (a buffer's //! making starts none), its blocks plain, as a driver's first device's are until a texture over //! host pages starts the check and it answers. +//! - **A scroll's buffers, made by the runtime**: a `Private` buffer of [`SCROLL_BUFFER_BYTES`], +//! as a guest's scroll's memory log placed them, eight to a pooled block, every +//! [`SCROLL_EVERY`], at most [`SCROLL_BUFFERS`] a phase: the runtime's pool of buffers grows a +//! block every eight creation calls, its first growth of each phase (the first after the +//! fragmenting's pause) among them, so that a block a creation call made itself shows as that +//! call's making. //! - **Exportable, made by the runtime**: an exportable `Private` buffer of //! [`RUNTIME_BUFFER_BYTES`] (`Device::new_exportable_buffer`) every [`RUNTIME_EVERY`], at most //! [`RUNTIME_BUFFERS`] a phase: the pool of exportable buffers grows, its blocks made ahead @@ -140,6 +146,10 @@ const GIB: u64 = 1 << 30; const RUNTIME_BUFFER_BYTES: u64 = 32 * MIB; const RUNTIME_BUFFERS: usize = 24; const RUNTIME_EVERY: Duration = Duration::from_millis(400); +/// A guest's `Private` buffer, as a scroll's memory log placed them: eight to a pooled block. +const SCROLL_BUFFER_BYTES: u64 = 7_471_104; +const SCROLL_BUFFERS: usize = 64; +const SCROLL_EVERY: Duration = Duration::from_millis(30); /// The images arm's textures: `RGBA8Unorm` of this side (4 MiB), one a frame at 60 Hz, at most /// this many a phase (768 MiB). const IMAGE_SIDE: u64 = 1024; @@ -429,6 +439,8 @@ fn monotonic() -> f64 { enum Making { /// The runtime's `Private` buffers (the module docs). Runtime, + /// The runtime's `Private` buffers of a guest's scroll (the module docs). + Scroll, /// The runtime's exportable `Private` buffers (the module docs). Exportable, /// The runtime's `Private` textures (the module docs). @@ -463,6 +475,7 @@ fn make( let mut times = Vec::new(); let (every, most) = match making { Making::Runtime | Making::Exportable => (RUNTIME_EVERY, RUNTIME_BUFFERS), + Making::Scroll => (SCROLL_EVERY, SCROLL_BUFFERS), Making::Images => (IMAGE_EVERY, IMAGE_TEXTURES), Making::ImagesMixed => (IMAGE_EVERY, MIXED_TEXTURES), Making::Raw { bytes, .. } => ( @@ -480,6 +493,11 @@ fn make( .new_buffer(RUNTIME_BUFFER_BYTES, ResourceOptions::private()) .unwrap(), ), + Making::Scroll => made.buffers.push( + device + .new_buffer(SCROLL_BUFFER_BYTES, ResourceOptions::private()) + .unwrap(), + ), Making::Exportable => made.buffers.push( device .new_exportable_buffer(RUNTIME_BUFFER_BYTES, ResourceOptions::private()) @@ -657,6 +675,8 @@ enum Arm { }, /// The runtime's exportable buffers. Exportable, + /// The runtime's buffers of a guest's scroll. + Scroll, /// The runtime's textures. Images, /// The runtime's textures of mixed sizes. @@ -705,6 +725,7 @@ fn arm(label: &str, arm: Arm) { Making::Runtime } Arm::Exportable => Making::Exportable, + Arm::Scroll => Making::Scroll, Arm::Images => Making::Images, Arm::ImagesMixed => Making::ImagesMixed, Arm::SystemHeap { bytes, export } => Making::Raw { @@ -1200,6 +1221,12 @@ fn reclaim_submit_bench_runtime_settled() { ); } +#[test] +#[ignore = "a measurement that presses the host's memory: run alone on its host by name"] +fn reclaim_submit_bench_scroll() { + arm("a scroll's buffers, made by the runtime", Arm::Scroll); +} + #[test] #[ignore = "a measurement that presses the host's memory: run alone on its host by name"] fn reclaim_submit_bench_images() { diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 721170e70..830f29c21 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -987,11 +987,14 @@ driver's. Each is open work. waited 1 ms (longest 0.39 to 0.67 ms), 16 empty blocks (1 GiB) kept between the clips. A first clip still makes its blocks: 16 allocations, against 44 when each picture had an allocation of its own, with 3 to 5 commits waiting 1 ms or more (longest 10.9 to 19.6 ms): its blocks' - `vkAllocateMemory` holds every submission. A pool that grew gets its next block made ahead - (`memory/ready.rs`; the pools of exportable buffers, which hold the pictures, among them), right - after a burst of the device's submissions ended, and a creation call that needs it while its - making still waits for that end makes it at once and waits for it, as for a block made in place. - Metal's command buffers wait for no allocation. Not announced. + `vkAllocateMemory` holds every submission. A pool that grew, and one near its next block (it + holds a pooled block and lacks room for three quarters of one, its first growth among them), gets + its next block made ahead (`memory/ready.rs`; the pools of exportable buffers, which hold the + pictures, among them), right after a burst of the device's submissions ended, and a creation + call that needs it while its making still waits for that end makes it at once and waits for it, + as for a block made in place. A pool near its next block keeps that block at rest for as long as + it stays so: one block a pool at most (64 MiB, or a plain pool's smaller block), counted against + the kept empty blocks' budget. Metal's command buffers wait for no allocation. Not announced. - **With unified memory, `Private` memory lies in the system heap's types before the device-local ones, its buffers' blocks there allocated exportable but for buffers that declare their padding unobserved: about 1 µs a submission for each such block held.** Where the device has unified memory and a heap that is not