How to measure function stack usage in C?

Is there a way to measure how much stack memory a function uses?

This question does not apply to recursive functions; however, I was interested to know how much stack memory will be a function, a recursive function.

I was interested in optimizing the function to use the stack memory; however, not knowing what optimizers the compiler is already doing, it just guesses if it makes real improvements or not.

To be clear, this is not a question of how to optimize for more efficient use of the stack.

So, is there any reliable way to find out how much stack memory a function uses in C?


Note. Assuming it does not use alloca arrays or variable lengths, this should be possible to find at compile time.

+6
source share
3 answers

Using alerts

This is the GCC specification (verified with gcc 4.9):

Add this to the above function:

 #pragma GCC diagnostic error "-Wframe-larger-than=" 

Which messages lead to errors, for example:

 error: the frame size of 272 bytes is larger than 1 bytes [-Werror=frame-larger-than=] 

While a bit of a strange way, you can at least do it quickly by editing the file.

Using CFLAGS

You can add -fstack-usage to your CFLAGS, which then writes text files along object files. See: https://gcc.gnu.org/onlinedocs/gnat_ugn/Static-Stack-Usage-Analysis.html Although this works very well, it may be a little awkward depending on your build / configuration - to create one file with another CFLAG, although this of course can be automated. - (thanks to @nos comment)


Note

It seems that most / all natural compiler methods rely on guesswork, which is not 100% accurate after optimization, so this at least gives a definitive answer using a free compiler.

+3
source

You can very easily find out how much stack space is occupied by a function call that has only one word of local variables as follows:

 static byte* p1; static byte* p2; void f1() { byte b; p1 = &b; f2(); } void f2() { byte b; p2 = &b; } void calculate() { f1(); int stack_space_used = (int)(p2 - p1); } 

(Note: the function declares a local variable, which is only a byte, but the compiler usually allocates for it the entire machine word on the stack.)

So this will tell you how much stack space the function call takes. The more local variables you add to the function, the more stack space there will be. Variables defined in different areas within a function usually do not complicate the situation, since the compiler usually allocates a separate area on the stack for each local variable without any optimization attempts based on the fact that some of these variables may never coexist.

+1
source

To calculate the stack usage for the current function, you can do something like this:

 void MyFunc( void ); void *pFnBottom = (void *)MyFunc; void *pFnTop; unsigned int uiStackUsage; void MyFunc( void ) { __asm__ ( mov pFnTop, esp ); uiStackUsage = (unsigned int)(pFnTop - pFnBottom); } 
0
source

Source: https://habr.com/ru/post/982404/


All Articles