如何在 mmap 中使用 PERF_SAMPLE_READ

How to use PERF_SAMPLE_READ with mmap

这个问题与 perf_event_open 系统调用有关,但没有它的标签。

我目前正在寻找使用枚举 perf_event_sample_formatPERF_SAMPLE_READ 成员从内存映射中检索一些数据,但由于未知原因,系统调用本身 return "invalid argument"(错误号 22)。


我有以下配置:

this->eventConfiguration.sample_freq = 11;
this->eventConfiguration.freq = true;
this->eventConfiguration.inherit = true;
this->eventConfiguration.sample_type = PERF_SAMPLE_CPU | PERF_SAMPLE_TIME | PERF_SAMPLE_PERIOD /*| PERF_SAMPLE_READ*/;

我正在跟踪的事件是 PERF_COUNT_HW_CPU_CYCLES

这是我的系统调用。我监视我电脑的每个内核 :

int fileDescriptor = syscall(__NR_perf_event_open, this->configuration.getEventConfiguration() , -1, i, -1, 0);

报错的处理如下图,不过我觉得没什么用...

if(fileDescriptor < 0) {
  switch(errno) {
    // here is some cases
  };
}

提前致谢! :-)

我找到了错误!

问题是当 perf_event_attr 结构的 inherit 成员设置时,内核不支持使用 PERF_SAMPLE_READ。

以下代码来自内核源代码:https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/events/core.c#n10788

/*
 * We currently do not support PERF_SAMPLE_READ on inherited events.
 * See perf_output_read().
 */
if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
    goto err_ns;