Friday, March 27, 2009

Energy management in Linux

Nota: este post es una continuación de este otro. Para más info, mirar ahí.
Note: this article is a follow-up of this one. Please take a look at it for extended info.
____________________________________________________

UPDATE: Check this post for related info.

I've been concerned about the energy consumption of my Thinkpad X200s for a while now. It's not that electricity is that expensive or I am sooo worried about global warming, it's more about squeezing the most out of the battery while trying not to damage it. My X200s has the 9cell battery which means 13 hours Lenovo-stated battery time. I'm going to use that twice in it's whole life. The point is in maximizing the long-time battery health. Lithium-ion batteries don't like to be deep-discharged too often, so my goal is to (almost) never let the charge drop below 20%. If someone only needs 2-3 hours of battery time and can charge it back to 100% after that, there is nothing to worry about. There is no way to burn 80% of the battery in that time with this laptop. Of course, if someone travels a lot and can't plug the laptop for a whole day, it makes sense to spend as few watts as possible.

Now, about battery life and energy consumption. Lenovo has made some serious effort to optimize the power drain in software and their Vista drivers are as of now unbeatable. In Vista, the minimum I've seen was 5.2W, in Linux 5.9W or 5.8W. Maybe 0.6W doesn't seem too much, but we are talking about more than 10% here! I've discussed this issue here with some folks, I recommend reading it.

First of all, use common sense. Don't expect 13 hours of number crunching, full-brightness video watching, usb-gadget powering or high-speed downloading. Turn down/off everything you don't use. If you are in a dark room, lower the screen brightness. If you are not listening to music, mute the volume. If you are, use headphones or set the volume a tad lower. Kill all the daemons you don't need. You got it, right?

Second point, my main source of info for all this stuff was this website. Follow all the steps you can. Make sure you have a tickless kernel. Turn on laptop-mode to spin down your hard drive when idle (ONLY if you have a SPINNING drive, you'll gain nothing if you have a SSD). Activate processor energy management and set the cpu frequency governor to ondemand or conservative. When you are familiar with all the goals, keep reading.

Now we are going to get our hands dirty. I'll explain this things for Arch Linux, the exact configuration files / paths may be located in a slightly different place in you distribution, use "locate" command or google to find them.

- General powersaving settings, aka laptop mode: Install and then activate laptop-mode editing the file "/etc/laptop-mode/laptop-mode.conf". After that, check all the files in "/etc/laptop-mode/conf.d/*" and activate all the things you can. Except maybe cpufreq, I rather let cpufreqd do that job, it's much simpler. Start laptop-mode with "/etc/rc.d/laptop-mode start" and add it to your daemon list in your rc.conf file. In case you skip something here, I'll probably mention it anyway, but laptop-mode comes in handy for automating it.

- Cpu frequency scaling. I recommend using cpufreqd, it is very very simple. Install it, do a "echo ondemand > /etc/conf.d/cpufreq" and start the daemon as shown above, also adding it to the daemon list.

- Turn off the video outputs. It sounds weird, but with the xorg 1.5 and the old intel 2.4 drivers, the video chipset doesn't detect the outputs right and turns on HDMI. The newer xorg 1.6 and intel driver 2.6 didn't have this problem when I tried them, but check it anyway. You can do it with "xrandr". If you see a star ( * ) on some line, that output is active. Turn it off with "xrandr --output HDMI-2 --off". You should check again on restart.

- Turn down the wifi power. This is a real power hog, it can drain more than 1.5W doing nothing. The control file is "/sys/class/net/wlan0/device/power_level". With "echo 5 > file" you maximize powersaving. Obviously, "cat file" gives you the wifi status. I've noticed some misbehavior lately, if you are not connected and with some network manager daemon running, it keeps going back to level 0 (maximum power usage). I usually fix it with:
# killall wicd-manager
# /etc/rc.d/wicd stop
# killall wpa_supplicant
# ifconfig wlan0 up
# echo 5 > /sys/class/net/wlan0/device/power_level
# ifconfig wlan0 down
# cat /sys/class/net/wlan0/device/power_level

If the last command shows a "INDEX:5", it'll probably stay that way, but checking it some time later would be good. As usual, let a script do that for you. If it is not 5 but 0, repeat the previous 3 steps. If you are conncected to some network, it usually stays at level 5 without any issue after a simple echo.

- Turn off bluetooth if you are not using it. As easy as "echo disable > /proc/acpi/ibm/bluetooth". Sometimes it doesn't want to wake up again, try a "lsusb" after re-enabling it. Again, acpid is a great way to handle it, here is a chunk of my handler:
*1005)
if [ "$(cat /var/local/bluetooth)" = "disabled" ]; then
echo "enable" > /proc/acpi/ibm/bluetooth
echo "enabled" > /var/local/bluetooth
lsusb > /dev/null
logger "ACPI bluetooth on"
else
echo "disable" > /proc/acpi/ibm/bluetooth
echo "disabled" > /var/local/bluetooth
logger "ACPI bluetooth off"
fi
;;



- Turn on SATA link powersaving. This one is pretty straightforward: "for i in /sys/class/scsi_host/host*/link_power_management_policy; do echo min_power > $i; done". Once every power cycle. If you connect the power plug and disconnect it, only one link file will go back to powersave, so you have to execute it every time you go on battery. Acpid, again, is a great way to do that.

- Turn on PCIe powersaving. The control file is "/sys/module/pcie_aspm/parameters/policy". You can "echo powersave" to it to turn it on and "echo performance" to turn it off. As always, cat will tell you the actual status.

- Activate the sound auto-suspend. Echo the timeout to "/sys/module/snd_hda_intel/parameters/power_save". If you are not using the sound at all, mute it with the hardware button, just in case.

- This one may be VERY DANGEROUS. You can turn off the fan if you are going to do lightweight work and you trust your programs to be infinite-loop-free, so firefox is out of the question here. You have to load the thinkpad_acpi module with "fan_control=1" parameter and then manage the control file "/proc/acpi/ibm/fan". Again, "echo" stuff to set the fan on/off, "cat" to get info. I HIGHLY recommend to use the watchdog function and some script to make sure the processor won't melt. You can read the temperatures at "/proc/acpi/ibm/thermal", "/sys/class/thermal/thermal_zone*/temp" and "/sys/bus/platform/devices/coretemp.*/temp1_input", among others. Google is your friend ;)

- If hal is polling you nonexistent (on a X200) CD drive, kill it.

- If you are not using it, close firefox. The diffrerence may be 1W. If you plan on use it later, a simple "killall -STOP firefox" will do the trick. When you are going to use it again, "killall -CONT firefox". Bind it to a hotkey if you want.

- Also, "ifconfig eth0 down" sure doesn't hurt if you are not using the good ol' ethernet interface.

- Unloading modules doesn't usually work, but you may try it (and post a comment with your findings!). Some people unloads uhci, snd_hda_intel, or iwlagn. I haven't seen any difference trying, but your mileage may vary. You can also take one core of the cpu offline, but it seems to cause to actually consume MORE power than sitting idle. I haven't measured it myself, so if you do, please let me know! The control file is "/sys/devices/system/cpu/cpu1/online", 0 takes it offline, 1 brings it back to work.

- Last but not least: install and run powertop. It's a wise program. Follow it's suggestions. I use it to activate USB autosuspend and see which programs and wreaking havoc. Firefox, amarok2 and any java-based ones are on top positions, but some other programs may be broken and causing the processor to work more than it should. Knotify4 in KDE 4.1 had a 100Hz timer, for instance.

If I remember any other tweak, I'll post it here, stay tuned.

With all these tricks, brightness at minimum, everything off and doing _nothing_, the X200s drains 5.8W. With wifi on, brightness 5/15, browsing and programming (without heavy compiling), it goes at around 6.5W. On full brightness, no wifi, just above 7W. Full brighness + wifi it's under 7.5W. You have to do some pretty heavy stuff to take it above 10W, watching HD movies at full volume and brightness will do it, but anyway, at 10W the battery still lasts more than 8 hours. If you want me to try any specific combination, let me know.

Finally, for more info, try these:
http://www.nabble.com/X200-Battery-Life-td20188937.html
http://www.nabble.com/Linux-Thinkpad-f13633.html
http://www.mail-archive.com/ibm-acpi-devel@lists.sourceforge.net/
Or ask a question in the comments.

10 comments:

mikar said...

Hey, i've really been looking forward to this.
And i have to say, this guide, again, is great, just as your installation guide.
Sadly i don't have time right now to try this out, but i can confirm that in vista you get below 5.8 W. I've had 5.34 at the lowest, but different than in linux it climbs up very fast.
surfing the net right now with 50% brightness and wifi on but sound off i have 6.36 W to 7.9 (when browsing fast).
But in vista i think there are more spikes and consumption is higher i.e. when watching a movie on 50% brightness i remember i had about 10 W and above.
Can't say for sure, though. Anway I really am looking forward to trying out all your stuff and finally get to use linux in mobile use. Up to now it just drained 3 W more than vista, which made me stick with windows.

So.. Thanks again!
I think i'll get to try this out by the end of the week, but no matter when, I'll post my results with archlinux on thinkpad x200s.

Bye

n0rdik0 said...

You are welcome :) I'm curious if you'll manage to acheive under 7W surfing, it took me a while to get this together, at first I cuoldn't take it below 7.5W no matter what. Maybe it's something with the kernel versions, I upgraded from 27 to 28 and some time later it just worked.

Anyway, I'm looking forward to reading your results, if you need further explanation on some point, I'll update the article.

mikar said...

Hi n0rdik0,

the last few days i finally found some time to apply power settings and compile kernels.
My results so far (not yet satisfying):

The power consumption on a standard installation of ubuntu and arch linux were quite similar.
I've got around 12 W with everything turned on and brightness at max and 8.5 W when turning off wifi, bt, fp, setting lowest brightness and spinning down harddrive.
When applying all the config tweaks like pcie, usb and sound autosuspend, hdparm, writeback_centisecs etc. this dropped down to around 7.3 on both distributions.
Note that they came with different kernel versions (ubuntu generic 2.6.28.11 and arch linux generic 2.6.29.1).
I first compiled a new kernel in ubuntu and messed up ;) Second try was better and i got down to 6.7 W at the lowest.
In archlinux I compiled a kernel with ABS and basically your kernel config with only few settings changed.
It dropped down from 7.3 here also to like 6.8.
Those consumptions (6.7 / 6.8) are only achieved when everything is turned off, hdd is spun down, brightness is set to minimum and every config tweak is applied.

I wonder if it's just a kernel version thing or if i'm missing something here?

BTW your kernel config was a great help.

regards,
mikar

mikar said...

Oh, one more thing:
Could you maybe tell me the exact version of the kernel, you used for compiling your current one?
Maybe it's because i'm using vanilla kernels that i don't get below 6.7.

Thanks.

n0rdik0 said...

I'm currently using the version 2.6.28.8, but these results were consistent all along the 2.6.28 series.

With version 2.6.29 I was able to see for a moment a 4.9W when I left the laptop alone I came back after a while, but it could be some glitch. Unfortunately, kernel 2.6.29 has a buggy intel wifi driver which makes me unable to connect to my 5GHz WPA2 AP, so I'm back to 2.6.28.

I don't know what are you missing, try to unload uhci module maybe, and obviously make sure you have taken all the steps in the guide. If you turn off the wifi, make the "echo 5" thing BEFORE you turn the HW switch off. You should be able to acheive at least 6.0W as a minimum, the 0.7W is certainly quite a lot.

Also, running powertop may reveal some program running wild, maybe it's that: some backruond daemon misbehaving.

mikar said...

Hello n0rdik0,

I've made some progress and just wanted to let you know.
Idling on this page right now with comfortable 5/15 brightness is 6.4-6.6 W.
When _everything_ turned off or down, including the fan and sound as well as FPR and any connection devices, it's 5.9 W.

This, however, is with Openbox DE (one wakeup per sec in idle) and SSD Intel X25-M, which consumes notacibly less than my harddisk (it's about 0.4 W less even when idling).

For comparison: In Windows Vista I get 4.26 when idling but that of course is with proprietary drivers.

One thing I noticed when going through your post again:
From 5.9 W the power consumption goes up to 9 Watts just by maxing out the brightness.
I suspect there might be a issue with the intel drivers.
Maybe it's a kernel thing since I upgraded to the latest prepatch (.30-rc7).
I guess I will just keep trying. If there's something wrong and i can figure it out, I'll let you know.
So, thanks again for all the information.

Regards,

mikar

n0rdik0 said...

Maybe there some regression in the kernel.

I bought 2Gb RAM more (4Gb total) so I switched to a x64 system with a .29 kernel and now I can barely idle at less than 7W.

I don't know if it's the 64 bits system o the new kernel, but the power usage is more than 0.5W higher than before.

Thanks for keeping this updated with new info!!

Offtopic: how do you like the intel disk? I guess that 0 noise and the temperature is already low enough with the magnetic one. What about the performance?

mikar said...

[quote=n0rdik0]
Maybe there some regression in the kernel.[/quote]

I actually tested some different kernel versions now. Power consumption is rather constant through 2.6.29.1/2/3/4 and the one I'm currently using: 30-rc7. It's rather better than worse with the newer one (5.9 W vs 5.8 W in rc7). Haven't had the chance to test 2.6.28 though, I think I will do that. It would be really great to get somewhere near 4.5 Watts or even 5 Watts when idling, but I guess it's just not possible with opensource-drivers.

[quote=n0rdik0]
I don't know if it's the 64 bits system o the new kernel, but the power usage is more than 0.5W higher than before.[/quote]

With 4 GB of RAM my system consumes about 0.3 to 0.5 Watts more than with 2 GB (idling). I haven't used 64bit arch OS yet but don't the consume slightly more resources? Maybe that's how it adds up.

[quote=n0rdik0]
Offtopic: how do you like the intel disk? I guess that 0 noise and the temperature is already low enough with the magnetic one. What about the performance?[/quote]

I've had several hardware upgrades in my previous Desktop-PC and even in the X200s (320 GB Harddrive to faster 500 GB, 2 GB of RAM to 4 GB). Most of them were hardly noticeable. But switching to the Intel X25-M felt like doubling the overall performance all at once.
You can read it in any review but it's a pretty good MLC unlike other MLCs which tend to have sudden performance breaks.
Its thrice as fast at maximal reading speed (80 MB/s HDD vs 250 MB/s SSD) and more importantly very fast at dealing with small files. I did benchmarks, but can't find them right now. It's 1.5 to 3 times faster than the very good HDD even at 0.5 to 8 KB chunks.
I think it's a perfect upgrade for the X200s, since the harddrive really is the bottleneck right now.

mikar said...

When reading through my last post I noticed I haven't said how the performance concretely increased. Here are some examples:
Boot speed decreased by around 50% (from 20 secs to 10 secs measured with bootchart). Actually it's 5-6 seconds until the desktop is loaded and disc activity stops.
Also - obviously - applications are executed much faster now. IIRC firefox is considered slightly slower on linux and even though I use my old windows profile which is 80 MBs big, Firefox loads in <1 sec.
Programs get installed way faster, updating font-cache is _quick_ etc etc.

I also get the feeling that compilation time of the kernel has decreased very much, but I don't know how that can be explained. I thought it's mostly CPU-related.
Maybe it's just because I stripped down my kernel very much since I last compiled it on a HDD, but it's about 7 minutes now which was over 40 minutes (not measured but annoingly long anyway) when I started stripping down and compiling on a harddrive.

n0rdik0 said...

Wow, great points there.

I didn't realize that adding 2Gb RAM could actually have an impact on system power usage, just because of the new hardware. Lucky me, nobody noticed... ;)

About the 64bit issue, it shouldn't have any negative performance just because of the 64 bits, and actually it could be a tiny improvement, with 4Gb systems. In 32bits, you have to enable PAE to use all 4Gb, which means 3 indirecctions instead of 2 for every memory access. In 64bits you use a bit more of the RAM, but you only perform 2 lookups. Less lookups, less processor activity, potentially less power. We are talking about 2%, 5% tops, so it is a tiny difference, maybe 0. zero something Watts.

I'd like to try to boot with just 2Gb and then with a .28 kernel (which has been the best so far) and find out what is causing the rise, and some consistent figures. I think that the 64/32 bit mode has hardly something to do with the power consumption.

And finally the SSD. I've thought of getting one, but $350 for a disk is a bit pricey. These thing goes down in price as we speak so maybe in a couple of months...

Oh, BTW, I just took the laptop apart and put it back together and I saw one funny detail: the fan is a "maglev" with 0.9W rating. Just like a Japanese train, but made in China.