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 /* Generic implementation of time calls. */
20
21 #include <grpc/support/port_platform.h>
22
23 #include <limits.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29
gpr_time_cmp(gpr_timespec a,gpr_timespec b)30 int gpr_time_cmp(gpr_timespec a, gpr_timespec b) {
31 int cmp = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
32 GPR_ASSERT(a.clock_type == b.clock_type);
33 if (cmp == 0 && a.tv_sec != INT64_MAX && a.tv_sec != INT64_MIN) {
34 cmp = (a.tv_nsec > b.tv_nsec) - (a.tv_nsec < b.tv_nsec);
35 }
36 return cmp;
37 }
38
gpr_time_min(gpr_timespec a,gpr_timespec b)39 gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b) {
40 return gpr_time_cmp(a, b) < 0 ? a : b;
41 }
42
gpr_time_max(gpr_timespec a,gpr_timespec b)43 gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b) {
44 return gpr_time_cmp(a, b) > 0 ? a : b;
45 }
46
gpr_time_0(gpr_clock_type type)47 gpr_timespec gpr_time_0(gpr_clock_type type) {
48 gpr_timespec out;
49 out.tv_sec = 0;
50 out.tv_nsec = 0;
51 out.clock_type = type;
52 return out;
53 }
54
gpr_inf_future(gpr_clock_type type)55 gpr_timespec gpr_inf_future(gpr_clock_type type) {
56 gpr_timespec out;
57 out.tv_sec = INT64_MAX;
58 out.tv_nsec = 0;
59 out.clock_type = type;
60 return out;
61 }
62
gpr_inf_past(gpr_clock_type type)63 gpr_timespec gpr_inf_past(gpr_clock_type type) {
64 gpr_timespec out;
65 out.tv_sec = INT64_MIN;
66 out.tv_nsec = 0;
67 out.clock_type = type;
68 return out;
69 }
70
to_seconds_from_sub_second_time(int64_t time_in_units,int64_t units_per_sec,gpr_clock_type type)71 static gpr_timespec to_seconds_from_sub_second_time(int64_t time_in_units,
72 int64_t units_per_sec,
73 gpr_clock_type type) {
74 gpr_timespec out;
75 if (time_in_units == INT64_MAX) {
76 out = gpr_inf_future(type);
77 } else if (time_in_units == INT64_MIN) {
78 out = gpr_inf_past(type);
79 } else {
80 if (time_in_units >= 0) {
81 out.tv_sec = time_in_units / units_per_sec;
82 } else {
83 out.tv_sec = (-((units_per_sec - 1) - (time_in_units + units_per_sec)) /
84 units_per_sec) -
85 1;
86 }
87 out.tv_nsec =
88 static_cast<int32_t>((time_in_units - out.tv_sec * units_per_sec) *
89 GPR_NS_PER_SEC / units_per_sec);
90 out.clock_type = type;
91 }
92 return out;
93 }
94
to_seconds_from_above_second_time(int64_t time_in_units,int64_t secs_per_unit,gpr_clock_type type)95 static gpr_timespec to_seconds_from_above_second_time(int64_t time_in_units,
96 int64_t secs_per_unit,
97 gpr_clock_type type) {
98 gpr_timespec out;
99 if (time_in_units >= INT64_MAX / secs_per_unit) {
100 out = gpr_inf_future(type);
101 } else if (time_in_units <= INT64_MIN / secs_per_unit) {
102 out = gpr_inf_past(type);
103 } else {
104 out.tv_sec = time_in_units * secs_per_unit;
105 out.tv_nsec = 0;
106 out.clock_type = type;
107 }
108 return out;
109 }
110
gpr_time_from_nanos(int64_t ns,gpr_clock_type clock_type)111 gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type clock_type) {
112 return to_seconds_from_sub_second_time(ns, GPR_NS_PER_SEC, clock_type);
113 }
114
gpr_time_from_micros(int64_t us,gpr_clock_type clock_type)115 gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type clock_type) {
116 return to_seconds_from_sub_second_time(us, GPR_US_PER_SEC, clock_type);
117 }
118
gpr_time_from_millis(int64_t ms,gpr_clock_type clock_type)119 gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type) {
120 return to_seconds_from_sub_second_time(ms, GPR_MS_PER_SEC, clock_type);
121 }
122
gpr_time_from_seconds(int64_t s,gpr_clock_type clock_type)123 gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type) {
124 return to_seconds_from_sub_second_time(s, 1, clock_type);
125 }
126
gpr_time_from_minutes(int64_t m,gpr_clock_type clock_type)127 gpr_timespec gpr_time_from_minutes(int64_t m, gpr_clock_type clock_type) {
128 return to_seconds_from_above_second_time(m, 60, clock_type);
129 }
130
gpr_time_from_hours(int64_t h,gpr_clock_type clock_type)131 gpr_timespec gpr_time_from_hours(int64_t h, gpr_clock_type clock_type) {
132 return to_seconds_from_above_second_time(h, 3600, clock_type);
133 }
134
gpr_time_add(gpr_timespec a,gpr_timespec b)135 gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b) {
136 gpr_timespec sum;
137 int64_t inc = 0;
138 GPR_ASSERT(b.clock_type == GPR_TIMESPAN);
139 // tv_nsec in a timespan is always +ve. -ve timespan is represented as (-ve
140 // tv_sec, +ve tv_nsec). For example, timespan = -2.5 seconds is represented
141 // as {-3, 5e8, GPR_TIMESPAN}
142 GPR_ASSERT(b.tv_nsec >= 0);
143 sum.clock_type = a.clock_type;
144 sum.tv_nsec = a.tv_nsec + b.tv_nsec;
145 if (sum.tv_nsec >= GPR_NS_PER_SEC) {
146 sum.tv_nsec -= GPR_NS_PER_SEC;
147 inc++;
148 }
149 if (a.tv_sec == INT64_MAX || a.tv_sec == INT64_MIN) {
150 sum = a;
151 } else if (b.tv_sec == INT64_MAX ||
152 (b.tv_sec >= 0 && a.tv_sec >= INT64_MAX - b.tv_sec)) {
153 sum = gpr_inf_future(sum.clock_type);
154 } else if (b.tv_sec == INT64_MIN ||
155 (b.tv_sec <= 0 && a.tv_sec <= INT64_MIN - b.tv_sec)) {
156 sum = gpr_inf_past(sum.clock_type);
157 } else {
158 sum.tv_sec = a.tv_sec + b.tv_sec;
159 if (inc != 0 && sum.tv_sec == INT64_MAX - 1) {
160 sum = gpr_inf_future(sum.clock_type);
161 } else {
162 sum.tv_sec += inc;
163 }
164 }
165 return sum;
166 }
167
gpr_time_sub(gpr_timespec a,gpr_timespec b)168 gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b) {
169 gpr_timespec diff;
170 int64_t dec = 0;
171 if (b.clock_type == GPR_TIMESPAN) {
172 diff.clock_type = a.clock_type;
173 // tv_nsec in a timespan is always +ve. -ve timespan is represented as (-ve
174 // tv_sec, +ve tv_nsec). For example, timespan = -2.5 seconds is represented
175 // as {-3, 5e8, GPR_TIMESPAN}
176 GPR_ASSERT(b.tv_nsec >= 0);
177 } else {
178 GPR_ASSERT(a.clock_type == b.clock_type);
179 diff.clock_type = GPR_TIMESPAN;
180 }
181 diff.tv_nsec = a.tv_nsec - b.tv_nsec;
182 if (diff.tv_nsec < 0) {
183 diff.tv_nsec += GPR_NS_PER_SEC;
184 dec++;
185 }
186 if (a.tv_sec == INT64_MAX || a.tv_sec == INT64_MIN) {
187 diff = a;
188 } else if (b.tv_sec == INT64_MIN ||
189 (b.tv_sec <= 0 && a.tv_sec >= INT64_MAX + b.tv_sec)) {
190 diff = gpr_inf_future(GPR_CLOCK_REALTIME);
191 } else if (b.tv_sec == INT64_MAX ||
192 (b.tv_sec >= 0 && a.tv_sec <= INT64_MIN + b.tv_sec)) {
193 diff = gpr_inf_past(GPR_CLOCK_REALTIME);
194 } else {
195 diff.tv_sec = a.tv_sec - b.tv_sec;
196 if (dec != 0 && diff.tv_sec == INT64_MIN + 1) {
197 diff = gpr_inf_past(GPR_CLOCK_REALTIME);
198 } else {
199 diff.tv_sec -= dec;
200 }
201 }
202 return diff;
203 }
204
gpr_time_similar(gpr_timespec a,gpr_timespec b,gpr_timespec threshold)205 int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold) {
206 int cmp_ab;
207
208 GPR_ASSERT(a.clock_type == b.clock_type);
209 GPR_ASSERT(threshold.clock_type == GPR_TIMESPAN);
210
211 cmp_ab = gpr_time_cmp(a, b);
212 if (cmp_ab == 0) return 1;
213 if (cmp_ab < 0) {
214 return gpr_time_cmp(gpr_time_sub(b, a), threshold) <= 0;
215 } else {
216 return gpr_time_cmp(gpr_time_sub(a, b), threshold) <= 0;
217 }
218 }
219
gpr_time_to_millis(gpr_timespec t)220 int32_t gpr_time_to_millis(gpr_timespec t) {
221 if (t.tv_sec >= 2147483) {
222 if (t.tv_sec == 2147483 && t.tv_nsec < 648 * GPR_NS_PER_MS) {
223 return 2147483 * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS;
224 }
225 return 2147483647;
226 } else if (t.tv_sec <= -2147483) {
227 /* TODO(ctiller): correct handling here (it's so far in the past do we
228 care?) */
229 return -2147483647;
230 } else {
231 return static_cast<int32_t>(t.tv_sec * GPR_MS_PER_SEC +
232 t.tv_nsec / GPR_NS_PER_MS);
233 }
234 }
235
gpr_timespec_to_micros(gpr_timespec t)236 double gpr_timespec_to_micros(gpr_timespec t) {
237 return static_cast<double>(t.tv_sec) * GPR_US_PER_SEC + t.tv_nsec * 1e-3;
238 }
239
gpr_convert_clock_type(gpr_timespec t,gpr_clock_type clock_type)240 gpr_timespec gpr_convert_clock_type(gpr_timespec t, gpr_clock_type clock_type) {
241 if (t.clock_type == clock_type) {
242 return t;
243 }
244
245 if (t.tv_sec == INT64_MAX || t.tv_sec == INT64_MIN) {
246 t.clock_type = clock_type;
247 return t;
248 }
249
250 if (clock_type == GPR_TIMESPAN) {
251 return gpr_time_sub(t, gpr_now(t.clock_type));
252 }
253
254 if (t.clock_type == GPR_TIMESPAN) {
255 return gpr_time_add(gpr_now(clock_type), t);
256 }
257
258 // If the given input hits this code, the same result is not guaranteed for
259 // the same input because it relies on `gpr_now` to calculate the difference
260 // between two different clocks. Please be careful when you want to use this
261 // function in unit tests. (e.g. https://github.com/grpc/grpc/pull/22655)
262 return gpr_time_add(gpr_now(clock_type),
263 gpr_time_sub(t, gpr_now(t.clock_type)));
264 }
265