cycleclock.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 // The implementation of CycleClock::Frequency.
00016 //
00017 // NOTE: only i386 and x86_64 have been well tested.
00018 // PPC, sparc, alpha, and ia64 are based on
00019 //    http://peter.kuscsik.com/wordpress/?p=14
00020 // with modifications by m3b.  See also
00021 //    https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h
00022 
00023 #include "absl/base/internal/cycleclock.h"
00024 
00025 #include <atomic>
00026 #include <chrono>  // NOLINT(build/c++11)
00027 
00028 #include "absl/base/internal/unscaledcycleclock.h"
00029 
00030 namespace absl {
00031 namespace base_internal {
00032 
00033 #if ABSL_USE_UNSCALED_CYCLECLOCK
00034 
00035 namespace {
00036 
00037 #ifdef NDEBUG
00038 #ifdef ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
00039 // Not debug mode and the UnscaledCycleClock frequency is the CPU
00040 // frequency.  Scale the CycleClock to prevent overflow if someone
00041 // tries to represent the time as cycles since the Unix epoch.
00042 static constexpr int32_t kShift = 1;
00043 #else
00044 // Not debug mode and the UnscaledCycleClock isn't operating at the
00045 // raw CPU frequency. There is no need to do any scaling, so don't
00046 // needlessly sacrifice precision.
00047 static constexpr int32_t kShift = 0;
00048 #endif
00049 #else
00050 // In debug mode use a different shift to discourage depending on a
00051 // particular shift value.
00052 static constexpr int32_t kShift = 2;
00053 #endif
00054 
00055 static constexpr double kFrequencyScale = 1.0 / (1 << kShift);
00056 static std::atomic<CycleClockSourceFunc> cycle_clock_source;
00057 
00058 CycleClockSourceFunc LoadCycleClockSource() {
00059   // Optimize for the common case (no callback) by first doing a relaxed load;
00060   // this is significantly faster on non-x86 platforms.
00061   if (cycle_clock_source.load(std::memory_order_relaxed) == nullptr) {
00062     return nullptr;
00063   }
00064   // This corresponds to the store(std::memory_order_release) in
00065   // CycleClockSource::Register, and makes sure that any updates made prior to
00066   // registering the callback are visible to this thread before the callback is
00067   // invoked.
00068   return cycle_clock_source.load(std::memory_order_acquire);
00069 }
00070 
00071 }  // namespace
00072 
00073 int64_t CycleClock::Now() {
00074   auto fn = LoadCycleClockSource();
00075   if (fn == nullptr) {
00076     return base_internal::UnscaledCycleClock::Now() >> kShift;
00077   }
00078   return fn() >> kShift;
00079 }
00080 
00081 double CycleClock::Frequency() {
00082   return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
00083 }
00084 
00085 void CycleClockSource::Register(CycleClockSourceFunc source) {
00086   // Corresponds to the load(std::memory_order_acquire) in LoadCycleClockSource.
00087   cycle_clock_source.store(source, std::memory_order_release);
00088 }
00089 
00090 #else
00091 
00092 int64_t CycleClock::Now() {
00093   return std::chrono::duration_cast<std::chrono::nanoseconds>(
00094              std::chrono::steady_clock::now().time_since_epoch())
00095       .count();
00096 }
00097 
00098 double CycleClock::Frequency() {
00099   return 1e9;
00100 }
00101 
00102 #endif
00103 
00104 }  // namespace base_internal
00105 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14