getrusage(2)
โจ getrusage(2) โจ
okay bestie, we're in section 2 of the Linux manual โ the System Calls Manual โ and today's main character is getrusage, the little syscall that tells you how many resources your process has been munching on. let's get into it ๐
๐ธ NAME ๐ธ
getrusage โ get resource usage
๐ LIBRARY ๐
Standard C library (libc, -lc)
โจ SYNOPSIS โจ
#include <sys/resource.h>
int getrusage(int who, struct rusage *usage);
๐ DESCRIPTION ๐
so getrusage() hands you back resource usage measures for who, and who can be one of these bad boys:
RUSAGE_SELFโ returns the resource usage stats for the calling process, which is the sum of resources used by ALL threads in the process. yes girl, all of them, no thread left behind.RUSAGE_CHILDRENโ gives you resource usage stats for all children of the calling process that have terminated and been waited for. and listen, these stats include the resources used by grandchildren and even further-removed descendants, but only if all of the intervening descendants waited on their terminated children. it's giving family tree of accountability ngl.RUSAGE_THREAD(since Linux 2.6.26) โ returns resource usage stats for the calling thread only, just that one thread, the individual. heads up: you must define the_GNU_SOURCEfeature test macro (before including any header file) to get the definition of this constant out of<sys/resource.h>. no macro, no constant, sorry babe ๐
The resource usages get returned in the structure that usage points to, and it looks like this:
struct rusage {
struct timeval ru_utime; /* user CPU time used */
struct timeval ru_stime; /* system CPU time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims (soft page faults) */
long ru_majflt; /* page faults (hard page faults) */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* IPC messages sent */
long ru_msgrcv; /* IPC messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
okay important vibe check: not all fields are completed โ the kernel sets the unmaintained fields to zero. (those unmaintained fields are there for compatibility with other systems, and also because maybe one day Linux will support them. manifesting โจ.) here's how you read each field:
ru_utimeโ total time spent executing in user mode, expressed as atimevalstructure (seconds plus microseconds).ru_stimeโ total time spent executing in kernel mode, also atimevalstructure (seconds plus microseconds).ru_maxrss(since Linux 2.6.32) โ the maximum resident set size used, in KiB. and forRUSAGE_CHILDRENthis is the resident set size of the largest child, NOT the maximum RSS of the whole process tree. read that twice bestie, people get it wrong constantly ๐ญru_ixrss(unmaintained) โ currently unused on Linux.ru_idrss(unmaintained) โ currently unused on Linux.ru_isrss(unmaintained) โ currently unused on Linux.ru_minfltโ the number of page faults serviced without any I/O activity; I/O activity gets avoided here by "reclaiming" a page frame from the list of pages awaiting reallocation.ru_majfltโ the number of page faults serviced that required I/O activity.ru_nswap(unmaintained) โ currently unused on Linux.ru_inblock(since Linux 2.6.22) โ the number of times the filesystem had to perform input.ru_oublock(since Linux 2.6.22) โ the number of times the filesystem had to perform output.ru_msgsnd(unmaintained) โ currently unused on Linux.ru_msgrcv(unmaintained) โ currently unused on Linux.ru_nsignals(unmaintained) โ currently unused on Linux.ru_nvcsw(since Linux 2.6) โ the number of times a context switch happened because a process voluntarily gave up the processor before its time slice was done (usually so it could await availability of a resource, so polite of it).ru_nivcsw(since Linux 2.6) โ the number of times a context switch happened because a higher priority process became runnable, or because the current process exceeded its time slice. rude but valid.
๐ซถ RETURN VALUE ๐ซถ
On success, zero is returned. on error, -1 is returned, and errno is set to indicate the error. classic.
๐ ERRORS ๐
EFAULTโ usage points outside the accessible address space.EINVALโ who is invalid.
๐ ATTRIBUTES ๐
for an explanation of the terms used in this section, go peek at attributes(7).
- Interface:
getrusage() - Attribute: Thread safety
- Value:
MT-Safe
so yeah, it's MT-Safe, we love a thread-safe icon ๐
โญ STANDARDS โญ
POSIX.1-2024.
POSIX.1 does specify getrusage(), but it specifies only the fields ru_utime and ru_stime. that's it. that's the list.
RUSAGE_THREAD is Linux-specific.
๐ HISTORY ๐
4.3BSD, SVr4, SUSv1, POSIX.1-2001 XSI.
Before Linux 2.6.9: if the disposition of SIGCHLD was set to SIG_IGN, then the resource usages of child processes were automatically included in the value returned by RUSAGE_CHILDREN โ even though POSIX.1-2001 explicitly prohibits this. this nonconformance got rectified in Linux 2.6.9 and later. glow up ๐
The structure definition shown at the start of this page was taken from 4.3BSD Reno.
Ancient systems provided a vtimes() function with a similar purpose to getrusage(). for backward compatibility, glibc (up until Linux 2.32) also provided vtimes(). all new applications should be written using getrusage(). (since Linux 2.33, glibc no longer provides a vtimes() implementation โ she's gone, periodt.)
๐ NOTES ๐
Resource usage metrics are preserved across an execve(2). slay.
๐ธ SEE ALSO ๐ธ
clock_gettime(2), getrlimit(2), times(2), wait(2), wait4(2), clock(3), proc_pid_stat(5), proc_pid_io(5)
ok that's the whole page bestie, go measure your resources responsibly ๐ฆ
and that's a wrap from the Linux man-pages, version 6.20, dated 2026-02-08 ๐ mwah, see you in the next man page ๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- Copyright 2002, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms