linuxtimegnu

Does GNU time memory output account for child processes too?


When running GNU time (/usr/bin/time) and checking for memory consumption, does its output account for the memory usage of the child processes of my target program?

Could not find anything in GNU's time manpage.


Solution

  • Yes.

    You can easily check with:

    $ /usr/bin/time -f '%M' sh -c 'perl -e "\$y=q{x}x(2*1024*1024)" & wait'
    8132
    $ /usr/bin/time -f '%M' sh -c 'perl -e "\$y=q{x}x(8*1024*1024)" & wait'
    20648
    

    GNU time is using the wait4 system call on Linux (via the wait3 glibc wrapper), and while undocumented, the resource usage it returns in the struct rusage also includes the descendands of the process waited for. You can look at the kernel implementation of wait4 in kernel/exit.c for all the details:

    $ grep -C2 RUSAGE_BOTH include/uapi/linux/resource.h
    #define RUSAGE_SELF     0
    #define RUSAGE_CHILDREN (-1)
    #define RUSAGE_BOTH     (-2)            /* sys_wait4() uses this */
    #define RUSAGE_THREAD   1               /* only the calling thread */
    

    FreeBSD and NetBSD also have a wait6 system call which returns separate info for the process waited for and for its descendants. They also clearly document that the rusage returned by wait3 and wait4 also includes grandchildren.