unscaledcycleclock.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include "absl/base/internal/unscaledcycleclock.h"
  15. #if ABSL_USE_UNSCALED_CYCLECLOCK
  16. #if defined(_WIN32)
  17. #include <intrin.h>
  18. #endif
  19. #if defined(__powerpc__) || defined(__ppc__)
  20. #ifdef __GLIBC__
  21. #include <sys/platform/ppc.h>
  22. #elif defined(__FreeBSD__)
  23. // clang-format off
  24. // This order does actually matter =(.
  25. #include <sys/types.h>
  26. #include <sys/sysctl.h>
  27. // clang-format on
  28. #include "absl/base/call_once.h"
  29. #endif
  30. #endif
  31. #include "absl/base/internal/sysinfo.h"
  32. namespace absl {
  33. ABSL_NAMESPACE_BEGIN
  34. namespace base_internal {
  35. #if defined(__i386__)
  36. int64_t UnscaledCycleClock::Now() {
  37. int64_t ret;
  38. __asm__ volatile("rdtsc" : "=A"(ret));
  39. return ret;
  40. }
  41. double UnscaledCycleClock::Frequency() {
  42. return base_internal::NominalCPUFrequency();
  43. }
  44. #elif defined(__x86_64__)
  45. double UnscaledCycleClock::Frequency() {
  46. return base_internal::NominalCPUFrequency();
  47. }
  48. #elif defined(__powerpc__) || defined(__ppc__)
  49. int64_t UnscaledCycleClock::Now() {
  50. #ifdef __GLIBC__
  51. return __ppc_get_timebase();
  52. #else
  53. #ifdef __powerpc64__
  54. int64_t tbr;
  55. asm volatile("mfspr %0, 268" : "=r"(tbr));
  56. return tbr;
  57. #else
  58. int32_t tbu, tbl, tmp;
  59. asm volatile(
  60. "0:\n"
  61. "mftbu %[hi32]\n"
  62. "mftb %[lo32]\n"
  63. "mftbu %[tmp]\n"
  64. "cmpw %[tmp],%[hi32]\n"
  65. "bne 0b\n"
  66. : [ hi32 ] "=r"(tbu), [ lo32 ] "=r"(tbl), [ tmp ] "=r"(tmp));
  67. return (static_cast<int64_t>(tbu) << 32) | tbl;
  68. #endif
  69. #endif
  70. }
  71. double UnscaledCycleClock::Frequency() {
  72. #ifdef __GLIBC__
  73. return __ppc_get_timebase_freq();
  74. #elif defined(_AIX)
  75. // This is the same constant value as returned by
  76. // __ppc_get_timebase_freq().
  77. return static_cast<double>(512000000);
  78. #elif defined(__FreeBSD__)
  79. static once_flag init_timebase_frequency_once;
  80. static double timebase_frequency = 0.0;
  81. base_internal::LowLevelCallOnce(&init_timebase_frequency_once, [&]() {
  82. size_t length = sizeof(timebase_frequency);
  83. sysctlbyname("kern.timecounter.tc.timebase.frequency", &timebase_frequency,
  84. &length, nullptr, 0);
  85. });
  86. return timebase_frequency;
  87. #else
  88. #error Must implement UnscaledCycleClock::Frequency()
  89. #endif
  90. }
  91. #elif defined(__aarch64__)
  92. // System timer of ARMv8 runs at a different frequency than the CPU's.
  93. // The frequency is fixed, typically in the range 1-50MHz. It can be
  94. // read at CNTFRQ special register. We assume the OS has set up
  95. // the virtual timer properly.
  96. int64_t UnscaledCycleClock::Now() {
  97. int64_t virtual_timer_value;
  98. asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
  99. return virtual_timer_value;
  100. }
  101. double UnscaledCycleClock::Frequency() {
  102. uint64_t aarch64_timer_frequency;
  103. asm volatile("mrs %0, cntfrq_el0" : "=r"(aarch64_timer_frequency));
  104. return aarch64_timer_frequency;
  105. }
  106. #elif defined(__riscv)
  107. int64_t UnscaledCycleClock::Now() {
  108. int64_t virtual_timer_value;
  109. asm volatile("rdcycle %0" : "=r"(virtual_timer_value));
  110. return virtual_timer_value;
  111. }
  112. double UnscaledCycleClock::Frequency() {
  113. return base_internal::NominalCPUFrequency();
  114. }
  115. #elif defined(_M_IX86) || defined(_M_X64)
  116. #pragma intrinsic(__rdtsc)
  117. int64_t UnscaledCycleClock::Now() { return __rdtsc(); }
  118. double UnscaledCycleClock::Frequency() {
  119. return base_internal::NominalCPUFrequency();
  120. }
  121. #endif
  122. } // namespace base_internal
  123. ABSL_NAMESPACE_END
  124. } // namespace absl
  125. #endif // ABSL_USE_UNSCALED_CYCLECLOCK