D3D12.X: coalesce contiguous WriteProcessMemory runs in write_changed_pages #3237
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3237"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Lakanya on 2026-11-28T23:40:11Z.
title_textures::publish_download()holds the globalTEXTURESmutex whilewrite_changed_pages()writes reverse-tiled GPU texture readbacks back into title memory. Previously,write_changed_pages()calledVirtualQuery+WriteProcessMemory(GetCurrentProcess(), ...)once per 4 KiB page. On Windows, eachWriteProcessMemorytriggersNtFlushInstructionCacheacross CPU cores; on multi-megabyte GPU-written textures (e.g. an 8.92 MB texture = 2,178 pages), this heldTEXTURESfor 24+ seconds and stalledCreatePlacedResourceX,ExecuteCommandLists,CopyTextureRegion, andmemory_report::snapshot.This PR coalesces contiguous changed 4 KiB pages into runs and issues one
VirtualQueryper VM region and oneWriteProcessMemoryper contiguous changed run, while continuing to skip unchanged pages (preservingMEM_WRITE_WATCHcleanliness) and failing closed on read-only,PAGE_GUARD, or unmapped pages.What is in it
crates/xcl/src/hle/d3d12x/title_textures.rs: addchanged_page_runs(), updatewrite_changed_pages()to coalesce contiguous changed 4 KiB pages per VM region into a singleWriteProcessMemorycall, and add unit testschanged_page_runs_coalesces_contiguous_and_skips_unchangedandwrite_changed_pages_coalesces_writes_and_respects_vm_protections.Tested
617.71), Windows 11 Pro 26300,rustc 1.100.0 (0ef1bc386 2026-11-09), debug & release.cargo test -p xcl --lib -- write_changed_pages changed_page_runs --nocapture): 2 passed, 0 failed, 0 ignored.cargo test --workspace --release --no-fail-fast -j 6 -- --test-threads=4: 135 passed, 4 failed, 20 ignored across integration/crate test binaries (27SKIPPEDGPU tests: wave64 / unsupported f16-f32 per-lane variants / heap windows);xcl --libexited early (0x80004005) on an unrelated in-processstop_titletest (tracked in #3187) and 4gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_*failed; aftercargo build --workspace --release -j 6,cargo test -p xcl --release --test e2e -- --test-threads=4: 4 passed, 1 failed (e2e).XCL_ACTIVITY=1,XCL_LOOK_STATS=2): 8.92 MB GPU-written texture publish dropped from24,023 ms(TEXTURES.lock()hold,03D presents) to14–30 ms(1WriteProcessMemorycall,0TEXTURES.lock()holds>= 200 ms,103D presents in 12 s).Regression run
Both runs used
XCL_PREPARE=0,XCL_DEVICE_TYPE=lockhart,XCL_TEXTURE_LEVEL_PAGES=1,XCL_SMALL_TEXTURES=1.Not tested
write_changed_pagesspeedup was measured separately on a 210 s in-world run on the same title.AI use
TEXTURES.lock()stall, implementedchanged_page_runs()and coalescedwrite_changed_pages(), wrote and ran the unit tests and paired smoke runs, and drafted this description.Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T01:14:35Z.
merge-check
base:
fa204595a4head:
b11fe096e2merge: clean (trial merge 984566ad2ed772872ac77a01ad323921064809a3, local, not pushed;
git diff --checkclean)build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 (XCL_DXC_DIR = DXC 1.9.2609) -> 2610 passed, 0 failed, 49 ignored; SKIPPED: 26 (21 no wave64 on this 32-lane GPU, 3 per-lane f16/f32 forms refused (#1426 semantics), 2 no heap windows); failing: none. Both new tests ran and passed:
changed_page_runs_coalesces_contiguous_and_skips_unchangedandwrite_changed_pages_coalesces_writes_and_respects_vm_protections(write-watch shows the unchanged page isn't dirtied; read-only and guard pages fail closed and the guard stays armed).review: none blocking; whole diff read (
title_textures.rsonly).changed_page_runsuses the same 4 KiB page slicing as before, unaligned start included, and merges adjacent changed slices.write_changed_pageskeeps the same acceptance test (committed, writable, not guard), now applied once perVirtualQueryregion instead of once per page. All pages of one region share state and protection, so at query time this is equivalent, and writes are split at region ends. On failure it still returns false after the earlier runs are written, as before. One note, not a blocker:region_endis reused across runs too, so a page whose protection the title changes after the first query (made read-only or guarded) isn't seen until the next region.WriteProcessMemorycan write through read-only protection, which is why the per-page check existed. The window is the duration of onewrite_changed_pagescall (it was one page before, so it was never race-free), and a concurrent unmap still makesWriteProcessMemoryfail.performance: fewer
WriteProcessMemorycalls whileTEXTURESis held when a GPU-written texture download publishes many contiguous changed pages; no other change. Not measured: nothing counts texture downloads, so I can't show this title reached the path. Title run (release build of the merge, scratch/fast-world.py fast profile, 30 s in world; same cache snapshot and harness as a mainfa204595run): main exit 0, 0 crashes, 0 removals, world entry 90.6 s, peak commit 28,620 MB, 70 WARN lines. This merge: exit 0, 0 crashes, 0 removals, world entry 102.1 s, peak commit 29,091 MB (+1.6 %), 79 WARN lines;failed_pipelines0, no translation errors. The only new WARN kind is the system-wide "running out of commit" (this process held 28,938 MB; another suite and build were running on the machine at the time). Both runs show 0 fps in the world because the cache doesn't match the translator identity; timings on this shared machine are noise.verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 5070 Ti 16 GB (driver 610.74, wave32 only), AMD Ryzen 9 9950X, Windows 11 build 26200; rustc 1.99.0, debug test profile, release build for the title run, DXC 1.9.2609 via XCL_DXC_DIR. Claude Code, Claude Opus 5.5: merge, build, suite, title run and full-diff review; this report.
Imported from ivall/xcl. Originally posted by andrewDoing on 2026-11-29T01:33:52Z.
Measured the syscall reduction on Windows (Ryzen 7 5700X3D) at
b11fe096. I compiled the parent and PR versions of the production loops with counting wrappers forwarding to the real VirtualQuery and WriteProcessMemory on an owned 16 MiB allocation. Each changed page differed by one byte. All ten runs read back the entire expected allocation.This quantifies where the optimization helps. Even when alternating changes prevent write coalescing, the region cache eliminates 2,047 queries. Payload remains page-granular in every case. The read-only gap was left untouched. These counts measure the publishing helper only. No title run or wall-time inference.
Tooling: OpenAI Codex generated and ran the synthetic harness and drafted this report.
Imported from ivall/xcl. Originally posted by CharlyRien on 2026-11-29T01:49:39Z.
A test of this PR in the world of the retail title. On our setup it removes a freeze we had traced to this very loop before the PR was opened.
Setup
mainfa204595, plus #3214 (cherry-picked), plus four local commits of ours that are not related to this PR: our thread sampler and its reader (#3232, off unlessXCL_THREAD_SAMPLEis set), and two lab switches (XCL_TEX_KEY_PAD,XCL_TRIM_VIEWS) that are off in both runs.b11fe096(cherry-picked,9c725b13) and one sampler commit (the sampler writes each sample to its file at once).XCL_HUGE_PSO_CONCURRENCY=8,XCL_HUGE_PSO_WANTED_CONCURRENCY=8,XCL_PSO_WAIT_MS=5000,XCL_ACTIVITY=1,XCL_NETWORK=0. C3f also hadXCL_THREAD_SAMPLEon from 220 to 370 s, to see the freeze if it came back.Measured
main7db2f391+ #3214 and the same local commits, without this PR, the same title froze in all four runs (C1, C1c, C1e, C1f).Read (a thread dump taken during C1f's freeze, at 294 s, symbolized with xcl.exe's PDB)
xcl-fences: intitle_textures::write_changed_pages<complete_fence_downloads<poll_fence_downloads(the code calls it withTEXTURESheld, as the description says).Mutex::lock_contended<title_textures::register<device_x::create_placed_resource_x.Mutex::lock_contended<title_textures::refresh_parts<refresh_for<prepare_title_submission<q_execute_command_lists.Deduced
VirtualQuery+WriteProcessMemoryloop heldTEXTURES, placement and submission waited on it.Not tested / caveats
scripts/smoke.ps1pair: these are 550–670 s title runs through our own harness.AI use
Imported from ivall/xcl. Originally posted by throny on 2026-11-29T08:41:46Z.
merge-check claim
head:
b11fe096e2Imported from ivall/xcl. Originally posted by throny on 2026-11-29T09:01:03Z.
merge-check
base:
d8bd9a38head:
b11fe096e2merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2620 passed, 0 failed, 49 ignored (27 SKIPPED lines). Base alone at
d8bd9a38: 2618 passed, 0 failed, 49 ignored.review: no blocking finding. Neighbouring changed pages are merged into runs: one region query per memory region and one write per run inside a region, instead of one of each per page. It still writes only changed pages, still fails closed on read-only, guard and uncommitted pages, and splits a write at region boundaries. Note: the region information is now kept across runs, so a protection change after the query is noticed later than before. The run-building function has its own test; the write test also passes on the old code (a regression guard, fine for a performance change).
performance: not measured here.
verdict: safe to merge
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).
Imported from ivall/xcl. Originally posted by crosettiremi on 2026-11-29T19:02:55Z.
An AMD data point for the stall this fixes, from the retail title on a Radeon RX 7900 XT 20 GB (driver 32.0.32029.2010), base
fa204595andd8bd9a38. I have not run this PR's head; our tree got its own change to the same loop the night this was open.Before: after the first world submit, the copy-back of GPU-written textures took 7 to 15 ms per 4 KiB page here (two virtual-memory queries per page, under the texture lock), about 100 s for 28 to 56 MiB. The title's next queue
Signalwaited behind it, and in one run the title's own hang watchdog ended the process. After (one write per contiguous run, publication outside the lock): 49 MB in 7.9 ms in the same phase.One thing our runs add: with the write batched, the remaining cost in this path was the lock, not the writes. If the per-page queries still run under the global texture lock on
mainafter this PR, a second thread'sSignalcan still wait for the whole batch. I have not checkedmainafter the merge for that.AI use: Claude Code (Claude Fable 5.2) read the logs and wrote this.