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