Write faults: a block just written rests before it is guarded again (VICTORY_GUARD_REST=0: not), a fault opens the view it came through alone (VICTORY_OPEN_ONE_VIEW=0: every view), guarded blocks kept as bits

A book of the faults (VICTORY_FAULT_BOOK=<file>, diagnostics) says of the
dorm: 586 faults a frame on 900 blocks, 245 of them on a block that had
faulted in the same frame already, 220 within 3 ms of the refresh that made
the block read-only; 69 ms a frame of the title's threads, of which 6 with
the guards' lock and 63 waiting for it. What is held under it is mostly
VirtualProtect (5 us and more in the game's process, one at a time whoever
asks: a bench found 14 threads no faster than one).

- A block a write fault opened is left open by the refreshes of the next
  3 ms and told as written at each.
- The other views of its page stay read-only, not clean.
- The sets of guarded, clean, absent and aliased blocks are bits, a range is
  looked at by words; the lock is let go between the runs a refresh guards;
  a fault that finds its block open takes no lock; the record of the title's
  views is read by many threads at once; the kernel is not asked about memory
  known to be a guarded view.

Run faults-2 (A/B of both switches, 20 phases of 10 s in the dorm): faults
601 -> 391 a frame, blocks made read-only 928 -> 407, the title's threads
27.5 -> 15.8 ms a frame in them (69 before), frame 64.8 -> 62.5 ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 11:36:09 -06:00
1 parent bc83b16387
commit 0578afffbb
6 files changed
+569 -123

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
//! 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,
/// Blocks `[a, b)` were opened ahead of the title's writes.
Ahead = 8,
}
#[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);
}
}