2008-04-02 07:04:35 pthreads SCHED_FIFO
Mattias K (SWEDEN)
Message: 53533 Hi,
I have 2 threads scheduled with the real time policy SCHED_FIFO. Both threads have the same priority so I would expect them both be running sharing the cpu and when both are finished let the main thread continue. The threads are just incrementing a value in an array, thread 1 is incrementing array index 1, thread 2 index 2 and the main thread is incrementing index 0.
The code (main.cpp):
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
static int a[3] = {0, 0, 0};
#define MAX_COUNT 100000000
static void *my_thread(void *arg)
{
int i;
int ind = (int)arg;
for (i=0; i<MAX_COUNT; i++)
{
a[ind] = i;
}
printf("Thread %d: %d %d %d \n\r",ind,a[0],a[1],a[2]);
pthread_exit(NULL);
}
int main(void)
{
pthread_t thread1, thread2;
pthread_attr_t thread_attr;
struct sched_param param;
int ret;
int i;
ret = pthread_attr_init (&thread_attr);
ret = pthread_attr_setschedpolicy (&thread_attr, SCHED_FIFO);
ret = pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED);
int max_priority = sched_get_priority_max (SCHED_FIFO);
ret = sched_getparam (0, ¶m);
param.sched_priority = max_priority;
ret = pthread_attr_setschedparam (&thread_attr, ¶m);
ret = pthread_create (&thread1, &thread_attr, &my_thread, (void*)1);
//ret = pthread_create (&thread1, NULL, &my_thread, (void*)1);
param.sched_priority = max_priority;
ret = pthread_attr_setschedparam (&thread_attr, ¶m);
ret = pthread_create(&thread2, &thread_attr, &my_thread, (void *)2);
for (i=0; i<MAX_COUNT; i++)
{
a[0] = i;
}
printf("Main: %d %d %d \n\r",a[0],a[1],a[2]);
pthread_exit(NULL);
return 0;
}
Build on PC:
g++ -O0 -g3 -Wall -pthread -o"priorities" main.cpp
Build on bfin:
bfin-uclinux-g++ -O0 -g3 -Wall -pthread -Wl, -elf2flt -o"priorities" main.cpp
Output from PC (Ubuntu 7.04):
>: ./priorities
Thread 1: 0 99999999 98746841
Thread 2: 0 99999999 99999999
Main: 99999999 99999999 99999999
Output from bfin:
>: ./priorities
Thread 1: 0 99999999 0
Thread 2: 0 99999999 99999999
Main: 99999999 99999999 99999999
On the blackfin the scheduler doesn't seem to yield for the other SCHED_FIFO thread which the PC does.
Can anybody double check this, am I implementing it wrong. I'm using the 2007RC3 uClinux dist and the toolchain from the branch (which have some pthread bugs fixed).
regards,
/Mattias
main.cpp
QuoteReplyEditDelete
2008-04-02 16:19:27 Re: pthreads SCHED_FIFO
Robin Getz (UNITED STATES)
Message: 53566 Mattias:
I don't think there are any POSIX requirements that force certain things to be done (threads running in a certain order), so when I have dug into this in the past, it was pretty much some archs share more fair than others.
-Robin
QuoteReplyEditDelete
2008-04-03 04:52:19 Re: pthreads SCHED_FIFO
Mattias K (SWEDEN)
Message: 53597 Hi,
Thanks for the response.
I have also read a bit about the topic but are not much clearer. It is quite hard to debug that the fifo threads behave as I want. And that is that they should run as soon as they are runnable (maximum time I should have to wait is the time to the next scheduler_tick (which should be less the 1ms away on a 1000 Hz system)).
Anybody know about this topic and/or have blackfin systems using fifo threads (SCHED_FIFO)
regards,
/Mattias
QuoteReplyEditDelete
2008-04-03 05:32:09 Re: pthreads SCHED_FIFO
Yi Li (CHINA)
Message: 53598 Maybe you can try SCHED_RR.
QuoteReplyEditDelete
2008-04-03 05:40:39 Re: pthreads SCHED_FIFO
Yi Li (CHINA)
Message: 53599 BTW, on linux, you may also change time quantum of SCHED_RR scheduling by setting the nice value (http://kerneltrap.org/man/linux/man2/sched_rr_get_interval.2).
QuoteReplyEditDelete
2008-04-04 06:26:59 Re: pthreads SCHED_FIFO
Mattias K (SWEDEN)
Message: 53637 Hi,
In my system I only have one real time thread, the other are default ones. So i think SCHED_FIFO suits my need better.
Unfortunately I see large scheduler latencies (which I first thought was a thread problem but which I now think is a scheduler latency problem). I will try to use xenomai and see if that helps.
/Mattias
QuoteReplyEditDelete
2008-04-08 03:54:45 Re: pthreads SCHED_FIFO
Yi Li (CHINA)
Message: 53811 How much is the schedule latency?
I tested using the test case in uclinux-dist/user/blkfin-test/latency-test using 2008r1, the schedule latency I saw on BF537 is less than 20ms.
QuoteReplyEditDelete
2008-04-08 04:01:30 Re: pthreads SCHED_FIFO
Yi Li (CHINA)
Message: 53813 And could you please post your test case for testing schedule latency? So I can have a try on my side.
QuoteReplyEditDelete
2008-11-13 09:43:12 Re: pthreads SCHED_FIFO
Mads le maire (NORWAY)
Message: 65239
I experience similiar problems on my stamp board. The scheduling delay can be up to 23ms, using a general purpose timer as reference. Setting scheduling policies and priority does not seem to affect this. I have programmed threaded applications on MMU arm (SA1100 at 198 mHz) using linux 2.4.18 and comparable latency measured with hardware timer, was never seen higher than 10ms under load, even if this processor was running on a much lower clock. Is there any reason for this long lattency in uClinux? Is the scheduling policies for pthreads implemented in the blackfin architecture. (I am aware that the realtime patches is not implemented.)