如何安装“perf”监控工具?

如何安装perf监控工具?

2你到底想要监控什么? - ddeimeke
9@ddeimeke 这个问题似乎不相关。 - abcd
@ddeimeke 没关系 - ABCD
12个回答

在Ubuntu上,我需要安装三个软件包:
apt-get install linux-tools-common linux-tools-generic linux-tools-`uname -r`

7当我尝试时,只需要使用sudo apt-get install linux-tools-common linux-tools-generic(跳过依赖于内核版本的部分)就足够了。 - Martin R.
7@Martin-R linux-tools-generic 永远指向最新内核版本的工具。当运行旧内核(例如,服务器已经运行了几个月)时,如果您想要 perf 而不必重新启动到较新的内核,您必须显式安装与该内核配对的工具(因此使用 uname -r shell 扩展)。 - notpeter
sudo apt-get update sudo apt-get -y install linux-tools-common linux-tools-generic linux-tools-uname -r - dylanninin
4sudo apt install linux-tools-generic 在 Ubuntu 18.04 上安装 - huangli
1无法找到软件包linux-tools-5.5.* 有什么办法可以在内核5.5上运行perf? - txs

perf实用程序可以在linux-tools软件包中找到。

16您也可能希望安装linux-tools-generic,因为它包含了perf所需的内核模块。 - Allen
1没错,@Allen,我需要的就是linux-tools-generic! - jerome
此外,如果您安装了HWE(像我一样),您需要安装linux-tools-generic-hwe-$version(我当前安装的是linux-tools-generic-hwe-20.04)。 - pizzapants184

确保您安装此工具的内核版本正确。如果您只输入以下命令:
sudo apt-get install linux-tools
它将为您的机器上最新的Linux版本进行安装,如果您正在引导先前的版本(例如出于稳定性原因),则会出错。在我的情况下,它安装了.45而不是.43,我不得不使用以下命令进行安装:
sudo apt-get install linux-tools-3.2.0.43
您可以通过输入以下命令找到您的内核版本: uname -r 为了确保它正常工作,您可以尝试以下操作: perf record /bin/ls

获取最新版本的perf,无论内核版本如何(perf版本与内核版本无关):
git clone --depth 1 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux/tools/perf
make
cp perf /usr/bin

使用 perf --version 命令来检查当前版本。

3为了参考,请安装构建依赖项 apt-get install build-essential git flex bison - Matthias Kuhn
2这是正确的答案,如果您使用较新的内核。所有指向使用dpkg和apt下载的答案都取决于dpkg软件包源是否能够访问基于您的内核版本的perf工具。在运行最新的Ubuntu 21.04镜像,并使用5.11内核时,通过使用dpkg进行下载后,perf工具会被“安装”,但是当尝试运行perf -h时,您将收到“警告:找不到适用于内核5.11.0-41的perf工具”(或者任何其他perf不可用的较新内核)。必须手动从linux-tools编译该工具。 - Dave
谢谢,正确! - Hussain
安装成功了,但似乎安装的是不同版本,它不支持 perf trace - stason
@stason 你期望的版本是什么?它正在安装什么? - bodha
截至目前,正在安装6.3.x版本。看起来v6删除了trace,而在v5中还存在。我非常怀疑v6的perf能否在内核5.x上正常工作。也许可以在回答中建议检查是否使用了与实际内核相同的主要版本? - stason


在14.04版本中,linux-tools软件包不存在无条件的情况下。
$ sudo apt-get install linux-tools
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package linux-tools is a virtual package provided by:
  linux-tools-virtual 3.13.0.36.43
  linux-tools-lowlatency 3.13.0.36.43
  linux-tools-generic-lts-trusty 3.13.0.36.43
  linux-tools-generic-lts-saucy 3.13.0.36.43
  linux-tools-generic 3.13.0.36.43
You should explicitly select one to install.

E: Package 'linux-tools' has no installation candidate

相反,您可以安装列出的软件包之一,具体取决于您已安装的内核。大多数用户将需要安装linux-tools-generic软件包。

对于Ubuntu 22.04来说,这是正确的答案。它会自动引入linux-tools-common和适用于您当前内核版本的特定软件包。 - davidA

在Ubuntu上,"perf"是一个调用特定内核perf二进制文件的shell脚本。
但实际上并不需要这个脚本,perf是兼容的。只需从/usr/bin中移除包装器,并将./lib/linux-tools*中的任何perf二进制文件复制到其中即可。

那个有效 - 谢谢!你可以直接调用 sudo /usr/lib/linux-tools-5.15.0-67/perf trace date - 尝试使用 locate /lib/linux-tools | grep perf 查找你可能拥有的版本。 - stason

我在我的电脑上安装了Linux 3.17.2并尝试使用

sudo apt-get install linux-tools-3.17.2

显示linux-tools不存在。 在这种情况下, 下载所需的Linux内核版本, 进入linux->tools->perf:
cd linux/tools/perf

然后运行make命令:

make

然后运行make install命令。
make install

然后重新启动。 您刚刚手动安装了perf。

不需要重新启动。而且,如果你要从源代码编译,linux-tools-3.17.2应该是不必要的。 - Chang Hyun Park

运行以下两个命令就可以实现!
sudo apt-get install linux-tools-common 
sudo apt-get install linux-tools

不起作用。我正在使用12.04版本。对于内核3.2.0-60,我收到了perf未找到的错误。您可能需要安装linux-tools-3.2.0-60-generic。 - Rishi Dua
这似乎不起作用。 - ABCD

这是在Ubuntu 18.04上对我有效的操作方法:
在收到“找不到命令'perf'”的提示后,建议使用“sudo apt install linux-tools-common”进行安装。然而,尽管如此,在之后尝试运行“perf”时仍然出现相同的结果。
WARNING: perf not found for kernel 4.15.0-39

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

  You may also want to install one of the following packages to keep up to date:
    linux-tools-generic
    linux-cloud-tools-generic

我决定使用sudo apt-get install linux-tools-generic,这就足够了。
如果你在尝试以非root用户身份使用perf时遇到权限错误,请参考这里: 以非root权限运行perf

我收到了相同的警告。但是我运行了你建议的命令,仍然收到了相同的警告。 - Caleb Stanford
linux-tools-oem 似乎已经足够了。 - Caleb Stanford
1@6005 感谢您的反馈,虽然我不知道为什么您会遇到不同的行为。 - Darkonaut
由于您从dpkg仓库下载的软件包从未为您的较新内核版本编译过,所以会发生这种情况。因此,逻辑构建了一个只运行此输出的二进制文件。您必须按照@bodha的建议进行操作。即使在编译之后,您还需要根据您的路径删除或移动这些二进制文件,并用您自己编译的二进制文件替换。您需要执行很多which perf命令来查找它们的位置。 - Dave