startup: probe DirectStorage destinations instead of walking them with VirtualQuery #2685

Manually merged
github merged 2 commits from gh-pr/2685/head into gh-pr/2685/base 2026-11-27 01:14:44 +00:00
Owner

@⁠lukaslw-lab on GitHub · 2026-11-26 02:00 UTC · original
lukaslw-lab:runtime/dstorage-destination-probe → main · +106 −5 · 3 files · 2 commits · merged 2026-11-26 03:19 UTC

Change

Fixes #2684 (DirectStorage destination check costs ~10 ms of kernel time per request).

destination_writable (#1672) keeps its verdicts but no longer calls VirtualQuery. It probes one byte per 4 KiB page of the declared capacity with an atomic OR of zero under a structured exception handler.

  • local_log.h: x2w_local::probe_writable, the write twin of probe_readable (same bounds, same guard re-arm). An OR of zero faults where a write would and changes no byte, also against a concurrent writer.
  • xcrdapi.cpp: destination_writable calls commit_lazy as before, then probe_writable.
  • docs/startup-runtime.md: the destination section describes the probe and its cost.
  • Precedent: probe_readable already avoids VirtualQuery for the same reason. Its comment says that on the title's multi-gigabyte regions one query takes about a millisecond because it walks the region. #1766 (slot 133 placements) and #2611 (unplaced memory SRVs) avoid the same scan in the D3D12 stand-in.
  • Not used: QueryWorkingSetEx reports protection only for resident pages, so it would still need the VirtualQuery fallback; region information has no current protection.
  • Overlap: draft #2603 adds a probe cache next to probe_readable in local_log.h (text conflict only, if it lands first). #2245 answers readability of unmapped memory from xmem; this probe faults only on a refusal.

Verification

Windows 10 22H2, i7-4790K, RTX 3080 Ti; dev and release; commit 975f4872 (on b2564017).

ctest --preset dev     --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save'
ctest --preset release --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save'

70/70 passed in each, with no capability skips.

  • startup_dstorage_destination: the existing read-only, guard, no-access and short-commit refusals and the writable and copy-on-write successes all pass unchanged. New: the guard page is still PAGE_GUARD after its refusal; a writable destination with a three-page capacity and one page of data succeeds with the two probed, unwritten pages unchanged.
  • Negative controls (local mutations, not in this PR): without the guard re-arm the test fails guard page lost its guard bit; with an OR of 1 instead of 0 it fails the new case (a byte past the data changed) and the four refusal cases (first page was written).
  • Startup A/B (release, warm cache, same switches; only XCRDAPI.dll differs; stand-in CPU time from !runaway every 20 s):
VirtualQuery check Probe
Requests 3,254 → ~9,900: stand-in kernel time ~73 s ~1.2 s
Kernel time per request 10–12 ms 0.1–0.2 ms
First load complete (16,618 blocks inflated) 110 s 70 s
25k / 50k blocks inflated 170 / 180 s 130 / 150 s
destination not writable refusals, device removal 0, none 0, none

The confirm presses landed at different times in the two runs, so the end-to-end times are indicative; the per-request kernel time does not depend on them.

Evidence and follow-up

  • Fixtures are source-built (VirtualAlloc and a pagefile-backed mapping in the test); no title data.
  • Cost: every page up to the declared capacity is touched, so a page past the written bytes would be faulted in, and a copy-on-write page gets its private copy just before the copy instead of during it. Commit does not change: commit_lazy already covered the whole capacity. A local counter (not in this PR) measured this in one startup run (220 s, first load and the streaming that follows): 81,873 destination checks, 1,595,238 probed pages, 0 pages past the last written page. Every declared capacity ended in the same page as the data.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed. (docs/startup-runtime.md)
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: the change, tests and this description were written with Claude; I ran the startup A/B.

🤖 Generated with Claude Code

**@⁠lukaslw-lab** on GitHub · 2026-11-26 02:00 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2685) `lukaslw-lab:runtime/dstorage-destination-probe` → `main` · +106 −5 · 3 files · 2 commits · merged 2026-11-26 03:19 UTC ## Change Fixes #2684 (DirectStorage destination check costs ~10 ms of kernel time per request). `destination_writable` (#1672) keeps its verdicts but no longer calls `VirtualQuery`. It probes one byte per 4 KiB page of the declared capacity with an atomic OR of zero under a structured exception handler. - `local_log.h`: `x2w_local::probe_writable`, the write twin of `probe_readable` (same bounds, same guard re-arm). An OR of zero faults where a write would and changes no byte, also against a concurrent writer. - `xcrdapi.cpp`: `destination_writable` calls `commit_lazy` as before, then `probe_writable`. - `docs/startup-runtime.md`: the destination section describes the probe and its cost. - Precedent: `probe_readable` already avoids `VirtualQuery` for the same reason. Its comment says that on the title's multi-gigabyte regions one query takes about a millisecond because it walks the region. #1766 (slot 133 placements) and #2611 (unplaced memory SRVs) avoid the same scan in the D3D12 stand-in. - Not used: `QueryWorkingSetEx` reports protection only for resident pages, so it would still need the `VirtualQuery` fallback; region information has no current protection. - Overlap: draft #2603 adds a probe cache next to `probe_readable` in `local_log.h` (text conflict only, if it lands first). #2245 answers readability of unmapped memory from xmem; this probe faults only on a refusal. ## Verification Windows 10 22H2, i7-4790K, RTX 3080 Ti; dev and release; commit `975f4872` (on `b2564017`). ``` ctest --preset dev --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save' ctest --preset release --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save' ``` 70/70 passed in each, with no capability skips. - `startup_dstorage_destination`: the existing read-only, guard, no-access and short-commit refusals and the writable and copy-on-write successes all pass unchanged. New: the guard page is still `PAGE_GUARD` after its refusal; a writable destination with a three-page capacity and one page of data succeeds with the two probed, unwritten pages unchanged. - Negative controls (local mutations, not in this PR): without the guard re-arm the test fails `guard page lost its guard bit`; with an OR of 1 instead of 0 it fails the new case (`a byte past the data changed`) and the four refusal cases (`first page was written`). - Startup A/B (release, warm cache, same switches; only `XCRDAPI.dll` differs; stand-in CPU time from `!runaway` every 20 s): | | `VirtualQuery` check | Probe | |---|---|---| | Requests 3,254 → ~9,900: stand-in kernel time | ~73 s | ~1.2 s | | Kernel time per request | 10–12 ms | 0.1–0.2 ms | | First load complete (16,618 blocks inflated) | 110 s | 70 s | | 25k / 50k blocks inflated | 170 / 180 s | 130 / 150 s | | `destination not writable` refusals, device removal | 0, none | 0, none | The confirm presses landed at different times in the two runs, so the end-to-end times are indicative; the per-request kernel time does not depend on them. ## Evidence and follow-up - Fixtures are source-built (`VirtualAlloc` and a pagefile-backed mapping in the test); no title data. - Cost: every page up to the declared capacity is touched, so a page past the written bytes would be faulted in, and a copy-on-write page gets its private copy just before the copy instead of during it. Commit does not change: `commit_lazy` already covered the whole capacity. A local counter (not in this PR) measured this in one startup run (220 s, first load and the streaming that follows): 81,873 destination checks, 1,595,238 probed pages, **0** pages past the last written page. Every declared capacity ended in the same page as the data. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. (`docs/startup-runtime.md`) - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: the change, tests and this description were written with Claude; I ran the startup A/B. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
destination_writable walked the destination with VirtualQuery. On the title's
multi-gigabyte regions each query scans to the end of the region, which cost the
storage worker about 10 ms of kernel time per request during the first load.

Probe one byte per page with an atomic OR of zero under SEH instead
(x2w_local::probe_writable, the write twin of probe_readable). It faults where a
write would, changes no byte, and re-arms a guard page it trips. The test now
also checks the guard bit after the refusal and that probed pages past the data
keep their bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review of #2685: the write probe raised a first-chance guard-page exception
that a vectored handler sees, and it gave a copy-on-write page its private
copy even when a later page refused the request.

Remove probe_writable. destination_writable now reads page attributes only:
one QueryWorkingSetEx per 512 pages accepts a destination whose pages are
all valid, PAGE_READWRITE or PAGE_EXECUTE_READWRITE and not guarded; any
other page goes to the VirtualQuery walk from main, unchanged.

The test adds a first vectored handler that must see no guard-page
exception on any refusal, a copy-on-write page followed by a no-access page
whose first page must stay shared, and a committed but untouched
destination that takes the fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 01:14:42 +00:00
github manually merged commit fe6d764dd7 into gh-pr/2685/base 2026-11-27 01:14:44 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-26 02:38 UTC · original · requested changes

The measured VirtualQuery cost is worth fixing. I ran a source-built MSVC /O2 witness against this head. With a vectored observer, VirtualQuery produced 0 guard notifications. probe_writable refused the same protected page and restored PAGE_GUARD, but the observer saw 1 first-chance guard exception before the local handler. A second case used a FILE_MAP_COPY page followed by PAGE_NOACCESS. The probe stopped after 4096 of 8192 bytes yet QueryWorkingSetEx showed the first page changed from shared to private, with the byte unchanged. Please keep non-touch validation for unproven page ranges. Any fast path needs a stable classification that establishes ordinary writable backing across the full range. Preserve the queue failure and no-touch controls, including zero guard notifications and an earlier copy-on-write page remaining shared when a later page refuses the request. See the Microsoft guard-page and vectored-exception-handler documentation.

**@⁠R0Y00** on GitHub · 2026-11-26 02:38 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2685#pullrequestreview-6067824197) · **requested changes** The measured VirtualQuery cost is worth fixing. I ran a source-built MSVC /O2 witness against this head. With a vectored observer, VirtualQuery produced 0 guard notifications. probe_writable refused the same protected page and restored PAGE_GUARD, but the observer saw 1 first-chance guard exception before the local handler. A second case used a FILE_MAP_COPY page followed by PAGE_NOACCESS. The probe stopped after 4096 of 8192 bytes yet QueryWorkingSetEx showed the first page changed from shared to private, with the byte unchanged. Please keep non-touch validation for unproven page ranges. Any fast path needs a stable classification that establishes ordinary writable backing across the full range. Preserve the queue failure and no-touch controls, including zero guard notifications and an earlier copy-on-write page remaining shared when a later page refuses the request. See the Microsoft guard-page and vectored-exception-handler documentation.
Author
Owner

@⁠lukaslw-lab on GitHub · 2026-11-26 03:08 UTC · original

Thanks for the witness. Both findings reproduce here: the new test below fails on the previous head's DLL with the check raised a guard-page exception and first page became private. fe6d764d removes the probe. Nothing reads or writes destination bytes any more.

  • Fast path: one QueryWorkingSetEx per 512 pages of the declared capacity. It accepts the destination only when every page is valid in the working set, not Bad, PAGE_READWRITE or PAGE_EXECUTE_READWRITE, and has no PAGE_GUARD bit. Win32Protection is read only when Valid is set (PSAPI_WORKING_SET_EX_BLOCK).
  • Anything else (not resident, copy-on-write, guard, no-access, uncommitted) goes to the unchanged VirtualQuery walk from main, so no destination is accepted that main would refuse.
Case (startup_dstorage_destination) Previous head This head
Guard middle page: guard notifications seen by a first vectored handler 1 0
Copy-on-write page, then PAGE_NOACCESS: first page Shared after the refusal no (private) yes, bytes unchanged
Read-only / no-access / uncommitted middle or last page refused, queue keeps working pass pass
Committed, never-touched destination (fallback path) accepted — pass

Load cost of the main executable's first load: one release build of main de75613f plus this branch, only XCRDAPI.dll swapped between the two runs (Windows 10 22H2, i7-4790K, RTX 3080 Ti, warm shader cache, same switches). Per-thread CPU time from !runaway every 20 s, paired with the storage heartbeat:

First load (requests 0 → ~9,900) main check this head
Stand-in kernel time, 0–100 s 70.1 s 1.6 s
Kernel time per request, per 20 s window 8.3–16.3 ms 0.1–0.3 ms
  • A local counter (not in this PR) counted 36,130 destinations on the fast path and 32,649 on the VirtualQuery walk over 170 s. The walks that remained cost about 9 µs each, the fast path about 23 µs.
  • After the first load both arms spend 4–8 ms of kernel time per request; that part is decompression page faults, not this check.

Limits:

  • The classification is a snapshot, as the VirtualQuery walk on main is: a protection change by another thread between the check and the copy is not covered by either.
  • The explicit PAGE_GUARD test in the fast path is not pinned by the guard case: on Windows 10 22H2 the guarded page already reports Valid = 0 and falls back. I kept it because the documentation does not say a guarded page cannot be valid.

ctest --preset dev|release --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save': 70/70 each.

This reply was prepared with AI assistance (Claude); the runs are mine.

**@⁠lukaslw-lab** on GitHub · 2026-11-26 03:08 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2685#issuecomment-7181426664) Thanks for the witness. Both findings reproduce here: the new test below fails on the previous head's DLL with `the check raised a guard-page exception` and `first page became private`. `fe6d764d` removes the probe. Nothing reads or writes destination bytes any more. - **Fast path:** one `QueryWorkingSetEx` per 512 pages of the declared capacity. It accepts the destination only when every page is valid in the working set, not `Bad`, `PAGE_READWRITE` or `PAGE_EXECUTE_READWRITE`, and has no `PAGE_GUARD` bit. `Win32Protection` is read only when `Valid` is set ([PSAPI_WORKING_SET_EX_BLOCK](https://learn.microsoft.com/en-us/windows/win32/api/psapi/ns-psapi-psapi_working_set_ex_block)). - **Anything else** (not resident, copy-on-write, guard, no-access, uncommitted) goes to the unchanged `VirtualQuery` walk from `main`, so no destination is accepted that `main` would refuse. | Case (`startup_dstorage_destination`) | Previous head | This head | |---|---|---| | Guard middle page: guard notifications seen by a first vectored handler | 1 | 0 | | Copy-on-write page, then `PAGE_NOACCESS`: first page `Shared` after the refusal | no (private) | yes, bytes unchanged | | Read-only / no-access / uncommitted middle or last page refused, queue keeps working | pass | pass | | Committed, never-touched destination (fallback path) accepted | — | pass | Load cost of the main executable's first load: one release build of `main` `de75613f` plus this branch, only XCRDAPI.dll swapped between the two runs (Windows 10 22H2, i7-4790K, RTX 3080 Ti, warm shader cache, same switches). Per-thread CPU time from `!runaway` every 20 s, paired with the storage heartbeat: | First load (requests 0 → ~9,900) | `main` check | this head | |---|---|---| | Stand-in kernel time, 0–100 s | 70.1 s | 1.6 s | | Kernel time per request, per 20 s window | 8.3–16.3 ms | 0.1–0.3 ms | - A local counter (not in this PR) counted 36,130 destinations on the fast path and 32,649 on the `VirtualQuery` walk over 170 s. The walks that remained cost about 9 µs each, the fast path about 23 µs. - After the first load both arms spend 4–8 ms of kernel time per request; that part is decompression page faults, not this check. Limits: - The classification is a snapshot, as the `VirtualQuery` walk on `main` is: a protection change by another thread between the check and the copy is not covered by either. - The explicit `PAGE_GUARD` test in the fast path is not pinned by the guard case: on Windows 10 22H2 the guarded page already reports `Valid = 0` and falls back. I kept it because the documentation does not say a guarded page cannot be valid. `ctest --preset dev|release --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save'`: 70/70 each. This reply was prepared with AI assistance (Claude); the runs are mine.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-26 03:19 UTC · original · approved these changes

This head addresses both previous refusals. The Windows Release queue test passed all 15 cases, including zero first-chance guard notifications, the refused copy-on-write request retaining its shared page, and a cold writable destination completing through the fallback. A separate MSVC /O2 witness ran the exact working-set helper across 513 resident pages. It accepted the interval, accepted the first 512 pages, then refused the interval after PAGE_GUARD was set on page 513. The guard remained armed and the observer saw zero exceptions.

**@⁠R0Y00** on GitHub · 2026-11-26 03:19 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2685#pullrequestreview-6068106386) · **approved these changes** This head addresses both previous refusals. The Windows Release queue test passed all 15 cases, including zero first-chance guard notifications, the refused copy-on-write request retaining its shared page, and a cold writable destination completing through the fallback. A separate MSVC /O2 witness ran the exact working-set helper across 513 resident pages. It accepted the interval, accepted the first 512 pages, then refused the interval after PAGE_GUARD was set on page 513. The guard remained armed and the observer saw zero exceptions.
This discussion has been locked. Commenting is limited to contributors.
No description provided.