简体   繁体   中英

C++ Linux get CPU nominal frequency

I need to get a CPU nominal frequency under Linux within C++ code. As far as I know, the /proc/cpuinfo contains a current frequency.

Is /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq

always reliable as nominal frequency in kHz?

Or is there a better way to get this information from C++?

C++ doesn't have any means to access this value, it doesn't even assume there is such a value. Even in your case, you are just looking at one of the CPUs, not all of them. This means that you will have to do something OS-dependent, which you can probably build in C++.

Anyhow, do you know of any tool providing that number? Get its sourcecode and see how it does it! Other than that, I'd look at /proc/cpuinfo and build the average of the "cpu MHz" values there. The /proc filesystem is basically a predecessor of the /sys filesystem, but the info should be the same.

There is not a single universal way how to read the nominal frequency. You can read the nominal frequency of Intel processor from /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq (nominal + 1 MHz) when using acpi-cpufreq scaling driver, however intel_pstate sets content of this file to maximum turbo frequency. Nevertheless there are alternative solutions. Intel CPUs have the nominal frequency written as a part of the CPU model name (readable using CPUID instruction when input EAX = 0x01 or from /proc/cpuinfo ) or you may read the MSR_PLATFORM_INFO (0xCE) register.

EDIT: AMD processors provide the nominal frequency in /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq without additional 1 MHz

EDIT2: Some processors may have /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_nominal_freq file, eg POWER9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM