Assembly CPU frequency measuring algorithm

Sergey Kivanov picture Sergey Kivanov · Sep 15, 2008 · Viewed 10.6k times · Source

What are the common algorithms being used to measure the processor frequency?

Answer

Nathan Fellman picture Nathan Fellman · Sep 15, 2008

Intel CPUs after Core Duo support two Model-Specific registers called IA32_MPERF and IA32_APERF.
MPERF counts at the maximum frequency the CPU supports, while APERF counts at the actual current frequency.

The actual frequency is given by:

freq = max_frequency * APERF / MPERF

You can read them with this flow

; read MPERF
mov ecx, 0xe7
rdmsr
mov mperf_var_lo, eax
mov mperf_var_hi, edx

; read APERF
mov ecx, 0xe8
rdmsr
mov aperf_var_lo, eax
mov aperf_var_hi, edx

but note that rdmsr is a privileged instruction and can run only in ring 0.

I don't know if the OS provides an interface to read these, though their main usage is for power management, so it might not provide such an interface.