# Simpleperf
Simpleperf is a native profiling tool for Android. It can be used to profile
both Android applications and native processes running on Android. It can
profile both Java and C++ code on Android. It can be used on Android L
and above.
Simpleperf is part of the Android Open Source Project. The source code is [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/).
The latest document is [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/README.md).
## Table of Contents
- [Introduction](#introduction)
- [Tools in simpleperf](#tools-in-simpleperf)
- [Android application profiling](#android-application-profiling)
- [Prepare an Android application](#prepare-an-android-application)
- [Record and report profiling data](#record-and-report-profiling-data)
- [Record and report call graph](#record-and-report-call-graph)
- [Report in html interface](#report-in-html-interface)
- [Show flame graph](#show-flame-graph)
- [Record both on CPU time and off CPU time](#record-both-on-cpu-time-and-off-cpu-time)
- [Profile from launch](#profile-from-launch)
- [Parse profiling data manually](#parse-profiling-data-manually)
- [Android platform profiling](#android-platform-profiling)
- [Executable commands reference](#executable-commands-reference)
- [How simpleperf works](#how-simpleperf-works)
- [Commands](#commands)
- [The list command](#the-list-command)
- [The stat command](#the-stat-command)
- [Select events to stat](#select-events-to-stat)
- [Select target to stat](#select-target-to-stat)
- [Decide how long to stat](#decide-how-long-to-stat)
- [Decide the print interval](#decide-the-print-interval)
- [Display counters in systrace](#display-counters-in-systrace)
- [The record command](#the-record-command)
- [Select events to record](#select-events-to-record)
- [Select target to record](#select-target-to-record)
- [Set the frequency to record](#set-the-frequency-to-record)
- [Decide how long to record](#decide-how-long-to-record)
- [Set the path to store profiling data](#set-the-path-to-store-profiling-data)
- [Record call graphs](#record-call-graphs-in-record-cmd)
- [Record both on CPU time and off CPU time](#record-both-on-cpu-time-and-off-cpu-time-in-record-cmd)
- [The report command](#the-report-command)
- [Set the path to read profiling data](#set-the-path-to-read-profiling-data)
- [Set the path to find binaries](#set-the-path-to-find-binaries)
- [Filter samples](#filter-samples)
- [Group samples into sample entries](#group-samples-into-sample-entries)
- [Report call graphs](#report-call-graphs-in-report-cmd)
- [Scripts reference](#scripts-reference)
- [app_profiler.py](#app_profiler-py)
- [Profile from launch of an application](#profile-from-launch-of-an-application)
- [run_simpleperf_without_usb_connection.py](#run_simpleperf_without_usb_connection-py)
- [binary_cache_builder.py](#binary_cache_builder-py)
- [run_simpleperf_on_device.py](#run_simpleperf_on_device-py)
- [report.py](#report-py)
- [report_html.py](#report_html-py)
- [inferno](#inferno)
- [pprof_proto_generator.py](#pprof_proto_generator-py)
- [report_sample.py](#report_sample-py)
- [simpleperf_report_lib.py](#simpleperf_report_lib-py)
- [Answers to common issues](#answers-to-common-issues)
- [Why we suggest profiling on android >= N devices](#why-we-suggest-profiling-on-android-n-devices)
- [Suggestions about recording call graphs](#suggestions-about-recording-call-graphs)
- [How to solve missing symbols in report](#how-to-solve-missing-symbols-in-report)
- [Bugs and contribution](#bugs-and-contribution)
## Introduction
Simpleperf contains two parts: the simpleperf executable and Python scripts.
The simpleperf executable works similar to linux-tools-perf, but has some specific features for
the Android profiling environment:
1. It collects more info in profiling data. Since the common workflow is "record on the device, and
report on the host", simpleperf not only collects samples in profiling data, but also collects
needed symbols, device info and recording time.
2. It delivers new features for recording.
a. When recording dwarf based call graph, simpleperf unwinds the stack before writing a sample
to file. This is to save storage space on the device.
b. Support tracing both on CPU time and off CPU time with --trace-offcpu option.
c. Support recording callgraphs of JITed and interpreted Java code on Android >= P.
3. It relates closely to the Android platform.
a. Is aware of Android environment, like using system properties to enable profiling, using
run-as to profile in application's context.
b. Supports reading symbols and debug information from the .gnu_debugdata section, because
system libraries are built with .gnu_debugdata section starting from Android O.
c. Supports profiling shared libraries embedded in apk files.
d. It uses the standard Android stack unwinder, so its results are consistent with all other
Android tools.
4. It builds executables and shared libraries for different usages.
a. Builds static executables on the device. Since static executables don't rely on any library,
simpleperf executables can be pushed on any Android device and used to record profiling data.
b. Builds executables on different hosts: Linux, Mac and Windows. These executables can be used
to report on hosts.
c. Builds report shared libraries on different hosts. The report library is used by different
Python scripts to parse profiling data.
Detailed documentation for the simpleperf executable is [here](#executable-commands-reference).
Python scripts are split into three parts according to their functions:
1. Scripts used for recording, like app_profiler.py, run_simpleperf_without_usb_connection.py.
2. Scripts used for reporting, like report.py, report_html.py, inferno.
3. Scripts used for parsing profiling data, like simpleperf_report_lib.py.
Detailed documentation for the Python scripts is [here](#scripts-reference).
## Tools in simpleperf
The simpleperf executables and Python scripts are located in simpleperf/ in ndk releases, and in
system/extras/simpleperf/scripts/ in AOSP. Their functions are listed below.
bin/: contains executables and shared libraries.
bin/android/${arch}/simpleperf: static simpleperf executables used on the device.
bin/${host}/${arch}/simpleperf: simpleperf executables used on the host, only supports reporting.
bin/${host}/${arch}/libsimpleperf_report.${so/dylib/dll}: report shared libraries used on the host.
[app_profiler.py](#app_profiler-py): recording profiling data.
[run_simpleperf_without_usb_connection.py](#run_simpleperf_without_usb_connection-py):
recording profiling data while the USB cable isn't connected.
[binary_cache_builder.py](#binary_cache_builder-py): building binary cache for profiling data.
[report.py](#report-py): reporting in stdio interface.
[report_html.py](#report_html-py): reporting in html interface.
[inferno.sh](#inferno) (or inferno.bat on Windows): generating flamegraph in html interface.
inferno/: implementation of inferno. Used by inferno.sh.
[pprof_proto_generator.py](#pprof_proto_generator-py): converting profiling data to the format
used by [pprof](https://github.com/google/pprof).
[report_sample.py](#report_sample-py): converting profiling data to the format used by [FlameGraph](https://github.com/brendangregg/FlameGraph).
[simpleperf_report_lib.py](#simpleperf_report_lib-py): library for parsing profiling data.
## Android application profiling
This section shows how to profile an Android application.
Some examples are [Here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/demo/README.