Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ if(EXISTS "${INTAKE_PYTHON}")
"${CMAKE_SOURCE_DIR}/tests/xmem_mapped_pages_commit_check.py" "$<TARGET_FILE:xmem_shim_test_controls>" failure)
set_tests_properties(xmem_mapped_pages_commit xmem_mapped_pages_commit_failure PROPERTIES TIMEOUT 60 RUN_SERIAL TRUE)
set_tests_properties(xmem_mapped_pages_commit_failure PROPERTIES ENVIRONMENT "X2W_XMEM_TEST_COMMIT_FAILURE=1")
# X2W_XMEM_LAZY_COMMIT=1: pages committed on first touch; kernel writes need x2w_xmem_commit_range (#1199).
add_test(NAME xmem_lazy_commit COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/xmem_lazy_commit_check.py" "$<TARGET_FILE:xmem_shim>")
set_tests_properties(xmem_lazy_commit PROPERTIES TIMEOUT 60 RUN_SERIAL TRUE ENVIRONMENT "X2W_XMEM_LAZY_COMMIT=1")
# Control: with the switch off, mapping commits every page as before.
add_test(NAME xmem_lazy_commit_off COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/xmem_lazy_commit_check.py" "$<TARGET_FILE:xmem_shim>" off)
set_tests_properties(xmem_lazy_commit_off PROPERTIES TIMEOUT 60 RUN_SERIAL TRUE ENVIRONMENT "X2W_XMEM_LAZY_COMMIT=0")
add_test(NAME xmem_lazy_startup COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/xmem_lazy_startup_check.py" "$<TARGET_FILE:xmem_shim>")
set_tests_properties(xmem_lazy_startup PROPERTIES TIMEOUT 60
ENVIRONMENT "X2W_XMEM_LAZY_COMMIT=1;X2W_XMEM_LAZY_STARTUP=1")
# Control: with X2W_XMEM_LAZY_STARTUP off, the startup blocks are committed as before.
add_test(NAME xmem_lazy_startup_off COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/xmem_lazy_startup_check.py" "$<TARGET_FILE:xmem_shim>" off)
set_tests_properties(xmem_lazy_startup_off PROPERTIES TIMEOUT 60
ENVIRONMENT "X2W_XMEM_LAZY_COMMIT=1;X2W_XMEM_LAZY_STARTUP=0")
add_test(NAME startup_heartbeat_commit COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_heartbeat_check.py"
"$<TARGET_FILE:xmem_shim>" "$<TARGET_FILE:startup_stub_xcrdapi>")
Expand Down Expand Up @@ -258,6 +275,17 @@ endforeach()
# startup blocks entirely below 2^40 or refuses them. The check loads xmem.dll by path; the deployed
# shim must ignore the test-build mutants, and each mutant in the test-controls build must make the
# named check fail.
# The import wrappers of X2W_XMEM_LAZY_COMMIT / X2W_XMEM_LAZY_STARTUP (#1199): ReadFile and VirtualFree of the
# main module, which the Python checks cannot reach.
add_executable(xmem_lazy_imports_check tests/xmem_lazy_imports_check.cpp)
target_compile_features(xmem_lazy_imports_check PRIVATE cxx_std_17)
add_dependencies(xmem_lazy_imports_check xmem_shim xmem_shim_test_controls)
add_test(NAME xmem_lazy_imports COMMAND xmem_lazy_imports_check "$<TARGET_FILE:xmem_shim>")
set_tests_properties(xmem_lazy_imports PROPERTIES TIMEOUT 60)
# A refused fault handler (test-controls build) leaves the switches off: pages and blocks are committed up front.
add_test(NAME xmem_lazy_handler_failure COMMAND xmem_lazy_imports_check
"$<TARGET_FILE:xmem_shim_test_controls>" handler-failure)
set_tests_properties(xmem_lazy_handler_failure PROPERTIES TIMEOUT 60)
add_executable(xmem_startup_below_40_check tests/xmem_startup_below_40_check.cpp)
target_link_libraries(xmem_startup_below_40_check PRIVATE OneCore)
target_compile_features(xmem_startup_below_40_check PRIVATE cxx_std_17)
Expand Down
57 changes: 57 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -9196,6 +9196,63 @@ cmake --build --preset dev --target xmem_startup_below_40_check
ctest --preset dev --no-tests=error -R '^xmem_startup_below_40' -V
```

### Mapped pages committed on first touch

Experimental, for [#1199](https://github.com/R0Y00/Xbox2Windows/issues/1199). Physical pages are 64 KiB slots of `SEC_RESERVE` slabs (see [Startup memory accounting](startup-runtime.md#startup-memory-accounting)). By default a fresh slot is committed when its page is first mapped. A startup run maps far more pages than it touches before the main load, and slab commit is charged to the system commit limit, not to the process. On a 16 GB host this pushes the system to its commit limit during the load.

`X2W_XMEM_LAZY_COMMIT=1` leaves a fresh slot uncommitted when its page is mapped. The first CPU access faults, and a vectored exception handler installed when `xmem.dll` loads commits that slot and resumes the thread. A thread that loses the race to commit the same slot finds it committed and retries once. If the handler cannot be installed, the switch acts as off: pages are committed when they are mapped and no import is wrapped. GPU-view addresses resolve to the CPU-view slot (the same section). Reused slots and `x2w_xmem_write_physical` still commit before use, as before.

Some writers cannot fault a slot in, so they commit the range first through the experimental export `x2w_xmem_commit_range(address, bytes)`:

- **Kernel-mode I/O.** `ReadFile` into an uncommitted mapped page fails with `ERROR_NOACCESS` instead of faulting. The main module's `ReadFile` import is wrapped to commit the buffer first. Only that import is wrapped. Other kernel-mode accessors of an untouched page fail the same way: `ReadFileEx`, overlapped reads issued elsewhere, `WriteFile` from a lazy page, and calls from other modules' imports. The startup runs reached only the main module's `ReadFile`.
- **GPU placement.** Device slot 133 commits a placed buffer's range before it measures the allocation.
- **Stand-in writes behind a commit check**, such as the DirectStorage destination check.

Without the switch, nothing changes: no handler is installed, no import is wrapped, and `x2w_xmem_commit_range` returns TRUE.

Measured in startup runs on Windows 10 22H2 (16 GB RAM, RTX 3080 Ti), same build and input, two runs each, with `-MemoryGiB 16`: system commit stayed about 5 GiB lower from 20 s to 170 s (23.3–24.6 GiB against 29.0–29.5 GiB), and 1.6 to 3.1 GiB lower in the late load, when most mapped pages have been touched. The process's own commit charge was the same with and without the switch. Where a run ends was not changed: on that host the late load also reaches the process commit cap.

| CTest case | Inputs and checks |
|---|---|
| `xmem_lazy_commit` | With the switch: mapping 1,024 pages (64 MiB) grows system commit by less than 24 MiB, and the pages stay `MEM_RESERVE`. A CPU write commits only its page, reads back, and the rest of the page reads zero. `ReadFile` into an untouched page fails with `ERROR_NOACCESS`; after `x2w_xmem_commit_range` the same read succeeds. Eight threads writing to the same 32 untouched pages at once all succeed. Released pages are zero-filled when their slots are reused. |
| `xmem_lazy_commit_off` | Control, with `X2W_XMEM_LAZY_COMMIT=0`: the same 1,024-page mapping commits every page when it is mapped (system commit grows by about 64 MiB), and `x2w_xmem_commit_range` returns TRUE. |

The other `xmem_` cases run without the switch and check that the default path is unchanged.

```powershell
cmake --build --preset release --target xmem_shim
ctest --preset release --no-tests=error -R '^xmem_' -V
```

### Startup blocks committed on first touch

Experimental, for [#1199](https://github.com/R0Y00/Xbox2Windows/issues/1199), on top of [Mapped pages committed on first touch](#mapped-pages-committed-on-first-touch). The [startup commit profile](#reserve-and-commit-startup-blocks) commits each block in full when it is allocated. Several of those blocks are large, and the title touches them gradually. Their commit is charged to the process, so in a startup run with `-MemoryGiB 16` the late load reaches the job's commit cap.

`X2W_XMEM_LAZY_STARTUP=1` takes effect only together with `X2W_XMEM_LAZY_COMMIT=1`. Blocks of 32 MiB and up from the startup commit profile are then reserved and recorded, with the same alignment and size as before. The same vectored exception handler commits the 256 KiB chunk a first access touches. A thread that finds its chunk already committed by another thread retries once. `x2w_xmem_commit_range` also commits the chunks behind a range in such a block, so the `ReadFile` wrapper and the stand-in writers above keep working.

- **GPU placement.** A GPU heap opened on a block covers the committed run from its base and is reused for later resources in that block. So device slot 133 calls the experimental export `x2w_xmem_commit_block(address)` before a placed buffer is measured. It commits the whole block once and removes it from the list.
- **Release.** The title releases these blocks through its `VirtualFree` import. The wrapper releases the block under the list's lock and removes it from the list only if the release succeeds, so a later allocation at the same addresses is never committed as part of the old block, and a refused release (a nonzero size, for instance) leaves the block reserved and still committed on first touch. The handler and `x2w_xmem_commit_range` hold the same lock from the lookup to the commit, so a release cannot run in between.
- **Below 2^40.** Blocks placed by [`X2W_XMEM_STARTUP_BELOW_40`](#startup-blocks-below-240) stay committed up front.
- **Smaller blocks** are committed as before. If the list cannot record a block, the block is committed in full instead.

Measured in startup runs on Windows 10 22H2 (16 GB RAM, RTX 3080 Ti), same input, with `-MemoryGiB 16` and both switches:

- **Process commit:** 10.8 GiB through startup (11.8 GiB with only `X2W_XMEM_LAZY_COMMIT`), and 14.7–15.1 GiB at 180 s (15.6 GiB).
- **Without the 40-bit placement:** three runs out of three reached the BVH builder's address check described in [#1758](https://github.com/R0Y00/Xbox2Windows/issues/1758) without a commit failure. Earlier runs with neither switch or only the first one did so in one run out of six.
- **With `-StartupBelow40`:** system commit had 3.75 GiB free at its peak, and the run reached the builder's upload allocation (command-list slot 148, #1758). In two runs without either switch, system commit fell to 0.74 and 1.03 GiB free before that point, and a host-side commit watchdog stopped them.

| CTest case | Inputs and checks |
|---|---|
| `xmem_lazy_startup` | With both switches: a 64 MiB block of type `0x20403000` and a 32 MiB large-page block are reserved, the second 2 MiB aligned; a 16 MiB block is committed. A CPU write commits only its 256 KiB chunk, reads back, and the rest of the chunk reads zero. `ReadFile` into an untouched chunk fails with `ERROR_NOACCESS`; after `x2w_xmem_commit_range` over a range that crosses into the next chunk, the read succeeds. Eight threads writing to the same 32 untouched chunks at once all succeed. `x2w_xmem_commit_block` commits a whole block, a write to its last bytes succeeds, and an address outside the lazy blocks returns TRUE. |
| `xmem_lazy_startup_off` | Control, with `X2W_XMEM_LAZY_STARTUP=0` and `X2W_XMEM_LAZY_COMMIT=1`: the same 64 MiB and 32 MiB blocks are committed when they are allocated, and `x2w_xmem_commit_block` returns TRUE. |
| `xmem_lazy_imports` | Source-built ([`tests/xmem_lazy_imports_check.cpp`](../tests/xmem_lazy_imports_check.cpp)), because the import wrappers patch the main module, which is `python.exe` in the checks above. With both switches, the program's own `ReadFile` reads a file into a mapped page that was never touched, and the bytes match. A 64 MiB startup block is released through its own `VirtualFree` and the same addresses are reserved again; a read there must still fault, and the range must stay reserved. Without the commit in the `ReadFile` wrapper the read fails with `ERROR_NOACCESS`; without the list removal in the `VirtualFree` wrapper the handler commits the new reservation. A second block is released with a nonzero size, which fails with `ERROR_INVALID_PARAMETER`; a write to an untouched page must then still be committed, and the block is released with size 0. Removing the block before the release is known to succeed fails that write. |
| `xmem_lazy_handler_failure` | The same program on the test-controls build, with `X2W_XMEM_TEST_LAZY_HANDLER_FAILURE=1` refusing the handler: a mapped page and a 64 MiB startup block must be committed and writable. Keeping the switch on without the handler leaves both reserved. |

```powershell
cmake --build --preset release --target xmem_shim xmem_shim_test_controls xmem_lazy_imports_check
ctest --preset release --no-tests=error -R '^xmem_lazy' -V
```

### Second commit and protection comparison

`xmem_commit_protection` ([`tests/xmem_commit_protection_check.cpp`](../tests/xmem_commit_protection_check.cpp)) is a source-built x64 check for [#870](https://github.com/R0Y00/Xbox2Windows/issues/870) using synthetic 64 KiB page patterns. Its scope is separate from the first-commit alignment/zero-fill/rounding check (`xmem_committed_memory`), the reserve-only profile in [#888](https://github.com/R0Y00/Xbox2Windows/pull/888), and the device/queue work in [#874](https://github.com/R0Y00/Xbox2Windows/issues/874).
Expand Down
6 changes: 5 additions & 1 deletion docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,8 @@ ctest --preset dev --no-tests=error -R '^startup_trap_header(_prefix_control|_pr
|---|---|
| `X2W_XMEM_PERMISSIVE=1` | accepts the reservation, mapping and allocation shapes a startup run uses |
| `X2W_XMEM_STARTUP_BELOW_40=1` (`run.ps1 -StartupBelow40`) | startup compatibility configuration ([#1758](https://github.com/R0Y00/Xbox2Windows/issues/1758)): the large-page startup blocks with attribute `0x283` (null address, type `0x20003000`, protection `0x2004` or `0x2404`) are placed so that the whole block, last byte included, lies below 2^40. A block that does not fit there returns NULL with the Windows error and logs `x2w-xmem startup block below 2^40 refused:`; nothing is truncated or placed higher. Other shapes and explicit addresses keep their placement. See [Startup blocks below 2^40](experiments.md#startup-blocks-below-240) |
| `X2W_XMEM_LAZY_COMMIT=1` | experimental ([#1199](https://github.com/R0Y00/Xbox2Windows/issues/1199)): a fresh physical-page slot is committed on its first CPU access instead of when its page is mapped. A vectored exception handler commits the slot. Writers that cannot fault one in (the main module's `ReadFile`, slot 133 placement, stand-in writes behind a commit check) call `x2w_xmem_commit_range` first. See [Mapped pages committed on first touch](experiments.md#mapped-pages-committed-on-first-touch) |
| `X2W_XMEM_LAZY_STARTUP=1` | experimental, with `X2W_XMEM_LAZY_COMMIT=1` ([#1199](https://github.com/R0Y00/Xbox2Windows/issues/1199)): the [startup commit profile](experiments.md#reserve-and-commit-startup-blocks) only reserves blocks of 32 MiB and up, and the same handler commits them 256 KiB at a time on first touch. Slot 133 commits a whole block (`x2w_xmem_commit_block`) before a GPU resource is placed in it. The main module's `VirtualFree` import drops released blocks. Blocks placed by `X2W_XMEM_STARTUP_BELOW_40` stay committed up front. See [Startup blocks committed on first touch](experiments.md#startup-blocks-committed-on-first-touch) |
| `X2W_XMEM_GPU_MIRROR=1` | pairs a no-access graphics reservation with the read-write reservation of the same size made just before it, and maps every physical page into both (same section) |
| `X2W_D3D12_PERMISSIVE=1` | wraps the device: recording command lists, console present, view tracking; on a runtime without shader model 6.6 (in-box Windows 10), root signatures with the heap-indexing flags (`0xC00`) are serialized again without them, and the copy kept for shader translation gets that call's bits back when `CreateRootSignature` receives the returned blob's own buffer (a copy of the bytes keeps the desktop flags; `d3d12_xs_shim_heap_indexing`). The retry runs only on runtimes that refuse the flags (before shader model 6.6); where the runtime accepts them, `d3d12_xs_shim_heap_indexing` reports a capability skip (77) |
| `X2W_D3D12_RENDER=1` | draws through translated pipelines from `X2W_SHADER_CACHE` and forwards [single-record indirect Dispatch](experiments.md#gpu-written-indirect-dispatch-arguments), [DrawIndexed](experiments.md#gpu-written-indirect-indexed-draw-arguments), [Draw](experiments.md#gpu-written-indirect-draw-arguments), and [DispatchMesh](experiments.md#gpu-written-indirect-mesh-dispatch-arguments) for supported desktop pipelines and tracked placed buffers |
Expand Down Expand Up @@ -2017,7 +2019,9 @@ committed, is zeroed on reuse, and is chosen before a fresh slot. The experiment
export reports open slabs and used and free slots. `xmem_slab_slots` checks the bookkeeping without Windows
calls. `xmem_mapped_pages_commit` measures commit for a small map, and `xmem_mapped_pages_commit_failure`
injects a commitment failure and checks that the reservation and slab counts are unchanged and the pages
can be mapped on retry. Caller-owned placeholders must already match the requested view size. The
can be mapped on retry. With the experimental `X2W_XMEM_LAZY_COMMIT=1`, a fresh slot waits for its first
CPU access instead of its mapping (see [Mapped pages committed on first touch](experiments.md#mapped-pages-committed-on-first-touch)).
Caller-owned placeholders must already match the requested view size. The
heartbeat also prints while the request counters are unchanged
when either memory value changed, so the cost of a stalled load is visible. Each report is followed
by a line of failed requests by cause and a line of page-destination requests by end (see
Expand Down
6 changes: 5 additions & 1 deletion src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3350,7 +3350,11 @@ HRESULT __stdcall device_place_resource_x(Wrapper* self, void* address, const D3
}
// The allocation holding the address, and whether every byte from its base to the end of the
// buffer is committed in it: one region-information call for a fully committed private
// allocation, otherwise a region walk that stops at the end of the buffer.
// allocation, otherwise a region walk that stops at the end of the buffer. The GPU cannot fault in a lazily
// mapped physical page (X2W_XMEM_LAZY_COMMIT), so the buffer's pages are committed first; a lazy startup
// block (X2W_XMEM_LAZY_STARTUP) is committed whole, as the heap opened on it is reused for the block.
x2w_local::commit_lazy(address, static_cast<std::size_t>(desc->Width));
x2w_local::commit_lazy_block(address);
x2w_startup::PlacedBufferRange range;
const bool committed = x2w_startup::measure_placed_buffer(address, desc->Width, range);
InterlockedIncrement64(&placed_buffer_calls);
Expand Down
Loading
Loading