Overhead of Thread Working Area in NIL and RT Topic is solved

Discussions and support about ChibiOS/NIL, the almost nil RTOS.
rizzir
Posts: 5
Joined: Mon Feb 29, 2016 7:48 pm

Overhead of Thread Working Area in NIL and RT  Topic is solved

Postby rizzir » Wed Aug 16, 2017 7:08 am

In the feature description of Nil it is said that tasks only take 7-24 Bytes of memory http://chibios.org/dokuwiki/doku.php?id ... l:features.

Out of curiosity I have compiled the NIL-ARMCM0-GENERIC example in demos/various/ and set the WA-size to 128 Bytes for thread1 and 256 Bytes for thread2. If I look into the .dmp file, I can see that waThread1 is 0x108 (=264) Bytes and waThread2 is 0x188 (=392) Bytes, which means, that the overhead in each work area is 136 Bytes. Obviously additional 136 Bytes are not the end of the world but I'm just curious because it was stated otherwise on the website. Does anybody know what the additional 136 Bytes are good for?

BTW: I have not touched the source of the example otherwise, including chconf.h. That means all OS-features except mutexes are enabled.

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: Overhead of Thread Working Area in NIL and RT

Postby Giovanni » Wed Aug 16, 2017 2:02 pm

Hi,

This is the expansion of the macro:

static THD_WORKING_AREA(waThread2, 128);

Code: Select all

stkalign_t waThread2[((size_t)((size_t)((sizeof (struct port_intctx) +                      \
                         sizeof (struct port_extctx) +                      \
                         ((size_t)(128)) + ((size_t)(64)))) +                \
                                               ((size_t)(sizeof (stkalign_t)) - 1U)) & ~((size_t)((sizeof (stkalign_t))) - 1U)) / sizeof (stkalign_t)]


Those two structure represent the CPU registers that are saved into the thread stack during context switching, the 64 is the configurable option PORT_INT_REQUIRED_STACK, the rest is stack alignment.

PORT_INT_REQUIRED_STACK is set safely to 64 bytes but it can be reduced a lot, it represents the stack space used for context switch after IRQ servicing. It is set to a large value because this size can change depending on compiler version and options. It is the stack frame size of the function chSchDoReschedule() which is 8 bytes using GCC 5.4 and -O2. It can be trimmed down a lot but in general better do this after development.

Giovanni

rizzir
Posts: 5
Joined: Mon Feb 29, 2016 7:48 pm

Re: Overhead of Thread Working Area in NIL and RT

Postby rizzir » Sat Aug 19, 2017 11:37 pm

Hello Giovanni,

thank you for your explanation. Now that totally makes sense to me. Probably I just should have looked at the macro expansion myself but anyway your explanation was really helpful, especially regarding the size of PORT_INT_REQUIRED_STACK. It is also good to know that the size for saving the registers during context switching is already accomodated for, so if I am not mistaken, the user-defined size of a working area is what the application itself can really use.

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: Overhead of Thread Working Area in NIL and RT

Postby Giovanni » Sun Aug 20, 2017 7:16 am

Correct.

Giovanni


Return to “ChibiOS/NIL”

Who is online

Users browsing this forum: No registered users and 6 guests