Return the correct resolution for clock_getres
authorChristopher Dykes <cdykes@fb.com>
Wed, 9 Nov 2016 19:07:23 +0000 (11:07 -0800)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Wed, 9 Nov 2016 19:08:33 +0000 (11:08 -0800)
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

index 79c71b7a462fe0d0f8a5cb2feeb3f138978ec932..11eb1be4128d7d0055120e4ae61e107018589133 100755 (executable)
@@ -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;