系统范围的perf_event_open



使用perf-cli,我们可以测量系统范围的计数器:

$ sudo perf stat -e cpu-cycles
^C
Performance counter stats for 'system wide':
4 247 009 923      cpu-cycles                                                  
2,183469627 seconds time elapsed

在perf_event_open手册中,我认为它相当于监视任何cpu(cpu==-1(上的任何pid(pid=-1(,但这似乎不可能:

Arguments
The pid and cpu arguments allow specifying which process and CPU
to monitor:
pid == 0 and cpu == -1
This measures the calling process/thread on any CPU.
pid == 0 and cpu >= 0
This measures the calling process/thread only when running
on the specified CPU.
pid > 0 and cpu == -1
This measures the specified process/thread on any CPU.
pid > 0 and cpu >= 0
This measures the specified process/thread only when
running on the specified CPU.
pid == -1 and cpu >= 0
This measures all processes/threads on the specified CPU.
This requires CAP_PERFMON (since Linux 5.8) or
CAP_SYS_ADMIN capability or a
/proc/sys/kernel/perf_event_paranoid value of less than 1.
pid == -1 and cpu == -1
This setting is invalid and will return an error.

这里的解决方法是什么?

这些选项似乎都没有为您聚合计数,它们要么在一个核心上计数,要么在上下文开关之间虚拟化计数器。

如果您查看系统范围内的perf stat -a(例如使用strace -f perf stat(的功能,您可以看到它在每个核心的每个事件中调用perf_event_open一次。它必须将一个事件在多个核心之间的计数相加;系统调用API不会帮你这么做。

最新更新