Skip to content

Commit a60b0ea

Browse files
committed
Minor code cleanups around timer and usleep() calls.
1 parent 4bdb68a commit a60b0ea

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/sb_timer.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,22 @@
4444
#include "sb_util.h"
4545
#include "ck_spinlock.h"
4646

47+
#define NS_PER_SEC 1000000000
48+
#define US_PER_SEC 1000000
49+
#define MS_PER_SEC 1000
50+
#define NS_PER_MS (NS_PER_SEC / MS_PER_SEC)
51+
4752
/* Convert nanoseconds to seconds and vice versa */
48-
#define NS2SEC(nsec) ((nsec)/1000000000.)
49-
#define SEC2NS(sec) ((uint64_t)(sec) * 1000000000)
53+
#define NS2SEC(nsec) ((nsec) / (double) NS_PER_SEC)
54+
#define SEC2NS(sec) ((uint64_t) (sec) * NS_PER_SEC)
5055

5156
/* Convert nanoseconds to milliseconds and vice versa */
52-
#define NS2MS(nsec) ((nsec)/1000000.)
53-
#define MS2NS(sec) ((sec)*1000000ULL)
57+
#define NS2MS(nsec) ((nsec) / (double) NS_PER_MS)
58+
#define MS2NS(sec) ((sec) * (uint64_t) NS_PER_MS)
5459

5560
/* Convert milliseconds to seconds and vice versa */
56-
#define MS2SEC(msec) ((msec)/1000.)
57-
#define SEC2MS(sec) ((sec)*1000)
61+
#define MS2SEC(msec) ((msec) / (double) MS_PER_SEC)
62+
#define SEC2MS(sec) ((sec) * MS_PER_SEC)
5863

5964
/* Difference between two 'timespec' values in nanoseconds */
6065
#define TIMESPEC_DIFF(a,b) (SEC2NS(a.tv_sec - b.tv_sec) + \
@@ -95,6 +100,12 @@ typedef struct
95100
} sb_timer_t;
96101

97102

103+
static inline int sb_nanosleep(uint64_t ns)
104+
{
105+
struct timespec ts = { ns / NS_PER_SEC, ns % NS_PER_SEC };
106+
return nanosleep(&ts, NULL);
107+
}
108+
98109
/* timer control functions */
99110

100111
/* Initialize timer */

src/sysbench.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,7 @@ static void *eventgen_thread_proc(void *arg)
875875
next_ns += intr_ns;
876876

877877
if (next_ns > curr_ns)
878-
{
879-
const uint64_t intr_ns = next_ns - curr_ns;
880-
struct timespec ts = { intr_ns / 1000000000, intr_ns % 1000000000 };
881-
nanosleep(&ts, NULL);
882-
}
878+
sb_nanosleep(next_ns - curr_ns);
883879

884880
/* Enqueue a new event */
885881
queue_array[i] = sb_timer_value(&sb_exec_timer);
@@ -935,7 +931,7 @@ static void *report_thread_proc(void *arg)
935931

936932
for (;;)
937933
{
938-
usleep(pause_ns / 1000);
934+
sb_nanosleep(pause_ns);
939935

940936
report_intermediate();
941937

@@ -955,7 +951,6 @@ static void *report_thread_proc(void *arg)
955951

956952
static void *checkpoints_thread_proc(void *arg)
957953
{
958-
unsigned long long pause_ns;
959954
unsigned long long next_ns;
960955
unsigned long long curr_ns;
961956
unsigned int i;
@@ -987,8 +982,7 @@ static void *checkpoints_thread_proc(void *arg)
987982
if (next_ns <= curr_ns)
988983
continue;
989984

990-
pause_ns = next_ns - curr_ns;
991-
usleep(pause_ns / 1000);
985+
sb_nanosleep(next_ns - curr_ns);
992986

993987
log_timestamp(LOG_NOTICE, NS2SEC(sb_timer_value(&sb_exec_timer)),
994988
"Checkpoint report:");

0 commit comments

Comments
 (0)