Re: a faster way to gettimeofday? rdtsc strangeness

From: Jamie Lokier (lk@tantalophile.demon.co.uk)
Date: Thu Mar 07 2002 - 20:32:23 EST


H. Peter Anvin wrote:
> > Can /proc/cpuinfo really be trusted in figuring out how long a cycle is?
>
> It uses RDTSC, so yes.

Yes and no. The kernel's measurement isn't as accurate as it could be.

I have some code which calibrates TSC cycles against gettimeofday().

Considering that gettimeofday() works by using the kernel's frequency
estimate that it shows in /proc/cpuinfo, the results from the code
should match /proc/cpuinfo very precisely ndeed.

However, over a million gettimeofday() calls, the frequency of the 100Hz
tick would tend to dominate the result, showing any error in the
kernel's estimate which appears in /proc/cpuinfo (which is derived from
measuring the length of a 100Hz tick in TSC cycles).

With 10^6 gettimeofday() calls, and linear regression through the set of
measurements when an interrupt didn't happen (checked by reading TSC
before and after the system call and filtering on the minimum), I get
superb results on a FreeBSD box:

        Kernel reports clock frequency: 746.154 MHz
        Measured clock frequency: 746.154 MHz

On various Linux boxes, however:

        Kernel reports clock frequency: 596.924 MHz
        Measured clock frequency: 596.929 MHz

        Kernel reports clock frequency: 866.708 MHz
        Measured clock frequency: 866.675 MHz

        Kernel reports clock frequency: 664.597 MHz
        Measured clock frequency: 664.582 MHz

        Kernel reports clock frequency: 366.601 MHz
        Measured clock frequency: 366.584 MHz

As you can see, the kernel consistently overestimates the CPU clock
frequency, or more precisely it overestimates the number of cycles in a
"100Hz" system clock tick. This is something that could be improved in
the kernel code, as FreeBSD demonstrates. Note that the clock frequency
probably varies by a similar order of magnitude to this error due to
thermal and power variation anyway. But it would be nice if the
measurements matched as they do in FreeBSD!

I have one of those laptops that Alan mentioned which does change the
CPU clock rate. It's a Toshiba 4070CDT, here running in fast then slow
mode:

        Kernel reports clock frequency: 366.601 MHz
        Measured clock frequency: 366.584 MHz

        Kernel reports clock frequency: 366.601 MHz
        Measured clock frequency: 184.503 MHz

It can switch between fast and slow modes while a program is running,
which occurs either when you press a special key combination, or the
mains power cuts off.

enjoy,
-- Jamie
-
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 : Thu Mar 07 2002 - 21:01:13 EST