OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 /* Generic implementation of time calls. */ |
| 35 |
| 36 #include <grpc/support/time.h> |
| 37 #include <limits.h> |
| 38 #include <stdio.h> |
| 39 #include <string.h> |
| 40 #include <grpc/support/log.h> |
| 41 |
| 42 int gpr_time_cmp(gpr_timespec a, gpr_timespec b) { |
| 43 int cmp = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec); |
| 44 GPR_ASSERT(a.clock_type == b.clock_type); |
| 45 if (cmp == 0) { |
| 46 cmp = (a.tv_nsec > b.tv_nsec) - (a.tv_nsec < b.tv_nsec); |
| 47 } |
| 48 return cmp; |
| 49 } |
| 50 |
| 51 gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b) { |
| 52 return gpr_time_cmp(a, b) < 0 ? a : b; |
| 53 } |
| 54 |
| 55 gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b) { |
| 56 return gpr_time_cmp(a, b) > 0 ? a : b; |
| 57 } |
| 58 |
| 59 gpr_timespec gpr_time_0(gpr_clock_type type) { |
| 60 gpr_timespec out; |
| 61 out.tv_sec = 0; |
| 62 out.tv_nsec = 0; |
| 63 out.clock_type = type; |
| 64 return out; |
| 65 } |
| 66 |
| 67 gpr_timespec gpr_inf_future(gpr_clock_type type) { |
| 68 gpr_timespec out; |
| 69 out.tv_sec = INT64_MAX; |
| 70 out.tv_nsec = 0; |
| 71 out.clock_type = type; |
| 72 return out; |
| 73 } |
| 74 |
| 75 gpr_timespec gpr_inf_past(gpr_clock_type type) { |
| 76 gpr_timespec out; |
| 77 out.tv_sec = INT64_MIN; |
| 78 out.tv_nsec = 0; |
| 79 out.clock_type = type; |
| 80 return out; |
| 81 } |
| 82 |
| 83 /* TODO(ctiller): consider merging _nanos, _micros, _millis into a single |
| 84 function for maintainability. Similarly for _seconds, _minutes, and _hours */ |
| 85 |
| 86 gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type type) { |
| 87 gpr_timespec result; |
| 88 result.clock_type = type; |
| 89 if (ns == INT64_MAX) { |
| 90 result = gpr_inf_future(type); |
| 91 } else if (ns == INT64_MIN) { |
| 92 result = gpr_inf_past(type); |
| 93 } else if (ns >= 0) { |
| 94 result.tv_sec = ns / GPR_NS_PER_SEC; |
| 95 result.tv_nsec = (int32_t)(ns - result.tv_sec * GPR_NS_PER_SEC); |
| 96 } else { |
| 97 /* Calculation carefully formulated to avoid any possible under/overflow. */ |
| 98 result.tv_sec = (-(999999999 - (ns + GPR_NS_PER_SEC)) / GPR_NS_PER_SEC) - 1; |
| 99 result.tv_nsec = (int32_t)(ns - result.tv_sec * GPR_NS_PER_SEC); |
| 100 } |
| 101 return result; |
| 102 } |
| 103 |
| 104 gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type type) { |
| 105 gpr_timespec result; |
| 106 result.clock_type = type; |
| 107 if (us == INT64_MAX) { |
| 108 result = gpr_inf_future(type); |
| 109 } else if (us == INT64_MIN) { |
| 110 result = gpr_inf_past(type); |
| 111 } else if (us >= 0) { |
| 112 result.tv_sec = us / 1000000; |
| 113 result.tv_nsec = (int32_t)((us - result.tv_sec * 1000000) * 1000); |
| 114 } else { |
| 115 /* Calculation carefully formulated to avoid any possible under/overflow. */ |
| 116 result.tv_sec = (-(999999 - (us + 1000000)) / 1000000) - 1; |
| 117 result.tv_nsec = (int32_t)((us - result.tv_sec * 1000000) * 1000); |
| 118 } |
| 119 return result; |
| 120 } |
| 121 |
| 122 gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type type) { |
| 123 gpr_timespec result; |
| 124 result.clock_type = type; |
| 125 if (ms == INT64_MAX) { |
| 126 result = gpr_inf_future(type); |
| 127 } else if (ms == INT64_MIN) { |
| 128 result = gpr_inf_past(type); |
| 129 } else if (ms >= 0) { |
| 130 result.tv_sec = ms / 1000; |
| 131 result.tv_nsec = (int32_t)((ms - result.tv_sec * 1000) * 1000000); |
| 132 } else { |
| 133 /* Calculation carefully formulated to avoid any possible under/overflow. */ |
| 134 result.tv_sec = (-(999 - (ms + 1000)) / 1000) - 1; |
| 135 result.tv_nsec = (int32_t)((ms - result.tv_sec * 1000) * 1000000); |
| 136 } |
| 137 return result; |
| 138 } |
| 139 |
| 140 gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type type) { |
| 141 gpr_timespec result; |
| 142 result.clock_type = type; |
| 143 if (s == INT64_MAX) { |
| 144 result = gpr_inf_future(type); |
| 145 } else if (s == INT64_MIN) { |
| 146 result = gpr_inf_past(type); |
| 147 } else { |
| 148 result.tv_sec = s; |
| 149 result.tv_nsec = 0; |
| 150 } |
| 151 return result; |
| 152 } |
| 153 |
| 154 gpr_timespec gpr_time_from_minutes(int64_t m, gpr_clock_type type) { |
| 155 gpr_timespec result; |
| 156 result.clock_type = type; |
| 157 if (m >= INT64_MAX / 60) { |
| 158 result = gpr_inf_future(type); |
| 159 } else if (m <= INT64_MIN / 60) { |
| 160 result = gpr_inf_past(type); |
| 161 } else { |
| 162 result.tv_sec = m * 60; |
| 163 result.tv_nsec = 0; |
| 164 } |
| 165 return result; |
| 166 } |
| 167 |
| 168 gpr_timespec gpr_time_from_hours(int64_t h, gpr_clock_type type) { |
| 169 gpr_timespec result; |
| 170 result.clock_type = type; |
| 171 if (h >= INT64_MAX / 3600) { |
| 172 result = gpr_inf_future(type); |
| 173 } else if (h <= INT64_MIN / 3600) { |
| 174 result = gpr_inf_past(type); |
| 175 } else { |
| 176 result.tv_sec = h * 3600; |
| 177 result.tv_nsec = 0; |
| 178 } |
| 179 return result; |
| 180 } |
| 181 |
| 182 gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b) { |
| 183 gpr_timespec sum; |
| 184 int64_t inc = 0; |
| 185 GPR_ASSERT(b.clock_type == GPR_TIMESPAN); |
| 186 sum.clock_type = a.clock_type; |
| 187 sum.tv_nsec = a.tv_nsec + b.tv_nsec; |
| 188 if (sum.tv_nsec >= GPR_NS_PER_SEC) { |
| 189 sum.tv_nsec -= GPR_NS_PER_SEC; |
| 190 inc++; |
| 191 } |
| 192 if (a.tv_sec == INT64_MAX || a.tv_sec == INT64_MIN) { |
| 193 sum = a; |
| 194 } else if (b.tv_sec == INT64_MAX || |
| 195 (b.tv_sec >= 0 && a.tv_sec >= INT64_MAX - b.tv_sec)) { |
| 196 sum = gpr_inf_future(sum.clock_type); |
| 197 } else if (b.tv_sec == INT64_MIN || |
| 198 (b.tv_sec <= 0 && a.tv_sec <= INT64_MIN - b.tv_sec)) { |
| 199 sum = gpr_inf_past(sum.clock_type); |
| 200 } else { |
| 201 sum.tv_sec = a.tv_sec + b.tv_sec; |
| 202 if (inc != 0 && sum.tv_sec == INT64_MAX - 1) { |
| 203 sum = gpr_inf_future(sum.clock_type); |
| 204 } else { |
| 205 sum.tv_sec += inc; |
| 206 } |
| 207 } |
| 208 return sum; |
| 209 } |
| 210 |
| 211 gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b) { |
| 212 gpr_timespec diff; |
| 213 int64_t dec = 0; |
| 214 if (b.clock_type == GPR_TIMESPAN) { |
| 215 diff.clock_type = a.clock_type; |
| 216 } else { |
| 217 GPR_ASSERT(a.clock_type == b.clock_type); |
| 218 diff.clock_type = GPR_TIMESPAN; |
| 219 } |
| 220 diff.tv_nsec = a.tv_nsec - b.tv_nsec; |
| 221 if (diff.tv_nsec < 0) { |
| 222 diff.tv_nsec += GPR_NS_PER_SEC; |
| 223 dec++; |
| 224 } |
| 225 if (a.tv_sec == INT64_MAX || a.tv_sec == INT64_MIN) { |
| 226 diff = a; |
| 227 } else if (b.tv_sec == INT64_MIN || |
| 228 (b.tv_sec <= 0 && a.tv_sec >= INT64_MAX + b.tv_sec)) { |
| 229 diff = gpr_inf_future(GPR_CLOCK_REALTIME); |
| 230 } else if (b.tv_sec == INT64_MAX || |
| 231 (b.tv_sec >= 0 && a.tv_sec <= INT64_MIN + b.tv_sec)) { |
| 232 diff = gpr_inf_past(GPR_CLOCK_REALTIME); |
| 233 } else { |
| 234 diff.tv_sec = a.tv_sec - b.tv_sec; |
| 235 if (dec != 0 && diff.tv_sec == INT64_MIN + 1) { |
| 236 diff = gpr_inf_past(GPR_CLOCK_REALTIME); |
| 237 } else { |
| 238 diff.tv_sec -= dec; |
| 239 } |
| 240 } |
| 241 return diff; |
| 242 } |
| 243 |
| 244 int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold) { |
| 245 int cmp_ab; |
| 246 |
| 247 GPR_ASSERT(a.clock_type == b.clock_type); |
| 248 GPR_ASSERT(threshold.clock_type == GPR_TIMESPAN); |
| 249 |
| 250 cmp_ab = gpr_time_cmp(a, b); |
| 251 if (cmp_ab == 0) return 1; |
| 252 if (cmp_ab < 0) { |
| 253 return gpr_time_cmp(gpr_time_sub(b, a), threshold) <= 0; |
| 254 } else { |
| 255 return gpr_time_cmp(gpr_time_sub(a, b), threshold) <= 0; |
| 256 } |
| 257 } |
| 258 |
| 259 int32_t gpr_time_to_millis(gpr_timespec t) { |
| 260 if (t.tv_sec >= 2147483) { |
| 261 if (t.tv_sec == 2147483 && t.tv_nsec < 648 * GPR_NS_PER_MS) { |
| 262 return 2147483 * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS; |
| 263 } |
| 264 return 2147483647; |
| 265 } else if (t.tv_sec <= -2147483) { |
| 266 /* TODO(ctiller): correct handling here (it's so far in the past do we |
| 267 care?) */ |
| 268 return -2147483647; |
| 269 } else { |
| 270 return (int32_t)(t.tv_sec * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS); |
| 271 } |
| 272 } |
| 273 |
| 274 double gpr_timespec_to_micros(gpr_timespec t) { |
| 275 return (double)t.tv_sec * GPR_US_PER_SEC + t.tv_nsec * 1e-3; |
| 276 } |
| 277 |
| 278 gpr_timespec gpr_convert_clock_type(gpr_timespec t, gpr_clock_type clock_type) { |
| 279 if (t.clock_type == clock_type) { |
| 280 return t; |
| 281 } |
| 282 |
| 283 if (t.tv_nsec == 0) { |
| 284 if (t.tv_sec == INT64_MAX) { |
| 285 t.clock_type = clock_type; |
| 286 return t; |
| 287 } |
| 288 if (t.tv_sec == INT64_MIN) { |
| 289 t.clock_type = clock_type; |
| 290 return t; |
| 291 } |
| 292 } |
| 293 |
| 294 if (clock_type == GPR_TIMESPAN) { |
| 295 return gpr_time_sub(t, gpr_now(t.clock_type)); |
| 296 } |
| 297 |
| 298 if (t.clock_type == GPR_TIMESPAN) { |
| 299 return gpr_time_add(gpr_now(clock_type), t); |
| 300 } |
| 301 |
| 302 return gpr_time_add(gpr_now(clock_type), |
| 303 gpr_time_sub(t, gpr_now(t.clock_type))); |
| 304 } |
OLD | NEW |