在Docker内启用perf

今天在学习 极客时间的系列课程-《 Linux性能优化实战》时,通过Docker运行了一个容器,需要借助perf来分析性能瓶颈,正常启动后,在容器内通过apt-get install -y linux-tools安装好了perf工具,但是启动时,却提示以下信息:

┌─Error:───────────────────────────────────────────────────────────┐
│No permission to enable cycles event.                             │
│                                                                  │
│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 3:                                           │
│                                                                  │
│  -1: Allow use of (almost) all events by all users               │
│>= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK│
│>= 1: Disallow CPU event access by users without CAP_SYS_ADMIN    │
│>= 2: Disallow kernel profiling by users without C                │
│                                                                  │
│                                                                  │
│Press any key...                                                  │
└──────────────────────────────────────────────────────────────────┘

提示没有权限,需要更改 /proc/sys/kernel/perf_event_paranoid 文件,然后尝试执行:

$ echo 0 > /proc/sys/kernel/perf_event_paranoid
bash: /proc/sys/kernel/perf_event_paranoid: Read-only file system

解决办法:
需要使用 –privileged 参数,所以必须要重新启动容器,重新安装相关package.
启动容器时:docker run --privileged xxx 即可. 再次执行perf top -g -p 1385
查看分析效果
阅读全文

作者的图片

DigDeeply

Technology Stack: PHP/Openresty/GoLang, and so on…

Web Develop Eneigneer

Beijing China