/src/abseil-cpp/absl/base/internal/cycleclock.h
Line | Count | Source |
1 | | // |
2 | | // Copyright 2017 The Abseil Authors. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | // you may not use this file except in compliance with the License. |
6 | | // You may obtain a copy of the License at |
7 | | // |
8 | | // https://www.apache.org/licenses/LICENSE-2.0 |
9 | | // |
10 | | // Unless required by applicable law or agreed to in writing, software |
11 | | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | // See the License for the specific language governing permissions and |
14 | | // limitations under the License. |
15 | | // |
16 | | |
17 | | // ----------------------------------------------------------------------------- |
18 | | // File: cycleclock.h |
19 | | // ----------------------------------------------------------------------------- |
20 | | // |
21 | | // This header file defines a `CycleClock`, which yields the value and frequency |
22 | | // of a cycle counter that increments at a rate that is approximately constant. |
23 | | // |
24 | | // NOTE: |
25 | | // |
26 | | // The cycle counter frequency is not necessarily related to the core clock |
27 | | // frequency and should not be treated as such. That is, `CycleClock` cycles are |
28 | | // not necessarily "CPU cycles" and code should not rely on that behavior, even |
29 | | // if experimentally observed. |
30 | | // |
31 | | // An arbitrary offset may have been added to the counter at power on. |
32 | | // |
33 | | // On some platforms, the rate and offset of the counter may differ |
34 | | // slightly when read from different CPUs of a multiprocessor. Usually, |
35 | | // we try to ensure that the operating system adjusts values periodically |
36 | | // so that values agree approximately. If you need stronger guarantees, |
37 | | // consider using alternate interfaces. |
38 | | // |
39 | | // The CPU is not required to maintain the ordering of a cycle counter read |
40 | | // with respect to surrounding instructions. |
41 | | |
42 | | #ifndef ABSL_BASE_INTERNAL_CYCLECLOCK_H_ |
43 | | #define ABSL_BASE_INTERNAL_CYCLECLOCK_H_ |
44 | | |
45 | | #include <atomic> |
46 | | #include <cstdint> |
47 | | |
48 | | #include "absl/base/attributes.h" |
49 | | #include "absl/base/config.h" |
50 | | #include "absl/base/internal/cycleclock_config.h" |
51 | | #include "absl/base/internal/unscaledcycleclock.h" |
52 | | #include "absl/base/internal/unscaledcycleclock_config.h" |
53 | | |
54 | | namespace absl { |
55 | | ABSL_NAMESPACE_BEGIN |
56 | | namespace base_internal { |
57 | | |
58 | | using CycleClockSourceFunc = int64_t (*)(); |
59 | | |
60 | | // ----------------------------------------------------------------------------- |
61 | | // CycleClock |
62 | | // ----------------------------------------------------------------------------- |
63 | | class CycleClock { |
64 | | public: |
65 | | // CycleClock::Now() |
66 | | // |
67 | | // Returns the value of a cycle counter that counts at a rate that is |
68 | | // approximately constant. |
69 | | static int64_t Now(); |
70 | | |
71 | | // CycleClock::Frequency() |
72 | | // |
73 | | // Returns the amount by which `CycleClock::Now()` increases per second. Note |
74 | | // that this value may not necessarily match the core CPU clock frequency. |
75 | | static double Frequency(); |
76 | | |
77 | | private: |
78 | | #if ABSL_USE_UNSCALED_CYCLECLOCK |
79 | | static CycleClockSourceFunc LoadCycleClockSource(); |
80 | | |
81 | | static constexpr int32_t kShift = kCycleClockShift; |
82 | | static constexpr double kFrequencyScale = kCycleClockFrequencyScale; |
83 | | |
84 | | ABSL_CONST_INIT static std::atomic<CycleClockSourceFunc> cycle_clock_source_; |
85 | | #endif // ABSL_USE_UNSCALED_CYCLECLOC |
86 | | |
87 | | CycleClock() = delete; // no instances |
88 | | CycleClock(const CycleClock&) = delete; |
89 | | CycleClock& operator=(const CycleClock&) = delete; |
90 | | |
91 | | friend class CycleClockSource; |
92 | | }; |
93 | | |
94 | | class CycleClockSource { |
95 | | private: |
96 | | // CycleClockSource::Register() |
97 | | // |
98 | | // Register a function that provides an alternate source for the unscaled CPU |
99 | | // cycle count value. The source function must be async signal safe, must not |
100 | | // call CycleClock::Now(), and must have a frequency that matches that of the |
101 | | // unscaled clock used by CycleClock. A nullptr value resets CycleClock to use |
102 | | // the default source. |
103 | | static void Register(CycleClockSourceFunc source); |
104 | | }; |
105 | | |
106 | | #if ABSL_USE_UNSCALED_CYCLECLOCK |
107 | | |
108 | 0 | inline CycleClockSourceFunc CycleClock::LoadCycleClockSource() { |
109 | | #if !defined(__x86_64__) |
110 | | // Optimize for the common case (no callback) by first doing a relaxed load; |
111 | | // this is significantly faster on non-x86 platforms. |
112 | | if (cycle_clock_source_.load(std::memory_order_relaxed) == nullptr) { |
113 | | return nullptr; |
114 | | } |
115 | | #endif // !defined(__x86_64__) |
116 | | |
117 | | // This corresponds to the store(std::memory_order_release) in |
118 | | // CycleClockSource::Register, and makes sure that any updates made prior to |
119 | | // registering the callback are visible to this thread before the callback |
120 | | // is invoked. |
121 | 0 | return cycle_clock_source_.load(std::memory_order_acquire); |
122 | 0 | } |
123 | | |
124 | | // Accessing globals in inlined code in Window DLLs is problematic. |
125 | | #ifndef _WIN32 |
126 | 0 | inline int64_t CycleClock::Now() { |
127 | 0 | auto fn = LoadCycleClockSource(); |
128 | 0 | if (fn == nullptr) { |
129 | 0 | return base_internal::UnscaledCycleClock::Now() >> kShift; |
130 | 0 | } |
131 | 0 | return fn() >> kShift; |
132 | 0 | } |
133 | | #endif |
134 | | |
135 | 0 | inline double CycleClock::Frequency() { |
136 | 0 | return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency(); |
137 | 0 | } |
138 | | |
139 | | #endif // ABSL_USE_UNSCALED_CYCLECLOCK |
140 | | |
141 | | } // namespace base_internal |
142 | | ABSL_NAMESPACE_END |
143 | | } // namespace absl |
144 | | |
145 | | #endif // ABSL_BASE_INTERNAL_CYCLECLOCK_H_ |