Task stack usage optimization

Hello, I have aplication with about 15 tasks. Debugger with FreeRTOS plugin says that I have from 200-700 bytes free on each task. What is the rule for optimization for task stack in general? How much can I reduce free space? How can I be sure it is enough? In application with many tasks it is possible to achieve significant ram saving if the right approach is adopted. Thanks

Task stack usage optimization

There is no point giving a task any more stack bytes that it is going to use because it would just be dead RAM, so if you need to optimize every byte of RAM you need to now how much stack each task uses. Like in any C program how many stack bytes are used depends on function call nesting depth, compiler optimization, how variables are declared, …

Task stack usage optimization

Thank you for answer but it is very general and does not help me much. I know that there is a dead RAM if stacks are oversized thats why I deal this problem. It is very difficult calculate function nesting in enviroment with OS. I also cannot just reduce on limit and then wait on stack overflow. So I was asking if anybody has a simple rule to be safe but on the other hand don’t waist ram.

Task stack usage optimization

There is practically no difference in the stack used with and without FreeRTOS if both are compiled with the same compiler. Without FreeRTOS there is the stack used by main() but no stack used by the task context. In a FreeRTOS task main() has no influence, but you need about 20 words for the context.

Task stack usage optimization

Rum, difinitively figuirng out how much stack a general program/task will take is a ‘hard’ task, and often the best choise is to run it (over a variety of input conditions) and measure it, and sometimes add a safety factor in case you missed a path. If the program doesn’t use recursion, than some compilers/linkers will have an option to compute the worse case stack usage of a function if it meets some basic limits. You can also do this by hand, but you may need a bit of help from the compiler. You can estimate the stack needed by a function by adding up the size of all the local variables and parameters it uses, adding a bit for temporaries needed, and call frames, and then adding what is needed by the things it calls (and the things they call and so on). You may have to guess on the requirements for library functions, but this is normally not the major usage.

Task stack usage optimization

Thanks Richard, What kind of help I can get from GCC? What could be the safety factor value? Are there any tools/debuggers/profilers etc… available specifically for FreeRTOS to help with this task? Are they effective? In the past I could see such tool from Infineon. Since the stack is dedicated to each task individually its sizing is very critical because overflow usually causes system crash.

Task stack usage optimization

I’m using this approach with GNU gcc which works well with FreeRTOS: GNU Static Stack Usage Analysis Erich

Task stack usage optimization

Sorry for the brevity of my reply. The FreeRTOS+Trace tool will trace memory allocations. See the FreeRTOS stack FAQ for a link to Erich’s blog post on getting stack usage info from GCC.