# 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]

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]

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:

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

Tagged : / / /