Skip to content

runtime: commit xmem pages and startup blocks on first touch (opt-in) - #2209

Merged
R0Y00 merged 7 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/lazy-commit
Nov 25, 2026
Merged

R0Y00 merged 7 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/lazy-commit

Conversation

@lukaslw-lab

@lukaslw-lab lukaslw-lab commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Change

Two experimental opt-in switches for #1199, one commit each: memory is committed on first touch instead of up front. Refs #1199.

1. X2W_XMEM_LAZY_COMMIT=1: mapped physical pages (#1199 item 2)

A fresh physical-page slot is committed on its first CPU access instead of when its page is mapped. Slab commit counts against the system commit limit, not the process.

  • Fault path. A vectored exception handler, installed when xmem.dll loads with the switch on, commits the slot behind a faulting address and resumes the thread. A thread that loses the race to the same slot retries once. GPU-view addresses resolve to the CPU-view slot.
  • Writers that cannot fault call the new experimental export x2w_xmem_commit_range(address, bytes) first:
    • the main module's ReadFile import is wrapped, because a kernel-mode write into an uncommitted mapped page fails with ERROR_NOACCESS instead of faulting;
    • device slot 133 commits a placed buffer's range before measuring it;
    • the stand-in writes behind a commit check (accessible(write), the DirectStorage destination check) commit first.
  • Reused slots and x2w_xmem_write_physical still commit before use.

2. X2W_XMEM_LAZY_STARTUP=1 (with the first): large startup blocks

The startup commit profile (#1098) only reserves blocks of 32 MiB and up. The same handler commits them 256 KiB at a time on first touch. Their commit counts against the process, so this is what the -MemoryGiB job cap sees.

  • x2w_xmem_commit_range also commits the chunks behind a range in such a block.
  • GPU placement. A GPU heap opened on a block covers the committed run from its base and is reused for later resources in it. So device slot 133 calls a new experimental export, x2w_xmem_commit_block(address), which commits the whole block once and removes it from the list.
  • Release. The main module's VirtualFree import is wrapped to remove a released block from the list first.
  • Blocks placed by X2W_XMEM_STARTUP_BELOW_40 stay committed up front; blocks below 32 MiB are committed as before; if the list cannot record a block, it is committed in full.

Without the switches nothing changes: no handler, no wrapped import, and both exports return TRUE. The second commit can be dropped on its own if the startup-block policy should stay as it is.

Docs: two rows in docs/startup-runtime.md Switches, a sentence in Startup memory accounting, and two sections in docs/experiments.md: "Mapped pages committed on first touch" and "Startup blocks committed on first touch".

Verification

Commit ea454aff (review fixes on b4c66763; the first part alone: eb587ad6) on ab9a020c, Windows 10 22H2 (build 19045), Visual Studio 2026 18.12 (MSVC 19.52), Windows SDK 10.0.26100, CMake 4.3.1, Python 3.13.7.

ctest --preset release --no-tests=error -R '^xmem_'
100% tests passed, 0 tests failed out of 39
ctest --preset release --no-tests=error -R '^(xmem_|startup_(heartbeat|local_log|placed|storage|dstorage|buffer_address|stub))'
100% tests passed, 0 tests failed out of 72

The first commit alone passes xmem_ 36/36 and the second set 69/69.

xmem_lazy_commit:      mapping 1024 pages (64 MiB) grew commit by 1 MiB ... ok
xmem_lazy_commit_off:  mapping 1024 pages (64 MiB) grew commit by 65 MiB ... ok (off)

Startup runs on Windows 10 22H2, 16 GB RAM, RTX 3080 Ti, -MemoryGiB 16, same input.

System commit (GiB, two runs each):

20 s 60–110 s 180 s 190 s
no switch 29.0 / 29.0 29.1–29.5 29.9 32.2
X2W_XMEM_LAZY_COMMIT 23.3 / 23.4 23.6–24.6 26.8 30.6

Process commit and end of run:

Switches Runs Process commit through startup At 180 s End
none 2 11.8 GiB 12.6 GiB job cap at 166.6 s; the #1758 builder throw at 196.1 s
X2W_XMEM_LAZY_COMMIT 2 11.8 GiB 15.6 GiB job cap at 130.7 s; a debugger race since fixed in #2197
both 3 10.8 GiB 14.7–15.1 GiB the #1758 builder throw at 181.8, 187.4 and 196.5 s, no commit failure

The job-cap stops are XMemMapPhysicalPagesScatter failing with error 1455 while the process commit heartbeat reads 16,389 MiB.

With -StartupBelow40 as well, so the run gets past the builder's address check (#1758):

Switches Runs System commit free at the peak Process commit peak End
both 3 3.75, 3.85, 3.31 GiB 13.9, 15.9, 16.2 GiB the builder's upload allocation (command-list slot 148, #1758), all three
none 2 0.74, 1.03 GiB 14.5, 16.1 GiB stopped by a host-side commit watchdog before that point

Two of the three runs with both switches are on this PR's final commit (b4c66763 with #1277 merged for Windows 10).

Evidence and follow-up

  • Tests and controls. Python checks that load the built xmem.dll (MIT, as the repository); no game data is used. xmem_lazy_commit_off and xmem_lazy_startup_off are the controls: with a switch at 0 the same mapping or blocks are committed up front, and the lazy assertions would fail on that path.
  • Import wrappers. xmem_lazy_imports is a source-built check whose own ReadFile and VirtualFree imports are wrapped, since the Python checks run in python.exe. Removing the commit in the ReadFile wrapper or the list removal in the VirtualFree wrapper makes it fail.
  • Other writers. Any other kernel-mode or GPU writer into lazy memory would need x2w_xmem_commit_range too. The ones listed are those the startup runs reached.
  • Measurements come from one host, with two or three runs per setup.
  • Possible follow-up: making the blocks placed below 2^40 lazy too. They are left out here, to keep that placement path unchanged. With -StartupBelow40, the 2.2 GiB block below 2^40 is committed up front, and in one run the process commit peaked 0.15 GiB under the 16 GiB cap. So this is the next limit on a 16 GB host.

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.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: prepared with Claude Code; the commands and results above were run on the author's PC.

🤖 Generated with Claude Code

lukaslw-lab and others added 2 commits November 25, 2026 05:55
…IT, R0Y00#1199)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…UP, R0Y00#1199)

With X2W_XMEM_LAZY_STARTUP=1 and X2W_XMEM_LAZY_COMMIT=1, commit_startup_block reserves blocks of 32 MiB
and up; the fault handler commits them 256 KiB at a time on first touch, x2w_xmem_commit_range commits the
chunks behind a range for writers that cannot fault, and x2w_xmem_commit_block commits a whole block before
slot 133 places a GPU resource in it. The title's VirtualFree import drops released blocks from the list.
Blocks placed below 2^40 stay committed up front.

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

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head b4c6676319da17de47d1782d4f7491731f052a7d (Linux-side review; no Windows run by me).

Checked

  • git diff --stat origin/main...HEAD: 10 files, +674/-21. Title is neutral, PR is MERGEABLE, CI is green (Windows build+test, Linux Python checks, gfx1013 fixtures all pass).
  • python3 tests/docs_links_check.py -> Checked 18 Markdown files and 415 links.
  • python3 tests/bidi_controls_check.py -> no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean.
  • ast.parse of both new tests: OK. They load xmem_shim.dll, so they only run on Windows; the CI Windows job covers them. I did not queue a Win2025 run.
  • Read src/xmem_mapped_pages.cpp end to end for the new paths. The handler takes memory.mutex like the existing remap handler. I found no site that touches a lazily mapped page while holding that mutex (commit_slot's memset hits a freshly committed view; x2w_xmem_write_physical uses section_for(..) with commit).
  • Mutation: not run. The tests need the Windows DLL, so a Linux mutation would prove nothing. Suggested mutants for the author: drop the x2w_xmem_commit_range call in read_file_committed, and drop the lazy_blocks.blocks.erase in virtual_free_lazy. Each should fail one of the new tests.

Findings

  • P1: none.
  • P2 src/xmem_mapped_pages.cpp (x2w_xmem_commit_range, the loop after the lazy_startup() block): when the range starts in a lazy block and runs past it, the physical-page loop restarts from start, not from from. It is harmless (backing_at returns null for block addresses), but a multi-GiB range costs a 64 KiB-step map lookup per page under memory.mutex. Start the loop at from & ~(page_bytes-1).
  • P2 read_file_committed: only the ReadFile import of the main module is wrapped. Other kernel-mode accessors of an uncommitted slot (ReadFileEx, WriteFile from a lazy page, imports in other modules) would fail with ERROR_NOACCESS the same way. Fine for an opt-in experiment, but please say so in docs/experiments.md.
  • nit: the thread-local retried is shared by the startup and slot paths. After a startup-path retry it is not cleared, so a later fault on the same chunk address range is passed on. A comment would help.

AI-assisted; commands listed were executed locally.

- x2w_xmem_commit_range starts the slot loop where the lazy startup blocks end, not at the range start.
- The startup-block fault path has its own retry, keyed on the chunk and on the number of released lazy blocks,
  so a new block at the addresses of a released one can still be raced once.
- docs: only the main module's ReadFile import is wrapped; name the kernel-mode accessors that are not.
- xmem_lazy_imports: a source-built check whose own ReadFile and VirtualFree imports are wrapped. Without the
  commit in the ReadFile wrapper the read fails with ERROR_NOACCESS; without the list removal in the
  VirtualFree wrapper the handler commits a new reservation at the released addresses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All three findings are addressed in ea454aff:

  • P2 x2w_xmem_commit_range: the slot loop now starts where the lazy blocks end (from), not at start.
  • P2 other kernel-mode accessors: docs/experiments.md now says only the main module's ReadFile import is wrapped, and that ReadFileEx, overlapped reads issued elsewhere, WriteFile from a lazy page and other modules' imports fail with ERROR_NOACCESS the same way.
  • Nit, shared retried: the block path now has its own retry, keyed on the chunk and on a count of released lazy blocks, as the remap handler keys on (page, break_id). A fault that stays is still passed on, and a new block later placed at the same addresses can still be raced once. Before, a stale retried could pass that one on.

About the suggested mutants: neither would have failed the Python tests. Both wrappers patch the main module's imports, and there the main module is python.exe. I added a source-built check, xmem_lazy_imports (tests/xmem_lazy_imports_check.cpp), whose own ReadFile and VirtualFree imports are wrapped:

Mutant Result
none PASS (8 checks)
no x2w_xmem_commit_range in read_file_committed ReadFile failed with error 998, check_failed: readfile_into_lazy_page
no erase in virtual_free_lazy check_failed: released_block_not_committed, check_failed: released_block_still_reserved

ctest --preset release --no-tests=error -R '^xmem_': 39/39 passed. The affected startup set: 72/72 passed (Windows 10 22H2, release). The branch still merges cleanly with current main (8b5a0f7c). A startup run on this commit (with #1277 for Windows 10, both switches and -StartupBelow40) reached the same command-list slot 148 stop as before, with no commit failure.

AI-assisted (Claude Code).

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of head ea454aff6382601d690f3888d4eee1fe886bd957 (Linux-side; no Win2025 run by me). Previous review was on b4c66763.

Checked

  • git diff --stat b4c66763 ea454aff: 4 files (CMakeLists.txt, docs/experiments.md, src/xmem_mapped_pages.cpp, new tests/xmem_lazy_imports_check.cpp), +152/-13. PR is MERGEABLE, title neutral. gh pr checks: Windows build+test, Linux Python checks and gfx1013 fixtures pass.
  • python3 tests/docs_links_check.py -> Checked 18 Markdown files and 416 links.
  • python3 tests/bidi_controls_check.py -> no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean.
  • Read the three fixes in src/xmem_mapped_pages.cpp and the new test.
  • Syntax check with the MinGW cross compiler fails on __try/__except (MSVC-only SEH), also in untouched local_log.h, so I could not build or run the new test on Linux. The CI log for the Windows job was not retrievable, so I cannot confirm from the log that xmem_lazy_imports ran. The job is green and the test is registered in CMakeLists.txt.
  • Mutation: not run on Linux for the same reason. The new test's own description gives the two mutants (drop the commit in the ReadFile wrapper, drop the list removal in the VirtualFree wrapper). Please confirm on Windows that each one turns xmem_lazy_imports red.

Previous findings

  • P2 commit-range loop restarting from start: fixed. The loop now starts at from & ~(page_bytes-1) (line 932), after the lazy blocks.
  • P2 other kernel-mode accessors: fixed in docs/experiments.md (lists ReadFileEx, overlapped reads, WriteFile from a lazy page, other modules' imports).
  • nit shared retried: fixed. The startup path now has its own retried_chunk + retried_releases; the slot path keeps its own retried. The release counter is bumped only when a block really left the list.

Findings on the new head

  • P1: none.
  • P2: none.
  • nit virtual_free_lazy: the counter is bumped before real_virtual_free. A racing thread can see the new count while the old block is still reserved; the only effect is one extra retry, so it is harmless. A one-line comment would help.
  • nit tests/xmem_lazy_imports_check.cpp: the temp file from GetTempFileNameA is not deleted if CreateFileA for reading fails. Cosmetic.

AI-assisted; commands listed were executed locally.

…mp file

- virtual_free_lazy: comment that the count is bumped before the release; a racing thread retries at most once more.
- xmem_lazy_imports_check: delete the temp file when it cannot be reopened for reading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Thanks for the re-review. Both nits are done in 76b995fd:

  • virtual_free_lazy: a comment says the count is bumped before the release, so a racing thread retries at most once more.
  • xmem_lazy_imports_check: the temp file is deleted when it cannot be reopened for reading.

On the mutants: confirmed on Windows, see the table in my previous comment. Each one turns xmem_lazy_imports red: check_failed: readfile_into_lazy_page (error 998) for the first, and released_block_not_committed with released_block_still_reserved for the second. On 76b995fd, ctest --preset release --no-tests=error -R '^xmem_' passes 39/39.

AI-assisted (Claude Code).

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of head 76b995fdadb445042399d7a9339418b6ff8301c8 (previous reviews covered b4c6676 and ea454af).

Delta since ea454af (git diff ea454af..HEAD --stat: 2 files, 7 insertions, 2 deletions)

  • src/xmem_mapped_pages.cpp:980: comment only. It documents that the release count is bumped after the block leaves the list and before VirtualFree. I traced lazy_commit_fault (lines 178-212): a thread that faults in that window sees the new count with the old block still reserved. The keyed retry (retried_chunk/retried_releases) lets it retry once and then pass the fault on, so the comment is accurate and no behavior changes.
  • tests/xmem_lazy_imports_check.cpp:99-104: the ReadFile call is now guarded against INVALID_HANDLE_VALUE, and the temp file is deleted with DeleteFileA if the reopen fails. This fixes the earlier temp-file leak and the call on an invalid handle. Correct.

Findings

  • P1: none.
  • P2: none.
  • Nit: none new.

Checks run locally (Linux)

  • git diff --check origin/main...HEAD: clean.
  • python3 tests/docs_links_check.py: self-test passed, 18 Markdown files and 416 links checked.
  • python3 tests/bidi_controls_check.py: self-test passed, 1490 files, no bidirectional controls.
  • gh pr checks 2209: Linux Python checks, Windows build and test, and gfx1013 fixtures all pass. The coverage chart and /O2 jobs are skipped. The PR is mergeable and the title is neutral.

Not run

  • The changed code is Windows-only (Win32 fault handler and CTest binaries), so I could not do a mutation run on Linux. I did not queue a Win2025 run because the delta is a comment plus test hardening. The Windows CI job passing on this head covers xmem_lazy_imports, xmem_lazy_commit and xmem_lazy_startup.

No blocking issues in this delta.

AI-assisted; commands listed were executed locally.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One release-path blocker on 76b995fd: virtual_free_lazy erases the lazy-block record before it knows whether VirtualFree succeeded. A failed MEM_RELEASE leaves the range reserved. A later first touch then has no record and cannot commit the page. Please keep the reservation and its registration consistent across failure and synchronize successful release with in-flight commit. A source-built check can try MEM_RELEASE with a nonzero size, verify failure, then write to an untouched page and finally release with size zero. Please also check that a failed handler installation leaves lazy allocation disabled rather than returning inaccessible pages.

@stan495 stan495 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified head 76b995fd on Windows x64 (dev preset, MSVC 19.44.35228.0, Python 3.12):

  • cmake --build --preset dev --target xmem_shim xmem_lazy_imports_check -> succeeded
  • ctest --preset dev --no-tests=error -R '^xmem_lazy_' -V -> 5/5 passed (xmem_lazy_commit, xmem_lazy_commit_off, xmem_lazy_startup, xmem_lazy_startup_off, xmem_lazy_imports)

Verdict: Approved at 76b995fd.

lukaslw-lab and others added 3 commits November 25, 2026 07:52
Resolve the map_pages conflict with R0Y00#2225: keep the deferred commit for fresh slots
(section_for(page, false)) and record R0Y00#2225's section-backing failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of R0Y00#2209: virtual_free_lazy removed a block from the lazy list before
VirtualFree ran, so a refused MEM_RELEASE left a reserved block that no first
touch could commit. The release now runs under the list lock and the block
leaves the list only when it succeeds. The fault handler, x2w_xmem_commit_range
and x2w_xmem_commit_block hold the same lock from the lookup to the commit, so
a release cannot run between them.

If the fault handler cannot be installed, the switches now act as off: pages
and blocks are committed up front and no import is wrapped.

xmem_lazy_imports adds a refused release (nonzero size) followed by a write to
an untouched page; xmem_lazy_handler_failure runs the test-controls build with
the handler refused (X2W_XMEM_TEST_LAZY_HANDLER_FAILURE=1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Thanks. Fixed in c40e3dac, and the branch is merged with current main (ac423c5f; one conflict with #2225 in map_pages, which keeps both the deferred commit and #2225's section-backing record).

Release path (virtual_free_lazy)

  • The real VirtualFree now runs under the lazy-list lock. The block leaves the list only when the release succeeds, so a refused MEM_RELEASE leaves it reserved and still committed on first touch. The caller's error is kept.
  • The fault handler, x2w_xmem_commit_range and x2w_xmem_commit_block hold the same lock from the lookup to the commit, so a successful release cannot run between them.

Handler installation

  • Lazy commit turns on only after AddVectoredExceptionHandler succeeds. Otherwise both switches act as off: pages and blocks are committed up front, no import is wrapped, and the log says so.

Checks (tests/xmem_lazy_imports_check.cpp)

Check What it does Mutant that turns it red
xmem_lazy_imports (new case) MEM_RELEASE with a nonzero size fails with ERROR_INVALID_PARAMETER; a write to an untouched page is then committed; release with size 0 succeeds erase before the release: refused_release_block_still_lazy, refused_release_write_committed
xmem_lazy_handler_failure (new) test-controls build with X2W_XMEM_TEST_LAZY_HANDLER_FAILURE=1: a mapped page and a 64 MiB startup block are committed and writable lazy kept on without the handler: all four checks fail

ctest --preset release --no-tests=error -R '^(xmem_|startup_slot148|startup_upload)' on ac423c5f: 47/48. The one failure is startup_slot148_acceptance_nodebug_warp (10 s timeout), the Windows 10 WARP no-debug case Jess-Doit reported on main in #870. dev: 47/47. Windows 10 19045, MSVC 19.52.

AI-assisted (Claude Code).

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review at head ac423c5fdf816992f59575de1454c9f3f0ee01d5 (delta since 76b995fd: c40e3dac plus two upstream merges).

Read of c40e3dac (src/xmem_mapped_pages.cpp, tests/xmem_lazy_imports_check.cpp, CMakeLists.txt, docs):

  • virtual_free_lazy now runs VirtualFree under the exclusive list lock, erases the block only if the release succeeded, and preserves GetLastError. A refused release (nonzero size) keeps the block reserved and lazy. This fixes the earlier gap.
  • The fault handler and x2w_xmem_commit_range hold the shared lock from lookup to commit, and x2w_xmem_commit_block holds the exclusive lock. A release cannot run in between. No lock-order issue: the list lock is dropped before memory.mutex is taken in every path I read.
  • lazy_handler_installed is constant-initialized and set before the wrappers are installed, so lazy_commit() and lazy_startup() act as off when the handler is refused. lazy_startup() no longer caches a handler-dependent value.
  • New tests cover the refused release (release_with_size_refused, refused_release_block_still_lazy) and the handler-failure path (xmem_lazy_handler_failure, test-controls build only).

Commands run locally (Linux, worktree at this head):

  • gh pr checks 2209: Linux Python checks, Windows build+test, gfx1013 fixtures all pass. /O2 job skipping.
  • python3 tests/docs_links_check.py -> pass (18 files, 427 links)
  • python3 tests/bidi_controls_check.py -> pass (1524 files)
  • git diff --check origin/main...HEAD -> clean
  • tests/xmem_lazy_*_check.py need the Windows binaries as arguments, so they were not run here. The new C++ tests are covered by the green Windows CI job only. I did not queue a separate Windows VM run, and I did not run a mutation of the new logic because the tests cannot execute on Linux.

Findings:

  • P1: none.
  • P2: none.
  • Nit: virtual_free_lazy holds the exclusive lock across the real VirtualFree. Fine for these block sizes, but a slow release will stall any first-touch fault on another thread. Worth a comment, not a blocker.
  • Nit: a MEM_DECOMMIT of part of a lazy block is not handled by the wrapper (unchanged from before this delta).

Scope: MIT, no third-party code, neutral title, mergeable. Opt-in switches, so default behaviour is unchanged.

AI-assisted; commands listed were executed locally.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failed-release and handler-installation findings are resolved. On the exact SHA, Windows Release passed all 24 selected lazy-commit, mapped-page, slot 148 and upload checks. The failed release retained the reservation and a subsequent write committed its page. The forced handler failure committed both memory paths up front. The earlier Windows 10 WARP timeout is tracked separately on the same upstream slot 148 fixture.

@R0Y00
R0Y00 merged commit 45fa67f into R0Y00:main Nov 25, 2026
5 checks passed
@lukaslw-lab
lukaslw-lab deleted the runtime/lazy-commit branch November 25, 2026 07:49
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