| 1 | # | ||||||||||||||||||
| 2 | # Copyright (c) 1995-2015 Jarkko Hietaniemi. All rights reserved. | ||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||
| 4 | # modify it under the same terms as Perl itself. | ||||||||||||||||||
| 5 | # | ||||||||||||||||||
| 6 | # Resource.pm | ||||||||||||||||||
| 7 | # | ||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 5 | require 5.002; | |||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | package BSD::Resource; | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 8 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | 8 | ▶ | use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD $VERSION); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 5 | $VERSION = '1.2909'; | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 8 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | 8 | ▶ | use AutoLoader; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 4 | require Exporter; | |||||||||||||||||
| 22 | 4 | require DynaLoader; | |||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 4 | @ISA = qw(Exporter DynaLoader); | |||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 4 | @EXPORT = qw( | |||||||||||||||||
| 27 | PRIO_MAX | ||||||||||||||||||
| 28 | PRIO_MIN | ||||||||||||||||||
| 29 | PRIO_PGRP | ||||||||||||||||||
| 30 | PRIO_PROCESS | ||||||||||||||||||
| 31 | PRIO_USER | ||||||||||||||||||
| 32 | RLIMIT_AIO_MEM | ||||||||||||||||||
| 33 | RLIMIT_AIO_OPS | ||||||||||||||||||
| 34 | RLIMIT_AS | ||||||||||||||||||
| 35 | RLIMIT_CORE | ||||||||||||||||||
| 36 | RLIMIT_CPU | ||||||||||||||||||
| 37 | RLIMIT_DATA | ||||||||||||||||||
| 38 | RLIMIT_FSIZE | ||||||||||||||||||
| 39 | RLIMIT_LOCKS | ||||||||||||||||||
| 40 | RLIMIT_MEMLOCK | ||||||||||||||||||
| 41 | RLIMIT_MSGQUEUE | ||||||||||||||||||
| 42 | RLIMIT_NICE | ||||||||||||||||||
| 43 | RLIMIT_NOFILE | ||||||||||||||||||
| 44 | RLIMIT_NPROC | ||||||||||||||||||
| 45 | RLIMIT_NPTS | ||||||||||||||||||
| 46 | RLIMIT_OFILE | ||||||||||||||||||
| 47 | RLIMIT_OPEN_MAX | ||||||||||||||||||
| 48 | RLIMIT_PTHREAD | ||||||||||||||||||
| 49 | RLIMIT_RSS | ||||||||||||||||||
| 50 | RLIMIT_RTPRIO | ||||||||||||||||||
| 51 | RLIMIT_RTTIME | ||||||||||||||||||
| 52 | RLIMIT_SBSIZE | ||||||||||||||||||
| 53 | RLIMIT_SIGPENDING | ||||||||||||||||||
| 54 | RLIMIT_STACK | ||||||||||||||||||
| 55 | RLIMIT_SWAP | ||||||||||||||||||
| 56 | RLIMIT_TCACHE | ||||||||||||||||||
| 57 | RLIMIT_VMEM | ||||||||||||||||||
| 58 | RLIM_INFINITY | ||||||||||||||||||
| 59 | RLIM_NLIMITS | ||||||||||||||||||
| 60 | RLIM_SAVED_CUR | ||||||||||||||||||
| 61 | RLIM_SAVED_MAX | ||||||||||||||||||
| 62 | RUSAGE_BOTH | ||||||||||||||||||
| 63 | RUSAGE_CHILDREN | ||||||||||||||||||
| 64 | RUSAGE_SELF | ||||||||||||||||||
| 65 | RUSAGE_THREAD | ||||||||||||||||||
| 66 | get_rlimits | ||||||||||||||||||
| 67 | getpriority | ||||||||||||||||||
| 68 | getrlimit | ||||||||||||||||||
| 69 | getrusage | ||||||||||||||||||
| 70 | setpriority | ||||||||||||||||||
| 71 | setrlimit | ||||||||||||||||||
| 72 | ); | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | 4 | Exporter::export_tags(); | |||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | 4 | @EXPORT_OK = qw(times); | |||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | # Grandfather old foo_h form to new :foo_h form | ||||||||||||||||||
| 79 | sub import { | ||||||||||||||||||
| 80 | 8 | ▶ | my $this = shift; | ||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.0
| |||||||||||||||||||
| 81 | 8 | ▶ | my @list = map { m/^\w+_h$/ ? ":$_" : $_ } @_; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 82 | 8 | local $Exporter::ExportLevel = 1; | |||||||||||||||||
| 83 | 8 | Exporter::import($this,@list); | |||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | 4 | bootstrap BSD::Resource; | |||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 4 | my $EINVAL = constant("EINVAL", 0); | |||||||||||||||||
| 89 | 4 | my $EAGAIN = constant("EAGAIN", 0); | |||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | sub AUTOLOAD { | ||||||||||||||||||
| 92 | 43 | ▶ | if ($AUTOLOAD =~ /::(_?[a-z])/) { | ||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=7 SCAR=20.1
Branch: 1 branch
| |||||||||||||||||||
| 93 | 18 | $AutoLoader::AUTOLOAD = $AUTOLOAD; | |||||||||||||||||
| 94 | 18 | goto &AutoLoader::AUTOLOAD; | |||||||||||||||||
| 95 | } | ||||||||||||||||||
| 96 | 25 | local $! = 0; | |||||||||||||||||
| 97 | 25 | my $constname = $AUTOLOAD; | |||||||||||||||||
| 98 | 25 | $constname =~ s/.*:://; | |||||||||||||||||
| 99 | 25 | ▶ | return if $constname eq 'DESTROY'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 100 | 25 | ▶ | my $val = constant($constname, @_ ? $_[0] : 0); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 101 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 102 | 25 | ▶ | if ($! == 0) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 103 | 6 | ▶ | *$AUTOLOAD = sub { $val }; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | elsif ($! == $EAGAIN) { # Not really a constant, so always call. | ||||||||||||||||||
| 106 | 0 | ▶ | *$AUTOLOAD = sub { constant($constname, $_[0]) }; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | elsif ($! == $EINVAL) { | ||||||||||||||||||
| 109 | 19 | croak "$constname is not a valid BSD::Resource macro"; | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | else { | ||||||||||||||||||
| 112 | 0 | croak "Your vendor has not defined BSD::Resource macro $constname, used"; | |||||||||||||||||
| 113 | } | ||||||||||||||||||
| 114 | 8 | ▶ | use strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 6 | goto &$AUTOLOAD; | |||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | 8 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | =pod | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | =head1 NAME | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | BSD::Resource - BSD process resource limit and priority functions | ||||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | =head1 SYNOPSIS | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | use BSD::Resource; | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | # | ||||||||||||||||||
| 131 | # the process resource consumption so far | ||||||||||||||||||
| 132 | # | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | ($usertime, $systemtime, | ||||||||||||||||||
| 135 | $maxrss, $ixrss, $idrss, $isrss, $minflt, $majflt, $nswap, | ||||||||||||||||||
| 136 | $inblock, $oublock, $msgsnd, $msgrcv, | ||||||||||||||||||
| 137 | $nsignals, $nvcsw, $nivcsw) = getrusage($ru_who); | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | $rusage = getrusage($ru_who); | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | # | ||||||||||||||||||
| 142 | # the process resource limits | ||||||||||||||||||
| 143 | # | ||||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | ($nowsoft, $nowhard) = getrlimit($resource); | ||||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | $rlimit = getrlimit($resource); | ||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | $success = setrlimit($resource, $newsoft, $newhard); | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | # | ||||||||||||||||||
| 152 | # the process scheduling priority | ||||||||||||||||||
| 153 | # | ||||||||||||||||||
| 154 | |||||||||||||||||||
| 155 | $nowpriority = getpriority($pr_which, $pr_who); | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | $success = setpriority($pr_which, $pr_who, $priority); | ||||||||||||||||||
| 158 | |||||||||||||||||||
| 159 | # The following is not a BSD function. | ||||||||||||||||||
| 160 | # It is a Perlish utility for the users of BSD::Resource. | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | $rlimits = get_rlimits(); | ||||||||||||||||||
| 163 | |||||||||||||||||||
| 164 | =head1 DESCRIPTION | ||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | =head2 getrusage | ||||||||||||||||||
| 167 | |||||||||||||||||||
| 168 | ($usertime, $systemtime, | ||||||||||||||||||
| 169 | $maxrss, $ixrss, $idrss, $isrss, $minflt, $majflt, $nswap, | ||||||||||||||||||
| 170 | $inblock, $oublock, $msgsnd, $msgrcv, | ||||||||||||||||||
| 171 | $nsignals, $nvcsw, $nivcsw) = getrusage($ru_who); | ||||||||||||||||||
| 172 | |||||||||||||||||||
| 173 | $rusage = getrusage($ru_who); | ||||||||||||||||||
| 174 | |||||||||||||||||||
| 175 | # $ru_who argument is optional; it defaults to RUSAGE_SELF | ||||||||||||||||||
| 176 | |||||||||||||||||||
| 177 | $rusage = getrusage(); | ||||||||||||||||||
| 178 | |||||||||||||||||||
| 179 | The $ru_who argument is either C<RUSAGE_SELF> (the current process) or | ||||||||||||||||||
| 180 | C<RUSAGE_CHILDREN> (all the child processes of the current process) | ||||||||||||||||||
| 181 | or it maybe left away in which case C<RUSAGE_SELF> is used. | ||||||||||||||||||
| 182 | |||||||||||||||||||
| 183 | The C<RUSAGE_CHILDREN> is the total sum of all the so far | ||||||||||||||||||
| 184 | I<terminated> (either successfully or unsuccessfully) child processes: | ||||||||||||||||||
| 185 | there is no way to find out information about child processes still | ||||||||||||||||||
| 186 | running. | ||||||||||||||||||
| 187 | |||||||||||||||||||
| 188 | On some systems (those supporting both getrusage() with the POSIX | ||||||||||||||||||
| 189 | threads) there can also be C<RUSAGE_THREAD>. The BSD::Resource supports | ||||||||||||||||||
| 190 | the C<RUSAGE_THREAD> if it is present but understands nothing more about | ||||||||||||||||||
| 191 | the POSIX threads themselves. Similarly for C<RUSAGE_BOTH>: some systems | ||||||||||||||||||
| 192 | support retrieving the sums of the self and child resource consumptions | ||||||||||||||||||
| 193 | simultaneously. | ||||||||||||||||||
| 194 | |||||||||||||||||||
| 195 | In list context getrusage() returns the current resource usages as a | ||||||||||||||||||
| 196 | list. On failure it returns an empty list. | ||||||||||||||||||
| 197 | |||||||||||||||||||
| 198 | The elements of the list are, in order: | ||||||||||||||||||
| 199 | index name meaning usually (quite system dependent) | ||||||||||||||||||
| 200 | |||||||||||||||||||
| 201 | 0 utime user time | ||||||||||||||||||
| 202 | 1 stime system time | ||||||||||||||||||
| 203 | 2 maxrss maximum shared memory or current resident set | ||||||||||||||||||
| 204 | 3 ixrss integral shared memory | ||||||||||||||||||
| 205 | 4 idrss integral or current unshared data | ||||||||||||||||||
| 206 | 5 isrss integral or current unshared stack | ||||||||||||||||||
| 207 | 6 minflt page reclaims | ||||||||||||||||||
| 208 | 7 majflt page faults | ||||||||||||||||||
| 209 | 8 nswap swaps | ||||||||||||||||||
| 210 | 9 inblock block input operations | ||||||||||||||||||
| 211 | 10 oublock block output operations | ||||||||||||||||||
| 212 | 11 msgsnd messages sent | ||||||||||||||||||
| 213 | 12 msgrcv messaged received | ||||||||||||||||||
| 214 | 13 nsignals signals received | ||||||||||||||||||
| 215 | 14 nvcsw voluntary context switches | ||||||||||||||||||
| 216 | 15 nivcsw involuntary context switches | ||||||||||||||||||
| 217 | |||||||||||||||||||
| 218 | In scalar context getrusage() returns the current resource usages as a | ||||||||||||||||||
| 219 | an object. The object can be queried via methods named exactly like | ||||||||||||||||||
| 220 | the middle column, I<name>, in the above table. | ||||||||||||||||||
| 221 | |||||||||||||||||||
| 222 | $ru = getrusage(); | ||||||||||||||||||
| 223 | print $ru->stime, "\n"; | ||||||||||||||||||
| 224 | |||||||||||||||||||
| 225 | $total_context_switches = $ru->nvcsw + $ru->nivcsw; | ||||||||||||||||||
| 226 | |||||||||||||||||||
| 227 | For a detailed description about the values returned by getrusage() | ||||||||||||||||||
| 228 | please consult your usual C programming documentation about | ||||||||||||||||||
| 229 | getrusage() and also the header file C<E<lt>sys/resource.hE<gt>>. | ||||||||||||||||||
| 230 | (In B<Solaris>, this might be C<E<lt>sys/rusage.hE<gt>>). | ||||||||||||||||||
| 231 | |||||||||||||||||||
| 232 | See also L</"KNOWN ISSUES">. | ||||||||||||||||||
| 233 | |||||||||||||||||||
| 234 | =head2 getrlimit | ||||||||||||||||||
| 235 | |||||||||||||||||||
| 236 | ($nowsoft, $nowhard) = getrlimit($resource); | ||||||||||||||||||
| 237 | |||||||||||||||||||
| 238 | $rlimit = getrlimit($resource); | ||||||||||||||||||
| 239 | |||||||||||||||||||
| 240 | The $resource argument can be one of | ||||||||||||||||||
| 241 | |||||||||||||||||||
| 242 | $resource usual meaning usual unit | ||||||||||||||||||
| 243 | |||||||||||||||||||
| 244 | RLIMIT_CPU CPU time seconds | ||||||||||||||||||
| 245 | |||||||||||||||||||
| 246 | RLIMIT_FSIZE file size bytes | ||||||||||||||||||
| 247 | |||||||||||||||||||
| 248 | RLIMIT_DATA data size bytes | ||||||||||||||||||
| 249 | RLIMIT_STACK stack size bytes | ||||||||||||||||||
| 250 | RLIMIT_CORE coredump size bytes | ||||||||||||||||||
| 251 | RLIMIT_RSS resident set size bytes | ||||||||||||||||||
| 252 | RLIMIT_MEMLOCK memory locked data size bytes | ||||||||||||||||||
| 253 | |||||||||||||||||||
| 254 | RLIMIT_NPROC number of processes 1 | ||||||||||||||||||
| 255 | |||||||||||||||||||
| 256 | RLIMIT_NOFILE number of open files 1 | ||||||||||||||||||
| 257 | RLIMIT_OFILE number of open files 1 | ||||||||||||||||||
| 258 | RLIMIT_OPEN_MAX number of open files 1 | ||||||||||||||||||
| 259 | |||||||||||||||||||
| 260 | RLIMIT_LOCKS number of file locks 1 | ||||||||||||||||||
| 261 | |||||||||||||||||||
| 262 | RLIMIT_AS (virtual) address space bytes | ||||||||||||||||||
| 263 | RLIMIT_VMEM virtual memory (space) bytes | ||||||||||||||||||
| 264 | |||||||||||||||||||
| 265 | RLIMIT_PTHREAD number of pthreads 1 | ||||||||||||||||||
| 266 | RLIMIT_TCACHE maximum number of 1 | ||||||||||||||||||
| 267 | cached threads | ||||||||||||||||||
| 268 | |||||||||||||||||||
| 269 | RLIMIT_AIO_MEM maximum memory locked bytes | ||||||||||||||||||
| 270 | for POSIX AIO | ||||||||||||||||||
| 271 | RLIMIT_AIO_OPS maximum number 1 | ||||||||||||||||||
| 272 | for POSIX AIO ops | ||||||||||||||||||
| 273 | |||||||||||||||||||
| 274 | RLIMIT_NPTS maximum number of 1 | ||||||||||||||||||
| 275 | pseudo-terminals | ||||||||||||||||||
| 276 | |||||||||||||||||||
| 277 | RLIMIT_SBSIZE socket buffer size bytes | ||||||||||||||||||
| 278 | |||||||||||||||||||
| 279 | RLIMIT_SWAP maximum swap size bytes | ||||||||||||||||||
| 280 | |||||||||||||||||||
| 281 | RLIMIT_MSGQUEUE POSIX mq size bytes | ||||||||||||||||||
| 282 | |||||||||||||||||||
| 283 | RLIMIT_RTPRIO maximum RT priority 1 | ||||||||||||||||||
| 284 | RLIMIT_RTTIME maximum RT time microseconds | ||||||||||||||||||
| 285 | RLIMIT_SIGPENDING pending signals 1 | ||||||||||||||||||
| 286 | |||||||||||||||||||
| 287 | B<What limits are available depends on the operating system>. | ||||||||||||||||||
| 288 | |||||||||||||||||||
| 289 | See below for C<get_rlimits()> on how to find out which limits are | ||||||||||||||||||
| 290 | available, for the exact documentation consult the documentation of | ||||||||||||||||||
| 291 | your operating system (setrlimit documentation, usually). | ||||||||||||||||||
| 292 | |||||||||||||||||||
| 293 | The two groups (C<NOFILE>, C<OFILE>, C<OPEN_MAX>) and (C<AS>, C<VMEM>) | ||||||||||||||||||
| 294 | are aliases within themselves. | ||||||||||||||||||
| 295 | |||||||||||||||||||
| 296 | Two meta-resource-symbols might exist | ||||||||||||||||||
| 297 | |||||||||||||||||||
| 298 | RLIM_NLIMITS | ||||||||||||||||||
| 299 | RLIM_INFINITY | ||||||||||||||||||
| 300 | |||||||||||||||||||
| 301 | C<RLIM_NLIMITS> being the number of possible (but not necessarily fully | ||||||||||||||||||
| 302 | supported) resource limits, see also the get_rlimits() call below. | ||||||||||||||||||
| 303 | C<RLIM_INFINITY> is useful in setrlimit(), the C<RLIM_INFINITY> is | ||||||||||||||||||
| 304 | often represented as minus one (-1). | ||||||||||||||||||
| 305 | |||||||||||||||||||
| 306 | In list context C<getrlimit()> returns the current soft and hard | ||||||||||||||||||
| 307 | resource limits as a list. On failure it returns an empty list. | ||||||||||||||||||
| 308 | |||||||||||||||||||
| 309 | Processes have soft and hard resource limits. On crossing the soft | ||||||||||||||||||
| 310 | limit they receive a signal (for example the C<SIGXCPU> or C<SIGXFSZ>, | ||||||||||||||||||
| 311 | corresponding to the C<RLIMIT_CPU> and C<RLIMIT_FSIZE>, respectively). | ||||||||||||||||||
| 312 | The processes can trap and handle some of these signals, please see | ||||||||||||||||||
| 313 | L<perlipc/Signals>. After the hard limit the processes will be | ||||||||||||||||||
| 314 | ruthlessly killed by the C<KILL> signal which cannot be caught. | ||||||||||||||||||
| 315 | |||||||||||||||||||
| 316 | B<NOTE>: the level of 'support' for a resource varies. Not all the systems | ||||||||||||||||||
| 317 | |||||||||||||||||||
| 318 | a) even recognise all those limits | ||||||||||||||||||
| 319 | b) really track the consumption of a resource | ||||||||||||||||||
| 320 | c) care (send those signals) if a resource limit is exceeded | ||||||||||||||||||
| 321 | |||||||||||||||||||
| 322 | Again, please consult your usual C programming documentation. | ||||||||||||||||||
| 323 | |||||||||||||||||||
| 324 | One notable exception for the better: officially B<HP-UX> does not | ||||||||||||||||||
| 325 | support getrlimit() at all but for the time being, it does seem to. | ||||||||||||||||||
| 326 | |||||||||||||||||||
| 327 | In scalar context C<getrlimit()> returns the current soft limit. | ||||||||||||||||||
| 328 | On failure it returns C<undef>. | ||||||||||||||||||
| 329 | |||||||||||||||||||
| 330 | =head2 getpriority | ||||||||||||||||||
| 331 | |||||||||||||||||||
| 332 | # $pr_which can be PRIO_USER, PRIO_PROCESS, or PRIO_PGRP, | ||||||||||||||||||
| 333 | # and in some systems PRIO_THREAD | ||||||||||||||||||
| 334 | |||||||||||||||||||
| 335 | $nowpriority = getpriority($pr_which, $pr_who); | ||||||||||||||||||
| 336 | |||||||||||||||||||
| 337 | # the default $pr_who is 0 (the current $pr_which) | ||||||||||||||||||
| 338 | |||||||||||||||||||
| 339 | $nowpriority = getpriority($pr_which); | ||||||||||||||||||
| 340 | |||||||||||||||||||
| 341 | # the default $pr_which is PRIO_PROCESS (the process priority) | ||||||||||||||||||
| 342 | |||||||||||||||||||
| 343 | $nowpriority = getpriority(); | ||||||||||||||||||
| 344 | |||||||||||||||||||
| 345 | getpriority() returns the current priority. B<NOTE>: getpriority() | ||||||||||||||||||
| 346 | can return zero or negative values completely legally. On failure | ||||||||||||||||||
| 347 | getpriority() returns C<undef> (and C<$!> is set as usual). | ||||||||||||||||||
| 348 | |||||||||||||||||||
| 349 | The priorities returned by getpriority() are in the (inclusive) range | ||||||||||||||||||
| 350 | C<PRIO_MIN>...C<PRIO_MAX>. The $pr_which argument can be any of | ||||||||||||||||||
| 351 | PRIO_PROCESS (a process) C<PRIO_USER> (a user), or C<PRIO_PGRP> (a | ||||||||||||||||||
| 352 | process group). The $pr_who argument tells which process/user/process | ||||||||||||||||||
| 353 | group, 0 signifying the current one. | ||||||||||||||||||
| 354 | |||||||||||||||||||
| 355 | Usual values for C<PRIO_MIN>, C<PRIO_MAX>, are -20, 20. A negative | ||||||||||||||||||
| 356 | value means better priority (more impolite process), a positive value | ||||||||||||||||||
| 357 | means worse priority (more polite process). | ||||||||||||||||||
| 358 | |||||||||||||||||||
| 359 | =head2 setrlimit | ||||||||||||||||||
| 360 | |||||||||||||||||||
| 361 | $success = setrlimit($resource, $newsoft, $newhard); | ||||||||||||||||||
| 362 | |||||||||||||||||||
| 363 | setrlimit() returns true on success and C<undef> on failure. | ||||||||||||||||||
| 364 | |||||||||||||||||||
| 365 | B<NOTE>: A normal user process can only lower its resource limits. | ||||||||||||||||||
| 366 | Soft or hard limit C<RLIM_INFINITY> means as much as possible, the | ||||||||||||||||||
| 367 | real hard limits are normally buried inside the kernel and are B<very> | ||||||||||||||||||
| 368 | system-dependent. | ||||||||||||||||||
| 369 | |||||||||||||||||||
| 370 | B<NOTE>: Even the soft limit that is actually set might be lower than | ||||||||||||||||||
| 371 | what requested for various reasons. One possibility is that the | ||||||||||||||||||
| 372 | actual limit on a resource might be controlled by some system variable | ||||||||||||||||||
| 373 | (e.g. in BSD systems the RLIMIT_NPROC can be capped by the system | ||||||||||||||||||
| 374 | variable C<maxprocperuid>, try C<sysctl -a kern.maxprocperuid>), | ||||||||||||||||||
| 375 | or in many environments core dumping has been disabled from normal | ||||||||||||||||||
| 376 | user processes. Another possibility is that a limit is rounded down | ||||||||||||||||||
| 377 | to some alignment or granularity, for example the memory limits might | ||||||||||||||||||
| 378 | be rounded down to the closest 4 kilobyte boundary. In other words, | ||||||||||||||||||
| 379 | do not expect to be able to setrlimit() a limit to a value and then be | ||||||||||||||||||
| 380 | able to read back the same value with getrlimit(). | ||||||||||||||||||
| 381 | |||||||||||||||||||
| 382 | =head2 setpriority | ||||||||||||||||||
| 383 | |||||||||||||||||||
| 384 | $success = setpriority($pr_which, $pr_who, $priority); | ||||||||||||||||||
| 385 | |||||||||||||||||||
| 386 | # NOTE! If there are two arguments the second one is | ||||||||||||||||||
| 387 | # the new $priority (not $pr_who) and the $pr_who is | ||||||||||||||||||
| 388 | # defaulted to 0 (the current $pr_which) | ||||||||||||||||||
| 389 | |||||||||||||||||||
| 390 | $success = setpriority($pr_which, $priority); | ||||||||||||||||||
| 391 | |||||||||||||||||||
| 392 | # The $pr_who defaults to 0 (the current $pr_which) and | ||||||||||||||||||
| 393 | # the $priority defaults to half of the PRIO_MAX, usually | ||||||||||||||||||
| 394 | # that amounts to 10 (being a nice $pr_which). | ||||||||||||||||||
| 395 | |||||||||||||||||||
| 396 | $success = setpriority($pr_which); | ||||||||||||||||||
| 397 | |||||||||||||||||||
| 398 | # The $pr_which defaults to PRIO_PROCESS. | ||||||||||||||||||
| 399 | |||||||||||||||||||
| 400 | $success = setpriority(); | ||||||||||||||||||
| 401 | |||||||||||||||||||
| 402 | setpriority() is used to change the scheduling priority. A positive | ||||||||||||||||||
| 403 | priority means a more polite process/process group/user; a negative | ||||||||||||||||||
| 404 | priority means a more impolite process/process group/user. | ||||||||||||||||||
| 405 | The priorities handled by setpriority() are [C<PRIO_MIN>,C<PRIO_MAX>]. | ||||||||||||||||||
| 406 | A normal user process can only lower its priority (make it more positive). | ||||||||||||||||||
| 407 | |||||||||||||||||||
| 408 | B<NOTE>: A successful call returns C<1>, a failed one C<0>. | ||||||||||||||||||
| 409 | |||||||||||||||||||
| 410 | See also L</"KNOWN ISSUES">. | ||||||||||||||||||
| 411 | |||||||||||||||||||
| 412 | =head2 times | ||||||||||||||||||
| 413 | |||||||||||||||||||
| 414 | use BSD::Resource qw(times); | ||||||||||||||||||
| 415 | |||||||||||||||||||
| 416 | ($user, $system, $child_user, $child_system) = times(); | ||||||||||||||||||
| 417 | |||||||||||||||||||
| 418 | The BSD::Resource module offers a times() implementation that has | ||||||||||||||||||
| 419 | usually slightly better time granularity than the times() by Perl | ||||||||||||||||||
| 420 | core. The time granularity of the latter is usually 1/60 seconds | ||||||||||||||||||
| 421 | while the former may achieve submilliseconds. | ||||||||||||||||||
| 422 | |||||||||||||||||||
| 423 | B<NOTE>: The current implementation uses two getrusage() system calls: | ||||||||||||||||||
| 424 | one with RUSAGE_SELF and one with RUSAGE_CHILDREN. Therefore the | ||||||||||||||||||
| 425 | operation is not `atomic': the times for the children are recorded | ||||||||||||||||||
| 426 | a little bit later. | ||||||||||||||||||
| 427 | |||||||||||||||||||
| 428 | B<NOTE>: times() is not imported by default by BSD::Resource. | ||||||||||||||||||
| 429 | You need to tell that you want to use it. | ||||||||||||||||||
| 430 | |||||||||||||||||||
| 431 | B<NOTE: times() is not a "real BSD" function. It is older UNIX.> | ||||||||||||||||||
| 432 | |||||||||||||||||||
| 433 | =head2 get_rlimits | ||||||||||||||||||
| 434 | |||||||||||||||||||
| 435 | use BSD::Resource qw{get_rlimits}; | ||||||||||||||||||
| 436 | my $limits = get_rlimits(); | ||||||||||||||||||
| 437 | |||||||||||||||||||
| 438 | B<NOTE: This is not a real BSD function. It is a convenience function | ||||||||||||||||||
| 439 | introduced by BSD::Resource.> | ||||||||||||||||||
| 440 | |||||||||||||||||||
| 441 | get_rlimits() returns a reference to hash which has the names of the | ||||||||||||||||||
| 442 | available resource limits as keys and their indices (those which | ||||||||||||||||||
| 443 | are needed as the first argument to getrlimit() and setrlimit()) | ||||||||||||||||||
| 444 | as values. For example: | ||||||||||||||||||
| 445 | |||||||||||||||||||
| 446 | use BSD::Resource qw{get_rlimits}; | ||||||||||||||||||
| 447 | my $limits = get_rlimits(); | ||||||||||||||||||
| 448 | for my $name (keys %$limits) { | ||||||||||||||||||
| 449 | my ($soft, $hard) = BSD::Resource::getrlimit($limits->{$name}); | ||||||||||||||||||
| 450 | print "$name soft $soft hard $hard\n"; | ||||||||||||||||||
| 451 | } | ||||||||||||||||||
| 452 | |||||||||||||||||||
| 453 | Note that a limit of -1 means unlimited. | ||||||||||||||||||
| 454 | |||||||||||||||||||
| 455 | =head1 ERRORS | ||||||||||||||||||
| 456 | |||||||||||||||||||
| 457 | =over 4 | ||||||||||||||||||
| 458 | |||||||||||||||||||
| 459 | =item * | ||||||||||||||||||
| 460 | |||||||||||||||||||
| 461 | Your vendor has not defined BSD::Resource macro ... | ||||||||||||||||||
| 462 | |||||||||||||||||||
| 463 | The code tried to call getrlimit/setrlimit for a resource limit that | ||||||||||||||||||
| 464 | your operating system vendor/supplier does not support. Portable code | ||||||||||||||||||
| 465 | should use get_rlimits() to check which resource limits are defined. | ||||||||||||||||||
| 466 | |||||||||||||||||||
| 467 | =back | ||||||||||||||||||
| 468 | |||||||||||||||||||
| 469 | =head1 EXAMPLES | ||||||||||||||||||
| 470 | |||||||||||||||||||
| 471 | # the user and system times so far by the process itself | ||||||||||||||||||
| 472 | |||||||||||||||||||
| 473 | ($usertime, $systemtime) = getrusage(); | ||||||||||||||||||
| 474 | |||||||||||||||||||
| 475 | # ditto in OO way | ||||||||||||||||||
| 476 | |||||||||||||||||||
| 477 | $ru = getrusage(); | ||||||||||||||||||
| 478 | |||||||||||||||||||
| 479 | $usertime = $ru->utime; | ||||||||||||||||||
| 480 | $systemtime = $ru->stime; | ||||||||||||||||||
| 481 | |||||||||||||||||||
| 482 | # get the current priority level of this process | ||||||||||||||||||
| 483 | |||||||||||||||||||
| 484 | $currprio = getpriority(); | ||||||||||||||||||
| 485 | |||||||||||||||||||
| 486 | =head1 KNOWN ISSUES | ||||||||||||||||||
| 487 | |||||||||||||||||||
| 488 | In B<AIX> (at least version 3, maybe later also releases) if the BSD | ||||||||||||||||||
| 489 | compatibility library is not installed or not found by the BSD::Resource | ||||||||||||||||||
| 490 | installation procedure and when using the getpriority() or setpriority(), | ||||||||||||||||||
| 491 | the C<PRIO_MIN> is 0 (corresponding to -20) and C<PRIO_MAX> is 39 | ||||||||||||||||||
| 492 | (corresponding to 19, the BSD priority 20 is unreachable). | ||||||||||||||||||
| 493 | |||||||||||||||||||
| 494 | In B<HP-UX> the getrusage() is not Officially Supported at all but for | ||||||||||||||||||
| 495 | the time being, it does seem to be. | ||||||||||||||||||
| 496 | |||||||||||||||||||
| 497 | In B<Mac OS X> a normal user cannot raise the C<RLIM_NPROC> over the | ||||||||||||||||||
| 498 | maxprocperuid limit (the default value is 266, try the command | ||||||||||||||||||
| 499 | C<sysctl -a kern.maxprocperuid>). | ||||||||||||||||||
| 500 | |||||||||||||||||||
| 501 | In B<NetBSD> C<RLIMIT_STACK> setrlimit() calls fail. | ||||||||||||||||||
| 502 | |||||||||||||||||||
| 503 | In B<Cygwin> C<RLIMIT_STACK> setrlimit calls fail. Also, setrlimit() | ||||||||||||||||||
| 504 | C<RLIMIT_NOFILE/RLIMIT_OFILE/RLIMIT_OFILE> calls return success, but | ||||||||||||||||||
| 505 | then the subsequent getrlimit calls show that the limits didn't really | ||||||||||||||||||
| 506 | change. | ||||||||||||||||||
| 507 | |||||||||||||||||||
| 508 | Because not all UNIX kernels are BSD and also because of the sloppy | ||||||||||||||||||
| 509 | support of getrusage() by many vendors many of the getrusage() values | ||||||||||||||||||
| 510 | may not be correctly updated. For example B<Solaris 1> claims in | ||||||||||||||||||
| 511 | C<E<lt>sys/rusage.hE<gt>> that the C<ixrss> and the C<isrss> fields | ||||||||||||||||||
| 512 | are always zero. In B<SunOS 5.5 and 5.6> the getrusage() leaves most | ||||||||||||||||||
| 513 | of the fields zero and therefore getrusage() is not even used, instead | ||||||||||||||||||
| 514 | of that the B</proc> interface is used. The mapping is not perfect: | ||||||||||||||||||
| 515 | the C<maxrss> field is really the B<current> resident size instead of the | ||||||||||||||||||
| 516 | maximum, the C<idrss> is really the B<current> heap size instead of the | ||||||||||||||||||
| 517 | integral data, and the C<isrss> is really the B<current> stack size | ||||||||||||||||||
| 518 | instead of the integral stack. The ixrss has no sensible counterpart | ||||||||||||||||||
| 519 | at all so it stays zero. | ||||||||||||||||||
| 520 | |||||||||||||||||||
| 521 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||
| 522 | |||||||||||||||||||
| 523 | Copyright 1995-2015 Jarkko Hietaniemi All Rights Reserved | ||||||||||||||||||
| 524 | |||||||||||||||||||
| 525 | This module free software; you can redistribute it and/or modify it | ||||||||||||||||||
| 526 | under the terms of the Artistic License 2.0 or GNU Lesser General | ||||||||||||||||||
| 527 | Public License 2.0. For more details, see the full text of the | ||||||||||||||||||
| 528 | licenses at <http://www.perlfoundation.org/artistic_license_2_0>, | ||||||||||||||||||
| 529 | and <http://www.gnu.org/licenses/gpl-2.0.html>. | ||||||||||||||||||
| 530 | |||||||||||||||||||
| 531 | =head1 AUTHOR | ||||||||||||||||||
| 532 | |||||||||||||||||||
| 533 | Jarkko Hietaniemi, C<jhi@iki.fi> | ||||||||||||||||||
| 534 | |||||||||||||||||||
| 535 | =cut | ||||||||||||||||||
| 536 | |||||||||||||||||||
| 537 | 5 | 1; | |||||||||||||||||
| 538 | __END__ | ||||||||||||||||||