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![Load Button](../tracing_load.png) 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![Tracing interface](../tracing.png) 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 90Adding More Trace Events 91------------------------ 92 93Adding more trace events involves using a set of `TRACE_` macros. The simplest example, to record 94the time spent in a function or other scope, is: 95 96~~~ 97#include "SkTraceEvent.h" 98... 99void doSomething() { 100 // Add an event for the duration of the current function (or other scope) 101 // "skia" is a category name, for filtering events while recording 102 // TRACE_FUNC is the event name, and expands to the name of the current function 103 TRACE_EVENT0("skia", TRACE_FUNC); 104 105 if (doExtraWork) { 106 TRACE_EVENT0("skia", "ExtraWorkBeingDone"); 107 ... 108 } 109} 110~~~ 111 112For more examples, including other kinds of trace events and attaching parameters to events, see 113the comments in 114[SkTraceEventCommon.h](https://cs.chromium.org/chromium/src/third_party/skia/src/core/SkTraceEventCommon.h). 115 116