LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-22-2007, 09:37 AM   #1
exit3219
Member
 
Registered: May 2005
Location: Moldova
Distribution: Kubuntu
Posts: 199

Rep: Reputation: 30
Question How do I measure stack usage of a C/C++ program?


Hi.
I know I can profile / measure heap usage of programs with valgrind (massif). But how do I measure the maximum stack usage of a program (during a full run)? Google and the gcc manpage were of no help. I also looked in /proc/`pidof myprocess`/status but I can't understand much there.
I just want to know if what I'm asking for is possible. Technically, I only need to know if my program exceeds a certain limit (1MB). Is there a gcc switch to limit the stack size? What is the (default) stack limit on Linux systems anyway?
Any help would be greatly appreciated.
 
Old 01-22-2007, 09:51 AM   #2
nnivanov
LQ Newbie
 
Registered: Nov 2006
Location: Russia, Velikiy Novgorod
Distribution: Slackware 12
Posts: 18

Rep: Reputation: 0
AFAIK, struct rusage which returns by wait3()/wait4 syscalls has one interesting field:

Code:
struct rusage
{
long ru_isrss;
}
Probably you should use wait4() for current pid with WNOHANG flag.

But I don't sure...
 
Old 01-22-2007, 10:18 AM   #3
exit3219
Member
 
Registered: May 2005
Location: Moldova
Distribution: Kubuntu
Posts: 199

Original Poster
Rep: Reputation: 30
Thanks for replying.
I never used that function before. It's not even clear to me what it does (from the manpage). From what I can see from the getrusage manpage, though:
Code:
The above struct was taken from 4.3BSD Reno.  Not all fields are meaningful under Linux.  In linux 2.4  only  the
       fields  ru_utime, ru_stime, ru_minflt, and ru_majflt are maintained.  Since Linux 2.6, ru_nvcsw and ru_nivcsw are
       also maintained.
Looks like ru_isrss is not "meaningful" on Linux
 
Old 01-22-2007, 09:50 PM   #4
nnivanov
LQ Newbie
 
Registered: Nov 2006
Location: Russia, Velikiy Novgorod
Distribution: Slackware 12
Posts: 18

Rep: Reputation: 0
wait3() is the same that wait() but with rusage. wait4() is the that waitpid() but also with rusage. Struct rusage just helps to get more information about exited process.

Here is two lines from my /usr/include/bits/resource.h in Linux:

/* Amount of stack memory used (kilobyte-seconds). */
long int ru_isrss;

Good luck!
 
Old 02-02-2007, 11:25 AM   #5
exit3219
Member
 
Registered: May 2005
Location: Moldova
Distribution: Kubuntu
Posts: 199

Original Poster
Rep: Reputation: 30
I've found an easy way to limit stack usage in bash (for all processes started from the shell):
ulimit -Ss 1024
for limiting stack to 1MB. The default was 8MB on my machine.

This fits my needs for now. Thanks, anyway!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linux stack usage xafwodahs Linux - General 2 05-11-2009 01:51 PM
Stack usage of an application tsenthilt Linux - Software 5 05-14-2006 10:09 AM
How to get current stack usage phuna Programming 4 08-11-2005 09:25 PM
need top clone- to measure CPU usage pao Linux - Software 4 10-07-2004 09:46 AM
measure memory usage unosoft Linux - Software 1 10-07-2003 06:25 PM


All times are GMT -5. The time now is 05:06 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration