3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/gpr/time_precise.h"
29 #include <sys/syscall.h>
31 #include <grpc/support/atm.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/time.h>
35 static struct timespec timespec_from_gpr(gpr_timespec gts) {
37 if (sizeof(time_t) < sizeof(int64_t)) {
38 /* fine to assert, as this is only used in gpr_sleep_until */
39 GPR_ASSERT(gts.tv_sec <= INT32_MAX && gts.tv_sec >= INT32_MIN);
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,
48 gpr_clock_type clock_type) {
50 * timespec.tv_sec can have smaller size than gpr_timespec.tv_sec,
51 * but we are only using this function to implement gpr_now
52 * so there's no need to handle "infinity" values.
55 rv.tv_sec = ts.tv_sec;
56 rv.tv_nsec = static_cast<int32_t>(ts.tv_nsec);
57 rv.clock_type = clock_type;
61 /** maps gpr_clock_type --> clockid_t for clock_gettime */
62 static const clockid_t clockid_for_gpr_clock[] = {CLOCK_MONOTONIC,
65 void gpr_time_init(void) { gpr_precise_clock_init(); }
67 static gpr_timespec now_impl(gpr_clock_type clock_type) {
69 GPR_ASSERT(clock_type != GPR_TIMESPAN);
70 if (clock_type == GPR_CLOCK_PRECISE) {
72 gpr_precise_clock_now(&ret);
75 #if defined(GPR_BACKWARDS_COMPATIBILITY_MODE) && defined(__linux__)
76 /* avoid ABI problems by invoking syscalls directly */
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);
85 /* For some reason Apple's OSes haven't implemented clock_gettime. */
87 #include <mach/mach.h>
88 #include <mach/mach_time.h>
91 static double g_time_scale;
92 static uint64_t g_time_start;
94 void gpr_time_init(void) {
95 mach_timebase_info_data_t tb = {0, 1};
96 gpr_precise_clock_init();
97 mach_timebase_info(&tb);
98 g_time_scale = tb.numer;
99 g_time_scale /= tb.denom;
100 g_time_start = mach_absolute_time();
103 static gpr_timespec now_impl(gpr_clock_type clock) {
105 struct timeval now_tv;
108 now.clock_type = clock;
110 case GPR_CLOCK_REALTIME:
111 // gettimeofday(...) function may return with a value whose tv_usec is
112 // greater than 1e6 on iOS The case is resolved with the guard at end of
114 gettimeofday(&now_tv, nullptr);
115 now.tv_sec = now_tv.tv_sec;
116 now.tv_nsec = now_tv.tv_usec * 1000;
118 case GPR_CLOCK_MONOTONIC:
119 now_dbl = ((double)(mach_absolute_time() - g_time_start)) * g_time_scale;
120 now.tv_sec = (int64_t)(now_dbl * 1e-9);
121 now.tv_nsec = (int32_t)(now_dbl - ((double)now.tv_sec) * 1e9);
123 case GPR_CLOCK_PRECISE:
124 gpr_precise_clock_now(&now);
130 // Guard the tv_nsec field in valid range for all clock types
131 while (GPR_UNLIKELY(now.tv_nsec >= 1e9)) {
135 while (GPR_UNLIKELY(now.tv_nsec < 0)) {
144 gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type) = now_impl;
146 #ifdef GPR_LOW_LEVEL_COUNTERS
147 gpr_atm gpr_now_call_count;
149 gpr_timespec gpr_now(gpr_clock_type clock_type) {
150 #ifdef GPR_LOW_LEVEL_COUNTERS
151 __atomic_fetch_add(&gpr_now_call_count, 1, __ATOMIC_RELAXED);
153 // validate clock type
154 GPR_ASSERT(clock_type == GPR_CLOCK_MONOTONIC ||
155 clock_type == GPR_CLOCK_REALTIME ||
156 clock_type == GPR_CLOCK_PRECISE);
157 gpr_timespec ts = gpr_now_impl(clock_type);
158 // tv_nsecs must be in the range [0, 1e9).
159 GPR_ASSERT(ts.tv_nsec >= 0 && ts.tv_nsec < 1e9);
163 void gpr_sleep_until(gpr_timespec until) {
166 struct timespec delta_ts;
170 /* We could simplify by using clock_nanosleep instead, but it might be
171 * slightly less portable. */
172 now = gpr_now(until.clock_type);
173 if (gpr_time_cmp(until, now) <= 0) {
177 delta = gpr_time_sub(until, now);
178 delta_ts = timespec_from_gpr(delta);
179 ns_result = nanosleep(&delta_ts, nullptr);
180 if (ns_result == 0) {
186 #endif /* GPR_POSIX_TIME */