linux下perf工具初体验

  |   0 评论   |   0 浏览

背景

使用perf工具,来定位指令集的性能问题。

初体验

安装

  • Ubuntu/Debian
sudo apt install linux-tools-$(uname -r) linux-tools-generic
  • RHEL/CentOS
sudo yum install perf

确认安装情况

perf --version

结果

perf --version
perf version 4.18.0-348.7.1.el8_5.x86_64

支持普通用户使用

echo 0 > /proc/sys/kernel/perf_event_paranoid

或者修改文件 /etc/sysctl.conf如下:

kernel.perf_event_paranoid = 0

使用

perf 和 git 的使用类似,里面包含一组子命令,每个子命令有自己的参数。

支持的事件

列出所有事件

perf list

branch-instructions OR branches                    [Hardware event]
  branch-misses                                      [Hardware event]
  bus-cycles                                         [Hardware event]
  cache-misses                                       [Hardware event]
  cache-references                                   [Hardware event]
  cpu-cycles OR cycles                               [Hardware event]
  instructions                                       [Hardware event]
  ref-cycles                                         [Hardware event]
  alignment-faults                                   [Software event]
  bpf-output                                         [Software event]
  context-switches OR cs                             [Software event]
  cpu-clock                                          [Software event]
  cpu-migrations OR migrations                       [Software event]
  dummy                                              [Software event]
  emulation-faults                                   [Software event]
  major-faults                                       [Software event]
  minor-faults                                       [Software event]
  page-faults OR faults                              [Software event]
  task-clock                                         [Software event]
  duration_time                                      [Tool event]
  L1-dcache-load-misses                              [Hardware cache event]
  L1-dcache-loads                                    [Hardware cache event]
  L1-dcache-stores                                   [Hardware cache event]
  L1-icache-load-misses                              [Hardware cache event]
  LLC-load-misses                                    [Hardware cache event]
  LLC-loads                                          [Hardware cache event]
  LLC-store-misses                                   [Hardware cache event]
  LLC-stores                                         [Hardware cache event]
  branch-load-misses                                 [Hardware cache event]
  branch-loads                                       [Hardware cache event]
  dTLB-load-misses                                   [Hardware cache event]
  dTLB-loads                                         [Hardware cache event]
  dTLB-store-misses                                  [Hardware cache event]
  dTLB-stores                                        [Hardware cache event]
  iTLB-load-misses                                   [Hardware cache event]
  iTLB-loads                                         [Hardware cache event]
  node-load-misses                                   [Hardware cache event]
  node-loads                                         [Hardware cache event]
  node-store-misses                                  [Hardware cache event]
  node-stores                                        [Hardware cache event]
  branch-instructions OR cpu/branch-instructions/    [Kernel PMU event]
  branch-misses OR cpu/branch-misses/                [Kernel PMU event]
  bus-cycles OR cpu/bus-cycles/                      [Kernel PMU event]
  cache-misses OR cpu/cache-misses/                  [Kernel PMU event]
  cache-references OR cpu/cache-references/          [Kernel PMU event]
  cpu-cycles OR cpu/cpu-cycles/                      [Kernel PMU event]
  instructions OR cpu/instructions/                  [Kernel PMU event]
  mem-loads OR cpu/mem-loads/                        [Kernel PMU event]
  mem-stores OR cpu/mem-stores/                      [Kernel PMU event]
  ref-cycles OR cpu/ref-cycles/                      [Kernel PMU event]
  topdown-fetch-bubbles OR cpu/topdown-fetch-bubbles/ [Kernel PMU event]
  topdown-recovery-bubbles OR cpu/topdown-recovery-bubbles/ [Kernel PMU event]
  topdown-slots-issued OR cpu/topdown-slots-issued/  [Kernel PMU event]
  topdown-slots-retired OR cpu/topdown-slots-retired/ [Kernel PMU event]
  topdown-total-slots OR cpu/topdown-total-slots/    [Kernel PMU event]
  cstate_core/c3-residency/                          [Kernel PMU event]
  cstate_core/c6-residency/                          [Kernel PMU event]
  cstate_core/c7-residency/                          [Kernel PMU event]
  cstate_pkg/c2-residency/                           [Kernel PMU event]
  cstate_pkg/c3-residency/                           [Kernel PMU event]
  cstate_pkg/c6-residency/                           [Kernel PMU event]
  cstate_pkg/c7-residency/                           [Kernel PMU event]
  intel_bts//                                        [Kernel PMU event]
  intel_pt//                                         [Kernel PMU event]
  msr/aperf/                                         [Kernel PMU event]
  msr/cpu_thermal_margin/                            [Kernel PMU event]
  msr/mperf/                                         [Kernel PMU event]
  msr/pperf/                                         [Kernel PMU event]
  msr/smi/                                           [Kernel PMU event]
  msr/tsc/                                           [Kernel PMU event]
  power/energy-pkg/                                  [Kernel PMU event]
  power/energy-ram/                                  [Kernel PMU event]
  uncore_iio_free_running_0/bw_in_port0/             [Kernel PMU event]
  uncore_iio_free_running_0/bw_in_port1/             [Kernel PMU event]
  uncore_iio_free_running_0/bw_in_port2/             [Kernel PMU event]
  uncore_iio_free_running_0/bw_in_port3/             [Kernel PMU event]
  uncore_iio_free_running_0/bw_out_port0/            [Kernel PMU event]
  uncore_iio_free_running_0/bw_out_port1/            [Kernel PMU event]
  uncore_iio_free_running_0/bw_out_port2/            [Kernel PMU event]
  uncore_iio_free_running_0/bw_out_port3/            [Kernel PMU event]
  uncore_iio_free_running_0/ioclk/                   [Kernel PMU event]
  uncore_iio_free_running_0/util_in_port0/           [Kernel PMU event]
  uncore_iio_free_running_0/util_in_port1/           [Kernel PMU event]
  uncore_iio_free_running_0/util_in_port2/           [Kernel PMU event]
  uncore_iio_free_running_0/util_in_port3/           [Kernel PMU event]
  uncore_iio_free_running_0/util_out_port0/          [Kernel PMU event]
  uncore_iio_free_running_0/util_out_port1/          [Kernel PMU event]
  uncore_iio_free_running_0/util_out_port2/          [Kernel PMU event]
  uncore_iio_free_running_0/util_out_port3/          [Kernel PMU event]
  uncore_iio_free_running_1/bw_in_port0/             [Kernel PMU event]
  uncore_iio_free_running_1/bw_in_port1/             [Kernel PMU event]
  uncore_iio_free_running_1/bw_in_port2/             [Kernel PMU event]
  uncore_iio_free_running_1/bw_in_port3/             [Kernel PMU event]
  uncore_iio_free_running_1/bw_out_port0/            [Kernel PMU event]
  uncore_iio_free_running_1/bw_out_port1/            [Kernel PMU event]
  uncore_iio_free_running_1/bw_out_port2/            [Kernel PMU event]
  uncore_iio_free_running_1/bw_out_port3/            [Kernel PMU event]
  uncore_iio_free_running_1/ioclk/                   [Kernel PMU event]
  uncore_iio_free_running_1/util_in_port0/           [Kernel PMU event]
  uncore_iio_free_running_1/util_in_port1/           [Kernel PMU event]
  uncore_iio_free_running_1/util_in_port2/           [Kernel PMU event]
  uncore_iio_free_running_1/util_in_port3/           [Kernel PMU event]
  uncore_iio_free_running_1/util_out_port0/          [Kernel PMU event]
  uncore_iio_free_running_1/util_out_port1/          [Kernel PMU event]
  uncore_iio_free_running_1/util_out_port2/          [Kernel PMU event]
  uncore_iio_free_running_1/util_out_port3/          [Kernel PMU event]
  uncore_iio_free_running_2/bw_in_port0/             [Kernel PMU event]
  uncore_iio_free_running_2/bw_in_port1/             [Kernel PMU event]
  uncore_iio_free_running_2/bw_in_port2/             [Kernel PMU event]
  uncore_iio_free_running_2/bw_in_port3/             [Kernel PMU event]
  uncore_iio_free_running_2/bw_out_port0/            [Kernel PMU event]
  uncore_iio_free_running_2/bw_out_port1/            [Kernel PMU event]
  uncore_iio_free_running_2/bw_out_port2/            [Kernel PMU event]
  uncore_iio_free_running_2/bw_out_port3/            [Kernel PMU event]
  uncore_iio_free_running_2/ioclk/                   [Kernel PMU event]
  uncore_iio_free_running_2/util_in_port0/           [Kernel PMU event]
  uncore_iio_free_running_2/util_in_port1/           [Kernel PMU event]
  uncore_iio_free_running_2/util_in_port2/           [Kernel PMU event]
  uncore_iio_free_running_2/util_in_port3/           [Kernel PMU event]
  uncore_iio_free_running_2/util_out_port0/          [Kernel PMU event]
  uncore_iio_free_running_2/util_out_port1/          [Kernel PMU event]
  uncore_iio_free_running_2/util_out_port2/          [Kernel PMU event]
  uncore_iio_free_running_2/util_out_port3/          [Kernel PMU event]
  uncore_iio_free_running_3/bw_in_port0/             [Kernel PMU event]
  uncore_iio_free_running_3/bw_in_port1/             [Kernel PMU event]
  uncore_iio_free_running_3/bw_in_port2/             [Kernel PMU event]
  uncore_iio_free_running_3/bw_in_port3/             [Kernel PMU event]
  uncore_iio_free_running_3/bw_out_port0/            [Kernel PMU event]
  uncore_iio_free_running_3/bw_out_port1/            [Kernel PMU event]
  uncore_iio_free_running_3/bw_out_port2/            [Kernel PMU event]
  uncore_iio_free_running_3/bw_out_port3/            [Kernel PMU event]
  uncore_iio_free_running_3/ioclk/                   [Kernel PMU event]
  uncore_iio_free_running_3/util_in_port0/           [Kernel PMU event]
  uncore_iio_free_running_3/util_in_port1/           [Kernel PMU event]
  uncore_iio_free_running_3/util_in_port2/           [Kernel PMU event]
  uncore_iio_free_running_3/util_in_port3/           [Kernel PMU event]
  uncore_iio_free_running_3/util_out_port0/          [Kernel PMU event]
  uncore_iio_free_running_3/util_out_port1/          [Kernel PMU event]
  uncore_iio_free_running_3/util_out_port2/          [Kernel PMU event]
  uncore_iio_free_running_3/util_out_port3/          [Kernel PMU event]
  uncore_iio_free_running_4/bw_in_port0/             [Kernel PMU event]
  uncore_iio_free_running_4/bw_in_port1/             [Kernel PMU event]
  uncore_iio_free_running_4/bw_in_port2/             [Kernel PMU event]
  uncore_iio_free_running_4/bw_in_port3/             [Kernel PMU event]
  uncore_iio_free_running_4/bw_out_port0/            [Kernel PMU event]
  uncore_iio_free_running_4/bw_out_port1/            [Kernel PMU event]
  uncore_iio_free_running_4/bw_out_port2/            [Kernel PMU event]
  uncore_iio_free_running_4/bw_out_port3/            [Kernel PMU event]
  uncore_iio_free_running_4/ioclk/                   [Kernel PMU event]
  uncore_iio_free_running_4/util_in_port0/           [Kernel PMU event]
  uncore_iio_free_running_4/util_in_port1/           [Kernel PMU event]
  uncore_iio_free_running_4/util_in_port2/           [Kernel PMU event]
  uncore_iio_free_running_4/util_in_port3/           [Kernel PMU event]
  uncore_iio_free_running_4/util_out_port0/          [Kernel PMU event]
  uncore_iio_free_running_4/util_out_port1/          [Kernel PMU event]
  uncore_iio_free_running_4/util_out_port2/          [Kernel PMU event]
  uncore_iio_free_running_4/util_out_port3/          [Kernel PMU event]
  uncore_iio_free_running_5/bw_in_port0/             [Kernel PMU event]
  uncore_iio_free_running_5/bw_in_port1/             [Kernel PMU event]
  uncore_iio_free_running_5/bw_in_port2/             [Kernel PMU event]
  uncore_iio_free_running_5/bw_in_port3/             [Kernel PMU event]
  uncore_iio_free_running_5/bw_out_port0/            [Kernel PMU event]
  uncore_iio_free_running_5/bw_out_port1/            [Kernel PMU event]
  uncore_iio_free_running_5/bw_out_port2/            [Kernel PMU event]
  uncore_iio_free_running_5/bw_out_port3/            [Kernel PMU event]
  uncore_iio_free_running_5/ioclk/                   [Kernel PMU event]
  uncore_iio_free_running_5/util_in_port0/           [Kernel PMU event]
  uncore_iio_free_running_5/util_in_port1/           [Kernel PMU event]
  uncore_iio_free_running_5/util_in_port2/           [Kernel PMU event]
  uncore_iio_free_running_5/util_in_port3/           [Kernel PMU event]
  uncore_iio_free_running_5/util_out_port0/          [Kernel PMU event]
  uncore_iio_free_running_5/util_out_port1/          [Kernel PMU event]
  uncore_iio_free_running_5/util_out_port2/          [Kernel PMU event]
  uncore_iio_free_running_5/util_out_port3/          [Kernel PMU event]
  uncore_imc_0/cas_count_read/                       [Kernel PMU event]
  uncore_imc_0/cas_count_write/                      [Kernel PMU event]
  uncore_imc_0/clockticks/                           [Kernel PMU event]
  uncore_imc_1/cas_count_read/                       [Kernel PMU event]
  uncore_imc_1/cas_count_write/                      [Kernel PMU event]
  uncore_imc_1/clockticks/                           [Kernel PMU event]
  uncore_imc_2/cas_count_read/                       [Kernel PMU event]
  uncore_imc_2/cas_count_write/                      [Kernel PMU event]
  uncore_imc_2/clockticks/                           [Kernel PMU event]
  uncore_imc_3/cas_count_read/                       [Kernel PMU event]
  uncore_imc_3/cas_count_write/                      [Kernel PMU event]
  uncore_imc_3/clockticks/                           [Kernel PMU event]
  uncore_imc_4/cas_count_read/                       [Kernel PMU event]
  uncore_imc_4/cas_count_write/                      [Kernel PMU event]
  uncore_imc_4/clockticks/                           [Kernel PMU event]
  uncore_imc_5/cas_count_read/                       [Kernel PMU event]
  uncore_imc_5/cas_count_write/                      [Kernel PMU event]
  uncore_imc_5/clockticks/                           [Kernel PMU event]

列出软件事件

$ perf list software

List of pre-defined events (to be used in -e):

  alignment-faults                                   [Software event]
  bpf-output                                         [Software event]
  context-switches OR cs                             [Software event]
  cpu-clock                                          [Software event]
  cpu-migrations OR migrations                       [Software event]
  dummy                                              [Software event]
  emulation-faults                                   [Software event]
  major-faults                                       [Software event]
  minor-faults                                       [Software event]
  page-faults OR faults                              [Software event]
  task-clock                                         [Software event]

  duration_time                                      [Tool event]

查看实时CPU profile

sudo perf top

参考

  1. Linux perf: How to Use the Command and Profiler
  2. AVX2 brings no performance improvement?
  3. Linux性能分析工具perf详解