diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index cd3338b0b..2635dca21 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -2669,7 +2669,13 @@ impl Memory { /// Of the blocks `pool`, its blocks of `bytes`, keeps made ahead ([`ahead_sizes`]), those /// `held` lacks (the bytes of each block made, being made and still to make): the larger - /// first. + /// first. Its own blocks are counted in bytes together with its room ([`Pool::room`]): none + /// lacks while those held and its room come to their worth ([`ahead_sizes`]'s count of + /// them) or more, and once they come to less, as many as bring those held to that worth. So a + /// block just taken from them, which leaves the pool a whole block of room, is made again + /// as soon as a placement takes any of that room, not only once the pool lacks room for a + /// whole block: the room left then is less than the next creation calls' placements may + /// take before the making ends (a 31.6 MiB texture after a 64 MiB block was taken). /// /// [`ahead_sizes`]: Self::ahead_sizes fn lacking(&mut self, pool: (Holds, u32), bytes: u64, held: &[u64]) -> Vec { @@ -2683,8 +2689,11 @@ impl Memory { // Its own blocks: those below its large placements' bytes, which those leave to them. let below = large.unwrap_or(u64::MAX); let have: u64 = held.iter().filter(|&&size| size < below).sum(); - let more = (count * bytes).saturating_sub(have) / bytes.max(1); - lacking.extend((0..more).map(|_| bytes)); + let worth = count * bytes; + if self.pool(pool.0, pool.1).room().saturating_add(have) < worth { + let more = worth.saturating_sub(have) / bytes.max(1); + lacking.extend((0..more).map(|_| bytes)); + } lacking } @@ -4617,6 +4626,7 @@ fn place( if let Some(placement) = guard.place_pooled(device, pool, (size, align), addressed, class)? { + ready_after_placing(&mut guard, pool, size); return Ok(guard.mark_overflowed(placement, overflowing)); } // A dedicated placement takes a kept dedicated block its size fits. @@ -4651,6 +4661,7 @@ fn place( if let Some(placement) = guard.place_pooled(device, pool, (size, align), addressed, class)? { + ready_after_placing(&mut guard, pool, size); return Ok(guard.mark_overflowed(placement, overflowing)); } } @@ -4734,9 +4745,11 @@ pub(super) fn keep_none(memory: &Mutex, device: &ash::Device) { /// (`memory/ready.rs`); only where that making fails or no thread starts is the block made here. 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. A pool that grew -/// within [`KEPT_EMPTY_FOR`] and lacks room for a whole block gets the blocks it lacks of a block's -/// worth made ahead ([`Memory::keep_ahead`]), through `functions`, made once a burst of `quiet`'s -/// queue uses ended; so does each pool of exportable buffers that grew so +/// within [`KEPT_EMPTY_FOR`], or is near its next block, gets the blocks it lacks of a block's +/// worth made ahead, counted in bytes with its room ([`Memory::lacking`], [`Memory::keep_ahead`]), +/// through `functions`, made once a burst of `quiet`'s queue uses ended (as it does after a +/// placement of a quarter of a block or more there, [`place`]); so does each pool of exportable +/// buffers that grew so /// (`allocate_exported`), whose room is otherwise left to its placements, which are the creation /// calls themselves. The pool host copies place in, where `copies` says the device /// copies color textures on the host, gets its blocks made ahead once it lacks room for half a @@ -4850,19 +4863,31 @@ pub(super) fn ahead_room(bytes: u64) -> u64 { /// Keep `pool`'s blocks made ahead (`memory/ready.rs`, [`Memory::keep_ahead`]), made once a burst /// of the device's queue uses ended, with a buffer of addresses where its blocks carry one, where -/// the pool grew within [`KEPT_EMPTY_FOR`] lacking room for a whole block, or, grown or not, is -/// near its next block ([`Pool::near_next`]). +/// the pool grew within [`KEPT_EMPTY_FOR`], or, grown or not, is near its next block +/// ([`Pool::near_next`]): those it lacks, its own blocks counted in bytes with its room +/// ([`Memory::lacking`]). fn ready_if_growing(memory: &mut Memory, pool: (Holds, u32)) { let bytes = memory.pooled_bytes(pool); let pool_of = memory.pool(pool.0, pool.1); let grew = (pool_of.grew(None)).is_some_and(|at| at.elapsed() < KEPT_EMPTY_FOR); - let near = pool_of.near_next(bytes); - if !(grew || near) || pool_of.has_room(bytes) { + if !grew && !pool_of.near_next(bytes) { return; } memory.keep_ahead(pool, bytes); } +/// After a placement of `size` bytes in `pool`'s blocks ([`place`]): where the pool's blocks are +/// made ahead and the placement took a quarter of a block or more, the room [`ahead_room`] leaves +/// the pool's next block's making before [`keep_room`]'s half block, those it lacks are kept +/// there and then ([`ready_if_growing`]), not at the next creation call's [`keep_room`], which +/// may need the block at once. A smaller placement leaves that to the next creation call. +fn ready_after_placing(memory: &mut Memory, pool: (Holds, u32), size: u64) { + let bytes = memory.pooled_bytes(pool); + if size >= bytes - ahead_room(bytes) && memory.makes_ahead(pool) { + ready_if_growing(memory, pool); + } +} + /// Keep blocks made ahead 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; [`Memory::keep_ahead`]), made @@ -4883,14 +4908,20 @@ fn ready_ahead(memory: &Mutex, device: &ash::Device, copies: impl FnOnce /// Add a block of `need` bytes or more made ahead for `pool` to it, `guard` held on `memory` /// (`memory/ready.rs`): at once where one is made; else once one is, waited for with the lock /// released ([`Memory::wait_for`]), and waited for again where another call took the block made -/// meanwhile. Whether one was added: not where no making serves the call, or the one waited for -/// failed. The lock is released as it returns. +/// meanwhile, or where the pool's thread ended without one. A thread ends so without a failure: +/// a making no call waited for as it began makes no block within the margin of the device's +/// allocations (`allocations.rs`'s `Allocations::room_ahead`), and its ending drops the sizes +/// queued behind it, the call's among them; the second wait is on a making the call waits for +/// from its start, which makes its block. Whether one was added: not where no making serves the +/// call, or the makings of both waits ended without its block. The lock is released as it +/// returns. fn take_ready<'a>( memory: &'a Mutex, mut guard: MutexGuard<'a, Memory>, pool: (Holds, u32), need: u64, ) -> bool { + let mut ended_without = false; loop { if guard.add_ready(pool, need) { return true; @@ -4903,7 +4934,10 @@ fn take_ready<'a>( let made = waiter.wait(); guard = lock(memory); if !made { - return guard.add_ready(pool, need); + if ended_without { + return guard.add_ready(pool, need); + } + ended_without = true; } } } diff --git a/crates/metal2vulkan-runtime/src/memory/ready.rs b/crates/metal2vulkan-runtime/src/memory/ready.rs index 9a97da968..cf88a9ea5 100644 --- a/crates/metal2vulkan-runtime/src/memory/ready.rs +++ b/crates/metal2vulkan-runtime/src/memory/ready.rs @@ -33,7 +33,7 @@ //! //! - **Ahead.** `memory::keep_room`, which the device's creation calls run, starts a making for a //! 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 +//! was added to it within `KEPT_EMPTY_FOR`), 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, 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 @@ -49,8 +49,17 @@ //! its own size (`memory::pooled_block_bytes`), one block of the largest such size besides //! them, which a call that needs one of the pool's own blocks leaves to those placements, so //! that a burst of textures that outruns the makings of the pool's own blocks does not take it -//! (`tests::blocks_ahead`). They are made one after another once a burst ended; those it lacks are queued after the blocks its thread still makes, or made on a -//! new thread where that ended. So each making stays as short as its block, and the pool's growth +//! (`tests::blocks_ahead`). Its own blocks are counted in bytes with its room +//! (`Memory::lacking`): it lacks them once those made ahead and the longest free range of its +//! blocks come to less than their worth, and then as many as bring those made ahead to it. So +//! the block a call just took, which leaves a whole block of room, is made again as soon as a +//! placement takes any of that room, not once the pool lacks room for a whole block: on an RX 550 +//! a 31.6 MiB texture placed in a 64 MiB block just taken left less than the half block the next +//! creation call keeps, and that call waited for a making it started itself +//! (`tests::blocks_ahead`'s mixed sizes, at textures 8, 16 and 24 of 32). A placement of a +//! quarter of a block or more starts the making there and then (`memory::place`), not at the +//! next creation call. They are made one after another once a burst ended; those it lacks are +//! queued after the blocks its thread still makes, or made on a new thread where that ended. So each making stays as short as its block, and the pool's growth //! still finds its blocks made: with one block of 16 MiB ahead at a time, a guest's scroll on a //! Radeon 780M whose blocks were made so made 82 to 114 blocks of images on its own threads and //! waited for 110 to 159 of those made ahead. It keeps them for as long as it stays near its @@ -79,7 +88,10 @@ //! the thread looks for a waiting call after it says it reads and before each run of reads. A //! call makes its own block (`WorkCounts::buffer_blocks_made_in_place`, //! `image_blocks_made_in_place`) only where no making serves it: a placement in the host copies' -//! pool while its thread reads; a call whose making failed, or whose host starts no thread; and +//! pool while its thread reads; a call whose makings failed twice (a making that ended without +//! its block, as one no call waited for as it began does within the allocation margin below, +//! dropping the sizes queued behind it, is waited for once more on a making the call waits for +//! from its start), or whose host starts no thread; and //! a placement before the device's first creation call, which gives the makings the device's //! functions and queues. A pool's blocks change only at those calls, never as a making here //! ends. diff --git a/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs b/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs index e2492e1f1..c92642398 100644 --- a/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs +++ b/crates/metal2vulkan-runtime/src/tests/blocks_ahead.rs @@ -405,22 +405,32 @@ const MIXED_ROUNDS: usize = 4; /// A pool of 64 MiB blocks whose placements take up to half a block each keeps its next block /// made ahead of the call that needs it: after the pool's first block, no creation call waits for -/// a block of images, and none makes one. On a Radeon 780M, `tests::reclaim_submit_bench`'s mixed -/// arm waited for 8 of its 13 blocks of images in its quiet phase, each making started only as -/// the call needed it. +/// a block of images, and none makes one. On an RX 550 the calls after a 31.6 MiB texture waited +/// for a making they started themselves (textures 8, 16 and 24) while the pool's blocks made +/// ahead were counted by whole blocks of room, and on a Radeon 780M +/// `tests::reclaim_submit_bench`'s mixed arm waited for 8 of its 13 blocks of images in its quiet +/// phase. Where the device's makings were slow and its blocks of images were made smaller +/// (`Memory::plain_block_made`, lavapipe), the first texture larger than them waits for its +/// block of its own size, which nothing foretold +/// (`placements_past_a_pool_s_small_blocks_find_a_block_of_their_size_made_ahead`); no other. #[test] fn mixed_sizes_find_their_blocks_made_ahead() { let scratch = Scratch::new("mixed"); let device = device(&scratch.0); let shared = device.shared(); let ahead = || crate::device::lock(&shared.memory).ahead_blocks(); + let plain = || crate::device::lock(&shared.memory).plain_block_bytes(); let mut held: Vec = Vec::new(); texture(&device, MIXED_SIDES[0], &mut held); let start = ahead(); - let mut waits = Vec::new(); + let (mut waits, mut first_large) = (Vec::new(), None); for at in 1..MIXED_SIDES.len() * MIXED_ROUNDS { + let (width, height) = MIXED_SIDES[at % MIXED_SIDES.len()]; + if first_large.is_none() && width * height * 4 > plain() { + first_large = Some(at); + } let before = ahead().images_waited_for; - texture(&device, MIXED_SIDES[at % MIXED_SIDES.len()], &mut held); + texture(&device, (width, height), &mut held); let after = ahead(); if after.images_waited_for > before { waits.push(at); @@ -445,10 +455,11 @@ fn mixed_sizes_find_their_blocks_made_ahead() { (end.images_ahead + end.images_in_place) - (start.images_ahead + start.images_in_place); println!( "BLOCKS-AHEAD {}: {} textures of mixed sizes in blocks of {} MiB; after the first, {blocks} \ - blocks of images, {} made ahead, {} waited for (at textures {waits:?}), {} in place", + blocks of images, {} made ahead, {} waited for (at textures {waits:?}; the first larger \ + than the blocks {first_large:?}), {} in place", device.name(), held.len(), - crate::device::lock(&shared.memory).plain_block_bytes() >> 20, + plain() >> 20, end.images_ahead - start.images_ahead, end.images_waited_for - start.images_waited_for, end.images_in_place - start.images_in_place, @@ -457,9 +468,12 @@ fn mixed_sizes_find_their_blocks_made_ahead() { end.images_in_place, start.images_in_place, "a creation call made a block of images itself" ); + let unforetold: Vec = (waits.iter().copied()) + .filter(|&at| Some(at) != first_large) + .collect(); assert!( - waits.is_empty(), - "creation calls waited for a block of images at textures {waits:?}" + unforetold.is_empty(), + "creation calls waited for a block of images at textures {unforetold:?}" ); } diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 27ddfd4cb..303c198e2 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -992,7 +992,10 @@ driver's. Each is open work. a block's worth of bytes made ahead (`memory/ready.rs`; the pools of exportable buffers, which hold the pictures, among them): as many of its blocks as one 64 MiB block holds, and while its placements larger than a plain pool's smaller blocks are recent, one block of their size - besides, one `vkAllocateMemory` each, right after a burst of the device's submissions ended. A + besides, one `vkAllocateMemory` each, right after a burst of the device's submissions ended. + Its own blocks are counted in bytes with its free room: they are made again once those made + ahead and its room come to less than a block's worth, a placement of a quarter of a block or + more starting their making at once rather than at the next creation call. A creation call that needs one while its making still waits for that end makes it at once and waits for it, as for a block made in place, and one that finds none of its size made waits for the ready thread to make it first. A pool near its next block keeps those blocks at rest for as