CPU time after the process finished
Is there a function in Linux which allows me to see how much CPU did a process use after it finished? I need something simila开发者_StackOverflow中文版r to bash "time" command. I am fork()ing the process and then waiting using wait() for a child to finish. The way of accurately measuring "real" time (actual time elapsed between fork() and exit()), even when wait() was called a long after the child process became zombie is also welcome, but I'm not sure if its possible.
Sure, wait3
and wait4
have you covered. Alternatively (and more portably) you could use getrusage(2)
.
The wait3() and wait4() system calls are similar to waitpid(2), but additionally return resource usage information about the child in the structure pointed to by
rusage
.
Example: wait3
struct rusage usage;
wait3(&status, 0, &usage);
Example: getrusage
Of course, wait3
and wait4
are just a convenience. So you could use getrusage
:
getrusage(RUSAGE_CHILDREN, &usage);
The disadvantage is that this tells you the resources used by ALL the terminated children.
So, once you get it, what do you do with rusage
? struct rusage
has the following form:
struct rusage {
struct timeval ru_utime; /* user CPU time used */
struct timeval ru_stime; /* system CPU time used */
/* More. */
};
The bash feature "times" reports the total user/system times used by the shell and its children. This feature, unfortunately, doesn't report total memory, i/o etc. IE: it doesn't employ getrusage (which it should).
The /usr/bin/time program will give you executon time, memory footprint. So you can do /usr/bin/time bash myprog.sh ... and get the accumulated times for all children.
精彩评论