Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Task stack usage optimization

Posted by rrrum on March 3, 2016

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

Posted by edwards3 on March 3, 2016

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

Posted by rrrum on March 4, 2016

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

Posted by edwards3 on March 4, 2016

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

Posted by richard_damon on March 5, 2016

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

Posted by rrrum on March 8, 2016

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

Posted by xz8987f on March 8, 2016

I'm using this approach with GNU gcc which works well with FreeRTOS: http://mcuoneclipse.com/2015/08/21/gnu-static-stack-usage-analysis/

Erich


Task stack usage optimization

Posted by rtel on March 8, 2016

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.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists