xref: /aosp_15_r20/external/perfetto/examples/sdk/README.md (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1*6dbdd20aSAndroid Build Coastguard Worker# Perfetto SDK example project
2*6dbdd20aSAndroid Build Coastguard Worker
3*6dbdd20aSAndroid Build Coastguard WorkerThis directory contains an example project using the [Perfetto
4*6dbdd20aSAndroid Build Coastguard WorkerSDK](https://perfetto.dev/docs/instrumentation/tracing-sdk). It demonstrates
5*6dbdd20aSAndroid Build Coastguard Workerhow to instrument your application with track events to give more context in
6*6dbdd20aSAndroid Build Coastguard Workerdeveloping, debugging and performance analysis.
7*6dbdd20aSAndroid Build Coastguard Worker
8*6dbdd20aSAndroid Build Coastguard WorkerDependencies:
9*6dbdd20aSAndroid Build Coastguard Worker
10*6dbdd20aSAndroid Build Coastguard Worker- [CMake](https://cmake.org/)
11*6dbdd20aSAndroid Build Coastguard Worker- C++17
12*6dbdd20aSAndroid Build Coastguard Worker
13*6dbdd20aSAndroid Build Coastguard Worker## Building
14*6dbdd20aSAndroid Build Coastguard Worker
15*6dbdd20aSAndroid Build Coastguard WorkerFirst, check out the latest Perfetto release:
16*6dbdd20aSAndroid Build Coastguard Worker
17*6dbdd20aSAndroid Build Coastguard Worker```bash
18*6dbdd20aSAndroid Build Coastguard Workergit clone https://android.googlesource.com/platform/external/perfetto -b v48.1
19*6dbdd20aSAndroid Build Coastguard Worker```
20*6dbdd20aSAndroid Build Coastguard Worker
21*6dbdd20aSAndroid Build Coastguard WorkerThen, build using CMake:
22*6dbdd20aSAndroid Build Coastguard Worker
23*6dbdd20aSAndroid Build Coastguard Worker```bash
24*6dbdd20aSAndroid Build Coastguard Workercd perfetto/examples/sdk
25*6dbdd20aSAndroid Build Coastguard Workercmake -B build
26*6dbdd20aSAndroid Build Coastguard Workercmake --build build
27*6dbdd20aSAndroid Build Coastguard Worker```
28*6dbdd20aSAndroid Build Coastguard Worker
29*6dbdd20aSAndroid Build Coastguard WorkerNote: If amalgamated source files are not present, generate them using
30*6dbdd20aSAndroid Build Coastguard Worker`cd perfetto ; tools/gen_amalgamated --output sdk/perfetto`.
31*6dbdd20aSAndroid Build Coastguard Worker[Learn more](https://perfetto.dev/docs/contributing/sdk-releasing#building-and-tagging-the-release)
32*6dbdd20aSAndroid Build Coastguard Workerat the release section.
33*6dbdd20aSAndroid Build Coastguard Worker
34*6dbdd20aSAndroid Build Coastguard Worker## Track event example
35*6dbdd20aSAndroid Build Coastguard Worker
36*6dbdd20aSAndroid Build Coastguard WorkerThe [basic example](example.cc) shows how to instrument an app with track
37*6dbdd20aSAndroid Build Coastguard Workerevents. Run it with:
38*6dbdd20aSAndroid Build Coastguard Worker
39*6dbdd20aSAndroid Build Coastguard Worker```bash
40*6dbdd20aSAndroid Build Coastguard Workerbuild/example
41*6dbdd20aSAndroid Build Coastguard Worker```
42*6dbdd20aSAndroid Build Coastguard Worker
43*6dbdd20aSAndroid Build Coastguard WorkerThe program will create a trace file in `example.perfetto-trace`, which can be
44*6dbdd20aSAndroid Build Coastguard Workerdirectly opened in the [Perfetto UI](https://ui.perfetto.dev). The result
45*6dbdd20aSAndroid Build Coastguard Workershould look like this:
46*6dbdd20aSAndroid Build Coastguard Worker
47*6dbdd20aSAndroid Build Coastguard Worker![Example trace loaded in the Perfetto UI](
48*6dbdd20aSAndroid Build Coastguard Worker  example.png "Example trace loaded in the Perfetto UI")
49*6dbdd20aSAndroid Build Coastguard Worker
50*6dbdd20aSAndroid Build Coastguard Worker## System-wide example
51*6dbdd20aSAndroid Build Coastguard Worker
52*6dbdd20aSAndroid Build Coastguard WorkerWhile the above example only records events from the program itself, with
53*6dbdd20aSAndroid Build Coastguard WorkerPerfetto it's also possible to combine app trace events with system-wide
54*6dbdd20aSAndroid Build Coastguard Workerprofiling data (e.g., ftrace on Linux). The repository has a [second
55*6dbdd20aSAndroid Build Coastguard Workerexample](example_system_wide.cc) which demonstrates this on Android.
56*6dbdd20aSAndroid Build Coastguard Worker
57*6dbdd20aSAndroid Build Coastguard WorkerRequirements:
58*6dbdd20aSAndroid Build Coastguard Worker- [Android NDK](https://developer.android.com/ndk)
59*6dbdd20aSAndroid Build Coastguard Worker- A device running Android Pie or newer
60*6dbdd20aSAndroid Build Coastguard Worker
61*6dbdd20aSAndroid Build Coastguard Worker> Tip: It's also possible to sideload Perfetto on pre-Pie Android devices.
62*6dbdd20aSAndroid Build Coastguard Worker> See the [build
63*6dbdd20aSAndroid Build Coastguard Worker> instructions](https://perfetto.dev/docs/contributing/build-instructions).
64*6dbdd20aSAndroid Build Coastguard Worker
65*6dbdd20aSAndroid Build Coastguard WorkerTo build:
66*6dbdd20aSAndroid Build Coastguard Worker
67*6dbdd20aSAndroid Build Coastguard Worker```bash
68*6dbdd20aSAndroid Build Coastguard Workerexport NDK=/path/to/ndk
69*6dbdd20aSAndroid Build Coastguard Workercmake -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
70*6dbdd20aSAndroid Build Coastguard Worker      -DANDROID_ABI=arm64-v8a \
71*6dbdd20aSAndroid Build Coastguard Worker      -DANDROID_PLATFORM=android-21 \
72*6dbdd20aSAndroid Build Coastguard Worker      -DANDROID_LD=lld \
73*6dbdd20aSAndroid Build Coastguard Worker      -DCMAKE_BUILD_TYPE=Release \
74*6dbdd20aSAndroid Build Coastguard Worker      -B build_android
75*6dbdd20aSAndroid Build Coastguard Workercmake --build build_android
76*6dbdd20aSAndroid Build Coastguard Worker```
77*6dbdd20aSAndroid Build Coastguard Worker
78*6dbdd20aSAndroid Build Coastguard WorkerNext, plug in an Android device into a USB port, download the example and run
79*6dbdd20aSAndroid Build Coastguard Workerit while simultaneously recording a trace using the `perfetto` command line
80*6dbdd20aSAndroid Build Coastguard Workertool:
81*6dbdd20aSAndroid Build Coastguard Worker
82*6dbdd20aSAndroid Build Coastguard Worker```bash
83*6dbdd20aSAndroid Build Coastguard Workeradb push build_android/example_system_wide ../system_wide_trace_cfg.pbtxt \
84*6dbdd20aSAndroid Build Coastguard Worker         /data/local/tmp/
85*6dbdd20aSAndroid Build Coastguard Workeradb shell "\
86*6dbdd20aSAndroid Build Coastguard Worker    cd /data/local/tmp; \
87*6dbdd20aSAndroid Build Coastguard Worker    rm -f /data/misc/perfetto-traces/example_system_wide.perfetto-trace; \
88*6dbdd20aSAndroid Build Coastguard Worker    cat system_wide_trace_cfg.pbtxt | \
89*6dbdd20aSAndroid Build Coastguard Worker        perfetto --config - --txt --background \
90*6dbdd20aSAndroid Build Coastguard Worker                 -o
91*6dbdd20aSAndroid Build Coastguard Worker                 /data/misc/perfetto-traces/example_system_wide.perfetto-trace; \
92*6dbdd20aSAndroid Build Coastguard Worker    ./example_system_wide"
93*6dbdd20aSAndroid Build Coastguard Worker```
94*6dbdd20aSAndroid Build Coastguard Worker
95*6dbdd20aSAndroid Build Coastguard WorkerFinally, retrieve the resulting trace:
96*6dbdd20aSAndroid Build Coastguard Worker
97*6dbdd20aSAndroid Build Coastguard Worker```bash
98*6dbdd20aSAndroid Build Coastguard Workeradb pull /data/misc/perfetto-traces/example_system_wide.perfetto-trace
99*6dbdd20aSAndroid Build Coastguard Worker```
100*6dbdd20aSAndroid Build Coastguard Worker
101*6dbdd20aSAndroid Build Coastguard WorkerWhen opened in the Perfetto UI, the trace now shows additional contextual
102*6dbdd20aSAndroid Build Coastguard Workerinformation such as CPU frequencies and kernel scheduler information.
103*6dbdd20aSAndroid Build Coastguard Worker
104*6dbdd20aSAndroid Build Coastguard Worker![Example system wide-trace loaded in the Perfetto UI](
105*6dbdd20aSAndroid Build Coastguard Worker  example_system_wide.png "Example system-wide trace in the Perfetto UI")
106*6dbdd20aSAndroid Build Coastguard Worker
107*6dbdd20aSAndroid Build Coastguard Worker> Tip: You can generate a new trace config with additional data sources using
108*6dbdd20aSAndroid Build Coastguard Worker> the [Perfetto UI](https://ui.perfetto.dev/#!/record) and replace
109*6dbdd20aSAndroid Build Coastguard Worker> `system_wide_trace_cfg.pbtxt` with the [generated config](
110*6dbdd20aSAndroid Build Coastguard Worker> https://ui.perfetto.dev/#!/record/instructions).
111*6dbdd20aSAndroid Build Coastguard Worker
112*6dbdd20aSAndroid Build Coastguard Worker## Custom data source example
113*6dbdd20aSAndroid Build Coastguard Worker
114*6dbdd20aSAndroid Build Coastguard WorkerThe [final example](example_custom_data_source.cc) shows how to use an
115*6dbdd20aSAndroid Build Coastguard Workerapplication defined data source to emit custom, strongly typed data into a
116*6dbdd20aSAndroid Build Coastguard Workertrace. Run it with:
117*6dbdd20aSAndroid Build Coastguard Worker
118*6dbdd20aSAndroid Build Coastguard Worker```bash
119*6dbdd20aSAndroid Build Coastguard Workerbuild/example_custom_data_source
120*6dbdd20aSAndroid Build Coastguard Worker```
121*6dbdd20aSAndroid Build Coastguard Worker
122*6dbdd20aSAndroid Build Coastguard WorkerThe program generates a trace file in `example_custom_data_source.perfetto-trace`,
123*6dbdd20aSAndroid Build Coastguard Workerwhich we can examine using Perfetto's `traceconv` tool to show the trace
124*6dbdd20aSAndroid Build Coastguard Workerpacket written by the custom data source:
125*6dbdd20aSAndroid Build Coastguard Worker
126*6dbdd20aSAndroid Build Coastguard Worker```bash
127*6dbdd20aSAndroid Build Coastguard Workertraceconv text example_custom_data_source.perfetto-trace
128*6dbdd20aSAndroid Build Coastguard Worker...
129*6dbdd20aSAndroid Build Coastguard Workerpacket {
130*6dbdd20aSAndroid Build Coastguard Worker  trusted_uid: 0
131*6dbdd20aSAndroid Build Coastguard Worker  timestamp: 42
132*6dbdd20aSAndroid Build Coastguard Worker  trusted_packet_sequence_id: 2
133*6dbdd20aSAndroid Build Coastguard Worker  previous_packet_dropped: true
134*6dbdd20aSAndroid Build Coastguard Worker  for_testing {
135*6dbdd20aSAndroid Build Coastguard Worker    str: "Hello world!"
136*6dbdd20aSAndroid Build Coastguard Worker  }
137*6dbdd20aSAndroid Build Coastguard Worker}
138*6dbdd20aSAndroid Build Coastguard Worker...
139*6dbdd20aSAndroid Build Coastguard Worker```
140