cycleclock.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // The implementation of CycleClock::Frequency.
  15. //
  16. // NOTE: only i386 and x86_64 have been well tested.
  17. // PPC, sparc, alpha, and ia64 are based on
  18. // http://peter.kuscsik.com/wordpress/?p=14
  19. // with modifications by m3b. See also
  20. // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h
  21. #include "absl/base/internal/cycleclock.h"
  22. #include <atomic>
  23. #include <chrono> // NOLINT(build/c++11)
  24. #include "absl/base/internal/unscaledcycleclock.h"
  25. namespace absl {
  26. ABSL_NAMESPACE_BEGIN
  27. namespace base_internal {
  28. #if ABSL_USE_UNSCALED_CYCLECLOCK
  29. namespace {
  30. #ifdef NDEBUG
  31. #ifdef ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
  32. // Not debug mode and the UnscaledCycleClock frequency is the CPU
  33. // frequency. Scale the CycleClock to prevent overflow if someone
  34. // tries to represent the time as cycles since the Unix epoch.
  35. static constexpr int32_t kShift = 1;
  36. #else
  37. // Not debug mode and the UnscaledCycleClock isn't operating at the
  38. // raw CPU frequency. There is no need to do any scaling, so don't
  39. // needlessly sacrifice precision.
  40. static constexpr int32_t kShift = 0;
  41. #endif
  42. #else
  43. // In debug mode use a different shift to discourage depending on a
  44. // particular shift value.
  45. static constexpr int32_t kShift = 2;
  46. #endif
  47. static constexpr double kFrequencyScale = 1.0 / (1 << kShift);
  48. static std::atomic<CycleClockSourceFunc> cycle_clock_source;
  49. CycleClockSourceFunc LoadCycleClockSource() {
  50. // Optimize for the common case (no callback) by first doing a relaxed load;
  51. // this is significantly faster on non-x86 platforms.
  52. if (cycle_clock_source.load(std::memory_order_relaxed) == nullptr) {
  53. return nullptr;
  54. }
  55. // This corresponds to the store(std::memory_order_release) in
  56. // CycleClockSource::Register, and makes sure that any updates made prior to
  57. // registering the callback are visible to this thread before the callback is
  58. // invoked.
  59. return cycle_clock_source.load(std::memory_order_acquire);
  60. }
  61. } // namespace
  62. int64_t CycleClock::Now() {
  63. auto fn = LoadCycleClockSource();
  64. if (fn == nullptr) {
  65. return base_internal::UnscaledCycleClock::Now() >> kShift;
  66. }
  67. return fn() >> kShift;
  68. }
  69. double CycleClock::Frequency() {
  70. return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
  71. }
  72. void CycleClockSource::Register(CycleClockSourceFunc source) {
  73. // Corresponds to the load(std::memory_order_acquire) in LoadCycleClockSource.
  74. cycle_clock_source.store(source, std::memory_order_release);
  75. }
  76. #else
  77. int64_t CycleClock::Now() {
  78. return std::chrono::duration_cast<std::chrono::nanoseconds>(
  79. std::chrono::steady_clock::now().time_since_epoch())
  80. .count();
  81. }
  82. double CycleClock::Frequency() {
  83. return 1e9;
  84. }
  85. #endif
  86. } // namespace base_internal
  87. ABSL_NAMESPACE_END
  88. } // namespace absl