Re: getrusage() -- negative ru_maxrss?!

From: Giorgos Keramidas <keramida_at_ceid.upatras.gr>
Date: Thu, 8 Jun 2006 05:30:27 +0300
On 2006-06-07 18:27, Mikhail Teterin <mi+mx_at_aldan.algebra.com> wrote:
> Hello!
> I have a program, which uses getrusage() to report its own
> performance before exiting.
>
> I noticed recently, that the ru_maxrss field is sometimes
> reported negative -- I don't think, I ever saw this last year,
> for example... Is the field considered usable and
> (semi-)accurate, or did FreeBSD abandon it (as Solaris did)?

ru_maxrss _is_ updated by the kernel, AFAICT.  What you see is
probably an overflow because of a large size and the
multiplication below.

> I currently print it as:
>
> 	fprintf(..., "... used %ld Kb ...",
>	    ... ru.ru_maxrss*getpagesize()/1024... );
>
> What's the right way to do it?

You probably want to split this in more parts, and check that no
overflow can occur:

	int	pagesize;
	long	kb, rss;

	pagesize = getpagesize();
	if (LONG_MAX / pagesize < ru.ru_maxrss)
		rss = ru.ru_maxrss * (pagesize / 1024);
	else
		rss = (ru.ru_maxrss * pagesize) / 1024;
	fprintf(..., "... used %ld Kb ...", ... rss ...);

It may even be more sensible to just use the first way of
calculating `rss' all the time:

	fprintf(..., "... used %ld Kb ...",
	    ... ru.ru_maxrss * (getpagesize() / 1024) ...);

You can check if this is an overflow by printing LONG_MAX and the value
of ru.ru_maxrss and going over the numbers yourself to make sure that
what you see is not an overflow.

- Giorgos
Received on Thu Jun 08 2006 - 03:06:39 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:56 UTC