blob: 0e65005b8914eb1ef65047f83a4f2c6420cb18fd [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2017 The Abseil 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// https://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// The implementation of CycleClock::Frequency.
16//
17// NOTE: only i386 and x86_64 have been well tested.
18// PPC, sparc, alpha, and ia64 are based on
19// http://peter.kuscsik.com/wordpress/?p=14
20// with modifications by m3b. See also
21// https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h
22
23#include "absl/base/internal/cycleclock.h"
24
25#include <atomic>
26#include <chrono> // NOLINT(build/c++11)
27
28#include "absl/base/internal/unscaledcycleclock.h"
29
30namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080031ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070032namespace base_internal {
33
34#if ABSL_USE_UNSCALED_CYCLECLOCK
35
36namespace {
37
38#ifdef NDEBUG
39#ifdef ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
40// Not debug mode and the UnscaledCycleClock frequency is the CPU
41// frequency. Scale the CycleClock to prevent overflow if someone
42// tries to represent the time as cycles since the Unix epoch.
43static constexpr int32_t kShift = 1;
44#else
45// Not debug mode and the UnscaledCycleClock isn't operating at the
46// raw CPU frequency. There is no need to do any scaling, so don't
47// needlessly sacrifice precision.
48static constexpr int32_t kShift = 0;
49#endif
50#else
51// In debug mode use a different shift to discourage depending on a
52// particular shift value.
53static constexpr int32_t kShift = 2;
54#endif
55
56static constexpr double kFrequencyScale = 1.0 / (1 << kShift);
57static std::atomic<CycleClockSourceFunc> cycle_clock_source;
58
59CycleClockSourceFunc LoadCycleClockSource() {
60 // Optimize for the common case (no callback) by first doing a relaxed load;
61 // this is significantly faster on non-x86 platforms.
62 if (cycle_clock_source.load(std::memory_order_relaxed) == nullptr) {
63 return nullptr;
64 }
65 // This corresponds to the store(std::memory_order_release) in
66 // CycleClockSource::Register, and makes sure that any updates made prior to
67 // registering the callback are visible to this thread before the callback is
68 // invoked.
69 return cycle_clock_source.load(std::memory_order_acquire);
70}
71
72} // namespace
73
74int64_t CycleClock::Now() {
75 auto fn = LoadCycleClockSource();
76 if (fn == nullptr) {
77 return base_internal::UnscaledCycleClock::Now() >> kShift;
78 }
79 return fn() >> kShift;
80}
81
82double CycleClock::Frequency() {
83 return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
84}
85
86void CycleClockSource::Register(CycleClockSourceFunc source) {
87 // Corresponds to the load(std::memory_order_acquire) in LoadCycleClockSource.
88 cycle_clock_source.store(source, std::memory_order_release);
89}
90
91#else
92
93int64_t CycleClock::Now() {
94 return std::chrono::duration_cast<std::chrono::nanoseconds>(
95 std::chrono::steady_clock::now().time_since_epoch())
96 .count();
97}
98
99double CycleClock::Frequency() {
100 return 1e9;
101}
102
103#endif
104
105} // namespace base_internal
Austin Schuhb4691e92020-12-31 12:37:18 -0800106ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700107} // namespace absl