What are "before" and "after" used in getrusage? Why and how they are used? I also find manuel's explanation about RUSAGE_SELF hard to understand. Thanks in advance.
1 Answer
As denoted by the man page for getrusage
, it takes 2 arguments, an int
and a struct rusage
. before
and after
are of type struct rusage
whose definition is
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 */
};
getrusage
basically takes a second argument of this type and sets some of its fields. Each field stores the information corresponding to the comment in front of it. For example, the ru_utime
is the total amount of time spent executing in user mode — the mode in which case the executing code has no direct access to the hardward.
To understand what RUSAGE_SELF
is, you need to understand what a process is and what a thread is. As the Wikipedia article suggests
a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.
Back to the first parameter, the value RUSAGE_SELF
basically tells getrusage
to fill the second argument with information about the resources used by the current process (i.e., the process that represents an instance of your program).
-
Thanks for providing such a detailed explanation. There is some other questions I don't understand. before and after is not specified in the definition of rusage, and I noticed that they are pointers in the spellers program. What do they point to? Feb 17, 2015 at 20:55
-
@eifphysics as the beginning of the answer denotes,
before
andafter
are not pointers. They are two variables of typestruct rusage
. A pointer to each one of them is passed togetrusage()
when called because it accepts an int and a pointer of typestruct rusage
.– kzidane ♦Feb 17, 2015 at 21:33 -
@Kareem: although I have not asked this question, I would like to thank you for this helpful explanation!– VitaleFeb 16, 2016 at 10:59
-
-