From 389fbf04b41e2002be44a1e3392bfade2f1deeef Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Wed, 3 Aug 2016 16:54:16 -0400 Subject: [PATCH] Port: Add OSX clock_gettime compat MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- include/lttng/health-internal.h | 2 +- src/bin/lttng-sessiond/main.c | 2 +- src/bin/lttng-sessiond/ust-clock.h | 4 +- src/bin/lttng-sessiond/ust-metadata.c | 2 +- src/bin/lttng/commands/create.c | 2 +- src/common/compat/Makefile.am | 3 +- src/common/compat/time.h | 78 +++++++++++++++++++ src/common/error.c | 2 +- src/common/error.h | 2 +- src/common/health/health.c | 2 +- src/common/sessiond-comm/inet.c | 6 +- src/common/sessiond-comm/inet6.c | 6 +- tests/regression/kernel/Makefile.am | 1 + tests/regression/kernel/select_poll_epoll.c | 2 +- tests/regression/tools/live/live_test.c | 2 +- .../lttng-ust-clock-override-test.c | 2 +- .../lttng-ust-getcpu-override-test.c | 2 +- tests/utils/Makefile.am | 1 + tests/utils/utils.c | 4 +- 19 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 src/common/compat/time.h diff --git a/include/lttng/health-internal.h b/include/lttng/health-internal.h index 2225e33f1..1623518d1 100644 --- a/include/lttng/health-internal.h +++ b/include/lttng/health-internal.h @@ -20,7 +20,7 @@ */ #include -#include +#include #include #include #include diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index 21bc0819d..b0674612b 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -2378,7 +2378,7 @@ static int spawn_consumer_thread(struct consumer_data *consumer_data) pthread_mutex_lock(&consumer_data->cond_mutex); /* Get time for sem_timedwait absolute timeout */ - clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout); + clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout); /* * Set the timeout for the condition timed wait even if the clock gettime * call fails since we might loop on that call and we want to avoid to diff --git a/src/bin/lttng-sessiond/ust-clock.h b/src/bin/lttng-sessiond/ust-clock.h index c2e0cbcc3..58ff8b9d0 100644 --- a/src/bin/lttng-sessiond/ust-clock.h +++ b/src/bin/lttng-sessiond/ust-clock.h @@ -20,7 +20,7 @@ #ifndef _UST_CLOCK_H #define _UST_CLOCK_H -#include +#include #include #include #include @@ -57,7 +57,7 @@ uint64_t trace_clock_read64_monotonic(void) { struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC, &ts)) { + if (lttng_clock_gettime(CLOCK_MONOTONIC, &ts)) { /* TODO Report error cleanly up the chain. */ PERROR("clock_gettime CLOCK_MONOTONIC"); ts.tv_sec = 0; diff --git a/src/bin/lttng-sessiond/ust-metadata.c b/src/bin/lttng-sessiond/ust-metadata.c index d24df1943..49edb1711 100644 --- a/src/bin/lttng-sessiond/ust-metadata.c +++ b/src/bin/lttng-sessiond/ust-metadata.c @@ -819,7 +819,7 @@ int measure_single_clock_offset(struct offset_sample *sample) int ret; monotonic[0] = trace_clock_read64(); - ret = clock_gettime(CLOCK_REALTIME, &rts); + ret = lttng_clock_gettime(CLOCK_REALTIME, &rts); if (ret < 0) { return ret; } diff --git a/src/bin/lttng/commands/create.c b/src/bin/lttng/commands/create.c index 0ae2b1096..0f7d1a438 100644 --- a/src/bin/lttng/commands/create.c +++ b/src/bin/lttng/commands/create.c @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/common/compat/Makefile.am b/src/common/compat/Makefile.am index 865fd7f70..2d2b02a0d 100644 --- a/src/common/compat/Makefile.am +++ b/src/common/compat/Makefile.am @@ -10,4 +10,5 @@ endif libcompat_la_SOURCES = poll.h fcntl.h endian.h mman.h dirent.h \ socket.h compat-fcntl.c uuid.h tid.h \ - getenv.h string.h prctl.h paths.h netdb.h $(COMPAT) + getenv.h string.h prctl.h paths.h netdb.h $(COMPAT) \ + time.h diff --git a/src/common/compat/time.h b/src/common/compat/time.h new file mode 100644 index 000000000..f51075dc3 --- /dev/null +++ b/src/common/compat/time.h @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2016 Michael Jeanson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _COMPAT_TIME_H +#define _COMPAT_TIME_H + +#include + +#ifdef __APPLE__ +typedef uint64_t timer_t; +typedef int clockid_t; + +#include +#include + +#define CLOCK_REALTIME CALENDAR_CLOCK +#define CLOCK_MONOTONIC SYSTEM_CLOCK + +static inline +int lttng_clock_gettime(clockid_t clk_id, struct timespec *tp) +{ + int ret = 0; + clock_serv_t clock; + mach_timespec_t now; + + if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC) { + ret = -1; + errno = EINVAL; + goto end; + } + + host_get_clock_service(mach_host_self(), clk_id, &clock); + + ret = clock_get_time(clock, &now); + if (ret != KERN_SUCCESS) { + ret = -1; + goto deallocate; + } + + tp->tv_sec = now.tv_sec; + tp->tv_nsec = now.tv_nsec; + +deallocate: + mach_port_deallocate(mach_task_self(), clock); +end: + return ret; +} + +#else /* __APPLE__ */ + +static inline +int lttng_clock_gettime(clockid_t clk_id, struct timespec *tp) +{ + return clock_gettime(clk_id, tp); +} + +#endif /* __APPLE__ */ + +#endif /* _COMPAT_TIME_H */ diff --git a/src/common/error.c b/src/common/error.c index d3d952a7d..1067e4852 100644 --- a/src/common/error.c +++ b/src/common/error.c @@ -46,7 +46,7 @@ const char *log_add_time(void) struct timespec tp; time_t now; - ret = clock_gettime(CLOCK_REALTIME, &tp); + ret = lttng_clock_gettime(CLOCK_REALTIME, &tp); if (ret < 0) { goto error; } diff --git a/src/common/error.h b/src/common/error.h index e8c811ee6..9b07e0a47 100644 --- a/src/common/error.h +++ b/src/common/error.h @@ -24,7 +24,7 @@ #include #include #include -#include +#include #ifndef _GNU_SOURCE #error "lttng-tools error.h needs _GNU_SOURCE" diff --git a/src/common/health/health.c b/src/common/health/health.c index 2667e3930..830b6f0ef 100644 --- a/src/common/health/health.c +++ b/src/common/health/health.c @@ -171,7 +171,7 @@ static int validate_state(struct health_app *ha, struct health_state *state) last = state->last; current = uatomic_read(&state->current); - ret = clock_gettime(CLOCK_MONOTONIC, ¤t_time); + ret = lttng_clock_gettime(CLOCK_MONOTONIC, ¤t_time); if (ret < 0) { PERROR("Error reading time\n"); /* error */ diff --git a/src/common/sessiond-comm/inet.c b/src/common/sessiond-comm/inet.c index b870deef4..84bee5bd9 100644 --- a/src/common/sessiond-comm/inet.c +++ b/src/common/sessiond-comm/inet.c @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include @@ -159,7 +159,7 @@ int connect_with_timeout(struct lttcomm_sock *sock) return -1; } - ret = clock_gettime(CLOCK_MONOTONIC, &orig_time); + ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time); if (ret == -1) { PERROR("clock_gettime"); return -1; @@ -213,7 +213,7 @@ int connect_with_timeout(struct lttcomm_sock *sock) } } /* ret == 0: timeout */ - ret = clock_gettime(CLOCK_MONOTONIC, &cur_time); + ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time); if (ret == -1) { PERROR("clock_gettime"); connect_ret = ret; diff --git a/src/common/sessiond-comm/inet6.c b/src/common/sessiond-comm/inet6.c index 51d02ebb9..1413c3517 100644 --- a/src/common/sessiond-comm/inet6.c +++ b/src/common/sessiond-comm/inet6.c @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include @@ -157,7 +157,7 @@ int connect_with_timeout(struct lttcomm_sock *sock) return -1; } - ret = clock_gettime(CLOCK_MONOTONIC, &orig_time); + ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time); if (ret == -1) { PERROR("clock_gettime"); return -1; @@ -211,7 +211,7 @@ int connect_with_timeout(struct lttcomm_sock *sock) } } /* ret == 0: timeout */ - ret = clock_gettime(CLOCK_MONOTONIC, &cur_time); + ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time); if (ret == -1) { PERROR("clock_gettime"); connect_ret = ret; diff --git a/tests/regression/kernel/Makefile.am b/tests/regression/kernel/Makefile.am index cfdeeb93b..e9ec04ea9 100644 --- a/tests/regression/kernel/Makefile.am +++ b/tests/regression/kernel/Makefile.am @@ -1,3 +1,4 @@ +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src EXTRA_DIST = test_event_basic test_all_events test_syscall \ test_clock_override test_rotation_destroy_flush \ test_select_poll_epoll diff --git a/tests/regression/kernel/select_poll_epoll.c b/tests/regression/kernel/select_poll_epoll.c index dfb52370b..4b703b380 100644 --- a/tests/regression/kernel/select_poll_epoll.c +++ b/tests/regression/kernel/select_poll_epoll.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #define BUF_SIZE 256 #define NB_FD 1 diff --git a/tests/regression/tools/live/live_test.c b/tests/regression/tools/live/live_test.c index cce6d1069..8ff97db25 100644 --- a/tests/regression/tools/live/live_test.c +++ b/tests/regression/tools/live/live_test.c @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/tests/regression/ust/clock-override/lttng-ust-clock-override-test.c b/tests/regression/ust/clock-override/lttng-ust-clock-override-test.c index deaa16405..b918f787c 100644 --- a/tests/regression/ust/clock-override/lttng-ust-clock-override-test.c +++ b/tests/regression/ust/clock-override/lttng-ust-clock-override-test.c @@ -25,7 +25,7 @@ */ #include -#include +#include #include #include #include diff --git a/tests/regression/ust/getcpu-override/lttng-ust-getcpu-override-test.c b/tests/regression/ust/getcpu-override/lttng-ust-getcpu-override-test.c index f86826ca4..5b4e55952 100644 --- a/tests/regression/ust/getcpu-override/lttng-ust-getcpu-override-test.c +++ b/tests/regression/ust/getcpu-override/lttng-ust-getcpu-override-test.c @@ -25,7 +25,7 @@ */ #include -#include +#include #include #include #include diff --git a/tests/utils/Makefile.am b/tests/utils/Makefile.am index 9d327e0ec..d2eb49fa6 100644 --- a/tests/utils/Makefile.am +++ b/tests/utils/Makefile.am @@ -1,5 +1,6 @@ SUBDIRS = . tap testapp +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src EXTRA_DIST = utils.sh test_utils.py babelstats.pl warn_processes.sh dist_noinst_SCRIPTS = utils.sh test_utils.py babelstats.pl noinst_LTLIBRARIES = libtestutils.la diff --git a/tests/utils/utils.c b/tests/utils/utils.c index e7bd9ce9f..7aa8667d4 100644 --- a/tests/utils/utils.c +++ b/tests/utils/utils.c @@ -16,7 +16,7 @@ */ #include -#include +#include #include #include #include @@ -43,7 +43,7 @@ int usleep_safe(useconds_t usec) struct timespec t1, t2; int64_t time_remaining_ns = (int64_t) usec * (int64_t) NSEC_PER_USEC; - ret = clock_gettime(CLOCK_MONOTONIC, &t1); + ret = lttng_clock_gettime(CLOCK_MONOTONIC, &t1); if (ret) { ret = -1; perror("clock_gettime"); -- 2.34.1