perf_event_paranoid == 1实际上对x86性能有什么限制?

Bee*_*ope 12 x86 profiling linux-kernel perf intel-pmu

较新的Linux内核具有sysfs可调参数/proc/sys/kernel/perf_event_paranoid,允许用户调整perf_events非root用户的可用功能,更高的数字更安全(提供相应更少的功能):

内核文档中,我们对各种值有以下行为:

perf_event_paranoid:

控制非特权用户对性能事件系统的使用(无CAP_SYS_ADMIN).默认值为2.

-1:允许所有用户使用(几乎)所有事件在没有CAP_IPC_LOCK的perf_event_mlock_kb之后忽略mlock限制

> = 0:没有CAP_SYS_ADMIN的用户不允许使用ftrace函数跟踪点不允许没有CAP_SYS_ADMIN的用户访问原始跟踪点

> = 1:禁止没有CAP_SYS_ADMIN的用户访问CPU事件

> = 2:禁止没有CAP_SYS_ADMIN的用户进行内核分析

1在我的perf_event_paranoid文件中应该"禁止CPU事件访问" - 但这究竟是什么意思?

普通读数意味着无法访问CPU性能计数器事件(例如Intel PMU事件),但似乎我可以访问那些就好了.例如:

$ perf stat sleep 1

 Performance counter stats for 'sleep 1':

          0.408734      task-clock (msec)         #    0.000 CPUs utilized          
                 1      context-switches          #    0.002 M/sec                  
                 0      cpu-migrations            #    0.000 K/sec                  
                57      page-faults               #    0.139 M/sec                  
         1,050,362      cycles                    #    2.570 GHz                    
           769,135      instructions              #    0.73  insn per cycle         
           152,661      branches                  #  373.497 M/sec                  
             6,942      branch-misses             #    4.55% of all branches        

       1.000830821 seconds time elapsed
Run Code Online (Sandbox Code Playgroud)

说到这里,很多事件都是CPU PMU事件(cycles,instructions,branches,branch-misses,cache-misses).

如果这些不是所引用的CPU事件,它们是什么?

Zul*_*lan 8

在这种情况下,CPU事件是指监视每个CPU而不是每个任务的事件.对于perf工具,这限制了使用

-C, --cpu=
    Count only on the list of CPUs provided. Multiple CPUs can be provided as a comma-separated list with no space: 0,1.
    Ranges of CPUs are specified with -: 0-2. In per-thread mode, this option is ignored. The -a option is still necessary
    to activate system-wide monitoring. Default is to count on all CPUs.

-a, --all-cpus
    system-wide collection from all CPUs (default if no target is specified)
Run Code Online (Sandbox Code Playgroud)

为此perf_event_open考虑以下情况:

pid == -1 and cpu >= 0
       This measures all processes/threads on the specified CPU.  This requires CAP_SYS_ADMIN capability or a /proc/sys/ker?
       nel/perf_event_paranoid value of less than 1.
Run Code Online (Sandbox Code Playgroud)

这可能是特定于版本的,引用的文档来自4.17.这是另一个相关的问题.

  • 对于它的价值,那个[接受的答案](https://unix.stackexchange.com/a/14256/87246)关于另一个问题似乎是错误的`paranoid == 2`.使用`paranoid == 2`我绝对可以使用`perf stat`来获取事件,但我看不到内核PMU计数(仅限用户). (3认同)
  • 通过查看代码,我可以确认这个答案是正确的.此外,执行检查`sysctl_perf_event_paranoid> 0`以确定是否允许在P4处理器上使用线程共享(在逻辑处理器之间共享)事件(正式称为TI事件). (2认同)