5

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++?

Dejwi
  • 4,393
  • 12
  • 45
  • 74

2 Answers2

1

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.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • I know, that this is OS-dependend and I have to get this value under Linux. Unfortunately /proc/cpuinfo contains a current frequency, not a maximum one. – Dejwi Dec 21 '15 at 08:46
1

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, e.g. POWER9

Andrew
  • 958
  • 13
  • 25
  • afaik, that's feature of OS build, if it "knows" that CPU. Programs literally have to figure out the model and cross-reference it with datasheet. E.g. if processor is too new or too old or too exotic, all bets are off. – Swift - Friday Pie May 19 '21 at 07:38
  • There are no bets, what I have written works perfectly for Intel processors. I am not so familiar with the AMD, but I am working with one right now, so I edited my answer to be more precise. – Andrew May 19 '21 at 08:08