1 // 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 
30 namespace absl {
31 ABSL_NAMESPACE_BEGIN
32 namespace base_internal {
33 
34 #if ABSL_USE_UNSCALED_CYCLECLOCK
35 
36 namespace {
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.
43 static 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.
48 static 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.
53 static constexpr int32_t kShift = 2;
54 #endif
55 
56 static constexpr double kFrequencyScale = 1.0 / (1 << kShift);
57 static std::atomic<CycleClockSourceFunc> cycle_clock_source;
58 
LoadCycleClockSource()59 CycleClockSourceFunc 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 
Now()74 int64_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 
Frequency()82 double CycleClock::Frequency() {
83   return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
84 }
85 
Register(CycleClockSourceFunc source)86 void 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 
93 int64_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 
99 double CycleClock::Frequency() {
100   return 1e9;
101 }
102 
103 #endif
104 
105 }  // namespace base_internal
106 ABSL_NAMESPACE_END
107 }  // namespace absl
108