A write through one view of a page is seen at its other views; page writes counted; levels on demand (off); warm-up; card memory numbers

Memory sync:
- The guard over every view of a physical page (code by myokex, victory-amd
  c6dc4d8, after pull request #115 by Spexivus, its correction by joroyt and
  a part of VIctory #1122 by tightfall): the title shows one page at several
  addresses and a copy was guarded at one only. 0.8 to 1.0 million writes
  through another view in a five-minute run on the test machine.
  VICTORY_VIEW_GUARD=0 turns it off.
- Kernel reads into title memory are read beside and copied in (Joshua Odom,
  10ea38e).
- Writes of the storage layer into physical pages are counted where the
  title has the pages mapped and can open the guards there
  (VICTORY_PAGE_WRITES, off: counted only). Failed page-destination reads
  are retried.

Card memory:
- Levels on demand, off unless VICTORY_LEVELS_ON_DEMAND=1: a texture the
  title fills takes card memory for the levels the title holds; held levels
  get memory only while the card has room and are asked for again. Its test
  runs on the software adapter; not run on a card yet.
- Refused texture slots are held back so a pass reaches the slots behind
  them; room can be kept for card copies of title buffers (off).

Tools: translate --warm all; the shader cache is keyed on the translator's
source; counters for copy-back races, page writes and the copies the
texture sweep never lets go; a walls trace.

Game runs of these changes on an RTX 4070: five five-minute runs without a
device loss or a failed request. This merged tree was built; its test
suites were run on the tree the changes come from (gpu 129, hle 97).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
fxcelessandClaude Fable 5.2 committed 2026-11-25 00:44:25 -05:00
1 parent 310c6eb6c1
commit 8dd30ab472
25 files changed
+4218 -240

No files matched your search

+357 -7
View File
@@ -14,11 +14,14 @@
//! wholly uncommitted are skipped while they remain so; one query can
//! confirm a whole region, including ordinary reservations between views.
//!
//! shortcut: writes into physical pages through another view (xmem's
//! `write_physical`, DirectStorage's page destinations) are not seen at
//! the guarded addresses; they go to the texture pool, whose textures are
//! hashed, not guarded. A system call writing into a guarded block fails
//! (none is known to).
//! Writes into physical pages through another view are seen too: the
//! runtime tells us the views of the pages it wrote itself ([`writing`],
//! xmem's `write_physical` and DirectStorage's page destinations), and a
//! write by the title through any view of a guarded page faults like one
//! at the guarded address (the guard makes every view of the page
//! read-only, [`open`] gives them all back; not a page of more than
//! [`MANY_VIEWS`] views). A
//! system call writing into a guarded block fails (none is known to).
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Mutex;
@@ -218,6 +221,12 @@ pub(crate) fn wrote(at: usize, len: usize) {
}
j.asked += 1;
let now = j.asked;
// The asking that stands moves with it: `asked` and `writes` give it as
// "now", and a page noted written after "now" stays stale for every
// copy that takes it (copied at `now`, written at `now + 1`): copied
// again at every refresh, and a reason to settle flights
// (fxceless/victory-fork#410).
j.ahead_asked = now;
for page in at / PAGE..at.saturating_add(len).div_ceil(PAGE) {
j.written.insert(page, now);
}
@@ -379,12 +388,17 @@ struct Guards {
/// Blocks already copied as zeros. While they remain wholly uncommitted,
/// the copy can skip hundreds of MB of failing kernel reads per world frame.
absent: BTreeSet<usize>,
/// Blocks whose physical page was guarded together with its other views
/// (those views' blocks are in it too): a write to one opens them all.
/// A write range can be huge (`fread`), so only these are looked up.
aliased: BTreeSet<usize>,
}
static GUARDS: Mutex<Guards> = Mutex::new(Guards {
protected: BTreeSet::new(),
clean: BTreeSet::new(),
absent: BTreeSet::new(),
aliased: BTreeSet::new(),
});
fn guards() -> std::sync::MutexGuard<'static, Guards> {
@@ -508,8 +522,77 @@ fn is_view(start: usize, end: usize) -> bool {
/// Makes blocks `[from, to)` read-only and notes them guarded and clean;
/// block by block if they span views, leaving out any that fail, and
/// noting those not committed (unmapped) absent.
/// noting those not committed (unmapped) absent. The same physical pages
/// at every other view of them are made read-only too: a write through
/// one of those is this memory written, and only a fault can say so.
/// `VICTORY_VIEW_GUARD=0` leaves the other views as they are, as before.
fn protect(g: &mut Guards, from: usize, to: usize) {
protect_here(g, from, to);
if !view_guard() {
return;
}
for (base, bytes) in crate::inplace::views_between(from.saturating_mul(BLOCK), to.saturating_mul(BLOCK)) {
let first = (base / BLOCK).max(from);
let last = (base.saturating_add(bytes).div_ceil(BLOCK)).min(to);
for block in first..last {
let shown = crate::inplace::pages_of(block * BLOCK);
// One view (nothing else to guard), or the page the title shows
// wherever it has nothing (thousands of views): each view of
// that one is guarded by itself.
if shown.len() < 2 || shown.len() > MANY_VIEWS {
continue;
}
#[cfg(feature = "diagnostics")]
ALIASES[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
for alias in shown {
let alias = alias / BLOCK;
g.aliased.insert(alias);
if alias != block {
protect_alias(g, alias);
}
}
}
}
}
/// Views of one page past which its views are not guarded together.
pub(crate) const MANY_VIEWS: usize = 64;
/// Whether all the views of a guarded page are guarded together
/// (`VICTORY_VIEW_GUARD`).
fn view_guard() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_VIEW_GUARD").is_none_or(|v| v != "0"))
}
/// Diagnostics: blocks guarded together with their other views, blocks
/// opened because another view of theirs was written, and blocks opened
/// because a view of theirs was mapped; since the start.
#[cfg(feature = "diagnostics")]
pub(crate) static ALIASES: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atomic::AtomicU64::new(0) }; 3];
/// Makes `block` read-only: another view of pages a copy has just taken
/// through its own view, so that a write through this one faults too. It is
/// not noted clean. The copies of title memory at this view have not taken
/// the pages: noted clean, the block would not be reported written at their
/// next check and they would keep the bytes from before the write (`guard`
/// reports a block that is read-only and not clean as written, then notes it
/// clean). A block that is clean stays so: had the pages been written since
/// its copies took them, `open` would have opened it.
fn protect_alias(g: &mut Guards, block: usize) {
use windows_sys::Win32::System::Memory::{PAGE_READONLY, VirtualProtect};
if g.protected.contains(&block) {
return;
}
let mut old = 0;
// SAFETY: title view memory; only its protection changes.
if unsafe { VirtualProtect((block * BLOCK) as *const _, BLOCK, PAGE_READONLY, &mut old) } != 0 {
g.protected.insert(block);
}
}
/// [`protect`] for one range, without the other views of its pages.
fn protect_here(g: &mut Guards, from: usize, to: usize) {
use windows_sys::Win32::System::Memory::{PAGE_READONLY, VirtualProtect};
let set = |from: usize, to: usize| {
let mut old = 0;
@@ -557,8 +640,31 @@ fn uncommitted_end(block: usize) -> usize {
}
/// Gives write access back to the guarded blocks of `[start, end)` and
/// notes them written; true if any was guarded.
/// notes them written; true if any was guarded. The same physical pages at
/// every other view of them are opened too: the write that faulted (or is
/// ours, `crate::host::copy_mapped`) goes through one view, and every
/// guard over the pages must copy them again.
fn open(g: &mut Guards, start: usize, end: usize) -> bool {
let any = open_here(g, start, end);
if start >= end || g.aliased.is_empty() {
return any;
}
let aliased: Vec<usize> = g.aliased.range(start / BLOCK..end.div_ceil(BLOCK)).copied().collect();
for block in aliased {
g.aliased.remove(&block);
for alias in crate::inplace::pages_of(block * BLOCK) {
if alias / BLOCK != block && open_here(g, alias, alias.saturating_add(BLOCK)) {
#[cfg(feature = "diagnostics")]
ALIASES[1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
g.aliased.remove(&(alias / BLOCK));
}
}
any
}
/// [`open`] for one range, without the other views of its pages.
fn open_here(g: &mut Guards, start: usize, end: usize) -> bool {
use windows_sys::Win32::System::Memory::{PAGE_READWRITE, VirtualProtect};
let blocks: Vec<usize> = g
.protected
@@ -586,6 +692,98 @@ pub fn writing(at: usize, len: usize) {
crate::descriptor::host::remapped(at, len);
}
/// What `VICTORY_PAGE_WRITES` asks to be done with a write the guards do
/// not see once its bytes are there ([`title_memory_written`]), as bits:
/// 1, the guarded blocks under it are opened and count as written (host
/// copies of buffers and textures read through tables take them again);
/// 2, the placed textures picked by index that lie under it are due again;
/// 4, both also for reads to plain addresses when they have finished.
/// Unset or `0`: nothing, counted only. `1`: bits 1 and 2. `guards`: bit 1
/// alone. `slots`: bit 2 alone. `all`: everything.
pub(crate) fn page_writes() -> u8 {
static MODE: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
*MODE.get_or_init(|| match std::env::var("VICTORY_PAGE_WRITES").as_deref().map(str::trim) {
Ok("1") => 3,
Ok("guards") => 1,
Ok("slots") => 2,
Ok("all") => 7,
_ => 0,
})
}
/// Since the start: writes into physical pages through the runtime's own
/// views that landed at addresses the title has mapped (one for each run
/// of addresses); their bytes; guarded blocks that lay under them (host
/// copies that would otherwise keep the bytes from before); reads to
/// plain addresses that finished.
pub(crate) static WRITTEN: [std::sync::atomic::AtomicU64; 4] = [const { std::sync::atomic::AtomicU64::new(0) }; 4];
/// The bytes of a write the guards do not see are there: title memory
/// `[at, at + len)`. `pages`: DirectStorage wrote them into physical pages
/// through a view of the runtime's own, and this is an address where the
/// title has those pages mapped (no title address faulted, nothing was
/// mapped or unmapped; the shortcut this file's header names). Otherwise a
/// read to a plain address has finished, of which [`writing`] was told
/// before it began.
///
/// Upstream's c75fdba does the same for its trackers (its `Writing` marks
/// the pages once more when dropped; page destinations are reported at
/// each address showing them). Here it is counted always and acted on as
/// [`page_writes`] says. Called without the runtime's page-table lock;
/// takes the guards' lock, then, alone, the lock of the ranges kept for
/// the texture pass.
pub fn title_memory_written(at: usize, len: usize, pages: bool) {
use std::sync::atomic::Ordering::Relaxed;
if len == 0 {
return;
}
let mode = page_writes();
let end = at.saturating_add(len);
if pages {
WRITTEN[0].fetch_add(1, Relaxed);
WRITTEN[1].fetch_add(len as u64, Relaxed);
} else {
WRITTEN[3].fetch_add(1, Relaxed);
if mode & 4 == 0 {
return;
}
}
{
let mut g = guards();
let guarded = g.protected.range(at / BLOCK..end.div_ceil(BLOCK)).count() as u64;
if pages {
WRITTEN[2].fetch_add(guarded, Relaxed);
}
if mode & 1 != 0 && guarded != 0 {
open(&mut g, at, end);
}
}
// A placed texture there changed with no descriptor written.
crate::descriptor::host::page_written(at, len);
}
/// The runtime mapped a view of the physical bytes `[offset, offset + len)`
/// of `section` (`crate::inplace::title_memory_view`): the view is
/// writable, so the same bytes guarded at another view must be checked
/// again — and are, the pages noted written, which also has the next check
/// guard the new view (`protect`).
pub(crate) fn views_mapped(section: usize, offset: u64, len: usize) {
if !view_guard() {
return;
}
let ranges = crate::inplace::views_of_section(section, offset, len);
if ranges.is_empty() {
return;
}
let mut g = guards();
for &(at, n) in &ranges {
if open_here(&mut g, at, at.saturating_add(n)) {
#[cfg(feature = "diagnostics")]
ALIASES[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
}
}
/// Held shared by a read of title memory in place, and alone by the runtime
/// while it changes what title memory shows.
///
@@ -657,6 +855,10 @@ pub fn title_memory_remapped(at: usize, len: usize) {
g.protected.remove(&block);
g.clean.remove(&block);
}
let aliased: Vec<usize> = g.aliased.range(at / BLOCK..at.saturating_add(len).div_ceil(BLOCK)).copied().collect();
for block in aliased {
g.aliased.remove(&block);
}
}
/// Registers the handler that takes writes to guarded blocks, once, ahead
@@ -1046,6 +1248,154 @@ mod tests {
title_memory_remapped(p, 3 * BLOCK);
}
/// A pagefile section mapped twice: two views of the same physical
/// pages, the second left for the test; the section handle, the two
/// addresses.
fn two_views(bytes: usize) -> (usize, usize, usize) {
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
use windows_sys::Win32::System::Memory::{
CreateFileMappingW, FILE_MAP_WRITE, MapViewOfFile,
};
// SAFETY: a fresh pagefile section, mapped twice; the views keep
// the section alive and the handle is closed by the caller.
unsafe {
let section = CreateFileMappingW(
INVALID_HANDLE_VALUE,
std::ptr::null(),
PAGE_READWRITE,
0,
bytes as u32,
std::ptr::null(),
);
assert!(!section.is_null());
let one = MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, bytes).Value as usize;
let two = MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, bytes).Value as usize;
assert_ne!(one, 0);
assert_ne!(two, 0);
crate::inplace::title_memory_view(one, bytes, section as usize, 0);
crate::inplace::title_memory_view(two, bytes, section as usize, 0);
(section as usize, one, two)
}
}
/// The section of [`two_views`] is closed and its views unmapped; the
/// guard is told they are gone first.
fn drop_views(section: usize, views: &[usize], bytes: usize) {
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::System::Memory::{MEMORY_MAPPED_VIEW_ADDRESS, UnmapViewOfFile};
for &view in views {
title_memory_remapped(view, bytes);
// SAFETY: a view of the section mapped by `two_views`.
unsafe {
UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS {
Value: view as *mut _,
})
};
}
// SAFETY: the section's last reference.
unsafe { CloseHandle(section as *mut std::ffi::c_void) };
}
/// The title wrote the pages of a guarded texture through another view
/// of them: the fault at that view is the guard's to see, and the
/// guarded view's next check reports the pages written.
#[test]
fn a_write_through_another_view_is_reported_at_the_guarded_one() {
const SIZE: usize = 2 * BLOCK;
let (section, guarded, other) = two_views(SIZE);
let pages = |block: usize| (guarded + block * BLOCK) / PAGE;
// The first check takes it in (and every view of its pages).
assert_eq!(guard(guarded, guarded + SIZE), Some(vec![pages(0), pages(1)]));
assert_eq!(guard(guarded, guarded + SIZE), Some(Vec::new()), "nothing written yet");
// Writing through the other view faults into the handler, which
// opens that view and the guarded one: the next check sees it.
// SAFETY: the second view, read-write once the handler has opened it.
unsafe { ((other + BLOCK + 8) as *mut u8).write(1) };
assert_eq!(guard(guarded, guarded + SIZE), Some(vec![pages(1)]));
assert_eq!(guard(guarded, guarded + SIZE), Some(Vec::new()));
drop_views(section, &[guarded, other], SIZE);
}
/// A view of guarded pages is mapped anew: it is writable, so the
/// guarded view checks its pages again (and guards the new view).
#[test]
fn a_view_mapped_beside_a_guarded_one_makes_it_check_again() {
const SIZE: usize = 2 * BLOCK;
let (section, guarded, other) = two_views(SIZE);
let pages = |block: usize| (guarded + block * BLOCK) / PAGE;
assert_eq!(guard(guarded, guarded + SIZE), Some(vec![pages(0), pages(1)]));
assert_eq!(guard(guarded, guarded + SIZE), Some(Vec::new()), "nothing written yet");
title_memory_remapped(other, SIZE);
crate::inplace::title_memory_view(other, SIZE, section, 0);
assert_eq!(guard(guarded, guarded + SIZE), Some(vec![pages(0), pages(1)]));
assert_eq!(guard(guarded, guarded + SIZE), Some(Vec::new()));
drop_views(section, &[guarded, other], SIZE);
}
/// A write of the runtime's own, told at one view of the pages
/// (`writing`): the other views' next checks report the pages written.
#[test]
fn a_write_told_at_one_view_is_reported_at_the_others() {
const SIZE: usize = 2 * BLOCK;
let (section, guarded, other) = two_views(SIZE);
let pages = |block: usize| (guarded + block * BLOCK) / PAGE;
assert_eq!(guard(guarded, guarded + SIZE), Some(vec![pages(0), pages(1)]));
assert_eq!(guard(guarded, guarded + SIZE), Some(Vec::new()), "nothing written yet");
writing(other + BLOCK, BLOCK);
assert_eq!(guard(guarded, guarded + SIZE), Some(vec![pages(1)]));
assert_eq!(guard(guarded, guarded + SIZE), Some(Vec::new()));
// Half a page and the next: both blocks, not just the first's.
writing(other + BLOCK / 2, BLOCK);
assert_eq!(guard(guarded, guarded + SIZE), Some(vec![pages(0), pages(1)]));
drop_views(section, &[guarded, other], SIZE);
}
/// Both views of the pages are checked, each for a copy of its own
/// (the title shows one physical page at several addresses, and each
/// address has its host copy): a write through either is reported at
/// both, whichever is checked first. Guarding the pages for the first
/// view's check must not note the second view clean, or its copy would
/// keep the bytes from before the write.
#[test]
fn a_write_is_reported_at_every_checked_view() {
const SIZE: usize = 2 * BLOCK;
let (section, one, two) = two_views(SIZE);
let pages = |view: usize, block: usize| (view + block * BLOCK) / PAGE;
assert_eq!(guard(one, one + SIZE), Some(vec![pages(one, 0), pages(one, 1)]));
assert_eq!(guard(two, two + SIZE), Some(vec![pages(two, 0), pages(two, 1)]), "the second view's first check");
assert_eq!(guard(one, one + SIZE), Some(Vec::new()), "nothing written yet");
assert_eq!(guard(two, two + SIZE), Some(Vec::new()), "nothing written yet");
// SAFETY: the second view, read-write once the handler has opened it.
unsafe { ((two + BLOCK + 8) as *mut u8).write(1) };
assert_eq!(guard(one, one + SIZE), Some(vec![pages(one, 1)]));
assert_eq!(guard(two, two + SIZE), Some(vec![pages(two, 1)]), "written through this view, checked second");
// SAFETY: the first view, as above.
unsafe { ((one + 8) as *mut u8).write(1) };
assert_eq!(guard(two, two + SIZE), Some(vec![pages(two, 0)]));
assert_eq!(guard(one, one + SIZE), Some(vec![pages(one, 0)]), "written through this view, checked second");
assert_eq!(guard(one, one + SIZE), Some(Vec::new()));
assert_eq!(guard(two, two + SIZE), Some(Vec::new()));
drop_views(section, &[one, two], SIZE);
}
/// A page shown in more than `MANY_VIEWS` views is guarded at each by
/// itself: the others stay writable, and nothing looks through them all.
#[test]
fn a_page_of_many_views_is_guarded_one_view_at_a_time() {
let (section, one, two) = two_views(BLOCK);
let filler = one + (1 << 45);
for n in 0..MANY_VIEWS {
crate::inplace::title_memory_view(filler + n * BLOCK, BLOCK, section, 0);
}
assert_eq!(guard(one, one + BLOCK), Some(vec![one / PAGE]));
assert!(writable(two), "another view of it is left alone");
// SAFETY: the guarded view, opened by the handler.
unsafe { ((one + 8) as *mut u8).write(1) };
assert_eq!(guard(one, one + BLOCK), Some(vec![one / PAGE]));
title_memory_remapped(filler, MANY_VIEWS * BLOCK);
drop_views(section, &[one, two], BLOCK);
}
/// A write or mapping that runs past the end of the address space (a
/// title's `fread` with a huge count) covers up to the end; the sum used
/// to overflow. Up there no test has guarded blocks.