Matthew Garrett ([info]mjg59) wrote,
@ 2008-05-08 23:10:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Entry tags:advogato

Modern CPUs are great. They have all sorts of advanced power saving features, which is one of those nice cases where everyone can save money, gain performance and claim environmental credentials at the same time. Everyone's a winner.

Well. Everyone's a winner as long as your software doesn't suck.

I've talked about the benefits of the tickless kernels and reducing wakeups and spending longer in deep C states before, so if you don't know about them then go and read that first. This time I'm going to focus on a different level of hardware, and a different level of suck.

For a long time, laptops supported changing the speed of processors when switching between AC and battery. CPU power consumption is proportional to frequency, so dropping the frequency meant a longer battery life. Of course, it also meant that it took longer to get anything done - the reason this was still a win was because CPUs in those days consumed just as much power when idle as when running. Transmeta introduced a technology called Longrun with their Crusoe processors, bringing the ability to drop both the frequency and the voltage of the CPU simultaneously. With power consumption being proportional to the square of the voltage, even a small drop resulted in worthwhile power savings. As the only really worthwhile thing Transmeta brought to the x86 world[1], this was unsurprisingly ripped off by everyone else. Intel introduced their Enhanced Speedstep, AMD gave people PowerNow and VIA have Longhaul.

Obviously, reducing the frequency of the CPU increased battery life. Everyone's happy?

No[2].

The problem is that nowadays, processors don't consume as much energy when they're idle as when they're running. The aforementioned C states mean that an idle processor consumes a tiny percentage of a loaded one - an ultra-low voltage Intel part will draw on the order of a watt. Executing code, even at the lowest voltage and frequency, will draw far more power. Obviously, we want to keep the processor idle for as long as possible. The easiest way to do this would be to never run anything, but that's not a real option. The alternative is to run when we have to, but make sure that we get it over with as quickly as possible so we can return to the idle state. Counterintuitively, that means switching to the highest voltage and frequency, executing the code and then dropping back into the idle state. By going faster, we save power[3].

In summary, the only sensible way to use a CPU is to run it as fast as possible in order to let it idle as much as possible, and drop the frequency and voltage when it's not doing anything. The. Only. Sensible. Way.

Some people write software that lets you choose different power profiles depending on whether you're on AC or battery. Typically, one of the choices lets you reduce the speed of your processor when you're on battery. This is bad. It is wrong. The people who implement these programs are dangerous. Do not listen to them. Do not endorse their product and/or newsletter. Do not allow your eldest child to engage in conjugal acts with them. Doing this will reduce your battery life. It will heat up your home. It will kill baby seals. The sea will rise and your car will float away. If you are already running it, make sure that it always sets your cpufreq governor to ondemand and does not limit the frequencies in use. Failure to do so will result in me setting you on fire[4].

The only legitimate reasons for limiting the speed of your CPU are to avoid overheating (which should be fixed in the kernel, really - having userspace in charge of ensuring the continued functioning of the machine is madness) or to make the machine quieter. And if you want your machine to be quieter, there should be a tickbox marked "Reduce performance in order to reduce noise" or something, which would take into account all the sources of heat in your machine rather than just your CPU. Encouraging the managing of acoustic levels by asking users to restrict the functionality of their CPU is just another way of saying "Look! We suck!". Letting the user choose a specific CPU governor or a specific frequency is not a useful thing to do. Don't do it unless you want to see dead kittens. Delivered by UPS.

[1] And, presumably, whatever else Intel and everyone else ended up licensing off them which resulted in their reinvention as an IP company rather than a CPU one, but that's just not interesting to me.

[2] Even ignoring the people that are unhappy for entirely unrelated reasons, such as injured toenails or the brutal murder of their family

[3] There's a corner case here, which is a system that is always entirely CPU bound. Say we halve the CPU's speed. Along with the voltage drop, that gets us down to about 20% of the original power consumption. Of course, it now takes twice as long to do anything and your screen, RAM, hard drive, chipset and so on are still drawing power, so will end up costing you twice as much power as they would have done if you'd run at full speed. If you do the maths, it works out that you save power if your processor's full-speed power consumption is more than 1.7 times that of the rest of the platform. In the real world, things are made more complicated by the rest of your platform consuming more power if you're working over a longer period of time - your hard drive is going to end up spending more time spun up, your memory bus is going to be active for longer and so on. You're basically not going to hit this case.

[4] While the burning of your body will result in carbon emissions, the reduction in power usage should offset this in the long run



(Post a new comment)


(Anonymous)
2008-05-08 11:42 pm UTC (link)
Speaking of powersaving software, does gnome-power-manager have any actual use besides haphazardly disabling your fan so your laptop overheats under high load?

(Reply to this)(Thread)


[info]mjg59
2008-05-09 12:11 am UTC (link)
Given that gnome-power-manager has no code that interacts with the fans...

(Reply to this)(Parent)


[info]base3
2008-05-08 11:54 pm UTC (link)
It seems that frequency dropping is not a solution looking for a problem, but a solution to the problem you mentioned in your previous post; that of a mostly idle system which is never idle for very long between brief moments of activity. Frequency dropping allows you to reduce power consumption while keeping latency down to a level that wouldn't permit deep sleep. I understand there is a software answer to this problem but it's not always easier in practice to use the correct software answer..

(Reply to this)(Thread)


[info]mjg59
2008-05-09 12:13 am UTC (link)
Right. If you're seeing very bursty and short loads, then staying at the lowest frequency and voltage is the right thing to do. This is why ondemand has thresholds that need to be hit before it'll raise the frequency. I'll write some more about this at some point...

(Reply to this)(Parent)


[info]fanf
2008-05-09 12:15 am UTC (link)
There's another aspect to CPU power that you didn't mention, which is the time-to-recovery, or latency to switch from low to high power modes, which can make it hard to implement an on-off plan. e.g. see http://jeffr-tech.livejournal.com/21310.html

This also reminds me of XMOS. Their idea is to design a multi-threaded CPU which spends most of its time switched off, but which can wake up and run the appropriate thread at full speed when an interrupt comes in. Really low interrupt latency, good performance, and low average power consumption. (Their chief techie is David May, ex of inmos and the transputer.)

(Reply to this)(Thread)


[info]mjg59
2008-05-09 12:26 am UTC (link)
That's mostly an issue with C states, rather than the frequency/voltage transitions. Going from C4 to C0 takes 17 microsends on my laptop. You're probably looking at the same sort of order of magnitude for P state transitions, but that's probably less of an issue given that you're already executing code.

Optimising idle behaviour is an interesting problem. Linux has pluggable cpu idle schedulers now. It'll be interesting to see whether anyone tries to improve on what's already there.

(Reply to this)(Parent)


[info]mas90
2008-05-09 01:11 am UTC (link)
Surely the noise generated by a system is roughly proportional to its power usage anyway (i.e. the power eventually becomes heat, which must be removed on average at the rate at which it is produced by means of a variable-speed fan), so the limiting of your processor's frequency may in fact increase noise on average?

(Of course, intermittently running your processor as fast and as hot as possible may produce short bursts of loud fan noise, depending on how quickly the fan controller reacts etc., but my point is that nonetheless on average your system will be quieter.)

(Reply to this)(Thread)

It is...
(Anonymous)
2008-05-09 05:49 pm UTC (link)
but don't call me Shirley!

(Reply to this)(Parent)


[info]jbailey
2008-05-09 02:11 am UTC (link)
So, [3] would be for Itanium users?

I still remember the Intel guy looking at me nervously when I asked him at UDS-Paris if they'd considered selling Itaniums for "Smart Homes", and using them as baseboard heaters for the winter months.

(Reply to this)

Your "corner case" is quite common, actually
[info]joe_buck
2008-05-09 02:36 am UTC (link)
Consider a processor that's doing real-time video compression. It's better to scale the voltage on that processor until it's entirely CPU-bound, than to try to put it to sleep between frames. The reason is that halving the voltage makes the processor twice as slow, but it consumes 1/4 the power. So having the full-speed processor sleep for half the time saves half the power, but halving the speed cuts the power consumption to 1/4.

So it seems that the processor architects aren't idiots after all. Consider the case where someone's watching a video on her laptop. You can't put the display to sleep, or the network card, or much of anything. But you can cut the processor voltage to the point where it's just fast enough to keep up with the demand, and save considerable power.

(Reply to this)(Thread)

Re: Your "corner case" is quite common, actually
[info]http://blogs.gnome.org/jamesh/
2008-05-09 09:04 am UTC (link)
Note that if you play your movie at twice normal speed and have the CPU run in its top speed, you won't need to keep the display, network, etc running as long.

These other savings may outweigh the savings from running the CPU at a lower clock rate and watching the movie at normal speed.

(Reply to this)(Parent)

Re: Your "corner case" is quite common, actually
[info]mjg59
2008-05-09 09:20 am UTC (link)
The video compression case is interesting, though one that's pretty easily monitored by the governor - and the ondemand threshold values effectively implement this right now. By default, ondemand won't ramp the frequency upwards unless you're averaging more than 30% load (not all of the CPU hardware is clocked in the same way, so certain parts will consume the same amount of power regardless of the frequency) which deals with this case. If you're doing small amounts of work all the time, it'll keep the processor slow rather than jumping between states. But you're right, I didn't discuss that issue.

The corner case I was describing was one where the processor will remain loaded regardless of the amount of work there is to be done. I think that's rather less common.

(Reply to this)(Parent)(Thread)

(no subject) - [info]king_of_wrong, 2008-05-09 07:38 pm UTC
(no subject) - (Anonymous), 2008-05-09 11:45 pm UTC
(no subject) - [info]king_of_wrong, 2008-05-10 08:10 am UTC
(no subject) - [info]martling, 2008-05-10 08:52 am UTC
Re: Your "corner case" is quite common, actually - [info]idlethread, 2008-05-09 09:30 am UTC

(Anonymous)
2008-05-09 04:54 am UTC (link)
To follow up on the previous poster, any real-time processing needs fine-grained control over cpu speed. Real-time video encoding is one case. I do live music shows, which requires my laptop to be responsive, but not overheat. That's another case. Since you're obviously unfamiliar with the notion of real-time processing, think about running Cheese, with effects and recording and such.

Just because you're opinionated doesn't mean you're right. Think about it the next time you're about to say something stupid.

(Reply to this)(Thread)

(no subject) - [info]pjc50, 2008-05-09 08:44 am UTC
(no subject) - (Anonymous), 2008-05-09 10:34 am UTC
(no subject) - [info]mjg59, 2008-05-09 09:03 am UTC
screen savers
(Anonymous)
2008-05-09 05:14 am UTC (link)
All those folks running screen savers on their laptops get a little better battery life if the screen saver runs slower. What's speed to a screen saver?

(Cf. the old joke that ends, "Sure, but what's time to a pig?")

(Reply to this)


[info]martling
2008-05-09 06:28 am UTC (link)
Letting the user choose a specific CPU governor or a specific frequency is not a useful thing to do.

NO. FUCK THE HELL OFF. THIS IS WHY I HATE LINUX NOW.

Hide it under advanced options, bury it in a configuration file, do whatever the fuck you like to suggest it is a bad idea but DO NOT STOP ME FROM DOING IT WHEN I ACTUALLY WANT TO.

My laptop overheats when run flat out for too long. The ondemand governor cuts the frequency a bit when it gets hot but not enough to stop it crossing the shutdown threshold. I used to be able to manually set the governor to "powersave" but now some bastard part of hal keeps taking over and changing it back. Can I make it stop without killing off hal completely? Is it configurable? Is it documented? Is it fuck.

Yes it's a strange edge case, yes I am someone who could go and read and patch the source if I had time. I don't care. I'm trying to get work done. Linux used to let me get on with it. Now increasingly it is just getting in my way like Windows, because it has too many layers of undocumented crap.

Edited at 2008-05-09 06:35 am UTC

(Reply to this)(Thread)

(no subject) - (Anonymous), 2008-05-09 08:31 am UTC
(no subject) - [info]pjc50, 2008-05-09 08:48 am UTC
(no subject) - [info]martling, 2008-05-09 09:16 am UTC
(no subject) - [info]mjg59, 2008-05-09 09:25 am UTC
(no subject) - [info]martling, 2008-05-09 03:12 pm UTC
(no subject) - [info]mjg59, 2008-05-09 03:19 pm UTC
(no subject) - [info]martling, 2008-05-09 03:42 pm UTC
(no subject) - (Anonymous), 2008-05-09 03:48 pm UTC
(no subject) - [info]martling, 2008-05-09 05:42 pm UTC
URI please? - (Anonymous), 2008-05-09 07:11 pm UTC
Re: URI please? - [info]martling, 2008-05-09 11:09 pm UTC
(no subject) - [info]simon80.myopenid.com, 2008-05-10 02:44 am UTC
(no subject) - [info]martling, 2008-05-10 07:06 am UTC
(no subject) - (Anonymous), 2008-05-09 02:08 pm UTC
(no subject) - [info]mjg59, 2008-05-09 09:08 am UTC
(no subject) - [info]reddragdiva, 2008-05-11 12:43 pm UTC
(no subject) - [info]lionsphil, 2008-05-09 11:56 pm UTC
(no subject) - [info]mjg59, 2008-05-10 12:01 am UTC
(no subject) - [info]lionsphil, 2008-05-10 12:28 am UTC
(no subject) - [info]mjg59, 2008-05-10 12:49 am UTC
(no subject) - [info]lionsphil, 2008-05-11 11:34 am UTC
(no subject) - [info]ninjaseg, 2008-05-12 12:11 am UTC
(no subject) - [info]martling, 2008-05-12 01:43 am UTC
which chips?
(Anonymous)
2008-05-09 08:35 am UTC (link)
So this applies to modern chips, but what about older chips. i have a varied bunch of CPUs
Desktop is a Xeon woodcrest. it seems not to do frequency scaling, but the power usage does vary with load, so i guess it is working as described above.

A via C3 samuel. pretty low power anyway

pentuim M. does lots of steps of scaling. maybe this is too old to have save much in C states.

PowerPC G4. does any of the above apply to non x86?

Will ondemand take old chips into account, or does it assume that everything is like a core2duo?

(Reply to this)(Thread)

Re: which chips? - [info]smcv, 2008-05-09 10:02 am UTC
Re: which chips? - (Anonymous), 2008-05-10 03:34 am UTC
Idle loop - (Anonymous), 2008-05-09 07:15 pm UTC
You forgot about fans
(Anonymous)
2008-05-09 10:45 am UTC (link)
You're forgetting something important here - fans use energy. As higher frequency = more heat = fan comes on (or spins faster), it doesn't necessarily follow that it saves power to either run at idle or full speed.

(Reply to this)(Thread)

Re: You forgot about fans - [info]mjg59, 2008-05-09 10:47 am UTC
Re: You forgot about fans - [info]bjh21, 2008-05-09 05:11 pm UTC
Re: You forgot about fans - (Anonymous), 2008-05-09 06:23 pm UTC
Re: You forgot about fans - (Anonymous), 2008-05-10 01:34 pm UTC
Re: You forgot about fans - (Anonymous), 2008-05-10 07:47 pm UTC
That's a nice theory
(Anonymous)
2008-05-09 08:49 pm UTC (link)
You've outlined a perfectly believable theory why, under certain loads, for certain processor designs, a certain idealized governor should work better than another. But you haven't shown me any data. As a scientist, you should know that a model of a complex system isn't worth much without experimental confirmation.

You've got a very specific prediction here: your laptop should use less power running "ondemand" than it does running "powersave". That's easy enough to measure. Measure it.

If you're right, I suspect you can get the kernel patched to reflect this. But I'm not going to believe it until you have numbers.

(Reply to this)(Thread)

Re: That's a nice theory - [info]http://mylid.net/jason.riedy, 2008-05-10 09:17 pm UTC
Halt instruction
(Anonymous)
2008-05-10 12:57 am UTC (link)
It's not strictly true that CPUs used to consume the same amount of power idle and not. Ok, so without special effort, it was only a slight difference, however Linux did put in that special effort.

The idle loop in Linux has always used the "halt" instruction on x86 chips to reduce power usage when nothing is happening. This used to be a big savings (now there are more effective ways). Way back, Linux used to get better battery life than Windows on laptops -- of course Microsoft later added power management, and Linux has slightly trailed in that respect.

(Reply to this)


[info]jmspeex
2008-05-10 04:32 am UTC (link)
The only legitimate reasons for limiting the speed of your CPU are to avoid overheating [...] or to make the machine quieter.

I recently found another reason. I was flying in a plane with power outlets in the seat in front. Plug in the laptop, power goes out after only a few seconds. After several tries, I finally figured out the problem was that my laptop (Dell D820) was consuming too much power. In the end, the only reliable way I found to use plug my laptop was to disable the second core in the bios and lock it to 1 GHz (it's a 2 GHz CPU). Then, I would remove the battery (to prevent it from drawing current when recharging) and power the thing on.

(Reply to this)

You are overlooking something
(Anonymous)
2008-05-10 06:53 am UTC (link)
If your CPU runs slower, it may also mean that it gets to do less work. Terminal emulators like xterm tend to consume lots of CPU time when they get enough CPU power not to be preempted. When there is less CPU available, they will get preempted and that means they start batching stuff, becoming vastly more effective.

As an extreme case, try

time xterm -e "hexdump -v /dev/zero|dd bs=1 count=1000000";read

I get

real 0m12.060s
user 0m3.064s
sys 0m3.400s


1000000+0 records out
1000000 bytes (1.0 MB) copied, 13.1505 s, 76.0 kB/s

Now if I, in contrast, do

while true;do :;done & time nice xterm -e "hexdump -v /dev/zero|dd bs=1 count=1000000";kill %1;read
[1] 9161

real 0m41.464s
user 0m1.620s
sys 0m3.472s

See? Less CPU available, less CPU used.

(Reply to this)


[info]reddragdiva
2008-05-11 12:50 pm UTC (link)
I actually wrote my post (an extra hour of battery free just by using Leengux instead of XP) without having seen this one first ...

(Reply to this)

ondemand vs. conservative
(Anonymous)
2008-05-12 07:40 pm UTC (link)
I've been forcing my laptops to use "ondemand" 75% of the time, but to switch to "conservative" when under 50% battery life, because I recall reading (in the kernel docs? cpufreqd man page?) that conservative has less aggressive ramp-up times. 95% of what I'm doing on the laptop is OOo or reading Akregator feeds when on battery, so I always figured that my HDD spinups and panel brightness were costing me more than CPU utilization.

Are my understandings about "conservative" vs. "ondemand" governors correct? If not, where can I head to correct my errors?

(Reply to this)


[info]dadebit
2008-05-14 05:40 am UTC (link)
I use to care a lot about battery life, and I did some tests comparing performances using Linux and Osx.
I tryed idling with wifi and bluetooth on, minimum backlight and hard disk always on.
Linux gave me 4 hours, osx 5 hours.
but the thing I want to underline is that.. using powertop I can see that the cpu
is in C4 for 99% of the time
and in C0 for 1% of the time
so i'm wandering.. is really 1% of the time the power consuming problem ?
I think that osx stays in C0 for more than 1% (I have no way to see if that's true)
Another thing I can say is that under haevy load linux uses around 25/28watts, and osx never goes over 20W, but that could be because osx throttles cpu using T states also
I find it strange to look for powersaving in the cpu wich is on for 1% of the time, maybe the power waste is somewherelse

(Reply to this)

Makes sense, to a point
[info]dnivie
2008-05-15 10:13 am UTC (link)
This seems to make sense only to a point.

As you correctly point out, if you scale back the frequency, you can scale back the voltage too, the practical result of which is that running at half speed may consume only 1/4th the power.

So, if there's some load that really requires half your CPU, that load is going to consume less power if done by scaling frequency, rather than keeping the throttle wide open and sleeping half the time.

If your point was just that the OS should detect this and do the clever thing, rather than letting the user make a choice, then sure, fine. That wasn't what you said though, you said: the only sensible way to use a CPU is to run it as fast as possible in order to let it idle as much as possible

Which isn't true. Infact, in the above scenario that'll result in double power-consumption relative to the slow-but-steady approach.

(Reply to this)


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