#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);
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;
}
#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