diff --git a/src/sb_timer.h b/src/sb_timer.h index 037f8cf..6a4250a 100644 --- a/src/sb_timer.h +++ b/src/sb_timer.h @@ -44,17 +44,22 @@ #include "sb_util.h" #include "ck_spinlock.h" +#define NS_PER_SEC 1000000000 +#define US_PER_SEC 1000000 +#define MS_PER_SEC 1000 +#define NS_PER_MS (NS_PER_SEC / MS_PER_SEC) + /* Convert nanoseconds to seconds and vice versa */ -#define NS2SEC(nsec) ((nsec)/1000000000.) -#define SEC2NS(sec) ((uint64_t)(sec) * 1000000000) +#define NS2SEC(nsec) ((nsec) / (double) NS_PER_SEC) +#define SEC2NS(sec) ((uint64_t) (sec) * NS_PER_SEC) /* Convert nanoseconds to milliseconds and vice versa */ -#define NS2MS(nsec) ((nsec)/1000000.) -#define MS2NS(sec) ((sec)*1000000ULL) +#define NS2MS(nsec) ((nsec) / (double) NS_PER_MS) +#define MS2NS(sec) ((sec) * (uint64_t) NS_PER_MS) /* Convert milliseconds to seconds and vice versa */ -#define MS2SEC(msec) ((msec)/1000.) -#define SEC2MS(sec) ((sec)*1000) +#define MS2SEC(msec) ((msec) / (double) MS_PER_SEC) +#define SEC2MS(sec) ((sec) * MS_PER_SEC) /* Difference between two 'timespec' values in nanoseconds */ #define TIMESPEC_DIFF(a,b) (SEC2NS(a.tv_sec - b.tv_sec) + \ @@ -95,6 +100,12 @@ typedef struct } sb_timer_t; +static inline int sb_nanosleep(uint64_t ns) +{ + struct timespec ts = { ns / NS_PER_SEC, ns % NS_PER_SEC }; + return nanosleep(&ts, NULL); +} + /* timer control functions */ /* Initialize timer */ diff --git a/src/sysbench.c b/src/sysbench.c index 36a1674..69f7b49 100644 --- a/src/sysbench.c +++ b/src/sysbench.c @@ -897,11 +897,7 @@ static void *eventgen_thread_proc(void *arg) next_ns += intr_ns; if (next_ns > curr_ns) - { - const uint64_t intr_ns = next_ns - curr_ns; - struct timespec ts = { intr_ns / 1000000000, intr_ns % 1000000000 }; - nanosleep(&ts, NULL); - } + sb_nanosleep(next_ns - curr_ns); /* Enqueue a new event */ queue_array[i] = sb_timer_value(&sb_exec_timer); @@ -957,7 +953,7 @@ static void *report_thread_proc(void *arg) for (;;) { - usleep(pause_ns / 1000); + sb_nanosleep(pause_ns); report_intermediate(); @@ -977,7 +973,6 @@ static void *report_thread_proc(void *arg) static void *checkpoints_thread_proc(void *arg) { - unsigned long long pause_ns; unsigned long long next_ns; unsigned long long curr_ns; unsigned int i; @@ -1009,8 +1004,7 @@ static void *checkpoints_thread_proc(void *arg) if (next_ns <= curr_ns) continue; - pause_ns = next_ns - curr_ns; - usleep(pause_ns / 1000); + sb_nanosleep(next_ns - curr_ns); log_timestamp(LOG_NOTICE, NS2SEC(sb_timer_value(&sb_exec_timer)), "Checkpoint report:");