Chris@10: /* Chris@10: * Copyright (c) 2003, 2007-11 Matteo Frigo Chris@10: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology Chris@10: * Chris@10: * Permission is hereby granted, free of charge, to any person obtaining Chris@10: * a copy of this software and associated documentation files (the Chris@10: * "Software"), to deal in the Software without restriction, including Chris@10: * without limitation the rights to use, copy, modify, merge, publish, Chris@10: * distribute, sublicense, and/or sell copies of the Software, and to Chris@10: * permit persons to whom the Software is furnished to do so, subject to Chris@10: * the following conditions: Chris@10: * Chris@10: * The above copyright notice and this permission notice shall be Chris@10: * included in all copies or substantial portions of the Software. Chris@10: * Chris@10: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@10: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@10: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@10: * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE Chris@10: * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION Chris@10: * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@10: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@10: * Chris@10: */ Chris@10: Chris@10: Chris@10: /* machine-dependent cycle counters code. Needs to be inlined. */ Chris@10: Chris@10: /***************************************************************************/ Chris@10: /* To use the cycle counters in your code, simply #include "cycle.h" (this Chris@10: file), and then use the functions/macros: Chris@10: Chris@10: ticks getticks(void); Chris@10: Chris@10: ticks is an opaque typedef defined below, representing the current time. Chris@10: You extract the elapsed time between two calls to gettick() via: Chris@10: Chris@10: double elapsed(ticks t1, ticks t0); Chris@10: Chris@10: which returns a double-precision variable in arbitrary units. You Chris@10: are not expected to convert this into human units like seconds; it Chris@10: is intended only for *comparisons* of time intervals. Chris@10: Chris@10: (In order to use some of the OS-dependent timer routines like Chris@10: Solaris' gethrtime, you need to paste the autoconf snippet below Chris@10: into your configure.ac file and #include "config.h" before cycle.h, Chris@10: or define the relevant macros manually if you are not using autoconf.) Chris@10: */ Chris@10: Chris@10: /***************************************************************************/ Chris@10: /* This file uses macros like HAVE_GETHRTIME that are assumed to be Chris@10: defined according to whether the corresponding function/type/header Chris@10: is available on your system. The necessary macros are most Chris@10: conveniently defined if you are using GNU autoconf, via the tests: Chris@10: Chris@10: dnl --------------------------------------------------------------------- Chris@10: Chris@10: AC_C_INLINE Chris@10: AC_HEADER_TIME Chris@10: AC_CHECK_HEADERS([sys/time.h c_asm.h intrinsics.h mach/mach_time.h]) Chris@10: Chris@10: AC_CHECK_TYPE([hrtime_t],[AC_DEFINE(HAVE_HRTIME_T, 1, [Define to 1 if hrtime_t is defined in ])],,[#if HAVE_SYS_TIME_H Chris@10: #include Chris@10: #endif]) Chris@10: Chris@10: AC_CHECK_FUNCS([gethrtime read_real_time time_base_to_time clock_gettime mach_absolute_time]) Chris@10: Chris@10: dnl Cray UNICOS _rtc() (real-time clock) intrinsic Chris@10: AC_MSG_CHECKING([for _rtc intrinsic]) Chris@10: rtc_ok=yes Chris@10: AC_TRY_LINK([#ifdef HAVE_INTRINSICS_H Chris@10: #include Chris@10: #endif], [_rtc()], [AC_DEFINE(HAVE__RTC,1,[Define if you have the UNICOS _rtc() intrinsic.])], [rtc_ok=no]) Chris@10: AC_MSG_RESULT($rtc_ok) Chris@10: Chris@10: dnl --------------------------------------------------------------------- Chris@10: */ Chris@10: Chris@10: /***************************************************************************/ Chris@10: Chris@10: #if TIME_WITH_SYS_TIME Chris@10: # include Chris@10: # include Chris@10: #else Chris@10: # if HAVE_SYS_TIME_H Chris@10: # include Chris@10: # else Chris@10: # include Chris@10: # endif Chris@10: #endif Chris@10: Chris@10: #define INLINE_ELAPSED(INL) static INL double elapsed(ticks t1, ticks t0) \ Chris@10: { \ Chris@10: return (double)t1 - (double)t0; \ Chris@10: } Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* Solaris */ Chris@10: #if defined(HAVE_GETHRTIME) && defined(HAVE_HRTIME_T) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef hrtime_t ticks; Chris@10: Chris@10: #define getticks gethrtime Chris@10: Chris@10: INLINE_ELAPSED(inline) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* AIX v. 4+ routines to read the real-time clock or time-base register */ Chris@10: #if defined(HAVE_READ_REAL_TIME) && defined(HAVE_TIME_BASE_TO_TIME) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef timebasestruct_t ticks; Chris@10: Chris@10: static __inline ticks getticks(void) Chris@10: { Chris@10: ticks t; Chris@10: read_real_time(&t, TIMEBASE_SZ); Chris@10: return t; Chris@10: } Chris@10: Chris@10: static __inline double elapsed(ticks t1, ticks t0) /* time in nanoseconds */ Chris@10: { Chris@10: time_base_to_time(&t1, TIMEBASE_SZ); Chris@10: time_base_to_time(&t0, TIMEBASE_SZ); Chris@10: return (((double)t1.tb_high - (double)t0.tb_high) * 1.0e9 + Chris@10: ((double)t1.tb_low - (double)t0.tb_low)); Chris@10: } Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* Chris@10: * PowerPC ``cycle'' counter using the time base register. Chris@10: */ Chris@10: #if ((((defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))) || (defined(__MWERKS__) && defined(macintosh)))) || (defined(__IBM_GCC_ASM) && (defined(__powerpc__) || defined(__ppc__)))) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned long long ticks; Chris@10: Chris@10: static __inline__ ticks getticks(void) Chris@10: { Chris@10: unsigned int tbl, tbu0, tbu1; Chris@10: Chris@10: do { Chris@10: __asm__ __volatile__ ("mftbu %0" : "=r"(tbu0)); Chris@10: __asm__ __volatile__ ("mftb %0" : "=r"(tbl)); Chris@10: __asm__ __volatile__ ("mftbu %0" : "=r"(tbu1)); Chris@10: } while (tbu0 != tbu1); Chris@10: Chris@10: return (((unsigned long long)tbu0) << 32) | tbl; Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(__inline__) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /* MacOS/Mach (Darwin) time-base register interface (unlike UpTime, Chris@10: from Carbon, requires no additional libraries to be linked). */ Chris@10: #if defined(HAVE_MACH_ABSOLUTE_TIME) && defined(HAVE_MACH_MACH_TIME_H) && !defined(HAVE_TICK_COUNTER) Chris@10: #include Chris@10: typedef uint64_t ticks; Chris@10: #define getticks mach_absolute_time Chris@10: INLINE_ELAPSED(__inline__) Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* Chris@10: * Pentium cycle counter Chris@10: */ Chris@10: #if (defined(__GNUC__) || defined(__ICC)) && defined(__i386__) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned long long ticks; Chris@10: Chris@10: static __inline__ ticks getticks(void) Chris@10: { Chris@10: ticks ret; Chris@10: Chris@10: __asm__ __volatile__("rdtsc": "=A" (ret)); Chris@10: /* no input, nothing else clobbered */ Chris@10: return ret; Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(__inline__) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */ Chris@10: #endif Chris@10: Chris@10: /* Visual C++ -- thanks to Morten Nissov for his help with this */ Chris@10: #if _MSC_VER >= 1200 && _M_IX86 >= 500 && !defined(HAVE_TICK_COUNTER) Chris@10: #include Chris@10: typedef LARGE_INTEGER ticks; Chris@10: #define RDTSC __asm __emit 0fh __asm __emit 031h /* hack for VC++ 5.0 */ Chris@10: Chris@10: static __inline ticks getticks(void) Chris@10: { Chris@10: ticks retval; Chris@10: Chris@10: __asm { Chris@10: RDTSC Chris@10: mov retval.HighPart, edx Chris@10: mov retval.LowPart, eax Chris@10: } Chris@10: return retval; Chris@10: } Chris@10: Chris@10: static __inline double elapsed(ticks t1, ticks t0) Chris@10: { Chris@10: return (double)t1.QuadPart - (double)t0.QuadPart; Chris@10: } Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */ Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* Chris@10: * X86-64 cycle counter Chris@10: */ Chris@10: #if (defined(__GNUC__) || defined(__ICC) || defined(__SUNPRO_C)) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned long long ticks; Chris@10: Chris@10: static __inline__ ticks getticks(void) Chris@10: { Chris@10: unsigned a, d; Chris@10: asm volatile("rdtsc" : "=a" (a), "=d" (d)); Chris@10: return ((ticks)a) | (((ticks)d) << 32); Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(__inline__) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /* PGI compiler, courtesy Cristiano Calonaci, Andrea Tarsi, & Roberto Gori. Chris@10: NOTE: this code will fail to link unless you use the -Masmkeyword compiler Chris@10: option (grrr). */ Chris@10: #if defined(__PGI) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned long long ticks; Chris@10: static ticks getticks(void) Chris@10: { Chris@10: asm(" rdtsc; shl $0x20,%rdx; mov %eax,%eax; or %rdx,%rax; "); Chris@10: } Chris@10: INLINE_ELAPSED(__inline__) Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /* Visual C++, courtesy of Dirk Michaelis */ Chris@10: #if _MSC_VER >= 1400 && (defined(_M_AMD64) || defined(_M_X64)) && !defined(HAVE_TICK_COUNTER) Chris@10: Chris@10: #include Chris@10: #pragma intrinsic(__rdtsc) Chris@10: typedef unsigned __int64 ticks; Chris@10: #define getticks __rdtsc Chris@10: INLINE_ELAPSED(__inline) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* Chris@10: * IA64 cycle counter Chris@10: */ Chris@10: Chris@10: /* intel's icc/ecc compiler */ Chris@10: #if (defined(__EDG_VERSION) || defined(__ECC)) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned long ticks; Chris@10: #include Chris@10: Chris@10: static __inline__ ticks getticks(void) Chris@10: { Chris@10: return __getReg(_IA64_REG_AR_ITC); Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(__inline__) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /* gcc */ Chris@10: #if defined(__GNUC__) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned long ticks; Chris@10: Chris@10: static __inline__ ticks getticks(void) Chris@10: { Chris@10: ticks ret; Chris@10: Chris@10: __asm__ __volatile__ ("mov %0=ar.itc" : "=r"(ret)); Chris@10: return ret; Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(__inline__) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /* HP/UX IA64 compiler, courtesy Teresa L. Johnson: */ Chris@10: #if defined(__hpux) && defined(__ia64) && !defined(HAVE_TICK_COUNTER) Chris@10: #include Chris@10: typedef unsigned long ticks; Chris@10: Chris@10: static inline ticks getticks(void) Chris@10: { Chris@10: ticks ret; Chris@10: Chris@10: ret = _Asm_mov_from_ar (_AREG_ITC); Chris@10: return ret; Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(inline) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /* Microsoft Visual C++ */ Chris@10: #if defined(_MSC_VER) && defined(_M_IA64) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned __int64 ticks; Chris@10: Chris@10: # ifdef __cplusplus Chris@10: extern "C" Chris@10: # endif Chris@10: ticks __getReg(int whichReg); Chris@10: #pragma intrinsic(__getReg) Chris@10: Chris@10: static __inline ticks getticks(void) Chris@10: { Chris@10: volatile ticks temp; Chris@10: temp = __getReg(3116); Chris@10: return temp; Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(inline) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* Chris@10: * PA-RISC cycle counter Chris@10: */ Chris@10: #if defined(__hppa__) || defined(__hppa) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned long ticks; Chris@10: Chris@10: # ifdef __GNUC__ Chris@10: static __inline__ ticks getticks(void) Chris@10: { Chris@10: ticks ret; Chris@10: Chris@10: __asm__ __volatile__("mfctl 16, %0": "=r" (ret)); Chris@10: /* no input, nothing else clobbered */ Chris@10: return ret; Chris@10: } Chris@10: # else Chris@10: # include Chris@10: static inline unsigned long getticks(void) Chris@10: { Chris@10: register ticks ret; Chris@10: _MFCTL(16, ret); Chris@10: return ret; Chris@10: } Chris@10: # endif Chris@10: Chris@10: INLINE_ELAPSED(inline) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* S390, courtesy of James Treacy */ Chris@10: #if defined(__GNUC__) && defined(__s390__) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned long long ticks; Chris@10: Chris@10: static __inline__ ticks getticks(void) Chris@10: { Chris@10: ticks cycles; Chris@10: __asm__("stck 0(%0)" : : "a" (&(cycles)) : "memory", "cc"); Chris@10: return cycles; Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(__inline__) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: /*----------------------------------------------------------------*/ Chris@10: #if defined(__GNUC__) && defined(__alpha__) && !defined(HAVE_TICK_COUNTER) Chris@10: /* Chris@10: * The 32-bit cycle counter on alpha overflows pretty quickly, Chris@10: * unfortunately. A 1GHz machine overflows in 4 seconds. Chris@10: */ Chris@10: typedef unsigned int ticks; Chris@10: Chris@10: static __inline__ ticks getticks(void) Chris@10: { Chris@10: unsigned long cc; Chris@10: __asm__ __volatile__ ("rpcc %0" : "=r"(cc)); Chris@10: return (cc & 0xFFFFFFFF); Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(__inline__) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: #if defined(__GNUC__) && defined(__sparc_v9__) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef unsigned long ticks; Chris@10: Chris@10: static __inline__ ticks getticks(void) Chris@10: { Chris@10: ticks ret; Chris@10: __asm__ __volatile__("rd %%tick, %0" : "=r" (ret)); Chris@10: return ret; Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(__inline__) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: #if (defined(__DECC) || defined(__DECCXX)) && defined(__alpha) && defined(HAVE_C_ASM_H) && !defined(HAVE_TICK_COUNTER) Chris@10: # include Chris@10: typedef unsigned int ticks; Chris@10: Chris@10: static __inline ticks getticks(void) Chris@10: { Chris@10: unsigned long cc; Chris@10: cc = asm("rpcc %v0"); Chris@10: return (cc & 0xFFFFFFFF); Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(__inline) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* SGI/Irix */ Chris@10: #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_SGI_CYCLE) && !defined(HAVE_TICK_COUNTER) Chris@10: typedef struct timespec ticks; Chris@10: Chris@10: static inline ticks getticks(void) Chris@10: { Chris@10: struct timespec t; Chris@10: clock_gettime(CLOCK_SGI_CYCLE, &t); Chris@10: return t; Chris@10: } Chris@10: Chris@10: static inline double elapsed(ticks t1, ticks t0) Chris@10: { Chris@10: return ((double)t1.tv_sec - (double)t0.tv_sec) * 1.0E9 + Chris@10: ((double)t1.tv_nsec - (double)t0.tv_nsec); Chris@10: } Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* Cray UNICOS _rtc() intrinsic function */ Chris@10: #if defined(HAVE__RTC) && !defined(HAVE_TICK_COUNTER) Chris@10: #ifdef HAVE_INTRINSICS_H Chris@10: # include Chris@10: #endif Chris@10: Chris@10: typedef long long ticks; Chris@10: Chris@10: #define getticks _rtc Chris@10: Chris@10: INLINE_ELAPSED(inline) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: Chris@10: /*----------------------------------------------------------------*/ Chris@10: /* MIPS ZBus */ Chris@10: #if HAVE_MIPS_ZBUS_TIMER Chris@10: #if defined(__mips__) && !defined(HAVE_TICK_COUNTER) Chris@10: #include Chris@10: #include Chris@10: #include Chris@10: Chris@10: typedef uint64_t ticks; Chris@10: Chris@10: static inline ticks getticks(void) Chris@10: { Chris@10: static uint64_t* addr = 0; Chris@10: Chris@10: if (addr == 0) Chris@10: { Chris@10: uint32_t rq_addr = 0x10030000; Chris@10: int fd; Chris@10: int pgsize; Chris@10: Chris@10: pgsize = getpagesize(); Chris@10: fd = open ("/dev/mem", O_RDONLY | O_SYNC, 0); Chris@10: if (fd < 0) { Chris@10: perror("open"); Chris@10: return NULL; Chris@10: } Chris@10: addr = mmap(0, pgsize, PROT_READ, MAP_SHARED, fd, rq_addr); Chris@10: close(fd); Chris@10: if (addr == (uint64_t *)-1) { Chris@10: perror("mmap"); Chris@10: return NULL; Chris@10: } Chris@10: } Chris@10: Chris@10: return *addr; Chris@10: } Chris@10: Chris@10: INLINE_ELAPSED(inline) Chris@10: Chris@10: #define HAVE_TICK_COUNTER Chris@10: #endif Chris@10: #endif /* HAVE_MIPS_ZBUS_TIMER */ Chris@10: