Skip to content

Commit

Permalink
core: remove use of clock_gettime(3)
Browse files Browse the repository at this point in the history
The portability of clock_gettime(3) on Mac OS / OS X
is... complicated.  Per
Homebrew/homebrew-core#6551 and
ofiwg#2508, remove the use of
clock_gettime(3) from the core of libfabric.  clock_gettime(3) is
still used in some providers that do not compile on OS X / MacOS, but
that's ok).

Signed-off-by: Jeff Squyres <jsquyres@cisco.com>
  • Loading branch information
jsquyres committed Nov 18, 2016
1 parent d107cd9 commit 4b481ba
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 53 deletions.
4 changes: 0 additions & 4 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ common_srcs = \
prov/util/src/util_mr.c

if MACOS
if !HAVE_CLOCK_GETTIME
common_srcs += src/osx/osd.c
endif

common_srcs += src/unix/osd.c
common_srcs += include/osx/osd.h
common_srcs += include/unix/osd.h
Expand Down
13 changes: 0 additions & 13 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,11 @@ fi
AC_DEFINE_UNQUOTED([PT_LOCK_SPIN], [$have_spinlock],
[Define to 1 if pthread_spin_init is available.])

have_clock_gettime=0

AC_CHECK_FUNCS([epoll_create])
if test "$ac_cv_func_epoll_create" = yes; then
AC_DEFINE([HAVE_EPOLL], [1], [Define if you have epoll support.])
fi

AC_SEARCH_LIBS([clock_gettime],[rt],
[have_clock_gettime=1],
[AC_CHECK_FUNCS([host_get_clock_service],
[],
[AC_MSG_ERROR([clock_gettime or host_get_clock_service
not found.])])])

AC_DEFINE_UNQUOTED(HAVE_CLOCK_GETTIME, [$have_clock_gettime],
[Define to 1 if clock_gettime is available.])
AM_CONDITIONAL(HAVE_CLOCK_GETTIME, [test $have_clock_gettime -eq 1])

dnl Check for gcc atomic intrinsics
AC_MSG_CHECKING(compiler support for c11 atomics)
AC_TRY_LINK([#include <stdatomic.h>],
Expand Down
4 changes: 3 additions & 1 deletion include/fi_lock.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2013-2014 Intel Corporation. All rights reserved.
* Copyright (c) 2016 Cisco Systems, Inc. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
Expand Down Expand Up @@ -47,7 +48,8 @@ extern "C" {
#endif


int fi_wait_cond(pthread_cond_t *cond, pthread_mutex_t *mut, int timeout);
/* timeout is expressed in miliseconds */
int fi_wait_cond(pthread_cond_t *cond, pthread_mutex_t *mut, int timeout_ms);


#if PT_LOCK_SPIN == 1
Expand Down
13 changes: 0 additions & 13 deletions include/osx/osd.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@
extern "C" {
#endif

/* macOS Sierra added clock_gettime to libc. This implementation should only
* take effect if it is not available.
*/
#if !HAVE_CLOCK_GETTIME

#define CLOCK_REALTIME CALENDAR_CLOCK
#define CLOCK_MONOTONIC SYSTEM_CLOCK

typedef int clockid_t;
int clock_gettime(clockid_t clk_id, struct timespec *tp);

#endif

static inline int ofi_shm_remap(struct util_shm *shm, size_t newsize, void **mapped)
{
return -1;
Expand Down
25 changes: 20 additions & 5 deletions src/unix/osd.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2013-2016 Intel Corporation. All rights reserved.
* Copyright (c) 2016 Cisco Systems, Inc. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
Expand Down Expand Up @@ -42,6 +43,7 @@
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>

#include "fi.h"
#include "fi_osd.h"
Expand All @@ -65,16 +67,29 @@ int fi_fd_nonblock(int fd)
return 0;
}

int fi_wait_cond(pthread_cond_t *cond, pthread_mutex_t *mut, int timeout)
/* timeout is expressed in miliseconds */
int fi_wait_cond(pthread_cond_t *cond, pthread_mutex_t *mut, int timeout_ms)
{
int ret;
struct timeval tv;
uint64_t t;
struct timespec ts;

if (timeout < 0)
if (timeout_ms < 0)
return pthread_cond_wait(cond, mut);

clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += timeout / 1000;
ts.tv_nsec += (timeout % 1000) * 1000000;
ret = gettimeofday(&tv, NULL);
if (ret < 0)
return ret;

/* Set t to now, expressed in microseconds */
t = (tv.tv_sec + (timeout_ms / 1000)) * 1000000;
t += (timeout_ms % 1000) * 1000;
t += tv.tv_usec;

/* Convert t to a struct timespec */
ts.tv_sec = t / 1000000;
ts.tv_nsec = t % 1000000;
return pthread_cond_timedwait(cond, mut, &ts);
}

Expand Down
71 changes: 54 additions & 17 deletions util/pingpong.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ int pp_debug;
})
#endif

struct pp_time {
time_t sec;
long nsec;
};

struct ct_pingpong {
struct fi_info *fi_pep, *fi, *hints;
struct fid_fabric *fabric;
Expand All @@ -182,7 +187,7 @@ struct ct_pingpong {
size_t buf_size, tx_size, rx_size;

int timeout;
struct timespec start, end;
struct pp_time start, end;

struct fi_av_attr av_attr;
struct fi_eq_attr eq_attr;
Expand All @@ -205,20 +210,52 @@ static const int integ_alphabet_length =
/*******************************************************************************
* Compatibility methods
******************************************************************************/
#if defined(__APPLE__) && !HAVE_CLOCK_GETTIME
int clock_gettime(clockid_t clk_id, struct timespec *tp)

int pp_clock_gettime(struct pp_time *ofit)
{
#if defined(__APPLE__)
/* clock_gettime(3) portability on OS X / Mac OS X is... complicated:
*
* 1. OS X 10.11 (El Capitan) and below do not have
* clock_gettime(3).
* 2. MacOS 10.12 (Sierra) and above has clock_gettime(3).
* 3. XCode 8.x allows you to build 10.12 applications on
* 10.11 by setting the env var SDKROOT to point to the
* 10.12 root. On 10.11, the SDK libraries contain a weak
* symbol for clock_gettime(3) (and some other POSIX
* functions). Meaning:
* 1. If you run the resulting executable on 10.11, you get
* a linker error because there is no strong symbol for
* clock_gettime behind the weak symbol.
* 2. If you run the resulting executable on 10.12, it
* works fine.
*
* We *could* have some fairly complicated configury on OS X /
* MacOS, but why bother? Libfabric is built on OS X / MacOS
* mainly for testing, not high performance -- meaning that
* using gettimeofday(3) is sufficient for timing purposes.
*/
int retval;
struct timeval tv;

retval = gettimeofday(&tv, NULL);

tp->tv_sec = tv.tv_sec;
tp->tv_nsec = tv.tv_usec * 1000;
ofit->sec = tv.tv_sec;
ofit->nsec = tv.tv_usec * 1000;

return retval;
#else
int retval;
struct timespec ts;

retval = clock_gettime(CLOCK_MONOTONIC, &ts);

ofit->sec = ts.tv_sec;
ofit->nsec = ts.tv_nsec;

return retval;
}
#endif
}

/*******************************************************************************
* Utils
Expand Down Expand Up @@ -804,12 +841,12 @@ static inline void pp_start(struct ct_pingpong *ct)
{
PP_DEBUG("Starting test chrono\n");
ct->opts.options |= PP_OPT_ACTIVE;
clock_gettime(CLOCK_MONOTONIC, &(ct->start));
pp_clock_gettime(&(ct->start));
}

static inline void pp_stop(struct ct_pingpong *ct)
{
clock_gettime(CLOCK_MONOTONIC, &(ct->end));
pp_clock_gettime(&(ct->end));
ct->opts.options &= ~PP_OPT_ACTIVE;
PP_DEBUG("Stopped test chrono\n");
}
Expand Down Expand Up @@ -1015,18 +1052,18 @@ char *cnt_str(char *str, size_t size, uint64_t cnt)
return str;
}

int64_t get_elapsed(const struct timespec *b, const struct timespec *a,
int64_t get_elapsed(const struct pp_time *b, const struct pp_time *a,
enum precision p)
{
int64_t elapsed;

elapsed = difftime(a->tv_sec, b->tv_sec) * 1000 * 1000 * 1000;
elapsed += a->tv_nsec - b->tv_nsec;
elapsed = difftime(a->sec, b->sec) * 1000 * 1000 * 1000;
elapsed += a->nsec - b->nsec;
return elapsed / p;
}

void show_perf(char *name, int tsize, int sent, int acked,
struct timespec *start, struct timespec *end, int xfers_per_iter)
struct pp_time *start, struct pp_time *end, int xfers_per_iter)
{
static int header = 1;
char str[PP_STR_LEN];
Expand Down Expand Up @@ -1097,17 +1134,17 @@ static int pp_get_cq_comp(struct fid_cq *cq, uint64_t *cur, uint64_t total,
int timeout)
{
struct fi_cq_err_entry comp;
struct timespec a = {0}, b = {0};
struct pp_time a = {0}, b = {0};
int ret = 0;

if (timeout >= 0)
clock_gettime(CLOCK_MONOTONIC, &a);
pp_clock_gettime(&a);

while (total - *cur > 0) {
ret = fi_cq_read(cq, &comp, 1);
if (ret > 0) {
if (timeout >= 0)
clock_gettime(CLOCK_MONOTONIC, &a);
pp_clock_gettime(&a);

(*cur)++;
} else if (ret < 0 && ret != -FI_EAGAIN) {
Expand All @@ -1120,8 +1157,8 @@ static int pp_get_cq_comp(struct fid_cq *cq, uint64_t *cur, uint64_t total,

return ret;
} else if (timeout >= 0) {
clock_gettime(CLOCK_MONOTONIC, &b);
if ((b.tv_sec - a.tv_sec) > timeout) {
pp_clock_gettime(&b);
if ((b.sec - a.sec) > timeout) {
fprintf(stderr, "%ds timeout expired\n",
timeout);
return -FI_ENODATA;
Expand Down

0 comments on commit 4b481ba

Please sign in to comment.