ia64/linux-2.6.18-xen.hg

annotate Documentation/laptop-mode.txt @ 897:329ea0ccb344

balloon: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

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