1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/gpr/time_precise.h"
22
23 #ifdef GPR_POSIX_TIME
24
25 #include <stdlib.h>
26 #include <time.h>
27 #include <unistd.h>
28 #ifdef __linux__
29 #include <sys/syscall.h>
30 #endif
31 #include <grpc/support/atm.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/time.h>
34
35 #include "src/core/lib/gprpp/crash.h"
36
timespec_from_gpr(gpr_timespec gts)37 static struct timespec timespec_from_gpr(gpr_timespec gts) {
38 struct timespec rv;
39 if (sizeof(time_t) < sizeof(int64_t)) {
40 // fine to assert, as this is only used in gpr_sleep_until
41 GPR_ASSERT(gts.tv_sec <= INT32_MAX && gts.tv_sec >= INT32_MIN);
42 }
43 rv.tv_sec = static_cast<time_t>(gts.tv_sec);
44 rv.tv_nsec = gts.tv_nsec;
45 return rv;
46 }
47
48 #if _POSIX_TIMERS > 0 || defined(__OpenBSD__)
gpr_from_timespec(struct timespec ts,gpr_clock_type clock_type)49 static gpr_timespec gpr_from_timespec(struct timespec ts,
50 gpr_clock_type clock_type) {
51 //
52 // timespec.tv_sec can have smaller size than gpr_timespec.tv_sec,
53 // but we are only using this function to implement gpr_now
54 // so there's no need to handle "infinity" values.
55 //
56 gpr_timespec rv;
57 rv.tv_sec = ts.tv_sec;
58 rv.tv_nsec = static_cast<int32_t>(ts.tv_nsec);
59 rv.clock_type = clock_type;
60 return rv;
61 }
62
63 /// maps gpr_clock_type --> clockid_t for clock_gettime
64 static const clockid_t clockid_for_gpr_clock[] = {CLOCK_MONOTONIC,
65 CLOCK_REALTIME};
66
gpr_time_init(void)67 void gpr_time_init(void) { gpr_precise_clock_init(); }
68
now_impl(gpr_clock_type clock_type)69 static gpr_timespec now_impl(gpr_clock_type clock_type) {
70 struct timespec now;
71 GPR_ASSERT(clock_type != GPR_TIMESPAN);
72 if (clock_type == GPR_CLOCK_PRECISE) {
73 gpr_timespec ret;
74 gpr_precise_clock_now(&ret);
75 return ret;
76 } else {
77 #if defined(GPR_BACKWARDS_COMPATIBILITY_MODE) && defined(__linux__)
78 // avoid ABI problems by invoking syscalls directly
79 syscall(SYS_clock_gettime, clockid_for_gpr_clock[clock_type], &now);
80 #else
81 clock_gettime(clockid_for_gpr_clock[clock_type], &now);
82 #endif
83 if (clock_type == GPR_CLOCK_MONOTONIC) {
84 // Add 5 seconds arbitrarily: avoids weird conditions in gprpp/time.cc
85 // when there's a small number of seconds returned.
86 now.tv_sec += 5;
87 }
88 return gpr_from_timespec(now, clock_type);
89 }
90 }
91 #else
92 // For some reason Apple's OSes haven't implemented clock_gettime.
93
94 #include <mach/mach.h>
95 #include <mach/mach_time.h>
96 #include <sys/time.h>
97
__anon548b3e250102() 98 static double g_time_scale = []() {
99 mach_timebase_info_data_t tb = {0, 1};
100 mach_timebase_info(&tb);
101 return static_cast<double>(tb.numer) / static_cast<double>(tb.denom);
102 }();
103 static uint64_t g_time_start = mach_absolute_time();
104
gpr_time_init(void)105 void gpr_time_init(void) { gpr_precise_clock_init(); }
106
now_impl(gpr_clock_type clock)107 static gpr_timespec now_impl(gpr_clock_type clock) {
108 gpr_timespec now;
109 struct timeval now_tv;
110 double now_dbl;
111
112 now.clock_type = clock;
113 switch (clock) {
114 case GPR_CLOCK_REALTIME:
115 // gettimeofday(...) function may return with a value whose tv_usec is
116 // greater than 1e6 on iOS The case is resolved with the guard at end of
117 // this function.
118 gettimeofday(&now_tv, nullptr);
119 now.tv_sec = now_tv.tv_sec;
120 now.tv_nsec = now_tv.tv_usec * 1000;
121 break;
122 case GPR_CLOCK_MONOTONIC:
123 // Add 5 seconds arbitrarily: avoids weird conditions in gprpp/time.cc
124 // when there's a small number of seconds returned.
125 now_dbl = 5.0e9 +
126 ((double)(mach_absolute_time() - g_time_start)) * g_time_scale;
127 now.tv_sec = (int64_t)(now_dbl * 1e-9);
128 now.tv_nsec = (int32_t)(now_dbl - ((double)now.tv_sec) * 1e9);
129 break;
130 case GPR_CLOCK_PRECISE:
131 gpr_precise_clock_now(&now);
132 break;
133 case GPR_TIMESPAN:
134 abort();
135 }
136
137 // Guard the tv_nsec field in valid range for all clock types
138 while (GPR_UNLIKELY(now.tv_nsec >= 1e9)) {
139 now.tv_sec++;
140 now.tv_nsec -= 1e9;
141 }
142 while (GPR_UNLIKELY(now.tv_nsec < 0)) {
143 now.tv_sec--;
144 now.tv_nsec += 1e9;
145 }
146
147 return now;
148 }
149 #endif
150
151 gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type) = now_impl;
152
gpr_now(gpr_clock_type clock_type)153 gpr_timespec gpr_now(gpr_clock_type clock_type) {
154 // validate clock type
155 GPR_ASSERT(clock_type == GPR_CLOCK_MONOTONIC ||
156 clock_type == GPR_CLOCK_REALTIME ||
157 clock_type == GPR_CLOCK_PRECISE);
158 gpr_timespec ts = gpr_now_impl(clock_type);
159 // tv_nsecs must be in the range [0, 1e9).
160 GPR_ASSERT(ts.tv_nsec >= 0 && ts.tv_nsec < 1e9);
161 return ts;
162 }
163
gpr_sleep_until(gpr_timespec until)164 void gpr_sleep_until(gpr_timespec until) {
165 gpr_timespec now;
166 gpr_timespec delta;
167 struct timespec delta_ts;
168 int ns_result;
169
170 for (;;) {
171 // We could simplify by using clock_nanosleep instead, but it might be
172 // slightly less portable.
173 now = gpr_now(until.clock_type);
174 if (gpr_time_cmp(until, now) <= 0) {
175 return;
176 }
177
178 delta = gpr_time_sub(until, now);
179 delta_ts = timespec_from_gpr(delta);
180 ns_result = nanosleep(&delta_ts, nullptr);
181 if (ns_result == 0) {
182 break;
183 }
184 }
185 }
186
187 #endif // GPR_POSIX_TIME
188