/src/abseil-cpp/absl/base/internal/unscaledcycleclock.cc
Line | Count | Source |
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 | | #include "absl/base/internal/unscaledcycleclock.h" |
16 | | |
17 | | #include "absl/base/config.h" |
18 | | #include "absl/base/internal/unscaledcycleclock_config.h" |
19 | | |
20 | | #if ABSL_USE_UNSCALED_CYCLECLOCK |
21 | | |
22 | | #if defined(_WIN32) |
23 | | #include <intrin.h> |
24 | | #endif |
25 | | |
26 | | #if defined(__powerpc__) || defined(__ppc__) |
27 | | #ifdef __GLIBC__ |
28 | | #include <sys/platform/ppc.h> |
29 | | #elif defined(__FreeBSD__) |
30 | | // clang-format off |
31 | | // This order does actually matter =(. |
32 | | #include <sys/types.h> |
33 | | #include <sys/sysctl.h> |
34 | | // clang-format on |
35 | | |
36 | | #include "absl/base/call_once.h" |
37 | | #endif |
38 | | #endif |
39 | | |
40 | | #include "absl/base/internal/sysinfo.h" |
41 | | |
42 | | namespace absl { |
43 | | ABSL_NAMESPACE_BEGIN |
44 | | namespace base_internal { |
45 | | |
46 | | #if defined(__i386__) |
47 | | |
48 | | int64_t UnscaledCycleClock::Now() { |
49 | | int64_t ret; |
50 | | __asm__ volatile("rdtsc" : "=A"(ret)); |
51 | | return ret; |
52 | | } |
53 | | |
54 | | double UnscaledCycleClock::Frequency() { |
55 | | return base_internal::NominalCPUFrequency(); |
56 | | } |
57 | | |
58 | | #elif defined(__x86_64__) |
59 | | |
60 | 0 | double UnscaledCycleClock::Frequency() { |
61 | 0 | return base_internal::NominalCPUFrequency(); |
62 | 0 | } |
63 | | |
64 | | #elif defined(__powerpc__) || defined(__ppc__) |
65 | | |
66 | | int64_t UnscaledCycleClock::Now() { |
67 | | #ifdef __GLIBC__ |
68 | | return static_cast<int64_t>(__ppc_get_timebase()); |
69 | | #else |
70 | | #ifdef __powerpc64__ |
71 | | int64_t tbr; |
72 | | asm volatile("mfspr %0, 268" : "=r"(tbr)); |
73 | | return tbr; |
74 | | #else |
75 | | int32_t tbu, tbl, tmp; |
76 | | asm volatile( |
77 | | "mftbu %[hi32]\n" |
78 | | "mftb %[lo32]\n" |
79 | | "mftbu %[tmp]\n" |
80 | | "cmpw %[tmp],%[hi32]\n" |
81 | | "bne $-16\n" // Retry on failure. |
82 | | : [hi32] "=r"(tbu), [lo32] "=r"(tbl), [tmp] "=r"(tmp)); |
83 | | return (static_cast<int64_t>(tbu) << 32) | tbl; |
84 | | #endif |
85 | | #endif |
86 | | } |
87 | | |
88 | | double UnscaledCycleClock::Frequency() { |
89 | | #ifdef __GLIBC__ |
90 | | return __ppc_get_timebase_freq(); |
91 | | #elif defined(__linux__) |
92 | | // Fallback for musl + ppc64le: use constant timebase frequency (512 MHz) |
93 | | // Must come after __GLIBC__. |
94 | | return static_cast<double>(512000000); |
95 | | #elif defined(_AIX) |
96 | | // This is the same constant value as returned by |
97 | | // __ppc_get_timebase_freq(). |
98 | | return static_cast<double>(512000000); |
99 | | #elif defined(__FreeBSD__) |
100 | | static once_flag init_timebase_frequency_once; |
101 | | static double timebase_frequency = 0.0; |
102 | | base_internal::LowLevelCallOnce(&init_timebase_frequency_once, [&]() { |
103 | | uint64_t freq = 0; |
104 | | size_t length = sizeof(freq); |
105 | | if (sysctlbyname("kern.timecounter.tc.timebase.frequency", &freq, &length, |
106 | | nullptr, 0) == 0) { |
107 | | timebase_frequency = static_cast<double>(freq); |
108 | | } |
109 | | }); |
110 | | return timebase_frequency; |
111 | | #else |
112 | | #error Must implement UnscaledCycleClock::Frequency() |
113 | | #endif |
114 | | } |
115 | | |
116 | | #elif defined(__aarch64__) |
117 | | |
118 | | double UnscaledCycleClock::Frequency() { |
119 | | uint64_t aarch64_timer_frequency; |
120 | | asm volatile("mrs %0, cntfrq_el0" : "=r"(aarch64_timer_frequency)); |
121 | | return aarch64_timer_frequency; |
122 | | } |
123 | | |
124 | | #elif defined(_M_IX86) || defined(_M_X64) |
125 | | |
126 | | #pragma intrinsic(__rdtsc) |
127 | | |
128 | | int64_t UnscaledCycleClock::Now() { return __rdtsc(); } |
129 | | |
130 | | double UnscaledCycleClock::Frequency() { |
131 | | return base_internal::NominalCPUFrequency(); |
132 | | } |
133 | | |
134 | | #endif |
135 | | |
136 | | } // namespace base_internal |
137 | | ABSL_NAMESPACE_END |
138 | | } // namespace absl |
139 | | |
140 | | #endif // ABSL_USE_UNSCALED_CYCLECLOCK |