4

Both Instruments and iprofiler are able to use performance counters (PMCs) to monitor application performance. But, I would like to know whether there is an API to access PMCs from an application. In Linux this can be done, for instance, by calling sys_perf_event_open, and then reading from the file descriptor returned by the syscall. Alternatively, libpfm can be used too.

For OS X, I have found the Intel Performance Counter Monitor. Unfortunately, it requires installing a kernel module. The module is not signed, and therefore it might not be straightforward to load it for end-users.

Given that both Instruments and iprofiler are actually able to access PMCs (without installing any kernel module), I wonder wether there is some (unlisted) API to access PMCs.

betabandido
  • 18,946
  • 11
  • 62
  • 76
  • Possible duplicate of [Record values of Performance Monitor Counters (PM events) on OS X without Instruments](http://stackoverflow.com/questions/32536551/record-values-of-performance-monitor-counters-pm-events-on-os-x-without-instru) – Sophie Alpert Apr 25 '17 at 20:13

1 Answers1

0

I don't know of any official API or premade library, but RDMSR and RDTSC are a security critical opcodes, and therefore limited to ring 0, so you will need to write an OSX kernel module to expose it. Just note that including such module into an end-user app will turn it into a security hazard. So I'm not surprised Apple and Intel avoid providing an easy "enter admin password here" access to it.

  • RDTSC (read timestamp) *can* be limited to ring 0 on some new hardware I think, but historically it works in any mode. High-precision timing isn't security critical (except as part of Spectre side-channels so browsers often disable it for client-side JS). I'd be surprised if MacOS defaults to stopping user-space from using RDTSC. (RDMSR is always kernel-only). RDPMC (read perf counter) is normally privileged, but the kernel can set it to be usable from user-space. – Peter Cordes Oct 03 '19 at 16:50