README.md
1# Simpleperf
2
3Simpleperf is a native CPU profiling tool for Android. It can be used to profile
4both Android applications and native processes running on Android. It can
5profile both Java and C++ code on Android. The simpleperf executable can run on Android >=L,
6and Python scripts can be used on Android >= N.
7
8Simpleperf is part of the Android Open Source Project.
9The source code is [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/).
10The latest document is [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/README.md).
11
12## Table of Contents
13
14- [Simpleperf](#simpleperf)
15 - [Table of Contents](#table-of-contents)
16 - [Introduction](#introduction)
17 - [Tools in simpleperf](#tools-in-simpleperf)
18 - [Android application profiling](#android-application-profiling)
19 - [Android platform profiling](#android-platform-profiling)
20 - [Executable commands reference](#executable-commands-reference)
21 - [Scripts reference](#scripts-reference)
22 - [Answers to common issues](#answers-to-common-issues)
23 - [Why we suggest profiling on Android >= N devices?](#why-we-suggest-profiling-on-android-gt-n-devices)
24 - [Suggestions about recording call graphs](#suggestions-about-recording-call-graphs)
25 - [How to solve missing symbols in report?](#how-to-solve-missing-symbols-in-report)
26 - [Fix broken callchain stopped at C functions](#fix-broken-callchain-stopped-at-c-functions)
27 - [Show annotated source code and disassembly](#show-annotated-source-code-and-disassembly)
28 - [Bugs and contribution](#bugs-and-contribution)
29
30
31## Introduction
32
33Simpleperf contains two parts: the simpleperf executable and Python scripts.
34
35The simpleperf executable works similar to linux-tools-perf, but has some specific features for
36the Android profiling environment:
37
381. It collects more info in profiling data. Since the common workflow is "record on the device, and
39 report on the host", simpleperf not only collects samples in profiling data, but also collects
40 needed symbols, device info and recording time.
41
422. It delivers new features for recording.
43 a. When recording dwarf based call graph, simpleperf unwinds the stack before writing a sample
44 to file. This is to save storage space on the device.
45 b. Support tracing both on CPU time and off CPU time with --trace-offcpu option.
46 c. Support recording callgraphs of JITed and interpreted Java code on Android >= P.
47
483. It relates closely to the Android platform.
49 a. Is aware of Android environment, like using system properties to enable profiling, using
50 run-as to profile in application's context.
51 b. Supports reading symbols and debug information from the .gnu_debugdata section, because
52 system libraries are built with .gnu_debugdata section starting from Android O.
53 c. Supports profiling shared libraries embedded in apk files.
54 d. It uses the standard Android stack unwinder, so its results are consistent with all other
55 Android tools.
56
574. It builds executables and shared libraries for different usages.
58 a. Builds static executables on the device. Since static executables don't rely on any library,
59 simpleperf executables can be pushed on any Android device and used to record profiling data.
60 b. Builds executables on different hosts: Linux, Mac and Windows. These executables can be used
61 to report on hosts.
62 c. Builds report shared libraries on different hosts. The report library is used by different
63 Python scripts to parse profiling data.
64
65Detailed documentation for the simpleperf executable is [here](#executable-commands-reference).
66
67Python scripts are split into three parts according to their functions:
68
691. Scripts used for recording, like app_profiler.py, run_simpleperf_without_usb_connection.py.
70
712. Scripts used for reporting, like report.py, report_html.py, inferno.
72
733. Scripts used for parsing profiling data, like simpleperf_report_lib.py.
74
75Detailed documentation for the Python scripts is [here](#scripts-reference).
76
77
78## Tools in simpleperf
79
80The simpleperf executables and Python scripts are located in simpleperf/ in ndk releases, and in
81system/extras/simpleperf/scripts/ in AOSP. Their functions are listed below.
82
83bin/: contains executables and shared libraries.
84
85bin/android/${arch}/simpleperf: static simpleperf executables used on the device.
86
87bin/${host}/${arch}/simpleperf: simpleperf executables used on the host, only supports reporting.
88
89bin/${host}/${arch}/libsimpleperf_report.${so/dylib/dll}: report shared libraries used on the host.
90
91*.py, inferno: Python scripts used for recording and reporting.
92
93
94## Android application profiling
95
96See [android_application_profiling.md](./android_application_profiling.md).
97
98
99## Android platform profiling
100
101See [android_platform_profiling.md](./android_platform_profiling.md).
102
103
104## Executable commands reference
105
106See [executable_commands_reference.md](./executable_commands_reference.md).
107
108
109## Scripts reference
110
111See [scripts_reference.md](./scripts_reference.md).
112
113
114## Answers to common issues
115
116### Why we suggest profiling on Android >= N devices?
117```
1181. Running on a device reflects a real running situation, so we suggest
119profiling on real devices instead of emulators.
1202. To profile Java code, we need ART running in oat mode, which is only
121available >= L for rooted devices, and >= N for non-rooted devices.
1223. Old Android versions are likely to be shipped with old kernels (< 3.18),
123which may not support profiling features like recording dwarf based call graphs.
1244. Old Android versions are likely to be shipped with Arm32 chips. In Arm32
125mode, recording stack frame based call graphs doesn't work well.
126```
127
128### Suggestions about recording call graphs
129
130Below is our experiences of dwarf based call graphs and stack frame based call graphs.
131
132dwarf based call graphs:
1331. Need support of debug information in binaries.
1342. Behave normally well on both ARM and ARM64, for both fully compiled Java code and C++ code.
1353. Can only unwind 64K stack for each sample. So usually can't show complete flamegraph. But
136 probably is enough for users to identify hot places.
1374. Take more CPU time than stack frame based call graphs. So the sample frequency is suggested
138 to be 1000 Hz. Thus at most 1000 samples per second.
139
140stack frame based call graphs:
1411. Need support of stack frame registers.
1422. Don't work well on ARM. Because ARM is short of registers, and ARM and THUMB code have different
143 stack frame registers. So the kernel can't unwind user stack containing both ARM/THUMB code.
1443. Also don't work well on fully compiled Java code on ARM64. Because the ART compiler doesn't
145 reserve stack frame registers.
1464. Work well when profiling native programs on ARM64. One example is profiling surfacelinger. And
147 usually shows complete flamegraph when it works well.
1485. Take less CPU time than dwarf based call graphs. So the sample frequency can be 4000 Hz or
149 higher.
150
151So if you need to profile code on ARM or profile fully compiled Java code, dwarf based call graphs
152may be better. If you need to profile C++ code on ARM64, stack frame based call graphs may be
153better. After all, you can always try dwarf based call graph first, because it always produces
154reasonable results when given unstripped binaries properly. If it doesn't work well enough, then
155try stack frame based call graphs instead.
156
157Simpleperf may need unstripped native binaries on the device to generate good dwarf based call
158graphs. It can be supported in two ways:
1591. Use unstripped native binaries when building the apk, as [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/demo/SimpleperfExampleWithNative/app/profiling.gradle).
1602. Download unstripped native libraries on device, as [here](#fix-broken-callchain-stopped-at-c-functions).
161
162### How to solve missing symbols in report?
163
164The simpleperf record command collects symbols on device in perf.data. But if the native libraries
165you use on device are stripped, this will result in a lot of unknown symbols in the report. A
166solution is to build binary_cache on host.
167
168```sh
169# Collect binaries needed by perf.data in binary_cache/.
170$ python binary_cache_builder.py -lib NATIVE_LIB_DIR,...
171```
172
173The NATIVE_LIB_DIRs passed in -lib option are the directories containing unstripped native
174libraries on host. After running it, the native libraries containing symbol tables are collected
175in binary_cache/ for use when reporting.
176
177```sh
178$ python report.py --symfs binary_cache
179
180# report_html.py searches binary_cache/ automatically, so you don't need to
181# pass it any argument.
182$ python report_html.py
183```
184
185### Fix broken callchain stopped at C functions
186
187When using dwarf based call graphs, simpleperf generates callchains during recording to save space.
188The debug information needed to unwind C functions is in .debug_frame section, which is usually
189stripped in native libraries in apks. To fix this, we can download unstripped version of native
190libraries on device, and ask simpleperf to use them when recording.
191
192To use simpleperf directly:
193
194```sh
195# create native_libs dir on device, and push unstripped libs in it (nested dirs are not supported).
196$ adb shell mkdir /data/local/tmp/native_libs
197$ adb push <unstripped_dir>/*.so /data/local/tmp/native_libs
198# run simpleperf record with --symfs option.
199$ adb shell simpleperf record xxx --symfs /data/local/tmp/native_libs
200```
201
202To use app_profiler.py:
203
204```sh
205$ python app_profiler.py -lib <unstripped_dir>
206```
207
208### Show annotated source code and disassembly
209
210To show hot places at source code and instruction level, we need to show source code and
211disassembly with event count annotation. Simpleperf supports showing annotated source code and
212disassembly for C++ code and fully compiled Java code. Simpleperf supports two ways to do it:
213
2141. Through report_html.py:
215
216 a. Generate perf.data and pull it on host.
217 b. Generate binary_cache, containing elf files with debug information. Use -lib option to add
218 libs with debug info. Do it with
219 `binary_cache_builder.py -i perf.data -lib <dir_of_lib_with_debug_info>`.
220 c. Use report_html.py to generate report.html with annotated source code and disassembly,
221 as described [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/scripts_reference.md#report_html_py).
222
2232. Through pprof.
224
225 a. Generate perf.data and binary_cache as above.
226 b. Use pprof_proto_generator.py to generate pprof proto file. `pprof_proto_generator.py`.
227 c. Use pprof to report a function with annotated source code, as described [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/scripts_reference.md#pprof_proto_generator_py).
228
229## Bugs and contribution
230
231Bugs and feature requests can be submitted at http://github.com/android-ndk/ndk/issues.
232Patches can be uploaded to android-review.googlesource.com as [here](https://source.android.com/setup/contribute/),
233or sent to email addresses listed [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/OWNERS).
234
235If you want to compile simpleperf C++ source code, follow below steps:
2361. Download AOSP master branch as [here](https://source.android.com/setup/build/requirements).
2372. Build simpleperf.
238```sh
239$ . build/envsetup.sh
240$ lunch aosp_arm64-userdebug
241$ mmma system/extras/simpleperf -j30
242```
243
244If built successfully, out/target/product/generic_arm64/system/bin/simpleperf is for ARM64, and
245out/target/product/generic_arm64/system/bin/simpleperf32 is for ARM.
246