Skip to content

perf(agc): answer verifications of the caller's own live stack without a page query - #459

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:perf/agc-own-stack-verify
Oct 3, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:perf/agc-own-stack-verify

Conversation

@oneandonlydean

Copy link
Copy Markdown
Collaborator

What

Driver::Submit checks the title's submission packet with GuestMemory::CheckRange, and titles pass that packet on their own thread stack. Host thread stacks are neither registry mappings nor write-watched, so no cached page table covers them, and on Linux every such check scans /proc/self/maps line by line. In PPSA21564 that was about 1.5 ms per submission on the render thread, 66% of that thread's samples in perf at the title screen.

A range between the calling thread's current frame and the top of its stack holds the frames of every caller, so it is mapped readable and writable for as long as the call lasts. verify() now accepts such a range without a page query:

  • the thread's stack bounds come from pthread_getattr_np (Linux) or GetCurrentThreadStackLimits (Windows), once per thread;
  • the current frame is the address of a local in the check;
  • a thread running on another stack (a fiber's, a signal stack) has its frame outside the thread's bounds and still gets the page query, as does any range that starts below the frame or runs past the stack's top.

GetCurrentThreadStackLimits needs _WIN32_WINNT 0x0602 on MinGW, as libkernel already sets. Several targets compile GuestMemory.cpp, so the definition is a property of that source file.

#157 caches the parsed maps for these queries; if it lands, this saves less, and I will measure it again then. #157 already conflicts with main in GuestMemory.cpp.

Tested

  • Linux (CachyOS, x86-64): full build with BUILD_TESTING=ON and ctest pass (145/145).
  • New RunLiveStackAccessTests in agc_driver_graphics_tests (tests/LiveStack.cpp): a packet on the test's own stack is accessible and passes CheckRange; a thread on a stack of its own with an inaccessible page above and one at the bottom sees its stack top accessible, a range past the top and the page above it rejected (Accessible and CheckRange), and the inaccessible page below its frames rejected. After the thread ends, that memory is reported as the page query says. The checks bound the new path: dropping the past-the-top bound fails with "a range past the stack's top counts as accessible", and starting the accepted range at the stack's bottom instead of the frame fails with "an inaccessible page of the stack below the live frames counts as accessible". Without the change the test passes too, since the answers are the same; only the cost differs.
  • Cost of one 64-byte CheckRange on the stack, in the test process (a short maps file): 12.3 us before, 0.01 us after. The title's maps file has thousands of lines (mostly driver mappings), hence the 1.5 ms above.
  • PPSA21564: on my integration branch since 2026-09-30 through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS).
  • My fix(agc): describe the pages after a gap in a Linux host page query #442 also edits GuestMemory.cpp, BdaTests.hpp and tests/Graphics.cpp; the two merge cleanly in either order.

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

…t a page query

Driver::Submit checks the title's submission packet, which the title
passes on its own thread stack. Host thread stacks are neither registry
mappings nor write-watched, so no page-state cache covers them and on
Linux every check scans /proc/self/maps line by line: about 1.5 ms per
submission on the render thread of PPSA21564 (66% of that thread's
samples in perf at the title).

A range between the calling thread's current frame and its stack's top
holds the frames of every caller, so it is mapped readable and writable
for as long as the call lasts; verify() now accepts it without a query.
A thread on another stack (a fiber's, a signal stack) has its frame
outside the thread's bounds and still gets the page query, as does any
range that runs past the stack's top or starts below the frame.

GetCurrentThreadStackLimits needs _WIN32_WINNT 0x0602 on MinGW, as
libkernel already sets; GuestMemory.cpp is compiled by several targets,
so the definition is a property of the source file.
@oneandonlydean
oneandonlydean force-pushed the perf/agc-own-stack-verify branch from 43f31ce to 5a81652 Compare October 3, 2026 14:02
@github-actions github-actions Bot added build agc libSceAgc* labels Oct 3, 2026
@oneandonlydean
oneandonlydean merged commit 53d1ebc into boykopovar:main Oct 3, 2026
3 checks passed
@oneandonlydean
oneandonlydean deleted the perf/agc-own-stack-verify branch October 3, 2026 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant