• perf top:查看当前全部进程及其性能消耗
    • sudo perf record:收集性能数据记录到文件,默认为perf.data,也会保留上一次的记录文件perf.data.old
    • sudo perf report:对记录文件进行分析,选中指定函数enter可看到调用栈,到最低层调用栈再enter可进入机器码查看对应行数的性能消耗,都采用-g则可找到耗时的对应行。
      • -i 指定分析的.data文件
    • sudo perf script:保存裸trace数据,直接观察采样点的数据分析
    • sudo perf annotate:在gcc编译时加入-g,在record后进行annotate,则可看到对应代码行和相关机器码,以及他们的性能消耗

    • -p 指明检测进程的pid,不指明则检测整个系统

    • -t 指明线程pid
    • -F 设置采样频率,默认频率为1000。但一般推荐设置为-F 99或-F 999,因为要避免引起lockstep采样:
      • 锁步采样是指分析样本以与应用程序中的循环相同的频率出现。 这样做的结果是样本经常出现在循环中的同一位置,因此它会认为操作是最常见的操作,并且可能是瓶颈。
    • -a 显示在所有CPU上的性能统计信息
    • -c 显示在指定CPU上的性能统计信息
    • -g 开启调用栈分析,记录函数间的调用关系
    • sleep 等待指定时间后停止,常用于record
    • -e 对指定的性能事件event进行分析,事件表见下,默认事件为cpu-cycles。

    如:分析pid为123456的cpu-clock情况,延时5秒,并查看函数调用
    sudo perf record -p 123456 -g -e cpu-clock sleep 5 (生成perf.data)
    sudo perf report -g (进入分析界面)
    如:分析可执行文件test
    sudo perf record ./test
    sudo perf report
    image.png全部参数:

    1. root@ubuntu:~# perf -h
    2. usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]
    3. The most commonly used perf commands are:
    4. annotate Read perf.data (created by perf record) and display annotated code
    5. archive Create archive with object files with build-ids found in perf.data file
    6. bench General framework for benchmark suites
    7. buildid-cache Manage build-id cache.
    8. buildid-list List the buildids in a perf.data file
    9. c2c Shared Data C2C/HITM Analyzer.
    10. config Get and set variables in a configuration file.
    11. data Data file related processing
    12. diff Read perf.data files and display the differential profile
    13. evlist List the event names in a perf.data file
    14. ftrace simple wrapper for kernel's ftrace functionality
    15. inject Filter to augment the events stream with additional information
    16. kallsyms Searches running kernel for symbols
    17. kmem Tool to trace/measure kernel memory properties
    18. kvm Tool to trace/measure kvm guest os
    19. list List all symbolic event types
    20. lock Analyze lock events
    21. mem Profile memory accesses
    22. record Run a command and record its profile into perf.data
    23. report Read perf.data (created by perf record) and display the profile
    24. sched Tool to trace/measure scheduler properties (latencies)
    25. script Read perf.data (created by perf record) and display trace output
    26. stat Run a command and gather performance counter statistics
    27. test Runs sanity tests.
    28. timechart Tool to visualize total system behavior during a workload
    29. top System profiling tool.
    30. version display the version of perf binary
    31. probe Define new dynamic tracepoints
    32. trace strace inspired tool
    33. See 'perf help COMMAND' for more information on a specific command.


    annotate perf annotate用于解析由perf record记录的数据文件perf.data并将代码注解显示。如果源代码开启了debug符号,则源码和汇编一起解析。如果源码未开启debug,则解析汇编代码
    archive 根据数据文件记录的build-id,将所有被采样到的elf文件打包。利用此压缩包,可以再任何机器上分析数据文件中记录的采样数据。
    bench perf中内置的benchmark。子系统:调度器和IPC机制、内存管理、NUMA调度、futex压力基准、epoll压力基准等
    buildid-cache 管理perf的buildid缓存,每个elf文件都有一个独一无二的buildid。buildid被perf用来关联性能数据与elf文件。
    buildid-list 列出perf.data文件中的buildid
    c2c 用于调试cache to cache的false sharing问题,用于Shared Data C2C/HITM分析,可以追踪cacheline竞争问题
    config perf config用于读取和配置 .perfconfig配置文件
    diff 对比两个数据文件的差异。能够给出每个符号(函数)在热点分析上的具体差异。
    evlist 列出数据文件perf.data中所有性能事件
    ftrace 是内核ftrace功能的简化封装,可以跟踪指定进程的内核函数调用栈
    inject 该工具读取perf record工具记录的事件流,并将其定向到标准输出
    kallsyms 查找运行中的内核符号
    kmem 针对内核内存(slab)子系统进行追踪测量的工具
    kvm 用于测试kvm客户机的性能参数
    list 列出event事件
    lock 分析内核锁统计信息
    mem 测试内存存取性能数据
    record 运行一个命令,并将其数据保存到perf.data中。随后,可以使用perf report进行分析
    report 显示perf数据
    sched 分析调度器性能
    script 执行测试脚本
    stat perf stat能完整统计应用整个生命周期的信息
    test 用于sanity test
    timechart 生成图标
    top 类似linux的top命令,查看整体性能
    version 查看版本信息
    probe 动态监测点
    trace 跟踪系统调用

    更详细的参考资料:
    原文链接:https://blog.csdn.net/qq_23662505/article/details/114669615