perf(libSceSysmodule): cache the unwind region of loaded images on Windows #6064
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6064"
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 JAB-dev on 2026-10-14T21:06:58Z.
Besiege's long freeze while it loads is about 75% shorter on Windows: the longest frame goes from about 13 s to about 3 s, and it presents about 1.16 times as many frames in the first 40 seconds. Steady-state frame time is unchanged.
What
sceSysmoduleGetModuleInfoForUnwindcalledVirtualQueryandGetMappedFileNameAon every call. On WindowsVirtualQuerytakes time proportional to the size of the region it describes: measured in Besiege it costs 2 us for a 16 KiB region, 51 us for 8 MiB and 167 us for 32 MiB, and Besiege's loaded images have regions of 8 to 32 MiB. A title that unwinds exceptions while loading calls the function for every frame of every throw: 80,000 calls in the first 40 seconds, 6.7 s ofVirtualQueryon one thread, while the rest of the game waits.GetMappedFileNameAis 4.6 us per call.The region (begin, size) and the file name of an address inside an image of the Windows loader's module list are now kept per region. A cached entry is used only while the loader still reports the same image for the address (same base, time stamp and size); otherwise it is dropped and the address is queried again. Addresses outside loaded images, such as guest memory, are queried every time, so a protection change there is still seen. The reported fields are unchanged: for an address in a cached run the entry returns the queried page and the bytes to the end of the run, which is what
VirtualQueryreturns. Linux is not affected.docs/dev/debt/libSceSysmodule.mdrecords that a page protection change inside a cached image after its first lookup is not noticed.Seen in PPSA20313 (Besiege, Unity IL2CPP). A thread sample of the game while loading showed one thread spending 32% of its samples in
NtQueryVirtualMemoryunder this function.Tested
Windows 11, WinLibs GCC 15.2.0 r7, Release, RTX 4070 SUPER, NVIDIA 610.88, 1920x1080. Besiege, the first 40 s after launch (
APS5_TRACE_SECONDS=0:40), warm shader cache, three alternating runs of main at7003eda5d6and of this branch atcd95f1a426, no otherAPS5_TRACE_*switch, the game's output sent tonul:The same three alternating runs with the game started in a console window instead (output to the console),
mainat44df21632and this branch merged onto it: longest frame 8.4 / 8.8 / 8.6 s onmain, 2.5 / 2.5 / 2.6 s with this PR; frames presented in 40 s 1240 / 1136 / 1196 -> 1355 / 1371 / 1364.Summary lines of the middle run of each:
main:
this PR:
guest_sysmodulenow also checks on Windows that the reported region and name equal whatVirtualQueryandGetMappedFileNameAreturn, for code, data and read-only data of the test executable, a host DLL, the stack and private memory, three times each so the later calls come from the cache; a protection change in private memory and a freed region are seen. The new assertions pass on main, because the change only affects speed. Mutation check: removing the check that the address is inside the cached region fails the test. Further addresses inside an already cached run, queried forward, backward (an earlier page after a later one) and repeated, are compared withVirtualQueryas well; those checks fail on the first commit of this branch and pass on its head.guest_sysmoduleandguest_unwind_module_infopass;tools/check_conventions.pypasses.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
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T09:22:00Z.
Windows run of head
cd95f1a42alone, merged ontomaind92a8dad3(clean merge; it also merges cleanly onto the newerdb81992c5, not built). This is one NVIDIA/Windows data point only.libspass with 0 errors and no warnings inExport.cpporGuestSysmodule.cpp.--repeat until-fail:3, 60/60 runs:guest_sysmodule,guest_unwind_module_info,unwind_record_bounds,windows_exceptions,guest_exception_handler,guest_raise_exception,guest_mutex_raise_exception,undeliverable_host_exceptionand the 12guest_exception_vtable-*cases. As you wrote, the extendedguest_sysmodulealso passes againstmain'slibSceSysmodule.prx.ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'): 812/814.conventions_function_bodiesandguest_none_relocationstimed out under load and passed when rerun alone.tools/check_conventions.pyandgit diff --checkpass.Cost per call. I wrote a small harness that loads each side's
libSceSysmodule.prxand callssceSysmoduleGetModuleInfoForUnwind20,000 times per case. Each side ran 3 times, alternating:main(µs/call).bsssection of the harness exe.datavariable.text(~40 KiB)Titles: none of the 47 local catalog titles I ran calls this function. I counted calls with a diagnostic build of
main'slibSceSysmodule.prx(a call counter and timer only), running each title for 30–40 s, and every title made 0 calls. That includes OpenRCT2 PPSA99728 forced to throw (libc++abi: terminating due to uncaught exception of type OpenRCT2::Ui::SDLException, identical on both sides). In these titles only the guestlibc.prximports it, next tosceKernelGetModuleInfoForUnwind. So I cannot reproduce the Besiege effect locally. As a no-regression check, I ran Middle Ages Ping Pong PPSA28319 (Unity IL2CPP, 0 calls). Each side had one warm-up and then 3 runs of 60 s, alternating, withAPS5_TRACE_SUMMARY_EVERY=10:main, min / medianThe longest frame always falls in the first 10 s, and both sides sit at 17–18 ms after that. Since this title never calls the function, the differences are run-to-run noise.
One behaviour change: the reported region is not always the same as on
main.VirtualQueryreturns the run of equal pages starting at the queried page. Onmain,seg0_addr/seg0_sizeare therefore that page and the bytes up to the end of the run. The cache keeps the first lookup's[page(first), end)and returns it for every later address in that run. Querying two addresses in the same run, in this order:The same happens for two addresses 8 MiB apart in a
.bsssection. With the PR, the value reported for an address depends on which address in that run was looked up first. The newguest_sysmodulechecks query each address three times but never a second address inside a cached run, so they pass. Returningpage(addr)andend - page(addr)from the cached entry should keep the output identical tomain; I have not tried it. I don't know whether the guest unwinder usesseg0_addr, but the description says the reported fields are unchanged, and this is a case where they change.Verdict: on Windows the build, the tests and the suite pass. The cache makes a call about 150 times cheaper for large image regions and about 90 times cheaper for small ones on this machine. No local title exercises the function, so this run cannot confirm the title-level gain, and PPSA28319 shows no regression. The reported region differs from
mainfor a second address inside an already cached run.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:14:07Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/debt/libSceSysmodule.mddocs/dev/debt/libSceSysmodule.mdcore/libs/tests/GuestSysmodule.cppL105docs/dev/debt/libSceSysmodule.mddocs/dev/debt/libSceSysmodule.mdSome 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/libSceSysmodule/Export.cppcore/libs/tests/GuestSysmodule.cppcore/libs/prx/libSceSysmodule/Export.cppImported from boykopovar/AnyPS5. Originally posted by JAB-dev on 2026-10-15T19:18:13Z.
Besiege (PPSA20313),
mainat44df21632vs the same with this PR merged (1b9b0a2de), Windows 11, RTX 4070 SUPER (610.88), 1080p, first 40 s from launch, three alternating runs each: longest frame 17.4 / 11.9 / 8.3 s -> 2.5 / 2.3 / 2.5 s; frames presented 635 / 1045 / 1203 -> 1298 / 1399 / 1372. Title-screen FPS is unchanged (32.2 / 31.6 -> 33.7 / 30.3).Imported from boykopovar/AnyPS5. Originally posted by jweidenh on 2026-10-15T19:56:55Z.
I reproduced the cached-region mismatch reported by screamm and prepared a correction on top of this PR's unchanged head
cd95f1a42675323231e0b7d3b05f2107c400807b:github.com/jweidenh/AnyPS5@86b6700faagithub.com/jweidenh/AnyPS5@0075fb6f62github.com/jweidenh/[email protected]The cache hit now returns the queried address rounded down to the system page boundary, with size
cached_end - queried_page. The page size comes fromGetSystemInfoonce; cache lookup and image-identity validation are unchanged. This follows the documented per-query bounds of VirtualQuery.The new Windows fixture uses committed, loader-owned executable image storage and checks eight mixed-order offsets across three equal-protection pages, repeating each lookup three times. It compares the region bounds and mapped name against Windows queries, checks the structure size and zero unwind fields, and poisons the output before each call. Existing image, private-memory, protection-change and freed-memory checks remain.
Native Windows 11 Pro build 26300, Ryzen 7 5800X, WinLibs GCC 15.2.0 r7, Release, SPIRV-Tools ON. This PC has an RTX 5090 / driver 617.42; these are CPU tests and do not validate GPU behavior.
The test-only baseline
86b6700ffailed exactly on the second image page: expected size 126,976 bytes versus cached size 131,072, with the cached base 4,096 bytes too early. The common region end was unchanged. That intended assertion failure was observed once; no baseline rerun was needed.Exact candidate
0075fb6fpassed all five selected tests without skips or retries:The regression reported all 24 page lookups passing. The regression executable and recorded shared runtime dependencies were byte-identical between baseline and candidate except for
libSceSysmodule.prx. Both source revisions use the identical fixture, without validation overlays. Focused executables and the fulllibstarget built successfully; conventions and diff checks passed. Exact source/gitlink pins, test inventory, library paths, binary hashes, JUnit and raw logs were checked, with an independent evidence audit. The reviewed private validation harness is pinned atd1e6ddf0e921af5f76377ecd44d27b3b62e7009a; its logs are retained locally, not a public CI artifact.This is focused Windows correctness coverage, not full-suite or Linux validation. I have not measured Besiege, startup latency or FPS. The existing documented limitation around page-protection changes inside cached images remains. AI-assisted implementation and validation.
Imported from boykopovar/AnyPS5. Originally posted by JAB-dev on 2026-10-15T20:08:40Z.
Thanks for the run, the difference is real:
VirtualQuerystarts the reported region at the queried page, and the cache returned the first lookup's page for every later address in the run. Fixed ina63e77e9f: the cached entry now returns the queried page and the bytes to the end of the run, as you suggested.guest_sysmodulenow also queries further addresses in the same run of a host image, forward, backward and repeated, and compares each withVirtualQuery; it fails oncd95f1a42and passes on this head.guest_sysmodule,guest_unwind_module_infoandtools/check_conventions.pypass.Imported from boykopovar/AnyPS5. Originally posted by JAB-dev on 2026-10-15T20:22:39Z.
Correction to my reply above: the backward lookup I described was not exercised, its guard could never be true.
fbe49d6a6replaces it with a real one: an ntdll address three pages in is queried first, then its first page, then the pages between, each compared withVirtualQuery. Run on its own, that block fails oncd95f1a42and passes on this head.guest_sysmodule,guest_unwind_module_infoandtools/check_conventions.pypass. @jweidenh your regression and fix reach the same result asa63e77e9f; thank you for reproducing it.Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-16T10:49:19Z.
Rerun on Windows of head
fbe49d6a6, merged ontomain5d9dcc42dtogether with #5973 (libkernel only, no shared file; both merges clean). This follows up my run ofcd95f1a42.alignasAGC test (#6452/#6478).libspasses, with no warnings inExport.cpporGuestSysmodule.cpp.--repeat until-fail:3:guest_sysmodule,guest_unwind_module_info,unwind_record_bounds,windows_exceptions,guest_exception_handlerandguest_raise_exceptionpass, 18/18 runs.a63e77e9f, so the cache returnsfound->firstagain, and kept the tests fromfbe49d6a6.guest_sysmodulethen aborts on aRequire(0xc0000409). With the fix restored, it passes..bss+1 MiB, +9 MiB, +1 MiB again,.text,.text+0x3000) now reportsseg0_addr/seg0_sizeequal toVirtualQueryfor all five lookups, as onmain. Oncd95f1a42, two of them differed.mainnumbers are noisy:main(µs/call).bss.datavariable.textctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'): 834/837, 2 skipped.hardware_oracle_compareandguest_file_lock_processtimed out under load and passed serially.guest_memoryfailed once at its host-wide commit-charge check (CheckRemappedDirectMemoryIsChargedOnce). It then passed serially, failed 1 of 2 runs in a 10x repeat, and passed a second 10x 10/10. Neither PR touches that code.tools/check_conventions.py --base 5d9dcc42dandgit diff --checkpass.Title: Middle Ages Ping Pong PPSA28319 (Unity IL2CPP; in my first run it made 0 calls to this function, which I did not re-measure here). 130 s per run, 2 alternating runs per side,
APS5_TRACE_SECONDS=0:40as in your measurements. Neither side has a stop line, and every run was killed at the time limit:mainr1 / r2These are equal within run-to-run noise, as expected for a title that does not call the function.
Verdict: the region mismatch I reported is fixed, and the new backward lookup in
guest_sysmodulecatches it on Windows. Build, tests and suite pass. Not checked: Linux, AMD/Intel GPUs, and Besiege (not available here, so the title-level gain is still only your measurement).Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T21:41:04Z.
Checked the diff, the build and
guest_sysmodule/scoped ctest on Linux (36/36), and relied on screamm's Windows runs for the Windows path (repeat runs of the sysmodule/unwind tests, negative control failing without the region fix, per-call cost 18-46 µs -> ~0.1 µs). The cache is validated against the loader's image identity, addresses outside images are still queried, and the debt entry records the remaining gap. Merging once the Windows build is green.