Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 22 Jul 2004 07:59:40 +1000
From:      John Birrell <jb@cimlogic.com.au>
To:        Bruce Evans <bde@zeta.org.au>
Cc:        current@freebsd.org
Subject:   Re: nanosleep returning early
Message-ID:  <20040721215940.GK22160@freebsd3.cimlogic.com.au>
In-Reply-To: <20040721220405.Y2346@epsplex.bde.org>
References:  <20040721081310.GJ22160@freebsd3.cimlogic.com.au> <20040721102620.GF1009@green.homeunix.org> <20040721220405.Y2346@epsplex.bde.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Jul 21, 2004 at 11:01:20PM +1000, Bruce Evans wrote:
> On Wed, 21 Jul 2004, Brian Fundakowski Feldman wrote:
> > On Wed, Jul 21, 2004 at 06:13:10PM +1000, John Birrell wrote:
> > >
> > > Today I increased HZ in a current kernel to 1000 when adding dummynet.
> > > Now I find that nanosleep regularly comes back a little early.
> > > Can anyone explain why?
> 
> The most obvious bug is that nanosleep() uses the low-accuracy interface
> getnanouptime().  I can't see why the the problem is more obvious with
> large HZ or why it affects short sleeps.  From kern_time.c 1.170:
> 
> % static int
> % nanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
> % {
> % 	struct timespec ts, ts2, ts3;
> % 	struct timeval tv;
> % 	int error;
> %
> % 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
> % 		return (EINVAL);
> % 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
> % 		return (0);
> % 	getnanouptime(&ts);
> 
> This may lag the actual (up)time by 1/HZ seconds.

For any NTP clock adjustment to have an effect, it must happen between
the time when my (crude) test gets the time and when the kernel calls
getnanouptime to fill ts. All the rest of the code in nanosleep1()
refers back to the value of ts. Any errors involved in the tv and hz
conversions would just cause the code to go around the loop and tsleep
more than once.

So, does increasing HZ expose the lower accuracy of getnanouptime() and is
that what I'm seeing?

If so, shouldn't the time interface provided by the kernel to userland
at least be consistently inaccurate? Or put another way, why should
nanosleep be any less accurate than gettimeofday? 


Bruce, have you seen this document: <http://www.dragonflybsd.org/docs/nanosleep/>?
I'm not looking for a critique here. The document talks about the sleep
overruns in various operatingg systems. There is a patch that was applied
to DragonFly which aplies to the FreeBSD code base too.

-- 
John Birrell



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040721215940.GK22160>