2020-03-02 18:09:56 +00:00
|
|
|
// Copyright 2019-2020, Collabora, Ltd.
|
2019-09-24 13:43:21 +00:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
/*!
|
|
|
|
* @file
|
|
|
|
* @brief Wrapper around OS native time functions.
|
2020-03-02 19:59:06 +00:00
|
|
|
*
|
|
|
|
* These should be preferred over directly using native OS time functions in
|
|
|
|
* potentially-portable code. Additionally, in most cases these are preferred
|
|
|
|
* over timepoints from @ref time_state for general usage in drivers, etc.
|
|
|
|
*
|
2019-09-24 13:43:21 +00:00
|
|
|
* @author Drew DeVault <sir@cmpwn.com>
|
2020-03-02 18:09:56 +00:00
|
|
|
* @author Jakob Bornecrantz <jakob@collabora.com>
|
2019-09-24 13:43:21 +00:00
|
|
|
*
|
|
|
|
* @ingroup aux_os
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-03-02 15:44:00 +00:00
|
|
|
#include "xrt/xrt_config_os.h"
|
2019-09-24 13:43:21 +00:00
|
|
|
#include "xrt/xrt_compiler.h"
|
|
|
|
|
|
|
|
#ifdef XRT_OS_LINUX
|
|
|
|
#include <time.h>
|
2020-02-27 13:53:03 +00:00
|
|
|
#include <sys/time.h>
|
2020-03-02 18:09:56 +00:00
|
|
|
#define XRT_HAVE_TIMESPEC
|
|
|
|
#define XRT_HAVE_TIMEVAL
|
|
|
|
|
2020-07-15 13:49:49 +00:00
|
|
|
#elif defined(XRT_OS_WINDOWS)
|
|
|
|
#include <time.h>
|
|
|
|
#define XRT_HAVE_TIMESPEC
|
|
|
|
|
2020-03-02 18:09:56 +00:00
|
|
|
#elif defined(XRT_DOXYGEN)
|
|
|
|
#include <time.h>
|
|
|
|
#define XRT_HAVE_TIMESPEC
|
|
|
|
#define XRT_HAVE_TIMEVAL
|
|
|
|
|
2019-09-24 13:43:21 +00:00
|
|
|
#else
|
|
|
|
#error "No time support on non-Linux platforms yet."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2020-03-02 18:09:56 +00:00
|
|
|
/*!
|
|
|
|
* @defgroup aux_os_time Portable Timekeeping
|
|
|
|
* @ingroup aux_os
|
|
|
|
*
|
|
|
|
* @brief Unifying wrapper around system time retrieval functions.
|
|
|
|
*/
|
|
|
|
|
2020-02-27 13:53:03 +00:00
|
|
|
|
2019-09-24 13:43:21 +00:00
|
|
|
/*!
|
2020-03-02 18:09:56 +00:00
|
|
|
* @defgroup aux_os_time_extra Extra Timekeeping Utilities
|
|
|
|
* @ingroup aux_os_time
|
|
|
|
*
|
|
|
|
* @brief Less-portable utility functions for manipulating system time, for
|
|
|
|
* interoperation with platform APIs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Sleep the given number of nanoseconds.
|
|
|
|
* @ingroup aux_os_time
|
2019-09-24 13:43:21 +00:00
|
|
|
*/
|
2020-03-02 20:01:18 +00:00
|
|
|
static inline void
|
2019-09-24 13:43:21 +00:00
|
|
|
os_nanosleep(long nsec)
|
|
|
|
{
|
2020-07-15 17:19:44 +00:00
|
|
|
#if defined(XRT_OS_LINUX)
|
2020-04-16 12:22:57 +00:00
|
|
|
struct timespec spec;
|
2020-09-07 13:25:00 +00:00
|
|
|
spec.tv_sec = (nsec / (1000 * 1000 * 1000));
|
|
|
|
spec.tv_nsec = (nsec % (1000 * 1000 * 1000));
|
|
|
|
nanosleep(&spec, NULL);
|
2020-07-15 17:19:44 +00:00
|
|
|
#elif defined(XRT_OS_WINDOWS)
|
|
|
|
Sleep(nsec / 1000);
|
2019-09-24 13:43:21 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-02 18:09:56 +00:00
|
|
|
#ifdef XRT_HAVE_TIMESPEC
|
2020-02-27 13:53:03 +00:00
|
|
|
/*!
|
2020-03-02 18:09:56 +00:00
|
|
|
* @brief Convert a timespec struct to nanoseconds.
|
|
|
|
* @ingroup aux_os_time_extra
|
2020-02-27 13:53:03 +00:00
|
|
|
*/
|
2020-03-02 20:01:18 +00:00
|
|
|
static inline uint64_t
|
2020-02-27 13:53:03 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-06-23 17:01:13 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Convert an nanosecond integer to a timespec struct.
|
|
|
|
* @ingroup aux_os_time_extra
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
os_ns_to_timespec(uint64_t ns, struct timespec *spec)
|
|
|
|
{
|
|
|
|
spec->tv_sec = (ns / (1000 * 1000 * 1000));
|
|
|
|
spec->tv_nsec = (ns % (1000 * 1000 * 1000));
|
|
|
|
}
|
2020-03-02 18:09:56 +00:00
|
|
|
#endif // XRT_HAVE_TIMESPEC
|
|
|
|
|
2020-02-27 13:53:03 +00:00
|
|
|
|
2020-03-02 18:09:56 +00:00
|
|
|
#ifdef XRT_HAVE_TIMEVAL
|
2020-02-27 13:53:03 +00:00
|
|
|
/*!
|
2020-03-02 18:09:56 +00:00
|
|
|
* @brief Convert a timeval struct to nanoseconds.
|
|
|
|
* @ingroup aux_os_time_extra
|
2020-02-27 13:53:03 +00:00
|
|
|
*/
|
2020-03-02 20:01:18 +00:00
|
|
|
static inline uint64_t
|
2020-02-27 13:53:03 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-03-02 18:09:56 +00:00
|
|
|
#endif // XRT_HAVE_TIMEVAL
|
|
|
|
|
2020-07-15 13:49:49 +00:00
|
|
|
#ifdef XRT_OS_WINDOWS
|
|
|
|
#define CLOCK_MONOTONIC 0
|
|
|
|
#define CLOCK_REALTIME 1
|
|
|
|
|
|
|
|
static int
|
|
|
|
clock_gettime(int clk_id, struct timespec *spec)
|
|
|
|
{
|
|
|
|
__int64 wintime;
|
|
|
|
|
|
|
|
//! @todo We should be using QueryPerformanceCounter
|
|
|
|
GetSystemTimeAsFileTime((FILETIME *)&wintime);
|
|
|
|
// 1jan1601 to 1jan1970
|
|
|
|
wintime -= 116444736000000000i64;
|
|
|
|
// seconds
|
|
|
|
spec->tv_sec = wintime / 10000000i64;
|
|
|
|
// nano-seconds
|
|
|
|
spec->tv_nsec = wintime % 10000000i64 * 100;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // XRT_OS_WINDOWS
|
2020-02-27 13:53:03 +00:00
|
|
|
|
|
|
|
/*!
|
2020-03-02 18:09:56 +00:00
|
|
|
* @brief Return a monotonic clock in nanoseconds.
|
|
|
|
* @ingroup aux_os_time
|
2020-02-27 13:53:03 +00:00
|
|
|
*/
|
2020-03-02 20:01:18 +00:00
|
|
|
static inline uint64_t
|
2020-02-27 13:53:03 +00:00
|
|
|
os_monotonic_get_ns(void)
|
|
|
|
{
|
2020-07-15 13:49:49 +00:00
|
|
|
#if defined(XRT_OS_LINUX) || defined(XRT_OS_WINDOWS)
|
2020-02-27 13:53:03 +00:00
|
|
|
struct timespec ts;
|
|
|
|
int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
if (ret != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return os_timespec_to_ns(&ts);
|
|
|
|
#endif
|
2020-03-02 18:09:56 +00:00
|
|
|
}
|
2020-02-27 13:53:03 +00:00
|
|
|
|
|
|
|
|
2019-09-24 13:43:21 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|