haskell-cpu-instruction-counter icon indicating copy to clipboard operation
haskell-cpu-instruction-counter copied to clipboard

Investigate whether and when perf_event_open() is process-wide or thread-wide

Open nh2 opened this issue 7 years ago • 3 comments

From the man page:

       The pid and cpu arguments allow specifying which process and CPU to
       monitor:

       pid == 0 and cpu == -1
              This measures the calling process/thread on any CPU.

So what now, is it the calling process or thread? How do I control that?

Quite likely, if I run perf_event_open() from a forked pthread, it's going to count only for that pthread. But what if I run it from the process's (only) main thread, and afterwards create a new pthread? Does the perf FD then record the events only for the main thread, or the whole process?

That would be important to know, so that we don't accidentally give wrong numbers when running in the threaded runtime.

nh2 avatar Jun 09 '18 14:06 nh2

There is a flag inherit on the perf_event_attr structure where you specify if is going to count for all threads or only the main thread

From the perf docs inherit: The inherit bit specifies that this counter should count events of child tasks as well as the task specified. This applies only to new children, not to any existing children at the time the counter is created (nor to any new children of existing children).

          Inherit does not work for some combinations of read_format
          values, such as PERF_FORMAT_GROUP.

VitorRamos avatar Nov 19 '18 10:11 VitorRamos

@VitorRamos Awesome, thanks for providing this info!

nh2 avatar Nov 24 '18 18:11 nh2

Now we just have to ensure that we use it appropriately.

nh2 avatar Nov 24 '18 18:11 nh2