Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cpp linux: about rdtsc

Tags:

c++

linux

rdtsc

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?

like image 718
kakush Avatar asked Apr 27 '26 21:04

kakush


1 Answers

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.

like image 127
Necrolis Avatar answered Apr 29 '26 09:04

Necrolis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!