TPIE

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