Created
December 25, 2013 09:34
-
-
Save oopsops/8121736 to your computer and use it in GitHub Desktop.
208.5日問題に関連してLinuxでrdtscを出力してみるサンプル gcc -o print_tsc print_tsc.c rdtscの値/(cps...一秒あたりのクロック数*60*60*24)で起動からの日数を概算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "rdtsc.h" | |
#include <stdio.h> | |
int main(void) | |
{ | |
unsigned long long start = rdtsc(); | |
printf("start time [clock] : %llu\n", start ); | |
int n = 5; | |
unsigned long long looplog =rdtsc(); | |
while (n--) { | |
sleep(1); | |
unsigned long long loop = rdtsc(); | |
printf("check time [clock] : %llu\n", loop ); | |
printf(" cps [clock] : %llu\n", loop - looplog ); | |
looplog = loop; | |
} | |
unsigned long long stop = rdtsc(); | |
printf("stop time [clock] : %llu\n", stop ); | |
printf("total time [clock] : %llu\n", stop - start); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef RDTSC_H_ | |
#define RDTSC_H_ | |
inline unsigned long long rdtsc() { | |
unsigned long long ret; | |
__asm__ __volatile__ ("cpuid" : : : "rax", "rbx", "rcx", "rdx"); | |
__asm__ __volatile__ ( \ | |
"rdtsc\n\t" \ | |
"shlq $32, %%rdx\n\t" \ | |
"orq %%rdx, %%rax\n\t" \ | |
"movq %%rax, %0" : "=g" (ret) : : "rax", "rdx"); | |
return ret; | |
} | |
#endif /* RDTSC_H_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment