Sunday, June 28, 2009

Soft realtime

Recently we wanted to try out some hard real time stuff. When we attempted it, the results were not satisfactory. Today, I just tried a program to see how a soft real time program performs against a normal program.
(Notice the use of SCHED_FIFO and a priority


#include <stdio.h>
#include <sched.h>
#include <sys/time.h>

int main()
{

int retval=-1;
struct sched_param sp;
struct timeval tv;
int old=0;


#if defined(SFT_RLT)

sp.sched_priority=20;
retval=sched_setscheduler(0,SCHED_FIFO,&sp);
printf("Ret= %d\n",retval);

#endif

while(1)
{

gettimeofday(&tv,NULL);
old=(tv.tv_usec-old)<0?1000*1000+tv.tv_usec-old:tv.tv_usec-old;
printf("%u %u %u \n",tv.tv_sec, tv.tv_usec,old);
old=tv.tv_usec;
usleep(1000*200);

}

return 0;
}


I compiled two binaries from this

gcc -DSFT_RLT rt.c -o rt
gcc rt.c -o nrt

As one can easily see, the first one is a soft realtime program and the second is a normal program. I ran these programs under two scenarios. One, when the system was relatively free; two, when the system was loaded. This is what I did and observed.

Normal scenario (X, FVWM, Terminal, bittorrent and Firefox)



./rt./nrt

1246142672 137029 200069
1246142672 337101 200072
1246142672 537171 200070
1246142672 737242 200071
1246142672 937312 200070
1246142673 137344 200032
1246142673 337413 200069
1246142673 537486 200073
1246142673 737557 200071
1246142673 937627 200070

1246142672 004983 200072
1246142672 205055 200072
1246142672 405127 200072
1246142672 605199 200072
1246142672 805265 200066
1246142673 005331 200066
1246142673 205402 200071
1246142673 405538 200136
1246142673 605604 200066
1246142673 805668 200064

Then I tried running the same under loaded condition. (Just played a movie using mplayer. The other processes which were earlier running were running now also)



./rt./nrt

1246146025 832916 200046
1246146026 032951 200035
1246146026 233010 200059
1246146026 433064 200054
1246146026 633097 200033
1246146026 833150 200053
1246146027 033203 200053
1246146027 233255 200052
1246146027 433286 200031
1246146027 633317 200031
1246146027 833368 200051

1246146025 803540 200145
1246146026 003647 200107
1246146026 203734 200087
1246146026 403823 200089
1246146026 603918 200095
1246146026 804006 200088
1246146027 004127 200121
1246146027 204204 200077
1246146027 404257 200053
1246146027 604314 200057
1246146027 804368 200054

I guess the difference in result is noticeable.This is easier to achieve compared to hard rt, since there is no kernel patches needed. When we make a break through with hard rt, I will post again

No comments: