Possible Duplicate:
Linux clock_gettime(CLOCK_MONOTONIC) strange non-monotonic behavior
After experiencing some problems with Erlang crashing, I've written a program that calls clock_gettime(CLOCK_MONOTONIC, &ts) repeatedly and checks to see if it ever goes backwards, and unfortunately it sometimes does go backwards.
This is the test program I'm using:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
struct timespec ts_start;
struct timespec ts_end;
int i;
clock_gettime(CLOCK_MONOTONIC, &ts_start);
clock_gettime(CLOCK_MONOTONIC, &ts_end);
for(i = 0; i<10000000; i++) {
if(ts_end.tv_sec <= ts_start.tv_sec
&& ts_end.tv_nsec < ts_start.tv_nsec) {
printf("ERROR!\n");
return 1;
}
ts_start.tv_sec = ts_end.tv_sec;
ts_start.tv_nsec = ts_end.tv_nsec;
clock_gettime(CLOCK_MONOTONIC, &ts_end);
}
printf("OK\n");
return 0;
}
On my Hyper-V VM (kernel 2.6.18-238.12.1.e15, have tried others) it occasionally outputs ERROR, but on a physical machine it always outputs OK.
Any idea why CLOCK_MONOTONIC wouldn't be monotonic?