unscaledcycleclock.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // UnscaledCycleClock
  16. // An UnscaledCycleClock yields the value and frequency of a cycle counter
  17. // that increments at a rate that is approximately constant.
  18. // This class is for internal use only, you should consider using CycleClock
  19. // instead.
  20. //
  21. // Notes:
  22. // The cycle counter frequency is not necessarily the core clock frequency.
  23. // That is, CycleCounter cycles are not necessarily "CPU cycles".
  24. //
  25. // An arbitrary offset may have been added to the counter at power on.
  26. //
  27. // On some platforms, the rate and offset of the counter may differ
  28. // slightly when read from different CPUs of a multiprocessor. Usually,
  29. // we try to ensure that the operating system adjusts values periodically
  30. // so that values agree approximately. If you need stronger guarantees,
  31. // consider using alternate interfaces.
  32. //
  33. // The CPU is not required to maintain the ordering of a cycle counter read
  34. // with respect to surrounding instructions.
  35. #ifndef ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_
  36. #define ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_
  37. #include <cstdint>
  38. #if defined(__APPLE__)
  39. #include <TargetConditionals.h>
  40. #endif
  41. #include "absl/base/port.h"
  42. // The following platforms have an implementation of a hardware counter.
  43. #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || \
  44. defined(__powerpc__) || defined(__ppc__) || \
  45. defined(_M_IX86) || defined(_M_X64)
  46. #define ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION 1
  47. #else
  48. #define ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION 0
  49. #endif
  50. // The following platforms often disable access to the hardware
  51. // counter (through a sandbox) even if the underlying hardware has a
  52. // usable counter. The CycleTimer interface also requires a *scaled*
  53. // CycleClock that runs at atleast 1 MHz. We've found some Android
  54. // ARM64 devices where this is not the case, so we disable it by
  55. // default on Android ARM64.
  56. #if defined(__native_client__) || \
  57. (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || \
  58. (defined(__ANDROID__) && defined(__aarch64__))
  59. #define ABSL_USE_UNSCALED_CYCLECLOCK_DEFAULT 0
  60. #else
  61. #define ABSL_USE_UNSCALED_CYCLECLOCK_DEFAULT 1
  62. #endif
  63. // UnscaledCycleClock is an optional internal feature.
  64. // Use "#if ABSL_USE_UNSCALED_CYCLECLOCK" to test for its presence.
  65. // Can be overridden at compile-time via -DABSL_USE_UNSCALED_CYCLECLOCK=0|1
  66. #if !defined(ABSL_USE_UNSCALED_CYCLECLOCK)
  67. #define ABSL_USE_UNSCALED_CYCLECLOCK \
  68. (ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION && \
  69. ABSL_USE_UNSCALED_CYCLECLOCK_DEFAULT)
  70. #endif
  71. #if ABSL_USE_UNSCALED_CYCLECLOCK
  72. // This macro can be used to test if UnscaledCycleClock::Frequency()
  73. // is NominalCPUFrequency() on a particular platform.
  74. #if (defined(__i386__) || defined(__x86_64__) || \
  75. defined(_M_IX86) || defined(_M_X64))
  76. #define ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
  77. #endif
  78. namespace absl {
  79. ABSL_NAMESPACE_BEGIN
  80. namespace time_internal {
  81. class UnscaledCycleClockWrapperForGetCurrentTime;
  82. } // namespace time_internal
  83. namespace base_internal {
  84. class CycleClock;
  85. class UnscaledCycleClockWrapperForInitializeFrequency;
  86. class UnscaledCycleClock {
  87. private:
  88. UnscaledCycleClock() = delete;
  89. // Return the value of a cycle counter that counts at a rate that is
  90. // approximately constant.
  91. static int64_t Now();
  92. // Return the how much UnscaledCycleClock::Now() increases per second.
  93. // This is not necessarily the core CPU clock frequency.
  94. // It may be the nominal value report by the kernel, rather than a measured
  95. // value.
  96. static double Frequency();
  97. // Allowed users
  98. friend class base_internal::CycleClock;
  99. friend class time_internal::UnscaledCycleClockWrapperForGetCurrentTime;
  100. friend class base_internal::UnscaledCycleClockWrapperForInitializeFrequency;
  101. };
  102. } // namespace base_internal
  103. ABSL_NAMESPACE_END
  104. } // namespace absl
  105. #endif // ABSL_USE_UNSCALED_CYCLECLOCK
  106. #endif // ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_