- Using strace
- access: Testing File Permissions
- fcntl: Locks and Other File Operations
- fsync and fdatasync: Flushing Disk Buffers
- getrlimit and setrlimit: Resource Limits
- getrusage: Process Statistics
- gettimeofday: Wall-Clock Time
- The mlock Family: Locking Physical Memory
- mprotect: Setting Memory Permissions
- nanosleep: High-Precision Sleeping
- readlink: Reading Symbolic Links
- sendfile: Fast Data Transfers
- setitimer: Setting Interval Timers
- sysinfo: Obtaining System Statistics
- uname
8.6 getrusage: Process Statistics
The getrusage system call retrieves process statistics from the kernel. It can be used to obtain statistics either for the current process by passing RUSAGE_SELF as the first argument, or for all terminated child processes that were forked by this process and its children by passing RUSAGE_CHILDREN. The second argument to rusage is a pointer to a struct rusage variable, which is filled with the statistics.
A few of the more interesting fields in struct rusage are listed here:
ru_utime—A struct timeval field containing the amount of user time, in seconds, that the process has used. User time is CPU time spent executing the user program, rather than in kernel system calls.
ru_stime—A struct timeval field containing the amount of system time, in seconds, that the process has used. System time is the CPU time spent executing system calls on behalf of the process.
ru_maxrss—The largest amount of physical memory occupied by the process's data at one time over the course of its execution.
The getrusage man page lists all the available fields. See Section 8.7, "gettimeofday: Wall-Clock Time," for information about struct timeval.
The function in Listing 8.5 prints out the current process's user and system time.
Listing 8.5 (print-cpu-times.c) Display Process User and System Times
#include <stdio.h> #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> void print_cpu_time() { struct rusage usage; getrusage (RUSAGE_SELF, &usage); printf ("CPU time: %ld.%06ld sec user, %ld.%06ld sec system\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec, usage.ru_stime.tv_sec, usage.ru_stime.tv_usec); }