From 248a4ac3b2eda07bc98d571984c74bdb7114a944 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Wed, 9 Nov 2016 11:07:23 -0800 Subject: [PATCH] Return the correct resolution for clock_getres Summary: When the implementation of `clock_gettime` for `REALTIME` and `MONOTONIC` was switched to use `std::chrono`, this didn't get switched over to the resolution of the new implementation. Reviewed By: yfeldblum Differential Revision: D4150297 fbshipit-source-id: a4578af85cae538a3ebb6da7e759058927c31d3e --- folly/portability/Time.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/folly/portability/Time.cpp b/folly/portability/Time.cpp index 79c71b7a..11eb1be4 100755 --- a/folly/portability/Time.cpp +++ b/folly/portability/Time.cpp @@ -171,26 +171,22 @@ extern "C" int clock_getres(clockid_t clock_id, struct timespec* res) { return -1; } + static constexpr size_t kNsPerSec = 1000000000; switch (clock_id) { + case CLOCK_REALTIME: { + constexpr auto perSec = double(std::chrono::system_clock::period::num) / + std::chrono::system_clock::period::den; + res->tv_sec = time_t(perSec); + res->tv_nsec = time_t(perSec * kNsPerSec); + return 0; + } case CLOCK_MONOTONIC: { - LARGE_INTEGER freq = performanceFrequency(); - if (freq.QuadPart == -1) { - errno = EINVAL; - return -1; - } - - static constexpr size_t kNsPerSec = 1000000000; - - res->tv_sec = 0; - res->tv_nsec = (long)((kNsPerSec + (freq.QuadPart >> 1)) / freq.QuadPart); - if (res->tv_nsec < 1) { - res->tv_nsec = 1; - } - + constexpr auto perSec = double(std::chrono::steady_clock::period::num) / + std::chrono::steady_clock::period::den; + res->tv_sec = time_t(perSec); + res->tv_nsec = time_t(perSec * kNsPerSec); return 0; } - - case CLOCK_REALTIME: case CLOCK_PROCESS_CPUTIME_ID: case CLOCK_THREAD_CPUTIME_ID: { DWORD adj, timeIncrement; -- 2.34.1