Resource(3)
π NAME π
Okay bestie, this is Resource(3) β a user-contributed Perl doc for BSD::Resource, and it is literally all about BSD process resource limit and priority functions. Iconic. Essential. Slightly chaotic. We love her. β¨
β¨ SYNOPSIS β¨
Here's the whole vibe-check code, exactly as the page gives it:
use BSD::Resource;
#
# the process resource consumption so far
#
($usertime, $systemtime,
$maxrss, $ixrss, $idrss, $isrss, $minflt, $majflt, $nswap,
$inblock, $oublock, $msgsnd, $msgrcv,
$nsignals, $nvcsw, $nivcsw) = getrusage($ru_who);
$rusage = getrusage($ru_who);
#
# the process resource limits
#
($nowsoft, $nowhard) = getrlimit($resource);
$rlimit = getrlimit($resource);
$success = setrlimit($resource, $newsoft, $newhard);
#
# the process scheduling priority
#
$nowpriority = getpriority($pr_which, $pr_who);
$success = setpriority($pr_which, $pr_who, $priority);
# The following is not a BSD function.
# It is a Perlish utility for the users of BSD::Resource.
$rlimits = get_rlimits();
πΈ DESCRIPTION πΈ
π§ getrusage
($usertime, $systemtime,
$maxrss, $ixrss, $idrss, $isrss, $minflt, $majflt, $nswap,
$inblock, $oublock, $msgsnd, $msgrcv,
$nsignals, $nvcsw, $nivcsw) = getrusage($ru_who);
$rusage = getrusage($ru_who);
# $ru_who argument is optional; it defaults to RUSAGE_SELF
$rusage = getrusage();
So the $ru_who argument is either RUSAGE_SELF (the current process) or RUSAGE_CHILDREN (all the child processes of the current process), or you can just leave it out and then RUSAGE_SELF is used. Default behavior, we stan. π
RUSAGE_CHILDREN is the total sum of all the so-far terminated child processes β either successfully or unsuccessfully, no judgment. There is no way to find out information about child processes that are still running. That's just the rules, bestie.
On some systems β the ones that support both getrusage() with the POSIX threads β there can also be RUSAGE_THREAD. BSD::Resource supports RUSAGE_THREAD if it is present, but understands nothing more about the POSIX threads themselves. Similarly for RUSAGE_BOTH: some systems support retrieving the sums of the self and child resource consumptions simultaneously. Cute.
In list context getrusage() returns the current resource usages as a list. On failure it returns an empty list. π
The list slots are, in order, and the meanings are usually (quite system dependent):
0βutimeβ user time1βstimeβ system time2βmaxrssβ maximum shared memory or current resident set3βixrssβ integral shared memory4βidrssβ integral or current unshared data5βisrssβ integral or current unshared stack6βminfltβ page reclaims7βmajfltβ page faults8βnswapβ swaps9βinblockβ block input operations10βoublockβ block output operations11βmsgsndβ messages sent12βmsgrcvβ messages received13βnsignalsβ signals received14βnvcswβ voluntary context switches15βnivcswβ involuntary context switches
In scalar context getrusage() returns the current resource usages as an object. The object can be queried via methods named exactly like the middle column, name, in the table above. Slay.
$ru = getrusage();
print $ru->stime, "\n";
$total_context_switches = $ru->nvcsw + $ru->nivcsw;
For a detailed description about the values returned by getrusage() please consult your usual C programming documentation about getrusage() and also the header file "<sys/resource.h>". (In Solaris, this might be "<sys/rusage.h>".) π
Also: see KNOWN ISSUES. Don't skip her, she's relevant.
π¦ getrlimit
($nowsoft, $nowhard) = getrlimit($resource);
$rlimit = getrlimit($resource);
The $resource argument can be one of these resource symbols, with their usual meaning and usual unit:
RLIMIT_CPUβ CPU time β secondsRLIMIT_FSIZEβ file size β bytesRLIMIT_DATAβ data size β bytesRLIMIT_STACKβ stack size β bytesRLIMIT_COREβ coredump size β bytesRLIMIT_RSSβ resident set size β bytesRLIMIT_MEMLOCKβ memory locked data size β bytesRLIMIT_NPROCβ number of processes β1RLIMIT_NOFILEβ number of open files β1RLIMIT_OFILEβ number of open files β1RLIMIT_OPEN_MAXβ number of open files β1RLIMIT_LOCKSβ number of file locks β1RLIMIT_ASβ (virtual) address space β bytesRLIMIT_VMEMβ virtual memory (space) β bytesRLIMIT_PTHREADβ number of pthreads β1RLIMIT_TCACHEβ maximum number of cached threads β1RLIMIT_AIO_MEMβ maximum memory locked for POSIX AIO β bytesRLIMIT_AIO_OPSβ maximum number for POSIX AIO ops β1RLIMIT_FREEMEMβ portion of the total memoryRLIMIT_NTHRβ maximum number of threads β1RLIMIT_NPTSβ maximum number of pseudo-terminals β1RLIMIT_RSESTACKβ RSE stack size β bytesRLIMIT_SBSIZEβ socket buffer size β bytesRLIMIT_SWAPβ maximum swap size β bytesRLIMIT_MSGQUEUEβ POSIX mq size β bytesRLIMIT_RTPRIOβ maximum RT priority β1RLIMIT_RTTIMEβ maximum RT time β microsecondsRLIMIT_SIGPENDINGβ pending signals β1
What limits are available depends on the operating system. Obviously. See below for get_rlimits() on how to find out which limits are available; for the exact documentation consult the documentation of your operating system (setrlimit documentation, usually).
The two groups (NOFILE, OFILE, OPEN_MAX) and (AS, VMEM) are aliases within themselves. Bestie, they're the same thing wearing different outfits. π
Two meta-resource-symbols might exist:
RLIM_NLIMITS
RLIM_INFINITY
RLIM_NLIMITS is the number of possible (but not necessarily fully supported) resource limits, see also the get_rlimits() call below. RLIM_INFINITY is useful in setrlimit(); RLIM_INFINITY is often represented as minus one (-1). No cap.
In list context getrlimit() returns the current soft and hard resource limits as a list. On failure it returns an empty list.
Processes have soft and hard resource limits. On crossing the soft limit they receive a signal (for example the SIGXCPU or SIGXFSZ, corresponding to the RLIMIT_CPU and RLIMIT_FSIZE, respectively). The processes can trap and handle some of these signals, please see "Signals" in perlipc. After the hard limit the processes will be ruthlessly killed by the KILL signal which cannot be caught. That's not a vibe, that's just facts. π
NOTE: the level of βsupportβ for a resource varies. Not all the systems: - a) even recognise all those limits - b) really track the consumption of a resource - c) care (send those signals) if a resource limit is exceeded
Again, please consult your usual C programming documentation.
One notable exception for the better: officially HP-UX does not support getrlimit() at all, but for the time being, it does seem to. We love a plot twist. π«Ά
In scalar context getrlimit() returns the current soft limit. On failure it returns undef.
π getpriority
# $pr_which can be PRIO_USER, PRIO_PROCESS, or PRIO_PGRP,
# and in some systems PRIO_THREAD
$nowpriority = getpriority($pr_which, $pr_who);
# the default $pr_who is 0 (the current $pr_which)
$nowpriority = getpriority($pr_which);
# the default $pr_which is PRIO_PROCESS (the process priority)
$nowpriority = getpriority();
getpriority() returns the current priority. NOTE: getpriority() can return zero or negative values completely legally. On failure getpriority() returns undef (and $! is set as usual).
The priorities returned by getpriority() are in the (inclusive) range PRIO_MIN...PRIO_MAX. The $pr_which argument can be any of PRIO_PROCESS (a process), PRIO_USER (a user), or PRIO_PGRP (a process group). The $pr_who argument tells which process/user/process group, 0 signifying the current one.
Usual values for PRIO_MIN, PRIO_MAX, are -20, 20. A negative value means better priority (more impolite process), a positive value means worse priority (more polite process). It's giving βbe nice or be noticed.β π
π setrlimit
$success = setrlimit($resource, $newsoft, $newhard);
setrlimit() returns true on success and undef on failure.
NOTE: A normal user process can only lower its resource limits.
Soft or hard limit RLIM_INFINITY means as much as possible; the real hard limits are normally buried inside the kernel and are very system-dependent.
NOTE: Even the soft limit that is actually set might be lower than what was requested, for various reasons. One possibility is that the actual limit on a resource might be controlled by some system variable (e.g. in BSD systems theRLIMIT_NPROCcan be capped by the system variablemaxprocperuid, trysysctl -a kern.maxprocperuid), or in many environments core dumping has been disabled from normal user processes. Another possibility is that a limit is rounded down to some alignment or granularity; for example, the memory limits might be rounded down to the closest 4 kilobyte boundary. In other words, do not expect to be able tosetrlimit()a limit to a value and then be able to read back the same value withgetrlimit(). She rounds. It's fine. π
π₯ setpriority
$success = setpriority($pr_which, $pr_who, $priority);
# NOTE! If there are two arguments the second one is
# the new $priority (not $pr_who) and the $pr_who is
# defaulted to 0 (the current $pr_which)
$success = setpriority($pr_which, $priority);
# The $pr_who defaults to 0 (the current $pr_which) and
# the $priority defaults to half of the PRIO_MAX, usually
# that amounts to 10 (being a nice $pr_which).
$success = setpriority($pr_which);
# The $pr_which defaults to PRIO_PROCESS.
$success = setpriority();
setpriority() is literally the tool you reach for when you wanna change the scheduling priority, no cap. A positive priority = a more polite process/process group/user π«Ά (it's giving "after you, bestie"). A negative priority = a more impolite process/process group/user π
(it's giving elbows out, main character energy). The priorities setpriority() deals with live in the range ["PRIO_MIN","PRIO_MAX"]. And here's the thing β a normal user process can only lower its priority (aka make it more positive). You can't just grab negative priority for free, that's a privileged move, periodt.
β οΈ NOTE: a successful call returns1, a failed one returns0. Yes, really. That's the vibe.
Also go peek at "KNOWN ISSUES" β it's relevant, trust. π
β° times β°
use BSD::Resource qw(times);
($user, $system, $child_user, $child_system) = times();
The BSD::Resource module serves up a times() implementation that usually has slightly better time granularity than the times() you get from Perl core π. Like, the Perl core one's granularity is typically 1/60 seconds, while this one may achieve submilliseconds. She's precise. She's iconic.
β οΈ NOTE: the current implementation uses twogetrusage()system calls β one withRUSAGE_SELFand one withRUSAGE_CHILDREN. So the operation is not 'atomic' π: the times for the children get recorded a little bit later than the rest. Slight lag, bestie.
β οΈ NOTE:times()is not imported by default byBSD::Resource. You gotta say you want it. Ask and you shall receive π.
β οΈ NOTE: times() is not a "real BSD" function. It's older UNIX, ok? Respect your elders. π
π get_rlimits π
use BSD::Resource qw{get_rlimits};
my $limits = get_rlimits();
β οΈ NOTE: this is not a real BSD function either β it's a convenience function introduced by BSD::Resource. A little gift, from us to you π.
get_rlimits() returns a reference to a hash whose keys are the names of the available resource limits and whose values are their indices (the exact things you need as the first argument to getrlimit() and setrlimit()). For example:
use BSD::Resource qw{get_rlimits};
my $limits = get_rlimits();
for my $name (keys %$limits) {
my ($soft, $hard) = BSD::Resource::getrlimit($limits->{$name});
print "$name soft $soft hard $hard\n";
}
Note that a limit of -1 means unlimited β¨ (go off, infinite vibes).
π¨ ERRORS π¨
Your vendor has not defined BSD::Resource macro ...β the code tried to callgetrlimit/setrlimitfor a resource limit that your operating system vendor/supplier does not support π¬. Portable code should useget_rlimits()to check which resource limits are actually defined. Do the vibe check first, bestie.
πΈ EXAMPLES πΈ
# the user and system times so far by the process itself
($usertime, $systemtime) = getrusage();
# ditto in OO way
$ru = getrusage();
$usertime = $ru->utime;
$systemtime = $ru->stime;
# get the current priority level of this process
$currprio = getpriority();
π KNOWN ISSUES π
Okay so this is the part where we air the grievances. Buckle up π’.
- In AIX (at least version 3, maybe later releases too) β if the BSD compatibility library is not installed or not found by the
BSD::Resourceinstallation procedure, and when you're usinggetpriority()orsetpriority()β"PRIO_MIN"is0(corresponding to-20) and"PRIO_MAX"is39(corresponding to19; the BSD priority20is unreachable). So close, yet so far π. - In HP-UX,
getrusage()is not Officially Supported at all, but for the time being it does seem to be. Living on a prayer, fr fr π. - In Mac OS X, a normal user cannot raise the
"RLIM_NPROC"over themaxprocperuidlimit. The default value is266, and you can try the commandsysctl -a kern.maxprocperuidto peek at it π. - In NetBSD,
"RLIMIT_STACK"setrlimit()calls fail. Justβ¦ fail. π - In Cygwin,
"RLIMIT_STACK"setrlimitcalls fail. Also,setrlimit()"RLIMIT_NOFILE/RLIMIT_OFILE/RLIMIT_OFILE"calls return success β but then the subsequentgetrlimitcalls show that the limits didn't really change π. It's giving "I said I'd do it" and then not doing it. - Because not all UNIX kernels are BSD, and also because of the sloppy support of
getrusage()by many vendors, many of thegetrusage()values may not be correctly updated. For example, Solaris 1 claims in"<sys/rusage.h>"that the"ixrss"and the"isrss"fields are always zero. In SunOS 5.5 and 5.6 thegetrusage()leaves most of the fields zero and thereforegetrusage()is not even used β instead of that, the/procinterface is used. And the mapping is not perfect, bestie: the"maxrss"field is really the current resident size instead of the maximum, the"idrss"is really the current heap size instead of the integral data, and the"isrss"is really the current stack size instead of the integral stack. Theixrsshas no sensible counterpart at all, so it just stays zero. Poor thing π₯².
π COPYRIGHT AND LICENSE π
Copyright 1995-2017 Jarkko Hietaniemi All Rights Reserved
This module is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0 or GNU Lesser General Public License 2.0. For more details, see the full text of the licenses at <http://www.perlfoundation.org/artistic_license_2_0>, and <http://www.gnu.org/licenses/gpl-2.0.html>.
π AUTHOR π
Jarkko Hietaniemi, "jhi@iki.fi"
That's the whole page, bestie β written by Jarkko Hietaniemi, shipped with perl v5.42.0, dated 2017-04-11, and filed under Resource(3). Slay, go read some more man pages π
β¨π¦
π¦ where this page comes from
- opensuse-leap 16.0: perl-BSD-Resource 1.2911-bp160.1.13 Β· Artistic-2.0 or LGPL-2.0
- opensuse-leap 16.0: perl-BSD-Resource 1.2911-bp160.1.13 Β· Artistic-2.0 or LGPL-2.0
- opensuse-leap 16.0: perl-BSD-Resource 1.2911-bp160.1.13 Β· Artistic-2.0 or LGPL-2.0
- opensuse-leap 16.0: perl-BSD-Resource 1.2911-bp160.1.13 Β· Artistic-2.0 or LGPL-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms