Skip to main content.
Google custom search

NetBSD Documentation: Power Management

Power Management for Laptops


Power Management for Laptops

Introduction (top)

This documentation describes some of the power management facilities available in NetBSD, including the Advanced Power Management (APM) support found in most modern laptops.

Currently most of the information on this page is i386 specific. When NetBSD starts supporting more laptop architectures, then this documentation will be expanded to cover the power management features for those architectures.

The Basics of Power Management (top)

The core of laptop power management is centered around Advanced Power Management, or APM for short. This is a set of BIOS provided service routines that provide an interface to the laptop hardware power management features. It is via this interface that NetBSD receives APM events (such as suspend requests) or makes power management requests or queries (such as the current battery level).

APM specifies two different power saving modes: standby and suspend. What this actually does at a hardware level is dependent on the particular laptop being used. standby typically means a light sleep, which consumes more power and is quicker to restore from than the suspend mode. Typically the APM timers for standby and suspend mode are configured in a machine's BIOS. After a machine has been determined idle for the appropriate period of time, the BIOS will request that the operating system go into the appropriate power saving mode.

At the NetBSD level, there are three components to APM support. The kernel driver, which is the interface to the APM BIOS, the apm daemon apmd, which interfaces with the APM kernel driver and runs the userland event scripts, and the command line program apm, which interfaces to apmd and can be used to query battery level or initiate a system suspend.

The way power management events generally flow is:

  1. The APM BIOS determines a machine has been idle for the appropriate amount of time, and queues a standby/suspend request.
  2. The APM kernel driver gets the request event the next time it polls the APM BIOS (typically once per second) and queues the standby/suspend request to the apm daemon.
  3. The apm daemon determines whether or not to accept this request, based on the current power source and the daemon configuration. If the request is accepted, it performs any standby/suspend activities, then sends the accept/reject notice back to the kernel driver.
  4. The kernel driver sends the accept/reject notice to the APM BIOS.
  5. If the event request was accepted, the APM BIOS places the hardware in the specified mode.

Configuring NetBSD APM Support (top)

First off, you need to compile a kernel with the APM driver enabled. Add (or uncomment) the following line in your kernel configuration file:

	apm0	at mainbus0	# Advanced power management

See how to build a kernel for details on how to build your own kernels.

Once you have a kernel with the APM kernel driver built, run the apm daemon (apmd) at boot time by setting the apmd variable /etc/rc.conf to YES.

Once you've done both of these steps, you can use the apm utility to interface with the APM system. For example:

% apm
Battery charge state: high
Battery remaining: 98 percent
A/C adapter state: not connected
Power management enabled

Read apm(8) and apmd(8) for more detailed information on how to use the APM support.

Power Management Hints and Tricks (top)

The primary interfaces for power management events are the scripts in /etc/apm: standby, suspend, and resume. apmd will run these scrips when it receives the appropriate APM event.

Some things you might want to do in a standby or suspend script are:

  • Turn off power to a network interface card. If you are using PCMCIA network interface cards, you can do this by ifconfig <interface> down.
  • Unmount network filesystems (if you plan on resuming a laptop in a place where that filesystem might not be accessible).
  • Stop running dhclient or pppd.

Conversely, you can run all the appropriate commands to undo all of the things you did at standby or suspend time in your resume script.

New in NetBSD 1.4 is the atactl command. This command can be used to control power management features of ATA devices (more commonly known as IDE). You can use the setidle option of atactl to set the standby timer used to control disk spindown. See atactl(8) for more specific information.

If you're wondering what value to set for the ATA standby timer, I would recommend reading the following papers:

  • A Quantitative Analysis of Disk Drive Power Management in Portable Computers, Kester Li, Roger Kumpf, Paul Horton, Thomas Anderson, Computer Science Division, University of California, Berkeley, Winter 1994 Usenix.
  • Thwarting the Power-Hungry Disk, Fred Douglas, P. Krishnan, Brian Marsh, Matsushita Information Technology Laboratory, Winter 1994 Usenix.

These papers both recommend low values for a standby timer, on the order of 2 to 8 seconds.

One problem with setting a low value for the standby timer is that by default NetBSD writes to the disk often enough that the disk is constantly spinning up (at least once every 30 seconds, or however often update runs). It turns out the most common cause of this is the filesystem updating the last access timestamps on files and the last modified time on device special files (such as pseudo-ttys). This behavior can be suppressed by the mount(8) options noatime and (new in NetBSD 1.4) nodevmtime. If you decide to do this as well, you might want to also change or remove completely the atrun job inside of cron - by default it will run once every 10 minutes and append an entry to the cron log file, causing a disk spinup.

It is also desirable in some circumstances to use different or disable completely power management settings depending if one is powered by line power or battery. A new feature in NetBSD 1.4 is two additional apmd scripts: line and battery. These scripts are run by apmd whenever APM detects a power source change. The line script is run upon transition to line/AC power, and the battery script is run upon transition to battery power. The script corresponding to the current power source is also run when apmd is first started.

You can place commands inside of these scripts to change the power management settings when using battery or line power. Here are some example scripts:

  • /etc/apm/line
    #!/bin/sh
    
    mount -u /
    mount -u /usr
    atactl wd0 setidle 0
  • /etc/apm/battery
    #!/bin/sh
    
    mount -u -o noatime,nodevmtime /
    mount -u -o noatime /usr
    atactl wd0 setidle 5

See also /usr/share/examples/apm/script for an example script that can be used to also handles configuration of network interfaces etc.

Another useful feature is the -a flag to apmd which will cause apmd to ignore standby or suspend events if the machine is currently on line power.

Squid is making my laptop disk spin up (top)

Squid probes one of its cache directories every 15 seconds. With 16 first level and 256 second level directories being used, as default, to construct the cache, some quick math suggests:

16 * 256 * 8k(block size) = ~32mb

Squid will slowly work its way through this, each time a new directory is read that it missed in the buffer cache. Tweaking squid.conf to reduce the number of directories should fix the problem of a laptop disk spinning up.

Preventing syslog from waking up the disk (top)

A problem when trying to spin the disk down is that it gets woken up periodically e.g. by syslogd(8) which tries to write things to disk.

An elegant way to work around that is by redirecting all syslog output to a virtual console instead of to disk. To do so, put the following into /etc/syslog.conf:

*.*		/dev/ttyE3
	

Of course you can choose whatever virtual console you prefer instead of /dev/ttyE3.


Back to NetBSD Documentation