Linux/Android perf

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

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Linux系统下进行交叉编译perf工具,需要以下几个步骤: 1. 首先需要获取合适的交叉编译工具链。根据目标硬件平台的架构选择对应的交叉编译工具链。可以从官方网站或第三方资源获取。 2. 下载perf源码。perf通常随着Linux内核一起发布,可以从Linux内核官方网站上下载到相应版本的源码。 3. 解压源码。使用压缩工具将下载得到的源码进行解压缩。 4. 设置环境变量。根据交叉编译工具链的路径设置相应的环境变量,例如PATH,ARCH,CROSS_COMPILE等。 5. 进入perf源码目录,在终端中输入命令"make perf"开始编译。编译过程中,会使用交叉编译工具链进行对应的编译操作。 6. 编译完成后,在源码目录中会生成一个名为"perf"的可执行文件,即为交叉编译后的perf工具。 需要注意的是,交叉编译perf工具的具体步骤和参数可能会因为不同的交叉编译工具链版本和目标平台的不同而有所差异。因此,在具体操作过程中需要根据实际情况进行调整。同时,确保操作系统中已经安装了相应的开发工具和依赖库,以保证正确的编译过程。 ### 回答2: 对于Linux系统上进行交叉编译perf工具,你需要先了解一些基本概念和步骤。 1. 交叉编译:交叉编译是在一种操作系统上生成运行于不同架构或操作系统上的可执行文件。对于在Linux上进行交叉编译,你需要安装一个交叉编译工具链,该工具链能够生成目标平台(如ARM、MIPS等)上的可执行文件。 2. Perf工具:Perf是一个性能分析工具,它可以帮助你监测和分析Linux系统上的各种性能指标,如CPU使用率、内存使用、I/O等。通过perf工具,你可以收集系统的性能数据,用于分析和优化程序的性能。 在进行Linux交叉编译perf的过程中,以下是基本的步骤: 1. 选择目标平台:确定你要在哪个目标平台上运行perf工具,根据该平台的架构和操作系统选择相应的交叉编译工具链。 2. 准备交叉编译工具链:安装并配置交叉编译工具链,确保在你的Linux系统上能够生成目标平台的可执行文件。 3. 获取perf源代码:从Linux内核的源代码中获取perf的源代码,它通常位于`tools/perf`目录下。 4. 配置交叉编译环境:进入perf源代码目录,在终端中设置交叉编译环境变量,包括CC、CROSS_COMPILE等。 5. 执行交叉编译命令:执行交叉编译的命令,如`make`或`make ARCH=arm`,根据你的目标平台进行设置。 6. 构建完成:编译过程完成后,你会在`perf`源代码目录中得到一个可执行文件,它是为目标平台交叉编译生成的perf工具。 请注意,交叉编译涉及到一些细节和特定的环境设置,具体步骤可能根据你的系统和目标平台而有所不同。在实际操作中,你可能需要参考相关的文档或资源,以获得更详细的指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值