xref: /aosp_15_r20/system/extras/simpleperf/doc/scripts_reference.md (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker# Scripts reference
2*288bf522SAndroid Build Coastguard Worker
3*288bf522SAndroid Build Coastguard Worker[TOC]
4*288bf522SAndroid Build Coastguard Worker
5*288bf522SAndroid Build Coastguard Worker## Record a profile
6*288bf522SAndroid Build Coastguard Worker
7*288bf522SAndroid Build Coastguard Worker### app_profiler.py
8*288bf522SAndroid Build Coastguard Worker
9*288bf522SAndroid Build Coastguard Worker`app_profiler.py` is used to record profiling data for Android applications and native executables.
10*288bf522SAndroid Build Coastguard Worker
11*288bf522SAndroid Build Coastguard Worker```sh
12*288bf522SAndroid Build Coastguard Worker# Record an Android application.
13*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -p simpleperf.example.cpp
14*288bf522SAndroid Build Coastguard Worker
15*288bf522SAndroid Build Coastguard Worker# Record an Android application with Java code compiled into native instructions.
16*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -p simpleperf.example.cpp --compile_java_code
17*288bf522SAndroid Build Coastguard Worker
18*288bf522SAndroid Build Coastguard Worker# Record the launch of an Activity of an Android application.
19*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -p simpleperf.example.cpp -a .SleepActivity
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard Worker# Record a native process.
22*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -np surfaceflinger
23*288bf522SAndroid Build Coastguard Worker
24*288bf522SAndroid Build Coastguard Worker# Record a native process given its pid.
25*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py --pid 11324
26*288bf522SAndroid Build Coastguard Worker
27*288bf522SAndroid Build Coastguard Worker# Record a command.
28*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -cmd \
29*288bf522SAndroid Build Coastguard Worker    "dex2oat --dex-file=/data/local/tmp/app-debug.apk --oat-file=/data/local/tmp/a.oat"
30*288bf522SAndroid Build Coastguard Worker
31*288bf522SAndroid Build Coastguard Worker# Record an Android application, and use -r to send custom options to the record command.
32*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -p simpleperf.example.cpp \
33*288bf522SAndroid Build Coastguard Worker    -r "-e cpu-clock -g --duration 30"
34*288bf522SAndroid Build Coastguard Worker
35*288bf522SAndroid Build Coastguard Worker# Record both on CPU time and off CPU time.
36*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -p simpleperf.example.cpp \
37*288bf522SAndroid Build Coastguard Worker    -r "-e task-clock -g -f 1000 --duration 10 --trace-offcpu"
38*288bf522SAndroid Build Coastguard Worker
39*288bf522SAndroid Build Coastguard Worker# Save profiling data in a custom file (like perf_custom.data) instead of perf.data.
40*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -p simpleperf.example.cpp -o perf_custom.data
41*288bf522SAndroid Build Coastguard Worker```
42*288bf522SAndroid Build Coastguard Worker
43*288bf522SAndroid Build Coastguard Worker### Profile from launch of an application
44*288bf522SAndroid Build Coastguard Worker
45*288bf522SAndroid Build Coastguard WorkerSometimes we want to profile the launch-time of an application. To support this, we added `--app` in
46*288bf522SAndroid Build Coastguard Workerthe record command. The `--app` option sets the package name of the Android application to profile.
47*288bf522SAndroid Build Coastguard WorkerIf the app is not already running, the record command will poll for the app process in a loop with
48*288bf522SAndroid Build Coastguard Workeran interval of 1ms. So to profile from launch of an application, we can first start the record
49*288bf522SAndroid Build Coastguard Workercommand with `--app`, then start the app. Below is an example.
50*288bf522SAndroid Build Coastguard Worker
51*288bf522SAndroid Build Coastguard Worker```sh
52*288bf522SAndroid Build Coastguard Worker$ ./run_simpleperf_on_device.py record --app simpleperf.example.cpp \
53*288bf522SAndroid Build Coastguard Worker    -g --duration 1 -o /data/local/tmp/perf.data
54*288bf522SAndroid Build Coastguard Worker# Start the app manually or using the `am` command.
55*288bf522SAndroid Build Coastguard Worker```
56*288bf522SAndroid Build Coastguard Worker
57*288bf522SAndroid Build Coastguard WorkerTo make it convenient to use, `app_profiler.py` supports using the `-a` option to start an Activity
58*288bf522SAndroid Build Coastguard Workerafter recording has started.
59*288bf522SAndroid Build Coastguard Worker
60*288bf522SAndroid Build Coastguard Worker```sh
61*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -p simpleperf.example.cpp -a .MainActivity
62*288bf522SAndroid Build Coastguard Worker```
63*288bf522SAndroid Build Coastguard Worker
64*288bf522SAndroid Build Coastguard Worker### api_profiler.py
65*288bf522SAndroid Build Coastguard Worker
66*288bf522SAndroid Build Coastguard Worker`api_profiler.py` is used to control recording in application code. It does preparation work
67*288bf522SAndroid Build Coastguard Workerbefore recording, and collects profiling data files after recording.
68*288bf522SAndroid Build Coastguard Worker
69*288bf522SAndroid Build Coastguard Worker[Here](./android_application_profiling.md#control-recording-in-application-code) are the details.
70*288bf522SAndroid Build Coastguard Worker
71*288bf522SAndroid Build Coastguard Worker### run_simpleperf_without_usb_connection.py
72*288bf522SAndroid Build Coastguard Worker
73*288bf522SAndroid Build Coastguard Worker`run_simpleperf_without_usb_connection.py` records profiling data while the USB cable isn't
74*288bf522SAndroid Build Coastguard Workerconnected. Maybe `api_profiler.py` is more suitable, which also don't need USB cable when recording.
75*288bf522SAndroid Build Coastguard WorkerBelow is an example.
76*288bf522SAndroid Build Coastguard Worker
77*288bf522SAndroid Build Coastguard Worker```sh
78*288bf522SAndroid Build Coastguard Worker$ ./run_simpleperf_without_usb_connection.py start -p simpleperf.example.cpp
79*288bf522SAndroid Build Coastguard Worker# After the command finishes successfully, unplug the USB cable, run the
80*288bf522SAndroid Build Coastguard Worker# SimpleperfExampleCpp app. After a few seconds, plug in the USB cable.
81*288bf522SAndroid Build Coastguard Worker$ ./run_simpleperf_without_usb_connection.py stop
82*288bf522SAndroid Build Coastguard Worker# It may take a while to stop recording. After that, the profiling data is collected in perf.data
83*288bf522SAndroid Build Coastguard Worker# on host.
84*288bf522SAndroid Build Coastguard Worker```
85*288bf522SAndroid Build Coastguard Worker
86*288bf522SAndroid Build Coastguard Worker### binary_cache_builder.py
87*288bf522SAndroid Build Coastguard Worker
88*288bf522SAndroid Build Coastguard WorkerThe `binary_cache` directory is a directory holding binaries needed by a profiling data file. The
89*288bf522SAndroid Build Coastguard Workerbinaries are expected to be unstripped, having debug information and symbol tables. The
90*288bf522SAndroid Build Coastguard Worker`binary_cache` directory is used by report scripts to read symbols of binaries. It is also used by
91*288bf522SAndroid Build Coastguard Worker`report_html.py` to generate annotated source code and disassembly.
92*288bf522SAndroid Build Coastguard Worker
93*288bf522SAndroid Build Coastguard WorkerBy default, `app_profiler.py` builds the binary_cache directory after recording. But we can also
94*288bf522SAndroid Build Coastguard Workerbuild `binary_cache` for existing profiling data files using `binary_cache_builder.py`. It is useful
95*288bf522SAndroid Build Coastguard Workerwhen you record profiling data using `simpleperf record` directly, to do system wide profiling or
96*288bf522SAndroid Build Coastguard Workerrecord without the USB cable connected.
97*288bf522SAndroid Build Coastguard Worker
98*288bf522SAndroid Build Coastguard Worker`binary_cache_builder.py` can either pull binaries from an Android device, or find binaries in
99*288bf522SAndroid Build Coastguard Workerdirectories on the host (via `-lib`).
100*288bf522SAndroid Build Coastguard Worker
101*288bf522SAndroid Build Coastguard Worker```sh
102*288bf522SAndroid Build Coastguard Worker# Generate binary_cache for perf.data, by pulling binaries from the device.
103*288bf522SAndroid Build Coastguard Worker$ ./binary_cache_builder.py
104*288bf522SAndroid Build Coastguard Worker
105*288bf522SAndroid Build Coastguard Worker# Generate binary_cache, by pulling binaries from the device and finding binaries in
106*288bf522SAndroid Build Coastguard Worker# SimpleperfExampleCpp.
107*288bf522SAndroid Build Coastguard Worker$ ./binary_cache_builder.py -lib path_of_SimpleperfExampleCpp
108*288bf522SAndroid Build Coastguard Worker```
109*288bf522SAndroid Build Coastguard Worker
110*288bf522SAndroid Build Coastguard Worker### run_simpleperf_on_device.py
111*288bf522SAndroid Build Coastguard Worker
112*288bf522SAndroid Build Coastguard WorkerThis script pushes the `simpleperf` executable on the device, and run a simpleperf command on the
113*288bf522SAndroid Build Coastguard Workerdevice. It is more convenient than running adb commands manually.
114*288bf522SAndroid Build Coastguard Worker
115*288bf522SAndroid Build Coastguard Worker## Viewing the profile
116*288bf522SAndroid Build Coastguard Worker
117*288bf522SAndroid Build Coastguard WorkerScripts in this section are for viewing the profile or converting profile data into formats used by
118*288bf522SAndroid Build Coastguard Workerexternal UIs. For recommended UIs, see [view_the_profile.md](view_the_profile.md).
119*288bf522SAndroid Build Coastguard Worker
120*288bf522SAndroid Build Coastguard Worker### report.py
121*288bf522SAndroid Build Coastguard Worker
122*288bf522SAndroid Build Coastguard Workerreport.py is a wrapper of the `report` command on the host. It accepts all options of the `report`
123*288bf522SAndroid Build Coastguard Workercommand.
124*288bf522SAndroid Build Coastguard Worker
125*288bf522SAndroid Build Coastguard Worker```sh
126*288bf522SAndroid Build Coastguard Worker# Report call graph
127*288bf522SAndroid Build Coastguard Worker$ ./report.py -g
128*288bf522SAndroid Build Coastguard Worker
129*288bf522SAndroid Build Coastguard Worker# Report call graph in a GUI window implemented by Python Tk.
130*288bf522SAndroid Build Coastguard Worker$ ./report.py -g --gui
131*288bf522SAndroid Build Coastguard Worker```
132*288bf522SAndroid Build Coastguard Worker
133*288bf522SAndroid Build Coastguard Worker### report_html.py
134*288bf522SAndroid Build Coastguard Worker
135*288bf522SAndroid Build Coastguard Worker`report_html.py` generates `report.html` based on the profiling data. Then the `report.html` can show
136*288bf522SAndroid Build Coastguard Workerthe profiling result without depending on other files. So it can be shown in local browsers or
137*288bf522SAndroid Build Coastguard Workerpassed to other machines. Depending on which command-line options are used, the content of the
138*288bf522SAndroid Build Coastguard Worker`report.html` can include: chart statistics, sample table, flamegraphs, annotated source code for
139*288bf522SAndroid Build Coastguard Workereach function, annotated disassembly for each function.
140*288bf522SAndroid Build Coastguard Worker
141*288bf522SAndroid Build Coastguard Worker```sh
142*288bf522SAndroid Build Coastguard Worker# Generate chart statistics, sample table and flamegraphs, based on perf.data.
143*288bf522SAndroid Build Coastguard Worker$ ./report_html.py
144*288bf522SAndroid Build Coastguard Worker
145*288bf522SAndroid Build Coastguard Worker# Add source code.
146*288bf522SAndroid Build Coastguard Worker$ ./report_html.py --add_source_code --source_dirs path_of_SimpleperfExampleCpp
147*288bf522SAndroid Build Coastguard Worker
148*288bf522SAndroid Build Coastguard Worker# Add disassembly.
149*288bf522SAndroid Build Coastguard Worker$ ./report_html.py --add_disassembly
150*288bf522SAndroid Build Coastguard Worker
151*288bf522SAndroid Build Coastguard Worker# Adding disassembly for all binaries can cost a lot of time. So we can choose to only add
152*288bf522SAndroid Build Coastguard Worker# disassembly for selected binaries.
153*288bf522SAndroid Build Coastguard Worker$ ./report_html.py --add_disassembly --binary_filter libgame.so
154*288bf522SAndroid Build Coastguard Worker# Add disassembly and source code for binaries belonging to an app with package name
155*288bf522SAndroid Build Coastguard Worker# com.example.myapp.
156*288bf522SAndroid Build Coastguard Worker$ ./report_html.py --add_source_code --add_disassembly --binary_filter com.example.myapp
157*288bf522SAndroid Build Coastguard Worker
158*288bf522SAndroid Build Coastguard Worker# report_html.py accepts more than one recording data file.
159*288bf522SAndroid Build Coastguard Worker$ ./report_html.py -i perf1.data perf2.data
160*288bf522SAndroid Build Coastguard Worker```
161*288bf522SAndroid Build Coastguard Worker
162*288bf522SAndroid Build Coastguard WorkerBelow is an example of generating html profiling results for SimpleperfExampleCpp.
163*288bf522SAndroid Build Coastguard Worker
164*288bf522SAndroid Build Coastguard Worker```sh
165*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -p simpleperf.example.cpp
166*288bf522SAndroid Build Coastguard Worker$ ./report_html.py --add_source_code --source_dirs path_of_SimpleperfExampleCpp \
167*288bf522SAndroid Build Coastguard Worker    --add_disassembly
168*288bf522SAndroid Build Coastguard Worker```
169*288bf522SAndroid Build Coastguard Worker
170*288bf522SAndroid Build Coastguard WorkerAfter opening the generated [`report.html`](./report_html.html) in a browser, there are several tabs:
171*288bf522SAndroid Build Coastguard Worker
172*288bf522SAndroid Build Coastguard WorkerThe first tab is "Chart Statistics". You can click the pie chart to show the time consumed by each
173*288bf522SAndroid Build Coastguard Workerprocess, thread, library and function.
174*288bf522SAndroid Build Coastguard Worker
175*288bf522SAndroid Build Coastguard WorkerThe second tab is "Sample Table". It shows the time taken by each function. By clicking one row in
176*288bf522SAndroid Build Coastguard Workerthe table, we can jump to a new tab called "Function".
177*288bf522SAndroid Build Coastguard Worker
178*288bf522SAndroid Build Coastguard WorkerThe third tab is "Flamegraph". It shows the graphs generated by [`inferno`](./inferno.md).
179*288bf522SAndroid Build Coastguard Worker
180*288bf522SAndroid Build Coastguard WorkerThe fourth tab is "Function". It only appears when users click a row in the "Sample Table" tab.
181*288bf522SAndroid Build Coastguard WorkerIt shows information of a function, including:
182*288bf522SAndroid Build Coastguard Worker
183*288bf522SAndroid Build Coastguard Worker1. A flamegraph showing functions called by that function.
184*288bf522SAndroid Build Coastguard Worker2. A flamegraph showing functions calling that function.
185*288bf522SAndroid Build Coastguard Worker3. Annotated source code of that function. It only appears when there are source code files for
186*288bf522SAndroid Build Coastguard Worker   that function.
187*288bf522SAndroid Build Coastguard Worker4. Annotated disassembly of that function. It only appears when there are binaries containing that
188*288bf522SAndroid Build Coastguard Worker   function.
189*288bf522SAndroid Build Coastguard Worker
190*288bf522SAndroid Build Coastguard Worker### inferno
191*288bf522SAndroid Build Coastguard Worker
192*288bf522SAndroid Build Coastguard Worker[`inferno`](./inferno.md) is a tool used to generate flamegraph in a html file.
193*288bf522SAndroid Build Coastguard Worker
194*288bf522SAndroid Build Coastguard Worker```sh
195*288bf522SAndroid Build Coastguard Worker# Generate flamegraph based on perf.data.
196*288bf522SAndroid Build Coastguard Worker# On Windows, use inferno.bat instead of ./inferno.sh.
197*288bf522SAndroid Build Coastguard Worker$ ./inferno.sh -sc --record_file perf.data
198*288bf522SAndroid Build Coastguard Worker
199*288bf522SAndroid Build Coastguard Worker# Record a native program and generate flamegraph.
200*288bf522SAndroid Build Coastguard Worker$ ./inferno.sh -np surfaceflinger
201*288bf522SAndroid Build Coastguard Worker```
202*288bf522SAndroid Build Coastguard Worker
203*288bf522SAndroid Build Coastguard Worker### purgatorio
204*288bf522SAndroid Build Coastguard Worker
205*288bf522SAndroid Build Coastguard Worker[`purgatorio`](../scripts/purgatorio/README.md) is a visualization tool to show samples in time order.
206*288bf522SAndroid Build Coastguard Worker
207*288bf522SAndroid Build Coastguard Worker### pprof_proto_generator.py
208*288bf522SAndroid Build Coastguard Worker
209*288bf522SAndroid Build Coastguard WorkerIt converts a profiling data file into `pprof.proto`, a format used by [pprof](https://github.com/google/pprof).
210*288bf522SAndroid Build Coastguard Worker
211*288bf522SAndroid Build Coastguard Worker```sh
212*288bf522SAndroid Build Coastguard Worker# Convert perf.data in the current directory to pprof.proto format.
213*288bf522SAndroid Build Coastguard Worker$ ./pprof_proto_generator.py
214*288bf522SAndroid Build Coastguard Worker# Show report in pdf format.
215*288bf522SAndroid Build Coastguard Worker$ pprof -pdf pprof.profile
216*288bf522SAndroid Build Coastguard Worker
217*288bf522SAndroid Build Coastguard Worker# Show report in html format. To show disassembly, add --tools option like:
218*288bf522SAndroid Build Coastguard Worker#  --tools=objdump:<ndk_path>/toolchains/llvm/prebuilt/linux-x86_64/aarch64-linux-android/bin
219*288bf522SAndroid Build Coastguard Worker# To show annotated source or disassembly, select `top` in the view menu, click a function and
220*288bf522SAndroid Build Coastguard Worker# select `source` or `disassemble` in the view menu.
221*288bf522SAndroid Build Coastguard Worker$ pprof -http=:8080 pprof.profile
222*288bf522SAndroid Build Coastguard Worker```
223*288bf522SAndroid Build Coastguard Worker
224*288bf522SAndroid Build Coastguard Worker### gecko_profile_generator.py
225*288bf522SAndroid Build Coastguard Worker
226*288bf522SAndroid Build Coastguard WorkerConverts `perf.data` to [Gecko Profile
227*288bf522SAndroid Build Coastguard WorkerFormat](https://github.com/firefox-devtools/profiler/blob/main/docs-developer/gecko-profile-format.md),
228*288bf522SAndroid Build Coastguard Workera format readable by both the [Perfetto UI](https://ui.perfetto.dev/) and
229*288bf522SAndroid Build Coastguard Worker[Firefox Profiler](https://profiler.firefox.com/).
230*288bf522SAndroid Build Coastguard Worker[View the profile](view_the_profile.md) provides more information on both options.
231*288bf522SAndroid Build Coastguard Worker
232*288bf522SAndroid Build Coastguard WorkerUsage:
233*288bf522SAndroid Build Coastguard Worker
234*288bf522SAndroid Build Coastguard Worker```
235*288bf522SAndroid Build Coastguard Worker# Record a profile of your application
236*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py -p simpleperf.example.cpp
237*288bf522SAndroid Build Coastguard Worker
238*288bf522SAndroid Build Coastguard Worker# Convert and gzip.
239*288bf522SAndroid Build Coastguard Worker$ ./gecko_profile_generator.py -i perf.data | gzip > gecko-profile.json.gz
240*288bf522SAndroid Build Coastguard Worker```
241*288bf522SAndroid Build Coastguard Worker
242*288bf522SAndroid Build Coastguard WorkerThen open `gecko-profile.json.gz` in https://ui.perfetto.dev/ or
243*288bf522SAndroid Build Coastguard Workerhttps://profiler.firefox.com/.
244*288bf522SAndroid Build Coastguard Worker
245*288bf522SAndroid Build Coastguard Worker### report_sample.py
246*288bf522SAndroid Build Coastguard Worker
247*288bf522SAndroid Build Coastguard Worker`report_sample.py` converts a profiling data file into the `perf script` text format output by
248*288bf522SAndroid Build Coastguard Worker`linux-perf-tool`.
249*288bf522SAndroid Build Coastguard Worker
250*288bf522SAndroid Build Coastguard WorkerThis format can be imported into:
251*288bf522SAndroid Build Coastguard Worker
252*288bf522SAndroid Build Coastguard Worker- [Perfetto](https://ui.perfetto.dev)
253*288bf522SAndroid Build Coastguard Worker- [FlameGraph](https://github.com/brendangregg/FlameGraph)
254*288bf522SAndroid Build Coastguard Worker- [Flamescope](https://github.com/Netflix/flamescope)
255*288bf522SAndroid Build Coastguard Worker- [Firefox
256*288bf522SAndroid Build Coastguard Worker  Profiler](https://github.com/firefox-devtools/profiler/blob/main/docs-user/guide-perf-profiling.md),
257*288bf522SAndroid Build Coastguard Worker  but prefer using `gecko_profile_generator.py`.
258*288bf522SAndroid Build Coastguard Worker- [Speedscope](https://github.com/jlfwong/speedscope/wiki/Importing-from-perf-(linux))
259*288bf522SAndroid Build Coastguard Worker
260*288bf522SAndroid Build Coastguard Worker```sh
261*288bf522SAndroid Build Coastguard Worker# Record a profile to perf.data
262*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py <args>
263*288bf522SAndroid Build Coastguard Worker
264*288bf522SAndroid Build Coastguard Worker# Convert perf.data in the current directory to a format used by FlameGraph.
265*288bf522SAndroid Build Coastguard Worker$ ./report_sample.py --symfs binary_cache >out.perf
266*288bf522SAndroid Build Coastguard Worker
267*288bf522SAndroid Build Coastguard Worker$ git clone https://github.com/brendangregg/FlameGraph.git
268*288bf522SAndroid Build Coastguard Worker$ FlameGraph/stackcollapse-perf.pl out.perf >out.folded
269*288bf522SAndroid Build Coastguard Worker$ FlameGraph/flamegraph.pl out.folded >a.svg
270*288bf522SAndroid Build Coastguard Worker```
271*288bf522SAndroid Build Coastguard Worker
272*288bf522SAndroid Build Coastguard Worker### stackcollapse.py
273*288bf522SAndroid Build Coastguard Worker
274*288bf522SAndroid Build Coastguard Worker`stackcollapse.py` converts a profiling data file (`perf.data`) to [Brendan
275*288bf522SAndroid Build Coastguard WorkerGregg's "Folded Stacks"
276*288bf522SAndroid Build Coastguard Workerformat](https://queue.acm.org/detail.cfm?id=2927301#:~:text=The%20folded%20stack%2Dtrace%20format,trace%2C%20followed%20by%20a%20semicolon).
277*288bf522SAndroid Build Coastguard Worker
278*288bf522SAndroid Build Coastguard WorkerFolded Stacks are lines of semicolon-delimited stack frames, root to leaf,
279*288bf522SAndroid Build Coastguard Workerfollowed by a count of events sampled in that stack, e.g.:
280*288bf522SAndroid Build Coastguard Worker
281*288bf522SAndroid Build Coastguard Worker```
282*288bf522SAndroid Build Coastguard WorkerBusyThread;__start_thread;__pthread_start(void*);java.lang.Thread.run 17889729
283*288bf522SAndroid Build Coastguard Worker```
284*288bf522SAndroid Build Coastguard Worker
285*288bf522SAndroid Build Coastguard WorkerAll similar stacks are aggregated and sample timestamps are unused.
286*288bf522SAndroid Build Coastguard Worker
287*288bf522SAndroid Build Coastguard WorkerFolded Stacks format is readable by:
288*288bf522SAndroid Build Coastguard Worker
289*288bf522SAndroid Build Coastguard Worker- The [FlameGraph](https://github.com/brendangregg/FlameGraph) toolkit
290*288bf522SAndroid Build Coastguard Worker- [Inferno](https://github.com/jonhoo/inferno) (Rust port of FlameGraph)
291*288bf522SAndroid Build Coastguard Worker- [Speedscope](https://speedscope.app/)
292*288bf522SAndroid Build Coastguard Worker
293*288bf522SAndroid Build Coastguard WorkerExample:
294*288bf522SAndroid Build Coastguard Worker
295*288bf522SAndroid Build Coastguard Worker```sh
296*288bf522SAndroid Build Coastguard Worker# Record a profile to perf.data
297*288bf522SAndroid Build Coastguard Worker$ ./app_profiler.py <args>
298*288bf522SAndroid Build Coastguard Worker
299*288bf522SAndroid Build Coastguard Worker# Convert to Folded Stacks format
300*288bf522SAndroid Build Coastguard Worker$ ./stackcollapse.py --kernel --jit | gzip > profile.folded.gz
301*288bf522SAndroid Build Coastguard Worker
302*288bf522SAndroid Build Coastguard Worker# Visualise with FlameGraph with Java Stacks and nanosecond times
303*288bf522SAndroid Build Coastguard Worker$ git clone https://github.com/brendangregg/FlameGraph.git
304*288bf522SAndroid Build Coastguard Worker$ gunzip -c profile.folded.gz \
305*288bf522SAndroid Build Coastguard Worker    | FlameGraph/flamegraph.pl --color=java --countname=ns \
306*288bf522SAndroid Build Coastguard Worker    > profile.svg
307*288bf522SAndroid Build Coastguard Worker```
308*288bf522SAndroid Build Coastguard Worker
309*288bf522SAndroid Build Coastguard Worker## simpleperf_report_lib.py
310*288bf522SAndroid Build Coastguard Worker
311*288bf522SAndroid Build Coastguard Worker`simpleperf_report_lib.py` is a Python library used to parse profiling data files generated by the
312*288bf522SAndroid Build Coastguard Workerrecord command. Internally, it uses libsimpleperf_report.so to do the work. Generally, for each
313*288bf522SAndroid Build Coastguard Workerprofiling data file, we create an instance of ReportLib, pass it the file path (via SetRecordFile).
314*288bf522SAndroid Build Coastguard WorkerThen we can read all samples through GetNextSample(). For each sample, we can read its event info
315*288bf522SAndroid Build Coastguard Worker(via GetEventOfCurrentSample), symbol info (via GetSymbolOfCurrentSample) and call chain info
316*288bf522SAndroid Build Coastguard Worker(via GetCallChainOfCurrentSample). We can also get some global information, like record options
317*288bf522SAndroid Build Coastguard Worker(via GetRecordCmd), the arch of the device (via GetArch) and meta strings (via MetaInfo).
318*288bf522SAndroid Build Coastguard Worker
319*288bf522SAndroid Build Coastguard WorkerExamples of using `simpleperf_report_lib.py` are in `report_sample.py`, `report_html.py`,
320*288bf522SAndroid Build Coastguard Worker`pprof_proto_generator.py` and `inferno/inferno.py`.
321*288bf522SAndroid Build Coastguard Worker
322*288bf522SAndroid Build Coastguard Worker## ipc.py
323*288bf522SAndroid Build Coastguard Worker`ipc.py`captures the instructions per cycle (IPC) of the system during a specified duration.
324*288bf522SAndroid Build Coastguard Worker
325*288bf522SAndroid Build Coastguard WorkerExample:
326*288bf522SAndroid Build Coastguard Worker```sh
327*288bf522SAndroid Build Coastguard Worker./ipc.py
328*288bf522SAndroid Build Coastguard Worker./ipc.py 2 20          # Set interval to 2 secs and total duration to 20 secs
329*288bf522SAndroid Build Coastguard Worker./ipc.py -p 284 -C 4   # Only profile the PID 284 while running on core 4
330*288bf522SAndroid Build Coastguard Worker./ipc.py -c 'sleep 5'  # Only profile the command to run
331*288bf522SAndroid Build Coastguard Worker```
332*288bf522SAndroid Build Coastguard Worker
333*288bf522SAndroid Build Coastguard WorkerThe results look like:
334*288bf522SAndroid Build Coastguard Worker```
335*288bf522SAndroid Build Coastguard WorkerK_CYCLES   K_INSTR      IPC
336*288bf522SAndroid Build Coastguard Worker36840      14138       0.38
337*288bf522SAndroid Build Coastguard Worker70701      27743       0.39
338*288bf522SAndroid Build Coastguard Worker104562     41350       0.40
339*288bf522SAndroid Build Coastguard Worker138264     54916       0.40
340*288bf522SAndroid Build Coastguard Worker```
341*288bf522SAndroid Build Coastguard Worker
342*288bf522SAndroid Build Coastguard Worker## sample_filter.py
343*288bf522SAndroid Build Coastguard Worker
344*288bf522SAndroid Build Coastguard Worker`sample_filter.py` generates sample filter files as documented in [sample_filter.md](https://android.googlesource.com/platform/system/extras/+/refs/heads/main/simpleperf/doc/sample_filter.md).
345*288bf522SAndroid Build Coastguard WorkerA filter file can be passed in `--filter-file` when running report scripts.
346*288bf522SAndroid Build Coastguard Worker
347*288bf522SAndroid Build Coastguard WorkerFor example, it can be used to split a large recording file into several report files.
348*288bf522SAndroid Build Coastguard Worker
349*288bf522SAndroid Build Coastguard Worker```sh
350*288bf522SAndroid Build Coastguard Worker$ sample_filter.py -i perf.data --split-time-range 2 -o sample_filter
351*288bf522SAndroid Build Coastguard Worker$ gecko_profile_generator.py -i perf.data --filter-file sample_filter_part1 \
352*288bf522SAndroid Build Coastguard Worker    | gzip >profile-part1.json.gz
353*288bf522SAndroid Build Coastguard Worker$ gecko_profile_generator.py -i perf.data --filter-file sample_filter_part2 \
354*288bf522SAndroid Build Coastguard Worker    | gzip >profile-part2.json.gz
355*288bf522SAndroid Build Coastguard Worker```
356