pax_global_header00006660000000000000000000000064125676422120014521gustar00rootroot0000000000000052 comment=09a65fb1ab6a66ea0d03c753dc533c2a789ccc8b laptop-mode-tools-1.68/000077500000000000000000000000001256764221200150365ustar00rootroot00000000000000laptop-mode-tools-1.68/COPYING000066400000000000000000000431311256764221200160730ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. laptop-mode-tools-1.68/Documentation/000077500000000000000000000000001256764221200176475ustar00rootroot00000000000000laptop-mode-tools-1.68/Documentation/laptop-mode.txt000066400000000000000000000740501256764221200226370ustar00rootroot00000000000000 NOTE: THIS IS A COPY OF THE DOCUMENT FROM THE LINUX KERNEL. DO *NOT* USE THE SCRIPTS FROM THIS DOCUMENT. THEY HAVE NOT BEEN KEPT UP-TO-DATE. -- Bart How to conserve battery power using laptop-mode ----------------------------------------------- Document Author: Bart Samwel (bart@samwel.tk) Date created: January 2, 2004 Last modified: July 10, 2004 Introduction ------------ Laptop mode is used to minimize the time that the hard disk needs to be spun up, to conserve battery power on laptops. It has been reported to cause significant power savings. Contents -------- * Introduction * Installation * Caveats * The Details * Tips & Tricks * Control script * ACPI integration * Monitoring tool Installation ------------ To use laptop mode, you don't need to set any kernel configuration options or anything. Simply install all the files included in this document, and laptop mode will automatically be started when you're on battery. For your convenience, a tarball containing an installer can be downloaded at: http://www.xs4all.nl/~bsamwel/laptop_mode/tools To configure laptop mode, you need to edit the configuration file, which is located in /etc/default/laptop-mode on Debian-based systems, or in /etc/sysconfig/laptop-mode on other systems. Unfortunately, automatic enabling of laptop mode does not work for laptops that don't have ACPI. On those laptops, you need to start laptop mode manually. To start laptop mode, run "laptop_mode start", and to stop it, run "laptop_mode stop". Caveats ------- * The downside of laptop mode is that you have a chance of losing up to 10 minutes of work. If you cannot afford this, don't use it! The supplied ACPI scripts automatically turn off laptop mode when the battery almost runs out, so that you won't lose any data at the end of your battery life. * Most desktop hard drives have a very limited lifetime measured in spindown cycles, typically about 50.000 times (it's usually listed on the spec sheet). Check your drive's rating, and don't wear down your drive's lifetime if you don't need to. * If you mount some of your ext3/reiserfs filesystems with the -n option, then the control script will not be able to remount them correctly. You must set DO_REMOUNTS=0 in the control script, otherwise it will remount them with the wrong options -- or it will fail because it cannot write to /etc/mtab. * If you have your filesystems listed as type "auto" in fstab, like I did, then the control script will not recognize them as filesystems that need remounting. You must list the filesystems with their true type instead. * It has been reported that some versions of the mutt mail client use file access times to determine whether a folder contains new mail. If you use mutt and experience this, you must disable the noatime remounting by setting the option DO_REMOUNT_NOATIME to 0 in the configuration file. The Details ----------- Laptop mode is controlled by the knob /proc/sys/vm/laptop_mode. This knob is present for all kernels that have the laptop mode patch, regardless of any configuration options. When the knob is set, any physical disk I/O (that might have caused the hard disk to spin up) causes Linux to flush all dirty blocks. The result of this is that after a disk has spun down, it will not be spun up anymore to write dirty blocks, because those blocks had already been written immediately after the most recent read operation. The value of the laptop_mode knob determines the time between the occurrence of disk I/O and when the flush is triggered. A sensible value for the knob is 5 seconds. Setting the knob to 0 disables laptop mode. To increase the effectiveness of the laptop_mode strategy, the laptop_mode control script increases dirty_expire_centisecs and dirty_writeback_centisecs in /proc/sys/vm to about 10 minutes (by default), which means that pages that are dirtied are not forced to be written to disk as often. The control script also changes the dirty background ratio, so that background writeback of dirty pages is not done anymore. Combined with a higher commit value (also 10 minutes) for ext3 or ReiserFS filesystems (also done automatically by the control script), this results in concentration of disk activity in a small time interval which occurs only once every 10 minutes, or whenever the disk is forced to spin up by a cache miss. The disk can then be spun down in the periods of inactivity. If you want to find out which process caused the disk to spin up, you can gather information by setting the flag /proc/sys/vm/block_dump. When this flag is set, Linux reports all disk read and write operations that take place, and all block dirtyings done to files. This makes it possible to debug why a disk needs to spin up, and to increase battery life even more. The output of block_dump is written to the kernel output, and it can be retrieved using "dmesg". When you use block_dump and your kernel logging level also includes kernel debugging messages, you probably want to turn off klogd, otherwise the output of block_dump will be logged, causing disk activity that is not normally there. Configuration ------------- The laptop mode configuration file is located in /etc/default/laptop-mode on Debian-based systems, or in /etc/sysconfig/laptop-mode on other systems. It contains the following options: MAX_AGE: Maximum time, in seconds, of hard drive spindown time that you are confortable with. Worst case, it's possible that you could lose this amount of work if your battery fails while you're in laptop mode. MINIMUM_BATTERY_MINUTES: Automatically disable laptop mode if the remaining number of minutes of battery power is less than this value. Default is 10 minutes. AC_HD/BATT_HD: The idle timeout that should be set on your hard drive when laptop mode is active (BATT_HD) and when it is not active (AC_HD). The defaults are 20 seconds (value 4) for BATT_HD and 2 hours (value 244) for AC_HD. The possible values are those listed in the manual page for "hdparm" for the "-S" option. HD: The device for which the spindown timeout should be adjusted by laptop mode. Default is /dev/hda. READAHEAD: Disk readahead, in 512-byte sectors, while laptop mode is active. A large readahead can prevent disk accesses for things like executable pages (which are loaded on demand while the application executes) and sequentially accessed data (MP3s). DO_REMOUNTS: The control script automatically remounts any mounted journaled filesystems with approriate commit interval options. When this option is set to 0, this feature is disabled. DO_REMOUNT_NOATIME: When remounting, should the filesystems be remounted with the noatime option? Normally, this is set to "1" (enabled), but there may be programs that require access time recording. DIRTY_RATIO: The percentage of memory that is allowed to contain "dirty" or unsaved data before a writeback is forced, while laptop mode is active. Corresponds to the /proc/sys/vm/dirty_ratio sysctl. DIRTY_BACKGROUND_RATIO: The percentage of memory that is allowed to contain "dirty" or unsaved data after a forced writeback is done due to an exceeding of DIRTY_RATIO. Set this nice and low. This corresponds to the /proc/sys/vm/dirty_background_ratio sysctl. Note that the behaviour of dirty_background_ratio is quite different when laptop mode is active and when it isn't. When laptop mode is inactive, dirty_background_ratio is the threshold percentage at which background writeouts start taking place. When laptop mode is active, however, background writeouts are disabled, and the dirty_background_ratio only determines how much writeback is done when dirty_ratio is reached. DO_CPU: Enable CPU frequency scaling when in laptop mode. (Requires CPUFreq to be setup. See Documentation/cpu-freq/user-guide.txt for more info. Disabled by default.) CPU_MAXFREQ: When on battery, what is the maximum CPU speed that the system should use? Legal values are "slowest" for the slowest speed that your CPU is able to operate at, or a value listed in /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies. Tips & Tricks ------------- * Bartek Kania reports getting up to 50 minutes of extra battery life (on top of his regular 3 to 3.5 hours) using a spindown time of 5 seconds (BATT_HD=1). * You can spin down the disk while playing MP3, by setting disk readahead to 8MB (READAHEAD=16384). Effectively, the disk will read a complete MP3 at once, and will then spin down while the MP3 is playing. (Thanks to Bartek Kania.) * Drew Scott Daniels observed: "I don't know why, but when I decrease the number of colours that my display uses it consumes less battery power. I've seen this on powerbooks too. I hope that this is a piece of information that might be useful to the Laptop Mode patch or it's users." * In syslog.conf, you can prefix entries with a dash ``-'' to omit syncing the file after every logging. When you're using laptop-mode and your disk doesn't spin down, this is a likely culprit. * Richard Atterer observed that laptop mode does not work well with noflushd (http://noflushd.sourceforge.net/), it seems that noflushd prevents laptop-mode from doing its thing. Configuration file for control and ACPI battery scripts ------------------------------------------------------- This allows the tunables to be changed for the scripts via an external configuration file It should be installed as /etc/default/laptop-mode on Debian, and as /etc/sysconfig/laptop-mode on Red Hat, SUSE, Mandrake, and other work-alikes. --------------------CONFIG FILE BEGIN------------------------------------------- # Maximum time, in seconds, of hard drive spindown time that you are # confortable with. Worst case, it's possible that you could lose this # amount of work if your battery fails you while in laptop mode. MAX_AGE=600 # Automatically disable laptop mode when the number of minutes of battery # that you have left goes below this threshold. MINIMUM_BATTERY_MINUTES=10 # Read-ahead, in 512-byte sectors. You can spin down the disk while playing MP3/OGG # by setting the disk readahead to 8MB (READAHEAD=16384). Effectively, the disk # will read a complete MP3 at once, and will then spin down while the MP3/OGG is # playing. READAHEAD=4096 # Shall we remount journaled fs. with appropiate commit interval? (1=yes) DO_REMOUNTS=1 # And shall we add the "noatime" option to that as well? (1=yes) DO_REMOUNT_NOATIME=1 # Dirty synchronous ratio. At this percentage of dirty pages the process # which # calls write() does its own writeback DIRTY_RATIO=40 # # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been # exceeded, the kernel will wake pdflush which will then reduce the amount # of dirty memory to dirty_background_ratio. Set this nice and low, so once # some writeout has commenced, we do a lot of it. # DIRTY_BACKGROUND_RATIO=5 # kernel default dirty buffer age DEF_AGE=30 DEF_UPDATE=5 DEF_DIRTY_BACKGROUND_RATIO=10 DEF_DIRTY_RATIO=40 DEF_XFS_AGE_BUFFER=15 DEF_XFS_SYNC_INTERVAL=30 DEF_XFS_BUFD_INTERVAL=1 # This must be adjusted manually to the value of HZ in the running kernel # on 2.4, until the XFS people change their 2.4 external interfaces to work in # centisecs. This can be automated, but it's a work in progress that still # needs# some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for # external interfaces, and that is currently always set to 100. So you don't # need to change this on 2.6. XFS_HZ=100 # Should the maximum CPU frequency be adjusted down while on battery? # Requires CPUFreq to be setup. # See Documentation/cpu-freq/user-guide.txt for more info DO_CPU=0 # When on battery what is the maximum CPU speed that the system should # use? Legal values are "slowest" for the slowest speed that your # CPU is able to operate at, or a value listed in: # /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies # Only applicable if DO_CPU=1. CPU_MAXFREQ=slowest # Idle timeout for your hard drive (man hdparm for valid values, -S option) # Default is 2 hours on AC (AC_HD=244) and 20 seconds for battery (BATT_HD=4). AC_HD=244 BATT_HD=4 # The drive for which to adjust the idle timeout. HD=/dev/hda # Set the spindown timeout on a hard drive? DO_HD=1 --------------------CONFIG FILE END--------------------------------------------- Control script -------------- Please note that this control script works for the Linux 2.4 and 2.6 series (thanks to Kiko Piris). --------------------CONTROL SCRIPT BEGIN---------------------------------------- #!/bin/bash # start or stop laptop_mode, best run by a power management daemon when # ac gets connected/disconnected from a laptop # # install as /sbin/laptop_mode # # Contributors to this script: Kiko Piris # Bart Samwel # Micha Feigin # Andrew Morton # Herve Eychenne # Dax Kelson # # Original Linux 2.4 version by: Jens Axboe ############################################################################# # Source config if [ -f /etc/default/laptop-mode ] ; then # Debian . /etc/default/laptop-mode elif [ -f /etc/sysconfig/laptop-mode ] ; then # Others . /etc/sysconfig/laptop-mode fi # Don't raise an error if the config file is incomplete # set defaults instead: # Maximum time, in seconds, of hard drive spindown time that you are # confortable with. Worst case, it's possible that you could lose this # amount of work if your battery fails you while in laptop mode. MAX_AGE=${MAX_AGE:-'600'} # Read-ahead, in kilobytes READAHEAD=${READAHEAD:-'4096'} # Shall we remount journaled fs. with appropiate commit interval? (1=yes) DO_REMOUNTS=${DO_REMOUNTS:-'1'} # And shall we add the "noatime" option to that as well? (1=yes) DO_REMOUNT_NOATIME=${DO_REMOUNT_NOATIME:-'1'} # Shall we adjust the idle timeout on a hard drive? DO_HD=${DO_HD:-'1'} # Adjust idle timeout on which hard drive? HD=${HD:-'/dev/hda'} # spindown time for HD (hdparm -S values) AC_HD=${AC_HD:-'244'} BATT_HD=${BATT_HD:-'4'} # Dirty synchronous ratio. At this percentage of dirty pages the process which # calls write() does its own writeback DIRTY_RATIO=${DIRTY_RATIO:-'40'} # cpu frequency scaling # See Documentation/cpu-freq/user-guide.txt for more info DO_CPU=${CPU_MANAGE:-'0'} CPU_MAXFREQ=${CPU_MAXFREQ:-'slowest'} # # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been # exceeded, the kernel will wake pdflush which will then reduce the amount # of dirty memory to dirty_background_ratio. Set this nice and low, so once # some writeout has commenced, we do a lot of it. # DIRTY_BACKGROUND_RATIO=${DIRTY_BACKGROUND_RATIO:-'5'} # kernel default dirty buffer age DEF_AGE=${DEF_AGE:-'30'} DEF_UPDATE=${DEF_UPDATE:-'5'} DEF_DIRTY_BACKGROUND_RATIO=${DEF_DIRTY_BACKGROUND_RATIO:-'10'} DEF_DIRTY_RATIO=${DEF_DIRTY_RATIO:-'40'} DEF_XFS_AGE_BUFFER=${DEF_XFS_AGE_BUFFER:-'15'} DEF_XFS_SYNC_INTERVAL=${DEF_XFS_SYNC_INTERVAL:-'30'} DEF_XFS_BUFD_INTERVAL=${DEF_XFS_BUFD_INTERVAL:-'1'} # This must be adjusted manually to the value of HZ in the running kernel # on 2.4, until the XFS people change their 2.4 external interfaces to work in # centisecs. This can be automated, but it's a work in progress that still needs # some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for external # interfaces, and that is currently always set to 100. So you don't need to # change this on 2.6. XFS_HZ=${XFS_HZ:-'100'} ############################################################################# KLEVEL="$(uname -r | { IFS='.' read a b c echo $a.$b } )" case "$KLEVEL" in "2.4"|"2.6") ;; *) echo "Unhandled kernel version: $KLEVEL ('uname -r' = '$(uname -r)')" >&2 exit 1 ;; esac if [ ! -e /proc/sys/vm/laptop_mode ] ; then echo "Kernel is not patched with laptop_mode patch." >&2 exit 1 fi if [ ! -w /proc/sys/vm/laptop_mode ] ; then echo "You do not have enough privileges to enable laptop_mode." >&2 exit 1 fi # Remove an option (the first parameter) of the form option= from # a mount options string (the rest of the parameters). parse_mount_opts () { OPT="$1" shift echo ",$*," | sed \ -e 's/,'"$OPT"'=[0-9]*,/,/g' \ -e 's/,,*/,/g' \ -e 's/^,//' \ -e 's/,$//' } # Remove an option (the first parameter) without any arguments from # a mount option string (the rest of the parameters). parse_nonumber_mount_opts () { OPT="$1" shift echo ",$*," | sed \ -e 's/,'"$OPT"',/,/g' \ -e 's/,,*/,/g' \ -e 's/^,//' \ -e 's/,$//' } # Find out the state of a yes/no option (e.g. "atime"/"noatime") in # fstab for a given filesystem, and use this state to replace the # value of the option in another mount options string. The device # is the first argument, the option name the second, and the default # value the third. The remainder is the mount options string. # # Example: # parse_yesno_opts_wfstab /dev/hda1 atime atime defaults,noatime # # If fstab contains, say, "rw" for this filesystem, then the result # will be "defaults,atime". parse_yesno_opts_wfstab () { L_DEV="$1" OPT="$2" DEF_OPT="$3" shift 3 L_OPTS="$*" PARSEDOPTS1="$(parse_nonumber_mount_opts $OPT $L_OPTS)" PARSEDOPTS1="$(parse_nonumber_mount_opts no$OPT $PARSEDOPTS1)" # Watch for a default atime in fstab FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)" if echo "$FSTAB_OPTS" | grep "$OPT" > /dev/null ; then # option specified in fstab: extract the value and use it if echo "$FSTAB_OPTS" | grep "no$OPT" > /dev/null ; then echo "$PARSEDOPTS1,no$OPT" else # no$OPT not found -- so we must have $OPT. echo "$PARSEDOPTS1,$OPT" fi else # option not specified in fstab -- choose the default. echo "$PARSEDOPTS1,$DEF_OPT" fi } # Find out the state of a numbered option (e.g. "commit=NNN") in # fstab for a given filesystem, and use this state to replace the # value of the option in another mount options string. The device # is the first argument, and the option name the second. The # remainder is the mount options string in which the replacement # must be done. # # Example: # parse_mount_opts_wfstab /dev/hda1 commit defaults,commit=7 # # If fstab contains, say, "commit=3,rw" for this filesystem, then the # result will be "rw,commit=3". parse_mount_opts_wfstab () { L_DEV="$1" OPT="$2" shift 2 L_OPTS="$*" PARSEDOPTS1="$(parse_mount_opts $OPT $L_OPTS)" # Watch for a default commit in fstab FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)" if echo "$FSTAB_OPTS" | grep "$OPT=" > /dev/null ; then # option specified in fstab: extract the value, and use it echo -n "$PARSEDOPTS1,$OPT=" echo ",$FSTAB_OPTS," | sed \ -e 's/.*,'"$OPT"'=//' \ -e 's/,.*//' else # option not specified in fstab: set it to 0 echo "$PARSEDOPTS1,$OPT=0" fi } deduce_fstype () { MP="$1" # My root filesystem unfortunately has # type "unknown" in /etc/mtab. If we encounter # "unknown", we try to get the type from fstab. cat /etc/fstab | grep -v '^#' | while read FSTAB_DEV FSTAB_MP FSTAB_FST FSTAB_OPTS FSTAB_DUMP FSTAB_DUMP ; do if [ "$FSTAB_MP" = "$MP" ]; then echo $FSTAB_FST exit 0 fi done } if [ $DO_REMOUNT_NOATIME -eq 1 ] ; then NOATIME_OPT=",noatime" fi case "$1" in start) AGE=$((100*$MAX_AGE)) XFS_AGE=$(($XFS_HZ*$MAX_AGE)) echo -n "Starting laptop_mode" if [ -d /proc/sys/vm/pagebuf ] ; then # (For 2.4 and early 2.6.) # This only needs to be set, not reset -- it is only used when # laptop mode is enabled. echo $XFS_AGE > /proc/sys/vm/pagebuf/lm_flush_age echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval elif [ -f /proc/sys/fs/xfs/lm_age_buffer ] ; then # (A couple of early 2.6 laptop mode patches had these.) # The same goes for these. echo $XFS_AGE > /proc/sys/fs/xfs/lm_age_buffer echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval elif [ -f /proc/sys/fs/xfs/age_buffer ] ; then # (2.6.6) # But not for these -- they are also used in normal # operation. echo $XFS_AGE > /proc/sys/fs/xfs/age_buffer echo $XFS_AGE > /proc/sys/fs/xfs/sync_interval elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then # (2.6.7 upwards) # And not for these either. These are in centisecs, # not USER_HZ, so we have to use $AGE, not $XFS_AGE. echo $AGE > /proc/sys/fs/xfs/age_buffer_centisecs echo $AGE > /proc/sys/fs/xfs/xfssyncd_centisecs echo 3000 > /proc/sys/fs/xfs/xfsbufd_centisecs fi case "$KLEVEL" in "2.4") echo 1 > /proc/sys/vm/laptop_mode echo "30 500 0 0 $AGE $AGE 60 20 0" > /proc/sys/vm/bdflush ;; "2.6") echo 5 > /proc/sys/vm/laptop_mode echo "$AGE" > /proc/sys/vm/dirty_writeback_centisecs echo "$AGE" > /proc/sys/vm/dirty_expire_centisecs echo "$DIRTY_RATIO" > /proc/sys/vm/dirty_ratio echo "$DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio ;; esac if [ $DO_REMOUNTS -eq 1 ]; then cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do PARSEDOPTS="$(parse_mount_opts "$OPTS")" if [ "$FST" = 'unknown' ]; then FST=$(deduce_fstype $MP) fi case "$FST" in "ext3"|"reiserfs") PARSEDOPTS="$(parse_mount_opts commit "$OPTS")" mount $DEV -t $FST $MP -o remount,$PARSEDOPTS,commit=$MAX_AGE$NOATIME_OPT ;; "xfs") mount $DEV -t $FST $MP -o remount,$OPTS$NOATIME_OPT ;; esac if [ -b $DEV ] ; then blockdev --setra $(($READAHEAD * 2)) $DEV fi done fi if [ $DO_HD -eq 1 ] ; then /sbin/hdparm -S $BATT_HD $HD > /dev/null 2>&1 /sbin/hdparm -B 1 $HD > /dev/null 2>&1 fi if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then if [ $CPU_MAXFREQ = 'slowest' ]; then CPU_MAXFREQ=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq` fi echo $CPU_MAXFREQ > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq fi echo "." ;; stop) U_AGE=$((100*$DEF_UPDATE)) B_AGE=$((100*$DEF_AGE)) echo -n "Stopping laptop_mode" echo 0 > /proc/sys/vm/laptop_mode if [ -f /proc/sys/fs/xfs/age_buffer -a ! -f /proc/sys/fs/xfs/lm_age_buffer ] ; then # These need to be restored, if there are no lm_*. echo $(($XFS_HZ*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer echo $(($XFS_HZ*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/sync_interval elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then # These need to be restored as well. echo $((100*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer_centisecs echo $((100*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/xfssyncd_centisecs echo $((100*$DEF_XFS_BUFD_INTERVAL)) > /proc/sys/fs/xfs/xfsbufd_centisecs fi case "$KLEVEL" in "2.4") echo "30 500 0 0 $U_AGE $B_AGE 60 20 0" > /proc/sys/vm/bdflush ;; "2.6") echo "$U_AGE" > /proc/sys/vm/dirty_writeback_centisecs echo "$B_AGE" > /proc/sys/vm/dirty_expire_centisecs echo "$DEF_DIRTY_RATIO" > /proc/sys/vm/dirty_ratio echo "$DEF_DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio ;; esac if [ $DO_REMOUNTS -eq 1 ] ; then cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do # Reset commit and atime options to defaults. if [ "$FST" = 'unknown' ]; then FST=$(deduce_fstype $MP) fi case "$FST" in "ext3"|"reiserfs") PARSEDOPTS="$(parse_mount_opts_wfstab $DEV commit $OPTS)" PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $PARSEDOPTS)" mount $DEV -t $FST $MP -o remount,$PARSEDOPTS ;; "xfs") PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $OPTS)" mount $DEV -t $FST $MP -o remount,$PARSEDOPTS ;; esac if [ -b $DEV ] ; then blockdev --setra 256 $DEV fi done fi if [ $DO_HD -eq 1 ] ; then /sbin/hdparm -S $AC_HD $HD > /dev/null 2>&1 /sbin/hdparm -B 255 $HD > /dev/null 2>&1 fi if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then echo `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq` > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq fi echo "." ;; *) echo "Usage: $0 {start|stop}" 2>&1 exit 1 ;; esac exit 0 --------------------CONTROL SCRIPT END------------------------------------------ ACPI integration ---------------- Dax Kelson submitted this so that the ACPI acpid daemon will kick off the laptop_mode script and run hdparm. The part that automatically disables laptop mode when the battery is low was writen by Jan Topinski. -----------------/etc/acpi/events/ac_adapter BEGIN------------------------------ event=ac_adapter action=/etc/acpi/actions/ac.sh event=battery.* action=/etc/acpi/actions/battery.sh %e ----------------/etc/acpi/events/ac_adapter END--------------------------------- ----------------/etc/acpi/actions/ac.sh BEGIN----------------------------------- #!/bin/bash # ac on/offline event handler status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/AC/state` case $status in "on-line") echo "AC mode: disabling laptop mode." /sbin/laptop_mode stop exit 0 ;; "off-line") echo "Battery mode: enabling laptop mode." /sbin/laptop_mode start exit 0 ;; esac ---------------------------/etc/acpi/actions/ac.sh END-------------------------- ---------------------------/etc/acpi/actions/battery.sh BEGIN------------------- #! /bin/bash # Automatically disable laptop mode when the battery almost runs out. BATT_INFO=/proc/acpi/battery/$2/state if [[ -f /proc/sys/vm/laptop_mode ]] then LM=`cat /proc/sys/vm/laptop_mode` if [[ $LM -gt 0 ]] then if [[ -f $BATT_INFO ]] then # Source the config file only now that we know we need if [ -f /etc/default/laptop-mode ] ; then # Debian . /etc/default/laptop-mode elif [ -f /etc/sysconfig/laptop-mode ] ; then # Others . /etc/sysconfig/laptop-mode fi MINIMUM_BATTERY_MINUTES=${MINIMUM_BATTERY_MINUTES:-'10'} ACTION="`cat $BATT_INFO | grep charging | cut -c 26-`" if [[ ACTION -eq "discharging" ]] then PRESENT_RATE=`cat $BATT_INFO | grep "present rate:" | sed "s/.* \([0-9][0-9]* \).*/\1/" ` REMAINING=`cat $BATT_INFO | grep "remaining capacity:" | sed "s/.* \([0-9][0-9]* \).*/\1/" ` fi if (($REMAINING * 60 / $PRESENT_RATE < $MINUMUM_BATTERY_MINUTES)) then /sbin/laptop_mode stop fi else 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." fi fi fi ---------------------------/etc/acpi/actions/battery.sh END--------------------- Monitoring tool --------------- Bartek Kania submitted this, it can be used to measure how much time your disk spends spun up/down. ---------------------------dslm.c BEGIN----------------------------------------- /* * Simple Disk Sleep Monitor * by Bartek Kania * Licenced under the GPL */ #include #include #include #include #include #include #include #include #include #include #ifdef DEBUG #define D(x) x #else #define D(x) #endif int endit = 0; /* Check if the disk is in powersave-mode * Most of the code is stolen from hdparm. * 1 = active, 0 = standby/sleep, -1 = unknown */ int check_powermode(int fd) { unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0}; int state; if (ioctl(fd, HDIO_DRIVE_CMD, &args) && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */ && ioctl(fd, HDIO_DRIVE_CMD, &args)) { if (errno != EIO || args[0] != 0 || args[1] != 0) { state = -1; /* "unknown"; */ } else state = 0; /* "sleeping"; */ } else { state = (args[2] == 255) ? 1 : 0; } D(printf(" drive state is: %d\n", state)); return state; } char *state_name(int i) { if (i == -1) return "unknown"; if (i == 0) return "sleeping"; if (i == 1) return "active"; return "internal error"; } char *myctime(time_t time) { char *ts = ctime(&time); ts[strlen(ts) - 1] = 0; return ts; } void measure(int fd) { time_t start_time; int last_state; time_t last_time; int curr_state; time_t curr_time = 0; time_t time_diff; time_t active_time = 0; time_t sleep_time = 0; time_t unknown_time = 0; time_t total_time = 0; int changes = 0; float tmp; printf("Starting measurements\n"); last_state = check_powermode(fd); start_time = last_time = time(0); printf(" System is in state %s\n\n", state_name(last_state)); while(!endit) { sleep(1); curr_state = check_powermode(fd); if (curr_state != last_state || endit) { changes++; curr_time = time(0); time_diff = curr_time - last_time; if (last_state == 1) active_time += time_diff; else if (last_state == 0) sleep_time += time_diff; else unknown_time += time_diff; last_state = curr_state; last_time = curr_time; printf("%s: State-change to %s\n", myctime(curr_time), state_name(curr_state)); } } changes--; /* Compensate for SIGINT */ total_time = time(0) - start_time; printf("\nTotal running time: %lus\n", curr_time - start_time); printf(" State changed %d times\n", changes); tmp = (float)sleep_time / (float)total_time * 100; printf(" Time in sleep state: %lus (%.2f%%)\n", sleep_time, tmp); tmp = (float)active_time / (float)total_time * 100; printf(" Time in active state: %lus (%.2f%%)\n", active_time, tmp); tmp = (float)unknown_time / (float)total_time * 100; printf(" Time in unknown state: %lus (%.2f%%)\n", unknown_time, tmp); } void ender(int s) { endit = 1; } void usage() { puts("usage: dslm [-w