perf(libc,agc): skip never-written guest pages in Windows heap mirror zero scans #15967
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15967"
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 boykopovar/AnyPS5. Originally posted by sktharunbalaji13-cmd on 2026-11-05T04:01:05Z.
What
On Windows, the heap mirror zero scans (
GuestNonzeroBlocks,GuestBytesZeroinResidentMirror.cpp) read every page of their range, because Windows has no/proc/self/pagemapto skip unpopulated pages the waypossiblyNonzerodoes on Linux. Reading a committed page that was never written makes Windows allocate a physical zero page for it, so a scan over a large, mostly empty heap fills the RAM by itself.WindowsMappings.hpp) keeps a bitmap of the 4 KiB pages it committed fresh under a write watch (Commitwith write watch,commitResident) that nothing has written since. Bits are cleared for every page aGetWriteWatchcall reports (all calls go through one wrapper), atomically with the reset that replacedResetWriteWatchwhere bits remain, and onreset,demote,evict, restored saved content, host writes and host imports (GPU writes through an import are not seen by the write watch).GuestArenaKnownZero_nid_no_patchreports the pages of a range that are still known zero, after a non-resettingGetWriteWatchof the range and taking the arena lock one 2 MiB chunk at a time.possiblyNonzeroandGuestBytesZeroon Windows skip those pages.MEM_WRITE_WATCH, read pages 0 and 5,GetWriteWatchreports 0 and 5). The old scans read everything early, so that report was consumed before anyone cared. With the scans skipping those pages, the first read could happen after a storage image took its layer generation and then pass for a CPU store, so the flush hook's CPU-kept fast path dropped a GPU result (agc_driver_resident_heap_mirror, StorageImageTests). A reported page that is still known zero and still reads as zero (checked withReadProcessMemory, only when the report is complete) is therefore no longer reported: its bytes have not changed since the commit.Generic behaviour: nothing depends on a title, address or size.
Tested
Windows 11 26200, RTX 4060 Laptop GPU (8 GB), NVIDIA driver 617.42, 16 GB RAM, MinGW GCC (winlibs) Release, on
main0540ef0ef2and on this branch, same machine:core/libs/tests/WindowsWriteWatch.cpp(CheckKnownZeroPrivate,CheckKnownZeroResident,CheckKnownZeroShared,CheckKnownZeroFirstReads): fresh commits are known zero and clipped to the query, CPU stores, resetting/fresh/armed collects, host write windows, host imports, resets, an evict plus remap with restored bytes, memory without a write watch and shared memory; a first read or a zero store of a fresh page is not collected as a write while a real store is.StorageImageZeroStoreTestsinResidentHeapMirror.cpp(review follow-up): an image store into a never-written block, a CPU store of 0, then a read must return 0. It fails without the GPU-write forgetting (the GPU value0x12345678wins) and passes with it. Every recorded GPU write (pending writes, fills, label stores, storage image layers,MarkWritten/MarkPossiblyWritten) forgets the range, and a storage image upload reads its still known-zero pages before taking its write generation.ctest -R "windows_write_watch|agc_driver_resident_heap_mirror|heap_mirror|agc_write_tracking$"withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1: 12/12 pass (agc_driver_resident_heap_mirrorfailed with the first version of this change and passes now).host_import_*,guest_memory*,guest_owned_memory*,committed_read_mapping,copied_writer_*,linear_target_*,private_collect,write_watch_coverage,storage_write_back_*,read_only_lease_writer,partially_resident_snapshot,copy_guest_bytes): pass, exceptagc_driver_direct_write_completion, which fails the same way onmainon this machine (3 of 3 runs, "the completion of a buffer write dropped image results recorded after it").VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationruns of the focused tests: the 12 focused tests above onmainand on this branch: noVUID-errors in either log, and neither log containswas not found but was requested by env var VK_INSTANCE_LAYERS.tools/trace_digest.pysummary lines,APS5_TRACE_SECONDS=0:40, same machine and scene, back-to-back runs (APS5_SKIP_REFUSED_DRAWS=1 APS5_PROFILE_DRAW=1):Traces: main, this branch (summaries: main, this branch). Both traces lost events once their buffers filled (about 35-38 s).
aps5-summary.jsonllines of the two captures:main0540ef0ef2:this branch d28560cdca1a:
tools/trace_digest.pyoutput,mainseconds 0:40 from present 0: 206 presents, frame p50 16.9 / p90 87.8 / p99 3326.6 / max 18738.7 ms, GPU busy 19.4 ms per present
NVIDIA GeForce RTX 4060 Laptop GPU, NVIDIA 617.42, Windows 10.0.26200, 3840x2160, commit
0540ef0ef2, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.WorkerWait 414.1, Driver.WorkerWait.idle_and_completion_poll 413.9, Driver.Packet 386.4, Driver.Packet.memory_wait 222.0, Driver.Packet.dispatch 153.1, Driver.Dispatch 143.3, Vulkan.Dispatch 102.6, Vulkan.Dispatch.shader_resources 101.7, batch 19.3, Driver.Dispatch.prepared_invocation 12.7
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
tools/trace_digest.pyoutput, this branchseconds 0:40 from present 0: 310 presents, frame p50 17.8 / p90 414.3 / p99 1017.9 / max 5108.1 ms, GPU busy 24.1 ms per present
NVIDIA GeForce RTX 4060 Laptop GPU, NVIDIA 617.42, Windows 10.0.26200, 3840x2160, commit d28560cdca1a, dropped: yes
Incomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.WorkerWait 320.5, Driver.WorkerWait.idle_and_completion_poll 320.2, Driver.Packet 204.7, Driver.Packet.memory_wait 103.8, Driver.Packet.dispatch 89.3, Driver.Dispatch 80.6, Vulkan.Dispatch 53.5, Vulkan.Dispatch.shader_resources 52.5, batch 24.2, Recorder.Sync 10.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
From the same two runs (
APS5_PROFILE_DRAWand the process counters):In the
mainrun the game's working set rose from 3.2 to 8.2 GB between 10 and 14 s, during that first build, while its commit stayed at 11.8-12.1 GB: the scan allocating zero pages. A separate check on the same machine: reading 1 GiB of committed, never-written memory adds 1024 MiB to the working set.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)tools/pr_overlap.py: #15900 also editsWindowsWriteWatch.cpp(a different test); 14 conflicting PRs shareResidentMirror.cpp,GuestArena.cpporWindowsMappings.hppand could not be compared automatically; none of them adds a known-zero query (checked by hand: #13809, #15295, #15296, #15900).🤖 Generated with Claude Code, OpenCode, Command Code and Gemini
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T15:48:39Z.
Review: COMMENTED
Ran
e59f347f94merged onmain302864d48con Windows 11 x64 (WinLibs GCC 15.2), on an NVIDIA laptop GPU (Turing, driver 621) and an Intel iGPU (driver 101.70xx), withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(Khronos validation layer 1.4.368),ctest -R '^(agc_driver_resident_heap_mirror|windows_write_watch)' -V:mainWindowsWriteWatch.cppusesWindowsMappings::KnownZero, which this PR adds, so there's no run of the tests without the change.windows_write_watch,windows_write_watch_verify_directopen none). No log haswas not found but was requested by env var VK_INSTANCE_LAYERS, and no log on either side has aVUID-. To be sure validation messages reach these logs, the same layer with best practices on printed 28BestPractices-warnings into anagc_driver_recorderlog on each GPU.AI-assisted: yes.