/usr/bin/reniced is in reniced 1.19-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | #!/usr/bin/perl -w
#
# reniced - renice running processes based on regular expressions
#
# Copyright (C) 2005,2007,2010 Christian Garbs <mitch@cgarbs.de>
# Licensed under GNU GPL. See COPYING for details.
use strict;
=head1 NAME
reniced - renice running processes based on regular expressions
=head1 SYNOPSIS
B<reniced>
S<[B<-h>]>
S<[B<-v>]>
S<[B<-o> I<format>]>
S<[I<configfile>]>
=head1 OVERVIEW
reniced takes a list of regular expressions, looks for processes (and
threads) matching them and renices the processes to given values.
reniced can also change io priorities.
=head1 DESCRIPTION
On start, reniced reads a configuration file. It consists of nice
values and regular expressions.
It then scans the process table using the L<ps(1)> command.
Whenever a process name from the CMD column matches a regular
expression, that process is reniced to the given value. If a process
matches multiple regular expressions, all rule matches are executed in
order and the last match wins.
When run as root, reniced will scan all processes (C<`ps H -e`>).
When run as a user, renice only scans the user's processes (C<`ps H --user`>).
=head2 Switches
=over 5
=item B<-h>
This prints the version number, a short help text and exits without
doing anything.
=item B<-v>
This activates verbose mode. Error messages, some statistics and all
renice actions are printed to stdout.
=item B<-o> I<format>
Set the L<ps(1)> output format to filter on. The default format is
C<comm>. See the B<-o> parameter in the L<ps(1)> manpage for details.
=item I<configfile>
This reads the regular expressions from an alternate configfile.
The default location of the configfile is C</etc/reniced.conf> if reniced
is run as root, C<~/.reniced> otherwise.
=back
=head2 Configuration file format
The configuration file is composed of single lines. Empty lines and
lines starting with a B<#> are ignored.
Every line must consist of a command followed by a whitespace and a
Perl regular expression.
The regular expression is matched against the L<ps(1)> output. For
every matched process the command is executed.
A command generally takes the form of a character followed by a
number. Multiple commands can be given simultaneously with no spaces
inbetween. Sometimes the number is optional.
=head3 Command characters
=over 5
=item B<n>
Sets the nice value of a process. Must be followed by a number,
usually within the range of -20 to 19.
For backwards compatibility a B<n> at the beginning of the command can
be left out (if the command starts with a number it is treated as a
nice value).
=item B<r>
Sets the io priority to the realtime scheduling class. The optional
number is treated as class data (typically 0-7, lower being higher
priority).
=item B<b>
Sets the io priority to the best-effort scheduling class. The
optional number is treated as class data (typically 0-7, lower being
higher priority).
=item B<i>
Sets the io priority to the idle scheduling class. No number needs to
be given as the idle scheduling class ignores the class data value.
=item B<o>
Sets the OOM killer adjustment in C</proc/$PID/oom_adj> to the given
number.
=back
=head3 Examples
=over 5
=item C<5 ^bash>
gives currently running bash shells a nice value of 5
=item C<b2 ^tar>
sets currently running tar-processes to io priority best-effort within class 2
=item C<i torrent>
sets currently running torrent-like applications to io priority idle
=item C<n-10r4 seti>
gives currently running seti-processes a nice value of -10 and sets
them to realtime io priority in class 4
=back
=head1 MODULES NEEDED
use BSD::Resource;
This module can be obtained from L<http://www.cpan.org>.
=head1 PROGRAMS NEEDED
ps
ionice
ionice is only needed if you want to change io priority. It can be
obtained from L<http://rlove.org/schedutils/>.
You also need a suitable kernel and scheduler, e.g. Linux 2.6 with
CFQ.
=head1 BUGS
reniced can run without the BSD::Resource module. In this case, the
PRIO_PROCESS is set to 0. This works on Linux 2.6.11 i686 but it
could break on other systems. Installing BSD::Resource is the safer
way.
Be careful using realtime priorities, don't starve other tasks.
Please report bugs to <F<mitch@cgarbs.de>>.
=head1 AUTHOR
reniced was written by Christian Garbs <F<mitch@cgarbs.de>>.
=head1 COPYRIGHT
reniced is Copyright (C) 2005,2007 by Christian Garbs. It is licensed
under the GNU GPL.
=head1 AVAILABILITY
Look for updates at L<http://www.cgarbs.de/stuff.en.html>.
=head1 SEE ALSO
L<ionice(1)>, L<renice(1)>, L<ps(1)>
=cut
### Global settings
# default values for rulefile position
my $rulefile_root = '/etc/reniced.conf';
my $rulefile_user = '~/.reniced';
# default debug value
my $debug = 0;
# default ps output format
my $psformat = 'comm';
# are we root?
my $root = $> == 0;
# a dynamically calculated constant :-)
my $PRIO_PROCESS;
#
my %SCHEDULING_CLASS = ( 1 => 'realtime',
2 => 'best-effort',
3 => 'idle' );
### Subroutines
sub show_help()
# print options
{
print << 'EOF';
Usage:
reniced [-h] [-v] [-o format] [configfile]
Options:
-h print help
-v be verbose
-o format output format to filter on
see ps(1) manpage for details
default: comm
configfile read alternative configuration file
default: /etc/reniced.conf for root
~/.reniced for others
Configuration file format:
# is a comment
command perl_regular_expression
Command format:
5 set nice value to 5
b2 set io priority to best effort class 2
i set io priority to idle
-10r4 set nice value to -10 and io priority to realtime class 4
o-10 set OOM value /proc/$PID/oom_adj to -10
Version:
reniced 1.19
EOF
;
}
sub debug(@)
# print debug messages
{
return unless $debug;
my $format = shift @_;
printf "$format\n", @_;
}
sub get_prio_process()
# get the numerical value for PRIO_PROCESS
{
# Check for BSD::Resource which has the constant
eval { require BSD::Resource; };
if (not $@) {
eval { use BSD::Resource qw(PRIO_PROCESS); };
$PRIO_PROCESS = PRIO_PROCESS;
debug 'PRIO_PROCESS set to %d via BSD::Resource', $PRIO_PROCESS;
} else {
# dirty fallback, works for my Linux 2.6.11 i686 GNU/Linux
# see setpriority(2) and /usr/include/bits/resource.h
$PRIO_PROCESS = 0;
debug 'PRIO_PROCESS to %d via fallback', $PRIO_PROCESS;
}
}
sub parse_options()
# check if "-v" is given
{
while (@ARGV) {
if ($ARGV[0] eq '-o') {
shift @ARGV;
if (@ARGV) {
$psformat = shift @ARGV;
} else {
die "-o is missing the format parameter\n";
}
next;
}
if ($ARGV[0] eq '-v') {
shift @ARGV;
$debug = 1;
next;
}
if ($ARGV[0] eq '-h') {
shift @ARGV;
show_help();
exit 0;
}
last;
}
}
sub find_rulefile()
# find rulefile
{
my $rulefile;
if ($root) {
$rulefile = $rulefile_root;
} else {
$rulefile = $rulefile_user;
}
if ($ARGV[0]) {
$rulefile = shift @ARGV;
}
$rulefile =~ s/^~/$ENV{HOME}/;
debug 'rulefile: %s', $rulefile;
return $rulefile;
}
sub read_rulefile()
# read rules
{
my $rulefile = find_rulefile();
my @rule;
open RULES, "<$rulefile" or die "can't open `$rulefile': $!\n";
while (my $line = <RULES>) {
chomp $line;
next if ($line =~ /^\s*$/);
next if ($line =~ /^#/);
if ($line =~ /^\s*((?:[norbi]?-?\d*)+)\s+(.+)/) {
my $command = $1;
my $rule = { REGEXP => $2 };
# add missing n at start (backwards compatibility)
$command =~ s/^(-?\d+)/n$1/;
# nice value starts with n
while ($command =~ s/n(-?\d+)//) {
$rule->{NICE} = $1;
}
# OOM value starts with o
while ($command =~ s/o(-?\d+)//) {
$rule->{OOMADJ} = $1;
}
# ionice values start with [rbi]
while ($command =~ s/([rbi])(\d+)?//) {
if ($1 eq 'r') {
$rule->{IOCLASS} = 1;
} elsif ($1 eq 'b') {
$rule->{IOCLASS} = 2;
} elsif ($1 eq 'i') {
$rule->{IOCLASS} = 3;
} else {
warn "rule line #$.: error during IOnice parsing: 1=`$1' 2=`$2' all=`$line'";
}
if (defined $2) {
$rule->{IONICE} = $2;
}
}
if (scalar keys %{$rule} > 1) {
push @rule, $rule;
} else {
warn "rule line #$. skipped: `$line'\n";
}
} else {
warn "rules line #$. skipped: `$line'\n";
}
}
close RULES or die "can't close `$rulefile': $!\n";
debug '%d rules read', scalar @rule;
return \@rule;
}
sub generate_ps_command()
# generate ps commandline
{
my $cmdline = 'ps';
if ($root) {
$cmdline .= " H -eo lwp,$psformat";
} else {
$cmdline .= " H -o lwp,$psformat --user $>";
}
debug 'ps commandline is: %s', $cmdline;
return $cmdline;
}
sub read_processes()
# read processes
{
my @proc;
my $cmdline = generate_ps_command();
open PS, "$cmdline|" or die "can't open `$cmdline': $!\n";
{
my $line = <PS>; # skip first line
while ($line = <PS>) {
chomp $line;
my $pid = substr($line, 0, 5 )+ 0;
my $cmd = substr($line, 6 );
push @proc, { PID => $pid, CMD => $cmd };
}
}
close PS or die "can't close `$cmdline': $!\n";
debug '%d processes read', scalar @proc;
return \@proc;
}
sub renice_processes($$)
# renice
{
my $rules = shift;
my $procs = shift;
foreach my $proc (@{$procs}) {
foreach my $rule (@{$rules}) {
if ($proc->{CMD} =~ /$rule->{REGEXP}/) {
# nice
if (exists $rule->{NICE}) {
my $success = setpriority $PRIO_PROCESS, $proc->{PID}, $rule->{NICE};
debug '%snice set to %d: %d/%s'
, $success ? '' : 'FAILED: ', $rule->{NICE}, $proc->{PID}, $proc->{CMD};
}
# OOM adjust
if (exists $rule->{OOMADJ}) {
my $procfile = '/proc/'.$proc->{PID}.'/oom_adj';
my $success = 1;
if ( open (PROC, '>', $procfile) ) {
print PROC $rule->{OOMADJ}."\n";
close PROC or $success = 0;
} else {
$success = 0;
}
debug '%sOOM adjust set to %d: %d/%s'
, $success ? '' : "FAILED ($!): ", $rule->{OOMADJ}, $proc->{PID}, $proc->{CMD};
}
# IO nice
if (exists $rule->{IOCLASS}) {
if (exists $rule->{IONICE}) {
my $success = system 'ionice', '-c', $rule->{IOCLASS}, '-p', $proc->{PID}, '-n', $rule->{IONICE};
debug '%sionice set to %s, class %d: %d/%s'
, $success ? 'FAILED: ' : '', $SCHEDULING_CLASS{$rule->{IOCLASS}}, $rule->{IONICE}, $proc->{PID}, $proc->{CMD};
} else {
my $success = system 'ionice', '-c', $rule->{IOCLASS}, '-p', $proc->{PID};
debug '%sionice set to %s: %d/%s'
, $success ? 'FAILED: ' : '', $SCHEDULING_CLASS{$rule->{IOCLASS}}, $proc->{PID}, $proc->{CMD};
}
}
}
}
}
}
### Main program
parse_options();
get_prio_process();
my $rules = read_rulefile();
exit unless @{$rules};
my $procs = read_processes();
exit unless @{$procs};
renice_processes($rules, $procs);
exit 0;
|