diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index db449fb73..17b4ab2df 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -1262,21 +1262,20 @@ pub(super) struct Memory { /// The pool a color texture's host-transfer image places in, once asked: `Some(None)` where /// the device copies no color texture on the host ([`Memory::host_copy_pool`]). host_copy_pool: Option>, - /// The block of that pool being made ready, or made ready and not yet in it - /// (`memory/ready.rs`). + /// The block of that pool being made ready, or made ready and not yet in it: it enters the + /// pool at the first placement there that finds no room (`memory/ready.rs`). readying: Option, - /// Its blocks made ready ahead and added to it, the placements that waited for one, and the - /// blocks a placement in it made itself, finding none ready (`WorkCounts`). + /// Its blocks made ready ahead and added to it, and the blocks a placement in it made itself, + /// finding none ready (`WorkCounts`). host_copy_blocks: HostCopyBlocks, } /// The counts of the blocks of the pool host copies place in ([`Memory::host_copy_pool`]). #[derive(Clone, Copy, Debug, Default)] pub(super) struct HostCopyBlocks { - /// Made ready ahead off the program's thread and added to the pool (`memory/ready.rs`). + /// Made ready ahead off the program's thread and added to the pool by a placement that found + /// no room (`memory/ready.rs`). pub(super) readied: u64, - /// Placements that found no room and waited for the block being made ready. - pub(super) waits: u64, /// Blocks a placement made itself, finding no room and none being made ready. pub(super) made_in_place: u64, } @@ -2705,14 +2704,15 @@ fn place( { return Ok(guard.mark_overflowed(placement, overflowing)); } - // The pool host copies place in, without room: the block being made ready for it - // rather than one of the placement's own, whose first touch would wait instead - // (`memory/ready.rs`). + // The pool host copies place in, without room: the block made ready for it (waited + // for where its making is still under way) rather than one of the placement's own, + // whose first touch would wait instead (`memory/ready.rs`). Where another placement + // took it meanwhile, the block it added may hold this one. let readying = (guard.readying.as_ref()).is_some_and(|ready| ready.pool() == pool); drop(guard); - if readying && adopt_ready(memory, true) { + if readying { + adopt_ready(memory); let mut guard = lock(memory); - guard.host_copy_blocks.waits += 1; if let Some(placement) = guard.place_pooled(device, pool, (size, align), addressed, class)? { @@ -2775,9 +2775,10 @@ fn make_room(memory: &Mutex, device: &ash::Device, type_index: u32) -> b /// with room rather than making one. A block of buffers gets its buffer of addresses (counted in /// `counts`) where blocks carry one. A block that fails to be made, or a pool the query fails for, /// is left to the placement that needs it. The pool host copies place in, where `copies` says the -/// device copies color textures on the host, keeps its room in blocks made ready off this thread -/// through `functions` (`memory/ready.rs`): the one made ready is added once it is, and another -/// started where the pool lacks room. +/// device copies color textures on the host, keeps its room in a block made ready off this thread +/// through `functions` (`memory/ready.rs`), started where the pool lacks room and none is held: +/// the first placement there that finds no room adds it, so that the pool's blocks change only +/// at its placements, never as a making off the thread ends. pub(super) fn keep_room( memory: &Mutex, device: &ash::Device, @@ -2785,7 +2786,6 @@ pub(super) fn keep_room( functions: &Arc, copies: impl FnOnce() -> bool, ) { - adopt_ready(memory, false); ready_ahead(memory, device, functions, copies); let (lacking, maker) = { let mut memory = lock(memory); @@ -2822,7 +2822,7 @@ pub(super) fn keep_room( /// Start making a block ready for the pool host copies place in (`memory/ready.rs`, /// [`Memory::host_copy_pool`], where `copies`, asked with `memory`'s lock released until the pool /// was found, says the device copies color textures on the host), through `functions`, where it -/// lacks room for half a block and none is being made ready. +/// lacks room for half a block and holds no block made ready or being made. fn ready_ahead( memory: &Mutex, device: &ash::Device, @@ -2843,27 +2843,19 @@ fn ready_ahead( memory.readying = ready::Readying::start(maker, Arc::clone(functions), pool, bytes); } -/// Add the block made ready for the pool host copies place in to it (`memory/ready.rs`), where -/// its making finished, or where `wait`, once it finished, waited for with `memory`'s lock -/// released: whether a block was added. -fn adopt_ready(memory: &Mutex, wait: bool) -> bool { - let readying = { - let mut memory = lock(memory); - match &memory.readying { - Some(readying) if wait || readying.finished() => memory.readying.take(), - _ => None, - } - }; - let Some(readying) = readying else { - return false; +/// Add the block made ready for the pool host copies place in to it (`memory/ready.rs`), its +/// making waited for with `memory`'s lock released where it is still under way. Nothing where +/// another placement took it first, or its making failed. +fn adopt_ready(memory: &Mutex) { + let Some(readying) = lock(memory).readying.take() else { + return; }; let pool = readying.pool(); let Some(block) = readying.take() else { - return false; + return; }; let mut memory = lock(memory); let bytes = memory.block_bytes; memory.add_block(pool, block, bytes, false, 0); memory.host_copy_blocks.readied += 1; - true } diff --git a/crates/metal2vulkan-runtime/src/memory/ready.rs b/crates/metal2vulkan-runtime/src/memory/ready.rs index 692db7494..732ba607a 100644 --- a/crates/metal2vulkan-runtime/src/memory/ready.rs +++ b/crates/metal2vulkan-runtime/src/memory/ready.rs @@ -15,14 +15,15 @@ //! cleared, and the write took 160 us. //! //! - **Ahead.** `memory::keep_room`, which the device's creation calls run, starts a block's making -//! here once the pool lacks room for half a block and none is under way; the first is started by -//! the device's first creation call, before the program queued work. The block enters the pool -//! once every page was read: a placement never lands in memory whose pages the host has not -//! touched, so its host copies fault on none, whatever kernel work is pending then (a later -//! block's clear included). -//! - **Waited for only for room.** A placement that finds the pool without room waits for the block -//! under way, rather than making one whose first touch would wait instead (`memory::place`); -//! where none is under way it makes one as before (counted, `WorkCounts::host_copy_blocks_made_in_place`). +//! here once the pool lacks room for half a block and holds no block made ready or being made; +//! the first is started by the device's first creation call, before the program queued work. +//! No image is placed in the block before every page was read, so its host copies fault on none +//! of them, whatever kernel work is pending then (a later block's clear included). +//! - **Added at a placement.** The first placement in the pool that finds no room adds the block, +//! waiting for its making where it is still under way, rather than making one whose first touch +//! would wait instead (`memory::place`, `WorkCounts::host_copy_blocks_readied`); where none is +//! held it makes one as before (`WorkCounts::host_copy_blocks_made_in_place`). The pool's blocks +//! change only at its placements, never as a making here ends. //! - **One at a time,** on a thread of its own that ends with the block; the device's teardown //! joins it 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. @@ -71,11 +72,6 @@ impl Readying { self.pool } - /// Whether its block is ready, or its making failed: [`take`](Self::take) waits for nothing. - pub(super) fn finished(&self) -> bool { - self.thread.is_finished() - } - /// Its block, waited for: `None` where it could not be made. pub(super) fn take(self) -> Option { self.thread.join().ok().flatten() diff --git a/crates/metal2vulkan-runtime/src/tests/zeroed_start.rs b/crates/metal2vulkan-runtime/src/tests/zeroed_start.rs index 539304394..6409c834c 100644 --- a/crates/metal2vulkan-runtime/src/tests/zeroed_start.rs +++ b/crates/metal2vulkan-runtime/src/tests/zeroed_start.rs @@ -83,12 +83,11 @@ fn a_shared_texture_made_while_the_gpu_is_busy_waits_for_none_of_it_and_reads_ze queued ({each:?} each), the busy work {status:?} as it returned; started on the host \ {}, zeroed {}; zeroInitializeDeviceMemory {zeroed}; a second one's first replace_region \ took {wrote:?}, the busy work {status_after_write:?} after it; host-copy blocks readied \ - {}, waited for {}, made in place {}", + {}, made in place {}", device.name(), after.images_started_on_the_host - before.images_started_on_the_host, after.images_started_zeroed - before.images_started_zeroed, wrote_counts.host_copy_blocks_readied, - wrote_counts.host_copy_ready_waits, wrote_counts.host_copy_blocks_made_in_place, ); assert_ne!( diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index dbe8932eb..68e08107a 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -4400,19 +4400,15 @@ pub struct WorkCounts { /// Its live dedicated blocks: a placement larger than half a block, or one placed apart. pub dedicated_memory_blocks_live: u64, /// Of `pooled_memory_blocks_made`, the blocks of the pool a color texture's host-transfer - /// image places in made ready off the program's thread: allocated, every page of their - /// mapping read once by the host, then added to the pool, so that no host copy faults on - /// memory whose kernel work waits behind the program's queued GPU work (`memory/ready.rs`; - /// on RADV after runtime suspend, a new block's clear). None on a device that copies no - /// color texture on the host. + /// image places in made ready off the program's thread: allocated and every page of their + /// mapping read once by the host, then added to the pool by the first placement there that + /// found no room, so that no host copy faults on memory whose kernel work waits behind the + /// program's queued GPU work (`memory/ready.rs`; on RADV after runtime suspend, a new + /// block's clear). None on a device that copies no color texture on the host. pub host_copy_blocks_readied: u64, - /// The placements in that pool that found no room and waited for the block being made ready - /// (`memory/ready.rs`): at most one per block, the device's first host-transfer textures - /// among them where they came before its first block was ready. - pub host_copy_ready_waits: u64, - /// The blocks of that pool a placement made itself, finding no room and none being made - /// ready: their pages untouched, a host copy's first touch there may wait for the memory's - /// kernel work (`memory/ready.rs`). + /// The blocks of that pool a placement made itself, finding no room and none made ready: + /// their pages untouched, a host copy's first touch there may wait for the memory's kernel + /// work (`memory/ready.rs`). pub host_copy_blocks_made_in_place: u64, /// Every block its allocator freed so far, by what made it go (`MemoryFrees`): one /// `vkFreeMemory` each, and the allocator's whole share of @@ -6923,7 +6919,6 @@ impl Device { pooled_memory_blocks_freed: freed, dedicated_memory_blocks_live: census.dedicated, host_copy_blocks_readied: host_copy.readied, - host_copy_ready_waits: host_copy.waits, host_copy_blocks_made_in_place: host_copy.made_in_place, memory_frees: MemoryFrees { dedicated: frees[FreeCause::Dedicated as usize], diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index bb5bc817f..1a249023e 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2741,7 +2741,6 @@ field metal2vulkan-runtime::WorkCounts.held_depths_released pub held_depths_rele field metal2vulkan-runtime::WorkCounts.host_alias_lookups pub host_alias_lookups: u64 field metal2vulkan-runtime::WorkCounts.host_copy_blocks_made_in_place pub host_copy_blocks_made_in_place: u64 field metal2vulkan-runtime::WorkCounts.host_copy_blocks_readied pub host_copy_blocks_readied: u64 -field metal2vulkan-runtime::WorkCounts.host_copy_ready_waits pub host_copy_ready_waits: u64 field metal2vulkan-runtime::WorkCounts.host_deallocators_delayed pub host_deallocators_delayed: u64 field metal2vulkan-runtime::WorkCounts.host_holds_looked_up pub host_holds_looked_up: u64 field metal2vulkan-runtime::WorkCounts.host_import_fds_kept pub host_import_fds_kept: u64