Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years ago.
RTOS Memory Usage Confusion
I'm very confused by the high memory consumption of the official RTOS library on the LPC11U24. I'm trying to create 4 threads in addition to the main thread, with each one flashing one of the on-board LEDs. This works fine for the first 3 threads, but trying to create the fourth one triggers an error. I can't see what the error is since nothing is printed out, but I'm pretty sure it can't allocate memory for the stack. This is with the default stack size of 512 bytes per task. If I reduce this to 256 bytes per task, everything works fine. Now here's the confusing part, according to this question, each thread consumes 172B in addition to the stack. So five threads should only be consuming 3420B. Now, according to the RTOS Memory Model, the Idle Thread, Timer Thread, and OS Scheduler also consume some RAM. I dug into RTX_Conf_CM.c and determined they each require 128B. Assuming they each require an additional 172B like regular threads, this brings our total to 4320B. So where is the rest of the RAM disappearing to? Shouldn't there be about 3872B left over? Here's the code I used to test with:
#include "mbed.h" #include "rtos.h" //LED objects DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); void led1_thread(void const *args) { while (true) { led1 = !led1; Thread::wait(50); } } void led2_thread(void const *args) { while (true) { led2 = !led2; Thread::wait(100); } } void led3_thread(void const *args) { while (true) { led3 = !led3; Thread::wait(150); } } void led4_thread(void const *args) { while (true) { led4 = !led4; Thread::wait(200); } } int main() { printf("Initializing RTOS...\n"); printf("\tCreating thread 1...\n"); Thread thread1(led1_thread, NULL, osPriorityNormal, 512); printf("\tCreating thread 2...\n"); Thread thread2(led2_thread, NULL, osPriorityNormal, 512); printf("\tCreating thread 3...\n"); Thread thread3(led3_thread, NULL, osPriorityNormal, 512); printf("\tCreating thread 4...\n"); Thread thread4(led4_thread, NULL, osPriorityNormal, 512); while (true) { printf("Hello!\n"); Thread::wait(1000); } }
Question relating to:
1 Answer
11 years ago.
I think there is a small bug in RTX which causes it to believe that there is only 4k of RAM in the LPC11U24 and not 8k. I have a fix here on my machine for this issue but I never issued a pull request as it was part of a bunch of other RTOS changes I was working on for better GCC support.
If you look here at the RTX code you will see this line:
#define INITIAL_SP (0x10001000UL)
That starts the main stack 4k below the actual top. It should probably be:
#define INITIAL_SP (0x10002000UL)
That fixed it, thanks! After changing the INITIAL_SP to 0x10002000, I was able to create 8 LED flashing threads in addition to the main thread using the default stack size. Which brings me to my next question... What's a reasonable minimum stack size?
NOTE: I submitted a pull request for this issue, so hopefully the changes will get merged back into the main library.
posted by 14 Nov 2013