laptop-mode.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. How to conserve battery power using laptop-mode
  2. -----------------------------------------------
  3. Document Author: Bart Samwel (bart@samwel.tk)
  4. Date created: January 2, 2004
  5. Last modified: December 06, 2004
  6. Introduction
  7. ------------
  8. Laptop mode is used to minimize the time that the hard disk needs to be spun up,
  9. to conserve battery power on laptops. It has been reported to cause significant
  10. power savings.
  11. Contents
  12. --------
  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.xs4all.nl/~bsamwel/laptop_mode/tools/
  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. If you want to find out which process caused the disk to spin up, you can
  80. gather information by setting the flag /proc/sys/vm/block_dump. When this flag
  81. is set, Linux reports all disk read and write operations that take place, and
  82. all block dirtyings done to files. This makes it possible to debug why a disk
  83. needs to spin up, and to increase battery life even more. The output of
  84. block_dump is written to the kernel output, and it can be retrieved using
  85. "dmesg". When you use block_dump and your kernel logging level also includes
  86. kernel debugging messages, you probably want to turn off klogd, otherwise
  87. the output of block_dump will be logged, causing disk activity that is not
  88. normally there.
  89. Configuration
  90. -------------
  91. The laptop mode configuration file is located in /etc/default/laptop-mode on
  92. Debian-based systems, or in /etc/sysconfig/laptop-mode on other systems. It
  93. contains the following options:
  94. MAX_AGE:
  95. Maximum time, in seconds, of hard drive spindown time that you are
  96. comfortable with. Worst case, it's possible that you could lose this
  97. amount of work if your battery fails while you're in laptop mode.
  98. MINIMUM_BATTERY_MINUTES:
  99. Automatically disable laptop mode if the remaining number of minutes of
  100. battery power is less than this value. Default is 10 minutes.
  101. AC_HD/BATT_HD:
  102. The idle timeout that should be set on your hard drive when laptop mode
  103. is active (BATT_HD) and when it is not active (AC_HD). The defaults are
  104. 20 seconds (value 4) for BATT_HD and 2 hours (value 244) for AC_HD. The
  105. possible values are those listed in the manual page for "hdparm" for the
  106. "-S" option.
  107. HD:
  108. The devices for which the spindown timeout should be adjusted by laptop mode.
  109. Default is /dev/hda. If you specify multiple devices, separate them by a space.
  110. READAHEAD:
  111. Disk readahead, in 512-byte sectors, while laptop mode is active. A large
  112. readahead can prevent disk accesses for things like executable pages (which are
  113. loaded on demand while the application executes) and sequentially accessed data
  114. (MP3s).
  115. DO_REMOUNTS:
  116. The control script automatically remounts any mounted journaled filesystems
  117. with appropriate commit interval options. When this option is set to 0, this
  118. feature is disabled.
  119. DO_REMOUNT_NOATIME:
  120. When remounting, should the filesystems be remounted with the noatime option?
  121. Normally, this is set to "1" (enabled), but there may be programs that require
  122. access time recording.
  123. DIRTY_RATIO:
  124. The percentage of memory that is allowed to contain "dirty" or unsaved data
  125. before a writeback is forced, while laptop mode is active. Corresponds to
  126. the /proc/sys/vm/dirty_ratio sysctl.
  127. DIRTY_BACKGROUND_RATIO:
  128. The percentage of memory that is allowed to contain "dirty" or unsaved data
  129. after a forced writeback is done due to an exceeding of DIRTY_RATIO. Set
  130. this nice and low. This corresponds to the /proc/sys/vm/dirty_background_ratio
  131. sysctl.
  132. Note that the behaviour of dirty_background_ratio is quite different
  133. when laptop mode is active and when it isn't. When laptop mode is inactive,
  134. dirty_background_ratio is the threshold percentage at which background writeouts
  135. start taking place. When laptop mode is active, however, background writeouts
  136. are disabled, and the dirty_background_ratio only determines how much writeback
  137. is done when dirty_ratio is reached.
  138. DO_CPU:
  139. Enable CPU frequency scaling when in laptop mode. (Requires CPUFreq to be setup.
  140. See Documentation/cpu-freq/user-guide.txt for more info. Disabled by default.)
  141. CPU_MAXFREQ:
  142. When on battery, what is the maximum CPU speed that the system should use? Legal
  143. values are "slowest" for the slowest speed that your CPU is able to operate at,
  144. or a value listed in /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies.
  145. Tips & Tricks
  146. -------------
  147. * Bartek Kania reports getting up to 50 minutes of extra battery life (on top
  148. of his regular 3 to 3.5 hours) using a spindown time of 5 seconds (BATT_HD=1).
  149. * You can spin down the disk while playing MP3, by setting disk readahead
  150. to 8MB (READAHEAD=16384). Effectively, the disk will read a complete MP3 at
  151. once, and will then spin down while the MP3 is playing. (Thanks to Bartek
  152. Kania.)
  153. * Drew Scott Daniels observed: "I don't know why, but when I decrease the number
  154. of colours that my display uses it consumes less battery power. I've seen
  155. this on powerbooks too. I hope that this is a piece of information that
  156. might be useful to the Laptop Mode patch or it's users."
  157. * In syslog.conf, you can prefix entries with a dash ``-'' to omit syncing the
  158. file after every logging. When you're using laptop-mode and your disk doesn't
  159. spin down, this is a likely culprit.
  160. * Richard Atterer observed that laptop mode does not work well with noflushd
  161. (http://noflushd.sourceforge.net/), it seems that noflushd prevents laptop-mode
  162. from doing its thing.
  163. * If you're worried about your data, you might want to consider using a USB
  164. memory stick or something like that as a "working area". (Be aware though
  165. that flash memory can only handle a limited number of writes, and overuse
  166. may wear out your memory stick pretty quickly. Do _not_ use journalling
  167. filesystems on flash memory sticks.)
  168. Configuration file for control and ACPI battery scripts
  169. -------------------------------------------------------
  170. This allows the tunables to be changed for the scripts via an external
  171. configuration file
  172. It should be installed as /etc/default/laptop-mode on Debian, and as
  173. /etc/sysconfig/laptop-mode on Red Hat, SUSE, Mandrake, and other work-alikes.
  174. --------------------CONFIG FILE BEGIN-------------------------------------------
  175. # Maximum time, in seconds, of hard drive spindown time that you are
  176. # comfortable with. Worst case, it's possible that you could lose this
  177. # amount of work if your battery fails you while in laptop mode.
  178. #MAX_AGE=600
  179. # Automatically disable laptop mode when the number of minutes of battery
  180. # that you have left goes below this threshold.
  181. MINIMUM_BATTERY_MINUTES=10
  182. # Read-ahead, in 512-byte sectors. You can spin down the disk while playing MP3/OGG
  183. # by setting the disk readahead to 8MB (READAHEAD=16384). Effectively, the disk
  184. # will read a complete MP3 at once, and will then spin down while the MP3/OGG is
  185. # playing.
  186. #READAHEAD=4096
  187. # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
  188. #DO_REMOUNTS=1
  189. # And shall we add the "noatime" option to that as well? (1=yes)
  190. #DO_REMOUNT_NOATIME=1
  191. # Dirty synchronous ratio. At this percentage of dirty pages the process
  192. # which
  193. # calls write() does its own writeback
  194. #DIRTY_RATIO=40
  195. #
  196. # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been
  197. # exceeded, the kernel will wake pdflush which will then reduce the amount
  198. # of dirty memory to dirty_background_ratio. Set this nice and low, so once
  199. # some writeout has commenced, we do a lot of it.
  200. #
  201. #DIRTY_BACKGROUND_RATIO=5
  202. # kernel default dirty buffer age
  203. #DEF_AGE=30
  204. #DEF_UPDATE=5
  205. #DEF_DIRTY_BACKGROUND_RATIO=10
  206. #DEF_DIRTY_RATIO=40
  207. #DEF_XFS_AGE_BUFFER=15
  208. #DEF_XFS_SYNC_INTERVAL=30
  209. #DEF_XFS_BUFD_INTERVAL=1
  210. # This must be adjusted manually to the value of HZ in the running kernel
  211. # on 2.4, until the XFS people change their 2.4 external interfaces to work in
  212. # centisecs. This can be automated, but it's a work in progress that still
  213. # needs# some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for
  214. # external interfaces, and that is currently always set to 100. So you don't
  215. # need to change this on 2.6.
  216. #XFS_HZ=100
  217. # Should the maximum CPU frequency be adjusted down while on battery?
  218. # Requires CPUFreq to be setup.
  219. # See Documentation/cpu-freq/user-guide.txt for more info
  220. #DO_CPU=0
  221. # When on battery what is the maximum CPU speed that the system should
  222. # use? Legal values are "slowest" for the slowest speed that your
  223. # CPU is able to operate at, or a value listed in:
  224. # /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
  225. # Only applicable if DO_CPU=1.
  226. #CPU_MAXFREQ=slowest
  227. # Idle timeout for your hard drive (man hdparm for valid values, -S option)
  228. # Default is 2 hours on AC (AC_HD=244) and 20 seconds for battery (BATT_HD=4).
  229. #AC_HD=244
  230. #BATT_HD=4
  231. # The drives for which to adjust the idle timeout. Separate them by a space,
  232. # e.g. HD="/dev/hda /dev/hdb".
  233. #HD="/dev/hda"
  234. # Set the spindown timeout on a hard drive?
  235. #DO_HD=1
  236. --------------------CONFIG FILE END---------------------------------------------
  237. Control script
  238. --------------
  239. Please note that this control script works for the Linux 2.4 and 2.6 series (thanks
  240. to Kiko Piris).
  241. --------------------CONTROL SCRIPT BEGIN----------------------------------------
  242. #!/bin/bash
  243. # start or stop laptop_mode, best run by a power management daemon when
  244. # ac gets connected/disconnected from a laptop
  245. #
  246. # install as /sbin/laptop_mode
  247. #
  248. # Contributors to this script: Kiko Piris
  249. # Bart Samwel
  250. # Micha Feigin
  251. # Andrew Morton
  252. # Herve Eychenne
  253. # Dax Kelson
  254. #
  255. # Original Linux 2.4 version by: Jens Axboe
  256. #############################################################################
  257. # Source config
  258. if [ -f /etc/default/laptop-mode ] ; then
  259. # Debian
  260. . /etc/default/laptop-mode
  261. elif [ -f /etc/sysconfig/laptop-mode ] ; then
  262. # Others
  263. . /etc/sysconfig/laptop-mode
  264. fi
  265. # Don't raise an error if the config file is incomplete
  266. # set defaults instead:
  267. # Maximum time, in seconds, of hard drive spindown time that you are
  268. # comfortable with. Worst case, it's possible that you could lose this
  269. # amount of work if your battery fails you while in laptop mode.
  270. MAX_AGE=${MAX_AGE:-'600'}
  271. # Read-ahead, in kilobytes
  272. READAHEAD=${READAHEAD:-'4096'}
  273. # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
  274. DO_REMOUNTS=${DO_REMOUNTS:-'1'}
  275. # And shall we add the "noatime" option to that as well? (1=yes)
  276. DO_REMOUNT_NOATIME=${DO_REMOUNT_NOATIME:-'1'}
  277. # Shall we adjust the idle timeout on a hard drive?
  278. DO_HD=${DO_HD:-'1'}
  279. # Adjust idle timeout on which hard drive?
  280. HD="${HD:-'/dev/hda'}"
  281. # spindown time for HD (hdparm -S values)
  282. AC_HD=${AC_HD:-'244'}
  283. BATT_HD=${BATT_HD:-'4'}
  284. # Dirty synchronous ratio. At this percentage of dirty pages the process which
  285. # calls write() does its own writeback
  286. DIRTY_RATIO=${DIRTY_RATIO:-'40'}
  287. # cpu frequency scaling
  288. # See Documentation/cpu-freq/user-guide.txt for more info
  289. DO_CPU=${CPU_MANAGE:-'0'}
  290. CPU_MAXFREQ=${CPU_MAXFREQ:-'slowest'}
  291. #
  292. # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been
  293. # exceeded, the kernel will wake pdflush which will then reduce the amount
  294. # of dirty memory to dirty_background_ratio. Set this nice and low, so once
  295. # some writeout has commenced, we do a lot of it.
  296. #
  297. DIRTY_BACKGROUND_RATIO=${DIRTY_BACKGROUND_RATIO:-'5'}
  298. # kernel default dirty buffer age
  299. DEF_AGE=${DEF_AGE:-'30'}
  300. DEF_UPDATE=${DEF_UPDATE:-'5'}
  301. DEF_DIRTY_BACKGROUND_RATIO=${DEF_DIRTY_BACKGROUND_RATIO:-'10'}
  302. DEF_DIRTY_RATIO=${DEF_DIRTY_RATIO:-'40'}
  303. DEF_XFS_AGE_BUFFER=${DEF_XFS_AGE_BUFFER:-'15'}
  304. DEF_XFS_SYNC_INTERVAL=${DEF_XFS_SYNC_INTERVAL:-'30'}
  305. DEF_XFS_BUFD_INTERVAL=${DEF_XFS_BUFD_INTERVAL:-'1'}
  306. # This must be adjusted manually to the value of HZ in the running kernel
  307. # on 2.4, until the XFS people change their 2.4 external interfaces to work in
  308. # centisecs. This can be automated, but it's a work in progress that still needs
  309. # some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for external
  310. # interfaces, and that is currently always set to 100. So you don't need to
  311. # change this on 2.6.
  312. XFS_HZ=${XFS_HZ:-'100'}
  313. #############################################################################
  314. KLEVEL="$(uname -r |
  315. {
  316. IFS='.' read a b c
  317. echo $a.$b
  318. }
  319. )"
  320. case "$KLEVEL" in
  321. "2.4"|"2.6")
  322. ;;
  323. *)
  324. echo "Unhandled kernel version: $KLEVEL ('uname -r' = '$(uname -r)')" >&2
  325. exit 1
  326. ;;
  327. esac
  328. if [ ! -e /proc/sys/vm/laptop_mode ] ; then
  329. echo "Kernel is not patched with laptop_mode patch." >&2
  330. exit 1
  331. fi
  332. if [ ! -w /proc/sys/vm/laptop_mode ] ; then
  333. echo "You do not have enough privileges to enable laptop_mode." >&2
  334. exit 1
  335. fi
  336. # Remove an option (the first parameter) of the form option=<number> from
  337. # a mount options string (the rest of the parameters).
  338. parse_mount_opts () {
  339. OPT="$1"
  340. shift
  341. echo ",$*," | sed \
  342. -e 's/,'"$OPT"'=[0-9]*,/,/g' \
  343. -e 's/,,*/,/g' \
  344. -e 's/^,//' \
  345. -e 's/,$//'
  346. }
  347. # Remove an option (the first parameter) without any arguments from
  348. # a mount option string (the rest of the parameters).
  349. parse_nonumber_mount_opts () {
  350. OPT="$1"
  351. shift
  352. echo ",$*," | sed \
  353. -e 's/,'"$OPT"',/,/g' \
  354. -e 's/,,*/,/g' \
  355. -e 's/^,//' \
  356. -e 's/,$//'
  357. }
  358. # Find out the state of a yes/no option (e.g. "atime"/"noatime") in
  359. # fstab for a given filesystem, and use this state to replace the
  360. # value of the option in another mount options string. The device
  361. # is the first argument, the option name the second, and the default
  362. # value the third. The remainder is the mount options string.
  363. #
  364. # Example:
  365. # parse_yesno_opts_wfstab /dev/hda1 atime atime defaults,noatime
  366. #
  367. # If fstab contains, say, "rw" for this filesystem, then the result
  368. # will be "defaults,atime".
  369. parse_yesno_opts_wfstab () {
  370. L_DEV="$1"
  371. OPT="$2"
  372. DEF_OPT="$3"
  373. shift 3
  374. L_OPTS="$*"
  375. PARSEDOPTS1="$(parse_nonumber_mount_opts $OPT $L_OPTS)"
  376. PARSEDOPTS1="$(parse_nonumber_mount_opts no$OPT $PARSEDOPTS1)"
  377. # Watch for a default atime in fstab
  378. FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
  379. if echo "$FSTAB_OPTS" | grep "$OPT" > /dev/null ; then
  380. # option specified in fstab: extract the value and use it
  381. if echo "$FSTAB_OPTS" | grep "no$OPT" > /dev/null ; then
  382. echo "$PARSEDOPTS1,no$OPT"
  383. else
  384. # no$OPT not found -- so we must have $OPT.
  385. echo "$PARSEDOPTS1,$OPT"
  386. fi
  387. else
  388. # option not specified in fstab -- choose the default.
  389. echo "$PARSEDOPTS1,$DEF_OPT"
  390. fi
  391. }
  392. # Find out the state of a numbered option (e.g. "commit=NNN") in
  393. # fstab for a given filesystem, and use this state to replace the
  394. # value of the option in another mount options string. The device
  395. # is the first argument, and the option name the second. The
  396. # remainder is the mount options string in which the replacement
  397. # must be done.
  398. #
  399. # Example:
  400. # parse_mount_opts_wfstab /dev/hda1 commit defaults,commit=7
  401. #
  402. # If fstab contains, say, "commit=3,rw" for this filesystem, then the
  403. # result will be "rw,commit=3".
  404. parse_mount_opts_wfstab () {
  405. L_DEV="$1"
  406. OPT="$2"
  407. shift 2
  408. L_OPTS="$*"
  409. PARSEDOPTS1="$(parse_mount_opts $OPT $L_OPTS)"
  410. # Watch for a default commit in fstab
  411. FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
  412. if echo "$FSTAB_OPTS" | grep "$OPT=" > /dev/null ; then
  413. # option specified in fstab: extract the value, and use it
  414. echo -n "$PARSEDOPTS1,$OPT="
  415. echo ",$FSTAB_OPTS," | sed \
  416. -e 's/.*,'"$OPT"'=//' \
  417. -e 's/,.*//'
  418. else
  419. # option not specified in fstab: set it to 0
  420. echo "$PARSEDOPTS1,$OPT=0"
  421. fi
  422. }
  423. deduce_fstype () {
  424. MP="$1"
  425. # My root filesystem unfortunately has
  426. # type "unknown" in /etc/mtab. If we encounter
  427. # "unknown", we try to get the type from fstab.
  428. cat /etc/fstab |
  429. grep -v '^#' |
  430. while read FSTAB_DEV FSTAB_MP FSTAB_FST FSTAB_OPTS FSTAB_DUMP FSTAB_DUMP ; do
  431. if [ "$FSTAB_MP" = "$MP" ]; then
  432. echo $FSTAB_FST
  433. exit 0
  434. fi
  435. done
  436. }
  437. if [ $DO_REMOUNT_NOATIME -eq 1 ] ; then
  438. NOATIME_OPT=",noatime"
  439. fi
  440. case "$1" in
  441. start)
  442. AGE=$((100*$MAX_AGE))
  443. XFS_AGE=$(($XFS_HZ*$MAX_AGE))
  444. echo -n "Starting laptop_mode"
  445. if [ -d /proc/sys/vm/pagebuf ] ; then
  446. # (For 2.4 and early 2.6.)
  447. # This only needs to be set, not reset -- it is only used when
  448. # laptop mode is enabled.
  449. echo $XFS_AGE > /proc/sys/vm/pagebuf/lm_flush_age
  450. echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
  451. elif [ -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
  452. # (A couple of early 2.6 laptop mode patches had these.)
  453. # The same goes for these.
  454. echo $XFS_AGE > /proc/sys/fs/xfs/lm_age_buffer
  455. echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
  456. elif [ -f /proc/sys/fs/xfs/age_buffer ] ; then
  457. # (2.6.6)
  458. # But not for these -- they are also used in normal
  459. # operation.
  460. echo $XFS_AGE > /proc/sys/fs/xfs/age_buffer
  461. echo $XFS_AGE > /proc/sys/fs/xfs/sync_interval
  462. elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
  463. # (2.6.7 upwards)
  464. # And not for these either. These are in centisecs,
  465. # not USER_HZ, so we have to use $AGE, not $XFS_AGE.
  466. echo $AGE > /proc/sys/fs/xfs/age_buffer_centisecs
  467. echo $AGE > /proc/sys/fs/xfs/xfssyncd_centisecs
  468. echo 3000 > /proc/sys/fs/xfs/xfsbufd_centisecs
  469. fi
  470. case "$KLEVEL" in
  471. "2.4")
  472. echo 1 > /proc/sys/vm/laptop_mode
  473. echo "30 500 0 0 $AGE $AGE 60 20 0" > /proc/sys/vm/bdflush
  474. ;;
  475. "2.6")
  476. echo 5 > /proc/sys/vm/laptop_mode
  477. echo "$AGE" > /proc/sys/vm/dirty_writeback_centisecs
  478. echo "$AGE" > /proc/sys/vm/dirty_expire_centisecs
  479. echo "$DIRTY_RATIO" > /proc/sys/vm/dirty_ratio
  480. echo "$DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio
  481. ;;
  482. esac
  483. if [ $DO_REMOUNTS -eq 1 ]; then
  484. cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
  485. PARSEDOPTS="$(parse_mount_opts "$OPTS")"
  486. if [ "$FST" = 'unknown' ]; then
  487. FST=$(deduce_fstype $MP)
  488. fi
  489. case "$FST" in
  490. "ext3"|"reiserfs")
  491. PARSEDOPTS="$(parse_mount_opts commit "$OPTS")"
  492. mount $DEV -t $FST $MP -o remount,$PARSEDOPTS,commit=$MAX_AGE$NOATIME_OPT
  493. ;;
  494. "xfs")
  495. mount $DEV -t $FST $MP -o remount,$OPTS$NOATIME_OPT
  496. ;;
  497. esac
  498. if [ -b $DEV ] ; then
  499. blockdev --setra $(($READAHEAD * 2)) $DEV
  500. fi
  501. done
  502. fi
  503. if [ $DO_HD -eq 1 ] ; then
  504. for THISHD in $HD ; do
  505. /sbin/hdparm -S $BATT_HD $THISHD > /dev/null 2>&1
  506. /sbin/hdparm -B 1 $THISHD > /dev/null 2>&1
  507. done
  508. fi
  509. if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
  510. if [ $CPU_MAXFREQ = 'slowest' ]; then
  511. CPU_MAXFREQ=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
  512. fi
  513. echo $CPU_MAXFREQ > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
  514. fi
  515. echo "."
  516. ;;
  517. stop)
  518. U_AGE=$((100*$DEF_UPDATE))
  519. B_AGE=$((100*$DEF_AGE))
  520. echo -n "Stopping laptop_mode"
  521. echo 0 > /proc/sys/vm/laptop_mode
  522. if [ -f /proc/sys/fs/xfs/age_buffer -a ! -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
  523. # These need to be restored, if there are no lm_*.
  524. echo $(($XFS_HZ*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer
  525. echo $(($XFS_HZ*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/sync_interval
  526. elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
  527. # These need to be restored as well.
  528. echo $((100*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer_centisecs
  529. echo $((100*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/xfssyncd_centisecs
  530. echo $((100*$DEF_XFS_BUFD_INTERVAL)) > /proc/sys/fs/xfs/xfsbufd_centisecs
  531. fi
  532. case "$KLEVEL" in
  533. "2.4")
  534. echo "30 500 0 0 $U_AGE $B_AGE 60 20 0" > /proc/sys/vm/bdflush
  535. ;;
  536. "2.6")
  537. echo "$U_AGE" > /proc/sys/vm/dirty_writeback_centisecs
  538. echo "$B_AGE" > /proc/sys/vm/dirty_expire_centisecs
  539. echo "$DEF_DIRTY_RATIO" > /proc/sys/vm/dirty_ratio
  540. echo "$DEF_DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio
  541. ;;
  542. esac
  543. if [ $DO_REMOUNTS -eq 1 ] ; then
  544. cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
  545. # Reset commit and atime options to defaults.
  546. if [ "$FST" = 'unknown' ]; then
  547. FST=$(deduce_fstype $MP)
  548. fi
  549. case "$FST" in
  550. "ext3"|"reiserfs")
  551. PARSEDOPTS="$(parse_mount_opts_wfstab $DEV commit $OPTS)"
  552. PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $PARSEDOPTS)"
  553. mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
  554. ;;
  555. "xfs")
  556. PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $OPTS)"
  557. mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
  558. ;;
  559. esac
  560. if [ -b $DEV ] ; then
  561. blockdev --setra 256 $DEV
  562. fi
  563. done
  564. fi
  565. if [ $DO_HD -eq 1 ] ; then
  566. for THISHD in $HD ; do
  567. /sbin/hdparm -S $AC_HD $THISHD > /dev/null 2>&1
  568. /sbin/hdparm -B 255 $THISHD > /dev/null 2>&1
  569. done
  570. fi
  571. if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
  572. echo `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq` > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
  573. fi
  574. echo "."
  575. ;;
  576. *)
  577. echo "Usage: $0 {start|stop}" 2>&1
  578. exit 1
  579. ;;
  580. esac
  581. exit 0
  582. --------------------CONTROL SCRIPT END------------------------------------------
  583. ACPI integration
  584. ----------------
  585. Dax Kelson submitted this so that the ACPI acpid daemon will
  586. kick off the laptop_mode script and run hdparm. The part that
  587. automatically disables laptop mode when the battery is low was
  588. written by Jan Topinski.
  589. -----------------/etc/acpi/events/ac_adapter BEGIN------------------------------
  590. event=ac_adapter
  591. action=/etc/acpi/actions/ac.sh %e
  592. ----------------/etc/acpi/events/ac_adapter END---------------------------------
  593. -----------------/etc/acpi/events/battery BEGIN---------------------------------
  594. event=battery.*
  595. action=/etc/acpi/actions/battery.sh %e
  596. ----------------/etc/acpi/events/battery END------------------------------------
  597. ----------------/etc/acpi/actions/ac.sh BEGIN-----------------------------------
  598. #!/bin/bash
  599. # ac on/offline event handler
  600. status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/$2/state`
  601. case $status in
  602. "on-line")
  603. /sbin/laptop_mode stop
  604. exit 0
  605. ;;
  606. "off-line")
  607. /sbin/laptop_mode start
  608. exit 0
  609. ;;
  610. esac
  611. ---------------------------/etc/acpi/actions/ac.sh END--------------------------
  612. ---------------------------/etc/acpi/actions/battery.sh BEGIN-------------------
  613. #! /bin/bash
  614. # Automatically disable laptop mode when the battery almost runs out.
  615. BATT_INFO=/proc/acpi/battery/$2/state
  616. if [[ -f /proc/sys/vm/laptop_mode ]]
  617. then
  618. LM=`cat /proc/sys/vm/laptop_mode`
  619. if [[ $LM -gt 0 ]]
  620. then
  621. if [[ -f $BATT_INFO ]]
  622. then
  623. # Source the config file only now that we know we need
  624. if [ -f /etc/default/laptop-mode ] ; then
  625. # Debian
  626. . /etc/default/laptop-mode
  627. elif [ -f /etc/sysconfig/laptop-mode ] ; then
  628. # Others
  629. . /etc/sysconfig/laptop-mode
  630. fi
  631. MINIMUM_BATTERY_MINUTES=${MINIMUM_BATTERY_MINUTES:-'10'}
  632. ACTION="`cat $BATT_INFO | grep charging | cut -c 26-`"
  633. if [[ ACTION -eq "discharging" ]]
  634. then
  635. PRESENT_RATE=`cat $BATT_INFO | grep "present rate:" | sed "s/.* \([0-9][0-9]* \).*/\1/" `
  636. REMAINING=`cat $BATT_INFO | grep "remaining capacity:" | sed "s/.* \([0-9][0-9]* \).*/\1/" `
  637. fi
  638. if (($REMAINING * 60 / $PRESENT_RATE < $MINIMUM_BATTERY_MINUTES))
  639. then
  640. /sbin/laptop_mode stop
  641. fi
  642. else
  643. 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."
  644. fi
  645. fi
  646. fi
  647. ---------------------------/etc/acpi/actions/battery.sh END--------------------
  648. Monitoring tool
  649. ---------------
  650. Bartek Kania submitted this, it can be used to measure how much time your disk
  651. spends spun up/down.
  652. ---------------------------dslm.c BEGIN-----------------------------------------
  653. /*
  654. * Simple Disk Sleep Monitor
  655. * by Bartek Kania
  656. * Licenced under the GPL
  657. */
  658. #include <unistd.h>
  659. #include <stdlib.h>
  660. #include <stdio.h>
  661. #include <fcntl.h>
  662. #include <errno.h>
  663. #include <time.h>
  664. #include <string.h>
  665. #include <signal.h>
  666. #include <sys/ioctl.h>
  667. #include <linux/hdreg.h>
  668. #ifdef DEBUG
  669. #define D(x) x
  670. #else
  671. #define D(x)
  672. #endif
  673. int endit = 0;
  674. /* Check if the disk is in powersave-mode
  675. * Most of the code is stolen from hdparm.
  676. * 1 = active, 0 = standby/sleep, -1 = unknown */
  677. int check_powermode(int fd)
  678. {
  679. unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
  680. int state;
  681. if (ioctl(fd, HDIO_DRIVE_CMD, &args)
  682. && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
  683. && ioctl(fd, HDIO_DRIVE_CMD, &args)) {
  684. if (errno != EIO || args[0] != 0 || args[1] != 0) {
  685. state = -1; /* "unknown"; */
  686. } else
  687. state = 0; /* "sleeping"; */
  688. } else {
  689. state = (args[2] == 255) ? 1 : 0;
  690. }
  691. D(printf(" drive state is: %d\n", state));
  692. return state;
  693. }
  694. char *state_name(int i)
  695. {
  696. if (i == -1) return "unknown";
  697. if (i == 0) return "sleeping";
  698. if (i == 1) return "active";
  699. return "internal error";
  700. }
  701. char *myctime(time_t time)
  702. {
  703. char *ts = ctime(&time);
  704. ts[strlen(ts) - 1] = 0;
  705. return ts;
  706. }
  707. void measure(int fd)
  708. {
  709. time_t start_time;
  710. int last_state;
  711. time_t last_time;
  712. int curr_state;
  713. time_t curr_time = 0;
  714. time_t time_diff;
  715. time_t active_time = 0;
  716. time_t sleep_time = 0;
  717. time_t unknown_time = 0;
  718. time_t total_time = 0;
  719. int changes = 0;
  720. float tmp;
  721. printf("Starting measurements\n");
  722. last_state = check_powermode(fd);
  723. start_time = last_time = time(0);
  724. printf(" System is in state %s\n\n", state_name(last_state));
  725. while(!endit) {
  726. sleep(1);
  727. curr_state = check_powermode(fd);
  728. if (curr_state != last_state || endit) {
  729. changes++;
  730. curr_time = time(0);
  731. time_diff = curr_time - last_time;
  732. if (last_state == 1) active_time += time_diff;
  733. else if (last_state == 0) sleep_time += time_diff;
  734. else unknown_time += time_diff;
  735. last_state = curr_state;
  736. last_time = curr_time;
  737. printf("%s: State-change to %s\n", myctime(curr_time),
  738. state_name(curr_state));
  739. }
  740. }
  741. changes--; /* Compensate for SIGINT */
  742. total_time = time(0) - start_time;
  743. printf("\nTotal running time: %lus\n", curr_time - start_time);
  744. printf(" State changed %d times\n", changes);
  745. tmp = (float)sleep_time / (float)total_time * 100;
  746. printf(" Time in sleep state: %lus (%.2f%%)\n", sleep_time, tmp);
  747. tmp = (float)active_time / (float)total_time * 100;
  748. printf(" Time in active state: %lus (%.2f%%)\n", active_time, tmp);
  749. tmp = (float)unknown_time / (float)total_time * 100;
  750. printf(" Time in unknown state: %lus (%.2f%%)\n", unknown_time, tmp);
  751. }
  752. void ender(int s)
  753. {
  754. endit = 1;
  755. }
  756. void usage()
  757. {
  758. puts("usage: dslm [-w <time>] <disk>");
  759. exit(0);
  760. }
  761. int main(int argc, char **argv)
  762. {
  763. int fd;
  764. char *disk = 0;
  765. int settle_time = 60;
  766. /* Parse the simple command-line */
  767. if (argc == 2)
  768. disk = argv[1];
  769. else if (argc == 4) {
  770. settle_time = atoi(argv[2]);
  771. disk = argv[3];
  772. } else
  773. usage();
  774. if (!(fd = open(disk, O_RDONLY|O_NONBLOCK))) {
  775. printf("Can't open %s, because: %s\n", disk, strerror(errno));
  776. exit(-1);
  777. }
  778. if (settle_time) {
  779. printf("Waiting %d seconds for the system to settle down to "
  780. "'normal'\n", settle_time);
  781. sleep(settle_time);
  782. } else
  783. puts("Not waiting for system to settle down");
  784. signal(SIGINT, ender);
  785. measure(fd);
  786. close(fd);
  787. return 0;
  788. }
  789. ---------------------------dslm.c END-------------------------------------------