Skip to content

Memory: readable answers a short range from the working set - #332

Closed
Vaximous wants to merge 1 commit into
ivall:mainfrom
Vaximous:shader-readable-working-set
Closed

Vaximous wants to merge 1 commit into
ivall:mainfrom
Vaximous:shader-readable-working-set

Conversation

@Vaximous

Copy link
Copy Markdown

gpu::shader::readable checks title pointers with VirtualQuery, which sizes the whole region around the address by walking it, so its cost follows the size of the region and not of the range asked about. In GTA VI's world load the page arrays of its DirectStorage requests (8–32 bytes, checked on enqueue in snapshot_page_array and again in execute, #289) live in a 2259 MB block of committed memory: each check took 20–25 ms, one VirtualQuery each. Thread dumps taken while no frames were presented found the title's streaming thread in readable → VirtualQuery, called from queue_enqueue_request → snapshot_page_array, while its main thread waited.

readable now first asks K32QueryWorkingSetEx about all pages of a range of up to 16 pages, in one call. If every page is valid in the working set with a readable protection (judged as the existing walk judges Protect), the answer is yes. Any other page (never touched, paged out, guard, reserved, no access) falls back to the existing walk, so the answers are the same as before. The query touches no page: a guard page keeps its guard.

Overlaps: #254 (open) removes the same VirtualQuery cost in kernelx::committed_page only; it does not touch readable. #232 decodes the title's watchdog, which fires when its threads stall.

What is in it

  • Memory: readable answers a short range from the working set: resident_and_readable and its call in readable, with the test readable_answers_from_the_working_set_without_touching_pages (crates/xcl/src/gpu/shader.rs). The test covers written, never-touched, no-access, guard and reserved pages, ranges that cross into them, a long range that takes the walk, that a guard page is still a guard page after it was asked about, and that written pages are answered from the working set.

Tested

  • NVIDIA GeForce RTX 4070 Ti 12 GB, driver 617.42 (32.0.16.1742), 32-lane waves; AMD Ryzen 9 7900X; 64 GB; Windows 11 Pro build 26300; rustc 1.100.0; tests in debug, game runs in release.
  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4 on aa1a1ec: 653 passed, 0 failed, 40 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • GTA VI (1.0.2.1) on a local tree of main plus other open PRs, which gets the title into its world load (menus walked with XCL_PRESS_A_AT), with timing around readable added for the runs only:
    • before: 416 readable calls of 20 ms or more, about 10.2 s in total; time in readable 366 ms over the first 524k calls, 14.1 s by 1.05M calls.
    • with this change: 524,288 calls, 524,281 of them answered from the working set, 494 ms and 531 ms in total in two runs, no call of 20 ms or more.
    • an earlier form of the fix (same fast path, by probing instead of asking the working set) in the world load: DirectStorage requests after 140 s 61.9k against 23–25.5k without it; frames presented in 300 s 1864 against 1636–1708. The title still does not leave its loading screen: this removes one stall of its streaming thread, not the others.

Regression run
scripts/smoke.ps1 -Game <GTA VI> -Seconds 60, no input, release builds; main at 4017f8f (this branch's base). The gpu: line is the first adapter the script found; the title ran on the RTX 4070 Ti. The PC was in use for other work during both runs.

main:

xcl-smoke v1
commit: 4017f8ff8faaae6007fd83910d230a027dd85a33
gpu: Virtual Desktop Monitor
title: GTAVI
seconds: 60
exit: 0x0
frames shown: 1250 (PresentX 1251)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 14655 MB

this PR:

xcl-smoke v1
commit: aa1a1ec653eff9183e80c041bd60ec34092108fc
gpu: Virtual Desktop Monitor
title: GTAVI
seconds: 60
exit: 0x0
frames shown: 1386 (PresentX 1386)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14662 MB

Not tested

  • AMD GPUs and GPUs with 64-lane waves; titles other than GTA VI.
  • The world load on this exact commit built from main alone. The smoke runs above stay in the menus. With the menus walked (XCL_PRESS_A_AT=14,17,21,40,60,66,72, 240 s), main and this branch both crash at about 120–128 s with a write fault at game_scarlett_master.exe+0x13936, before the phase where the slow calls were measured; whole-run DirectStorage requests were 45,718 (main) and 46,184 (this branch).
  • The ~1M-call phase of the world load with the working-set form of the fix: the two timed runs ended in the title's watchdog (Retail GTA VI: null write at +0x2215c35 (#159) still hits on NVIDIA RTX 4070 Ti, with and without XCL_NETWORK=0 #232) at about 80 s, as an unchanged build of the same tree did in a run next to them, so the 524k-call figures above are from the menus and the start of loading.

AI use

  • Claude Code. Claude Opus 5.5: the diagnosis (thread dumps, symbol resolution, timing), the code and the test. Claude Fable 5.2: planning, review of the diff (it replaced a first version that probed pages and would have consumed guard pages) and this description. Runs and tests were made on the hardware named above.

gpu::shader::readable checks title pointers with VirtualQuery, and
VirtualQuery sizes the whole region around the address by walking it, so
its cost follows the size of the region, not of the range asked about.
In GTA VI's world load the page arrays of its DirectStorage requests
(8-32 bytes, checked when a request is enqueued and again when it runs)
live in a 2259 MB block of committed memory: each check took 20-25 ms,
one VirtualQuery each. 416 calls of 20 ms or more took ~10.2 s, and time
in readable went from 366 ms over the first 524k calls to 14.1 s by
1.05M calls. Thread dumps taken while no frames were presented found the
title's streaming thread in readable -> VirtualQuery, called from
queue_enqueue_request -> snapshot_page_array, while its main thread waited.

readable now asks K32QueryWorkingSetEx first, for all pages of a range of
up to 16 pages in one call. When every page is valid in the working set
with a readable protection (no PAGE_NOACCESS, no PAGE_GUARD, as the
VirtualQuery walk judges Protect), the answer is yes. Any other page
(never touched, paged out, guard, reserved, no access) falls back to the
existing walk, so every answer is the same as before. The query touches
no page: a guard page keeps its guard.

Measured on the integration tree, with timing added only for the run:
524,288 calls, 524,281 answered from the working set, 494 ms in total,
none of 20 ms or more. Both runs crashed in a loading pause before the
world load, so the ~1M-call phase where the slow calls were measured was
not reached; an unchanged build crashes there the same way.

Assisted-by: Claude Code (Claude Opus 5.5)
@throny

throny commented Nov 24, 2026

Copy link
Copy Markdown

Test report for aa1a1ec, merged onto current main 2b4bb43 (clean merge, merge commit 5128f81): pass.

Tested

  • Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 4080 (driver 610.88), Intel i9-12900K, 64 GB; Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace && cargo test --workspace -- --test-threads=4: 671 passed, 0 failed, 41 ignored (lib: 631 / 0 / 21, readable_answers_from_the_working_set_without_touching_pages ok)
  • SKIPPED on this adapter: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)
  • Control: with the new call in readable disabled (if false && resident_and_readable(..)), gpu::shader::tests still pass (4/4). So every readable answer in the new test is the same with and without the fast path, which is what the description claims. It also means only the direct resident_and_readable asserts check the fast path; no test fails if readable stops calling it.

Reviewed (read the whole diff)

  • PSAPI_WORKING_SET_EX_BLOCK: bit 0 Valid, bits 4–14 Win32Protection, as the code reads them. Valid is checked first, and the protection test is the same one the VirtualQuery walk applies (!= 0, no PAGE_NOACCESS / PAGE_GUARD).
  • p != 0, len != 0 and the overflow check run before resident_and_readable, so end - 1 cannot underflow.
  • Overlap: DirectStorage: EnqueueRequest reads its page array and file vtable without sizing regions #322 removes the same VirtualQuery cost on the same path (snapshot_page_array and the file vtable check) by not calling readable there. The two do not conflict textually, but each makes part of the other's measured gain moot.

Not tested

  • The timing claims (no timing run here), no regression run (scripts/smoke.ps1) on a title, no XCL_SAMPLE_* game-data tests.
  • AMD and wave64 hardware, release build.

AI use

  • Claude Code (Claude Opus 5.5) ran the build, the tests and the control, read the diff and wrote this report. The runs above are real, on the hardware named.

@Cattn

Cattn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Remove the overlap with #322

ivall added a commit that referenced this pull request Nov 24, 2026
… Vaximous)

gpu::shader::readable checked title pointers with VirtualQuery, which sizes the whole region
around the address by walking it, so its cost follows the region, not the range. In a retail
game's world load the page arrays of its DirectStorage requests (8-32 bytes, checked at
enqueue and again at execute) live in a 2 GB block of committed memory: each check took
20-25 ms. readable now asks K32QueryWorkingSetEx about up to 16 pages first, in one call; only
a page not valid in the working set falls back to the walk.
@ivall

ivall commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Merged as a squash commit (a6f5f7a) with the game's name left out of the comment and the message - thanks! Checked on an RX 9070 (RDNA4) on main a6f5f7a, together with #335, #322, #331, #330, #338, #318 and #332: cargo test --workspace -- --test-threads=4 - 636 unit tests passed, 0 failed; the retail game reaches its menu and draws it (frame dump) and goes on past it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants