Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | /* Generic implementation of time calls. */ |
| 20 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 21 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 22 | #include <grpc/support/time.h> |
| 23 | #include <limits.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 26 | |
| 27 | int gpr_time_cmp(gpr_timespec a, gpr_timespec b) { |
| 28 | int cmp = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec); |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 29 | GPR_ASSERT(a.clock_type == b.clock_type); |
yang-g | 07429fa | 2017-03-29 10:57:31 -0700 | [diff] [blame] | 30 | if (cmp == 0 && a.tv_sec != INT64_MAX && a.tv_sec != INT64_MIN) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 31 | cmp = (a.tv_nsec > b.tv_nsec) - (a.tv_nsec < b.tv_nsec); |
| 32 | } |
| 33 | return cmp; |
| 34 | } |
| 35 | |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 36 | gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b) { |
| 37 | return gpr_time_cmp(a, b) < 0 ? a : b; |
| 38 | } |
| 39 | |
| 40 | gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b) { |
| 41 | return gpr_time_cmp(a, b) > 0 ? a : b; |
| 42 | } |
| 43 | |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 44 | gpr_timespec gpr_time_0(gpr_clock_type type) { |
| 45 | gpr_timespec out; |
| 46 | out.tv_sec = 0; |
| 47 | out.tv_nsec = 0; |
| 48 | out.clock_type = type; |
| 49 | return out; |
| 50 | } |
| 51 | |
| 52 | gpr_timespec gpr_inf_future(gpr_clock_type type) { |
| 53 | gpr_timespec out; |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 54 | out.tv_sec = INT64_MAX; |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 55 | out.tv_nsec = 0; |
| 56 | out.clock_type = type; |
| 57 | return out; |
| 58 | } |
| 59 | |
| 60 | gpr_timespec gpr_inf_past(gpr_clock_type type) { |
| 61 | gpr_timespec out; |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 62 | out.tv_sec = INT64_MIN; |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 63 | out.tv_nsec = 0; |
| 64 | out.clock_type = type; |
| 65 | return out; |
| 66 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 68 | static gpr_timespec to_seconds_from_sub_second_time(int64_t time_in_units, |
| 69 | int64_t units_per_sec, |
| 70 | gpr_clock_type type) { |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 71 | gpr_timespec out; |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 72 | if (time_in_units == INT64_MAX) { |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 73 | out = gpr_inf_future(type); |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 74 | } else if (time_in_units == INT64_MIN) { |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 75 | out = gpr_inf_past(type); |
| 76 | } else { |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 77 | if (time_in_units >= 0) { |
| 78 | out.tv_sec = time_in_units / units_per_sec; |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 79 | } else { |
thinkerou | b433830 | 2016-07-09 10:40:44 +0800 | [diff] [blame] | 80 | out.tv_sec = (-((units_per_sec - 1) - (time_in_units + units_per_sec)) / |
| 81 | units_per_sec) - |
| 82 | 1; |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 83 | } |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 84 | out.tv_nsec = (int32_t)((time_in_units - out.tv_sec * units_per_sec) * |
| 85 | GPR_NS_PER_SEC / units_per_sec); |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 86 | out.clock_type = type; |
| 87 | } |
| 88 | return out; |
| 89 | } |
| 90 | |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 91 | static gpr_timespec to_seconds_from_above_second_time(int64_t time_in_units, |
| 92 | int64_t secs_per_unit, |
| 93 | gpr_clock_type type) { |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 94 | gpr_timespec out; |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 95 | if (time_in_units >= INT64_MAX / secs_per_unit) { |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 96 | out = gpr_inf_future(type); |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 97 | } else if (time_in_units <= INT64_MIN / secs_per_unit) { |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 98 | out = gpr_inf_past(type); |
| 99 | } else { |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 100 | out.tv_sec = time_in_units * secs_per_unit; |
thinkerou | d4c1abb | 2016-06-21 10:25:08 +0800 | [diff] [blame] | 101 | out.tv_nsec = 0; |
| 102 | out.clock_type = type; |
| 103 | } |
| 104 | return out; |
| 105 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | |
murgatroid99 | 309830f | 2016-02-05 11:30:00 -0800 | [diff] [blame] | 107 | gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type type) { |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 108 | return to_seconds_from_sub_second_time(ns, GPR_NS_PER_SEC, type); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | } |
| 110 | |
murgatroid99 | 309830f | 2016-02-05 11:30:00 -0800 | [diff] [blame] | 111 | gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type type) { |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 112 | return to_seconds_from_sub_second_time(us, GPR_US_PER_SEC, type); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | } |
| 114 | |
murgatroid99 | 309830f | 2016-02-05 11:30:00 -0800 | [diff] [blame] | 115 | gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type type) { |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 116 | return to_seconds_from_sub_second_time(ms, GPR_MS_PER_SEC, type); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | } |
| 118 | |
murgatroid99 | 309830f | 2016-02-05 11:30:00 -0800 | [diff] [blame] | 119 | gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type type) { |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 120 | return to_seconds_from_sub_second_time(s, 1, type); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | } |
| 122 | |
murgatroid99 | 309830f | 2016-02-05 11:30:00 -0800 | [diff] [blame] | 123 | gpr_timespec gpr_time_from_minutes(int64_t m, gpr_clock_type type) { |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 124 | return to_seconds_from_above_second_time(m, 60, type); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 125 | } |
| 126 | |
murgatroid99 | 309830f | 2016-02-05 11:30:00 -0800 | [diff] [blame] | 127 | gpr_timespec gpr_time_from_hours(int64_t h, gpr_clock_type type) { |
thinkerou | 614e6f9 | 2016-07-09 10:11:28 +0800 | [diff] [blame] | 128 | return to_seconds_from_above_second_time(h, 3600, type); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b) { |
| 132 | gpr_timespec sum; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 133 | int64_t inc = 0; |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 134 | GPR_ASSERT(b.clock_type == GPR_TIMESPAN); |
| 135 | sum.clock_type = a.clock_type; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | sum.tv_nsec = a.tv_nsec + b.tv_nsec; |
Nicolas Noble | 65b0759 | 2015-02-23 15:28:04 -0800 | [diff] [blame] | 137 | if (sum.tv_nsec >= GPR_NS_PER_SEC) { |
| 138 | sum.tv_nsec -= GPR_NS_PER_SEC; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 139 | inc++; |
| 140 | } |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 141 | if (a.tv_sec == INT64_MAX || a.tv_sec == INT64_MIN) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | sum = a; |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 143 | } else if (b.tv_sec == INT64_MAX || |
| 144 | (b.tv_sec >= 0 && a.tv_sec >= INT64_MAX - b.tv_sec)) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 145 | sum = gpr_inf_future(sum.clock_type); |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 146 | } else if (b.tv_sec == INT64_MIN || |
| 147 | (b.tv_sec <= 0 && a.tv_sec <= INT64_MIN - b.tv_sec)) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 148 | sum = gpr_inf_past(sum.clock_type); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | } else { |
| 150 | sum.tv_sec = a.tv_sec + b.tv_sec; |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 151 | if (inc != 0 && sum.tv_sec == INT64_MAX - 1) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 152 | sum = gpr_inf_future(sum.clock_type); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 153 | } else { |
| 154 | sum.tv_sec += inc; |
| 155 | } |
| 156 | } |
| 157 | return sum; |
| 158 | } |
| 159 | |
| 160 | gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b) { |
| 161 | gpr_timespec diff; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 162 | int64_t dec = 0; |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 163 | if (b.clock_type == GPR_TIMESPAN) { |
| 164 | diff.clock_type = a.clock_type; |
| 165 | } else { |
| 166 | GPR_ASSERT(a.clock_type == b.clock_type); |
| 167 | diff.clock_type = GPR_TIMESPAN; |
| 168 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 169 | diff.tv_nsec = a.tv_nsec - b.tv_nsec; |
| 170 | if (diff.tv_nsec < 0) { |
Nicolas Noble | 65b0759 | 2015-02-23 15:28:04 -0800 | [diff] [blame] | 171 | diff.tv_nsec += GPR_NS_PER_SEC; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 172 | dec++; |
| 173 | } |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 174 | if (a.tv_sec == INT64_MAX || a.tv_sec == INT64_MIN) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 175 | diff = a; |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 176 | } else if (b.tv_sec == INT64_MIN || |
| 177 | (b.tv_sec <= 0 && a.tv_sec >= INT64_MAX + b.tv_sec)) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 178 | diff = gpr_inf_future(GPR_CLOCK_REALTIME); |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 179 | } else if (b.tv_sec == INT64_MAX || |
| 180 | (b.tv_sec >= 0 && a.tv_sec <= INT64_MIN + b.tv_sec)) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 181 | diff = gpr_inf_past(GPR_CLOCK_REALTIME); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | } else { |
| 183 | diff.tv_sec = a.tv_sec - b.tv_sec; |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 184 | if (dec != 0 && diff.tv_sec == INT64_MIN + 1) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 185 | diff = gpr_inf_past(GPR_CLOCK_REALTIME); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 186 | } else { |
| 187 | diff.tv_sec -= dec; |
| 188 | } |
| 189 | } |
| 190 | return diff; |
| 191 | } |
| 192 | |
| 193 | int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold) { |
| 194 | int cmp_ab; |
| 195 | |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 196 | GPR_ASSERT(a.clock_type == b.clock_type); |
| 197 | GPR_ASSERT(threshold.clock_type == GPR_TIMESPAN); |
| 198 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | cmp_ab = gpr_time_cmp(a, b); |
| 200 | if (cmp_ab == 0) return 1; |
| 201 | if (cmp_ab < 0) { |
| 202 | return gpr_time_cmp(gpr_time_sub(b, a), threshold) <= 0; |
| 203 | } else { |
| 204 | return gpr_time_cmp(gpr_time_sub(a, b), threshold) <= 0; |
| 205 | } |
| 206 | } |
| 207 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 208 | int32_t gpr_time_to_millis(gpr_timespec t) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 209 | if (t.tv_sec >= 2147483) { |
| 210 | if (t.tv_sec == 2147483 && t.tv_nsec < 648 * GPR_NS_PER_MS) { |
| 211 | return 2147483 * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS; |
| 212 | } |
| 213 | return 2147483647; |
| 214 | } else if (t.tv_sec <= -2147483) { |
| 215 | /* TODO(ctiller): correct handling here (it's so far in the past do we |
| 216 | care?) */ |
Nicolas "Pixel" Noble | 6ba50fd | 2015-01-13 19:25:37 -0800 | [diff] [blame] | 217 | return -2147483647; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 218 | } else { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 219 | return (int32_t)(t.tv_sec * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 222 | |
| 223 | double gpr_timespec_to_micros(gpr_timespec t) { |
murgatroid99 | 5e71d7a | 2015-06-19 12:24:44 -0700 | [diff] [blame] | 224 | return (double)t.tv_sec * GPR_US_PER_SEC + t.tv_nsec * 1e-3; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 225 | } |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 226 | |
| 227 | gpr_timespec gpr_convert_clock_type(gpr_timespec t, gpr_clock_type clock_type) { |
| 228 | if (t.clock_type == clock_type) { |
| 229 | return t; |
| 230 | } |
| 231 | |
yang-g | 1aba869 | 2017-03-28 21:41:02 -0700 | [diff] [blame] | 232 | if (t.tv_sec == INT64_MAX || t.tv_sec == INT64_MIN) { |
| 233 | t.clock_type = clock_type; |
| 234 | return t; |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if (clock_type == GPR_TIMESPAN) { |
| 238 | return gpr_time_sub(t, gpr_now(t.clock_type)); |
| 239 | } |
| 240 | |
| 241 | if (t.clock_type == GPR_TIMESPAN) { |
| 242 | return gpr_time_add(gpr_now(clock_type), t); |
| 243 | } |
| 244 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 245 | return gpr_time_add(gpr_now(clock_type), |
| 246 | gpr_time_sub(t, gpr_now(t.clock_type))); |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 247 | } |