OLD | NEW |
1 // Copyright (c) 2004, Google Inc. | 1 // Copyright (c) 2004, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 29 matching lines...) Expand all Loading... |
40 // Also, in some out of order CPU implementations, the CycleClock is not | 40 // Also, in some out of order CPU implementations, the CycleClock is not |
41 // serializing. So if you're trying to count at cycles granularity, your | 41 // serializing. So if you're trying to count at cycles granularity, your |
42 // data might be inaccurate due to out of order instruction execution. | 42 // data might be inaccurate due to out of order instruction execution. |
43 // ---------------------------------------------------------------------- | 43 // ---------------------------------------------------------------------- |
44 | 44 |
45 #ifndef GOOGLE_BASE_CYCLECLOCK_H_ | 45 #ifndef GOOGLE_BASE_CYCLECLOCK_H_ |
46 #define GOOGLE_BASE_CYCLECLOCK_H_ | 46 #define GOOGLE_BASE_CYCLECLOCK_H_ |
47 | 47 |
48 #include "base/basictypes.h" // make sure we get the def for int64 | 48 #include "base/basictypes.h" // make sure we get the def for int64 |
49 #include "base/arm_instruction_set_select.h" | 49 #include "base/arm_instruction_set_select.h" |
50 // base/sysinfo.h is really big and we don't want to include it unless | |
51 // it is necessary. | |
52 #if defined(__arm__) || defined(__mips__) | |
53 # include "base/sysinfo.h" | |
54 #endif | |
55 #if defined(__MACH__) && defined(__APPLE__) | 50 #if defined(__MACH__) && defined(__APPLE__) |
56 # include <mach/mach_time.h> | 51 # include <mach/mach_time.h> |
57 #endif | 52 #endif |
58 // For MSVC, we want to use '_asm rdtsc' when possible (since it works | 53 // For MSVC, we want to use '_asm rdtsc' when possible (since it works |
59 // with even ancient MSVC compilers), and when not possible the | 54 // with even ancient MSVC compilers), and when not possible the |
60 // __rdtsc intrinsic, declared in <intrin.h>. Unfortunately, in some | 55 // __rdtsc intrinsic, declared in <intrin.h>. Unfortunately, in some |
61 // environments, <windows.h> and <intrin.h> have conflicting | 56 // environments, <windows.h> and <intrin.h> have conflicting |
62 // declarations of some other intrinsics, breaking compilation. | 57 // declarations of some other intrinsics, breaking compilation. |
63 // Therefore, we simply declare __rdtsc ourselves. See also | 58 // Therefore, we simply declare __rdtsc ourselves. See also |
64 // http://connect.microsoft.com/VisualStudio/feedback/details/262047 | 59 // http://connect.microsoft.com/VisualStudio/feedback/details/262047 |
65 #if defined(_MSC_VER) && !defined(_M_IX86) | 60 #if defined(_MSC_VER) && !defined(_M_IX86) |
66 extern "C" uint64 __rdtsc(); | 61 extern "C" uint64 __rdtsc(); |
67 #pragma intrinsic(__rdtsc) | 62 #pragma intrinsic(__rdtsc) |
68 #endif | 63 #endif |
69 #if defined(ARMV3) || defined(__mips__) | 64 #ifdef ARMV3 |
70 #include <sys/time.h> | 65 #include <sys/time.h> |
71 #endif | 66 #endif |
72 | 67 |
73 // NOTE: only i386 and x86_64 have been well tested. | 68 // NOTE: only i386 and x86_64 have been well tested. |
74 // PPC, sparc, alpha, and ia64 are based on | 69 // PPC, sparc, alpha, and ia64 are based on |
75 // http://peter.kuscsik.com/wordpress/?p=14 | 70 // http://peter.kuscsik.com/wordpress/?p=14 |
76 // with modifications by m3b. See also | 71 // with modifications by m3b. See also |
77 // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h | 72 // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h |
78 struct CycleClock { | 73 struct CycleClock { |
79 // This should return the number of cycles since power-on. Thread-safe. | 74 // This should return the number of cycles since power-on. Thread-safe. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // the code is being compiled with a non-ancient compiler. | 117 // the code is being compiled with a non-ancient compiler. |
123 _asm rdtsc | 118 _asm rdtsc |
124 #elif defined(_MSC_VER) | 119 #elif defined(_MSC_VER) |
125 return __rdtsc(); | 120 return __rdtsc(); |
126 #elif defined(ARMV3) | 121 #elif defined(ARMV3) |
127 #if defined(ARMV6) // V6 is the earliest arch that has a standard cyclecount | 122 #if defined(ARMV6) // V6 is the earliest arch that has a standard cyclecount |
128 uint32 pmccntr; | 123 uint32 pmccntr; |
129 uint32 pmuseren; | 124 uint32 pmuseren; |
130 uint32 pmcntenset; | 125 uint32 pmcntenset; |
131 // Read the user mode perf monitor counter access permissions. | 126 // Read the user mode perf monitor counter access permissions. |
132 asm volatile ("mrc p15, 0, %0, c9, c14, 0" : "=r" (pmuseren)); | 127 asm("mrc p15, 0, %0, c9, c14, 0" : "=r" (pmuseren)); |
133 if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. | 128 if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. |
134 asm volatile ("mrc p15, 0, %0, c9, c12, 1" : "=r" (pmcntenset)); | 129 asm("mrc p15, 0, %0, c9, c12, 1" : "=r" (pmcntenset)); |
135 if (pmcntenset & 0x80000000ul) { // Is it counting? | 130 if (pmcntenset & 0x80000000ul) { // Is it counting? |
136 asm volatile ("mrc p15, 0, %0, c9, c13, 0" : "=r" (pmccntr)); | 131 asm("mrc p15, 0, %0, c9, c13, 0" : "=r" (pmccntr)); |
137 // The counter is set up to count every 64th cycle | 132 // The counter is set up to count every 64th cycle |
138 return static_cast<int64>(pmccntr) * 64; // Should optimize to << 6 | 133 return static_cast<int64>(pmccntr) * 64; // Should optimize to << 6 |
139 } | 134 } |
140 } | 135 } |
141 #endif | 136 #endif |
142 struct timeval tv; | 137 struct timeval tv; |
143 gettimeofday(&tv, NULL); | 138 gettimeofday(&tv, NULL); |
144 return static_cast<int64>((tv.tv_sec + tv.tv_usec * 0.000001) | 139 return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec; |
145 * CyclesPerSecond()); | |
146 #elif defined(__mips__) | |
147 // mips apparently only allows rdtsc for superusers, so we fall | |
148 // back to gettimeofday. It's possible clock_gettime would be better. | |
149 struct timeval tv; | |
150 gettimeofday(&tv, NULL); | |
151 return static_cast<int64>((tv.tv_sec + tv.tv_usec * 0.000001) | |
152 * CyclesPerSecond()); | |
153 #else | 140 #else |
154 // The soft failover to a generic implementation is automatic only for ARM. | 141 // The soft failover to a generic implementation is automatic only for ARM. |
155 // For other platforms the developer is expected to make an attempt to create | 142 // For other platforms the developer is expected to make an attempt to create |
156 // a fast implementation and use generic version if nothing better is available. | 143 // a fast implementation and use generic version if nothing better is available. |
157 #error You need to define CycleTimer for your O/S and CPU | 144 #error You need to define CycleTimer for your O/S and CPU |
158 #endif | 145 #endif |
159 } | 146 } |
160 }; | 147 }; |
161 | 148 |
162 | 149 |
163 #endif // GOOGLE_BASE_CYCLECLOCK_H_ | 150 #endif // GOOGLE_BASE_CYCLECLOCK_H_ |
OLD | NEW |