getrusage() -- negative ru_maxrss?!
Giorgos Keramidas
keramida at ceid.upatras.gr
Thu Jun 8 05:06:40 UTC 2006
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
More information about the freebsd-current
mailing list