14

I am aware of these answers:

https://stackoverflow.com/questions/9948987/detect-current-cpu-clock-speed-programmatically-on-os-x

View the current clock Speed of a CPU in OS X?

Which refer to Intel Power Gadget as a way to obtain current CPU frequency, is there a program/method to obtain this information from the terminal?

sysctl hw.cpufrequency only shows the nominal frequency of the CPU, i.e. what the manufacturer has stated. It does not consider Turbo Boost or lower frequencies when CPU is idle. (For example if CPU is i5, 1.4GHz, it will always state 1.4GHz)

The C solution given it SO using sysctl.h has the same effect.

  • The answers behind the StackOverflow link contain a Terminal based answer. Can you describe how this doesn't work for you? – nohillside Jun 29 '18 at 8:39
  • All solutions on SO only show the nominal CPU frequency, it does not change according the the actual frequency, and does not correlate with Power Gadget – amanusk Jun 29 '18 at 9:18
  • Related: stackoverflow.com/a/8353086/4703819 – Allan Jun 29 '18 at 10:11
  • From what I understand, the "frequency" is calculated based on a number of factors. There's no "probe" connected to the CPU to measure in real-time what the frequency actually is. – Allan Jun 30 '18 at 13:40
  • 1
    If it works on Linux, would it be an option to look at the source code there? – nohillside Jul 1 '18 at 10:09
4
+50

Since your comment mentions that you're really just focused on enabling Terminal (or more accurately not desktop)...

I think that the Intel Power Gadget will help you get your info from Terminal. From Intel:

Intel® Power Gadget also provides a C/C++ Application Programming Interface (API) for accessing this power and frequency data in your program; the API is supported on Windows and Mac OS X. For more information on the API's, see:

And on that same page, also a link to Using the Intel Power Gadget API on Mac OS X

Sorry this isn't a link to an out of the box solution; I spent a few minutes with google to see if I could find someone who already did this without luck.

  • 1
    This is the best answer so far. I was hoping there is a way to obtain frequency information without installation of external software (with something like sysctl), or by using open source software. – amanusk Jul 3 '18 at 21:19
2

Try top -F -n0 -s3 | grep "CPU usage"

top display and update sorted information about processes
-F do not calculate statistics on shared libraries (frameworks)
-n0 display zero processes (because we're not interested in them)
-s3 update every 3 seconds (default is 1)
grep shows us only the line containing the phrase CPU usage

Example Output:

$ top -F -n0 -s3 | grep "CPU usage"
CPU usage: 9.36% user, 20.0% sys, 70.63% idle 
CPU usage: 8.35% user, 8.19% sys, 83.45% idle 
CPU usage: 8.18% user, 7.77% sys, 84.4% idle 
CPU usage: 5.89% user, 8.1% sys, 86.8% idle

You could even parse the output for display elsewhere.

Be aware that top is very resource intensive whilst it is running.

Edit: Ah, you want frequency. I know iStat Menus has to install a helper to get that info in realtime.

  • 2
    Indeed frequency is the issue, top and htop do not provide that unfortunately. – amanusk Jul 5 '18 at 17:03
2

I have to think this is a bit of an X Y question in that "What are you going to do once you get this number?" and want to answer that directly, but let's dive a bit into what you're trying to measure. CPU interrupts on macOS are shaped in intervals of 150 ms and much of this detail is public from WWDC 2013 and later on power management, App Nap (Session 209 in particular is both good and approachable) and battery life optimizations on macOS.

timer coalescing - energy savings over a time scale of 150 ms on macOS from WWDC 2013 session 209

Since the code and each core of a CPU can and will change hundreds of times a second based on ephemeral load factors, power optimizations that consider what's visible on the screen, what network data arrives, the idea that a modern CPU even has one "common" clock rate at any one point in time seems to vastly over-simplify reality. With 8 cores on many MacBook Pro and dozens of cores on the iMac Pro - you're boiling a ton of complexity down to one number.

The boosts when a single core can run over clocked are less likely to be easily measured, but you can measure thermal throttling very simply with the thermal logging of pmset.

pmset -g therm

You can of course log the spontaneous changes to the thermal throttling of CPU with pmset -g thermlog and then map that to the CPU specifications if you can gather them elsewhere.

  • An option to obtain a per-core frequency is also welcome :). The frequency can change many times a second, but a sample of that second would most likely be enough, for example, if you want to know frequency at idle or in full Turbo (like in a stress test). Determining power and temperature is actually possible (although not straight forward) by reading SMC keys, I was hoping there is an SMC key for frequency as well. The fact is, there is software that obtains this information (Intel power gadget), the question is how can one do this without extra software. – amanusk Jul 8 '18 at 8:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .