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

+137 -7
View File
@@ -161,6 +161,126 @@ fn mirror(handle: u64) {
}
}
/// Whether [`sync_heaps`] is done (`VICTORY_HEAP_SYNC=0`: not).
#[cfg(windows)]
fn sync_on() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_SYNC").is_none_or(|v| v != "0"))
}
/// Whether a submission's [`sync_heaps`] is done by a thread of its own
/// beside the rest of the preparing (`VICTORY_HEAP_SYNC_ASIDE=0`: in front of
/// it, as before).
///
/// It took 13 ms a frame of the submitting thread (session play-1125-0203:
/// 230 changed heap pages and 470,000 slots in 30 submissions). What it makes
/// are the host heap's descriptors, which the card reads: it has to be done
/// before the lists go to the queue, and nothing of the preparing looks at
/// them (what a draw reads through a table it made itself, when recorded).
#[cfg(windows)]
fn sync_aside() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| sync_on() && std::env::var_os("VICTORY_HEAP_SYNC_ASIDE").is_none_or(|v| v != "0"))
}
/// How long that thread waits for a submission before it makes a round of its
/// own (`VICTORY_HEAP_SYNC_EARLY=<ms>`, 4 if not given; `0`: none). The title
/// copies its records while it records, and the first submission of a frame
/// found all of them: 8 ms waited for (run cpu-2). A round made meanwhile
/// leaves it those written since.
#[cfg(windows)]
fn sync_early() -> Option<std::time::Duration> {
static EVERY: std::sync::OnceLock<Option<std::time::Duration>> = std::sync::OnceLock::new();
*EVERY.get_or_init(|| {
let ms = std::env::var("VICTORY_HEAP_SYNC_EARLY").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(4u64);
(ms > 0).then(|| std::time::Duration::from_millis(ms))
})
}
/// The thread's work: rounds asked for, rounds done.
#[cfg(windows)]
static SYNC_ROUNDS: (std::sync::Mutex<(u64, u64)>, std::sync::Condvar) = (std::sync::Mutex::new((0, 0)), std::sync::Condvar::new());
/// [`sync_heaps`], begun; done when it is dropped (with [`sync_aside`]), or
/// when this returns.
#[cfg(windows)]
pub(crate) struct SyncedHeaps(Option<u64>);
#[cfg(windows)]
pub(crate) fn begin_sync_heaps() -> SyncedHeaps {
if !sync_aside() {
sync_heaps();
return SyncedHeaps(None);
}
static STARTED: std::sync::Once = std::sync::Once::new();
static THREAD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
STARTED.call_once(|| {
let made = std::thread::Builder::new().name("victory-heap-sync".into()).spawn(|| {
let rounds = || SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
/// A round is over, however it ends.
struct Over;
impl Drop for Over {
fn drop(&mut self) {
SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner()).1 += 1;
SYNC_ROUNDS.1.notify_all();
}
}
// Between the rounds asked for, while submissions come.
let mut last = std::time::Instant::now();
loop {
let mut r = rounds();
let mut asked = r.0 != r.1;
while !asked {
let Some(every) = sync_early().filter(|_| last.elapsed().as_millis() < 500) else {
r = SYNC_ROUNDS.1.wait(r).unwrap_or_else(|e| e.into_inner());
asked = r.0 != r.1;
continue;
};
let (back, over) = SYNC_ROUNDS.1.wait_timeout(r, every).unwrap_or_else(|e| e.into_inner());
r = back;
asked = r.0 != r.1;
if over.timed_out() {
break;
}
}
drop(r);
let _over = asked.then(|| {
last = std::time::Instant::now();
Over
});
let _ = std::panic::catch_unwind(sync_heaps);
}
});
THREAD.store(made.is_ok(), std::sync::atomic::Ordering::Release);
});
if !THREAD.load(std::sync::atomic::Ordering::Acquire) {
sync_heaps();
return SyncedHeaps(None);
}
let mut rounds = SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
rounds.0 += 1;
let mine = rounds.0;
drop(rounds);
SYNC_ROUNDS.1.notify_all();
SyncedHeaps(Some(mine))
}
#[cfg(windows)]
impl Drop for SyncedHeaps {
fn drop(&mut self) {
let Some(mine) = self.0 else {
return;
};
let began = std::time::Instant::now();
let mut rounds = SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
while rounds.1 < mine {
rounds = SYNC_ROUNDS.1.wait(rounds).unwrap_or_else(|e| e.into_inner());
}
drop(rounds);
SYNCED[6].fetch_add(began.elapsed().as_micros() as u64, std::sync::atomic::Ordering::Relaxed);
}
}
/// Mirrors records copied directly into placed shader-visible heaps. Calls
/// to `Create*View` are already mirrored by [`mirror`]; raw CPU copies leave
/// only write-watch pages to reveal which slots changed.
@@ -173,8 +293,7 @@ fn mirror(handle: u64) {
#[cfg(windows)]
pub(crate) fn sync_heaps() {
use std::sync::atomic::Ordering::Relaxed;
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if !*ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_SYNC").is_none_or(|v| v != "0")) {
if !sync_on() {
return;
}
let started = std::time::Instant::now();
@@ -278,9 +397,10 @@ pub(crate) fn sync_heaps() {
/// shader-visible heaps looked at, slots on them, slots whose record was
/// not the one last seen and was mirrored, the microseconds it took; then
/// new records left to the draws that read them through a table, and new
/// records that waited for one ([`host::HostHeap::copied_wait`]).
/// records that waited for one ([`host::HostHeap::copied_wait`]); and the
/// microseconds submissions waited for it ([`sync_aside`]).
#[cfg(windows)]
pub(crate) static SYNCED: [std::sync::atomic::AtomicU64; 6] = [const { std::sync::atomic::AtomicU64::new(0) }; 6];
pub(crate) static SYNCED: [std::sync::atomic::AtomicU64; 7] = [const { std::sync::atomic::AtomicU64::new(0) }; 7];
/// With the frame line: what [`sync_heaps`] did per frame over `frames`.
/// Nothing while no heap page changed.
@@ -295,20 +415,21 @@ pub(crate) fn sync_line(frames: f64) {
lost as f64 / frames
);
}
let [pages, looked, mirrored, micros, tabled, later] = [0, 1, 2, 3, 4, 5].map(|i| SYNCED[i].swap(0, std::sync::atomic::Ordering::Relaxed));
let [pages, looked, mirrored, micros, tabled, later, waited] = [0, 1, 2, 3, 4, 5, 6].map(|i| SYNCED[i].swap(0, std::sync::atomic::Ordering::Relaxed));
if pages == 0 {
return;
}
tracing::info!(
"descriptor records the title copied into its heap as bytes (VICTORY_HEAP_SYNC=0: not looked for): per frame \
{:.1} changed heap pages, {:.0} slots looked at, {:.1} whose record was new and was mirrored, {:.1} new ones of \
draws' tables left to the draws, {:.1} that waited for a draw first, {:.2} ms",
draws' tables left to the draws, {:.1} that waited for a draw first, {:.2} ms ({:.2} ms waited for)",
pages as f64 / frames,
looked as f64 / frames,
mirrored as f64 / frames,
tabled as f64 / frames,
later as f64 / frames,
micros as f64 / 1000.0 / frames
micros as f64 / 1000.0 / frames,
waited as f64 / 1000.0 / frames
);
}
@@ -350,6 +471,15 @@ pub(crate) fn lent_heap() -> Option<usize> {
.map(|(h, _)| h)
}
/// `f` of the title heap at `heap`, if that heap still exists; it stays
/// until `f` is done.
#[cfg(windows)]
pub(crate) fn with_heap<T>(heap: usize, f: impl FnOnce(&Heap) -> T) -> Option<T> {
let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
// SAFETY: registered heaps are live (removed when dropped).
heaps.iter().any(|&(_, _, h)| h == heap).then(|| f(unsafe { &*(heap as *const Heap) }))
}
/// Gives back dynamic slots of the host heap behind the title heap at
/// `heap`, if that heap still exists.
#[cfg(windows)]