D.W. D.W. - 16 days ago 5
C Question

Units of `ru_maxrss` on BSD?

getrusage
returns a struct containing

long ru_maxrss; /* max resident set size */


What are the units of
ru_maxrss
, on BSD operating systems? Is this value in bytes, or in kilobytes? By "BSD", I mean FreeBSD, OpenBSD, but not Mac OS X / Darwin.

The FreeBSD man page and OpenBSD man page say the units are in kilobytes:

ru_maxrss the maximum resident set size utilized (in kilobytes).


However, I know that on Darwin / Mac OS X, the units are in bytes (even though online man pages claim otherwise), and I have also found some statements online that claim on BSD
ru_maxrss
is also returned in bytes (see e.g. here). Which is correct?

Answer

On FreeBSD it's in kilobytes, as evidenced by the source in kern_clock.c:

/* Update resource usage integrals and maximums. */
MPASS(p->p_vmspace != NULL);
vm = p->p_vmspace;
ru = &td->td_ru;
ru->ru_ixrss += pgtok(vm->vm_tsize) * cnt;
ru->ru_idrss += pgtok(vm->vm_dsize) * cnt;
ru->ru_isrss += pgtok(vm->vm_ssize) * cnt;
rss = pgtok(vmspace_resident_count(vm));
if (ru->ru_maxrss < rss)
    ru->ru_maxrss = rss;

pgtok() computes kb from page size.

But who would believe the kernel source? Lets run a program:

$ cat x.c
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>

int main(void)
{
    struct rusage r;

    if (getrusage( RUSAGE_SELF, &r) == 0) {
       printf ("ru_maxrss=%lu\n", (unsigned long)r.ru_maxrss);
    }
    return 0;
}
$ ./a.out
ru_maxrss=5824
$ ./a.out
ru_maxrss=0
$ ./a.out
ru_maxrss=216
$ ./a.out
ru_maxrss=1880

These numbers aren't multiples of 1024, so can't be bytes.