GPU profiling

The GPU side of a recompiled game is the runtime's Vulkan renderer replaying the game's NVN (or Maxwell) commands: the shaders are the game's, translated to SPIR-V; the passes, barriers and copies are the renderer's. GPU profiling asks two questions: is the GPU the bound at all, and if so, which pass, draw or shader.

CPU-bound or GPU-bound

vk_gpu_profile: where the GPU's time goes

--vk-gpu-profile=1 (src/nx/gpu/vk/gpuprof.c) times every rendering pass of each frame on the GPU, with its first color (or depth) target, its draw count, and what was recorded between it and the pass before (copies by kind and bytes, barriers, dispatches). Every 10 s the log gets the frame's span, the time in passes, the time in the gaps by what they held, and the targets that took the most. It also times each command buffer, and splits the queue's idle time into "before the CPU submitted the next one" (the GPU had nothing to do: a round trip, between frames, or the recording was slower than the GPU) and "after" (a semaphore wait). It needs timestamps on the graphics queue (timestampComputeAndGraphics) and, for the CPU alignment, calibrated timestamps; the log says when a device runs it as 0.

"$g" --state-load=2000:1 --seconds=60 --fps-limit=0 --vsync=0 --vk-gpu-profile=1 --log=1
grep 'vulkan: GPU' ~/.local/state/scarletnx/recompiled/<id>/<id>.log   # the 10-second summaries

It works in every build, with no external tool, on every platform with timestamps (Android included). Off, it costs a branch per pass. --log=3 asks for the same counters, and adds the CPU's time per resource category every 600 presents, so a steady scene's CPU cost needs no setting of its own. It is the first GPU tool to reach for: a pass that owns most of the frame, a pass of milliseconds at a few hundred pixels (the slowest passes are listed with the draws, copies, barriers and dispatches they held), or a gap full of copies (that is how 955 MB of buffer copies a frame were found), says where to look next.

The GPU track in Tracy

In a Tracy build, a connected run takes the same timestamps and puts each pass on the GPU's track, named by its target, with each compute batch's dispatches; the GPU clock is aligned to the CPU's once a frame, so a long pass shows against the CPU work that waited for it. With a client attached, a zone is added on every draw and dispatch (named by pipeline, topology and count); GP_QUERIES gives 16384 timestamps and GP_DISP 4096 dispatches a frame slot, and the log says how many passes went untimed. A timestamp between draws keeps the GPU from overlapping them on many devices, so the draw zones inflate the frame: use them to rank draws inside a pass, not to time the frame. See Development: Profiling.

RenderDoc

RenderDoc captures one frame's Vulkan calls and replays them: every pass, draw, resource, pipeline state and shader (with SPIR-V disassembly and, for a translated shader, the GLSL-like decompilation), and per-draw GPU durations in its Event Browser (Time column, via the clock button). The runtime loads it itself:

# capture presents 500 and 800, to work/prof/rd_<frame>.rdc
"$g" --renderdoc=work/prof/rd --renderdoc-frames=500,800
# or the first present after 45 seconds
"$g" --renderdoc=work/prof/rd --renderdoc-frames=45s

With renderdoc set, Ctrl+Shift+F12 (hotkey_capture) or the game menu's Capture frame captures the frame after the next present; a game started from RenderDoc's own UI (qrenderdoc, Launch Application on <id>) works the same. RenderDoc writes the capture inside the present that ends it and the game stops meanwhile (up to a minute for a frame of 1.4 GB); the log and a notice say when it is written.

RenderDoc's timings are the replay's, one draw at a time; they rank draws well and frame times poorly.

Vendor GPU profilers

For counters below the API (occupancy, cache hit rates, stalls by unit), use the GPU vendor's profiler on the runtime like on any Vulkan application. Launch "$games/<id>/<id>" with the options above as the application:

GPUToolNotes
NVIDIANsight Graphics (Frame Debugger, GPU Trace)Linux and Windows; GPU Trace gives per-unit throughput across a frame range
AMDRadeon GPU ProfilerAMD's driver, or RADV with MESA_VK_TRACE=rgp (trigger with MESA_VK_TRACE_TRIGGER=<file> and touch <file>)
IntelMesa's INTEL_MEASURE, GPAINTEL_MEASURE logs per-draw/per-renderpass GPU time to CSV
Mesa, anyMESA_VK_TRACE, perfetto data sources where the driver has themnames depend on the driver and version
Arm Mali, Qualcomm AdrenoArm Streamline / Performance Studio, Snapdragon Profiler, Android GPU Inspectorfor the Android app

VK_KHR_performance_query counters, where a driver has them, are reachable through snx-shaderperf --counters for a single shader. NVIDIA's driver has none.

One shader: snx-shaderperf

A pass is slow because of one shader: take it out of the game and time it alone.

  1. Keep the game's shaders: --shader-corpus=$PWD/work/corpus (every shader is translated, none kept for the next run), or diag=1 where the game takes no command line (Android: captures/shaders beside the game).

  2. Translate them to SPIR-V: build/snx-shaderc work/corpus --validate --out work/translated (--validate needs spirv-val).

  3. Find the slow one: its hash is in Tracy's shader and pipeline zones, and in RenderDoc's pipeline state.

  4. Time it, and a variant of it, in one run:

    build/snx-shaderperf --reflect work/translated/<hash>.spv
    build/snx-shaderperf work/translated/<hash>.spv work/variant.spv --size 1920x1080 --check --target-rse 0.005 \
        --json=work/prof/shader.json

    It interleaves the shaders' submissions, rejects outliers, and gives a ratio of medians with a bootstrap interval and a verdict; on NVIDIA it also reports registers, binary size and shared memory per stage (VK_KHR_pipeline_executable_properties). --check hashes everything the shaders write, so a faster variant that computes something else is caught. Compare only shaders measured in the same run.

The tool's options are in tools/shaderperf/README.md.

The renderer's CPU side

In many games the GPU idles while the GPU thread records the frame: then a "GPU problem" is a CPU profile of one thread. Profile the GPU thread with perf/pmcstat (--tid, or --sort comm and its name), and look for the functions The CPU cost of a draw names (buf_get, cb_get, tex_resolve, stage_set, vk_shader_hash, vk_mirror_harvest, libc copies). Tracy's draws, dispatches, pipeline make ms, pipeline wait ms and texture upload MB/s give the counts to divide by. Settings that move this work are worth one A/B run each: vk_record (a recording thread of its own), vk_descriptors_mode, vk_memory, vk_buffer_pages (Settings); vertex_input_dynamic_state (Settings); tools/tune_gpu.py searches them for a game.

Pipeline compiles are the other CPU-side GPU cost: the first frames of a new scene make pipelines (pipeline make ms); the next run loads them from <id>.<driver>.vkshaders. tools/prepare_pipeline_probe.py with tools/bench_pipeline_compile.py times the captured pipelines' compiles in fresh processes, with the drivers' disk caches off.

GPU memory

Running out of video memory looks like a GPU slowdown (evictions, uploads, textures over the bus) and sometimes as a lost device. Watch the stats box's video memory against the budget, Tracy's GPU memory and budget plots and texture cache misses/s, and the resources:/memory: lines of --log=2. A Vulkan out-of-memory logs each heap and the descriptor pools. vk_budget_mb and vk_max_allocation_mb impose a smaller budget, to reproduce a small GPU's behaviour on a large one (Settings); astc_textures changes what ASTC textures cost on a GPU without ASTC (Settings).

Every game log has, every 600 presents with no option, where the renderer's caches put that memory: the vulkan: caches: line (render targets, textures and buffers, in MB). A line of the same textures and render targets by what their images hold, each with its count, its MB of the GPU's memory and its MB in system memory (an image moved there for want of room), comes when an allocation finds no memory (at most every 600 presents) and when the GPU is lost, and every 600 presents with vram_by_format=1 (Settings). A racing game's race on a GPU without ASTC, 157 s in, with 6175 MB of textures:

vulkan: the caches' textures and render targets by what their images hold (every 600 presents, vram_by_format=1):
ASTC decoded 1629, 3648 MB (0 MB in system memory); ASTC as BC7 2, 859 MB (0 MB in system memory); ASTC as
BC1/BC4/BC5 795, 435 MB (0 MB in system memory); BC1 to BC7 as they are 670, 582 MB (0 MB in system memory);
uncompressed 324, 649 MB (0 MB in system memory); render targets 1013, 893 MB (0 MB in system memory)

ASTC decoded is ASTC held as RGBA8 where the GPU samples no ASTC, 4 to 36 times its blocks' size (4x4 to 12x12); ASTC as BC7 and ASTC as BC1/BC4/BC5 what the encoder made of it; ASTC as it is and BC1 to BC7 as they are what the GPU samples itself; other formats converted ETC2, EAC and the BC or 16-bit packings a GPU lacks; uncompressed the rest. A target's copies are in the targets' line after it, not here.


Made from docs/dev/profiling.md at 69237645142c. GPL-3.0-or-later.