perf(libkernel): find guest allocations and direct mappings by address #4216

Merged
Mouad-Scriptz-0 merged 1 commit from upstream-pr/4216 into main 2026-10-13 11:09:55 +00:00
Mouad-Scriptz-0 commented 2026-10-12 01:39:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mouad-Scriptz-0 on 2026-10-12T01:39:35Z.


What

sceKernelVirtualQuery, sceKernelMunmap, sceKernelReleaseDirectMemory and the reservation paths copied the whole allocation registry (or walked every direct mapping) under one lock on each call. Minecraft (PPSA17221) holds about 7,000 direct mappings and spent its world generation in these walks: free() → sceKernelVirtualQuery/sceKernelReleaseDirectMemory dominated every thread's stacks (eu-stack), and the render thread blocked on the registry lock behind them.

  • GuestAllocationsLookup_nid_postfix(address, next) answers "which range holds this address" (and the find-next flag of sceKernelVirtualQuery) from the registry's ordered map; sceKernelVirtualQuery uses it instead of acquiring a copy of all ranges.
  • Range replacement computes a RangePatch (ranges to erase, ranges to insert) and applies it to the live map instead of rebuilding the map; allocationRemains and Covers start from the containing range.
  • main's physical-span index (5b7cb8d1, consulted by WatchMapping for shared backings) now also answers ErasePhysMappings (sceKernelReleaseDirectMemory) and DirectMemoryViews through one MappingsSharingPhys helper, so a release touches only the mappings over the released range; the index is kept on every platform for that instead of Linux only.
  • guest_memory gains two cases: sceKernelVirtualQuery finds the containing range, the split after sceKernelMprotect, and the next range after a hole (flags 1); releasing a physical range in the middle of 48 separately mapped pages plus an alias unmaps exactly those views.

Behaviour is unchanged; only the lookups are. With #4118 and the flip event PR, Minecraft goes from a crawl in world generation to playable.

Tested

Fedora 44 (kernel 7.2.8, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, Intel Arc Meteor Lake, Mesa 26.2.3), rebased onto main c36fd0a0: guest_memory, guest_memory_pool_stats, guest_reallocalign, agc_write_tracking, agc_write_watch_coverage pass; tools/check_conventions.py --base origin/main and git diff --check pass. Minecraft (PPSA17221) and Zombie Cure Lab (PPSA22623) run with it (Linux). Not tested on Windows.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823928089 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4216). Originally posted by [Mouad-Scriptz-0](https://github.com/Mouad-Scriptz-0) on 2026-10-12T01:39:35Z. --- ### What `sceKernelVirtualQuery`, `sceKernelMunmap`, `sceKernelReleaseDirectMemory` and the reservation paths copied the whole allocation registry (or walked every direct mapping) under one lock on each call. Minecraft (PPSA17221) holds about 7,000 direct mappings and spent its world generation in these walks: `free()` → `sceKernelVirtualQuery`/`sceKernelReleaseDirectMemory` dominated every thread's stacks (`eu-stack`), and the render thread blocked on the registry lock behind them. - `GuestAllocationsLookup_nid_postfix(address, next)` answers "which range holds this address" (and the find-next flag of `sceKernelVirtualQuery`) from the registry's ordered map; `sceKernelVirtualQuery` uses it instead of acquiring a copy of all ranges. - Range replacement computes a `RangePatch` (ranges to erase, ranges to insert) and applies it to the live map instead of rebuilding the map; `allocationRemains` and `Covers` start from the containing range. - `main`'s physical-span index (5b7cb8d1, consulted by `WatchMapping` for shared backings) now also answers `ErasePhysMappings` (`sceKernelReleaseDirectMemory`) and `DirectMemoryViews` through one `MappingsSharingPhys` helper, so a release touches only the mappings over the released range; the index is kept on every platform for that instead of Linux only. - `guest_memory` gains two cases: `sceKernelVirtualQuery` finds the containing range, the split after `sceKernelMprotect`, and the next range after a hole (`flags` 1); releasing a physical range in the middle of 48 separately mapped pages plus an alias unmaps exactly those views. Behaviour is unchanged; only the lookups are. With #4118 and the flip event PR, Minecraft goes from a crawl in world generation to playable. ### Tested Fedora 44 (kernel 7.2.8, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, Intel Arc Meteor Lake, Mesa 26.2.3), rebased onto `main` c36fd0a0: `guest_memory`, `guest_memory_pool_stats`, `guest_reallocalign`, `agc_write_tracking`, `agc_write_watch_coverage` pass; `tools/check_conventions.py --base origin/main` and `git diff --check` pass. Minecraft (PPSA17221) and Zombie Cure Lab (PPSA22623) run with it (Linux). Not tested on Windows. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Pacuka commented 2026-10-12 16:18:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-12T16:18:28Z.


Data point from Minecraft (PPSA17221) on Linux / AMD: CachyOS kernel 7.2.2, RX 6650 XT (RADV, Mesa 26.2.4), GCC 16.2.1, Release. Base: main d592d54e1 + #4215 #4218 #454 #3633 #3604. Host import on.

Without this PR the render thread spent most of its time in HostImportFor. About 3% of the lookups found the registry generation changed, and then GuestAllocationsAcquire_nid_postfix copied all ~7,000 ranges. That took ~3.6 ms per lease and 111 s in total over a ~3 min run, about 125 us on average per in-place draw input. With this PR the same counter is 6 s over a comparable run. The in-game FPS counter went from ~2 to ~6 in a world.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6128187954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4216#issuecomment-6128187954). Originally posted by [Pacuka](https://github.com/Pacuka) on 2026-10-12T16:18:28Z. --- Data point from Minecraft (PPSA17221) on Linux / AMD: CachyOS kernel 7.2.2, RX 6650 XT (RADV, Mesa 26.2.4), GCC 16.2.1, Release. Base: main `d592d54e1` + #4215 #4218 #454 #3633 #3604. Host import on. Without this PR the render thread spent most of its time in `HostImportFor`. About 3% of the lookups found the registry generation changed, and then `GuestAllocationsAcquire_nid_postfix` copied all ~7,000 ranges. That took ~3.6 ms per lease and 111 s in total over a ~3 min run, about 125 us on average per in-place draw input. With this PR the same counter is 6 s over a comparable run. The in-game FPS counter went from ~2 to ~6 in a world.
xXPBSXx commented 2026-10-12 16:27:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T16:27:15Z.


Windows test of head 327e3b810 merged onto current main (2a5eca11c, clean merge; built together with two other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors, -j2.

guest_memory (the test this PR changes) passes. The 25 tests matching ^(guest_memory|guest_np_auth|windows_exceptions|guest_exception|guest_kernel) all pass.

Not checked: that the test fails without the change, Linux, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6128326915 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4216#issuecomment-6128326915). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T16:27:15Z. --- Windows test of head `327e3b810` merged onto current `main` (`2a5eca11c`, clean merge; built together with two other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors, `-j2`. `guest_memory` (the test this PR changes) passes. The 25 tests matching `^(guest_memory|guest_np_auth|windows_exceptions|guest_exception|guest_kernel)` all pass. Not checked: that the test fails without the change, Linux, a title run. AI-assisted (Claude Code).
oneandonlydean commented 2026-10-13 00:33:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T00:33:12Z.


Needs a rebase: docs/dev/TechnicalDebt.md was split into docs/dev/debt/, and that is the only conflict. Please drop the entry instead of moving it; it describes the index, not a gap.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135330229 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4216#issuecomment-6135330229). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T00:33:12Z. --- Needs a rebase: `docs/dev/TechnicalDebt.md` was split into `docs/dev/debt/`, and that is the only conflict. Please drop the entry instead of moving it; it describes the index, not a gap.
github-actions[bot] commented 2026-10-13 10:32:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T10:32:44Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
#1835 core/libs/prx/libc/src/GuestAllocations.cpp L427-428
#5053 core/libs/tests/GuestMemory.cpp L2151
#5073 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp L349-351
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143611092 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4216#issuecomment-6143611092). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T10:32:44Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1835 | `core/libs/prx/libc/src/GuestAllocations.cpp` [L427-428](https://github.com/boykopovar/AnyPS5/blob/b1328fc0f9012e820fb92c8fdbbb4af02007bd48/core/libs/prx/libc/src/GuestAllocations.cpp#L427-L428) | | | | #5053 | `core/libs/tests/GuestMemory.cpp` [L2151](https://github.com/boykopovar/AnyPS5/blob/b1328fc0f9012e820fb92c8fdbbb4af02007bd48/core/libs/tests/GuestMemory.cpp#L2151) | | | | #5073 | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` [L349-351](https://github.com/boykopovar/AnyPS5/blob/b1328fc0f9012e820fb92c8fdbbb4af02007bd48/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp#L349-L351) | | |
oneandonlydean commented 2026-10-13 11:09:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T11:09:51Z.


Rebased onto main 77b9f7270c and force-pushed. The only conflict was docs/dev/TechnicalDebt.md; the physical index entry is dropped rather than moved to docs/dev/debt/, since it describes the index, not a gap. No code changes; the commit keeps your authorship. guest_memory, guest_memory_pool_stats, guest_reallocalign, agc_write_tracking, agc_write_watch_coverage and the agc_/guest_ ctest set pass on Linux. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6144221650 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4216#issuecomment-6144221650). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T11:09:51Z. --- Rebased onto main 77b9f7270c and force-pushed. The only conflict was `docs/dev/TechnicalDebt.md`; the physical index entry is dropped rather than moved to `docs/dev/debt/`, since it describes the index, not a gap. No code changes; the commit keeps your authorship. `guest_memory`, `guest_memory_pool_stats`, `guest_reallocalign`, `agc_write_tracking`, `agc_write_watch_coverage` and the `agc_`/`guest_` ctest set pass on Linux. Merging.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4216
No description provided.