From 775ef44ed8f6d52db99f570568092cd6effb870e Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Fri, 14 Aug 2020 19:51:45 +0300 Subject: [PATCH] Clocks: use CLOCK_[UPTIME|UPTIME_RAW|BOOTIME] when possible for Clocks::monotonic GitOrigin-RevId: d0aeb9d98d54b298093cba3b2ca97c6ab16eba28 --- tdutils/td/utils/port/Clocks.cpp | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tdutils/td/utils/port/Clocks.cpp b/tdutils/td/utils/port/Clocks.cpp index abee5d68b..a5ea19fa4 100644 --- a/tdutils/td/utils/port/Clocks.cpp +++ b/tdutils/td/utils/port/Clocks.cpp @@ -13,6 +13,42 @@ namespace td { double Clocks::monotonic() { // TODO write system specific functions, because std::chrono::steady_clock is steady only under Windows +#ifdef CLOCK_BOOTTIME + { + static bool skip = []() { + struct timespec spec; + return clock_gettime(CLOCK_BOOTTIME, &spec) != 0; + }(); + struct timespec spec; + if (!skip && clock_gettime(CLOCK_BOOTTIME, &spec) == 0) { + return static_cast(spec.tv_nsec) * 1e-9 + static_cast(spec.tv_sec); + } + } +#endif +#ifdef CLOCK_UPTIME_RAW + { + static bool skip = []() { + struct timespec spec; + return clock_gettime(CLOCK_UPTIME_RAW, &spec) != 0; + }(); + struct timespec spec; + if (!skip && clock_gettime(CLOCK_UPTIME_RAW, &spec) == 0) { + return static_cast(spec.tv_nsec) * 1e-9 + static_cast(spec.tv_sec); + } + } +#endif +#ifdef CLOCK_UPTIME + { + static bool skip = []() { + struct timespec spec; + return clock_gettime(CLOCK_UPTIME, &spec) != 0; + }(); + struct timespec spec; + if (!skip && clock_gettime(CLOCK_UPTIME, &spec) == 0) { + return static_cast(spec.tv_nsec) * 1e-9 + static_cast(spec.tv_sec); + } + } +#endif auto duration = std::chrono::steady_clock::now().time_since_epoch(); return static_cast(std::chrono::duration_cast(duration).count()) * 1e-9; }