1# Examples of using simpleperf to profile Android applications 2 3## Table of Contents 4 5- [Introduction](#introduction) 6- [Profiling Java application](#profiling-java-application) 7- [Profiling Java/C++ application](#profiling-javac-application) 8- [Profiling Kotlin application](#profiling-kotlin-application) 9 10## Introduction 11 12Simpleperf is a native profiler used on Android platform. It can be used to profile Android 13applications. It's document is at [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/README.md). 14Instructions of preparing your Android application for profiling are [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/README.md#Android-application-profiling). 15This directory is to show examples of using simpleperf to profile Android applications. The 16meaning of each directory is as below: 17 18 ../scripts/ -- contain simpleperf binaries and scripts. 19 SimpleperfExamplePureJava/ -- contains an Android Studio project using only Java code. 20 SimpleperfExampleWithNative/ -- contains an Android Studio project using both Java and C++ code. 21 SimpleperfExampleOfKotlin/ -- contains an Android Studio project using Kotlin code. 22 23It can be downloaded as below: 24 25 $ git clone https://android.googlesource.com/platform/system/extras 26 $ cd extras/simpleperf/demo 27 28## Profiling Java application 29 30 Android Studio project: SimpleExamplePureJava 31 test device: Android O (Google Pixel XL) 32 test device: Android N (Google Nexus 5X) 33 34steps: 351. Build and install app: 36``` 37# Open SimpleperfExamplesPureJava project with Android Studio, 38# and build this project sucessfully, otherwise the `./gradlew` command below will fail. 39$ cd SimpleperfExamplePureJava 40 41# On windows, use "gradlew" instead. 42$ ./gradlew clean assemble 43$ adb install -r app/build/outputs/apk/app-profiling.apk 44``` 45 462. Record profiling data: 47``` 48$ cd ../../scripts/ 49$ python app_profiler.py -p com.example.simpleperf.simpleperfexamplepurejava 50``` 51 523. Show profiling data: 53``` 54a. show call graph in txt mode 55 # On windows, use "bin\windows\x86\simpleperf" instead. 56 $ bin/linux/x86_64/simpleperf report -g | more 57 If on other hosts, use corresponding simpleperf binary. 58b. show call graph in gui mode 59 $ python report.py -g 60c. show samples in source code 61 $ python annotate.py -s ../demo/SimpleperfExamplePureJava 62 $ gvim annotated_files/java/com/example/simpleperf/simpleperfexamplepurejava/MainActivity.java 63 check the annoated source file MainActivity.java. 64``` 65 66## Profiling Java/C++ application 67 68 Android Studio project: SimpleExampleWithNative 69 test device: Android O (Google Pixel XL) 70 test device: Android N (Google Nexus 5X) 71 72steps: 731. Build and install app: 74``` 75# Open SimpleperfExamplesWithNative project with Android Studio, 76# and build this project sucessfully, otherwise the `./gradlew` command below will fail. 77$ cd SimpleperfExampleWithNative 78 79# On windows, use "gradlew" instead. 80$ ./gradlew clean assemble 81$ adb install -r app/build/outputs/apk/app-profiling.apk 82``` 83 842. Record profiling data: 85``` 86$ cd ../../scripts/ 87$ python app_profiler.py -p com.example.simpleperf.simpleperfexamplewithnative 88 It runs the application and collects profiling data in perf.data, binaries on device in binary_cache/. 89``` 90 913. Show profiling data: 92``` 93a. show call graph in txt mode 94 # On windows, use "bin\windows\x86\simpleperf" instead. 95 $ bin/linux/x86_64/simpleperf report -g | more 96 If on other hosts, use corresponding simpleperf binary. 97b. show call graph in gui mode 98 $ python report.py -g 99c. show samples in source code 100 $ python annotate.py -s ../demo/SimpleperfExampleWithNative 101 $ find . -name "native-lib.cpp" | xargs gvim 102 check the annoated source file native-lib.cpp. 103``` 104 105## Profiling Kotlin application 106 107 Android Studio project: SimpleExampleOfKotlin 108 test device: Android O (Google Pixel XL) 109 test device: Android N (Google Nexus 5X) 110 111steps: 1121. Build and install app: 113``` 114# Open SimpleperfExamplesOfKotlin project with Android Studio, 115# and build this project sucessfully, otherwise the `./gradlew` command below will fail. 116$ cd SimpleperfExampleOfKotlin 117 118# On windows, use "gradlew" instead. 119$ ./gradlew clean assemble 120$ adb install -r app/build/outputs/apk/profiling/app-profiling.apk 121``` 122 1232. Record profiling data: 124``` 125$ cd ../../scripts/ 126$ python app_profiler.py -p com.example.simpleperf.simpleperfexampleofkotlin 127 It runs the application and collects profiling data in perf.data, binaries on device in binary_cache/. 128``` 129 1303. Show profiling data: 131``` 132a. show call graph in txt mode 133 # On windows, use "bin\windows\x86\simpleperf" instead. 134 $ bin/linux/x86_64/simpleperf report -g | more 135 If on other hosts, use corresponding simpleperf binary. 136b. show call graph in gui mode 137 $ python report.py -g 138c. show samples in source code 139 $ python annotate.py -s ../demo/SimpleperfExampleOfKotlin 140 $ find . -name "MainActivity.kt" | xargs gvim 141 check the annoated source file MainActivity.kt. 142``` 143