From owner-freebsd-current@FreeBSD.ORG Thu Jun 8 05:06:39 2006 Return-Path: X-Original-To: current@freebsd.org Delivered-To: freebsd-current@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0382216BABF for ; Thu, 8 Jun 2006 02:28:50 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3EE2D43D48 for ; Thu, 8 Jun 2006 02:28:47 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from gothmog.pc (host5.bedc.ondsl.gr [62.103.39.229]) (authenticated bits=128) by igloo.linux.gr (8.13.6/8.13.6/Debian-1) with ESMTP id k582Ssm6010929 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 8 Jun 2006 05:28:56 +0300 Received: from gothmog.pc (gothmog [127.0.0.1]) by gothmog.pc (8.13.6/8.13.6) with ESMTP id k582UR0C002006; Thu, 8 Jun 2006 05:30:27 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from giorgos@localhost) by gothmog.pc (8.13.6/8.13.6/Submit) id k582URVG002005; Thu, 8 Jun 2006 05:30:27 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Thu, 8 Jun 2006 05:30:27 +0300 From: Giorgos Keramidas To: Mikhail Teterin Message-ID: <20060608023027.GA1891@gothmog.pc> References: <200606071827.24207.mi+mx@aldan.algebra.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200606071827.24207.mi+mx@aldan.algebra.com> X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (score=-0.876, required 5, autolearn=not spam, ALL_TRUSTED -1.44, AWL 0.24, PLING_QUERY 0.33) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: current@freebsd.org Subject: Re: getrusage() -- negative ru_maxrss?! X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 Jun 2006 05:06:40 -0000 On 2006-06-07 18:27, Mikhail Teterin 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