771 lines · plain
1===============================================2How to conserve battery power using laptop-mode3===============================================4 5Document Author: Bart Samwel (bart@samwel.tk)6 7Date created: January 2, 20048 9Last modified: December 06, 200410 11Introduction12------------13 14Laptop mode is used to minimize the time that the hard disk needs to be spun up,15to conserve battery power on laptops. It has been reported to cause significant16power savings.17 18.. Contents19 20 * Introduction21 * Installation22 * Caveats23 * The Details24 * Tips & Tricks25 * Control script26 * ACPI integration27 * Monitoring tool28 29 30Installation31------------32 33To use laptop mode, you don't need to set any kernel configuration options34or anything. Simply install all the files included in this document, and35laptop mode will automatically be started when you're on battery. For36your convenience, a tarball containing an installer can be downloaded at:37 38 http://www.samwel.tk/laptop_mode/laptop_mode/39 40To configure laptop mode, you need to edit the configuration file, which is41located in /etc/default/laptop-mode on Debian-based systems, or in42/etc/sysconfig/laptop-mode on other systems.43 44Unfortunately, automatic enabling of laptop mode does not work for45laptops that don't have ACPI. On those laptops, you need to start laptop46mode manually. To start laptop mode, run "laptop_mode start", and to47stop it, run "laptop_mode stop". (Note: The laptop mode tools package now48has experimental support for APM, you might want to try that first.)49 50 51Caveats52-------53 54* The downside of laptop mode is that you have a chance of losing up to 1055 minutes of work. If you cannot afford this, don't use it! The supplied ACPI56 scripts automatically turn off laptop mode when the battery almost runs out,57 so that you won't lose any data at the end of your battery life.58 59* Most desktop hard drives have a very limited lifetime measured in spindown60 cycles, typically about 50.000 times (it's usually listed on the spec sheet).61 Check your drive's rating, and don't wear down your drive's lifetime if you62 don't need to.63 64* If you mount some of your ext3/reiserfs filesystems with the -n option, then65 the control script will not be able to remount them correctly. You must set66 DO_REMOUNTS=0 in the control script, otherwise it will remount them with the67 wrong options -- or it will fail because it cannot write to /etc/mtab.68 69* If you have your filesystems listed as type "auto" in fstab, like I did, then70 the control script will not recognize them as filesystems that need remounting.71 You must list the filesystems with their true type instead.72 73* It has been reported that some versions of the mutt mail client use file access74 times to determine whether a folder contains new mail. If you use mutt and75 experience this, you must disable the noatime remounting by setting the option76 DO_REMOUNT_NOATIME to 0 in the configuration file.77 78 79The Details80-----------81 82Laptop mode is controlled by the knob /proc/sys/vm/laptop_mode. This knob is83present for all kernels that have the laptop mode patch, regardless of any84configuration options. When the knob is set, any physical disk I/O (that might85have caused the hard disk to spin up) causes Linux to flush all dirty blocks. The86result of this is that after a disk has spun down, it will not be spun up87anymore to write dirty blocks, because those blocks had already been written88immediately after the most recent read operation. The value of the laptop_mode89knob determines the time between the occurrence of disk I/O and when the flush90is triggered. A sensible value for the knob is 5 seconds. Setting the knob to910 disables laptop mode.92 93To increase the effectiveness of the laptop_mode strategy, the laptop_mode94control script increases dirty_expire_centisecs and dirty_writeback_centisecs in95/proc/sys/vm to about 10 minutes (by default), which means that pages that are96dirtied are not forced to be written to disk as often. The control script also97changes the dirty background ratio, so that background writeback of dirty pages98is not done anymore. Combined with a higher commit value (also 10 minutes) for99ext3 or ReiserFS filesystems (also done automatically by the control script),100this results in concentration of disk activity in a small time interval which101occurs only once every 10 minutes, or whenever the disk is forced to spin up by102a cache miss. The disk can then be spun down in the periods of inactivity.103 104 105Configuration106-------------107 108The laptop mode configuration file is located in /etc/default/laptop-mode on109Debian-based systems, or in /etc/sysconfig/laptop-mode on other systems. It110contains the following options:111 112MAX_AGE:113 114Maximum time, in seconds, of hard drive spindown time that you are115comfortable with. Worst case, it's possible that you could lose this116amount of work if your battery fails while you're in laptop mode.117 118MINIMUM_BATTERY_MINUTES:119 120Automatically disable laptop mode if the remaining number of minutes of121battery power is less than this value. Default is 10 minutes.122 123AC_HD/BATT_HD:124 125The idle timeout that should be set on your hard drive when laptop mode126is active (BATT_HD) and when it is not active (AC_HD). The defaults are12720 seconds (value 4) for BATT_HD and 2 hours (value 244) for AC_HD. The128possible values are those listed in the manual page for "hdparm" for the129"-S" option.130 131HD:132 133The devices for which the spindown timeout should be adjusted by laptop mode.134Default is /dev/hda. If you specify multiple devices, separate them by a space.135 136READAHEAD:137 138Disk readahead, in 512-byte sectors, while laptop mode is active. A large139readahead can prevent disk accesses for things like executable pages (which are140loaded on demand while the application executes) and sequentially accessed data141(MP3s).142 143DO_REMOUNTS:144 145The control script automatically remounts any mounted journaled filesystems146with appropriate commit interval options. When this option is set to 0, this147feature is disabled.148 149DO_REMOUNT_NOATIME:150 151When remounting, should the filesystems be remounted with the noatime option?152Normally, this is set to "1" (enabled), but there may be programs that require153access time recording.154 155DIRTY_RATIO:156 157The percentage of memory that is allowed to contain "dirty" or unsaved data158before a writeback is forced, while laptop mode is active. Corresponds to159the /proc/sys/vm/dirty_ratio sysctl.160 161DIRTY_BACKGROUND_RATIO:162 163The percentage of memory that is allowed to contain "dirty" or unsaved data164after a forced writeback is done due to an exceeding of DIRTY_RATIO. Set165this nice and low. This corresponds to the /proc/sys/vm/dirty_background_ratio166sysctl.167 168Note that the behaviour of dirty_background_ratio is quite different169when laptop mode is active and when it isn't. When laptop mode is inactive,170dirty_background_ratio is the threshold percentage at which background writeouts171start taking place. When laptop mode is active, however, background writeouts172are disabled, and the dirty_background_ratio only determines how much writeback173is done when dirty_ratio is reached.174 175DO_CPU:176 177Enable CPU frequency scaling when in laptop mode. (Requires CPUFreq to be setup.178See Documentation/admin-guide/pm/cpufreq.rst for more info. Disabled by default.)179 180CPU_MAXFREQ:181 182When on battery, what is the maximum CPU speed that the system should use? Legal183values are "slowest" for the slowest speed that your CPU is able to operate at,184or a value listed in /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies.185 186 187Tips & Tricks188-------------189 190* Bartek Kania reports getting up to 50 minutes of extra battery life (on top191 of his regular 3 to 3.5 hours) using a spindown time of 5 seconds (BATT_HD=1).192 193* You can spin down the disk while playing MP3, by setting disk readahead194 to 8MB (READAHEAD=16384). Effectively, the disk will read a complete MP3 at195 once, and will then spin down while the MP3 is playing. (Thanks to Bartek196 Kania.)197 198* Drew Scott Daniels observed: "I don't know why, but when I decrease the number199 of colours that my display uses it consumes less battery power. I've seen200 this on powerbooks too. I hope that this is a piece of information that201 might be useful to the Laptop Mode patch or its users."202 203* In syslog.conf, you can prefix entries with a dash `-` to omit syncing the204 file after every logging. When you're using laptop-mode and your disk doesn't205 spin down, this is a likely culprit.206 207* Richard Atterer observed that laptop mode does not work well with noflushd208 (http://noflushd.sourceforge.net/), it seems that noflushd prevents laptop-mode209 from doing its thing.210 211* If you're worried about your data, you might want to consider using a USB212 memory stick or something like that as a "working area". (Be aware though213 that flash memory can only handle a limited number of writes, and overuse214 may wear out your memory stick pretty quickly. Do _not_ use journalling215 filesystems on flash memory sticks.)216 217 218Configuration file for control and ACPI battery scripts219-------------------------------------------------------220 221This allows the tunables to be changed for the scripts via an external222configuration file223 224It should be installed as /etc/default/laptop-mode on Debian, and as225/etc/sysconfig/laptop-mode on Red Hat, SUSE, Mandrake, and other work-alikes.226 227Config file::228 229 # Maximum time, in seconds, of hard drive spindown time that you are230 # comfortable with. Worst case, it's possible that you could lose this231 # amount of work if your battery fails you while in laptop mode.232 #MAX_AGE=600233 234 # Automatically disable laptop mode when the number of minutes of battery235 # that you have left goes below this threshold.236 MINIMUM_BATTERY_MINUTES=10237 238 # Read-ahead, in 512-byte sectors. You can spin down the disk while playing MP3/OGG239 # by setting the disk readahead to 8MB (READAHEAD=16384). Effectively, the disk240 # will read a complete MP3 at once, and will then spin down while the MP3/OGG is241 # playing.242 #READAHEAD=4096243 244 # Shall we remount journaled fs. with appropriate commit interval? (1=yes)245 #DO_REMOUNTS=1246 247 # And shall we add the "noatime" option to that as well? (1=yes)248 #DO_REMOUNT_NOATIME=1249 250 # Dirty synchronous ratio. At this percentage of dirty pages the process251 # which252 # calls write() does its own writeback253 #DIRTY_RATIO=40254 255 #256 # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been257 # exceeded, the kernel will wake flusher threads which will then reduce the258 # amount of dirty memory to dirty_background_ratio. Set this nice and low,259 # so once some writeout has commenced, we do a lot of it.260 #261 #DIRTY_BACKGROUND_RATIO=5262 263 # kernel default dirty buffer age264 #DEF_AGE=30265 #DEF_UPDATE=5266 #DEF_DIRTY_BACKGROUND_RATIO=10267 #DEF_DIRTY_RATIO=40268 #DEF_XFS_AGE_BUFFER=15269 #DEF_XFS_SYNC_INTERVAL=30270 #DEF_XFS_BUFD_INTERVAL=1271 272 # This must be adjusted manually to the value of HZ in the running kernel273 # on 2.4, until the XFS people change their 2.4 external interfaces to work in274 # centisecs. This can be automated, but it's a work in progress that still275 # needs# some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for276 # external interfaces, and that is currently always set to 100. So you don't277 # need to change this on 2.6.278 #XFS_HZ=100279 280 # Should the maximum CPU frequency be adjusted down while on battery?281 # Requires CPUFreq to be setup.282 # See Documentation/admin-guide/pm/cpufreq.rst for more info283 #DO_CPU=0284 285 # When on battery what is the maximum CPU speed that the system should286 # use? Legal values are "slowest" for the slowest speed that your287 # CPU is able to operate at, or a value listed in:288 # /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies289 # Only applicable if DO_CPU=1.290 #CPU_MAXFREQ=slowest291 292 # Idle timeout for your hard drive (man hdparm for valid values, -S option)293 # Default is 2 hours on AC (AC_HD=244) and 20 seconds for battery (BATT_HD=4).294 #AC_HD=244295 #BATT_HD=4296 297 # The drives for which to adjust the idle timeout. Separate them by a space,298 # e.g. HD="/dev/hda /dev/hdb".299 #HD="/dev/hda"300 301 # Set the spindown timeout on a hard drive?302 #DO_HD=1303 304 305Control script306--------------307 308Please note that this control script works for the Linux 2.4 and 2.6 series (thanks309to Kiko Piris).310 311Control script::312 313 #!/bin/bash314 315 # start or stop laptop_mode, best run by a power management daemon when316 # ac gets connected/disconnected from a laptop317 #318 # install as /sbin/laptop_mode319 #320 # Contributors to this script: Kiko Piris321 # Bart Samwel322 # Micha Feigin323 # Andrew Morton324 # Herve Eychenne325 # Dax Kelson326 #327 # Original Linux 2.4 version by: Jens Axboe328 329 #############################################################################330 331 # Source config332 if [ -f /etc/default/laptop-mode ] ; then333 # Debian334 . /etc/default/laptop-mode335 elif [ -f /etc/sysconfig/laptop-mode ] ; then336 # Others337 . /etc/sysconfig/laptop-mode338 fi339 340 # Don't raise an error if the config file is incomplete341 # set defaults instead:342 343 # Maximum time, in seconds, of hard drive spindown time that you are344 # comfortable with. Worst case, it's possible that you could lose this345 # amount of work if your battery fails you while in laptop mode.346 MAX_AGE=${MAX_AGE:-'600'}347 348 # Read-ahead, in kilobytes349 READAHEAD=${READAHEAD:-'4096'}350 351 # Shall we remount journaled fs. with appropriate commit interval? (1=yes)352 DO_REMOUNTS=${DO_REMOUNTS:-'1'}353 354 # And shall we add the "noatime" option to that as well? (1=yes)355 DO_REMOUNT_NOATIME=${DO_REMOUNT_NOATIME:-'1'}356 357 # Shall we adjust the idle timeout on a hard drive?358 DO_HD=${DO_HD:-'1'}359 360 # Adjust idle timeout on which hard drive?361 HD="${HD:-'/dev/hda'}"362 363 # spindown time for HD (hdparm -S values)364 AC_HD=${AC_HD:-'244'}365 BATT_HD=${BATT_HD:-'4'}366 367 # Dirty synchronous ratio. At this percentage of dirty pages the process which368 # calls write() does its own writeback369 DIRTY_RATIO=${DIRTY_RATIO:-'40'}370 371 # cpu frequency scaling372 # See Documentation/admin-guide/pm/cpufreq.rst for more info373 DO_CPU=${CPU_MANAGE:-'0'}374 CPU_MAXFREQ=${CPU_MAXFREQ:-'slowest'}375 376 #377 # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been378 # exceeded, the kernel will wake flusher threads which will then reduce the379 # amount of dirty memory to dirty_background_ratio. Set this nice and low,380 # so once some writeout has commenced, we do a lot of it.381 #382 DIRTY_BACKGROUND_RATIO=${DIRTY_BACKGROUND_RATIO:-'5'}383 384 # kernel default dirty buffer age385 DEF_AGE=${DEF_AGE:-'30'}386 DEF_UPDATE=${DEF_UPDATE:-'5'}387 DEF_DIRTY_BACKGROUND_RATIO=${DEF_DIRTY_BACKGROUND_RATIO:-'10'}388 DEF_DIRTY_RATIO=${DEF_DIRTY_RATIO:-'40'}389 DEF_XFS_AGE_BUFFER=${DEF_XFS_AGE_BUFFER:-'15'}390 DEF_XFS_SYNC_INTERVAL=${DEF_XFS_SYNC_INTERVAL:-'30'}391 DEF_XFS_BUFD_INTERVAL=${DEF_XFS_BUFD_INTERVAL:-'1'}392 393 # This must be adjusted manually to the value of HZ in the running kernel394 # on 2.4, until the XFS people change their 2.4 external interfaces to work in395 # centisecs. This can be automated, but it's a work in progress that still needs396 # some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for external397 # interfaces, and that is currently always set to 100. So you don't need to398 # change this on 2.6.399 XFS_HZ=${XFS_HZ:-'100'}400 401 #############################################################################402 403 KLEVEL="$(uname -r |404 {405 IFS='.' read a b c406 echo $a.$b407 }408 )"409 case "$KLEVEL" in410 "2.4"|"2.6")411 ;;412 *)413 echo "Unhandled kernel version: $KLEVEL ('uname -r' = '$(uname -r)')" >&2414 exit 1415 ;;416 esac417 418 if [ ! -e /proc/sys/vm/laptop_mode ] ; then419 echo "Kernel is not patched with laptop_mode patch." >&2420 exit 1421 fi422 423 if [ ! -w /proc/sys/vm/laptop_mode ] ; then424 echo "You do not have enough privileges to enable laptop_mode." >&2425 exit 1426 fi427 428 # Remove an option (the first parameter) of the form option=<number> from429 # a mount options string (the rest of the parameters).430 parse_mount_opts () {431 OPT="$1"432 shift433 echo ",$*," | sed \434 -e 's/,'"$OPT"'=[0-9]*,/,/g' \435 -e 's/,,*/,/g' \436 -e 's/^,//' \437 -e 's/,$//'438 }439 440 # Remove an option (the first parameter) without any arguments from441 # a mount option string (the rest of the parameters).442 parse_nonumber_mount_opts () {443 OPT="$1"444 shift445 echo ",$*," | sed \446 -e 's/,'"$OPT"',/,/g' \447 -e 's/,,*/,/g' \448 -e 's/^,//' \449 -e 's/,$//'450 }451 452 # Find out the state of a yes/no option (e.g. "atime"/"noatime") in453 # fstab for a given filesystem, and use this state to replace the454 # value of the option in another mount options string. The device455 # is the first argument, the option name the second, and the default456 # value the third. The remainder is the mount options string.457 #458 # Example:459 # parse_yesno_opts_wfstab /dev/hda1 atime atime defaults,noatime460 #461 # If fstab contains, say, "rw" for this filesystem, then the result462 # will be "defaults,atime".463 parse_yesno_opts_wfstab () {464 L_DEV="$1"465 OPT="$2"466 DEF_OPT="$3"467 shift 3468 L_OPTS="$*"469 PARSEDOPTS1="$(parse_nonumber_mount_opts $OPT $L_OPTS)"470 PARSEDOPTS1="$(parse_nonumber_mount_opts no$OPT $PARSEDOPTS1)"471 # Watch for a default atime in fstab472 FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"473 if echo "$FSTAB_OPTS" | grep "$OPT" > /dev/null ; then474 # option specified in fstab: extract the value and use it475 if echo "$FSTAB_OPTS" | grep "no$OPT" > /dev/null ; then476 echo "$PARSEDOPTS1,no$OPT"477 else478 # no$OPT not found -- so we must have $OPT.479 echo "$PARSEDOPTS1,$OPT"480 fi481 else482 # option not specified in fstab -- choose the default.483 echo "$PARSEDOPTS1,$DEF_OPT"484 fi485 }486 487 # Find out the state of a numbered option (e.g. "commit=NNN") in488 # fstab for a given filesystem, and use this state to replace the489 # value of the option in another mount options string. The device490 # is the first argument, and the option name the second. The491 # remainder is the mount options string in which the replacement492 # must be done.493 #494 # Example:495 # parse_mount_opts_wfstab /dev/hda1 commit defaults,commit=7496 #497 # If fstab contains, say, "commit=3,rw" for this filesystem, then the498 # result will be "rw,commit=3".499 parse_mount_opts_wfstab () {500 L_DEV="$1"501 OPT="$2"502 shift 2503 L_OPTS="$*"504 PARSEDOPTS1="$(parse_mount_opts $OPT $L_OPTS)"505 # Watch for a default commit in fstab506 FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"507 if echo "$FSTAB_OPTS" | grep "$OPT=" > /dev/null ; then508 # option specified in fstab: extract the value, and use it509 echo -n "$PARSEDOPTS1,$OPT="510 echo ",$FSTAB_OPTS," | sed \511 -e 's/.*,'"$OPT"'=//' \512 -e 's/,.*//'513 else514 # option not specified in fstab: set it to 0515 echo "$PARSEDOPTS1,$OPT=0"516 fi517 }518 519 deduce_fstype () {520 MP="$1"521 # My root filesystem unfortunately has522 # type "unknown" in /etc/mtab. If we encounter523 # "unknown", we try to get the type from fstab.524 cat /etc/fstab |525 grep -v '^#' |526 while read FSTAB_DEV FSTAB_MP FSTAB_FST FSTAB_OPTS FSTAB_DUMP FSTAB_DUMP ; do527 if [ "$FSTAB_MP" = "$MP" ]; then528 echo $FSTAB_FST529 exit 0530 fi531 done532 }533 534 if [ $DO_REMOUNT_NOATIME -eq 1 ] ; then535 NOATIME_OPT=",noatime"536 fi537 538 case "$1" in539 start)540 AGE=$((100*$MAX_AGE))541 XFS_AGE=$(($XFS_HZ*$MAX_AGE))542 echo -n "Starting laptop_mode"543 544 if [ -d /proc/sys/vm/pagebuf ] ; then545 # (For 2.4 and early 2.6.)546 # This only needs to be set, not reset -- it is only used when547 # laptop mode is enabled.548 echo $XFS_AGE > /proc/sys/vm/pagebuf/lm_flush_age549 echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval550 elif [ -f /proc/sys/fs/xfs/lm_age_buffer ] ; then551 # (A couple of early 2.6 laptop mode patches had these.)552 # The same goes for these.553 echo $XFS_AGE > /proc/sys/fs/xfs/lm_age_buffer554 echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval555 elif [ -f /proc/sys/fs/xfs/age_buffer ] ; then556 # (2.6.6)557 # But not for these -- they are also used in normal558 # operation.559 echo $XFS_AGE > /proc/sys/fs/xfs/age_buffer560 echo $XFS_AGE > /proc/sys/fs/xfs/sync_interval561 elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then562 # (2.6.7 upwards)563 # And not for these either. These are in centisecs,564 # not USER_HZ, so we have to use $AGE, not $XFS_AGE.565 echo $AGE > /proc/sys/fs/xfs/age_buffer_centisecs566 echo $AGE > /proc/sys/fs/xfs/xfssyncd_centisecs567 echo 3000 > /proc/sys/fs/xfs/xfsbufd_centisecs568 fi569 570 case "$KLEVEL" in571 "2.4")572 echo 1 > /proc/sys/vm/laptop_mode573 echo "30 500 0 0 $AGE $AGE 60 20 0" > /proc/sys/vm/bdflush574 ;;575 "2.6")576 echo 5 > /proc/sys/vm/laptop_mode577 echo "$AGE" > /proc/sys/vm/dirty_writeback_centisecs578 echo "$AGE" > /proc/sys/vm/dirty_expire_centisecs579 echo "$DIRTY_RATIO" > /proc/sys/vm/dirty_ratio580 echo "$DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio581 ;;582 esac583 if [ $DO_REMOUNTS -eq 1 ]; then584 cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do585 PARSEDOPTS="$(parse_mount_opts "$OPTS")"586 if [ "$FST" = 'unknown' ]; then587 FST=$(deduce_fstype $MP)588 fi589 case "$FST" in590 "ext3"|"reiserfs")591 PARSEDOPTS="$(parse_mount_opts commit "$OPTS")"592 mount $DEV -t $FST $MP -o remount,$PARSEDOPTS,commit=$MAX_AGE$NOATIME_OPT593 ;;594 "xfs")595 mount $DEV -t $FST $MP -o remount,$OPTS$NOATIME_OPT596 ;;597 esac598 if [ -b $DEV ] ; then599 blockdev --setra $(($READAHEAD * 2)) $DEV600 fi601 done602 fi603 if [ $DO_HD -eq 1 ] ; then604 for THISHD in $HD ; do605 /sbin/hdparm -S $BATT_HD $THISHD > /dev/null 2>&1606 /sbin/hdparm -B 1 $THISHD > /dev/null 2>&1607 done608 fi609 if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then610 if [ $CPU_MAXFREQ = 'slowest' ]; then611 CPU_MAXFREQ=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`612 fi613 echo $CPU_MAXFREQ > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq614 fi615 echo "."616 ;;617 stop)618 U_AGE=$((100*$DEF_UPDATE))619 B_AGE=$((100*$DEF_AGE))620 echo -n "Stopping laptop_mode"621 echo 0 > /proc/sys/vm/laptop_mode622 if [ -f /proc/sys/fs/xfs/age_buffer -a ! -f /proc/sys/fs/xfs/lm_age_buffer ] ; then623 # These need to be restored, if there are no lm_*.624 echo $(($XFS_HZ*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer625 echo $(($XFS_HZ*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/sync_interval626 elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then627 # These need to be restored as well.628 echo $((100*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer_centisecs629 echo $((100*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/xfssyncd_centisecs630 echo $((100*$DEF_XFS_BUFD_INTERVAL)) > /proc/sys/fs/xfs/xfsbufd_centisecs631 fi632 case "$KLEVEL" in633 "2.4")634 echo "30 500 0 0 $U_AGE $B_AGE 60 20 0" > /proc/sys/vm/bdflush635 ;;636 "2.6")637 echo "$U_AGE" > /proc/sys/vm/dirty_writeback_centisecs638 echo "$B_AGE" > /proc/sys/vm/dirty_expire_centisecs639 echo "$DEF_DIRTY_RATIO" > /proc/sys/vm/dirty_ratio640 echo "$DEF_DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio641 ;;642 esac643 if [ $DO_REMOUNTS -eq 1 ] ; then644 cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do645 # Reset commit and atime options to defaults.646 if [ "$FST" = 'unknown' ]; then647 FST=$(deduce_fstype $MP)648 fi649 case "$FST" in650 "ext3"|"reiserfs")651 PARSEDOPTS="$(parse_mount_opts_wfstab $DEV commit $OPTS)"652 PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $PARSEDOPTS)"653 mount $DEV -t $FST $MP -o remount,$PARSEDOPTS654 ;;655 "xfs")656 PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $OPTS)"657 mount $DEV -t $FST $MP -o remount,$PARSEDOPTS658 ;;659 esac660 if [ -b $DEV ] ; then661 blockdev --setra 256 $DEV662 fi663 done664 fi665 if [ $DO_HD -eq 1 ] ; then666 for THISHD in $HD ; do667 /sbin/hdparm -S $AC_HD $THISHD > /dev/null 2>&1668 /sbin/hdparm -B 255 $THISHD > /dev/null 2>&1669 done670 fi671 if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then672 echo `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq` > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq673 fi674 echo "."675 ;;676 *)677 echo "Usage: $0 {start|stop}" 2>&1678 exit 1679 ;;680 681 esac682 683 exit 0684 685 686ACPI integration687----------------688 689Dax Kelson submitted this so that the ACPI acpid daemon will690kick off the laptop_mode script and run hdparm. The part that691automatically disables laptop mode when the battery is low was692written by Jan Topinski.693 694/etc/acpi/events/ac_adapter::695 696 event=ac_adapter697 action=/etc/acpi/actions/ac.sh %e698 699/etc/acpi/events/battery::700 701 event=battery.*702 action=/etc/acpi/actions/battery.sh %e703 704/etc/acpi/actions/ac.sh::705 706 #!/bin/bash707 708 # ac on/offline event handler709 710 status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/$2/state`711 712 case $status in713 "on-line")714 /sbin/laptop_mode stop715 exit 0716 ;;717 "off-line")718 /sbin/laptop_mode start719 exit 0720 ;;721 esac722 723 724/etc/acpi/actions/battery.sh::725 726 #! /bin/bash727 728 # Automatically disable laptop mode when the battery almost runs out.729 730 BATT_INFO=/proc/acpi/battery/$2/state731 732 if [[ -f /proc/sys/vm/laptop_mode ]]733 then734 LM=`cat /proc/sys/vm/laptop_mode`735 if [[ $LM -gt 0 ]]736 then737 if [[ -f $BATT_INFO ]]738 then739 # Source the config file only now that we know we need740 if [ -f /etc/default/laptop-mode ] ; then741 # Debian742 . /etc/default/laptop-mode743 elif [ -f /etc/sysconfig/laptop-mode ] ; then744 # Others745 . /etc/sysconfig/laptop-mode746 fi747 MINIMUM_BATTERY_MINUTES=${MINIMUM_BATTERY_MINUTES:-'10'}748 749 ACTION="`cat $BATT_INFO | grep charging | cut -c 26-`"750 if [[ ACTION -eq "discharging" ]]751 then752 PRESENT_RATE=`cat $BATT_INFO | grep "present rate:" | sed "s/.* \([0-9][0-9]* \).*/\1/" `753 REMAINING=`cat $BATT_INFO | grep "remaining capacity:" | sed "s/.* \([0-9][0-9]* \).*/\1/" `754 fi755 if (($REMAINING * 60 / $PRESENT_RATE < $MINIMUM_BATTERY_MINUTES))756 then757 /sbin/laptop_mode stop758 fi759 else760 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."761 fi762 fi763 fi764 765 766Monitoring tool767---------------768 769Bartek Kania submitted this, it can be used to measure how much time your disk770spends spun up/down. See tools/laptop/dslm/dslm.c771