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