From d375843b65d3893cfeb211c2d58cc1c2ae01cb00 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Thu, 27 Feb 2020 13:53:03 +0000 Subject: [PATCH] os/time: Add time getting and conversion functions For Linux it is based on CLOCK_MONOTONIC. --- src/xrt/auxiliary/os/os_time.h | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/xrt/auxiliary/os/os_time.h b/src/xrt/auxiliary/os/os_time.h index 9adf49ee9..b34c2ab05 100644 --- a/src/xrt/auxiliary/os/os_time.h +++ b/src/xrt/auxiliary/os/os_time.h @@ -15,6 +15,7 @@ #ifdef XRT_OS_LINUX #include +#include #else #error "No time support on non-Linux platforms yet." #endif @@ -23,6 +24,7 @@ extern "C" { #endif + /*! * Write an output report to the given device. */ @@ -38,6 +40,48 @@ os_nanosleep(long nsec) #endif } +#ifdef XRT_OS_LINUX +/*! + * Convert a timespec struct to nanoseconds. + */ +XRT_MAYBE_UNUSED static inline uint64_t +os_timespec_to_ns(struct timespec *spec) +{ + uint64_t ns = 0; + ns += (uint64_t)spec->tv_sec * 1000 * 1000 * 1000; + ns += (uint64_t)spec->tv_nsec; + return ns; +} + +/*! + * Convert a timeval struct to nanoseconds. + */ +XRT_MAYBE_UNUSED static inline uint64_t +os_timeval_to_ns(struct timeval *val) +{ + uint64_t ns = 0; + ns += (uint64_t)val->tv_sec * 1000 * 1000 * 1000; + ns += (uint64_t)val->tv_usec * 1000; + return ns; +} + +/*! + * Return a monotonic clock in nanoseconds. + */ +XRT_MAYBE_UNUSED static inline uint64_t +os_monotonic_get_ns(void) +{ + struct timespec ts; + int ret = clock_gettime(CLOCK_MONOTONIC, &ts); + if (ret != 0) { + return 0; + } + + return os_timespec_to_ns(&ts); +} +#endif + + #ifdef __cplusplus } #endif