perf_event_open() always get zero when specifying CPU ID

34 Views Asked by At

I am reading Perf performance counters using this example:

https://gist.github.com/teqdruid/2473071

However, instead of:

fd[0] = perf_event_open(&attr[0], getpid(), -1, -1, 0);

I only want to know count events for the the caller thread (which is pinned to it's CPU earlier).

According to the man page I need to pass in the CPU ID:

pid > 0 and cpu >= 0
              This measures the specified process/thread only when
              running on the specified CPU.

https://man7.org/linux/man-pages/man2/perf_event_open.2.html

So I am passing the CPU id using sched_getcpu():

fd[0] = perf_event_open(&attr[0], getpid(), sched_getcpu(), -1, 0);

However, I always get zero for any counter, whereas if I replace the cpu id with -1 I do get results.

I check the CPU ID at the start is the same as the one when reading counters.

I am running using sudo etc.

Does perf_event_open() expect CPU ID in another way?

0

There are 0 best solutions below