[Babel-users] Main Loop Frequency

Toke Høiland-Jørgensen toke at toke.dk
Tue Mar 7 14:21:46 UTC 2017


Lorenzo Ghiro <lorenzo.ghiro at studenti.unitn.it> writes:

> Hi everybody!
>
> I would like to know how to schedule an operation (write to file) with
> below-second frequency, any advise?
>
> My strategy up to now has been to check, in the main loop, how many
> time passed from the last operation's occurence. Given that variable
> now is updated in every loop iteration, my code looks like this:
>
> struct timeval now, lastOp;
> gettime(&now);
> gettime(&lastOp);
> ...
> //babel main loop
> while(1) {
> gettime(&now);
> if (microSecDiff(&now, &lastOp)>499000) {
> doThatOperation();
> gettime(&lastOp); 
> }
> ...
> }
>
> However, running a Babel instance on my laptop, I notice that the main
> loop is performed with a strange frequency...sometimes approx 1Hz
> sometimes slower. Consequently, if I perform a check of how many
> microseconds are passed from the last loop iteration I miss the
> below-second deadline that I desire.
>
> The main loop frequency that I notice is standard or anomalous? What I
> notice is related to the "clock monotonicity" enforced with the use of
> gettime()? Or I have this problem just because my laptop has a bad
> clock?

There are lots of reasons why a userspace program can experience
scheduler jitter. Outside of real-time operating systems, there are no
guarantees, so no matter what you do, you'll have to deal with the case
where you miss a deadline.

On Linux, probably the closest you can get to reliable scheduling is to
use the timerfd facility to create a file descriptor that you can wait
on to get fairly accurate scheduling (as good as the kernel can provide
you). See `man timerfd_create`. If you are doing other things in the
loop, incorporating such a timer is not necessarily straight forward,
though. If what you are doing is thread safe, you could do it in a
separate thread, perhaps?

-Toke



More information about the Babel-users mailing list