Re: gettimeofday() system call timing curiosity

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Fri Mar 08 2002 - 13:54:16 EST


On Fri, 8 Mar 2002, Jamie Lokier wrote:

> Jamie Lokier wrote:
> > It takes the median of 1000 samples of the TSC time taken to do a
> > rdtsc/gettimeofday/rdtsc measurement, then uses that as the threshold
> > for deciding which of the subsequent 1000000 measurements are accepted.
> > Then linear regression through the accepted points.
> >
> > I see a couple of results there which suggests a probable fault in the
> > filtering algorithm. Perhaps it should simply use the smallest TSC time
> > taken as the threshold.
>
> I've looked more closely. Of all the machines I have access to, only my
> laptop shows the anomolous measurements.
>
> It turns out that the median of "time in TSC cycles to do a
> rdtsc+gettimeofday+rdtsc measurement" varies from run to run. It only
> varies between two values, though.
>
[SNIPPED...]

The following program clearly shows that Linux will not return the
same gettimeofday values twice in succession. Since it provably takes
less than 1 microsecond to make a system call on a modern machine,
Linux must be waiting within the gettimeofday procedure long enough
to make certain that the time has changed. This may be screwing up
any performance measurments made with gettimeofday().

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

int main(void);
int main()
{
   struct timeval tv, pv;
   tv.tv_sec = tv.tv_usec = pv.tv_sec = pv.tv_usec = 0;
   for(;;)
   {
        (void)gettimeofday(&tv, NULL);
        if((tv.tv_sec != pv.tv_sec) || (tv.tv_usec != pv.tv_usec))
            printf("sec = %ld usec = %ld\n", tv.tv_sec, tv.tv_usec);
         else
            puts("The same!");
         pv = tv;
   }
   return 0;
}

Cheers,
Dick Johnson

Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).

        Bill Gates? Who?

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Fri Mar 15 2002 - 22:00:09 EST