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