Skip to content

Memory: map_pages asks VirtualQuery once per run of fresh pages - #556

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
frozht:frozht/xcl-mappages-stall
Nov 24, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
frozht:frozht/xcl-mappages-stall

Conversation

@frozht

@frozht frozht commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

map_pages (XMemMapPhysicalPages) asked committed_page about the address and about the home of every page. A fresh page is not in the working set, so each one fell through to a VirtualQuery, which walks the whole run of pages that are in the same state. A retail title maps 98,304 fresh 64 KiB pages (6 GiB) in one call at startup: two VirtualQuery calls per page, a ~13 s stall under Wine (~132 µs a page). #254 only covers pages that are already in the working set.

The loop now keeps the run that each VirtualQuery returned, one for the addresses and one for the homes, and answers the next pages from it. Both runs are dropped whenever unmap_away commits or decommits memory and whenever move_home_away gives homes back (every 4096 pages). Both functions now return whether they did. Nothing else in a step changes a state that is asked about again. The one exception is an address that is also another page's home (a home in plain memory), and the answer there was already wrong before this change.

No issue covers this one. #156 mentions XMemMapPhysicalPagesScatter but is about NVIDIA PSO builds.

Open #531 and #849 each add a branch to the same loop that moves a page from its old address to the new one, decommitting the old address. That branch has to drop both runs, because the old address can lie in a run that was already answered: if either lands first, I add that here when rebasing; if this lands first, they need it.

What is in it

  • Memory: map_pages asks VirtualQuery once per run of fresh pages, in crates/xcl/src/hle/kernelx.rs: StateRun and committed_page_in (committed_page now calls it with an empty run); unmap_away and move_home_away return whether they changed memory; map_pages drops the runs when they do. A cfg(test) thread-local counts the VirtualQuery calls.
  • Four new tests in the same file's mod tests:
    • mapping_fresh_pages_does_not_query_each_page: 64 fresh pages with one committed page among them.
    • mapping_lazy_pages_again_does_not_query_each_page
    • pages_mapped_again_at_another_address_keep_what_was_written
    • pages_mapped_again_at_the_same_address_keep_what_was_written

Tested

  • Linux (Nobara, kernel 7.3), wine-staging 11.22 + vkd3d-proton, AMD RX 7900 XT (RADV). Cross-built for x86_64-pc-windows-gnu, release.
  • cargo test ... -p xcl --lib hle::kernelx under Wine: 28 passed, 0 failed, 1 ignored.
  • The two query-count tests fail without the fix: 127 VirtualQuery calls for 64 fresh pages (now 5), and 128 for the same pages mapped again (now at most 8). The two remap tests fail if the runs are not dropped after unmap_away moves a page home.
  • Scratch timing of the same call shape under Wine: 16,384 fresh pages took 3.05 s before (186 µs a page) and 4.2 ms after (0.26 µs a page).
  • Retail title startup, 120 s runs without input, two each, on main 7ebf1b8 and on 7ebf1b8 + this commit: the first title activity in xcl.log came at 20.1 s and 18.1 s before, 6.2 s and 6.0 s after; the first present at 24.7 s and 25.0 s before, 8.8 s and 13.5 s after. All four runs exited normally, with no device removal.
  • cargo test --release --target x86_64-pc-windows-gnu --workspace -- --test-threads=4 under Wine, on this commit before the rebase onto 508a0c7 (same diff): 674 passed, 6 failed, 41 ignored, 0 SKIPPED. The 6 failures are the same 6 as on main under Wine (origin/main 2b4bb43: 664 passed, 6 failed, 41 ignored): gpu_f64_roots, gpu_f64_math_random, gpu_nonfused_mad_and_mac_prevent_fma_contraction, e2e test_title_runs_and_game_folder_is_untouched, disk_free_space_ex_reports_pls_quota, set_predication_buffer_state_and_kickoff.

Regression run
Not run: this was built and tested on Linux under Wine, with no Windows machine available. Could someone on Windows run scripts/smoke.ps1 -Game <folder> on main and on this PR's head and paste both xcl-smoke blocks? The Wine runs of the retail title above are the closest check I have.

Not tested

  • Windows (only Wine), and so scripts/smoke.ps1.
  • The full suite after the rebase onto 508a0c7 (it ran on the same diff on a6f5f7a).
  • NVIDIA (and any GPU other than the RX 7900 XT).
  • The path where move_home_away gives homes back every 4096 pages and drops the runs: no test reaches it.

AI use

  • Claude Code. Claude Sonnet 5.5 wrote the code and the tests. Claude Opus 5.5 reviewed them and wrote this description; the work was coordinated by Claude Code with Claude Opus 5.5.

🤖 Generated with Claude Code

`map_pages` asks `committed_page` for the address and for the home of every page; a page not
in the working set (every fresh one) fell through to a VirtualQuery, which walks the whole
run of pages in one state. A title that maps 98,304 fresh pages (6 GiB) in one call took
~13 s under Wine (~132 us a page); a scratch run of the same call shape measured 186 us a
page for 4096 and 16384 pages, 0.26 us a page now.

The loop remembers the run each VirtualQuery returned for the addresses and for the homes
and answers the next pages from it. The runs are dropped whenever `unmap_away` commits or
decommits (it now says so; a page never used is only unmarked and keeps them, so mapping lazy
pages again is covered too) and when `move_home_away` hands homes back (every 4096: it
now says so). The rest of a step changes only an address, or a home that is not asked about
again: the addresses rise, and a page mapped twice is unmapped first. The one exception
is an address that is another page's home (a home in plain memory): the answer for it
was wrong before the runs too.

Tests: 64 fresh pages with a committed one among them took 127 VirtualQuery calls before,
5 now; the same pages mapped again, 128 and at most 8. Pages written and mapped again in
one call, at another address or at the same one, keep their contents (they fail when the
run is not dropped after `unmap_away` moved a page home). The counter behind the first two
is a `cfg(test)` thread-local in `committed_page_in`.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@thehollowedone

Copy link
Copy Markdown

Tested

Native Windows 11 build 26300, Ryzen 7 9800X3D, Rust 1.99.1 MSVC, debug, at 38e58e1. cargo test --offline --locked -p xcl --lib hle::kernelx::tests -- --test-threads=1 --nocapture reported 26 passed, 0 failed and 1 ignored. In a scratch copy I made the new tests require sparse homes instead of returning early and printed their query counts. The runtime code was unchanged.

  • Mapping 64 fresh pages with one already committed took 4 VirtualQuery calls.
  • Remapping 64 untouched lazy pages took 2 calls at the same address and 2 at a different address.
  • Both same-address and different-address remaps preserved the written values in the new tests.

Not tested

The 4,096-page eviction boundary, integration with #531, the full workspace, and retail smoke or startup timing.

AI use

Codex with GPT-6.2 Sol added the scratch-only test assertions and counters, ran the tests, and drafted this report.

@fromthemoonx

Copy link
Copy Markdown

NVIDIA run, since Not tested lists NVIDIA. This is the whole suite on native Windows, both at the head and merged onto current main.

Tested

  • NVIDIA GeForce RTX 3060 12 GB (32-lane waves), driver 32.0.16.1088; AMD Ryzen 7 5700X; native Windows 11 Pro 26H2 (build 26300.9974); rustc 1.100.0 stable-x86_64-pc-windows-msvc; DXC 1.9.2602.17 (Windows SDK 10.0.26100.0)
  • Exact head 38e58e1d17c7c99abde4994274fe199c2b5dbc59 (on its base 508a0c7), clean worktree, debug: cargo build --workspace, then cargo test --workspace -- --test-threads=4: 684 passed, 0 failed, 41 ignored (lib 644 / 0 / 21). All four new tests passed: mapping_fresh_pages_does_not_query_each_page, mapping_lazy_pages_again_does_not_query_each_page, pages_mapped_again_at_the_same_address_keep_what_was_written, pages_mapped_again_at_another_address_keep_what_was_written.
  • Merged onto current main 077ce40 (git auto-merged kernelx.rs, no conflict), debug: cargo test --workspace -- --test-threads=4 --skip a_freed_block_is_decommitted_before_its_address_is_reused: 719 passed, 0 failed, 41 ignored, 1 filtered out (lib 678 / 0 / 21). The skip is Memory: a sparse alloc/free loop makes a new 64 MiB pool each cycle; lib tests fill a 12 GB card (#348's test) #913: on this 12 GB card, that test fills video memory and makes unrelated tests fail with E_OUTOFMEMORY.
  • cargo test -p xcl --lib -- --test-threads=4 hle::kernelx::tests on the merged tree, 10 runs: 10 of 10 passed (27 passed, 1 ignored each)
  • Release, at the head: cargo test --release -p xcl --lib -- --test-threads=4 hle::kernelx::tests: 26 passed, 0 failed, 1 ignored
  • SKIPPED (no wave64 on this adapter): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware
  • XCL_SAMPLE_* not set

Not tested

  • The speed-up itself: no retail title here (no smoke.ps1) and no timing of a 6 GiB XMemMapPhysicalPages call, so the ~13 s Wine stall is not measured either way. The new tests check the query count, not time.
  • Under Wine.

AI use

  • Claude Code (Claude Opus 5.5) ran the builds and tests on the machine above and wrote this report from the logs.

@ivall
ivall merged commit 3ca8566 into xcl-dev:main Nov 24, 2026
0 of 4 checks passed
@ivall

ivall commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Merged (9aec53d). Checked on an RX 9070 (RDNA4) on main 9aec53d together with #532 and #556: cargo test --workspace -- --test-threads=4 - 683 unit tests passed, 0 failed; the retail game plays its intro and menus at 30 fps as before and starts loading the world. Conflict with #849 (merged before): its move_away branch now drops both runs when a page actually moves (Ok(true)), and keeps them for a lazy page that is only unmarked (Ok(false)), which mapping_lazy_pages_again_does_not_query_each_page requires.

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