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