cycleclock.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <cstdint>
  43. #include "absl/base/config.h"
  44. namespace absl {
  45. ABSL_NAMESPACE_BEGIN
  46. namespace base_internal {
  47. // -----------------------------------------------------------------------------
  48. // CycleClock
  49. // -----------------------------------------------------------------------------
  50. class CycleClock {
  51. public:
  52. // CycleClock::Now()
  53. //
  54. // Returns the value of a cycle counter that counts at a rate that is
  55. // approximately constant.
  56. static int64_t Now();
  57. // CycleClock::Frequency()
  58. //
  59. // Returns the amount by which `CycleClock::Now()` increases per second. Note
  60. // that this value may not necessarily match the core CPU clock frequency.
  61. static double Frequency();
  62. private:
  63. CycleClock() = delete; // no instances
  64. CycleClock(const CycleClock&) = delete;
  65. CycleClock& operator=(const CycleClock&) = delete;
  66. };
  67. using CycleClockSourceFunc = int64_t (*)();
  68. class CycleClockSource {
  69. private:
  70. // CycleClockSource::Register()
  71. //
  72. // Register a function that provides an alternate source for the unscaled CPU
  73. // cycle count value. The source function must be async signal safe, must not
  74. // call CycleClock::Now(), and must have a frequency that matches that of the
  75. // unscaled clock used by CycleClock. A nullptr value resets CycleClock to use
  76. // the default source.
  77. static void Register(CycleClockSourceFunc source);
  78. };
  79. } // namespace base_internal
  80. ABSL_NAMESPACE_END
  81. } // namespace absl
  82. #endif // ABSL_BASE_INTERNAL_CYCLECLOCK_H_