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