Stack size and uxTaskGetStackHighWaterMark disagree?

espmpo
Posts: 1
Joined: Fri Aug 16, 2024 3:10 am

Stack size and uxTaskGetStackHighWaterMark disagree?

Postby espmpo » Thu Nov 14, 2024 11:35 pm

Hello,

I'm trying to optimize some of my freeRTOS tasks.

ESP-IDF's documentation clearly states that their fork of freeRTOS takes stack creation size in BYTES.
Inspection of task.h/.c in esp-idf's freertos kernel confirms that this is in bytes.

Meanwhile, uxTaskGetStackHighWaterMark documents that it returns a value numerated in WORDS.

Certainly, return values from uxTaskGetStackHighWaterMark get smaller as one adds nested calls in a given task, testing the stack depth/ceiling and increasing the highwatermark.

However, the HighWaterMark return value, when multiplied from words into bytes, is much larger than the allocated stack size in bytes, when little of the stack has been used.
This suggest that uxTaskGetStackHighWaterMark is actually returning the remaining untouched stack size in bytes?

For example:
I have a task that was created w/ xTaskCreate and usStackDepth was set to 2060 (515 words).
A call to uxTaskGetStackHighWaterMark (or uxTaskGetStackHighWaterMark2 for that matter) right out of the gate when the task starts yields:
I (431) blink_led: Configured to blink GPIO LED!
I (431) blink_led: TaskHighWaterMark: (rem) WORDS == 1544, BYTES == 6176
I (441) blink_led: TotalAllocTaskSze: WORDS == 515, BYTES == 2060
...does some gpio stuff that utilizes more stack.
I (5451) blink_led: TaskHighWaterMark: (rem) WORDS == 24, BYTES == 96
I (5451) blink_led: TotalAllocTaskSze: WORDS == 515, BYTES == 2060

The initial amount of WORDS assessed by TaskHighWaterMark is clearly too high, but would make sense as bytes.

Related to all of this: is there a way with VS Code to view the stack's contents short of dumping raw memory to the terminal?
Thanks, mpo

MicroController
Posts: 1725
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Stack size and uxTaskGetStackHighWaterMark disagree?

Postby MicroController » Fri Nov 15, 2024 12:42 pm

The FreeRTOS docs may be a little ambiguous there. FreeRTOS's stack management operates on elements of type StackType_t (="words"), which a port can define as it needs. In case of the IDF, StackType_t is uint8_t, so all references to stack "words" in the docs refer to this 1-byte "word" type.
If you want to be extra portable in your code, you can always multiply/divide by sizeof(StackType_t) to convert between byte counts and word counts.

snutw_
Posts: 34
Joined: Wed Aug 21, 2024 1:48 pm

Re: Stack size and uxTaskGetStackHighWaterMark disagree?

Postby snutw_ » Mon Nov 18, 2024 9:48 am

espmpo wrote:
Thu Nov 14, 2024 11:35 pm
Related to all of this: is there a way with VS Code to view the stack's contents short of dumping raw memory to the terminal?
Thanks, mpo
Isn't this possible in debuging?

Who is online

Users browsing this forum: No registered users and 124 guests