cycle.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright (c) 2003, 2007 Matteo Frigo
  3. * Copyright (c) 2003, 2007 Massachusetts Institute of Technology
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. /* machine-dependent cycle counters code. Needs to be inlined. */
  26. /***************************************************************************/
  27. /* To use the cycle counters in your code, simply #include "cycle.h" (this
  28. file), and then use the functions/macros:
  29. ticks getticks(void);
  30. ticks is an opaque typedef defined below, representing the current time.
  31. You extract the elapsed time between two calls to gettick() via:
  32. double elapsed(ticks t1, ticks t0);
  33. which returns a double-precision variable in arbitrary units. You
  34. are not expected to convert this into human units like seconds; it
  35. is intended only for *comparisons* of time intervals.
  36. (In order to use some of the OS-dependent timer routines like
  37. Solaris' gethrtime, you need to paste the autoconf snippet below
  38. into your configure.ac file and #include "config.h" before cycle.h,
  39. or define the relevant macros manually if you are not using autoconf.)
  40. */
  41. /***************************************************************************/
  42. /* This file uses macros like HAVE_GETHRTIME that are assumed to be
  43. defined according to whether the corresponding function/type/header
  44. is available on your system. The necessary macros are most
  45. conveniently defined if you are using GNU autoconf, via the tests:
  46. dnl ---------------------------------------------------------------------
  47. AC_C_INLINE
  48. AC_HEADER_TIME
  49. AC_CHECK_HEADERS([sys/time.h c_asm.h intrinsics.h mach/mach_time.h])
  50. AC_CHECK_TYPE([hrtime_t],[AC_DEFINE(HAVE_HRTIME_T, 1, [Define to 1 if hrtime_t is defined in <sys/time.h>])],,[#if HAVE_SYS_TIME_H
  51. #include <sys/time.h>
  52. #endif])
  53. AC_CHECK_FUNCS([gethrtime read_real_time time_base_to_time clock_gettime mach_absolute_time])
  54. dnl Cray UNICOS _rtc() (real-time clock) intrinsic
  55. AC_MSG_CHECKING([for _rtc intrinsic])
  56. rtc_ok=yes
  57. AC_TRY_LINK([#ifdef HAVE_INTRINSICS_H
  58. #include <intrinsics.h>
  59. #endif], [_rtc()], [AC_DEFINE(HAVE__RTC,1,[Define if you have the UNICOS _rtc() intrinsic.])], [rtc_ok=no])
  60. AC_MSG_RESULT($rtc_ok)
  61. dnl ---------------------------------------------------------------------
  62. */
  63. /***************************************************************************/
  64. #if TIME_WITH_SYS_TIME
  65. # include <sys/time.h>
  66. # include <time.h>
  67. #else
  68. # if HAVE_SYS_TIME_H
  69. # include <sys/time.h>
  70. # else
  71. # include <time.h>
  72. # endif
  73. #endif
  74. #define INLINE_ELAPSED(INL) static INL double elapsed(ticks t1, ticks t0) \
  75. { \
  76. return (double)t1 - (double)t0; \
  77. }
  78. /*----------------------------------------------------------------*/
  79. /* Solaris */
  80. #if defined(HAVE_GETHRTIME) && defined(HAVE_HRTIME_T) && !defined(HAVE_TICK_COUNTER)
  81. typedef hrtime_t ticks;
  82. #define getticks gethrtime
  83. INLINE_ELAPSED(inline)
  84. #define HAVE_TICK_COUNTER
  85. #endif
  86. /*----------------------------------------------------------------*/
  87. /* AIX v. 4+ routines to read the real-time clock or time-base register */
  88. #if defined(HAVE_READ_REAL_TIME) && defined(HAVE_TIME_BASE_TO_TIME) && !defined(HAVE_TICK_COUNTER)
  89. typedef timebasestruct_t ticks;
  90. static __inline ticks getticks(void)
  91. {
  92. ticks t;
  93. read_real_time(&t, TIMEBASE_SZ);
  94. return t;
  95. }
  96. static __inline double elapsed(ticks t1, ticks t0) /* time in nanoseconds */
  97. {
  98. time_base_to_time(&t1, TIMEBASE_SZ);
  99. time_base_to_time(&t0, TIMEBASE_SZ);
  100. return (((double)t1.tb_high - (double)t0.tb_high) * 1.0e9 +
  101. ((double)t1.tb_low - (double)t0.tb_low));
  102. }
  103. #define HAVE_TICK_COUNTER
  104. #endif
  105. /*----------------------------------------------------------------*/
  106. /*
  107. * PowerPC ``cycle'' counter using the time base register.
  108. */
  109. #if ((((defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))) || (defined(__MWERKS__) && defined(macintosh)))) || (defined(__IBM_GCC_ASM) && (defined(__powerpc__) || defined(__ppc__)))) && !defined(HAVE_TICK_COUNTER)
  110. typedef unsigned long long ticks;
  111. static __inline__ ticks getticks(void)
  112. {
  113. unsigned int tbl, tbu0, tbu1;
  114. do {
  115. __asm__ __volatile__ ("mftbu %0" : "=r"(tbu0));
  116. __asm__ __volatile__ ("mftb %0" : "=r"(tbl));
  117. __asm__ __volatile__ ("mftbu %0" : "=r"(tbu1));
  118. } while (tbu0 != tbu1);
  119. return (((unsigned long long)tbu0) << 32) | tbl;
  120. }
  121. INLINE_ELAPSED(__inline__)
  122. #define HAVE_TICK_COUNTER
  123. #endif
  124. /* MacOS/Mach (Darwin) time-base register interface (unlike UpTime,
  125. from Carbon, requires no additional libraries to be linked). */
  126. #if defined(HAVE_MACH_ABSOLUTE_TIME) && defined(HAVE_MACH_MACH_TIME_H) && !defined(HAVE_TICK_COUNTER)
  127. #include <mach/mach_time.h>
  128. typedef uint64_t ticks;
  129. #define getticks mach_absolute_time
  130. INLINE_ELAPSED(__inline__)
  131. #define HAVE_TICK_COUNTER
  132. #endif
  133. /*----------------------------------------------------------------*/
  134. /*
  135. * Pentium cycle counter
  136. */
  137. #if (defined(__GNUC__) || defined(__ICC)) && defined(__i386__) && !defined(HAVE_TICK_COUNTER)
  138. typedef unsigned long long ticks;
  139. static __inline__ ticks getticks(void)
  140. {
  141. ticks ret;
  142. __asm__ __volatile__("rdtsc": "=A" (ret));
  143. /* no input, nothing else clobbered */
  144. return ret;
  145. }
  146. INLINE_ELAPSED(__inline__)
  147. #define HAVE_TICK_COUNTER
  148. #define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */
  149. #endif
  150. /* Visual C++ -- thanks to Morten Nissov for his help with this */
  151. #if _MSC_VER >= 1200 && _M_IX86 >= 500 && !defined(HAVE_TICK_COUNTER)
  152. #include <windows.h>
  153. typedef LARGE_INTEGER ticks;
  154. #define RDTSC __asm __emit 0fh __asm __emit 031h /* hack for VC++ 5.0 */
  155. static __inline ticks getticks(void)
  156. {
  157. ticks retval;
  158. __asm {
  159. RDTSC
  160. mov retval.HighPart, edx
  161. mov retval.LowPart, eax
  162. }
  163. return retval;
  164. }
  165. static __inline double elapsed(ticks t1, ticks t0)
  166. {
  167. return (double)t1.QuadPart - (double)t0.QuadPart;
  168. }
  169. #define HAVE_TICK_COUNTER
  170. #define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */
  171. #endif
  172. /*----------------------------------------------------------------*/
  173. /*
  174. * X86-64 cycle counter
  175. */
  176. #if (defined(__GNUC__) || defined(__ICC) || defined(__SUNPRO_C)) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER)
  177. typedef unsigned long long ticks;
  178. static __inline__ ticks getticks(void)
  179. {
  180. unsigned a, d;
  181. asm volatile("rdtsc" : "=a" (a), "=d" (d));
  182. return ((ticks)a) | (((ticks)d) << 32);
  183. }
  184. INLINE_ELAPSED(__inline__)
  185. #define HAVE_TICK_COUNTER
  186. #endif
  187. /* PGI compiler, courtesy Cristiano Calonaci, Andrea Tarsi, & Roberto Gori.
  188. NOTE: this code will fail to link unless you use the -Masmkeyword compiler
  189. option (grrr). */
  190. #if defined(__PGI) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER)
  191. typedef unsigned long long ticks;
  192. static ticks getticks(void)
  193. {
  194. asm(" rdtsc; shl $0x20,%rdx; mov %eax,%eax; or %rdx,%rax; ");
  195. }
  196. INLINE_ELAPSED(__inline__)
  197. #define HAVE_TICK_COUNTER
  198. #endif
  199. /* Visual C++, courtesy of Dirk Michaelis */
  200. #if _MSC_VER >= 1400 && (defined(_M_AMD64) || defined(_M_X64)) && !defined(HAVE_TICK_COUNTER)
  201. #include <intrin.h>
  202. #pragma intrinsic(__rdtsc)
  203. typedef unsigned __int64 ticks;
  204. #define getticks __rdtsc
  205. INLINE_ELAPSED(__inline)
  206. #define HAVE_TICK_COUNTER
  207. #endif
  208. /*----------------------------------------------------------------*/
  209. /*
  210. * IA64 cycle counter
  211. */
  212. /* intel's icc/ecc compiler */
  213. #if (defined(__EDG_VERSION) || defined(__ECC)) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
  214. typedef unsigned long ticks;
  215. #include <ia64intrin.h>
  216. static __inline__ ticks getticks(void)
  217. {
  218. return __getReg(_IA64_REG_AR_ITC);
  219. }
  220. INLINE_ELAPSED(__inline__)
  221. #define HAVE_TICK_COUNTER
  222. #endif
  223. /* gcc */
  224. #if defined(__GNUC__) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
  225. typedef unsigned long ticks;
  226. static __inline__ ticks getticks(void)
  227. {
  228. ticks ret;
  229. __asm__ __volatile__ ("mov %0=ar.itc" : "=r"(ret));
  230. return ret;
  231. }
  232. INLINE_ELAPSED(__inline__)
  233. #define HAVE_TICK_COUNTER
  234. #endif
  235. /* HP/UX IA64 compiler, courtesy Teresa L. Johnson: */
  236. #if defined(__hpux) && defined(__ia64) && !defined(HAVE_TICK_COUNTER)
  237. #include <machine/sys/inline.h>
  238. typedef unsigned long ticks;
  239. static inline ticks getticks(void)
  240. {
  241. ticks ret;
  242. ret = _Asm_mov_from_ar (_AREG_ITC);
  243. return ret;
  244. }
  245. INLINE_ELAPSED(inline)
  246. #define HAVE_TICK_COUNTER
  247. #endif
  248. /* Microsoft Visual C++ */
  249. #if defined(_MSC_VER) && defined(_M_IA64) && !defined(HAVE_TICK_COUNTER)
  250. typedef unsigned __int64 ticks;
  251. # ifdef __cplusplus
  252. extern "C"
  253. # endif
  254. ticks __getReg(int whichReg);
  255. #pragma intrinsic(__getReg)
  256. static __inline ticks getticks(void)
  257. {
  258. volatile ticks temp;
  259. temp = __getReg(3116);
  260. return temp;
  261. }
  262. INLINE_ELAPSED(inline)
  263. #define HAVE_TICK_COUNTER
  264. #endif
  265. /*----------------------------------------------------------------*/
  266. /*
  267. * PA-RISC cycle counter
  268. */
  269. #if defined(__hppa__) || defined(__hppa) && !defined(HAVE_TICK_COUNTER)
  270. typedef unsigned long ticks;
  271. # ifdef __GNUC__
  272. static __inline__ ticks getticks(void)
  273. {
  274. ticks ret;
  275. __asm__ __volatile__("mfctl 16, %0": "=r" (ret));
  276. /* no input, nothing else clobbered */
  277. return ret;
  278. }
  279. # else
  280. # include <machine/inline.h>
  281. static inline unsigned long getticks(void)
  282. {
  283. register ticks ret;
  284. _MFCTL(16, ret);
  285. return ret;
  286. }
  287. # endif
  288. INLINE_ELAPSED(inline)
  289. #define HAVE_TICK_COUNTER
  290. #endif
  291. /*----------------------------------------------------------------*/
  292. /* S390, courtesy of James Treacy */
  293. #if defined(__GNUC__) && defined(__s390__) && !defined(HAVE_TICK_COUNTER)
  294. typedef unsigned long long ticks;
  295. static __inline__ ticks getticks(void)
  296. {
  297. ticks cycles;
  298. __asm__("stck 0(%0)" : : "a" (&(cycles)) : "memory", "cc");
  299. return cycles;
  300. }
  301. INLINE_ELAPSED(__inline__)
  302. #define HAVE_TICK_COUNTER
  303. #endif
  304. /*----------------------------------------------------------------*/
  305. #if defined(__GNUC__) && defined(__alpha__) && !defined(HAVE_TICK_COUNTER)
  306. /*
  307. * The 32-bit cycle counter on alpha overflows pretty quickly,
  308. * unfortunately. A 1GHz machine overflows in 4 seconds.
  309. */
  310. typedef unsigned int ticks;
  311. static __inline__ ticks getticks(void)
  312. {
  313. unsigned long cc;
  314. __asm__ __volatile__ ("rpcc %0" : "=r"(cc));
  315. return (cc & 0xFFFFFFFF);
  316. }
  317. INLINE_ELAPSED(__inline__)
  318. #define HAVE_TICK_COUNTER
  319. #endif
  320. /*----------------------------------------------------------------*/
  321. #if defined(__GNUC__) && defined(__sparc_v9__) && !defined(HAVE_TICK_COUNTER)
  322. typedef unsigned long ticks;
  323. static __inline__ ticks getticks(void)
  324. {
  325. ticks ret;
  326. __asm__ __volatile__("rd %%tick, %0" : "=r" (ret));
  327. return ret;
  328. }
  329. INLINE_ELAPSED(__inline__)
  330. #define HAVE_TICK_COUNTER
  331. #endif
  332. /*----------------------------------------------------------------*/
  333. #if (defined(__DECC) || defined(__DECCXX)) && defined(__alpha) && defined(HAVE_C_ASM_H) && !defined(HAVE_TICK_COUNTER)
  334. # include <c_asm.h>
  335. typedef unsigned int ticks;
  336. static __inline ticks getticks(void)
  337. {
  338. unsigned long cc;
  339. cc = asm("rpcc %v0");
  340. return (cc & 0xFFFFFFFF);
  341. }
  342. INLINE_ELAPSED(__inline)
  343. #define HAVE_TICK_COUNTER
  344. #endif
  345. /*----------------------------------------------------------------*/
  346. /* SGI/Irix */
  347. #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_SGI_CYCLE) && !defined(HAVE_TICK_COUNTER)
  348. typedef struct timespec ticks;
  349. static inline ticks getticks(void)
  350. {
  351. struct timespec t;
  352. clock_gettime(CLOCK_SGI_CYCLE, &t);
  353. return t;
  354. }
  355. static inline double elapsed(ticks t1, ticks t0)
  356. {
  357. return ((double)t1.tv_sec - (double)t0.tv_sec) * 1.0E9 +
  358. ((double)t1.tv_nsec - (double)t0.tv_nsec);
  359. }
  360. #define HAVE_TICK_COUNTER
  361. #endif
  362. /*----------------------------------------------------------------*/
  363. /* Cray UNICOS _rtc() intrinsic function */
  364. #if defined(HAVE__RTC) && !defined(HAVE_TICK_COUNTER)
  365. #ifdef HAVE_INTRINSICS_H
  366. # include <intrinsics.h>
  367. #endif
  368. typedef long long ticks;
  369. #define getticks _rtc
  370. INLINE_ELAPSED(inline)
  371. #define HAVE_TICK_COUNTER
  372. #endif
  373. /*----------------------------------------------------------------*/
  374. /* MIPS ZBus */
  375. #if HAVE_MIPS_ZBUS_TIMER
  376. #if defined(__mips__) && !defined(HAVE_TICK_COUNTER)
  377. #include <sys/mman.h>
  378. #include <unistd.h>
  379. #include <fcntl.h>
  380. typedef uint64_t ticks;
  381. static inline ticks getticks(void)
  382. {
  383. static uint64_t* addr = 0;
  384. if (addr == 0)
  385. {
  386. uint32_t rq_addr = 0x10030000;
  387. int fd;
  388. int pgsize;
  389. pgsize = getpagesize();
  390. fd = open ("/dev/mem", O_RDONLY | O_SYNC, 0);
  391. if (fd < 0) {
  392. perror("open");
  393. return NULL;
  394. }
  395. addr = mmap(0, pgsize, PROT_READ, MAP_SHARED, fd, rq_addr);
  396. close(fd);
  397. if (addr == (uint64_t *)-1) {
  398. perror("mmap");
  399. return NULL;
  400. }
  401. }
  402. return *addr;
  403. }
  404. INLINE_ELAPSED(inline)
  405. #define HAVE_TICK_COUNTER
  406. #endif
  407. #endif /* HAVE_MIPS_ZBUS_TIMER */