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-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             require 5.002;
10              
11             package BSD::Resource;
12              
13 7     7   4059 use strict;
  7         14  
  7         294  
14 7     7   33 use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD $VERSION);
  7         12  
  7         816  
15              
16             $VERSION = '1.2909';
17              
18 7     7   37 use Carp;
  7         13  
  7         533  
19 7     7   87896 use AutoLoader;
  7         11939  
  7         43  
20              
21             require Exporter;
22             require DynaLoader;
23              
24             @ISA = qw(Exporter DynaLoader);
25              
26             @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             Exporter::export_tags();
75              
76             @EXPORT_OK = qw(times);
77              
78             # Grandfather old foo_h form to new :foo_h form
79             sub import {
80 8     8   48 my $this = shift;
81 8 50       22 my @list = map { m/^\w+_h$/ ? ":$_" : $_ } @_;
  3         19  
82 8         18 local $Exporter::ExportLevel = 1;
83 8         16582 Exporter::import($this,@list);
84             }
85              
86             bootstrap BSD::Resource;
87              
88             my $EINVAL = constant("EINVAL", 0);
89             my $EAGAIN = constant("EAGAIN", 0);
90              
91             sub AUTOLOAD {
92 43 100   43   13620084 if ($AUTOLOAD =~ /::(_?[a-z])/) {
93 18         46 $AutoLoader::AUTOLOAD = $AUTOLOAD;
94 18         85 goto &AutoLoader::AUTOLOAD;
95             }
96 25         165 local $! = 0;
97 25         50 my $constname = $AUTOLOAD;
98 25         123 $constname =~ s/.*:://;
99 25 50       85 return if $constname eq 'DESTROY';
100 25 50       132 my $val = constant($constname, @_ ? $_[0] : 0);
101 7     7   2817 no strict 'refs';
  7         14  
  7         1230  
102 25 100       210 if ($! == 0) {
    50          
    50          
103 6     98   100 *$AUTOLOAD = sub { $val };
  98         834  
104             }
105             elsif ($! == $EAGAIN) { # Not really a constant, so always call.
106 0     0   0 *$AUTOLOAD = sub { constant($constname, $_[0]) };
  0         0  
107             }
108             elsif ($! == $EINVAL) {
109 19         2304 croak "$constname is not a valid BSD::Resource macro";
110             }
111             else {
112 0         0 croak "Your vendor has not defined BSD::Resource macro $constname, used";
113             }
114 7     7   43 use strict 'refs';
  7         14  
  7         476  
115              
116 6         48 goto &$AUTOLOAD;
117             }
118 7     7   49 use strict;
  7         19  
  7         982  
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 (the current process) or
180             C (all the child processes of the current process)
181             or it maybe left away in which case C is used.
182              
183             The C is the total sum of all the so far
184             I (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. The BSD::Resource supports
190             the C if it is present but understands nothing more about
191             the POSIX threads themselves. Similarly for C: 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, 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 Csys/resource.hE>.
230             (In B, this might be Csys/rusage.hE>).
231              
232             See also L.
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.
288              
289             See below for C 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, C, C) and (C, C)
294             are aliases within themselves.
295              
296             Two meta-resource-symbols might exist
297              
298             RLIM_NLIMITS
299             RLIM_INFINITY
300              
301             C being the number of possible (but not necessarily fully
302             supported) resource limits, see also the get_rlimits() call below.
303             C is useful in setrlimit(), the C is
304             often represented as minus one (-1).
305              
306             In list context C 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 or C,
311             corresponding to the C and C, respectively).
312             The processes can trap and handle some of these signals, please see
313             L. After the hard limit the processes will be
314             ruthlessly killed by the C signal which cannot be caught.
315              
316             B: 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 does not
325             support getrlimit() at all but for the time being, it does seem to.
326              
327             In scalar context C returns the current soft limit.
328             On failure it returns C.
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: getpriority()
346             can return zero or negative values completely legally. On failure
347             getpriority() returns C (and C<$!> is set as usual).
348              
349             The priorities returned by getpriority() are in the (inclusive) range
350             C...C. The $pr_which argument can be any of
351             PRIO_PROCESS (a process) C (a user), or C (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, C, 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 on failure.
364              
365             B: A normal user process can only lower its resource limits.
366             Soft or hard limit C means as much as possible, the
367             real hard limits are normally buried inside the kernel and are B
368             system-dependent.
369              
370             B: 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, try C),
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,C].
406             A normal user process can only lower its priority (make it more positive).
407              
408             B: A successful call returns C<1>, a failed one C<0>.
409              
410             See also L.
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: 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: times() is not imported by default by BSD::Resource.
429             You need to tell that you want to use it.
430              
431             B
432              
433             =head2 get_rlimits
434              
435             use BSD::Resource qw{get_rlimits};
436             my $limits = get_rlimits();
437              
438             B
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 (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 is 0 (corresponding to -20) and C is 39
492             (corresponding to 19, the BSD priority 20 is unreachable).
493              
494             In B the getrusage() is not Officially Supported at all but for
495             the time being, it does seem to be.
496              
497             In B a normal user cannot raise the C over the
498             maxprocperuid limit (the default value is 266, try the command
499             C).
500              
501             In B C setrlimit() calls fail.
502              
503             In B C setrlimit calls fail. Also, setrlimit()
504             C 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 claims in
511             Csys/rusage.hE> that the C and the C fields
512             are always zero. In B the getrusage() leaves most
513             of the fields zero and therefore getrusage() is not even used, instead
514             of that the B interface is used. The mapping is not perfect:
515             the C field is really the B resident size instead of the
516             maximum, the C is really the B heap size instead of the
517             integral data, and the C is really the B 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 ,
529             and .
530              
531             =head1 AUTHOR
532              
533             Jarkko Hietaniemi, C
534              
535             =cut
536              
537             1;
538             __END__