How time command works - hewigovens/hewigovens.github.com GitHub Wiki

bash-4.2 analysis,time is a shell reserved word.

//entry:execute_cmd.c line 1185

if have getrusage/gettimeofday

if have time

struct tms

//Record current time(before)

//execute time command's arguments
//e.g. time find . -name "*~" -exec rm {} \;
//internal function calls: 
execute_command_internal->make_child->execute_in_subshell

//Record current time(after)

//Calculate

//for more, linux system call

glibc->getrusage()->sys_getrusage

getrusage() can get CPU time of current process and child processes.

in glibc, getrusage is a stub function.

VSDO, linux-gate-xx.so/linux-vsdo-xx.so

RUSAGE_SELF
RUSAGE_CHILDREN

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 */
           };