Released as 20240522 ('Tbilisi')
[parallel.git] / src / niceload.pod
blobf7ba7b86c2e6f49df4c1d6459c34f5c5df2e43fc
1 #!/usr/bin/perl -w
3 # SPDX-FileCopyrightText: 2021-2024 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
4 # SPDX-License-Identifier: GFDL-1.3-or-later
5 # SPDX-License-Identifier: CC-BY-SA-4.0
7 =head1 NAME
9 niceload - slow down a program when the load average is above a certain limit
11 =head1 SYNOPSIS
13 B<niceload> [-v] [-h] [-n nice] [-I io] [-L load] [-M mem] [-N]
14 [--sensor program] [-t time] [-s time|-f factor]
15 ( command | -p PID [-p PID ...] | --prg program )
18 =head1 DESCRIPTION
20 GNU B<niceload> will slow down a program when the load average (or
21 other system activity) is above a certain limit. When the limit is
22 reached the program will be suspended for some time. Then resumed
23 again for some time. Then the load average is checked again and we
24 start over.
26 Instead of load average B<niceload> can also look at disk I/O, amount
27 of free memory, or swapping activity.
29 If the load is 3.00 then the default settings will run a program
30 like this:
32 run 1 second, suspend (3.00-1.00) seconds, run 1 second, suspend
33 (3.00-1.00) seconds, run 1 second, ...
35 =head1 OPTIONS
37 =over 9
39 =item B<-B>
41 =item B<--battery>
43 Suspend if the system is running on battery. Shorthand for:
44 -l -1 --sensor 'cat /sys/class/power_supply/BAT0/status
45 /proc/acpi/battery/BAT0/state 2>/dev/null | grep -i -q discharging;
46 echo $?'
49 =item B<-f> I<FACTOR>
51 =item B<--factor> I<FACTOR>
53 Suspend time factor. Dynamically set B<-s> as amount over limit *
54 factor. Default is 1.
57 =item B<-H>
59 =item B<--hard>
61 Hard limit. B<--hard> will suspend the process until the system is
62 under the limits. The default is B<--soft>.
65 =item B<--io> I<iolimit>
67 =item B<-I> I<iolimit>
69 Limit for I/O. The amount of disk I/O will be computed as a value 0 -
70 10, where 0 is no I/O and 10 is at least one disk is 100% saturated.
72 B<--io> will set both B<--start-io> and B<--run-io>.
75 =item B<--load> I<loadlimit>
77 =item B<-L> I<loadlimit>
79 Limit for load average.
81 B<--load> will set both B<--start-load> and B<--run-load>.
84 =item B<--mem> I<memlimit>
86 =item B<-M> I<memlimit>
88 Limit for free memory. This is the amount of bytes available as free
89 + cache. This limit is treated opposite other limits: If the system
90 is above the limit the program will run, if it is below the limit the
91 program will stop
93 I<memlimit> can be postfixed with K, M, G, T, or P which would
94 multiply the size with 1024, 1048576, 1073741824, or 1099511627776
95 respectively.
97 B<--mem> will set both B<--start-mem> and B<--run-mem>.
100 =item B<--noswap>
102 =item B<-N>
104 No swapping. If the system is swapping both in and out it is a good
105 indication that the system is memory stressed.
107 B<--noswap> is over limit if the system is swapping both in and out.
109 B<--noswap> will set both B<--start-noswap> and B<--run-noswap>.
112 =item B<--net>
114 Shorthand for B<--nethops 3>.
117 =item B<--nethops> I<h>
119 Network nice. Pause if the internet connection is overloaded.
121 B<niceload> finds a router I<h> hops closer to the internet. It
122 B<ping>s this every second. If the latency is more than 50% bigger
123 than the median, it is regarded as being over the limit.
125 B<--nethops> can be combined with B<--hard>. Without B<--hard> the
126 program may be able to queue up so much traffic that it will take
127 longer than the B<--suspend> time to clear it. B<--hard> is useful for
128 traffic that does not break by being suspended for a longer time.
130 B<--nethops> can be combined with a high B<--suspend>. This way a
131 program can be allowed to do a bit of traffic now and then. This is
132 useful to keep the connection alive.
135 =item B<-n> I<niceness>
137 =item B<--nice> I<niceness>
139 Sets niceness. See B<nice>(1).
142 =item B<-p> I<PID>[,I<PID>]
144 =item B<--pid> I<PID>[,I<PID>]
146 Process IDs of processes to suspend. You can specify multiple process
147 IDs with multiple B<-p> I<PID> or by separating the PIDs with comma.
150 =item B<--prg> I<program>
152 =item B<--program> I<program>
154 Name of running program to suspend. You can specify multiple programs
155 with multiple B<--prg> I<program>. If no processes with the name
156 I<program> is found, B<niceload> with search for substrings containing
157 I<program>.
160 =item B<--quote>
162 =item B<-q>
164 Quote the command line. Useful if the command contains chars like *,
165 $, >, and " that should not be interpreted by the shell.
168 =item B<--run-io> I<iolimit>
170 =item B<--ri> I<iolimit>
172 =item B<--run-load> I<loadlimit>
174 =item B<--rl> I<loadlimit>
176 =item B<--run-mem> I<memlimit>
178 =item B<--rm> I<memlimit>
180 Run limit. The running program will be slowed down if the system is
181 above the limit. See: B<--io>, B<--load>, B<--mem>, B<--noswap>.
184 =item B<--sensor> I<sensor program>
186 Read sensor. Use I<sensor program> to read a sensor.
188 This will keep the CPU temperature below 80 deg C on GNU/Linux:
190 niceload -l 80000 -f 0.001 --sensor 'sort -n /sys/devices/platform/coretemp*/temp*_input' gzip *
192 This will stop if the disk space < 100000.
194 niceload -H -l -100000 --sensor "df . | awk '{ print \$4 }'" echo
197 =item B<--start-io> I<iolimit>
199 =item B<--si> I<iolimit>
201 =item B<--start-load> I<loadlimit>
203 =item B<--sl> I<loadlimit>
205 =item B<--start-mem> I<memlimit>
207 =item B<--sm> I<memlimit>
209 Start limit. The program will not start until the system is below the
210 limit. See: B<--io>, B<--load>, B<--mem>, B<--noswap>.
213 =item B<--soft>
215 =item B<-S>
217 Soft limit. B<niceload> will suspend a process for a while and then
218 let it run for a second thus only slowing down a process while the
219 system is over one of the given limits. This is the default.
222 =item B<--suspend> I<SEC>
224 =item B<-s> I<SEC>
226 Suspend time. Suspend the command this many seconds when the max load
227 average is reached.
230 =item B<--recheck> I<SEC>
232 =item B<-t> I<SEC>
234 Recheck load time. Sleep SEC seconds before checking load
235 again. Default is 1 second.
238 =item B<--verbose>
240 =item B<-v>
242 Verbose. Print some extra output on what is happening. Use B<-v> until
243 you know what your are doing.
245 =back
247 =head1 EXAMPLE: See niceload in action
249 In terminal 1 run: top
251 In terminal 2 run:
253 B<niceload -q perl -e '$|=1;do{$l==$r or print "."; $l=$r}until(($r=time-$^T)>>B<50)'>
255 This will print a '.' every second for 50 seconds and eat a lot of
256 CPU. When the load rises to 1.0 the process is suspended.
259 =head1 EXAMPLE: Run updatedb
261 Running B<updatedb> can often starve the system for disk I/O and thus result in a high load.
263 Run B<updatedb> but suspend B<updatedb> if the load is above 2.00:
265 B<niceload -L 2 updatedb>
268 =head1 EXAMPLE: Run rsync
270 B<rsync> can, just like B<updatedb>, starve the system for disk I/O
271 and thus result in a high load.
273 Run B<rsync> but keep load below 3.4. If load reaches 7 sleep for
274 (7-3.4)*12 seconds:
276 B<niceload -L 3.4 -f 12 rsync -Ha /home/ /backup/home/>
279 =head1 EXAMPLE: Ensure enough disk cache
281 Assume the program B<foo> uses 2 GB files intensively. B<foo> will run
282 fast if the files are in disk cache and be slow as a crawl if they are
283 not in the cache.
285 To ensure 2 GB are reserved for disk cache run:
287 B<niceload --hard --run-mem 2g foo>
289 This will not guarantee that the 2 GB memory will be used for the
290 files for B<foo>, but it will stop B<foo> if the memory for disk cache
291 is too low.
294 =head1 ENVIRONMENT VARIABLES
296 None. In future versions $NICELOAD will be able to contain default settings.
298 =head1 EXIT STATUS
300 Exit status should be the same as the command being run (untested).
302 =head1 REPORTING BUGS
304 Report bugs to <bug-parallel@gnu.org>.
306 =head1 AUTHOR
308 Copyright (C) 2004-11-19 Ole Tange, http://ole.tange.dk
310 Copyright (C) 2005-2010 Ole Tange, http://ole.tange.dk
312 Copyright (C) 2010-2024 Ole Tange, http://ole.tange.dk and Free
313 Software Foundation, Inc.
315 =head1 LICENSE
317 This program is free software; you can redistribute it and/or modify
318 it under the terms of the GNU General Public License as published by
319 the Free Software Foundation; either version 3 of the License, or
320 at your option any later version.
322 This program is distributed in the hope that it will be useful,
323 but WITHOUT ANY WARRANTY; without even the implied warranty of
324 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
325 GNU General Public License for more details.
327 You should have received a copy of the GNU General Public License
328 along with this program. If not, see <http://www.gnu.org/licenses/>.
330 =head2 Documentation license I
332 Permission is granted to copy, distribute and/or modify this
333 documentation under the terms of the GNU Free Documentation License,
334 Version 1.3 or any later version published by the Free Software
335 Foundation; with no Invariant Sections, with no Front-Cover Texts, and
336 with no Back-Cover Texts. A copy of the license is included in the
337 file LICENSES/GFDL-1.3-or-later.txt.
339 =head2 Documentation license II
341 You are free:
343 =over 9
345 =item B<to Share>
347 to copy, distribute and transmit the work
349 =item B<to Remix>
351 to adapt the work
353 =back
355 Under the following conditions:
357 =over 9
359 =item B<Attribution>
361 You must attribute the work in the manner specified by the author or
362 licensor (but not in any way that suggests that they endorse you or
363 your use of the work).
365 =item B<Share Alike>
367 If you alter, transform, or build upon this work, you may distribute
368 the resulting work only under the same, similar or a compatible
369 license.
371 =back
373 With the understanding that:
375 =over 9
377 =item B<Waiver>
379 Any of the above conditions can be waived if you get permission from
380 the copyright holder.
382 =item B<Public Domain>
384 Where the work or any of its elements is in the public domain under
385 applicable law, that status is in no way affected by the license.
387 =item B<Other Rights>
389 In no way are any of the following rights affected by the license:
391 =over 2
393 =item *
395 Your fair dealing or fair use rights, or other applicable
396 copyright exceptions and limitations;
398 =item *
400 The author's moral rights;
402 =item *
404 Rights other persons may have either in the work itself or in
405 how the work is used, such as publicity or privacy rights.
407 =back
409 =back
411 =over 9
413 =item B<Notice>
415 For any reuse or distribution, you must make clear to others the
416 license terms of this work.
418 =back
420 A copy of the full license is included in the file as
421 LICENCES/CC-BY-SA-4.0.txt
424 =head1 DEPENDENCIES
426 GNU B<niceload> uses Perl, and the Perl modules POSIX, and
427 Getopt::Long.
429 =head1 SEE ALSO
431 B<parallel>(1), B<nice>(1), B<uptime>(1)
433 =cut