I am trying to use perf stat to measure the energy consumed by part of my code. To do that, I need to run my application first and once it reaches a certain point, I run perf stat, passing application's pid to it using -p flag. While this approach works for typical performance counters, it does not work with PMU events like power/energy-pkg/ or power/energy-ram. Does anyone know any workaround to capture the energy consumed by a block of the code?
Minimal, Reproducible Example:
$ cat example.c
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid= getpid();
printf("my pid is %d\n", pid);
sleep(15);
return 0;
}
$ gcc example.c
$ sudo perf stat -e power/energy-pkg/ ./a.out #(This works gracefully)
$ ./a.out &
$ pid=$!
$ sudo perf stat -e power/energy-pkg/ -p $pid #(This doesn't work)
Performance counter stats for process id '1749416':
<not supported> Joules power/energy-pkg/
PS: I already went through the following posts, but they didn't resolve my issues: