• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Quickstart: Record traces on Android
2
3Perfetto allows you to collect system-wide performance traces from Android
4devices from a variety of data sources (kernel scheduler via ftrace, userspace
5instrumentation via atrace and all other data sources listed in this site).
6
7## Starting the tracing services
8
9Perfetto is based on [platform services](/docs/concepts/service-model.md)
10that are avilable since Android 9 (P) but are enabled by default only since
11Android 11 (R).
12On Android 9 (P) and 10 (Q) you need to do the following to ensure that the
13tracing services are enabled before getting started:
14
15```bash
16# Needed only on Android 9 (P) and 10 (Q) on non-Pixel phones.
17adb shell setprop persist.traced.enable 1
18```
19
20## Recording a trace
21
22Command line tools (usage examples below in this page):
23
24* Using the [`tools/record_android_trace`](/tools/record_android_trace) helper script.
25* Using directly the `/system/bin/perfetto` command on device [[reference](/docs/reference/perfetto-cli.md)].
26
27UI tools:
28
29* Through the record page in the [Perfetto UI](https://ui.perfetto.dev).
30* Using the on-device [System Tracing App](https://developer.android.com/topic/performance/tracing/on-device)
31
32### Recording a trace through the Perfetto UI
33
34Navigate to [ui.perfetto.dev](https://ui.perfetto.dev/#!/record) and select
35**Record new trace** from the left menu.
36From this page, select and turn on the data sources you want to include in the
37trace. More detail about the different data sources can be found in the
38_Data sources_ section of the docs.
39
40![Record page of the Perfetto UI](/docs/images/record-trace.png)
41
42If you are unsure, start by turning on **Scheduling details** under the **CPU** tab.
43
44Ensure your device is connected and select **Add ADB device**. Once your device
45has successfully paired (you may need to allow USB debugging on the device), select the **Start Recording** button.
46
47Allow time for the trace to be collected (10s by default) and then you should
48see the trace appear.
49
50![Perfetto UI with a trace loaded](/docs/images/trace-view.png)
51
52Your trace may look different depending on which data sources you enabled.
53
54### Recording a trace through the cmdline
55
56**Prerequisites**
57
58For the cmdline based workflow you will need the `adb` (Android Debug Bridge)
59executable to be in your PATH. ADB binaries for Linux, Mac or Windows can be
60downloaded from https://developer.android.com/studio/releases/platform-tools .
61
62**Using the helper script**
63
64We suggest using the `tools/record_android_trace` script to record traces from
65the command line. It is the equivalent of running `adb shell perfetto` but it
66helps with getting the paths right, auto-pulling the trace once done and opening
67it on the browser.
68
69If you are already familiar with `systrace` or `atrace`, both cmdline tools
70support a systrace-equivalent syntax:
71
72On Linux and Mac:
73
74```bash
75curl -O https://raw.githubusercontent.com/google/perfetto/master/tools/record_android_trace
76chmod u+x record_android_trace
77
78# See ./record_android_trace --help for more
79./record_android_trace -o trace_file.perfetto-trace -t 10s -b 32mb sched gfx wm
80```
81
82On Windows:
83
84```bash
85curl -O https://raw.githubusercontent.com/google/perfetto/master/tools/record_android_trace
86python3 record_android_trace -o trace_file.perfetto-trace -t 10s -b 32mb sched gfx wm
87```
88
89**Using the on-device /system/bin/perfetto command**
90
91Or, if you want to use directly the on-device binary do instead:
92
93```bash
94adb shell perfetto -o /data/misc/perfetto-traces/trace_file.perfetto-trace -t 20s sched freq idle am wm gfx view
95```
96
97Caveats when using directly the `adb shell perfetto` workflow:
98
99* Ctrl+C, which normally causes a graceful termination of the trace, is not
100  propagated by ADB when using `adb shell perfetto` but only when using an
101  interactive PTY-based session via `adb shell`.
102* On non-rooted devices before Android 12, the config can only be passed as
103  `cat config | adb shell perfetto -c -` (-: stdin) because of over-restrictive
104  SELinux rules. Since Android 12 `/data/misc/perfetto-configs` can be used for
105  storing configs.
106* On devices before Android 10, adb cannot directly pull
107  `/data/misc/perfetto-traces`. Use
108  `adb shell cat /data/misc/perfetto-traces/trace > trace` to work around.
109* When capturing longer traces, e.g. in the context of benchmarks or CI, use
110  `PID=$(perfetto --background)` and then `kill $PID` to stop.
111
112#### Full trace config
113
114The short syntax allows to enable only a subset of the data sources; for full
115control of the trace config, pass the full trace config in input.
116
117See the [_Trace configuration_ page](/docs/concepts/config.md) and the examples
118in each data source doc page for detailed instructions about how to configure
119all the various knobs of Perfetto.
120
121If you are running on a Mac or Linux host, or are using a bash-based terminal
122on Windows, you can use the following:
123
124WARNING: The below command does not work on Android P because the `--txt` option
125was introduced in Q. The binary protobuf format should be used instead; the
126details of this can be found on the
127[_Trace configuration_ page](https://perfetto.dev/docs/concepts/config#pbtx-vs-binary-format).
128
129```bash
130cat<<EOF>config.pbtx
131duration_ms: 10000
132
133buffers: {
134    size_kb: 8960
135    fill_policy: DISCARD
136}
137buffers: {
138    size_kb: 1280
139    fill_policy: DISCARD
140}
141data_sources: {
142    config {
143        name: "linux.ftrace"
144        ftrace_config {
145            ftrace_events: "sched/sched_switch"
146            ftrace_events: "power/suspend_resume"
147            ftrace_events: "sched/sched_process_exit"
148            ftrace_events: "sched/sched_process_free"
149            ftrace_events: "task/task_newtask"
150            ftrace_events: "task/task_rename"
151            ftrace_events: "ftrace/print"
152            atrace_categories: "gfx"
153            atrace_categories: "view"
154            atrace_categories: "webview"
155            atrace_categories: "camera"
156            atrace_categories: "dalvik"
157            atrace_categories: "power"
158        }
159    }
160}
161data_sources: {
162    config {
163        name: "linux.process_stats"
164        target_buffer: 1
165        process_stats_config {
166            scan_all_processes_on_start: true
167        }
168    }
169}
170EOF
171
172./record_android_trace -c config.pbtx -o trace_file.perfetto-trace
173```
174
175Or alternatively, when using directly the on-device command:
176
177```bash
178cat config.pbtx | adb shell perfetto -c - --txt -o /data/misc/perfetto-traces/trace.perfetto-trace
179```
180
181Alternatively, first push the trace config file and then invoke perfetto:
182
183```bash
184adb push config.pbtx /data/local/tmp/config.pbtx
185adb shell 'cat /data/local/tmp/config.pbtx | perfetto --txt -c - -o /data/misc/perfetto-traces/trace.perfetto-trace'
186```
187
188NOTE: because of strict SELinux rules, on non-rooted builds of Android, passing
189directly the file path as `-c /data/local/tmp/config` will fail, hence the
190`-c -` + stdin piping above. From Android 12 (S), `/data/misc/perfetto-configs/`
191can be used instead.
192
193Pull the file using `adb pull /data/misc/perfetto-traces/trace ~/trace.perfetto-trace`
194and open it in the [Perfetto UI](https://ui.perfetto.dev).
195
196NOTE: On devices before Android 10, adb cannot directly pull
197      `/data/misc/perfetto-traces`. Use
198       `adb shell cat /data/misc/perfetto-traces/trace > trace.perfetto-trace`
199       to work around.
200
201The full reference for the `perfetto` cmdline interface can be found
202[here](/docs/reference/perfetto-cli.md).
203