cycleclock.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: cycleclock.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This header file defines a `CycleClock`, which yields the value and frequency
  21. // of a cycle counter that increments at a rate that is approximately constant.
  22. //
  23. // NOTE:
  24. //
  25. // The cycle counter frequency is not necessarily related to the core clock
  26. // frequency and should not be treated as such. That is, `CycleClock` cycles are
  27. // not necessarily "CPU cycles" and code should not rely on that behavior, even
  28. // if experimentally observed.
  29. //
  30. // An arbitrary offset may have been added to the counter at power on.
  31. //
  32. // On some platforms, the rate and offset of the counter may differ
  33. // slightly when read from different CPUs of a multiprocessor. Usually,
  34. // we try to ensure that the operating system adjusts values periodically
  35. // so that values agree approximately. If you need stronger guarantees,
  36. // consider using alternate interfaces.
  37. //
  38. // The CPU is not required to maintain the ordering of a cycle counter read
  39. // with respect to surrounding instructions.
  40. #ifndef ABSL_BASE_INTERNAL_CYCLECLOCK_H_
  41. #define ABSL_BASE_INTERNAL_CYCLECLOCK_H_
  42. #include <atomic>
  43. #include <cstdint>
  44. #include "absl/base/attributes.h"
  45. #include "absl/base/config.h"
  46. #include "absl/base/internal/cycleclock_config.h"
  47. #include "absl/base/internal/unscaledcycleclock.h"
  48. namespace absl {
  49. ABSL_NAMESPACE_BEGIN
  50. namespace base_internal {
  51. using CycleClockSourceFunc = int64_t (*)();
  52. // -----------------------------------------------------------------------------
  53. // CycleClock
  54. // -----------------------------------------------------------------------------
  55. class CycleClock {
  56. public:
  57. // CycleClock::Now()
  58. //
  59. // Returns the value of a cycle counter that counts at a rate that is
  60. // approximately constant.
  61. static int64_t Now();
  62. // CycleClock::Frequency()
  63. //
  64. // Returns the amount by which `CycleClock::Now()` increases per second. Note
  65. // that this value may not necessarily match the core CPU clock frequency.
  66. static double Frequency();
  67. private:
  68. #if ABSL_USE_UNSCALED_CYCLECLOCK
  69. static CycleClockSourceFunc LoadCycleClockSource();
  70. static constexpr int32_t kShift = kCycleClockShift;
  71. static constexpr double kFrequencyScale = kCycleClockFrequencyScale;
  72. ABSL_CONST_INIT static std::atomic<CycleClockSourceFunc> cycle_clock_source_;
  73. #endif // ABSL_USE_UNSCALED_CYCLECLOC
  74. CycleClock() = delete; // no instances
  75. CycleClock(const CycleClock&) = delete;
  76. CycleClock& operator=(const CycleClock&) = delete;
  77. friend class CycleClockSource;
  78. };
  79. class CycleClockSource {
  80. private:
  81. // CycleClockSource::Register()
  82. //
  83. // Register a function that provides an alternate source for the unscaled CPU
  84. // cycle count value. The source function must be async signal safe, must not
  85. // call CycleClock::Now(), and must have a frequency that matches that of the
  86. // unscaled clock used by CycleClock. A nullptr value resets CycleClock to use
  87. // the default source.
  88. static void Register(CycleClockSourceFunc source);
  89. };
  90. #if ABSL_USE_UNSCALED_CYCLECLOCK
  91. inline CycleClockSourceFunc CycleClock::LoadCycleClockSource() {
  92. #if !defined(__x86_64__)
  93. // Optimize for the common case (no callback) by first doing a relaxed load;
  94. // this is significantly faster on non-x86 platforms.
  95. if (cycle_clock_source_.load(std::memory_order_relaxed) == nullptr) {
  96. return nullptr;
  97. }
  98. #endif // !defined(__x86_64__)
  99. // This corresponds to the store(std::memory_order_release) in
  100. // CycleClockSource::Register, and makes sure that any updates made prior to
  101. // registering the callback are visible to this thread before the callback
  102. // is invoked.
  103. return cycle_clock_source_.load(std::memory_order_acquire);
  104. }
  105. // Accessing globals in inlined code in Window DLLs is problematic.
  106. #ifndef _WIN32
  107. inline int64_t CycleClock::Now() {
  108. auto fn = LoadCycleClockSource();
  109. if (fn == nullptr) {
  110. return base_internal::UnscaledCycleClock::Now() >> kShift;
  111. }
  112. return fn() >> kShift;
  113. }
  114. #endif
  115. inline double CycleClock::Frequency() {
  116. return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
  117. }
  118. #endif // ABSL_USE_UNSCALED_CYCLECLOCK
  119. } // namespace base_internal
  120. ABSL_NAMESPACE_END
  121. } // namespace absl
  122. #endif // ABSL_BASE_INTERNAL_CYCLECLOCK_H_