I have written the following code to get current CPU frequency in my kernel module C file:
#include <linux/cpufreq.h>
void func() {
printk(KERN_ALERT "CPU Freq in KHz: %d\n", cpufreq_get(3));
}
I am getting CPU Freq in KHz: 0
I have checked that my kernel is built with CONFIG_CPU_FREQ=y
, CONFIG_X86_INTEL_PSTATE=y
with the following code grep -i cpu_freq /boot/config-$(uname -r)
and grep -i pstate /boot/config-$(uname -r)
.
I can check frequency easily in the sysfs file system:
cat /sys/devices/system/cpu/cpu5/cpufreq/scaling_cur_freq
Why is it not accessible in Linux Kernel module?
The user-space utility cpufreq-info
is also giving output about current CPU frequency.
I am running on Ubuntu 16.04 X86_64.
EDIT:
The following code also outputs CPU Freq in KHz: 0
:
void func() {
#ifdef CONFIG_CPU_FREQ
printk(KERN_ALERT "CPU Freq in KHz: %d\n", cpufreq_get(3));
#endif
}
Therefore, CONFIG_CPU_FREQ
is surely defined. I have checked with #pragma and it has 1
value.