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