perf(agc): cache the main image's page states on Linux #7788
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7788"
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 ANONYMOUSZED-beep on 2026-10-19T07:35:51Z.
What
On Linux,
GuestMemory's page table covered only the guest arena. Every check of an address in the main executable (its code, read-only data and the writable data thatbackWritableImageAnonymouslybacks with anonymous pages) went to the host: onePROCMAP_QUERYioctl on 6.11+ kernels, and a parse of the whole of/proc/self/mapson older ones (Ubuntu 24.04's 6.8, orAPS5_NO_PROCMAP_QUERY=1). This is the part left open at the end of #6183 ("PageStates::initializeonly sets up the image span on Windows, so on Linux every lookup there goes to the host"), measured there at about 140 MB/s of/proc/self/mapsreads by one thread in Guardians of the Galaxy after #6184.PageStates::initializenow sets up the image span on Linux too, from the executable'sPT_LOADsegments asdl_iterate_phdrreports them (the first entry, the main program), page-aligned from the lowest segment start to the highest segment end.APS5_NO_IMAGE_PAGE_CACHE=1leaves it uncached on both systems, as before on Windows.Invalidation is the same as on Windows: guest
mprotectof the main image goes throughGuestAllocations::Mutation(DoMprotectregisters the main image on Linux as well and protects throughMutation::Protect), whose recorded change reaches the invalidator, which forgets the pages in both spans.backWritableImageAnonymouslyand the loader's RELRO protection run beforemain, before the table exists. The Linux write watch uses userfaultfd write protection, which does not change the VMA's permissions, so it does not make cached states stale.RunUnmappedGapTests(agc_driver_graphics) gains a case for a page-aligned page in the test executable's.bss: after it is checked writable, it is made read-only behind the registry and must still read as writable from the table; afterGuestAllocationsInvalidateit must read as read-only, and after it is restored and invalidated, as writable again.Tested
Linux (Amazon Linux 2023, kernel 6.18, 2-core VM), GCC 14, Release:
agc_driver_graphicsandagc_driver_graphics_maps_fallback(APS5_NO_PROCMAP_QUERY=1) pass, and the 35 ctest entries that runagc_driver_graphics_tests,agc_driver_testsandagc_shader_memory_testsall pass.APS5_NO_IMAGE_PAGE_CACHE=1(the behaviour ofmain), the new case fails with "a checked main image page was queried again instead of read from the page table".GuestMemory::Accessible(page, 4096, true)calls on the test's image page, in a process with about 50 lines in/proc/self/maps:PROCMAP_QUERYAPS5_NO_PROCMAP_QUERY=1APS5_NO_IMAGE_PAGE_CACHE=1, asmain)The parse cost grows with the length of
/proc/self/maps(about 7,500 lines in the #6183 title).Not tested: no title was run, so there are no performance-report summary lines or traces; Windows is unchanged (the new code is in the non-Windows branch); macOS.
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)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T07:36:04Z.
This pull request is labeled
perf, so it needs the summary line of a capture onmainand one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste bothaps5-summary.jsonllines, attach the two traces and add the output oftools/trace_digest.py.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T07:44:16Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/tests/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppImported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T10:55:13Z.
Review: COMMENTED
The new image-span code is in the non-Windows branch (
#ifndef _WIN32aroundmainImageExtent, and the#elseofPageStates::initialize), so macOS compiles it too, and macOS has no<link.h>,dl_phdr_info,dl_iterate_phdrorPT_LOAD. Onmainthis is hidden becauselibSceAgcDriverdoes not build on macOS yet, but it breaks the macOS series from #1, which puts an#elif defined(__APPLE__)include block in front of the one that gets<link.h>.Checked on macOS 27.2, Apple M2 Max, Apple clang 21.0.0,
-arch x86_64 -std=c++20 -fsyntax-onlyonGuestMemory.cpp, on top of mugurc'sci/macos-series-2(0d43cff93):unknown type name 'dl_phdr_info'(line 503),use of undeclared identifier 'PT_LOAD'(line 510)#ifdef __linux__onmainImageExtentand#elif defined(__linux__)ininitializeGuarding both with
__linux__(the title already says "on Linux") keeps macOS on the uncached path; a macOS image span would need dyld (_dyld_get_image_header(0)and its segments) rather thandl_iterate_phdr. Only a syntax check, I did not build or run the tests.Written with AI assistance (Claude Code). I ran the checks above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T02:13:21Z.
Checked the new image span against
dl_iterate_phdr's main-program entry and the invalidation path (DoMprotectregisters the main image on Linux and goes throughMutation::Protect); the new test pins a value only the page table gives.Added one commit:
mainImageExtentand the Linux branch ofPageStates::initializeare now guarded with__linux__(reported by @jotitadev:dl_phdr_info/PT_LOADdon't exist on macOS), so other platforms stay on the uncached path. Full ctest passes after the rebase; merged.