Suspend and resume: Difference between revisions

From OLPC
Jump to navigation Jump to search
(A script to force manual suspensions)
(Need to emphasize the Ctrl-C of script)
Line 59: Line 59:
Manual suspensions should in general not be necessary in ebook mode. If the book is not being looked at, it should be closed. Using the power button to suspend an ebook and then stuff it into a backpack is likely to wake it up from having the power button jostled. The main additional power saving available in manual suspend is from turning off the screen and the backlight.
Manual suspensions should in general not be necessary in ebook mode. If the book is not being looked at, it should be closed. Using the power button to suspend an ebook and then stuff it into a backpack is likely to wake it up from having the power button jostled. The main additional power saving available in manual suspend is from turning off the screen and the backlight.


For now, one trick to do manual suspensions is to use the Terminal to run a saved script like the following. Run it before closing/walking away from your XO, and press Ctrl-C a few times to wake back up. Warning - don't just press alt-tab or a zoom key while it's suspended like this, the suspend will keep trying to trigger. Oh, and don't forget to "chmod +x mysuspend" and "./mysuspend" to use it!
For now, one trick to do manual suspensions is to use the Terminal to run a saved script like the following. Run it before closing/walking away from your XO, and press '''Ctrl-C''' a few times to wake back up. Warning - don't just press alt-tab or a zoom key while it's suspended like this, the suspend will keep trying to trigger. Oh, and don't forget to "chmod +x mysuspend" and "./mysuspend" to use it!


<!--Need to loop because the wifi will break the CPU snooze. Five seconds seems like a reasonable time window to get back to terminal to correct things if you forget to quit the script. This could be better done in a Python "Snooze" activity, which would give up on any key, or loss of focus, but NOT the usual gang of kernel events. Down with dead batteries! -->
<!--Need to loop because the wifi will break the CPU snooze. Five seconds seems like a reasonable time window to get back to terminal to correct things if you forget to quit the script. This could be better done in a Python "Snooze" activity, which would give up on any key, or loss of focus, but NOT the usual gang of kernel events. Down with dead batteries! -->

Revision as of 04:03, 28 December 2007

The XO can suspend and resume with little effort; as there are no moving parts to spin down, total time to resume can possibly be brought down to below 100ms. Currently it takes about 900-1000 ms in build 623. Short suspensions would allow for suspension between keystrokes for slow applications, and for aggressive suspend modes that conserve significant amounts of power.

JG says the time is dominated by spewing debugging messages out the serial port, and that when we last checked by turning them off, we were somewhere in the 200ms range. Merely outputting them to the LCD console is no faster, unfortunately; they actually have to be removed or suppressed or something.

There are also some substantial delays associated with powering-up the USB bus after suspending. The gen2 laptop should NOT attach its network via USB -- or at least not via an externally visible, powered, USB bus that gets turned off during suspend.

To modify a B2 to do a partial job of suspending, see B2 Suspend ECR. Further fixes were made between B4 and C2.

Categories of Suspensions

There's a fair bit of confusion about suspend and resume. There are many different power-saving features, some of which are suspend/resume and some of which are not. There are different kinds of suspends. This page is an effort to bring some clarity to the subject.

The two fundamental categories of suspends are 'manual' and 'automatic'. Manual suspends are done on specific command by the user, e.g. by closing the laptop lid. Such suspends should not be resumed without manual action by the user (e.g. opening the lid). Automatic suspends are done by the laptop on its own initiative, without input from or consultation with the user. These should be transparent to the user -- the user should not be able to tell whether the laptop is suspended or not. They should be automatically resumed whenever the laptop needs to do something.

OS code in build 624 produces a suspension when the power button is pressed, that has elements of both a manual and an automatic suspend. The two kinds of suspends should not be confused; but numerous bugs in suspend are reported because of this confusion. E.g. manual suspends are waking up when a packet arrives, when a key is hit, or when the battery charge changes. That's useful for automated testing, but not for end users. From a programming point of view, people talk about it being troublesome to "wake the CPU for some event (like a battery charge update, or a switch from ebook mode to laptop-closed) and then have it immediately go back to sleep". Automatic suspends make such things trivial.

Automatic Suspensions

The laptop should suspend automatically whenever the CPU and DMA activity is expected to be idle for a significant period of time. This is strictly a power-saving measure, and should not result in any user-visible changes to the screen or peripherals. Anything and everything that requires CPU attention should cause it to power-up immediately. Before suspending, the system will set a timer to ensure that it wakes up before the next scheduled event in the kernel or any user process.

Unfortunately, the USB bus must be powered down when the CPU is; and the network chip is attached to USB. Thus the chip has a separate "wakeup" wire to ask the system to resume when the network chip needs service. Another impact is that the system can't suspend when the USB bus is in use by an external device (unless it's a USB mass storage device and has been fully allowed to write any cached info and quiesce itself).

A further complication is that the USB implementation constantly does DMA, to poll the plugged-in devices, whether or not any real user visible I/O activity is happening. Thus, the decision to enter an automatic suspension must ignore the DMA caused by the USB idle polling, as well as ignoring the presence of the network chip on the USB bus, in order to successfully decide to suspend.

Because DMA must be inactive when the CPU is powered down (there's no bus arbiter), the CPU will not be able to suspend whenever audio input or output is occurring; nor when the video camera is in use. Pending reads and writes to flash, SD, or network will all prevent automatic suspend.

Automatic suspension may eventually be implemented with a new Linux kernel infrastructure called "cpuidle", which allows the CPU to decide what to do when it becomes idle. But until that works, it is being handled by the OHM daemon, which watches idle times in the X server and perhaps on ttys, and asks the kernel to suspend.

Manual Suspensions

Once automatic suspensions are working, the only time that end-users would normally want to explicitly suspend the laptop is by closing the lid. It's conceivable that scenarios could be invented, such as when a USB device is plugged in and automatic suspend won't work, when the user might want to specifically suspend the laptop. But they can do so by closing the lid. The laptop should only resume when the lid is opened (or perhaps to do an automatic clean shutdown and power-off when the battery is about to go dead). Pressing keys, moving the mouse, network packets should all be ignored (keys and mouse motions are ghosts, possibly from jostling or dropping the laptop). Indeed, the keyboard, mouse, USB, SD card, camera, and network should all be put into maximum power-saving mode (ideally with all power removed) when a manual suspension happens.

Pressing the power button could cause a different kind of suspend, if one is needed (either for testing, or by end users). Or the power button could be reclaimed for other uses. Laptops are going to live a large part of their lives closed, so that kind of suspend must work. Having only one kind of user-visible suspend would eliminate user confusion.

Perhaps the power button should be repurposed for "suspend to disk" (i.e. suspend to flash), which would allow the system to go into a state that consumes really tiny amounts of power (for only the EC), without requiring a reboot.

Manual suspensions should in general not be necessary in ebook mode. If the book is not being looked at, it should be closed. Using the power button to suspend an ebook and then stuff it into a backpack is likely to wake it up from having the power button jostled. The main additional power saving available in manual suspend is from turning off the screen and the backlight.

For now, one trick to do manual suspensions is to use the Terminal to run a saved script like the following. Run it before closing/walking away from your XO, and press Ctrl-C a few times to wake back up. Warning - don't just press alt-tab or a zoom key while it's suspended like this, the suspend will keep trying to trigger. Oh, and don't forget to "chmod +x mysuspend" and "./mysuspend" to use it!

#!/bin/sh
xset dpms force standby
while sleep 5 ;do
    su -c "echo mem > /sys/power/state"
done

Testing Suspensions

The most important tool for testing suspension is a setup for monitoring the power consumption of the laptop. The "Tinderbox" was designed to measure power consumption, and various measurements have been taken with it, but there's no automated (e.g. nightly) testing that can tell us whether our software, as it evolves, is burning more or less power than it did yesterday.

Richard Smith suggests examining the couloumb counter in the battery to determine how much energy is used over time. (cat /sys/devices/platform/olpc-battery.0/power_supply/olpc-battery/accum_current; it is in bizarre units.) That's a great resource, but requires that the tests be run with AC power off. This is easy for manual testing, but might require additional infrastructure in an automated testbed.

Kernel Support For Suspend

This sysfs file lets user programs control whether the system wakes up from WiFi events:

 echo 3 > /sys/module/usb8xxx/parameters/wakeup

for wake on bcast and unicast, for example. The value is the sum of 1 for broadcast, 2 for unicast, 4 for "mac event". It was added to the kernel on 13 Nov 2007.

Other Non-Suspend Power Saving Strategies

When the notebook is in ebook mode (and the keyboard and mouse are covered up), those peripherals should be unpowered, or should be put into their lowest possible power consumption.

The SD interface may require special consideration. The most common case is that there is nothing in the slot; this should not block automatic suspension, and indeed an empty slot should be almost completely powered down at all times, whether the CPU is suspended or not. Ideally the system should be able to detect an inserted card, but should spend almost zero power until a card is inserted. I do not yet know if the hardware is capable of this. If it isn't, perhaps inserting an SD card should require a specific software or hardware action by the user (e.g. pressing the power button, or clicking on something in the Journal).

The next most common case of an inserted but largely unused SD/MMC memory card can eventually be handled by letting it write its cache, quiesce, then silently powering it down. Resume its power later, whenever the software needs to access it. For increased safety of its file system integrity, the file system should probably be sync'd or quietly internally dismounted before powering down the slit (and quietly remounted at the first access; /proc/mounts would continue to show it as mounted throughout, but the file system structures on the media would be synchronized as if dismounted).

The same strategy would be fruitful for USB mass storage devices. They need not burn power forever when plugged in and used briefly.

USB non-mass-storage devices, such as network interfaces, serial ports, keyboards, mice, etc, may sit idle for a long time "unused" by the CPU and user software, yet have the potential to interrupt at any time from an external event. All of these devices would prevent automatic suspensions, and thus they cause battery life to drop dramatically. It may be useful to have a user-settable control which would allow such devices to be powered down when not in active use. If the user set this control, they'd be overriding the system's "safe" default decision to always keep the device powered on (and thus to avoid all automatic suspensions).