From wakelocks to a real solution
Getting Linux power management to work well has been a long, drawn-out process, much of which involves fixing device drivers and applications, one at a time. There is also a lot of work which has gone into ensuring that the CPU remains in an idle state as much as possible. One of the reasons that some developers found the wakelock interface jarring was that the Android developers chose a different approach to power management. Rather than minimize power consumption at any given time, the Android code simply tries to suspend the entire device whenever possible. There are a couple of reasons for this approach, one of which we will get to below.
But we'll start with a very simple reason why Android goes for the "suspend the entire world" solution: because they can. The hardware that Android runs on, like many embedded systems (but unlike most x86-based systems), has been designed to suspend and resume quickly. So the Android developers see no reason to do things any other way. But that leads to comments like this one from Matthew Garrett:
A solution that's focused on powering down as much unused hardware as possible regardless of the system state benefits the x86 world as well as the embedded world, so I think there's a fairly strong argument that it's a better solution than one requiring an explicit system state change.
Matthew also notes that it's possible to solve the power management problem without fully suspending the system; he gives the Nokia tablets as an example of a successful implementation which uses finer-grained power management.
That said, it seems clear that the full-suspend approach to power management is not going to go away. Some hardware is designed to work best that way, so Linux needs to support that mode of operation. So there has been some talk about how to design wakelocks in a way which fits better into the kernel as a whole. On the kernel side, there is some dispute as to whether the wakelock mechanism is needed at all; drivers can already inhibit an attempt by the kernel to suspend the system. But there is some justice to the claim that it's better if the kernel knows it can't suspend the system without having to poll every driver.
One simple solution, proposed by Matthew, would be a simple pair of functions: inhibit_suspend() and uninhibit_suspend(). On production systems, they would manipulate an atomic counter; when the counter is zero, the system can be suspended. These functions could take a device structure as an argument; debugging versions could then track which devices are blocking a suspend at any given time. The user-space equivalent could be a file like /dev/inhibit_suspend; as long as at least one process holds that file open, the system will continue to run. All told, it looks like a simple API without many of the problems seen in the wakelock code.
There were a few complaints from the Android side, but the biggest sticking point appears to be over timeouts. The wakelock API implements an automatic timeout which causes the "lock" to go away after a given time. There appear to be a few reasons for the existence of the timeouts:
- Since not all drivers use the wakelock API, timeouts are required to
prevent suspending the system while those drivers are running. The
proposed solution to this one is to instrument all of the drivers
which need to keep the system running. Once an acceptable API is
merged into the kernel, drivers can be modified as needed.
- If a process holding a wakelock dies unexpectedly, the timeout will
keep the system running while the watchdog code restarts the faulting
process. The problem here is that timeouts encode a recovery policy
in the kernel and do little to ensure that operation is actually
correct. What has been proposed instead is that the user-space
"inhibit suspend" policy be encapsulated into a separate daemon which
would make the decisions on when to keep the system awake.
- User-space applications may simply screw up and forget to allow the system to suspend.
The final case above is also used as an argument for the full-suspend approach to power management. Even if an ill-behaved application goes into a loop and refuses to quit, the system will eventually suspend and save its battery anyway. This is an argument which does not fly particularly well with a lot of kernel developers, who respond that, rather than coding the kernel to protect against poor applications, one should simply fix those applications. Arjan van de Ven points out that, since the advent of PowerTop, the bulk of the problems with open-source applications have been fixed.
In this space, though, it is harder to get a handle on all of these problems. Brian Swetland describes the situation this way:
- carrier deploys a device
- carrier agrees to allow installation of arbitrary third party apps without some horrible certification program requiring app authors to jump through hoops, wait ages for approval, etc
- users rejoice and install all kinds of apps
- some apps are poorly written and impact battery life
- users complain to carrier about battery life
Matthew also acknowledges the problem:
It is a real problem, but it still is not at all clear that attempts to fix such problems in the kernel are advisable - or that they will be successful in the end. Ben Herrenschmidt offers a different solution: a daemon which monitors application behavior and warns the user when a given application is seen to be behaving badly. That would at least let users know where the real problem is. But it is, of course, no substitute for the real solution: run open-source applications on the phone so that poor behavior can be fixed by users if need be.
The Android platform is explicitly designed to enable proprietary
applications, though. It may prove to be able to attract those
applications in a way which standard desktop Linux has never quite managed
to do. So some sort of solution to the problem of power management in the
face of badly-written applications will need to be found. The Android
developers like wakelocks as that solution for now, but they also appear to
be interested in working with the community to find a more
globally-acceptable solution. What that solution will look like, though,
is unlikely to become clear without a lot more discussion.
Index entries for this article | |
---|---|
Kernel | Android |
Kernel | Power management/Opportunistic suspend |
Posted Feb 19, 2009 7:36 UTC (Thu)
by aleXXX (subscriber, #2742)
[Link] (10 responses)
Hmm, I don't agree with that. Isn't it after all similar to memory
Alex
Posted Feb 19, 2009 9:23 UTC (Thu)
by dgm (subscriber, #49227)
[Link]
Posted Feb 19, 2009 9:32 UTC (Thu)
by mjg59 (subscriber, #23239)
[Link] (6 responses)
Posted Feb 19, 2009 10:40 UTC (Thu)
by khim (subscriber, #9252)
[Link] (5 responses)
If you run some wild application you can make your system slow down so
much that sshing to it and killing offending process is impossible. Somehow
the answer "fix your userspace" was never considered "good enough" and
years were spent developing many systems (quotas, cotainers, VMs) to make
it safe to run any application and still have control over system. Sure: any application will consume resources. But with phone you need
guarantee that consumed resources (all resources including power) are
limited by some arbitrary value. If it's enough for program - it'll work
great, if not - I can decide if fancy screen-saver worth giving it half of battery resources. The same story as with preemptive vs cooperative multitasking: cooperative multitasking works great if you have control over all programs
(see Novel Netware 3.x), but if not - it's disaster (see Windows 3.x and/or
MacOS before MacOS X).
Posted Feb 19, 2009 10:50 UTC (Thu)
by mjg59 (subscriber, #23239)
[Link] (4 responses)
Posted Feb 19, 2009 12:10 UTC (Thu)
by khim (subscriber, #9252)
[Link] (3 responses)
Somehow I doubt you can save as much power by using any other approach. XO tried to do this, G1 is doing this - I'm pretty sure it'll be standard approach in some niches for years to come. And why should a single poorly-written application be able to suck your battery dry if system is designed to mostly live in suspended mode? Kernel already is doing things like that. Only there kernel guarantees small amount of time for "normal" process here it gurantees only small amount of work time for any process. Different systems, different requirements...
Posted Feb 19, 2009 12:20 UTC (Thu)
by mjg59 (subscriber, #23239)
[Link] (2 responses)
Posted Feb 24, 2009 18:30 UTC (Tue)
by tbird20d (subscriber, #1901)
[Link] (1 responses)
Posted Feb 24, 2009 18:49 UTC (Tue)
by mjg59 (subscriber, #23239)
[Link]
1) The hardware to support it. That's increasingly the case - multiple vendors provide this kind of functionality.
(1) is entirely out of our control. For hardware that supports low-latency full-system suspend/resume and doesn't support ultra low-power runtime idle modes, we don't have any option - the only solution is some sort of automatic system suspend.
However, I'm going to argue that that's not especially interesting. Hardware that falls into this category is a decreasing proportion of the market. ARM is mostly heading towards supporting sufficiently deep runtime idle. x86 doesn't have sufficiently low-latency suspend/resume for automatic suspend to be practical. Optimising for this scenario is optimising for a dying market segment.
(2) and (3) are interesting because they benefit the entire Linux market, not merely a segment of the embedded market. Enhancing our driver framework allows us to save power in everything from the phone to the server. Ensuring that our software stack doesn't engage in pathological behaviour provides the same benefits.
Concentrating on wakelocks simply ignores the reality that they provide no benefit in most usecases. In the Android case, they're a bandaid to hide inadequacies in other software layers.
Posted Feb 27, 2009 7:51 UTC (Fri)
by efexis (guest, #26355)
[Link]
If we had the man hours to put into the software that would be great, but, it's cheaper to protect against human error (and malace) instead by using software and circuitry. This is often resisted as many feel the desire to Do It Right, but then you get things like probes on Mars deadlocking, and kernel guys going "let's just implement priority inheritance to get it working". I seem to recall Linus being resisant to priority inheritance in the Linux kernel, but eventually an implementation did get in (http://lwn.net/Articles/178253/). Whilst this may not be Doing It Right, it is definitely Doing the Right Thing.
Alex
Posted Apr 29, 2010 3:52 UTC (Thu)
by cventers (guest, #31465)
[Link]
Memory protection is also largely implemented in hardware, and is a fundamental component of how multiple processes can coexist on one computer and still appear to run simultaneously.
That's wayyyyyy different from adding hacks to the kernel to fix broken applications. That reduces kernel quality and encourages app developers to be lazy. It's something Microsoft would do -- add kernel hacks to make Office or Borland work right.
Posted Feb 19, 2009 8:01 UTC (Thu)
by i3839 (guest, #31386)
[Link] (1 responses)
A daemon like that could also implement the timeout behaviour, solving the problem described here:
> User-space applications may simply screw up and forget to allow the
Simple kernel interface + user space daemon seems like a working solution.
Posted Feb 19, 2009 8:24 UTC (Thu)
by alonz (subscriber, #815)
[Link]
It could even use the Android permissions model to decide which
applications get this preferential treatment.
Posted Feb 19, 2009 21:28 UTC (Thu)
by xyzzy (guest, #1984)
[Link] (1 responses)
Posted Feb 21, 2009 18:14 UTC (Sat)
by man_ls (guest, #15091)
[Link]
Besides, there will be other native applications running on the G1, which (even if they under direct control of Goog... ahem, Android) must be written with power management in mind. It seems easier to just push power management to the kernel and let it do its thing.
Posted Mar 4, 2009 10:09 UTC (Wed)
by amit (subscriber, #1274)
[Link]
Of course, in an open mobile world where any app can be installed should be restricted and selinux can help really well here.
From wakelocks to a real solution
> kernel developers, who respond that, rather than coding the kernel to
> protect against poor applications, one should simply fix those
> applications
protection ? If we would trust all userspace applications to be bugfree
and not access memory which is not theirs, there would be no need for
protected memory.
In the same way this protects the system against programs behaving badly
memory-wise, some protection against applications behaving badly
power-consumtion-wise seems like a good thing to me.
From wakelocks to a real solution
From wakelocks to a real solution
I don't buy this argument either
I don't buy this argument either
Why?
Stopping every single userspace process from running is an awfully blunt tool to prevent poorly written apps from spending battery power
Why?
Why?
Why?
2) The OS to support high quality driver power management. That requires paying attention to application requirements and aggressively reducing the power consumption of hardware when those requirements are relaxed.
3) The userspace applications not to use resources unnecessarily, or some way to actively prevent them from being given them.
From wakelocks to a real solution
From wakelocks to a real solution
From wakelocks to a real solution
> policy be encapsulated into a separate daemon which would make the
> decisions on when to keep the system awake.
> system to suspend.
Such a daemon could also allow some applications to hold the phone active
for a longer time—for example, media players or games.
From wakelocks to a real solution
From wakelocks to a real solution
They could, but I would guess the VM is not very smart either -- it does whatever userspace tells it to do. Solving the problem in the not-quite-Java VM would add complexity in a layer which (at least theoretically) is not involved in power management, just in secure execution.
From wakelocks to a real solution
From wakelocks to a real solution