Repository navigation
GPU: write faults read the bytes they save without the guard lock (revives #3035) - #3754
reallyvibe wants to merge 5 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)
|
NVIDIA row for this PR: its tests are CPU-only ( RTX 3070 (desktop), driver 616.56, Windows 11 Pro 10.0.26100. Head
Nothing NVIDIA-specific: the code and its tests are about CPU page guards, and the GPU plays no part. Not tested:
AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report. Update for the new head |
A write fault keeps the bytes it read without the guard lock only if the page is still guarded and its OPENED count did not move, so every path that takes a page out of the guarded set must move its count: alias pages too, and every page of a run opened at once. The test opens a guarded three-page run, and the three pages linked to it as another mapping, through changed() while holding the guard lock, and checks that each page's count moved. It uses no threads or timing: every count change is made under that lock, and the six pages are consecutive, so each has a count of its own. A batched open that takes pages out of the guarded set without moving their counts fails it. Test only; no production code changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @upipibi for the RTX 3070 run, including the AI-assisted (Claude Code). |
This revives #3035, which was closed without merging, and adds independent validation on native Windows. The optimisation is not mine: the first four commits are #3035's, unchanged (together the same tree as its head
9e0eaa81), applied cleanly ontomain63bcca43, and each one's(cherry picked from …)line records where it came from. They carry a neutral author identity. The fifth commit,5c4bdd6d, is mine and test-only: a deterministic regression test for theOPENEDinvariant (see "Integration risk"). It is the only difference from #3035's head; no production code differs. My part is that test, 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()).opening_guarded_pages_moves_each_ones_opened_count(5c4bdd6d) checks the invariant directly. It opens a guarded three-page run, and the three pages linked to it as another mapping, throughchanged()while holding the guard lock, and asserts that each page's count moved. It uses no threads or timing, and the six pages are consecutive, so each has a count of its own. It covers every opening that goes throughchanged()(write faults,writing, remaps, new aliases), not a path that bypasses it.#3745 (
de7ba3fa) merges with this PR without a textual conflict, and the result drops the bumps. I checked that only as awatch.rssimulation, not a full integration: this branch's tree with the mergedwatch.rs, without #3745's other files.watch::testson the simulationsaved_bytes_are_the_page_as_taken_under_concurrent_faultsalso failed in 4 of 4 runs ("saved other bytes than taken")open_guardedKnown 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.
Validated by others on this PR
b86f2ed: GPU: write faults read the bytes they save without the guard lock #3035's two concurrency tests passed 3 of 3 runs each in debug, withVICTORY_WARP=1, and in release;main's code abovemod testsand this PR's tests kept, the same tests also passed 3 of 3 in each mode, as expected:mainnever keeps bytes read without the lock;OPENEDcomparison replaced bytrue):saved_bytes_are_the_page_as_taken_under_concurrent_faultsfailed in 3 of 3 debug and 3 of 3 release runs ("page N saved other bytes than taken");concurrent_writes_and_takes_lose_no_writepassed in all 6;victory-gpulib suite: 231 passed, 2 failed, 2 ignored on this PR, and 229 passed, 2 failed, 1 ignored onmain63bcca4. The same 2 fail on both (dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views), the NVIDIA failures GPU: write faults read the bytes they save without the guard lock #3035 reported onmain;5c4bdd6:watch::18 passed, 1 ignored, in 3 of 3 debug and 3 of 3 release runs;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)guard_lock_contention_bench, plusopening_guarded_pages_moves_each_ones_opened_count; per-test outcomes otherwise identical)shadow::saved-byte and copy-back tests on the GPU (8) †concurrent_writes_and_takes_lose_no_write, release, 20 runs †saved_bytes_are_the_page_as_taken_under_concurrent_faults, release, 20 runs †opening_guarded_pages_moves_each_ones_opened_countalone, 20 runs each: debug, release,--all-featuresdebug and releasewatch::testswith--all-features, debug and releaseOPENEDcomparison replaced bytrue, 10 release runs †cargo clippy -p victory-gpu --all-targets -- -D warnings(default /--all-features)cargo fmt --all --check5c4bdd6d, rechecked forwatch.rsonly, the one file it changes: rustfmt reports nothing in the new test)cargo xtask policy† Run on
b86f2eda, before the test-only commit; that commit changes nothing these runs exercise. Every other row was run on5c4bdd6d.Correction: an earlier version of this description gave the branch's no-GPU count as "143: 142 passed, 1 ignored (the 2 new tests)". #3035 adds 3 tests, one of them an ignored benchmark, so that tree had 144: 142 passed, 2 ignored.
Not 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 tob86f2eda; this PR's own checks cover5c4bdd6d.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).