Question

I have written a very simple C program in an attempt to understand rdtsc in C (Linux).The program is given below.

#include <stdio.h>

static inline unsigned long long tick()
{
        unsigned long long d;
        __asm__ __volatile__ ("rdtsc" : "=A" (d) );
        return d;
}

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

My processor configuration is as below.

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    2
Core(s) per socket:    4
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 30
Stepping:              5
CPU MHz:               1197.000
BogoMIPS:              5862.24
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              8192K
NUMA node0 CPU(s):     0-7

From the output it looks like the processor is 1.2GHz which in my understanding means that there will be 1200 x 10^6 ticks per seconds.

The output of the above program is consistenly 88 when I run it on my machine.What is surprising is that even if I remove ' c = (a + b)*11000;' from between the two ticks still the output is 88.

1) Why is the output not increasing.(it should show slightly higher based on cycles taken to execute he above statement.)

2) Does any of the other parameters listed in cpuinfo above affect this other than CPU MHz.

Was it helpful?

Solution

There is a bug when using rdtsc on X86_64. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21249

tick() must be correctd as follow:

static inline unsigned long long tick()
{

    unsigned long low, high;
    __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high));
    return ((unsigned long long)high << 32) | low);


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