I would like to know how to get performance monitoring counter measurements on a Mac. With Linux, we can use the "perf stat" command to get measurements. With a Mac it's harder. Specifically, I would like to know how I can get the number of branch mispredictions and the number of branches executed for a program running from the command line, preferably without recompiling the program. I have done some Googling but what I need is a solution; if you can show me how you brought up a terminal, typed in some command (maybe using some tool you downloaded), and got the number of mispredictions for, say /bin/ls, that would be great.
-
Check https://stackoverflow.com/a/61131670/196561 post - Mac OS has https://en.wikipedia.org/wiki/Instruments_(software) to profile apps and there is CLI variant of tool https://help.apple.com/instruments/mac/current/#/devb14ffaa5. Hardware perf counters are usually privileged and needs special drivers or helper in OS kernel. There are sampling and rate over time views for Instruments GUI with support of HW PMU counters. https://www.robertpieta.com/counters-in-instruments/ – osgx Jul 27 '20 at 14:27
-
I thought Pin was just for instrumenting programs but how do you read performance monitoring counter registers with pin in macOS? that would be very helpful. Thanks – parisa Jul 27 '20 at 15:49
-
not intel Pin, but `instruments` tool from xcode - https://help.apple.com/instruments/mac/current/#/devb14ffaa5. There is CLI variant of using instruments to collect performance counters (PMU), but to use it you should first select or create correct counters Template with GUI. Not sure how to view collected data with CLI tool. Just use GUI Instruments https://help.apple.com/instruments/mac/current/ – osgx Jul 27 '20 at 17:45
-
The program is a binary executable that I don't want to recompile. Instrumenting it would perturb the branch predictor so I need to run the original program unmodified – parisa Jul 27 '20 at 19:00
-
parisa, Instruments tool, both in GUI and CLI mode does not require any recompiling. It is just named "Instruments.app" and is part of Xcode ("Xcode, choose Xcode > Open Developer Tool > Instruments."), but it will work for most template without recompilation. – osgx Jul 27 '20 at 19:14
1 Answers
Official macOS tool to work with profiling, both based on software events and on hardware events (performance counters), is Instruments tool - https://help.apple.com/instruments/mac/current/. It seems to be part of XCode development tools.
This tool have some limited variants to collect profiles from command-line mode, check https://help.apple.com/instruments/mac/current/#/devb14ffaa5
https://help.apple.com/instruments/mac/current/#/devba105ecc
Launch Instruments from the command line
You can use the open command to launch any app in OS X via Terminal, including Instruments.
Launch Terminal (in /Applications/Utilities/).
Run the following command:
open /Applications/Xcode.app/Contents/Applications/Instruments.app
The Instruments app launches.
Note: You can also use either of two command-line utilities to profile an app without actually displaying the Instruments user interface.
instruments
— This utility profiles an app using a specified template. The results can be saved to a file and then manually opened in the main Instruments app for viewing and analysis. To learn more, go to Profile with the instruments command-line tool.
To get IPC or branches, use GUI to create profile with usage of counters usage (1 or 2) and save it as template, and then use CLI tool instruments
to collect trace data. Trace file can be viewed with Instruments GUI; there was incomplete attempt to decode that files.
The page https://medium.com/@pavelkucera/counting-branch-mispredictions-on-macos-7397ae8c5b51 also lists another variant to work with hardware counters on macOS, the https://github.com/opcm/pcm project:
You first have to build the tool, but then it is easy to use:
run pmu-query.py enter “BR_INST_RETIRED.NOT_TAKEN”, the result should be similar to: cpu/umask=0x10,event=0xC4,name=BR_INST_RETIRED.NOT_TAKEN/ run ./pcm-core.x -e event where “event” stands for the result from the previous step
This gives you continuous results for all the running processes. Find info on how to profile a single process in
./pmc-core.x --help
The good news is that results are easily readable as they can be output as csv file. The bad news is that profiling a single process still includes activity from other processes.

- 90,338
- 53
- 357
- 513
-
Thanks, ./pmc-core.x seems like what I want. But how can I monitor only one program and not interrupt it for sampling during execution? – parisa Jul 27 '20 at 19:37
-
Something like that works ./pcm-core.x -- /bin/ls -e cpu/umask=0x10,event=0xC4,name=BR_INST_RETIRED.NOT_TAKEN/ But still I don't want the sampling to interrupt the program execution. – parisa Jul 27 '20 at 19:55
-
`pcm-core.x` of https://github.com/opcm/pcm/blob/master/pcm-core.cpp does not interrupt your program by default. With `./pcm-core.x 1` it will interrupt one time per second to print current values of counters. – osgx Jul 27 '20 at 20:38