Skip to content

Commit

Permalink
util: refactor so ifdef's are withing each time-related function
Browse files Browse the repository at this point in the history
The time-related functions have two implementations, one if clock_gettime() is
available and the other if not.

Previously, there was one big ifdef-else with the clock_gettime()
implementation of each function followed by the other implementation of each
function.

With this commit, each function is defined once, with an ifdef-else inside it
with the two implementations of that function. For ease of review, no other
code changes are made, but the intent will become obvious with later changes.
  • Loading branch information
kgaillot authored and chrissie-c committed Feb 14, 2022
1 parent 5f8278a commit 4f82b0b
Showing 1 changed file with 22 additions and 33 deletions.
55 changes: 22 additions & 33 deletions lib/util.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2010 Red Hat, Inc.
* Copyright (C) 2010-2022 Red Hat, Inc.
*
* All rights reserved.
*
Expand Down Expand Up @@ -136,22 +136,26 @@ qb_timespec_add_ms(struct timespec *ts, int32_t ms)
#endif /* S_SPLINT_S */
}

#ifdef HAVE_MONOTONIC_CLOCK
uint64_t
qb_util_nano_current_get(void)
{
#ifdef HAVE_MONOTONIC_CLOCK
uint64_t nano_monotonic;
struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);
nano_monotonic =
(ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t) ts.tv_nsec;
return (nano_monotonic);
#else
return qb_util_nano_from_epoch_get();
#endif /* HAVE_MONOTONIC_CLOCK */
}

uint64_t
qb_util_nano_from_epoch_get(void)
{
#ifdef HAVE_MONOTONIC_CLOCK
uint64_t nano_monotonic;
struct timespec ts;
#ifdef CLOCK_REALTIME_COARSE
Expand All @@ -162,11 +166,22 @@ qb_util_nano_from_epoch_get(void)
nano_monotonic =
(ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t) ts.tv_nsec;
return (nano_monotonic);
#else
uint64_t nano_from_epoch;
struct timeval time_from_epoch;
gettimeofday(&time_from_epoch, 0);

nano_from_epoch = ((time_from_epoch.tv_sec * QB_TIME_NS_IN_SEC) +
(time_from_epoch.tv_usec * QB_TIME_NS_IN_USEC));

return (nano_from_epoch);
#endif /* HAVE_MONOTONIC_CLOCK */
}

uint64_t
qb_util_nano_monotonic_hz(void)
{
#ifdef HAVE_MONOTONIC_CLOCK
uint64_t nano_monotonic_hz;
struct timespec ts;

Expand All @@ -181,56 +196,30 @@ qb_util_nano_monotonic_hz(void)
QB_TIME_NS_IN_SEC / ((ts.tv_sec * QB_TIME_NS_IN_SEC) + ts.tv_nsec);

return (nano_monotonic_hz);
#else
return sysconf(_SC_CLK_TCK);
#endif /* HAVE_MONOTONIC_CLOCK */
}

void
qb_util_timespec_from_epoch_get(struct timespec *ts)
{
#ifdef HAVE_MONOTONIC_CLOCK
#ifdef CLOCK_REALTIME_COARSE
clock_gettime(CLOCK_REALTIME_COARSE, ts);
#else
clock_gettime(CLOCK_REALTIME, ts);
#endif
}

#else
uint64_t
qb_util_nano_current_get(void)
{
return qb_util_nano_from_epoch_get();
}

uint64_t
qb_util_nano_monotonic_hz(void)
{
return sysconf(_SC_CLK_TCK);
}

void
qb_util_timespec_from_epoch_get(struct timespec *ts)
{
struct timeval time_from_epoch;
gettimeofday(&time_from_epoch, 0);

#ifndef S_SPLINT_S
ts->tv_sec = time_from_epoch.tv_sec;
ts->tv_nsec = time_from_epoch.tv_usec * QB_TIME_NS_IN_USEC;
#endif /* S_SPLINT_S */
}

uint64_t
qb_util_nano_from_epoch_get(void)
{
uint64_t nano_from_epoch;
struct timeval time_from_epoch;
gettimeofday(&time_from_epoch, 0);

nano_from_epoch = ((time_from_epoch.tv_sec * QB_TIME_NS_IN_SEC) +
(time_from_epoch.tv_usec * QB_TIME_NS_IN_USEC));

return (nano_from_epoch);
}
#endif /* HAVE_MONOTONIC_CLOCK */
}

struct qb_util_stopwatch {
uint64_t started;
Expand Down

0 comments on commit 4f82b0b

Please sign in to comment.