How to get used process stack size in runtime?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
kreyl
Posts: 59
Joined: Sun Jan 13, 2013 11:46 pm
Been thanked: 4 times

How to get used process stack size in runtime?

Post 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.
User avatar
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?

Post 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
crispus
Posts: 24
Joined: Sat Aug 27, 2016 10:14 am
Has thanked: 4 times
Been thanked: 3 times

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

Post by crispus »

In this case you can only to know unused stack, not the stack usage percentage.
alexblack
Posts: 277
Joined: Mon Sep 24, 2012 3:52 pm
Been thanked: 33 times

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

Post 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.
alexblack
Posts: 277
Joined: Mon Sep 24, 2012 3:52 pm
Been thanked: 33 times

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

Post by alexblack »

In last OS versions it must be 'th->wabase' instead of 'th->stklimit'.
User avatar
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?

Post by Giovanni »

In last version you can also fill the main and process stack with 0x55, there is an option in ctr0.s.

Giovanni
Post Reply