52 #ifndef ABSL_USE_CYCLECLOCK_FOR_GET_CURRENT_TIME_NANOS 53 #if ABSL_USE_UNSCALED_CYCLECLOCK 54 #define ABSL_USE_CYCLECLOCK_FOR_GET_CURRENT_TIME_NANOS 1 56 #define ABSL_USE_CYCLECLOCK_FOR_GET_CURRENT_TIME_NANOS 0 60 #if defined(__APPLE__) || defined(_WIN32) 61 #include "absl/time/internal/get_current_time_chrono.inc" 63 #include "absl/time/internal/get_current_time_posix.inc" 67 #ifndef GET_CURRENT_TIME_NANOS_FROM_SYSTEM 68 #define GET_CURRENT_TIME_NANOS_FROM_SYSTEM() \ 69 ::absl::time_internal::GetCurrentTimeNanosFromSystem() 72 #if !ABSL_USE_CYCLECLOCK_FOR_GET_CURRENT_TIME_NANOS 78 #else // Use the cyclecounter-based implementation below. 81 #ifndef GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW 82 #define GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW() \ 83 ::absl::time_internal::UnscaledCycleClockWrapperForGetCurrentTime::Now() 87 static int64_t stats_initializations;
88 static int64_t stats_reinitializations;
89 static int64_t stats_calibrations;
90 static int64_t stats_slow_paths;
91 static int64_t stats_fast_slow_paths;
94 namespace time_internal {
97 class UnscaledCycleClockWrapperForGetCurrentTime {
112 static int64_t GetCurrentTimeNanosFromKernel(uint64_t last_cycleclock,
113 uint64_t *cycleclock) {
117 static std::atomic<uint64_t> approx_syscall_time_in_cycles{10 * 1000};
119 uint64_t local_approx_syscall_time_in_cycles =
120 approx_syscall_time_in_cycles.load(std::memory_order_relaxed);
122 int64_t current_time_nanos_from_system;
123 uint64_t before_cycles;
124 uint64_t after_cycles;
125 uint64_t elapsed_cycles;
128 before_cycles = GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW();
130 after_cycles = GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW();
132 elapsed_cycles = after_cycles - before_cycles;
133 if (elapsed_cycles >= local_approx_syscall_time_in_cycles &&
136 if (local_approx_syscall_time_in_cycles < 1000 * 1000) {
137 local_approx_syscall_time_in_cycles =
138 (local_approx_syscall_time_in_cycles + 1) << 1;
140 approx_syscall_time_in_cycles.store(
141 local_approx_syscall_time_in_cycles,
142 std::memory_order_relaxed);
144 }
while (elapsed_cycles >= local_approx_syscall_time_in_cycles ||
145 last_cycleclock - after_cycles < (static_cast<uint64_t>(1) << 16));
149 static std::atomic<uint32_t> seen_smaller{ 0 };
153 if ((local_approx_syscall_time_in_cycles >> 1) < elapsed_cycles) {
155 seen_smaller.store(0, std::memory_order_relaxed);
156 }
else if (seen_smaller.fetch_add(1, std::memory_order_relaxed) >= 3) {
158 const uint64_t new_approximation =
159 local_approx_syscall_time_in_cycles -
160 (local_approx_syscall_time_in_cycles >> 3);
161 approx_syscall_time_in_cycles.store(new_approximation,
162 std::memory_order_relaxed);
163 seen_smaller.store(0, std::memory_order_relaxed);
166 *cycleclock = after_cycles;
167 return current_time_nanos_from_system;
184 static inline uint64_t SeqAcquire(std::atomic<uint64_t> *seq) {
185 uint64_t x = seq->fetch_add(1, std::memory_order_relaxed);
192 std::atomic_thread_fence(std::memory_order_release);
199 static inline void SeqRelease(std::atomic<uint64_t> *seq, uint64_t x) {
202 seq->store(x, std::memory_order_release);
208 enum { kScale = 30 };
214 static const uint64_t kMinNSBetweenSamples = 2000 << 20;
218 static_assert(((kMinNSBetweenSamples << (kScale + 1)) >> (kScale + 1)) ==
219 kMinNSBetweenSamples,
220 "cannot represent kMaxBetweenSamplesNSScaled");
226 static std::atomic<uint64_t> seq(0);
229 struct TimeSampleAtomic {
230 std::atomic<uint64_t> raw_ns;
231 std::atomic<uint64_t> base_ns;
232 std::atomic<uint64_t> base_cycles;
233 std::atomic<uint64_t> nsscaled_per_cycle;
236 std::atomic<uint64_t> min_cycles_per_sample;
242 uint64_t base_cycles;
243 uint64_t nsscaled_per_cycle;
244 uint64_t min_cycles_per_sample;
247 static struct TimeSampleAtomic last_sample;
254 static void ReadTimeSampleAtomic(
const struct TimeSampleAtomic *atomic,
255 struct TimeSample *sample) {
256 sample->base_ns = atomic->base_ns.load(std::memory_order_relaxed);
257 sample->base_cycles = atomic->base_cycles.load(std::memory_order_relaxed);
258 sample->nsscaled_per_cycle =
259 atomic->nsscaled_per_cycle.load(std::memory_order_relaxed);
260 sample->min_cycles_per_sample =
261 atomic->min_cycles_per_sample.load(std::memory_order_relaxed);
262 sample->raw_ns = atomic->raw_ns.load(std::memory_order_relaxed);
298 uint64_t base_cycles;
299 uint64_t nsscaled_per_cycle;
300 uint64_t min_cycles_per_sample;
310 uint64_t now_cycles = GET_CURRENT_TIME_NANOS_CYCLECLOCK_NOW();
315 seq_read0 = seq.load(std::memory_order_acquire);
317 base_ns = last_sample.base_ns.load(std::memory_order_relaxed);
318 base_cycles = last_sample.base_cycles.load(std::memory_order_relaxed);
320 last_sample.nsscaled_per_cycle.load(std::memory_order_relaxed);
321 min_cycles_per_sample =
322 last_sample.min_cycles_per_sample.load(std::memory_order_relaxed);
327 std::atomic_thread_fence(std::memory_order_acquire);
333 seq_read1 = seq.load(std::memory_order_relaxed);
346 uint64_t delta_cycles = now_cycles - base_cycles;
347 if (seq_read0 == seq_read1 && (seq_read0 & 1) == 0 &&
348 delta_cycles < min_cycles_per_sample) {
349 return base_ns + ((delta_cycles * nsscaled_per_cycle) >> kScale);
351 return GetCurrentTimeNanosSlowPath();
357 static uint64_t SafeDivideAndScale(uint64_t
a, uint64_t
b) {
360 int safe_shift = kScale;
361 while (((a << safe_shift) >> safe_shift) != a) {
364 uint64_t scaled_b = b >> (kScale - safe_shift);
365 uint64_t quotient = 0;
367 quotient = (a << safe_shift) / scaled_b;
372 static uint64_t UpdateLastSample(
373 uint64_t now_cycles, uint64_t now_ns, uint64_t delta_cycles,
388 static int64_t GetCurrentTimeNanosSlowPath()
LOCKS_EXCLUDED(lock) {
395 static uint64_t last_now_cycles;
397 uint64_t now_ns = GetCurrentTimeNanosFromKernel(last_now_cycles, &now_cycles);
398 last_now_cycles = now_cycles;
400 uint64_t estimated_base_ns;
404 struct TimeSample sample;
405 ReadTimeSampleAtomic(&last_sample, &sample);
410 uint64_t delta_cycles = now_cycles - sample.base_cycles;
411 if (delta_cycles < sample.min_cycles_per_sample) {
414 estimated_base_ns = sample.base_ns +
415 ((delta_cycles * sample.nsscaled_per_cycle) >> kScale);
416 stats_fast_slow_paths++;
419 UpdateLastSample(now_cycles, now_ns, delta_cycles, &sample);
424 return estimated_base_ns;
430 static uint64_t UpdateLastSample(uint64_t now_cycles, uint64_t now_ns,
431 uint64_t delta_cycles,
432 const struct TimeSample *sample)
434 uint64_t estimated_base_ns = now_ns;
435 uint64_t lock_value = SeqAcquire(&seq);
441 if (sample->raw_ns == 0 ||
442 sample->raw_ns + static_cast<uint64_t>(5) * 1000 * 1000 * 1000 < now_ns ||
443 now_ns < sample->raw_ns || now_cycles < sample->base_cycles) {
445 last_sample.raw_ns.store(now_ns, std::memory_order_relaxed);
446 last_sample.base_ns.store(estimated_base_ns, std::memory_order_relaxed);
447 last_sample.base_cycles.store(now_cycles, std::memory_order_relaxed);
448 last_sample.nsscaled_per_cycle.store(0, std::memory_order_relaxed);
449 last_sample.min_cycles_per_sample.store(0, std::memory_order_relaxed);
450 stats_initializations++;
451 }
else if (sample->raw_ns + 500 * 1000 * 1000 < now_ns &&
452 sample->base_cycles + 100 < now_cycles) {
454 if (sample->nsscaled_per_cycle != 0) {
457 uint64_t estimated_scaled_ns;
461 estimated_scaled_ns = (delta_cycles >> s) * sample->nsscaled_per_cycle;
462 }
while (estimated_scaled_ns / sample->nsscaled_per_cycle !=
463 (delta_cycles >> s));
464 estimated_base_ns = sample->base_ns +
465 (estimated_scaled_ns >> (kScale - s));
470 uint64_t ns = now_ns - sample->raw_ns;
471 uint64_t measured_nsscaled_per_cycle = SafeDivideAndScale(ns, delta_cycles);
473 uint64_t assumed_next_sample_delta_cycles =
474 SafeDivideAndScale(kMinNSBetweenSamples, measured_nsscaled_per_cycle);
476 int64_t diff_ns = now_ns - estimated_base_ns;
487 ns = kMinNSBetweenSamples + diff_ns - (diff_ns / 16);
488 uint64_t new_nsscaled_per_cycle =
489 SafeDivideAndScale(ns, assumed_next_sample_delta_cycles);
490 if (new_nsscaled_per_cycle != 0 &&
491 diff_ns < 100 * 1000 * 1000 && -diff_ns < 100 * 1000 * 1000) {
493 last_sample.nsscaled_per_cycle.store(
494 new_nsscaled_per_cycle, std::memory_order_relaxed);
495 uint64_t new_min_cycles_per_sample =
496 SafeDivideAndScale(kMinNSBetweenSamples, new_nsscaled_per_cycle);
497 last_sample.min_cycles_per_sample.store(
498 new_min_cycles_per_sample, std::memory_order_relaxed);
499 stats_calibrations++;
501 last_sample.nsscaled_per_cycle.store(0, std::memory_order_relaxed);
502 last_sample.min_cycles_per_sample.store(0, std::memory_order_relaxed);
503 estimated_base_ns = now_ns;
504 stats_reinitializations++;
506 last_sample.raw_ns.store(now_ns, std::memory_order_relaxed);
507 last_sample.base_ns.store(estimated_base_ns, std::memory_order_relaxed);
508 last_sample.base_cycles.store(now_cycles, std::memory_order_relaxed);
514 SeqRelease(&seq, lock_value);
516 return estimated_base_ns;
519 #endif // ABSL_USE_CYCLECLOCK_FOR_GET_CURRENT_TIME_NANOS 529 std::numeric_limits<unsigned long>::max());
542 while (nanosleep(&sleep_time, &sleep_time) != 0 && errno == EINTR) {
556 absl::SleepOnce(to_sleep);
557 duration -= to_sleep;
#define EXCLUSIVE_LOCKS_REQUIRED(...)
timespec ToTimespec(Duration d)
void Lock() EXCLUSIVE_LOCK_FUNCTION()
void Unlock() UNLOCK_FUNCTION()
constexpr Time FromUnixDuration(Duration d)
constexpr Duration Milliseconds(int64_t n)
ABSL_ATTRIBUTE_WEAK void AbslInternalSleepFor(absl::Duration duration)
int64_t GetCurrentTimeNanos()
constexpr Duration MakeDuration(int64_t hi, uint32_t lo)
#define LOCKS_EXCLUDED(...)
#define GET_CURRENT_TIME_NANOS_FROM_SYSTEM()
#define ABSL_ATTRIBUTE_WEAK
#define ABSL_ATTRIBUTE_NOINLINE
constexpr Duration Seconds(int64_t n)
constexpr Duration Nanoseconds(int64_t n)
constexpr Duration ZeroDuration()
#define ABSL_ATTRIBUTE_COLD