Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

getrusage(2)

FieldValue
Numbers (x64 / i386 / arm64-generic / arm32)98 / 77 / 165 / 77
Since (kernel)1.0
Man pageman2/getrusage.2 (cached roff, dated 2021-03-22; split out of getrlimit.2 in 2004 per the page header)
glibc wrappergetrusage@@GLIBC_2.2.5 (nm -D, glibc 2.41); musl: src/misc/getrusage.c, time64 redirect __getrusage_time64 (include/sys/resource.h line 109)
Aliases coverednone
Ledger IDSC-getrusage

Purpose

getrusage() returns resource usage measures – CPU time, page-fault counts, context-switch counts, I/O counts – for the calling process (RUSAGE_SELF), for all terminated-and-waited children (RUSAGE_CHILDREN), or for the calling thread (RUSAGE_THREAD, since 2.6.26, gated on _GNU_SOURCE) (getrusage.2, DESCRIPTION).

Synopsis

#include <sys/resource.h>
int getrusage(int who, struct rusage *usage);

Arguments and behavior

usage points to a struct rusage of 16 long/timeval fields. Field-by-field, verified against getrusage.2:

FieldMeaningState
ru_utimeUser CPU time (timeval)maintained
ru_stimeSystem CPU timemaintained
ru_maxrssMax resident set size in kilobytes; for RUSAGE_CHILDREN the largest child’s RSSsince 2.6.32
ru_ixrss, ru_idrss, ru_isrssIntegral shared/data/stack memoryunmaintained, always 0 on Linux
ru_minfltPage faults serviced without I/O (reclaims)maintained
ru_majfltPage faults that required I/Omaintained
ru_nswapSwapsunmaintained
ru_inblock / ru_oublockBlock I/O operationssince 2.6.22
ru_msgsnd / ru_msgrcv / ru_nsignalsIPC messages, signalsunmaintained
ru_nvcsw / ru_nivcswVoluntary / involuntary context switchessince 2.6

RUSAGE_CHILDREN aggregates children that have terminated and been waited for; grandchildren count too when every intermediate descendant waited (getrusage.2, DESCRIPTION). Unmaintained fields are zeroed by the kernel for compatibility with other systems (getrusage.2, DESCRIPTION).

Return value and errors

0 on success, -1 with errno: EFAULT (usage outside accessible address space) or EINVAL (invalid who) (getrusage.2, ERRORS).

Security and notes

Historically getrusage(2) underreported when SIGCHLD was being ignored; that nonconformance is fixed since 2.6.9 (getrusage.2 page header note). RUSAGE_THREAD needs _GNU_SOURCE before including any header (getrusage.2, DESCRIPTION). The child-accounting contract is what makes process monitors (time(1), shells’ % job stats) work without reading /proc per child; wait4(2) and wait4.md expose the same struct on wait. The data source is the task’s accounting fields, the same ones BSD accounting writes to the acct(5) file format (cross-ref: acct.md). For live totals of an arbitrary other process, /proc/<pid>/stat is the interface, not getrusage.

References

  • Cached man page: source-cache/man-pages-sparse/man2/getrusage.2
  • Online: https://man7.org/linux/man-pages/man2/getrusage.2.html
  • Header: /usr/include/x86_64-linux-gnu/sys/resource.h (struct rusage, RUSAGE_*)
  • Kernel tables: source-cache/kernel/syscall_64.tbl (98), syscall_32.tbl (77), syscall_generic.tbl (165), syscall_arm.tbl (77)
  • musl: source-cache/musl-clone/musl/src/misc/getrusage.c, include/sys/resource.h
  • Ledger row SC-getrusage