Repository navigation
GPU: write faults read the bytes they save without the guard lock (revives #3035) - #3754
Open
reallyvibe wants to merge 4 commits into
Open
reallyvibe wants to merge 4 commits into
reallyvibe wants to merge 4 commits into
Conversation
A benchmark of the guard lock under contention (issue Marvisak#400 puts about 940 write faults a frame at about 450 us each, mostly waiting for the lock): writer threads fault on a hot set of guarded pages while a submission thread arms and takes every tracker in rounds. Prints faults a second, fault latency (median, 99th percentile) and a take round's median per writer count. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 19856bc)
Twelve writer threads fault on guarded pages while a taker arms, takes and copies every tracker (and reports pages taken by copy-back): at the end every page whose copy differs from title memory must still be changed, and no write may fault past the handler. Against a version that made pages read-only outside the guard lock with a race between a stale fault and the take, it failed 2 of 10 release runs (the process died); main passes 20 of 20. The contention benchmark gains GUARD_BENCH_SPIN_US, the time each writer works between writes (closer to a title's fault rate than writing back to back). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 86510ac)
A thread opens 8-page ranges with `writing` every 200 us, as a file read or DirectStorage does, and the benchmark prints the median and 99th percentile of each (the opening and the marking after). Issue Marvisak#400 puts `writing` first among the guard lock's holders (174 ms a frame). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit a2189cb)
A write fault on a page a tracker is armed for saves the page's 64 KiB before opening it, and did so (an allocation and a kernel copy) while holding the guard lock that every title thread's write fault, every take and every kernel write wait for (issue Marvisak#400: about 450 us a fault, the lock held about 600 ms a world frame). The fault now reads the bytes with the lock released and keeps them only if the page stayed guarded meanwhile: a count per page (folded onto 4,096) is bumped under the lock whenever a page is opened, the only way a guarded page's bytes can change, and checked when the lock is taken again. Otherwise the bytes are read under the lock, as before. The bytes go to the page's own save only, not to its aliases'. A new test checks the oracle under contention: whatever a tracker saved of a page equals the copy taken of it; with the opened-count check removed it fails 10 of 10 runs. The concurrency tests now stop their writers when an assertion fails instead of hanging. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 9e0eaa8)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This revives #3035, which was closed without merging, and adds independent validation on native Windows. The optimisation is not mine: the four commits are #3035's, unchanged (the same tree as its head
9e0eaa81), applied cleanly ontomain63bcca43, and each one's(cherry picked from …)line records where it came from. The commits carry a neutral author identity. My part is the re-checking below and the coordination.Claim: #1633 (comment)
What it changes
watch.rs's write-fault handler saves a page's 64 KiB before its first write, for trackers armed on it. Today it reads those bytes while holdingwatch::GUARDS, so every other write fault and every take waits behind the copy.With this change, when the page is guarded and a tracker would save it, the handler:
OPENEDcount;Otherwise the bytes are read under the lock, as before (one extra 64 KiB read). Only the first armed tracker of the faulting page uses the read-ahead bytes. Alias pages and other trackers of the same page still read under the lock.
Why the kept bytes are the pre-write bytes
open()is the only place a page leavesprotected(the onlyprotected.remove). It bumpsOPENED[page]before callingVirtualProtect(PAGE_READWRITE).Every path that makes a guarded page writable while holding
GUARDSgoes throughopen(): write faults,writing(kernel writes and our own copy-backs), remaps and unmaps (title_memory_remapped), new aliases (title_memory_aliased), all throughchanged→open_guarded, and tracker drops (Tracked::drop).So "guarded at both ends, count unchanged" means no
GUARDSholder opened the page during the read, and title code could not have written it without faulting.Two kinds of writer change a guarded page's bytes without
GUARDS:write_physicalandPageWindow), which are reported only after they land;watch.rsdocuments).Neither ever took
GUARDS, so the lock onmaindid not exclude them either. The read still happens right after the handler first takes the lock, as onmain; only the time between the read and storing the bytes grows, and these writers cannot change bytes already read. Their exposure is unchanged by this PR, not fixed by it.The count is folded onto 4,096 slots. A collision only makes the check fail more often, and then the bytes are read under the lock.
The read goes through
ReadProcessMemory, which fails instead of faulting if the page was freed meanwhile; that case also falls back to the read under the lock.This argument comes from reading the code; the tests below check it under contention but cannot prove it.
Integration risk: pages opened outside
open()The argument above holds on
mainonly because every opening goes throughopen(). neonbyte1 found the case that breaks it on a stack that opens a whole run of guarded pages with oneVirtualProtectand removes them fromprotecteddirectly (comment): a fault's unlocked read can straddle a complete open, copy-back write and re-guard, find the page guarded with an unchangedOPENEDcount, and keep stale bytes. The fix there is to bumpOPENEDfor every page such a path opens. So whichever lands second of this PR and any of these must add those bumps:open_range, guarded pages opened in runs);3a2aa2fc);open_guardedopens runs and removes pages fromprotectedwithoutopen()).Known test failures, before and here
shadow::tests::partial_refresh_under_marked_range_updates_saved_bytesfailed once in about 160 full runs of the change (0 in about 170 runs of the base). Its author could not reproduce or explain it. It remains unexplained. Since then it passed in pawrenity's 10 release runs ofshadow::on GPU: write faults read the bytes they save without the guard lock #3035's head and in one run on the GPU here; neither is enough to rule out a rare failure.mainas well (dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views), and twodescriptor::hosttests failing now and then on both (views_bound_while_another_slot_gets_its_texture_do_not_wait,a_slot_rewritten_while_its_view_is_prepared_views_the_new_record), which a reviewer traced to the shared test hook GPU tests: each test's prepare hook runs on its own thread #2585 fixes. I did not run those here.Measured before it closed (others' numbers)
mainplus the unmerged PRs that setup needs (listed in Frame budget: where the time goes in the prison yard (RTX 5090, Linux/Wine), and which PRs move it #3425, including copy-back in page runs, the case above), not on plainmain:guard_contention(KainanP, comment):GUARDSwait is 0.55× ofmain(quartiles 0.51–0.60), and the refresh's falls from 8.7 to 0.05 ms;guard_lock_contention_benchtables reported a 2–4% loss of fault throughput when writers saturate the lock; fyernald measured a 5–6% gain there instead on another machine, with the 15-writer fault p50 up 15%. I did not rerun that benchmark.OPENEDcomparison replaced bytrue,saved_bytes_are_the_page_as_taken_under_concurrent_faultsfailed 10 of 10 release runs.I have no game files. Everything I ran is synthetic and says nothing about frame time.
Tested here
Windows 11 26H2 (build 26300), Ryzen 7 7445HS, RTX 4050 Laptop (driver 616.64), Rust 1.98.1 MSVC,
CARGO_BUILD_JOBS=1.main63bcca4victory-gpulib tests that need no GPU (debug)shadow::saved-byte and copy-back tests on the GPU (8)concurrent_writes_and_takes_lose_no_write, release, 20 runssaved_bytes_are_the_page_as_taken_under_concurrent_faults, release, 20 runsOPENEDcomparison replaced bytrue, 10 release runscargo clippy -p victory-gpu --all-targets -- -D warnings(default /--all-features)cargo fmt --all --checkcargo xtask policyNot run here: the other 91
victory-gputests (GPU and shader tests beyond those 8), and the rest of the workspace. Windows CI run 45754273382 (Tests, Clippy and diagnostics Clippy) succeeded on #3035's head, whose tree is identical to this branch's.The tests cannot show whether the read-ahead bytes are ever kept rather than read again: if the check always failed, every test would still pass. The benchmark below is what shows that.
Native-Windows contention benchmark
Same machine. KainanP's
guard_contention(ab96ee44, test-only, on63bcca43) applied to bothmain63bcca43and this branch, plus one identical addition: a write count, printed as writes per second (all writers' writes during the refresh, divided by its duration). Both test binaries were built with the same toolchain and settings (release,--locked). The benchmark'sWAITEDtimes every lock acquisition that findsGUARDStaken, in both builds; this branch's handler takes the lock twice when it reads ahead, and both waits count.Defaults otherwise: 256 MB pool, copies of 32 pages, 1,400 pages dirtied before each refresh, 11 counted rounds per run (each run's figure is its median). One warm-up run of each build per configuration, then 5 measured runs alternating
mainand branch (order flipped each repetition, 10 s pause before each run); nothing armed was extended to 10 runs because 5 left it ambiguous. Heavy background apps were closed; on AC power, Balanced plan.Medians over runs, with the observed range. "Writer wait" is the writers' total wait for
GUARDSper round; "refresh wait" is the refresh's.mainReading:
mainon writes/s, writer wait and refresh wait. The writers' wait is 0.64× ofmain's here (KainanP measured 0.55× on their machine).Limits: one laptop, synthetic workload, 5 runs per configuration (10 for nothing armed), no significance tests, no thermal readings, a few light background apps still running.
Reproduce, and every measured run
The write counter, applied to
watch.rson both sides after KainanP's commit:Steps (Git Bash; the same on both sides):
Each run's medians, from its
guard_contention:line, in the order each pair ran:mainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainInteractions
Changed::save. Whichever lands second rebases.page_bytes()could later use GPU: a page's saved bytes are read into an unzeroed buffer #3262's unzeroed read or GPU saved-page reuse with a 4 MiB idle limit #3720's pooled pages.open(): see "Integration risk" above.AI-assisted (Claude Code).