File Coverage

blib/lib/BSD/Resource.pm
Criterion Covered Total %
statement 68 71 95.7
branch 26 34 76.4
condition n/a
subroutine 18 19 94.7
pod 5 6 83.3
total 117 130 90.0


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