Joe Gregorio | 02f7202 | 2021-03-27 10:12:45 -0400 | [diff] [blame] | 1 | |
| 2 | --- |
| 3 | title: "Tracing Skia Execution" |
| 4 | linkTitle: "Tracing Skia Execution" |
| 5 | |
| 6 | --- |
| 7 | |
| 8 | |
| 9 | Introduction |
| 10 | ------------ |
| 11 | |
| 12 | Skia is instrumented to provide execution traces in several ways. Within Chrome, Skia is traced |
| 13 | with the standard [tracing interface](chrome://tracing), along with the rest of Chromium. In |
| 14 | the Android framework, Skia's tracing is integrated into |
| 15 | [atrace](https://source.android.com/devices/tech/debug/ftrace). |
| 16 | |
| 17 | For standalone builds, Skia's tools (DM, nanobench, and Viewer) are capable of tracing execution |
| 18 | in three ways, controlled by the `--trace` command line argument. |
| 19 | |
| 20 | Standalone Tracing |
| 21 | ------------------ |
| 22 | |
| 23 | Most arguments to `--trace` will be interpreted as a filename (the two exceptions are described |
| 24 | below), 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 | |
| 32 | This creates a file `gl_bleed_gms.json` in the current directory. There are limitations in Chrome's |
| 33 | tracing tool that prevent loading a file larger than 256 MB. To stay under that limit (and avoid |
| 34 | clutter and slowdown in the interface), it's best to run a small number of tests/benchmarks when |
| 35 | tracing. Once you have generated a file in this way, go to |
| 36 | [chrome://tracing](chrome://tracing), click Load: |
| 37 | |
Joe Gregorio | 9601185 | 2021-04-05 09:17:20 -0400 | [diff] [blame] | 38 |  |
Joe Gregorio | 02f7202 | 2021-03-27 10:12:45 -0400 | [diff] [blame] | 39 | |
| 40 | ... then select the JSON file. The data will be loaded and can be navigated/inspected using the |
| 41 | tracing tools. Tip: press '?' for a help screen explaining the available keyboard and mouse |
| 42 | controls. |
| 43 | |
Joe Gregorio | 9601185 | 2021-04-05 09:17:20 -0400 | [diff] [blame] | 44 |  |
Joe Gregorio | 02f7202 | 2021-03-27 10:12:45 -0400 | [diff] [blame] | 45 | |
| 46 | Android ATrace |
| 47 | -------------- |
| 48 | |
| 49 | Running any tool with `--trace atrace` on an Android device will cause the application to forward |
| 50 | tracing information to [atrace](https://source.android.com/devices/tech/debug/ftrace). On other |
| 51 | platforms, this has no effect. |
| 52 | |
| 53 | If you run `systrace` from the host command line, you will need to supply `-a <app_name>`, |
| 54 | and the `<app_name>` argument will need to exactly match the command line used on the target |
| 55 | device. For example, if you use `adb shell "cd /data/local/tmp; ./nanobench --trace atrace ..."` |
| 56 | you must pass `-a ./nanobench` or systrace will ignore events from the application. |
| 57 | |
| 58 | Console Logging |
| 59 | --------------- |
| 60 | |
| 61 | For 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 | |
| 90 | Adding More Trace Events |
| 91 | ------------------------ |
| 92 | |
| 93 | Adding more trace events involves using a set of `TRACE_` macros. The simplest example, to record |
| 94 | the time spent in a function or other scope, is: |
| 95 | |
| 96 | ~~~ |
| 97 | #include "SkTraceEvent.h" |
| 98 | ... |
| 99 | void 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 | |
| 112 | For more examples, including other kinds of trace events and attaching parameters to events, see |
| 113 | the comments in |
| 114 | [SkTraceEventCommon.h](https://cs.chromium.org/chromium/src/third_party/skia/src/core/SkTraceEventCommon.h). |
| 115 | |