Showing posts with label rdtsc. Show all posts
Showing posts with label rdtsc. Show all posts

Friday, March 25, 2011

rdtsc in Windows and Linux

Code in Windows, you have to compile it by VC++

#include <intrin.h>
unsigned __int64 rdtsc()
{
  return __rdtsc();
}


Code in Linux 

#include <stdint.h>
#include <stdio.h>

  __inline__ uint64_t rdtsc() {
    uint32_t lo, hi;
    __asm__ __volatile__ (      // serialize
    "xorl %%eax,%%eax \n        cpuid"
    ::: "%rax", "%rbx", "%rcx", "%rdx");
    /* We cannot use "=A", since this would use %rax on x86_64 */
    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
    return (uint64_t)hi << 32 | lo;
  }
int main()
{
    printf("rdtsc is: %llu\n",rdtsc());
    return 1;
}

more detailed information: http://en.wikipedia.org/wiki/Time_Stamp_Counter

Another version in Linux:
/************ Record TSC to SMRAM offset 0xA9000 ****************/
movl $0xA9000, %ebx
rdtsc
movl %eax, (%ebx)
movl %edx, 4(%ebx)

unsigned int lo = *(unsigned int*)(0xA9000);
unsigned int hi = *(unsigned int*)(0xA9004);
unsigned long long beforeConfig = (unsigned long long)hi << 32 | lo;