/ usr / bin / time的平均居民集大小始终为0

Zil*_*ate 2 linux

我正在尝试使用/ usr / bin / time来收集程序的平均驻留集大小。我用几个程序尝试了此命令,但平均居民集大小始终为零。例如:

/ usr / bin /时间结果

我的操作系统是ubuntu 10.04。

您能告诉我原因是什么或如何解决吗?非常感谢你!

Ser*_*kov 6

看,/usr/bin/time本身不会计算此数据。它用于wait4从Linux获取此数据。这是对wait4的描述:

>man wait4

       pid_t wait4(pid_t pid, int *status, int options,
                   struct rusage *rusage);
Run Code Online (Sandbox Code Playgroud)

问题在于,并非所有字段struct ruusage都得到维护:

   Not all fields are completed; unmaintained fields are set to zero by
   the kernel.  (The unmaintained fields are provided for compatibility
   with other systems, and because they may one day be supported on
   Linux.)  The fields are interpreted as follows:
Run Code Online (Sandbox Code Playgroud)

尽管保留了maxrss,但是关于rss的其他字段却没有。因此它只返回return 0,/ usr / bin / time也显示0。

此外,如果您查看GNU时间的源文件,您将看到以下代码:

/*
FMT is the format string, interpreted as described above.
*/
static void
summarize (fp, fmt, command, resp)
     FILE *fp;
     const char *fmt;
     const char **command;
     RESUSE *resp;
{

  while (*fmt)
    {
      switch (*fmt)
    {

        case 'M':       /* Maximum resident set size.  */
          fprintf (fp, "%lu", ptok ((UL) resp->ru.ru_maxrss));
          break;

        case 't':       /* Average resident set size.  */
          fprintf (fp, "%lu",
               MSEC_TO_TICKS (v) == 0 ? 0 :
               ptok ((UL) resp->ru.ru_idrss) / MSEC_TO_TICKS (v));
Run Code Online (Sandbox Code Playgroud)

由于Linux将ru_idrss返回为0,因此平均常驻集大小为0。

有用的链接: