LWN.net Logo

Yet another opportunity for opportunistic suspend

By Jonathan Corbet
October 18, 2011
Your editor was innocently looking at some papers on his desk the other day when his computer abruptly decided to suspend itself. Rawhide is fun in that way; combined with GNOME's delight in forgetting user settings, it can produce no end of surprises to brighten one's working experience. The ability to suspend a desktop system to RAM is actually quite a nice feature, but your editor prefers to have a say in when it happens. Thankfully, GNOME still (so far) allows automatic suspend to be turned off. But it is clear that the suspend-to-RAM functionality is seeing increased use in a number of contexts; it is not just for laptops and Android anymore. Your editor's desktop is not the only place where stakeholders want some control over when the system sleeps and when it needs to stay running.

Indeed, control over automatic suspension of the system is at the core of the debate over Android's opportunistic suspend mechanism. As usage of suspend-to-RAM increases, so does interest in creating a proper mechanism for determining when a suspend can happen. A new patch set from Rafael Wysocki has restarted this discussion and led to, possibly, a surprising conclusion.

Rafael started with the conclusion that "whatever the kernel has to offer in this area is either too complicated to use in practice or inadequate for other reasons." He then went on to propose a new mechanism that, he hoped, would simplify things. It came in two parts:

  • A new sysfs knob, /sys/power/sleep_mode, which provided overall control of the suspend-to-RAM and hibernation functionality. If a suitably-privileged process writes "disabled" to this file, no attempt to suspend or hibernate the system will succeed. It is a sort of high-power wakelock that ensures the system will keep running while important work is being done.

  • Applications wanting to keep the system awake would open a new device, /dev/sleepctl, and execute an ioctl() to that effect. After this call, attempts to suspend the system would block until the application explicitly drops its lock or until a 500ms (by default) timeout period expires. The "stay awake" operation would also be done by the system at resume time to give processes time to perform whatever tasks need to be done.

It is probably safe to say that these patches will not be merged in anything resembling this form. Leading the opposition was Neil Brown, who asserted that the job could be done in user space, and, indeed, should be done that way. According to Neil:

The only sane way to handle suspend is for any (suitably privileged) process to be able to request that suspend doesn't happen, and then for one process to initiate suspend when no-one is blocking it.

Communication with that process, Neil claimed, should be no harder than using Rafael's simplified interface to communicate with the kernel. After a fair amount of discussion, Neil came up with a proposal for how he thinks things should actually work. As one would expect from the above quote, it centers around a single daemon with the responsibility for suspending and resuming the system. A decision to suspend the system is never made by the kernel, and, if everybody is following the rules, by no other user-space process.

The daemon has a pair of modes; it starts in the "on demand" mode where the system will only be suspended after an explicit request to do so. That request could come from the user closing the lid or pressing a button sequence; in this case, the system should suspend in short order regardless of what is happening, and it should not resume without an explicit user action. Suspend can also be requested by a suitably-privileged application; in this case the operation is only carried out if nothing is blocking it, and the system can be automatically resumed at some future time. This mode was also referred to as the "legacy" mode; it needs to be supported but it is not how things are expected to run most of the time.

Other processes in the system can affect suspend behavior by talking to the daemon. One of the things a sufficiently-privileged process can do is to ask the daemon to go into "immediate" mode; in that mode, the system will suspend anytime there is no known reason to stay awake. The immediate mode, thus, closely mirrors the opportunistic suspend mechanism used on Android systems. When the daemon is in immediate mode, it no longer makes sense for any process in the system to ask the system to suspend - the daemon is already prepared to suspend whenever the opportunity arises. So the rest of the interface is concerned with when the system should be awake.

Any process with an interest in suspend and resume events can open a socket to the daemon and request notification anytime a suspend is being contemplated. That process should respond to such notifications with a message saying that it is ready for the suspend to happen; it can, optionally, add a request that the system stay awake for the time being if there is work that must be done. If no processes block the suspend, the system will go to sleep; another message will be sent to all processes once the system resumes.

There is an interesting variant on this mechanism whereby processes can register one or more file descriptors with the daemon. In this case, the daemon will only query the associated processes before suspending if one or more of the given file descriptors is reported as readable by poll(). A readable file descriptor thus functions in a manner similar to a driver-acquired wakelock in the Android system. If a device wakes the system and provides input for a user-space process to read, the daemon will see that the file descriptor is readable and avoid suspending the system until that input has been consumed and acted upon. Meanwhile, processes that clearly have no need to block suspend will not need to wake up and respond to a notification every time a suspend is contemplated.

The daemon also allows processes to request that the system be awake at some future time. A tool like cron can use this feature to, say, wake the system late at night to run a backup.

At a first glance, this approach looks like it should be able to handle the opportunistic suspend problem without the need to add more mechanism to the kernel. But it must be remembered that this is a problem that has defeated a number of initially reasonable-looking solutions. Whether this proposal will fare better - and whether the various desktop and mobile environments will adopt it - remains to be seen.


(Log in to post comments)

Yet another opportunity for opportunistic suspend

Posted Oct 20, 2011 13:57 UTC (Thu) by intgr (subscriber, #39733) [Link]

Not sure if this is useful for smartphones/Android, but I definitely want a "suspend policy" daemon on my desktop and laptop computers.

Session managers already do something similar for screen savers (though that's in user's session scope, not global). Also desktop environment developers have introduced lots of new odd daemons recently, I'm surprised they haven't came up with a suspend policy framework yet.

I've currently set all my computers to suspend after 1 hour of inactivity which works great, but I'd like to block suspend when I'm just using my computer to play music and not otherwise using the computer. Also, I want to suspend my laptop, but not when I have music playing. This seems impossible to do for now and makes opportunistic suspend quite annoying. Also I don't want my laptop suspending when I'm listening to music and just close the lid. I just want the screen to shut off its backlight.

So music players could come with a checkbox "[x] Block suspend while playing music" and my BitTorrent client could have "[x] Block suspend until downloads have completed"

Combined with wake-on-LAN, I see this could even be useful for home servers (e.g. NAS) to save power while everyone is asleep.

Yet another opportunity for opportunistic suspend

Posted Oct 20, 2011 16:30 UTC (Thu) by yokem_55 (subscriber, #10498) [Link]

Apple's Wifi routers and their desktops have a feature called wake-on-demand, that will send the requisite wake-on-lan packet to a sleeping machine when a bonjour published service is accessed. So, in theory your system could sleep until you want to ssh into it, and then the router, detecting the attempted access to the sleeping system, sends then wake on lan packet to the system allowing your ssh connection to proceed. Sadly this feature has been an Apple only thing, and while Apple has the source for it's implementation available (it's deeply tied into Darwin's packet filtering), it hasn't gotten any real attention in the Openwrt and related communities.

Yet another opportunity for opportunistic suspend

Posted Oct 20, 2011 22:23 UTC (Thu) by kirkland (guest, #53307) [Link]

The PowerNap project/package in Ubuntu provides exactly the kind of policy manager you're talking about. It was designed to be sort of a "screensaver for servers, except saving power instead of the screen", but it works just as well on desktop and laptops. I know LWN and its readers love to hate Ubuntu/Canonical/Launchpad, but in case you're curious, see: http://launchpad.net/powernap.

Cheers,
Dustin

Yet another opportunity for opportunistic suspend

Posted Oct 20, 2011 22:42 UTC (Thu) by jake (editor, #205) [Link]

> I know LWN and its readers love to hate Ubuntu/Canonical/Launchpad

wow, sorry you feel that way. I *know* it's not true for LWN, and I don't really think it's true for our readers either. There are some vocal critics without a doubt, but "love to hate" seems a bit over the top to me.

YMMV,

jake

Yet another opportunity for opportunistic suspend

Posted Oct 21, 2011 13:52 UTC (Fri) by kirkland (guest, #53307) [Link]

Okay, Jake, you're right -- my comment was inappropriate. I was in a bit of bad mood when I wrote that. My apologies.

Anyway, a friend linked me to this interesting article about opportunistic suspend and suggested that I link PowerNap in as a pertinent tool. I should have did just that and left the criticism to others. Sorry.

Yet another opportunity for opportunistic suspend

Posted Oct 21, 2011 4:16 UTC (Fri) by Darkmere (subscriber, #53695) [Link]

There is some commentary about that package and the dubious value of how good it actually is. Since we're not seeing enough inflammatory comments about Canonical/Ubuntu/Launchpad on the frontpage, I'll have to post a link here below:

https://plus.google.com/115547683951727699051/posts/eJ2pd...

( For the uninvited, large parts of the first paragraph was snark and sarcasm.)

Yet another opportunity for opportunistic suspend

Posted Oct 20, 2011 22:55 UTC (Thu) by mclasen@redhat.com (subscriber, #31786) [Link]

> Session managers already do something similar for screen savers (though
> that's in user's session scope, not global). Also desktop environment
> developers have introduced lots of new odd daemons recently, I'm surprised
> they haven't came up with a suspend policy framework yet.

The inhibit api of gnome-session lets you do just that:

http://people.gnome.org/~mccann/gnome-session/docs/gnome-...

Not sure what 'odd daemons' you have in mind, but as far as desktops are concerned, the policy belongs in the user session.

Yet another opportunity for opportunistic suspend

Posted Oct 21, 2011 11:48 UTC (Fri) by intgr (subscriber, #39733) [Link]

> The inhibit api of gnome-session lets you do just that
> as far as desktops are concerned, the policy belongs in the user session
Not if this policy affects all users of a computer. The computer shouldn't autosuspend if another user has important applications running that block suspend.

> Not sure what 'odd daemons' you have in mind
If you haven't peeked into your process list lately, here's a few:
colord, upowerd, udisks-daemon, rtkit-daemon, polkitd, accounts-daemon, NetworkManager, modem-manager

Yet another opportunity for opportunistic suspend

Posted Oct 21, 2011 8:02 UTC (Fri) by pabs (subscriber, #43278) [Link]

If your computer/tablet/phone can play music while suspended (probably some ARM devices can do this) then your preferences would mean extra power use.

Yet another opportunity for opportunistic suspend

Posted Oct 27, 2011 16:10 UTC (Thu) by Wol (guest, #4433) [Link]

Equally, they would avoid trashed files ...

I had a load of audacity projects on my Linpus Aspire and did an mv to move them to my desktop.

With hindsight leaving the Aspire on the desk to finish copying was a major mistake - it kept suspending, breaking network links, and I ended up having to repeatedly wake it. What's worse, it didn't move stuff properly, and having started working on those projects on the desktop, I discovered a load of files had been trashed, leaving holes in the audio. Seeing as I'd done an mv, not a cp, recovery was not possible ... :-(

Suspending at the wrong time will trash your data ...

Cheers,
Wol

Yet another opportunity for opportunistic suspend

Posted Oct 27, 2011 17:12 UTC (Thu) by intgr (subscriber, #39733) [Link]

> I had a load of audacity projects on my Linpus Aspire and did an mv to move them to my desktop.

This is a problem with opportunistic suspend. This is a bug in the protocol/file system you used to access the remote machine. A proper one would return the network error back to mv as an I/O error, and mv would abort without deleting the source file.

Copyright © 2011, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds