Search code examples
c++linuxlinux-kernelperf

How to use PERF_SAMPLE_READ with mmap


This question is related to the perf_event_open syscall, but there is no tag for it.

I'm currently looking to use the PERF_SAMPLE_READ member of the enum perf_event_sample_format to retreive some data from a memory map, but for an unknown reason, the syscall itself return "invalid argument" (errno 22).


I have the following configuration :

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*/;

The event I'm tracking is PERF_COUNT_HW_CPU_CYCLES.

There is my syscall. I spy each core of my computer :

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

The handling of the error is shown below, but I don't think it's useful...

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

Thanks in advance ! :-)


Solution

  • I've found the bug !

    The problem is that the kernel don't support the use of PERF_SAMPLE_READ when the inherit member of the perf_event_attr structure is set.

    The following code is from the kernel sources : 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;