Using a Mailbox increases thread callup timing

Discussions and support about ChibiOS/RT, the free embedded RTOS.
JSStabl
Posts: 78
Joined: Tue Feb 25, 2020 4:06 pm
Has thanked: 3 times
Been thanked: 2 times

Using a Mailbox increases thread callup timing

Postby JSStabl » Tue Aug 23, 2022 3:08 pm

Hi everyone,

we implemented a new feature in our code that required a new mailbox. After hat we faced the issue that the thread-callup timings have increased. Not much, but enough to make a system-in-the-loop test fail.

we filled the mailbox here in sender.c, which is located in Thread A:

Code: Select all

int send(uint8_t payload) {
    ...
    msg_t mbStatus = MSG_RESET;
     mbStatus = chMBPostTimeout(&modbusDebugCommandMailbox, (msg_t) &payload, 500);
    if (mbStatus == MSG_OK) {returncode = 0;}
    return returncode;
}


we read out the mailbox here in receiver.c in Thread B:

Code: Select all

    extern mailbox_t modbusDebugCommandMailbox;
    msg_t mbStatus = chMBFetchTimeout(&modbusDebugCommandMailbox, &msg, 500);
    uint8_t *payload = (uint8_t *) msg;


we defined and initialised the mailbox in a common file

Code: Select all

#define MODBUS_DEBUG_COMMAND_MAILBOX_BUFFER_SIZE 32

mailbox_t modbusDebugCommandMailbox;
msg_t modbusDebugCommandMailboxBuffer[MODBUS_DEBUG_COMMAND_MAILBOX_BUFFER_SIZE];

    memset(modbusDebugCommandMailboxBuffer, 0, sizeof(modbusDebugCommandMailboxBuffer));
    chMBObjectInit(&modbusDebugCommandMailbox, modbusDebugCommandMailboxBuffer, MODBUS_DEBUG_COMMAND_MAILBOX_BUFFER_SIZE);



So the mailbox is used to send data from Thread A to Thread B.

There is another Thread C, that has higher priority than A and B and is called every 66µs. This one doesn't use this mailbox at all. We measure the timings of this Thread C. It has an endless loop in it where we measure the time between the beginning of the loop and the end. In the production Code it takes about 56µs to complete. We found that with the mailboxes it takes about 1.5µs more time to complete. After removing the mailbox and implement the feature with a simple array (without mutexes) that stores the data, the timings were back to normal. After introducing the necessary mutexes to it, the timings were up again.

Has anyone an idea on why this happens and how to solve it?

thanks in advance and best

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: Using a Mailbox increases thread callup timing

Postby Giovanni » Tue Aug 23, 2022 4:32 pm

Hi,

Not sure but mailboxes are not exactly "lightweight", are synchronous messages an option in your scenario? those are much much faster, the message is passed directly thread-to-thread without buffering.

Just a note, why send "payload" by pointer? just send through the value and save that dirty access from one thread into the stack of another thread.

Giovanni

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Using a Mailbox increases thread callup timing

Postby faisal » Tue Aug 23, 2022 6:18 pm

If you need a queue, you can probably get away without using a mutex if you have a power of 2 sized circular buffer and single producer single consumer design (and atomic word sized reads). That's faster than a chibi object fifo (mainly because of no mutex).

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: Using a Mailbox increases thread callup timing

Postby Giovanni » Tue Aug 23, 2022 6:31 pm

faisal wrote:If you need a queue, you can probably get away without using a mutex if you have a power of 2 sized circular buffer and single producer single consumer design (and atomic word sized reads). That's faster than a chibi object fifo (mainly because of no mutex).


That is a good idea, creating one for your specific use case would save cycles, if the scenario is one-to-one then it could be based on suspend/resume operations.

Anyway look at synchronous messages 1st.

Giovanni

JSStabl
Posts: 78
Joined: Tue Feb 25, 2020 4:06 pm
Has thanked: 3 times
Been thanked: 2 times

Re: Using a Mailbox increases thread callup timing

Postby JSStabl » Thu Aug 25, 2022 10:28 am

Hi Giovanni and faisal,

thank you so much for your help, I really appreciate that!

In the meantime I found another solution that works without any communication and comes with a shortcoming we can live with. That's why I cannot tell right now whether the queue approach works out. Anyway I learned that the mailboxes are not as lightweight as I thought and for this insight I'm grateful!

regarding the pointer: in my production code I don't pass an int but a struct. I tried to keep the snippets lean, but probably went too far :D

all the best!


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 9 guests