| 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 the __rdtsc intrinsic, declared in <intrin.h>. |
| 59 // with even ancient MSVC compilers), and when not possible the | 54 // Unfortunately, in some environments, <windows.h> and <intrin.h> have |
| 60 // __rdtsc intrinsic, declared in <intrin.h>. Unfortunately, in some | 55 // conflicting declarations of some other intrinsics, breaking compilation. |
| 61 // environments, <windows.h> and <intrin.h> have conflicting | |
| 62 // declarations of some other intrinsics, breaking compilation. | |
| 63 // Therefore, we simply declare __rdtsc ourselves. See also | 56 // Therefore, we simply declare __rdtsc ourselves. See also |
| 64 // http://connect.microsoft.com/VisualStudio/feedback/details/262047 | 57 // http://connect.microsoft.com/VisualStudio/feedback/details/262047 |
| 65 #if defined(_MSC_VER) && !defined(_M_IX86) | 58 #if defined(_MSC_VER) |
| 66 extern "C" uint64 __rdtsc(); | 59 extern "C" uint64 __rdtsc(); |
| 67 #pragma intrinsic(__rdtsc) | 60 #pragma intrinsic(__rdtsc) |
| 68 #endif | 61 #endif |
| 69 #if defined(ARMV3) || defined(__mips__) | |
| 70 #ifdef HAVE_SYS_TIME_H | 62 #ifdef HAVE_SYS_TIME_H |
| 71 #include <sys/time.h> | 63 #include <sys/time.h> |
| 72 #endif | 64 #endif |
| 73 #endif | |
| 74 | 65 |
| 75 // NOTE: only i386 and x86_64 have been well tested. | 66 // NOTE: only i386 and x86_64 have been well tested. |
| 76 // PPC, sparc, alpha, and ia64 are based on | 67 // PPC, sparc, alpha, and ia64 are based on |
| 77 // http://peter.kuscsik.com/wordpress/?p=14 | 68 // http://peter.kuscsik.com/wordpress/?p=14 |
| 78 // with modifications by m3b. See also | 69 // with modifications by m3b. See also |
| 79 // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h | 70 // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h |
| 80 struct CycleClock { | 71 struct CycleClock { |
| 81 // This should return the number of cycles since power-on. Thread-safe. | 72 // This should return the number of cycles since power-on. Thread-safe. |
| 82 static inline int64 Now() { | 73 static inline int64 Now() { |
| 83 #if defined(__MACH__) && defined(__APPLE__) | 74 #if defined(__MACH__) && defined(__APPLE__) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 110 return (tbu1 << 32) | tbl; | 101 return (tbu1 << 32) | tbl; |
| 111 #elif defined(__sparc__) | 102 #elif defined(__sparc__) |
| 112 int64 tick; | 103 int64 tick; |
| 113 asm(".byte 0x83, 0x41, 0x00, 0x00"); | 104 asm(".byte 0x83, 0x41, 0x00, 0x00"); |
| 114 asm("mov %%g1, %0" : "=r" (tick)); | 105 asm("mov %%g1, %0" : "=r" (tick)); |
| 115 return tick; | 106 return tick; |
| 116 #elif defined(__ia64__) | 107 #elif defined(__ia64__) |
| 117 int64 itc; | 108 int64 itc; |
| 118 asm("mov %0 = ar.itc" : "=r" (itc)); | 109 asm("mov %0 = ar.itc" : "=r" (itc)); |
| 119 return itc; | 110 return itc; |
| 120 #elif defined(_MSC_VER) && defined(_M_IX86) | |
| 121 // Older MSVC compilers (like 7.x) don't seem to support the | |
| 122 // __rdtsc intrinsic properly, so I prefer to use _asm instead | |
| 123 // when I know it will work. Otherwise, I'll use __rdtsc and hope | |
| 124 // the code is being compiled with a non-ancient compiler. | |
| 125 _asm rdtsc | |
| 126 #elif defined(_MSC_VER) | 111 #elif defined(_MSC_VER) |
| 127 return __rdtsc(); | 112 return __rdtsc(); |
| 128 #elif defined(ARMV3) | 113 #elif defined(ARMV3) |
| 129 #if defined(ARMV6) // V6 is the earliest arch that has a standard cyclecount | 114 #if defined(ARMV6) // V6 is the earliest arch that has a standard cyclecount |
| 130 uint32 pmccntr; | 115 uint32 pmccntr; |
| 131 uint32 pmuseren; | 116 uint32 pmuseren; |
| 132 uint32 pmcntenset; | 117 uint32 pmcntenset; |
| 133 // Read the user mode perf monitor counter access permissions. | 118 // Read the user mode perf monitor counter access permissions. |
| 134 asm volatile ("mrc p15, 0, %0, c9, c14, 0" : "=r" (pmuseren)); | 119 asm("mrc p15, 0, %0, c9, c14, 0" : "=r" (pmuseren)); |
| 135 if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. | 120 if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. |
| 136 asm volatile ("mrc p15, 0, %0, c9, c12, 1" : "=r" (pmcntenset)); | 121 asm("mrc p15, 0, %0, c9, c12, 1" : "=r" (pmcntenset)); |
| 137 if (pmcntenset & 0x80000000ul) { // Is it counting? | 122 if (pmcntenset & 0x80000000ul) { // Is it counting? |
| 138 asm volatile ("mrc p15, 0, %0, c9, c13, 0" : "=r" (pmccntr)); | 123 asm("mrc p15, 0, %0, c9, c13, 0" : "=r" (pmccntr)); |
| 139 // The counter is set up to count every 64th cycle | 124 // The counter is set up to count every 64th cycle |
| 140 return static_cast<int64>(pmccntr) * 64; // Should optimize to << 6 | 125 return static_cast<int64>(pmccntr) * 64; // Should optimize to << 6 |
| 141 } | 126 } |
| 142 } | 127 } |
| 143 #endif | 128 #endif |
| 144 struct timeval tv; | 129 struct timeval tv; |
| 145 gettimeofday(&tv, NULL); | 130 gettimeofday(&tv, NULL); |
| 146 return static_cast<int64>((tv.tv_sec + tv.tv_usec * 0.000001) | 131 return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec; |
| 147 * CyclesPerSecond()); | |
| 148 #elif defined(__mips__) | |
| 149 // mips apparently only allows rdtsc for superusers, so we fall | |
| 150 // back to gettimeofday. It's possible clock_gettime would be better. | |
| 151 struct timeval tv; | |
| 152 gettimeofday(&tv, NULL); | |
| 153 return static_cast<int64>((tv.tv_sec + tv.tv_usec * 0.000001) | |
| 154 * CyclesPerSecond()); | |
| 155 #else | 132 #else |
| 156 // The soft failover to a generic implementation is automatic only for ARM. | 133 // The soft failover to a generic implementation is automatic only for ARM. |
| 157 // For other platforms the developer is expected to make an attempt to create | 134 // For other platforms the developer is expected to make an attempt to create |
| 158 // a fast implementation and use generic version if nothing better is available. | 135 // a fast implementation and use generic version if nothing better is available. |
| 159 #error You need to define CycleTimer for your O/S and CPU | 136 #error You need to define CycleTimer for your O/S and CPU |
| 160 #endif | 137 #endif |
| 161 } | 138 } |
| 162 }; | 139 }; |
| 163 | 140 |
| 164 | 141 |
| 165 #endif // GOOGLE_BASE_CYCLECLOCK_H_ | 142 #endif // GOOGLE_BASE_CYCLECLOCK_H_ |
| OLD | NEW |