unscaledcycleclock.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "mftbu %[hi32]\n"
  61. "mftb %[lo32]\n"
  62. "mftbu %[tmp]\n"
  63. "cmpw %[tmp],%[hi32]\n"
  64. "bne $-16\n" // Retry on failure.
  65. : [hi32] "=r"(tbu), [lo32] "=r"(tbl), [tmp] "=r"(tmp));
  66. return (static_cast<int64_t>(tbu) << 32) | tbl;
  67. #endif
  68. #endif
  69. }
  70. double UnscaledCycleClock::Frequency() {
  71. #ifdef __GLIBC__
  72. return __ppc_get_timebase_freq();
  73. #elif defined(_AIX)
  74. // This is the same constant value as returned by
  75. // __ppc_get_timebase_freq().
  76. return static_cast<double>(512000000);
  77. #elif defined(__FreeBSD__)
  78. static once_flag init_timebase_frequency_once;
  79. static double timebase_frequency = 0.0;
  80. base_internal::LowLevelCallOnce(&init_timebase_frequency_once, [&]() {
  81. size_t length = sizeof(timebase_frequency);
  82. sysctlbyname("kern.timecounter.tc.timebase.frequency", &timebase_frequency,
  83. &length, nullptr, 0);
  84. });
  85. return timebase_frequency;
  86. #else
  87. #error Must implement UnscaledCycleClock::Frequency()
  88. #endif
  89. }
  90. #elif defined(__aarch64__)
  91. // System timer of ARMv8 runs at a different frequency than the CPU's.
  92. // The frequency is fixed, typically in the range 1-50MHz. It can be
  93. // read at CNTFRQ special register. We assume the OS has set up
  94. // the virtual timer properly.
  95. int64_t UnscaledCycleClock::Now() {
  96. int64_t virtual_timer_value;
  97. asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
  98. return virtual_timer_value;
  99. }
  100. double UnscaledCycleClock::Frequency() {
  101. uint64_t aarch64_timer_frequency;
  102. asm volatile("mrs %0, cntfrq_el0" : "=r"(aarch64_timer_frequency));
  103. return aarch64_timer_frequency;
  104. }
  105. #elif defined(__riscv)
  106. int64_t UnscaledCycleClock::Now() {
  107. int64_t virtual_timer_value;
  108. asm volatile("rdcycle %0" : "=r"(virtual_timer_value));
  109. return virtual_timer_value;
  110. }
  111. double UnscaledCycleClock::Frequency() {
  112. return base_internal::NominalCPUFrequency();
  113. }
  114. #elif defined(_M_IX86) || defined(_M_X64)
  115. #pragma intrinsic(__rdtsc)
  116. int64_t UnscaledCycleClock::Now() { return __rdtsc(); }
  117. double UnscaledCycleClock::Frequency() {
  118. return base_internal::NominalCPUFrequency();
  119. }
  120. #endif
  121. } // namespace base_internal
  122. ABSL_NAMESPACE_END
  123. } // namespace absl
  124. #endif // ABSL_USE_UNSCALED_CYCLECLOCK