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--n-devices) 24 - [Suggestions about recording call graphs](#suggestions-about-recording-call-graphs) 25 - [Why we can't always get complete DWARF-based call graphs](#why-we-cant-always-get-complete-dwarf-based-call-graphs) 26 - [How to solve missing symbols in report?](#how-to-solve-missing-symbols-in-report) 27 - [Fix broken callchain stopped at C functions](#fix-broken-callchain-stopped-at-c-functions) 28 - [Show annotated source code and disassembly](#show-annotated-source-code-and-disassembly) 29 - [Bugs and contribution](#bugs-and-contribution) 30 31 32## Introduction 33 34Simpleperf contains two parts: the simpleperf executable and Python scripts. 35 36The simpleperf executable works similar to linux-tools-perf, but has some specific features for 37the Android profiling environment: 38 391. It collects more info in profiling data. Since the common workflow is "record on the device, and 40 report on the host", simpleperf not only collects samples in profiling data, but also collects 41 needed symbols, device info and recording time. 42 432. It delivers new features for recording. 44 a. When recording dwarf based call graph, simpleperf unwinds the stack before writing a sample 45 to file. This is to save storage space on the device. 46 b. Support tracing both on CPU time and off CPU time with --trace-offcpu option. 47 c. Support recording callgraphs of JITed and interpreted Java code on Android >= P. 48 493. It relates closely to the Android platform. 50 a. Is aware of Android environment, like using system properties to enable profiling, using 51 run-as to profile in application's context. 52 b. Supports reading symbols and debug information from the .gnu_debugdata section, because 53 system libraries are built with .gnu_debugdata section starting from Android O. 54 c. Supports profiling shared libraries embedded in apk files. 55 d. It uses the standard Android stack unwinder, so its results are consistent with all other 56 Android tools. 57 584. It builds executables and shared libraries for different usages. 59 a. Builds static executables on the device. Since static executables don't rely on any library, 60 simpleperf executables can be pushed on any Android device and used to record profiling data. 61 b. Builds executables on different hosts: Linux, Mac and Windows. These executables can be used 62 to report on hosts. 63 c. Builds report shared libraries on different hosts. The report library is used by different 64 Python scripts to parse profiling data. 65 66Detailed documentation for the simpleperf executable is [here](#executable-commands-reference). 67 68Python scripts are split into three parts according to their functions: 69 701. Scripts used for recording, like app_profiler.py, run_simpleperf_without_usb_connection.py. 71 722. Scripts used for reporting, like report.py, report_html.py, inferno. 73 743. Scripts used for parsing profiling data, like simpleperf_report_lib.py. 75 76Detailed documentation for the Python scripts is [here](#scripts-reference). 77 78 79## Tools in simpleperf 80 81The simpleperf executables and Python scripts are located in simpleperf/ in ndk releases, and in 82system/extras/simpleperf/scripts/ in AOSP. Their functions are listed below. 83 84bin/: contains executables and shared libraries. 85 86bin/android/${arch}/simpleperf: static simpleperf executables used on the device. 87 88bin/${host}/${arch}/simpleperf: simpleperf executables used on the host, only supports reporting. 89 90bin/${host}/${arch}/libsimpleperf_report.${so/dylib/dll}: report shared libraries used on the host. 91 92*.py, inferno, purgatorio: Python scripts used for recording and reporting. Details are in [scripts_reference.md](scripts_reference.md). 93 94 95## Android application profiling 96 97See [android_application_profiling.md](./android_application_profiling.md). 98 99 100## Android platform profiling 101 102See [android_platform_profiling.md](./android_platform_profiling.md). 103 104 105## Executable commands reference 106 107See [executable_commands_reference.md](./executable_commands_reference.md). 108 109 110## Scripts reference 111 112See [scripts_reference.md](./scripts_reference.md). 113 114 115## Answers to common issues 116 117### Why we suggest profiling on Android >= N devices? 118``` 1191. Running on a device reflects a real running situation, so we suggest 120profiling on real devices instead of emulators. 1212. To profile Java code, we need ART running in oat mode, which is only 122available >= L for rooted devices, and >= N for non-rooted devices. 1233. Old Android versions are likely to be shipped with old kernels (< 3.18), 124which may not support profiling features like recording dwarf based call graphs. 1254. Old Android versions are likely to be shipped with Arm32 chips. In Arm32 126mode, recording stack frame based call graphs doesn't work well. 127``` 128 129### Suggestions about recording call graphs 130 131Below is our experiences of dwarf based call graphs and stack frame based call graphs. 132 133dwarf based call graphs: 1341. Need support of debug information in binaries. 1352. Behave normally well on both ARM and ARM64, for both fully compiled Java code and C++ code. 1363. Can only unwind 64K stack for each sample. So usually can't show complete flamegraph. But 137 probably is enough for users to identify hot places. 1384. Take more CPU time than stack frame based call graphs. So the sample frequency is suggested 139 to be 1000 Hz. Thus at most 1000 samples per second. 140 141stack frame based call graphs: 1421. Need support of stack frame registers. 1432. Don't work well on ARM. Because ARM is short of registers, and ARM and THUMB code have different 144 stack frame registers. So the kernel can't unwind user stack containing both ARM/THUMB code. 1453. Also don't work well on fully compiled Java code on ARM64. Because the ART compiler doesn't 146 reserve stack frame registers. 1474. Work well when profiling native programs on ARM64. One example is profiling surfacelinger. And 148 usually shows complete flamegraph when it works well. 1495. Take less CPU time than dwarf based call graphs. So the sample frequency can be 4000 Hz or 150 higher. 151 152So if you need to profile code on ARM or profile fully compiled Java code, dwarf based call graphs 153may be better. If you need to profile C++ code on ARM64, stack frame based call graphs may be 154better. After all, you can always try dwarf based call graph first, because it always produces 155reasonable results when given unstripped binaries properly. If it doesn't work well enough, then 156try stack frame based call graphs instead. 157 158Simpleperf may need unstripped native binaries on the device to generate good dwarf based call 159graphs. It can be supported in two ways: 1601. Use unstripped native binaries when building the apk, as [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/demo/SimpleperfExampleWithNative/app/profiling.gradle). 1612. Download unstripped native libraries on device, as [here](#fix-broken-callchain-stopped-at-c-functions). 162 163### Why we can't always get complete DWARF-based call graphs? 164 165DWARF-based call graphs are generated by unwinding thread stacks. When a sample is generated, up to 16664KB stack data is dumped by the kernel. By unwinding the stack based on dwarf information, we get 167a callchain. But the thread stack can be much longer than 64KB. In that case, we can't unwind to 168the thread start point. 169 170To alleviate the problem, simpleperf joins callchains after recording them. If two callchains of 171a thread have an entry containing the same ip and sp address, then simpleperf tries to join them to 172make the callchains longer. In that case, the longer we run, the more samples we get. This makes it 173more likely to get complete callchains, but it's still not guaranteed to get complete call graphs. 174 175### How to solve missing symbols in report? 176 177The simpleperf record command collects symbols on device in perf.data. But if the native libraries 178you use on device are stripped, this will result in a lot of unknown symbols in the report. A 179solution is to build binary_cache on host. 180 181```sh 182# Collect binaries needed by perf.data in binary_cache/. 183$ python binary_cache_builder.py -lib NATIVE_LIB_DIR,... 184``` 185 186The NATIVE_LIB_DIRs passed in -lib option are the directories containing unstripped native 187libraries on host. After running it, the native libraries containing symbol tables are collected 188in binary_cache/ for use when reporting. 189 190```sh 191$ python report.py --symfs binary_cache 192 193# report_html.py searches binary_cache/ automatically, so you don't need to 194# pass it any argument. 195$ python report_html.py 196``` 197 198### Fix broken callchain stopped at C functions 199 200When using dwarf based call graphs, simpleperf generates callchains during recording to save space. 201The debug information needed to unwind C functions is in .debug_frame section, which is usually 202stripped in native libraries in apks. To fix this, we can download unstripped version of native 203libraries on device, and ask simpleperf to use them when recording. 204 205To use simpleperf directly: 206 207```sh 208# create native_libs dir on device, and push unstripped libs in it (nested dirs are not supported). 209$ adb shell mkdir /data/local/tmp/native_libs 210$ adb push <unstripped_dir>/*.so /data/local/tmp/native_libs 211# run simpleperf record with --symfs option. 212$ adb shell simpleperf record xxx --symfs /data/local/tmp/native_libs 213``` 214 215To use app_profiler.py: 216 217```sh 218$ python app_profiler.py -lib <unstripped_dir> 219``` 220 221### Show annotated source code and disassembly 222 223To show hot places at source code and instruction level, we need to show source code and 224disassembly with event count annotation. Simpleperf supports showing annotated source code and 225disassembly for C++ code and fully compiled Java code. Simpleperf supports two ways to do it: 226 2271. Through report_html.py: 228 229 a. Generate perf.data and pull it on host. 230 b. Generate binary_cache, containing elf files with debug information. Use -lib option to add 231 libs with debug info. Do it with 232 `binary_cache_builder.py -i perf.data -lib <dir_of_lib_with_debug_info>`. 233 c. Use report_html.py to generate report.html with annotated source code and disassembly, 234 as described [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/scripts_reference.md#report_html_py). 235 2362. Through pprof. 237 238 a. Generate perf.data and binary_cache as above. 239 b. Use pprof_proto_generator.py to generate pprof proto file. `pprof_proto_generator.py`. 240 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). 241 242## Bugs and contribution 243 244Bugs and feature requests can be submitted at http://github.com/android-ndk/ndk/issues. 245Patches can be uploaded to android-review.googlesource.com as [here](https://source.android.com/setup/contribute/), 246or sent to email addresses listed [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/OWNERS). 247 248If you want to compile simpleperf C++ source code, follow below steps: 2491. Download AOSP main branch as [here](https://source.android.com/setup/build/requirements). 2502. Build simpleperf. 251```sh 252$ . build/envsetup.sh 253$ lunch aosp_arm64-userdebug 254$ mmma system/extras/simpleperf -j30 255``` 256 257If built successfully, out/target/product/generic_arm64/system/bin/simpleperf is for ARM64, and 258out/target/product/generic_arm64/system/bin/simpleperf32 is for ARM. 259