GETRUSAGE(2)

πŸ’– getrusage(2) β€” the resource utilization tea πŸ’–

okay bestie, so this is a System Calls Manual page, section 2, and it's literally all about getrusage β€” the little syscall that tells you what your processes have been consuming. peak main character energy fr fr.


✨ NAME ✨

getrusage β€” get information about resource utilization


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)


πŸŽ€ SYNOPSIS πŸŽ€

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#define    RUSAGE_SELF      0
#define    RUSAGE_CHILDREN -1
#define    RUSAGE_THREAD   1

int
getrusage(int who, struct rusage *rusage);

πŸ¦‹ DESCRIPTION πŸ¦‹

So the getrusage() system call hands you back the deets on the resources that have been used up by the current thread, the current process, or all of its terminated child processes. slay.

The who argument is one of RUSAGE_THREAD, RUSAGE_SELF, or RUSAGE_CHILDREN β€” pick your fighter πŸ’…

And the buffer that rusage points at gets filled in with this exact structure, no cap:

struct rusage {
        struct timeval ru_utime; /* user time used */
        struct timeval ru_stime; /* system time used */
        long ru_maxrss;          /* max resident set size */
        long ru_ixrss;           /* integral shared text memory size */
        long ru_idrss;           /* integral unshared data size */
        long ru_isrss;           /* integral unshared stack size */
        long ru_minflt;          /* page reclaims */
        long ru_majflt;          /* page faults */
        long ru_nswap;           /* swaps */
        long ru_inblock;         /* block input operations */
        long ru_oublock;         /* block output operations */
        long ru_msgsnd;          /* messages sent */
        long ru_msgrcv;          /* messages received */
        long ru_nsignals;        /* signals received */
        long ru_nvcsw;           /* voluntary context switches */
        long ru_nivcsw;          /* involuntary context switches */
};

okay now let's read the fields, because each one is giving something different:

  • ru_utime β€” the total amount of time spent executing in user mode. that's the fun part, the actual doing stuff time ✨
  • ru_stime β€” the total amount of time spent in the system executing on behalf of the process(es). the kernel's doing your bidding, respectfully πŸ’œ
  • ru_maxrss β€” the maximum resident set size utilized, in kilobytes.
  • ru_ixrss β€” an β€œintegral” value showing how much memory was used by the text segment that was also shared among other processes. this one's expressed in units of kilobytes * ticks-of-execution. and what's a tick? ticks are statistics clock ticks. the statistics clock has a frequency of sysconf(_SC_CLK_TCK) ticks per second. iykyk 🧚
  • ru_idrss β€” an integral value of the amount of unshared memory chillin' in the data segment of a process, expressed in units of kilobytes * ticks-of-execution.
  • ru_isrss β€” an integral value of the amount of unshared memory chillin' in the stack segment of a process, expressed in units of kilobytes * ticks-of-execution.
  • ru_minflt β€” the number of page faults serviced without any I/O activity. here, I/O activity gets avoided by β€œreclaiming” a page frame from the list of pages awaiting reallocation. lowkey efficient 😌
  • ru_majflt β€” the number of page faults serviced that did require I/O activity. the dramatic ones πŸ’€
  • ru_nswap β€” the number of times a process was β€œswapped” out of main memory.
  • ru_inblock β€” the number of times the file system had to perform input.
  • ru_oublock β€” the number of times the file system had to perform output.
  • ru_msgsnd β€” the number of IPC messages sent.
  • ru_msgrcv β€” the number of IPC messages received.
  • ru_nsignals β€” the number of signals delivered.
  • ru_nvcsw β€” the number of times a context switch resulted due to a process voluntarily giving up the processor before its time slice was completed. usually it does this to await availability of a resource. so polite of it 🫢
  • ru_nivcsw β€” 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. not so polite, but we respect the hustle πŸ’…

πŸ“ NOTES πŸ“

Heads up bestie: the numbers ru_inblock and ru_oublock account only for real I/O. data supplied by the caching mechanism is charged only to the first process to read or write the data. so if it looks suspiciously small, that's why πŸ‘€


🌸 RETURN VALUES 🌸

The getrusage() function returns the value 0 if successful β€” icon behavior. otherwise the value -1 is returned and the global variable errno is set to indicate the error. sad girl hours 😭


🌈 ERRORS 🌈

The getrusage() system call will fail if:

  • [EINVAL] β€” The who argument is not a valid value. you gave it a vibe it doesn't recognize 🚫
  • [EFAULT] β€” The address specified by the rusage argument is not in a valid part of the process address space. it's pointing into the void, bestie πŸ’€

⭐ SEE ALSO ⭐

gettimeofday(2), wait(2), clocks(7)


πŸ‘‘ HISTORY πŸ‘‘

The getrusage() system call appeared in 4.2BSD. The RUSAGE_THREAD facility first appeared in FreeBSD 8.1. ancient lore, but make it fashion πŸŽ€


πŸ™ˆ BUGS πŸ™ˆ

There is no way to obtain information about a child process that has not yet terminated. so if it's still alive and kicking, you simply cannot peek. patience, bestie πŸ¦‹

and that's the whole page, periodt πŸ’… this was the Debian edition, dated May 1, 2010 β€” thanks for reading my ted talk, ily, go check your rusage πŸ«ΆπŸ’–βœ¨

πŸ“¦ where this page comes from