Detect current CPU Clock Speed Programmatically on OS X?
Solution 1
Try this tool called "Intel Power Gadget". It displays IA frequency and IA power in real time.
http://software.intel.com/sites/default/files/article/184535/intel-power-gadget-2.zip
Solution 2
You can query the CPU speed easily via sysctl
, either by command line:
sysctl hw.cpufrequency
Or via C:
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>
int main() {
int mib[2];
unsigned int freq;
size_t len;
mib[0] = CTL_HW;
mib[1] = HW_CPU_FREQ;
len = sizeof(freq);
sysctl(mib, 2, &freq, &len, NULL, 0);
printf("%u\n", freq);
return 0;
}
Solution 3
Since it's an Intel processor, you could always use RDTSC. That's an assembler instruction that returns the current cycle counter — a 64bit counter that increments every cycle. It'd be a little approximate but e.g.
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
uint64_t rdtsc(void)
{
uint32_t ret0[2];
__asm__ __volatile__("rdtsc" : "=a"(ret0[0]), "=d"(ret0[1]));
return ((uint64_t)ret0[1] << 32) | ret0[0];
}
int main(int argc, const char * argv[])
{
uint64_t startCount = rdtsc();
sleep(1);
uint64_t endCount = rdtsc();
printf("Clocks per second: %llu", endCount - startCount);
return 0;
}
Output 'Clocks per second: 2002120630' on my 2Ghz MacBook Pro.
Tim
iOS Developer. Used to work at a place, decided working at home was cooler.
Updated on June 27, 2022Comments
-
Tim almost 2 years
I just bought a nifty MBA 13" Core i7. I'm told the CPU speed varies automatically, and pretty wildly, too. I'd really like to be able to monitor this with a simple app.
Are there any Cocoa or C calls to find the current clock speed, without actually affecting it?
Edit: I'm OK with answers using Terminal calls, as well as programmatic.
Thanks!
-
Tim about 12 yearsOn my MBA, it shows 1.8ghz, which is exactly right, but is there any way to force the i7 into turbo boost?
-
Mysticial about 12 years
rdtsc
is not a reliable way to measure a processor's clock speed. It does not adjust itself to turbo-boost/CPU-throttling (among other things). -
Tommy about 12 years@Mysticial on the contrary; it's usually advised as being an unreliable way to measure a CPU's clock speed exactly because it is affected by turbo boost and CPU throttling.
-
Mysticial about 12 years@Tommy My tests suggests other wise. On my Xeon at stock 3.2 GHz
rdtsc
always says 3.2 GHz regardless of what I set the multiplier to (2.4, 2.6, 2.8, 3.0, 3.2 GHz... all get reported as 3.2 GHz byrdtsc
)rdtsc
seems to change only when you start messing with the bus speeds. -
Tim about 12 yearsIs there a way to determine what the multiplier is? I'm interested in both knowing what speedstep pulls the CPU down to at idle and where TurboBoost puts the CPU under load.
-
Mysticial about 12 years@Tim, I think you have to access the BIOS to get bus-speed/multiplier information. There's no easy way to do that since there are hundreds of different motherboard/BIOS/CPU systems...
-
Tim about 12 years@Mystical I've seen some apps around which claim to find the information about current core speed, including Intel's own app (on Windows), but no source code anywhere. I'm sure there's a way to do it - Intel has a big ole' pdf with sample code but I can't get it to compile - though I'll continue trying.
-
Tim over 11 yearsUnfortunately, this seems to return the "box advertised" speed (now 2.3ghz on my MBPR) but not the SpeedStep adjusted speed.
-
Tim over 11 yearsHowever, this is a useful and easy insight into the use of sysctl, so thank you for that!
-
Tim about 11 yearsThis is really exactly what I wanted. It even includes sample objective-c... Perfect!
-
Tim about 11 yearsRunning
sysctl kern.clockrate
at command line gives me a steadykern.clockrate: { hz = 100, tick = 10000, tickadj = 2, profhz = 100, stathz = 100 }
where the Intel CPU profiler is showing varying values, so I don't think this works with the speedstep adjustments. -
mist about 10 yearsDoes not give current, but advertised speed. hw.cpufrequency, hw.cpufrequency_min, hw.cpufrequency_max all give the same.
-
Ken Aspeslagh about 9 yearsThis code will overflow the Hz on the new generation of > 4Ghz processors. Need to use this instead. sysctlbyname("hw.cpufrequency_max", &speed, &len, NULL, 0); to support a 64-bit value for the frequency.
-
rogerdpack over 7 yearshmm
sysctl hw.cpufrequency
always reports 2500000000 for me. The Intel Power Gadgetreports varying "IA" values from 1.3Ghz to 3.4GHz, wonder if there's a way to get more up to date values...