Matthew Garrett ([info]mjg59) wrote,
@ 2007-05-17 02:25:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Entry tags:advogato, ubuntu

Runtime power saving on Linux - the CPU
Modern processors have a variety of low power modes that can be entered while idle. These "C states" are numbered, currently ranging from C0 (a running CPU) to C4 (a deep runtime sleep state). The problem with these states is that the deeper the sleep, the longer it takes the CPU to wake up. In order to avoid excessive reductions in performance, the kernel must keep track of the processor usage pattern and avoid putting the CPU into a deep sleep mode when it's likely that it'll be needed for more work in the near future.

Traditionally, the kernel has had a fixed timer tick - that is, a fixed number of times a second, the timer will generate an interrupt, wake the kernel and allow processes to be rescheduled. This limits the maximum amount of time the CPU can remain idle, as this timer will fire even when the system is otherwise idle. A tick rate of 1000Hz is desirable for reducing latency, but will also result in the maximum sleep period being 1ms. This is less than ideal.

2.6.21 introduced dynamic tick functionality. Now, rather than having a fixed tick interval, if the system is idle the kernel looks at all outstanding timers and schedules a wakeup in time to answer the next timer. This allows much longer periods of sleep without compromising latency. However, for this to be useful it's desirable to have as few timer wakeup events as possible. The longer the CPU is going to be asleep, the deeper the sleep state that can be used.

Intel have recently released Powertop, an application that tracks the causes of wakeups. These wakeups can fall into two categories - kernel and userspace. Pure userspace timers will usually be due to an application having a timer to handle some sort of trivial activity, like blinking a cursor or polling for state. There are a couple of ways to improve this:

  • Just remove the timer - does it actually need to exist at all?
  • If you're polling for state, consider whether it would be possible to move to an event driven model. For example, right now screensavers poll the X server in order to obtain information about whether the session is idle. The X server has to keep track of this information anyway, so a simple extension could be added to notify applications when the user has been idle for a certain period of time.
  • Make sure you're only waking up when you need to. For instance, you might want to periodically check whether any new email has appeared. If there's no route to the internet, don't bother.
  • If you must use timers, try to schedule them to go off simultaneously. It's better to wake the kernel up once and do twice as much work than it is to wake it up twice. If you're a glib application, g_timeout_add_seconds() will round to the nearest second, so use it if possible.
Things in the kernel can be trickier. Kernel interrupts may be appearing because of some kernel code specifying a timer, but an alternative is that a userspace application is poking them. Many of the same considerations as userspace apply here:
  • If you don't need especially high-precision timing, use round_jiffies(). It'll result in synchronisation of many wakeups, and reduce the overall number.
  • If you're being woken up by userspace, figure out why. HAL polls storage devices every couple of seconds, generating several interrupts. This is necessary because most storage devices don't notify the system when media insertion occurs. Conversely, alsa sends notification events whenever the hardware state changes, and so it's not necessary for userspace to poll. If you can provide useful information to interested parties without them having to repeatedly ask, then do it.
  • If your hardware is idle, then do what you can to quiesce it. The Appletouch pad can be put into a mode where it doesn't send packets until touched, but once touched will continue sending packets. Watch for a stream of contentless interrupts, and put the hardware back to sleep.
  • If the hardware can generate interrupts when something happens, then use them - don't poll unnecessarily
Fixing these issues can range from the trivial (removing unnecessary timers) to the complicated (teaching gstreamer about alsa notifications, teaching the mixer applet to listen to signals from gstreamer). It's all helpful, though. Ideally you want your processor to be averaging at least 20ms in the deepest C state before it's woken up again. There's a lot of low-hanging fruit out there, and every fix improves battery life.



(Post a new comment)

Does linux have better power management than Windows ?
[info]http://certifi.ca/gng
2007-05-17 02:45 am UTC (link)
I like the idea of reduce power usage whenever possible, not just for notebook.

I got one of these new notebook with Core Duo which has this C state feature and do a measurement on the average power usage(under Vista and XP). It seems that for normal usage, it draws about 14W(where I believe 3-6W goes to the display system + LCD) under Vista and about 5-10% more under XP(so there is some improvement in the new OS). This is very impressive as I can run it 24/7 as a home server/media center without hurting the environment too much yet with the maximum performance when needed. I used to do it with Xbox running linux but that amounts to at least 30W(the machine along, no display).

My past experience with linux is that its power management is way behind Windows. Anyone knows how they compare with Windows now ?

(Reply to this) (Thread)

Re: Does linux have better power management than Windows ?
[info]mjg59
2007-05-17 02:52 am UTC (link)
Linux is probably behind Windows, but no longer way behind. You're probably looking at a small number of watts difference between Windows and Linux, and we're rapidly implementing new power management functionality and integrating that into userspace. I'd expect to be most of the way there within a year.

(Reply to this) (Parent)(Thread)

Re: Does linux have better power management than Windows ?
[info]fooishbar
2007-05-17 09:43 am UTC (link)
Depends what you mean. Linux on some systems seems to scale down infinitely better than Windows, and even better than other competing systems targeted towards mobile systems; maybe Linux on general-use laptops has a little while to go, but the suggestion that Linux is somehow inherently unsuited to aggressive power saving is complete crap.

(Reply to this) (Parent)(Thread)


[info]rachelneelie
2008-08-11 01:37 am UTC (link)
Maybe you'll even collaborate with others. Maybe you'll even get ocal advertising. But, in the end, don't stress about schedules and deadlines.

(Reply to this) (Parent)(Thread)

(no subject) - [info]adamdelany, 2008-08-11 02:28 am UTC
(no subject) - [info]hectorpaques, 2008-08-11 06:14 am UTC
(no subject) - [info]darrylmullenax, 2008-08-11 09:28 am UTC

(Reply from suspended user)

(Reply from suspended user)

(Reply from suspended user)

(Reply from suspended user)

(Reply from suspended user)

[info]viktorialidoc
2008-07-13 10:47 am UTC (link)
Posted by: Anonymous Coward on December AM Linux is a threat to Windows because MS has no way to keep up.

(Reply to this) (Parent)(Thread)

(no subject) - [info]charlenehodak, 2008-07-15 01:11 am UTC
(no subject) - [info]kittymavex, 2008-07-16 01:24 am UTC
(no subject) - [info]eliasmille, 2008-08-11 01:48 am UTC
(no subject) - [info]jospehkraenwyn, 2008-08-11 04:41 am UTC
(no subject) - [info]efrainyurkiewi, 2008-08-11 05:59 am UTC
(no subject) - [info]paulamaclaurin, 2008-08-11 08:32 am UTC
(no subject) - [info]anniewezyn, 2008-07-16 04:06 am UTC
(no subject) - [info]beckymcconiel, 2008-08-06 04:10 am UTC
(no subject) - [info]heatherkirkcon, 2008-08-10 10:36 pm UTC
(no subject) - [info]franklynluckas, 2008-08-11 02:15 am UTC
(no subject) - [info]alexiahaymaker, 2008-08-11 12:40 am UTC
(no subject) - [info]solomonseals, 2008-08-11 01:39 am UTC
(no subject) - [info]rodrickwachowi, 2008-08-11 04:25 am UTC
(no subject) - [info]vancewalthall, 2008-08-11 07:46 pm UTC

(Reply from suspended user)

[info]rodgersaulman
2008-08-06 04:13 am UTC (link)
Posted by: Anonymous Coward on December AM Linux is a threat to Windows because MS has no way to keep up.

(Reply to this) (Parent)(Thread)

(no subject) - [info]rachelneelie, 2008-08-11 12:06 am UTC

(Reply from suspended user)

[info]teresaporin
2008-07-16 01:16 am UTC (link)
I also like the idea of being able to hit the power button, not wait for all those applications to load, and get to work with whatever I want to do right off the bat.

(Reply to this) (Parent)


[info]alejandromcenr
2008-08-11 01:01 am UTC (link)
Cheers I use power mid to change my channels its old and wired but it works a treat. . . Title: Re: Newbies Discussion here only Post by: cyprusdunc on 25 February, PM Hi m8,it is the old adage if it works why change it,powermids as you say have been around a long time,if it works well keep it,not always an improvement happens when you upgrade, Dunc.

(Reply to this) (Parent)


[info]vincesatterly
2008-08-11 01:22 am UTC (link)
Cheers I use power mid to change my channels its old and wired but it works a treat. . . Title: Re: Newbies Discussion here only Post by: cyprusdunc on 25 February, PM Hi m8,it is the old adage if it works why change it,powermids as you say have been around a long time,if it works well keep it,not always an improvement happens when you upgrade, Dunc.

(Reply to this) (Parent)


[info]tonybeattie
2008-08-12 11:58 am UTC (link)
Cheers I use power mid to change my channels its old and wired but it works a treat. . . Title: Re: Newbies Discussion here only Post by: cyprusdunc on 25 February, PM Hi m8,it is the old adage if it works why change it,powermids as you say have been around a long time,if it works well keep it,not always an improvement happens when you upgrade, Dunc.

(Reply to this) (Parent)

(Reply from suspended user)
glib_timeout_new_seconds ?
[info]joey.kitenet.net
2007-05-17 04:01 am UTC (link)
The only mention of glib_timeout_new_seconds I can find is in this blog.

I couldn't think of a nice way to do with using standard stuff so I was going to go look at how glib does it..

(Reply to this) (Thread)

Re: glib_timeout_new_seconds ?
[info]mjg59
2007-05-17 04:07 am UTC (link)
Eh. glib_timeout_source_new_seconds() rather than glib_timeout_new_seconds() - sorry about that, I'll fix that in the post. It's new in 2.14.

(Reply to this) (Parent)

Re: glib_timeout_new_seconds ?
[info]printf.net
2007-05-17 04:08 am UTC (link)
> The only mention of glib_timeout_new_seconds I can find is in this blog.

He means g_timeout_add_seconds().

- Chris.

(Reply to this) (Parent)

Re: glib_timeout_new_seconds ?
[info]mjg59
2007-05-17 04:08 am UTC (link)
g_timeout_source_new_seconds(), even. Wow, I suck at language.

(Reply to this) (Parent)(Thread)

Re: glib_timeout_new_seconds ?
[info]joey.kitenet.net
2007-05-17 04:45 am UTC (link)
Hmm, looks like this offsets the timers so they're not on the new second boundry but are some machine/session-specific offset from it. Makes it sorta hard to interoperate with that if one isn't using glib and would still like to wake up near the same time.

(Reply to this) (Parent)(Thread)

Re: glib_timeout_new_seconds ? - (Anonymous), 2007-05-17 05:19 am UTC

[info]printf.net
2007-05-17 04:29 am UTC (link)
> It's all helpful, though. Ideally you want your processor to be averaging at least 20ms in the deepest C state before it's woken up again. There's a lot of low-hanging fruit out there, and every fix improves battery life.

Not to mention the possibility -- which is equal parts terrifying and motivating -- where each fix you make to an app that OLPC is considering using results in several million children reclaiming $timeperiod of their day from having to yank on a pullcord to recharge their machine. :)

(NetworkManager also wakes up regularly, to poll /sys/.../eth0/carrier to see if you've removed an ethernet cable. Sigh.)

- Chris.

(Reply to this) (Thread)


[info]lionsphil
2007-05-17 08:43 am UTC (link)
Also, presumably, CPU temperature. Given how warm modern laptops tend to be, I think we clearly need a "Help Prevent Global Warming: Stop Polling!" campaign. ;)

(Reply to this) (Parent)(Thread)


[info]nbrown2
2007-12-19 06:12 pm UTC (link)
How about "help stop global warming, use a laptop" campaign? Desktops are way more energy intensive than laptops.

Nathan
How Can We Help Stop Global Warming With A Bracelet?

Learn How To Help Stop Global Warming By Knowing Your Paper Recycling Facts

(Reply to this) (Parent)(Thread)

(no subject) - [info]lionsphil, 2007-12-19 06:14 pm UTC
(no subject) - (Anonymous), 2007-12-19 06:17 pm UTC
ARM over x86??? - (Anonymous), 2008-06-12 05:24 am UTC

[info]jmtd
2007-05-17 08:55 am UTC (link)
Good post, thanks. Coming across one like this on p.k.o makes the 20+ anime ones a small price to pay.

(Reply to this)


[info]ewx
2007-05-17 12:26 pm UTC (link)
A time resolution parameter to select/poll workalikes would perhaps help - "I want to be woken up (when these FDs are ready or...) after this amount of time, plus-or-minus some error" - would be handy, as not every application uses glib.

(Reply to this)

appletouch patch
(Anonymous)
2007-05-17 01:59 pm UTC (link)
The article sums it up nicely. On which Macbook did you test your appletouch patch (http://www.linuxpowertop.org/patches/appletouch.patch) (which is great btw. as it is the wakeup hog nr.1)?

I tried it on my Macbook (Non-Pro) vs. a 2.6.21-mactel kernel and it indeed showed a huge improvement with a decreased wakeup count, however it also introduced problems using the keyboard and touchpad thus I can't really use it yet.

- Right-click using two-fingers works but the popup immediately closes. (I am using synaptics in Xorg) Holding down the single touchpad button makes it stay though, thus it looks like some bug possibly introduced with the code.
- After some delay I can not type anymore in gnome-terminal until I use the touchpad to "wake up" the thing and make the characters I typed appear.

I don't know what can cause the right-click issue by looking at your patch but I guess the keyboard should also be taken into account when suspending the touchpad or similar? I'll dig into it when time since this looks like a clear canditate for upstream appletouch. ;)

(Reply to this) (Thread)

Re: appletouch patch
[info]mjg59
2007-05-17 08:03 pm UTC (link)
The issue with the first version of the patch was that it was managing to cause the left mouse button state to become jammed. The lack of functioning keyboard was just a consequence of that - try typing in a terminal while holding down the left mouse button :) There's still a couple of issues that I'm trying to figure out.

(Reply to this) (Parent)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…