1. Overview

Modern processors have the capability of operating in different clock frequencies and voltage configurations. Therefore, it’s possible to change the speed of a processor depending on the load in the system.

There are several CPU (Central Processing Unit) governors that estimate the needed capacity and control the speed of a processor accordingly.

In this tutorial, we’ll discuss how to get the CPU governor of a processor. First, we’ll have a brief introduction to what CPU governors are. Then, we’ll learn about the methods to get the CPU governor.

2. What Is a CPU Governor?

Sometimes, it might be necessary to run programs as fast as possible with the highest performance. A processor can process more instructions when the clock frequency or the voltage is higher. However, it consumes more energy in this case.

Operating a processor at the highest performance for a long time may not be desirable because of power supply capacity or thermal reasons. Therefore, it might be necessary to switch the operation of a processor between different states by changing the clock frequency or voltage configuration. These states are known as performance states, i.e., P-states.

Changing the P-state of a processor depending on the utilization of the system is known as CPU performance scaling or CPU frequency scaling. CPU performance scaling in the Linux kernel is implemented by the CPUFreq subsystem. The CPU governor part of this subsystem estimates the needed CPU capacity and controls the frequency according to a target frequency.

There are several types of CPU governors:

  • performance: It forces the CPU to run at the highest possible clock frequency.
  • powersave: It forces the CPU to run at the lowest possible clock frequency.
  • userspace: It sets the CPU’s frequency to a specific value using the scaling_setspeed parameter.
  • ondemand: It forces the CPU to run at the highest possible clock frequency when the system load is high. However, it forces the CPU to run at the lowest possible clock frequency when the system load is low.
  • conservative: It’s similar to the ondemand CPU governor, but it switches between frequencies more gradually.
  • interactive: This CPU governor sets the CPU speed like ondemand. However, it increases the CPU speed more aggressively depending on the amount of CPU-intensive activities.

In addition to the P-states, there are also C-states, which are designed to optimize power consumption in idle mode.

3. How to Get the CPU Governor

Let’s start by checking the number of logical cores of the host we’re working on:

$ nproc --all
24

The nproc –all command prints the number of available processing units. We have 24 logical cores on our host.

3.1. Using cpupower frequency-info

cpupower is a collection of tools for getting and setting the power-related parameters of a CPU. We can use the cpupower frequency-info command to get the information about CPU performance scaling:

$ cpupower frequency-info
analyzing CPU 0:
  driver: intel_cpufreq
  CPUs which run at the same hardware frequency: 0
  CPUs which need to have their frequency coordinated by software: 0
  maximum transition latency: 20.0 us
  hardware limits: 800 MHz – 1.50 Ghz
  available cpufreq governors: conservative ondemand userspace powersave performance schedutil
  current policy: frequency should be within 1.50 GHz and 1.50 GHz.
                  The governor “performance” may decide which speed to use
                  within this range.
  current CPU frequency: Unable to call hardware
  current CPU frequency: 1.36 GHz (asserted by call to kernel)
  boost state support:
    Supported: no
    Active: no

As is apparent from the first line in the output (analyzing CPU 0:), the command displays the information only about the first core by default. We can use the -c option to specify the cores we’re interested in. For example, we can run the cpupower -c 0,23 frequency-info command to get information about the first and last cores on our host.

The current policy field in the output displays the CPU governor, which is performance in our case. In other words, the first CPU, CPU 0, on our host uses the performance CPU governor.

3.2. Using sysfs

In addition to the CPU governor, cpupower frequency-info displays a bunch of information such as the driver, current CPU frequency, etc. However, we can obtain the CPU governor of a core without additional information using the interface in the sysfs virtual file system, which provides access to the kernel data structures related to the hardware.

Let’s get the CPU governor of the first core, CPU 0:

$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
performance

The first core’s CPU governor is performance, as expected.

There’s a directory for each core on the host in the /sys/devices/system/cpu directory. For example, /sys/devices/system/cpu/cpu0 corresponds to the first core, while /sys/devices/system/cpu/cpu23 corresponds to the last core in our system. The /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor file contains the CPU governor of the first core.

Let’s check the last core’s CPU governor:

$ cat /sys/devices/system/cpu/cpu23/cpufreq/scaling_governor
performance

The last core’s CPU governor is also performance.

To see the effect of changing the CPU governor on the scaling_governor file, let’s change the CPU governor of the last core on our host to powersave using the cpupower frequency-set command:

$ cpupower –c 23 frequency-set –g powersave
Setting cpu: 23

The cpupower frequency-set command modifies the Cpufreq settings of a processor. Its -g option specifies the CPU governor, which is powersave in our example. We specify the core using the -c option.

Let’s check the last core’s CPU governor once more:

$ cat /sys/devices/system/cpu/cpu23/cpufreq/scaling_governor
powersave

The CPU governor type is powersave now, as expected.

4. Conclusion

In this article, we discussed how to get the CPU governor of a processor. First, we learned that there are several P-states of a processor. The CPU governor part of the CPUFreq subsystem controls the frequency based on a target frequency. The target frequency may be fixed for some CPU governors like performance but may be variable for some other CPU governors like ondemand.

Then, we saw that we can use the cpupower frequency-info command to get the CPU governor. However, this command also provides information about the other settings of the CPUFreq subsystem besides the CPU governor.

Finally, we learned that we can use the sysfs file system to get just the CPU governor of a processor. The CPU governor of each CPU is listed in the corresponding scaling_governor file.