]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/getrusage.2
Many pages: Fix style issues reported by `make lint-groff`
[thirdparty/man-pages.git] / man2 / getrusage.2
1 .\" Copyright (c) 1992 Drew Eckhardt, March 28, 1992
2 .\" and Copyright (c) 2002 Michael Kerrisk
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" 2004-11-16 -- mtk: the getrlimit.2 page, which formerly included
7 .\" coverage of getrusage(2), has been split, so that the latter is
8 .\" now covered in its own getrusage.2. For older details of change
9 .\" history, etc., see getrlimit.2
10 .\"
11 .\" Modified 2004-11-16, mtk, Noted that the nonconformance
12 .\" when SIGCHLD is being ignored is fixed in 2.6.9.
13 .\" 2008-02-22, Sripathi Kodi <sripathik@in.ibm.com>: Document RUSAGE_THREAD
14 .\" 2008-05-25, mtk, clarify RUSAGE_CHILDREN + other clean-ups.
15 .\" 2010-05-24, Mark Hills <mark@pogo.org.uk>: Description of fields,
16 .\" document ru_maxrss
17 .\" 2010-05-24, mtk, enhanced description of various fields
18 .\"
19 .TH GETRUSAGE 2 2021-03-22 "Linux" "Linux Programmer's Manual"
20 .SH NAME
21 getrusage \- get resource usage
22 .SH LIBRARY
23 Standard C library
24 .RI ( libc ", " \-lc )
25 .SH SYNOPSIS
26 .nf
27 .B #include <sys/resource.h>
28 .PP
29 .BI "int getrusage(int " who ", struct rusage *" usage );
30 .fi
31 .SH DESCRIPTION
32 .BR getrusage ()
33 returns resource usage measures for
34 .IR who ,
35 which can be one of the following:
36 .TP
37 .B RUSAGE_SELF
38 Return resource usage statistics for the calling process,
39 which is the sum of resources used by all threads in the process.
40 .TP
41 .B RUSAGE_CHILDREN
42 Return resource usage statistics for all children of the
43 calling process that have terminated and been waited for.
44 These statistics will include the resources used by grandchildren,
45 and further removed descendants,
46 if all of the intervening descendants waited on their terminated children.
47 .TP
48 .BR RUSAGE_THREAD " (since Linux 2.6.26)"
49 Return resource usage statistics for the calling thread.
50 The
51 .B _GNU_SOURCE
52 feature test macro must be defined (before including
53 .I any
54 header file)
55 in order to obtain the definition of this constant from
56 .IR <sys/resource.h> .
57 .PP
58 The resource usages are returned in the structure pointed to by
59 .IR usage ,
60 which has the following form:
61 .PP
62 .in +4n
63 .EX
64 struct rusage {
65 struct timeval ru_utime; /* user CPU time used */
66 struct timeval ru_stime; /* system CPU time used */
67 long ru_maxrss; /* maximum resident set size */
68 long ru_ixrss; /* integral shared memory size */
69 long ru_idrss; /* integral unshared data size */
70 long ru_isrss; /* integral unshared stack size */
71 long ru_minflt; /* page reclaims (soft page faults) */
72 long ru_majflt; /* page faults (hard page faults) */
73 long ru_nswap; /* swaps */
74 long ru_inblock; /* block input operations */
75 long ru_oublock; /* block output operations */
76 long ru_msgsnd; /* IPC messages sent */
77 long ru_msgrcv; /* IPC messages received */
78 long ru_nsignals; /* signals received */
79 long ru_nvcsw; /* voluntary context switches */
80 long ru_nivcsw; /* involuntary context switches */
81 };
82 .EE
83 .in
84 .PP
85 Not all fields are completed;
86 unmaintained fields are set to zero by the kernel.
87 (The unmaintained fields are provided for compatibility with other systems,
88 and because they may one day be supported on Linux.)
89 The fields are interpreted as follows:
90 .TP
91 .I ru_utime
92 This is the total amount of time spent executing in user mode,
93 expressed in a
94 .I timeval
95 structure (seconds plus microseconds).
96 .TP
97 .I ru_stime
98 This is the total amount of time spent executing in kernel mode,
99 expressed in a
100 .I timeval
101 structure (seconds plus microseconds).
102 .TP
103 .IR ru_maxrss " (since Linux 2.6.32)"
104 This is the maximum resident set size used (in kilobytes).
105 For
106 .BR RUSAGE_CHILDREN ,
107 this is the resident set size of the largest child, not the maximum
108 resident set size of the process tree.
109 .TP
110 .IR ru_ixrss " (unmaintained)"
111 This field is currently unused on Linux.
112 .\" On some systems,
113 .\" this is the integral of the text segment memory consumption,
114 .\" expressed in kilobyte-seconds.
115 .TP
116 .IR ru_idrss " (unmaintained)"
117 This field is currently unused on Linux.
118 .\" On some systems, this is the integral of the data segment memory consumption,
119 .\" expressed in kilobyte-seconds.
120 .TP
121 .IR ru_isrss " (unmaintained)"
122 This field is currently unused on Linux.
123 .\" On some systems, this is the integral of the stack memory consumption,
124 .\" expressed in kilobyte-seconds.
125 .TP
126 .I ru_minflt
127 The number of page faults serviced without any I/O activity; here
128 I/O activity is avoided by \*(lqreclaiming\*(rq a page frame from
129 the list of pages awaiting reallocation.
130 .TP
131 .I ru_majflt
132 The number of page faults serviced that required I/O activity.
133 .TP
134 .IR ru_nswap " (unmaintained)"
135 This field is currently unused on Linux.
136 .\" On some systems, this is the number of swaps out of physical memory.
137 .TP
138 .IR ru_inblock " (since Linux 2.6.22)"
139 The number of times the filesystem had to perform input.
140 .TP
141 .IR ru_oublock " (since Linux 2.6.22)"
142 The number of times the filesystem had to perform output.
143 .TP
144 .IR ru_msgsnd " (unmaintained)"
145 This field is currently unused on Linux.
146 .\" On FreeBSD 6.2, this appears to measure messages sent over sockets
147 .\" On some systems,
148 .\" this field records the number of messages sent over sockets.
149 .TP
150 .IR ru_msgrcv " (unmaintained)"
151 This field is currently unused on Linux.
152 .\" On FreeBSD 6.2, this appears to measure messages received over sockets
153 .\" On some systems,
154 .\" this field records the number of messages received over sockets.
155 .TP
156 .IR ru_nsignals " (unmaintained)"
157 This field is currently unused on Linux.
158 .\" On some systems, this field records the number of signals received.
159 .TP
160 .IR ru_nvcsw " (since Linux 2.6)"
161 The number of times a context switch resulted due to a process
162 voluntarily giving up the processor before its time slice was
163 completed (usually to await availability of a resource).
164 .TP
165 .IR ru_nivcsw " (since Linux 2.6)"
166 The number of times a context switch resulted due to a higher
167 priority process becoming runnable or because the current process
168 exceeded its time slice.
169 .SH RETURN VALUE
170 On success, zero is returned.
171 On error, \-1 is returned, and
172 .I errno
173 is set to indicate the error.
174 .SH ERRORS
175 .TP
176 .B EFAULT
177 .I usage
178 points outside the accessible address space.
179 .TP
180 .B EINVAL
181 .I who
182 is invalid.
183 .SH ATTRIBUTES
184 For an explanation of the terms used in this section, see
185 .BR attributes (7).
186 .ad l
187 .nh
188 .TS
189 allbox;
190 lbx lb lb
191 l l l.
192 Interface Attribute Value
193 T{
194 .BR getrusage ()
195 T} Thread safety MT-Safe
196 .TE
197 .hy
198 .ad
199 .sp 1
200 .SH CONFORMING TO
201 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
202 POSIX.1 specifies
203 .BR getrusage (),
204 but specifies only the fields
205 .I ru_utime
206 and
207 .IR ru_stime .
208 .PP
209 .B RUSAGE_THREAD
210 is Linux-specific.
211 .SH NOTES
212 Resource usage metrics are preserved across an
213 .BR execve (2).
214 .PP
215 In Linux kernel versions before 2.6.9, if the disposition of
216 .B SIGCHLD
217 is set to
218 .B SIG_IGN
219 then the resource usages of child processes
220 are automatically included in the value returned by
221 .BR RUSAGE_CHILDREN ,
222 although POSIX.1-2001 explicitly prohibits this.
223 This nonconformance is rectified in Linux 2.6.9 and later.
224 .\" See the description of getrusage() in XSH.
225 .\" A similar statement was also in SUSv2.
226 .PP
227 The structure definition shown at the start of this page
228 was taken from 4.3BSD Reno.
229 .PP
230 Ancient systems provided a
231 .BR vtimes ()
232 function with a similar purpose to
233 .BR getrusage ().
234 For backward compatibility, glibc (up until version 2.32) also provides
235 .BR vtimes ().
236 All new applications should be written using
237 .BR getrusage ().
238 (Since version 2.33, glibc no longer provides an
239 .BR vtimes ()
240 implementation.)
241 .PP
242 See also the description of
243 .IR /proc/ pid /stat
244 in
245 .BR proc (5).
246 .SH SEE ALSO
247 .BR clock_gettime (2),
248 .BR getrlimit (2),
249 .BR times (2),
250 .BR wait (2),
251 .BR wait4 (2),
252 .BR clock (3)