[Linux服务器] Linux 下 perf 的使用

900 0
Honkers 2025-7-20 14:39:58 | 显示全部楼层 |阅读模式


一、概述

1、perf 作用

perf 是一个性能分析工具(基于 Linux 内核提供的性能事件 perf_event 口),用于对 Linux 系统进行性能调优和性能分析。它可以通过收集硬件性能计数器、跟踪系统事件和采样程序调用栈等方式来提供详细的性能统计信息。

perf 依赖事件进行统计,这里的事件是通过采样机制,并不是 clock 级别的统计;根据使用 perf 工具的不同按测量事件的类型进行统计。

2、常用的工具集

除了 perf 命令本身,还有一些常用的工具集可以与 perf 搭配使用来进行更深入的性能分析和调优。以下是一些常用的 perf 工具集:

  1. perf stat:用于收集和显示性能计数器统计信息,可以通过 perf stat 命令来监测进程或命令的整体性能指标,如指令数、缓存命中率、分支预测错误等。
  2. perf record:用于采集程序执行期间的硬件性能计数器数据、事件和调用栈信息,并将其保存到数据文件中。可以使用 perf record 命令来启动采样,并通过 perf report 命令来分析采样数据。
  3. perf report:用于分析通过 perf record 收集的性能采样数据,并生成性能分析报告。可以使用 perf report 命令来查看调用栈信息、函数耗时和性能热点等。
  4. perf top:用于实时监测进程的性能指标,并显示当前的性能热点。可以使用 perf top 命令来查看 CPU 使用情况、函数执行次数和事件计数等。
  5. perf annotate:用于以源代码的方式显示采样数据和调用栈信息,并标注每个源代码行的性能指标。可以使用 perf annotate 命令来查看性能热点和优化建议。
  6. perf diff:用于比较和分析两个不同版本的程序的性能差异。可以使用 perf diff 命令来对比两个 perf 数据文件,并生成性能差异报告。
  7. perf probe:用于动态添加和移除性能探针,以收集特定代码路径的性能数据。可以使用 perf probe 命令来添加探针,并通过 perf record 和 perf report 命令来收集和分析探针数据。

还有一些针对性性能检查工具:如针对锁的 lock;针对调度的 sched;针对 slab 分配器性能 kmem;自定义检查点 probe 等。可以通过命令:perf 或 perf -h 来查看:

  1. [projectsauron]:~/$ perf -h ## 或 perf
  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. daemon Run record sessions on background
  12. data Data file related processing
  13. diff Read perf.data files and display the differential profile
  14. evlist List the event names in a perf.data file
  15. ftrace simple wrapper for kernel's ftrace functionality
  16. inject Filter to augment the events stream with additional information
  17. iostat Show I/O performance metrics
  18. kallsyms Searches running kernel for symbols
  19. kmem Tool to trace/measure kernel memory properties
  20. kvm Tool to trace/measure kvm guest os
  21. list List all symbolic event types
  22. lock Analyze lock events
  23. mem Profile memory accesses
  24. record Run a command and record its profile into perf.data
  25. report Read perf.data (created by perf record) and display the profile
  26. sched Tool to trace/measure scheduler properties (latencies)
  27. script Read perf.data (created by perf record) and display trace output
  28. stat Run a command and gather performance counter statistics
  29. test Runs sanity tests.
  30. timechart Tool to visualize total system behavior during a worklo
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行