Skip to content

feat(agc): record a present window as a Chrome trace and a summary line - #4526

Merged
adriadam10 merged 4 commits into
boykopovar:mainfrom
AlxcBTW:feat/trace-capture
Oct 12, 2026
Merged

adriadam10 merged 4 commits into
boykopovar:mainfrom
AlxcBTW:feat/trace-capture

Conversation

@AlxcBTW

@AlxcBTW AlxcBTW commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Step 1 of #4477. A window of presents is recorded as a Chrome trace-event file plus one summary line, so perf numbers in issues and PRs come from the same capture. The GPU track, the compile threads and the perf templates are follow-ups.

What

  • Switch. APS5_TRACE_FRAMES=<start>:<count> records <count> presents from present <start> (counted from 0). APS5_TRACE_SECONDS=<start>:<length> records from the first present at least <start> s after the first present until the first present <length> s later. The window opens and closes at a present. A malformed value, or both switches together, fails the first present.
  • Events. PerformanceTimer already brackets the driver's phases with the FrameTiming keys. While the window is open, a timer on a queue worker or on the presenter also records its scope and each Mark stage as a complete event (ph:X): Driver.WorkerWait, Driver.Packet, Driver.Packet.draw, Graphics.Draw, Recorder.Submit, Driver.Present, Driver.Present.inflight_wait, Vulkan.Retire, and so on. Each present is a global present instant with its number. The thread tracks use the existing thread roles (queue worker 0x0, presenter).
  • Buffers. Each queue worker allocates a buffer for 262,144 events (8 MiB) when it starts, the presenter at its first present. Only the owning thread writes it (one release store per event, no lock). A full buffer stops that thread and sets trace_dropped. Every slice is added to a per-thread total by scope and stage; a slice under 20 us is not written, and its time goes to the thread's counter <thread> folded (ms per present).
  • Output. When the window closes, the presenter collects the buffers and formats the JSON once. A ProfileOutput of its own (the existing one writes to stderr) gzips it on its writer thread to aps5-trace-<commit>-<pid>.json.gz and appends the summary line to aps5-summary.jsonl, both in APS5_TRACE_DIR or the working directory. The deflate stream comes from the zlib that libjpeg-turbo already builds from source (the one libSceZlib links), so there is no new submodule. That zlib is built with NO_GZIP, so TraceFile writes the gzip header and the CRC-32 and size trailer itself.
  • Summary. commit, dirty, env (every APS5_*; a value containing / or \ is written as <path>), gpu, driver (VkPhysicalDeviceDriverProperties name and info), os, resolution (the video output's), window, presents, frame_ms {p50, p90, p99, max} of the intervals between presents, gpu_busy_ms_per_present (the batch timestamps behind [present]), worker_busy_pct per queue worker (share of the window in Driver.Packet, Driver.Suspend, Driver.EndSubmission, Driver.Rewind and Driver.Completion, the non-wait part of FrameTiming's worker accounting), top_phases_ms_per_frame (the eight largest scopes and stages, without Driver.WorkerWait), trace_dropped and trace. commit and dirty come from a header that a build step regenerates from git on every build (trace_revision.cmake, rewritten only when it changes), so an incremental build never reports a stale commit.
  • Cost. Without the switches a PerformanceTimer does one relaxed load and one more null check, and nothing is allocated. With a switch set, the recorder writes the batch timestamp pair for the whole run, as with APS5_PROFILE_DRAW, so gpu_busy_ms_per_present needs no profile switch.
  • Documented in docs/dev/BUILD.md (Performance trace).

Not in this step: the GPU track, compile threads, Submission.* and lock-site events, a batches-in-flight counter, the templates. From the questions in #4477: the window defaults to presents, and the summary goes only into aps5-summary.jsonl, not into the trace.

Tested

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

  • agc_performance_trace (new): window edges by frames and by seconds, rejected values, the buffer cap and the dropped flag, folding of short slices and the per-present counter, JSON string escaping, the exact JSON of a small thread, every summary field, a written .json.gz inflated and checked against its CRC-32 and size, and appended summary lines.
  • Non-GPU suite (Lenovo node, ctest -E "^agc_driver|^video_out|^guest_video"): 615 of 616 pass, including agc_performance_trace and prx_thread_local_destructors; log_macros fails as on main on that machine. The BUILD_TESTING build also links agc_driver_mesh_tests and the other targets that compile the driver sources.
  • Sword of the Sea (PPSA25344), main menu, RTX 5070 Laptop (NVIDIA 616.92): APS5_TRACE_FRAMES=120:120 wrote a 2.5 MB trace (26.6 MB of JSON, 284,124 events on queue worker 0x0, queue worker 0x20 and presenter, nothing dropped) and its summary line. Perfetto's trace processor v58.2 (the engine of ui.perfetto.dev) imports it with no errors. Details are in a comment below. The first capture wrote nothing, because the bundled zlib has no gzip wrapper; the second commit fixes that.
  • 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. The build is this PR's two commits on our lab branch, which carries the local patches the title still needs (cf01fcab4 in the summary).

APS5_TRACE_FRAMES=120:120: the window opened at present 120 and closed at present 240, 15.8 s later.

  • File: aps5-trace-<commit>-<pid>.json.gz, 2.5 MB (26.6 MB of JSON).
  • Content: 284,124 events. 283,643 complete events on three thread tracks: queue worker 0x0 250,882, queue worker 0x20 31,110, presenter 1,651. Also 120 present instants, 357 <thread> folded counter samples (119 per thread, one per present) and 4 metadata events. No buffer filled, so trace_dropped is false. Slices nest up to 7 deep, and no two slices on one thread overlap partially.
  • Perfetto: Perfetto's trace processor (trace_processor_shell v58.2, the engine behind ui.perfetto.dev) imports the file with no error in its stats: 283,763 slices including the instants, three thread tracks, and the three counter tracks. A script also checked the JSON: every event has name, ph, ts, pid and tid, and every X event has dur ≥ 0.

The summary line (APS5_TRACE_DIR is a path, so it is written as <path>):

{"commit":"cf01fcab4c2d","dirty":false,"env":{"APS5_HOST_IMPORT_JOIN":"1","APS5_NO_PAD_INPUT":"1","APS5_RESOURCE_CACHE_ENTRIES":"8192","APS5_TRACE_DIR":"<path>","APS5_TRACE_FRAMES":"120:120"},"gpu":"NVIDIA GeForce RTX 5070 Laptop GPU","driver":"NVIDIA 616.92","os":"Windows 10.0.26200","resolution":"3840x2160","window":{"spec":"frames 120:120","first_present":120,"ms":15841.663},"presents":120,"frame_ms":{"p50":129.535,"p90":154.547,"p99":168.741,"max":170.205},"gpu_busy_ms_per_present":78.555,"worker_busy_pct":{"queue worker 0x0":99.423,"queue worker 0x20":79.485},"top_phases_ms_per_frame":{"Driver.Packet":233.912,"Driver.Packet.dispatch":91.290,"Driver.Packet.memory_wait":76.991,"Driver.Dispatch":72.094,"Vulkan.Dispatch":41.527,"Driver.Packet.draw":38.134,"Driver.Draw":38.049,"Vulkan.Dispatch.shader_resources":36.774},"trace_dropped":false,"trace":"aps5-trace-cf01fcab4c2d-60720.json.gz"}

top_phases_ms_per_frame adds up all traced threads, so Driver.Packet (both queue workers) is longer than a frame.

The first capture wrote no file, which is what the second commit fixes. The zlib in the tree (libjpeg-turbo's) is built with NO_GZIP, so deflateInit2 refused the gzip wrapper. The error line was also lost, because the title's stderr is buffered. The trace now writes the gzip header and the CRC-32 and size trailer around a raw deflate stream and flushes its result line, and a test inflates a written file.

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

AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

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
#4607 core/libs/prx/libSceAgcDriver/Execution/include/PerformanceTimer.hpp L3, L276-277, L300-301

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Read through dafae93. The code is fine by reading: malformed values throw at the first present, nothing falls back, and with the variables unset the cost is one relaxed atomic load per PerformanceTimer and a null check in Finish/Mark, no allocation and nothing per draw.

What it brings in is the part that needs a decision, and it is the owner's rather than mine:

  • three new variables, APS5_TRACE_FRAMES, APS5_TRACE_SECONDS and APS5_TRACE_DIR;
  • jpeg_turbo linked into libSceAgcDriver (and three test targets) to reach its bundled zlib, with the gzip framing written by hand because that zlib is built with NO_GZIP;
  • a build step that runs git rev-parse and git status --porcelain on every build, so the driver relinks whenever HEAD or the dirty flag changes;
  • aps5-summary.jsonl, which lists every APS5_* variable of the run;
  • this trace format as the shared one. feat(agc): report performance timer scopes to Tracy with APS5_ENABLE_TRACY #4607 wires the same timers to Tracy, and the two conflict in PerformanceTimer.hpp.

#4477 has no answer from him yet, so I am leaving this open until it does.

Two things in the meantime:

#4570 has no new variable or dependency, so it can go in on its own once its build is green. It conflicts with this one in CMakeLists.txt and docs/dev/BUILD.md, so this one will need a rebase after it.

@oneandonlydean oneandonlydean self-assigned this Oct 12, 2026
@AlxcBTW

AlxcBTW commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

@oneandonlydean thanks for the read. The VulkanDevice.cpp space is fixed in a2c49da; that line is now identical to main, so the hunk only adds the driver name.

Agreed on the order: #4570 first, then I merge main into this one. #4632 (GPU track) and #4590 (perf template) build on it and will follow it.

If it makes the owner's call easier, I can shrink what this brings in:

  • drop the jpeg_turbo link and write plain .json (about 10x larger, but no compression dependency);
  • take commit from an existing build version string instead of the git rev-parse step, or leave it out;
  • keep aps5-summary.jsonl to an allow-list of variables.

AI-assisted: yes (Claude Code)

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Update: this goes in as step 1 of #4477 once the Linux job of the build above is green, the byteFeatures = hunk is dropped and it is rebased over #4570.

@adriadam10

Copy link
Copy Markdown
Collaborator

Reviewed a2c49da on Linux (GCC 16.2, Release):

  • libSceAgcDriver builds; agc_performance_trace, agc_profile_output and agc_driver_mesh pass.
  • The gzip writer checked against readers that don't share its zlib: a 16 MB trace written by TraceFile::WriteGzip against the bundled zlib with NO_GZIP (and an empty one) passes gzip -t, and Python's gzip returns the same bytes.
  • On the points left open above: the jpeg_turbo link reuses the zlib that libSceZlib already links, so nothing new is vendored; the revision step is needed because the tree has no version string yet, and copy_if_different only relinks when HEAD or the dirty flag changes; env is limited to APS5_* and masks paths. Fine with all three as they are.
  • Nit, not blocking: a <thread> folded sample is written on the thread's first slice after a present, so the last present of the window gets none (119 samples for 120 presents in your capture). Its time is still in the totals and the summary; a line in BUILD.md or a flush at close would make the counter match "one per present".

Ready from my side once it is rebased over #4570, as Dean said.

AlxcBTW and others added 4 commits October 12, 2026 21:42
APS5_TRACE_FRAMES=<start>:<count> (or APS5_TRACE_SECONDS=<start>:<length>)
records every PerformanceTimer scope and stage of the queue workers and the
presenter inside the window as complete events, named after their frame
timing keys, into a preallocated buffer per thread. Slices under 20 us are
counted per thread instead of written; a full buffer stops that thread and
sets trace_dropped. When the window closes, a ProfileOutput writer thread
writes aps5-trace-<commit>-<pid>.json.gz and appends one line to
aps5-summary.jsonl. Without either switch a PerformanceTimer costs one more
predictable branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The zlib in the tree (libjpeg-turbo's) is built with NO_GZIP, so
deflateInit2 refuses the gzip wrapper (windowBits 31) and no trace was
written. The trace file now writes the gzip header, a raw deflate stream,
and the CRC-32 and size trailer itself. The writer's result line is flushed,
since stderr is buffered in a title process. The file writing moves to
TraceFile, and a test inflates a written file and checks its CRC-32 and
size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10

Copy link
Copy Markdown
Collaborator

#4570 is merged. I rebased this on top of it: the conflicts in libSceAgcDriver's CMakeLists.txt and docs/dev/BUILD.md keep both sides (agc_stack_walk next to agc_performance_trace, and "Worker sampler" followed by "Performance trace"). I also added 6116fc7, one sentence in BUILD.md about the nit above: the last present of a window has no <thread> folded sample.

Linux (GCC 16.2), full build: agc_performance_trace, agc_stack_walk, agc_profile_output, agc_driver_mesh and the unwind/exception tests all pass (12/12). Running the full build workflow now, and I'll merge once it's green.

@adriadam10
adriadam10 merged commit 3e0e01f into boykopovar:main Oct 12, 2026
8 checks passed
@AlxcBTW
AlxcBTW deleted the feat/trace-capture branch October 12, 2026 20:15
@TideOPL

TideOPL commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

@AlxcBTW I'd rather the profiler just runs in the background all the time so I can have a game running for a prolonged period, gather data and iterate from there.

@AlxcBTW

AlxcBTW commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

@TideOPL makes sense. A long session is where the interesting stalls show up. The window is bounded today because every slice is buffered (262,144 events per thread). I'd add two modes in a follow-up PR:

  • APS5_TRACE_SUMMARY_EVERY=<seconds>: no per-event buffer, only the folded per-scope totals. Every N seconds it appends one line to aps5-summary.jsonl (frame p50/p90/p99, GPU busy, worker busy %, top phases for that interval). It's cheap enough to leave on for hours, and trace_digest.py would plot the lines over time.
  • APS5_TRACE_EVERY=<period>:<length>[:<keep>]: repeats the full Chrome-trace window, e.g. 10 s every 5 min, and keeps the last <keep> trace files. That way, when something goes wrong 40 minutes in, a recent full trace is on disk.

Would that cover what you want, or do you need a full trace of the whole session? That would need a streaming writer instead of the in-memory buffer.

AI-assisted: yes (Claude Code)

@TideOPL

TideOPL commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

@AlxcBTW, these are good. I would say a third option for streaming is ideal, looking long term when games stay up for longer than 1 - 2 hours, long traces could be great.

@AlxcBTW

AlxcBTW commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

@TideOPL agreed. I'll do it in three small PRs, in this order:

  1. APS5_TRACE_SUMMARY_EVERY: interval summary lines, cheap enough to leave on. It's in progress.
  2. APS5_TRACE_EVERY: repeated full windows with rotation.
  3. Streaming: each thread's buffer is flushed in chunks to a writer thread, which appends gzip members to a rolling set of trace files with a disk cap. That way a 2-hour session stays bounded and the last N minutes are always complete. Perfetto reads concatenated gzip JSON fine, and if the files get too large, Perfetto's protobuf format is the next step.

I'll tag you on each.

AI-assisted: yes (Claude Code)

@AlxcBTW

AlxcBTW commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

@TideOPL step 1 is up: #4812 (APS5_TRACE_SUMMARY_EVERY=<seconds>). It writes one summary line per interval, has no event buffer and can stay on for hours. trace_digest.py aps5-summary.jsonl prints a row per interval with p99/GPU sparklines. Repeated windows and streaming follow as their own PRs.

AI-assisted: yes (Claude Code)

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants