From owner-freebsd-stable@FreeBSD.ORG Thu Mar 2 22:25:10 2006 Return-Path: X-Original-To: stable@freebsd.org Delivered-To: freebsd-stable@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1327116A420 for ; Thu, 2 Mar 2006 22:25:10 +0000 (GMT) (envelope-from nik@freebsd.org) Received: from jc.ngo.org.uk (jc.ngo.org.uk [69.55.225.33]) by mx1.FreeBSD.org (Postfix) with ESMTP id 58F5343D53 for ; Thu, 2 Mar 2006 22:25:08 +0000 (GMT) (envelope-from nik@freebsd.org) Received: from [192.168.0.240] (i-83-67-27-141.freedom2surf.net [83.67.27.141]) by jc.ngo.org.uk (8.13.4/8.13.5) with ESMTP id k22MOa2O077170 for ; Thu, 2 Mar 2006 14:25:07 -0800 (PST) (envelope-from nik@freebsd.org) Message-ID: <44077091.3060604@freebsd.org> Date: Thu, 02 Mar 2006 22:24:17 +0000 From: Nik Clayton User-Agent: Thunderbird 1.5 (X11/20060113) MIME-Version: 1.0 To: stable@freebsd.org Content-Type: multipart/mixed; boundary="------------030502040701030607060607" X-Spam-Score: (0) X-Scanned-By: MIMEDefang 2.54 on 69.55.225.33 Cc: Subject: Failing to understand getrusage() X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Mar 2006 22:25:10 -0000 This is a multi-part message in MIME format. --------------030502040701030607060607 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit I'm failing to understand how getrusage() works, which is a bit perplexing, because it doesn't seem like it would be terribly complicated. I've attached the code. My aim is to verify that I can use getrusage() to do (admittedly crude) instrumentation of which functions in my program are allocating lots of memory[1]. So I figure I can call getrusage() at various points and look at the ru_maxrss member. I'm confused because the results I'm getting don't make a great deal of sense. The attached program, when run 10 times one after the other, produces the following output; before: 0, after: 1300 before: 0, after: 0 before: 0, after: 0 before: 188, after: 188 before: 0, after: 0 before: 452, after: 452 before: 0, after: 0 before: 0, after: 1316 before: 0, after: 0 before: 0, after: 0 'before' is ru_maxrss before 'malloc(1024 * 1024)', 'after' is ru_maxrss afterwards. Different runs produce slightly different numbers -- the point is that they vary widely, and in some cases are '0' for both cases. Those results don't look sane to me. From doing some googling it looks like there may be a delay between the process's resident set size increasing and the stats that getrusage() uses being updated. If that's the case, can I (portably?) wait for the getrusage() stats to synchronise before I read them? If it's not the case, what am I doing wrong? Is there another (portable) mechanism I use to find out how big my process is? N [1] I'm actually instrumenting Perl applications, using Perl's built-in debugger, instrumenting at the Perl level, so solutions of the form "Use profiling application" aren't going to be applicable. I'm using BSD::Resource, which wraps getrusage(), and odd results from that led me to this C test. --------------030502040701030607060607 Content-Type: text/plain; name="1mb.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="1mb.c" /* Call getrusage() before and after allocating 1MB of RAM, and see what the maxrss figure is */ #include #include #include #include int main(void) { struct rusage ru; char *ptr; int rc; rc = getrusage(RUSAGE_SELF, &ru); if(rc == -1) { perror("getrusage(): "); return 1; } printf("before: %ld, ", ru.ru_maxrss); ptr = malloc(1024 * 1024); if(ptr == NULL) { perror("malloc(): "); return 1; } memset(ptr, 0, 1024 * 1024); /* Zero-fill the memory */ rc = getrusage(RUSAGE_SELF, &ru); if(rc == -1) { perror("getrusage(): "); return 1; } printf("after: %ld\n", ru.ru_maxrss); free(ptr); return 0; } --------------030502040701030607060607--