| 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 |
#ifndef GRPC_CORE_LIB_GPRPP_TIME_H |
| 16 |
#define GRPC_CORE_LIB_GPRPP_TIME_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <stdint.h> |
| 21 |
|
| 22 |
#include <limits> |
| 23 |
#include <ostream> |
| 24 |
#include <string> |
| 25 |
|
| 26 |
#include "absl/types/optional.h" |
| 27 |
|
| 28 |
#include <grpc/event_engine/event_engine.h> |
| 29 |
#include <grpc/impl/codegen/gpr_types.h> |
| 30 |
#include <grpc/support/log.h> |
| 31 |
#include <grpc/support/time.h> |
| 32 |
|
| 33 |
#include "src/core/lib/gpr/time_precise.h" |
| 34 |
#include "src/core/lib/gpr/useful.h" |
| 35 |
|
| 36 |
#define GRPC_LOG_EVERY_N_SEC(n, format, ...) \ |
| 37 |
do { \ |
| 38 |
static std::atomic<uint64_t> prev{0}; \ |
| 39 |
uint64_t now = grpc_core::Timestamp::FromTimespecRoundDown( \ |
| 40 |
gpr_now(GPR_CLOCK_MONOTONIC)) \ |
| 41 |
.milliseconds_after_process_epoch(); \ |
| 42 |
if ((now - prev.exchange(now)) > (n)*1000) { \ |
| 43 |
gpr_log(GPR_INFO, format, __VA_ARGS__); \ |
| 44 |
} \ |
| 45 |
} while (0) |
| 46 |
|
| 47 |
namespace grpc_core { |
| 48 |
|
| 49 |
namespace time_detail { |
| 50 |
|
| 51 |
inline int64_t MillisAdd(int64_t a, int64_t b) { |
| 52 |
if (a == std::numeric_limits<int64_t>::max() || |
| 53 |
b == std::numeric_limits<int64_t>::max()) { |
| 54 |
return std::numeric_limits<int64_t>::max(); |
| 55 |
} |
| 56 |
if (a == std::numeric_limits<int64_t>::min() || |
| 57 |
b == std::numeric_limits<int64_t>::min()) { |
| 58 |
return std::numeric_limits<int64_t>::min(); |
| 59 |
} |
| 60 |
return SaturatingAdd(a, b); |
| 61 |
} |
| 62 |
|
| 63 |
constexpr inline int64_t MillisMul(int64_t millis, int64_t mul) { |
| 64 |
return millis >= std::numeric_limits<int64_t>::max() / mul |
| 65 |
? std::numeric_limits<int64_t>::max() |
| 66 |
: millis <= std::numeric_limits<int64_t>::min() / mul |
| 67 |
? std::numeric_limits<int64_t>::min() |
| 68 |
: millis * mul; |
| 69 |
} |
| 70 |
|
| 71 |
} // namespace time_detail |
| 72 |
|
| 73 |
class Duration; |
| 74 |
|
| 75 |
// Timestamp represents a discrete point in time. |
| 76 |
class Timestamp { |
| 77 |
public: |
| 78 |
// Base interface for time providers. |
| 79 |
class Source { |
| 80 |
public: |
| 81 |
// Return the current time. |
| 82 |
virtual Timestamp Now() = 0; |
| 83 |
virtual void InvalidateCache() {} |
| 84 |
|
| 85 |
protected: |
| 86 |
// We don't delete through this interface, so non-virtual dtor is fine. |
| 87 |
~Source() = default; |
| 88 |
}; |
| 89 |
|
| 90 |
class ScopedSource : public Source { |
| 91 |
public: |
| 92 |
ScopedSource() : previous_(thread_local_time_source_) { |
| 93 |
thread_local_time_source_ = this; |
| 94 |
} |
| 95 |
ScopedSource(const ScopedSource&) = delete; |
| 96 |
ScopedSource& operator=(const ScopedSource&) = delete; |
| 97 |
void InvalidateCache() override { previous_->InvalidateCache(); } |
| 98 |
|
| 99 |
protected: |
| 100 |
~ScopedSource() { thread_local_time_source_ = previous_; } |
| 101 |
Source* previous() const { return previous_; } |
| 102 |
|
| 103 |
private: |
| 104 |
Source* const previous_; |
| 105 |
}; |
| 106 |
|
| 107 |
constexpr Timestamp() = default; |
| 108 |
// Constructs a Timestamp from a gpr_timespec. |
| 109 |
static Timestamp FromTimespecRoundDown(gpr_timespec t); |
| 110 |
static Timestamp FromTimespecRoundUp(gpr_timespec t); |
| 111 |
|
| 112 |
// Construct a Timestamp from a gpr_cycle_counter. |
| 113 |
static Timestamp FromCycleCounterRoundUp(gpr_cycle_counter c); |
| 114 |
static Timestamp FromCycleCounterRoundDown(gpr_cycle_counter c); |
| 115 |
|
| 116 |
static Timestamp Now() { return thread_local_time_source_->Now(); } |
| 117 |
|
| 118 |
static constexpr Timestamp FromMillisecondsAfterProcessEpoch(int64_t millis) { |
| 119 |
return Timestamp(millis); |
| 120 |
} |
| 121 |
|
| 122 |
static constexpr Timestamp ProcessEpoch() { return Timestamp(0); } |
| 123 |
|
| 124 |
static constexpr Timestamp InfFuture() { |
| 125 |
return Timestamp(std::numeric_limits<int64_t>::max()); |
| 126 |
} |
| 127 |
|
| 128 |
static constexpr Timestamp InfPast() { |
| 129 |
return Timestamp(std::numeric_limits<int64_t>::min()); |
| 130 |
} |
| 131 |
|
| 132 |
constexpr bool operator==(Timestamp other) const { |
| 133 |
return millis_ == other.millis_; |
| 134 |
} |
| 135 |
constexpr bool operator!=(Timestamp other) const { |
| 136 |
return millis_ != other.millis_; |
| 137 |
} |
| 138 |
constexpr bool operator<(Timestamp other) const { |
| 139 |
return millis_ < other.millis_; |
| 140 |
} |
| 141 |
constexpr bool operator<=(Timestamp other) const { |
| 142 |
return millis_ <= other.millis_; |
| 143 |
} |
| 144 |
constexpr bool operator>(Timestamp other) const { |
| 145 |
return millis_ > other.millis_; |
| 146 |
} |
| 147 |
constexpr bool operator>=(Timestamp other) const { |
| 148 |
return millis_ >= other.millis_; |
| 149 |
} |
| 150 |
Timestamp& operator+=(Duration duration); |
| 151 |
|
| 152 |
bool is_process_epoch() const { return millis_ == 0; } |
| 153 |
|
| 154 |
uint64_t milliseconds_after_process_epoch() const { return millis_; } |
| 155 |
|
| 156 |
gpr_timespec as_timespec(gpr_clock_type type) const; |
| 157 |
|
| 158 |
std::string ToString() const; |
| 159 |
|
| 160 |
private: |
| 161 |
explicit constexpr Timestamp(int64_t millis) : millis_(millis) {} |
| 162 |
|
| 163 |
int64_t millis_ = 0; |
| 164 |
static thread_local Timestamp::Source* thread_local_time_source_; |
| 165 |
}; |
| 166 |
|
| 167 |
class ScopedTimeCache final : public Timestamp::ScopedSource { |
| 168 |
public: |
| 169 |
Timestamp Now() override; |
| 170 |
|
| 171 |
void InvalidateCache() override { |
| 172 |
cached_time_ = absl::nullopt; |
| 173 |
Timestamp::ScopedSource::InvalidateCache(); |
| 174 |
} |
| 175 |
void TestOnlySetNow(Timestamp now) { cached_time_ = now; } |
| 176 |
|
| 177 |
private: |
| 178 |
absl::optional<Timestamp> cached_time_; |
| 179 |
}; |
| 180 |
|
| 181 |
// Duration represents a span of time. |
| 182 |
class Duration { |
| 183 |
public: |
| 184 |
constexpr Duration() noexcept : millis_(0) {} |
| 185 |
|
| 186 |
static Duration FromTimespec(gpr_timespec t); |
| 187 |
static Duration FromSecondsAndNanoseconds(int64_t seconds, int32_t nanos); |
| 188 |
static Duration FromSecondsAsDouble(double seconds); |
| 189 |
|
| 190 |
static constexpr Duration Zero() { return Duration(0); } |
| 191 |
|
| 192 |
// Smallest representatable positive duration. |
| 193 |
static constexpr Duration Epsilon() { return Duration(1); } |
| 194 |
|
| 195 |
static constexpr Duration NegativeInfinity() { |
| 196 |
return Duration(std::numeric_limits<int64_t>::min()); |
| 197 |
} |
| 198 |
|
| 199 |
static constexpr Duration Infinity() { |
| 200 |
return Duration(std::numeric_limits<int64_t>::max()); |
| 201 |
} |
| 202 |
|
| 203 |
static constexpr Duration Hours(int64_t hours) { |
| 204 |
return Minutes(time_detail::MillisMul(hours, 60)); |
| 205 |
} |
| 206 |
|
| 207 |
static constexpr Duration Minutes(int64_t minutes) { |
| 208 |
return Seconds(time_detail::MillisMul(minutes, 60)); |
| 209 |
} |
| 210 |
|
| 211 |
static constexpr Duration Seconds(int64_t seconds) { |
| 212 |
return Milliseconds(time_detail::MillisMul(seconds, GPR_MS_PER_SEC)); |
| 213 |
} |
| 214 |
|
| 215 |
static constexpr Duration Milliseconds(int64_t millis) { |
| 216 |
return Duration(millis); |
| 217 |
} |
| 218 |
|
| 219 |
static constexpr Duration MicrosecondsRoundDown(int64_t micros) { |
| 220 |
return Duration(micros / GPR_US_PER_MS); |
| 221 |
} |
| 222 |
|
| 223 |
static constexpr Duration NanosecondsRoundDown(int64_t nanos) { |
| 224 |
return Duration(nanos / GPR_NS_PER_MS); |
| 225 |
} |
| 226 |
|
| 227 |
static constexpr Duration MicrosecondsRoundUp(int64_t micros) { |
| 228 |
return Duration(micros / GPR_US_PER_MS + (micros % GPR_US_PER_MS != 0)); |
| 229 |
} |
| 230 |
|
| 231 |
static constexpr Duration NanosecondsRoundUp(int64_t nanos) { |
| 232 |
return Duration(nanos / GPR_NS_PER_MS + (nanos % GPR_NS_PER_MS != 0)); |
| 233 |
} |
| 234 |
|
| 235 |
constexpr bool operator==(Duration other) const { |
| 236 |
return millis_ == other.millis_; |
| 237 |
} |
| 238 |
constexpr bool operator!=(Duration other) const { |
| 239 |
return millis_ != other.millis_; |
| 240 |
} |
| 241 |
constexpr bool operator<(Duration other) const { |
| 242 |
return millis_ < other.millis_; |
| 243 |
} |
| 244 |
constexpr bool operator<=(Duration other) const { |
| 245 |
return millis_ <= other.millis_; |
| 246 |
} |
| 247 |
constexpr bool operator>(Duration other) const { |
| 248 |
return millis_ > other.millis_; |
| 249 |
} |
| 250 |
constexpr bool operator>=(Duration other) const { |
| 251 |
return millis_ >= other.millis_; |
| 252 |
} |
| 253 |
Duration& operator/=(int64_t divisor) { |
| 254 |
if (millis_ == std::numeric_limits<int64_t>::max()) { |
| 255 |
*this = divisor < 0 ? NegativeInfinity() : Infinity(); |
| 256 |
} else if (millis_ == std::numeric_limits<int64_t>::min()) { |
| 257 |
*this = divisor < 0 ? Infinity() : NegativeInfinity(); |
| 258 |
} else { |
| 259 |
millis_ /= divisor; |
| 260 |
} |
| 261 |
return *this; |
| 262 |
} |
| 263 |
Duration& operator*=(double multiplier); |
| 264 |
Duration& operator+=(Duration other) { |
| 265 |
millis_ += other.millis_; |
| 266 |
return *this; |
| 267 |
} |
| 268 |
|
| 269 |
constexpr int64_t millis() const { return millis_; } |
| 270 |
double seconds() const { return static_cast<double>(millis_) / 1000.0; } |
| 271 |
|
| 272 |
// NOLINTNEXTLINE: google-explicit-constructor |
| 273 |
operator grpc_event_engine::experimental::EventEngine::Duration() const; |
| 274 |
|
| 275 |
gpr_timespec as_timespec() const; |
| 276 |
|
| 277 |
std::string ToString() const; |
| 278 |
|
| 279 |
// Returns the duration in the JSON form corresponding to a |
| 280 |
// google.protobuf.Duration proto, as defined here: |
| 281 |
// https://developers.google.com/protocol-buffers/docs/proto3#json |
| 282 |
std::string ToJsonString() const; |
| 283 |
|
| 284 |
private: |
| 285 |
explicit constexpr Duration(int64_t millis) : millis_(millis) {} |
| 286 |
|
| 287 |
int64_t millis_; |
| 288 |
}; |
| 289 |
|
| 290 |
inline Duration operator+(Duration lhs, Duration rhs) { |
| 291 |
return Duration::Milliseconds( |
| 292 |
time_detail::MillisAdd(lhs.millis(), rhs.millis())); |
| 293 |
} |
| 294 |
|
| 295 |
inline Duration operator-(Duration lhs, Duration rhs) { |
| 296 |
return Duration::Milliseconds( |
| 297 |
time_detail::MillisAdd(lhs.millis(), -rhs.millis())); |
| 298 |
} |
| 299 |
|
| 300 |
inline Timestamp operator+(Timestamp lhs, Duration rhs) { |
| 301 |
return Timestamp::FromMillisecondsAfterProcessEpoch(time_detail::MillisAdd( |
| 302 |
lhs.milliseconds_after_process_epoch(), rhs.millis())); |
| 303 |
} |
| 304 |
|
| 305 |
inline Timestamp operator-(Timestamp lhs, Duration rhs) { |
| 306 |
return Timestamp::FromMillisecondsAfterProcessEpoch(time_detail::MillisAdd( |
| 307 |
lhs.milliseconds_after_process_epoch(), -rhs.millis())); |
| 308 |
} |
| 309 |
|
| 310 |
inline Timestamp operator+(Duration lhs, Timestamp rhs) { return rhs + lhs; } |
| 311 |
|
| 312 |
inline Duration operator-(Timestamp lhs, Timestamp rhs) { |
| 313 |
return Duration::Milliseconds( |
| 314 |
time_detail::MillisAdd(lhs.milliseconds_after_process_epoch(), |
| 315 |
-rhs.milliseconds_after_process_epoch())); |
| 316 |
} |
| 317 |
|
| 318 |
inline Duration operator*(Duration lhs, double rhs) { |
| 319 |
if (lhs == Duration::Infinity()) { |
| 320 |
return rhs < 0 ? Duration::NegativeInfinity() : Duration::Infinity(); |
| 321 |
} |
| 322 |
if (lhs == Duration::NegativeInfinity()) { |
| 323 |
return rhs < 0 ? Duration::Infinity() : Duration::NegativeInfinity(); |
| 324 |
} |
| 325 |
return Duration::FromSecondsAsDouble(lhs.millis() * rhs / 1000.0); |
| 326 |
} |
| 327 |
|
| 328 |
inline Duration operator*(double lhs, Duration rhs) { return rhs * lhs; } |
| 329 |
|
| 330 |
inline Duration operator/(Duration lhs, int64_t rhs) { |
| 331 |
lhs /= rhs; |
| 332 |
return lhs; |
| 333 |
} |
| 334 |
|
| 335 |
inline Duration Duration::FromSecondsAndNanoseconds(int64_t seconds, |
| 336 |
int32_t nanos) { |
| 337 |
return Seconds(seconds) + NanosecondsRoundDown(nanos); |
| 338 |
} |
| 339 |
|
| 340 |
inline Duration Duration::FromSecondsAsDouble(double seconds) { |
| 341 |
double millis = seconds * 1000.0; |
| 342 |
if (millis >= static_cast<double>(std::numeric_limits<int64_t>::max())) { |
| 343 |
return Infinity(); |
| 344 |
} |
| 345 |
if (millis <= static_cast<double>(std::numeric_limits<int64_t>::min())) { |
| 346 |
return NegativeInfinity(); |
| 347 |
} |
| 348 |
return Milliseconds(static_cast<int64_t>(millis)); |
| 349 |
} |
| 350 |
|
| 351 |
inline Duration& Duration::operator*=(double multiplier) { |
| 352 |
*this = *this * multiplier; |
| 353 |
return *this; |
| 354 |
} |
| 355 |
|
| 356 |
inline Timestamp& Timestamp::operator+=(Duration duration) { |
| 357 |
return *this = (*this + duration); |
| 358 |
} |
| 359 |
|
| 360 |
void TestOnlySetProcessEpoch(gpr_timespec epoch); |
| 361 |
|
| 362 |
std::ostream& operator<<(std::ostream& out, Timestamp timestamp); |
| 363 |
std::ostream& operator<<(std::ostream& out, Duration duration); |
| 364 |
|
| 365 |
} // namespace grpc_core |
| 366 |
|
| 367 |
#endif // GRPC_CORE_LIB_GPRPP_TIME_H |