Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Stack size calculation - any tools available?

0 views
Skip to first unread message

Gerd

unread,
Nov 16, 2009, 5:20:38 PM11/16/09
to
I need to allocate the stack for my system with a proper size. I don't
want do a vage estimation, so I would like to know: Is there a tool
that scans C source code, and calculates the required stack size,
based on the call graph? Only plain C, no recursion.

Any suggestions are welcome.

Thanks.

Marcel Müller

unread,
Nov 16, 2009, 6:23:51 PM11/16/09
to
Hi,

Gerd wrote:
> I need to allocate the stack for my system with a proper size. I don't
> want do a vage estimation, so I would like to know: Is there a tool
> that scans C source code, and calculates the required stack size,
> based on the call graph? Only plain C, no recursion.

the C language *cannot* supply an exact stack size.

This is only possible if you apply hard restrictions. E.g. your code
must be compilable without any forward declarations of functions.
Otherwise you cannot calculate the stack size for all nested function
calls. And for the same reason the program must not contain any
recursion. This has to be emulated with heap memory.
Function pointers, alloca and C99 VLA are not allowed, too.
Also any library used must meet the same constrains. Do you have the
source of all libraries you use?
And last but not least, once you managed all of the above the result is
platform dependent.

What you can do is to get a typical stack size from application runs.
E.g. you might run all test cases (including load tests!) and observe
the used stack space. The compiler or the operating system may aid you.
Compilers sometimes provide switches that initialize the stack memory to
a certain pattern. When a thread completes you can easily see how much
stack was used. The operating system might track the stack size by using
guard pages. You simply have to query the commited stack area in this case.


Marcel

Michael Schumacher

unread,
Nov 16, 2009, 9:44:51 PM11/16/09
to
Gerd wrote:

Die Benutzung eines amerikanischen Newsservers verpflichtet einen
nicht dazu, Beiträge in de.-Gruppen in Englisch zu verfassen... ;-)

Ich vermute mal, daß es sich hier um ein "Embedded System" handelt
und die Speicher-Ressourcen knapp sind, bzw. beim Starten einer
"Task" über das Betriebssystem der Stackbedarf explizit angegeben
werden muß.

C -- als Sprache -- hilft da natürlich nicht, aber bevor man mit
(evtl. kostenpflichtigen) Tools anfängt, empfehle ich folgenden
"Trick", den ich häufig und erfolgreich eingesetzt habe: einfach
vor Programm-/Taskstart den überdimensionierten Stackbereich mit
einem bestimmten Muster vollschreiben (z.B. 0xff, 0xbaff, oder
0xdeadbeef), dann das Programm bzw. die Task starten und dafür
sorgen (z.B. durch fingierte Eingabedaten), daß möglichst alle
Programmteile ausgeführt werden; einen Breakpoint auf das Ende
von "main()" setzen, und bei Erreichen desselben via Monitor
oder Debugger anhand des vorgegebenen Musters nachschauen, wie
weit sich der Stack "durchgefressen" hat (ausgehend vom aktuellen
Stackpointer, der jetzt ja wieder ziemlich am Ursprungspunkt
angekommen ist). Zur Sicherheit addiert man nochmal 10% oder
mindestens 1 KB drauf, und erhält so einen brauchbaren Wert.

Hat bei mir immer perfekt funktioniert, nur muß halt wirklich
sichergestellt sein, daß nirgendwo (auch nicht in Bibliotheken!)
Rekursionen oder dynamische alloca()-Aufrufe vorkommen. Falls
doch, müssen die o.g. "Eingabedaten" so beschaffen sein, daß die
maximale Rekursionstiefe erreicht, und/oder die maximalen
Stackbereiche angefordert werden.


mike

Stefan Scholl

unread,
Nov 18, 2009, 2:53:15 AM11/18/09
to
Hach, das weckt Erinnerungen an den l�ngsten Thread in
de.comp.sys.amiga.*: SAS/C Stacksize berechnen

;-)


0 new messages