In Linux, managing CPU frequency is essential for balancing performance and power consumption. CPU frequency scaling allows the system to dynamically adjust the processor's clock speed based on the current workload. This capability is particularly useful for conserving energy in laptops, reducing heat output in servers, and optimizing performance for various tasks.

To set up CPU frequency scaling, Linux provides tools that allow users to control how the CPU adjusts its speed. Two common approaches are using cpufreq utilities and the sysfs interface. Each method offers different levels of control and flexibility, making it suitable for different use cases and user preferences.

This article provides detailed instructions on how to configure CPU frequency scaling on a Linux system using both cpufreq utilities and the sysfs interface. Each method includes steps to verify the configuration through real-time testing, ensuring that the CPU scales its frequency as expected.

Steps to set up CPU frequency scaling using cpufreq:

Cpufreq is a utility that allows users to adjust and monitor the CPU frequency on Linux systems. It supports different governors that define how the CPU should scale its frequency. The available governors include performance, powersave, ondemand, conservative, and schedutil. Each governor has its specific behavior, from maximizing performance to minimizing power consumption.

Setting up CPU frequency scaling with cpufreq involves selecting the appropriate governor based on your needs. The following steps will guide you through the installation and configuration of cpufreq to manage CPU frequency scaling on your Linux system.

  1. Install the cpufrequtils package from your distribution's package manager.
    $ sudo apt update && sudo apt install cpufrequtils  //Ubuntu and derivatives//
    $ sudo zypper refresh && sudo zypper install cpufrequtils  //openSUSE//
    $ sudo dnf install cpufrequtils  //Fedora and RHEL//
  2. Check the current CPU frequency and available governors.
    $ cpufreq-info

    The cpufreq-info command displays the current CPU frequency, the available governors, and the active governor for each CPU core.

  3. Set the desired governor for your CPU.
    $ sudo cpufreq-set -g ondemand

    Replace ondemand with the governor of your choice, such as performance, powersave, or conservative.

  4. Verify the active governor and the CPU frequency.
    $ cpufreq-info

    The output should reflect the newly set governor and the corresponding frequency behavior for each CPU core.

  5. Test the CPU frequency scaling by generating a workload.
    $ yes > /dev/null &

    This command creates a high CPU load, allowing you to observe how the frequency scales in response to the increased demand.

  6. Monitor the CPU frequency during the test.
    $ watch -n 1 "cpufreq-info | grep 'current CPU frequency'"

    This command displays the current CPU frequency in real-time, updating every second. It helps verify that the frequency scales according to the workload.

  7. Stop the test process once you have verified the frequency scaling.
    $ kill PID

Steps to set up CPU frequency scaling using sysfs:

The sysfs interface provides a more manual approach to controlling CPU frequency scaling in Linux. This method involves directly interacting with the sysfs filesystem, which exposes kernel parameters to user space. By writing to specific files within sysfs, users can set the CPU governor, adjust frequency limits, and control other aspects of CPU frequency scaling.

This approach offers greater flexibility and is particularly useful for users who want to fine-tune their system's performance. The following steps guide you through the process of setting up CPU frequency scaling using the sysfs interface.

  1. Ensure the cpufreq driver is loaded and the sysfs interface is accessible.
    $ ls /sys/devices/system/cpu/cpu*/cpufreq/

    If the output lists available files, such as scaling_governor and scaling_max_freq, the interface is available.

  2. Set the desired governor for each CPU core.
    $ echo "ondemand" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

    Replace ondemand with the governor you prefer. This command applies the governor to all CPU cores.

  3. Set the minimum and maximum CPU frequency limits.
    $ echo "1000000" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_min_freq
    $ echo "3000000" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq

    Replace 1000000 and 3000000 with the desired minimum and maximum frequencies in kHz. These values set the frequency range within which the CPU can scale.

  4. Verify the settings by checking the active governor and frequency limits.
    $ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    $ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_min_freq
    $ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq

    The output should match the settings you configured, indicating that the CPU frequency scaling is correctly set up.

  5. Test the CPU frequency scaling by generating a workload.
    $ yes > /dev/null &

    This command creates a high CPU load, allowing you to observe how the frequency scales in response to the increased demand.

  6. Monitor the CPU frequency during the test.
    $ watch -n 1 "cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq"

    This command displays the current CPU frequency in real-time, updating every second. It helps verify that the frequency scales according to the workload.

  7. Stop the test process once you have verified the frequency scaling.
    $ kill PID
Discuss the article:

Comment anonymously. Login not required.