Skip to content

D3D12.X: XCL_WATCH_TRAP=1 leaves memory nobody writes out of the write-watch looks (off by default) - #2934

Draft
Kalmarv wants to merge 2 commits into
xcl-dev:mainfrom
Kalmarv:watch-trap
Draft

Kalmarv wants to merge 2 commits into
xcl-dev:mainfrom
Kalmarv:watch-trap

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Every submit looks at all watched memory with GetWriteWatch, and the call is paid per byte looked at (about 0.5 ms per GiB here). In a streamed world 17.6 GB are watched, and most of it is written once when it streams in and not again. With XCL_WATCH_TRAP=1 a 2 MiB block that 64 looks in a row found clean is made read-only and left out of the looks; its first write faults into a vectored handler, which gives the block its protection back and returns it to the looks. Off by default. Part of #2688.

Two open PRs work on the same cost in other ways: #1034 makes 64 KiB tiles of the sparse windows read-only (XCL_SPARSE_COOL=1), and #2891 looks at cold 16 MiB chunks once a frame instead of at every submit. This one covers every watched range (sparse windows and title textures alike) and stops looking at a cold block altogether until it is written. It was not run together with either.

What is in it

  • hle/d3d12x/watch_trap.rs (new, first commit): the cold blocks, the handler, the look. A block that cannot be made read-only (pinned, nothing committed, not all read-write) is tried again after another 64 looks; a block with nothing committed leaves the looks as a hole until a commit in it. At most 32 blocks are made read-only per look, and each gets a last look after the protection, for a write in between.
  • A kernel write into a read-only page fails with ERROR_NOACCESS instead of faulting, so the handler cannot catch it. The title's ReadFile buffers get their protection back and stay pinned until the read is over (vfs_hooks.rs: read_start / read_end; a pending read when a look sees its OVERLAPPED completed), and their pages are then reported as written.
  • Hooks, one line each (a guard that gives the blocks it meets their protection back and keeps them writable while it lives): kernelx.rs (VirtualAlloc / VirtualFree / VirtualProtect(Ex), the physical-page homes), kernelx/lazy.rs, sparse.rs (commits and decommits), and XCL's own checks whether memory is writable: eh.rs, cpu_writes.rs, dstorage.rs, dstorage/parallel.rs.
  • title_textures.rs: refresh_once calls watch_trap::look instead of its loop when the trap is on, and the sorted page list gets a dedup (a finished read and write-watch can both report a page). Nothing else changes with the variable unset.
  • Second commit, watch_trap.rs only: the look walks the read-only spans once instead of copying them into a new map and making two map queries per span (about 9,000 spans a look late in the world).
  • Size: 465 lines of code and 242 of tests in the new file, about 40 lines of hooks in ten files. It is over the ~400 guideline and not split because it is one switch: the handler, the pins and every hook are needed before the first block may be made read-only.

Tested

  • RTX 5070 Ti (16 GB), driver 610.74, Ryzen 9 9950X, 31 GB RAM, Windows 11 Pro 26200, Rust 1.99.0.
  • On this tree, release (run on fcc0636, which has the same tree as this head; only the commit trailers changed since): cargo test --release -p xcl --lib -- watch_trap written_pages_past --test-threads=4: 7 passed, 0 failed. The six watch_trap tests 40 times in a row: 0 failures.
  • Whole suite on this head dac9681, debug, DXC 1.9.2609 through XCL_DXC_DIR: cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 1452 passed, 0 failed, 47 ignored. 25 SKIPPED: 21 no wave64, 3 refused f16 forms, 1 no heap windows.
  • Title ea3b5e4d, on our integration tree of that time, which has the driver-heap windows of D3D12.X: UMA windows in a driver heap on NVIDIA, 4.9x FPS (0.62 -> 3.03) #2844 (XCL_UMA_DRIVER=custom), not on main: one timed run per arm with the GPU alone, the trap the only difference. 5.79 against 4.01 presents/s over 107-223 s of the run (+44 %), 8.45 against 4.60 over 187-229 s; the same draws and dispatches per present (788 / 5,024 against 804 / 5,152), clean exit, no device removal. Late looks go over 0.5-1.8 GB of the 17.6 GB watched (5.4 GB read-only, about 4,400 holes); 4 blocks were refused in the run.
  • The first form, which tried a refused block again at every look, was 32 % slower than without the trap (2.72 against 4.01); that is why refused blocks wait and empty blocks become holes.
  • A threshold of 8 looks instead of 64 (tried on our tree, not in this PR, one untimed run per arm) doubled the look: about 2.1 ms against 1.1, of it 1.3 ms protecting, about 11 faults a look.
  • Second commit: a look with a take that does nothing, 18 GiB watched in 64 ranges, 8,861 spans, 355 blocks left to look at, no GPU: 336 us before, 40 us after. In the title (same tree, one untimed run per arm): the write-watch part of a look 1.06 ms against 1.29 ms.

Regression run

Running: a 480 s pair on title ea3b5e4d (main 8b957be and this head). Both xcl-smoke blocks follow here before this leaves draft.

Not tested

AI use

  • Claude Code with Claude Opus 5.5 wrote the trap and its tests; Claude Code with Claude Fable 5 wrote the second commit, ran the tests and wrote this description.

🤖 Generated with Claude Code

Kalmarv and others added 2 commits November 26, 2026 19:37
Every submit looks at all watched memory with GetWriteWatch, and the call
is paid per byte looked at (about 0.5 ms per GiB). In a streamed world
17.6 GB are watched, and most of it is written once when it streams in
and not again.

With XCL_WATCH_TRAP=1 a 2 MiB block of a watched range that 64 looks in a
row found clean is made read-only and left out of the looks. Its first
write faults into a vectored handler, which gives the block its
protection back and returns it to the looks; the write happens after
that, so write-watch reports it as before. A block that cannot be made
read-only (pinned, nothing committed, not all read-write) is tried again
after another 64 looks, not at every look, and a block with nothing
committed leaves the looks as a hole until a commit in it.

A kernel write into a read-only page fails instead of faulting, so the
title's ReadFile buffers get their protection back and stay pinned until
the read is over, and their pages are then reported as written. Commits,
decommits and protection changes through kernelx, lazy pages and sparse
commits give the blocks they meet their protection back first, and so do
XCL's own checks whether memory is writable.

Off by default; without the variable only a sorted list gets a dedup.

Measured on title ea3b5e4d on a tree with the driver-heap windows of
xcl-dev#2844 (RTX 5070 Ti, one timed run per arm, the trap the only change):
5.79 against 4.01 presents/s over 107-223 s of the run, 8.45 against
4.60 over 187-229 s, the same draws per present, no device removal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A look copied the read-only spans into a new map and then made two map
queries per span to find the pieces still to look at. In the late world
of title ea3b5e4d that is about 9,000 spans a look and 20 looks a
present. hot_pieces now walks the spans once, under the lock, while the
ranges ascend (a look's are sorted and disjoint), and starts the walk
again for a range that does not.

Measured without a GPU, a look with a take that does nothing, 18 GiB
watched in 64 ranges, 8,861 spans, 355 blocks left to look at: 336 us
before, 40 us after (RTX 5070 Ti machine, release build). In the title,
on a tree with the driver-heap windows of xcl-dev#2844 (one untimed run per
arm): the write-watch part of a look 1.06 ms against 1.29 ms.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ettehn

ettehn commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Independent Windows check on dac9681: I compiled the PR’s watch_trap.rs directly in a standalone Rust harness (only the log macro is stubbed) and added a local test with a real overlapped ReadFile on a local named pipe. XCL_WATCH_TRAP=1, 7 module tests passed. The added test also passed 20/20 runs in fresh processes.

The test starts with a read-only watched block, calls read_start and submits a read with no pipe data yet. Windows returns ERROR_IO_PENDING. Across 65 looks the destination remains pinned and does not get frozen. The writer then sends 32 bytes. After GetOverlappedResultEx confirms completion, the next look reports the destination page exactly once and the block can be frozen again. The usual GetWriteWatch callback is a no-op in this fixture, so the dirty page comes from the pending-read completion path.

The fixture calls the watch-trap hooks directly around Win32 I/O. It does not exercise VFS import routing, GPU upload, or a retail title. The full lib build ran out of disk space before linking. OpenAI Codex (6.2sol) assisted with verification.

@ettehn

ettehn commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Follow-up on dac9681b57cb64f749d7977cf4ee477d83ca3aa7: I used a real local named pipe and two destination buffers in separate watched blocks. I waited for the first ReadFile to complete with GetOverlappedResultEx, then submitted the second using the same OVERLAPPED before the next look. While the second read was pending, the look kept both records pending and the first block could not go read-only. It became eligible again after the second read completed. Native GetWriteWatch independently reported the first page in this buffered-pipe case, so I did not observe a missed dirty page.

In a local candidate, read_start retires the previous record for that OVERLAPPED before the next ReadFile replaces its status. The first block can then go read-only while the second read stays pinned. The standalone comparison reproduced the expected distinction in 20/20 fresh processes per variant, and all seven harness tests passed with the candidate. This uses the watch-trap hooks directly and does not measure title performance or demonstrate a failure on unbuffered reads. OVERLAPPED lifetime

OpenAI Codex (6.2sol) assisted with verification.

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.

2 participants