29 #include <sys/syscall.h>
35 static struct timespec timespec_from_gpr(
gpr_timespec gts) {
37 if (
sizeof(time_t) <
sizeof(
int64_t)) {
41 rv.tv_sec =
static_cast<time_t
>(gts.tv_sec);
42 rv.tv_nsec = gts.tv_nsec;
46 #if _POSIX_TIMERS > 0 || defined(__OpenBSD__)
47 static gpr_timespec gpr_from_timespec(
struct timespec ts,
62 static const clockid_t clockid_for_gpr_clock[] = {CLOCK_MONOTONIC,
75 #if defined(GPR_BACKWARDS_COMPATIBILITY_MODE) && defined(__linux__)
77 syscall(SYS_clock_gettime, clockid_for_gpr_clock[clock_type], &
now);
79 clock_gettime(clockid_for_gpr_clock[clock_type], &
now);
81 return gpr_from_timespec(
now, clock_type);
87 #include <mach/mach.h>
88 #include <mach/mach_time.h>
91 static double g_time_scale = []() {
92 mach_timebase_info_data_t tb = {0, 1};
93 mach_timebase_info(&tb);
94 return static_cast<double>(tb.numer) /
static_cast<double>(tb.denom);
96 static uint64_t g_time_start = mach_absolute_time();
105 now.clock_type = clock;
111 gettimeofday(&now_tv,
nullptr);
112 now.tv_sec = now_tv.tv_sec;
113 now.tv_nsec = now_tv.tv_usec * 1000;
116 now_dbl = ((double)(mach_absolute_time() - g_time_start)) * g_time_scale;
118 now.tv_nsec = (
int32_t)(now_dbl - ((
double)
now.tv_sec) * 1e9);
143 #ifdef GPR_LOW_LEVEL_COUNTERS
147 #ifdef GPR_LOW_LEVEL_COUNTERS
148 __atomic_fetch_add(&gpr_now_call_count, 1, __ATOMIC_RELAXED);
163 struct timespec delta_ts;
175 delta_ts = timespec_from_gpr(delta);
176 ns_result =
nanosleep(&delta_ts,
nullptr);
177 if (ns_result == 0) {