#!/usr/bin/perl -w =head1 NAME niceload - slow down a program when the load average is above a certain limit =head1 SYNOPSIS B [-v] [-h] [-n nice] [-I io] [-L load] [-M mem] [-N] [--sensor program] [-t time] [-s time|-f factor] ( command | -p PID [-p PID ...] | --prg program ) =head1 DESCRIPTION GNU B will slow down a program when the load average (or other system activity) is above a certain limit. When the limit is reached the program will be suspended for some time. Then resumed again for some time. Then the load average is checked again and we start over. Instead of load average B can also look at disk I/O, amount of free memory, or swapping activity. If the load is 3.00 then the default settings will run a program like this: run 1 second, suspend (3.00-1.00) seconds, run 1 second, suspend (3.00-1.00) seconds, run 1 second, ... =head1 OPTIONS =over 9 =item B<-B> (beta testing) =item B<--battery> (beta testing) Suspend if the system is running on battery. Short hand for: -l -1 --sensor 'cat /sys/class/power_supply/BAT0/status /proc/acpi/battery/BAT0/state 2>/dev/null |grep -i -q discharging; echo $?' =item B<-f> I =item B<--factor> I Suspend time factor. Dynamically set B<-s> as amount over limit * factor. Default is 1. =item B<-H> =item B<--hard> Hard limit. B<--hard> will suspend the process until the system is under the limits. The default is B<--soft>. =item B<--io> I =item B<-I> I Limit for I/O. The amount of disk I/O will be computed as a value 0 - 10, where 0 is no I/O and 10 is at least one disk is 100% saturated. B<--io> will set both B<--start-io> and B. =item B<--load> I =item B<-L> I Limit for load average. B<--load> will set both B<--start-load> and B. =item B<--mem> I =item B<-M> I Limit for free memory. This is the amount of bytes available as free + cache. This limit is treated opposite other limits: If the system is above the limit the program will run, if it is below the limit the program will stop I can be postfixed with K, M, G, T, or P which would multiply the size with 1024, 1048576, 1073741824, or 1099511627776 respectively. B<--mem> will set both B<--start-mem> and B. =item B<--noswap> =item B<-N> No swapping. If the system is swapping both in and out it is a good indication that the system is memory stressed. B<--noswap> is over limit if the system is swapping both in and out. B<--noswap> will set both B<--start-noswap> and B. =item B<-n> I =item B<--nice> I Sets niceness. See B(1). =item B<-p> I =item B<--pid> I Process ID of process to suspend. You can specify multiple process IDs with multiple B<-p> I. =item B<--prg> I =item B<--program> I Name of running program to suspend. You can specify multiple programs with multiple B<--prg> I. =item B<--quote> =item B<-q> Quote the command line. Useful if the command contains chars like *, $, >, and " that should not be interpreted by the shell. =item B<--run-io> I =item B<--ri> I =item B<--run-load> I =item B<--rl> I =item B<--run-mem> I =item B<--rm> I Run limit. The running program will be slowed down if the system is above the limit. See: B<--io>, B<--load>, B<--mem>, B<--noswap>. =item B<--sensor> I Read sensor. Use I to read a sensor. This will keep the CPU temperature below 80 deg C on GNU/Linux: niceload -l 80000 -f 0.001 --sensor 'sort -n /sys/devices/platform/coretemp*/temp*_input' gzip * This will stop if the disk space < 100000. niceload -H -l -100000 --sensor "df . | awk '{ print \$4 }'" echo =item B<--start-io> I =item B<--si> I =item B<--start-load> I =item B<--sl> I =item B<--start-mem> I =item B<--sm> I Start limit. The program will not start until the system is below the limit. See: B<--io>, B<--load>, B<--mem>, B<--noswap>. =item B<--soft> =item B<-S> Soft limit. B will suspend a process for a while and then let it run for a second thus only slowing down a process while the system is over one of the given limits. This is the default. =item B<--suspend> I =item B<-s> I Suspend time. Suspend the command this many seconds when the max load average is reached. =item B<--recheck> I =item B<-t> I Recheck load time. Sleep SEC seconds before checking load again. Default is 1 second. =item B<--verbose> =item B<-v> Verbose. Print some extra output on what is happening. Use B<-v> until you know what your are doing. =back =head1 EXAMPLE: See niceload in action In terminal 1 run: top In terminal 2 run: B>B<50)'> This will print a '.' every second for 50 seconds and eat a lot of CPU. When the load rises to 1.0 the process is suspended. =head1 EXAMPLE: Run updatedb Running updatedb can often starve the system for disk I/O and thus result in a high load. Run updatedb but suspend updatedb if the load is above 2.00: B =head1 EXAMPLE: Run rsync rsync can just like updatedb starve the system for disk I/O and thus result in a high load. Run rsync but keep load below 3.4. If load reaches 7 sleep for (7-3.4)*12 seconds: B =head1 EXAMPLE: Ensure enough disk cache Assume the program B uses 2 GB files intensively. B will run fast if the files are in disk cache and be slow as a crawl if they are not in the cache. To ensure 2 GB are reserved for disk cache run: B This will not guarantee that the 2 GB memory will be used for the files for B, but it will stop B if the memory for disk cache is too low. =head1 ENVIRONMENT VARIABLES None. In future versions $NICELOAD will be able to contain default settings. =head1 EXIT STATUS Exit status should be the same as the command being run (untested). =head1 REPORTING BUGS Report bugs to . =head1 AUTHOR Copyright (C) 2004-11-19 Ole Tange, http://ole.tange.dk Copyright (C) 2005,2006,2006,2008,2009,2010 Ole Tange, http://ole.tange.dk Copyright (C) 2010,2011,2012 Ole Tange, http://ole.tange.dk and Free Software Foundation, Inc. =head1 LICENSE Copyright (C) 2010,2011,2012 Free Software Foundation, Inc. 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 3 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, see . =head2 Documentation license I Permission is granted to copy, distribute and/or modify this documentation under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the file fdl.txt. =head2 Documentation license II You are free: =over 9 =item B to copy, distribute and transmit the work =item B to adapt the work =back Under the following conditions: =over 9 =item B You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). =item B If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. =back With the understanding that: =over 9 =item B Any of the above conditions can be waived if you get permission from the copyright holder. =item B Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license. =item B In no way are any of the following rights affected by the license: =over 2 =item * Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations; =item * The author's moral rights; =item * Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights. =back =back =over 9 =item B For any reuse or distribution, you must make clear to others the license terms of this work. =back A copy of the full license is included in the file as cc-by-sa.txt. =head1 DEPENDENCIES GNU B uses Perl, and the Perl modules POSIX, and Getopt::Long. =head1 SEE ALSO B(1), B(1), B(1) =cut