xref: /aosp_15_r20/external/perfetto/docs/case-studies/android-boot-tracing.md (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1# Recording traces on Android boot
2
3Since Android 13 (T), perfetto can be configured to automatically start
4recording traces on boot. This can be useful to profile the boot process.
5
6## Steps
7
8* Create a file with the desired [trace configuration](/docs/concepts/config.md)
9  in Text format (not binary). Example (more in [/test/configs/](/test/configs/)):
10  ```
11  # One buffer allocated within the central tracing binary for the entire trace,
12  # shared by the two data sources below.
13  buffers {
14    size_kb: 32768
15    fill_policy: DISCARD
16  }
17
18  # Ftrace data from the kernel, mainly the process scheduling events.
19  data_sources {
20    config {
21      name: "linux.ftrace"
22      target_buffer: 0
23      ftrace_config {
24        ftrace_events: "sched_switch"
25        ftrace_events: "sched_waking"
26        ftrace_events: "sched_wakeup_new"
27
28        ftrace_events: "task_newtask"
29        ftrace_events: "task_rename"
30
31        ftrace_events: "sched_process_exec"
32        ftrace_events: "sched_process_exit"
33        ftrace_events: "sched_process_fork"
34        ftrace_events: "sched_process_free"
35        ftrace_events: "sched_process_hang"
36        ftrace_events: "sched_process_wait"
37      }
38    }
39  }
40
41  # Resolve process commandlines and parent/child relationships, to better
42  # interpret the ftrace events, which are in terms of pids.
43  data_sources {
44    config {
45      name: "linux.process_stats"
46      target_buffer: 0
47    }
48  }
49
50  # 10s trace, but can be stopped prematurely via `adb shell pkill -u perfetto`.
51  duration_ms: 10000
52  ```
53* Put the file on the device at `/data/misc/perfetto-configs/boottrace.pbtxt`:
54  ```
55  adb push <yourfile> /data/misc/perfetto-configs/boottrace.pbtxt
56  ```
57* Enable the `perfetto_trace_on_boot` service:
58  ```
59  adb shell setprop persist.debug.perfetto.boottrace 1
60  ```
61  The property is reset on boot. In order to trace the next boot, the command
62  must be reissued.
63* Reboot the device.
64* The output trace will be written at
65  `/data/misc/perfetto-traces/boottrace.perfetto-trace`. The file will be
66  removed before a new trace is started.
67  ```
68  adb pull /data/misc/perfetto-traces/boottrace.perfetto-trace
69  ```
70  **N.B.:** The file will appear after the recording has stopped (be sure to set
71  `duration_ms` to a reasonable value in the config) or after the first
72  `flush_period_ms`.
73* `boottrace.perfetto-trace` can now be opened in
74  [ui.perfetto.dev](https://ui.perfetto.dev/)
75
76## Implementation details
77* The trace will start only after persistent properties are loaded, which
78  happens after /data has been mounted.
79* The command to start the trace is implemented as oneshot init service.
80