nanosleep returning early

From: John Birrell <jb_at_cimlogic.com.au>
Date: Wed, 21 Jul 2004 18:13:10 +1000
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?

I would have expected that the *overrun* beyond the required time to vary,
but never that it would come back early.

------------------
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
    struct  timespec    rmt;
    struct  timespec    rqt;
    struct  timeval     abstime;
    struct  timeval     curtime;

    gettimeofday(&curtime,NULL);
    rqt.tv_sec  = 5;
    rqt.tv_nsec = 50000000;
    abstime.tv_sec  = curtime.tv_sec  + rqt.tv_sec;
    abstime.tv_usec = curtime.tv_usec + rqt.tv_nsec / 1000;
    if (abstime.tv_usec >= 1000000) {
        abstime.tv_sec  += 1;
        abstime.tv_usec -= 1000000;
	}
	printf("curtime %ld.%06ld\n",curtime.tv_sec,curtime.tv_usec);
	printf("rqt     %ld.%09ld\n",(long) rqt.tv_sec,rqt.tv_nsec);
    if (nanosleep(&rqt,&rmt) != 0) {
	} else if (gettimeofday(&curtime,NULL) != 0) {
	} else if (curtime.tv_sec < abstime.tv_sec || (curtime.tv_sec == abstime.tv_sec && curtime.tv_usec < abstime.tv_usec)) {
		printf("Early: curtime %ld.%06ld abstime %ld.%06ld\n",curtime.tv_sec,curtime.tv_usec,abstime.tv_sec,abstime.tv_usec);
	}
    return(0);
}
------------------

-- 
John Birrell
Received on Wed Jul 21 2004 - 06:13:13 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:02 UTC