Thread Stack Usage ?

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Thread Stack Usage ?

Post by Tabulous »

Hi couple of questions re thread stacks

1) What the easy way to find out what is consuming the stack working area ? I.e. what variables are responsible ?
2) Just had a stack issue, the failure is instant reset of MCU, no hard faults(as i capture these) where signaled, ive got all the debug defines switched on. Why was this not captured in the RTOS, the idea that the MCU can just reset with no warning is abit worrying.
User avatar
Cesare
Posts: 38
Joined: Tue Jul 11, 2017 11:51 am
Has thanked: 4 times
Been thanked: 4 times

Re: Thread Stack Usage ?

Post by Cesare »

Hi,
I had the same question, then I wrote this function which I call on demand using the shell

Code: Select all


static void stat_threads(BaseSequentialStream *chp)
{
  static const char *states[] = {CH_STATE_NAMES};
  thread_t *tp;
  size_t n = 0;
  size_t sz;
  uint32_t used_pct;

  chprintf(chp, "\r\n");
  chprintf(chp, "     begin        end   size   used    %% prio     state         name\r\n");
  chprintf(chp, "--------------------------------------------------------------------\r\n");

  tp = chRegFirstThread();
  do {
     n = 0;
#if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE)
    uint32_t stklimit = (uint32_t)tp->wabase;
#else
    uint32_t stklimit = 0U;
#endif

    uint8_t *begin = (uint8_t *)stklimit;
    uint8_t *end = (uint8_t *)tp;
    sz = end - begin;

    while(begin < end)
       if(*begin++ == CH_DBG_STACK_FILL_VALUE) ++n;

    used_pct = (n * 100) / sz;

    chprintf(chp, "0x%08lx 0x%08lx %6u %6u %3u%% %4lu %9s %12s\r\n",    stklimit,                                                                                           (uint32_t)tp,
                                                                                             sz,
                                                                                             n,
                                                                                             used_pct,
                                                                                             (uint32_t)tp->prio,
                                                                                             states[tp->state],
                                                                                             tp->name == NULL ? "" : tp->name);

    tp = chRegNextThread(tp);
  } while (tp != NULL);

  chprintf(chp, "\r\n");
}

Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: Thread Stack Usage ?

Post by Tabulous »

Cesare wrote:Hi,
I had the same question, then I wrote this function which I call on demand using the shell

Code: Select all


static void stat_threads(BaseSequentialStream *chp)
{
  static const char *states[] = {CH_STATE_NAMES};
  thread_t *tp;
  size_t n = 0;
  size_t sz;
  uint32_t used_pct;

  chprintf(chp, "\r\n");
  chprintf(chp, "     begin        end   size   used    %% prio     state         name\r\n");
  chprintf(chp, "--------------------------------------------------------------------\r\n");

  tp = chRegFirstThread();
  do {
     n = 0;
#if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE)
    uint32_t stklimit = (uint32_t)tp->wabase;
#else
    uint32_t stklimit = 0U;
#endif

    uint8_t *begin = (uint8_t *)stklimit;
    uint8_t *end = (uint8_t *)tp;
    sz = end - begin;

    while(begin < end)
       if(*begin++ == CH_DBG_STACK_FILL_VALUE) ++n;

    used_pct = (n * 100) / sz;

    chprintf(chp, "0x%08lx 0x%08lx %6u %6u %3u%% %4lu %9s %12s\r\n",    stklimit,                                                                                           (uint32_t)tp,
                                                                                             sz,
                                                                                             n,
                                                                                             used_pct,
                                                                                             (uint32_t)tp->prio,
                                                                                             states[tp->state],
                                                                                             tp->name == NULL ? "" : tp->name);

    tp = chRegNextThread(tp);
  } while (tp != NULL);

  chprintf(chp, "\r\n");
}



I've got something similar, but that does not answer my questions really. Yes i can see stack usage, but that is no good when it resets. I want to know why it reset and what variable caused the stack to got off the rails
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Thread Stack Usage ?

Post by Giovanni »

Hi,

The stack usage is influenced by:

- The function stack frame which depends on the automatic variables defined in the function.
- The call tree, deeper trees require more stack.

Giovanni
User avatar
Cesare
Posts: 38
Joined: Tue Jul 11, 2017 11:51 am
Has thanked: 4 times
Been thanked: 4 times

Re: Thread Stack Usage ?

Post by Cesare »

Sometimes to understand the real stack issue in the application could be very tedious.
I usually start the developing phase setting very huge thread stacks and then monitoring the usage in polling
Post Reply