From 1ee899977d701768f6437e2b178a06e46283f821 Mon Sep 17 00:00:00 2001 From: Dmitry Kozlyuk Date: Wed, 21 Apr 2021 22:33:49 +0300 Subject: [PATCH] eal: add timespec_get shim C11 timespec_get() is not provided on some platforms: * MinGW-w64 does not currently implement it [1]. * FreeBSD 11 with Clang 10.0.0 does not provide it. Add internal shims to Windows and FreeBSD EALs. For Windows, it can be removed after [1] is fixed. [1]: https://sourceforge.net/p/mingw-w64/mailman/message/37224689/ Signed-off-by: Dmitry Kozlyuk Acked-by: Jie Zhou Acked-by: Nick Connolly --- lib/eal/freebsd/include/rte_os_shim.h | 18 +++++++++++++++ lib/eal/windows/include/rte_os_shim.h | 33 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/eal/freebsd/include/rte_os_shim.h b/lib/eal/freebsd/include/rte_os_shim.h index 1e85229ca9..862d4bf596 100644 --- a/lib/eal/freebsd/include/rte_os_shim.h +++ b/lib/eal/freebsd/include/rte_os_shim.h @@ -3,6 +3,8 @@ #ifndef _RTE_OS_SHIM_ #define _RTE_OS_SHIM_ +#include + #include /** @@ -11,4 +13,20 @@ * Provides semi-standard OS facilities by convenient names. */ +#ifndef TIME_UTC + +#define TIME_UTC 1 + +static inline int +rte_timespec_get(struct timespec *now, int base) +{ + if (base != TIME_UTC || clock_gettime(CLOCK_REALTIME, now) < 0) + return 0; + return base; +} + +#define timespec_get(ts, base) rte_timespec_get(ts, base) + +#endif /* !defined TIME_UTC */ + #endif /* _RTE_OS_SHIM_ */ diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h index f40fb62d1d..433fa02c43 100644 --- a/lib/eal/windows/include/rte_os_shim.h +++ b/lib/eal/windows/include/rte_os_shim.h @@ -3,7 +3,10 @@ #ifndef _RTE_OS_SHIM_ #define _RTE_OS_SHIM_ +#include + #include +#include /** * @file @@ -33,4 +36,34 @@ #define IPPROTO_SCTP 132 #endif +#ifdef RTE_TOOLCHAIN_GCC + +#define TIME_UTC 1 + +static inline int +rte_timespec_get(struct timespec *now, int base) +{ + /* 100ns ticks from 1601-01-01 to 1970-01-01 */ + static const uint64_t EPOCH = 116444736000000000ULL; + static const uint64_t TICKS_PER_SEC = 10000000; + static const uint64_t NS_PER_TICK = 100; + + FILETIME ft; + uint64_t ticks; + + if (base != TIME_UTC) + return 0; + + GetSystemTimePreciseAsFileTime(&ft); + ticks = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime; + ticks -= EPOCH; + now->tv_sec = ticks / TICKS_PER_SEC; + now->tv_nsec = (ticks - now->tv_sec * TICKS_PER_SEC) * NS_PER_TICK; + return base; +} + +#define timespec_get(ts, base) rte_timespec_get(ts, base) + +#endif /* RTE_TOOLCHAIN_GCC */ + #endif /* _RTE_OS_SHIM_ */ -- 2.20.1