How to scale CPU frequency with DVFS framework
From SomLabs Wiki
How to scale CPU frequency with DVFS framework
Intoduction
CPU frequency scaling enables the operating system to scale the CPU frequency in order to minimize power usage (saving power when the full performance of the CPU is not needed). CPU frequency scaling is implemented in the Linux kernel as a Dynamic Voltage and Frequency Scaling (DVFS) framework. DVFS allows to set min/max CPU frequency and choose a scaling governor that governs it.
Dynamic Frequency Scaling – Linux configuration and sysfs interface
To enable dynamic frequency scaling, we need to have a kernel with the CPUfreq drivers built-in (done by default when available):
CPU Power Management ---> CPU Frequency scaling ---> [*] CPU Frequency scaling <*> CPU frequency translation statistics [ ] CPU frequency translation statistics details *** CPU frequency scaling drivers *** < > Generic DT based cpufreq driver <*> Freescale i.MX6 cpufreq support <*> Freescale i.MX7 cpufreq support
The SoMLabs modules supports DVFS feature by default, there is no need to perform manual kernel recompilation.
DVFS framework exports information to userspace through sysfs interface:
root@somlabs:/# ls -l /sys/devices/system/cpu/cpu0/cpufreq/ total 0 -r--r--r-- 1 root root 4096 Apr 24 06:54 affected_cpus -r-------- 1 root root 4096 Apr 24 06:54 cpuinfo_cur_freq -r--r--r-- 1 root root 4096 Apr 24 06:54 cpuinfo_max_freq -r--r--r-- 1 root root 4096 Apr 24 06:54 cpuinfo_min_freq -r--r--r-- 1 root root 4096 Apr 24 06:54 cpuinfo_transition_latency -r--r--r-- 1 root root 4096 Apr 24 06:54 related_cpus -r--r--r-- 1 root root 4096 Apr 24 06:54 scaling_available_frequencies -r--r--r-- 1 root root 4096 Apr 24 06:54 scaling_available_governors -r--r--r-- 1 root root 4096 Apr 24 06:54 scaling_cur_freq -r--r--r-- 1 root root 4096 Apr 24 06:54 scaling_driver -rw-r--r-- 1 root root 4096 Apr 24 06:54 scaling_governor -rw-r--r-- 1 root root 4096 Apr 24 06:54 scaling_max_freq -rw-r--r-- 1 root root 4096 Apr 24 06:54 scaling_min_freq -rw-r--r-- 1 root root 4096 Apr 24 06:54 scaling_setspeed drwxr-xr-x 2 root root 0 Apr 24 06:54 stats
For dynamic frequency scaling, the most important part of interface is:
scaling_available_frequencies | list of available frequencies [kHz] |
scaling_available_governors | this file shows the CPUfreq governors available in this kernel |
scaling_cur_freq | current frequency of the CPU as determined by the governor and cpufreq core [kHz] |
scaling_governor | show the current governor. By "echoing" the name of another governor you can change it |
scaling_min_freq
scaling_max_freq |
show the current "policy limits" (in kHz). By “echoing” new values into these files, you can change these limits |
source: www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt
Frequency Scaling How-To
To get list of supported frequencies for iMX6 ULL SoC:
root@somlabs:/# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
198000 396000 528000 792000
To ask for the current frequency type:
root@somlabs:/# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
198000
Now let’s perform simple stress test, to check whether the CPU frequency scaling works corectly:
root@somlabs:/# dd if=/dev/zero of=/dev/null &
root@somlabs:/# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
792000
To show the current "policy limits" (lower and upper frequency limits):
root@somlabs:/# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
198000
root@somlabs:/# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq 792000
By “echoing” new values into scaling_min_freq and scaling_max_freq files, you can change these limits. For example you can decrease the maximum frequency to 528 MHz:
root@somlabs:/# echo 528000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
Scaling Governors
The strategy of frequency scaling is controlled by the "governor" which continuosly monitors the system perfomance requirements. The “scaling governor” decides, what CPU frequency is used at each time.
Linux offers different CPU scaling governors. Let’s list all available governors with the following command:
root@somlabs:/# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
interactive conservative userspace powersave ondemand performance
userspace | allows the user or any userspace
program running with UID "root", to set the CPU to a specific frequency by making a sysfs file scaling_setspeed available in the CPU-device directory |
powersave | sets the CPU statically to the lowest frequency within the borders of scaling_min_freq and scaling_max_freq |
ondemand | sets the CPU frequency depending on the current system load |
performance | sets the CPU statically to the highest frequency within the borders of scaling_min_freq and scaling_max_freq |
interactive | designed for latency-sensitive,
interactive workloads; this governor sets the CPU speed dependin on usage, similar to "ondemand" and "conservative" governors; however, the governor is more aggressive about scaling the CPU speed up in response to CPU-intensive activity |
conservative | much like the "ondemand"
governor, sets the CPU depending on the current usage. It differs in behaviour in that it gracefully increases and decreases the CPU speed rather than jumping to max speed the moment there is any load on the CPU. This behaviour more suitable in a battery powered environment |
(source: www.kernel.org/doc/Documentation/cpu-freq/governors.txt)
In case of SoMLabs modules, default governor is ondemand. The default governor (and list of available governors) can be configured through make menuconfig tool:
CPU Power Management --->
CPU Frequency scaling ---> Default CPUFreq governor (ondemand) --→ -*- 'performance' governor <*> 'powersave' governor <*> 'userspace' governor for userspace frequency scaling -*- 'ondemand' cpufreq policy governor <*> 'interactive' cpufreq policy governor <*> 'conservative' cpufreq governor
To switch over to another governor (for example powersave):
root@somlabs:/# echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
powersave
In order to ask for the current governor:
root@somlabs:/# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
powersave
To find out more about the governors, please visit: