Merge night/cpu: processor work, wait instructions patched, submit thread (off)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 06:21:22 -06:00
commit 55babd1acf
14 files changed
+2287 -240

No files matched your search

+264 -11
View File
@@ -285,7 +285,9 @@ pub(crate) fn submissions() -> (u64, u64) {
/// Drops `object` once every submission running now has finished: at once
/// if none is.
pub(crate) fn retire<T: 'static>(object: T) {
let started = STARTED.load(Ordering::Acquire);
// (Those the title made and the host has not begun get the next numbers.)
let queued = crate::queue::queued();
let started = STARTED.load(Ordering::Acquire) + queued;
if started <= FINISHED.load(Ordering::Acquire) {
RETIREMENTS[0].fetch_add(1, Ordering::Relaxed);
drop(object);
@@ -967,6 +969,9 @@ pub(crate) struct HostList {
/// Scratch buffers of the batched clamps (`crate::indirect::batch`), and
/// how many batches of this recording have a place in them.
batch_args: Mutex<(Vec<ID3D12Resource>, u32)>,
/// Refreshes of host copies that a thread of ours is still finding, and
/// where among the list's steps they belong ([`aside`]).
coming: Mutex<Option<(usize, std::sync::Arc<aside::Coming>)>>,
/// Counts the list in [`ALIVE`] while it lives.
_alive: Counted,
/// The texts of the markers `note_op` set, in order (for device-removed
@@ -3227,11 +3232,13 @@ fn late_line(frames: f64) {
}
}
pub(crate) mod aside;
pub(crate) mod awake;
mod card_steps;
pub(crate) mod indirects;
mod place;
mod send;
pub(crate) use send::{goes_as_words, words_under};
#[cfg(feature = "diagnostics")]
pub(crate) mod spans;
mod torn;
@@ -3633,6 +3640,7 @@ impl Host {
card_budget: OnceLock::new(),
indirect_args: Mutex::default(),
batch_args: Mutex::default(),
coming: Mutex::default(),
_alive: Counted::new(),
#[cfg(feature = "diagnostics")]
notes,
@@ -4622,6 +4630,8 @@ impl Host {
/// Runs closed lists and waits for them, and with [`late`] for
/// everything sent to the card before them, which is settled first.
pub(crate) fn execute(&self, runs: &[&HostList]) {
// Behind what the title's queues were given before it.
crate::queue::before_sync();
let _ = self.submit(runs, false);
}
@@ -4917,11 +4927,17 @@ impl Host {
let mut submitted = self.submitted.lock().unwrap_or_else(|e| e.into_inner());
let was_working = working(true);
submitting(1, runs.len());
#[cfg(feature = "diagnostics")]
let ahead = Split::begin();
#[cfg(feature = "diagnostics")]
SPLIT[11].fetch_add(called.elapsed().as_nanos() as u64, Ordering::Relaxed);
// 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 };
#[cfg(feature = "diagnostics")]
ahead.part(12);
if !late {
self.settle(&mut submitted, WHY_SYNC);
} else if own.is_none()
@@ -4937,9 +4953,11 @@ impl Host {
self.settle(&mut submitted, why);
}
}
#[cfg(feature = "diagnostics")]
ahead.part(13);
// Descriptors the title copied into placed heaps directly, before
// command lists that index those slots are submitted.
crate::descriptor::sync_heaps();
let heaps_synced = crate::descriptor::begin_sync_heaps();
// That list runs first, and is one of the submission's from here on.
let with: Vec<&HostList>;
let runs = match &own {
@@ -4972,14 +4990,13 @@ impl Host {
SWEPT.store(now_ms(), Ordering::Relaxed);
}
let behind = late && flying();
// The slots that waited for a texture's host copy, before the
// lists that may read them run.
crate::descriptor::make_waiting();
let this_submission = STARTED.fetch_add(1, Ordering::AcqRel) + 1;
// The ranges shaders write are counted for it (with the relay).
crate::shadow::preparing(this_submission);
let started = std::time::Instant::now();
#[cfg(feature = "diagnostics")]
ahead.part(14);
#[cfg(feature = "diagnostics")]
let lock = (started - called).as_micros() as u64;
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::Submissions, 1);
@@ -5040,6 +5057,9 @@ impl Host {
split.part(1);
// What flights in the air may write.
let air = line().written.clone();
if ahead_of_writes() {
batch.refresh_ahead(runs, |list, i| own.as_ref().is_some_and(|own| own.takes(list, i)));
}
for list in runs {
list.run_deferred(&mut batch, &air, |i| own.as_ref().is_some_and(|own| own.takes(list, i)));
firsts.push(lists.len());
@@ -5048,6 +5068,7 @@ impl Host {
}
lists.push(list.list.cast().ok());
}
batch.fill();
batch.flush();
crate::watch::ask_once_end();
#[cfg(feature = "diagnostics")]
@@ -5090,6 +5111,12 @@ impl Host {
}
#[cfg(feature = "diagnostics")]
split.part(4);
drop(heaps_synced);
// The slots that waited for a texture's host copy, before the
// lists that may read them run.
crate::descriptor::make_waiting();
#[cfg(feature = "diagnostics")]
split.part(15);
let prepared = std::time::Instant::now();
if late {
*submitted += 1;
@@ -5573,6 +5600,7 @@ impl HostList {
}
self.batches_common();
self.card_budget_out();
self.take_coming();
#[cfg(feature = "diagnostics")]
if let Some((heap, back)) = self.stamps() {
let count = (self.notes.lock().unwrap_or_else(|e| e.into_inner()).len() as u32).min(STAMPS - 1);
@@ -5590,12 +5618,35 @@ impl HostList {
}
}
/// `work` finds, on a thread of ours, refreshes that belong at this
/// place among the list's steps; they are there when the list is closed.
pub(crate) fn refreshes_coming(&self, work: impl FnOnce() -> Vec<aside::Refresh> + Send + 'static) {
self.take_coming();
let at = self.deferred().copies.len();
let coming = aside::begin(work);
*self.coming.lock().unwrap_or_else(|e| e.into_inner()) = Some((at, coming));
}
/// The refreshes that were still to come are put among the steps.
fn take_coming(&self) {
let Some((at, coming)) = self.coming.lock().unwrap_or_else(|e| e.into_inner()).take() else {
return;
};
let found = coming.wait();
let mut deferred = self.deferred();
let at = at.min(deferred.copies.len());
deferred
.copies
.splice(at..at, found.into_iter().map(|(shadow, from, to)| Step::Refresh(shadow, from as usize, (to - from) as usize)));
}
/// Only after the list's last submission finished (every one is waited
/// for).
pub(crate) fn reset(&self) {
#[cfg(feature = "diagnostics")]
let started = std::time::Instant::now();
self._alive.used();
*self.coming.lock().unwrap_or_else(|e| e.into_inner()) = None;
*self.deferred() = Deferred::default();
self.states.lock().unwrap_or_else(|e| e.into_inner()).clear();
self.twins.lock().unwrap_or_else(|e| e.into_inner()).clear();
@@ -6477,6 +6528,7 @@ impl HostList {
{
copies += 1;
}
batch.fill();
batch.title_written();
copy_mapped(dst as *mut u8, src as *const u8, len, true);
// With the submission's one asking: the journal learns it.
@@ -6489,17 +6541,23 @@ impl HostList {
(started, len)
}
&Step::Fill(dst, src, len, _) => {
let started = std::time::Instant::now();
#[cfg(feature = "diagnostics")]
{
copies += 1;
}
if wide_fills() {
batch.fills.push((dst, src, len));
bytes += len as u64;
continue;
}
let started = std::time::Instant::now();
copy_mapped(dst as *mut u8, src as *const u8, len, false);
(started, len)
}
Step::Placed(p) => {
let started = std::time::Instant::now();
let free = !touches(air, p.src, p.src + p.len) && !touches(air, p.dst, p.dst + p.len);
batch.fill();
if free {
batch.title_written();
}
@@ -6518,6 +6576,7 @@ impl HostList {
continue;
}
Step::Run(work) | Step::Host(work, _) => {
batch.fill();
#[cfg(feature = "diagnostics")]
let started = std::time::Instant::now();
if matches!(step, Step::Run(_)) {
@@ -6610,9 +6669,12 @@ impl std::hash::Hasher for AddressHash {
/// of the steps: the refreshes, and the copies and fills (those with the
/// refreshes a copy of title memory flushes). Of the asking, which ranges it
/// is of. Of the refreshes, those before ranges are given to shaders, one
/// copy at a time: nanoseconds, and how many.
/// copy at a time: nanoseconds, and how many. Ahead of all that: the wait for
/// the submission lock, the steps done on the card planned ([`card_steps`]),
/// the flights waited for, the heaps' records and what is swept; and, at the
/// end, the wait for the heaps' records.
#[cfg(feature = "diagnostics")]
static SPLIT: [std::sync::atomic::AtomicU64; 11] = [const { std::sync::atomic::AtomicU64::new(0) }; 11];
static SPLIT: [std::sync::atomic::AtomicU64; 16] = [const { std::sync::atomic::AtomicU64::new(0) }; 16];
/// Since the last frame-rate line: flushes of refreshes, their refreshes,
/// those known to copy nothing, and those done on several threads.
@@ -6646,6 +6708,7 @@ impl Split {
fn split_line(frames: f64) {
let ms: Vec<f64> = SPLIT.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / 1e6 / frames).collect();
let f: Vec<f64> = FLUSHES.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / frames).collect();
let e: Vec<f64> = crate::watch::early_asks().iter().map(|&n| n as f64 / frames).collect();
let a: Vec<f64> = crate::watch::asks().iter().map(|&n| n as f64 / frames).collect();
let h: Vec<f64> = crate::draw::HEAP_SPLIT.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / 1e6 / frames).collect();
let s: Vec<f64> = crate::shadow::send_stats().iter().map(|&n| n as f64 / frames).collect();
@@ -6653,7 +6716,9 @@ fn split_line(frames: f64) {
"preparing per frame, the submitting thread: {:.1} ms asking the write watch ({:.1} of them finding what to ask), {:.1} putting the title's bytes under flights' \
writes, {:.1} in the lists' steps (refreshes {:.1} in {:.0} flushes of {:.0}, {:.0} of them known to copy nothing, {:.0} on \
several threads, {:.1} one at a time in {:.0} before shader writes; copies and fills {:.1}, the refreshes before copies among them), {:.1} snapshots, {:.1} for copies in the \
card's memory, {:.1} sending the lists (VICTORY_LIGHT_REFRESH is {})",
card's memory, {:.1} sending the lists (VICTORY_LIGHT_REFRESH is {}); ahead of these {:.1} waiting for the submission lock, {:.1} \
planning steps done on the card, {:.1} waiting for flights, {:.1} the heaps' records and what is swept; at the end {:.1} waiting \
for the heaps' records; asked early by a thread of its own {:.0} pieces of 2 MiB, {:.1} ms (VICTORY_ASK_EARLY)",
ms[0],
ms[8],
ms[1],
@@ -6670,6 +6735,13 @@ fn split_line(frames: f64) {
ms[4],
ms[5],
if crate::watch::light() { "on" } else { "off" },
ms[11],
ms[12],
ms[13],
ms[14],
ms[15],
e[0],
e[1] / 1e3,
);
tracing::info!(
"asking about title memory per frame, all threads: {:.0} kernel askings over {:.0} MB ({:.1} ms, {:.0} pages of 4 KiB found \
@@ -6696,10 +6768,12 @@ fn split_line(frames: f64) {
s[3],
);
tracing::info!(
"the pass over the heap's views per frame: {:.1} ms looking at slots, {:.1} their copies' spans and refreshes, {:.1} textures",
"the pass over the heap's views per frame: {:.1} ms looking at slots, {:.1} their copies' spans and refreshes, {:.1} textures; the \
first two beside the recording (VICTORY_HEAP_PASS_ASIDE), {:.1} ms waited for at the lists' close",
h[0],
h[1],
h[2],
aside::WAITED.swap(0, Ordering::Relaxed) as f64 / 1e3 / frames,
);
}
@@ -6773,6 +6847,102 @@ struct Batch {
/// What copies at their place in the submission's lists put into title
/// memory ([`place`]): the host copy, the title address, the bytes.
placed: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, Vec<u8>)>,
/// Fills not done yet (destination, source, bytes): [`wide_fills`].
fills: Vec<(usize, usize, usize)>,
/// Copies (by address) brought up to date ahead of the steps, and the
/// range (from, to) ([`Batch::refresh_ahead`]).
ahead: ByCopy<(usize, usize)>,
}
/// Whether the copies that a submission's shaders write are brought up to
/// date together, on several threads, ahead of its steps
/// (`VICTORY_REFRESH_AHEAD=0`: each before the step that gives it to them).
///
/// 580 copies a frame, one after the other on the submitting thread, 3.4 to
/// 6.5 ms (session play-1125-0203).
fn ahead_of_writes() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_REFRESH_AHEAD").is_none_or(|v| v != "0") && wide_refresh() && crate::watch::light())
}
/// What a step is to the copies' refreshes, for [`written_spans`].
enum Touch {
/// A refresh of a copy (by address), from, to.
Refresh(usize, usize, usize),
/// The copy is given to shaders.
Write(usize),
/// Title memory is written.
Title,
Nothing,
}
/// Of `steps`, up to the first that writes title memory: the copies given to
/// shaders, each with the range its refreshes before that cover.
fn written_spans(steps: impl Iterator<Item = Touch>) -> Vec<(usize, usize, usize)> {
// (From, to, given to shaders.)
let mut spans: ByCopy<(usize, usize, bool)> = ByCopy::default();
for step in steps {
match step {
Touch::Refresh(copy, from, to) => {
let span = spans.entry(copy).or_insert((from, to, false));
if !span.2 {
(span.0, span.1) = (span.0.min(from), span.1.max(to));
}
}
// (Not refreshed before: nothing of it is due.)
Touch::Write(copy) => {
if let Some(span) = spans.get_mut(&copy) {
span.2 = true;
}
}
Touch::Title => break,
Touch::Nothing => {}
}
}
spans.into_iter().filter(|(_, s)| s.2).map(|(copy, (from, to, _))| (copy, from, to)).collect()
}
#[cfg(test)]
mod written_spans_tests {
use super::{Touch, written_spans};
#[test]
fn what_is_written_is_refreshed_ahead_up_to_its_write_and_to_title_writes() {
use Touch::{Nothing, Refresh, Title, Write};
let mut found = written_spans(
[
Refresh(1, 10, 20),
Refresh(2, 0, 5),
Nothing,
Refresh(1, 30, 40),
Write(1),
Refresh(1, 0, 100),
Write(3),
Refresh(3, 1, 2),
Refresh(4, 7, 9),
Write(4),
Title,
Write(2),
]
.into_iter(),
);
found.sort_unstable();
assert_eq!(found, [(1, 10, 40), (4, 7, 9)]);
}
}
/// Whether the fills of a submission's upload memory are done together on
/// several threads (`VICTORY_WIDE_FILLS=0`: one after the other, where the
/// lists name them).
///
/// They are 7,000 to 12,000 a frame, some 500 bytes each, 5 ms of the
/// submitting thread. Each reads title memory and writes memory of its own
/// that the card alone reads, when the lists run; they are done before the
/// next step that writes title memory or runs code of ours, and before the
/// submission goes on from its steps.
fn wide_fills() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_WIDE_FILLS").is_none_or(|v| v != "0") && wide_refresh())
}
/// Whether a submission's textures are brought up to date on several
@@ -6797,6 +6967,9 @@ impl Batch {
fn add(&mut self, shadow: &std::sync::Arc<crate::shadow::Shadow>, address: usize, len: usize) {
let end = address + len;
let key = std::sync::Arc::as_ptr(shadow) as usize;
if self.ahead.get(&key).is_some_and(|&(from, to)| from <= address && end <= to) {
return;
}
match self.index.get(&key) {
Some(&at) => {
let r = &mut self.refreshes[at];
@@ -6812,6 +6985,8 @@ impl Batch {
/// Before shaders are given a range of `shadow`'s copy: its pending
/// refresh is done, the others stay pending.
fn flush_one(&mut self, shadow: &std::sync::Arc<crate::shadow::Shadow>) {
// From here on it is refreshed around what the shaders write.
self.ahead.remove(&(std::sync::Arc::as_ptr(shadow) as usize));
let Some(at) = self.index.remove(&(std::sync::Arc::as_ptr(shadow) as usize)) else {
return;
};
@@ -6855,9 +7030,85 @@ impl Batch {
/// first, and textures are checked again after it.
fn title_written(&mut self) {
self.flush();
self.ahead.clear();
self.textures.clear();
}
/// Before the steps of `runs` ([`ahead_of_writes`]); copies and fills
/// that `taken` names are not done on the CPU.
fn refresh_ahead(&mut self, runs: &[&HostList], taken: impl Fn(&HostList, usize) -> bool) {
let mut copies: ByCopy<std::sync::Arc<crate::shadow::Shadow>> = ByCopy::default();
let mut touches = Vec::new();
'steps: for list in runs {
for (i, step) in list.deferred().copies.iter().enumerate() {
touches.push(match step {
Step::Refresh(shadow, at, len) => {
let key = std::sync::Arc::as_ptr(shadow) as usize;
copies.entry(key).or_insert_with(|| shadow.clone());
Touch::Refresh(key, *at, *at + *len)
}
Step::GpuWrite(shadow, ..) => Touch::Write(std::sync::Arc::as_ptr(shadow) as usize),
Step::Copy(..) if taken(list, i) => Touch::Nothing,
Step::Copy(..) | Step::Run(_) | Step::Placed(_) => break 'steps,
_ => Touch::Nothing,
});
}
}
let due: Vec<_> = written_spans(touches.into_iter())
.into_iter()
.filter_map(|(key, from, to)| Some((copies.get(&key)?.clone(), from, to)))
.collect();
for (shadow, from, to) in &due {
self.ahead.insert(std::sync::Arc::as_ptr(shadow) as usize, (*from, *to));
}
#[cfg(feature = "diagnostics")]
let began = std::time::Instant::now();
let started = std::time::Instant::now();
let before = PREPARING[0].load(Ordering::Relaxed);
crate::shadow::wide_each(due.len(), move |i| {
let (shadow, from, to) = &due[i];
if !shadow.nothing_new(*from, *to - *from) {
Self::update(shadow, *from, *to);
}
});
// The time counted is the time it took, not the threads' sum.
let summed = PREPARING[0].load(Ordering::Relaxed).saturating_sub(before);
PREPARING[0].fetch_sub(summed.saturating_sub(started.elapsed().as_micros() as u64), Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
{
let ns = began.elapsed().as_nanos() as u64;
SPLIT[6].fetch_add(ns, Ordering::Relaxed);
SPLIT[9].fetch_add(ns, Ordering::Relaxed);
}
}
/// The fills noted are done.
fn fill(&mut self) {
// Below this many the threads cost more than they save.
const FEW: usize = 96;
const PART: usize = 64;
if self.fills.is_empty() {
return;
}
let started = std::time::Instant::now();
if self.fills.len() < FEW {
for (dst, src, len) in self.fills.drain(..) {
copy_mapped(dst as *mut u8, src as *const u8, len, false);
}
} else {
let fills = std::mem::take(&mut self.fills);
crate::shadow::wide_each(fills.len().div_ceil(PART), move |i| {
for &(dst, src, len) in fills.iter().skip(i * PART).take(PART) {
copy_mapped(dst as *mut u8, src as *const u8, len, false);
}
});
}
let took = started.elapsed();
PREPARING[0].fetch_add(took.as_micros() as u64, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
SPLIT[7].fetch_add(took.as_nanos() as u64, Ordering::Relaxed);
}
/// The textures due are brought up to date.
fn refresh_textures(&mut self) {
if self.due.len() < 2 || !wide_textures() {
@@ -7442,6 +7693,7 @@ impl Meter {
crate::shadow::exact_line(frames);
crate::descriptor::sync_line(frames);
late_line(frames);
crate::queue::behind_line(frames);
card_clock_line(frames, ms);
#[cfg(feature = "diagnostics")]
spans::line(frames);
@@ -8902,7 +9154,8 @@ mod refresh_tests {
assert_eq!(look(true, "taken"), 0);
// SAFETY: the test's own memory.
unsafe { ((region + 600) as *mut u8).write_volatile(1) };
assert_eq!(look(false, "written at its end"), PAGE - 500);
// (The kernel names the 4 KiB, the guards only the block.)
assert_eq!(look(false, "written at its end"), if view { PAGE } else { 4096 } - 500);
assert_eq!(look(true, "taken again"), 0);
// Outside the range, on its last page.
// SAFETY: as above.