Page 1 of 1

How to get used process stack size in runtime?

Posted: Fri Mar 04, 2016 7:32 am
by kreyl
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.

Re: How to get used process stack size in runtime?

Posted: Fri Mar 04, 2016 12:18 pm
by Giovanni
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

Re: How to get used process stack size in runtime?

Posted: Sun Sep 18, 2016 4:21 pm
by crispus
In this case you can only to know unused stack, not the stack usage percentage.

Re: How to get used process stack size in runtime?

Posted: Mon Sep 26, 2016 10:05 pm
by alexblack
I used this function (not working in main, because main workspace do not filled by 0x55):

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?

Posted: Mon Sep 26, 2016 10:15 pm
by alexblack
In last OS versions it must be 'th->wabase' instead of 'th->stklimit'.

Re: How to get used process stack size in runtime?

Posted: Tue Sep 27, 2016 7:49 am
by Giovanni
In last version you can also fill the main and process stack with 0x55, there is an option in ctr0.s.

Giovanni