jump to navigation

CPU frequency scaling in Linux June 22, 2008

Posted by idebian in Debian, GNU/Linux, Hardware.
Tags:
trackback

The recent CPUs have a feature which let them scale their operating frequency (changing a voltage power supply input) according to the system/user needs.
This way, when the entire processor resources are not needed, the system can greatly reduces the overall power consumption, lowering temperatures and fan speed ;)

This post addresses the frequency scaling setup in Linux: enjoy :D

Introduction

The overall system that support dynamic frequency scaling in Linux is represented below:

Linux cpufreq architecture

The architecture’s elements are (from the HW to the SW levels):

  • A CPU with frequency scaling support (of course):
  • A CPU-specific driver:
    • acpi_cpufreq: all the ACPI aware (Intel Core2 Duo, AthlonX2, …)
    • powernow_k8: all the AMD K8 (Athlon64, Opteron64, …)
    • longhaul: VIA C3, C7, Nano
  • cpufreq module provides a common interface to the various low level, CPU-specific frequency-control technologies and high-level CPU frequency-controlling policies.
  • kernel governors change the CPU frequency based on different criteria such as CPU usage:
    • Performance keeps the CPU at the highest possible frequency
    • Powersave keeps the CPU at the lowest possible frequency
    • Userspace exports the available frequency information to the user level (through the /sys file system) and permits user-space control of the CPU frequency
    • Ondemand scales the CPU frequencies according to the CPU usage (like does the userspace frequency scaling daemons, but in kernel)
    • Conservative acts like the ondemand but increases frequency step by step
  • User space demons adapt the frequency policy to different situations:
    • powernowd: easy and complete
    • cpufreqd: sophisticated but somewhat complicated to setup

Debian implementation

in Debian all of the above kernel options are compiled as modules out of the box. What we are required to do is simply load the appropriate kernel module to get SpeedStep/Cool’n'Quiet functionality.

ACPI CPU module

Let’s start loading the CPU specific driver, which in my case is acpi-cpufreq:

$ sudo modprobe acpi-cpufreq

Now verify with through the sys interface the processor capabilities:

$ more /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
3066000 2933000 2667000 2400000 2133000 1867000 1600000 800000
$ more /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
performance

The performance governor is the default one; we are going to add others soon.

The actual CPU frequency is also reported in the /proc/cpunifo:

$ more /proc/cpuinfo | grep MHz
cpu MHz : 3066.000
cpu MHz : 3066.000

Kernel governor

Since we are going to use the ondemand governor we need to load the corresponding module:

$ sudo modprobe cpufreq_ondemand

The new governor is now listed in the available governors:

$ more /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
ondemand performance

Setting the actual governor is as easy as type (we will automagically configure it later on):

# echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

verify that it went through:

$ more /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
ondemand
$ more /proc/cpuinfo | grep MHz
cpu MHz : 800.000
cpu MHz : 800.000

Congratulations!!! We’ve just set the Scaling Frequency system and governor.
We just need to make such behavior permanent (surviving a reboot).

Alternatives: a userspace governor

According to me kernel governors represent a better solution not only in terms of speed but because they can use other information available in the kernel, like scheduler, interrupt load, etc.

Despite that, someone could opt for a userspace governor: in this case Debian offers an off-the-shelf solution which is powernowd.
Simply install powernowd package and eventually tweak its configuration file in /etc/default/powernowd.

Finalization

In order to automatically load the needed modules we just add them to the /etc/modules file:

# echo "cpu_driver" >> /etc/modules
# echo "governor" >> /etc/modules

Which in my case are:

# echo acpi_cpufreq >> /etc/modules
# echo cpufreq_ondemand >> /etc/modules

The governor set in sysfs can be loaded using the sysfsutils package which is exactly targeted at this aim: it loads at boot-time a sysfs preset stored in the file /etc/sysfs.conf.
Let’s modify the /etc/sysfs.conf to include a line:

devices/system/cpu/cpu0/cpufreq/scaling_governor = ondemand

Reboot and enjoy ;)

Junkies

In case you like those junky stuff like applets and screenlets (I do like them) there are plenty of such graphical fontends to the cpufreq system.
This is for example GNOME CPU Frequency Scaling Monitor applet.

Gnome CPU Frequency Scaling Monitor applet

References

  1. How to make use of Dynamic Frequency Scaling
  2. Enhanced Intel SpeedStep® Technology and Demand-Based Switching on Linux

Comments»

1. E@zyVG - June 23, 2008

Excellent explanation and howto. I see this blog is going to be Debian related only.

2. idebian - June 23, 2008

I wish to keep the blog as general as possible but anyway there will be a strong Debian influence :)

3. Ramesh | The Geek Stuff - June 23, 2008

idebian,

Excellent article on CPU Frequency. I really enjoyed reading it.

Ramesh
The Geek Stuff

4. Paul Siu - June 24, 2008

Thanks, this is the first article I have seem about the user governor. I was wondering what that was for!

5. Cialisst - July 17, 2008

Thx for article

6. Cialis Generico - August 5, 2008

Useful post. good luck.

7. Lonney - December 5, 2008

Hey, thanks for the guide, works on Intel Celeron D processors too, clocked mine from 2.8GHz down to 350MHz. Ideal for that home Linux box that sits idle most of the time!

Also:

/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

still reports: ondemand performance, after the changes, have double checked this.

And missing is /etc/sysfs.conf, I have Debian Etch installed.

But it appears to have worked:

$ more /proc/cpuinfo | grep MHz
cpu MHz : 349.991

8. boblee - June 5, 2009

Great Blogging!!
Keep Your Good Work Going!!

Processor

9. .dhy - June 15, 2009

nice post! thanks!

10. Wawrzek - September 8, 2009

Nice article, but what to do when:

root@berlin:/lib/modules# sudo modprobe acpi-cpufreq

FATAL: Module acpi_cpufreq not found.

root@berlin:/lib/modules# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 9.04
Release: 9.04
Codename: jaunty

Machine – ThinkPad T21