Linux 下 perf 的使用


一、概述

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 recordperf report 命令来收集和分析探针数据。

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

[projectsauron]:~/$ perf -h ## 或 perf

 usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

 The most commonly used perf commands are:
   annotate        Read perf.data (created by perf record) and display annotated code
   archive         Create archive with object files with build-ids found in perf.data file
   bench           General framework for benchmark suites
   buildid-cache   Manage build-id cache.
   buildid-list    List the buildids in a perf.data file
   c2c             Shared Data C2C/HITM Analyzer.
   config          Get and set variables in a configuration file.
   daemon          Run record sessions on background
   data            Data file related processing
   diff            Read perf.data files and display the differential profile
   evlist          List the event names in a perf.data file
   ftrace          simple wrapper for kernel's ftrace functionality
   inject          Filter to augment the events stream with additional information
   iostat          Show I/O performance metrics
   kallsyms        Searches running kernel for symbols
   kmem            Tool to trace/measure kernel memory properties
   kvm             Tool to trace/measure kvm guest os
   list            List all symbolic event types
   lock            Analyze lock events
   mem             Profile memory accesses
   record          Run a command and record its profile into perf.data
   report          Read perf.data (created by perf record) and display the profile
   sched           Tool to trace/measure scheduler properties (latencies)
   script          Read perf.data (created by perf record) and display trace output
   stat            Run a command and gather performance counter statistics
   test            Runs sanity tests.
   timechart       Tool to visualize total system behavior during a workload
   top             System profiling
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值