Skip to content

feat(agc): write the worker sampler's stacks as collapsed stacks - #4570

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
AlxcBTW:feat/sampler-collapsed
Oct 12, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
AlxcBTW:feat/sampler-collapsed

Conversation

@AlxcBTW

@AlxcBTW AlxcBTW commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Step 2 of #4477. The worker sampler (APS5_SAMPLE_WORKER) also writes the queue worker's whole stacks as collapsed stacks, which speedscope.app and flamegraph.pl open as a flame graph. Its walk now gets through the libraries' DWARF-only frames, so most stacks reach the thread's root. The perf template that asks for a trace and a flame graph is a follow-up.

What

  • Collapsed stacks. Next to <file>, every 20 s and at exit, the sampler writes <file stem>.collapsed.txt: one frame;...;frame count line per distinct stack of queue worker 0, root first, each frame as module+0xRVA. A walk that ends before the thread's root starts with [truncated]. # lines hold the sample count, the walk depth, the truncated walks, the mean and longest time the worker was suspended, and each module's name, size and PE timestamp (no paths), so a file can be checked against the binary it is symbolized with. The file is replaced through a temporary file, so a reader never sees a partial one. <file> keeps its format.
  • DWARF frames. The libraries are built with -fno-asynchronous-unwind-tables, so most of their functions have no .pdata entry, only a DWARF record in .ehfram. RtlLookupFunctionEntry finds nothing there, and the walk ended at the first such frame. The worker's walk now steps such a frame through its record: CFA rule, return address and the saved registers. A leaf without either kind of record takes its return address from rsp if that address has unwind data.
  • Safety while the worker is suspended. No allocation and no lock: the frames go into a fixed array, and every register the step restores is read inside the worker's stack (bounds from its TIB). A module the walk meets for the first time ends that walk; its .ehfram is indexed after the worker resumes. Walks go up to 64 frames. The process sampler (APS5_SAMPLE_THREADS) keeps its 12 frames through .pdata only.
  • Reuse. The CIE/FDE decoding (DecodeRange) and the call frame instruction interpreter (Instructions) are libc's, used by its _Unwind_* functions. They move unchanged from Unwind.cpp into exceptions/Unwind.hpp as inline functions. The sampler doesn't call libc's Step, because Step indexes modules lazily under a mutex and reads saved registers anywhere in memory, and neither is allowed while the worker is suspended. StackWalk.hpp holds the per-module FDE index, the step and the collapsed-stack formatting, and doesn't depend on Windows.
  • Documented in docs/dev/BUILD.md (Worker sampler, which also lists APS5_SAMPLE_THREADS).

Tested

Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release.

  • agc_stack_walk (new): a synthetic .eh_frame (a zR CIE with a pc-relative FDE for a push rbp; mov rbp, rsp prologue) and a stack array. It steps at the function's entry, after the push, and in the body through the frame pointer. A pc outside the record gives no record, a read outside the stack is refused with the registers unchanged, and MinGW's return column 32 is read (Windows; elsewhere the CIE is not indexed). It also checks the collapsed-stack text: merged stacks, root first, and [truncated]. It passes on Windows and, built alone, on Linux with GCC 13.3.
  • Non-GPU suite (Lenovo node, ctest -E "^agc_driver|^video_out|^guest_video"): 618 of 619 pass, including agc_stack_walk, windows_exceptions, guest_exception_handler, guest_raise_exception, guest_unwind_module_info, unwind_record_bounds and prx_thread_local_destructors. log_macros fails as on main on that machine. libc's Unwind.cpp, Personality.cpp and ExceptionPersonality.cpp also compile without warnings on Linux (GCC 13.3, syntax check).
  • Sword of the Sea (PPSA25344), main menu, 150 s: 8,966 samples in 3,319 distinct stacks, and 99.9 % of the walks reach the thread's root (10 are [truncated]). Depth is 18.8 frames on average and 48 at most. The worker was suspended 102 us per sample on average. Before this change, no walk on the same menu reached the root. Details are in a comment below.
  • Conventions: tools/check_conventions.py --base origin/main passes.

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
  • Unblocks: none
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

@AlxcBTW

AlxcBTW commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Capture from a title run: Sword of the Sea (PPSA25344), main menu, RTX 5070 Laptop (NVIDIA 616.92), Windows 11 26200, 150 s. The build is this PR's commit on our lab branch (with #4526), which carries the local patches the title still needs. Our lab branch also had an older local version of this sampler, which was reverted for this build. APS5_SAMPLE_WORKER=<dir>\worker.txt.

  • Files: worker.txt (136 KB, the existing format) and worker.collapsed.txt (1.5 MB): 8,966 samples in 3,319 distinct stacks, 14 # module lines with base names only. The lab stopped the title at the timeout, so these are from the last 20 s write.
  • Walks: 8,956 of 8,966 (99.9 %) reach the thread's root (ntdll.dll, through libSceAgcDriver.prx, libstdc++-6.dll, ucrtbase.dll and KERNEL32.DLL), and 10 are [truncated]. Depth is 18.8 frames on average and 48 at most, so none hit the 64-frame cap. Before this change, the walk ended at the first frame that has only a DWARF record: in an earlier run of the same menu on our lab branch, no walk reached the root, and the largest inclusive count was 2,804 of 10,248 samples.
  • Cost: the worker was suspended 101.8 us per sample on average (suspended_us_mean). The longest suspension was 16.3 ms (suspended_us_max).
  • Format: a script checked every line other than #: frame;...;frame count, each frame module+0x<hex>, with [truncated] only as the first frame. That is the line format speedscope's "collapsed stack" import and flamegraph.pl read; both skip the # lines. The frames are unsymbolized RVAs, as in worker.txt.

The sampler output itself is not attached because it comes from a title run.

AI-assisted: yes (Claude Code)

@AlxcBTW
AlxcBTW marked this pull request as ready for review October 12, 2026 16:07
@Scramblrrrr

Copy link
Copy Markdown
Contributor

Preparing a007 Windows diagnostic cross-test of7628167beb7d2b05f1c4300d157c03149b0f70b1. Applied cleanly on our current integration, source-only308d7a09d02380489422b5c6df7ff40527d846c7: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-worker-stacks-cross-test . Conventions pass. Will freshly build stack-walk plus relevant Windows unwind/exception tests before staging. Current game is still on older9e54939c9 graphics with sampling OFF; no runtime verification claim for4570 yet. The earlier copying hot samples lacked reliable callers, so DWARF walks should help distinguish costs. Profiles/title data stay local; public results will be aggregate native hot-path metadata. AI-assisted: Codex.

@Scramblrrrr

Copy link
Copy Markdown
Contributor

Windows diagnostic preparation completed on head 4a4fc98: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-dispatch-provenance-cross-test . Carries credited #4570 (canonical 7628167) on the #4586 integration, plus opt-in failure-only dispatch provenance. Original Vulkan argument address/CPU reason and failed indirect command argument address/initiator/queue/opcode are reported; validation unchanged.

Fresh focused checks: 8/8 passed in 17.50 s (stack walk, Windows exceptions, guest handler/raise/unwind module info, oversized GPU dispatch, indirect argument ordering, WRITE_DATA ordering), no skips. Final driver-trace follow-up: 3/3 GPU dispatch checks passed in 1.25 s. The oversized fixture emitted direct-input provenance and still rejected the invalid dimensions. Conventions pass. exception_runtime_tests is not an available target in this Windows configuration and is not included in the count.

Patched graphics SHA256 0044167482FC6932E575001A44E2E40B7557D4F8AA55270C13531918AB56AF82; patched libc 7540B4D811D1F1EB5C534A3DD29109319042B0DA592A0F15D114CF83F34D74F1. Preparing a sampled diagnostic007 repeat with 12 GiB import budget; not a quiet FPS comparison. Current title runs and raw profiles remain local; no runtime stack-walk result yet. AI-assisted: Codex.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Conflicts with main in core/libs/prx/libc/src/exception/Unwind.cpp since #4511 went in: it changed the CIE/FDE decoding this PR moves into the header, so the moved code needs #4511's change applied by hand. Please rebase; the full build was green on 7628167, and I'll rebuild and merge once the new head is up. #4526 goes in after it.

APS5_SAMPLE_WORKER=<file> now also writes <file stem>.collapsed.txt: one
"frame;...;frame count" line per distinct stack of queue worker 0, root
first, frames as module+0xRVA, which speedscope.app and flamegraph.pl
open. Walks go up to 64 frames, and nothing allocates while the worker is
suspended.

The libraries are built with -fno-asynchronous-unwind-tables, so most of
their functions have no .pdata entry, only a DWARF record in .ehfram, and a
walk ended at the first such frame. The worker's walk now steps those
frames through their record, with the CIE/FDE decoding and the call frame
instructions of libc's unwinder, which move from Unwind.cpp into its
header unchanged. A module is indexed after the worker resumes, the first
time a walk meets it, and every register the step restores is read inside
the worker's stack. The process sampler (APS5_SAMPLE_THREADS) keeps its 12
frames through .pdata.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the feat/sampler-collapsed branch from 7628167 to 428963d Compare October 12, 2026 19:30
@adriadam10

Copy link
Copy Markdown
Collaborator

Rebased onto main (428963d). The only conflict was #4511's change to Unwind.cpp: its two new Frame fields (foreign, caller, Windows only) now sit in the struct this PR moves into exceptions/Unwind.hpp; the rest of Unwind.cpp keeps #4511's code as on main. Checked on Linux (GCC 16.2, Release): full build, and the unwind/exception tests pass with agc_stack_walk (13/13). Full Linux + Windows build dispatched for the new head.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@adriadam10
adriadam10 merged commit cf4557e into boykopovar:main Oct 12, 2026
14 checks passed
@AlxcBTW
AlxcBTW deleted the feat/sampler-collapsed branch October 12, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants