How does this code calculate the number of CPU cycles elapsed?
Asked Answered
E

1

10

Taken from this SO thread, this piece of code calculates the number of CPU cycles elapsed running code between lines //1 and //2.

$ cat cyc.c 
#include<stdio.h>

static __inline__ unsigned long long rdtsc(void)
{
  unsigned long long int x;
     __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
     return x;
}

int main() {
    unsigned long long cycles = rdtsc(); //1
    cycles = rdtsc() - cycles;           //2
    printf("Time is %d\n", (unsigned)cycles);
    return 0;
}

$ gcc cyc.c -o cyc
$ ./cyc
Time is 73
$ ./cyc
Time is 74
$ ./cyc
Time is 63
$ ./cyc
Time is 73
$

How does the rdtsc() function work?

Escalante answered 9/10, 2010 at 22:22 Comment(0)
S
11

The function executes the x86 instruction RTDSC, which happens to have an opcode of 0x0f, 0x31. The processor keeps track of clock cycles internally, and this reads that number.

Of course, this only works on x86 procs, other processors will need different instructions.

The Time Stamp Counter is a 64-bit register present on all x86 processors since the Pentium. It counts the number of ticks since reset. Instruction RDTSC returns the TSC in EDX:EAX. Its opcode is 0F 31.[1] Pentium competitors such as the Cyrix 6x86 did not always have a TSC and may consider RDTSC an illegal instruction. Cyrix included a Time Stamp Counter in their MII.

http://en.wikipedia.org/wiki/Time_Stamp_Counter

Schram answered 9/10, 2010 at 22:28 Comment(3)
What does the "=A" part do?Escalante
It also have problems on some molticore AMD processors, because each of them has its own counter that are not synchronized, so if you happen to run on different cores when you take the timestamps, you could have surprises. So, asking the OS for this thread to run on the same core would be a good idea.Wordsmith
@Lazer, it's a little GCC magic that says that the values in EDX:EAX should be placed into the C variable x - gcc.gnu.org/onlinedocs/gcc/…. Look at the "A" entry under the Intel section.Schram

© 2022 - 2024 — McMap. All rights reserved.