Friday, January 26, 2007

Debian HOW-TO : CPU power management

Last revision : February 5th, 2007

CPU frequency management is one of the keys to power preservation.

The Linux kernel now provides all the necessary tools to properly manage CPU frequency : no need to use a daemon (like cpufreqd or powernowd) to take care of your CPU.

Of course the benefits of such power management are obvious for a laptop, but most desktop users should also consider this.

In this tutorial, I use sudo to get root privileges.


  • Prerequisites


Debian Etch (and Sid) should automatically configure CPU frequency management on most processors that supports it, so it might very well be already enabled. You can verify if that is the case using this command :

cpufreq-info


and analyze the output regarding the current policy.

If CPU frequency management is off (or the command is not found), then you can go on with this tutorial.

In order to make this work, you need to install the required packages:

sudo apt-get install cpufrequtils sysfsutils


Next, verify your exact CPU model :

cat /proc/cpuinfo | grep "model name"


Which should output something like that :

model name      : Intel(R) Pentium(R) M processor 1.73GHz


Once you know your exact CPU type, the next step is to load the proper modules : the CPU frequency driver and the CPU frequency policy governor.


  • CPU frequency driver


As you may guess the CPU frequency driver will differ depending on your type of CPU. For example, my laptop is equipped with a Pentium M, so I type :

sudo modprobe speedstep_centrino


to load the proper driver.

Some of the other common drivers (or modules) are :

AMD K6 processors : powernow_k6
AMD K7 processors (Athlon, Duron, Sempron 32 bits) : powernow_k7
AMD K8 processors (Athlon 64, Turion 64, Sempron 64, Opteron 64) : powernow_k8

Pentium 4, Celeron D, Pentium D, Celeron M : p4_clockmod
Pentium M, Core Duo, Core 2 Duo : speedstep_centrino

There are of course other CPU frequency drivers. In doubt, you can use the generic driver : acpi_cpufreq


  • CPU policy governor


Once the proper driver is loaded, you need to choose the desired CPU policy governor. This policy governor will manage the actual behavior of your CPU. Here is some policy governors and their module names :

performance, which sets the CPU statically to the highest possible frequency : cpufreq_performance
powersave, which is the opposite, clocks the CPU statically to the lowest frequency : cpufreq_powersave
ondemand, which sets the CPU speed dynamically depending on the work load (ideal for desktops) : cpufreq_ondemand
conservative, which also sets the CPU dynamically, but less aggressively then the ondemand governor (ideal for laptops) : cpufreq_conservative

For example, my machine has a Pentium M processor, so I type :

sudo modprobe speedstep_centrino
sudo modprobe cpufreq_ondemand


to load both the CPU frequency driver and the CPU policy governor.


  • CPU configuration


Once the modules are loaded, you need to configure the policy governor. For example, I use the ondemand governor, so :

echo ondemand | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor


will enable it.

You can verify that everything went well with this command :

cpufreq-info


It should output your actual frequency, as well as the governor presently in use.


  • System configuration


If everything is good, then you can make this configuration permanent. First make sure the proper modules are loaded at startup (in /etc/modules).

So in my case :


echo speedstep_centrino | sudo tee -a /etc/modules
echo cpufreq_ondemand | sudo tee -a /etc/modules


Finally, ensure that the CPU uses your policy governor of choice by default. Simply edit the file /etc/sysfs.conf with a line like this one :

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


That's it !

55 comments:

virens said...

Thanks for a great post, very informative!

Anonymous said...

Thats an informative article. Thanks.
One appendix: The CeleronM works with the p4_clockmod - driver

prasinos said...

Excellent article. Thank you.

Hugues said...

Anonymous,

You are right about the Celeron M : it doesn't support speedstep, so it uses the p4_clockmod driver. The downside is that this driver (the p4 one) has a higher latency then speedstep one : the transition from one speed to another is a little less smooth.

Thanks to everyone for the feedback,

Hugues

Anonymous said...

The name of the module for AMD K8 processor family is powernow_8k.ko (leave out the "d") ...

Anonymous said...

Arg, wrong again: The name is powernow_k8.ko ...

Hugues said...

You are completely right. The occasional brain cramp I guess, as I knew it. Thanks for the correction.

Hugues

Billy McCann said...

Great blog, hugues.

I use a Pentium D 935+, a dual-core.

I have to add a

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

line to /etc/sysfs.conf to get "ondemand" to apply to the second processor.

Thanks for your blog! I'm pretty new to Linux and i find your instructions informative and easy to use.

Billy McCann said...

Great blog, hugues.

I use a Pentium D 935+, a dual-core.

I have to add a

/devices/system/cpu/cpu1/cpufreq/scaling_governor = performance

line to /etc/sysfs.conf to get "ondemand" to apply to the second processor.

Thanks for your blog! I'm pretty new to Linux and i find your instructions informative and easy to use.

Billy McCann said...

Sorry for the double post. 8/

Anonymous said...

When I type modprobe powernow_k7
I get this error:
FATAL: Error inserting powernow_k7 (/lib/modules/2.6.18-3-k7/kernel/arch/i386/kernel/cpu/cpufreq/powernow-k7.ko): No such device

What is a problem?

Hugues said...

Hi Anonymous,

Unfortunately the powernow_k7 module only supports AMD K7 mobile processors. So even if your CPU is a AMD K7, it has to be a mobile one. Perhaps that is not the case with your CPU ?

Thanks for your comment,

Hugues

Anonymous said...

Hello there,


I have a serious problem with my Sony Vaio.
When the AC adapter is plugged in, the system is slower then when the AC adapter is unplugged.

Really strange, please can anyone enlighten me with this issue

Anonymous said...

Great advice. Read it, done it, works!

-Stefan

Anonymous said...

you mention pentium 4
does this also apply to a piv northwood 1.6 ghz?
it would be strange ... no ? that a k6 can do that magic while a cpu from a few generations later can't

Hugues said...

My understanding is that it does apply to a P4 Northwood. Either way, it is perfectly safe to try it. If your CPU doesn't support it, the module p4-clockmod will simply refuse to load itself.

Thank you for your comment,

Hugues

Anonymous said...

At least in 2.6.20.4, the centrino driver is now deprecated by the ACPI driver (which needs ACPI support and ACPI "Processor" support turned on before it will appear)

Also, debian etch gives you one more step: the /etc/default/cpufrequtils allows you to configure which governor (as well as max and min speeds to choose) to set up when you boot the computer (you'll need to make sure the drivers are loaded, of course).

Hugues said...

The centrino driver is not depreciated in any way.

The latest kernel I tried, 2.6.21-rc7, still has the speedstep-centrino driver (2.6.20.x had it too).

Of course, acpi-cpufreq is present in both kernels mentioned as well.

Like you said, you can use /etc/default/cpufrequtils file to specify the desired governor. There are many ways to load the desired CPU frequency driver and governor at boot.

Thanks for reading, and thank you for your comment,

Hugues

Aku said...

Thanks mate, Great guide and very easy to understand

Anonymous said...

Great post! What I really want to know is why this isn't enabled by default on all systems?

Jobezone said...

Thanks a lot for this Howto! No need for daemons, horray!

For extra coolness points, add the Processor Scaling Monitor applet to your Gnome Panel. Then do:

sudo dpkg-reconfigure gnome-applets

and answer yes. You'll now be able to change frequencies and behaviour of the CPU using the little applet.

Huygens said...

Strangely enough, I have an AMD Sempron with 64bit and Cool'n'Quiet capabilities, so it should be compatible with the powernow_k8 module (as far as other web sites are stating).
However, dmesg reports
powernow-k8: Power state transitions not supported
So I guess you must be right when you say that it supports only AMD mobile processor...
Do you know any ways to activate the Cool'n'Quiet feature of the AMDs?

Hugues said...

Hi Huygens,

Most information regarding Cool'n'Quiet in Linux point to using the powernowd-k8 as stated here. As stated in the Linux kernel documentation though, official support is for mobile K8 CPUs.

modprobe processor
modprobe powernowd-k8


should be all you need to do (the module processor normally loads itself automatically, but you can make sure it does).

Good luck with that,

Hugues

fher98 said...

Geez man... before reading this article my laptop was buzzing away like a jet all the time,.. but know I can barely notice its on!!! two thumbs up for you dude.!!!

If i can only find something to help me runnin opengl accel with my ATI to play Warcraft on my laptop,.. youllbe my new hero blink blink..


thanks

Hugues said...

Hi fher98,

Thanks for your comment.

About your ATI card, did you look at my tutorial on the subject (which can be useful if you run Debian)?

Hugues

fher98 said...

Hugues:

Ill be sure to check the ATI article thanks.

Right now im having a strange result with the CPU power management, im running lenny on a toshiba 3.2 P4 HT, everthing works nicely when im working(quiet fans,.. cool keyboard) but when I leave the machine for like 15-20 minutes and when I get back... the desktop is froze. Let me explain, i can see what i was doing, and resize the window but everything behind it is gray,.. and i dont have access to the panels... very strange.Also if i ctrl+alt+f1 y get into the console like 30 secs later, I type root in the login and thats it... no asking for password nothing.

Im kinda starting to believe that the cpu goes sooo low or the HD just turns off that everything goes reeeal slow. This also happens when i disconnect from power, any idea... sorry to bother you ;-)

Hugues said...

Hi fher98,

If you believe that the low CPU frequency is the problem, then you can set a minimum frequency, and see if it helps.

Verify your CPU available frequencies:

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies

Which should output various numbers, representing the frequencies supported by your CPU (in Mhz).

Just choose one that you find the minimum usable, and write it in the file /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq (using /etc/sysfs.conf or /etc/default/cpufrequtils).

Good luck with that. And thanks for your comments,

Hugues

Nanne said...

Thanks for this helpfull howto! set it up in no-time :)

for some reason i couldn't get the powersafe mod to work, but conservative dumpt my speed to the lowest emediatly, so that looks conservative enough..

(btw, the error message was:
FATAL: Module cpufreq_powersafe not found. )

Anonymous said...

Thanks for tutorial... One question: I got laptop with Celeron M420, when I try to do modprobe p4_clockmod I receive :

FATAL: Error inserting p4_clockmod (/lib/modules/2.6.18-3-k7/kernel/arch/i386/kernel/cpu/cpufreq/p4_clockmod.ko): No such device

Any ideas why? The same effect with acpi_cpufreq module...

Hugues said...

Hi Nanne,

Make sure you type it right, as the name of the governor is powersave (and not "powersafe").

Thanks for your comment,

Hugues

Hugues said...

Anonymous,

My guess is that you are using the wrong kernel, as the 2.6.18-3-k7 one you are mentioning is tailored for AMD K7 CPUs. So obviously it won't allow you to load modules specific to Intel processors.

You can verify the exact kernel you are using with this command:

uname -a

I suggest you install a more appropriate kernel for your CPU:

apt-get install linux-image-686

Thanks for reading,

Hugues

Nanne said...

could i be that stupid?
.....

i guess i could :).
Ill try that later on, thanks for the reply :D

Anonymous said...

Very very thanks! It is just what I've been searching for for about two weeks and almost I was preparing for compiling a customized kernel thinking that this feature was not prebuild on the stock kernel!

I followed all the steps and now my new Athlon64 dual core runs just cool.

I want to comment also that the Ubuntu 7.04 both 64 and 32 bit already come with the Powernow! feature preactivated.

Regarding some people's complaints about error messages when attempting to load the driver module I suggest please look at your computer's BIOS since ALSO the AMD's "Cool 'n' quite" feature must be enabled on the BIOS.

Also remember to have an updated BIOS version.

In my case I have a desktop Athlon64 X2 6000+ on an Asrock ALiveNF6G-DVI motherboard with an AMI BIOS v.2.10 and just works wanderfull.

Thank you again.

--Rafael--

Hugues said...

Hi Rafael,

Thanks a lot for your support.

Hugues

orbin said...

Thanks for your guide.

I can't seem to get the desired governor to stick at boot however. I have added the required ... = conservative line to /etc/sysfs.conf but it still gets set to ondemand each startup.

Kernel is 2.6.21-2-k7. CPU is an AMD Sempron 2800+.

Appreciate any advice.

Hugues said...

Hello Orbin,

There are many ways to specify the desired governor to use.

Besides using /etc/sysfs.conf, the other possibility is to use /etc/default/cpufrequtils (once you have installed the package cpufrequtils).

Good luck,

Hugues

Anonymous said...

i'm having the same problem as the anonymous with the 420

I have a celeron 520, and get FATAL: Error inserting p4_clockmod (/lib/modules/2.6.20-16-generic/kernel/arch/i386/kernel/cpu/cpufreq/p4-clockmod.ko): No such device

i guess the newer celerons don't support scaling?

Hugues said...

Hello,

Did you try the acpi_cpufreq module?

Thanks for reading,

Hugues

James said...

I had been unable to get power saving working properly on my Toshiba lappy for two years. This tutorial did it. Thank you!

Robert said...

Thanks a lot for this one ... works like a charm on my AMD Athlon(tm) X2 Dual Core Processor BE-2400 :)

Anonymous said...

THANKS A LOT !!!!!!!!!!!!!!!!!!!!!!!!

Anonymous said...

tried it on an Athlon 64/4200 x2 - Biostar GeForce 6100 motherboard.

Looked great with the CPU running in low power mode and running up to full power when needed.

Right up until it crashed, turning the video into a blank with short lines across it and locking up the keyboard so hard that even SysRq S - U - B wouldn't reboot the system, I had to unplug it and power it back up to get it to reboot.

There may be a conflict with the nvidia video driver, but I don't have any more time to deal with this. Since I have suspend working correctly, the power savings from cool & quiet are probably fairly marginal anyway.

The problem is not with suspend, it wasn't enabled for the second crash.

Installed powernowd by mistake before finding out where the powernow-k8 driver is in the kernel, but I don't see how this would have triggered this sort of crash.

I removed the recommended programs, commented the modules out of config files, and in general, undid as much as I could of the instructions I followed to get it working.

powernow-k8 is STILL loading. But hopefully, that's all it's doing.

michel said...

Easy to follow and worked for both: amd64 x2 and my PIII laptop :D

Thank You!

Anonymous said...

thanks a lot!
Works perfect with my AMD Athlon X2 4850e

Anonymous said...

I'd like to have this feature on my desktop. Running ubuntu 8.04. So far:

gerard@desktop:~$ cpufreq-info

analyzing CPU 0:
no or unknown cpufreq driver is active on this CPU
analyzing CPU 1:
no or unknown cpufreq driver is active on this CPU
gerard@desktop:~$ cat /proc/cpuinfo | grep "model name"
model name : AMD Athlon(tm) 64 X2 Dual Core Processor 4000+

Then: FATAL: Error inserting powernow_k8 (/lib/modules/2.6.24-21-generic/kernel/arch/x86/kernel/cpu/cpufreq/powernow-k8.ko): No such device

Any ideas?

nadavkav said...

great help , thanks :-)

btw,
/etc/default/cpufrequtils was not initially on my (newly installed) debian sid 64bit system.
so i created it and place a one line command to enable the governor i wanted to use.

GOVERNOR="performance"

Nicolas said...

Your trick doesn't work on my Debian 5.0...

Anonymous said...

Worked fine on my desktop amd64.Enjoying the peace.Before the cpu used to make lots of noise and produce lots and lots of heat.After having seen the power of cool-n-quiet in windows i wanted it in linux.Now i finally have it,all thanks to u.......................

Anonymous said...

Great post; thanks a lot! I'd been told that desktop P4s didn't support scaling, so I was resigned to fan noise (and dirty looks) till I tried this!

On my Lenny install I had to apt-get cpufrequtils to get the cpufreq-info command; it also adds some /etc/rc.d scripts, which might be why the previous poster didn't have luck with /etc/defaults/cpufrequtils?

saf said...

I have tried various tutorials to get this working in ubuntu with my pentium D, and was told my cpu doesnt support cpufreq, thanks poster I can really shut those idiots up now and brag how everything i can do in windows (RMclock)I can do in linux

Anonymous said...

I tried it with a Phenom II 940 using powernow_k8 and it worked.

Elder G said...
This comment has been removed by the author.
Anonymous said...

Hello,


excellent, simply excellent : Simple, efficient, worked like a charm from the first try on my k8, thank you very much !

Hayri Kar said...

To problem ;
/etc/sysfs.conf
or
/etc/default/cpufrequtils

my lenny;
uname -a
Linux XXX 2.6.26-2-486 #1 Tue Mar 9 17:10:34 UTC 2010 i686 GNU/Linux

cat /etc/init.d/cpufrequtils
or
cat /etc/init.d/cpufrequtils | grep GOVERNOR

See GOVERNOR="ondemand" or whatelse..
Change it to what you want.

/etc/init.d/cpufrequtils restart

Brad Fallon said...

Should I leave my CPU (Core Duo) at full power or should I keep it in adaptive mode? Any Negative effects?