Current and correct CPU clock and asm("cpuid")

Peter Senna Tschudin peter.senna at gmail.com
Sun Oct 2 21:27:21 EDT 2011


Dear list members,

I'm following:

http://people.virginia.edu/~chg5w/page3/assets/MeasuringUnix.pdf

And I'm trying to measure executing time of simple operations with RDTSC.

See the code below:

#include <stdio.h>
#define CPU_THOUSAND_HZ 800000
typedef unsigned long long ticks;
static __inline__ ticks getticks(void) {
	unsigned a, d;
	asm("cpuid");
	asm volatile("rdtsc" : "=a" (a), "=d" (d));
	return (((ticks)a) | (((ticks)d) << 32));
}

void main() {
	ticks tickBegin, tickEnd;
	tickBegin = getticks();

	// code to time

	tickEnd = getticks();
	double time = (tickEnd-tickBegin)/CPU_THOUSAND_HZ;

	printf ("%Le\n", time);
}

How can the C code detects the correct value for CPU_THOUSAND_HZ? The
problems I see are:
 - It is needed to collect the information for the CPU that will run
the process. On Core i7 processors, different cores can run at
different clock speed at same time.
 - If the clock changes during the execution of process, what should
it do? When is the best time for collecting the clock speed?

The authors of the paper are not sure about the effects of
"asm("cpuid");" Does it ensure that the entire process will run on the
same CPU, and will serialize it avoiding out of order execution by the
CPU?

Thank you very much! :-)

Peter


-- 
Peter Senna Tschudin
peter.senna at gmail.com
gpg id: 48274C36



More information about the Kernelnewbies mailing list