Very high stack usage with FreeRTOS and Eclipse-Arduino

RA5040
Posts: 26
Joined: Mon Jul 30, 2018 1:20 pm

Very high stack usage with FreeRTOS and Eclipse-Arduino

Postby RA5040 » Sun Sep 02, 2018 9:20 pm

I had a suspected stack overflow issue and checked stack usage using the FreeRTOS utility function uxTaskGetStackHighWaterMark.

The max usage on a task written in Eclipse-Arduino is over 10,000 bytes (2500 words). This task does nothing (it just has a vTaskDelay and nothing else).

I then checked the esp-idf Blink task from the command line and it uses a max of under 2000 bytes (500 words). With the Blink task cut down to nothing but vTaskDelay, the max stack usage drops to 1500 bytes.

What are the possible reasons for that and what can be done about it? 10KB is a huge amount amount of stack space. Even 1500bytes seems excessive (even taking into account the 4-byte word size).

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Very high stack usage with FreeRTOS and Eclipse-Arduino

Postby ESP_Angus » Mon Sep 03, 2018 2:02 am

Hi RA5040,

I'm unsure what you mean by "Eclipse-Arduino". Can you post some examples of the functions you are measuring stack usage for, please?


Angus

RA5040
Posts: 26
Joined: Mon Jul 30, 2018 1:20 pm

Re: Very high stack usage with FreeRTOS and Eclipse-Arduino

Postby RA5040 » Mon Sep 03, 2018 6:26 am

Eclipse-Arduino (also called Sloeber) is a plugin for Eclipse that allows one to write Arduino code in Eclipse.

I've checked the code in the Arduino IDE also and the results are the same.

Here is the test code:


static TaskHandle_t beeperTaskHandle = NULL;

void BeeperTask(void * parameter) {
int hilo = 0;
for (;;) {
hilo = !hilo;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

void setup() {
// put your setup code here, to run once:
xTaskCreate(BeeperTask, "Beeper task", 3000, (void *) 1, 1, &beeperTaskHandle);
Serial.begin(115200);
}

void loop() {

Serial.printf("beeperTaskHandle Usage (bytes) = %d\n",uxTaskGetStackHighWaterMark(beeperTaskHandle)*4);

}

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: Very high stack usage with FreeRTOS and Eclipse-Arduino

Postby chegewara » Mon Sep 03, 2018 6:57 am

First of all you are checking and printing highwater stack size, which means it's max unused stack size. Second, you are checking stack size of beepTask not looperTask, and third of all why you are multiplying it by 4?
That's why you have 3000*4 = 12000 bytes.

Now about looperTask stack size, it's by default about 8kB because most of usage cases arduino users will use only 1 task and this stack needs to be big enough to handle many drivers.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Very high stack usage with FreeRTOS and Eclipse-Arduino

Postby ESP_Angus » Mon Sep 03, 2018 8:27 am

As chegewara says, the result of uxTaskGetStackHighWaterMark() is the minimum amount of free stack space for the lifetime of the task. Larger values == less stack used.

A difference between ESP-IDF and vanilla FreeRTOS is that stack sizes are represented in bytes, not words. So there's no need to multiply by 4. A full list of differences can be found here: https://docs.espressif.com/projects/esp ... s-smp.html

If the result of uxTaskGetStackHighWaterMark(beeperTaskHandle) is 2500 with stack size 3000 then you can surmise that peak stack usage is 3000 - 2500 = 500 bytes. This is still a little high for a task which does almost nothing, but most of this is one-off cost due to needing stack for CPU exception handlers.

RA5040
Posts: 26
Joined: Mon Jul 30, 2018 1:20 pm

Re: Very high stack usage with FreeRTOS and Eclipse-Arduino

Postby RA5040 » Mon Sep 03, 2018 12:54 pm

hmmm ... I'm a bit embarrassed!

Thank you for pointing out what should have been obvious (reminder to myself to read the documentation more carefully!!).

Regarding the other points:
- I did want to get the stack usage of the Blink task, not the loop task
- I didn't realize that the xtaskcreate in esp-idf freertos specified the stack size in bytes ... I wonder why they didn't stick to the freertos spec?

Anyway, thank you :)

Who is online

Users browsing this forum: No registered users and 136 guests