An introduction to Linux CPU governor

One of the advanced features in Linux is the CPU governor. It helps you to control the speed, performance, temperature, and even quietness of your computer. In this article, we explain how to manage the CPU governor in Linux.

Understanding CPU frequency driver

The first step is recognizing which driver is currently used by your Linux kernel to manage your computer’s CPU. Different CPU models use distinctive drivers. We have listed these kernel modules that may be used by your computer: [1]

ModuleDescription
intel_pstateThis driver implements a scaling driver with an internal governor for Intel Core (Sandy Bridge and newer) processors.
acpi_cpufreqCPUFreq driver which utilizes the ACPI Processor Performance States. This driver also supports the Intel Enhanced SpeedStep (previously supported by the deprecated speedstep-centrino module).
speedstep-libCPUFreq driver for Intel SpeedStep-enabled processors (mostly Atoms and older Pentiums)
powernow-k8CPUFreq driver for K8/K10 Athlon 64/Opteron/Phenom processors. Since Linux 3.7 ‘acpi-cpufreq’ will automatically be used for more modern AMD CPUs.
pcc-cpufreqThis driver supports Processor Clocking Control interface by Hewlett-Packard and Microsoft Corporation which is useful on some ProLiant servers.
p4-clockmodCPUFreq driver for Intel Pentium 4/Xeon/Celeron processors which lowers the CPU temperature by skipping clocks. (You probably want to use a SpeedStep driver instead.)

You can use the following command to see the list of available modules: [1]

ls /usr/lib/modules/$(uname -r)/kernel/drivers/cpufreq/

Further, the following command shows which of these modules is currently loaded by the Linux kernel: [1]

cpupower frequency-info

Although you can use the “cpupower” command to see the activated module in your Linux, you need to install this command on your computer. E.g., you can install the “cpupower” command in Fedora-based Linux by using “sudo dnf install kernel-tools,” and in Debian-based Linux by using “sudo apt install linux-cpupower.”

Here, we focus on two of these modules — “intel_pstate” for Intel CPUs and “acpi-cpufreq” for AMD CPUs. In these cases, you can check two paths to figure out which driver you are using. If the following directory exists, you are using the “intel_pstate” for managing the CPU:

ls /sys/devices/system/cpu/intel_pstate

In addition, you can check following path for the “acpi_cpufreq” module:

ls /sys/devices/system/cpu/cpufreq

Setup CPU boosting

Linux can make a trade-off between performance and power saving by using dynamic CPU frequency management. You can use lower frequency to save power and quietness in a portable computer or utilize boosted frequency to improve your computer’s performance during a heavy workload. Dynamic frequency is an automatic procedure. However, you can determine the max-boost frequency that a kernel is allowed to use.

To turn on CPU boosting in the “intel_pstate,” you can use the following command:

sudo -i
echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo

and for turning it off, you can use follows:

sudo -i
echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo

To see the current status, using following command:

cat /sys/devices/system/cpu/intel_pstate/no_turbo

Note that you need to be the root user (by using “sudo -i) to change the boosting status, manually.

In the “acpi_cpufreq,” you can use the following command to turn the CPU boosting on:

sudo -i
echo 1 > /sys/devices/system/cpu/cpufreq/boost

and the following for turning it off:

sudo -i
echo 0 > /sys/devices/system/cpu/cpufreq/boost

Further, the current status can be seen by using:

cat /sys/devices/system/cpu/cpufreq/boost

Note that turning off/on the CPU boosting is completely different in the “intel_pstate,” “acpi_cpufreq.” You have the “no_turbo” file that makes a DISABLE boosting process for the Intel CPU if you enable it by writing a “1” in this file. By contrast, you have the “boost” file — which can ENABLE the boosting process in the “acpi_cpufreq” module if “1” is written in the file.

CPU boosting as a service

Creating a “systemd” service is a convenient way to manage CPU boosting. It can enable or disable the CPU boosting during the boot process. Here, we create a service to disable CPU boosting when the service is started and turn the boosting on when the service is stopped. You can create this service for the “intel_pstate” by using the following command:

sudo cat << EOF | sudo tee \
/etc/systemd/system/disable-cpu-boost.service
[Unit]
Description=Disable CPU Boost for acpi
 
[Service]
ExecStart=/bin/sh -c "/usr/bin/echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo"
ExecStop=/bin/sh -c "/usr/bin/echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo"
RemainAfterExit=yes
 
[Install]
WantedBy=sysinit.target
EOF

and for the “acpi_cpufreq” by using the following command:

sudo cat << EOF | sudo tee \
/etc/systemd/system/disable-cpu-boost.service
[Unit]
Description=Disable CPU Boost for acpi
 
[Service]
ExecStart=/bin/sh -c "/usr/bin/echo 0 > /sys/devices/system/cpu/cpufreq/boost"
ExecStop=/bin/sh -c "/usr/bin/echo 1 > /sys/devices/system/cpu/cpufreq/boost"
RemainAfterExit=yes
 
[Install]
WantedBy=sysinit.target
EOF

Note that you must copy/paste all of the lines in the service command at the same time — do not copy/paste them line by line in your Linux terminal.

You can use the following command to enable this service:

sudo systemctl enable disable-cpu-boost.service

You can use the following command to disable this service:

sudo systemctl disable disable-cpu-boost.service

You can use the following command to start this service:

sudo systemctl start disable-cpu-boost.service

You can use the following command to stop this service:

sudo systemctl stop disable-cpu-boost.service

Understanding CPU governors

CPU boosting is not the only option that Linux uses to provide better performance. There are several tools that manage the userspace — including “thermald,” “i7z,” “cpupower,” “cpupower-gui,” “turbostat,” etc. [1].

In this article, we focus on the “cpupower.” We have used it when we try to recognize the activated CPU driver. As mentioned before, you can use the following command to install the “cpupower” in Fedora-based Linux:

sudo dnf install kernel-tools

and this one for Debian-based Linux:

sudo apt install linux-cpupower

The following command uses “cpupower” to report the frequency information:

cpupower frequency-info

The output can be something like this:

As you can see, the information includes the activated driver, the available frequency steps, the available governors, the current activated policy, and the boost state, etc. We have talked about boosting in detail. Here, we want to talk about the CPU frequency governor and its policies. We have different governor policies: [2]

PolicyDescription
cpufreq_performanceThe Performance governor forces the CPU to use the highest possible clock frequency. This frequency will be statically set, and will not change. As such, this particular governor offers no power saving benefit. It is only suitable for hours of a heavy workload, and even then only during times wherein the CPU is rarely (or never) idle [2].
cpufreq_powersaveBy contrast, the Powersave governor forces the CPU to use the lowest possible clock frequency. This frequency will be statically set, and will not change. As such, this particular governor offers maximum power savings, but at the cost of the lowest CPU performance. [2].
cpufreq_ondemandThe On-demand governor is a dynamic governor that allows the CPU to achieve maximum clock frequency when the system load is high, and also minimum clock frequency when the system is idle. While this allows the system to adjust power consumption accordingly with respect to system load, it does so at the expense of latency between frequency switching. As such, latency can offset any performance/power-saving benefits offered by the On-demand governor if the system switches between idle and heavy workloads too often [2].
cpufreq_conservativeLike the On-demand governor, the Conservative governor also adjusts the clock frequency according to usage. However, while the On-demand governor does so in a more aggressive manner (that is from maximum to minimum and back), the Conservative governor switches between frequencies more gradually. This means that the Conservative governor will adjust to a clock frequency that it deems fit for the load, rather than simply choosing between maximum and minimum. While this can possibly provide significant savings in power consumption, it does so at an ever greater latency than the On-demand governor [2].
cpufreq_userspaceThe Userspace governor allows userspace programs (or any process running as root) to set the frequency. Of all the governors, Userspace is the most customizable; and depending on how it is configured, it can offer the best balance between performance and consumption for your system [2].

You can use the following command to change the activated CPU governor policy to the “cpufreq_performance:

sudo cpupower frequency-set -g performance

and to the “cpufreq_ondemand:”

sudo cpupower frequency-set -g ondemand

and to the “cpufreq_conservative:

sudo cpupower frequency-set -g conservative

and to the “cpufreq_userspace:

sudo cpupower frequency-set -g userspace
sudo cpupower --cpu all frequency-set --freq <YOUR_FREQ>MHz

Here, you have to replace <YOUR_FREQ> with a frequency. For instance, you can set it to 2200MHz:

sudo cpupower frequency-set -g userspace
sudo cpupower --cpu all frequency-set --freq 2200MHz

Note that the frequency must be listed in the “available frequency steps” when you run the “cpupower frequency-info” command.

One of the most interesting things for “cpupower” is that we can use different policies for the distinctive cores. You can use the “–cpu <cpulist>” keyword to specify that the following policy will be applied to which cores of CPU. The <cpulist> can be like the following examples:

InputEquivalent to
allall cores
0-30,1,2,3
0-7:20,2,4,6
1,3,5-71,3,5,6,7
0-3:2,8-15:40,2,8,12

As an example, you can use following commands to set the first four cores with performance policy and the second four cores as power save policy:

sudo cpupower --cpu 0-3 frequency-set -g performance
sudo cpupower --cpu 4-7 frequency-set -g powersave

Another useful option for the “cpupower” is the idle information. You can use the following command to retrieve CPU idle kernel information:

sudo cpupower idle-info

Further, you can monitor the current status of each core by using the following command:

sudo cpupower monitor

Using “cpufreq” extension in GNOME

Cpufreq” is an extension for the GNOME desktop that can make an easy process to monitor and manage CPU governor. You can install this extension via this link: https://extensions.gnome.org/extension/1082/cpufreq/

The source code for this extension is also available via the following link:

https://github.com/konkor/cpufreq

Reference

[1] https://wiki.archlinux.org/index.php/CPU_frequency_scaling

[2] https://docs.fedoraproject.org/en-US/Fedora/20/html/Power_Management_Guide/cpufreq_governors.html#governor_types

Tagged : / / /

Leave a Reply

Your email address will not be published. Required fields are marked *