• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..18-Oct-2019-

README.mdH A D18-Oct-20199.1 KiB203144

android_application_profiling.mdH A D18-Oct-201910.1 KiB273206

android_platform_profiling.mdH A D18-Oct-20191.7 KiB3729

executable_commands_reference.mdH A D18-Oct-201922.2 KiB584442

inferno.mdH A D18-Oct-20193.9 KiB10475

scripts_reference.mdH A D18-Oct-20199.2 KiB234171

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- [Introduction](#introduction)
15- [Tools in simpleperf](#tools-in-simpleperf)
16- [Android application profiling](#android-application-profiling)
17- [Android platform profiling](#android-platform-profiling)
18- [Executable commands reference](#executable-commands-reference)
19- [Scripts reference](#scripts-reference)
20- [Answers to common issues](#answers-to-common-issues)
21    - [Why we suggest profiling on android >= N devices](#why-we-suggest-profiling-on-android-n-devices)
22    - [Suggestions about recording call graphs](#suggestions-about-recording-call-graphs)
23    - [How to solve missing symbols in report](#how-to-solve-missing-symbols-in-report)
24- [Bugs and contribution](#bugs-and-contribution)
25
26
27## Introduction
28
29Simpleperf contains two parts: the simpleperf executable and Python scripts.
30
31The simpleperf executable works similar to linux-tools-perf, but has some specific features for
32the Android profiling environment:
33
341. It collects more info in profiling data. Since the common workflow is "record on the device, and
35   report on the host", simpleperf not only collects samples in profiling data, but also collects
36   needed symbols, device info and recording time.
37
382. It delivers new features for recording.
39   a. When recording dwarf based call graph, simpleperf unwinds the stack before writing a sample
40      to file. This is to save storage space on the device.
41   b. Support tracing both on CPU time and off CPU time with --trace-offcpu option.
42   c. Support recording callgraphs of JITed and interpreted Java code on Android >= P.
43
443. It relates closely to the Android platform.
45   a. Is aware of Android environment, like using system properties to enable profiling, using
46      run-as to profile in application's context.
47   b. Supports reading symbols and debug information from the .gnu_debugdata section, because
48      system libraries are built with .gnu_debugdata section starting from Android O.
49   c. Supports profiling shared libraries embedded in apk files.
50   d. It uses the standard Android stack unwinder, so its results are consistent with all other
51      Android tools.
52
534. It builds executables and shared libraries for different usages.
54   a. Builds static executables on the device. Since static executables don't rely on any library,
55      simpleperf executables can be pushed on any Android device and used to record profiling data.
56   b. Builds executables on different hosts: Linux, Mac and Windows. These executables can be used
57      to report on hosts.
58   c. Builds report shared libraries on different hosts. The report library is used by different
59      Python scripts to parse profiling data.
60
61Detailed documentation for the simpleperf executable is [here](#executable-commands-reference).
62
63Python scripts are split into three parts according to their functions:
64
651. Scripts used for recording, like app_profiler.py, run_simpleperf_without_usb_connection.py.
66
672. Scripts used for reporting, like report.py, report_html.py, inferno.
68
693. Scripts used for parsing profiling data, like simpleperf_report_lib.py.
70
71Detailed documentation for the Python scripts is [here](#scripts-reference).
72
73
74## Tools in simpleperf
75
76The simpleperf executables and Python scripts are located in simpleperf/ in ndk releases, and in
77system/extras/simpleperf/scripts/ in AOSP. Their functions are listed below.
78
79bin/: contains executables and shared libraries.
80
81bin/android/${arch}/simpleperf: static simpleperf executables used on the device.
82
83bin/${host}/${arch}/simpleperf: simpleperf executables used on the host, only supports reporting.
84
85bin/${host}/${arch}/libsimpleperf_report.${so/dylib/dll}: report shared libraries used on the host.
86
87*.py, inferno: Python scripts used for recording and reporting.
88
89
90## Android application profiling
91
92See [android_application_profiling.md](./android_application_profiling.md).
93
94
95## Android platform profiling
96
97See [android_platform_profiling.md](./android_platform_profiling.md).
98
99
100## Executable commands reference
101
102See [executable_commands_reference.md](./executable_commands_reference.md).
103
104
105## Scripts reference
106
107See [scripts_reference.md](./scripts_reference.md).
108
109
110## Answers to common issues
111
112### Why we suggest profiling on Android >= N devices?
113```
1141. Running on a device reflects a real running situation, so we suggest
115profiling on real devices instead of emulators.
1162. To profile Java code, we need ART running in oat mode, which is only
117available >= L for rooted devices, and >= N for non-rooted devices.
1183. Old Android versions are likely to be shipped with old kernels (< 3.18),
119which may not support profiling features like recording dwarf based call graphs.
1204. Old Android versions are likely to be shipped with Arm32 chips. In Arm32
121mode, recording stack frame based call graphs doesn't work well.
122```
123
124### Suggestions about recording call graphs
125
126Below is our experiences of dwarf based call graphs and stack frame based call graphs.
127
128dwarf based call graphs:
1291. Need support of debug information in binaries.
1302. Behave normally well on both ARM and ARM64, for both fully compiled Java code and C++ code.
1313. Can only unwind 64K stack for each sample. So usually can't show complete flamegraph. But
132   probably is enough for users to identify hot places.
1334. Take more CPU time than stack frame based call graphs. So the sample frequency is suggested
134   to be 1000 Hz. Thus at most 1000 samples per second.
135
136stack frame based call graphs:
1371. Need support of stack frame registers.
1382. Don't work well on ARM. Because ARM is short of registers, and ARM and THUMB code have different
139   stack frame registers. So the kernel can't unwind user stack containing both ARM/THUMB code.
1403. Also don't work well on fully compiled Java code on ARM64. Because the ART compiler doesn't
141   reserve stack frame registers.
1424. Work well when profiling native programs on ARM64. One example is profiling surfacelinger. And
143   usually shows complete flamegraph when it works well.
1445. Take less CPU time than dwarf based call graphs. So the sample frequency can be 4000 Hz or
145   higher.
146
147So if you need to profile code on ARM or profile fully compiled Java code, dwarf based call graphs
148may be better. If you need to profile C++ code on ARM64, stack frame based call graphs may be
149better. After all, you can always try dwarf based call graph first, because it always produces
150reasonable results when given unstripped binaries properly. If it doesn't work well enough, then
151try stack frame based call graphs instead.
152
153Simpleperf needs to have unstripped native binaries on the device to generate good dwarf based call
154graphs. It can be supported in two ways:
1551. Use unstripped native binaries when building the apk, as [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/demo/SimpleperfExampleWithNative/app/profiling.gradle).
1562. Pass directory containing unstripped native libraries to app_profiler.py via -lib. And it will
157   download the unstripped native libraries on the device.
158
159```sh
160$ python app_profiler.py -lib NATIVE_LIB_DIR
161```
162
163### How to solve missing symbols in report?
164
165The simpleperf record command collects symbols on device in perf.data. But if the native libraries
166you use on device are stripped, this will result in a lot of unknown symbols in the report. A
167solution is to build binary_cache on host.
168
169```sh
170# Collect binaries needed by perf.data in binary_cache/.
171$ python binary_cache_builder.py -lib NATIVE_LIB_DIR,...
172```
173
174The NATIVE_LIB_DIRs passed in -lib option are the directories containing unstripped native
175libraries on host. After running it, the native libraries containing symbol tables are collected
176in binary_cache/ for use when reporting.
177
178```sh
179$ python report.py --symfs binary_cache
180
181# report_html.py searches binary_cache/ automatically, so you don't need to
182# pass it any argument.
183$ python report_html.py
184```
185
186## Bugs and contribution
187
188Bugs and feature requests can be submitted at http://github.com/android-ndk/ndk/issues.
189Patches can be uploaded to android-review.googlesource.com as [here](https://source.android.com/setup/contribute/),
190or sent to email addresses listed [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/OWNERS).
191
192If you want to compile simpleperf C++ source code, follow below steps:
1931. Download AOSP master branch as [here](https://source.android.com/setup/build/requirements).
1942. Build simpleperf.
195```sh
196$ . build/envsetup.sh
197$ lunch aosp_arm64-userdebug
198$ mmma system/extras/simpleperf -j30
199```
200
201If built successfully, out/target/product/generic_arm64/system/bin/simpleperf is for ARM64, and
202out/target/product/generic_arm64/system/bin/simpleperf32 is for ARM.
203