1 2--- 3title: "Tracing Skia Execution" 4linkTitle: "Tracing Skia Execution" 5 6--- 7 8 9Introduction 10------------ 11 12Skia is instrumented to provide execution traces in several ways. Within Chrome, Skia is traced 13with the standard [tracing interface](chrome://tracing), along with the rest of Chromium. In 14the Android framework, Skia's tracing is integrated into 15[atrace](https://source.android.com/devices/tech/debug/ftrace). 16 17For standalone builds, Skia's tools (DM, nanobench, and Viewer) are capable of tracing execution 18in three ways, controlled by the `--trace` command line argument. 19 20Standalone Tracing 21------------------ 22 23Most arguments to `--trace` will be interpreted as a filename (the two exceptions are described 24below), and trace events will be written to that file in JSON format, suitable for viewing with 25[chrome://tracing](chrome://tracing). 26 27<!--?prettify lang=sh?--> 28 29 # Run DM on several GMs to get tracing data 30 out/Release/dm --config gl --match bleed --trace gl_bleed_gms.json 31 32This creates a file `gl_bleed_gms.json` in the current directory. There are limitations in Chrome's 33tracing tool that prevent loading a file larger than 256 MB. To stay under that limit (and avoid 34clutter and slowdown in the interface), it's best to run a small number of tests/benchmarks when 35tracing. Once you have generated a file in this way, go to 36[chrome://tracing](chrome://tracing), click Load: 37 38 39 40... then select the JSON file. The data will be loaded and can be navigated/inspected using the 41tracing tools. Tip: press '?' for a help screen explaining the available keyboard and mouse 42controls. 43 44 45 46Android ATrace 47-------------- 48 49Running any tool with `--trace atrace` on an Android device will cause the application to forward 50tracing information to [atrace](https://source.android.com/devices/tech/debug/ftrace). On other 51platforms, this has no effect. 52 53If you run `systrace` from the host command line, you will need to supply `-a <app_name>`, 54and the `<app_name>` argument will need to exactly match the command line used on the target 55device. For example, if you use `adb shell "cd /data/local/tmp; ./nanobench --trace atrace ..."` 56you must pass `-a ./nanobench` or systrace will ignore events from the application. 57 58Console Logging 59--------------- 60 61For simple situations, all tracing events can be directed to the console with `--trace debugf`: 62 63<!--?prettify lang=sh?--> 64 65 # Run DM on a single GM with SkDebugf tracing 66 out/Release/dm --config gl --match ^gamma$ --trace debugf 67 68~~~ 69[ 0] <skia.gpu> GrDrawingManager::internalFlush id=1 #0 { 70[ 0] } GrDrawingManager::internalFlush 71[ 0] <skia.gpu> GrGpu::createTexture id=1 #1 { 72[ 0] } GrGpu::createTexture 73[ 0] <skia.gpu> GrRenderTargetContext::discard id=1 #2 { 74[ 0] } GrRenderTargetContext::discard 75[ 0] <skia.gpu> SkGpuDevice::clearAll id=1 #3 { 76[ 1] <skia.gpu> GrRenderTargetContext::clear id=1 #4 { 77[ 1] } GrRenderTargetContext::clear 78[ 0] } SkGpuDevice::clearAll 79[ 0] <skia> SkCanvas::drawRect() #5 { 80[ 1] <skia.gpu> SkGpuDevice::drawRect id=1 #6 { 81[ 2] <skia.gpu> GrRenderTargetContext::drawRect id=1 #7 { 82[ 3] <skia.gpu> GrRenderTargetContext::addDrawOp id=1 #8 { 83[ 3] } GrRenderTargetContext::addDrawOp 84[ 2] } GrRenderTargetContext::drawRect 85[ 1] } SkGpuDevice::drawRect 86[ 0] } SkCanvas::drawRect() 87... 88~~~ 89 90Tracing with Perfetto 91-------------- 92Running any tool with `--trace perfetto` will cause the application to forward 93tracing information to [Perfetto](https://perfetto.dev/docs/instrumentation/track-events). 94Perfetto only supports Linux, Mac, and Android and will not run on other platforms. 95 96By default, Perfetto tracing within Skia has been configured to handle relatively short 97(~10 seconds or less) trace events and sessions (for example, a subset of tests rather than the 98entire testing suite). For any tracing sessions longer than ~10 seconds, it is recommended to use the `--longPerfettoTrace` runtime option which will change Skia's Perfetto configuration to accommodate 99the longer trace. Long traces conducted without this runtime option run the risk of overwriting 100events, leading to a loss of data. 101 102The trace output file path can be changed with runtime arguments. `--perfettoOutputDir` sets the 103output directory, `--perfettoOutputFileName` sets the output file name (without file extension), 104and `--perfettoOutputFileExtension` sets the output file extension. By default, the trace file will 105be placed in the build output directory as `trace.perfetto-trace`. 106 107You can also elect to generate different trace files for each nanobench benchmark. To do so, use 108the 109`--splitPerfettoTracesByBenchmark` option. Note that this will lead to the output files being 110named after the different benchmarks. 111 112These trace files can be visualized using 113[Perfetto's web visualization tool](https://ui.perfetto.dev/). To visualize larger trace files 114(anything greater than around 2 GB), see 115[these instructions](https://perfetto.dev/docs/visualization/large-traces). 116 117Should you run into any issues or unexpected results, Perfetto has some resources which may help. 118To identify potential root causes, check the "Info and stats" page on the web visualization tool, or 119by running SQL queries on the trace file (online, or by using 120[the trace processor application](https://perfetto.dev/docs/analysis/trace-processor)). 121To diagnose these issues, see 122[this section](https://perfetto.dev/docs/concepts/buffers#debugging-data-losses) 123on debugging data losses and 124[this section](https://perfetto.dev/docs/concepts/buffers#flushes-and-windowed-trace-importing) 125on out-of-order events which may appear unexpectedly long. 126 127 128Adding More Trace Events 129------------------------ 130 131Adding more trace events involves using a set of `TRACE_` macros. The simplest example, to record 132the time spent in a function or other scope, is: 133 134~~~ 135#include "SkTraceEvent.h" 136... 137void doSomething() { 138 // Add an event for the duration of the current function (or other scope) 139 // "skia" is a category name, for filtering events while recording 140 // TRACE_FUNC is the event name, and expands to the name of the current function 141 TRACE_EVENT0("skia", TRACE_FUNC); 142 143 if (doExtraWork) { 144 TRACE_EVENT0("skia", "ExtraWorkBeingDone"); 145 ... 146 } 147} 148~~~ 149 150For more examples, including other kinds of trace events and attaching parameters to events, see 151the comments in 152[SkTraceEventCommon.h](https://cs.chromium.org/chromium/src/third_party/skia/src/core/SkTraceEventCommon.h). 153 154