Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure CPU frequency (x86 / x64)

I'm looking for some kind of a library that gives me accurate CPU frequency values periodically on both Intel and AMD processors, on 32-bit and 64-bit Windows.

The purpose of this is to accuratly measure CPU load on a given computer. The problem is that calling QueryPerformanceCounter() returns clock ticks (used to measure the duration of an activity) but the underlying CPU frequency is not constant because of SpeedStep or TurboBoost. I've found several computers where turning off SpeedStep / TurboBoost in the BIOS and doesn't prevent CPU frequency scaling based on load.

I'm trying to see if there are any libraries available that could be used to detect CPU frequency changes (much like how Throttlestop / CPU-Z or even the Overview tab of Resource Monitor in Windows 7) so that I could query and save this information along with my other measurements. Performance counters don't seem to return reliable information, as I have computers that always return 100% CPU frequency, even when other tools show dynamic frequency changes.

I searched for such libraries but most results come back with gadgets, etc., that are not useful.

like image 253
xxbbcc Avatar asked May 08 '12 19:05

xxbbcc


People also ask

How do I check my CPU frequency?

Clock speed (also “clock rate” or “frequency”) is one of the most significant. If you're wondering how to check your clock speed, click the Start menu (or click the Windows* key) and type “System Information.” Your CPU's model name and clock speed will be listed under “Processor”.

Is CPU measured in Hz?

Your CPU processes many instructions (low-level calculations like arithmetic) from different programs every second. The clock speed measures the number of cycles your CPU executes per second, measured in GHz (gigahertz).

What is CPU frequency MHz?

MHz and GHz Are the HeartbeatSpeed and Width Megahertz (MHz) and gigahertz (GHz) are the CPU's clock speed, and the number of bits (8, 16, etc.) are the width of the CPU's registers. The combination of speed and width determines the inherent processing performance of the CPU chip.


1 Answers

You can combine a high-resolution timer with a clock cycle counter to compute the current clock rate. On modern CPUs, the cycle counter can be read with this function:

static inline uint64_t get_cycles()
{
  uint64_t t;
  asm volatile ("rdtsc" : "=A"(t));
  return t;
}

Note that this is per CPU, so if your program gets moved around CPUs, you're in trouble. If you know about CPU pinning techniques on your platform, you might like to try those.

For high resolution time measurement, you can use the tools in <chrono>; here's a semi-useful post of mine on the topic.

like image 184
Kerrek SB Avatar answered Sep 18 '22 11:09

Kerrek SB