Skip to content

perf(agc): cache page permissions within registered allocations - #1330

Closed
TibixDev wants to merge 2 commits into
boykopovar:mainfrom
TibixDev:pr/tracked-page-permissions
Closed

TibixDev wants to merge 2 commits into
boykopovar:mainfrom
TibixDev:pr/tracked-page-permissions

Conversation

@TibixDev

@TibixDev TibixDev commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Status after review

Current page caches overlap the original motivation.

Current GuestMemory.cpp already caches arena page states, additionally caches the Windows image extent, and registers ForgetPages with GuestAllocationsSetInvalidator_nid_postfix. This changes the baseline substantially beyond the original #157 comparison. The new Skyrim loading sample on main + experimental NGS reports zero OS query calls while servicing tens of thousands of verifies; that is a limited workload observation, not a universal conclusion.

There is no new evidence that an extra 64-entry registered-range cache pays for its previously measured 7–20% invalidation/large-set/unregistered regressions. Nor is there a demonstrated need for another disable knob/export. A future version must audit permission changes outside Mutation against the existing invalidation path. #157 is merged; the carried copy is historical and is not an open dependency. No new code push.

What

Cache Linux page permissions only inside registered allocation bounds. Keep at most 64 positive results per thread. Mapping generations invalidate them; a busy registry skips the cache fill. Unregistered host memory still uses a kernel query.

Tested

Earlier branch-specific tests and measurements remain in the discussion. They have not been rerun on a redesigned current-main version of this PR. The review audit and shared NGS-assisted title exploration do not establish that this head is merge-ready. No full Linux/Windows platform CI was dispatched in this pass.

Checklist

Pacuka and others added 2 commits October 8, 2026 09:04
Integrate upstream PR boykopovar#157 at a1427b8. Query individual VMAs through the kernel and retain the text scan on older kernels.

Local validation: graphics, shader-memory and write-tracking tests pass with the ioctl path and with APS5_NO_PROCMAP_QUERY=1. The Skyrim performance capture identified repeated maps parsing as a CPU hotspot.

AI-assisted upstream implementation and local integration.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Cache positive kernel mapping queries within registered allocation bounds, invalidated by the allocation generation. Use a bounded per-thread cache and a nonblocking registry description so cache fills never wait behind allocation or unmap work. Keep kernel queries for unregistered memory and APS5_NO_MAPPING_CACHE for comparison.

Tested on Linux: agc_driver, agc_driver_graphics, agc_driver_pm4; graphics tests also pass with the cache disabled and with the proc-maps text fallback. Cover protection changes, partial unmaps, address reuse, adjacent unregistered mappings, busy registries and cross-thread invalidation.

Written with AI assistance.
@TibixDev
TibixDev force-pushed the pr/tracked-page-permissions branch from 1b1fc32 to ebfe2b7 Compare October 8, 2026 06:07
@TibixDev

TibixDev commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on e9846679. The #157 prerequisite retains Pacuka's authorship.

Measured upstream plus #157 (5b65345a) against that same baseline plus this PR's final commit (ebfe2b70). Thus both sides use PROCMAP_QUERY.

Fedora 44, GCC 16 Release, Ryzen 5 5600, CPU 3. Six processes per variant, ABBA × 3. Each process has one warmup and nine batches of 4,096 calls to production GuestMemory::Accessible. Requests rotate across separate registered 4 KiB ranges in one host mapping. The unregistered case uses the same host mapping without registry entries. No game or build ran during timing.

Registered ranges Registry state Upstream + #157 median (ns) PR median (ns) Baseline range PR range
0 stable 650.5 693.9 643.7–810.9 679.5–704.6
1 stable 651.7 22.1 642.0–800.9 21.9–22.5
1 invalidated 678.2 729.6 673.6–690.0 719.6–737.3
64 stable 652.4 39.8 639.1–732.2 39.6–67.5
64 invalidated 690.2 763.4 676.3–949.7 743.8–839.6
128 stable 654.3 784.4 640.1–889.6 775.9–861.3
128 invalidated 710.3 769.3 677.1–882.4 757.1–850.1

Stable sets of 1 and 64 ranges use 96.6% and 93.9% less time. The 128-range cycle exceeds the 64-entry cache and has a 19.9% slower median. Ending a registry mutation before every query forces invalidation; those medians regress by 7.6–10.6%. The unregistered median regresses by 6.7%, with overlapping ranges. These costs are included rather than omitted from the result.

Every query returns the expected access result. Driver, graphics, and PM4 tests pass. Graphics tests also pass with the cache disabled and with the text /proc/self/maps fallback. These include adjacent unregistered memory, protection changes, partial unmaps, address reuse, a busy registry, and cross-thread invalidation. Contribution checks pass. No title frame time or Windows result is claimed. AI-assisted.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

#157 is in; please rebase to drop its copy (it conflicts in GuestMemory.cpp).

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the measurements. Two things before this can go in:

  • Rebase. #157 is merged; as @oneandonlydean asked, rebase onto main to drop its copy of describePages (the PR conflicts in GuestMemory.cpp). After that, only the final commit should remain.
  • Justification. Your own table shows this cache is 7–20 % slower in the invalidated, 128-range and unregistered cases, and #157 alone already removes the slowdown in a real title (see Dean's test on #157: 60 fps held where main fell to single digits). Please show a case from a title or a workload where the query cost is still visible after #157 and this cache fixes it; otherwise the regressions outweigh a gain that only appears in stable sets of ≤ 64 ranges.
  • Staleness. The cache is only invalidated by GuestAllocations mutations. A host-side mprotect inside a registered allocation outside a Mutation (your test needs the explicit GuestAllocationsInvalidate call for it) keeps returning the old permissions. Please state which callers can do that, or drop the cache for them.
  • Surface. APS5_NO_MAPPING_CACHE is a new environment knob and GuestAllocationsTryDescribeRange_nid_no_patch a new libc export-style function; keep them only if needed.

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still blocked on the same points from the previous review:

  • Rebase. #157 is merged; the PR still carries its copy of describePages and conflicts in GuestMemory.cpp. Only the final commit (ebfe2b70b) should remain.
  • Justification. The measured gains are in stable sets of ≤ 64 ranges; the invalidated, 128-range and unregistered cases are 7–20 % slower, and #157 alone already removes the slowdown in a real title (@oneandonlydean held 60 fps where main dropped to single digits). Please show a title or workload where the query cost is still visible after #157 and this cache fixes it. Without that the regressions and the extra surface outweigh the gain.
  • Surface. If it stays, say why APS5_NO_MAPPING_CACHE and the new GuestAllocationsTryDescribeRange_nid_no_patch export are needed, and which callers can change permissions inside a registered allocation outside a Mutation (the test needs the explicit GuestAllocationsInvalidate call for that).

Closing as stale later if there is no activity; feel free to reopen rebased with the evidence.

@TibixDev

Copy link
Copy Markdown
Contributor Author

Review-pass update:

Current GuestMemory.cpp already caches arena page states, additionally caches the Windows image extent, and registers ForgetPages with GuestAllocationsSetInvalidator_nid_postfix. This changes the baseline substantially beyond the original #157 comparison. The new Skyrim loading sample on main + experimental NGS reports zero OS query calls while servicing tens of thousands of verifies; that is a limited workload observation, not a universal conclusion.

There is no new evidence that an extra 64-entry registered-range cache pays for its previously measured 7–20% invalidation/large-set/unregistered regressions. Nor is there a demonstrated need for another disable knob/export. A future version must audit permission changes outside Mutation against the existing invalidation path. #157 is merged; the carried copy is historical and is not an open dependency. No new code push.

I have updated the PR description to match this status. The PR remains open; the outstanding review requirements are not being marked resolved. AI-assisted audit.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Closing for now: main's page cache (#157) now covers it. With the queue this large we're closing stalled perf PRs that don't show a gain in a title; reopen it with before/after numbers from a title scene (the summary lines from the performance report form) if you pick it up again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* libc needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants