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