perf(gpu): reduce watch GUARDS lock contention during holes, protect, and kernel I/O #513
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!513
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-513"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Not4Eric on GitHub · 2026-12-04 20:24 UTC · original
Not4Eric:perf/watch-reduce-guards-contention→main· +44 −21 · 1 files · 1 commits · merged 2026-12-05 17:29 UTCWhat this does
Reduces
watch::GUARDSwrite-lock and per-block claim contention between faulting title threads, refreshes, and kernel I/O:guards_shared()instead of exclusiveguards()when sampling blocks inwatch::look_at_holes().protect_claimed()to chunks ofPROTECT_CHUNK = 32blocks so faulting title threads wait on at most a small chunk instead of an entire multi-megabyte run.GUARDS(under a per-blockClaims::wait_for) before takingguards_timed()inwatch::reading()andwatch::kernel_writing(), keepingVirtualAlloc/MapViewOfFilesyscalls off the globalGUARDSwrite lock while still re-checking underguards_timed()for any protected or racing pages.Checks
look_at_holes,protect_claimed,reading, andkernel_writingincrates/victory-gpu/src/watch.rs.watch::testsdirectly exercisereadingandkernel_writingsemantics; no prior behavioural bug existed for a failing-before unit test.um/residency.rsis untouched here) and #428 (many-viewsclean bit).cargo xtask policypassed (360 files ok); no fork-only files, configs, or build outputs.Verified
cargo xtask policy—360 files okPKG=victory-gpu ./build.sh test --lib -- watch::tests::a_refresh_leaves_open_what_the_kernel_is_writing watch::tests::kernel_reads_into_guarded_blocks_after_opening --test-threads=1(Proton Wine on Linux) —2 passed; 0 failed, 0 warnings (note: 4 unrelatedwatch::tests relying on native WindowsMEM_WRITE_WATCH/MEM_RESERVE_PLACEHOLDERbehaviour fail under Wine)Cost
Small extra
VirtualProtect+elsewherewalk per 32-block chunk inprotect_claimedinstead of once per run (traded for much shorterGUARDSwrite-lock and per-block claim stalls); 0 new warnings.AI-assisted: yes
@Kal0s-design on GitHub · 2026-12-04 21:15 UTC · original
Checked
aeac5b0on native Windows (real VirtualProtect/GetWriteWatch, didn't run the game), next to current main.watch::tests, 12 runs per side (6 with one test thread, 6 with two). Everything passes every time exceptthe_rest_of_the_one_asking_has_a_number_of_its_own, which failed 11 of 12 here and 11 of 12 on main. Alone it passes 10 of 10 on both, so it's an ordering flake main already has. Fullvictory-gpu --libwith two threads, three runs per side, shows the same native-only failures main has (the twoindirecthost_list tests, the twopipeline::hostlibrary loads) plus that flake now and then. Clippy adds nothing new in watch.rs, with or withoutdiagnostics.I also ran a throwaway stress test, not pushed. It uses a 256-block view with 6 threads writing and asking about ranges of up to ~240 blocks, so
protect_claimedruns cross several 32-block chunks. Next to them one thread loopskernel_writing()(writing into the range while it's held) andreading(), and another asks about a half-mapped placeholder and callslook_at_holes(), soheldisn't empty. 15 runs per side at 4000 iterations per thread gave no lost write, no hang and no stray access violation. Withlook_at_holes()in a tight loop the writers finished in about 10.7 s here against 16.3 s on main. Without that thread the two are the same (about 3.6 s each). So in this test the gain comes from the shared lock inlook_at_holes, which the game only calls once a frame, and the chunking itself didn't change the timing.The lazy-page pre-pass wasn't exercised.
each_pendingreturns at once unless the UMA lazy pool is active, so in the testreading()returns early andkernel_writing()only runs the old part. That part is from reading only.On the code
look_at_holes(watch.rs:1727) only readsheld(atomic bits) andFROMunder the shared lock, and the removal still happens underguards()at 1748. A stale snapshot was already possible on main, so this looks fine.protect_claimed(2227) lets go of claims after each chunk, the wayguard_sharedalready does between runs. A block is still set inPROTECTEDand made read-only only while its own claim is held (2400/2453 throughprotect_run, 2361 throughcover, always givenSome(claims)), and a fault waits for that claim (1551). So a fault in a later chunk finds the block either not guarded yet or fully guarded. The cost is oneVirtualProtectand oneelsewherewalk per 32 blocks instead of per run, which is small but not quite none.BLOCK == PAGE == 64 KiB, so one claim covers one lazy page, and everyPROTECTED.setis under the block's claim, so the check andmaterializecan't race a protect.watch::retire(1612) takes no claim and can land between the pre-pass andguards_timed(). A retired page is KEPT without REAL, whichkind()(um/lazy.rs:244) still calls Pending, so the second pass makes it again. An unbind or release racing it goes through the same per-page lazy lock (um/lazy.rs:565, 431), so a latematerializejust sees it's no longer bound.title_memory_unmapping(2856) only clears watch state, and either order is fine there.GUARDS, then drops it beforeguards_timed(). Nobody holding a claim waits forGUARDSalone, and claim, then lazy page lock, thenMOVINGread is the order the fault path already uses (1551-1553). The fault handler itself isn't touched. TheBUSYdoc (1174-1176) still says claims are taken holdingGUARDSshared, which these two callers no longer do.Nothing blocking from my side.
AI-assisted: yes
aeac5b0931tof768c44b12@Not4Eric on GitHub · 2026-12-05 00:00 UTC · original
Thank you so much for helping, I'm not the best at this and I'm heavily relying on AI to even do this
Pushed updates addressing your review:
BUSY's doc comment inwatch.rsto reflect thatreadingandkernel_writingalso claim blocks viaClaimswhile materializing lazy pages before acquiringGUARDS.@Kal0s-design on GitHub · 2026-12-05 00:59 UTC · original
Re-checked
f768c44on native Windows (didn't run the game).The BUSY doc at watch.rs:1174-1177 now covers reading and kernel_writing claiming blocks before GUARDS, which matches the code. Nothing else changed. The full lib run shows only the four failures main already has natively plus the ordering flake in the_rest_of_the_one_asking_has_a_number_of_its_own (it failed 2 of 3 watch:: runs here, and main has the same flake), clippy has nothing new in watch.rs, and it still merges cleanly onto current main.
Two leftovers, neither blocking. The lazy-page pre-pass still isn't covered by any test, since each_pending returns at once without the UMA lazy pool, so that part is reviewed from reading only. And "Cost: none" in the description could mention the extra VirtualProtect per 32 blocks in protect_claimed, small as it is.
AI-assisted: yes
github-bot referenced this pull request2026-12-05 11:23:55 +00:00
github-bot referenced this pull request2026-12-05 11:23:58 +00:00