Going through this thread, I understand that clock_gettime () and clock_getres () are an opportunity to implement QueryPerformanceCounter and QueryPerformanceFrequency in Linux environments.
Many examples suggest reading the value returned by clock_gettime and using it. But it will only be the number of nanoseconds, not the number of ticks! Thus, the only way to get the number of ticks in Linux is to use a combination of clock_gettime () and clock_getres (). Please correct me if I am wrong.
So, I implemented my QueryPerformanceCounter (and it seems very ugly!) So I wanted to know if there are better ways to implement QueryPerformanceCounter for Linux. My code below is
__int64 QueryPerformanceCounter()
{
__int64 nsec_count, nsec_per_tick;
struct timespec ts1, ts2;
if (clock_gettime(CLOCK_MONOTONIC, &ts1) != 0) {
printf("clock_gettime() failed");
return -1;
}
nsec_count = ts1.tv_nsec + ts1.tv_sec * nsec_per_sec;
if (clock_getres(CLOCK_MONOTONIC, &ts2) != 0) {
LOG_ERROR("clock_getres() failed");
return -1;
}
nsec_per_tick = ts2.tv_nsec + ts2.tv_sec * nsec_per_sec;
return (nsec_count / nsec_per_tick);
}
Thank!