cpp linux: about rdtsc

1.2k views Asked by At

I'm using the following function in my code:

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

Does this function return number of ticks since last boot? Where can I find documentation about this function?

1

There are 1 answers

3
Necrolis On

RDTSC gets the number of cpu cycles since last reset, see wikipedia.

the Intel Instruction Set Manual Vol 2A & B, as a more trusted source:

The processor monotonically increments the time-stamp counter MSR every clock cycle and resets it to 0 whenever the processor is reset. See “Time Stamp Counter” in Chapter 16 of the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 3B, for specific details of the time stamp counter behavior.

as a side note: you shouldn't be emitting bytes, rather use the actual RTDSC instruction mnemonic, makes it far more readable.