Question

I have the following C file rdtsc.c that demonstrates the use of rdtsc() in C.I have conditional compile for both 64-bit versions and 32-bit versions.

#include <stdio.h>


#ifdef X86_64
static inline unsigned long long tick()
{

    unsigned long low, high;
    __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high));
    return ((unsigned long long)high << 32) | low);
}
#else
static inline unsigned long long tick()
{
     unsigned long long d;
     __asm__ __volatile__ ("rdtsc" : "=A" (d) );
     return d;
}
#endif

int main()
{
        long long res;
        volatile int a = 1;
        volatile int b = 3;
        volatile int c = 0;
        res=tick();
        c = (a + b)*11000;
        res=tick()-res;
        printf("ticks %lld",res);
        return 0;
}

I run the above program in a loop from the shell as shown below.

for i in {1..10} ; do { ./a.out ; printf "\n"; } ; done

The output that I get is given below.

ticks 96
ticks 108
ticks 8698
ticks 8613
ticks 108
ticks 84
ticks 96
ticks 108
ticks 96
ticks 96

Since the same operation is being done.I would certainly expect the values to be more urniform.How do I account for this disparity in the values?

Was it helpful?

Solution

Multitasking should indeed be the answer. When you get higher values (~9000) your process gets preempted. Looking at the values with this in mind they will seem uniform.

OTHER TIPS

On multicore-CPU platform, 'TSC' must be firstly synchronization.

You can use QueryPerformanceCounter and QueryPerformanceFrequency

Reference http://lwn.net/Articles/211051

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top