RUNTIME 2273: held fold logs go back at the command buffer's release, and the spares keep as many as a commit held
Every compute encoder logs now, and a group of folded encoders holds its logs until the commit (encoder_fold.rs), which gave each back alone to spares that kept 16: a 2300-encoder command buffer allocated about 2280 logs and freed them at its commit. dispatch_chain --host 20 --only encoders --dispatches 2300 on the RTX 5080 node, no buffer over host pages: the commit's share per dispatch went from 113-126 ns to 221-223 ns. SpareLogs::give_held clears the held logs outside the lock and keeps them under one lock, and each kind's spares keep as many as the last commit that held logs gave back (at least 16), all of a kind together within 1 MiB of room (SPARE_ROOM_BYTES, what 16 logs of SPARE_LOG_BYTES took before), so a next command buffer's encoders reuse them rather than allocate. Giving them back under one lock at the commit did not bring the commit's share back: dispatch_chain --host 20 --only encoders --dispatches 2300 on the RTX 5080 node, no buffer over host pages, ns a dispatch (two runs each): commit 137/127 before every compute encoder logged, 225/238 with the logs given back one by one, 235/217 under one lock; bind 50/50, 97/97, 60/59, the spares' reuse taking the first pushes' allocations off the bind. Each held log's clearing (its calls' handles let go of) and move stayed on the committing thread. split_failure now leaves the logs no split takes held, and the release gives them back (State::unsplit_logs, SpareLogs::give_held) beside the logs the encoders kept, on the queue's handler thread, before the waiters return. A 2300-encoder command buffer's held logs (about 0.7 MiB) live until it completes rather than until its commit. RUNTIME_VERSION 2273: fold logs are spared across command buffers and given back on the handler thread. Verified (dchain, at 3c895e7444 = this tree but the pin file): lavapipe full runtime suite 2583/2584 (the other: host_ram_copy_device private-bit, roip's); clippy x3 and rustdoc 0 (28705, 28706). ABAB on the 5080 node CPU, dispatch_chain --host 20 --runs 9 --only encoders --dispatches 2300, ns a dispatch: commit base 127/128, RV2272 214/219, this 130/139; bind 50/50, 98/97, 59/61; committing thread 3424/3467, 3545/3568, 3463/3500 (+35 over base); the clearing (~79) runs on the queue's handler thread (208/209 vs 129/129). Squashed from steelbrain/dchain3 8554aa06cc..0053613e6c.
This commit is contained in:
1 parent
6a986439d1
commit
28e55eac0b
6 files changed
+143
-60
No files matched your search
@@ -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<Failure>,
|
||||
) -> (Option<Failure>, Vec<Vec<usize>>, Vec<Replay>) {
|
||||
) -> (Option<Failure>, Vec<Vec<usize>>) {
|
||||
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<Replay> {
|
||||
self.held_logs.drain(..).map(|held| held.log).collect()
|
||||
}
|
||||
|
||||
/// `encoders`, a group's encoders the commit split that run (`split_failure`), whose
|
||||
|
||||
@@ -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<Box<crate::encoder_fold::Fold>>,
|
||||
/// 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<crate::encoder_fold::Stashed>,
|
||||
/// 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<usize>,
|
||||
_,
|
||||
@@ -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<Arc<Replay>> = (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();
|
||||
|
||||
@@ -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<Barrier>, fold: Option<Barrier>) -> Option<Barr
|
||||
}
|
||||
}
|
||||
|
||||
/// A log of a group's member not kept by itself, held until the commit (the module docs).
|
||||
/// A log of a group's member not kept by itself, held for the commit (the module docs).
|
||||
pub(crate) struct Stashed {
|
||||
/// The member's place among the command buffer's ended encoders.
|
||||
pub(crate) encoder: usize,
|
||||
|
||||
@@ -85,9 +85,10 @@ impl RenderLog {
|
||||
}
|
||||
}
|
||||
|
||||
/// Its lists emptied for a next pass's log, its descriptor, calls and handles let go of:
|
||||
/// `None` where their room is more than `SPARE_LOG_BYTES` (`CallLog::cleared`).
|
||||
pub(crate) fn cleared(self: Box<RenderLog>) -> Option<RenderRoom> {
|
||||
/// 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<RenderLog>) -> Option<(RenderRoom, usize)> {
|
||||
let RenderLog {
|
||||
descriptor,
|
||||
calls,
|
||||
@@ -95,14 +96,13 @@ impl RenderLog {
|
||||
..
|
||||
} = *self;
|
||||
drop(descriptor);
|
||||
if indexed.capacity() * std::mem::size_of::<DrawIndexed>() > SPARE_LOG_BYTES {
|
||||
let indexed_room = indexed.capacity() * std::mem::size_of::<DrawIndexed>();
|
||||
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.
|
||||
|
||||
@@ -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<C> CallLog<C> {
|
||||
}
|
||||
|
||||
/// 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<CallLog<C>> {
|
||||
let room = self.calls.capacity() * std::mem::size_of::<C>()
|
||||
/// kept, with that room in bytes: `None` where it is more than [`SPARE_LOG_BYTES`].
|
||||
pub(super) fn cleared(mut self) -> Option<(CallLog<C>, 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::<C>()
|
||||
+ self.bytes.capacity()
|
||||
+ self.buffers.capacity() * std::mem::size_of::<Option<Buffer>>()
|
||||
+ self.words.capacity() * std::mem::size_of::<u64>()
|
||||
@@ -490,24 +501,25 @@ impl<C> CallLog<C> {
|
||||
+ self.held_samplers.room()
|
||||
+ self.held_render_states.room()
|
||||
+ self.held_depth_stencils.room()
|
||||
+ self.refused.capacity() * std::mem::size_of::<u32>();
|
||||
if room > SPARE_LOG_BYTES {
|
||||
return None;
|
||||
}
|
||||
self.clear();
|
||||
Some(self)
|
||||
+ self.refused.capacity() * std::mem::size_of::<u32>()
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<T>(Mutex<Vec<T>>);
|
||||
struct Spares<T>(Mutex<Kept<T>>);
|
||||
|
||||
/// The logs one kind's [`Spares`] keeps.
|
||||
struct Kept<T> {
|
||||
/// 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<T> Default for Spares<T> {
|
||||
fn default() -> Spares<T> {
|
||||
Spares(Mutex::new(Vec::new()))
|
||||
Spares(Mutex::new(Kept {
|
||||
logs: Vec::new(),
|
||||
room: 0,
|
||||
most: SPARE_LOGS,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Kept<T> {
|
||||
/// 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<T> {
|
||||
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<T> Spares<T> {
|
||||
fn take(&self) -> Option<T> {
|
||||
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<Replay>) {
|
||||
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<BlitCall> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -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
|
||||
|
||||
Reference in new issue
Block a user