nanosleep returning early

John Birrell jb at cimlogic.com.au
Wed Jul 21 01:13:13 PDT 2004


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


More information about the freebsd-current mailing list