linuxtimegetrusage

What is the difference between these methods to obtain resource usage?


In Linux, we can use two ways to find out resources used like time, page faults, page swaps, context switching. One of the ways is using the getrusage() function, the other method is using the command /usr/bin/time -v [command to check usage]. What is the difference between these ways of finding resource usage?


Solution

  • When you use a command like time(1) it must use a system call such as getrusage(2) by way of its system library wrapper. This is building a request with the right system call number and structure to indicate it wants rusage information for the processes' children.

    For compatibility across UNIX/POSIX operating systems, which specific functions are chosen to build a command is done from a hierarchy of options to adequately cover the OSes the command runs on. (Some OSes may not implement everything or have various quirks.)

    In time's case it will prefer to group waiting for the child and getting its usage into calling wait3 which in turn is implemented as a wrapper around the even more complex wait4, which has its own systemcall number.

    Both wait3/4 and getrusage fill the same rusage structure with information, and since time only directly calls one child process, calling wait3() as it does or breaking this into less featured calls like wait();getrusage(RUSAGE_CHILDREN) is in essence the same. Therefore, time is effectively displaying the same data as getrusage provides (together with some more general data it assembles from the system like real time elapsed using calls to gettimeofday).

    The real difference among the systemcall wrapper functions is:

    To verify they are the same, one could change time to an alternate version, recompile and compare:

      while ((caught = wait3 (&status, 0, NULL)) != pid)
        {
          if (caught == -1) {
             getrusage(RUSAGE_CHILDREN, &resp->ru);
             return 0;
          }
        }