There were (at least) three sample implementations of a
C-function to read the Pentium's time-stamp counter. I
originally implemented two and when I discovered a problem
with the results of one, I implemented the third which was
supposed to be an improved version of the second. The
results of the first method are exactly what one would
expect from a nominally P-MMX 233 but the results from the
2nd and 3rd are not. Since my competence in assembly
language ended with the demise of the 8088's real-mode, I'd
hope that someone could point out the problem with these
implementations. [AFAIK, I created all three functions
with a simple cut-and-paste]. The program's output is
included in the comment-block at the end.
/********************************************************
* Get the minimum #include files necessary to run
********************************************************/
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
/********************************************************
* This is Method1 in the following
********************************************************/
__inline__ unsigned long long int method1(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
/********************************************************
* This is Method2 in the following
********************************************************/
__inline__ unsigned long long int method2(void)
{
unsigned long long int x;
__asm__("rdtsc\n\t"
"mov %%edx, %%ecx\n\t"
:"=A" (x));
return x;
}
/********************************************************
* This is Method2A in the following
********************************************************/
__inline__ unsigned long long int method2A(void)
{
unsigned long long int x;
/* the CPUID instruction is mostly irrelevant; it just
flushes the pipeline for good measure */
__asm__("cpuid\n\t"
"rdtsc\n\t"
"mov %%edx, %%ecx\n\t"
:"=A" (x));
return x;
}
/********************************************************
*********************************************************
* This is the test program *
*********************************************************
********************************************************/
int main()
{
/********************************************************
* Define starting(_1) and ending(_2) time for each
* method
********************************************************/
long long int method2_1
, method2_2
, method1_1
, method1_2
, method2A_1
, method2A_2;
/********************************************************
* Read the starting times
********************************************************/
method1_1 = method1();
method2_1 = method2();
method2A_1= method2A();
/********************************************************
* wait for a known period of time
********************************************************/
sleep(1);
/********************************************************
* read the ending times after the wait
********************************************************/
method1_2 = method1();
method2_2 = method2();
method2A_2= method2A();
/********************************************************
* print out the results
********************************************************/
printf( "method1 : %10lld - %10lld = %10lld \n" ,
method1_2 , method1_1 , method1_2 - method1_1 );
printf( "method2 : %10lld - %10lld = %10lld \n" ,
method2_2 , method2_1 , method2_2 - method2_1 );
printf( "method2A : %10lld - %10lld = %10lld \n" ,
method2A_2, method2A_1, method2A_2- method2A_1);
printf( " -- also m2_1 - m1_1 = %10lld\n" , method2_1 -
method1_1 );
printf( " -- and m2A_1- m2_1 = %10lld\n" , method2A_1 -
method2_1 );
printf( " finally m2A_1- m1_1 = %10lld\n" , method2A_1 -
method1_1 );
return(0);
}
/********************************************************
* SAMPLE OUTPUT *
*********************************************************
method1 : 23573589435592 - 23573353927542 = 235508050
method2 : 23573353927556 - 23573353927556 = 0
method2A : 23573353927607 - 23573353927607 = 0
-- also m2_1 - m1_1 = 14
-- and m2A_1- m2_1 = 51
finally m2A_1- m1_1 = 65
*********************************************************
* THIS PATTERN IS COMPLETELY REPEATABLE *
* The order of the execution doesn't matter *
********************************************************/
-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
--
For more information on Real-Time Linux see:
http://www.rtlinux.org/