]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
time: use mach_absolute_time() for monotonic time
authorKefu Chai <kchai@redhat.com>
Sat, 9 Sep 2017 17:19:28 +0000 (01:19 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 16 Sep 2017 02:43:32 +0000 (10:43 +0800)
* use mach_absolute_time() for monotonic time
  mach_absolute_time() is faster and monotonic, see
  https://developer.apple.com/library/content/qa/qa1398/_index.html

  for its implementation, see
  https://opensource.apple.com/source/xnu/xnu-3248.60.10/libsyscall/wrappers/mach_absolute_time.s

  it's using rdtsc.
* and remove unnecessary headers from ceph_time.h

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/ceph_time.cc
src/common/ceph_time.h

index be7c5f889c6a3cf3412e62d5408c728f3306b5fc..cd41d0581ed8327d744011f23f7bd0eba11f5889 100644 (file)
 #include "ceph_time.h"
 #include "config.h"
 
-#if defined(DARWIN)
+#if defined(__APPLE__)
+#include <mach/mach.h>
+#include <mach/mach_time.h>
+
+#ifndef NSEC_PER_SEC
+#define NSEC_PER_SEC 1000000000ULL
+#endif
+
 int clock_gettime(int clk_id, struct timespec *tp)
 {
-  if (clk_id == CALENDAR_CLOCK) {
+  if (clk_id == CLOCK_REALTIME) {
     // gettimeofday is much faster than clock_get_time
     struct timeval now;
     int ret = gettimeofday(&now, NULL);
@@ -28,15 +35,14 @@ int clock_gettime(int clk_id, struct timespec *tp)
     tp->tv_sec = now.tv_sec;
     tp->tv_nsec = now.tv_usec * 1000L;
   } else {
-    clock_serv_t cclock;
-    mach_timespec_t mts;
-
-    host_get_clock_service(mach_host_self(), clk_id, &cclock);
-    clock_get_time(cclock, &mts);
-    mach_port_deallocate(mach_task_self(), cclock);
-
-    tp->tv_sec = mts.tv_sec;
-    tp->tv_nsec = mts.tv_nsec;
+    uint64_t t = mach_absolute_time();
+    static mach_timebase_info_data_t timebase_info;
+    if (timebase_info.denom == 0) {
+      (void)mach_timebase_info(&timebase_info);
+    }
+    auto nanos = t * timebase_info.numer / timebase_info.denom;
+    tp->tv_sec = nanos / NSEC_PER_SEC;
+    tp->tv_nsec = nanos - (tp->tv_sec * NSEC_PER_SEC);
   }
   return 0;
 }
index 6845b09730dcb642eac8a93ed4143881582b19b2..49707e98c3ac1d8495110685d9f82aea72456de6 100644 (file)
 
 #if defined(__APPLE__)
 #include <sys/_types/_timespec.h>
-#include <mach/mach.h>
-#include <mach/clock.h>
 
-#define CLOCK_REALTIME CALENDAR_CLOCK
-#define CLOCK_MONOTONIC SYSTEM_CLOCK
 #define CLOCK_REALTIME_COARSE CLOCK_REALTIME
 #define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC