2

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.

sal_guy
  • 199
  • 2
  • 14
  • You probably need to include the kernel header with the current configurations (kconfig.h) so that you get CONFIG_CPU_FREQ defined before cpufreq.h is included. In kernel space, if that isn't defined, cpufreq.h replaces the cpufreq_get() call with an inline static definition that always returns 0. – FBergo May 01 '18 at 02:02
  • Please see my edit. I have checked and `CONFIG_CPU_FREQ` is defined. – sal_guy May 01 '18 at 13:02
  • cpufreq_get() works on a test module I wrote (Fedora 23, kernel 4.8.13). The scaling_cur_freq code in the kernel uses arch_freq_get_on_cpu() when the cpufreq driver is not active. See this test module: https://github.com/fbergo/freqmod , one of the two printed values should be the current frequency (I get 0 on f[0], 1400000 on f[1] on my machine). – FBergo May 01 '18 at 14:22
  • I think Fedora might have a different cpufreq driver. My driver is `intel_pstate`. I checked. `arch_freq_get_on_cpu` is not even defined in my case. `cpufreq_get` and `cpufreq_generic_get` both are not working. – sal_guy May 01 '18 at 19:43

0 Answers0