cycleclock.cc
Go to the documentation of this file.
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 
24 
25 #include <atomic>
26 #include <chrono> // NOLINT(build/c++11)
27 
29 
30 namespace absl {
31 namespace base_internal {
32 
33 #if ABSL_USE_UNSCALED_CYCLECLOCK
34 
35 namespace {
36 
37 #ifdef NDEBUG
38 #ifdef ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
39 // Not debug mode and the UnscaledCycleClock frequency is the CPU
40 // frequency. Scale the CycleClock to prevent overflow if someone
41 // tries to represent the time as cycles since the Unix epoch.
42 static constexpr int32_t kShift = 1;
43 #else
44 // Not debug mode and the UnscaledCycleClock isn't operating at the
45 // raw CPU frequency. There is no need to do any scaling, so don't
46 // needlessly sacrifice precision.
47 static constexpr int32_t kShift = 0;
48 #endif
49 #else
50 // In debug mode use a different shift to discourage depending on a
51 // particular shift value.
52 static constexpr int32_t kShift = 2;
53 #endif
54 
55 static constexpr double kFrequencyScale = 1.0 / (1 << kShift);
56 static std::atomic<CycleClockSourceFunc> cycle_clock_source;
57 
58 CycleClockSourceFunc LoadCycleClockSource() {
59  // Optimize for the common case (no callback) by first doing a relaxed load;
60  // this is significantly faster on non-x86 platforms.
61  if (cycle_clock_source.load(std::memory_order_relaxed) == nullptr) {
62  return nullptr;
63  }
64  // This corresponds to the store(std::memory_order_release) in
65  // CycleClockSource::Register, and makes sure that any updates made prior to
66  // registering the callback are visible to this thread before the callback is
67  // invoked.
68  return cycle_clock_source.load(std::memory_order_acquire);
69 }
70 
71 } // namespace
72 
73 int64_t CycleClock::Now() {
74  auto fn = LoadCycleClockSource();
75  if (fn == nullptr) {
76  return base_internal::UnscaledCycleClock::Now() >> kShift;
77  }
78  return fn() >> kShift;
79 }
80 
81 double CycleClock::Frequency() {
82  return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
83 }
84 
86  // Corresponds to the load(std::memory_order_acquire) in LoadCycleClockSource.
87  cycle_clock_source.store(source, std::memory_order_release);
88 }
89 
90 #else
91 
92 int64_t CycleClock::Now() {
93  return std::chrono::duration_cast<std::chrono::nanoseconds>(
94  std::chrono::steady_clock::now().time_since_epoch())
95  .count();
96 }
97 
99  return 1e9;
100 }
101 
102 #endif
103 
104 } // namespace base_internal
105 } // namespace absl
Time Now()
Definition: clock.cc:37
int64_t(*)() CycleClockSourceFunc
Definition: cycleclock.h:73
Definition: algorithm.h:29
static void Register(CycleClockSourceFunc source)


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:18