| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <grpc/support/port_platform.h> |
| 16 |
|
| 17 |
#include "src/core/lib/gprpp/time.h" |
| 18 |
|
| 19 |
#include <atomic> |
| 20 |
#include <chrono> |
| 21 |
#include <limits> |
| 22 |
#include <string> |
| 23 |
#include <utility> |
| 24 |
|
| 25 |
#include "absl/strings/str_format.h" |
| 26 |
|
| 27 |
#include <grpc/impl/codegen/gpr_types.h> |
| 28 |
|
| 29 |
#include "src/core/lib/gprpp/no_destruct.h" |
| 30 |
|
| 31 |
// IWYU pragma: no_include <ratio> |
| 32 |
|
| 33 |
namespace grpc_core { |
| 34 |
|
| 35 |
namespace { |
| 36 |
|
| 37 |
std::atomic<int64_t> g_process_epoch_seconds; |
| 38 |
std::atomic<gpr_cycle_counter> g_process_epoch_cycles; |
| 39 |
|
| 40 |
class GprNowTimeSource final : public Timestamp::Source { |
| 41 |
public: |
| 42 |
Timestamp Now() override { |
| 43 |
return Timestamp::FromTimespecRoundDown(gpr_now(GPR_CLOCK_MONOTONIC)); |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
GPR_ATTRIBUTE_NOINLINE std::pair<int64_t, gpr_cycle_counter> InitTime() { |
| 48 |
gpr_cycle_counter cycles_start = 0; |
| 49 |
gpr_cycle_counter cycles_end = 0; |
| 50 |
int64_t process_epoch_seconds = 0; |
| 51 |
|
| 52 |
// Check the current time... if we end up with zero, try again after 100ms. |
| 53 |
// If it doesn't advance after sleeping for 1100ms, crash the process. |
| 54 |
for (int i = 0; i < 11; i++) { |
| 55 |
cycles_start = gpr_get_cycle_counter(); |
| 56 |
gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC); |
| 57 |
cycles_end = gpr_get_cycle_counter(); |
| 58 |
process_epoch_seconds = now.tv_sec - 1; |
| 59 |
if (process_epoch_seconds != 0) { |
| 60 |
break; |
| 61 |
} |
| 62 |
gpr_sleep_until(gpr_time_add(now, gpr_time_from_millis(100, GPR_TIMESPAN))); |
| 63 |
} |
| 64 |
|
| 65 |
// Time does not seem to be increasing from zero... |
| 66 |
GPR_ASSERT(process_epoch_seconds != 0); |
| 67 |
int64_t expected = 0; |
| 68 |
gpr_cycle_counter process_epoch_cycles = (cycles_start + cycles_end) / 2; |
| 69 |
GPR_ASSERT(process_epoch_cycles != 0); |
| 70 |
if (!g_process_epoch_seconds.compare_exchange_strong( |
| 71 |
expected, process_epoch_seconds, std::memory_order_relaxed, |
| 72 |
std::memory_order_relaxed)) { |
| 73 |
process_epoch_seconds = expected; |
| 74 |
do { |
| 75 |
process_epoch_cycles = |
| 76 |
g_process_epoch_cycles.load(std::memory_order_relaxed); |
| 77 |
} while (process_epoch_cycles == 0); |
| 78 |
} else { |
| 79 |
g_process_epoch_cycles.store(process_epoch_cycles, |
| 80 |
std::memory_order_relaxed); |
| 81 |
} |
| 82 |
return std::make_pair(process_epoch_seconds, process_epoch_cycles); |
| 83 |
} |
| 84 |
|
| 85 |
gpr_timespec StartTime() { |
| 86 |
int64_t sec = g_process_epoch_seconds.load(std::memory_order_relaxed); |
| 87 |
if (GPR_UNLIKELY(sec == 0)) sec = InitTime().first; |
| 88 |
return {sec, 0, GPR_CLOCK_MONOTONIC}; |
| 89 |
} |
| 90 |
|
| 91 |
gpr_cycle_counter StartCycleCounter() { |
| 92 |
gpr_cycle_counter cycles = |
| 93 |
g_process_epoch_cycles.load(std::memory_order_relaxed); |
| 94 |
if (GPR_UNLIKELY(cycles == 0)) cycles = InitTime().second; |
| 95 |
return cycles; |
| 96 |
} |
| 97 |
|
| 98 |
gpr_timespec MillisecondsAsTimespec(int64_t millis, gpr_clock_type clock_type) { |
| 99 |
// special-case infinities as Timestamp can be 32bit on some |
| 100 |
// platforms while gpr_time_from_millis always takes an int64_t. |
| 101 |
if (millis == std::numeric_limits<int64_t>::max()) { |
| 102 |
return gpr_inf_future(clock_type); |
| 103 |
} |
| 104 |
if (millis == std::numeric_limits<int64_t>::min()) { |
| 105 |
return gpr_inf_past(clock_type); |
| 106 |
} |
| 107 |
|
| 108 |
if (clock_type == GPR_TIMESPAN) { |
| 109 |
return gpr_time_from_millis(millis, GPR_TIMESPAN); |
| 110 |
} |
| 111 |
return gpr_time_add(gpr_convert_clock_type(StartTime(), clock_type), |
| 112 |
gpr_time_from_millis(millis, GPR_TIMESPAN)); |
| 113 |
} |
| 114 |
|
| 115 |
int64_t TimespanToMillisRoundUp(gpr_timespec ts) { |
| 116 |
GPR_ASSERT(ts.clock_type == GPR_TIMESPAN); |
| 117 |
double x = GPR_MS_PER_SEC * static_cast<double>(ts.tv_sec) + |
| 118 |
static_cast<double>(ts.tv_nsec) / GPR_NS_PER_MS + |
| 119 |
static_cast<double>(GPR_NS_PER_SEC - 1) / |
| 120 |
static_cast<double>(GPR_NS_PER_SEC); |
| 121 |
if (x <= static_cast<double>(std::numeric_limits<int64_t>::min())) { |
| 122 |
return std::numeric_limits<int64_t>::min(); |
| 123 |
} |
| 124 |
if (x >= static_cast<double>(std::numeric_limits<int64_t>::max())) { |
| 125 |
return std::numeric_limits<int64_t>::max(); |
| 126 |
} |
| 127 |
return static_cast<int64_t>(x); |
| 128 |
} |
| 129 |
|
| 130 |
int64_t TimespanToMillisRoundDown(gpr_timespec ts) { |
| 131 |
GPR_ASSERT(ts.clock_type == GPR_TIMESPAN); |
| 132 |
double x = GPR_MS_PER_SEC * static_cast<double>(ts.tv_sec) + |
| 133 |
static_cast<double>(ts.tv_nsec) / GPR_NS_PER_MS; |
| 134 |
if (x <= static_cast<double>(std::numeric_limits<int64_t>::min())) { |
| 135 |
return std::numeric_limits<int64_t>::min(); |
| 136 |
} |
| 137 |
if (x >= static_cast<double>(std::numeric_limits<int64_t>::max())) { |
| 138 |
return std::numeric_limits<int64_t>::max(); |
| 139 |
} |
| 140 |
return static_cast<int64_t>(x); |
| 141 |
} |
| 142 |
|
| 143 |
} // namespace |
| 144 |
|
| 145 |
thread_local Timestamp::Source* Timestamp::thread_local_time_source_{ |
| 146 |
NoDestructSingleton<GprNowTimeSource>::Get()}; |
| 147 |
|
| 148 |
Timestamp ScopedTimeCache::Now() { |
| 149 |
if (!cached_time_.has_value()) { |
| 150 |
previous()->InvalidateCache(); |
| 151 |
cached_time_ = previous()->Now(); |
| 152 |
} |
| 153 |
return cached_time_.value(); |
| 154 |
} |
| 155 |
|
| 156 |
Timestamp Timestamp::FromTimespecRoundUp(gpr_timespec ts) { |
| 157 |
return FromMillisecondsAfterProcessEpoch(TimespanToMillisRoundUp(gpr_time_sub( |
| 158 |
gpr_convert_clock_type(ts, GPR_CLOCK_MONOTONIC), StartTime()))); |
| 159 |
} |
| 160 |
|
| 161 |
Timestamp Timestamp::FromTimespecRoundDown(gpr_timespec ts) { |
| 162 |
return FromMillisecondsAfterProcessEpoch( |
| 163 |
TimespanToMillisRoundDown(gpr_time_sub( |
| 164 |
gpr_convert_clock_type(ts, GPR_CLOCK_MONOTONIC), StartTime()))); |
| 165 |
} |
| 166 |
|
| 167 |
Timestamp Timestamp::FromCycleCounterRoundUp(gpr_cycle_counter c) { |
| 168 |
return Timestamp::FromMillisecondsAfterProcessEpoch( |
| 169 |
TimespanToMillisRoundUp(gpr_cycle_counter_sub(c, StartCycleCounter()))); |
| 170 |
} |
| 171 |
|
| 172 |
Timestamp Timestamp::FromCycleCounterRoundDown(gpr_cycle_counter c) { |
| 173 |
return Timestamp::FromMillisecondsAfterProcessEpoch( |
| 174 |
TimespanToMillisRoundDown(gpr_cycle_counter_sub(c, StartCycleCounter()))); |
| 175 |
} |
| 176 |
|
| 177 |
gpr_timespec Timestamp::as_timespec(gpr_clock_type clock_type) const { |
| 178 |
return MillisecondsAsTimespec(millis_, clock_type); |
| 179 |
} |
| 180 |
|
| 181 |
std::string Timestamp::ToString() const { |
| 182 |
if (millis_ == std::numeric_limits<int64_t>::max()) { |
| 183 |
return "@∞"; |
| 184 |
} |
| 185 |
if (millis_ == std::numeric_limits<int64_t>::min()) { |
| 186 |
return "@-∞"; |
| 187 |
} |
| 188 |
return "@" + std::to_string(millis_) + "ms"; |
| 189 |
} |
| 190 |
|
| 191 |
gpr_timespec Duration::as_timespec() const { |
| 192 |
return MillisecondsAsTimespec(millis_, GPR_TIMESPAN); |
| 193 |
} |
| 194 |
|
| 195 |
Duration Duration::FromTimespec(gpr_timespec t) { |
| 196 |
return Duration::Milliseconds(TimespanToMillisRoundUp(t)); |
| 197 |
} |
| 198 |
|
| 199 |
std::string Duration::ToString() const { |
| 200 |
if (millis_ == std::numeric_limits<int64_t>::max()) { |
| 201 |
return "∞"; |
| 202 |
} |
| 203 |
if (millis_ == std::numeric_limits<int64_t>::min()) { |
| 204 |
return "-∞"; |
| 205 |
} |
| 206 |
return std::to_string(millis_) + "ms"; |
| 207 |
} |
| 208 |
|
| 209 |
std::string Duration::ToJsonString() const { |
| 210 |
gpr_timespec ts = as_timespec(); |
| 211 |
return absl::StrFormat("%d.%09ds", ts.tv_sec, ts.tv_nsec); |
| 212 |
} |
| 213 |
|
| 214 |
Duration::operator grpc_event_engine::experimental::EventEngine::Duration() |
| 215 |
const { |
| 216 |
return std::chrono::milliseconds( |
| 217 |
Clamp(millis_, std::numeric_limits<int64_t>::min() / GPR_NS_PER_MS, |
| 218 |
std::numeric_limits<int64_t>::max() / GPR_NS_PER_MS)); |
| 219 |
} |
| 220 |
|
| 221 |
void TestOnlySetProcessEpoch(gpr_timespec epoch) { |
| 222 |
g_process_epoch_seconds.store( |
| 223 |
gpr_convert_clock_type(epoch, GPR_CLOCK_MONOTONIC).tv_sec); |
| 224 |
} |
| 225 |
|
| 226 |
std::ostream& operator<<(std::ostream& out, Timestamp timestamp) { |
| 227 |
return out << timestamp.ToString(); |
| 228 |
} |
| 229 |
|
| 230 |
std::ostream& operator<<(std::ostream& out, Duration duration) { |
| 231 |
return out << duration.ToString(); |
| 232 |
} |
| 233 |
|
| 234 |
} // namespace grpc_core |