定义

Simpleperf 是Google随NDK一起发布的一款profile工具(注:从NDK r13开始),它是针对Android平台的一个 native 层性能分析工具。

使用步骤

本篇是分析运行在android设备下的程序

1. 将NDK中Simpleperf工具的可执行程序 push 到手机上

cd <NDK>/simpleperf/bin/android/<对应的版本,根据被测程序和CPU,来选择对应的版本>

例如 :我的路径是 /home/hqb/Android/Sdk/ndk/21.3.6528147/simpleperf/bin/android/arm64

cd /home/hqb/Android/Sdk/ndk/21.3.6528147/simpleperf/bin/android/arm64
adb push simpleperf /data/local/tmp
adb shell chmod 777 /data/local/tmp/simpleperf

2. 启动手机上的被测程序,ps 出该程序的进程ID

adb shell ps -ef | grep "需要分析的应用程序包名"

3. 使用Simpleperf工具进行分析

(1)record-记录运行结果数据:

adb shell /data/local/tmp/simpleperf record -p <进程号> --duration <持续的时间(秒为单位)> -o <输出文件名称> --call-graph fp

--call-graph dwarf 用在32位系统中,64位则采用--call-graph fp

例子:adb shell /data/local/tmp/simpleperf record -p 4844 --duration 10 -o /data/local/tmp/perf.data --call-graph fp

输出:simpleperf I cmd_record.cpp:658] Samples recorded: 12013. Samples lost: 0.

(2)报告结果数据

adb shell /data/local/tmp/simpleperf report -i /data/local/tmp/perf.data -o /data/local/tmp/perf_report.txt

4. 解析火焰图

(1)将data文件和txt文件,从手机pull到电脑
电脑上新建simpleperf_test目录,例子:/home/hqb/simpleperf_test

mkdir simpleperf_test
adb pull /data/local/tmp/perf.data /home/hqb/simpleperf_test
adb pull /data/local/tmp/perf_report.txt /home/hqb/simpleperf_test

经常使用simpleperf就用alias设置别名

vim ~/.bashrc
alias pf='python /home/hqb/Android/Sdk/ndk/21.3.6528147/simpleperf/report_html.py'
source ~/.bashrc

(2)data格式转化成html格式

cd /home/hqb/simpleperf_test

//设置别名
pf -i ./perf.data -o ./perf.html

//如果不设置别名
python /home/hqb/Android/Sdk/ndk/21.3.6528147/simpleperf/report_html.py -i ./perf.data -o ./perf.html

(3)下载FlameGraph到simpleperf_test目录下,将simpleperf复制到simpleperf_test目录下

git clone <https://github.com/brendangregg/FlameGraph.git>
chmod 777 FlameGraph/flamegraph.pl
chmod 777 FlameGraph/stackcollapse-perf.pl

cp -r /home/hqb/Android/Sdk/ndk/21.3.6528147/simpleperf /home/hqb/simpleperf_test

(4)生成火焰图

cd /home/hqb/simpleperf_test
python ./simpleperf/report_sample.py > out.perf
./FlameGraph/stackcollapse-perf.pl out.perf > out.folded
./FlameGraph/flamegraph.pl out.folded > out.svg

out.svg就是最后得到的火焰图,用浏览器打开就可以看

文章目录