You are not logged in.

#1 2004-07-17 22:29:23

Registered: 2004-04-12
Posts: 72

Missing Documentation for kernel26/laptop_mode.txt

Since the kernel26 package is stripped of all documentation, I can't get the laptop_mode script for 2.6.7. Do you compile your own kernel? Could you post the laptop_mode.txt on this thread please?


#2 2004-07-17 22:34:55

Registered: 2004-02-04
Posts: 1,185

Re: Missing Documentation for kernel26/laptop_mode.txt

How to conserve battery power using laptop-mode

Document Author: Bart Samwel (bart at
Date created: January 2, 2004
Last modified: April 3, 2004


Laptopmode is used to minimize the time that the hard disk needs to be spun up,
to conserve battery power on laptops. It has been reported to cause significant
power savings.


* Introduction
* The short story
* Caveats
* The details
* Tips & Tricks
* Control script
* ACPI integration
* Monitoring tool

The short story

To use laptop mode, you don't need to set any kernel configuration options
or anything. You simply need to run the laptop_mode control script (which
is included in this document) as follows:

# laptop_mode start

Then set your harddisk spindown time to a relatively low value with hdparm:

hdparm -S 4 /dev/hda

The value -S 4 means 20 seconds idle time before spindown. Your harddisk will
now only spin up when a disk cache miss occurs, or at least once every 10
minutes to write back any pending changes.

To stop laptop_mode, run "laptop_mode stop".


* The downside of laptop mode is that you have a chance of losing up
  to 10 minutes of work. If you cannot afford this, don't use it! It's
  wise to turn OFF laptop mode when you're almost out of battery --
  although this will make the battery run out faster, at least you'll
  lose less work when it actually runs out. I'm still looking for someone
  to submit instructions on how to turn off laptop mode when battery is low,
  e.g., using ACPI events. I don't have a laptop myself, so if you do and
  you care to contribute such instructions, please do.

* Most desktop hard drives have a very limited lifetime measured in spindown
  cycles, typically about 50.000 times (it's usually listed on the spec sheet).
  Check your drive's rating, and don't wear down your drive's lifetime if you
  don't need to.

* If you mount some of your ext3/reiserfs filesystems with the -n option, then
  the control script will not be able to remount them correctly. You must set
  DO_REMOUNTS=0 in the control script, otherwise it will remount them with the
  wrong options -- or it will fail because it cannot write to /etc/mtab.

* If you have your filesystems listed as type "auto" in fstab, like I did, then
  the control script will not recognize them as filesystems that need remounting.

* It has been reported that some versions of the mutt mail client use file access
  times to determine whether a folder contains new mail. If you use mutt and
  experience this, you must disable the noatime remounting in the control script
  by setting DO_REMOUNT_NOATIME=0.

The details

Laptop-mode is controlled by the flag /proc/sys/vm/laptop_mode. This flag is
present for all kernels that have the laptop mode patch, regardless of any
configuration options. When the flag is set, any physical disk read operation
(that might have caused the hard disk to spin up) causes Linux to flush all dirty
blocks. The result of this is that after a disk has spun down, it will not be spun
up anymore to write dirty blocks, because those blocks had already been written
immediately after the most recent read operation.

To increase the effectiveness of the laptop_mode strategy, the laptop_mode
control script increases dirty_expire_centisecs and dirty_writeback_centisecs in
/proc/sys/vm to about 10 minutes (by default), which means that pages that are
dirtied are not forced to be written to disk as often. The control script also
changes the dirty background ratio, so that background writeback of dirty pages
is not done anymore. Combined with a higher commit value (also 10 minutes) for
ext3 or ReiserFS filesystems (also done automatically by the control script),
this results in concentration of disk activity in a small time interval which
occurs only once every 10 minutes, or whenever the disk is forced to spin up by
a cache miss. The disk can then be spun down in the periods of inactivity.

If you want to find out which process caused the disk to spin up, you can
gather information by setting the flag /proc/sys/vm/block_dump. When this flag
is set, Linux reports all disk read and write operations that take place, and
all block dirtyings done to files. This makes it possible to debug why a disk
needs to spin up, and to increase battery life even more. The output of
block_dump is written to the kernel output, and it can be retrieved using
"dmesg". When you use block_dump, you may want to turn off klogd, otherwise
the output of block_dump will be logged, causing disk activity that is not
normally there.

If 10 minutes is too much or too little downtime for you, you can configure
this downtime as follows. In the control script, set the MAX_AGE value to the
maximum number of seconds of disk downtime that you would like. You should
then set your filesystem's commit interval to the same value. The dirty ratio
is also configurable from the control script.

If you don't like the idea of the control script remounting your filesystems
for you, you can change DO_REMOUNTS to 0 in the script.

Thanks to Kiko Piris, the control script can be used to enable laptop mode on
both the Linux 2.4 and 2.6 series.

Tips & Tricks

* Bartek Kania reports getting up to 50 minutes of extra battery life (on top
  of his regular 3 to 3.5 hours) using very aggressive power management (hdparm
  -B1) and a spindown time of 5 seconds (hdparm -S1).

* You can spin down the disk while playing MP3, by setting the disk readahead
  to 8MB (hdparm -a 16384). Effectively, the disk will read a complete MP3 at
  once, and will then spin down while the MP3 is playing. (Thanks to Bartek

* Drew Scott Daniels observed: "I don't know why, but when I decrease the number
  of colours that my display uses it consumes less battery power. I've seen
  this on powerbooks too. I hope that this is a piece of information that
  might be useful to the Laptop Mode patch or it's users."

* One thing which will cause disks to spin up is not-present application
  and dynamic library text pages.  The kernel will load program text off disk
  on-demand, so each time you invoke an application feature for the first
  time, the kernel needs to spin the disk up to go and fetch that part of the

  So it is useful to increase the disk readahead parameter greatly, so that
  the kernel will pull all of the executable's pages into memory on the first

  The supplied script does this.

* In syslog.conf, you can prefix entries with a dash ``-'' to omit syncing the
  file after every logging. When you're using laptop-mode and your disk doesn't
  spin down, this is a likely culprit.

* Richard Atterer observed that laptop mode does not work well with noflushd
  (, it seems that noflushd prevents laptop-mode
  from doing its thing.

Control script

Please note that this control script works for the Linux 2.4 and 2.6 series.

--------------------CONTROL SCRIPT BEGIN------------------------------------------

# start or stop laptop_mode, best run by a power management daemon when
# ac gets connected/disconnected from a laptop
# install as /sbin/laptop_mode
# Contributors to this script:   Kiko Piris
#                 Bart Samwel
#                 Micha Feigin
#                 Andrew Morton
#                 Herve Eychenne
#                 Dax Kelson
# Original Linux 2.4 version by: Jens Axboe


# Age time, in seconds. should be put into a sysconfig file

# Read-ahead, in kilobytes

# Shall we remount journaled fs. with appropiate commit interval? (1=yes)

# And shall we add the "noatime" option to that as well? (1=yes)

# Dirty synchronous ratio.  At this percentage of dirty pages the process which
# calls write() does its own writeback

# Allowed dirty background ratio, in percent.  Once DIRTY_RATIO has been
# exceeded, the kernel will wake pdflush which will then reduce the amount
# of dirty memory to dirty_background_ratio.  Set this nice and low, so once
# some writeout has commenced, we do a lot of it.

# kernel default dirty buffer age

# This must be adjusted manually to the value of HZ in the running kernel
# on 2.4, until the XFS people change their 2.4 external interfaces to work in
# centisecs. This can be automated, but it's a work in progress that still needs
# some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for external
# interfaces, and that is currently always set to 100. So you don't need to
# change this on 2.6.


KLEVEL="$(uname -r |
           IFS='.' read a b c
           echo $a.$b
case "$KLEVEL" in
        echo "Unhandled kernel version: $KLEVEL ('uname -r' = '$(uname -r)')" >&2
        exit 1

if [ ! -e /proc/sys/vm/laptop_mode ] ; then
    echo "Kernel is not patched with laptop_mode patch." >&2
    exit 1

if [ ! -w /proc/sys/vm/laptop_mode ] ; then
    echo "You do not have enough privileges to enable laptop_mode." >&2
    exit 1

# Remove an option (the first parameter) of the form option=<number> from
# a mount options string (the rest of the parameters).
parse_mount_opts () {
    echo ",$*," | sed       
     -e 's/,'"$OPT"'=[0-9]*,/,/g'   
     -e 's/,,*/,/g'           
     -e 's/^,//'           
     -e 's/,$//'

# Remove an option (the first parameter) without any arguments from
# a mount option string (the rest of the parameters).
parse_nonumber_mount_opts () {
    echo ",$*," | sed       
     -e 's/,'"$OPT"',/,/g'       
     -e 's/,,*/,/g'           
     -e 's/^,//'           
     -e 's/,$//'

# Find out the state of a yes/no option (e.g. "atime"/"noatime") in
# fstab for a given filesystem, and use this state to replace the
# value of the option in another mount options string. The device
# is the first argument, the option name the second, and the default
# value the third. The remainder is the mount options string.
# Example:
# parse_yesno_opts_wfstab /dev/hda1 atime atime defaults,noatime
# If fstab contains, say, "rw" for this filesystem, then the result
# will be "defaults,atime".
parse_yesno_opts_wfstab () {
    shift 3
    PARSEDOPTS1="$(parse_nonumber_mount_opts $OPT $L_OPTS)"
    PARSEDOPTS1="$(parse_nonumber_mount_opts no$OPT $PARSEDOPTS1)"
    # Watch for a default atime in fstab
    FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
    if echo "$FSTAB_OPTS" | grep "$OPT" > /dev/null ; then
        # option specified in fstab: extract the value and use it
        if echo "$FSTAB_OPTS" | grep "no$OPT" > /dev/null ; then
            echo "$PARSEDOPTS1,no$OPT"
            # no$OPT not found -- so we must have $OPT.
            echo "$PARSEDOPTS1,$OPT"
        # option not specified in fstab -- choose the default.
        echo "$PARSEDOPTS1,$DEF_OPT"

# Find out the state of a numbered option (e.g. "commit=NNN") in
# fstab for a given filesystem, and use this state to replace the
# value of the option in another mount options string. The device
# is the first argument, and the option name the second. The
# remainder is the mount options string in which the replacement
# must be done.
# Example:
# parse_mount_opts_wfstab /dev/hda1 commit defaults,commit=7
# If fstab contains, say, "commit=3,rw" for this filesystem, then the
# result will be "rw,commit=3".
parse_mount_opts_wfstab () {
    shift 2
    PARSEDOPTS1="$(parse_mount_opts $OPT $L_OPTS)"
    # Watch for a default commit in fstab
    FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
    if echo "$FSTAB_OPTS" | grep "$OPT=" > /dev/null ; then
        # option specified in fstab: extract the value, and use it
        echo -n "$PARSEDOPTS1,$OPT="
        echo ",$FSTAB_OPTS," | sed
         -e 's/.*,'"$OPT"'=//'   
         -e 's/,.*//'
        # option not specified in fstab: set it to 0
        echo "$PARSEDOPTS1,$OPT=0"

if [ $DO_REMOUNT_NOATIME -eq 1 ] ; then

case "$1" in
        echo -n "Starting laptop_mode"

        if [ -d /proc/sys/vm/pagebuf ] ; then
            # (For 2.4 and early 2.6.)
            # This only needs to be set, not reset -- it is only used when
            # laptop mode is enabled.
            echo $XFS_AGE > /proc/sys/vm/pagebuf/lm_flush_age
            echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
        elif [ -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
            # (A couple of early 2.6 laptop mode patches had these.)
            # The same goes for these.
            echo $XFS_AGE > /proc/sys/fs/xfs/lm_age_buffer
            echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
        elif [ -f /proc/sys/fs/xfs/age_buffer ] ; then
            # (2.6.6)
            # But not for these -- they are also used in normal
            # operation.
            echo $XFS_AGE > /proc/sys/fs/xfs/age_buffer
            echo $XFS_AGE > /proc/sys/fs/xfs/sync_interval
        elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
            # (2.6.7 upwards)
            # And not for these either. These are in centisecs,
            # not USER_HZ, so we have to use $AGE, not $XFS_AGE.
            echo $AGE > /proc/sys/fs/xfs/age_buffer_centisecs
            echo $AGE > /proc/sys/fs/xfs/xfssyncd_centisecs
            echo 3000 > /proc/sys/fs/xfs/xfsbufd_centisecs

        case "$KLEVEL" in
                echo 1                    > /proc/sys/vm/laptop_mode
                echo "30 500 0 0 $AGE $AGE 60 20 0"    > /proc/sys/vm/bdflush
                echo 5                    > /proc/sys/vm/laptop_mode
                echo "$AGE"                > /proc/sys/vm/dirty_writeback_centisecs
                echo "$AGE"                > /proc/sys/vm/dirty_expire_centisecs
                echo "$DIRTY_RATIO"            > /proc/sys/vm/dirty_ratio
                echo "$DIRTY_BACKGROUND_RATIO"        > /proc/sys/vm/dirty_background_ratio
        if [ $DO_REMOUNTS -eq 1 ]; then
            cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
                PARSEDOPTS="$(parse_mount_opts "$OPTS")"
                case "$FST" in
                        PARSEDOPTS="$(parse_mount_opts commit "$OPTS")"
                        mount $DEV -t $FST $MP -o remount,$PARSEDOPTS,commit=$MAX_AGE$NOATIME_OPT
                        mount $DEV -t $FST $MP -o remount,$OPTS$NOATIME_OPT
                if [ -b $DEV ] ; then
                    blockdev --setra $(($READAHEAD * 2)) $DEV
        echo "."
        echo -n "Stopping laptop_mode"
        echo 0 > /proc/sys/vm/laptop_mode
        if [ -f /proc/sys/fs/xfs/age_buffer -a ! -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
            # These need to be restored, if there are no lm_*.
            echo $(($XFS_HZ*$DEF_XFS_AGE_BUFFER))         > /proc/sys/fs/xfs/age_buffer
            echo $(($XFS_HZ*$DEF_XFS_SYNC_INTERVAL))     > /proc/sys/fs/xfs/sync_interval
        elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
            # These need to be restored as well.
            echo $((100*$DEF_XFS_AGE_BUFFER))    > /proc/sys/fs/xfs/age_buffer_centisecs
            echo $((100*$DEF_XFS_SYNC_INTERVAL))    > /proc/sys/fs/xfs/xfssyncd_centisecs
            echo $((100*$DEF_XFS_BUFD_INTERVAL))    > /proc/sys/fs/xfs/xfsbufd_centisecs
        case "$KLEVEL" in
                echo "30 500 0 0 $U_AGE $B_AGE 60 20 0"    > /proc/sys/vm/bdflush
                echo "$U_AGE"                > /proc/sys/vm/dirty_writeback_centisecs
                echo "$B_AGE"                > /proc/sys/vm/dirty_expire_centisecs
                echo "$DEF_DIRTY_RATIO"            > /proc/sys/vm/dirty_ratio
                echo "$DEF_DIRTY_BACKGROUND_RATIO"    > /proc/sys/vm/dirty_background_ratio
        if [ $DO_REMOUNTS -eq 1 ] ; then
            cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
                # Reset commit and atime options to defaults.
                case "$FST" in
                        PARSEDOPTS="$(parse_mount_opts_wfstab $DEV commit $OPTS)"
                        PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $PARSEDOPTS)"
                        mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
                        PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $OPTS)"
                        mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
                if [ -b $DEV ] ; then
                    blockdev --setra 256 $DEV
        echo "."
        echo "Usage: $0 {start|stop}" 2>&1
        exit 1


exit 0
--------------------CONTROL SCRIPT END--------------------------------------------

ACPI integration

Dax Kelson submitted this so that the ACPI acpid daemon will
kick off the laptop_mode script and run hdparm.

---------------------------/etc/acpi/events/ac_adapter BEGIN-------------------------------------------
---------------------------/etc/acpi/events/ac_adapter END-------------------------------------------

---------------------------/etc/acpi/actions/ BEGIN-------------------------------------------

# cpu throttling
# cat /proc/acpi/processor/CPU0/throttling for more info

# spindown time for HD (man hdparm for valid values)
# I prefer 2 hours for acad and 20 seconds for batt

# ac/battery event handler

status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/AC/state`

case $status in
                echo "Setting HD spindown for AC mode."
                /sbin/laptop_mode stop
                /sbin/hdparm -S $ACAD_HD /dev/hda > /dev/null 2>&1
                /sbin/hdparm -B 255 /dev/hda > /dev/null 2>&1
                #echo -n $ACAD_CPU:$ACAD_THR > /proc/acpi/processor/CPU0/limit
                exit 0
                echo "Setting HD spindown for battery mode."
                /sbin/laptop_mode start
                /sbin/hdparm -S $BATT_HD /dev/hda > /dev/null 2>&1
                /sbin/hdparm -B 1 /dev/hda > /dev/null 2>&1
                #echo -n $BATT_CPU:$BATT_THR > /proc/acpi/processor/CPU0/limit
                exit 0
---------------------------/etc/acpi/actions/ END-------------------------------------------

Monitoring tool

Bartek Kania submitted this, it can be used to measure how much time your disk
spends spun up/down.

---------------------------dslm.c BEGIN-------------------------------------------
* Simple Disk Sleep Monitor
*  by Bartek Kania
* Licenced under the GPL
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>

#ifdef DEBUG
#define D(x) x
#define D(x)

int endit = 0;

/* Check if the disk is in powersave-mode
* Most of the code is stolen from hdparm.
* 1 = active, 0 = standby/sleep, -1 = unknown */
int check_powermode(int fd)
    unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
    int state;

    if (ioctl(fd, HDIO_DRIVE_CMD, &args)
    && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
    && ioctl(fd, HDIO_DRIVE_CMD, &args)) {
    if (errno != EIO || args[0] != 0 || args[1] != 0) {
        state = -1; /* "unknown"; */
    } else
        state = 0; /* "sleeping"; */
    } else {
    state = (args[2] == 255) ? 1 : 0;
    D(printf(" drive state is:  %dn", state));

    return state;

char *state_name(int i)
    if (i == -1) return "unknown";
    if (i == 0) return "sleeping";
    if (i == 1) return "active";

    return "internal error";

char *myctime(time_t time)
    char *ts = ctime(&time);
    ts[strlen(ts) - 1] = 0;

    return ts;

void measure(int fd)
    time_t start_time;
    int last_state;
    time_t last_time;
    int curr_state;
    time_t curr_time = 0;
    time_t time_diff;
    time_t active_time = 0;
    time_t sleep_time = 0;
    time_t unknown_time = 0;
    time_t total_time = 0;
    int changes = 0;
    float tmp;

    printf("Starting measurementsn");

    last_state = check_powermode(fd);
    start_time = last_time = time(0);
    printf("  System is in state %snn", state_name(last_state));

    while(!endit) {
    curr_state = check_powermode(fd);

    if (curr_state != last_state || endit) {
        curr_time = time(0);
        time_diff = curr_time - last_time;

        if (last_state == 1) active_time += time_diff;
        else if (last_state == 0) sleep_time += time_diff;
        else unknown_time += time_diff;

        last_state = curr_state;
        last_time = curr_time;

        printf("%s: State-change to %sn", myctime(curr_time),
    changes--; /* Compensate for SIGINT */

    total_time = time(0) - start_time;
    printf("nTotal running time:  %lusn", curr_time - start_time);
    printf(" State changed %d timesn", changes);

    tmp = (float)sleep_time / (float)total_time * 100;
    printf(" Time in sleep state:   %lus (%.2f%%)n", sleep_time, tmp);
    tmp = (float)active_time / (float)total_time * 100;
    printf(" Time in active state:  %lus (%.2f%%)n", active_time, tmp);

    tmp = (float)unknown_time / (float)total_time * 100;
    printf(" Time in unknown state: %lus (%.2f%%)n", unknown_time, tmp);

void ender(int s)
    endit = 1;

void usage()
    puts("usage: dslm [-w <time>] <disk>");

int main(int ac, char **av)
    int fd;
    char *disk = 0;
    int settle_time = 60;

    /* Parse the simple command-line */
    if (ac == 2)
    disk = av[1];
    else if (ac == 4) {
    settle_time = atoi(av[2]);
    disk = av[3];
    } else

    if (!(fd = open(disk, O_RDONLY|O_NONBLOCK))) {
    printf("Can't open %s, because: %sn", disk, strerror(errno));

    if (settle_time) {
    printf("Waiting %d seconds for the system to settle down to "
           "'normal'n", settle_time);
    } else
    puts("Not waiting for system to settle down");

    signal(SIGINT, ender);



    return 0;
---------------------------dslm.c END---------------------------------------------

I edited it slightly, changed the @ to "at" and made the ### lines shorter. it's also from 2.6.8-rc1, but according to the file it didn't change since April anyway.


#3 2004-07-17 23:23:29

Registered: 2004-04-12
Posts: 72

Re: Missing Documentation for kernel26/laptop_mode.txt

nice, thanks i3839.

I wonder how I can get this without having to bother you every time a new kernel comes out? (I did check google, so I'm not being lazy- its not on the laptop_mode maintainers site, or at Since it is documentation, maybe I should bug Dusty  tongue  Just kidding Dusty)


#4 2004-07-20 15:57:29

Registered: 2004-02-04
Posts: 1,185

Re: Missing Documentation for kernel26/laptop_mode.txt

Well, I would just keep the kernel source on hd and download the patches to upgrade to a newer kernel when one comes out, then you don't have to download 30 mb each time, but just 2 or less.

Can also read it online, e.g. from here and here.


#5 2004-07-20 19:19:28

Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986

Re: Missing Documentation for kernel26/laptop_mode.txt

Not much sense in documenting stuff that's already been documented. I could put a link to it in the WHERETO, however, but the links seem to be updated as often as the source. wink



Board footer

Powered by FluxBB