Repository navigation
GPU: avoid heap allocations and batch unprotects in watch fault hot path - #3343
danielibabet wants to merge 2 commits into
Conversation
Dukestar69
left a comment
There was a problem hiding this comment.
Tested on Windows against origin/main (63bcca4). cargo test -p victory-gpu --lib watch:: -- --test-threads=1 (15/15), clippy, and xtask policy pass, but there is a bug in the batched open_guarded path:
- In
open_guarded(crates/victory-gpu/src/watch.rs), the batchedVirtualProtect((start * PAGE) as *const _, (end - start) * PAGE, PAGE_READWRITE, &mut old)ignores its return value after removing all pages instart..endfromg.protected. - On Windows,
VirtualProtectfails atomically (0) if[start, end)spans two separateVirtualAlloc/MapViewOfFileallocations (differentAllocationBase) or includes a page that was unmapped. When that happens, none of the still-mapped pages in the run are restored toPAGE_READWRITE, yet they have already been removed fromg.protected, so the next write to any of them will trigger an unhandled access violation instead of being handled by the watch fault handler. - Compare
protect_notingright above it, which checks theVirtualProtectreturn value and falls back to per-pageVirtualProtecton failure. CheckingVirtualProtect(...) == 0inopen_guardedand falling back to per-pageVirtualProtect(oropen) forstart..endfixes this cleanly. - Minor:
first + 1 == lastcan overflow iffirst == usize::MAX(e.g.first.checked_add(1) == Some(last)is safer).
Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
|
Good catch @Dukestar69! Fixed both in
Re-ran |
fuzeofficlal
left a comment
There was a problem hiding this comment.
Verified locally merged onto origin/main (63bcca43) at PR head 65d26e3c (Windows 11 x64, Ryzen 9 9950X, NVIDIA RTX 5090 + Microsoft WARP, VICTORY_D3D12_DEBUG=1):
crates/victory-gpu/src/watch.rs:changed(L717-L722): single-page non-aliased fault fast path avoids heap allocations and range scans underGUARDSwhile preservingmark_changed->openordering and return semantics;first.checked_add(1) == Some(last)preventsusize::MAXoverflow.open_guarded(L746-L787): batches contiguous guarded runs into singleVirtualProtect(..., PAGE_READWRITE)calls, checks!= 0before removingstart..endfromg.protected, and falls back to per-pageopen(g, page)when a batched call fails (e.g. across allocation boundaries or decommitted pages).aliases(L290-L293) andchanged(L724-L728): skipalias_oftraversal when empty.
cargo xtask policy: 191 files ok.cargo clippy --locked -p victory-gpu --all-targets -- -D warnings(default and--all-features): 0 warnings.cargo test --locked -p victory-gpu --features diagnostics --lib -- watch::: 15/15 passed on both RTX 5090 and WARP.- Extra adversarial synthetic probe (not committed):
- Decommitted the middle page (
MEM_DECOMMIT) of a 6-page guarded run beforewriting(p, 6 * PAGE), triggering the batchedVirtualProtectfailure path and confirming per-pageopenremoved all 6 entries fromg.protectedand restoredPAGE_READWRITEon all 5 remaining pages without faulting on subsequent writes. - Verified non-contiguous runs (
[0, 1]and[4, 5]),usize::MAXboundary calls, and an 8-thread concurrent write-fault stress test across 64 guarded pages x 20 rounds (1,280 faults in 8.67 ms, 100% captured).
- Decommitted the middle page (
- Also verified a clean co-merge with
#3341(both touchwatch.rs): 0 clippy warnings and 26/26watch::+shadow::tests passing.
Synthetic tests only; looks ready to merge.
|
Measured in game on an RTX 5090 (driver 620.73.04), Linux, GE-Proton11-14 Wine with its vkd3d-proton, GTA VI's opening prison yard, warm cache; base is
About 1 ms off copy-back, within noise otherwise. For context, our counters put the copy-back of marked pages at ~51 ms/frame here, of which page writes and guard lease/protect rounds are ~94% of the thread time (2,829 lease rounds per frame) and the title reads ~3.5%; the single-page fast path doesn't reach the multi-page runs that dominate in this scene. AI-assisted (Claude); measured on my machine, no game data shared. |
|
Contention numbers for this PR from a shared benchmark (for #3425). Method. The benchmark
Reading. With 4 writers the writers' time in their writes and their wait for Correctness. AI use: Claude Code (Claude Opus 5.5). |
|
Local benchmark of the fault fast path,
So the gain is small and shows up only under contention: about 5-8 % at 8 threads and 3-6 % at 16, nothing measurable with 1-4. That fits what the PR changes: each fault's cost is mostly the kernel's exception dispatch, and this trims the user-side work done under AI-assisted (Claude Code, Claude Opus 5.5 wrote and ran the local benchmark). |
|
Offscreen verification on Linux/Wine 11, RTX 4060 Laptop / 16 GiB RAM:
The tests cover armed before-images, alias writes, retakes, foreign/unmapped pages, overflow and failed range-opening fallback. The integrated suite initializes the physical NVIDIA device, but these are Win32 tracking/byte-consistency checks, not GPU shader execution or a measured frame-rate gain. Why we selected it: our actual rendering profile has substantial protection/VEH cost (14 late copy-stat reports: medians 852.6 ms protecting, 762.5 ms in VEH, 107.3 ms moving bytes; nested/cross-thread timers overlap and must not be summed). Your change targets opening/handler overhead, not all read-only 28 Nov follow-through: the changed mixed stack rendered palms/buildings/lights, but no matched title speedup is established. Fifteen late reports still show median 822.7 ms watches, 887.5 ms page protection and 447.3 ms fault handling versus 133.2 ms moving bytes; these overlap and reflect different workload/caches from the earlier profile. The trial then hit NVIDIA Xid109/CTX SWITCH TIMEOUT in a 25-list submission, followed by the title watchdog. This is not attributed to #3343; per-command/checkpoint traces were disabled. Title/GPU runs are parked. Full chronology, hardware/provenance, performance and limitations are in the updated progress report. No resource threshold was raised or shader work bypassed. |
Summary
watch::changedfor single-page write faults without aliases (which covers nearly all vectored exception handler faults inon_write), skipping bothVecallocations andalias_of/protectedrange scans under the globalGUARDSmutex.alias_ofiteration inwatch::aliaseswhenalias_ofis empty, and skipalias_ofrange collection inwatch::changedwhenalias_ofis empty.watch::open_guardedinto singleVirtualProtect(..., PAGE_READWRITE)calls instead of one syscall per page, with a single-page fast path.Verification
cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views:229 passed; 0 failed; 1 ignored; 2 filtered outcargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warningscargo run -p xtask --locked -- policy:asset policy: 191 files ok