feat(agc): report performance timer scopes to Tracy with APS5_ENABLE_TRACY #4607

Merged
EagleBearerYS merged 2 commits from upstream-pr/4607 into main 2026-10-12 21:09:21 +00:00
EagleBearerYS commented 2026-10-12 16:41:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-12T16:41:25Z.


What

Item 7 of #4477: Tracy as an optional build flag for deep dives, compiled out when off. It complements the trace capture of #4526 with the same PerformanceTimer scopes, but live in the Tracy profiler, unbounded in time, with its statistics, histograms and frame view.

  • Build. -DAPS5_ENABLE_TRACY=ON (default OFF) builds the Tracy 0.14.1 client from the new 3rdparty/tracy submodule as a shared library, so libSceAgcDriver and libSceVideoOut feed one profiler instance. It is written next to the .prx files (libs/unpatched and libs: libTracyClient.dll, libTracyClient.so.0.14.1 on Linux) and is not NID-patched; the patcher only rewrites imports that start with sce or end in _nid_postfix. Without the flag the submodule is not configured and TracyZone is an empty [[no_unique_address]] member, so the libraries compile to the same code as main (checked below).
  • Zones. PerformanceTimer opens a zone named after its scope and closes it in Finish, the same span as its FrameTiming metric: all 41 scopes, Driver.WorkerWait, Driver.Packet, Graphics.Draw, Resources.Prepare, Recorder.Submit, Vulkan.QueuePresent, Shader.Register, VideoOut.Flip and the rest. Source locations are interned per scope name in a fixed table (a lock only on a name's first use), so a zone is two queue items and no allocation, also per packet.
  • Frames and threads. Driver::Present ends a frame after it releases the flip. The queue workers and the presenter name their threads with the roles PinWorkerThread already gets (queue worker 0x0, presenter); guest threads keep the names libkernel gives them.
  • Client settings. TRACY_ON_DEMAND: nothing is recorded or buffered until a profiler connects. TRACY_ONLY_LOCALHOST. No sampling, system tracing, call stacks or code transfer, so a capture holds no code, module paths or other processes of the machine. TRACY_NO_CRASH_HANDLER, because Tracy's handler would take the access violations the emulator handles itself. rocprofiler-sdk is not searched on the system.
  • Windows. MinGW's emulated TLS frees thread_local objects before their destructors run (#4029). Tracy keeps each thread's queue producer in a thread_local moodycamel::ProducerToken: its destructor would read the producer from freed memory and write through it, and the queue's destructor writes into the token. On MinGW the client is therefore linked with a no-op __cxa_thread_atexit (cmake/TracyThreadExit.cpp) and built with TRACY_DELAYED_INIT: no thread_local destructor of the client runs, and its queue lives in a heap object that is never destroyed, so nothing touches a dead token. A thread that emitted a zone and ended keeps its event blocks (2 MiB each) until the process exits. The client's WINVER and _WIN32_WINNT stay private to it.
  • Documented in docs/dev/BUILD.md (Tracy).

On top of #4526. Both hook PerformanceTimer, as #4477 planned ("the same zones would feed both outputs"): Finish first closes the Tracy zone, then writes #4526's trace event and the FrameTiming metrics.

Tested

Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, RTX 3070 Laptop (NVIDIA 617.42). Rebased onto main 3e0e01f35 (#4526 merged); the capture table below is from main 2a5eca11c, before #4526, and 33ae282e4 gave the same results.

  • Title capture. Unpacking (PPSA07117) at its menu, built with -DAPS5_ENABLE_TRACY=ON, with Tracy 0.14.1's tracy-capture attached after 35 s for 20 s: 1,174 frames in 20.16 s (one Driver.Present and one VideoOut.Flip each) and 1,095,198 zones of 25 scopes from both libraries, in a 5 MB trace. Largest totals from tracy-csvexport:

    Zone Count Total ms Mean us
    Driver.WorkerWait 2,341 13,065 5,580.8
    VideoOut.Flip 1,170 11,771 10,060.9
    Driver.Present 1,170 11,630 9,940.5
    Vulkan.Retire 2,340 11,041 4,718.3
    Driver.Packet 693,040 6,831 9.9
    Driver.Draw 42,139 5,482 130.1
    Graphics.Draw 37,839 4,150 109.7
    Recorder.Submit 9,588 519 54.2
    Shader.ResolveGraphicsStagesAbi 42,156 454 10.8
    Vulkan.QueuePresent 1,170 277 236.5
    Recorder.Reap 107,448 258 2.4

    The game's driver threads are named queue worker 0x0 and presenter (read back with GetThreadDescription, which Tracy's SetThreadName also sets). Closing the window ends the game with exit code 0, also when it is closed while the profiler is connected; that capture (688 frames, 641,520 zones) ends by itself when the client goes away.

  • With #4526. On 3e0e01f35 the 19 tests below and agc_performance_trace pass. One Unpacking run with APS5_TRACE_FRAMES=1800:600 and tracy-capture attached produced both outputs: #4526's aps5-trace-*.json.gz and summary line (600 presents, frame_ms p50 17.3 / p90 19.1 / max 21.5, trace_dropped false) and a Tracy capture (1,146 frames, 1,069,578 zones in 20.1 s).

  • Driver threads ending while connected. With tracy-capture attached, video_out_flip_lifetime, _reopen, _controls, _overlay, _unopened, agc_driver_draw_cache_reuse, agc_driver_draw_cache_reuse_off and agc_driver_dword_pattern_fill pass; each records its frames and zones, and its queue workers and presenter end when the driver stops.

  • Tests with the flag on: patched_libraries, host_thread_local, prx_thread_local_destructors, agc_profile_output, agc_driver_pm4, agc_driver_pm4_failure, agc_driver_draw_cache_reuse, agc_driver_draw_cache_reuse_off, agc_driver_dword_pattern_fill, texture_format_lookup, linear_display_buffer, guest_agc_driver_resources, guest_rendering_wait and the six video_out_flip_* (with ANYPS5_VIDEO_OUT_TEST_DRIVER=windows; the offscreen driver skips them on Windows): 19 of 19 pass.

  • Flag off. With APS5_ENABLE_TRACY=OFF the objects of Presentation.cpp, WorkerAffinity.cpp, PacketExecution.cpp (the per-packet timer) and libSceVideoOut's VideoOutDriver.cpp are byte-identical to the ones built from main's sources.

  • MinGW thread_local destructors. A DLL thread_local whose destructor runs HeapValidate on its own emulated-TLS block, used by three std::threads of an exe: with this toolchain each thread exit runs the destructor on a freed block; with the no-op __cxa_thread_atexit linked into the DLL none runs, and __mingw_cxa_thread_atexit is no longer linked. In libTracyClient.dll the only registration, the per-thread data of GetProfilerThreadData, tail-calls the no-op.

  • Linux (Ubuntu 24.04 in WSL2, GCC 13.3, lavapipe, with CI's configure flags and test environment; on a21f9b73f, before the rebase). With -DAPS5_ENABLE_TRACY=ON the same tests pass: 13 of 18, with the five video_out_flip_* that need a window skipped as in CI; prx_thread_local_destructors is MinGW-only. libTracyClient.so.0.14.1 is in libs and libs/unpatched, and libSceAgcDriver and libSceVideoOut list it as NEEDED with RUNPATH $ORIGIN. Tracy 0.14.1's Linux tracy-capture records agc_driver_draw_cache_reuse (3 frames, 105 zones) and agc_driver_dword_pattern_fill (3 frames, 149 zones), the same counts as on Windows. The default build, with the flag off, passes the same set.

  • Conventions: tools/check_conventions.py --base origin/main passes. Replaying the overlap check lists only #4526.

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)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4832352235 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4607). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-12T16:41:25Z. --- ### What Item 7 of #4477: [Tracy](https://github.com/wolfpld/tracy) as an optional build flag for deep dives, compiled out when off. It complements the trace capture of #4526 with the same `PerformanceTimer` scopes, but live in the Tracy profiler, unbounded in time, with its statistics, histograms and frame view. - **Build.** `-DAPS5_ENABLE_TRACY=ON` (default `OFF`) builds the Tracy 0.14.1 client from the new `3rdparty/tracy` submodule as a shared library, so libSceAgcDriver and libSceVideoOut feed one profiler instance. It is written next to the `.prx` files (`libs/unpatched` and `libs`: `libTracyClient.dll`, `libTracyClient.so.0.14.1` on Linux) and is not NID-patched; the patcher only rewrites imports that start with `sce` or end in `_nid_postfix`. Without the flag the submodule is not configured and `TracyZone` is an empty `[[no_unique_address]]` member, so the libraries compile to the same code as `main` (checked below). - **Zones.** `PerformanceTimer` opens a zone named after its scope and closes it in `Finish`, the same span as its `FrameTiming` metric: all 41 scopes, `Driver.WorkerWait`, `Driver.Packet`, `Graphics.Draw`, `Resources.Prepare`, `Recorder.Submit`, `Vulkan.QueuePresent`, `Shader.Register`, `VideoOut.Flip` and the rest. Source locations are interned per scope name in a fixed table (a lock only on a name's first use), so a zone is two queue items and no allocation, also per packet. - **Frames and threads.** `Driver::Present` ends a frame after it releases the flip. The queue workers and the presenter name their threads with the roles `PinWorkerThread` already gets (`queue worker 0x0`, `presenter`); guest threads keep the names libkernel gives them. - **Client settings.** `TRACY_ON_DEMAND`: nothing is recorded or buffered until a profiler connects. `TRACY_ONLY_LOCALHOST`. No sampling, system tracing, call stacks or code transfer, so a capture holds no code, module paths or other processes of the machine. `TRACY_NO_CRASH_HANDLER`, because Tracy's handler would take the access violations the emulator handles itself. `rocprofiler-sdk` is not searched on the system. - **Windows.** MinGW's emulated TLS frees `thread_local` objects before their destructors run (#4029). Tracy keeps each thread's queue producer in a `thread_local` `moodycamel::ProducerToken`: its destructor would read the producer from freed memory and write through it, and the queue's destructor writes into the token. On MinGW the client is therefore linked with a no-op `__cxa_thread_atexit` (`cmake/TracyThreadExit.cpp`) and built with `TRACY_DELAYED_INIT`: no `thread_local` destructor of the client runs, and its queue lives in a heap object that is never destroyed, so nothing touches a dead token. A thread that emitted a zone and ended keeps its event blocks (2 MiB each) until the process exits. The client's `WINVER` and `_WIN32_WINNT` stay private to it. - Documented in `docs/dev/BUILD.md` (Tracy). **On top of #4526.** Both hook `PerformanceTimer`, as #4477 planned ("the same zones would feed both outputs"): `Finish` first closes the Tracy zone, then writes #4526's trace event and the `FrameTiming` metrics. ### Tested Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, RTX 3070 Laptop (NVIDIA 617.42). Rebased onto `main` 3e0e01f35 (#4526 merged); the capture table below is from `main` 2a5eca11c, before #4526, and 33ae282e4 gave the same results. - **Title capture.** Unpacking (PPSA07117) at its menu, built with `-DAPS5_ENABLE_TRACY=ON`, with Tracy 0.14.1's `tracy-capture` attached after 35 s for 20 s: 1,174 frames in 20.16 s (one `Driver.Present` and one `VideoOut.Flip` each) and 1,095,198 zones of 25 scopes from both libraries, in a 5 MB trace. Largest totals from `tracy-csvexport`: | Zone | Count | Total ms | Mean us | |---|---:|---:|---:| | `Driver.WorkerWait` | 2,341 | 13,065 | 5,580.8 | | `VideoOut.Flip` | 1,170 | 11,771 | 10,060.9 | | `Driver.Present` | 1,170 | 11,630 | 9,940.5 | | `Vulkan.Retire` | 2,340 | 11,041 | 4,718.3 | | `Driver.Packet` | 693,040 | 6,831 | 9.9 | | `Driver.Draw` | 42,139 | 5,482 | 130.1 | | `Graphics.Draw` | 37,839 | 4,150 | 109.7 | | `Recorder.Submit` | 9,588 | 519 | 54.2 | | `Shader.ResolveGraphicsStagesAbi` | 42,156 | 454 | 10.8 | | `Vulkan.QueuePresent` | 1,170 | 277 | 236.5 | | `Recorder.Reap` | 107,448 | 258 | 2.4 | The game's driver threads are named `queue worker 0x0` and `presenter` (read back with `GetThreadDescription`, which Tracy's `SetThreadName` also sets). Closing the window ends the game with exit code 0, also when it is closed while the profiler is connected; that capture (688 frames, 641,520 zones) ends by itself when the client goes away. - **With #4526.** On 3e0e01f35 the 19 tests below and `agc_performance_trace` pass. One Unpacking run with `APS5_TRACE_FRAMES=1800:600` and `tracy-capture` attached produced both outputs: #4526's `aps5-trace-*.json.gz` and summary line (600 presents, `frame_ms` p50 17.3 / p90 19.1 / max 21.5, `trace_dropped` false) and a Tracy capture (1,146 frames, 1,069,578 zones in 20.1 s). - **Driver threads ending while connected.** With `tracy-capture` attached, `video_out_flip_lifetime`, `_reopen`, `_controls`, `_overlay`, `_unopened`, `agc_driver_draw_cache_reuse`, `agc_driver_draw_cache_reuse_off` and `agc_driver_dword_pattern_fill` pass; each records its frames and zones, and its queue workers and presenter end when the driver stops. - **Tests with the flag on:** `patched_libraries`, `host_thread_local`, `prx_thread_local_destructors`, `agc_profile_output`, `agc_driver_pm4`, `agc_driver_pm4_failure`, `agc_driver_draw_cache_reuse`, `agc_driver_draw_cache_reuse_off`, `agc_driver_dword_pattern_fill`, `texture_format_lookup`, `linear_display_buffer`, `guest_agc_driver_resources`, `guest_rendering_wait` and the six `video_out_flip_*` (with `ANYPS5_VIDEO_OUT_TEST_DRIVER=windows`; the offscreen driver skips them on Windows): 19 of 19 pass. - **Flag off.** With `APS5_ENABLE_TRACY=OFF` the objects of `Presentation.cpp`, `WorkerAffinity.cpp`, `PacketExecution.cpp` (the per-packet timer) and libSceVideoOut's `VideoOutDriver.cpp` are byte-identical to the ones built from `main`'s sources. - **MinGW `thread_local` destructors.** A DLL `thread_local` whose destructor runs `HeapValidate` on its own emulated-TLS block, used by three `std::thread`s of an exe: with this toolchain each thread exit runs the destructor on a freed block; with the no-op `__cxa_thread_atexit` linked into the DLL none runs, and `__mingw_cxa_thread_atexit` is no longer linked. In `libTracyClient.dll` the only registration, the per-thread data of `GetProfilerThreadData`, tail-calls the no-op. - **Linux** (Ubuntu 24.04 in WSL2, GCC 13.3, lavapipe, with CI's configure flags and test environment; on a21f9b73f, before the rebase). With `-DAPS5_ENABLE_TRACY=ON` the same tests pass: 13 of 18, with the five `video_out_flip_*` that need a window skipped as in CI; `prx_thread_local_destructors` is MinGW-only. `libTracyClient.so.0.14.1` is in `libs` and `libs/unpatched`, and libSceAgcDriver and libSceVideoOut list it as `NEEDED` with `RUNPATH $ORIGIN`. Tracy 0.14.1's Linux `tracy-capture` records `agc_driver_draw_cache_reuse` (3 frames, 105 zones) and `agc_driver_dword_pattern_fill` (3 frames, 149 zones), the same counts as on Windows. The default build, with the flag off, passes the same set. - **Conventions:** `tools/check_conventions.py --base origin/main` passes. Replaying the overlap check lists only #4526. ### 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] Unblocks: none - [x] AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-12 21:09:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-12T21:09:18Z.


Reviewed and tested on Linux, now that #4526, #4632 and #4581 are on main:

  • 3rdparty/tracy is at 30997d5ca, the v0.14.1 tag of wolfpld/tracy.
  • Linux (GCC 16.2, RADV), -DAPS5_ENABLE_TRACY=ON:
    • libTracyClient.so.0.14.1 is in libs and libs/unpatched.
    • libSceAgcDriver.prx and libSceVideoOut.prx list it as NEEDED with RUNPATH $ORIGIN.
    • 23 of 23 tests pass: patched_libraries, host_thread_local, agc_profile_output, agc_driver_pm4*, draw_cache_reuse*, dword_pattern_fill, agc_performance_trace, flip/present_mode and the rest. The five window video_out_flip_* are skipped with the offscreen driver, as in CI.
  • Trace and Tracy in the same build: APS5_TRACE_FRAMES still writes its trace and summary line, since both outputs hang off PerformanceTimer. One aftergpu run under the validation layer failed its timing check once ("GPU work landed at 0.0 ms"). It didn't happen again in 10 more runs (with and without the trace, with a cold shader cache), and the build without Tracy behaves the same, so I'm putting it down to load on this laptop.
  • Your branch merges cleanly with current main. The full build workflow is green on Linux and Windows (run 38305815614).

Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132649583 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4607#issuecomment-6132649583). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-12T21:09:18Z. --- Reviewed and tested on Linux, now that #4526, #4632 and #4581 are on main: - `3rdparty/tracy` is at 30997d5ca, the `v0.14.1` tag of wolfpld/tracy. - Linux (GCC 16.2, RADV), `-DAPS5_ENABLE_TRACY=ON`: - `libTracyClient.so.0.14.1` is in `libs` and `libs/unpatched`. - `libSceAgcDriver.prx` and `libSceVideoOut.prx` list it as `NEEDED` with `RUNPATH $ORIGIN`. - 23 of 23 tests pass: patched_libraries, host_thread_local, agc_profile_output, agc_driver_pm4*, draw_cache_reuse*, dword_pattern_fill, agc_performance_trace, flip/present_mode and the rest. The five window `video_out_flip_*` are skipped with the offscreen driver, as in CI. - **Trace and Tracy in the same build**: `APS5_TRACE_FRAMES` still writes its trace and summary line, since both outputs hang off `PerformanceTimer`. One `aftergpu` run under the validation layer failed its timing check once ("GPU work landed at 0.0 ms"). It didn't happen again in 10 more runs (with and without the trace, with a cold shader cache), and the build without Tracy behaves the same, so I'm putting it down to load on this laptop. - Your branch merges cleanly with current `main`. The full build workflow is green on Linux and Windows (run 38305815614). 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!4607
No description provided.