1*58b9f456SAndroid Build Coastguard Worker // ----------------------------------------------------------------------
2*58b9f456SAndroid Build Coastguard Worker // CycleClock
3*58b9f456SAndroid Build Coastguard Worker // A CycleClock tells you the current time in Cycles. The "time"
4*58b9f456SAndroid Build Coastguard Worker // is actually time since power-on. This is like time() but doesn't
5*58b9f456SAndroid Build Coastguard Worker // involve a system call and is much more precise.
6*58b9f456SAndroid Build Coastguard Worker //
7*58b9f456SAndroid Build Coastguard Worker // NOTE: Not all cpu/platform/kernel combinations guarantee that this
8*58b9f456SAndroid Build Coastguard Worker // clock increments at a constant rate or is synchronized across all logical
9*58b9f456SAndroid Build Coastguard Worker // cpus in a system.
10*58b9f456SAndroid Build Coastguard Worker //
11*58b9f456SAndroid Build Coastguard Worker // If you need the above guarantees, please consider using a different
12*58b9f456SAndroid Build Coastguard Worker // API. There are efforts to provide an interface which provides a millisecond
13*58b9f456SAndroid Build Coastguard Worker // granularity and implemented as a memory read. A memory read is generally
14*58b9f456SAndroid Build Coastguard Worker // cheaper than the CycleClock for many architectures.
15*58b9f456SAndroid Build Coastguard Worker //
16*58b9f456SAndroid Build Coastguard Worker // Also, in some out of order CPU implementations, the CycleClock is not
17*58b9f456SAndroid Build Coastguard Worker // serializing. So if you're trying to count at cycles granularity, your
18*58b9f456SAndroid Build Coastguard Worker // data might be inaccurate due to out of order instruction execution.
19*58b9f456SAndroid Build Coastguard Worker // ----------------------------------------------------------------------
20*58b9f456SAndroid Build Coastguard Worker
21*58b9f456SAndroid Build Coastguard Worker #ifndef BENCHMARK_CYCLECLOCK_H_
22*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_CYCLECLOCK_H_
23*58b9f456SAndroid Build Coastguard Worker
24*58b9f456SAndroid Build Coastguard Worker #include <cstdint>
25*58b9f456SAndroid Build Coastguard Worker
26*58b9f456SAndroid Build Coastguard Worker #include "benchmark/benchmark.h"
27*58b9f456SAndroid Build Coastguard Worker #include "internal_macros.h"
28*58b9f456SAndroid Build Coastguard Worker
29*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_OS_MACOSX)
30*58b9f456SAndroid Build Coastguard Worker #include <mach/mach_time.h>
31*58b9f456SAndroid Build Coastguard Worker #endif
32*58b9f456SAndroid Build Coastguard Worker // For MSVC, we want to use '_asm rdtsc' when possible (since it works
33*58b9f456SAndroid Build Coastguard Worker // with even ancient MSVC compilers), and when not possible the
34*58b9f456SAndroid Build Coastguard Worker // __rdtsc intrinsic, declared in <intrin.h>. Unfortunately, in some
35*58b9f456SAndroid Build Coastguard Worker // environments, <windows.h> and <intrin.h> have conflicting
36*58b9f456SAndroid Build Coastguard Worker // declarations of some other intrinsics, breaking compilation.
37*58b9f456SAndroid Build Coastguard Worker // Therefore, we simply declare __rdtsc ourselves. See also
38*58b9f456SAndroid Build Coastguard Worker // http://connect.microsoft.com/VisualStudio/feedback/details/262047
39*58b9f456SAndroid Build Coastguard Worker #if defined(COMPILER_MSVC) && !defined(_M_IX86)
40*58b9f456SAndroid Build Coastguard Worker extern "C" uint64_t __rdtsc();
41*58b9f456SAndroid Build Coastguard Worker #pragma intrinsic(__rdtsc)
42*58b9f456SAndroid Build Coastguard Worker #endif
43*58b9f456SAndroid Build Coastguard Worker
44*58b9f456SAndroid Build Coastguard Worker #if !defined(BENCHMARK_OS_WINDOWS) || defined(BENCHMARK_OS_MINGW)
45*58b9f456SAndroid Build Coastguard Worker #include <sys/time.h>
46*58b9f456SAndroid Build Coastguard Worker #include <time.h>
47*58b9f456SAndroid Build Coastguard Worker #endif
48*58b9f456SAndroid Build Coastguard Worker
49*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_OS_EMSCRIPTEN
50*58b9f456SAndroid Build Coastguard Worker #include <emscripten.h>
51*58b9f456SAndroid Build Coastguard Worker #endif
52*58b9f456SAndroid Build Coastguard Worker
53*58b9f456SAndroid Build Coastguard Worker namespace benchmark {
54*58b9f456SAndroid Build Coastguard Worker // NOTE: only i386 and x86_64 have been well tested.
55*58b9f456SAndroid Build Coastguard Worker // PPC, sparc, alpha, and ia64 are based on
56*58b9f456SAndroid Build Coastguard Worker // http://peter.kuscsik.com/wordpress/?p=14
57*58b9f456SAndroid Build Coastguard Worker // with modifications by m3b. See also
58*58b9f456SAndroid Build Coastguard Worker // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h
59*58b9f456SAndroid Build Coastguard Worker namespace cycleclock {
60*58b9f456SAndroid Build Coastguard Worker // This should return the number of cycles since power-on. Thread-safe.
Now()61*58b9f456SAndroid Build Coastguard Worker inline BENCHMARK_ALWAYS_INLINE int64_t Now() {
62*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_OS_MACOSX)
63*58b9f456SAndroid Build Coastguard Worker // this goes at the top because we need ALL Macs, regardless of
64*58b9f456SAndroid Build Coastguard Worker // architecture, to return the number of "mach time units" that
65*58b9f456SAndroid Build Coastguard Worker // have passed since startup. See sysinfo.cc where
66*58b9f456SAndroid Build Coastguard Worker // InitializeSystemInfo() sets the supposed cpu clock frequency of
67*58b9f456SAndroid Build Coastguard Worker // macs to the number of mach time units per second, not actual
68*58b9f456SAndroid Build Coastguard Worker // CPU clock frequency (which can change in the face of CPU
69*58b9f456SAndroid Build Coastguard Worker // frequency scaling). Also note that when the Mac sleeps, this
70*58b9f456SAndroid Build Coastguard Worker // counter pauses; it does not continue counting, nor does it
71*58b9f456SAndroid Build Coastguard Worker // reset to zero.
72*58b9f456SAndroid Build Coastguard Worker return mach_absolute_time();
73*58b9f456SAndroid Build Coastguard Worker #elif defined(BENCHMARK_OS_EMSCRIPTEN)
74*58b9f456SAndroid Build Coastguard Worker // this goes above x86-specific code because old versions of Emscripten
75*58b9f456SAndroid Build Coastguard Worker // define __x86_64__, although they have nothing to do with it.
76*58b9f456SAndroid Build Coastguard Worker return static_cast<int64_t>(emscripten_get_now() * 1e+6);
77*58b9f456SAndroid Build Coastguard Worker #elif defined(__i386__)
78*58b9f456SAndroid Build Coastguard Worker int64_t ret;
79*58b9f456SAndroid Build Coastguard Worker __asm__ volatile("rdtsc" : "=A"(ret));
80*58b9f456SAndroid Build Coastguard Worker return ret;
81*58b9f456SAndroid Build Coastguard Worker #elif defined(__x86_64__) || defined(__amd64__)
82*58b9f456SAndroid Build Coastguard Worker uint64_t low, high;
83*58b9f456SAndroid Build Coastguard Worker __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
84*58b9f456SAndroid Build Coastguard Worker return (high << 32) | low;
85*58b9f456SAndroid Build Coastguard Worker #elif defined(__powerpc__) || defined(__ppc__)
86*58b9f456SAndroid Build Coastguard Worker // This returns a time-base, which is not always precisely a cycle-count.
87*58b9f456SAndroid Build Coastguard Worker int64_t tbl, tbu0, tbu1;
88*58b9f456SAndroid Build Coastguard Worker asm("mftbu %0" : "=r"(tbu0));
89*58b9f456SAndroid Build Coastguard Worker asm("mftb %0" : "=r"(tbl));
90*58b9f456SAndroid Build Coastguard Worker asm("mftbu %0" : "=r"(tbu1));
91*58b9f456SAndroid Build Coastguard Worker tbl &= -static_cast<int64_t>(tbu0 == tbu1);
92*58b9f456SAndroid Build Coastguard Worker // high 32 bits in tbu1; low 32 bits in tbl (tbu0 is garbage)
93*58b9f456SAndroid Build Coastguard Worker return (tbu1 << 32) | tbl;
94*58b9f456SAndroid Build Coastguard Worker #elif defined(__sparc__)
95*58b9f456SAndroid Build Coastguard Worker int64_t tick;
96*58b9f456SAndroid Build Coastguard Worker asm(".byte 0x83, 0x41, 0x00, 0x00");
97*58b9f456SAndroid Build Coastguard Worker asm("mov %%g1, %0" : "=r"(tick));
98*58b9f456SAndroid Build Coastguard Worker return tick;
99*58b9f456SAndroid Build Coastguard Worker #elif defined(__ia64__)
100*58b9f456SAndroid Build Coastguard Worker int64_t itc;
101*58b9f456SAndroid Build Coastguard Worker asm("mov %0 = ar.itc" : "=r"(itc));
102*58b9f456SAndroid Build Coastguard Worker return itc;
103*58b9f456SAndroid Build Coastguard Worker #elif defined(COMPILER_MSVC) && defined(_M_IX86)
104*58b9f456SAndroid Build Coastguard Worker // Older MSVC compilers (like 7.x) don't seem to support the
105*58b9f456SAndroid Build Coastguard Worker // __rdtsc intrinsic properly, so I prefer to use _asm instead
106*58b9f456SAndroid Build Coastguard Worker // when I know it will work. Otherwise, I'll use __rdtsc and hope
107*58b9f456SAndroid Build Coastguard Worker // the code is being compiled with a non-ancient compiler.
108*58b9f456SAndroid Build Coastguard Worker _asm rdtsc
109*58b9f456SAndroid Build Coastguard Worker #elif defined(COMPILER_MSVC)
110*58b9f456SAndroid Build Coastguard Worker return __rdtsc();
111*58b9f456SAndroid Build Coastguard Worker #elif defined(BENCHMARK_OS_NACL)
112*58b9f456SAndroid Build Coastguard Worker // Native Client validator on x86/x86-64 allows RDTSC instructions,
113*58b9f456SAndroid Build Coastguard Worker // and this case is handled above. Native Client validator on ARM
114*58b9f456SAndroid Build Coastguard Worker // rejects MRC instructions (used in the ARM-specific sequence below),
115*58b9f456SAndroid Build Coastguard Worker // so we handle it here. Portable Native Client compiles to
116*58b9f456SAndroid Build Coastguard Worker // architecture-agnostic bytecode, which doesn't provide any
117*58b9f456SAndroid Build Coastguard Worker // cycle counter access mnemonics.
118*58b9f456SAndroid Build Coastguard Worker
119*58b9f456SAndroid Build Coastguard Worker // Native Client does not provide any API to access cycle counter.
120*58b9f456SAndroid Build Coastguard Worker // Use clock_gettime(CLOCK_MONOTONIC, ...) instead of gettimeofday
121*58b9f456SAndroid Build Coastguard Worker // because is provides nanosecond resolution (which is noticable at
122*58b9f456SAndroid Build Coastguard Worker // least for PNaCl modules running on x86 Mac & Linux).
123*58b9f456SAndroid Build Coastguard Worker // Initialize to always return 0 if clock_gettime fails.
124*58b9f456SAndroid Build Coastguard Worker struct timespec ts = {0, 0};
125*58b9f456SAndroid Build Coastguard Worker clock_gettime(CLOCK_MONOTONIC, &ts);
126*58b9f456SAndroid Build Coastguard Worker return static_cast<int64_t>(ts.tv_sec) * 1000000000 + ts.tv_nsec;
127*58b9f456SAndroid Build Coastguard Worker #elif defined(__aarch64__)
128*58b9f456SAndroid Build Coastguard Worker // System timer of ARMv8 runs at a different frequency than the CPU's.
129*58b9f456SAndroid Build Coastguard Worker // The frequency is fixed, typically in the range 1-50MHz. It can be
130*58b9f456SAndroid Build Coastguard Worker // read at CNTFRQ special register. We assume the OS has set up
131*58b9f456SAndroid Build Coastguard Worker // the virtual timer properly.
132*58b9f456SAndroid Build Coastguard Worker int64_t virtual_timer_value;
133*58b9f456SAndroid Build Coastguard Worker asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
134*58b9f456SAndroid Build Coastguard Worker return virtual_timer_value;
135*58b9f456SAndroid Build Coastguard Worker #elif defined(__ARM_ARCH)
136*58b9f456SAndroid Build Coastguard Worker // V6 is the earliest arch that has a standard cyclecount
137*58b9f456SAndroid Build Coastguard Worker // Native Client validator doesn't allow MRC instructions.
138*58b9f456SAndroid Build Coastguard Worker #if (__ARM_ARCH >= 6)
139*58b9f456SAndroid Build Coastguard Worker uint32_t pmccntr;
140*58b9f456SAndroid Build Coastguard Worker uint32_t pmuseren;
141*58b9f456SAndroid Build Coastguard Worker uint32_t pmcntenset;
142*58b9f456SAndroid Build Coastguard Worker // Read the user mode perf monitor counter access permissions.
143*58b9f456SAndroid Build Coastguard Worker asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r"(pmuseren));
144*58b9f456SAndroid Build Coastguard Worker if (pmuseren & 1) { // Allows reading perfmon counters for user mode code.
145*58b9f456SAndroid Build Coastguard Worker asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r"(pmcntenset));
146*58b9f456SAndroid Build Coastguard Worker if (pmcntenset & 0x80000000ul) { // Is it counting?
147*58b9f456SAndroid Build Coastguard Worker asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(pmccntr));
148*58b9f456SAndroid Build Coastguard Worker // The counter is set up to count every 64th cycle
149*58b9f456SAndroid Build Coastguard Worker return static_cast<int64_t>(pmccntr) * 64; // Should optimize to << 6
150*58b9f456SAndroid Build Coastguard Worker }
151*58b9f456SAndroid Build Coastguard Worker }
152*58b9f456SAndroid Build Coastguard Worker #endif
153*58b9f456SAndroid Build Coastguard Worker struct timeval tv;
154*58b9f456SAndroid Build Coastguard Worker gettimeofday(&tv, nullptr);
155*58b9f456SAndroid Build Coastguard Worker return static_cast<int64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
156*58b9f456SAndroid Build Coastguard Worker #elif defined(__mips__)
157*58b9f456SAndroid Build Coastguard Worker // mips apparently only allows rdtsc for superusers, so we fall
158*58b9f456SAndroid Build Coastguard Worker // back to gettimeofday. It's possible clock_gettime would be better.
159*58b9f456SAndroid Build Coastguard Worker struct timeval tv;
160*58b9f456SAndroid Build Coastguard Worker gettimeofday(&tv, nullptr);
161*58b9f456SAndroid Build Coastguard Worker return static_cast<int64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
162*58b9f456SAndroid Build Coastguard Worker #elif defined(__s390__) // Covers both s390 and s390x.
163*58b9f456SAndroid Build Coastguard Worker // Return the CPU clock.
164*58b9f456SAndroid Build Coastguard Worker uint64_t tsc;
165*58b9f456SAndroid Build Coastguard Worker asm("stck %0" : "=Q"(tsc) : : "cc");
166*58b9f456SAndroid Build Coastguard Worker return tsc;
167*58b9f456SAndroid Build Coastguard Worker #else
168*58b9f456SAndroid Build Coastguard Worker // The soft failover to a generic implementation is automatic only for ARM.
169*58b9f456SAndroid Build Coastguard Worker // For other platforms the developer is expected to make an attempt to create
170*58b9f456SAndroid Build Coastguard Worker // a fast implementation and use generic version if nothing better is available.
171*58b9f456SAndroid Build Coastguard Worker #error You need to define CycleTimer for your OS and CPU
172*58b9f456SAndroid Build Coastguard Worker #endif
173*58b9f456SAndroid Build Coastguard Worker }
174*58b9f456SAndroid Build Coastguard Worker } // end namespace cycleclock
175*58b9f456SAndroid Build Coastguard Worker } // end namespace benchmark
176*58b9f456SAndroid Build Coastguard Worker
177*58b9f456SAndroid Build Coastguard Worker #endif // BENCHMARK_CYCLECLOCK_H_
178