Skip to content

xmem: make page runs cheaper under Wine - #2911

Open
JohnConnorNPC wants to merge 35 commits into
R0Y00:mainfrom
JohnConnorNPC:memory/merged-page-runs
Open

JohnConnorNPC wants to merge 35 commits into
R0Y00:mainfrom
JohnConnorNPC:memory/merged-page-runs

Conversation

@JohnConnorNPC

@JohnConnorNPC JohnConnorNPC commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

How to test

Fixtures, on Windows with the dev preset:

cmake --preset dev
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^(xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first|page_runs_device|shim_page_runs)|startup_dstorage_break_window|linear_buffer_backing_share)' -V
  • This selects 31 tests in our configure. xmem_page_runs_device_hardware and linear_buffer_backing_share_hardware
    need a physical GPU. The three _warp tests need WARP.
  • xmem_placeholder_split_control, _deployed and _partial skip (77) where the OS reports the placeholder bit.
  • The pr preset now builds these programs and runs 28 of these tests that it did not run before: the five fixture
    families and the three _warp tests. The _hardware tests stay out of it.

Start-up run:

  1. Build xmem.dll and the stand-in DLLs as for any start-up run: cmake --preset dev and
    cmake --build --preset dev. On Linux, use the clang-cl cross build in docs/non-windows-hosts.md.
  2. Run the start-up launcher with lazy memory and shared views on, and no timeout.
    • Windows: tools/startup/run.ps1 <usual exe/data/log arguments> -Seconds 0 -MemoryGiB 40 -FakeUser -Render -Translate -GpuMirror -HostCpu -StartupBelow40 -LazyMemory.
    • -LazyMemory sets X2W_XMEM_LAZY_COMMIT, X2W_XMEM_LAZY_STARTUP and X2W_XMEM_SHARED_VIEWS. It does not set
      X2W_XMEM_JOIN_CALLS, so joins across mapping calls stay off unless you set it yourself.
    • Linux: tools/startup/run.sh ... --seconds 0 --memory-gib 40 --fake-user --render --translate --gpu-mirror --host-cpu --startup-below-40, with X2W_XMEM_LAZY_COMMIT=1 X2W_XMEM_LAZY_STARTUP=1 in the environment.
      run.sh sets X2W_XMEM_SHARED_VIEWS=1 and X2W_XMEM_JOIN_CALLS=1 unless they are already set.
    • Both platforms: also set X2W_DEVICE_TYPE=6 X2W_PRESENT_COMPOSITE=1 X2W_D3D12_RELEASE_RETIRED=1 X2W_D3D12_RELEASE_RETIRED_VIEWS=1 X2W_XAPU_TYPED_COMPLETIONS=1.
    • Our runs also set X2W_D3D12_BUDGET=1 and used an input script that answers the account prompt once. In the
      branch runs in Evidence, the menus also appear to have been driven by hand with a controller (see Evidence).
  3. What our Wine runs of an earlier head showed (see Evidence). No start-up run of the final head has been made.
    • The run reached the menus, and the x2w-d3d12 heartbeat: N frames presented count kept rising past about
      170 s. In our like-for-like runs, main stopped presenting at 15-16.5 s and hit the executable's start-up
      watchdog at 52-56 s.
    • Known open item: after the menu, the executable showed its own "data file missing or corrupt" dialog, of
      undetermined cause.
    • On Windows, @pigalot's runs of 4a43a173 with the lazy and shared-views switches on ended in a write access
      violation in the executable, with and without this PR (Evidence). With the switches off, the run with this PR
      reached the 900 s cap.

XMem pages are mapped from 64 KiB sections. With X2W_XMEM_SHARED_VIEWS=1, pages that are adjacent in the
address space and adjacent slots of one section share one view, called a run. Unmapping one page of a run takes
the run apart and maps its other pages again (a run break). A console executable under test maps nearly every
page with its own call: 45595 of 45602 recorded runs had one page. Under Wine each MapViewOfFile3 is a server
round trip. In one run its cost rose from 0.04 ms to 0.92 ms per call as the view count grew, so start-up spent
most of its time mapping.

This PR has one aim: XMem page runs under Wine are cheaper to build, and code that reads those pages sees them
committed while a run changes. Groups 1 to 3 are the first 17 commits, in commit order. The 17 review commits after
them are listed under "Review changes". Each group below describes the final head and names the review commit that
changed it.

Switches on the final head:

  • Shared views (X2W_XMEM_SHARED_VIEWS=1) gate group 3's reader waits and its import wrappers. run.ps1 -LazyMemory
    sets it.
  • Joins across mapping calls also need X2W_XMEM_JOIN_CALLS=1. run.sh sets it and run.ps1 does not.
  • Without shared views, readers are called once and pages are mapped as on main.
  • Groups 1 and 2, split-first and the fault retries need no switch. See Switches.

Group 1: slot 31 placeholder splits under Wine (3 commits)

  • xmem: split slot 31 placeholders when the OS hides the placeholder bit.
    • Wine reports placeholders without the 0x100 region flag. As a result, every queue slot 31 split or
      coalesce whose run was not exactly one placeholder failed with ERROR_INVALID_ADDRESS.
    • A one-time probe turns a fallback on only where the bit is missing. Where the OS reports it, nothing changes.
    • The fallback now checks every region a view covers (1615efc5), and its known limit is in Known limits
      (f9391cca).
  • Test xmem_placeholder_split.
    • Its control, xmem_placeholder_split_control, turns the fallback off in the test-controls build and passes
      only when the split step fails. _deployed sets the same switch for the deployed DLL, which ignores it
      (cb8cc808). _partial was added in 1615efc5. All three skip (77) where the OS reports the bit.
    • Docs: docs/experiments/xmem-page-runs.md and the README row (0cfdd607).

Group 2: DirectStorage copies through xmem.dll (3 commits)

  • xmem: copy DirectStorage destinations under the mapping mutex and startup: copy DirectStorage data through x2w_xmem_copy_to_destination.
    • XCRDAPI's VirtualQuery pre-check could run while a break had the destination page unmapped for a moment.
      It then refused a destination that was mapped both before and after.
    • The check and the copy now run in xmem.dll. Since f19890e4, they run under memory.mutex only when shared
      views are on and the destination or the source overlaps a recorded run. Otherwise, and always with shared views
      off, they run without the mutex.
    • Both paths check resident read-write pages first, as XCRDAPI does without xmem.dll (startup: probe DirectStorage destinations instead of walking them with VirtualQuery #2685), and then fall back
      to the region walk.
    • Without xmem.dll, XCRDAPI checks and copies as before.
    • A copy that fails now fails the request. Since 302618bd, XCRDAPI logs the outcome by error code.
  • Test startup_dstorage_break_window.
    • It holds a break open and submits a request into a page that reads MEM_RESERVE inside it. The request
      must wait for the hold, then complete with every byte in place.
    • Its control, startup_dstorage_break_window_control, builds XCRDAPI without the export lookup and must
      report "refused while the run was broken".
    • _plain, _views_off (f19890e4) and _outcomes (302618bd) are described under Review changes.

Group 3: page runs (11 commits)

Fewer map calls:

  • xmem: split an owned placeholder before mapping a page into it.
    • A map into the middle of a larger placeholder used to fail once, then split. Now the split comes first, so a
      single-page call makes one MapViewOfFile3 attempt instead of two. If the split or the map fails, the old
      order runs.
    • Test xmem_map_split_first: 8 maps for 8 pages. Its control, xmem_map_split_first_control
      (X2W_XMEM_TEST_MAP_FIRST=1, test-controls build since cb8cc808), measures 2 maps per page.

Readers outside run breaks. A touch of a page inside a break faults, and the fault handler retries it. A region
query or a kernel copy does not fault: it sees the page as reserved, or fails.

  • xmem: answer non-faulting readers outside run breaks.
    • New export x2w_xmem_stable_call(fn, context). It runs fn once when no break begins or ends around it,
      and otherwise runs it again under the mapping mutex.
    • These readers now answer through it: XMemVirtualQuery, and in the stand-ins x2w_local::writable,
      slot 133 measure_placed_buffer and snapshot_samplers.
  • xmem: answer the main module's VirtualQueryEx outside run breaks.
    • With shared views on, the main module's VirtualQueryEx import goes through a wrapper that uses
      stable_call.
    • The module is pinned only when an import was changed.
  • xmem: run readers once without shared views, and write their answers after a break.
    • Without the switch, stable_call calls fn directly.
    • Query answers go to a local copy first and are written to the caller's buffer after the call. That buffer
      may be in a lazily committed page.
  • xmem: write back only what XMemVirtualQuery's query writes, and pass lines at the end: the write-back covers
    the bytes the query writes and no more.
  • xmem: run title ReadFile/WriteFile again after a run break refused the buffer.
    • The main module's ReadFile and WriteFile imports are wrapped.
    • A synchronous call is run again, up to 4 attempts, only when it failed with ERROR_NOACCESS or
      ERROR_INVALID_USER_BUFFER, a break overlapped it, its buffer lies in a run and, since 4f7d47de, the
      handle is a disk file.
    • Under Wine before this change: ReadFile into the run failed with 998 and left the position as it was, and
      WriteFile from it failed with 1784. After it, the read gets all 4096 bytes.

Fault retries across breaks:

  • xmem: retry a fault in the break after the one the handler saw end fixed a pre-existing bug. A handler that
    read the break generation after the break ended named the next break instead. A fault on the same page in that
    next break was then not retried. The hold-ignored child of xmem_page_runs_check.py crashed in 6 of 6 runs
    before the fix and passed 6 of 6 after it.
  • xmem: retry a fault from any of the last 64 breaks, not only the last added a 64-break history.
    c8391709 replaces both mechanisms. Each run now keeps the id of the break inside which it was mapped, and
    the handler retries a fault on such a page once per break and thread, however many breaks follow on other runs.

Joins:

  • xmem: join a page mapped on its own call to the view before it. Since d4f1344e this needs
    X2W_XMEM_JOIN_CALLS=1 as well as shared views.
    • A call whose pages form one run joins the recorded run that ends at its address.
    • It joins only when its first page is the next slot of the same section and both lie in one shim
      reservation.
    • The run is unmapped inside a break, the placeholders are joined, and one view is mapped over both.
    • If a step before the unmap is refused, nothing changes. If one after it is refused, the old layout is
      restored.
    • Without the join switch, every call keeps its own view, as on main.
  • xmem: stop a joined run at 64 pages.
    • A join that would make the run longer than 64 pages maps the new pages separately.
    • Fixture timing under Wine (median of 3 to 5 runs), unmapping the middle page of an R-page run:
      • the break costs 0.105 ms at R = 16, 0.275 ms at 64, 2.112 ms at 512 and 13.176 ms at 4096;
      • at the cap, a join costs about as much as mapping its own view (1.515 ms against 1.392 ms).
  • xmem: say the run cap bounds joined runs only, and test the break ring's edge.
    • Only joined views are capped. A view mapped by one call, or the surviving pages that a break maps back
      together, can still be longer, as before this PR.
    • Its ring-edge test became xmem_run_break_readers_stamps in c8391709.

Tests and changed expectations:

  • xmem_run_break_readers, with _control, _next_break_control, _default, _lazy, _two_runs, _mirror
    and _stamps.
    • They hold a break open in the test-controls build and check that each reader waits for it and then sees the
      page committed. The readers cover ReadFile and WriteFile too. A pipe ReadFile must fail inside the
      window without being run again.
    • The control build calls the readers directly, and they answer inside the window.
  • xmem_cross_call_merge, with _control (one view per call under X2W_XMEM_TEST_NO_MERGE=1), _deployed
    (the deployed shim ignores that switch), _default (no shared views) and _no_join (shared views without the
    join switch: one view per call). Its capped case covers the 64-page cap.
  • Changed expectations (no check is weakened):
    • xmem_page_runs_check.py is main's again (three views), because joins are off by default.
    • xmem_page_runs_device_check. ad1980cb (runtime: bring XMem page-run fixes into cpp-migration #2918's f6020249, same patch-id) maps the single pages from high
      to low addresses. d4f1344e maps the upper 8-page call first. No call then has a view that ends at its
      address, so with the join switch every call still keeps its own view. New xmem_page_runs_device_join_warp
      runs it with X2W_XMEM_JOIN_CALLS=1. The earlier description said this test's view boundaries were the same
      as before. That was wrong at 4a43a173, as @Not-Saii's Windows run showed.
    • linear_buffer_backing_check. 238ed78a (runtime: bring XMem page-run fixes into cpp-migration #2918's 27778f4f, same patch-id) maps the upper page of the
      split buffer first. The fixture also clears the join switch.

Review changes (17 commits)

These answer @Not-Saii's review of 4a43a173 and our own audit, in commit order:

  • ad1980cb, 238ed78a: the two fixture fixes above.
  • 14a9589f: the page-run test registrations move verbatim to tests/cmake/xmem_page_runs.cmake.
    CMakeLists.txt is main's again.
  • e9e0b441: the slab accounting comment is back above x2w_xmem_slab_stats, and the fixture comments no longer
    point at notes outside the repository.
  • d4f1344e: joins across mapping calls need X2W_XMEM_JOIN_CALLS=1 (review point 3).
    • New xmem_cross_call_merge_no_join and xmem_page_runs_device_join_warp.
    • Every mode of the branch's fixtures sets or clears the switch itself.
  • cb8cc808: X2W_XMEM_TEST_MAP_FIRST and X2W_XMEM_TEST_NO_PLACEHOLDER_FALLBACK are read only in the
    test-controls build. The deployed xmem.dll contains neither name. New _deployed rows.
  • 1615efc5: the placeholder fallback checks every region a view covers, not only the first of each allocation
    (review point 4).
    • New xmem_placeholder_split_partial.
    • Under Wine with the old walk, the batch succeeded and the view replaced the committed page (byte 0x53
      instead of the 0x7a tag). With the new walk it is refused and the page keeps its tag.
  • c8391709: each run carries the break that mapped it, which replaces the 64-break history (review point 2).
    • xmem_run_break_readers_stamps: a fault held through 63, 64 and 200 later breaks is retried each time (under
      Wine the later breaks took 3, 4 and 10 ms).
    • A page made no-access faults once where no break mapped it, and twice after a break did.
    • A fault that cannot be the transient one is passed on without taking the mutex.
  • 4f7d47de: the ReadFile/WriteFile retry applies to disk files only.
    • New window case ReadFile_pipe. Under Wine the pipe read fails with ERROR_NOACCESS and both messages stay
      queued.
  • f19890e4: a DirectStorage copy takes the mapping mutex only when it touches a run, and checks resident pages
    first (review point 1).
    • New x2w_xmem_copy_counts export, test-controls build only.
    • New startup_dstorage_break_window_plain: a VirtualAlloc destination copies without the mutex, and a run
      destination copies with it.
    • New startup_dstorage_break_window_views_off: with shared views off, an XMem page destination copies without
      the mutex.
  • 302618bd: XCRDAPI logs each copy outcome by its error code: "destination not writable", "copy faulted (part of
    the destination may be written)", "invalid copy request", or "copy failed (error N)".
    • New startup_dstorage_break_window_outcomes, which uses the test-controls build's X2W_XMEM_TEST_COPY_ERROR.
  • 0cfdd607: the note moves to docs/experiments/xmem-page-runs.md, and docs/experiments.md is main's again.
    • The pr preset builds the fixture programs and runs 28 of these tests that it did not run before.
    • startup_run_sh stays out of it: it skips on Windows, and the Linux Python job runs its check.
  • Round 1 of our audit:
    • f9391cca: states the placeholder fallback's limit (Known limits).
    • f4cc2900: says which half of the pipe retry reason is measured.
    • 863c0459, bf9e448f: comments only.
    • 0c6be971: README link layout.

Switches

Change Active when
Slot 31 placeholder fallback the OS hides the placeholder bit (probed at first use)
Split-first map, fault retries always
DirectStorage check and copy in xmem.dll when XCRDAPI finds xmem.dll's export; under the mapping mutex only with shared views on and a range that overlaps a run
stable_call waits, the VirtualQueryEx/ReadFile/WriteFile wrappers X2W_XMEM_SHARED_VIEWS=1 (run.ps1 -LazyMemory and run.sh set it)
Joins across mapping calls and their 64-page cap X2W_XMEM_SHARED_VIEWS=1 and X2W_XMEM_JOIN_CALLS=1 (run.sh sets it, run.ps1 does not)
X2W_XMEM_TEST_* (MAP_FIRST, NO_MERGE, MERGE_CAP, HOLD_WINDOW, HOLD_RETRY, FAULT, NO_PLACEHOLDER_FALLBACK, COPY_ERROR) test-controls build only. The deployed xmem.dll has no X2W_XMEM_TEST_ string.

Test

Host: Linux, cross-built with clang-cl 19 and xwin. Tests ran under Proton Experimental (wine-11.0) with a runner
that applies each test's ctest properties.

Check Base 6ee878d Earlier head 4a43a17 Final head 0c6be97
Every test that runs on this host, first runner and prefix: passed / failed / skipped (77) 578 / 1729 / 58 of 2365 596 / 1729 / 58 of 2383 not run
The same, fixed runner and second prefix not run not run 614 / 1668 / 109 of 2391
Pure-Python checks (the CI python-linux command) all 210 pass all 210 pass all 210 pass
  • The final head was run with a fixed runner in a second prefix.
    • The runner counts a test that cannot start (exit 53 or 126) as not started, whatever WILL_FAIL says. There
      were 0 such tests.
    • In the second prefix, tests that import d3d12.dll start.
    • Base was not run that way, so the final head has no like-for-like base count.
  • By row against the 4a43a173 run, 79 rows changed:
    • 51 moved to a skip (77). These are D3D12 tests that now start and skip without WARP. The first runner counted
      3 of them (unified_buffer_paths_*_control) as passes.
    • 16 rows went from fail to pass.
      • 13 of them could not start or could not load a DLL in the first prefix (exit 53 or 126, or a missing DLL).
      • compute_heap_view_controls failed there with a Wine client error. It was the earlier description's flake.
      • startup_storage_directory_batch failed there with an AssertionError, which was not investigated.
      • The last one is startup_local_log_reopen_control (below).
    • 8 rows are new and pass. xmem_page_runs_device_join_warp is new and fails: there is no WARP here.
      _ring_edge became _stamps.
    • 2 rows went from pass to fail:
      • xmem_startup_below_40_mutant_no_limit timed out (60 s) in the full run and passes alone.
      • xmem_lazy_commit_off is unmeasured under Wine. Its commit-charge reading moves with host load, and it
        already failed at 238ed78a, before any review commit. Its earlier passes are not results.
    • startup_local_log_reopen_control passed once in the full run. It fails alone and in every other run (base,
      4a43a173, the review head), so the pass is not a change.
  • Build scope: ninja -k 0 ends with the same 419 failed targets on base and on this head: the MASM fixtures and
    the generator-driven targets, which the cross toolchain cannot build. Those tests did not run here.
  • A pass supports only the behaviour that test checks. It does not show that anything starts up.
  • Negative controls and mutations:
    • Every new check has a control that fails, or measures the old behaviour.
    • Each review commit's mutations were run once on the final tree under Wine. Each one fails its paired check:
      • join switch forced on (_no_join, xmem_shim_page_runs);
      • no join switch in the merge fixture (xmem_cross_call_merge), and the run.sh check's "no join calls" mutant;
      • either test switch read in the deployed build (_deployed);
      • placeholder walk of the first region only (_partial);
      • rollback unmap without MEM_PRESERVE_PLACEHOLDER (_partial);
      • stamp honoured only within 64 breaks (later_breaks_64/_200);
      • stamp ignored (persistent_fault);
      • pre-check without its mapped-view test (xmem_run_break_readers, _stamps);
      • retry on any handle type (ReadFile_pipe);
      • DirectStorage copy always under the mutex (_plain, _views_off), and run lookup without the shared-views
        gate (_views_off);
      • the old two-way outcome text (_outcomes).
    • The first 17 commits' mutations each failed their check at 4a43a173:
      • drop the slot-adjacency condition, the break around the join, the restore or the reservation bound;
      • join without shared views, or drop the 64-page cap test;
      • stable_call never retries;
      • the ReadFile/WriteFile wrapper returns 0, or retries with no bound.
        They were not run again on the final head. The ring mutations no longer apply, because the ring is gone.
  • Not pinned by any mutation here:
    • The new AllocationBase/RegionSize clauses of xmem_placeholder_split_partial. The unmap mutation fails
      that check, but it also changes the page state. No mutation was found that changes only the base or the size.
    • The lock skip in the c8391709 pre-check, which is a performance change only.
    • The lazy-slot cases of the ReadFile and VirtualQueryEx wrappers. Wine writes into an uncommitted mapped slot
      without faulting, so their mutations do not fail here. They are unmeasured on Windows as well.

Windows CI on this head:

  • The Build and test on Windows job ran the pr preset on 0c6be971 (run 45921712354, image
    windows-2025-vs2026). It ran 68 tests, and all of them passed.
    • Of the 28 page-run tests, 25 passed and 3 skipped (77): xmem_placeholder_split_control, _deployed and
      _partial, because Windows reports the placeholder bit.
    • The 25 include xmem_page_runs_device_warp, xmem_page_runs_device_join_warp and
      linear_buffer_backing_share_warp.
  • Those three need WARP and fail under Wine here ("WARP device not created"). CI is their only result on this head.
  • The _hardware variants have no result on this head. runtime: bring XMem page-run fixes into cpp-migration #2918 reported f6020249 passing on WARP and on a GTX 1080 Ti.

Evidence from a running title

Local runs of a console executable under test on Linux under Wine/Proton. These runs are of the branch up to its
third round commit (ReadFile/WriteFile), not of the final head. Each run had no timeout, and each configuration
was run once unless a count is given. Switches: 40 GiB, render, host CPU rewrites, GPU mirror, a fake user,
start-up blocks below 2^40, lazy commit and X2W_XMEM_SHARED_VIEWS=1. Joins were on, because that branch had no
join switch.

Like for like: the same launcher, environment and arguments, with only the build different. Main already
honours all three memory switches, but its shared views are the basic version.

Build Runs Frames presented until First archive that loading opens after the menus (after apparent hand input; see below) Start-up watchdog fired at
Main 6ee878d 2 15.1 and 16.5 s never opened 52.2 and 56.3 s
Main + groups 1 and 2 + split-first (no readers, no joins) 1 15.1 s never opened 53.5 s
Branch 2 167.8 and 169.0 s 69.8 and 71.5 s 204.5 and 208.5 s
Branch with the ReadFile/WriteFile retry turned off 1 173.5 s 79.3 s 213.8 s
Branch + the local menu-skipping change 1 242.6 s 53.3 s 282.4 s
  • "Frames presented until" is the last frame heartbeat. Main and the split-first build stop presenting at about
    15 s, and no frame follows in the 37-40 s before the watchdog fires.
  • Every run above ends at the same start-up watchdog, on the same thread stack. The branch builds keep
    presenting until 168-243 s before it fires. What the watchdog waits for in the branch runs is not determined.
  • In the branch run watched on screen, the menus appeared at about 70 s. The input script pressed A once, at about 50 s.
    The branch runs' menus appear to have been driven by hand as well: each run read an A 12-21 s before the script's,
    the runs without the menu-skipping change read 4-6 direction-pad presses before loading began, and a controller is
    visible to the executable. So the archive times likely show when someone picked a menu item, not how fast start-up
    ran, and they are not comparable between rows. Main never reached a point that reads input.
  • The local menu-skipping change is a runtime-proxy change that is not in this PR. With it, a run reads a save
    and skips the menus:
    • One of the two main runs had it. It did not get main past the stop: presents stopped at 15.1 s and the
      watchdog fired at 52.2 s.
  • One branch run started from the main run's exact shader cache, with the same name-and-size manifest and no new
    file during the run. It behaved like the other branch run, and main had the same environment, storage and
    driver state. So neither the shader cache nor that shared state separates the two builds.
  • Hypothesis, not tested: on main, a reader faults inside a run break and nothing handles the fault there.
  • Open item: in the branch run watched on screen, the executable showed its own "data file missing or corrupt"
    dialog after the menu.
    • Every archive it opened returned success at the file layer. The cause is undetermined.
    • Main never gets that far, so it is not shown that main would avoid the dialog.
  • The first branch run's log also has 11757 first-chance read AVs inside the D3D12 stand-in. Earlier builds on an
    older main did not show them. This PR changes no D3D12 code. Hypothesis, not tested: they come from main.

Other controls:

  • Main 6ee878d without shared views: no frame was presented, and start-up stopped on the same watchdog.
    • One run had lazy commit on, and its first stop came at 32.4 s.
    • The other had lazy memory off, and stopped at 32.3 s.
  • Older main (52905a3): hang at about 47 s in 2 of 2 runs.
  • The same groups before the cap (with a local counters-only layer, not in this PR): 2 of 2 runs had no
    start-up hang (1064 s and 408 s), with 54545 and 54549 joins and 0 lost.
    • After loading, frames fell to 2.4/s and 0.55/s, against 11.8/s with split-first only.
    • The cause was breaks of runs thousands of pages long, which is why the cap commit exists.

Windows runs by reviewers. These are the first Windows measurements of this work, and all of them are of earlier
heads:

  • @Not-Saii, fixtures at 4a43a173.
    • Setup: Windows 11 build 26300.9974, dev preset (Debug). WARP and an RTX 4090 (driver 32.0.16.1088), with
      the debug layer off.
    • 46 tests: 43 passed, 2 failed and 1 skipped (77). 9 _hardware tests: 7 passed and 2 failed.
    • The failures were xmem_page_runs_device and linear_buffer_backing_share, in both variants. All of them pass
      at 6ee878d8. ad1980cb and 238ed78a address them.
    • On this head, the _warp variants pass in CI (Test). The _hardware variants have not been run.
  • @pigalot, start-up at 4a43a173, Windows 11 with the D3D12 hardware backend, 900 s cap.
    • Lazy and shared-views switches on: a second-chance write access violation in the executable at 191.7 s
      (4706 frames). Main without this PR, same switches: the same kind of fault at 250.0 s (5410 frames), at a
      different address.
    • Switches off: the run reached the 900 s cap, with 16381 frames and no fault.
    • DirectStorage: each run had the same single failure, "destination capacity exceeded".
  • @MasterDomino, start-up with lazy memory off.
    • Setup: two matched pairs of 420 s runs on Windows 11 build 26300.9974, an RX 9070 XT (driver 32.0.32029.2010),
      release, debug layer off. Builds: runtime: bring XMem page-run fixes into cpp-migration #2918's head 27778f4f, which carries the same change as this PR at
      238ed78a, against cpp-migration 40365770.
    • All four runs ended on the timeout. Frames, storage requests, textures, pipelines and exceptions matched within
      noise.
    • Token null bindings were about 1.6 times the base in both pairs, which he marked as a hint, not a result.
    • With shared views off, f19890e4 takes no mutex and no run lookup for a DirectStorage copy, which is the path
      he named. That has not been measured.

Measured vs not

  • Measured:
    • the fixture behaviour above under Wine;
    • the counters and frame counts of the runs listed;
    • the reviewers' Windows runs of earlier heads;
    • CI's pr preset run of the final head.
  • Not measured:
    • any Windows run of the final head outside CI's pr preset, including the _hardware variants;
    • any start-up run of the final head;
    • whether xmem_lazy_commit_off passes on this head under Wine. Host noise decides it here;
    • whether f19890e4 changes the token null count in lazy-off runs;
    • how often the executable calls VirtualQueryEx (it imports it, but the calls were not counted);
    • what the start-up watchdog waits for in the branch runs;
    • the cause of the data-file dialog.
  • Known limits:
    • Placeholder fallback (Wine only).
      • It cannot tell a reserved page of a private allocation made with VirtualAlloc2(MEM_REPLACE_PLACEHOLDER)
        from a placeholder, because Wine does not refuse the release for that kind of allocation.
      • After a refused view, the batch retries each page on its own. That cut such a page out of the caller's
        allocation: the committed page's allocation then started one page later.
      • Where the OS reports the placeholder bit, the fallback is not used.
    • ReadFile/WriteFile retry.
      • It assumes that the failed call had no partial side effect. Only Wine's full failure was measured.
      • On a pipe, Wine keeps the data queued. Whether Windows consumes it is unmeasured, which is why only disk
        files are retried.
      • Only the main module's ReadFile and WriteFile imports are wrapped. Not wrapped: ReadFileEx/WriteFileEx,
        scatter/gather, DeviceIoControl, sockets, I/O from other modules and I/O through GetProcAddress.
      • Overlapped I/O bypasses the retry.
    • A genuine fault on a page whose run a break mapped is retried once per thread before it is passed on, and again
      after each later break that maps the page again.
  • Readers not moved: test-only CPU upload helpers, the opt-in capture path, the pointer-like-qword count
    heuristic, and MEM_PRIVATE heap paths.
  • Nothing here claims that a title runs or reaches gameplay.

The region query reports placeholders with Flags bit 0x100 on Windows. Wine leaves
it 0, so alias_placeholders refused every split or coalesce and queue slot 31
batches failed with ERROR_INVALID_ADDRESS whenever a run was not exactly one
placeholder. Probe once: if the bit is missing and VirtualFree with
MEM_PRESERVE_PLACEHOLDER refuses a plain reservation and leaves it whole, offer
any reserved region to the split. A plain one then fails the split instead of
being released. Where the bit is reported nothing changes.
…get control

xmem_placeholder_split maps two pool pages into the middle of an XMem reservation
through X2WMapPageBatch and checks the bytes, a write through the destination and
the reserved neighbours. A batch onto a committed page in the same reservation must
fail and keep its bytes. xmem_placeholder_split_control disables the fallback and
passes only when the split step fails; it skips where the OS reports the
placeholder bit.
Add x2w_xmem_copy_to_destination. It checks that the whole destination
capacity is committed and writable, and copies the data, both under
memory.mutex. A run break (unmapping a page of a shared run and mapping
its other pages again) also runs under that mutex, so it can neither
make the check see a transient reservation nor start during the copy.

Lazily mapped pages of both ranges are committed before the mutex is
taken. A fault under the mutex is passed on: the lazy commit handler now
skips mapping threads, as retry_remapped_page already did.

The region walk moves from XCRDAPI's destination_writable to
x2w_local::committed_writable, so both DLLs share one implementation.
A plain-memory destination is now checked and written by xmem.dll under
its mapping mutex when the export is there. Before, the VirtualQuery
pre-check could run while an XMem run break had the destination's page
unmapped for a moment, and refuse a destination that was mapped before
and after. Without xmem.dll the check and the copy run in XCRDAPI as
before.

A copy that faults on its source now fails the request ("copy faulted
(source not readable)") instead of raising in the stand-in.
startup_dstorage_break_window holds an unmap of page 5 of an 8-page run
in the window where the run is apart (the xmem test-controls build's
X2W_XMEM_TEST_HOLD_WINDOW), checks that page 2 reads MEM_RESERVE there,
and submits a memory-source request into page 2. The request must not
complete within the 1000 ms hold, then complete with S_OK with every
byte in place after the release. It also covers a request with no break
and a read-only destination refused on the xmem path.

startup_dstorage_break_window_control builds XCRDAPI with the export
lookup's name changed, so it judges the destination with its own
VirtualQuery inside the window. It must report "refused while the run
was broken".
A map into the middle of a larger placeholder always fails first, so every
single-page XMemMapPhysicalPages call made two MapViewOfFile3 attempts. Under
Wine each attempt is a server round trip whose cost grows with the view count.
Split first when the target is not an exact placeholder; fall back to the
map-first path if the split or the map fails. X2W_XMEM_TEST_MAP_FIRST=1 keeps
the old order for tests. x2w_xmem_map_counts reports the attempts.
With X2W_XMEM_SHARED_VIEWS=1, unmapping a page inside a run unmaps the whole run and maps its other pages
again. A touch of those pages in between is retried by the fault handler, but a region query or a kernel copy
does not fault: it reads the pages as reserved, or fails.

x2w_xmem_stable_call(fn, context) runs fn when no break begins or ends around it, else again under the mapping
mutex. XMemVirtualQuery, and through x2w_local::outside_run_breaks the stand-ins' x2w_local::writable,
measure_placed_buffer (slot 133) and snapshot_samplers, now answer that way. Without shared views no break
begins, so fn runs once as before.

xmem_run_break_readers holds a break open with the test-controls build and checks that each reader waits for
it and then sees the page committed; its control build runs the stand-in readers directly and they answer
inside the window.
The title queries its own pages through its VirtualQueryEx import. With X2W_XMEM_SHARED_VIEWS=1 xmem.dll now
points that import at a wrapper that runs the query through x2w_xmem_stable_call, so a page of a run that is
being taken apart is not reported as reserved. The module is pinned only when an import was changed (the pin
is undone with the import if it is refused), so a main module without the import can still unload the shim
before its first mapping call.

xmem_run_break_readers checks the wrapped import of its own executable inside a held break.
retry_remapped_page names the break a fault belongs to as seen | 1. When the
handler reads the generation after the break has ended (even), that names the
next break, so a fault on the same page in that next break is not retried and
the access violation ends the process. Name the break that just ended instead:
(seen - 1) | 1.

Back-to-back breaks over one page made this show up: with a reader on page 20
and page 21 toggled, the hold-ignored child of xmem_page_runs_check.py crashed
in 6 of 6 runs once map calls also open a break, and passed 6 of 6 with this.

Test build: X2W_XMEM_TEST_HOLD_RETRY=<name> holds the handler before it reads
the break. xmem_run_break_readers gets a case that faults in break A, is held
until A ends, and faults on the same page in break B. The
xmem_run_break_readers_next_break_control test runs it without the hold.
…after a break

Without X2W_XMEM_SHARED_VIEWS, every unmap still counts in the break generation
(begin_break is not gated on the switch), so x2w_xmem_stable_call made
default-mode readers wait for memory.mutex. Each page has a view of its own
there, and no break takes a page away from a reader, so stable_call now runs fn
directly until shared views are on. The comment, and the message of "xmem:
answer non-faulting readers outside run breaks", saying no break begins without
the switch were wrong.

XMemVirtualQuery and the VirtualQueryEx import wrapper answered into the
caller's buffer inside stable_call. On its mutex path the lazy-commit handler
passes faults on, so a buffer in an uncommitted mapped slot was not committed
there. Both now answer into a local copy and write it out after the call.
A refused pin of the VirtualQueryEx wrapper is logged with its error and the
restore count. measure_placed_buffer keeps the query count of a pass that ran
again. The docs describe stable_call and the load-time read of the switch.

"xmem: answer the main module's VirtualQueryEx outside run breaks" said the
title queries its own pages through VirtualQueryEx. An import scan shows only
that the executable under test imports it. How often it is called was not
measured.

Checks in xmem_run_break_readers: a break that begins while fn runs makes it run
again under the mutex (pins the after-check); default mode answers inside an
unmap (xmem_run_break_readers_default); lazy commit with the output in an
uncommitted slot (xmem_run_break_readers_lazy); the reader's 300 ms now count
from its start.
Start-up maps nearly every page with a call of its own (45595 of 45602 runs
had one page), and under Wine each map costs more the more views the process
has. With X2W_XMEM_SHARED_VIEWS=1, a mapping call whose pages form one run now
joins the recorded run that ends at its address. It joins only when its first
page is the next slot of the section behind that run's last page, and both lie
in one reservation of the shim.

The new pages' range is split to a placeholder of its own, the run is unmapped
inside a break (begin_break, as for an unmap), the two placeholders are joined,
and one view is mapped over both. A refused step before the unmap changes
nothing. After it, the run is mapped again as it was (map_recorded) and the
new pages get a view of their own. If the run cannot be restored, its pages are
forgotten and the call fails, as in an unmap. Default mode is unchanged.

Test build: X2W_XMEM_TEST_NO_MERGE=1 turns the merge off; X2W_XMEM_TEST_FAULT
stages 4 (joined map) and 5 (joining the placeholders) refuse a step.

xmem_cross_call_merge covers 32 single-page calls -> one view, an unmap in
the middle, slots in reverse order (32 views), a reader held in the window,
both refused steps, and two reservations. xmem_cross_call_merge_control
keeps the old one-view-per-call layout under NO_MERGE.
xmem_cross_call_merge_deployed shows the deployed shim ignores that switch.
xmem_cross_call_merge_default covers default mode.

xmem_page_runs_check.py expected "Mapping the page again does not join the
pieces: three views". The page now joins the lower piece (two views). The
three-view layout stays asserted by xmem_cross_call_merge_control.
xmem_page_runs_device_check maps the upper view first and the single pages in
reverse slot order, so its view boundaries stay.
An unmap inside a run takes the whole run apart and maps its other pages
again, and a cross-call merge does the same to the run it joins. Nothing
bounded the joined run, so after loading every title unmap inside a run of
thousands of pages cost one long break: 46-72 ms each in a gate run under Wine,
with readers waiting 63 s in all and presents at 2.4/s.

merge_with_previous now leaves a page on its own when the joined run would be
longer than max_merged_run_pages (64); the next pages join that one. The test
build's X2W_XMEM_TEST_MERGE_CAP replaces the cap for a timing mode.

Measured under Wine with every byte of the run written (xmem_cross_call_merge_check
timing, median of 5): an unmap inside a run of 64 pages takes 0.28 ms, of 512
pages 2.1 ms, of 4096 pages 13 ms; joining a page to a run of 63 takes 1.5 ms,
about what a page mapped on its own costs (1.4 ms). The fixture process has a
few views, the title process tens of thousands.

xmem_cross_call_merge gets a capped case: 192 single-page calls make three
views of 64 pages, and an unmap held in its window in the middle one takes
apart 64 pages while the outer two stay mapped. The control, deployed and
default modes now print their pass line at the end of main, only with no
failed check.
begin_break replaced the recorded ranges, so retry_remapped_page saw only the
last break. A handler that ran after a later break on another run had begun
found its address outside those ranges and passed the fault on. Joins made that
the normal case: one permissive XMemMapPhysicalPages call joins the CPU run and
then its GPU mirror run under one memory.mutex hold, and each map call opens a
break of its own.

RemapWindow now keeps the ranges of the last 64 breaks with their id (the odd
generation). The handler names the newest break whose ranges hold the address,
so a fault in a later break of the same page is still retried once more.

xmem_run_break_readers_two_runs faults in break A on run 1 and holds the
handler until break B on a disjoint run 2 is held open. xmem_run_break_readers_mirror
does the same across the CPU join and the GPU mirror join of one call
(X2W_XMEM_PERMISSIVE, X2W_XMEM_GPU_MIRROR). With one kept break both end the
process with an access violation (exit 5). The RemapWindow and hold_window
comments now also name the merge.
…e buffer

Inside a run break a kernel copy into or out of a page of the run fails at
once, with no fault to retry. Measured under Wine with the break held open
(xmem_run_break_readers ReadFile_import/WriteFile_import, before this change):
ReadFile fails with ERROR_NOACCESS and leaves the file position at 0,
WriteFile fails with ERROR_INVALID_USER_BUFFER.

With X2W_XMEM_SHARED_VIEWS=1 at load, the main module's ReadFile and WriteFile
imports go through a wrapper. A synchronous call that fails with one of those
errors while a break was in progress, or began or ended around it, and whose
buffer lies in a run, waits for memory.mutex and runs again, at most 4 times in
all. Other failures return at once as before: a read into a reserved page in
no run fails at once (io_outside_runs). With lazy commit the ReadFile wrapper
calls read_file_committed inside (lazy_read_file). Overlapped I/O, a call that
a break cuts short after part of the copy, and I/O from other modules stay
uncovered.
…lines at the end

XMemVirtualQuery copied all 64 bytes of the caller's buffer before the query and
wrote all 64 back after it. era_query writes 49 (the MBI and the tail byte at
0x30), so bytes 49-63 were read and rewritten where they never were before, and
a buffer of 49-63 bytes that ends at a page end could fault. The caller's buffer
is no longer read; on success tail_offset + 1 bytes go back, on failure none.

xmem_run_break_readers_default prints its pass line at the end of main, only
with no failed check, so a crash after the cases does not read as a pass.
xmem_page_runs_device_check prints a check_failed line before the map-order
returns, which were silent and looked like a refused device.
…g's edge

The cap of 64 pages (max_merged_run_pages) is checked only where a page joins
the run before it (merge_with_previous). A run mapped by one call (map_run in
map_pages_locked) is not capped, and neither are the survivors map_recorded maps
again after an unmap inside such a run. Both are as long as before the cap; a
single large call is the title's own choice and was not the regression. The
comment above the cap and docs/startup-runtime.md said the cap bounds every
later break of a run; they now say it bounds the runs that joining builds, and
name the two uncapped paths.

xmem_run_break_readers_ring_edge holds a fault in break A before its handler
reads the breaks, ends A, then makes 63 or 64 later breaks (map and unmap a
lone page). After 63, A is still recorded and the fault is retried; after 64,
A's slot (RemapWindow::kept) holds the newest break and the fault is passed
on, which the reader's own __except catches.
@JohnConnorNPC
JohnConnorNPC marked this pull request as ready for review November 26, 2026 19:37
@pigalot

pigalot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Tested on Windows 11 (D3D12 hardware backend) with the switches from "How to test", plus X2W_D3D12_BUDGET=1 and fresh storage for each run. Each run was capped at 900 s. In all three runs the executable got past the account prompt the same way.

Build X2W_XMEM_LAZY_COMMIT / LAZY_STARTUP / SHARED_VIEWS End Frames presented Peak commit
main 6ee878d + this PR (4a43a17) on second-chance write access violation in the executable at 191.7 s 4706 (to 183 s) 8.0 GiB
main 6ee878d + this PR (4a43a17) off timeout at 900 s, no fault 16381 (to 894 s) 23.0 GiB
main 6ee878d, without this PR on second-chance write access violation in the executable at 250.0 s 5410 (to 239 s) 8.0 GiB
  • On Windows the lazy/shared-views mode ends the run with a write fault in the executable's own code, with or without this PR. The two faults are at different addresses. So this PR does not seem to cause it. It is not the main 15-16 s present stall described above either.
  • With the switches off, this PR runs the full 900 s and keeps presenting.
  • DirectStorage: each run has the same single failure, destination capacity exceeded. The new copy faulted (source not readable) message never appeared.

All three builds also carried local diagnostics and input automation that are not part of this PR.

This comment was AI-assisted.

@Not-Saii

Not-Saii commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Review of the head 4a43a173, with a Windows run of the tests this PR adds or touches.

Two existing tests fail on this head and pass on main on the same machine, in their WARP and their hardware variants, both because single-page calls now join. Every new test passes on Windows. After the test report there are four review points and some smaller ones.

Windows test run

Windows 11 build 26300.9974, x64. MSVC 19.44.35230, dev preset (a Debug build). The WARP tests ran on Microsoft Basic Render Driver 10.0.26100.9818 with the D3D12 debug layer off. The _hardware tests ran on an NVIDIA GeForce RTX 4090 (10de:2684), driver 32.0.16.1088, also with the debug layer off. Head 4a43a173, control main 6ee878d8, both built in the same build directory.

cmake --preset dev
cmake --build --preset dev --target xmem_shim xmem_shim_test_controls startup_stub_xcrdapi startup_stub_xcrdapi_break_window_control xmem_run_break_readers_check xmem_run_break_readers_control xmem_cross_call_merge_check xmem_placeholder_split_check xmem_map_split_first_check startup_dstorage_break_window_check startup_dstorage_destination_check startup_dstorage_file_read_check xmem_page_runs_device_check linear_buffer_backing_check
ctest --preset dev --no-tests=error -R '^xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first|page_runs)|^xmem_shim_(page_runs|mapped_pages)$|^startup_dstorage_|^linear_buffer_backing_' -E '_hardware$' -V
ctest --preset dev --no-tests=error -R '^(xmem_page_runs_device|linear_buffer_backing_.*)_hardware$' -V

The build had no errors or warnings. The first ctest line selects 46 tests: 43 passed, 2 failed and 1 skipped (77). The second selects the 9 _hardware variants: 7 passed, 2 failed and none skipped.

  • Failed: xmem_page_runs_device_warp, linear_buffer_backing_share_warp, and the _hardware variant of each. All four pass at 6ee878d8: the two WARP tests with -R '^(xmem_page_runs_device_warp|linear_buffer_backing_share_warp)$' (2 of 2), and the whole second ctest line (9 of 9).
  • Skipped (77): xmem_placeholder_split_control, as expected, because Windows reports the placeholder bit.
  • Passed: the eight xmem_run_break_readers* tests, the four xmem_cross_call_merge* tests, xmem_placeholder_split, xmem_map_split_first and its control, startup_dstorage_break_window and its control, startup_dstorage_destination, the two startup_dstorage_file_read* tests, xmem_shim_page_runs, xmem_shim_mapped_pages, and the other 21 linear_buffer_backing_* tests of the first line. On hardware, the other seven linear_buffer_backing_*_hardware tests.

Pull-request CI covers almost none of this. On a pull request the Windows job builds and runs the pr preset only. That preset builds xmem_shim but none of the new test programs, and of the tests above its filter matches only xmem_shim_mapped_pages. So where the description says Windows CI is the first real check, that check only happens on main after a merge.

The two failures

xmem_page_runs_device_warp, which I also reported on #2905:

FAIL across two single-page views: the buffer touches 1 view(s), expected 2

A page gets its section slot when it is first mapped (section_for, called from map_pages_locked), not when XMemAllocatePhysicalPages returns its token. Mapping single[7 - i] therefore still takes consecutive slots in call order, and each call joins the one before it. The reversed case of tests/xmem_cross_call_merge_check.cpp handles this by mapping all the pages through one view first. This test needs the same step. xmem_page_runs_device_hardware fails with the same line.

linear_buffer_backing_share_warp, in its split case:

share    xmem     split  FAIL: slot 133 -> 0x00000000, copied (share 0x80070032, feature 0x00000000, 131072 bytes in 1 refreshes, staging 1); GPU read B=1024/1024

At 6ee878d8 the same row passes with share 0x80070057. allocate_split maps alloc_pages - 1 pages and then one more page with a second call, and expects a view boundary inside the buffer. The second call now joins the first, so sharing is no longer refused with E_INVALIDARG. linear_buffer_backing_share_hardware fails on the same row with the same values. The description lists this family as not measured.

The pin row of the same test changes too. It is measured, not asserted. OpenExistingHeapFromAddress at the address of a mapping call returns 0x80070057 on this head and 0x00000000 at 6ee878d8. The likely reason is that the address is no longer the base of a view once the call has joined its neighbour, but I have not confirmed that.

Review points

  1. With xmem.dll loaded, a DirectStorage copy skips the check from startup: probe DirectStorage destinations instead of walking them with VirtualQuery #2685 and holds the mapping mutex during the region walk. x2w_xmem_copy_to_destination (src/xmem_mapped_pages.cpp:1864) judges the destination with x2w_local::committed_writable alone, which is the VirtualQuery walk. startup: probe DirectStorage destinations instead of walking them with VirtualQuery #2685 added resident_read_write, a QueryWorkingSetEx check, because that walk cost about 10 ms per request. It is now reached only without xmem.dll. The walk and the copy both run under memory.mutex, so mapping calls, lazy-commit faults and break retries wait behind them. The start-up runs in my comment on #2905 fit this: one 16-byte copy held the mutex for 69.7 ms, and the longest holds (24.8 to 102.3 ms) belonged to copies of 16 bytes to 450 KiB. Could the resident check run first, and the mutex be taken only when the destination or the source overlaps a run?

  2. Is a history of 64 breaks enough now that a joining call is a break? retry_remapped_page retries a fault only while its break is still in RemapWindow::recent. Before this PR breaks were rare. Now every single-page call that joins is one, or two with a GPU mirror, so the history spans 32 to 64 map calls. Take a thread that faults during the join that fills a run. If its handler only runs after 64 later joins on the next run, the access violation is passed on although the page is mapped. xmem_run_break_readers_ring_edge asserts that outcome. With lazy commit on, lazy_commit_fault retries once per page and hides the first miss. With shared views alone, nothing does. I have not reproduced this. Stamping each run with the generation that last remapped it, and comparing that under memory.mutex, would give a test that does not expire.

  3. Joins are active on native Windows too, because run.ps1 -LazyMemory sets X2W_XMEM_SHARED_VIEWS. Every single-page call that continues a run now unmaps and remaps up to 63 live pages. The description's own timing puts a join at the cap at about the cost of a separate map under Wine (1.515 ms against 1.392 ms), so the gain there comes from having fewer views. Nothing measures a gain on Windows, and the two failures above show that the layout change reaches code outside xmem. The Windows start-up runs so far, the one reported above by pigalot and mine on xmem: keep shared page runs consistent for readers, and join single-page maps into them #2905, got as far as main did with the switches on. So I see no breakage from the joins on Windows, and no measured benefit either. Would a separate switch for joins make sense, so that -LazyMemory on Windows keeps the layout it had?

  4. Under Wine, the placeholder fallback checks one page per allocation. I read this from the code and did not reproduce it. In alias_placeholders (src/xmem_mapped_pages.cpp:1365) the fallback accepts an allocation when the page at address reads MEM_RESERVE, then skips the whole allocation. The probe shows that the OS refuses a reservation that was never a placeholder. A block that replaced a placeholder is a different case, since VirtualFree(MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER) is the documented way to free one back to a placeholder. Suppose the title commits a block at an address inside an XMem reservation and later decommits part of it. Is a slot 31 batch that names that part refused under Wine? On Windows the bit check refuses it.

Smaller things

  • X2W_XMEM_TEST_MAP_FIRST (src/xmem_mapped_pages.cpp:395) and X2W_XMEM_TEST_NO_PLACEHOLDER_FALLBACK (line 1332) are read by the deployed xmem.dll, and their controls run against xmem_shim. The Switches table lists both as test-controls build only. X2W_XMEM_TEST_NO_MERGE is guarded and has a _deployed test.
  • The ReadFile/WriteFile retry does not look at the handle type. On a pipe or an append handle the failed call may already have consumed or written bytes. Limiting the retry to FILE_TYPE_DISK would keep it to the case that was measured.
  • src/startup_stubs/xcrdapi.cpp:954 logs every error except ERROR_NOACCESS as "copy faulted (source not readable)". That includes ERROR_INVALID_PARAMETER and a fault on the destination.
  • x2w_xmem_map_counts was inserted between the slab-accounting comment and x2w_xmem_slab_stats, so that comment now sits on the wrong export (src/xmem_mapped_pages.cpp:1767).
  • tests/xmem_cross_call_merge_check.cpp refers to "ready.md's table" (line 329) and "the review notes" (line 13). Neither is in the repository.
  • Repository conventions. The new registrations are in CMakeLists.txt, where tests/cmake/README.md asks for a tests/cmake/<family>.cmake fragment. The xmem_placeholder_split note went into docs/experiments.md, where AGENTS.md says new notes go in docs/experiments/<topic>.md. AGENTS.md also asks for one reviewable behavior per pull request, and the description names three groups. The slot 31 fallback and the fault-retry fix look independent of the joins and could land on their own.

Measured here: the test results above, on one machine and one GPU. Not measured: points 2 and 4, which come from reading the code, and any start-up run in this pass.

Edited once, to add the _hardware variants.

AI-assisted (Claude Code).

@JohnConnorNPC

Copy link
Copy Markdown
Contributor Author

@pigalot Thanks for the Windows runs of 4a43a173.

Noted from your table (Windows 11, D3D12 hardware backend):

  • With the memory switches off, this PR ran the full 900 s and kept presenting (16381 frames to 894 s).
  • With them on, the run ended on a write fault in the executable at 191.7 s with this PR and at 250.0 s without it, at different addresses. So on Windows, main with the switches on gets well past the point where it stops under Wine (52-56 s in our runs), and these runs show no gain from this PR there.
  • DirectStorage: one destination capacity exceeded failure per run, and no copy faulted (source not readable) line.

Whether joins should stay on under -LazyMemory on Windows is being considered together with the review points below.

This comment was AI-assisted.

@JohnConnorNPC

JohnConnorNPC commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

@Not-Saii Thanks for the review and for the Windows test run of 4a43a173.

Both failures are valid at 4a43a173, and the description was wrong about the device test:

  • xmem_page_runs_device: a page gets its section slot at its first map (section_for, called from map_pages_locked). The single-page calls therefore take consecutive slots in call order, and each call joins the view before it. The description said this test's view boundaries were the same as before. That came from reading the code, not from a run, and it was wrong. So was the test's comment about reverse slot order.
  • linear_buffer_backing_share: the second call in allocate_split takes the next slot at the next address, so under X2W_XMEM_SHARED_VIEWS=1 it joins the first call, and sharing is no longer refused with E_INVALIDARG.
  • Our Wine host could not run either family. Tests that import d3d12.dll could not start in our test prefix, and all four of these exited 53 there, on main and on this head alike. Our suite therefore counted them among the failures both sides share.

Fixture changes:

I checked your four review points and the smaller ones against the code at 4a43a173, and each matches it as you describe. I have not reproduced points 2 and 4. How to address them is being considered: the DirectStorage copy path, the break history, a separate switch for joins, the placeholder fallback, the CI coverage, where the test registrations and notes live, and the scope of this PR.

This comment was AI-assisted.

MasterDomino added a commit that referenced this pull request Nov 26, 2026
#2931)

This reverts commit ee76dfa. Those are the 17 XMem commits of #2911, which
is still under review against main, plus two fixture changes. Keeping them
off cpp-migration keeps the branch to the native compiler work. When #2911
lands on main, merging main into cpp-migration brings in the reviewed
version.

Every file #2918 touched is restored to its state at cd752a8. The later
#2917 touches none of them.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
…runs.cmake

The three blocks this branch added to CMakeLists.txt move verbatim into a
fragment (tests/cmake/README.md), so CMakeLists.txt is main's again. The
fragment is included after every target and helper it names. The configured
tests (names, commands, properties) and the compile and link commands of the
moved targets are the same before and after. The break-window fixture's comment
names the fragment for its negative control.
The R0Y00#1199 comment had ended up above x2w_xmem_map_counts. The cross-call
merge fixture's comments now say what the timing mode prints instead of
pointing at notes outside the repository. Comments only.
A mapping call joins the shared view that ends at its address only with
X2W_XMEM_JOIN_CALLS=1, read once with X2W_XMEM_SHARED_VIEWS by the first
mapping call; without shared views it does nothing. It is off by default,
so the layout with shared views alone is main's again (one view per call).
run.sh sets it beside X2W_XMEM_SHARED_VIEWS, where the joins were measured;
run.ps1 -LazyMemory does not.

With the switch off and no test changes, these fail under Wine and now set
it: xmem_cross_call_merge, xmem_cross_call_merge_deployed and
xmem_run_break_readers_mirror; xmem_shim_page_runs is main's check again
(three views). Every mode of the branch's fixtures sets or clears the switch
itself. New: xmem_cross_call_merge_no_join (deployed shim, shared views, no
join switch: one view per call) and xmem_page_runs_device_join_warp (the
view boundaries hold with joins on). startup_run_sh_check checks the export.
X2W_XMEM_TEST_MAP_FIRST and X2W_XMEM_TEST_NO_PLACEHOLDER_FALLBACK are read
only under X2W_XMEM_TEST_CONTROLS; the deployed xmem.dll no longer contains
either name and behaves as if both were unset.

xmem_map_split_first_control and xmem_placeholder_split_control now run
against xmem_shim_test_controls. New xmem_map_split_first_deployed and
xmem_placeholder_split_deployed set the switch for the deployed shim and
must give the default result (the placeholder one skips with 77 where the
OS reports the placeholder bit, as its control does). Each mode of both
check programs sets or clears its switch itself, so the CTest entries no
longer pass it.
Where the region query does not report the placeholder bit (Wine),
alias_placeholders accepted a view after one VirtualQuery at the start of
each allocation and then skipped to the allocation's end. It now walks
every VirtualQuery region the view covers and requires each to be
reserved.

New xmem_placeholder_split_partial (test-controls build, shared views):
two pages of the XMem reservation become a private reservation whose
second page is committed and tagged, and a batch maps a two-page view
there. With the old walk under Wine (Proton Experimental) the batch
succeeded: alias refusals 0, one placeholder release tried, and the
committed page became a mapped view reading the pool page (0x53 instead
of the 0x7a tag). With the new walk the view is refused (alias refusals
2, one release: the per-page retry maps the reserved page alone and the
rollback unmaps it), the batch fails with 0x80070057 and the committed
page keeps its tag. The new x2w_xmem_alias_counts export (@27) counts
alias_placeholders refusals and placeholder releases on the
X2WMapPageBatch path in the test-controls build; the deployed build
returns FALSE. Where the OS reports the bit the case skips (77).
…eak history

retry_remapped_page retried a fault only while the break that took the
page apart was among the last 64 recorded. A handler that ran after 64
later breaks on other runs passed the fault on although the page was
mapped again, which xmem_run_break_readers_ring_edge asserted.

Each recorded view now keeps the id of the break inside which it was
mapped (0 outside breaks): the survivors map_recorded maps again after an
unmap, a joined run, and the views of a page batch. The handler reads
that stamp under memory.mutex, after the break in progress, and retries
once per page and stamp per thread, so the id never expires while the
view lasts. The ring and its ranges are gone; the generation stays for
x2w_xmem_stable_call and the ReadFile/WriteFile retry.

Because the handler is registered first and now needs the mutex for the
lookup, faults that cannot be the transient one skip it: with the
generation even before and unchanged after a VirtualQuery of the page and
no mapped view there, the fault is passed on at once (faults on lazily
committed memory take this path). Any other fault waits for the mapping
call, as the ring lookup's hits did.

xmem_run_break_readers_ring_edge becomes xmem_run_break_readers_stamps:
63, 64 and 200 later breaks (under Wine 3, 4 and 10 ms), all retried; the
64th used to be passed on. New in the same mode: a page made no-access
faults once on a run no break mapped (passed on at once) and twice after
a break mapped it (one retry), counted by a first-chance handler.
The ReadFile/WriteFile wrappers ran a synchronous call again after a run
break whenever it failed with ERROR_NOACCESS or ERROR_INVALID_USER_BUFFER
into or out of a run. On a pipe, socket or console the failed call may
already have consumed or sent data, so they now do that only when
GetFileType reports FILE_TYPE_DISK, asked only on that failure path.

New window case ReadFile_pipe in xmem_run_break_readers: an anonymous
pipe with two 4096-byte messages queued, read into the unmapped page.
Under Wine (Proton Experimental) the read fails there with ERROR_NOACCESS
and leaves both messages queued. Before this change the wrapper waited
for the break and ran the read again, which failed as well: under Wine a
pipe read into a mapped page of a run fails with
ERROR_INVALID_USER_BUFFER even outside a break (into a VirtualAlloc
buffer it reads the first message). Now the read fails inside the window;
the case also checks that its error is one the wrapper retries on a disk
file, so answering at once is not vacuous.
…uches a run

x2w_xmem_copy_to_destination held memory.mutex for every copy and judged
the destination with the region walk alone, so with xmem.dll loaded the
working-set check from R0Y00#2685 never ran and every copy waited for, and
blocked, the mapping calls.

With shared views on, a short hold of the mutex now looks up whether the
destination or the source overlaps a recorded run. Only then do the check
and the copy run under the mutex, as before. Otherwise, and always with
shared views off (each page has a view of its own and no unmap takes a
neighbour away), both run without it. Both paths check resident
read-write pages first and fall back to the region walk, as XCRDAPI does
without xmem.dll; resident_read_write moves from xcrdapi.cpp to
src/startup_stubs/resident_pages.h, which both include. A run can appear
over a range only through a mapping call on those addresses during the
copy, which is the caller's race.

New x2w_xmem_copy_counts (@24) counts copies made with and without the
mutex in the test-controls build; the deployed build returns FALSE. New
startup_dstorage_break_window_plain: a VirtualAlloc destination copies
without the mutex (0 with, 1 without) and a destination in the run with it
(1, 0); startup_dstorage_break_window_views_off: with shared views off a
destination in an XMem page copies without it (0, 1). The break-window
case and its control are unchanged.
A copy that x2w_xmem_copy_to_destination did not complete was logged as
"copy faulted (source not readable)" for every error but ERROR_NOACCESS.
XCRDAPI now names each: ERROR_NOACCESS "destination not writable",
ERROR_INVALID_ADDRESS "copy faulted (part of the destination may be
written)", ERROR_INVALID_PARAMETER "invalid copy request", any other
"copy failed (error N)". The StorageFailure causes are unchanged. The
export's comment says that ERROR_INVALID_ADDRESS also covers a
destination page that stopped being writable during the copy.

New startup_dstorage_break_window_outcomes: the test-controls build's
X2W_XMEM_TEST_COPY_ERROR=<code> fails the copy with that code before
anything is written; for 998, 487, 87 and 31 the request fails, its
destination keeps its bytes and the logged line carries the text above
(the check points the standard error handle at a temporary file).
…tures to the pr preset

docs/experiments.md is main's again. Its xmem_placeholder_split paragraph
moves unchanged to docs/experiments/xmem-page-runs.md, with short
sections for the split-first, cross-call join, run-break and DirectStorage
fixtures and the X2W_XMEM_JOIN_CALLS switch, each with what was measured
under Wine and its ctest command. The README row keeps its -R line and
links the note next to the command.

The pr build preset also builds xmem_shim_test_controls, the two XCRDAPI
builds and the fixtures' check programs, including
xmem_page_runs_device_check and linear_buffer_backing_check. Its test
include adds the xmem_run_break_readers, xmem_cross_call_merge,
xmem_placeholder_split, xmem_map_split_first and
startup_dstorage_break_window families, xmem_page_runs_device(_join)_warp
and linear_buffer_backing_share_warp: 28 tests that the pr include did not
match before, every add_test of tests/cmake/xmem_page_runs.cmake plus the
two named outside it. startup_run_sh stays out: it skips on Windows, and
the Linux Python job runs tests/startup_run_sh_check.py.
…for MEM_REPLACE_PLACEHOLDER allocations

xmem_placeholder_split_partial now checks that the two pages start as one
allocation, prints where each page's allocation starts after the batch,
and asserts that the first page is still one reserved page whose
allocation base is its own address.

Under Wine (Proton Experimental) the batch's per-page retry releases the
reserved first page and the rollback unmaps it again, which cuts that
page out of the caller's allocation: the committed page's allocation
starts at +0 pages before the batch and at +1 after it. The fallback
cannot tell a reserved page of a private allocation made with
VirtualAlloc2(MEM_REPLACE_PLACEHOLDER) from a placeholder, because Wine
does not refuse the release for that kind of allocation. The comment
above alias_placeholders said the OS refuses the release for a plain
reservation. That holds for VirtualAlloc and not for this kind, so it
now names both and the limit, and so does the note in
docs/experiments/xmem-page-runs.md.
… is measured

docs/startup-runtime.md gave "the failed call may already have consumed
or sent data" as the reason a pipe, socket or console call is not run
again, followed by a Wine measurement that shows the data was left
queued. It now states the Wine result on its own and says that whether
Windows consumes or sends data in such a failed call is unmeasured. The
comment above the ReadFile/WriteFile wrappers gets the same sentence.
…by its stamp lookup

The header of tests/xmem_run_break_readers_check.cpp and the comments
in next_break_case, two_runs_case and mirror still said the handler
"reads the generation" or "the breaks", the wording from when it looked
breaks up in a history of recent ones. They now say what it does: it
finds the page mapped again and looks up the stamp the break left on
the run. The next-break-control line printed by the fixture and one
comment in tests/cmake/xmem_page_runs.cmake follow. Comments and one
log line only.
…xture's usage comment

The Usage comment at the top of tests/xmem_cross_call_merge_check.cpp
lacked the no-join mode that the header describes and the usage line
printed by main lists. Comment only.
…row's first link

The README row for queue slot 31 carried the link to
docs/experiments/xmem-page-runs.md after the ctest command in its
second column. It now sits next to the experiments.md link in the
first column. Layout only.
@JohnConnorNPC

Copy link
Copy Markdown
Contributor Author

The branch is now at 0c6be971, 34 commits on main 6ee878d8. The description lists each commit and what it changes. The 17 commits after 4a43a173 address @Not-Saii's review points 1 to 4 and the smaller ones (see "Review changes"), and they carry #2918's f6020249 and 27778f4f as ad1980cb and 238ed78a (same patch-ids).

@MasterDomino Thanks for the lazy-off runs on #2918 (comment 7204145940), and for labelling the token null difference as a hint.

  • The path you named changed in f19890e4. With shared views off, a DirectStorage copy through xmem.dll now takes no mapping mutex and no run lookup. It checks resident read-write pages first and then falls back to the region walk, as XCRDAPI does without xmem.dll (startup: probe DirectStorage destinations instead of walking them with VirtualQuery #2685). With shared views on, it takes the mutex only when the destination or the source overlaps a recorded run.
  • Measured under Wine with the test-controls build's counters:
    • startup_dstorage_break_window_views_off: with shared views off, an XMem page destination was copied 0 times with the mutex and once without it.
    • startup_dstorage_break_window_plain: a VirtualAlloc destination was copied without the mutex, and a run destination with it.
  • Your runs were of 27778f4f, which held the mutex for every copy. If you can, a re-run of the same lazy-off pair on 0c6be971 would test your hypothesis: does the token null count follow that path? We cannot run it, because our host has no Windows.

Windows fixture run: could a reviewer with Windows run these on 0c6be971?

cmake --preset dev
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^(xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first|page_runs_device|shim_page_runs)|startup_dstorage_break_window|linear_buffer_backing_share)' -V
  • This selects 31 tests in our configure:
    • xmem_page_runs_device_hardware and linear_buffer_backing_share_hardware need a physical GPU.
    • The three _warp tests need WARP.
    • xmem_placeholder_split_control, _deployed and _partial should skip (77) where the OS reports the placeholder bit.
  • The pr CI preset now runs 28 of them, all but the _hardware two and xmem_shim_page_runs.
  • CI's Build and test on Windows job ran the pr preset on 0c6be971 (run 45921712354, image windows-2025-vs2026). It ran 68 tests and all of them passed. Of the 28 above, 25 passed and 3 skipped (77): xmem_placeholder_split_control, _deployed and _partial.
    • The 25 include xmem_page_runs_device_warp, xmem_page_runs_device_join_warp and linear_buffer_backing_share_warp.
    • xmem_page_runs_device and linear_buffer_backing_share failed at 4a43a173.
  • Still without a Windows result: xmem_page_runs_device_hardware, linear_buffer_backing_share_hardware and xmem_shim_page_runs, and every test on a physical GPU.
  • Please give the adapter, driver, Windows build and debug-layer status with the counts.

Measured here, on Linux under Proton Experimental (wine-11.0) with a clang-cl cross build at 0c6be971:

  • The 31 tests above: 26 passed. The three _warp tests failed because there is no WARP here, and the two _hardware tests skipped (77).
  • The full suite: 614 passed, 1668 failed and 109 skipped (77) of 2391. The failures are the host's: no D3D12, WARP or GPU, and targets the cross toolchain cannot build.
  • The pure-Python checks: all 210 passed.
  • xmem_lazy_commit_off is unmeasured under Wine, because host load decides it.

Not measured: a Windows run of this head outside CI's pr preset, and any start-up run of this head. Nothing here claims that a title runs.

This comment was AI-assisted.

@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.

Independent Windows integration run at 104617d6 with current main c09145a7 (8c7bdd99 locally). Both Debug and Release /O2 built the listed fixtures. ctest --preset <dev|release> --no-tests=error -V -R '^(xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first|page_runs_device|shim_page_runs)|startup_dstorage_break_window|linear_buffer_backing_share)' gave 28 passes and zero failures in each preset. The three Wine-oriented placeholder controls (control, deployed, partial) skipped with 77 because Windows reports the placeholder bit.

The DirectStorage break fixture waited and copied all bytes, and the negative control reported a refusal during the break. The prior review's page-runs and linear-buffer-sharing fixtures passed on both WARP and RTX 4060 Laptop, including the WARP join variant. Windows 11 build 26200.9974, WARP driver 10.0.26100.9818 and RTX driver 32.0.16.1656, debug layer off for these D3D12 cases. Hosted Windows and Linux checks are green.

This run covers the Windows fixture matrix. The PR's original-executable and Wine evidence is from earlier heads. Can the Wine tester repeat a current-head run with the default run.sh joining behavior and add the matching control run with X2W_XMEM_JOIN_CALLS=0? The final-head startup performance and the Wine-only placeholder branches are the remaining evidence gaps from this run.

@JohnConnorNPC

Copy link
Copy Markdown
Contributor Author

Thanks for the independent Windows run and for the adapter and driver details.

Fixture-level Wine evidence at 104617d6: under Proton Experimental's Wine on Linux (test binaries cross-built with clang-cl 19 against the Windows SDK, our CTest-driven Wine runner, 2 jobs), all four placeholder-split rows pass: xmem_placeholder_split, xmem_placeholder_split_control, xmem_placeholder_split_deployed and xmem_placeholder_split_partial. The partial row's result line:

PASS xmem_placeholder_split_partial (exit 0) last: x2w-xmem placeholder bit reported=0, plain reservation kept by a placeholder release=1

These are fixture results only. They are not a start-up run and say nothing about start-up performance at this head.

Once the Wine test machine is free, I'll repeat the start-up run at the current head with run.sh's default joining, plus the matching control run with X2W_XMEM_JOIN_CALLS=0. For each run I'll post the commit, the command, the environment and the end line or first mismatch.

This comment was AI-assisted.

@JohnConnorNPC

Copy link
Copy Markdown
Contributor Author

Correction: the branch runs' "first archive" times in the Evidence table (69.8, 71.5, 79.3 and 53.3 s) came after what appears to have been hand controller input in the menus: button reads before the script's A, direction-pad presses, and a controller visible to the executable. The body did not say this, so those times are not start-up timings and are not comparable between rows. I've edited the body to say so.

New measurement at 0c6be971 with the input script only (one A, no hand input), under Wine: the executable reads the A at 49 s and then opens no archive after the menus before we stopped the runs at about 6.5 min (3 runs). With the local menu-skipping change, which is not in this PR, it opens that archive at 67.3 s (1 run).

This comment was AI-assisted.

@JohnConnorNPC

Copy link
Copy Markdown
Contributor Author

Start-up runs under Wine for the joining switch, as asked in the last review. Two parts are measured. The current head is not yet.

Measured: 0c6be971 (this PR's head before merging main) with the local menu-skipping change. That change is not in this PR. It lets a script-only run read a save and skip the menus. Without it, the menus need input the script doesn't give: the runs read the script's one A at about 49 s and then wait in the menu (3 runs, stopped at about 6.5 min).

  • Launcher: tools/startup/run.sh --seconds 0 --memory-gib 40 --continue-asserts 1000 --fake-user --render --translate --gpu-mirror --host-cpu --startup-below-40, an input script that presses A once, X2W_XMEM_LAZY_COMMIT=1 X2W_XMEM_LAZY_STARTUP=1, and the switches listed in the body. X2W_TITLE_ID/X2W_STORAGE_MB were set by hand (see below).
  • A: run.sh's defaults, so X2W_XMEM_SHARED_VIEWS=1 and X2W_XMEM_JOIN_CALLS=1. C: the same with X2W_XMEM_JOIN_CALLS=0. In the executable's environment, that is the only difference.
  • Environment: Linux 6.12, Proton Experimental, an NVIDIA RTX 4070 Ti SUPER, a copy of a warm shader cache (key revision 28), xapu built with Opus. One run each, no timeout.
run A read first archive after the menus last frame heartbeat end
A (joins on) 49.0 s 67.3 s 2683 frames at 164.9 s write access violation in the executable, 200.7 s
C (joins off) 49.3 s 75.7 s 6556 frames at 625.6 s the same access violation, at the same address, 661.9 s
  • First difference: C presented no frame from 10 to 20 s (288 frames at both marks, A 320 at 20 s). C then opened the archive 8.4 s later.
  • Both runs end with the same second-chance access violation, at the same address, as the earlier branch runs in the body's table.
  • With one run each, the different end times (200.7 vs 661.9 s) are not a measured effect of the switch.

Not measured yet: the current head. main's #2979 changes the shader key revision from 28 to 29, so every earlier translation misses. A current-head run first needs those shaders translated again. So far the start-up path has asked for 1855 rev-29 requests. 3 graphics requests translated; the rest were refused, mostly compute. That run is next, once the test machine is free.

Found along the way, outside this PR: on Linux, run.sh looks for MicrosoftGame.config, and this package ships MicrosoftGame.Config, so the manifest's title ID and storage size are not read. run.sh also needs a desktop DISPLAY in its environment.

This comment was AI-assisted.

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