| 1 |
AUTHOR: Ken Moffat <ken@kenmoffat.uklinux.net> |
|---|
| 2 |
DATE: 2005-06-15 |
|---|
| 3 |
LICENSE: MIT License |
|---|
| 4 |
SYNOPSIS: cpu frequency scaling using the kernel or powernowd. |
|---|
| 5 |
DESCRIPTION: With processors that support it (e.g. recent athlon64, |
|---|
| 6 |
many laptop processors), you can reduce the processor frequency, to |
|---|
| 7 |
reduce wasted energy or conserve battery power. |
|---|
| 8 |
|
|---|
| 9 |
PREREQUISITES: 2.6.9 or later kernel, the bootscripts target LFS-6.1 |
|---|
| 10 |
|
|---|
| 11 |
HINT: |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
Overview |
|---|
| 15 |
________ |
|---|
| 16 |
|
|---|
| 17 |
Frequency scaling is an important part of increasing the battery life |
|---|
| 18 |
of notebooks, but it also has a place in reducing power consumption, |
|---|
| 19 |
and thus waste heat, on desktops and servers. Most CPUs that support |
|---|
| 20 |
it will have at least two valid frequency/voltage settings, e.g. my |
|---|
| 21 |
athlon64 Winchester 3200 (2.0 GHz) can run at approx 1.0 GHz, 1.8 GHz, |
|---|
| 22 |
and 2.0 GHz. Give the ondemand governor a heavy load and the speed |
|---|
| 23 |
quickly ramps up. After the load has fallen back, the speed will |
|---|
| 24 |
first fall to 1.8 GHz and then some seconds later to 1.0 GHz. |
|---|
| 25 |
|
|---|
| 26 |
The kernel began by offering 'powersave' (slow) and 'performance' |
|---|
| 27 |
(fast) drivers, with a 'userspace' alternative to allow a daemon to |
|---|
| 28 |
dynamically change the frequency. As of 2.6.9, an 'ondemand' driver |
|---|
| 29 |
was made available. Other demand-based governors, such as |
|---|
| 30 |
'conservative', have been introduced in later kernels. |
|---|
| 31 |
|
|---|
| 32 |
Meanwhile, the powernowd daemon was developed, see |
|---|
| 33 |
http://www.deater.net/john/powernowd.html - this will dynamically |
|---|
| 34 |
change the frequency as the load goes up and down. |
|---|
| 35 |
|
|---|
| 36 |
It would seem that the 'ondemand' driver makes powernowd redundant, |
|---|
| 37 |
but some CPUs (like the ppc 7447) apparently have a very high |
|---|
| 38 |
transition latency which causes the ondemand governor to decline to |
|---|
| 39 |
manage them - on my 7447A the powersave and performance drivers |
|---|
| 40 |
('governors') work, but trying to install 'ondemand' fails. Equally, |
|---|
| 41 |
on a laptop powernowd will probably be a little quicker at dropping |
|---|
| 42 |
the speed, and can therefore give a little more battery life. |
|---|
| 43 |
|
|---|
| 44 |
On x86 and x86_64 it appears that your bios has to support frequency |
|---|
| 45 |
scaling (typically labelled Cool'n'Quiet on boards with AMD CPUs) - |
|---|
| 46 |
after upgrading to a new mobo/cpu, I lost the ability to use both the |
|---|
| 47 |
'ondemand' governor and powernowd until I configured the bios properly. |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
Kernel Configuration |
|---|
| 51 |
____________________ |
|---|
| 52 |
|
|---|
| 53 |
On the Processor menu, select CPU Frequency scaling |
|---|
| 54 |
|
|---|
| 55 |
Set the Default CPUFreq governor to userspace (the default is often |
|---|
| 56 |
'performance') |
|---|
| 57 |
|
|---|
| 58 |
Select the governors you wish to use, typically 'performance', |
|---|
| 59 |
'powersave', and try 'ondemand'. Feel free to select any other |
|---|
| 60 |
available governors. I assume most people will want to build these |
|---|
| 61 |
governors in, rather than compiling them as modules. |
|---|
| 62 |
|
|---|
| 63 |
Select the correct cpu-freq hardware driver (on a mac there is nothing |
|---|
| 64 |
further to select, on x86, choose from POWERNOW_K7, POWERNOW_K8, |
|---|
| 65 |
SPEEDSTEP_CENTRINO, and similar options). |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
Testing |
|---|
| 69 |
_______ |
|---|
| 70 |
|
|---|
| 71 |
After you've booted the new kernel, you can test which of the kernel |
|---|
| 72 |
governors work. As root, echo the governor name to |
|---|
| 73 |
/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor - presumably, |
|---|
| 74 |
people with SMP boxes need to try this for each CPU. Then test the |
|---|
| 75 |
status of the command, e.g. with 'echo $?' - if the governor loaded, |
|---|
| 76 |
the status will be 0 and you can use that governor, otherwise the |
|---|
| 77 |
status will be 1. |
|---|
| 78 |
|
|---|
| 79 |
You can view the current CPU frequency in /proc/cpuinfo. To test the |
|---|
| 80 |
ondemand or other variable-speed governors, run something "expensive" |
|---|
| 81 |
(e.g. untar the gcc .bz2, or do a big compile) and repeatedly check |
|---|
| 82 |
the spead in the cpuinfo file. You should find that it ramps up |
|---|
| 83 |
shortly after the load starts, and falls back a few seconds after the |
|---|
| 84 |
load drops. [ BUT, if you are in an xterm on a kernel > 2.6.12-rc5, |
|---|
| 85 |
see the ignore_nice line in my bootscript below. ] |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
Bootscript for kernel cpufreq |
|---|
| 89 |
_____________________________ |
|---|
| 90 |
|
|---|
| 91 |
If one or more of the demand-based governors works for you, you're |
|---|
| 92 |
almost done, you just need to ensure the appropriate governor is |
|---|
| 93 |
loaded after you boot. For my desktop box (athlon64) I use the |
|---|
| 94 |
following : |
|---|
| 95 |
|
|---|
| 96 |
cat >>/etc/rc.d/init.d/cpufreq << EOF |
|---|
| 97 |
#!/bin/sh |
|---|
| 98 |
|
|---|
| 99 |
. /etc/sysconfig/rc |
|---|
| 100 |
. ${rc_functions} |
|---|
| 101 |
|
|---|
| 102 |
case "${1}" in |
|---|
| 103 |
start|demand) |
|---|
| 104 |
boot_mesg "Enabling ondemand cpu frequency" |
|---|
| 105 |
echo ondemand \ |
|---|
| 106 |
>/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor |
|---|
| 107 |
boot_mesg "ignoring niceness for ondemand" |
|---|
| 108 |
echo 1 \ |
|---|
| 109 |
>/sys/boot/devices/system/cpu0/cpufreq/ondemand/ignore_nice |
|---|
| 110 |
;; |
|---|
| 111 |
powersave) |
|---|
| 112 |
boot_mesg "Enabling powersave cpu frequency" |
|---|
| 113 |
echo powersave \ |
|---|
| 114 |
>/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor |
|---|
| 115 |
;; |
|---|
| 116 |
performance) |
|---|
| 117 |
boot_mesg "Enabling performance cpu frequency" |
|---|
| 118 |
echo performance \ |
|---|
| 119 |
>/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor |
|---|
| 120 |
;; |
|---|
| 121 |
*) |
|---|
| 122 |
echo "Usage: ${0} {start|demand|powersave|performance}" |
|---|
| 123 |
exit 1 |
|---|
| 124 |
;; |
|---|
| 125 |
esac |
|---|
| 126 |
EOF |
|---|
| 127 |
|
|---|
| 128 |
chmod 754 /etc/rc.d/init.d/cpufreq |
|---|
| 129 |
|
|---|
| 130 |
ln -s ../init.d/cpufreq /etc/rc.d/rcsysinit.d/S45cpufreq |
|---|
| 131 |
|
|---|
| 132 |
Note that this allows me to set powersave or performance if I ever wish |
|---|
| 133 |
to, and that I just install this in rcsysinit.d so there is no 'stop' |
|---|
| 134 |
option. |
|---|
| 135 |
|
|---|
| 136 |
The 'ignore_nice' ensures the old behaviour of ondemand for kernels |
|---|
| 137 |
newer than 2.6.12-rc5 : without this, anything I start in an xterm has a |
|---|
| 138 |
niceness of 10 and the kernel doesn't allow it to influence the cpu |
|---|
| 139 |
frequency. |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
Powernowd |
|---|
| 143 |
_________ |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
This is based on 0.96. Untar it, you'll notice the Makefile is |
|---|
| 147 |
described as -very- simple. Unfortunately, it installs into /usr/sbin |
|---|
| 148 |
- on a laptop, you probably want to bring it up as part of the boot |
|---|
| 149 |
process, so I think it belongs in /sbin. The easy way to do that is |
|---|
| 150 |
|
|---|
| 151 |
make && install -m 755 powernowd /sbin |
|---|
| 152 |
|
|---|
| 153 |
There is an example powernowd.init in the package, which could be |
|---|
| 154 |
adapted for the bootscript, using variations of "dyn" (dynamic), "low", |
|---|
| 155 |
"high" - I go with something a little simple - dynamic frequency |
|---|
| 156 |
control at all times: |
|---|
| 157 |
|
|---|
| 158 |
Bootscript for powernowd |
|---|
| 159 |
________________________ |
|---|
| 160 |
|
|---|
| 161 |
cat >>/etc/rc.d/init.d/powernowd << EOF |
|---|
| 162 |
#!/bin/sh |
|---|
| 163 |
|
|---|
| 164 |
. /etc/sysconfig/rc |
|---|
| 165 |
. ${rc_functions} |
|---|
| 166 |
|
|---|
| 167 |
case "${1}" in |
|---|
| 168 |
start) |
|---|
| 169 |
boot_mesg "Starting powernowd" |
|---|
| 170 |
loadproc /sbin/powernowd |
|---|
| 171 |
;; |
|---|
| 172 |
status) |
|---|
| 173 |
boot_mesg "Starting powernowd" |
|---|
| 174 |
statusproc /sbin/powernowd |
|---|
| 175 |
;; |
|---|
| 176 |
stop) |
|---|
| 177 |
boot_mesg "Starting powernowd" |
|---|
| 178 |
killproc /sbin/powernowd |
|---|
| 179 |
;; |
|---|
| 180 |
*) |
|---|
| 181 |
echo "Usage: ${0} {start|status|stop}" |
|---|
| 182 |
exit 1 |
|---|
| 183 |
;; |
|---|
| 184 |
esac |
|---|
| 185 |
EOF |
|---|
| 186 |
|
|---|
| 187 |
chmod 754 /etc/rc.d/init.d/powernowd |
|---|
| 188 |
|
|---|
| 189 |
ln -s ../init.d/powernowd /etc/rc.d/rcsysinit.d/S45powernowd |
|---|
| 190 |
ln -s ../init.d/powernowd /etc/rc.d/rc0.d/K45powernowd |
|---|
| 191 |
ln -s ../init.d/powernowd /etc/rc.d/rc6.d/K45powernowd |
|---|
| 192 |
|
|---|
| 193 |
ACKNOWLEDGEMENTS: |
|---|
| 194 |
_________________ |
|---|
| 195 |
|
|---|
| 196 |
Barry Shilliday for bringing the scaling_governors to my attention |
|---|
| 197 |
in an article in PC Plus magazine. |
|---|
| 198 |
|
|---|
| 199 |
Eric Piel for explaining how to restore the old behaviour for the |
|---|
| 200 |
ondemand governor. |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
CHANGELOG: |
|---|
| 204 |
__________ |
|---|
| 205 |
|
|---|
| 206 |
2005-07-12 First version. |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
Updated versions of this hint may be found at |
|---|
| 210 |
http://www.kenmoffat.uklinux.net/hints/ |
|---|
| 211 |
|
|---|