Merge local/waits

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 21:41:29 -06:00
commit da89c8a5a9
6 files changed
+1084 -48

No files matched your search

+188 -46
View File
@@ -1677,16 +1677,19 @@ enum Step {
/// A copy into title memory (destination, source, bytes): the title's
/// buffer copies, which later refreshes must see.
Copy(usize, usize, usize),
/// A copy of title memory into the list's own upload memory.
Fill(usize, usize, usize),
/// A copy of title memory into the list's own upload memory; if a
/// descriptor slot shows that memory and nothing else does (a constant
/// buffer of a table made for the draw), the title heap and the slot.
Fill(usize, usize, usize, Option<(usize, u32)>),
/// Brings a range (address, bytes) of a buffer's host copy up to date:
/// once per submission, whichever lists ask, until a `Copy` or `Run`
/// intervenes.
Refresh(std::sync::Arc<crate::shadow::Shadow>, usize, usize),
/// Work that may write title memory.
Run(Box<dyn Fn() + Send>),
/// Work that writes host memory only.
Host(Box<dyn Fn() + Send>),
/// Work that writes host memory only, and the title memory it reads
/// (from, to; empty for none).
Host(Box<dyn Fn() + Send>, (usize, usize)),
/// Shaders write a range (address, bytes) of a buffer's host copy.
GpuWrite(std::sync::Arc<crate::shadow::Shadow>, usize, usize),
/// Brings a texture's staging buffer up to date: once per submission,
@@ -2054,6 +2057,8 @@ struct Sent {
number: u64,
card: Option<CardPlan>,
back: Vec<Declared>,
/// What was taken off it to be done on the card.
own: Option<card_steps::Taken>,
/// Its bottom-of-pipe writes.
writes: Vec<(usize, crate::queue::Written, usize)>,
}
@@ -2069,6 +2074,8 @@ struct Flight {
order: Vec<usize>,
card: Option<CardPlan>,
back: Vec<Declared>,
/// The list of ours that ran ahead of them ([`card_steps`]).
own: Option<card_steps::Taken>,
}
/// One thing in the line of what waits for the card, in order.
@@ -2102,6 +2109,8 @@ struct Line {
/// With [`relay`]: the bottom-of-pipe writes of the flights being
/// landed (taken out of `line`, not done yet), for [`late_met`].
in_hand: Vec<(usize, crate::queue::Written, usize)>,
/// With [`card_steps`]: what the flights being landed write.
landing: Vec<card_steps::Writer>,
}
// SAFETY: D3D12 objects (free-threaded) and host lists nothing records
@@ -2117,6 +2126,7 @@ static LINE: Mutex<Line> = Mutex::new(Line {
poke: false,
landed: Vec::new(),
in_hand: Vec::new(),
landing: Vec::new(),
});
/// Told when a flight joins [`LINE`] or it is poked.
static LINE_MOVED: std::sync::Condvar = std::sync::Condvar::new();
@@ -2187,14 +2197,14 @@ fn flying() -> bool {
/// runs steps of ours that may read or write it anywhere; what the
/// flights' shaders write is not there yet.
fn late_blocked(runs: &[&HostList]) -> Option<usize> {
let written = {
let (written, copies) = {
let line = line();
if line.written.is_empty() {
return None;
}
line.written.clone()
(line.written.clone(), card_steps::copies(&line))
};
blocked_by(&written, runs, true)
blocked_by(&written, runs, true).or_else(|| card_steps::copied_read(&copies, runs).then_some(WHY_READ))
}
/// Whether a small fill of upload memory, or copy of a title buffer, goes on without waiting for
@@ -2223,48 +2233,131 @@ fn fill_is_titles(src: usize, len: usize) -> bool {
on && len <= SMALL && (crate::watch::written_since_taken(src, len) || !crate::watch::landed_lately(src, len, 600))
}
/// Diagnostics: the reads that made a submission wait for flights: what
/// kind, where, how much, and the declared write it lies in; how often and
/// how long the wait was.
/// Diagnostics: the waits for flights, by what waited, why it was not done
/// on the card ([`card_steps`]), its bytes, and the declared write it lies
/// in (start, bytes).
#[cfg(feature = "diagnostics")]
type Blame = std::collections::HashMap<(&'static str, usize, usize, usize, usize), (u64, u64)>;
type Blamed = (&'static str, &'static str, usize, usize, usize);
/// How often and how long, and the address the last one read.
#[cfg(feature = "diagnostics")]
static BLAME: Mutex<Option<Blame>> = Mutex::new(None);
static BLAME: Mutex<Option<std::collections::HashMap<Blamed, (u64, u64, usize)>>> = Mutex::new(None);
/// What the wait that comes next is for, and why not on the card.
#[cfg(feature = "diagnostics")]
static BLAMED: Mutex<Option<(&'static str, usize, usize, usize, usize)>> = Mutex::new(None);
static BLAMED: Mutex<(Option<(Blamed, usize)>, &'static str)> = Mutex::new((None, ""));
/// Diagnostics: shader writes declared over a megabyte or more: the range
/// (from, to) and what declares it ([`wide_write`]): how often, and its
/// shaders.
#[cfg(feature = "diagnostics")]
static WIDE_WRITES: Mutex<Option<std::collections::HashMap<(usize, usize, usize), (u64, String)>>> = Mutex::new(None);
/// Diagnostics: a draw or dispatch declares that it writes `len` bytes at
/// `address`, through a root view of `pipeline`, a view in a table (0) or a
/// raw address (1).
#[cfg(feature = "diagnostics")]
pub(crate) fn wide_write(address: u64, len: u64, pipeline: usize) {
if len < 1 << 20 {
return;
}
let mut all = WIDE_WRITES.lock().unwrap_or_else(|e| e.into_inner());
let e = all
.get_or_insert_with(Default::default)
.entry((address as usize, (address + len) as usize, pipeline))
.or_insert_with(|| {
let shaders = match pipeline {
0 => "a view in a table".into(),
1 => "a raw address".into(),
p => crate::pipeline::host_pipeline(p as crate::object::This).map_or_else(String::new, |p| {
p.stages.iter().map(|s| s.shader.hash.to_string()).collect::<Vec<_>>().join("+")
}),
};
(0, shaders)
});
e.0 += 1;
}
#[cfg(feature = "diagnostics")]
fn blame(kind: &'static str, written: &[(usize, usize)], at: usize, len: usize) {
let i = written.partition_point(|r| r.1 <= at);
let (from, to) = written.get(i).copied().unwrap_or((0, 0));
*BLAMED.lock().unwrap_or_else(|e| e.into_inner()) = Some((kind, at, len, from, to - from));
let mut blamed = BLAMED.lock().unwrap_or_else(|e| e.into_inner());
let why = blamed.1;
blamed.0 = Some(((kind, why, len, from, to - from), at));
}
/// The wait for what [`blame`] last named took `us`.
/// Why [`card_steps`] left the submission to the wait that comes next.
#[cfg(feature = "diagnostics")]
fn blamed(us: u64) {
if let Some(key) = BLAMED.lock().unwrap_or_else(|e| e.into_inner()).take() {
let mut all = BLAME.lock().unwrap_or_else(|e| e.into_inner());
let e = all.get_or_insert_with(Default::default).entry(key).or_insert((0, 0));
e.0 += 1;
e.1 += us;
}
fn refused(why: &'static str) {
*BLAMED.lock().unwrap_or_else(|e| e.into_inner()) = (None, why);
}
/// The wait for what [`blame`] last named, or a wait of another reason than
/// title memory read, took `us`.
#[cfg(feature = "diagnostics")]
fn blamed(why: usize, us: u64) {
let (named, refused) = std::mem::replace(&mut *BLAMED.lock().unwrap_or_else(|e| e.into_inner()), (None, ""));
let (key, at) = match (why, named) {
(WHY_READ, Some(named)) => named,
(WHY_READ, None) => (("a host copy of what a copy done on the card wrote", refused, 0, 0, 0), 0),
_ => {
let kind = match why {
WHY_SYNC => "a synchronous submission, a present or the sweep",
WHY_VALUE => "a memory value",
WHY_STALE => "title memory changed under shader writes",
WHY_RUN => "a step of ours",
WHY_MOST => "lists in the air",
WHY_FENCE => "a blocking fence wait",
_ => "another reason",
};
((kind, "", 0, 0, 0), 0)
}
};
let mut all = BLAME.lock().unwrap_or_else(|e| e.into_inner());
let e = all.get_or_insert_with(Default::default).entry(key).or_default();
*e = (e.0 + 1, e.1 + us, at);
}
/// For the frame-rate line: the five that waited longest.
#[cfg(feature = "diagnostics")]
fn log_blame(frames: f64) {
let all = BLAME.lock().unwrap_or_else(|e| e.into_inner()).take().unwrap_or_default();
let wide = WIDE_WRITES.lock().unwrap_or_else(|e| e.into_inner()).take().unwrap_or_default();
let mut all: Vec<_> = all.into_iter().collect();
all.sort_unstable_by_key(|(_, (_, us))| std::cmp::Reverse(*us));
for ((kind, at, len, from, size), (n, us)) in all.into_iter().take(8) {
all.sort_unstable_by_key(|(_, (_, us, _))| std::cmp::Reverse(*us));
for ((kind, why, len, from, size), (n, us, at)) in all.into_iter().take(5) {
// The passes that declare it written, the ones that do so most.
let mut passes: std::collections::BTreeMap<&str, u64> = std::collections::BTreeMap::new();
for ((a, b, _), (n, shaders)) in &wide {
if *a < at + len && at < *b {
*passes.entry(shaders).or_default() += n;
}
}
let mut passes: Vec<(&str, u64)> = passes.into_iter().collect();
passes.sort_unstable_by_key(|p| std::cmp::Reverse(p.1));
let passes: Vec<String> =
passes.into_iter().take(5).map(|(shaders, n)| format!("{shaders} {:.1}x", n as f64 / frames)).collect();
tracing::info!(
"waited for flights, per frame: {:.1} ms in {:.2} waits for {kind} {at:#x}+{len:#x}, in shader writes declared over {from:#x}+{size:#x}",
"waited for flights, per frame: {:.1} ms in {:.2} waits for {kind} +{len:#x} (last at {at:#x}), in shader writes declared \
over {from:#x}+{size:#x} (declared per frame over a megabyte or more there, by shader: {}){}{why}",
us as f64 / 1e3 / frames,
n as f64 / frames
n as f64 / frames,
passes.join(", "),
if why.is_empty() { "" } else { "; not done on the card: " },
);
}
}
/// Whether flights that write `written` stop a read of `len` bytes at `src`.
fn read_blocked(written: &[(usize, usize)], src: usize, len: usize) -> bool {
touches(written, src, src + len) && !fill_is_titles(src, len)
}
/// Whether they stop a title buffer copy, and if so whether by its source.
fn copy_blocked(written: &[(usize, usize)], dst: usize, src: usize, len: usize) -> Option<bool> {
let read = read_blocked(written, src, len);
(read || touches(written, dst, dst + len)).then_some(read)
}
/// [`late_blocked`] against `written`, the title memory flights' shaders
/// may write (in order, joined); `count` whether the kind of read is
/// counted for the frame line.
@@ -2276,12 +2369,11 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op
for step in &list.deferred().copies {
match step {
&Step::Copy(dst, src, len) => {
let read = touches(written, src, src + len) && !fill_is_titles(src, len);
if read || touches(written, dst, dst + len) {
if let Some(_read) = copy_blocked(written, dst, src, len) {
if count {
LATE_STATS[15].fetch_add(1, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
if read {
if _read {
blame("a buffer copy's source", written, src, len);
} else {
blame("a buffer copy's destination", written, dst, len);
@@ -2290,8 +2382,8 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op
return Some(WHY_READ);
}
}
&Step::Fill(_, src, len) => {
if touches(written, src, src + len) && !fill_is_titles(src, len) {
&Step::Fill(_, src, len, _) => {
if read_blocked(written, src, len) {
if count {
LATE_STATS[16].fetch_add(1, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
@@ -2312,7 +2404,7 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op
}
}
Step::Run(_) => return Some(WHY_RUN),
Step::Refresh(..) | Step::GpuWrite(..) | Step::Host(_) => {}
Step::Refresh(..) | Step::GpuWrite(..) | Step::Host(..) => {}
}
}
}
@@ -2552,7 +2644,7 @@ fn relay_wait(why: usize, done: impl Fn(&Line) -> bool) {
LATE_WAITED[why.min(10)].fetch_add(us, Ordering::Relaxed);
SUBMISSION_US[1].fetch_add(us, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
blamed(us);
blamed(why, us);
}
/// What the thread that lands flights fetches card copies' pieces with
@@ -2617,6 +2709,9 @@ fn relay_worker() {
_ => None,
})
.collect();
if card_steps::stage() > 0 {
line.landing = card_steps::writers(&items);
}
break Ok(items);
}
};
@@ -2746,6 +2841,9 @@ fn late_line(frames: f64) {
r[4] / 1000.0,
);
}
card_steps::log(frames);
#[cfg(feature = "diagnostics")]
log_blame(frames);
if late_exact() {
let [pages, runs, bytes] = crate::shadow::merged_stats().map(|n| n as f64 / frames);
tracing::info!(
@@ -2756,6 +2854,8 @@ fn late_line(frames: f64) {
}
}
mod card_steps;
#[cfg(test)]
mod late_tests {
use super::{continues, join_ranges, touches};
@@ -3759,6 +3859,9 @@ impl Host {
if let Some(card) = flight.card {
self.card_done(card);
}
if let Some(own) = flight.own {
own.landed();
}
landed.push((flight.lists, flight.order));
}
Late::Write(at, what, bytes) => what.write(at as *mut u8, bytes),
@@ -3781,10 +3884,12 @@ impl Host {
poke,
landed: kept,
in_hand,
landing,
..
} = &mut *line;
kept.extend(landed);
in_hand.clear();
landing.clear();
// What is still in the air, from the flights that are.
written.clear();
*lists = 0;
@@ -3981,6 +4086,7 @@ impl Host {
order,
card: sent.card,
back: sent.back,
own: sent.own,
}));
items.extend(sent.writes.into_iter().map(|(at, what, bytes)| Late::Write(at, what, bytes)));
*flying >= late_most()
@@ -4025,6 +4131,7 @@ impl Host {
order: Vec::new(),
card: sent.card,
back: sent.back,
own: sent.own,
}));
items.extend(sent.writes.into_iter().map(|(at, what, bytes)| Late::Write(at, what, bytes)));
}
@@ -4205,17 +4312,32 @@ impl Host {
// Flights in the air ([`late`]): a submission that is waited for
// settles them first, one sent ahead only if preparing it reads
// what they write.
// Or the reads are done on the card, behind the flights.
let own = if late { self.card_steps(runs) } else { None };
if !late {
self.settle(&mut submitted, WHY_SYNC);
} else if let Some(why) = late_blocked(runs) {
} else if own.is_none()
&& let Some(why) = late_blocked(runs)
{
if relay_exact() && why == WHY_READ {
// Only until the flights that may write what is read have
// landed: their copy-back's end, not the card's other work.
relay_wait(why, |line| blocked_by(&line.written, runs, false).is_none());
relay_wait(why, |line| {
blocked_by(&line.written, runs, false).is_none() && !card_steps::copied_read(&card_steps::copies(line), runs)
});
} else {
self.settle(&mut submitted, why);
}
}
// That list runs first, and is one of the submission's from here on.
let with: Vec<&HostList>;
let runs = match &own {
Some(own) => {
with = std::iter::once(own.list()).chain(runs.iter().copied()).collect();
&with[..]
}
None => runs,
};
let behind = late && flying();
// Before this submission counts as started: what the sweep lets go
// of is named by no list, and nothing runs now (behind flights the
@@ -4286,7 +4408,7 @@ impl Host {
settled += settling.elapsed();
}
for list in runs {
list.run_deferred(&mut batch);
list.run_deferred(&mut batch, |i| own.as_ref().is_some_and(|own| own.takes(list, i)));
firsts.push(lists.len());
if let Some(prelude) = list.prelude() {
lists.push(Some(prelude));
@@ -4340,6 +4462,7 @@ impl Host {
number: this_submission,
card,
back,
own,
writes,
};
working(was_working);
@@ -5224,7 +5347,15 @@ impl HostList {
pub(crate) fn defer_fill(&self, dst: *mut u8, src: *const u8, len: usize) {
self.deferred()
.copies
.push(Step::Fill(dst as usize, src as usize, len));
.push(Step::Fill(dst as usize, src as usize, len, None));
}
/// As [`HostList::defer_fill`], for upload memory that only `slot` (a
/// title heap's address, and the slot) shows.
pub(crate) fn defer_constants(&self, dst: *mut u8, src: *const u8, len: usize, slot: (usize, u32)) {
self.deferred()
.copies
.push(Step::Fill(dst as usize, src as usize, len, Some(slot)));
}
/// Brings `[address, address + len)` of a buffer's host copy up to date
@@ -5344,7 +5475,7 @@ impl HostList {
// list's last run (mesh dispatches: the largest group count
// asked for, the groups asked in all; with the card's copy, as
// the list's end copied them here): taken, then zeroed.
deferred.copies.push(Step::Host(Box::new(move || unsafe {
let notes = Box::new(move || unsafe {
let words = at as *mut u32;
let (largest, all) = (words.add(1).read_volatile(), words.add(2).read_volatile());
MESH_GROUPS[0].fetch_max(u64::from(largest), Ordering::Relaxed);
@@ -5352,7 +5483,8 @@ impl HostList {
for k in 0..3 {
words.add(k).write_volatile(0);
}
})));
});
deferred.copies.push(Step::Host(notes, (0, 0)));
drop(deferred);
if let Some(card) = card {
self.zero_card_budget(card);
@@ -5528,7 +5660,13 @@ impl HostList {
/// As [`HostList::defer`], for work that writes only host memory.
pub(crate) fn defer_host(&self, work: impl Fn() + Send + 'static) {
self.deferred().copies.push(Step::Host(Box::new(work)));
self.deferred().copies.push(Step::Host(Box::new(work), (0, 0)));
}
/// As [`HostList::defer_host`], for work that reads `len` bytes of title
/// memory at `from`.
pub(crate) fn defer_host_read(&self, from: usize, len: usize, work: impl Fn() + Send + 'static) {
self.deferred().copies.push(Step::Host(Box::new(work), (from, from + len)));
}
/// Brings a texture's staging buffer up to date when the list is
@@ -5539,11 +5677,16 @@ impl HostList {
/// Runs the list's submission-time steps; refreshes go into `batch`
/// (the title's big buffers, a 174 MB one among them, are read by most
/// lists of a submission, through many ranges).
fn run_deferred(&self, batch: &mut Batch) {
/// lists of a submission, through many ranges). Copies and fills that
/// `taken` names (by their place) are left out: they are done on the
/// card ([`card_steps`]).
fn run_deferred(&self, batch: &mut Batch, taken: impl Fn(usize) -> bool) {
#[cfg(feature = "diagnostics")]
use crate::copystat::{C, add};
for step in &self.deferred().copies {
for (i, step) in self.deferred().copies.iter().enumerate() {
if matches!(step, Step::Copy(..) | Step::Fill(..)) && taken(i) {
continue;
}
let started = std::time::Instant::now();
let copied = match step {
&Step::Copy(dst, src, len) => {
@@ -5558,7 +5701,7 @@ impl HostList {
crate::watch::wrote(dst, len);
len
}
&Step::Fill(dst, src, len) => {
&Step::Fill(dst, src, len, _) => {
#[cfg(feature = "diagnostics")]
{
add(C::Copies, 1);
@@ -5576,7 +5719,7 @@ impl HostList {
batch.add(shadow, *address, *len);
continue;
}
Step::Run(work) | Step::Host(work) => {
Step::Run(work) | Step::Host(work, _) => {
if matches!(step, Step::Run(_)) {
batch.title_written();
}
@@ -6326,7 +6469,6 @@ impl Meter {
);
#[cfg(feature = "diagnostics")]
{
log_blame(frames);
let put = [0, 1, 2, 3].map(|i| crate::shadow::PUT[i].swap(0, Ordering::Relaxed) as f64 / frames);
tracing::info!(
"what shaders wrote put into title memory per frame (VICTORY_EXACT_BACK): {:.0} pieces in {:.0} writes, {:.1} MB; {:.0} pieces joined over gaps",