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

Merged
frozht merged 1 commit from upstream-pr/556 into main 2026-11-24 09:43:32 +00:00
frozht commented 2026-11-24 01:44:57 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by frozht on 2026-11-24T01:44:57Z.


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

<!-- greyfinch-sync:ivall/xcl:issue:6572705725 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/556). Originally posted by [frozht](https://github.com/frozht) on 2026-11-24T01:44:57Z. --- `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](https://claude.com/claude-code)
thehollowedone commented 2026-11-24 07:05:23 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by thehollowedone on 2026-11-24T01:49:19Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7119478257 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/556#issuecomment-7119478257). Originally posted by [thehollowedone](https://github.com/thehollowedone) on 2026-11-24T01:49:19Z. --- **Tested** Native Windows 11 build 26300, Ryzen 7 9800X3D, Rust 1.99.1 MSVC, debug, at [38e58e1](https://github.com/ivall/xcl/commit/38e58e1d17c7c99abde4994274fe199c2b5dbc59). `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.
upstream-sync closed this pull request 2026-11-24 10:04:06 +00:00
fromthemoonx commented 2026-11-24 10:04:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-11-24T09:10:04Z.


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 #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.
<!-- greyfinch-sync:ivall/xcl:comment:7127046399 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/556#issuecomment-7127046399). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-11-24T09:10:04Z. --- 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 #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 commented 2026-11-24 10:04:49 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-24T09:43:34Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7127803328 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/556#issuecomment-7127803328). Originally posted by [ivall](https://github.com/ivall) on 2026-11-24T09:43:34Z. --- 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 in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!551
No description provided.