perf(libc): carve fresh private guest pages out of their placeholder #10070
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10070"
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 thuanquang on 2026-10-23T08:09:46Z.
What
On Windows,
DirectMemory.cppcommits guest memory throughCommitArenaRangewith a 16 KiB granule: every anonymous map (mmap,mmap_alignedand theMAP_FIXEDpath), a fixed remap of a registered range to anonymous memory, and anmprotectof an arena range (which commits the pages that are not committed yet and only changes the protection of the rest). That callsWindowsMappings::Commit. For each fresh 16 KiB page,Commitranreset()before replacing the placeholder: 10VirtualQuerycalls, one placeholder split and theVirtualAlloc2, so 12 system calls per page. When the map lands at the arena's first-fit frontier, the placeholder those queries look at is the arena's free tail, hundreds of GiB. For 256 fresh pages one page into a 1 GiB placeholder,main's loop makes 2,561VirtualQuerycalls; this change makes one.This PR keeps one host allocation per 16 KiB page: each page is still its own allocation with the same protection and write watch,
Commitreports the same created ranges as before, andmunmap,MAP_FIXEDremaps, views and collects see the same layout as before. What changes is how a fresh range is cut out of its placeholder:Commitqueries the placeholder once, splits off the part before the range (if the range starts inside the placeholder) and the part after it (if the placeholder extends past the range), and then carves the pages from that bounded placeholder: one split and oneVirtualAlloc2per page, and no split for the last page.reset(), and committed memory in the range still only changes its protection, as onmain.std::invalid_argumentbefore touching the address space; the carve loop needs a granule to make progress. Every caller passes a non-zero multiple of 4 KiB (DirectMemory.cpp16 KiB,GuestHeap.cppand the driver'sLifecycle.cppthe whole range).windows_write_watchcounts the queries through a counter that only that test defines (APS5_COUNT_MAPPING_QUERIES,WindowsMappings::MemoryQueries(), copied exactly from #9505 so whichever PR lands second drops its copy).Overlap:
APS5_COUNT_MAPPING_QUERIES,MemoryQueries(), the increment inquery()and the test's compile definition) are copied verbatim from its heada2e0bf19e6, so whichever PR lands second drops its copy. #9505 also adds twobreakViewcalls toreset(): pages carved from one placeholder no longer go throughreset(), while the coalescing path still does, and there #9505's calls run as onmain.allocatecall that this change moves intoreplace; whichever of the two lands second has to keep that record insidereplace.Commit.MapZeroto the same file (read-only zero-page views for unbacked AMM PRT pages) and editsDirectMemory.cpp; it merges without conflicts with this branch, and a laterCommitover its committed zero-page views takes the protection-only branch, as onmain.tests/WindowsWriteWatch.cpp(new test functions and their calls inmain()at the same places), and with #9098 also inWindowsMappings.hpp(the movedallocatecall).Tested
Windows 11 26200, Ryzen 5 5600X, AMD Radeon RX 6700 XT (driver 32.0.21045.16010) for the GPU tests and the title, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON, this head73f9b5d9fbonmain9d6adeb550.windows_write_watch, red onmain'sCommit: withmain'sWindowsMappings.hppplus only the counter hunks, the test fails withcommitting 256 fresh pages queried the address space 2561 times. With this change it passes 10 times in a row.ctest -R "^(windows_write_watch|windows_mappings_release.*|guest_memory.*|guest_mmap.*|guest_mprotect.*|guest_protection_report|guest_flexible_memory_.*|guest_page_table_stats|guest_kernel_stack_protect|guest_mlock|agc_write_tracking.*|agc_resident_direct_remap|agc_write_watch_coverage|agc_import_.*|agc_host_import_.*|agc_heap_mirror_.*)$",ANYPS5_REQUIRE_VULKAN=1) pass three times in a row.guest_memory'sCheckRemappedDirectMemoryIsChargedOncefailed once in another series of CPU-only runs while a build ran next to it; it compares the system-wide commit charge, and it fails the same way onmain9d6adeb550(1 of 3 runs under the same load).ctest --parallel 4 --timeout 300(1937 tests) on this head and onmain9d6adeb550: 1932 pass on both, and the same five fail on both:log_macrosandlog_macros_timestamps(a local artefact of this tree's ccache base directory, which makes__FILE__relative) and the threeagc_driver_sample_ordertests. In an earlier full run onc51ca2b323,guest_memoryexited with 0xc0000409 once; it passed in both reruns, and its system-wide commit charge check is the one that flakes under load onmaintoo.Title, PPSA14363 (METAL GEAR SOLID Master Collection Vol. 1), same machine, PC idle (no input for over 20 minutes, no other game running), the exclusive GPU lock and the build lock held for the whole series, so nothing else ran on the PC. One run directory prepared from
main9d6adeb550; each arm swaps in all of its build's.prxfiles (checked by hash before every run): A =main9d6adeb5500c, B = this change built asc51ca2b3232e, which has the same production code as this head73f9b5d9fb(the head only moves one new test function into the anonymous namespace and rewords the commit message). Order W(arm)-A-B-B-A-A-B-B-A, 45 s each from start-up,APS5_TRACE_SUMMARY_EVERY=10, warm shader cache (all hits). The first 10 s window after the first present covers the boot's anonymous maps:main), 4 runsThe later windows match on both sides (599-600 presents, p99 about 17.2 ms). Summary lines of one run per side, first window:
tools/performance_compare.py on those two lines
Performance comparison
9d6adeb550Frame and GPU times (ms)
Top phases (ms per frame)
One more B start wrote only the first characters of its first stderr line in its 45 s and never reached guest code (no other output); the next six starts of the same files reached the guest in 0.96-1.49 s. Traces are not attached: for this commercial title we share summary lines only.
Not tested: Linux (the change is Windows-only code), other CPUs and Windows builds.
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)🤖 Generated with Claude Code