Simpleperf工具使用介绍

Simpleperf下载

下载Android ndk-bundle ,在simpleperf/bin/android目录下包含有不同体系架构的 Android 上运行的静态二进制文件,在arm目录下打开命令窗口,执行命令:

adb push simpleperf  data/data/
adb shell
cd data/data/
chmod 777 simpleperf
./simpleperf stat -p xxx(pid 或tid) --duration xxx(时间)

Simpleperf的工作原理:

现代CPU具有称为性能监视单元(PMU)的硬件组件。PMU有几个硬件计数器,计算发生了多少个cpu周期,执行了多少指令或发生了多少缓存未命中等事件。
Linux内核将这些硬件计数器包装到硬件性能事件中。此外,Linux内核还提供与硬件无关的软件事件和跟踪点事件。Linux内核通过perfper_event_open系统调用将所有事件公开给用户空间,simpleperf使用该系统调用。
Simpleperf有三个主要命令:stat,record和report。
stat命令总结了一段时间内配置文件进程中发生的事件数。以下是它的工作原理:

  1. 给定用户选项,simpleperf通过对内核进行系统调用来启用分析。
  2. 在配置文件进程运行时,内核启用计数器。
  3. 在分析之后,simpleperf从内核中读取计数器,并报告计数器摘要。
    record命令记录一段时间内的配置文件进程的样本。以下是它的工作原理:
  4. 给定用户选项,simpleperf通过对内核进行系统调用来启用分析。
  5. Simpleperf在simpleperf和内核之间创建映射缓冲区。
  6. 在配置文件进程运行时,内核启用计数器。
  7. 每次发生给定数量的事件时,内核都会将样本转储到映射的缓冲区。
  8. Simpleperf从映射的缓冲区中读取样本,并将分析数据存储在名为perf.data的文件中。
    report命令读取perf.data和配置文件进程使用的任何共享库,并输出一个报告,显示花费的时间。

Simpleperf支持的命令

debug-unwind命令:基于debug / test dwarf的离线展开,用于调试simpleperf。
dump命令:转储perf.data中的内容,用于调试simpleperf。
help命令:打印其他命令的帮助信息。
kmem命令:收集内核内存分配信息(将被Python脚本替换)。
list命令:列出Android设备支持的所有事件类型。
记录命令:配置文件处理并在perf.data中存储分析数据。
report命令:报告perf.data中的分析数据。
report-sample命令:报告perf.data中的每个样本,用于支持集成Android Studio中的simpleperf。
stat命令:profiles处理并打印计数器摘要。
每个命令都支持不同的选项,可以通过帮助消息查看,如下示例:

# List all commands.
$ simpleperf --help
# Print help message for record command.
$ simpleperf record --help

Stat命令

stat命令用于获取已分析进程的事件计数器值。通过传递选项,我们可以选择要使用的事件,要监视的进程/线程,监视的时间和打印间隔。

# Stat using default events (cpu-cycles,instructions,...), and monitor process 7394 for 10 seconds.
$./simpleperf stat -p 7394 --duration 10
Performance counter statistics:

 1,320,496,145  cpu-cycles         # 0.131736 GHz                   (100%)
   510,426,028  instructions   # 2.587047 cycles per instruction(100%)
     4,692,338  branch-misses      # 468.118 K/sec                 (100%)
886.008130(ms) task-clock         # 0.088390 cpus used           (100%)
           753    context-switches   # 75.121 /sec                  (100%)
           870    page-faults        # 86.793 /sec                   (100%)

Total test time: 10.023829 seconds.

选择要统计的事件:

# Stat event cpu-cycles.
$ simpleperf stat -e cpu-cycles -p 11904 --duration 10

# Stat event cache-references and cache-misses.
$ simpleperf stat -e cache-references,cache-misses -p 11904 --duration 10

运行stat命令时,如果硬件事件的数量大于PMU中可用的硬件计数器的数量,则内核在事件之间共享硬件计数器,因此每个事件仅在总时间的一部分中受到监视。在下面的示例中,每行末尾都有一个百分比,显示实际监控每个事件的总时间百分比。

# Stat using event cache-references, cache-references:u,....
$ simpleperf stat -p 7394 -e cache-references,cache-references:u,cache-references:k \
      -e cache-misses,cache-misses:u,cache-misses:k,instructions --duration 1
Performance counter statistics:

4,331,018  cache-references     # 4.861 M/sec    (87%)
3,064,089  cache-references:u   # 3.439 M/sec    (87%)
1,364,959  cache-references:k   # 1.532 M/sec    (87%)
   91,721  cache-misses         # 102.918 K/sec  (87%)
   45,735  cache-misses:u       # 51.327 K/sec   (87%)
   38,447  cache-misses:k       # 43.131 K/sec   (87%)
9,688,515  instructions         # 10.561 M/sec   (89%)

Total test time: 1.026802 seconds.

在上面的示例中,每个事件的监控时间约占总时间的87%。但无法保证始终同时监控任何一对事件。如果我们想要同时监控某些事件,我们可以使用–group。

# Stat using event cache-references, cache-references:u,....
$ simpleperf stat -p 7964 --group cache-references,cache-misses \
      --group cache-references:u,cache-misses:u --group cache-references:k,cache-misses:k \
      -e instructions --duration 1
Performance counter statistics:

3,638,900  cache-references     # 4.786 M/sec          (74%)
   65,171  cache-misses         # 1.790953% miss rate  (74%)
2,390,433  cache-references:u   # 3.153 M/sec          (74%)
   32,280  cache-misses:u       # 1.350383% miss rate  (74%)
  879,035  cache-references:k   # 1.251 M/sec          (68%)
   30,303  cache-misses:k       # 3.447303% miss rate  (68%)
8,921,161  instructions         # 10.070 M/sec         (86%)

Total test time: 1.029843 seconds.

选择目标为stat
我们可以通过-p或-t选择要监视的进程或线程。监视进程与监视进程中的所有线程相同。Simpleperf还可以派生子进程来运行新命令,然后监视子进程

# Stat process 11904 and 11905.
$ simpleperf stat -p 11904,11905 --duration 10

# Stat thread 11904 and 11905.
$ simpleperf stat -t 11904,11905 --duration 10

# Start a child process running `ls`, and stat it.
$ simpleperf stat ls

# Stat the process of an Android application. This only works for debuggable apps on non-rooted
# devices.
$ simpleperf stat --app com.example.simpleperf.simpleperfexamplewithnative

# Stat system wide using -a.
$ simpleperf stat -a --duration 10

决定统计有多长
在监视现有线程时,我们可以使用–duration来决定监视多长时间。监视运行新命令的子进程时,simpleperf监视直到子进程结束。在这种情况下,我们可以使用Ctrl-C随时停止监控。

# Stat process 11904 for 10 seconds.
$ simpleperf stat -p 11904 --duration 10

# Stat until the child process running `ls` finishes.
$ simpleperf stat ls

# Stop monitoring using Ctrl-C.
$ simpleperf stat -p 11904 --duration 10

确定打印间隔
监视perf计数器时,我们也可以使用–interval来决定打印间隔。

# Print stat for process 11904 every 300ms.
$ simpleperf stat -p 11904 --duration 10 --interval 300

# Print system wide stat at interval of 300ms for 10 seconds. Note that system wide profiling needs
# root privilege.
$ su 0 simpleperf stat -a --duration 10 --interval 300

在systrace中显示计数器
Simpleperf还可以使用systrace在收集的跟踪中转储计数器。以下是执行系统范围统计的示例。

# Capture instructions (kernel only) and cache misses with interval of 300 milliseconds for 15
# seconds.
$ su 0 simpleperf stat -e instructions:k,cache-misses -a --interval 300 --duration 15
# On host launch systrace to collect trace for 10 seconds.
(HOST)$ external/chromium-trace/systrace.py --time=10 -o new.html sched gfx view
# Open the collected new.html in browser and perf counters will be shown up.

更多simpleperf的使用命令请参见https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/executable_commands_reference.md

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值