GNU 时间内存输出也考虑子进程吗?

lec*_*eco 5 linux time gnu

当运行 GNU time ( /usr/bin/time) 并检查内存消耗时,其输出是否考虑了目标程序的子进程的内存使用情况?

在 GNU 的时间联机帮助页中找不到任何内容。

小智 10

是的。

您可以轻松地检查:

$ /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
Run Code Online (Sandbox Code Playgroud)

wait4GNU time在 Linux 上使用系统调用(通过wait3glibc 包装器),虽然没有记录,但它返回的资源使用情况还struct rusage包括等待的进程的后代。您可以查看wait4in的内核实现kernel/exit.c以了解所有详细信息:

$ 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 */
Run Code Online (Sandbox Code Playgroud)

FreeBSD 和 NetBSD 还有一个wait6系统调用,它返回等待的进程及其后代的单独信息。他们还清楚地记录了,这些食物是由孙辈返回的wait3wait4也包括孙辈。