Skip to content

Commit

Permalink
time: Make ceph_time clocks work under BSD
Browse files Browse the repository at this point in the history
Linux's naming things _COARSE is non-standard. Other systems name them other
things.

BSD also names its precise clocks _PRECISE and defines the unmarked versions to
_FAST, so make sure we use _PRECISE when we want a high-resolution timer.

Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
  • Loading branch information
adamemerson committed Jan 25, 2016
1 parent eea2689 commit 8955c3b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/common/ceph_time.h
Expand Up @@ -68,7 +68,14 @@ namespace ceph {

static time_point now() noexcept {
struct timespec ts;
#if defined(CLOCK_REALTIME_PRECISE)
// FreeBSD defines CLOCK_REALTIME to CLOCK_REALTIME_FAST. This
// way we'll have high-resolution for real_clock on both
// BSD and Linux.
clock_gettime(CLOCK_REALTIME_PRECISE, &ts);
#else
clock_gettime(CLOCK_REALTIME, &ts);
#endif
return from_timespec(ts);
}
// We need a version of 'now' that can take a CephContext for
Expand Down Expand Up @@ -151,7 +158,18 @@ namespace ceph {

static time_point now() noexcept {
struct timespec ts;
#if defined(CLOCK_REALTIME_COARSE)
// Linux systems have _COARSE clocks.
clock_gettime(CLOCK_REALTIME_COARSE, &ts);
#elif defined(CLOCK_REALTIME_FAST)
// BSD systems have _FAST clocks.
clock_gettime(CLOCK_REALTIME_FAST, &ts);
#else
// And if we find neither, you may wish to consult your system's
// documentation.
#warning Falling back to CLOCK_REALTIME, may be slow.
clock_gettime(CLOCK_REALTIME, &ts);
#endif
return from_timespec(ts);
}
static time_point now(const CephContext* cct) noexcept;
Expand Down Expand Up @@ -214,7 +232,14 @@ namespace ceph {

static time_point now() noexcept {
struct timespec ts;
#if defined(CLOCK_MONOTONIC_PRECISE)
// FreeBSD defines CLOCK_MONOTONIC to CLOCK_MONOTONIC_FAST. This
// way we'll have high-resolution for mono_clock on both
// BSD and Linux.
clock_gettime(CLOCK_MONOTONIC_PRECISE, &ts);
#else
clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
return time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
}

Expand All @@ -233,7 +258,18 @@ namespace ceph {

static time_point now() noexcept {
struct timespec ts;
#if defined(CLOCK_MONOTONIC_COARSE)
// Linux systems have _COARSE clocks.
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
#elif defined(CLOCK_MONOTONIC_FAST)
// BSD systems have _FAST clocks.
clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
#else
// And if we find neither, you may wish to consult your system's
// documentation.
#warning Falling back to CLOCK_MONOTONIC, may be slow.
clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
return time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
}
};
Expand Down

0 comments on commit 8955c3b

Please sign in to comment.