#include #include #include #include #include #include #include #include #include #include // for i in $(seq 50); do provoke > /dev/null & done static volatile int run = 1; static void ctrl_c_handler(int sig) { run = 0; } static uint64_t milliseconds(void) { struct timespec time; if (clock_gettime(CLOCK_MONOTONIC, &time) == 0) { return ((uint64_t)time.tv_sec * 1000ULL) + (time.tv_nsec / 1000000); } fprintf(stderr, "clock_gettime() failed: %s\n", strerror(errno)); exit(-1); } int main(void) { uint32_t i, error_cnt = 0; uint64_t prev_time = 0; int tid = syscall(SYS_gettid); signal(SIGINT, ctrl_c_handler); for (i = 0; i < 10000 && run; i++) { uint64_t cur_time = milliseconds(); if (cur_time < prev_time) { error_cnt++; fprintf(stderr, "Error: tid = %d: clock_gettime(): Prev = %" PRIu64 ", Cur = %" PRIu64 ", diff = -%" PRIu64 "\n", tid, prev_time, cur_time, prev_time - cur_time); } else { fprintf(stdout, "Info: tid = %d: clock_gettime(): Prev = %" PRIu64 ", Cur = %" PRIu64 ", diff = %" PRIu64 "\n", tid, prev_time, cur_time, cur_time - prev_time); } prev_time = cur_time; } fprintf(stderr, "tid = %d: Ran %u times. error_cnt = %u, success_cnt = %u\n", tid, i, error_cnt, i - error_cnt); return error_cnt ? -1 : 0; }