I need to measure time based on Time Stmp Counter (TSC) for some reasons. To read TSC, I am using the 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, it works fine. Except one thing. When it reaches the maximum counter value (some value higher than 4,256,448,731, in my environment,) the counter value gets reset to 0 and keeps going on.
In this situation, is there any way to see how many times TSC has been reset?
For example, the code below does not print a correct time difference:
#include <stdio.h>
int main() {
long long start, end;
start = RDTSC64();
// long long works to do
end = RDTSC64();
printf("%lld \n", end - start);
}