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