当前位置: 首页 > 工具软件 > Perf-Tools > 使用案例 >

linux安装perf工具

公西财
2023-12-01

centos系统yum安装:

sudo yum install perf

安装完成,键入perf查看可用选项。
但一般情况下,这样的安装完成后,普通用户下perf stat|top|record……并不能正常工作,提示

$ perf stat 
Error:
You may not have permission to collect system-wide stats.

Consider tweaking /proc/sys/kernel/perf_event_paranoid,
which controls use of the performance events system by
unprivileged users (without CAP_SYS_ADMIN).

The current value is 2:

  -1: Allow use of (almost) all events by all users
      Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
>= 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN
      Disallow raw tracepoint access by users without CAP_SYS_ADMIN
>= 1: Disallow CPU event access by users without CAP_SYS_ADMIN
>= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN

To make this setting permanent, edit /etc/sysctl.conf too, e.g.:

	kernel.perf_event_paranoid = -1

两种解决方法:
1)临时使用,加sudo权限,这要求当前用户在sudoer list内
2)修改kenel.perf_event_paranoid参数

a) 临时修改(本地是centos系统,如下方法为ubuntu,未测试),重启后失效

如果是ubuntu16.04系统:

echo 0 > /proc/sys/kernel/kptr_restrict
echo -1 > /proc/sys/kernel/perf_event_paranoid

如果是ubuntu18.04系统(未实测):

echo -1 > /proc/sys/kernel/perf_event_paranoid

b) 永久修改,重启后仍有效
编辑/etc/sysctl.conf,在文件末尾加上:

kernel.kptr_restrict=0  (如果是ubuntu16.04则加入该配置)
kernel.perf_event_paranoid= -1

最后,使用sysctl -p /etc/sysctl.conf命令reload配置文件

相关资料

Installing and Using Perf in Ubuntu and CentOS
perf 安装及使用
linux perf tool probe&trace.pdf
perf

 类似资料: