Page 1 of 2

Thread performance

Posted: Wed Oct 12, 2011 9:19 pm
by DrunkenDonkey
I designed my program around having some separate threads in different modules that would read stuff from ports and process as needed. Today I decided to test with something different than few bytes around, just the serial and found out that I'm doing something very wrong. Using the latest build, just updated I have:
main task that is doing:

Code: Select all

   
while (TRUE)
{
   chThdSleepMilliseconds(500);
}


and serial task, with HIGHPRIO that does

Code: Select all

         
static u8 cmdBuf[256];
static u16 cmdLen=0;
while (TRUE)
{
   u16 read=sdReadTimeout(&SD2,&cmdBuf[cmdLen],sizeof(cmdBuf),1000);
   cmdLen+=read;
   if(serialDataCB(cmdBuf,cmdLen))
   {
      cmdLen=0;
      break;
   }
}


serialDataCB in question just checks if the packet is complete and then executes the command, otherwise returns 0

Code: Select all

u16 serialDataCB(u8 *buffer, u16 length)
{
   if(length<4)
      return 0;

   u16 cmdLen=(buffer[2]<<8)|buffer[3];
   if(length<(4+cmdLen))
      return 0;
...
}


Now... this code can't keep up with 115200 serial communication and can't get 134 bytes without losing several on the way. Am I on the wrong track and threads can't be used this way? If I increase the serial buffer from 16 to some higher value, I start getting the data, which means the code is executing slow enough to fill up those 16 byte (thats... ~1.4ms) meanwhile? And thats only with 1 thread (besides idle) running, but I plan to have 3 dealing with serial communication, one i2c, one spi... :(

I also haven't touched anything in the init about the frequency, no idea actually what the default cpu frequency is.

Re: Thread performance

Posted: Wed Oct 12, 2011 9:32 pm
by Giovanni
Hi,

sdReadTimeout() does not return until *all* the specified amount is read (256 bytes) or a timeout occurs (after 1 second), if you send small packets the thread will have to wait one second before processing the received amount.

This explains the low throughput.

If you are waiting for 4 bytes packets then specify 4 as size.

Giovanni

Re: Thread performance

Posted: Wed Oct 12, 2011 9:36 pm
by DrunkenDonkey
Thanks, but I put that 1000 just to test the timeout, was with TIMEOUT_INFINITE before, tried just using sdGet(&SD2) to just take them one by one, but got only 60ish from 134. From the host side, I send 134 bytes packet, from the STM side, even with that timeout of 1000, I get at best 124, and if I increase SERIAL_BUFFERS_SIZE I start to get the whole packet. Sure, increasing is a workaround, but I just don't want to go there, as the bottleneck still stays somewhere :(

Re: Thread performance

Posted: Wed Oct 12, 2011 9:49 pm
by Giovanni
You should use another way: specify TIME_IMMEDIATE as timeout. This makes sdReadTimeout() just return all the data present in the buffer in that moment, without waiting.
Of course you don't want to poll the buffer in order to get data, you have to wait on the serial driver event source, when an event is broadcasted by the driver then you use sdReadTimeout() to empty the whole buffer and then go into waiting the event again.

1) Register the receiver thread on the SD2 event source using chEvtRegisterMask(), use 1 (bit 0) as mask for example.
2) Wait for an event using chEvtWaitOne() or chEvtWaitOneTimeout(), the thread waits until the buffer begins to fill and the event is broadcasted.
3) Read all data in the buffer using sdReadTimeout() with TIME_IMMEDIATE, this is not blocking.
4) Process data.
5) goto 2 again.

This process is entirely asynchronous and does not require timeouts (you can timeout from the event wait if you want) and is independent from the buffer size if you read the data fast enough.

Note that the same thread could wait on multiple events, you could process everything in one thread asynchronously if you want. This is one of the application models supported by ChibiOS: event driven.

Just another note, using u16 saves you nothing in 32bits ARM application, often it is slower than a 32 bit word and there are no memory savings either unless you use structured data (structs or arrays).

Giovanni

Re: Thread performance

Posted: Wed Oct 12, 2011 9:52 pm
by DrunkenDonkey
Giovanni, I just modified the code to use just timeout reads to see if it goes any better:

Code: Select all

cmdLen=0;

cmdLen+=sdReadTimeout(&SD2,&cmdBuf[cmdLen],4,1000);
if(cmdLen!=4)
   break;

u16 toRead=(cmdBuf[2]<<8)|cmdBuf[3];
cmdLen+=sdReadTimeout(&SD2,&cmdBuf[cmdLen],toRead,1000);
if(cmdLen!=(4+toRead))
   break;


In short it reads packet header, sees what the data is there, then reads the body too. Results with packed size of 204 (4 header, 200 data)

Code: Select all

SERIAL_BUFFERS_SIZE=16, read 180/204
SERIAL_BUFFERS_SIZE=20, read 186/204
SERIAL_BUFFERS_SIZE=30, read 198/204
SERIAL_BUFFERS_SIZE=32, read 203/204
SERIAL_BUFFERS_SIZE=34, read 204/204 - we got a winner :D


I... just didn't expect the cpu to work that slow and dunno if this is normal or not.

I will modify it again to use events, but something tells me if it can't handle direct reads with timeouts, events won't do any better...

Re: Thread performance

Posted: Wed Oct 12, 2011 10:01 pm
by Giovanni
There is a benchmark in the test suite (test 11.9) that writes/reads characters from a queue one at time, it is able to write/read 651460 bytes per second on the STM32 at 72MHz. I don't think this is the problem.

Giovanni

Re: Thread performance

Posted: Wed Oct 12, 2011 10:31 pm
by DrunkenDonkey
:(
I tried altering cpu frequency, but only managed to kill the serial port (tho threads were working... twice as fast). Tried with events too, got quite horrible results:

Code: Select all

         
cmdLen=0;

EventListener elisten;
chEvtRegisterMask(&SD2.event,&elisten,1);
while (TRUE)
{
   chEvtWaitOneTimeout(1,1000);
   cmdLen+=sdReadTimeout(&SD2,&cmdBuf[cmdLen],sizeof(cmdBuf)-cmdLen,TIME_IMMEDIATE);
   chEvtClearFlags(ALL_EVENTS); //with or without that one
   if(cmdLen>=4)
   {
      u16 toRead=(cmdBuf[2]<<8)|cmdBuf[3];
      if(cmdLen>=(toRead+4))
         break;
   }
}


That receives only the size of the serial buffer, in the current case, 34 bytes and not a single one more. I'll be calling it a day and bang my head again tomorrow ;)

Re: Thread performance

Posted: Thu Oct 13, 2011 7:53 am
by Giovanni
You should not explicitly use chEvtClearFlags(), flags are automatically cleared inside chEvtWaitTimeout(), by clearing them you create a race condition, flags could be cleared without having been served yet.

Anyway, hard to help without seeing the whole, do you have threads at the same priority level that use much CPU? this could delay the processing of the receiver thread.

Giovanni

Re: Thread performance

Posted: Thu Oct 13, 2011 10:07 pm
by DrunkenDonkey
Oh for... I just had the time to do some tries (yeah, had some party, bit drunk :D ), and found what is it... obviously I decided it is a good idea to enable checks in the chconf.h for the sake of inexperienced me getting some warnings in case I try to do some crap. I take that back, it is a bad idea to do so ;)
Sorry Giovanni, even in the face of certain death I could not remember I did that in the early days (and still don't, but new project with the comm code only and side by side comparison revealed that)

Re: Thread performance

Posted: Fri Oct 14, 2011 11:04 pm
by barthess
hi DrunkenDonkey,
I successfully used SD with my xbee modems on 115200. And now use it for GPS reading. Roots of your problems somewhere else. Try to investigate CPU usage time of all threads with Eclipse plugin.