[Tool][opt] perf 安装使用教程

1 ubuntu 安装步骤

  1. 安装linux-tools-common

    1
    sudo apt-get install linux-tools-common
  2. 此时运行perf仍然失败:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ➜  matrixdb git:(main) ✗ perf
    WARNING: perf not found for kernel 5.15.0-69

    You may need to install the following packages for this specific kernel:
    linux-tools-5.15.0-69-generic
    linux-cloud-tools-5.15.0-69-generic

    You may also want to install one of the following packages to keep up to date:
    linux-tools-generic
    linux-cloud-tools-generic
  3. 根据提示先进行对应包的更新:

    1
    sudo apt-get install linux-tools-generic linux-cloud-tools-generic
  4. 下载对应包:

    1
    sudo apt-get install linux-tools-5.15.0-69-generic linux-cloud-tools-5.15.0-69-generic
  5. 进行验证,下载成功:

    1
    2
    ➜  matrixdb git:(main) ✗ perf -v
    perf version 5.15.87

2 卸载

1
sudo apt-get remove linux-tools-common linux-tools-$(uname -r) linux-cloud-tools-$(uname -r)

3 报错

3.1 报错1:The cpu_core/cycles/ event is not supported.

1
2
3
➜  build git:(p1) ✗ sudo perf record ./test/buffer_pool_manager_instance_test
Error:
The cpu_core/cycles/ event is not supported.

在公司 macbook 上没有出现报错,在实验室 win11 上出现上述问题。

参考 虚拟机Linux使用perf stat提示cycles not supported,解决方法如下:

  1. 关闭VMware虚拟机电源,找到硬件配置选项中CPU
  2. 勾选☑️虚拟化CPU性能计数器
  3. 重启问题解决(但是我的机器勾选后无法启动虚拟机了🥦

4 使用

4.1 抓取性能信息

1
2
3
4
5
# 进程
sudo perf record --call-graph=dwarf -p 479061

# 可执行文件
sudo perf record --call-graph=dwarf ./exec

4.2 结果展示

1
sudo perf report -i perf.data --no-inline -g

4.3 快捷键

  1. 展开:shift + +

X 参考