Lazy views of title pages (switch, off by default)
VICTORY_LAZY_VIEWS=1: the title's own mapping calls (XMemMapPhysicalPages, and the scatter form a page at a time) note the view in the books and tell the GPU side of it as before, and make its addresses a placeholder of their own (a split; a VirtualQuery to make sure), but map the host view only when something touches it. A view no one touched is let go with no system call, and mapped again at the same addresses with none. The title streams into its pool one call a page (365,000 calls, 25.7 s in 4 minutes of run x1440-streamlog; 846 ms of 1.7 s on a cutscene's critical path, timeline tl.120), DirectStorage fills the pages through views of its own, and the page-mapping copies read the pool from the books alone. A lazy view is made real (xmem::materialize, the books' lock only) by: - any fault in it, the title's or ours (direct reads and writes, our safe copies and puts): the GPU side's fault handler, first in the process, asks before anything else and makes the access again (victory_gpu::use_lazy_views); - the GPU side before what that handler does not see: reads and writes through the kernel (host::read, write_mapped, copy_mapped's title destinations), kernel writes the runtime announces (kernel_writing: file reads, DirectStorage memory destinations), guarding a range (watch::writes), and asking Windows whether memory is committed (texture::committed); - the title's XMemVirtualQuery, XMemVirtualAlloc at an address, and VirtualFree, which then see a view as before. Lock order: the remapping lock (victory_gpu::title_memory_remapping) is now taken before the views' lock in every mapping call, so a reader that holds the reading lock and faults into a lazy view gets the views' lock while a mapping call waits for its lock (the old order, views first, would wait for good). Page-mapping copies and side-by-side mapping map as before and let lazy views go as their own. The streaming log's M line gets three more fields with the switch: lazy views noted, let go untouched, made real. Tests: lazy_views_are_mapped_when_touched (a read faults and maps it with the pages' bytes; a write lands in the pages; untouched ones go without a host view; a lazy view cut in two stays lazy at its ends; XMemVirtualQuery maps it), a_reader_in_place_faults_into_a_lazy_view_while_a_mapping_waits. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
dfe9acca03
commit
f12996ca37
5 files changed
+408
-15
No files matched your search
@@ -8287,7 +8287,9 @@ pub(crate) fn copy_mapped(dst: *mut u8, src: *const u8, len: usize, title_dst: b
|
||||
return;
|
||||
}
|
||||
if title_dst {
|
||||
// The kernel writes the destination: it must not be guarded.
|
||||
// The kernel writes the destination: it must not be guarded, and it
|
||||
// must be a mapping of the host's.
|
||||
crate::watch::materialize(dst as usize, len);
|
||||
crate::watch::writing(dst as usize, len);
|
||||
}
|
||||
#[cfg(feature = "diagnostics")]
|
||||
@@ -8440,6 +8442,9 @@ pub(crate) fn read_there(src: *const u8, dst: &mut [u8]) -> usize {
|
||||
fn read(src: *const u8, dst: *mut u8, len: usize) -> bool {
|
||||
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
|
||||
use windows_sys::Win32::System::Threading::GetCurrentProcess;
|
||||
// (The kernel's read does not fault into the handler that makes lazily
|
||||
// mapped title memory real.)
|
||||
crate::watch::materialize(src as usize, len);
|
||||
if crate::safecopy::on() {
|
||||
// SAFETY: the destination is mapped writable memory of `len` bytes;
|
||||
// a source page that is not there fails the copy.
|
||||
@@ -8481,6 +8486,7 @@ fn direct_writes() -> bool {
|
||||
pub(crate) fn write_mapped(dst: *mut u8, src: *const u8, len: usize) {
|
||||
use windows_sys::Win32::System::Diagnostics::Debug::WriteProcessMemory;
|
||||
use windows_sys::Win32::System::Threading::GetCurrentProcess;
|
||||
crate::watch::materialize(dst as usize, len);
|
||||
let direct = direct_writes();
|
||||
let since = crate::watch::changes();
|
||||
let write = |at: usize, n: usize| {
|
||||
|
||||
@@ -114,6 +114,6 @@ pub use inplace::{title_memory_freeing, title_memory_section, title_memory_secti
|
||||
#[cfg(windows)]
|
||||
pub use watch::{
|
||||
kernel_writing, title_memory_allocated, title_memory_freed, title_memory_reading, title_memory_remapped,
|
||||
title_memory_remapping, title_memory_splitting, title_memory_unmapping, title_memory_written,
|
||||
title_memory_remapping, title_memory_splitting, title_memory_unmapping, title_memory_written, use_lazy_views,
|
||||
writing as kernel_will_write,
|
||||
};
|
||||
@@ -1753,6 +1753,9 @@ pub(crate) fn staged(resource: This, ask: bool) -> Option<(u64, bool)> {
|
||||
/// Whether `[at, at + len)` is committed memory (streamed textures may be
|
||||
/// unmapped until their pages arrive).
|
||||
pub(crate) fn committed(at: *const u8, len: usize) -> bool {
|
||||
// Memory the runtime mapped lazily is committed as far as the title
|
||||
// goes: made real before Windows is asked.
|
||||
crate::watch::materialize(at as usize, len);
|
||||
if by_query() {
|
||||
return committed_by_query(at, len);
|
||||
}
|
||||
|
||||
@@ -659,6 +659,11 @@ pub(crate) fn asked(start: usize, end: usize) -> Option<(u64, Vec<(usize, u64)>)
|
||||
/// kernel just reported. `None` if the memory is not write-watched (nor,
|
||||
/// with `guarded`, view memory, which is then guarded).
|
||||
pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Vec<(usize, u64)>)> {
|
||||
// Guarding asks Windows about the pages and protects them: a lazy view
|
||||
// is none to it yet.
|
||||
if guarded {
|
||||
materialize(start, end.saturating_sub(start));
|
||||
}
|
||||
let (first, last) = (start / PAGE, end.div_ceil(PAGE));
|
||||
let of = |j: &Journal| -> Vec<(usize, u64)> { j.written.range(first..last).map(|(&p, &w)| (p, w)).collect() };
|
||||
// Asked ahead with the submission's other ranges: the journal has it.
|
||||
@@ -2065,6 +2070,8 @@ pub struct KernelWrite(usize, usize);
|
||||
/// write fail (the kernel does not fault into our handler): a read cut
|
||||
/// short, which the title takes for what the file holds.
|
||||
pub fn kernel_writing(at: usize, len: usize) -> KernelWrite {
|
||||
// The kernel's write does not fault into the handler.
|
||||
materialize(at, len);
|
||||
let (first, last) = (at / BLOCK, at.saturating_add(len).div_ceil(BLOCK));
|
||||
{
|
||||
let mut g = guards_timed();
|
||||
@@ -2554,6 +2561,29 @@ fn title_memory_changed(at: usize, len: usize) -> std::sync::MutexGuard<'static,
|
||||
g
|
||||
}
|
||||
|
||||
/// The runtime's way to make title memory it mapped only in its books
|
||||
/// real (`xmem`, `VICTORY_LAZY_VIEWS=1`): `(address, bytes)` makes every
|
||||
/// such view that overlaps the range a mapping of the host's, and says
|
||||
/// whether there was one (and all of them could be made).
|
||||
static LAZY_VIEWS: std::sync::OnceLock<fn(usize, usize) -> bool> = std::sync::OnceLock::new();
|
||||
|
||||
/// The runtime maps title memory lazily: views it tells of (as mapped, by
|
||||
/// [`title_memory_remapped`] and `title_memory_view`) may be no mapping of
|
||||
/// the host's until something touches them. `materialize` makes them one.
|
||||
pub fn use_lazy_views(materialize: fn(usize, usize) -> bool) {
|
||||
let _ = LAZY_VIEWS.set(materialize);
|
||||
}
|
||||
|
||||
/// Makes title memory `[at, at + len)` that the runtime mapped lazily a
|
||||
/// mapping of the host's ([`use_lazy_views`]); whether any of it was lazy.
|
||||
/// Called before every touch of title memory the fault handler below does
|
||||
/// not see: reads and writes through the kernel, protection changes, and
|
||||
/// questions to Windows about the pages. (A fault in title memory, the
|
||||
/// title's or ours, comes to [`install`]'s handler, which asks first.)
|
||||
pub(crate) fn materialize(at: usize, len: usize) -> bool {
|
||||
LAZY_VIEWS.get().is_some_and(|f| f(at, len.max(1)))
|
||||
}
|
||||
|
||||
/// Registers the handler that takes writes to guarded blocks, once, ahead
|
||||
/// of the crash reporter's.
|
||||
pub(crate) fn install() {
|
||||
@@ -2578,6 +2608,14 @@ pub(crate) fn install() {
|
||||
unsafe { (*context).Rip = exit as u64 };
|
||||
EXCEPTION_CONTINUE_EXECUTION
|
||||
};
|
||||
// Memory the runtime mapped lazily: mapped now, and the access is
|
||||
// made again (any access, the title's or ours).
|
||||
if r.ExceptionCode == EXCEPTION_ACCESS_VIOLATION as NTSTATUS
|
||||
&& r.NumberParameters >= 2
|
||||
&& materialize(r.ExceptionInformation[1], 1)
|
||||
{
|
||||
return EXCEPTION_CONTINUE_EXECUTION;
|
||||
}
|
||||
if r.ExceptionCode != EXCEPTION_ACCESS_VIOLATION as NTSTATUS
|
||||
|| r.NumberParameters < 2
|
||||
|| r.ExceptionInformation[0] != WRITE
|
||||
|
||||
Reference in new issue
Block a user