You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cycleclock.h 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/unscaledcycleclock.h"
  47. namespace absl {
  48. ABSL_NAMESPACE_BEGIN
  49. namespace base_internal {
  50. using CycleClockSourceFunc = int64_t (*)();
  51. // -----------------------------------------------------------------------------
  52. // CycleClock
  53. // -----------------------------------------------------------------------------
  54. class CycleClock {
  55. public:
  56. // CycleClock::Now()
  57. //
  58. // Returns the value of a cycle counter that counts at a rate that is
  59. // approximately constant.
  60. static int64_t Now();
  61. // CycleClock::Frequency()
  62. //
  63. // Returns the amount by which `CycleClock::Now()` increases per second. Note
  64. // that this value may not necessarily match the core CPU clock frequency.
  65. static double Frequency();
  66. private:
  67. #if ABSL_USE_UNSCALED_CYCLECLOCK
  68. static CycleClockSourceFunc LoadCycleClockSource();
  69. #ifdef NDEBUG
  70. #ifdef ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
  71. // Not debug mode and the UnscaledCycleClock frequency is the CPU
  72. // frequency. Scale the CycleClock to prevent overflow if someone
  73. // tries to represent the time as cycles since the Unix epoch.
  74. static constexpr int32_t kShift = 1;
  75. #else
  76. // Not debug mode and the UnscaledCycleClock isn't operating at the
  77. // raw CPU frequency. There is no need to do any scaling, so don't
  78. // needlessly sacrifice precision.
  79. static constexpr int32_t kShift = 0;
  80. #endif
  81. #else // NDEBUG
  82. // In debug mode use a different shift to discourage depending on a
  83. // particular shift value.
  84. static constexpr int32_t kShift = 2;
  85. #endif // NDEBUG
  86. static constexpr double kFrequencyScale = 1.0 / (1 << kShift);
  87. ABSL_CONST_INIT static std::atomic<CycleClockSourceFunc> cycle_clock_source_;
  88. #endif // ABSL_USE_UNSCALED_CYCLECLOC
  89. CycleClock() = delete; // no instances
  90. CycleClock(const CycleClock&) = delete;
  91. CycleClock& operator=(const CycleClock&) = delete;
  92. friend class CycleClockSource;
  93. };
  94. class CycleClockSource {
  95. private:
  96. // CycleClockSource::Register()
  97. //
  98. // Register a function that provides an alternate source for the unscaled CPU
  99. // cycle count value. The source function must be async signal safe, must not
  100. // call CycleClock::Now(), and must have a frequency that matches that of the
  101. // unscaled clock used by CycleClock. A nullptr value resets CycleClock to use
  102. // the default source.
  103. static void Register(CycleClockSourceFunc source);
  104. };
  105. #if ABSL_USE_UNSCALED_CYCLECLOCK
  106. inline CycleClockSourceFunc CycleClock::LoadCycleClockSource() {
  107. #if !defined(__x86_64__)
  108. // Optimize for the common case (no callback) by first doing a relaxed load;
  109. // this is significantly faster on non-x86 platforms.
  110. if (cycle_clock_source_.load(std::memory_order_relaxed) == nullptr) {
  111. return nullptr;
  112. }
  113. #endif // !defined(__x86_64__)
  114. // This corresponds to the store(std::memory_order_release) in
  115. // CycleClockSource::Register, and makes sure that any updates made prior to
  116. // registering the callback are visible to this thread before the callback
  117. // is invoked.
  118. return cycle_clock_source_.load(std::memory_order_acquire);
  119. }
  120. // Accessing globals in inlined code in Window DLLs is problematic.
  121. #ifndef _WIN32
  122. inline int64_t CycleClock::Now() {
  123. auto fn = LoadCycleClockSource();
  124. if (fn == nullptr) {
  125. return base_internal::UnscaledCycleClock::Now() >> kShift;
  126. }
  127. return fn() >> kShift;
  128. }
  129. #endif
  130. inline double CycleClock::Frequency() {
  131. return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
  132. }
  133. #endif // ABSL_USE_UNSCALED_CYCLECLOCK
  134. } // namespace base_internal
  135. ABSL_NAMESPACE_END
  136. } // namespace absl
  137. #endif // ABSL_BASE_INTERNAL_CYCLECLOCK_H_