Page 1 of 1

Stack overflow but uxTaskGetStackHighWaterMark much smaller

Posted: Mon May 10, 2021 8:34 am
by bastian.h.jaeger
Hi,

I am trying to reduce my stack utilization in a customer project right now. So far I have no stripped down code to share, but I am working on it. But the question might be answered without it.

I have some code working in event loop.

When I set the stack size to 6000 for each and check the watermark of my event loops (2 loops), they show me

Code: Select all

I (09:19:07.443) Common:      task         prio  watermark
I (09:19:10.795) Common:      Sns1EvtTsk   24    1072
I (09:19:10.800) Common:      Sns2EvtTsk   24    584
But when I reduce the stack size of my Sns2 to 4000, I get something like

Code: Select all

I (09:17:05.262) Snsor1: [1620638226000] measuring now ...

***ERROR*** A stack overflow in task RtcEvtTsk has been detected.

Backtrace:0x400d2d93:0x3ffb96700x400873b9:0x3ffb9690 0x4008a569:0x3ffb96b0 0x400890b5:0x3ffb9730 0x400874b8:0x3ffb9750 0x4008746a:0x00000000  |<-CORRUPTED
0x400d2d93: panic_abort at /home/user/dev/official/esp-idf/components/esp_system/panic.c:354

0x400873b9: esp_system_abort at /home/user/dev/official/esp-idf/components/esp_system/esp_system.c:126

0x4008a569: vApplicationStackOverflowHook at /home/user/dev/official/esp-idf/components/freertos/port/xtensa/port.c:490

0x400890b5: vTaskSwitchContext at /home/user/dev/official/esp-idf/components/freertos/tasks.c:3274

0x400874b8: _frxt_dispatch at /home/user/dev/official/esp-idf/components/freertos/port/xtensa/portasm.S:432

0x4008746a: _frxt_int_exit at /home/user/dev/official/esp-idf/components/freertos/port/xtensa/portasm.S:231
Maybe to mention: The call in the event callback to Sns2 triggers Sns1.

No my question:
How is it possible to run in a stack overflow with a stack size of 4000, if my water mark shows my a maximum which is much smaller?

Re: Stack overflow but uxTaskGetStackHighWaterMark much smaller

Posted: Mon May 10, 2021 8:58 am
by ESP_jakob
Hi!

Several things to consider here. First, printf functions may consume quite some memory. I heard that 1 to 1.5k can be considered usual. Please not that the high water mark API reports the minimum available stack space: https://docs.espressif.com/projects/esp ... skHandle_t. The numbers from your tasks mean that at least 500 or 1000 bytes had been free. If you reduce the stack by 1 or 2k, you'll almost certainly run into an overflow.

Best,
Jakob