Skip to content

Commit

Permalink
Minor code cleanups around timer and usleep() calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
akopytov committed Jan 16, 2018
1 parent 4bdb68a commit a60b0ea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
23 changes: 17 additions & 6 deletions src/sb_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) + \
Expand Down Expand Up @@ -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 */
Expand Down
12 changes: 3 additions & 9 deletions src/sysbench.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,11 +875,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);
Expand Down Expand Up @@ -935,7 +931,7 @@ static void *report_thread_proc(void *arg)

for (;;)
{
usleep(pause_ns / 1000);
sb_nanosleep(pause_ns);

report_intermediate();

Expand All @@ -955,7 +951,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;
Expand Down Expand Up @@ -987,8 +982,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:");
Expand Down

0 comments on commit a60b0ea

Please sign in to comment.