nh2 / haskell-cpu-instruction-counter

Measuring CPU instructions in Haskell using Linux Performance Counters
MIT License
83 stars 5 forks source link

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

Open nh2 opened 6 years ago

nh2 commented 6 years ago

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.

VitorRamos commented 5 years ago

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.
nh2 commented 5 years ago

@VitorRamos Awesome, thanks for providing this info!

nh2 commented 5 years ago

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