Re: timerfd waking up before timer really expires

From: Clemens Ladisch
Date: Wed Mar 04 2015 - 03:13:17 EST


Lucas De Marchi wrote:
> I was debugging my application and noticed that a timerfd event was being
> triggered *before* the timer expires.
>
> I reduced the scope of the program to test a single timerfd and measure the
> difference in the result of clock_gettime() between two reads.
>
> loop_time_fd = setup_timerfd(interval, 0);
> do {
> read(loop_time_fd, &events, sizeof(events));
> ... = now_usec();
> } while (1);
>
> For whatever interval I configure and 10000 iterations, what I'm seeing in the
> elapsed vector are values like
>
> interval +- 70usec

Let us assume that the timer itself is perfectly accurate, and that all
wakeups of your program are immediately when the timerfd becomes ready,
except for one iteration, where there is a scheduling delay. Then the
measured interval before this delayed wakeup is longer, while the
measured interval after this wakeup is shorter by the same amount.

To detect early wakeups, you must not check whether the interval between
two consecutive wakeups is too short, but whether the interval between
a wakeup and the time when the timerfd was actually started is shorter
than N Ã the timer interval.


Regards,
Clemens
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/