os/time: Add time getting and conversion functions

For Linux it is based on CLOCK_MONOTONIC.
This commit is contained in:
Jakob Bornecrantz 2020-02-27 13:53:03 +00:00
parent 039e29e7ad
commit d375843b65

View file

@ -15,6 +15,7 @@
#ifdef XRT_OS_LINUX
#include <time.h>
#include <sys/time.h>
#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