1*288bf522SAndroid Build Coastguard Worker# Examples of using simpleperf to profile Android applications 2*288bf522SAndroid Build Coastguard Worker 3*288bf522SAndroid Build Coastguard Worker## Table of Contents 4*288bf522SAndroid Build Coastguard Worker 5*288bf522SAndroid Build Coastguard Worker- [Examples of using simpleperf to profile Android applications](#examples-of-using-simpleperf-to-profile-android-applications) 6*288bf522SAndroid Build Coastguard Worker - [Table of Contents](#table-of-contents) 7*288bf522SAndroid Build Coastguard Worker - [Introduction](#introduction) 8*288bf522SAndroid Build Coastguard Worker - [Profile a Java application](#profile-a-java-application) 9*288bf522SAndroid Build Coastguard Worker - [Profile a Java/C++ application](#profile-a-javac-application) 10*288bf522SAndroid Build Coastguard Worker - [Profile a Kotlin application](#profile-a-kotlin-application) 11*288bf522SAndroid Build Coastguard Worker- [Profile via app_api](#profile-via-app_api) 12*288bf522SAndroid Build Coastguard Worker 13*288bf522SAndroid Build Coastguard Worker## Introduction 14*288bf522SAndroid Build Coastguard Worker 15*288bf522SAndroid Build Coastguard WorkerSimpleperf is a native profiler used on Android platform. It can be used to profile Android 16*288bf522SAndroid Build Coastguard Workerapplications. Its documentation is [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/README.md). 17*288bf522SAndroid Build Coastguard WorkerInstructions of preparing your Android application for profiling are [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/README.md#Android-application-profiling). 18*288bf522SAndroid Build Coastguard WorkerThis directory is to show examples of using simpleperf to profile Android applications. The 19*288bf522SAndroid Build Coastguard Workermeaning of each directory is as below: 20*288bf522SAndroid Build Coastguard Worker 21*288bf522SAndroid Build Coastguard Worker ../scripts/ -- contain simpleperf binaries and scripts. 22*288bf522SAndroid Build Coastguard Worker SimpleperfExampleJava/ -- contains an Android Studio project using only Java code. 23*288bf522SAndroid Build Coastguard Worker SimpleperfExampleCpp/ -- contains an Android Studio project using both Java and C++ code. 24*288bf522SAndroid Build Coastguard Worker SimpleperfExampleKotlin/ -- contains an Android Studio project using Kotlin code. 25*288bf522SAndroid Build Coastguard Worker CppApi/ -- contains an Android Studio project using c++ app_api to record. 26*288bf522SAndroid Build Coastguard Worker JavaApi/ -- contains an Android Studio project using Java app_api to record. 27*288bf522SAndroid Build Coastguard Worker 28*288bf522SAndroid Build Coastguard WorkerIt can be downloaded as below: 29*288bf522SAndroid Build Coastguard Worker 30*288bf522SAndroid Build Coastguard Worker```sh 31*288bf522SAndroid Build Coastguard Worker$ git clone https://android.googlesource.com/platform/system/extras 32*288bf522SAndroid Build Coastguard Worker$ cd extras/simpleperf/demo 33*288bf522SAndroid Build Coastguard Worker``` 34*288bf522SAndroid Build Coastguard Worker 35*288bf522SAndroid Build Coastguard WorkerThe testing environment: 36*288bf522SAndroid Build Coastguard Worker 37*288bf522SAndroid Build Coastguard Worker``` 38*288bf522SAndroid Build Coastguard WorkerAndroid Studio 3.2 39*288bf522SAndroid Build Coastguard Workertest device: Android O (Google Pixel 2) 40*288bf522SAndroid Build Coastguard Workertest device: Android N (Google Nexus 6P) 41*288bf522SAndroid Build Coastguard WorkerPlease make sure your device having Android version >= N. 42*288bf522SAndroid Build Coastguard Worker``` 43*288bf522SAndroid Build Coastguard Worker 44*288bf522SAndroid Build Coastguard Worker## Profile a Java application 45*288bf522SAndroid Build Coastguard Worker 46*288bf522SAndroid Build Coastguard WorkerAndroid Studio project: SimpleExampleJava 47*288bf522SAndroid Build Coastguard Worker 48*288bf522SAndroid Build Coastguard Workersteps: 49*288bf522SAndroid Build Coastguard Worker1. Build and install the application: 50*288bf522SAndroid Build Coastguard Worker 51*288bf522SAndroid Build Coastguard Worker```sh 52*288bf522SAndroid Build Coastguard Worker# Open SimpleperfExampleJava project with Android Studio, 53*288bf522SAndroid Build Coastguard Worker# and build this project successfully, otherwise the `./gradlew` command below will fail. 54*288bf522SAndroid Build Coastguard Worker$ cd SimpleperfExampleJava 55*288bf522SAndroid Build Coastguard Worker 56*288bf522SAndroid Build Coastguard Worker# Build and install a debuggable app. We can also build and install a released app on Android >= Q. 57*288bf522SAndroid Build Coastguard Worker# On windows, use "gradlew" instead. 58*288bf522SAndroid Build Coastguard Worker$ ./gradlew clean assemble 59*288bf522SAndroid Build Coastguard Worker$ adb install -r app/build/outputs/apk/debug/app-debug.apk 60*288bf522SAndroid Build Coastguard Worker``` 61*288bf522SAndroid Build Coastguard Worker 62*288bf522SAndroid Build Coastguard Worker2. Record profiling data: 63*288bf522SAndroid Build Coastguard Worker 64*288bf522SAndroid Build Coastguard Worker```sh 65*288bf522SAndroid Build Coastguard Worker$ cd ../../scripts/ 66*288bf522SAndroid Build Coastguard Worker# app_profiler.py collects profiling data in perf.data, and binaries on device in binary_cache/. 67*288bf522SAndroid Build Coastguard Worker$ python app_profiler.py -p simpleperf.example.java 68*288bf522SAndroid Build Coastguard Worker``` 69*288bf522SAndroid Build Coastguard Worker 70*288bf522SAndroid Build Coastguard Worker3. Show profiling data: 71*288bf522SAndroid Build Coastguard Worker 72*288bf522SAndroid Build Coastguard Worker```sh 73*288bf522SAndroid Build Coastguard Worker# report_html.py generates profiling result in report.html. 74*288bf522SAndroid Build Coastguard Worker$ python report_html.py --add_source_code --source_dirs ../demo --add_disassembly 75*288bf522SAndroid Build Coastguard Worker``` 76*288bf522SAndroid Build Coastguard Worker 77*288bf522SAndroid Build Coastguard Worker## Profile a Java/C++ application 78*288bf522SAndroid Build Coastguard Worker 79*288bf522SAndroid Build Coastguard WorkerAndroid Studio project: SimpleExampleCpp 80*288bf522SAndroid Build Coastguard Worker 81*288bf522SAndroid Build Coastguard Workersteps: 82*288bf522SAndroid Build Coastguard Worker1. Build and install the application: 83*288bf522SAndroid Build Coastguard Worker 84*288bf522SAndroid Build Coastguard Worker```sh 85*288bf522SAndroid Build Coastguard Worker# Open SimpleperfExampleCpp project with Android Studio, 86*288bf522SAndroid Build Coastguard Worker# and build this project sucessfully, otherwise the `./gradlew` command below will fail. 87*288bf522SAndroid Build Coastguard Worker$ cd SimpleperfExampleCpp 88*288bf522SAndroid Build Coastguard Worker 89*288bf522SAndroid Build Coastguard Worker# On windows, use "gradlew" instead. 90*288bf522SAndroid Build Coastguard Worker$ ./gradlew clean assemble 91*288bf522SAndroid Build Coastguard Worker$ adb install -r app/build/outputs/apk/debug/app-debug.apk 92*288bf522SAndroid Build Coastguard Worker``` 93*288bf522SAndroid Build Coastguard Worker 94*288bf522SAndroid Build Coastguard Worker2. Record profiling data: 95*288bf522SAndroid Build Coastguard Worker 96*288bf522SAndroid Build Coastguard Worker```sh 97*288bf522SAndroid Build Coastguard Worker$ cd ../../scripts/ 98*288bf522SAndroid Build Coastguard Worker# app_profiler.py collects profiling data in perf.data, and binaries on device in binary_cache/. 99*288bf522SAndroid Build Coastguard Worker$ python app_profiler.py -p simpleperf.example.cpp -lib app/build 100*288bf522SAndroid Build Coastguard Worker``` 101*288bf522SAndroid Build Coastguard Worker 102*288bf522SAndroid Build Coastguard Worker3. Show profiling data: 103*288bf522SAndroid Build Coastguard Worker 104*288bf522SAndroid Build Coastguard Worker```sh 105*288bf522SAndroid Build Coastguard Worker# report_html.py generates profiling result in report.html. 106*288bf522SAndroid Build Coastguard Worker$ python report_html.py --add_source_code --source_dirs ../demo --add_disassembly 107*288bf522SAndroid Build Coastguard Worker``` 108*288bf522SAndroid Build Coastguard Worker 109*288bf522SAndroid Build Coastguard Worker## Profile a Kotlin application 110*288bf522SAndroid Build Coastguard Worker 111*288bf522SAndroid Build Coastguard WorkerAndroid Studio project: SimpleExampleKotlin 112*288bf522SAndroid Build Coastguard Worker 113*288bf522SAndroid Build Coastguard Workersteps: 114*288bf522SAndroid Build Coastguard Worker1. Build and install the application: 115*288bf522SAndroid Build Coastguard Worker 116*288bf522SAndroid Build Coastguard Worker```sh 117*288bf522SAndroid Build Coastguard Worker# Open SimpleperfExampleKotlin project with Android Studio, 118*288bf522SAndroid Build Coastguard Worker# and build this project sucessfully, otherwise the `./gradlew` command below will fail. 119*288bf522SAndroid Build Coastguard Worker$ cd SimpleperfExampleKotlin 120*288bf522SAndroid Build Coastguard Worker 121*288bf522SAndroid Build Coastguard Worker# Build and install a debuggable app. We can also build and install a released app on Android >= Q. 122*288bf522SAndroid Build Coastguard Worker# On windows, use "gradlew" instead. 123*288bf522SAndroid Build Coastguard Worker$ ./gradlew clean assemble 124*288bf522SAndroid Build Coastguard Worker$ adb install -r app/build/outputs/apk/debug/app-debug.apk 125*288bf522SAndroid Build Coastguard Worker``` 126*288bf522SAndroid Build Coastguard Worker 127*288bf522SAndroid Build Coastguard Worker2. Record profiling data: 128*288bf522SAndroid Build Coastguard Worker 129*288bf522SAndroid Build Coastguard Worker```sh 130*288bf522SAndroid Build Coastguard Worker$ cd ../../scripts/ 131*288bf522SAndroid Build Coastguard Worker# app_profiler.py collects profiling data in perf.data, and binaries on device in binary_cache/. 132*288bf522SAndroid Build Coastguard Worker$ python app_profiler.py -p simpleperf.example.kotlin 133*288bf522SAndroid Build Coastguard Worker``` 134*288bf522SAndroid Build Coastguard Worker 135*288bf522SAndroid Build Coastguard Worker3. Show profiling data: 136*288bf522SAndroid Build Coastguard Worker 137*288bf522SAndroid Build Coastguard Worker```sh 138*288bf522SAndroid Build Coastguard Worker# report_html.py generates profiling result in report.html. 139*288bf522SAndroid Build Coastguard Worker$ python report_html.py --add_source_code --source_dirs ../demo --add_disassembly 140*288bf522SAndroid Build Coastguard Worker``` 141*288bf522SAndroid Build Coastguard Worker 142*288bf522SAndroid Build Coastguard Worker# Profile via app_api 143*288bf522SAndroid Build Coastguard Worker 144*288bf522SAndroid Build Coastguard WorkerAndroid Studio project: CppApi and JavaApi 145*288bf522SAndroid Build Coastguard Worker 146*288bf522SAndroid Build Coastguard Workersteps: 147*288bf522SAndroid Build Coastguard Worker1. Build and install the application: 148*288bf522SAndroid Build Coastguard Worker 149*288bf522SAndroid Build Coastguard Worker```sh 150*288bf522SAndroid Build Coastguard Worker# Open CppApi project with Android Studio, 151*288bf522SAndroid Build Coastguard Worker# and build this project sucessfully, otherwise the `./gradlew` command below will fail. 152*288bf522SAndroid Build Coastguard Worker$ cd CppApi 153*288bf522SAndroid Build Coastguard Worker 154*288bf522SAndroid Build Coastguard Worker# On windows, use "gradlew" instead. 155*288bf522SAndroid Build Coastguard Worker$ ./gradlew clean assemble 156*288bf522SAndroid Build Coastguard Worker$ adb install -r app/build/outputs/apk/debug/app-debug.apk 157*288bf522SAndroid Build Coastguard Worker``` 158*288bf522SAndroid Build Coastguard Worker 159*288bf522SAndroid Build Coastguard Worker2. Prepare recording environment. 160*288bf522SAndroid Build Coastguard Worker 161*288bf522SAndroid Build Coastguard Worker```sh 162*288bf522SAndroid Build Coastguard Worker$ cd ../../scripts/ 163*288bf522SAndroid Build Coastguard Worker$ python api_profiler.py prepare 164*288bf522SAndroid Build Coastguard Worker``` 165*288bf522SAndroid Build Coastguard Worker 166*288bf522SAndroid Build Coastguard Worker3. Run the CppApi app. 167*288bf522SAndroid Build Coastguard Worker 168*288bf522SAndroid Build Coastguard Worker```sh 169*288bf522SAndroid Build Coastguard Worker# launch the app via cmdline, can also launch it on device. 170*288bf522SAndroid Build Coastguard Worker# A profiling file is generated each time running the app. 171*288bf522SAndroid Build Coastguard Worker$ adb shell am start simpleperf.demo.cpp_api/.MainActivity 172*288bf522SAndroid Build Coastguard Worker``` 173*288bf522SAndroid Build Coastguard Worker 174*288bf522SAndroid Build Coastguard Worker4. Collect profiling data. 175*288bf522SAndroid Build Coastguard Worker 176*288bf522SAndroid Build Coastguard Worker```sh 177*288bf522SAndroid Build Coastguard Worker$ python api_profiler.py collect -p simpleperf.demo.cpp_api 178*288bf522SAndroid Build Coastguard Worker``` 179*288bf522SAndroid Build Coastguard Worker 180*288bf522SAndroid Build Coastguard Worker5. Report profiling data. 181*288bf522SAndroid Build Coastguard Worker 182*288bf522SAndroid Build Coastguard Worker```sh 183*288bf522SAndroid Build Coastguard Worker$ python report_html.py -i simpleperf_data/* --aggregate-by-thread-name 184*288bf522SAndroid Build Coastguard Worker``` 185