This is a statically linkable version of the GNU Time program. Much of the code is ported from that project. You can find more informaiton about the GNU Time project at [[http://directory.fsf.org/wiki/Time]].
This library can be used in two ways. It can be used to track resources used by the entire process in much the same way as GNU Time. It can also be used to track resource usage of single threads.
To track the resources used by a process create a variable of type
struct resuse
and pass that to the resuse_start()
function with the scope
set to RESUSE_SCOPE_PROC
. At the point that you want to collect resources at
use the function resuse_end()
passing in the same resuse
object.
int main(int argc, char * argv[]){
struct resuse resuse;
resuse_start(&resuse, RESUSE_SCOPE_PROC);
// ...
resuse_end(&resuse);
// ...
}
To track the resources used by a single thread follow the same directions as
for a process, but change the scope to RESUSE_SCOPE_THREAD
. Note that to
collect thread resources _GNU_SOURCE
must be defined.
void * thread(void* param){
struct resuse resuse;
resuse_start(&resuse, RESUSE_SCOPE_THREAD);
// ...
resuse_end(&resuse);
// ...
}
After collecting the resource data libresuse
provides resuse_fprint()
which
will format and print the results. resuse_fprint()
takes as a parameter a
format string which works similarly to printf()
. It uses %
followed by
a unique character that will be expanded into the correct resource information.
This is a list of commands that resuse_fprintf()
recognizes.
%
== A literal `%'D
== Average unshared data size in KBE
== Elapsed real (wall clock) time in [hour:]min:secF
== major page faults (required physical I/O)I
== File system inputsK
== Average total mem usageM
== Maximum resident set size in KBO
== File system outputsP
== Percent of CPU this job gotR
== Minor page faultsS
== System (kernel) timeU
== User timeW
== Times swapped outX
== Average amount of shared text in KBZ
== Page sizec
== Involuntary context switchese
== Elapsed real time in secondsk
== Signals deliveredp
== Average unshared stack size in KBr
== Socket messages receiveds
== Socket messages sentt
== Average resident set size in KBw
== Voluntary context switches
Printing the real, user, and system time a process existed:
resuse_fprint(stdout, "Real Time: %E, User Time: %U, System Time: %S, CPU Usage: %P\n", &resuse);