Repository navigation
Conversation
… instead of PAGE_READONLY guard faults
…ing/demoting watched pages
|
Confirmed a missed write on Cherry-pickable fix and both regression tests. Hardware polling stays active until the read-only guard takes over. All 23 active watch tests passed. A separate local test injected a write between the two polls and verified delivery to both holders. CPU writes + tracking, median µs/update across six interleaved runs per build (main / PR / fix, 128 pages): all pages written 296.0 / 57.6 / 57.6, every eighth page 43.1 / 18.6 / 18.6, four dense updates followed by two quiet updates 199.5 / 173.8 / 174.2. These timings cover the CPU tracker only. AI-assisted with Codex Astra 6.2 High. |
A clipped refresh or an outstanding host write can leave a cooled buffer page writable. Keep write-watch active through the final poll and preserve pending bytes for clipped copies and shared holders. AI-assisted with Codex Astra 6.2 High.
|
Awesome catch @Desamodeth — thank you for the reproducers, the clean fix, and the independent benchmark confirmation ( Cherry-picked
|
|
An interaction with #3585 (open, same
With both in, a watched page the title wrote without a fault is not in the changed set. The batch skips it, and the host copy keeps its old bytes. Whichever lands second needs the watched-page poll in AI-assisted (Claude Code, Claude Opus 5.5 read both heads and checked the lines). |
|
Good catch @upipibi — verified against #3585 ( Since both PRs target |
|
Exact head Linux/Wine 11, 16GiB host. CPU Win32/tracking only, no GPU dispatch. Fresh intended-tree build8.12s. Your existing sequential Interleaving:
This is the path the current sequential test cannot exercise. A global zero count is not synchronization with registration of the first watched page. Conservative correction: serialize arming with promotion under Evidence Test-only hook beside #[cfg(test)]
static LAB_ARM_HOOK: Mutex<Option<Box<dyn Fn() + Send>>> = Mutex::new(None);At the start of the #[cfg(test)]
if let Some(hook) = LAB_ARM_HOOK.lock().unwrap().as_ref() {
hook();
}The subprocess makes this isolated from other tests' global watched-page state; both rendezvous waits have ten-second deadlines: #[test]
fn arming_during_hot_promotion_keeps_before_image() {
const CHILD: &str = "VICTORY_LAB_ARM_RACE_CHILD";
if std::env::var_os(CHILD).is_none() {
let output = std::process::Command::new(std::env::current_exe().unwrap())
.env(CHILD, "1")
.args(["--exact", "watch::tests::arming_during_hot_promotion_keeps_before_image",
"--nocapture", "--test-threads=1"])
.output().unwrap();
assert!(output.status.success(), "{}\n{}",
String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr));
return;
}
// Isolated child: unrelated parallel tests cannot change WATCHED_PAGES.
let p = pool(1);
let t = track_copy(p, PAGE, true).expect("tracked write-watched pool");
assert_eq!(t.take(p, p + PAGE), [(p, p + PAGE)]);
for k in 1..u32::from(HOT_THRESHOLD) {
// SAFETY: owned pool page, with the normal write-fault handler.
unsafe { (p as *mut u32).write_volatile(k) };
assert_eq!(t.take(p, p + PAGE), [(p, p + PAGE)]);
}
// The next take promotes it, but arming first observes no watched pages.
unsafe { (p as *mut u32).write_volatile(0xAAAA_5555) };
assert_eq!(WATCHED_PAGES.load(Ordering::Acquire), 0);
let (ready_tx, ready_rx) = std::sync::mpsc::channel();
let (resume_tx, resume_rx) = std::sync::mpsc::channel();
let resume_rx = Mutex::new(resume_rx);
*LAB_ARM_HOOK.lock().unwrap() = Some(Box::new(move || {
ready_tx.send(()).unwrap();
resume_rx.lock().unwrap().recv_timeout(std::time::Duration::from_secs(10)).unwrap();
}));
std::thread::scope(|scope| {
let arm = scope.spawn(|| t.arm(p, p + PAGE));
ready_rx.recv_timeout(std::time::Duration::from_secs(10)).unwrap();
assert_eq!(t.take(p, p + PAGE), [(p, p + PAGE)]);
assert!(guards().watched.contains(&(p / PAGE)), "promotion really occurred");
resume_tx.send(()).unwrap();
arm.join().unwrap();
});
*LAB_ARM_HOOK.lock().unwrap() = None;
// An armed GPU-write range must retain these pre-write bytes.
unsafe { (p as *mut u32).write_volatile(0xDEAD_BEEF) };
let before = t.saved(p / PAGE).map(|bytes| u32::from_le_bytes(bytes[..4].try_into().unwrap()));
t.disarm();
drop(t);
free(p);
assert_eq!(before, Some(0xAAAA_5555), "arming lost its pre-write snapshot");
} |
|
Great catch @pickles976 — that Fixed in
Re-verified locally on Windows 11 x64 (RTX 3080 Ti):
|
Problem
On every submission,
watch::Tracked::take_splitre-guards modified 64 KiB pages withVirtualProtect(PAGE_READONLY)(triggering cross-core TLB shootdowns). For hot CPU-written buffer pages (ring buffers, per-frame constants, instance/command data written every frame), the title's very next CPU write immediately faults intoon_write(hardware#PF→ kernel exception dispatch → user-mode VEH → globalGUARDSmutex →VirtualProtect(PAGE_READWRITE)TLB shootdown → replay).victory-hlealready allocates runtime pools withMEM_WRITE_WATCH, andwatch.rsalready queriesGetWriteWatchto classify them — but never used hardware write-watch to track writes on hot pages.Approach
Keep
PAGE_READONLYguards for cold pages (preserving zero-syscallTracked::clean()viaDIRTYbits), section/aliased views, texture stagings, descriptor heaps, and armed GPU-write ranges, while adaptively promoting hotMEM_WRITE_WATCHbuffer-copy pages to batched hardwareGetWriteWatch:HOT_THRESHOLD = 3): After 3 consecutive guard faults on a page with no physical aliases, where every holder is an unarmed host buffer copy (copy == true),protect_notingcallsResetWriteWatch(which atomically verifies the page is aMEM_WRITE_WATCHallocation and clears its dirty bits) and moves the page intoGuards::watchedwithout callingVirtualProtect(PAGE_READONLY).WATCH_BATCH_PAGES = 64, up to 4 MiB per syscall):Tracked::take_splitgroups contiguous watched pages into runs of up to 64 pages and polls/resets them in a singleGetWriteWatch(WRITE_WATCH_FLAG_RESET, ...)call using a stack buffer sized for all64 × 16 = 1,024hardware 4 KiB subpages, propagating dirty bits to any other buffer copies sharing those pages.COOL_TAKES = 2): If a watched page sees 2 consecutive quiet takes with 0 CPU writes, it is demoted back toPAGE_READONLYso cold trackers return to zero-syscallTracked::clean().Tracked::armacquiresGUARDSbeforeSaved(matching global lock order) and atomically demotes any watched page in the armed range back toPAGE_READONLY(polling pending writes first) soon_writealways capturesChanged::savepre-write bytes before concurrent CPU writes land.Tracked::changedperforms a non-resettingGetWriteWatch(0, ...)peek on watched pages soShadow::copy_back_marked/Shadow::copy_back_pagenever falsely drop GPU writes on untouched hot pages.Tracked::taken,track(..., copy=false),title_memory_remapped,title_memory_aliased, andDrop for Trackedall cleanly demote/clear watched state with zero lost writes.Benchmarks (
--release, Windows 11 x64, Ryzen 9 5900X + NVIDIA GeForce RTX 3080 Ti)1. End-to-end 8-thread CPU writers + real D3D12
Shadow::updateon RTX 3080 Ti (512hot pages = 32 MiB × 100 frames,cargo test --release --locked -p victory-gpu -- watch::tests::bench_multithreaded_shadow_refresh_on_host_gpu --ignored --nocapture):PAGE_READONLY+ VEH)MEM_WRITE_WATCH)Shadow::update(take + 32 MiB copy)2. Single-threaded tracking-only A/B (
256hot pages = 16 MiB × 100 frames,cargo test --release --locked -p victory-gpu -- watch::tests::bench_hot_write_watch_vs_guards --ignored --nocapture):PAGE_READONLYguards + VEH:88.45 ms(0.88 ms/frame)MEM_WRITE_WATCH:19.42 ms(0.19 ms/frame) — 4.6× fasterVerification (Windows 11 x64, RTX 3080 Ti + WARP, Rust 1.98.1)
cargo run --locked -p xtask -- policy:191 files okcargo clippy --locked -p victory-gpu --all-targets -- -D warnings: 0 warningscargo clippy --locked -p victory-gpu --all-targets --all-features -- -D warnings: 0 warningscargo test --locked -p victory-gpu -- watch::: 21/21 passed (2 benchmarks ignored)VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --features diagnostics -- watch:: shadow:: bindless:: descriptor::: 56/56 passedwatch:: shadow:: bindless:: descriptor::: 55/55 passed (excluding the pre-existing NVIDIA 1M view-heap testdynamic_slots_are_held_until_released)