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