From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759976AbbCDINO (ORCPT ); Wed, 4 Mar 2015 03:13:14 -0500 Received: from smtprelay05.ispgateway.de ([80.67.31.94]:39483 "EHLO smtprelay05.ispgateway.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759510AbbCDINM (ORCPT ); Wed, 4 Mar 2015 03:13:12 -0500 X-Greylist: delayed 417 seconds by postgrey-1.27 at vger.kernel.org; Wed, 04 Mar 2015 03:13:12 EST Message-ID: <54F6BCF3.4010404@ladisch.de> Date: Wed, 04 Mar 2015 09:06:11 +0100 From: Clemens Ladisch User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: Lucas De Marchi , lkml CC: Thomas Gleixner , Steven Rostedt Subject: Re: timerfd waking up before timer really expires References: In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Df-Sender: bGludXgta2VybmVsQGNsLmRvbWFpbmZhY3Rvcnkta3VuZGUuZGU= Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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