c - How to determine how many times Time Stamp Counter (TSC) is reset -
i need measure time based on time stmp counter (tsc) reasons. read tsc, using code below:
#include <stdio.h> #include <inttypes.h> inline volatile uint32_t rdtsc32() { register uint32_t tsc asm("eax"); asm volatile (".byte 15, 49" : : : "eax", "edx"); return tsc; } inline volatile uint64_t rdtsc64() { register uint64_t tsc asm("rax"); asm volatile (".byte 15, 49" : : : "rax", "rdx"); return tsc; } int main() { while (1) { printf("%" priu64 "\n", rdtsc64()); } }
when i've tested it, works fine. except 1 thing. when reaches maximum counter value (some value higher 4,256,448,731, in environment,) counter value gets reset 0 , keeps going on.
in situation, there way see how many times tsc has been reset?
for example, code below not print correct time difference:
#include <stdio.h> int main() { long long start, end; start = rdtsc64(); // long long works end = rdtsc64(); printf("%lld \n", end - start); }
the time stamp counter 64-bit, see statement on wipedia page:
the time stamp counter (tsc) 64-bit register present on x86 processors since pentium.
for reason you're getting truncated value 32 bits, why wraps. 64-bit value need 146 years of continuous counting @ 4 ghz wrap.
your code seems want use both eax
, edx
hold 2 32 bit halves, expected. must go wrong when moving values single c variable. believe snippet you're using gcc; perhaps that's no longer compiler?
inspect generated assembly, , check compiler docs proper intrinsic function instead. this question has answers, compiler-specific assembly.
Comments
Post a Comment