Search code examples
linuxkernelhardware

How can I get current cpufreq in kernel code?


I want know cpufreq in kernel code and use it but I don't know how to get it.

IN /proc/cpuinfo :
cpu MHz :

Multiple queries, the CPU frequency does not change.


Solution

  • To obtain CPU frequency, utilise the linux/cpufreq.h header in the Linux kernel is likely the most effective method. While /proc/cpuinfo in user space does offer information on CPU frequency, it might not accurately represent immediate fluctuations, given the dynamic adjustments made by CPU frequency scaling policies and the frequency at which this data is refreshed.

    #include <linux/cpufreq.h>
    ...
    unsigned int freq = cpufreq_get(0); // Get frequency of CPU 0 in kHz
    ...