Skip to content

feat(agc): add the GPU, GPU mutex waits and shader preparation to the trace - #4632

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
AlxcBTW:feat/trace-gpu-track
Oct 12, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
AlxcBTW:feat/trace-gpu-track

Conversation

@AlxcBTW

@AlxcBTW AlxcBTW commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Step 3 of #4477, on top of #4526. The trace gets a gpu track (a slice per recorder batch, and with APS5_PROFILE_GPU=1 a slice per command class range), slices for waits on the GPU mutex, and the shader preparation threads. A frame that is GPU-bound then shows as the GPU busy while the presenter waits.

What

  • GPU track. Recorder::readGpuTiming hands each range it reads to PerformanceTrace::GpuRange while a window is open. That is the batch's timestamp pair, which a trace switch already turns on (feat(agc): record a present window as a Chrome trace and a summary line #4526), and, with APS5_PROFILE_GPU=1, each command class range under its [gputime] name: draw, copy, fill, storage-upload, present-blit, and so on. Program ranges are left out. The ranges go into a buffer of 262,144 entries under a mutex of their own (reserved when the window opens, so appending never allocates); past that, trace_dropped is set. Outside a window GpuRange is one relaxed load.
  • Clock. GPU timestamps are mapped onto the CPU clock with calibrated timestamps. VK_KHR_calibrated_timestamps, or the EXT version, is enabled only when a trace switch is set, and only if the device offers the device domain and the host domain the CPU clock uses: QPC on Windows, CLOCK_MONOTONIC elsewhere. VulkanDevice::CalibrateClock takes a pair when the window opens and another when it closes, and the mapping is linear between them, so the two clocks agree at both ends of the window. Without the extension there is no gpu track, and the [trace] line says so.
  • GPU mutex waits. On a traced thread, an acquisition of GpuMutex that has to wait becomes a GpuMutex.wait.<site> slice. The site is the one TagGpuLockSite names for the [lock] line (dispatch, draw, present, flush, ...), now also kept while a window is open. Outside a window lock() costs one more predictable branch.
  • Shader preparation threads are bound as shader prepare with buffers of 32,768 events. Their Shader.PrepareArtifact and other timers show up in the trace.
  • A batch is added when its timestamps are read, after its fence, so the batches of the last frames of the window, which finish after it closes, are not in the track. Documented in docs/dev/BUILD.md (Performance trace).

Tested

Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, RTX 5070 Laptop (NVIDIA 616.92).

  • agc_performance_trace: new case for the clock mapping. Ranges are mapped linearly between two calibrations (here the GPU clock runs at twice the CPU's rate), a range that starts before the window or has no length is dropped, and with one calibration the mapping uses the CPU rate.
  • Non-GPU suite (Lenovo node, ctest -E "^agc_driver|^video_out|^guest_video"): 615 of 616 pass on 795dc6b, including agc_performance_trace and prx_thread_local_destructors. log_macros fails as on main on that machine.
  • Sword of the Sea (PPSA25344), main menu, 150 s, APS5_TRACE_FRAMES=120:120 and APS5_PROFILE_GPU=1: the gpu track has 124,454 slices: 8,087 batches and the class ranges, which nest one level deep inside their batch. No slices overlap partially, and no two batches overlap. The batches cover the whole window and add up to 79.14 ms per present. The summary's gpu_busy_ms_per_present, from the same timestamps through the [present] accounting, is 79.15 ms. Every batch starts after the start of a Recorder.Submit slice on a queue worker (p1 15 us, median 141 us after the latest one), so the mapped GPU clock does not run ahead of the CPU's. Perfetto's trace processor v58.2 imports the file (3.7 MB) with no errors. A first run with a smaller GPU buffer filled it halfway through and set trace_dropped; the buffer now holds 262,144 ranges. 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: feat(agc): record a present window as a Chrome trace and a summary line #4526
  • 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 on our lab branch (with #4526 and #4570), which carries the local patches the title still needs. APS5_TRACE_FRAMES=120:120 APS5_PROFILE_GPU=1, 150 s run.

  • File: 3.7 MB .json.gz (36.4 MB of JSON), 398,575 events, trace_dropped false. Threads: queue worker 0x0 239,997 slices, queue worker 0x20 31,887, presenter 1,744, gpu 124,454. Ten shader prepare threads are named, but the menu prepared no shaders inside the window, so they have no slices.
  • GPU track: 8,087 batch slices, with the class ranges one level inside them. No slices overlap partially and no two batches overlap; the GPU runs one queue. The batches span the whole 14.8 s window and add up to 79.14 ms per present. The summary's gpu_busy_ms_per_present (the [present] accounting of the same timestamps) is 79.15 ms. Per present, by class: draw 12.1 ms, storage-writeback 10.1, storage-upload 7.7, staging-out 2.6, staging-in 2.5, shadow-publish 1.1, dispatch-trail 1.0, label-run 0.6.
  • Clock: every batch starts after the start of a Recorder.Submit slice on a queue worker: the gap to the latest one is 15 us at p1, 141 us at the median and 13.7 ms at p99. A mapping that ran ahead of the CPU clock would put batches before their submit.
  • Perfetto: trace_processor_shell v58.2 imports it with no error in stats: 398,202 slices on four thread tracks (depth up to 8 on queue worker 0x0, 1 on gpu), 120 present instants, and the three folded counters.

A first capture with a 65,536-range GPU buffer filled it at 7.5 s and set trace_dropped, which is what the flag is for. With APS5_PROFILE_GPU=1 the menu makes about 8,400 ranges a second, so the buffer is now 262,144 ranges, the size of a thread's.

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

AI-assisted: yes (Claude Code)

… trace

The trace gets a gpu track: one slice per recorder batch from its timestamp
pair, and with APS5_PROFILE_GPU=1 one per command class range under the
[gputime] class names. The GPU clock is mapped onto the CPU clock from
calibrated timestamps (VK_KHR_calibrated_timestamps or the EXT version,
enabled only when a trace switch is set) taken when the window opens and
when it closes. A traced thread's wait for the GPU mutex becomes a
GpuMutex.wait.<site> slice, and the shader preparation threads are traced
with smaller buffers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the feat/trace-gpu-track branch from 795dc6b to ad10d4f Compare October 12, 2026 20:16
@adriadam10

Copy link
Copy Markdown
Collaborator

#4526 is merged. I rebased this on main: only this PR's commit is left, and it applied cleanly. I also added ad10d4f, which updates the comment in TagGpuLockSite: the tag is now read inside a trace window too.

Tested on Linux (GCC 16.2, Radeon 660M, RADV):

  • agc_performance_trace, agc_profile_output, agc_stack_walk, agc_driver_present_mode, agc_driver_flip_compute, agc_driver_mesh and trace_digest pass (8/8).
  • GPU track on RADV, with a local harness (not pushed) that runs eight times a DCB busy loop followed by a flip, and APS5_TRACE_FRAMES=1:5. RADV offers VK_KHR_calibrated_timestamps with the device and CLOCK_MONOTONIC domains, so the track is there.
    • The track has 5 batch slices, which don't overlap.
    • Each batch starts 180-480 us after its Recorder.Submit. The first one was already in flight when the window opened.
    • The batches add up to 287 ms in a 305 ms window. With APS5_PROFILE_GPU=1 the class ranges (dispatch-lead, dispatch-trail, label-run) show up under their batch.
  • Validation layer: no VUID with the trace or without it, in this harness and in video_out_flip_tests present/backtoback/aftergpu.

One thing for later, not for this PR: in that short window the summary's gpu_busy_ms_per_present came to 437 ms in total, more than the window's wall time. It comes from SettleRetired, which settles a retired range after the fact, so a short window with long batches also counts work from before it opened. Over 120 presents that evens out, as your 79.14 / 79.15 shows. Once the gpu track exists, the summary could take the GPU busy time from the track instead.

The full build workflow runs next, and I'll merge when it's green.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@adriadam10
adriadam10 force-pushed the feat/trace-gpu-track branch from ad10d4f to eb8a725 Compare October 12, 2026 20:38
@adriadam10

Copy link
Copy Markdown
Collaborator

I dropped ad10d4f again: conventions doesn't let a comment change without a TechnicalDebt edit. That leaves the PR as eb8a725, the same code the full build above passed on (Linux and Windows green, run 38303183197).

@adriadam10
adriadam10 merged commit 84659e1 into boykopovar:main Oct 12, 2026
8 checks passed
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.

3 participants