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