Files
6gbvram---16gb-ram/crates/victory-gpu/src/faultbook.rs
T
readmet3xt-labandClaude Opus 5.5 3f55056817 Write faults open guarded blocks without the guards' lock (VICTORY_FAULT_UNLOCKED=1; off)
Of 281 ms the title's threads spent in the write-fault handler over 14
frames of play, 252 ms were waits for the guards' lock (timeline tl.190;
tl.130 and tl2's tl.185 the same): faults queued behind other faults and
behind refreshes guarding, up to 5 ms, while a fault's own work took 8
microseconds at the median.

With the switch a fault takes the block's bit in OPENING (whoever finds it
set waits up to 100 microseconds for that fault, then goes the locked
way), tells the other views of the page as written when the block is
guarded with them (ALIASED, a mirror of Guards::aliased: TOLD and BESIDE
set, PUBLISHED cleared), clears the block's PUBLISHED, sets its BESIDE,
changes the protection itself and queues the block. The next holder of
the lock settles the guards' notes before anything else (guards ->
settle). Until then the holders of the lock treat OPENING and TOLD blocks
as written and leave them as they are (guard), publish takes its bit back
if it finds one, and protect_alias does not count an OPENING block as
read-only for another view. OPENING's doc comment gives the argument.

Also: BESIDE is forgotten when the mapping of a block changes (the block
is told as written then anyway; a bit left behind kept a hole mapped at
the address from being known by its bits). asked_for! moves to switch.rs
for both files. Diagnostics: the line of the trackers' lock waits counts
faults opened under the lock (waiting, holding), without it, and those
that went the locked way after all; the fault book has What::Unlocked.

Tests: the single-thread tests whose writes fault, again with faults
that take no lock; the many-askers test with all and half the writers'
faults taking no lock; a fault settled by the next holder; what holders
do with an unsettled block; eight threads faulting on the same blocks at
once through two views; ranges of bits cleared.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 19:59:20 +05:30

118 lines
4.4 KiB
Rust

//! A book of the title's write faults on guarded view memory
//! (`VICTORY_FAULT_BOOK=<file>`; builds with diagnostics): which block, when
//! in the frame, what it waited for, and what else happened to the block
//! (taken by a refresh, asked about), for 40 frames from each of the times
//! `VICTORY_FAULT_BOOK_AT` gives (seconds after the start; 150,210,270 unless
//! given). Written as `<file>.<n>.bin`, records of 40 bytes: ticks of the
//! performance counter, two words, thread, what.
#![cfg(feature = "diagnostics")]
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering::Relaxed};
use std::sync::{Mutex, OnceLock};
/// What a record is of.
#[derive(Clone, Copy)]
#[repr(u32)]
pub(crate) enum What {
/// A frame ended (a: its number).
Frame = 0,
/// A write fault opened block `a`; b: blocks opened with it (the upper
/// 32 bits), nanoseconds of waiting for the guards' lock; c: nanoseconds
/// with it.
Fault = 1,
/// Blocks `[a, b)` were found written, or never taken, and made read-only.
Taken = 2,
/// `[a, b)` was asked about and known unwritten by its bits.
Known = 3,
/// `[a, b)` was asked about block by block; c: how many were written.
Asked = 4,
/// We opened block `a` ourselves.
Ours = 5,
/// Another view of a page taken, block `a`, was made read-only.
Alias = 6,
/// A fault on block `a` that someone else had opened; b: nanoseconds.
Open = 7,
/// The instruction at `a` made the fault before (block `b`).
Site = 9,
/// A store (the instruction at `a`) was done beside the guard of block
/// `b`.
Stored = 10,
/// A write fault opened block `a` without the guards' lock
/// (`VICTORY_FAULT_UNLOCKED`); b: other views of its page told as
/// written; c: nanoseconds in it.
Unlocked = 11,
}
#[repr(C)]
#[derive(Clone, Copy)]
struct Record {
at: i64,
a: u64,
b: u64,
c: u64,
thread: u32,
what: u32,
}
static ON: AtomicBool = AtomicBool::new(false);
static BOOK: Mutex<Vec<Record>> = Mutex::new(Vec::new());
fn ticks() -> i64 {
let mut t = 0;
// SAFETY: an out pointer of the right size.
unsafe { windows_sys::Win32::System::Performance::QueryPerformanceCounter(&mut t) };
t
}
/// Whether records are kept now.
pub(crate) fn on() -> bool {
ON.load(Relaxed)
}
pub(crate) fn note(what: What, a: usize, b: usize, c: u64) {
if !on() {
return;
}
// SAFETY: a question about this thread.
let thread = unsafe { windows_sys::Win32::System::Threading::GetCurrentThreadId() };
let record = Record { at: ticks(), a: a as u64, b: b as u64, c, thread, what: what as u32 };
BOOK.lock().unwrap_or_else(|e| e.into_inner()).push(record);
}
/// A frame ended.
pub(crate) fn frame() {
const FRAMES: u64 = 40;
static SETTINGS: OnceLock<Option<(String, Vec<u64>)>> = OnceLock::new();
static BEGAN: OnceLock<std::time::Instant> = OnceLock::new();
static LEFT: AtomicU64 = AtomicU64::new(0);
static MADE: AtomicU64 = AtomicU64::new(0);
let began = *BEGAN.get_or_init(std::time::Instant::now);
let Some((file, times)) = SETTINGS.get_or_init(|| {
let file = std::env::var("VICTORY_FAULT_BOOK").ok().filter(|f| !f.is_empty())?;
let times = std::env::var("VICTORY_FAULT_BOOK_AT").unwrap_or_else(|_| "150,210,270".into());
Some((file, times.split(',').filter_map(|t| t.trim().parse().ok()).collect()))
}) else {
return;
};
note(What::Frame, crate::frame::current() as usize, 0, 0);
if on() {
if LEFT.fetch_sub(1, Relaxed) > 1 {
return;
}
ON.store(false, Relaxed);
let records = std::mem::take(&mut *BOOK.lock().unwrap_or_else(|e| e.into_inner()));
let file = format!("{file}.{}.bin", MADE.fetch_add(1, Relaxed));
std::thread::spawn(move || {
// SAFETY: plain data, every byte of it written.
let bytes = unsafe { std::slice::from_raw_parts(records.as_ptr().cast::<u8>(), size_of_val(&records[..])) };
match std::fs::write(&file, bytes) {
Ok(()) => tracing::info!(file, records = records.len(), "book of faults written"),
Err(e) => tracing::warn!(file, %e, "book of faults not written"),
}
});
} else if times.get(MADE.load(Relaxed) as usize).is_some_and(|&t| began.elapsed().as_secs() >= t) {
LEFT.store(FRAMES, Relaxed);
ON.store(true, Relaxed);
}
}