blib/lib/BSD/Resource.pm

filter: Statementstmt 96.9% 96 / 99 Branchbran 76.4% 26 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.7% 15 / 16 Podpod 75.0% 3 / 4 totaltotal 91.5% 140 / 153 SCAR 32.3
CC
25
Cov
92%
AUTOLOAD
7.4
setpriority
5.0
_find_rlimit
4.2
CRAP 25.4
↑ Summary Resource.xs »
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 5 require 5.002;
9  
10 package BSD::Resource;
11  
12 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 $VERSION = '1.2910';
16  
17 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use AutoLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 require Exporter;
21 4 require DynaLoader;
22  
23 4 @ISA = qw(Exporter DynaLoader);
24  
25 4 @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 4 Exporter::export_tags();
83  
84 4 @EXPORT_OK = qw(times);
85  
86 # Grandfather old foo_h form to new :foo_h form
87 sub import {
88 8 ▶ my $this = shift;
Subroutine import: called CC=2 SCAR=7.0
89 8 ▶ my @list = map { m/^\w+_h$/ ? ":$_" : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
/^\w+_h$/ ? : 0 3 3
90 8 local $Exporter::ExportLevel = 1;
91 8 Exporter::import($this,@list);
92 }
93  
94 4 bootstrap BSD::Resource;
95  
96 4 my $EINVAL = constant("EINVAL", 0);
97 4 my $EAGAIN = constant("EAGAIN", 0);
98  
99 sub AUTOLOAD {
100 43 ▶ if ($AUTOLOAD =~ /::(_?[a-z])/) {
Subroutine AUTOLOAD: called CC=7 SCAR=20.1
Branch: 1 branch
BranchTrueFalseTotal
if ($BSD::Resource::AUTOLOAD =~ /::(_?[a-z])/) 18 25 43
101 18 $AutoLoader::AUTOLOAD = $AUTOLOAD;
102 18 goto &AutoLoader::AUTOLOAD;
103 }
104 25 local $! = 0;
105 25 my $constname = $AUTOLOAD;
106 25 $constname =~ s/.*:://;
107 25 ▶ return if $constname eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "DESTROY" 0 25 25
108 25 ▶ my $val = constant($constname, @_ ? $_[0] : 0);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 25 25
109 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 25 ▶ if ($! == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($! == 0) { } 6 19 25
elsif ($! == $EAGAIN) { } 0 19 19
elsif ($! == $EINVAL) { } 19 0 19
111 6 ▶ *$AUTOLOAD = sub { $val };
Subroutine __ANON__: called CC=1 SCAR=0.0
112 }
113 elsif ($! == $EAGAIN) { # Not really a constant, so always call.
114 0 ▶ *$AUTOLOAD = sub { constant($constname, $_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 }
116 elsif ($! == $EINVAL) {
117 19 croak "$constname is not a valid BSD::Resource macro";
118 }
119 else {
120 0 croak "Your vendor has not defined BSD::Resource macro $constname, used";
121 }
122 8 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 6 goto &$AUTOLOAD;
125 }
126 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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<RUSAGE_SELF> (the current process) or
188 C<RUSAGE_CHILDREN> (all the child processes of the current process)
189 or it maybe left away in which case C<RUSAGE_SELF> is used.
190  
191 The C<RUSAGE_CHILDREN> is the total sum of all the so far
192 I<terminated> (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<RUSAGE_THREAD>. The BSD::Resource supports
198 the C<RUSAGE_THREAD> if it is present but understands nothing more about
199 the POSIX threads themselves. Similarly for C<RUSAGE_BOTH>: 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<name>, 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 C<E<lt>sys/resource.hE<gt>>.
238 (In B<Solaris>, this might be C<E<lt>sys/rusage.hE<gt>>).
239  
240 See also L</"KNOWN ISSUES">.
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<What limits are available depends on the operating system>.
303  
304 See below for C<get_rlimits()> 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<NOFILE>, C<OFILE>, C<OPEN_MAX>) and (C<AS>, C<VMEM>)
309 are aliases within themselves.
310  
311 Two meta-resource-symbols might exist
312  
313 RLIM_NLIMITS
314 RLIM_INFINITY
315  
316 C<RLIM_NLIMITS> being the number of possible (but not necessarily fully
317 supported) resource limits, see also the get_rlimits() call below.
318 C<RLIM_INFINITY> is useful in setrlimit(), the C<RLIM_INFINITY> is
319 often represented as minus one (-1).
320  
321 In list context C<getrlimit()> 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<SIGXCPU> or C<SIGXFSZ>,
326 corresponding to the C<RLIMIT_CPU> and C<RLIMIT_FSIZE>, respectively).
327 The processes can trap and handle some of these signals, please see
328 L<perlipc/Signals>. After the hard limit the processes will be
329 ruthlessly killed by the C<KILL> signal which cannot be caught.
330  
331 B<NOTE>: 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<HP-UX> does not
340 support getrlimit() at all but for the time being, it does seem to.
341  
342 In scalar context C<getrlimit()> returns the current soft limit.
343 On failure it returns C<undef>.
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<NOTE>: getpriority()
361 can return zero or negative values completely legally. On failure
362 getpriority() returns C<undef> (and C<$!> is set as usual).
363  
364 The priorities returned by getpriority() are in the (inclusive) range
365 C<PRIO_MIN>...C<PRIO_MAX>. The $pr_which argument can be any of
366 PRIO_PROCESS (a process) C<PRIO_USER> (a user), or C<PRIO_PGRP> (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<PRIO_MIN>, C<PRIO_MAX>, 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<undef> on failure.
379  
380 B<NOTE>: A normal user process can only lower its resource limits.
381 Soft or hard limit C<RLIM_INFINITY> means as much as possible, the
382 real hard limits are normally buried inside the kernel and are B<very>
383 system-dependent.
384  
385 B<NOTE>: 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<maxprocperuid>, try C<sysctl -a kern.maxprocperuid>),
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<PRIO_MIN>,C<PRIO_MAX>].
421 A normal user process can only lower its priority (make it more positive).
422  
423 B<NOTE>: A successful call returns C<1>, a failed one C<0>.
424  
425 See also L</"KNOWN ISSUES">.
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<NOTE>: 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<NOTE>: times() is not imported by default by BSD::Resource.
444 You need to tell that you want to use it.
445  
446 B<NOTE: times() is not a "real BSD" function. It is older UNIX.>
447  
448 =head2 get_rlimits
449  
450 use BSD::Resource qw{get_rlimits};
451 my $limits = get_rlimits();
452  
453 B<NOTE: This is not a real BSD function. It is a convenience function
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<AIX> (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<PRIO_MIN> is 0 (corresponding to -20) and C<PRIO_MAX> is 39
507 (corresponding to 19, the BSD priority 20 is unreachable).
508  
509 In B<HP-UX> the getrusage() is not Officially Supported at all but for
510 the time being, it does seem to be.
511  
512 In B<Mac OS X> a normal user cannot raise the C<RLIM_NPROC> over the
513 maxprocperuid limit (the default value is 266, try the command
514 C<sysctl -a kern.maxprocperuid>).
515  
516 In B<NetBSD> C<RLIMIT_STACK> setrlimit() calls fail.
517  
518 In B<Cygwin> C<RLIMIT_STACK> setrlimit calls fail. Also, setrlimit()
519 C<RLIMIT_NOFILE/RLIMIT_OFILE/RLIMIT_OFILE> 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<Solaris 1> claims in
526 C<E<lt>sys/rusage.hE<gt>> that the C<ixrss> and the C<isrss> fields
527 are always zero. In B<SunOS 5.5 and 5.6> the getrusage() leaves most
528 of the fields zero and therefore getrusage() is not even used, instead
529 of that the B</proc> interface is used. The mapping is not perfect:
530 the C<maxrss> field is really the B<current> resident size instead of the
531 maximum, the C<idrss> is really the B<current> heap size instead of the
532 integral data, and the C<isrss> is really the B<current> 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 <http://www.perlfoundation.org/artistic_license_2_0>,
544 and <http://www.gnu.org/licenses/gpl-2.0.html>.
545  
546 =head1 AUTHOR
547  
548 Jarkko Hietaniemi, C<jhi@iki.fi>
549  
550 =cut
551  
552 5 1;
553 __END__
↑ Summary Resource.xs »