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 810efe5829
commit 7bc7c0748d
6 files changed
+569 -123

No files matched your search

+24 -9
View File
@@ -37,7 +37,6 @@
use std::collections::BTreeMap;
use std::ffi::c_void;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
use windows::Win32::Foundation::HANDLE;
@@ -281,7 +280,9 @@ struct Views {
whole: Vec<(usize, usize)>,
}
static VIEWS: Mutex<Views> = Mutex::new(Views {
/// Read by many threads at once (write faults, landings), changed when the
/// runtime maps or unmaps.
static VIEWS: std::sync::RwLock<Views> = std::sync::RwLock::new(Views {
views: BTreeMap::new(),
pages: BTreeMap::new(),
pages_twice: 0,
@@ -289,14 +290,18 @@ static VIEWS: Mutex<Views> = Mutex::new(Views {
whole: Vec::new(),
});
fn views() -> std::sync::MutexGuard<'static, Views> {
VIEWS.lock().unwrap_or_else(|e| e.into_inner())
fn views() -> std::sync::RwLockReadGuard<'static, Views> {
VIEWS.read().unwrap_or_else(|e| e.into_inner())
}
fn views_mut() -> std::sync::RwLockWriteGuard<'static, Views> {
VIEWS.write().unwrap_or_else(|e| e.into_inner())
}
/// The runtime made a section of `bytes` for physical pages; `committed`
/// if with `SEC_COMMIT` (only such a section can be opened as a heap).
pub fn title_memory_section(section: usize, bytes: u64, committed: bool) {
let mut v = views();
let mut v = views_mut();
v.sections.retain(|s| s.0 != section);
// A handle's number given out again: the view was of the section that
// had it before (it stays mapped: someone may be writing through it).
@@ -308,7 +313,7 @@ pub fn title_memory_section(section: usize, bytes: u64, committed: bool) {
/// sees is of it any more, and [`beside`]'s view goes.
pub fn title_memory_section_gone(section: usize) {
let whole = {
let mut v = views();
let mut v = views_mut();
let at = v.whole.iter().position(|w| w.0 == section);
at.map(|i| v.whole.swap_remove(i).1)
};
@@ -324,7 +329,7 @@ pub fn title_memory_section_gone(section: usize) {
/// `offset` on. Call after [`crate::title_memory_remapped`] of the range.
pub fn title_memory_view(at: usize, len: usize, section: usize, offset: u64) {
{
let mut v = views();
let mut v = views_mut();
v.views.insert(at, (len, section, offset));
for k in 0..len.div_ceil(K64) {
let shown = v.pages.entry((section, offset + (k * K64) as u64)).or_default();
@@ -343,7 +348,7 @@ pub fn title_memory_view(at: usize, len: usize, section: usize, offset: u64) {
/// the views that touch it are forgotten (the runtime tells again those it
/// maps anew).
pub(crate) fn views_gone(at: usize, len: usize) {
let mut v = views();
let mut v = views_mut();
if v.views.is_empty() {
return;
}
@@ -529,7 +534,17 @@ pub(crate) fn cover(at: usize, len: usize) -> Cover {
/// `crate::watch::title_memory_reading`: until it lets go the title sees
/// the same pages at `at`.
pub(crate) fn beside(at: usize, len: usize) -> Option<usize> {
let mut v = views();
let known = {
let v = views();
let Cover::Run(section, offset) = cover_in(&v.views, at, len) else {
return None;
};
v.whole.iter().find(|w| w.0 == section).map(|w| (w.1, offset))
};
if let Some((base, offset)) = known {
return (base != 0).then(|| base + offset as usize);
}
let mut v = views_mut();
let Cover::Run(section, offset) = cover_in(&v.views, at, len) else {
return None;
};