随笔 - 3  文章 - 0  评论 - 0  阅读 - 26796

perf 安装及使用

环境:ubuntu16.04

1.在terminal输入如下命令,安装与当前内核版本一致的工具

sudo apt-get install linux-tools-common
sudo apt-get install linux-tools-"$(uname -r)"
sudo apt-get install linux-cloud-tools-"$(uname -r)"
sudo apt-get install linux-tools-generic
sudo apt-get install linux-cloud-tools-generic

安装完成后,terminal输入perf,可以显示perf的用法介绍。

 

2.一般环境中,安装完成后,使用perf top/perf record是不能正常工作的:

WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,
check /proc/sys/kernel/kptr_restrict.

Samples in kernel functions may not be resolved if a suitable vmlinux
file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved
even with a suitable vmlinux or kallsyms file.

perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error 13 (Permission denied)
perf_event_open(..., 0) failed unexpectedly with error 13 (Permission denied)
Error:
You may not have permission to collect 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 3:

-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权限,如sudo perf top。

(2)设置kenel.perf_event_paranoid:

       (2.1)临时设置,需要sudo su切换到root用户后再进行如下操作,操作完成后返回原用户,此时输入perf top可以正常工作。该方法系统重启后失效。

    如果是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

  (2.2)修改配置文件,重启后仍有效。

    编辑/etc/sysctl.conf,在文件末尾加上:

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

      kernel.perf_event_paranoid= -1

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

 

参考:

https://www.cnblogs.com/arnoldlu/p/6241297.html

posted on   难得明白  阅读(17772)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示