58

I need the "perf" utility to monitor the program on my Mac. I know linux comes with it, but is it available on Mac?

I am working on a OSX 10.9 Mavericks and tried "port search" for perf or linux-tools, but I couldn't get any results.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
wlhee
  • 2,334
  • 4
  • 18
  • 19
  • 3
    The perf(1) is heavily dependent on Linux-specific code. Your best bet is probably to try out http://code.google.com/p/gperftools/ which should compile on Mavericks as well. –  Apr 21 '14 at 16:45

4 Answers4

36

As @Sami Laine said in his comment, the Linux perf tool is dependent on Linux specific code. It relies on the perf_event_open system call which is not standardized.

Note: Maybe you could search how MacOSX users are using recent hardware performance counters.

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
  • 8
    @thirty, MacOSX users can access hardware performance counters via `Instruments` application. There is manual how to use them: http://stackoverflow.com/a/13075880/196561 – osgx Apr 28 '14 at 02:36
32

Instruments app

On macOS you can use the Instruments application to profile your code.

I like to use the "Time Profiler" which will show you how much time your application is its various parts during execution. I haven't used perf myself, but from talks/videos that I've seen this seems to be the most common use.

To use the "Time Profiler":

  1. Run Instruments, select Time Profiler
  2. At the top left, select your target (executable).
  3. Hit the Record button on the top left and let it run for a little while.
  4. Pause or Stop the execution and drill down on your calls in the main window.

Hope this helps.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
nenchev
  • 1,998
  • 28
  • 16
  • 2
    Can it profile for any hardware events other than time? e.g. cache misses or branch mispredicts? – Peter Cordes Mar 12 '19 at 03:22
  • @PeterCordes Indeed - how about basic branch prediction stats?! – ATV Mar 06 '23 at 15:28
  • @ATV: [osgx commented](https://stackoverflow.com/questions/23200704/install-perf-on-mac/45624891#comment35726412_23323081) that Instruments can indeed access HW performance counters, linking [Is there anyway to read performance counters on OS X Mountain Lion?](https://stackoverflow.com/a/13075880) . I don't have a Mac and have never used it, but osgx has posted a lot of `perf` answers so I suspect they know what they're talking about. – Peter Cordes Mar 06 '23 at 22:06
15

On OSX you can use sample together with filtercalltree.

Both have useful help text if you run them without commands, but an example invocation to sample process id 1234 for the default 10 seconds at 1ms resolution would be something like:

sample 1234 -f output.prof
filtercalltree output.prof

Once you've generated your call graph, FlameGraph is another great tool for visualizing it, and it supports sample call graphs via the stackcollpase-sample.awk script.

cincodenada
  • 2,877
  • 25
  • 35
Gabriel
  • 2,841
  • 4
  • 33
  • 43
3

Check out Google Perf Tool

If you dont have brew installed:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null

If you have brew installed:

brew install gperftools

Reference: https://github.com/gperftools/gperftools

qwerty
  • 2,065
  • 2
  • 28
  • 39
Dee Dee
  • 31
  • 1