Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | f215f18 | 2016-02-01 16:16:14 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
Hongyu Chen | a0f149e | 2015-08-24 16:27:19 -0700 | [diff] [blame] | 35 | #include <src/core/support/time_precise.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 36 | |
| 37 | #ifdef GPR_POSIX_TIME |
| 38 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <time.h> |
| 41 | #include <unistd.h> |
Craig Tiller | 22a9139 | 2016-02-01 14:34:00 -0800 | [diff] [blame] | 42 | #ifdef __linux__ |
| 43 | #include <sys/syscall.h> |
| 44 | #endif |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 45 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <grpc/support/time.h> |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 47 | #include "src/core/support/block_annotate.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
David Klempner | 9b60fa3 | 2015-02-04 09:48:53 -0800 | [diff] [blame] | 49 | static struct timespec timespec_from_gpr(gpr_timespec gts) { |
| 50 | struct timespec rv; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 51 | if (sizeof(time_t) < sizeof(int64_t)) { |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 52 | /* fine to assert, as this is only used in gpr_sleep_until */ |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 53 | GPR_ASSERT(gts.tv_sec <= INT32_MAX && gts.tv_sec >= INT32_MIN); |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 54 | } |
| 55 | rv.tv_sec = (time_t)gts.tv_sec; |
David Klempner | 9b60fa3 | 2015-02-04 09:48:53 -0800 | [diff] [blame] | 56 | rv.tv_nsec = gts.tv_nsec; |
| 57 | return rv; |
| 58 | } |
| 59 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | #if _POSIX_TIMERS > 0 |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 61 | static gpr_timespec gpr_from_timespec(struct timespec ts, |
Vijay Pai | 7b080ba | 2015-09-29 22:22:36 +0000 | [diff] [blame] | 62 | gpr_clock_type clock_type) { |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 63 | /* |
| 64 | * timespec.tv_sec can have smaller size than gpr_timespec.tv_sec, |
| 65 | * but we are only using this function to implement gpr_now |
| 66 | * so there's no need to handle "infinity" values. |
| 67 | */ |
David Klempner | 9b60fa3 | 2015-02-04 09:48:53 -0800 | [diff] [blame] | 68 | gpr_timespec rv; |
| 69 | rv.tv_sec = ts.tv_sec; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 70 | rv.tv_nsec = (int32_t)ts.tv_nsec; |
Vijay Pai | 7b080ba | 2015-09-29 22:22:36 +0000 | [diff] [blame] | 71 | rv.clock_type = clock_type; |
David Klempner | 9b60fa3 | 2015-02-04 09:48:53 -0800 | [diff] [blame] | 72 | return rv; |
| 73 | } |
| 74 | |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 75 | /** maps gpr_clock_type --> clockid_t for clock_gettime */ |
Craig Tiller | 22a9139 | 2016-02-01 14:34:00 -0800 | [diff] [blame] | 76 | static const clockid_t clockid_for_gpr_clock[] = {CLOCK_MONOTONIC, |
| 77 | CLOCK_REALTIME}; |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 78 | |
Craig Tiller | 504ed59 | 2015-10-08 07:38:43 -0700 | [diff] [blame] | 79 | void gpr_time_init(void) { gpr_precise_clock_init(); } |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 80 | |
Vijay Pai | 7b080ba | 2015-09-29 22:22:36 +0000 | [diff] [blame] | 81 | gpr_timespec gpr_now(gpr_clock_type clock_type) { |
David Klempner | 9b60fa3 | 2015-02-04 09:48:53 -0800 | [diff] [blame] | 82 | struct timespec now; |
Vijay Pai | 7b080ba | 2015-09-29 22:22:36 +0000 | [diff] [blame] | 83 | GPR_ASSERT(clock_type != GPR_TIMESPAN); |
| 84 | if (clock_type == GPR_CLOCK_PRECISE) { |
Hongyu Chen | a0f149e | 2015-08-24 16:27:19 -0700 | [diff] [blame] | 85 | gpr_timespec ret; |
| 86 | gpr_precise_clock_now(&ret); |
| 87 | return ret; |
| 88 | } else { |
Craig Tiller | 71ea4a1 | 2016-02-04 15:06:41 -0800 | [diff] [blame] | 89 | #if defined(GPR_BACKWARDS_COMPATIBILITY_MODE) && defined(__linux__) |
Craig Tiller | 22a9139 | 2016-02-01 14:34:00 -0800 | [diff] [blame] | 90 | /* avoid ABI problems by invoking syscalls directly */ |
| 91 | syscall(SYS_clock_gettime, clockid_for_gpr_clock[clock_type], &now); |
| 92 | #else |
Vijay Pai | 7b080ba | 2015-09-29 22:22:36 +0000 | [diff] [blame] | 93 | clock_gettime(clockid_for_gpr_clock[clock_type], &now); |
Craig Tiller | 22a9139 | 2016-02-01 14:34:00 -0800 | [diff] [blame] | 94 | #endif |
Vijay Pai | 7b080ba | 2015-09-29 22:22:36 +0000 | [diff] [blame] | 95 | return gpr_from_timespec(now, clock_type); |
Hongyu Chen | a0f149e | 2015-08-24 16:27:19 -0700 | [diff] [blame] | 96 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | } |
| 98 | #else |
| 99 | /* For some reason Apple's OSes haven't implemented clock_gettime. */ |
vjpai | c234087 | 2015-02-13 11:12:36 -0800 | [diff] [blame] | 100 | |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 101 | #include <mach/mach.h> |
| 102 | #include <mach/mach_time.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 103 | #include <sys/time.h> |
vjpai | c234087 | 2015-02-13 11:12:36 -0800 | [diff] [blame] | 104 | |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 105 | static double g_time_scale; |
| 106 | static uint64_t g_time_start; |
| 107 | |
| 108 | void gpr_time_init(void) { |
Craig Tiller | f1bff01 | 2015-07-06 11:20:50 -0700 | [diff] [blame] | 109 | mach_timebase_info_data_t tb = {0, 1}; |
Craig Tiller | 1c4319a | 2015-10-08 08:43:37 -0700 | [diff] [blame] | 110 | gpr_precise_clock_init(); |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 111 | mach_timebase_info(&tb); |
| 112 | g_time_scale = tb.numer; |
| 113 | g_time_scale /= tb.denom; |
| 114 | g_time_start = mach_absolute_time(); |
| 115 | } |
| 116 | |
| 117 | gpr_timespec gpr_now(gpr_clock_type clock) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | gpr_timespec now; |
| 119 | struct timeval now_tv; |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 120 | double now_dbl; |
| 121 | |
Craig Tiller | 21038ce | 2015-07-14 06:55:47 -0700 | [diff] [blame] | 122 | now.clock_type = clock; |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 123 | switch (clock) { |
| 124 | case GPR_CLOCK_REALTIME: |
| 125 | gettimeofday(&now_tv, NULL); |
| 126 | now.tv_sec = now_tv.tv_sec; |
| 127 | now.tv_nsec = now_tv.tv_usec * 1000; |
| 128 | break; |
| 129 | case GPR_CLOCK_MONOTONIC: |
| 130 | now_dbl = (mach_absolute_time() - g_time_start) * g_time_scale; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 131 | now.tv_sec = (int64_t)(now_dbl * 1e-9); |
| 132 | now.tv_nsec = (int32_t)(now_dbl - ((double)now.tv_sec) * 1e9); |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 133 | break; |
Hongyu Chen | a0f149e | 2015-08-24 16:27:19 -0700 | [diff] [blame] | 134 | case GPR_CLOCK_PRECISE: |
| 135 | gpr_precise_clock_now(&now); |
| 136 | break; |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 137 | case GPR_TIMESPAN: |
| 138 | abort(); |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | return now; |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | void gpr_sleep_until(gpr_timespec until) { |
| 146 | gpr_timespec now; |
| 147 | gpr_timespec delta; |
David Klempner | 9b60fa3 | 2015-02-04 09:48:53 -0800 | [diff] [blame] | 148 | struct timespec delta_ts; |
vjpai | 50d6534 | 2015-09-24 17:25:35 -0700 | [diff] [blame] | 149 | int ns_result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 150 | |
| 151 | for (;;) { |
| 152 | /* We could simplify by using clock_nanosleep instead, but it might be |
| 153 | * slightly less portable. */ |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 154 | now = gpr_now(until.clock_type); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | if (gpr_time_cmp(until, now) <= 0) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | delta = gpr_time_sub(until, now); |
David Klempner | 9b60fa3 | 2015-02-04 09:48:53 -0800 | [diff] [blame] | 160 | delta_ts = timespec_from_gpr(delta); |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 161 | GRPC_SCHEDULING_START_BLOCKING_REGION; |
vjpai | 50d6534 | 2015-09-24 17:25:35 -0700 | [diff] [blame] | 162 | ns_result = nanosleep(&delta_ts, NULL); |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 163 | GRPC_SCHEDULING_END_BLOCKING_REGION; |
vjpai | 50d6534 | 2015-09-24 17:25:35 -0700 | [diff] [blame] | 164 | if (ns_result == 0) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | } |
| 168 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 169 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 170 | #endif /* GPR_POSIX_TIME */ |