Tracy: the whole run on one timeline

Tracy is the profiler the runtime is instrumented for: frames, every SVC and IPC command, the GPU thread's jobs, Vulkan recording and submission, shader translation and pipeline creation, texture uploads and encodes, the JIT, audio, input, and the GPU's own passes, on one timeline with plots of every counter the runtime keeps. It is the right first tool for "why is this frame slow".

Build it (cmake --preset tracy -B build/tracy && cmake --build build/tracy) and get Tracy 0.15.0's GUI (tracy-profiler) and tracy-capture: the server must be the client's version. Then either:

# record a run to a file, and open it in the GUI when the run ends
build/tracy/scarletnx --game="$games/<id>" --tracy-capture --seconds=60

# start with the client up, and connect the GUI to 127.0.0.1:8086 (discovery is off)
build/tracy/scarletnx --game="$games/<id>" --tracy

# record without the GUI, on another machine or over ssh
tracy-capture -o work/prof/trace.tracy -f -s 60 &
build/tracy/scarletnx --game="$games/<id>" --tracy
wait

or, while a game runs, press F10 for a capture beside the game that runs for tracy_capture_seconds (60 s by default); the log names its file. The launcher has the same as settings (Settings > Debug > Tracy profiler, Settings > Launch > Tracy capture).

A walk through a trace:

  1. Frames. The frame bar on top; click the slow frames. frame ms and its neighbours (jit miss ms, pipeline wait ms, texture uploads, shaders) say at once whether a hitch was a compile.
  2. Threads. Find the thread whose zones fill the slow frame: the GPU thread (gpu job, vk record, ...), a guest thread (SVC zones, IPC commands), the JIT. A thread with gaps is waiting; vk lock wait appears only when the Vulkan lock was contended.
  3. The GPU track. The passes on the GPU's clock, aligned to the CPU's once a frame. A long pass with the CPU idle is GPU-bound; the CPU busy while the GPU idles (gpu idle: plots) is CPU-bound.
  4. Statistics. Instrumentation sums the zones; with the sampler on, Sampling lists the hot functions with their call stacks.

Turn on the sampler (--tracy-sampling=1) for the time between the zones. It needs Linux perf events (a normal user has them unless kernel.perf_event_paranoid forbids it), samples at 1 kHz (TRACY_SAMPLING_HZ), and names the runtime, the game's _code.so and shared libraries' exports; the JIT's code stays [unknown]. Without it, Tracy shows the instrumented zones alone, which costs much less.

Turn on tracy_context_switches (--tracy-context-switches=1) for the OS's context switches on the threads' timelines. A guest thread's wait and preemption then show against the game's zones. On Linux this needs permission to read perf events and tracefs (kernel.perf_event_paranoid, and a high ulimit -n), and Tracy turns it off with a debug line when it cannot. Tracy couples it to its sampler, so the sample stacks come on with it. Either setting turns on both the sampler and the context switches.

Each guest thread's wait also appears as a fiber, named for what the thread blocked on: an object, a mutex, a condition variable, an address, a pause, a service request or a game pause. The Fibers pane lists them with their durations, so a thread's idle time can be sorted by why it waited without reading the log.

Two more settings add information for flame graphs. tracy_zone_callstack (--tracy-zone-callstack=N, 0 to 16 frames, 0 by default) gives a call stack only to zones written with SNX_ZONE_CS: pipeline make compile callstack, shader translate maxwell, kern core switch, kern core handoff, kern mutex handoff, kern svc unknown, svc hid cmd, state memory check, state memory load and state compress. Those zones show their callers without every zone paying for a stack. perf_map (--perf-map=1) writes a Linux perf map (/tmp/perf-<pid>.map) for the code the runtime's JIT compiles, so a sampled flame graph names those frames instead of [unknown]. The map is written as loads compile, with un-emitted lines appended at exit; 0 writes no map.

While a GUI is attached, its parameter pane has six live controls: Frame image seconds, Memory detail, Memory callstack depth, Zone callstack depth, GPU zone callstack depth and Lock marks. The matching .ini values are the startup defaults; changing a parameter acts on the next emission without a restart. Memory detail can only be enabled this way: once on, it stays on for the run so every tracked allocation keeps its free event.

The frame sets dropdown separates the unnamed set from present, guest, audio, jit and state. audio, jit and state are discontinuous sets: their spans are the work between a start and an end, not frames between presents. Guest threads carry a group hint naming their emulated core, so the thread view can group them. Shader translation failures also send a message with a four-frame call stack, and a JIT add uses a transient zone whose name is the code's address. The Source view can ask for the shader summary that the translator registers.

Tracy 0.15.0 has no call that writes a trace from the app. A fatal outside a signal marks the trace's message pane with the reason and closes the client, so a capture writes the events up to it. A fatal from the crash handler exits before the client can close. Its capture keeps the events already sent, which may not include the reason. Start with --tracy-capture to keep a trace of a crash, and read the game's log for the reason. The runtime's own crash handler stays in charge of the signal.

Things to know (more in Development):

Sections, lock marks and callstacks

A few additions to the instrumentation show more of what a run was doing. They emit nothing while no viewer is attached, and each one a setting gates has its own cost when it is on.

A section is a high-level phase on Tracy's section track, written with SNX_SECTION_SCOPED. game load, shader preparation, save state and load state use it. Unlike a zone, a section is not a stack. Its enter and leave bracket one piece of work and carry an id, so the section track shows the phase against the zones of every thread. No setting gates it. With a viewer connected it costs one event at the start and one at the end of each phase.

Lock marks (tracy_lock_marks=1) put the source line that holds a named lock on the lock pane. More of the runtime's mutexes announce themselves now, among them the Vulkan device, the guest memory, the GPU queue, the shader translate and pipeline layout locks, and the audio renderer's table. The pane shows who held a lock and who waited on it, and a failed try lock is an event too, so a try that won and one that lost both appear. Each acquisition costs one Tracy event, and the source lines are interned in a small table, so leave this off unless the trace needs lock waits.

Allocation callstacks come from tracy_memory_callstack (0 to 16 frames, 0 by default). It adds a CPU call stack to the detailed memory events of tracy_memory_detail, so the Memory view says which code made each allocation. It needs tracy_memory_detail=1 as well, and every game malloc and free then pays for the stack, so keep both off unless the pools and their callers are what the trace is for. SNX_MEM_DISCARD also emits a discard for a named pool, so a pool cleared as a whole shows that rather than a list of frees.

GPU pass-zone callstacks come from tracy_gpu_zone_callstack (0 to 16 frames, 0 by default). It adds the CPU stack where a pass was recorded to each pass zone on the GPU track. It walks a stack for every pass zone, so it costs much more than the zone callstacks of tracy_zone_callstack and belongs on pass-level zones only. SNX_ZONE_COLOR sets a zone's color, so a rare case such as a failed SVC shows red against the usual zones without a separate zone name.

Tracy's GUI has a built-in Achievements tutorial, and several of its items unlock from the data a run sends with no further setup. tracy_sampling=1 sends callstack samples, which unlock Sampling program execution. tracy_frame_images=N attaches pictures to frames, which the Playback view and its A picture is worth a thousand words item under Instrumenting frames need. tracy_context_switches=1, tracy_zone_callstack=N and tracy_memory_detail=1 feed the context-switch, callstack and Memory views that the same tutorial leads through. Frame marks and zones are always on, so First profiling session and Instrumenting your application unlock on the first connected run.


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