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