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