3

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?

palacsint
  • 28,416
  • 10
  • 82
  • 109
kakush
  • 3,334
  • 14
  • 47
  • 68

1 Answers1

1

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.

Necrolis
  • 25,836
  • 3
  • 63
  • 101
  • I was surprised by the `.byte` way of doing it as well, but it seems lifted from here - http://www.mcs.anl.gov/~kazutomo/rdtsc.html . I wonder why they do it like that? – mattjgalloway Jan 16 '12 at 14:37
  • @mattjgalloway: I remember older inline assemblers would sometimes require inline byte emission because they didn't recognize the mnemonic, that that was LONG ago, there isn't any advantage other than confusing the reader – Necrolis Jan 16 '12 at 14:41
  • Yeh that would make sense :-). – mattjgalloway Jan 16 '12 at 14:50