Hi!
Chibios v 16.1.3, stm32f072
Is it possible to get current memory usage of a thread? Something like thread size in Eclipse plugin, but in runtime.
I had a look at port_switch function, where stack check is performed, but imlementing the same code inside a thread gives strange results.
How to get used process stack size in runtime?
Moderators: RoccoMarco, lbednarz, tfAteba
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: How to get used process stack size in runtime?
Hi,
Stack usage is done by measuring the filler left in thread working areas, it is not related to the stack overflow check.
You could do the same, from stack base limit count how many consecutive 0x55 are there upward.
Giovanni
Stack usage is done by measuring the filler left in thread working areas, it is not related to the stack overflow check.
You could do the same, from stack base limit count how many consecutive 0x55 are there upward.
Giovanni
Re: How to get used process stack size in runtime?
I used this function (not working in main, because main workspace do not filled by 0x55):
In any thread include this line (for example):
Note: DEBIG_LINE it is my printf like macro/function for sending text to PC Terminal (it is multichannel and support colors).
This function do not compilled if some debug option is turned off in chconf.h file.
Code: Select all
/** ------------------------------------------------------------------------ **
* @brief Print stack usage
*
* @param[in] name - name of the thread for debug out
* @param[in] wa_size - size of the working area
* ------------------------------------------------------------------------- */
void DEBUG_STACK_USAGE(const char *name, int wa_size)
{
// calculate real size for stack
wa_size -= PORT_WA_SIZE(0);
uint32_t limit = wa_size;
if (limit <= 0)
limit = 4096;
thread_t *th = chThdGetSelfX();
uint32_t *p = (uint32_t *) th->stklimit;
// searching for end of stack usage
int free = 0;
while (free < limit)
{
if (*p++ != 0x55555555)
break;
free += sizeof(uint32_t);
}
if (wa_size > 0)
{
if (free < 100)
{
DEBUG_LINE(DEBUG_CH1 "\r" DEBUG_COLOR_BG_RED "%s: STACK USAGE: free %d bytes, used %d bytes\r\r" DEBUG_COLOR_RESET DEBUG_CH0,
name, free, wa_size - free);
} else
{
DEBUG_LINE(DEBUG_CH1 "\r" /*DEBUG_COLOR_YELLOW*/ "%s: STACK USAGE: free %d bytes, used %d bytes\r\r" DEBUG_COLOR_RESET DEBUG_CH0, name, free, wa_size - free);
}
} else
{
if (free < 100)
{
DEBUG_LINE(DEBUG_CH1 "\r" DEBUG_COLOR_BG_RED "%s: STACK USAGE: free %d bytes\r\r" DEBUG_COLOR_RESET DEBUG_CH0 ,
name, free);
} else
{
DEBUG_LINE(DEBUG_CH1 "\r" /*DEBUG_COLOR_YELLOW*/ "%s: STACK USAGE: free %d bytes\r\r" DEBUG_COLOR_RESET DEBUG_CH0,
name, free);
}
}
}
In any thread include this line (for example):
Code: Select all
#define DEBUG_MODULE "CALCULATION"
...
THD_WORKING_AREA(wa_main_calculation, 1024);
THD_FUNCTION(main_calculation, arg)
{
...
DEBUG_STACK_USAGE(DEBUG_MODULE, sizeof(wa_main_calculation));
...
}
Note: DEBIG_LINE it is my printf like macro/function for sending text to PC Terminal (it is multichannel and support colors).
This function do not compilled if some debug option is turned off in chconf.h file.
Re: How to get used process stack size in runtime?
In last OS versions it must be 'th->wabase' instead of 'th->stklimit'.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: How to get used process stack size in runtime?
In last version you can also fill the main and process stack with 0x55, there is an option in ctr0.s.
Giovanni
Giovanni