Measure processor frequency (x86 / x64)

I am looking for some kind of library that periodically gives me the exact values โ€‹โ€‹of the processor frequency on Intel and AMD processors, on 32-bit and 64-bit Windows.

The purpose of this is to accurately measure the CPU utilization on this computer. The problem is that calling QueryPerformanceCounter() returns clock ticks (used to measure activity duration), but the base frequency of the processor is not constant due to SpeedStep or TurboBoost. I found several computers where shutting down SpeedStep / TurboBoost in the BIOS does not prevent scaling of the processor frequency depending on the load.

I'm trying to find out if there are any libraries that can be used to detect changes in the processor frequency (same as Throttlestop / CPU-Z or even the Resource Monitor Overview tab in Windows 7), so that I could query and save this information along with my other measurements. Performance counters do not seem to return reliable information, since I have computers that always return 100% of the processor frequency, even if other tools show dynamic changes in frequency.

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

+6
source share
3 answers

You can combine a high-resolution timer with a clock counter to calculate the current clock frequency. On modern processors, the loop counter can be read using this function:

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

Please note what kind of processor it is, so if your program moves around processors, you have problems. If you know about methods of securing the processor on your platform, you can try them.

To measure high-resolution time, you can use the tools in <chrono> ; here is a semi useful post on this topic.

+6
source

Try to focus on what you are trying to do, not how to do it.

What is your ultimate goal?

If, as you say, you are trying to "measure CPU utilization on a given computer," on Windows, this may be a good practice using "PdhOpenQuery" and the "Pdh *" family functions.

See also this answer:

How to determine CPU and memory consumption from inside the process?

+1
source

Take a look at the __rdtsc internal function (#include "intrin.h" in Visual Studio).

This gives the hour counter directly from the processor via the x86 / x64 RDTSC (Read Timestamp) function.

+1
source

Source: https://habr.com/ru/post/915218/


All Articles