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_SOURCE feature 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 a timeval structure (seconds plus microseconds).
  • ru_stime โ€” total time spent executing in kernel mode, also a timeval structure (seconds plus microseconds).
  • ru_maxrss (since Linux 2.6.32) โ€” the maximum resident set size used, in KiB. and for RUSAGE_CHILDREN this 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms