53

In Xcode's Instruments, there is a tool called Counters that exposes low-level counter information provided by the CPU, such as the number of instructions executed or number of cache misses:

screenshot of Counters tool in Instruments

This is similar to the Linux syscall perf_event_open introduced in Linux 2.6.32. On Linux, I can use perf_event_open then start/stop profiling around the section of my code I'm interested in. I'd like to record the same type of stats on OS X: counting the instructions (etc.) that a certain piece of code takes, and getting the result in an automated fashion. (I don't want to use the Instruments GUI to analyze the data.)

Are there any APIs that allow this (ex: using dtrace or similar)? From some searching it sounds like the private AppleProfileFamily.framework might have the necessary hooks, but it's unclear how to go about linking to or using it.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
  • 1
    One potential solution here would be to use the Intel PMC kext that exposes this information (http://www.campos.cc/blog/2014/03/15/using-intel-performance-monitor-on-mac-os-x), but it doesn't appear to provide per-process information, only totals, which is less useful to me. – Sophie Alpert Sep 12 '15 at 08:23
  • 4
    Apple engineer Kris Markel says on Twitter: "There's nothing available at this time. Please file an enhancement request through bugreport.apple.com." (https://twitter.com/existopher/status/644943616889126912) – Sophie Alpert Sep 19 '15 at 00:02
  • [PAPI can be compiled on OS X](http://icl.cs.utk.edu/papi/forum/viewtopic.php?f=2&t=1274). Unfortunately, hardware counters are not supported on this platform, only software events. This makes it much less useful. – Sjlver Jun 21 '16 at 16:46

1 Answers1

1

In GNU/Linux I use Intel's PCM to monitor CPU utilization. I'm not sure if this works fine on OSX, but as far as I know the source-code is including the MacMSRDriver directory. I have no any OSX device, never test it anyway.

In case this source compiled on your device, Just run:

pcm.x -r -- your_program your_program_parameter

or if you want advanced profiling, use pcm-core.x instead or you can build your own code based on pcm-core.cpp

  • I noted in comments to my original question that the PMC kext doesn't appear to provide per-process information. Let me know if I'm missing something. – Sophie Alpert Jun 30 '16 at 01:18
  • I never use PMC kext. Btw, what I mention here isnt about PMC but the Intel's PCM. You can see the documentation at https://software.intel.com/en-us/articles/intel-performance-counter-monitor – Max Rafiandy Nov 07 '16 at 07:21