Record values of Performance Monitor Counters (PM events) on OS X without Instruments

MacosProfilingInstruments

Macos Problem Overview


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.

Macos Solutions


Solution 1 - Macos

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSophie AlpertView Question on Stackoverflow
Solution 1 - MacosMax RafiandyView Answer on Stackoverflow