Profiling

Profiling walks through every profiler, CPU and GPU, against a recompiled game; this section is the reference of the Tracy integration it uses.

The GPU benchmarks and the search for faster GPU settings (tools/bench_nvn_draws.py, tools/bench_gpu_descriptors.py, tools/bench_gpu_hotpaths.py, tools/tune_gpu.py) are described with the measurements they made, in GPU preparation benchmarks and Graphics descriptor measurements.

The profiler is not part of the ordinary runtime: SNX_TRACY defaults to OFF, so scarletnx carries the pinned Tracy 0.15.0 client only in a build that asks for it — cmake --preset tracy -B build/tracy && cmake --build build/tracy, or -DSNX_TRACY=ON in a build of your own (Android's app leaves it out, and iOS cannot have it). The client starts only when profiling is requested: the Settings' Debug rows (tracy profiler, tracy capture), --tracy-capture, --tracy, or F10. Without one, a run does not connect.

A game runs profiled from the terminal, the runtime's own flags and nothing else:

games="${XDG_DATA_HOME:-$HOME/.local/share}/scarletnx/recompiled"
build/tracy/scarletnx --game="$games/<id>" --tracy-capture   # records the run, and opens its trace when it ends
build/tracy/scarletnx --game="$games/<id>" --tracy           # a run for the GUI to attach to
build/tracy/scarletnx --game="$games/<id>" --tracy-capture --script=script --shot=shot  # a test's run, profiled

The short way is the launcher: turn on Settings > Debug > Tracy profiler and start the game. The client comes up with the run; connect the Tracy GUI to 127.0.0.1:8086 when it is up. Broadcast discovery is off, so the GUI's connect to the next instrumented process does not find it. A run recorded to a file needs no GUI at all: Settings > Launch > Tracy capture records it and opens the trace in the profiler when the game ends.

--tracy starts the client without recording a file. The client off, an emission is one atomic load; the client up but nothing attached, a zone is a couple of loads on the emitting thread (a thread that is not attached asks at every zone, so a thread that makes few — the frontend's one a frame, the audio clock's one a tick — is traced from its next one; an attached one asks every few thousand) and the other emissions skip their call; attached, the cost is Tracy's own.

The sampler, which says where the time went between the zones, waits to be asked for: Settings > Debug > Tracy sampling (tracy_sampling) starts it at the first connection, so a run that leaves it off pays nothing and its timeline reads as the instrumented zones alone (with JIT'd guest code and the drivers' own threads, the sample stacks read as a wall of [unknown]). It samples at 1 kHz (the build-time TRACY_SAMPLING_HZ define changes that) and stops with the client. The zones written with SNX_ZONE_CS ask for a call stack when tracy_zone_callstack is set: 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. The wait stacks, which walk the stack at every context switch, are a build option. The default build defines TRACY_NO_WAIT_STACKS and leaves them off. Configure with -DSNX_TRACY_WAIT_STACKS=ON to remove that define and build them in. Either tracy_sampling or tracy_context_switches starts them; the latter needs the tracefs and so root on Linux. A Tracy build keeps the frame pointers -O2 drops, as the sampler walks them. The sampler needs the kernel's perf events, which a normal user has.

The instrumentation also has SNX_SECTION_SCOPED for phases on Tracy's section track, SNX_ZONE_COLOR to color an open zone, SNX_MEM_DISCARD to mark a pool cleared as a whole and SNX_LOCK_MARK to put the source line on a named lock. Lock announcements now cover more of the runtime's mutexes and show both outcomes of a try lock. Profiling says what each shows and what it costs.

In the launcher, Settings > Launch > Tracy capture turns on a capture for each Switch run. The runtime starts tracy-capture (from Settings > Debug > Tracy capture tool (tracy_capture_tool), beside the game, or on PATH) and writes captures/tracy_<game>_<date>.tracy beside the game when the run ends. It passes Settings > Debug > Tracy port (tracy_port) on when the setting names one. With the setting off, the runtime does not start the client; use --tracy for a GUI-only run, or the launcher's Settings > Debug > Tracy profiler, which starts the run with the client up for the GUI to attach to. --tracy-capture also needs Tracy 0.15.0's tracy-capture; a runtime built with -DSNX_TRACY=OFF (Android, iOS) refuses both flags. A capture that cannot start, or one whose handshake the loading game missed three times, leaves the run going without a capture and says why in the log: a setting does not cost the game its launch. A capture gets a port of its own, so an earlier capture (or a GUI) on 8086 is not in the way, and the run waits for the capture before it starts the game: the handshake needs an answer, and a game loading a dump starves it.

To capture only a later part of a run, leave that setting off and press F10 in the game: it starts the client and a capture beside the game that runs for tracy_capture_seconds (60 s by default), and logs its path. Run it again after it finishes for another segment. A connected GUI and tracy-capture are both viewers of the same one-connection client, so disconnect the GUI first if it is open.

With --tracy, connect the Tracy 0.15.0 GUI to 127.0.0.1:8086, or to tracy_port's port when the setting names one; broadcast discovery is off. tracy-capture records the same stream to a file without the GUI. It can start before or after the game, and -s bounds the recording; it writes the trace when that ends or the game exits. A normal exit (Esc, the game's own exit, or an unattended limit) ends the zones still open on the exiting thread, waits a moment for the other threads' short zones, sends the client's queued events and closes the connection, so the capture sees a clean end rather than a dropped socket. If a thread is still inside a zone after that wait, the client is left for _exit to end: the socket still closes and tracy-capture still writes its file, instead of being killed before it has written anything. The SVCs that wait (SleepThread, WaitSynchronization, ArbitrateLock, WaitProcessWideKeyAtomic, WaitForAddress) and the GPU thread's idle wait are deliberately not zones: one left open would keep the server from acknowledging the client's Terminate.

tracy-capture -o trace.tracy -f -s 60 &
build/tracy/scarletnx --game="$games/<id>" --tracy
wait

The timeline has the frontend tick, presents, GPU jobs and idle waits, Vulkan record/land/release/submit/wait/present work, shader translation and pipeline creation, texture uploads, the replay of an NVN submission, compute dispatches, the publishes of surfaces over guest memory, the BC7 encodes between frames, the landings of the guest memory the GPU owns, the input tick, the audio and host1x clocks, every returning SVC except the waits above (named as in hos.svc) and every IPC command (the service and command number in the zone's text). Frames appear with the frame ms, guest memory, GPU memory and budget, per-frame draws, shader translations, pipelines made, texture uploads, and the pipeline cache's hits and the time making and waiting for pipelines. The texture cache's rates (texture cache hits/s, texture cache misses/s, texture upload MB/s, texture frees/s) and the memory the GPU owns (own traps/s, own land MB/s) go on the frontend tick, beside guest memory and the io rates, so a rate is read over the same second of wall time as the others. Zones that work on a count carry it as their value, such as the command buffers in a submit, the GPU channel of a job, the words of a translated shader, the records in a record pass, the bytes of an uploaded texture (with its dimensions in the text), the frames an audio tick runs behind its schedule, and the voices an audio frame plays. A translated shader's zone names its stage and hash, and a pipeline's its hash, so a draw's work follows from the shader it made. The Vulkan lock records a vk lock wait zone only when the lock is contended, so a wait shows apart from the work done under it and an uncontended acquisition adds no zone.

The GPU's own track comes with it: the rendering passes of each frame are zones on the GPU's clock, named by the target they draw to, and each dispatch a compute batch holds. With a client attached, a zone is put on every draw and dispatch as well, named by pipeline, topology and count, so the pass that is unexpectedly expensive says which of its draws it was: a timestamp between draws leaves the queue waiting for the one before it on many devices, so the zones come only with an attached client. The pool is bounded (GP_QUERIES 16384 timestamps and GP_DISP 4096 dispatches a frame slot), and the log says how many passes went untimed. Each zone is written outside the predication it draws under, whose region writes no timestamp. The GPU clock is put on the CPU's timeline once a frame, so a pass that took the GPU long shows against the CPU's work that waited for it. The timestamps come from the graphics queue's, which the device must have; they are taken when the runtime is profiled (a GUI connected or a capture running) or when vk_gpu_profile=1 asks for them, which also logs what the GPU's time went to every 10 s. The binary is not deployed beside every game the way an ordinary build is, so a game it is not run through the profiled driver for keeps the runtime it already had: run it through the profiled driver once, and launcher rows work from then. SNX_TRACY=OFF leaves the client out; iOS has none. The Windows-only profile=1 sampler is separate.

The guest's own file I/O is in the trace too: a read or a write is a zone with the file's name and the bytes, and io read MB/s and io write MB/s are the rate. The game's assets are not read during a run, though: they are mapped from the dump, and the dump is read once, by the driver, before the runtime starts.


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