Thread Stack Usage ?

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

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

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

Thread Stack Usage ?

Postby Tabulous » Thu Feb 15, 2018 11:37 am

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: 36
Joined: Tue Jul 11, 2017 11:51 am
Location: Milan, Italy
Has thanked: 3 times
Been thanked: 3 times

Re: Thread Stack Usage ?

Postby Cesare » Thu Feb 15, 2018 12:01 pm

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: 509
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 17 times

Re: Thread Stack Usage ?

Postby Tabulous » Thu Feb 15, 2018 12:32 pm

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: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Thread Stack Usage ?

Postby Giovanni » Thu Feb 15, 2018 2:10 pm

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: 36
Joined: Tue Jul 11, 2017 11:51 am
Location: Milan, Italy
Has thanked: 3 times
Been thanked: 3 times

Re: Thread Stack Usage ?

Postby Cesare » Thu Feb 15, 2018 2:26 pm

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


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 19 guests