Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Jun 2006 05:30:27 +0300
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        Mikhail Teterin <mi+mx@aldan.algebra.com>
Cc:        current@freebsd.org
Subject:   Re: getrusage() -- negative ru_maxrss?!
Message-ID:  <20060608023027.GA1891@gothmog.pc>
In-Reply-To: <200606071827.24207.mi%2Bmx@aldan.algebra.com>
References:  <200606071827.24207.mi%2Bmx@aldan.algebra.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2006-06-07 18:27, Mikhail Teterin <mi+mx@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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060608023027.GA1891>