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
30namespace absl {
31namespace base_internal {
32
33#if ABSL_USE_UNSCALED_CYCLECLOCK
34
35namespace {
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.
42static 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.
47static 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.
52static constexpr int32_t kShift = 2;
53#endif
54
55static constexpr double kFrequencyScale = 1.0 / (1 << kShift);
56static std::atomic<CycleClockSourceFunc> cycle_clock_source;
57
58CycleClockSourceFunc 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
73int64_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
81double CycleClock::Frequency() {
82 return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
83}
84
85void CycleClockSource::Register(CycleClockSourceFunc source) {
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
92int64_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
98double CycleClock::Frequency() {
99 return 1e9;
100}
101
102#endif
103
104} // namespace base_internal
105} // namespace absl
106