Issue
I have a C program which I would like to report:
- its elapsed wall clock time (how long it has run for)
- userspace cpu time used (possibly more than wall clock time if multi-threaded and busy)
- kernel/iowait/etc time used
I can do this from outside, using the "time" command, but I need each child process to report its own usage. "time" aggregates the parent and child processes, but I need a line per child process.
Solution
It looks like POSIX times()
might be the answer. I'll confirm if this is the correct answer, after testing tomorrow.
After further reading, getrusage()
looked best:
struct rusage rusage = {0};
int ret = getrusage(RUSAGE_SELF, &rusage);
if (ret) {
perror("worker_exit()");
exit(errno);
}
double ru_utime = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec / 1000000.0;
double ru_stime = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec / 1000000.0;
Answered By - fadedbee Answer Checked By - Marie Seifert (WPSolving Admin)