c - perf_event_open 权限被拒绝,除了使用 sudo 或更改 perf_event_paranoid 文件之外,还有其他方法吗?
问题描述
我能找到的关于该主题的唯一信息是这个链接:perf_event_open 总是返回 -1,根据我的理解建议使用 CONFIG_HW_PERF_EVENTS 进行配置,但我仍然遇到同样的问题。
我正在实施一个受以下手册页启发的程序perf_event_open
:
static long
perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags)
{
int ret;
ret= syscall(__NR_perf_event_open, hw_event, pid, cpu,
group_fd, flags);
return ret;
}
struct perf_event_attr pe;
int pid = fork();
if (pid > 0 ) {
memset(&pe, 0, sizeof(pe));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(pe);
pe.config = PERF_COUNT_HW_CPU_CYCLES;
pe.disabled = 0;
pe.exclude_kernel = 0;
pe.exclude_hv = 0;
fd = perf_event_open(&pe, pid, -1, -1, 0);
if (fd == -1) {
perror(0);
exit(EXIT_FAILURE);
}
}
我总是得到 fd 的 -1 返回,并且 perror 表示权限被拒绝。
当然我可以使用 sudo 来解决这个问题,但是还有其他方法可以允许执行 perf_event_open 的权限吗?
PS:我不想更改 perf_event_paranoid 文件,这会使程序在设置为 -1 时工作;我想它会在2点。
解决方案
Linux系统调用的返回值perf_event_open()
部分部分说明:
... EACCES Returned when the requested event requires CAP_PERFMON (since Linux 5.8) or CAP_SYS_ADMIN permissions (or a more permissive perf_event paranoid setting). Some common cases where an unprivileged process may encounter this error: attaching to a process owned by a different user; monitoring all processes on a given CPU (i.e., specifying the pid argument as -1); and not setting exclude_kernel when the paranoid setting requires it. ... EPERM Returned on many (but not all) architectures when an unsupported exclude_hv, exclude_idle, exclude_user, or exclude_kernel setting is specified. It can also happen, as with EACCES, when the requested event requires CAP_PERFMON (since Linux 5.8) or CAP_SYS_ADMIN permissions (or a more permissive perf_event paranoid setting). This includes setting a breakpoint on a kernel address, and (since Linux 3.13) setting a kernel function-trace tracepoint.
从发布的示例代码中,考虑到您对偏执设置的陈述,这些值pe.exclude_kernel = 0;
或可能导致权限问题。pe.exclude_hv = 0;
推荐阅读
- c# - 如何使用 ReactiveCommand?
- python - 将组分配给值
- vba - 在 VBA 中编写公式会导致编译错误 - 双引号
- python - 计算python列表中的字符串匹配百分比
- android - inputType =“数字|文本”不起作用 - Android
- javascript - Bootstrap maxlength 用 div 计数
- javascript - 创建一个复制数组元素的按钮
- ubuntu - 无法远程访问 Kibana
- mysql - MYSQL WORKBENCH 5.7,从查询缓慢创建表
- ios12 - 我们可以在 IntentViewController 中处理 tableview didSelect 委托或 UIButton Action 吗?