Questions about THD_WORKING_AREA_SIZE

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Post Reply
josesimoes
Posts: 91
Joined: Sat Feb 18, 2017 11:50 am
Has thanked: 43 times
Been thanked: 23 times

Questions about THD_WORKING_AREA_SIZE

Post by josesimoes »

Hi,

I would like to understand what exactly fits into the THD_WORKING_AREA_SIZE.
Trying to optimize a project and can't find enough documentation to help on this.
How can one know (or estimate) the proper size for this?

Thanks!
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Questions about THD_WORKING_AREA_SIZE

Post by Giovanni »

Hi,

The parameter is the stack size reserved for the thread. The system then adds:
- Space for a thread_t structure.
- Space for port_intctx and port_extctx structures.
- Space for IRQ servicing, architecture dependent.
- Space for stack alignment enforcing, architecture dependent.

Note that he space for IRQ servicing is modifiable with a PORT option, it is set very conservatively by default, you may trim that down but consider that the required space may change depending on compiler version/settings.

Code: Select all

/**
 * @brief   Per-thread stack overhead for interrupts servicing.
 * @details This constant is used in the calculation of the correct working
 *          area size.
 * @note    In this port this value is conservatively set to 64 because the
 *          function @p chSchDoReschedule() can have a stack frame, especially
 *          with compiler optimizations disabled. The value can be reduced
 *          when compiler optimizations are enabled.
 */
#if !defined(PORT_INT_REQUIRED_STACK) || defined(__DOXYGEN__)
#define PORT_INT_REQUIRED_STACK         64
#endif


Giovanni
josesimoes
Posts: 91
Joined: Sat Feb 18, 2017 11:50 am
Has thanked: 43 times
Been thanked: 23 times

Re: Questions about THD_WORKING_AREA_SIZE

Post by josesimoes »

OK got you! I'll look into that piece of code closelly.

What goes into that "thread stack"?
Are there any hints on how to estimate the proper size for it?

Or perhaps a better question would be: how do I know that a var goes into the thread stack and not onto the "regular" stack? ;)
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Questions about THD_WORKING_AREA_SIZE

Post by Giovanni »

Hi,

Each thread has a separate stack, there is no "regular stack", that is the stack of the main thread.

The stack must be large enough to contain all stack frames of functions called by the thread, the worst case depth. If in doubt make it very large, run your program making sure in uses all code paths then measure stack usage using the debug plugin.

Giovanni
josesimoes
Posts: 91
Joined: Sat Feb 18, 2017 11:50 am
Has thanked: 43 times
Been thanked: 23 times

Re: Questions about THD_WORKING_AREA_SIZE

Post by josesimoes »

understood! that was very helpful
Post Reply