当前位置: 首页 > 工具软件 > not-perf > 使用案例 >

Linux/Android perf

乐正意智
2023-12-01

perf介绍

perf是Linux系统中的性能调试工具,它是一个应用层的工具,但是会从kernel的文件节点获取信息,它的源代码存放在kernel中的tools/perf目录,它在编译的时候会使用到内核代码的头文件,所以不同版本的内核一般都要使用对应版本的perf工具。虽然它的代码存在于内核目录中,但是在编译内核时并不会主动编译perf工具,如果要编译需要进入到tools/perf目录中执行make。

perf利用CPU中的一些硬件监控单元(PMU)来统计CPU的事件,它提供CPU周期统计,执行指令统计,缓存命中统计等功能,由于是硬件的,所以它对于系统性能的影响很小,内核将这些硬件计数器封装到perf硬件事件(hardware event)中,除此之外内核还提供了软件事件(software event)和跟踪点事件(tracepoint event)。这些事件经过内核统计后,上层的perf工具可以抓取出数据并进行性能分析。

perf 编译安装

perf具有很好的扩展特性,它在编译时会动态检测系统特性,根据系统情况使能perf支持的功能:

cd tools/perf
make

如下是在我本机上编译时打印的信息:

Auto-detecting system features:
...                         dwarf: [ OFF ]
...            dwarf_getlocations: [ OFF ]
...                         glibc: [ on  ]
...                          gtk2: [ OFF ]
...                      libaudit: [ OFF ]
...                        libbfd: [ OFF ]
...                        libelf: [ OFF ]
...                       libnuma: [ OFF ]
...        numa_num_possible_cpus: [ OFF ]
...                       libperl: [ OFF ]
...                     libpython: [ OFF ]
...                      libslang: [ OFF ]
...                     libcrypto: [ on  ]
...                     libunwind: [ OFF ]
...            libdw-dwarf-unwind: [ OFF ]
...                          zlib: [ on  ]
...                          lzma: [ OFF ]
...                     get_cpuid: [ on  ]
...                           bpf: [ on  ]

Makefile.config:291: No libelf found. Disables 'probe' tool, jvmti and BPF support in 'perf record'. Please install libelf-dev, libelf-devel or elfutils-libelf-devel
Makefile.config:399: No sys/sdt.h found, no SDT events are defined, please install systemtap-sdt-devel or systemtap-sdt-dev
Makefile.config:472: Disabling post unwind, no support found.
Makefile.config:522: No libaudit.h found, disables 'trace' tool, please install audit-libs-devel or libaudit-dev
Makefile.config:548: slang not found, disables TUI support. Please install slang-devel, libslang-dev or libslang2-dev
Makefile.config:562: GTK2 not found, disables GTK2 support. Please install gtk2-devel or libgtk2.0-dev
Makefile.config:588: Missing perl devel files. Disabling perl scripting support, please install perl-ExtUtils-Embed/libperl-dev
Makefile.config:619: No 'python-config' tool was found: disables Python support - please install python-devel/python-dev
Makefile.config:712: No liblzma found, disables xz kernel module decompression, please install xz-devel/liblzma-dev
Makefile.config:725: No numa.h found, disables 'perf bench numa mem' benchmark, please install numactl-devel/libnuma-devel/libnuma-dev

可以看到它会自动检测系统中所安装的库,根据这些库来使能perf所支持的功能。虽然我的系统中有很多库不存在,但是并不会影响perf的编译,只是会导致perf特定功能不可用。

perf 直接安装

除了上面通过编译的形式生成perf工具,对于ubuntu系统还可以直接利用软件源下载安装:

sudo apt-get install linux-tools-common
sudo apt-get install linux-tools-`uname -r`

我们需要安装这两个包,其中第二个就是对应内核版本的工具包,前面有提到过perf需要安装对应内核的版本,否则可能会引起一些功能不可用。

simpleperf

上述介绍都是基于Linux平台上perf工具的编译和安装,那么对于android平台要如何进行perf调试呢?Android旧的版本自带有perf工具: external/linux-tools-perf 。由于某些原因,google在新版的android中已经使用simpleperf代替了原有的linux-tools-perf。新版本的android simpleperf代码目录:android/system/extras/simpleperf

simpleperf的命令行选项和linux-tools-perf基本上是一样的,并且simpleperf针对android平台做了特定的增强。需要注意的是simpleperf只支持剖析elf二进制格式的指令,我们可以编译simpleperf后直接放到Android设备上运行和分析,和perf工具一样。除此外,Android NDK中也提供了simpleperf性能分析工具,NDK中提供的是一个套件,包含了device端和host端,一个负责收集数据,一个负责解析数据和分析数据。host端还包括了python脚本便于一键式收集和分析,跟systrace一样。

simpleperf命令简介

simpleperf是Linux perf的简化版本,比如它不包含 perf top 命令。perf工具是一个基于进程的性能分析工具,它的参数必须要传入相关的进程信息,或者-a指定所有进程。

  • help
$ simpleperf --help
Usage: simpleperf [common options] subcommand [args_for_subcommand]
common options:
    -h/--help     Print this help information.
    --log <severity> Set the minimum severity of logging. Possible severities
                     include verbose, debug, warning, info, error, fatal.
                     Default is info.
    --version     Print version of simpleperf.
subcommands:
    debug-unwind        Debug/test offline unwinding.
    dump                dump perf record file
    help                print help information for simpleperf
    kmem                collect kernel memory allocation information
    list                list available event types
    record              record sampling info in perf.data
    report              report sampling information in perf.data
    report-sample       report raw sample information in perf.data
    stat                gather performance counter information

help命令会列出simpleperf支持的选项,如果想要查看各个选项进一步的帮助信息,可以通过 simpleperf help xxx 来查看。

  • list
$ simpleperf help list

Usage: simpleperf list [options] [hw|sw|cache|raw|tracepoint]
       List all available event types.
       Filters can be used to show only event types belong to selected types:
         hw          hardware events
         sw          software events
         cache       hardware cache events
         raw         raw pmu events
         tracepoint  tracepoint events

此命令可以详细查看simpleperf list命令帮助文档,如上所述,list可以支持hw、sw、cache、raw、tracepoint事件参数,比如想要列出simpleperf支持的hw事件:

$ simpleperf list hw             
List of hardware events:
  cpu-cycles
  instructions
  cache-references
  cache-misses
  branch-misses
  bus-cycles
  stalled-cycles-frontend
  stalled-cycles-backend

可以看到支持的hw events包括上述几个。

  • record
simpleperf record -a --duration 30    //使用simpleperf抓取系统级别的perf数据,持续30s
simpleperf record --app org.codeaurora.snapcam -g //抓取一个app的启动到结束过程,可以先于app启动命令
simpleperf record -p 1 --duration 30  //使用simpleperf抓取进程为1的init进程的perf数据,持续30s
simpleperf record -g -p 1 --duration 30 //-g选项增加了函数调用关系数据
simpleperf record -e 'cpu-cycles' -p 811 --duration 30  //统计进程811的cpu周期这个硬件事件信息,-e指定相关的perf event

simpleperf统计CPU利用率是利用CPU定期中断,然后检测此时运行的函数来统计次数的,默认是不包含函数内部成员执行时间的,只有在report中增加–children,函数内部调用才会被记录到该函数命中次数统计中。

  • report
simpleperf report //输出数据分析report

simpleperf report -g //输出函数调用report
simpleperf report --comms sudogame //输出线程名称为sudogame的perf报告
simpleperf report --tids tid1,tid2    //只打印tid1、tid2线程的信息
simpleperf report --sort pid,tid,comm,dso //以pid、tid、comm、dso的格式输出report统计信息
simpleperf report --sort symbol           //以函数格式输出report统计信息,每个函数独立的时间,不包含子函数
simpleperf report --children --sort symbol  //以函数格式输出report统计信息,并且把子函数的时间计算到父函数的统计中
simpleperf report --dso mylib.so  --sort symbol   //输出对应动态库最耗时的函数
simpleperf report --dso mylib.so  --symfs . --sort symbol   //输出对应动态库最耗时的函数(symfs指定寻找so文件的相对路劲)
simpleperf report --tids threadID --sort dso //查找线程中最耗时的共享库

simpleperf record在当前目录产生一个perf.data,simpleperf report会解析当前目录下的perf.data,如果不存在会报错。

  • report-sample
simpleperf report-sample -i perf.data -o out.perf

输出原始采样信息(raw sample information)。

simpleperf NDK脚本简介

除了上述在device端可以直接使用的simpleperf命令外,NDK提供了host端使用的脚本,甚至可以生成火焰图:

$ git clone https://github.com/brendangregg/FlameGraph.git
$ python report_sample.py --symfs binary_cache >out.perf   //converting profiling data to the format used by  FlameGraph
$ FlameGraph/stackcollapse-perf.pl out.perf >out.folded
$ FlameGraph/flamegraph.pl out.folded >a.svg

 类似资料: