Re: gettimeofday problem

Jan Hudec
2002-06-26T15:32:42Z

On Tue, Jun 25, 2002 at 05:17:52AM -0400, Christian Robert wrote:
> John Alvord wrote:
> > Maybe this is the result of floating point rounding errors. Floating
> > point is notorious for occaisional strange results. I suggest redoing
> > the test program to keep all results in integer and seeing what
> > happens...
> You were close.
> Programming error on my part.

What about comparing the struct timeval things directly? There is even a
timercmp macro for that (well I noticed that in the manpage when I
have olrady had the test written; the macro can only do sharp comparsions).

Something like this:
(I am now running it on three machines - Athlon 850, Pentium 1500 and dual
Pentium III 500 - all seem to be OK so far)

#include<stdio.h>
#include<errno.h>
#include<sys/time.h>
#include<signal.h>

volatile int loop = 1;

void sigint(int foo) {
loop = 0;
}

int main(void) {
unsigned long long cnt = 0, bcnt = 0, ecnt = 0;
struct timeval old, new = {0, 0};

signal(SIGINT, sigint);
while(loop && cnt < (1LLU<<54)) {
cnt++;
old = new;
if(gettimeofday(&new, NULL)) {
ecnt++;
printf("Error #%llu: count=%llu"
" error/count=0.%04llu errno=%i (%s)\n",
ecnt, cnt, (10000*ecnt)/cnt, errno,
sys_errlist[errno]);
continue;
}
if((new.tv_sec < old.tv_sec) || ((new.tv_sec == old.tv_sec) && (new.tv_usec < old.tv_usec))) {
bcnt++;
printf("Skew #%llu: count=%llu errors=%llu"
" skew/good count=0.%04llu, new=(%li,"
" %li) old=(%li, %li)\n", bcnt, cnt,
ecnt, (10000*bcnt)/(cnt-ecnt),
new.tv_sec, new.tv_usec, old.tv_sec,
old.tv_usec);
}
}

printf("Counted %llu, errors %llu (0.%04llu), skews %llu"
" (0.%04llu)\n", cnt, ecnt, (10000*ecnt)/cnt, bcnt,
(10000*bcnt)/(cnt-ecnt));
return 0;
}

-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <bulb@ucw.cz>