laptop-mode.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. ===============================================
  2. How to conserve battery power using laptop-mode
  3. ===============================================
  4. Document Author: Bart Samwel (bart@samwel.tk)
  5. Date created: January 2, 2004
  6. Last modified: December 06, 2004
  7. Introduction
  8. ------------
  9. Laptop mode is used to minimize the time that the hard disk needs to be spun up,
  10. to conserve battery power on laptops. It has been reported to cause significant
  11. power savings.
  12. .. Contents
  13. * Introduction
  14. * Installation
  15. * Caveats
  16. * The Details
  17. * Tips & Tricks
  18. * Control script
  19. * ACPI integration
  20. * Monitoring tool
  21. Installation
  22. ------------
  23. To use laptop mode, you don't need to set any kernel configuration options
  24. or anything. Simply install all the files included in this document, and
  25. laptop mode will automatically be started when you're on battery. For
  26. your convenience, a tarball containing an installer can be downloaded at:
  27. http://www.samwel.tk/laptop_mode/laptop_mode/
  28. To configure laptop mode, you need to edit the configuration file, which is
  29. located in /etc/default/laptop-mode on Debian-based systems, or in
  30. /etc/sysconfig/laptop-mode on other systems.
  31. Unfortunately, automatic enabling of laptop mode does not work for
  32. laptops that don't have ACPI. On those laptops, you need to start laptop
  33. mode manually. To start laptop mode, run "laptop_mode start", and to
  34. stop it, run "laptop_mode stop". (Note: The laptop mode tools package now
  35. has experimental support for APM, you might want to try that first.)
  36. Caveats
  37. -------
  38. * The downside of laptop mode is that you have a chance of losing up to 10
  39. minutes of work. If you cannot afford this, don't use it! The supplied ACPI
  40. scripts automatically turn off laptop mode when the battery almost runs out,
  41. so that you won't lose any data at the end of your battery life.
  42. * Most desktop hard drives have a very limited lifetime measured in spindown
  43. cycles, typically about 50.000 times (it's usually listed on the spec sheet).
  44. Check your drive's rating, and don't wear down your drive's lifetime if you
  45. don't need to.
  46. * If you mount some of your ext3/reiserfs filesystems with the -n option, then
  47. the control script will not be able to remount them correctly. You must set
  48. DO_REMOUNTS=0 in the control script, otherwise it will remount them with the
  49. wrong options -- or it will fail because it cannot write to /etc/mtab.
  50. * If you have your filesystems listed as type "auto" in fstab, like I did, then
  51. the control script will not recognize them as filesystems that need remounting.
  52. You must list the filesystems with their true type instead.
  53. * It has been reported that some versions of the mutt mail client use file access
  54. times to determine whether a folder contains new mail. If you use mutt and
  55. experience this, you must disable the noatime remounting by setting the option
  56. DO_REMOUNT_NOATIME to 0 in the configuration file.
  57. The Details
  58. -----------
  59. Laptop mode is controlled by the knob /proc/sys/vm/laptop_mode. This knob is
  60. present for all kernels that have the laptop mode patch, regardless of any
  61. configuration options. When the knob is set, any physical disk I/O (that might
  62. have caused the hard disk to spin up) causes Linux to flush all dirty blocks. The
  63. result of this is that after a disk has spun down, it will not be spun up
  64. anymore to write dirty blocks, because those blocks had already been written
  65. immediately after the most recent read operation. The value of the laptop_mode
  66. knob determines the time between the occurrence of disk I/O and when the flush
  67. is triggered. A sensible value for the knob is 5 seconds. Setting the knob to
  68. 0 disables laptop mode.
  69. To increase the effectiveness of the laptop_mode strategy, the laptop_mode
  70. control script increases dirty_expire_centisecs and dirty_writeback_centisecs in
  71. /proc/sys/vm to about 10 minutes (by default), which means that pages that are
  72. dirtied are not forced to be written to disk as often. The control script also
  73. changes the dirty background ratio, so that background writeback of dirty pages
  74. is not done anymore. Combined with a higher commit value (also 10 minutes) for
  75. ext3 or ReiserFS filesystems (also done automatically by the control script),
  76. this results in concentration of disk activity in a small time interval which
  77. occurs only once every 10 minutes, or whenever the disk is forced to spin up by
  78. a cache miss. The disk can then be spun down in the periods of inactivity.
  79. Configuration
  80. -------------
  81. The laptop mode configuration file is located in /etc/default/laptop-mode on
  82. Debian-based systems, or in /etc/sysconfig/laptop-mode on other systems. It
  83. contains the following options:
  84. MAX_AGE:
  85. Maximum time, in seconds, of hard drive spindown time that you are
  86. comfortable with. Worst case, it's possible that you could lose this
  87. amount of work if your battery fails while you're in laptop mode.
  88. MINIMUM_BATTERY_MINUTES:
  89. Automatically disable laptop mode if the remaining number of minutes of
  90. battery power is less than this value. Default is 10 minutes.
  91. AC_HD/BATT_HD:
  92. The idle timeout that should be set on your hard drive when laptop mode
  93. is active (BATT_HD) and when it is not active (AC_HD). The defaults are
  94. 20 seconds (value 4) for BATT_HD and 2 hours (value 244) for AC_HD. The
  95. possible values are those listed in the manual page for "hdparm" for the
  96. "-S" option.
  97. HD:
  98. The devices for which the spindown timeout should be adjusted by laptop mode.
  99. Default is /dev/hda. If you specify multiple devices, separate them by a space.
  100. READAHEAD:
  101. Disk readahead, in 512-byte sectors, while laptop mode is active. A large
  102. readahead can prevent disk accesses for things like executable pages (which are
  103. loaded on demand while the application executes) and sequentially accessed data
  104. (MP3s).
  105. DO_REMOUNTS:
  106. The control script automatically remounts any mounted journaled filesystems
  107. with appropriate commit interval options. When this option is set to 0, this
  108. feature is disabled.
  109. DO_REMOUNT_NOATIME:
  110. When remounting, should the filesystems be remounted with the noatime option?
  111. Normally, this is set to "1" (enabled), but there may be programs that require
  112. access time recording.
  113. DIRTY_RATIO:
  114. The percentage of memory that is allowed to contain "dirty" or unsaved data
  115. before a writeback is forced, while laptop mode is active. Corresponds to
  116. the /proc/sys/vm/dirty_ratio sysctl.
  117. DIRTY_BACKGROUND_RATIO:
  118. The percentage of memory that is allowed to contain "dirty" or unsaved data
  119. after a forced writeback is done due to an exceeding of DIRTY_RATIO. Set
  120. this nice and low. This corresponds to the /proc/sys/vm/dirty_background_ratio
  121. sysctl.
  122. Note that the behaviour of dirty_background_ratio is quite different
  123. when laptop mode is active and when it isn't. When laptop mode is inactive,
  124. dirty_background_ratio is the threshold percentage at which background writeouts
  125. start taking place. When laptop mode is active, however, background writeouts
  126. are disabled, and the dirty_background_ratio only determines how much writeback
  127. is done when dirty_ratio is reached.
  128. DO_CPU:
  129. Enable CPU frequency scaling when in laptop mode. (Requires CPUFreq to be setup.
  130. See Documentation/admin-guide/pm/cpufreq.rst for more info. Disabled by default.)
  131. CPU_MAXFREQ:
  132. When on battery, what is the maximum CPU speed that the system should use? Legal
  133. values are "slowest" for the slowest speed that your CPU is able to operate at,
  134. or a value listed in /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies.
  135. Tips & Tricks
  136. -------------
  137. * Bartek Kania reports getting up to 50 minutes of extra battery life (on top
  138. of his regular 3 to 3.5 hours) using a spindown time of 5 seconds (BATT_HD=1).
  139. * You can spin down the disk while playing MP3, by setting disk readahead
  140. to 8MB (READAHEAD=16384). Effectively, the disk will read a complete MP3 at
  141. once, and will then spin down while the MP3 is playing. (Thanks to Bartek
  142. Kania.)
  143. * Drew Scott Daniels observed: "I don't know why, but when I decrease the number
  144. of colours that my display uses it consumes less battery power. I've seen
  145. this on powerbooks too. I hope that this is a piece of information that
  146. might be useful to the Laptop Mode patch or its users."
  147. * In syslog.conf, you can prefix entries with a dash `-` to omit syncing the
  148. file after every logging. When you're using laptop-mode and your disk doesn't
  149. spin down, this is a likely culprit.
  150. * Richard Atterer observed that laptop mode does not work well with noflushd
  151. (http://noflushd.sourceforge.net/), it seems that noflushd prevents laptop-mode
  152. from doing its thing.
  153. * If you're worried about your data, you might want to consider using a USB
  154. memory stick or something like that as a "working area". (Be aware though
  155. that flash memory can only handle a limited number of writes, and overuse
  156. may wear out your memory stick pretty quickly. Do _not_ use journalling
  157. filesystems on flash memory sticks.)
  158. Configuration file for control and ACPI battery scripts
  159. -------------------------------------------------------
  160. This allows the tunables to be changed for the scripts via an external
  161. configuration file
  162. It should be installed as /etc/default/laptop-mode on Debian, and as
  163. /etc/sysconfig/laptop-mode on Red Hat, SUSE, Mandrake, and other work-alikes.
  164. Config file::
  165. # Maximum time, in seconds, of hard drive spindown time that you are
  166. # comfortable with. Worst case, it's possible that you could lose this
  167. # amount of work if your battery fails you while in laptop mode.
  168. #MAX_AGE=600
  169. # Automatically disable laptop mode when the number of minutes of battery
  170. # that you have left goes below this threshold.
  171. MINIMUM_BATTERY_MINUTES=10
  172. # Read-ahead, in 512-byte sectors. You can spin down the disk while playing MP3/OGG
  173. # by setting the disk readahead to 8MB (READAHEAD=16384). Effectively, the disk
  174. # will read a complete MP3 at once, and will then spin down while the MP3/OGG is
  175. # playing.
  176. #READAHEAD=4096
  177. # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
  178. #DO_REMOUNTS=1
  179. # And shall we add the "noatime" option to that as well? (1=yes)
  180. #DO_REMOUNT_NOATIME=1
  181. # Dirty synchronous ratio. At this percentage of dirty pages the process
  182. # which
  183. # calls write() does its own writeback
  184. #DIRTY_RATIO=40
  185. #
  186. # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been
  187. # exceeded, the kernel will wake flusher threads which will then reduce the
  188. # amount of dirty memory to dirty_background_ratio. Set this nice and low,
  189. # so once some writeout has commenced, we do a lot of it.
  190. #
  191. #DIRTY_BACKGROUND_RATIO=5
  192. # kernel default dirty buffer age
  193. #DEF_AGE=30
  194. #DEF_UPDATE=5
  195. #DEF_DIRTY_BACKGROUND_RATIO=10
  196. #DEF_DIRTY_RATIO=40
  197. #DEF_XFS_AGE_BUFFER=15
  198. #DEF_XFS_SYNC_INTERVAL=30
  199. #DEF_XFS_BUFD_INTERVAL=1
  200. # This must be adjusted manually to the value of HZ in the running kernel
  201. # on 2.4, until the XFS people change their 2.4 external interfaces to work in
  202. # centisecs. This can be automated, but it's a work in progress that still
  203. # needs# some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for
  204. # external interfaces, and that is currently always set to 100. So you don't
  205. # need to change this on 2.6.
  206. #XFS_HZ=100
  207. # Should the maximum CPU frequency be adjusted down while on battery?
  208. # Requires CPUFreq to be setup.
  209. # See Documentation/admin-guide/pm/cpufreq.rst for more info
  210. #DO_CPU=0
  211. # When on battery what is the maximum CPU speed that the system should
  212. # use? Legal values are "slowest" for the slowest speed that your
  213. # CPU is able to operate at, or a value listed in:
  214. # /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
  215. # Only applicable if DO_CPU=1.
  216. #CPU_MAXFREQ=slowest
  217. # Idle timeout for your hard drive (man hdparm for valid values, -S option)
  218. # Default is 2 hours on AC (AC_HD=244) and 20 seconds for battery (BATT_HD=4).
  219. #AC_HD=244
  220. #BATT_HD=4
  221. # The drives for which to adjust the idle timeout. Separate them by a space,
  222. # e.g. HD="/dev/hda /dev/hdb".
  223. #HD="/dev/hda"
  224. # Set the spindown timeout on a hard drive?
  225. #DO_HD=1
  226. Control script
  227. --------------
  228. Please note that this control script works for the Linux 2.4 and 2.6 series (thanks
  229. to Kiko Piris).
  230. Control script::
  231. #!/bin/bash
  232. # start or stop laptop_mode, best run by a power management daemon when
  233. # ac gets connected/disconnected from a laptop
  234. #
  235. # install as /sbin/laptop_mode
  236. #
  237. # Contributors to this script: Kiko Piris
  238. # Bart Samwel
  239. # Micha Feigin
  240. # Andrew Morton
  241. # Herve Eychenne
  242. # Dax Kelson
  243. #
  244. # Original Linux 2.4 version by: Jens Axboe
  245. #############################################################################
  246. # Source config
  247. if [ -f /etc/default/laptop-mode ] ; then
  248. # Debian
  249. . /etc/default/laptop-mode
  250. elif [ -f /etc/sysconfig/laptop-mode ] ; then
  251. # Others
  252. . /etc/sysconfig/laptop-mode
  253. fi
  254. # Don't raise an error if the config file is incomplete
  255. # set defaults instead:
  256. # Maximum time, in seconds, of hard drive spindown time that you are
  257. # comfortable with. Worst case, it's possible that you could lose this
  258. # amount of work if your battery fails you while in laptop mode.
  259. MAX_AGE=${MAX_AGE:-'600'}
  260. # Read-ahead, in kilobytes
  261. READAHEAD=${READAHEAD:-'4096'}
  262. # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
  263. DO_REMOUNTS=${DO_REMOUNTS:-'1'}
  264. # And shall we add the "noatime" option to that as well? (1=yes)
  265. DO_REMOUNT_NOATIME=${DO_REMOUNT_NOATIME:-'1'}
  266. # Shall we adjust the idle timeout on a hard drive?
  267. DO_HD=${DO_HD:-'1'}
  268. # Adjust idle timeout on which hard drive?
  269. HD="${HD:-'/dev/hda'}"
  270. # spindown time for HD (hdparm -S values)
  271. AC_HD=${AC_HD:-'244'}
  272. BATT_HD=${BATT_HD:-'4'}
  273. # Dirty synchronous ratio. At this percentage of dirty pages the process which
  274. # calls write() does its own writeback
  275. DIRTY_RATIO=${DIRTY_RATIO:-'40'}
  276. # cpu frequency scaling
  277. # See Documentation/admin-guide/pm/cpufreq.rst for more info
  278. DO_CPU=${CPU_MANAGE:-'0'}
  279. CPU_MAXFREQ=${CPU_MAXFREQ:-'slowest'}
  280. #
  281. # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been
  282. # exceeded, the kernel will wake flusher threads which will then reduce the
  283. # amount of dirty memory to dirty_background_ratio. Set this nice and low,
  284. # so once some writeout has commenced, we do a lot of it.
  285. #
  286. DIRTY_BACKGROUND_RATIO=${DIRTY_BACKGROUND_RATIO:-'5'}
  287. # kernel default dirty buffer age
  288. DEF_AGE=${DEF_AGE:-'30'}
  289. DEF_UPDATE=${DEF_UPDATE:-'5'}
  290. DEF_DIRTY_BACKGROUND_RATIO=${DEF_DIRTY_BACKGROUND_RATIO:-'10'}
  291. DEF_DIRTY_RATIO=${DEF_DIRTY_RATIO:-'40'}
  292. DEF_XFS_AGE_BUFFER=${DEF_XFS_AGE_BUFFER:-'15'}
  293. DEF_XFS_SYNC_INTERVAL=${DEF_XFS_SYNC_INTERVAL:-'30'}
  294. DEF_XFS_BUFD_INTERVAL=${DEF_XFS_BUFD_INTERVAL:-'1'}
  295. # This must be adjusted manually to the value of HZ in the running kernel
  296. # on 2.4, until the XFS people change their 2.4 external interfaces to work in
  297. # centisecs. This can be automated, but it's a work in progress that still needs
  298. # some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for external
  299. # interfaces, and that is currently always set to 100. So you don't need to
  300. # change this on 2.6.
  301. XFS_HZ=${XFS_HZ:-'100'}
  302. #############################################################################
  303. KLEVEL="$(uname -r |
  304. {
  305. IFS='.' read a b c
  306. echo $a.$b
  307. }
  308. )"
  309. case "$KLEVEL" in
  310. "2.4"|"2.6")
  311. ;;
  312. *)
  313. echo "Unhandled kernel version: $KLEVEL ('uname -r' = '$(uname -r)')" >&2
  314. exit 1
  315. ;;
  316. esac
  317. if [ ! -e /proc/sys/vm/laptop_mode ] ; then
  318. echo "Kernel is not patched with laptop_mode patch." >&2
  319. exit 1
  320. fi
  321. if [ ! -w /proc/sys/vm/laptop_mode ] ; then
  322. echo "You do not have enough privileges to enable laptop_mode." >&2
  323. exit 1
  324. fi
  325. # Remove an option (the first parameter) of the form option=<number> from
  326. # a mount options string (the rest of the parameters).
  327. parse_mount_opts () {
  328. OPT="$1"
  329. shift
  330. echo ",$*," | sed \
  331. -e 's/,'"$OPT"'=[0-9]*,/,/g' \
  332. -e 's/,,*/,/g' \
  333. -e 's/^,//' \
  334. -e 's/,$//'
  335. }
  336. # Remove an option (the first parameter) without any arguments from
  337. # a mount option string (the rest of the parameters).
  338. parse_nonumber_mount_opts () {
  339. OPT="$1"
  340. shift
  341. echo ",$*," | sed \
  342. -e 's/,'"$OPT"',/,/g' \
  343. -e 's/,,*/,/g' \
  344. -e 's/^,//' \
  345. -e 's/,$//'
  346. }
  347. # Find out the state of a yes/no option (e.g. "atime"/"noatime") in
  348. # fstab for a given filesystem, and use this state to replace the
  349. # value of the option in another mount options string. The device
  350. # is the first argument, the option name the second, and the default
  351. # value the third. The remainder is the mount options string.
  352. #
  353. # Example:
  354. # parse_yesno_opts_wfstab /dev/hda1 atime atime defaults,noatime
  355. #
  356. # If fstab contains, say, "rw" for this filesystem, then the result
  357. # will be "defaults,atime".
  358. parse_yesno_opts_wfstab () {
  359. L_DEV="$1"
  360. OPT="$2"
  361. DEF_OPT="$3"
  362. shift 3
  363. L_OPTS="$*"
  364. PARSEDOPTS1="$(parse_nonumber_mount_opts $OPT $L_OPTS)"
  365. PARSEDOPTS1="$(parse_nonumber_mount_opts no$OPT $PARSEDOPTS1)"
  366. # Watch for a default atime in fstab
  367. FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
  368. if echo "$FSTAB_OPTS" | grep "$OPT" > /dev/null ; then
  369. # option specified in fstab: extract the value and use it
  370. if echo "$FSTAB_OPTS" | grep "no$OPT" > /dev/null ; then
  371. echo "$PARSEDOPTS1,no$OPT"
  372. else
  373. # no$OPT not found -- so we must have $OPT.
  374. echo "$PARSEDOPTS1,$OPT"
  375. fi
  376. else
  377. # option not specified in fstab -- choose the default.
  378. echo "$PARSEDOPTS1,$DEF_OPT"
  379. fi
  380. }
  381. # Find out the state of a numbered option (e.g. "commit=NNN") in
  382. # fstab for a given filesystem, and use this state to replace the
  383. # value of the option in another mount options string. The device
  384. # is the first argument, and the option name the second. The
  385. # remainder is the mount options string in which the replacement
  386. # must be done.
  387. #
  388. # Example:
  389. # parse_mount_opts_wfstab /dev/hda1 commit defaults,commit=7
  390. #
  391. # If fstab contains, say, "commit=3,rw" for this filesystem, then the
  392. # result will be "rw,commit=3".
  393. parse_mount_opts_wfstab () {
  394. L_DEV="$1"
  395. OPT="$2"
  396. shift 2
  397. L_OPTS="$*"
  398. PARSEDOPTS1="$(parse_mount_opts $OPT $L_OPTS)"
  399. # Watch for a default commit in fstab
  400. FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
  401. if echo "$FSTAB_OPTS" | grep "$OPT=" > /dev/null ; then
  402. # option specified in fstab: extract the value, and use it
  403. echo -n "$PARSEDOPTS1,$OPT="
  404. echo ",$FSTAB_OPTS," | sed \
  405. -e 's/.*,'"$OPT"'=//' \
  406. -e 's/,.*//'
  407. else
  408. # option not specified in fstab: set it to 0
  409. echo "$PARSEDOPTS1,$OPT=0"
  410. fi
  411. }
  412. deduce_fstype () {
  413. MP="$1"
  414. # My root filesystem unfortunately has
  415. # type "unknown" in /etc/mtab. If we encounter
  416. # "unknown", we try to get the type from fstab.
  417. cat /etc/fstab |
  418. grep -v '^#' |
  419. while read FSTAB_DEV FSTAB_MP FSTAB_FST FSTAB_OPTS FSTAB_DUMP FSTAB_DUMP ; do
  420. if [ "$FSTAB_MP" = "$MP" ]; then
  421. echo $FSTAB_FST
  422. exit 0
  423. fi
  424. done
  425. }
  426. if [ $DO_REMOUNT_NOATIME -eq 1 ] ; then
  427. NOATIME_OPT=",noatime"
  428. fi
  429. case "$1" in
  430. start)
  431. AGE=$((100*$MAX_AGE))
  432. XFS_AGE=$(($XFS_HZ*$MAX_AGE))
  433. echo -n "Starting laptop_mode"
  434. if [ -d /proc/sys/vm/pagebuf ] ; then
  435. # (For 2.4 and early 2.6.)
  436. # This only needs to be set, not reset -- it is only used when
  437. # laptop mode is enabled.
  438. echo $XFS_AGE > /proc/sys/vm/pagebuf/lm_flush_age
  439. echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
  440. elif [ -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
  441. # (A couple of early 2.6 laptop mode patches had these.)
  442. # The same goes for these.
  443. echo $XFS_AGE > /proc/sys/fs/xfs/lm_age_buffer
  444. echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
  445. elif [ -f /proc/sys/fs/xfs/age_buffer ] ; then
  446. # (2.6.6)
  447. # But not for these -- they are also used in normal
  448. # operation.
  449. echo $XFS_AGE > /proc/sys/fs/xfs/age_buffer
  450. echo $XFS_AGE > /proc/sys/fs/xfs/sync_interval
  451. elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
  452. # (2.6.7 upwards)
  453. # And not for these either. These are in centisecs,
  454. # not USER_HZ, so we have to use $AGE, not $XFS_AGE.
  455. echo $AGE > /proc/sys/fs/xfs/age_buffer_centisecs
  456. echo $AGE > /proc/sys/fs/xfs/xfssyncd_centisecs
  457. echo 3000 > /proc/sys/fs/xfs/xfsbufd_centisecs
  458. fi
  459. case "$KLEVEL" in
  460. "2.4")
  461. echo 1 > /proc/sys/vm/laptop_mode
  462. echo "30 500 0 0 $AGE $AGE 60 20 0" > /proc/sys/vm/bdflush
  463. ;;
  464. "2.6")
  465. echo 5 > /proc/sys/vm/laptop_mode
  466. echo "$AGE" > /proc/sys/vm/dirty_writeback_centisecs
  467. echo "$AGE" > /proc/sys/vm/dirty_expire_centisecs
  468. echo "$DIRTY_RATIO" > /proc/sys/vm/dirty_ratio
  469. echo "$DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio
  470. ;;
  471. esac
  472. if [ $DO_REMOUNTS -eq 1 ]; then
  473. cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
  474. PARSEDOPTS="$(parse_mount_opts "$OPTS")"
  475. if [ "$FST" = 'unknown' ]; then
  476. FST=$(deduce_fstype $MP)
  477. fi
  478. case "$FST" in
  479. "ext3"|"reiserfs")
  480. PARSEDOPTS="$(parse_mount_opts commit "$OPTS")"
  481. mount $DEV -t $FST $MP -o remount,$PARSEDOPTS,commit=$MAX_AGE$NOATIME_OPT
  482. ;;
  483. "xfs")
  484. mount $DEV -t $FST $MP -o remount,$OPTS$NOATIME_OPT
  485. ;;
  486. esac
  487. if [ -b $DEV ] ; then
  488. blockdev --setra $(($READAHEAD * 2)) $DEV
  489. fi
  490. done
  491. fi
  492. if [ $DO_HD -eq 1 ] ; then
  493. for THISHD in $HD ; do
  494. /sbin/hdparm -S $BATT_HD $THISHD > /dev/null 2>&1
  495. /sbin/hdparm -B 1 $THISHD > /dev/null 2>&1
  496. done
  497. fi
  498. if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
  499. if [ $CPU_MAXFREQ = 'slowest' ]; then
  500. CPU_MAXFREQ=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
  501. fi
  502. echo $CPU_MAXFREQ > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
  503. fi
  504. echo "."
  505. ;;
  506. stop)
  507. U_AGE=$((100*$DEF_UPDATE))
  508. B_AGE=$((100*$DEF_AGE))
  509. echo -n "Stopping laptop_mode"
  510. echo 0 > /proc/sys/vm/laptop_mode
  511. if [ -f /proc/sys/fs/xfs/age_buffer -a ! -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
  512. # These need to be restored, if there are no lm_*.
  513. echo $(($XFS_HZ*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer
  514. echo $(($XFS_HZ*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/sync_interval
  515. elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
  516. # These need to be restored as well.
  517. echo $((100*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer_centisecs
  518. echo $((100*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/xfssyncd_centisecs
  519. echo $((100*$DEF_XFS_BUFD_INTERVAL)) > /proc/sys/fs/xfs/xfsbufd_centisecs
  520. fi
  521. case "$KLEVEL" in
  522. "2.4")
  523. echo "30 500 0 0 $U_AGE $B_AGE 60 20 0" > /proc/sys/vm/bdflush
  524. ;;
  525. "2.6")
  526. echo "$U_AGE" > /proc/sys/vm/dirty_writeback_centisecs
  527. echo "$B_AGE" > /proc/sys/vm/dirty_expire_centisecs
  528. echo "$DEF_DIRTY_RATIO" > /proc/sys/vm/dirty_ratio
  529. echo "$DEF_DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio
  530. ;;
  531. esac
  532. if [ $DO_REMOUNTS -eq 1 ] ; then
  533. cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
  534. # Reset commit and atime options to defaults.
  535. if [ "$FST" = 'unknown' ]; then
  536. FST=$(deduce_fstype $MP)
  537. fi
  538. case "$FST" in
  539. "ext3"|"reiserfs")
  540. PARSEDOPTS="$(parse_mount_opts_wfstab $DEV commit $OPTS)"
  541. PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $PARSEDOPTS)"
  542. mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
  543. ;;
  544. "xfs")
  545. PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $OPTS)"
  546. mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
  547. ;;
  548. esac
  549. if [ -b $DEV ] ; then
  550. blockdev --setra 256 $DEV
  551. fi
  552. done
  553. fi
  554. if [ $DO_HD -eq 1 ] ; then
  555. for THISHD in $HD ; do
  556. /sbin/hdparm -S $AC_HD $THISHD > /dev/null 2>&1
  557. /sbin/hdparm -B 255 $THISHD > /dev/null 2>&1
  558. done
  559. fi
  560. if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
  561. echo `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq` > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
  562. fi
  563. echo "."
  564. ;;
  565. *)
  566. echo "Usage: $0 {start|stop}" 2>&1
  567. exit 1
  568. ;;
  569. esac
  570. exit 0
  571. ACPI integration
  572. ----------------
  573. Dax Kelson submitted this so that the ACPI acpid daemon will
  574. kick off the laptop_mode script and run hdparm. The part that
  575. automatically disables laptop mode when the battery is low was
  576. written by Jan Topinski.
  577. /etc/acpi/events/ac_adapter::
  578. event=ac_adapter
  579. action=/etc/acpi/actions/ac.sh %e
  580. /etc/acpi/events/battery::
  581. event=battery.*
  582. action=/etc/acpi/actions/battery.sh %e
  583. /etc/acpi/actions/ac.sh::
  584. #!/bin/bash
  585. # ac on/offline event handler
  586. status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/$2/state`
  587. case $status in
  588. "on-line")
  589. /sbin/laptop_mode stop
  590. exit 0
  591. ;;
  592. "off-line")
  593. /sbin/laptop_mode start
  594. exit 0
  595. ;;
  596. esac
  597. /etc/acpi/actions/battery.sh::
  598. #! /bin/bash
  599. # Automatically disable laptop mode when the battery almost runs out.
  600. BATT_INFO=/proc/acpi/battery/$2/state
  601. if [[ -f /proc/sys/vm/laptop_mode ]]
  602. then
  603. LM=`cat /proc/sys/vm/laptop_mode`
  604. if [[ $LM -gt 0 ]]
  605. then
  606. if [[ -f $BATT_INFO ]]
  607. then
  608. # Source the config file only now that we know we need
  609. if [ -f /etc/default/laptop-mode ] ; then
  610. # Debian
  611. . /etc/default/laptop-mode
  612. elif [ -f /etc/sysconfig/laptop-mode ] ; then
  613. # Others
  614. . /etc/sysconfig/laptop-mode
  615. fi
  616. MINIMUM_BATTERY_MINUTES=${MINIMUM_BATTERY_MINUTES:-'10'}
  617. ACTION="`cat $BATT_INFO | grep charging | cut -c 26-`"
  618. if [[ ACTION -eq "discharging" ]]
  619. then
  620. PRESENT_RATE=`cat $BATT_INFO | grep "present rate:" | sed "s/.* \([0-9][0-9]* \).*/\1/" `
  621. REMAINING=`cat $BATT_INFO | grep "remaining capacity:" | sed "s/.* \([0-9][0-9]* \).*/\1/" `
  622. fi
  623. if (($REMAINING * 60 / $PRESENT_RATE < $MINIMUM_BATTERY_MINUTES))
  624. then
  625. /sbin/laptop_mode stop
  626. fi
  627. else
  628. 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."
  629. fi
  630. fi
  631. fi
  632. Monitoring tool
  633. ---------------
  634. Bartek Kania submitted this, it can be used to measure how much time your disk
  635. spends spun up/down. See tools/laptop/dslm/dslm.c