From owner-freebsd-questions@FreeBSD.ORG Sat May 29 23:57:14 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 5056016A4CE for ; Sat, 29 May 2004 23:57:14 -0700 (PDT) Received: from smtpout.mac.com (smtpout.mac.com [17.250.248.87]) by mx1.FreeBSD.org (Postfix) with ESMTP id C66F043D2F for ; Sat, 29 May 2004 23:57:13 -0700 (PDT) (envelope-from cswiger@mac.com) Received: from mac.com (smtpin01-en2 [10.13.10.146]) by smtpout.mac.com (Xserve/MantshX 2.0) with ESMTP id i4RIKf56028282; Thu, 27 May 2004 11:20:41 -0700 (PDT) Received: from [10.1.1.193] (nfw2.codefab.com [199.103.21.225] (may be forged)) (authenticated bits=0)i4RIKejW009706; Thu, 27 May 2004 11:20:40 -0700 (PDT) In-Reply-To: <20040527010804.M72742@server.gisp.dk> References: <20040527010804.M72742@server.gisp.dk> Mime-Version: 1.0 (Apple Message framework v618) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <8EF1411C-B00A-11D8-AA39-003065ABFD92@mac.com> Content-Transfer-Encoding: 7bit From: Charles Swiger Date: Thu, 27 May 2004 14:20:40 -0400 To: Michael Sig Birkmose X-Mailer: Apple Mail (2.618) cc: freebsd-questions@freebsd.org Subject: Re: Measuring the stack usage of a program X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 30 May 2004 06:57:14 -0000 On May 26, 2004, at 7:09 PM, Michael Sig Birkmose wrote: > Does anyone know, if it is possible to meassure the maximum stack > usage of > a C program throughout it's entire execution? Sure. See "man getrusage", specificly: long ru_isrss; /* integral unshared stack size */ ...which tends to give you the maximum usage on most systems (because they don't shrink the stack if it becomes smaller). You can also compare the addresses of automatic variables within the code of the program itself to see how the stack grows: /* Test program to measure stack usage... */ void * test_function(int count) { int foo = 1; #if 0 /* make the local frame much bigger */ char buf[1000]; sprintf(buf, "%d\n", count); #endif if (count > 0) { return test_function(count - 1); } else return &foo; } int main(int argc, char *argv[]) { int depth = 100; unsigned long delta; if (argc > 1) depth = atoi(argv[1]); delta = (unsigned long)&depth - (unsigned long)test_function(depth); printf("\nchange in stack size: %lu bytes.\n", delta); return 0; } [ Yeah, yeah, I should use ptrdiff_t, but you get the idea... ] -- -Chuck