Back to home page

OSCL-LXR

 
 

    


0001 ===============================================
0002 How to conserve battery power using laptop-mode
0003 ===============================================
0004 
0005 Document Author: Bart Samwel (bart@samwel.tk)
0006 
0007 Date created: January 2, 2004
0008 
0009 Last modified: December 06, 2004
0010 
0011 Introduction
0012 ------------
0013 
0014 Laptop mode is used to minimize the time that the hard disk needs to be spun up,
0015 to conserve battery power on laptops. It has been reported to cause significant
0016 power savings.
0017 
0018 .. Contents
0019 
0020    * Introduction
0021    * Installation
0022    * Caveats
0023    * The Details
0024    * Tips & Tricks
0025    * Control script
0026    * ACPI integration
0027    * Monitoring tool
0028 
0029 
0030 Installation
0031 ------------
0032 
0033 To use laptop mode, you don't need to set any kernel configuration options
0034 or anything. Simply install all the files included in this document, and
0035 laptop mode will automatically be started when you're on battery. For
0036 your convenience, a tarball containing an installer can be downloaded at:
0037 
0038         http://www.samwel.tk/laptop_mode/laptop_mode/
0039 
0040 To configure laptop mode, you need to edit the configuration file, which is
0041 located in /etc/default/laptop-mode on Debian-based systems, or in
0042 /etc/sysconfig/laptop-mode on other systems.
0043 
0044 Unfortunately, automatic enabling of laptop mode does not work for
0045 laptops that don't have ACPI. On those laptops, you need to start laptop
0046 mode manually. To start laptop mode, run "laptop_mode start", and to
0047 stop it, run "laptop_mode stop". (Note: The laptop mode tools package now
0048 has experimental support for APM, you might want to try that first.)
0049 
0050 
0051 Caveats
0052 -------
0053 
0054 * The downside of laptop mode is that you have a chance of losing up to 10
0055   minutes of work. If you cannot afford this, don't use it! The supplied ACPI
0056   scripts automatically turn off laptop mode when the battery almost runs out,
0057   so that you won't lose any data at the end of your battery life.
0058 
0059 * Most desktop hard drives have a very limited lifetime measured in spindown
0060   cycles, typically about 50.000 times (it's usually listed on the spec sheet).
0061   Check your drive's rating, and don't wear down your drive's lifetime if you
0062   don't need to.
0063 
0064 * If you mount some of your ext3/reiserfs filesystems with the -n option, then
0065   the control script will not be able to remount them correctly. You must set
0066   DO_REMOUNTS=0 in the control script, otherwise it will remount them with the
0067   wrong options -- or it will fail because it cannot write to /etc/mtab.
0068 
0069 * If you have your filesystems listed as type "auto" in fstab, like I did, then
0070   the control script will not recognize them as filesystems that need remounting.
0071   You must list the filesystems with their true type instead.
0072 
0073 * It has been reported that some versions of the mutt mail client use file access
0074   times to determine whether a folder contains new mail. If you use mutt and
0075   experience this, you must disable the noatime remounting by setting the option
0076   DO_REMOUNT_NOATIME to 0 in the configuration file.
0077 
0078 
0079 The Details
0080 -----------
0081 
0082 Laptop mode is controlled by the knob /proc/sys/vm/laptop_mode. This knob is
0083 present for all kernels that have the laptop mode patch, regardless of any
0084 configuration options. When the knob is set, any physical disk I/O (that might
0085 have caused the hard disk to spin up) causes Linux to flush all dirty blocks. The
0086 result of this is that after a disk has spun down, it will not be spun up
0087 anymore to write dirty blocks, because those blocks had already been written
0088 immediately after the most recent read operation. The value of the laptop_mode
0089 knob determines the time between the occurrence of disk I/O and when the flush
0090 is triggered. A sensible value for the knob is 5 seconds. Setting the knob to
0091 0 disables laptop mode.
0092 
0093 To increase the effectiveness of the laptop_mode strategy, the laptop_mode
0094 control script increases dirty_expire_centisecs and dirty_writeback_centisecs in
0095 /proc/sys/vm to about 10 minutes (by default), which means that pages that are
0096 dirtied are not forced to be written to disk as often. The control script also
0097 changes the dirty background ratio, so that background writeback of dirty pages
0098 is not done anymore. Combined with a higher commit value (also 10 minutes) for
0099 ext3 or ReiserFS filesystems (also done automatically by the control script),
0100 this results in concentration of disk activity in a small time interval which
0101 occurs only once every 10 minutes, or whenever the disk is forced to spin up by
0102 a cache miss. The disk can then be spun down in the periods of inactivity.
0103 
0104 
0105 Configuration
0106 -------------
0107 
0108 The laptop mode configuration file is located in /etc/default/laptop-mode on
0109 Debian-based systems, or in /etc/sysconfig/laptop-mode on other systems. It
0110 contains the following options:
0111 
0112 MAX_AGE:
0113 
0114 Maximum time, in seconds, of hard drive spindown time that you are
0115 comfortable with. Worst case, it's possible that you could lose this
0116 amount of work if your battery fails while you're in laptop mode.
0117 
0118 MINIMUM_BATTERY_MINUTES:
0119 
0120 Automatically disable laptop mode if the remaining number of minutes of
0121 battery power is less than this value. Default is 10 minutes.
0122 
0123 AC_HD/BATT_HD:
0124 
0125 The idle timeout that should be set on your hard drive when laptop mode
0126 is active (BATT_HD) and when it is not active (AC_HD). The defaults are
0127 20 seconds (value 4) for BATT_HD  and 2 hours (value 244) for AC_HD. The
0128 possible values are those listed in the manual page for "hdparm" for the
0129 "-S" option.
0130 
0131 HD:
0132 
0133 The devices for which the spindown timeout should be adjusted by laptop mode.
0134 Default is /dev/hda. If you specify multiple devices, separate them by a space.
0135 
0136 READAHEAD:
0137 
0138 Disk readahead, in 512-byte sectors, while laptop mode is active. A large
0139 readahead can prevent disk accesses for things like executable pages (which are
0140 loaded on demand while the application executes) and sequentially accessed data
0141 (MP3s).
0142 
0143 DO_REMOUNTS:
0144 
0145 The control script automatically remounts any mounted journaled filesystems
0146 with appropriate commit interval options. When this option is set to 0, this
0147 feature is disabled.
0148 
0149 DO_REMOUNT_NOATIME:
0150 
0151 When remounting, should the filesystems be remounted with the noatime option?
0152 Normally, this is set to "1" (enabled), but there may be programs that require
0153 access time recording.
0154 
0155 DIRTY_RATIO:
0156 
0157 The percentage of memory that is allowed to contain "dirty" or unsaved data
0158 before a writeback is forced, while laptop mode is active. Corresponds to
0159 the /proc/sys/vm/dirty_ratio sysctl.
0160 
0161 DIRTY_BACKGROUND_RATIO:
0162 
0163 The percentage of memory that is allowed to contain "dirty" or unsaved data
0164 after a forced writeback is done due to an exceeding of DIRTY_RATIO. Set
0165 this nice and low. This corresponds to the /proc/sys/vm/dirty_background_ratio
0166 sysctl.
0167 
0168 Note that the behaviour of dirty_background_ratio is quite different
0169 when laptop mode is active and when it isn't. When laptop mode is inactive,
0170 dirty_background_ratio is the threshold percentage at which background writeouts
0171 start taking place. When laptop mode is active, however, background writeouts
0172 are disabled, and the dirty_background_ratio only determines how much writeback
0173 is done when dirty_ratio is reached.
0174 
0175 DO_CPU:
0176 
0177 Enable CPU frequency scaling when in laptop mode. (Requires CPUFreq to be setup.
0178 See Documentation/admin-guide/pm/cpufreq.rst for more info. Disabled by default.)
0179 
0180 CPU_MAXFREQ:
0181 
0182 When on battery, what is the maximum CPU speed that the system should use? Legal
0183 values are "slowest" for the slowest speed that your CPU is able to operate at,
0184 or a value listed in /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies.
0185 
0186 
0187 Tips & Tricks
0188 -------------
0189 
0190 * Bartek Kania reports getting up to 50 minutes of extra battery life (on top
0191   of his regular 3 to 3.5 hours) using a spindown time of 5 seconds (BATT_HD=1).
0192 
0193 * You can spin down the disk while playing MP3, by setting disk readahead
0194   to 8MB (READAHEAD=16384). Effectively, the disk will read a complete MP3 at
0195   once, and will then spin down while the MP3 is playing. (Thanks to Bartek
0196   Kania.)
0197 
0198 * Drew Scott Daniels observed: "I don't know why, but when I decrease the number
0199   of colours that my display uses it consumes less battery power. I've seen
0200   this on powerbooks too. I hope that this is a piece of information that
0201   might be useful to the Laptop Mode patch or its users."
0202 
0203 * In syslog.conf, you can prefix entries with a dash `-` to omit syncing the
0204   file after every logging. When you're using laptop-mode and your disk doesn't
0205   spin down, this is a likely culprit.
0206 
0207 * Richard Atterer observed that laptop mode does not work well with noflushd
0208   (http://noflushd.sourceforge.net/), it seems that noflushd prevents laptop-mode
0209   from doing its thing.
0210 
0211 * If you're worried about your data, you might want to consider using a USB
0212   memory stick or something like that as a "working area". (Be aware though
0213   that flash memory can only handle a limited number of writes, and overuse
0214   may wear out your memory stick pretty quickly. Do _not_ use journalling
0215   filesystems on flash memory sticks.)
0216 
0217 
0218 Configuration file for control and ACPI battery scripts
0219 -------------------------------------------------------
0220 
0221 This allows the tunables to be changed for the scripts via an external
0222 configuration file
0223 
0224 It should be installed as /etc/default/laptop-mode on Debian, and as
0225 /etc/sysconfig/laptop-mode on Red Hat, SUSE, Mandrake, and other work-alikes.
0226 
0227 Config file::
0228 
0229   # Maximum time, in seconds, of hard drive spindown time that you are
0230   # comfortable with. Worst case, it's possible that you could lose this
0231   # amount of work if your battery fails you while in laptop mode.
0232   #MAX_AGE=600
0233 
0234   # Automatically disable laptop mode when the number of minutes of battery
0235   # that you have left goes below this threshold.
0236   MINIMUM_BATTERY_MINUTES=10
0237 
0238   # Read-ahead, in 512-byte sectors. You can spin down the disk while playing MP3/OGG
0239   # by setting the disk readahead to 8MB (READAHEAD=16384). Effectively, the disk
0240   # will read a complete MP3 at once, and will then spin down while the MP3/OGG is
0241   # playing.
0242   #READAHEAD=4096
0243 
0244   # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
0245   #DO_REMOUNTS=1
0246 
0247   # And shall we add the "noatime" option to that as well? (1=yes)
0248   #DO_REMOUNT_NOATIME=1
0249 
0250   # Dirty synchronous ratio.  At this percentage of dirty pages the process
0251   # which
0252   # calls write() does its own writeback
0253   #DIRTY_RATIO=40
0254 
0255   #
0256   # Allowed dirty background ratio, in percent.  Once DIRTY_RATIO has been
0257   # exceeded, the kernel will wake flusher threads which will then reduce the
0258   # amount of dirty memory to dirty_background_ratio.  Set this nice and low,
0259   # so once some writeout has commenced, we do a lot of it.
0260   #
0261   #DIRTY_BACKGROUND_RATIO=5
0262 
0263   # kernel default dirty buffer age
0264   #DEF_AGE=30
0265   #DEF_UPDATE=5
0266   #DEF_DIRTY_BACKGROUND_RATIO=10
0267   #DEF_DIRTY_RATIO=40
0268   #DEF_XFS_AGE_BUFFER=15
0269   #DEF_XFS_SYNC_INTERVAL=30
0270   #DEF_XFS_BUFD_INTERVAL=1
0271 
0272   # This must be adjusted manually to the value of HZ in the running kernel
0273   # on 2.4, until the XFS people change their 2.4 external interfaces to work in
0274   # centisecs. This can be automated, but it's a work in progress that still
0275   # needs# some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for
0276   # external interfaces, and that is currently always set to 100. So you don't
0277   # need to change this on 2.6.
0278   #XFS_HZ=100
0279 
0280   # Should the maximum CPU frequency be adjusted down while on battery?
0281   # Requires CPUFreq to be setup.
0282   # See Documentation/admin-guide/pm/cpufreq.rst for more info
0283   #DO_CPU=0
0284 
0285   # When on battery what is the maximum CPU speed that the system should
0286   # use? Legal values are "slowest" for the slowest speed that your
0287   # CPU is able to operate at, or a value listed in:
0288   # /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
0289   # Only applicable if DO_CPU=1.
0290   #CPU_MAXFREQ=slowest
0291 
0292   # Idle timeout for your hard drive (man hdparm for valid values, -S option)
0293   # Default is 2 hours on AC (AC_HD=244) and 20 seconds for battery (BATT_HD=4).
0294   #AC_HD=244
0295   #BATT_HD=4
0296 
0297   # The drives for which to adjust the idle timeout. Separate them by a space,
0298   # e.g. HD="/dev/hda /dev/hdb".
0299   #HD="/dev/hda"
0300 
0301   # Set the spindown timeout on a hard drive?
0302   #DO_HD=1
0303 
0304 
0305 Control script
0306 --------------
0307 
0308 Please note that this control script works for the Linux 2.4 and 2.6 series (thanks
0309 to Kiko Piris).
0310 
0311 Control script::
0312 
0313   #!/bin/bash
0314 
0315   # start or stop laptop_mode, best run by a power management daemon when
0316   # ac gets connected/disconnected from a laptop
0317   #
0318   # install as /sbin/laptop_mode
0319   #
0320   # Contributors to this script:   Kiko Piris
0321   #                              Bart Samwel
0322   #                              Micha Feigin
0323   #                              Andrew Morton
0324   #                              Herve Eychenne
0325   #                              Dax Kelson
0326   #
0327   # Original Linux 2.4 version by: Jens Axboe
0328 
0329   #############################################################################
0330 
0331   # Source config
0332   if [ -f /etc/default/laptop-mode ] ; then
0333         # Debian
0334         . /etc/default/laptop-mode
0335   elif [ -f /etc/sysconfig/laptop-mode ] ; then
0336         # Others
0337           . /etc/sysconfig/laptop-mode
0338   fi
0339 
0340   # Don't raise an error if the config file is incomplete
0341   # set defaults instead:
0342 
0343   # Maximum time, in seconds, of hard drive spindown time that you are
0344   # comfortable with. Worst case, it's possible that you could lose this
0345   # amount of work if your battery fails you while in laptop mode.
0346   MAX_AGE=${MAX_AGE:-'600'}
0347 
0348   # Read-ahead, in kilobytes
0349   READAHEAD=${READAHEAD:-'4096'}
0350 
0351   # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
0352   DO_REMOUNTS=${DO_REMOUNTS:-'1'}
0353 
0354   # And shall we add the "noatime" option to that as well? (1=yes)
0355   DO_REMOUNT_NOATIME=${DO_REMOUNT_NOATIME:-'1'}
0356 
0357   # Shall we adjust the idle timeout on a hard drive?
0358   DO_HD=${DO_HD:-'1'}
0359 
0360   # Adjust idle timeout on which hard drive?
0361   HD="${HD:-'/dev/hda'}"
0362 
0363   # spindown time for HD (hdparm -S values)
0364   AC_HD=${AC_HD:-'244'}
0365   BATT_HD=${BATT_HD:-'4'}
0366 
0367   # Dirty synchronous ratio.  At this percentage of dirty pages the process which
0368   # calls write() does its own writeback
0369   DIRTY_RATIO=${DIRTY_RATIO:-'40'}
0370 
0371   # cpu frequency scaling
0372   # See Documentation/admin-guide/pm/cpufreq.rst for more info
0373   DO_CPU=${CPU_MANAGE:-'0'}
0374   CPU_MAXFREQ=${CPU_MAXFREQ:-'slowest'}
0375 
0376   #
0377   # Allowed dirty background ratio, in percent.  Once DIRTY_RATIO has been
0378   # exceeded, the kernel will wake flusher threads which will then reduce the
0379   # amount of dirty memory to dirty_background_ratio.  Set this nice and low,
0380   # so once some writeout has commenced, we do a lot of it.
0381   #
0382   DIRTY_BACKGROUND_RATIO=${DIRTY_BACKGROUND_RATIO:-'5'}
0383 
0384   # kernel default dirty buffer age
0385   DEF_AGE=${DEF_AGE:-'30'}
0386   DEF_UPDATE=${DEF_UPDATE:-'5'}
0387   DEF_DIRTY_BACKGROUND_RATIO=${DEF_DIRTY_BACKGROUND_RATIO:-'10'}
0388   DEF_DIRTY_RATIO=${DEF_DIRTY_RATIO:-'40'}
0389   DEF_XFS_AGE_BUFFER=${DEF_XFS_AGE_BUFFER:-'15'}
0390   DEF_XFS_SYNC_INTERVAL=${DEF_XFS_SYNC_INTERVAL:-'30'}
0391   DEF_XFS_BUFD_INTERVAL=${DEF_XFS_BUFD_INTERVAL:-'1'}
0392 
0393   # This must be adjusted manually to the value of HZ in the running kernel
0394   # on 2.4, until the XFS people change their 2.4 external interfaces to work in
0395   # centisecs. This can be automated, but it's a work in progress that still needs
0396   # some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for external
0397   # interfaces, and that is currently always set to 100. So you don't need to
0398   # change this on 2.6.
0399   XFS_HZ=${XFS_HZ:-'100'}
0400 
0401   #############################################################################
0402 
0403   KLEVEL="$(uname -r |
0404                {
0405                IFS='.' read a b c
0406                echo $a.$b
0407              }
0408   )"
0409   case "$KLEVEL" in
0410         "2.4"|"2.6")
0411                 ;;
0412         *)
0413                 echo "Unhandled kernel version: $KLEVEL ('uname -r' = '$(uname -r)')" >&2
0414                 exit 1
0415                 ;;
0416   esac
0417 
0418   if [ ! -e /proc/sys/vm/laptop_mode ] ; then
0419         echo "Kernel is not patched with laptop_mode patch." >&2
0420         exit 1
0421   fi
0422 
0423   if [ ! -w /proc/sys/vm/laptop_mode ] ; then
0424         echo "You do not have enough privileges to enable laptop_mode." >&2
0425         exit 1
0426   fi
0427 
0428   # Remove an option (the first parameter) of the form option=<number> from
0429   # a mount options string (the rest of the parameters).
0430   parse_mount_opts () {
0431         OPT="$1"
0432         shift
0433         echo ",$*," | sed               \
0434          -e 's/,'"$OPT"'=[0-9]*,/,/g'   \
0435          -e 's/,,*/,/g'                 \
0436          -e 's/^,//'                    \
0437          -e 's/,$//'
0438   }
0439 
0440   # Remove an option (the first parameter) without any arguments from
0441   # a mount option string (the rest of the parameters).
0442   parse_nonumber_mount_opts () {
0443         OPT="$1"
0444         shift
0445         echo ",$*," | sed               \
0446          -e 's/,'"$OPT"',/,/g'          \
0447          -e 's/,,*/,/g'                 \
0448          -e 's/^,//'                    \
0449          -e 's/,$//'
0450   }
0451 
0452   # Find out the state of a yes/no option (e.g. "atime"/"noatime") in
0453   # fstab for a given filesystem, and use this state to replace the
0454   # value of the option in another mount options string. The device
0455   # is the first argument, the option name the second, and the default
0456   # value the third. The remainder is the mount options string.
0457   #
0458   # Example:
0459   # parse_yesno_opts_wfstab /dev/hda1 atime atime defaults,noatime
0460   #
0461   # If fstab contains, say, "rw" for this filesystem, then the result
0462   # will be "defaults,atime".
0463   parse_yesno_opts_wfstab () {
0464         L_DEV="$1"
0465         OPT="$2"
0466         DEF_OPT="$3"
0467         shift 3
0468         L_OPTS="$*"
0469         PARSEDOPTS1="$(parse_nonumber_mount_opts $OPT $L_OPTS)"
0470         PARSEDOPTS1="$(parse_nonumber_mount_opts no$OPT $PARSEDOPTS1)"
0471         # Watch for a default atime in fstab
0472         FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
0473         if echo "$FSTAB_OPTS" | grep "$OPT" > /dev/null ; then
0474                 # option specified in fstab: extract the value and use it
0475                 if echo "$FSTAB_OPTS" | grep "no$OPT" > /dev/null ; then
0476                         echo "$PARSEDOPTS1,no$OPT"
0477                 else
0478                         # no$OPT not found -- so we must have $OPT.
0479                         echo "$PARSEDOPTS1,$OPT"
0480                 fi
0481         else
0482                 # option not specified in fstab -- choose the default.
0483                 echo "$PARSEDOPTS1,$DEF_OPT"
0484         fi
0485   }
0486 
0487   # Find out the state of a numbered option (e.g. "commit=NNN") in
0488   # fstab for a given filesystem, and use this state to replace the
0489   # value of the option in another mount options string. The device
0490   # is the first argument, and the option name the second. The
0491   # remainder is the mount options string in which the replacement
0492   # must be done.
0493   #
0494   # Example:
0495   # parse_mount_opts_wfstab /dev/hda1 commit defaults,commit=7
0496   #
0497   # If fstab contains, say, "commit=3,rw" for this filesystem, then the
0498   # result will be "rw,commit=3".
0499   parse_mount_opts_wfstab () {
0500         L_DEV="$1"
0501         OPT="$2"
0502         shift 2
0503         L_OPTS="$*"
0504         PARSEDOPTS1="$(parse_mount_opts $OPT $L_OPTS)"
0505         # Watch for a default commit in fstab
0506         FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
0507         if echo "$FSTAB_OPTS" | grep "$OPT=" > /dev/null ; then
0508                 # option specified in fstab: extract the value, and use it
0509                 echo -n "$PARSEDOPTS1,$OPT="
0510                 echo ",$FSTAB_OPTS," | sed \
0511                  -e 's/.*,'"$OPT"'=//'  \
0512                  -e 's/,.*//'
0513         else
0514                 # option not specified in fstab: set it to 0
0515                 echo "$PARSEDOPTS1,$OPT=0"
0516         fi
0517   }
0518 
0519   deduce_fstype () {
0520         MP="$1"
0521         # My root filesystem unfortunately has
0522         # type "unknown" in /etc/mtab. If we encounter
0523         # "unknown", we try to get the type from fstab.
0524         cat /etc/fstab |
0525         grep -v '^#' |
0526         while read FSTAB_DEV FSTAB_MP FSTAB_FST FSTAB_OPTS FSTAB_DUMP FSTAB_DUMP ; do
0527                 if [ "$FSTAB_MP" = "$MP" ]; then
0528                         echo $FSTAB_FST
0529                         exit 0
0530                 fi
0531         done
0532   }
0533 
0534   if [ $DO_REMOUNT_NOATIME -eq 1 ] ; then
0535         NOATIME_OPT=",noatime"
0536   fi
0537 
0538   case "$1" in
0539         start)
0540                 AGE=$((100*$MAX_AGE))
0541                 XFS_AGE=$(($XFS_HZ*$MAX_AGE))
0542                 echo -n "Starting laptop_mode"
0543 
0544                 if [ -d /proc/sys/vm/pagebuf ] ; then
0545                         # (For 2.4 and early 2.6.)
0546                         # This only needs to be set, not reset -- it is only used when
0547                         # laptop mode is enabled.
0548                         echo $XFS_AGE > /proc/sys/vm/pagebuf/lm_flush_age
0549                         echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
0550                 elif [ -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
0551                         # (A couple of early 2.6 laptop mode patches had these.)
0552                         # The same goes for these.
0553                         echo $XFS_AGE > /proc/sys/fs/xfs/lm_age_buffer
0554                         echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
0555                 elif [ -f /proc/sys/fs/xfs/age_buffer ] ; then
0556                         # (2.6.6)
0557                         # But not for these -- they are also used in normal
0558                         # operation.
0559                         echo $XFS_AGE > /proc/sys/fs/xfs/age_buffer
0560                         echo $XFS_AGE > /proc/sys/fs/xfs/sync_interval
0561                 elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
0562                         # (2.6.7 upwards)
0563                         # And not for these either. These are in centisecs,
0564                         # not USER_HZ, so we have to use $AGE, not $XFS_AGE.
0565                         echo $AGE > /proc/sys/fs/xfs/age_buffer_centisecs
0566                         echo $AGE > /proc/sys/fs/xfs/xfssyncd_centisecs
0567                         echo 3000 > /proc/sys/fs/xfs/xfsbufd_centisecs
0568                 fi
0569 
0570                 case "$KLEVEL" in
0571                         "2.4")
0572                                 echo 1                                  > /proc/sys/vm/laptop_mode
0573                                 echo "30 500 0 0 $AGE $AGE 60 20 0"     > /proc/sys/vm/bdflush
0574                                 ;;
0575                         "2.6")
0576                                 echo 5                                  > /proc/sys/vm/laptop_mode
0577                                 echo "$AGE"                             > /proc/sys/vm/dirty_writeback_centisecs
0578                                 echo "$AGE"                             > /proc/sys/vm/dirty_expire_centisecs
0579                                 echo "$DIRTY_RATIO"                     > /proc/sys/vm/dirty_ratio
0580                                 echo "$DIRTY_BACKGROUND_RATIO"          > /proc/sys/vm/dirty_background_ratio
0581                                 ;;
0582                 esac
0583                 if [ $DO_REMOUNTS -eq 1 ]; then
0584                         cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
0585                                 PARSEDOPTS="$(parse_mount_opts "$OPTS")"
0586                                 if [ "$FST" = 'unknown' ]; then
0587                                         FST=$(deduce_fstype $MP)
0588                                 fi
0589                                 case "$FST" in
0590                                         "ext3"|"reiserfs")
0591                                                 PARSEDOPTS="$(parse_mount_opts commit "$OPTS")"
0592                                                 mount $DEV -t $FST $MP -o remount,$PARSEDOPTS,commit=$MAX_AGE$NOATIME_OPT
0593                                                 ;;
0594                                         "xfs")
0595                                                 mount $DEV -t $FST $MP -o remount,$OPTS$NOATIME_OPT
0596                                                 ;;
0597                                 esac
0598                                 if [ -b $DEV ] ; then
0599                                         blockdev --setra $(($READAHEAD * 2)) $DEV
0600                                 fi
0601                         done
0602                 fi
0603                 if [ $DO_HD -eq 1 ] ; then
0604                         for THISHD in $HD ; do
0605                                 /sbin/hdparm -S $BATT_HD $THISHD > /dev/null 2>&1
0606                                 /sbin/hdparm -B 1 $THISHD > /dev/null 2>&1
0607                         done
0608                 fi
0609                 if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
0610                         if [ $CPU_MAXFREQ = 'slowest' ]; then
0611                                 CPU_MAXFREQ=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
0612                         fi
0613                         echo $CPU_MAXFREQ > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
0614                 fi
0615                 echo "."
0616                 ;;
0617         stop)
0618                 U_AGE=$((100*$DEF_UPDATE))
0619                 B_AGE=$((100*$DEF_AGE))
0620                 echo -n "Stopping laptop_mode"
0621                 echo 0 > /proc/sys/vm/laptop_mode
0622                 if [ -f /proc/sys/fs/xfs/age_buffer -a ! -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
0623                         # These need to be restored, if there are no lm_*.
0624                         echo $(($XFS_HZ*$DEF_XFS_AGE_BUFFER))           > /proc/sys/fs/xfs/age_buffer
0625                         echo $(($XFS_HZ*$DEF_XFS_SYNC_INTERVAL))        > /proc/sys/fs/xfs/sync_interval
0626                 elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
0627                         # These need to be restored as well.
0628                         echo $((100*$DEF_XFS_AGE_BUFFER))       > /proc/sys/fs/xfs/age_buffer_centisecs
0629                         echo $((100*$DEF_XFS_SYNC_INTERVAL))    > /proc/sys/fs/xfs/xfssyncd_centisecs
0630                         echo $((100*$DEF_XFS_BUFD_INTERVAL))    > /proc/sys/fs/xfs/xfsbufd_centisecs
0631                 fi
0632                 case "$KLEVEL" in
0633                         "2.4")
0634                                 echo "30 500 0 0 $U_AGE $B_AGE 60 20 0" > /proc/sys/vm/bdflush
0635                                 ;;
0636                         "2.6")
0637                                 echo "$U_AGE"                           > /proc/sys/vm/dirty_writeback_centisecs
0638                                 echo "$B_AGE"                           > /proc/sys/vm/dirty_expire_centisecs
0639                                 echo "$DEF_DIRTY_RATIO"                 > /proc/sys/vm/dirty_ratio
0640                                 echo "$DEF_DIRTY_BACKGROUND_RATIO"      > /proc/sys/vm/dirty_background_ratio
0641                                 ;;
0642                 esac
0643                 if [ $DO_REMOUNTS -eq 1 ] ; then
0644                         cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
0645                                 # Reset commit and atime options to defaults.
0646                                 if [ "$FST" = 'unknown' ]; then
0647                                         FST=$(deduce_fstype $MP)
0648                                 fi
0649                                 case "$FST" in
0650                                         "ext3"|"reiserfs")
0651                                                 PARSEDOPTS="$(parse_mount_opts_wfstab $DEV commit $OPTS)"
0652                                                 PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $PARSEDOPTS)"
0653                                                 mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
0654                                                 ;;
0655                                         "xfs")
0656                                                 PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $OPTS)"
0657                                                 mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
0658                                                 ;;
0659                                 esac
0660                                 if [ -b $DEV ] ; then
0661                                         blockdev --setra 256 $DEV
0662                                 fi
0663                         done
0664                 fi
0665                 if [ $DO_HD -eq 1 ] ; then
0666                         for THISHD in $HD ; do
0667                                 /sbin/hdparm -S $AC_HD $THISHD > /dev/null 2>&1
0668                                 /sbin/hdparm -B 255 $THISHD > /dev/null 2>&1
0669                         done
0670                 fi
0671                 if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
0672                         echo `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq` > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
0673                 fi
0674                 echo "."
0675                 ;;
0676         *)
0677                 echo "Usage: $0 {start|stop}" 2>&1
0678                 exit 1
0679                 ;;
0680 
0681   esac
0682 
0683   exit 0
0684 
0685 
0686 ACPI integration
0687 ----------------
0688 
0689 Dax Kelson submitted this so that the ACPI acpid daemon will
0690 kick off the laptop_mode script and run hdparm. The part that
0691 automatically disables laptop mode when the battery is low was
0692 written by Jan Topinski.
0693 
0694 /etc/acpi/events/ac_adapter::
0695 
0696         event=ac_adapter
0697         action=/etc/acpi/actions/ac.sh %e
0698 
0699 /etc/acpi/events/battery::
0700 
0701         event=battery.*
0702         action=/etc/acpi/actions/battery.sh %e
0703 
0704 /etc/acpi/actions/ac.sh::
0705 
0706   #!/bin/bash
0707 
0708   # ac on/offline event handler
0709 
0710   status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/$2/state`
0711 
0712   case $status in
0713           "on-line")
0714                   /sbin/laptop_mode stop
0715                   exit 0
0716           ;;
0717           "off-line")
0718                   /sbin/laptop_mode start
0719                   exit 0
0720           ;;
0721   esac
0722 
0723 
0724 /etc/acpi/actions/battery.sh::
0725 
0726   #! /bin/bash
0727 
0728   # Automatically disable laptop mode when the battery almost runs out.
0729 
0730   BATT_INFO=/proc/acpi/battery/$2/state
0731 
0732   if [[ -f /proc/sys/vm/laptop_mode ]]
0733   then
0734      LM=`cat /proc/sys/vm/laptop_mode`
0735      if [[ $LM -gt 0 ]]
0736      then
0737        if [[ -f $BATT_INFO ]]
0738        then
0739           # Source the config file only now that we know we need
0740           if [ -f /etc/default/laptop-mode ] ; then
0741                   # Debian
0742                   . /etc/default/laptop-mode
0743           elif [ -f /etc/sysconfig/laptop-mode ] ; then
0744                   # Others
0745                   . /etc/sysconfig/laptop-mode
0746           fi
0747           MINIMUM_BATTERY_MINUTES=${MINIMUM_BATTERY_MINUTES:-'10'}
0748 
0749           ACTION="`cat $BATT_INFO | grep charging | cut -c 26-`"
0750           if [[ ACTION -eq "discharging" ]]
0751           then
0752              PRESENT_RATE=`cat $BATT_INFO | grep "present rate:" | sed  "s/.* \([0-9][0-9]* \).*/\1/" `
0753              REMAINING=`cat $BATT_INFO | grep "remaining capacity:" | sed  "s/.* \([0-9][0-9]* \).*/\1/" `
0754           fi
0755           if (($REMAINING * 60 / $PRESENT_RATE < $MINIMUM_BATTERY_MINUTES))
0756           then
0757              /sbin/laptop_mode stop
0758           fi
0759        else
0760          logger -p daemon.warning "You are using laptop mode and your battery interface $BATT_INFO is missing. This may lead to loss of data when the battery runs out. Check kernel ACPI support and /proc/acpi/battery folder, and edit /etc/acpi/battery.sh to set BATT_INFO to the correct path."
0761        fi
0762      fi
0763   fi
0764 
0765 
0766 Monitoring tool
0767 ---------------
0768 
0769 Bartek Kania submitted this, it can be used to measure how much time your disk
0770 spends spun up/down.  See tools/laptop/dslm/dslm.c