CPU frequency scaling in Linux June 22, 2008
Posted by idebian in Debian, GNU/Linux, Hardware.Tags: Frequency scaling
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
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):
- Intel SpeedStep
- AMD Cool’n'Quiet
- VIA LongHaul
- 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:
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
Excellent explanation and howto. I see this blog is going to be Debian related only.
I wish to keep the blog as general as possible but anyway there will be a strong Debian influence
idebian,
Excellent article on CPU Frequency. I really enjoyed reading it.
Ramesh
The Geek Stuff
Thanks, this is the first article I have seem about the user governor. I was wondering what that was for!
Thx for article
Useful post. good luck.
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
Great Blogging!!
Keep Your Good Work Going!!
Processor
nice post! thanks!
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
Thank you so much for this article. Just got my T400 but couldn’t get my cpu scaling to work, and then I saw this and it works beautifully!
Keep it up
[...] which governs how the CPU is utilized from a power/performance perspective (see CPU Frequency Scaling in Linux for more). [...]
ondemand is on the governors available, however after doing
echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
the governor returns to ‘performance’
there’s no ‘cpufreq_ondemand’ as result of lsmod, I think that could be the problem, however I can’t find a way to load that module. Thanks!
Can we change cpu frequency independent of voltage. Say scaling the frequency but the voltage remain at fixed value.
System may fail to operate but i wish to know if it is possible by some hack in cpufreq?
Wow!!That’s what I’ve benn looking for for a long time~!!
Thanks for sharing this post~~.
[...] I was searching for the technique to better monitor CPU usage, I stumbled across a blog post about CPU frequency scaling. I read it and learned that some processors were able to lower its frequency to save power and [...]
[...] [...]