man-pages/man2/getrusage.2.html
2021-03-31 01:06:50 +01:00

373 lines
10 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETRUSAGE</TITLE>
</HEAD><BODY>
<H1>GETRUSAGE</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
getrusage - get resource usage
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/time.h">sys/time.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/sys/resource.h">sys/resource.h</A>&gt;</B>
<P>
<B>int getrusage(int </B><I>who</I><B>, struct rusage *</B><I>usage</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<P>
<B>getrusage</B>()
returns resource usage measures for
<I>who</I>,
which can be one of the following:
<DL COMPACT>
<DT id="1"><B>RUSAGE_SELF</B>
<DD>
Return resource usage statistics for the calling process,
which is the sum of resources used by all threads in the process.
<DT id="2"><B>RUSAGE_CHILDREN</B>
<DD>
Return resource usage statistics for all children of the
calling process that have terminated and been waited for.
These statistics will include the resources used by grandchildren,
and further removed descendants,
if all of the intervening descendants waited on their terminated children.
<DT id="3"><B>RUSAGE_THREAD</B> (since Linux 2.6.26)
<DD>
Return resource usage statistics for the calling thread.
The
<B>_GNU_SOURCE</B>
feature test macro must be defined (before including
<I>any</I>
header file)
in order to obtain the definition of this constant from
<I>&lt;<A HREF="file:///usr/include/sys/resource.h">sys/resource.h</A>&gt;</I>.
</DL>
<P>
The resource usages are returned in the structure pointed to by
<I>usage</I>,
which has the following form:
<P>
struct rusage {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;timeval&nbsp;ru_utime;&nbsp;/*&nbsp;user&nbsp;CPU&nbsp;time&nbsp;used&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;timeval&nbsp;ru_stime;&nbsp;/*&nbsp;system&nbsp;CPU&nbsp;time&nbsp;used&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_maxrss;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;maximum&nbsp;resident&nbsp;set&nbsp;size&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_ixrss;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;integral&nbsp;shared&nbsp;memory&nbsp;size&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_idrss;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;integral&nbsp;unshared&nbsp;data&nbsp;size&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_isrss;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;integral&nbsp;unshared&nbsp;stack&nbsp;size&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_minflt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;page&nbsp;reclaims&nbsp;(soft&nbsp;page&nbsp;faults)&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_majflt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;page&nbsp;faults&nbsp;(hard&nbsp;page&nbsp;faults)&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_nswap;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;swaps&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_inblock;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;block&nbsp;input&nbsp;operations&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_oublock;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;block&nbsp;output&nbsp;operations&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_msgsnd;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;IPC&nbsp;messages&nbsp;sent&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_msgrcv;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;IPC&nbsp;messages&nbsp;received&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_nsignals;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;signals&nbsp;received&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_nvcsw;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;voluntary&nbsp;context&nbsp;switches&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;ru_nivcsw;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;involuntary&nbsp;context&nbsp;switches&nbsp;*/
};
<P>
Not all fields are completed;
unmaintained fields are set to zero by the kernel.
(The unmaintained fields are provided for compatibility with other systems,
and because they may one day be supported on Linux.)
The fields are interpreted as follows:
<DL COMPACT>
<DT id="4"><I>ru_utime</I>
<DD>
This is the total amount of time spent executing in user mode,
expressed in a
<I>timeval</I>
structure (seconds plus microseconds).
<DT id="5"><I>ru_stime</I>
<DD>
This is the total amount of time spent executing in kernel mode,
expressed in a
<I>timeval</I>
structure (seconds plus microseconds).
<DT id="6"><I>ru_maxrss</I> (since Linux 2.6.32)
<DD>
This is the maximum resident set size used (in kilobytes).
For
<B>RUSAGE_CHILDREN</B>,
this is the resident set size of the largest child, not the maximum
resident set size of the process tree.
<DT id="7"><I>ru_ixrss</I> (unmaintained)
<DD>
This field is currently unused on Linux.
<DT id="8"><I>ru_idrss</I> (unmaintained)
<DD>
This field is currently unused on Linux.
<DT id="9"><I>ru_isrss</I> (unmaintained)
<DD>
This field is currently unused on Linux.
<DT id="10"><I>ru_minflt</I>
<DD>
The number of page faults serviced without any I/O activity; here
I/O activity is avoided by ``reclaiming'' a page frame from
the list of pages awaiting reallocation.
<DT id="11"><I>ru_majflt</I>
<DD>
The number of page faults serviced that required I/O activity.
<DT id="12"><I>ru_nswap</I> (unmaintained)
<DD>
This field is currently unused on Linux.
<DT id="13"><I>ru_inblock</I> (since Linux 2.6.22)
<DD>
The number of times the filesystem had to perform input.
<DT id="14"><I>ru_oublock</I> (since Linux 2.6.22)
<DD>
The number of times the filesystem had to perform output.
<DT id="15"><I>ru_msgsnd</I> (unmaintained)
<DD>
This field is currently unused on Linux.
<DT id="16"><I>ru_msgrcv</I> (unmaintained)
<DD>
This field is currently unused on Linux.
<DT id="17"><I>ru_nsignals</I> (unmaintained)
<DD>
This field is currently unused on Linux.
<DT id="18"><I>ru_nvcsw</I> (since Linux 2.6)
<DD>
The number of times a context switch resulted due to a process
voluntarily giving up the processor before its time slice was
completed (usually to await availability of a resource).
<DT id="19"><I>ru_nivcsw</I> (since Linux 2.6)
<DD>
The number of times a context switch resulted due to a higher
priority process becoming runnable or because the current process
exceeded its time slice.
</DL>
<P>
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="20"><B>EFAULT</B>
<DD>
<I>usage</I>
points outside the accessible address space.
<DT id="21"><B>EINVAL</B>
<DD>
<I>who</I>
is invalid.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>getrusage</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
POSIX.1 specifies
<B>getrusage</B>(),
but specifies only the fields
<I>ru_utime</I>
and
<I>ru_stime</I>.
<P>
<B>RUSAGE_THREAD</B>
is Linux-specific.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Resource usage metrics are preserved across an
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
<P>
Including
<I>&lt;<A HREF="file:///usr/include/sys/time.h">sys/time.h</A>&gt;</I>
is not required these days, but increases portability.
(Indeed,
<I>struct timeval</I>
is defined in
<I>&lt;<A HREF="file:///usr/include/sys/time.h">sys/time.h</A>&gt;</I>.)
<P>
In Linux kernel versions before 2.6.9, if the disposition of
<B>SIGCHLD</B>
is set to
<B>SIG_IGN</B>
then the resource usages of child processes
are automatically included in the value returned by
<B>RUSAGE_CHILDREN</B>,
although POSIX.1-2001 explicitly prohibits this.
This nonconformance is rectified in Linux 2.6.9 and later.
<P>
The structure definition shown at the start of this page
was taken from 4.3BSD Reno.
<P>
Ancient systems provided a
<B>vtimes</B>()
function with a similar purpose to
<B>getrusage</B>().
For backward compatibility, glibc also provides
<B>vtimes</B>().
All new applications should be written using
<B>getrusage</B>().
<P>
See also the description of
<I>/proc/[pid]/stat</I>
in
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5).
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+times">times</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+wait">wait</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+wait4">wait4</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+clock">clock</A></B>(3)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="22"><A HREF="#lbAB">NAME</A><DD>
<DT id="23"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="24"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="25"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="26"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="27"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="28"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="29"><A HREF="#lbAI">NOTES</A><DD>
<DT id="30"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="31"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>