Page 1 of 1

-fstack-usage support

Posted: Thu Jan 31, 2019 3:09 pm
by dragondgold
I'm using ESP-IDF v3.1.2 trying to use the -fstack-usage compiler flag. I'm basically trying to achieve what this page explains https://mcuoneclipse.com/2015/08/21/gnu ... -analysis/. I want this compiler flag to optimize the stack usage of my tasks because in large and complex codebases the usual uxTaskGetStackHighWaterMark() method to optimize the stack usage is time-consuming and error-prone.

I have added the -fstack-usage to my components CFLAGS but when compiling the .su files are not being generated. Does the XTensa GCC support the -fstack-usage? I cannot find any information about this.

Re: -fstack-usage support

Posted: Thu Jan 31, 2019 11:08 pm
by ESP_Angus
This is very neat, how do I not know this feature existed!?

I was able to get .su files for a component's source files with the following additions to component.mk (for GNU make based build system):

Code: Select all

CFLAGS += -fstack-usage
(Reference)

And the following addition to CMakeLists.txt (for CMake based build system):

Code: Select all

component_compile_options(-fstack-usage)
(Reference)

Note that, as the linked blog post explains, some additional scripting magic is required to find total static call stack stack usage, rather than individual function's' individual stack. There is an extra complexity for ESP-IDF applications because some functions are in the ESP32 ROM (including the default implementations of printf and scanf, which are big stack hogs). And I assume there's no way to measure static stack usage of a recursive function, although most ESP-IDF apps probably don't have a lot of these![*]

There might be some tooling we can do to make this easier in ESP-IDF (including providing .su files for the ROM, maybe).

[*] Edit: on a moment's further reflection there are a bunch of things C can do - function pointers for one - which probably make reliable static stack measurement hard in a lot of cases. But may still be useful for calculating simple call stack overheads.

Re: -fstack-usage support

Posted: Tue Feb 05, 2019 6:18 pm
by dragondgold
ESP_Angus wrote:
Thu Jan 31, 2019 11:08 pm
This is very neat, how do I not know this feature existed!?

I was able to get .su files for a component's source files with the following additions to component.mk (for GNU make based build system):

Code: Select all

CFLAGS += -fstack-usage
(Reference)

And the following addition to CMakeLists.txt (for CMake based build system):

Code: Select all

component_compile_options(-fstack-usage)
(Reference)

Note that, as the linked blog post explains, some additional scripting magic is required to find total static call stack stack usage, rather than individual function's' individual stack. There is an extra complexity for ESP-IDF applications because some functions are in the ESP32 ROM (including the default implementations of printf and scanf, which are big stack hogs). And I assume there's no way to measure static stack usage of a recursive function, although most ESP-IDF apps probably don't have a lot of these![*]

There might be some tooling we can do to make this easier in ESP-IDF (including providing .su files for the ROM, maybe).

[*] Edit: on a moment's further reflection there are a bunch of things C can do - function pointers for one - which probably make reliable static stack measurement hard in a lot of cases. But may still be useful for calculating simple call stack overheads.
Thank you! That did the job :)

Yes, ROM functions and function called through pointers will still need to be considered apart but I think it could be useful to have a general idea of the stack usage or almost a complete stack usage analysis in simpler tasks. Providing the .su files for the ROM functions would be awesome!