Re: [PATCH] Intel clock speed detection

Robert Woodcock (rcw@oz.net)
Sat, 27 Dec 1997 20:13:26 -0800 (PST)


Phile Brutsche wrote:
>On another note, I just looked at your patch. It uses the rdtsc
>instruction; this instruction does not exist on anything less than a
>Pentium. I'm not sure what the kernel would do on '486 or '386, but it
>would'nt be pretty. A better solution for this would be to time the bsf
>instruction on those chips. I have source in C and assembly if you need
>help.

Can you mail that to me at the return address?

Currently I'm using this userspace code I pieced together myself (well,
not the rdtsc function, I ripped that from the I/O Port Programming HOWTO)
which is so consistent that it's making me wonder if my motherboard clock
chip is a little bit off and is actually running my CPU at 99.57mhz
instead of 100. (I gave this program to several other people with pentiums
and it seems to consistently report either high and low depending on the
system.)

mercury:~$ (while :; do mhz.estimate; done)
99.569984 MHz processor.
99.569979 MHz processor.
99.570001 MHz processor.
99.569946 MHz processor.

Kinda makes you wonder about those motherboard benchmarks where all the
motherboards are within a few tenths of a percent of each other. :)

Compile as usual, gcc mhz.estimate.c -o mhz.estimate.

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

/* returns number of clock cycles since last reboot */
__inline__ unsigned long long int rdtsc()
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}

main ()
{
struct timezone tz;
struct timeval tvstart, tvstop;
unsigned long long int cycles[2]; /* gotta be 64 bit */
unsigned int microseconds; /* total time taken */

bzero(&tz, sizeof(tz));

/* get this function in cached memory */
gettimeofday(&tvstart, &tz);
cycles[0] = rdtsc();
gettimeofday(&tvstart, &tz);

/* we don't trust that this is any specific length of time */
sleep(1);

cycles[1] = rdtsc();
gettimeofday(&tvstop, &tz);
microseconds = ((tvstop.tv_sec-tvstart.tv_sec)*1000000) +
(tvstop.tv_usec-tvstart.tv_usec);

printf("%f MHz processor.\n",
(float)(cycles[1]-cycles[0])/microseconds);
}

--
Robert Woodcock - robert@olex.com
All I want is a warm bed and a kind word and unlimited power.
		-- Ashleigh Brilliant