CPU frequency scaling

From Alpine Linux
Revision as of 01:40, 19 September 2017 by John3-16 (talk | contribs)

CPU frequency scaling is a feature of many modern processors whereby the CPU frequency can be changed at runtime. In this way, the system can be optimized for either powersaving (minimal frequency), performance (maximal frequency), or a combination (automatic switching). The latter would e.g. be optimal for a server that is idle for most of its uptime, but must sustain high CPU throughput when it does receive requests.

Electing a governor

The CPU frequency scaling is handled by a so-called CPU governor, which decides which frequencies to use and when to switch between them. The most common governors are the following:

Governor Frequency Switching
performance Maximum None
powersave Minimum None
ondemand Automatic Immediate
conservative Automatic Gradual
userspace Custom Custom

In order to check which governors are available for your processors:

cat /sys/devices/system/cpu/cpufreq/policy*/scaling_available_governors

To e.g. change the governor of processor 0 to ondemand:

echo ondemand > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor

To see that the governor you chose works as expected, you may compare scaling_cur_freq to scaling_min_freq and scaling_max_freq for different system loads. All of these files are located in the same folder /sys/devices/system/cpu/cpufreq/policy0 as the governor settings above.

Manipulating the governor

Some governors can be configured further; these settings can be found in the folders /sys/devices/system/cpu/cpufreq/*. For instance, the ondemand governor defaults to switching to a higher frequency when the CPU usage increases beyond 95%. If we wish to lower this threshold to e.g. 80%, we could run:

echo 80 > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold

Automatic configuration

Once we have found a governor that we are satisfied with, we might want to keep it when the system is rebooted. This can be done by creating a local service in /etc/local.d. First, create a new local service /etc/local.d/cpufreq.start with the relevant CPU frequency scaling commands discussed above:

#!/bin/sh

# Set the governor to ondemand for all processors
for cpu in /sys/devices/system/cpu/cpufreq/policy*; do
  echo ondemand > ${cpu}/scaling_governor              
done

# Reduce the boost threshold to 80%
echo 80 > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold

Make the boot script executable, make sure local services are started at boot, and sync your changes:

chmod +x /etc/local.d/cpufreq.start
rc-update add local default
lbu commit

Reboot your computer, and CPU frequency scaling should be enabled automatically.