diff --git a/crates/metal2vulkan-runtime/src/command_buffer/fold.rs b/crates/metal2vulkan-runtime/src/command_buffer/fold.rs index aaccc4e24..5a349b384 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/fold.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/fold.rs @@ -143,16 +143,15 @@ impl State { /// predication laid a loop or left work out, `Predication::alone`), the failure fails that /// whole group instead. With /// `failure`, the encoders that run of each group split, the units to encode again. The logs - /// held for the commit that no split takes come back, for the caller to give back to the - /// device's spares without this lock. + /// held for the commit that no split takes stay held, for the command buffer's release to + /// give back to the device's spares ([`unsplit_logs`](Self::unsplit_logs)). pub(super) fn split_failure( &mut self, failure: Option, - ) -> (Option, Vec>, Vec) { + ) -> (Option, Vec>) { let mut units = Vec::new(); let Some(mut failure) = failure else { - let held = self.held_logs.drain(..).map(|held| held.log).collect(); - return (None, units, held); + return (None, units); }; let groups: BTreeSet<(usize, usize)> = (self.ended.iter()) .filter_map(|record| record.group.as_ref()) @@ -199,8 +198,14 @@ impl State { if !whole.is_empty() { failure.encoders = failure.encoders.with(&whole, self.ended.len()); } - let held = self.held_logs.drain(..).map(|held| held.log).collect(); - (Some(failure), units, held) + (Some(failure), units) + } + + /// The logs held for the commit that no split took (`split_failure`): for the command + /// buffer's release to give back to the device's spares without this lock, on its queue's + /// handler thread, as it gives back the logs its encoders kept (`replay.rs`). + pub(super) fn unsplit_logs(&mut self) -> Vec { + self.held_logs.drain(..).map(|held| held.log).collect() } /// `encoders`, a group's encoders the commit split that run (`split_failure`), whose diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index 27437706f..ba0839c3a 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -830,8 +830,9 @@ pub(super) struct State { /// compute encoder may join, recording on into it (`encoder_fold.rs`); held by that encoder /// while it is open. pub(super) fold: Option>, - /// The logs of the closed groups' encoders kept for nothing else, held until the commit, - /// whose failure may split a group (`fold.rs`'s `split_failure`). + /// The logs of the closed groups' encoders kept for nothing else, held for the commit, whose + /// failure may split a group (`fold.rs`'s `split_failure`), and given back to the device's + /// spares in the release. pub(super) held_logs: Vec, /// The encoders that run of each group the commit's failure split, which its call encodes /// again before the first submission (`reencode.rs`). @@ -3848,7 +3849,7 @@ impl CommandBuffer { for log in unkept { device.shared().scheduler.spare_logs().give(log); } - let (segments, places, failure, reached, hangs, (split, held)): ( + let (segments, places, failure, reached, hangs, split): ( Vec<(Deferred, bool)>, Vec, _, @@ -3972,7 +3973,7 @@ impl CommandBuffer { // A group of compute encoders in one `VkCommandBuffer` the failure fails in part is // split: its encoders that run are encoded again apart before the first submission // (`encoder_fold.rs`'s **Failed**). - let (failure, split, held) = state.split_failure(failure); + let (failure, split) = state.split_failure(failure); // The encoders that run: all but those `failure` fails. let runs = |encoder: usize| Failure::runs(failure.as_ref(), encoder); state.committing = true; @@ -4016,11 +4017,8 @@ impl CommandBuffer { .dirty .committed(&mut lock(&self.0.dirty), &held, waits, unnoted); } - (segments, places, failure, reached, hangs, (split, held)) + (segments, places, failure, reached, hangs, split) }; - for log in held { - device.shared().scheduler.spare_logs().give(log); - } // What the commands of its encoders that run use is resident from its commit on, as // Metal's is: a no-copy buffer or a heap among it is reached unnamed from here, by its // own commands too, the work recorded below included (`live_buffers.rs`). Listed without @@ -6453,8 +6451,9 @@ impl CommandBufferShared { }; drop(resource); } - // Its encoders' logs go here too, back to the device's spares (`replay.rs`): they - // hold what their calls named, which Metal holds until the command buffer completed + // Its encoders' logs go here too, those they kept and those held for its commit + // (`fold.rs`), back to the device's spares (`replay.rs`): they hold what their calls + // named, which Metal holds until the command buffer completed // (a binding replaced before any command among it), and no thread but this one may // hold such a handle past the completion. let logs: Vec> = (lock(&self.state).ended.iter_mut()) @@ -6464,6 +6463,8 @@ impl CommandBufferShared { for log in logs { spares.give_last(log); } + let held = lock(&self.state).unsplit_logs(); + spares.give_held(held); let residency = lock(&self.state).residency.take(); drop(residency); let residencies = lock(&self.state).let_go_of_uses(); diff --git a/crates/metal2vulkan-runtime/src/encoder_fold.rs b/crates/metal2vulkan-runtime/src/encoder_fold.rs index e25eff693..a9524d6db 100644 --- a/crates/metal2vulkan-runtime/src/encoder_fold.rs +++ b/crates/metal2vulkan-runtime/src/encoder_fold.rs @@ -50,12 +50,16 @@ //! predication laid a loop or left work out, so a command of theirs encoded again would run //! outside its block: `Predication::alone`; or a call answers otherwise when encoded again), //! the failure fails the whole group (docs/API.md, "A command buffer committed as failed"). -//! - **Logs.** A group's members' logs are held from the group's close until the commit, which -//! keeps them for every member where one member keeps its own for a rebind, keeps those of a -//! group its failure splits, and gives the rest back to the device's spares. A log holds one +//! - **Logs.** A group's members' logs are held from the group's close for the commit, which +//! keeps them for every member where one member keeps its own for a rebind, and keeps those of +//! a group its failure splits; the rest stay held until the command buffer's release, which +//! gives them back to the device's spares under one lock on its queue's handler thread +//! (`SpareLogs::give_held`), as it gives back the logs its encoders kept. The spares keep as +//! many as it gave back within 1 MiB of room, so a next command buffer's encoders reuse them +//! rather than allocate. A log holds one //! `ComputeCall` per call (at most 64 bytes, `tests::encoder_fold`) besides the bytes and handles //! its slices take in its arenas: a one-dispatch encoder's five calls (state, three buffers, the -//! dispatch) hold about 320 bytes, so 2,300 such encoders hold about 0.7 MiB until the commit. +//! dispatch) hold about 320 bytes, so 2,300 such encoders hold about 0.7 MiB until their command buffer completes. use crate::key_hash::KeyMap; use crate::replay::Replay; @@ -248,7 +252,7 @@ pub(crate) fn joined(own: Option, fold: Option) -> Option) -> Option { + /// Its lists emptied for a next pass's log, its descriptor, calls and handles let go of, with + /// their room in bytes: `None` where either list's is more than `SPARE_LOG_BYTES` + /// (`CallLog::cleared`). + pub(crate) fn cleared(self: Box) -> Option<(RenderRoom, usize)> { let RenderLog { descriptor, calls, @@ -95,14 +96,13 @@ impl RenderLog { .. } = *self; drop(descriptor); - if indexed.capacity() * std::mem::size_of::() > SPARE_LOG_BYTES { + let indexed_room = indexed.capacity() * std::mem::size_of::(); + if indexed_room > SPARE_LOG_BYTES { return None; } indexed.clear(); - Some(RenderRoom { - calls: calls.cleared()?, - indexed, - }) + let (calls, calls_room) = calls.cleared()?; + Some((RenderRoom { calls, indexed }, calls_room + indexed_room)) } /// The bytes it holds. diff --git a/crates/metal2vulkan-runtime/src/replay.rs b/crates/metal2vulkan-runtime/src/replay.rs index 4cf0b4a9e..5fa6adcd9 100644 --- a/crates/metal2vulkan-runtime/src/replay.rs +++ b/crates/metal2vulkan-runtime/src/replay.rs @@ -47,8 +47,9 @@ //! 27.0.1, probe `dealloc-probe`). And no other thread may hold such a handle past the //! completion: a no-copy buffer's deallocator would run where it went, on the scheduler's thread //! for one, whose own hold of the command buffer keeps the deallocator's wait for it from -//! returning (`scheduler::Spent`). A log let go of in the release, or at its encoder's end -//! where it is not kept, is cleared (its calls and handles go then) and its room kept for the +//! returning (`scheduler::Spent`). A log let go of in the release (a group of compute +//! encoders' held for the commit among them, `encoder_fold.rs`), or at its encoder's end where +//! it is not kept, is cleared (its calls and handles go then) and its room kept for the //! device's next encoders ([`SpareLogs`]), so the encoding thread's logs reuse their memory //! rather than allocate what another thread frees. //! - **Notices.** A call encoded again announces no kind its first encoding announced: each kind @@ -478,9 +479,19 @@ impl CallLog { } /// The log emptied for another encoder, its calls and handles let go of and its lists' room - /// kept: `None` where that room is more than [`SPARE_LOG_BYTES`]. - pub(super) fn cleared(mut self) -> Option> { - let room = self.calls.capacity() * std::mem::size_of::() + /// kept, with that room in bytes: `None` where it is more than [`SPARE_LOG_BYTES`]. + pub(super) fn cleared(mut self) -> Option<(CallLog, usize)> { + let room = self.room(); + if room > SPARE_LOG_BYTES { + return None; + } + self.clear(); + Some((self, room)) + } + + /// The room of its lists, in bytes. + fn room(&self) -> usize { + self.calls.capacity() * std::mem::size_of::() + self.bytes.capacity() + self.buffers.capacity() * std::mem::size_of::>() + self.words.capacity() * std::mem::size_of::() @@ -490,24 +501,25 @@ impl CallLog { + self.held_samplers.room() + self.held_render_states.room() + self.held_depth_stencils.room() - + self.refused.capacity() * std::mem::size_of::(); - if room > SPARE_LOG_BYTES { - return None; - } - self.clear(); - Some(self) + + self.refused.capacity() * std::mem::size_of::() } } -/// How many cleared logs of each kind of encoder the device keeps ([`SpareLogs`]). +/// How many cleared logs of each kind of encoder the device keeps, at least ([`SpareLogs`]). const SPARE_LOGS: usize = 16; /// The most room a cleared log keeps for a next encoder, in bytes; a log with more goes. pub(super) const SPARE_LOG_BYTES: usize = 64 * 1024; -/// Cleared logs the device keeps for its next encoders (the module docs): at most [`SPARE_LOGS`] -/// of each kind, each with at most [`SPARE_LOG_BYTES`] of room, and no call or handle. An -/// encoder that logs takes one, or a new log where none is kept; its log comes back as its +/// The most room the cleared logs of one kind the device keeps take together, in bytes: what +/// [`SPARE_LOGS`] logs of [`SPARE_LOG_BYTES`] each take. +const SPARE_ROOM_BYTES: usize = SPARE_LOGS * SPARE_LOG_BYTES; + +/// Cleared logs the device keeps for its next encoders (the module docs): of each kind, +/// [`SPARE_LOGS`], or for compute encoders as many as the last command buffer that held logs +/// for its commit gave back ([`give_held`](Self::give_held)) where that is more; each with at most [`SPARE_LOG_BYTES`] of +/// room, all of a kind together with at most [`SPARE_ROOM_BYTES`] (1 MiB), and no call or handle. +/// An encoder that logs takes one, or a new log where none is kept; its log comes back as its /// encoder ends where its command buffer does not keep it, and else in the command buffer's /// release, on its queue's handler thread. #[derive(Default)] @@ -518,25 +530,67 @@ pub(crate) struct SpareLogs { } /// One kind's [`SpareLogs`]. -struct Spares(Mutex>); +struct Spares(Mutex>); + +/// The logs one kind's [`Spares`] keeps. +struct Kept { + /// Each log with its room, in bytes. + logs: Vec<(T, usize)>, + /// Their room together, in bytes. + room: usize, + /// How many it keeps at most: [`SPARE_LOGS`], or what the last command buffer that held logs + /// for its commit gave back where that is more. + most: usize, +} impl Default for Spares { fn default() -> Spares { - Spares(Mutex::new(Vec::new())) + Spares(Mutex::new(Kept { + logs: Vec::new(), + room: 0, + most: SPARE_LOGS, + })) + } +} + +impl Kept { + /// Keep `spare`, of `room` bytes, where it keeps fewer than it may and their room stays within + /// [`SPARE_ROOM_BYTES`]; else hand it back. + fn keep(&mut self, spare: T, room: usize) -> Option { + if self.logs.len() >= self.most || self.room + room > SPARE_ROOM_BYTES { + return Some(spare); + } + self.room += room; + self.logs.push((spare, room)); + None } } impl Spares { fn take(&self) -> Option { - lock(&self.0).pop() + let mut kept = lock(&self.0); + let (spare, room) = kept.logs.pop()?; + kept.room -= room; + Some(spare) } - /// Keep `spare` where fewer than [`SPARE_LOGS`] are kept; else it goes, after the lock. - fn give(&self, spare: T) { - let mut spares = lock(&self.0); - if spares.len() < SPARE_LOGS { - spares.push(spare); + /// Keep `spare`, of `room` bytes, where it may ([`Kept::keep`]); else it goes, after the lock. + fn give(&self, spare: T, room: usize) { + let gone = lock(&self.0).keep(spare, room); + drop(gone); + } + + /// Keep `spares`, each with its room, under one lock, keeping as many as they are where that + /// is more than [`SPARE_LOGS`] ([`Kept::keep`]); those it does not keep go after the lock. + fn give_all(&self, spares: Vec<(T, usize)>) { + let mut gone = Vec::new(); + let mut kept = lock(&self.0); + kept.most = spares.len().max(SPARE_LOGS); + for (spare, room) in spares { + gone.extend(kept.keep(spare, room)); } + drop(kept); + drop(gone); } } @@ -546,6 +600,25 @@ impl SpareLogs { self.compute.take().unwrap_or_else(CallLog::new) } + /// Clear `held`, the logs a command buffer held for its commit for its groups of compute + /// encoders and no split took (`command_buffer/fold.rs`), in its release, and keep them for + /// next encoders under one lock: as many as they are, within [`SPARE_ROOM_BYTES`], so a next + /// command buffer's encoders as many reuse their room rather than allocate. Their calls and + /// handles go now. + pub(super) fn give_held(&self, held: Vec) { + if held.is_empty() { + return; + } + let mut compute = Vec::with_capacity(held.len()); + for replay in held { + match replay { + Replay::Compute { log, .. } => compute.extend(log.cleared()), + other => self.give(other), + } + } + self.compute.give_all(compute); + } + /// A blit encoder's log: a kept one, or a new one. pub(super) fn blit(&self) -> CallLog { self.blit.take().unwrap_or_else(CallLog::new) @@ -567,18 +640,18 @@ impl SpareLogs { pub(super) fn give(&self, replay: Replay) { match replay { Replay::Compute { log, .. } => { - if let Some(log) = log.cleared() { - self.compute.give(log); + if let Some((log, room)) = log.cleared() { + self.compute.give(log, room); } } Replay::Blit(log) => { - if let Some(log) = log.cleared() { - self.blit.give(log); + if let Some((log, room)) = log.cleared() { + self.blit.give(log, room); } } Replay::Render(log) => { - if let Some(room) = log.cleared() { - self.render.give(room); + if let Some((log, room)) = log.cleared() { + self.render.give(log, room); } } } diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 68d4b638c..23536d178 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 522; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2272; +pub const RUNTIME_VERSION: u32 = 2273; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles