无法将性能事件添加到具有现有领导者的组中

发布于 2025-01-16 02:19:15 字数 1805 浏览 2 评论 0原文

我正在尝试使用以下代码片段(例如)将 perf 事件添加到具有现有领导者的事件组中:

struct perf_event_attr perf_event;
int cpu_fd = -1;
int process_fd = -1;

memset(&perf_event, 0, sizeof(perf_event));
perf_event.type = PERF_TYPE_HARDWARE;
perf_event.size = sizeof(perf_event);
perf_event.config = config; // config (PERF_COUNT_HW_CPU_CYCLES) is passed as a function's argument
perf_event.use_clockid = 1;
perf_event.clockid = CLOCK_MONOTONIC;
perf_event.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_GROUP;

// First step:
// Create leader of perf events group
// cpu_idx is passed as an function's argument
cpu_fd = perf_event_open(&perf_event, -1, cpu_idx, -1,
    PERF_FLAG_FD_CLOEXEC);

if (cpu_fd < 0) {
    printf("Unable to open new CPU PMU event.\n");
    return STATUS_NOK;
}

memset(&perf_event, 0, sizeof(perf_event));
perf_event.type = PERF_TYPE_HARDWARE;
perf_event.size = sizeof(perf_event);
perf_event.config = config; // same as above (PERF_COUNT_HW_CPU_CYCLES)
perf_event.use_clockid = 1;
perf_event.clockid = CLOCK_MONOTONIC;
perf_event.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED;

// Second step:
// Attempting to add new perf event for account current process CPU cycles
// in existing group
// cpu_idx is passed as an function's argument
process_fd = perf_event_open(&perf_event, getpid(), cpu_idx, cpu_fd,
    PERF_FLAG_FD_CLOEXEC);

if (process_fd < 0) {
    printf("Unable to open new process PMU event.\n");
    return STATUS_NOK;
}

在第一步中,创建领导者 perf 事件,用于使用 cpu_idx 计算所需 CPU 上的 CPU 周期总数。 第二步,尝试向现有领导者添加新的性能事件,以便使用 cpu_idx 计算所需 CPU 上的当前进程 CPU 周期。

但由于未知原因,创建新的 process_fd 性能描述符总是失败,并出现 errno EINVAL(无效参数)。我尝试通过内核中的系统调用跟踪来调查根本原因,但似乎我完全陷入困境。

您能帮忙找出此类问题的根本原因吗? 从 perf_event_open 系统调用返回 EINVAL 情况的可能原因有很多,我很欣赏任何调试建议或想法。

非常感谢您的帮助!

I'm trying to add perf event into group of events with an existing leader using the following code snippet (as example):

struct perf_event_attr perf_event;
int cpu_fd = -1;
int process_fd = -1;

memset(&perf_event, 0, sizeof(perf_event));
perf_event.type = PERF_TYPE_HARDWARE;
perf_event.size = sizeof(perf_event);
perf_event.config = config; // config (PERF_COUNT_HW_CPU_CYCLES) is passed as a function's argument
perf_event.use_clockid = 1;
perf_event.clockid = CLOCK_MONOTONIC;
perf_event.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_GROUP;

// First step:
// Create leader of perf events group
// cpu_idx is passed as an function's argument
cpu_fd = perf_event_open(&perf_event, -1, cpu_idx, -1,
    PERF_FLAG_FD_CLOEXEC);

if (cpu_fd < 0) {
    printf("Unable to open new CPU PMU event.\n");
    return STATUS_NOK;
}

memset(&perf_event, 0, sizeof(perf_event));
perf_event.type = PERF_TYPE_HARDWARE;
perf_event.size = sizeof(perf_event);
perf_event.config = config; // same as above (PERF_COUNT_HW_CPU_CYCLES)
perf_event.use_clockid = 1;
perf_event.clockid = CLOCK_MONOTONIC;
perf_event.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED;

// Second step:
// Attempting to add new perf event for account current process CPU cycles
// in existing group
// cpu_idx is passed as an function's argument
process_fd = perf_event_open(&perf_event, getpid(), cpu_idx, cpu_fd,
    PERF_FLAG_FD_CLOEXEC);

if (process_fd < 0) {
    printf("Unable to open new process PMU event.\n");
    return STATUS_NOK;
}

On first step leader perf event is created for accounting the total number of CPU cycles on required CPU with cpu_idx.
On the second step there is an attempt to add new perf event to existing leader in order to account current process CPU cycles on required CPU with cpu_idx.

But for unknown reason creation of new process_fd perf descriptor always fails with errno EINVAL (Invalid argument). I tried to investigate root cause with syscall tracing in the kernel but it seems I completely stuck.

Could you please help to find out root cause of such issue?
These are many possible reasons for EINVAL case returned from perf_event_open syscall, I appreciate any suggestions or ideas for debugging.

Thanks a lot for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文