From 12d767123fea2f605c5adc57ffe80d40c6d11449 Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Wed, 20 Mar 2024 17:24:39 +0100 Subject: [PATCH 1/2] maint: Replace usage of folly in `clock.hpp` Signed-off-by: Julien Jerphanion --- cpp/arcticdb/util/clock.hpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/cpp/arcticdb/util/clock.hpp b/cpp/arcticdb/util/clock.hpp index 0ddc5481cc..a1da36b68b 100644 --- a/cpp/arcticdb/util/clock.hpp +++ b/cpp/arcticdb/util/clock.hpp @@ -9,29 +9,20 @@ #include -#include -#include -#ifndef CLOCK_REALTIME_COARSE #include -#endif namespace arcticdb::util { class SysClock { public: static entity::timestamp nanos_since_epoch() { - return (*folly::chrono::clock_gettime_ns)(CLOCK_REALTIME); - } -#if defined(_WIN32) || defined(__APPLE__) - static entity::timestamp coarse_nanos_since_epoch() { auto t = std::chrono::system_clock::now(); return std::chrono::duration_cast(t.time_since_epoch()).count(); } -#else static entity::timestamp coarse_nanos_since_epoch() { - return (*folly::chrono::clock_gettime_ns)(CLOCK_REALTIME_COARSE); + auto t = std::chrono::system_clock::now(); + return std::chrono::duration_cast(t.time_since_epoch()).count(); } -#endif }; From 58d55ff3b588ba21060c981275a220bd4252b6aa Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Thu, 4 Apr 2024 14:48:44 +0200 Subject: [PATCH 2/2] OS-specific implementations for coarse grained version Signed-off-by: Julien Jerphanion Co-authored-by: William Dealtry --- cpp/arcticdb/util/clock.hpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/cpp/arcticdb/util/clock.hpp b/cpp/arcticdb/util/clock.hpp index a1da36b68b..af9a66277b 100644 --- a/cpp/arcticdb/util/clock.hpp +++ b/cpp/arcticdb/util/clock.hpp @@ -9,19 +9,42 @@ #include +#include +#if defined(_WIN32) #include +#elif defined(__linux__) +#include +#elif defined(__APPLE__) +#include +#endif + namespace arcticdb::util { class SysClock { public: static entity::timestamp nanos_since_epoch() { - auto t = std::chrono::system_clock::now(); - return std::chrono::duration_cast(t.time_since_epoch()).count(); + auto now = std::chrono::system_clock::now(); + auto now_ns = std::chrono::time_point_cast(now); + return now_ns.time_since_epoch().count(); } static entity::timestamp coarse_nanos_since_epoch() { - auto t = std::chrono::system_clock::now(); - return std::chrono::duration_cast(t.time_since_epoch()).count(); +#if defined(__linux__) + timespec ts; + clock_gettime(CLOCK_REALTIME_COARSE, &ts); + return static_cast(ts.tv_sec) * 1000000000LL + ts.tv_nsec; +#elif defined(_WIN32) + return nanos_since_epoch(); +#elif defined(__APPLE__) + static mach_timebase_info_data_t info = {0, 0}; + if (info.denom == 0) { + mach_timebase_info(&info); + } + uint64_t now = mach_absolute_time(); + now *= info.numer; + now /= info.denom; + return now; +#endif } };