1 | /*
|
---|
2 | * Copyright (c) 2003, 2006 Matteo Frigo
|
---|
3 | * Copyright (c) 2003, 2006 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 | /* $Id: cycle.h,v 1.52 2006-02-08 02:36:47 athena Exp $ */
|
---|
27 |
|
---|
28 | /* machine-dependent cycle counters code. Needs to be inlined. */
|
---|
29 |
|
---|
30 | /***************************************************************************/
|
---|
31 | /* To use the cycle counters in your code, simply #include "cycle.h" (this
|
---|
32 | file), and then use the functions/macros:
|
---|
33 |
|
---|
34 | CycleCounterTicks getticks(void);
|
---|
35 |
|
---|
36 | CycleCounterTicks is an opaque typedef defined below, representing the current time.
|
---|
37 | You extract the elapsed time between two calls to gettick() via:
|
---|
38 |
|
---|
39 | double elapsed(CycleCounterTicks t1, CycleCounterTicks t0);
|
---|
40 |
|
---|
41 | which returns a double-precision variable in arbitrary units. You
|
---|
42 | are not expected to convert this into human units like seconds; it
|
---|
43 | is intended only for *comparisons* of time intervals.
|
---|
44 |
|
---|
45 | (In order to use some of the OS-dependent timer routines like
|
---|
46 | Solaris' gethrtime, you need to paste the autoconf snippet below
|
---|
47 | into your configure.ac file and #include "config.h" before cycle.h,
|
---|
48 | or define the relevant macros manually if you are not using autoconf.)
|
---|
49 | */
|
---|
50 |
|
---|
51 | /***************************************************************************/
|
---|
52 | /* This file uses macros like HAVE_GETHRTIME that are assumed to be
|
---|
53 | defined according to whether the corresponding function/type/header
|
---|
54 | is available on your system. The necessary macros are most
|
---|
55 | conveniently defined if you are using GNU autoconf, via the tests:
|
---|
56 |
|
---|
57 | dnl ---------------------------------------------------------------------
|
---|
58 |
|
---|
59 | AC_C_INLINE
|
---|
60 | AC_HEADER_TIME
|
---|
61 | AC_CHECK_HEADERS([sys/time.h c_asm.h intrinsics.h mach/mach_time.h])
|
---|
62 |
|
---|
63 | 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
|
---|
64 | #include <sys/time.h>
|
---|
65 | #endif])
|
---|
66 |
|
---|
67 | AC_CHECK_FUNCS([gethrtime read_real_time time_base_to_time clock_gettime mach_absolute_time])
|
---|
68 |
|
---|
69 | dnl Cray UNICOS _rtc() (real-time clock) intrinsic
|
---|
70 | AC_MSG_CHECKING([for _rtc intrinsic])
|
---|
71 | rtc_ok=yes
|
---|
72 | AC_TRY_LINK([#ifdef HAVE_INTRINSICS_H
|
---|
73 | #include <intrinsics.h>
|
---|
74 | #endif], [_rtc()], [AC_DEFINE(HAVE__RTC,1,[Define if you have the UNICOS _rtc() intrinsic.])], [rtc_ok=no])
|
---|
75 | AC_MSG_RESULT($rtc_ok)
|
---|
76 |
|
---|
77 | dnl ---------------------------------------------------------------------
|
---|
78 | */
|
---|
79 |
|
---|
80 | /***************************************************************************/
|
---|
81 |
|
---|
82 | #ifndef QBENCHLIB_CYCLE_H
|
---|
83 | #define QBENCHLIB_CYCLE_H
|
---|
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(CycleCounterTicks t1, CycleCounterTicks t0) \
|
---|
97 | { \
|
---|
98 | return (double)(t1 - t0); \
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*----------------------------------------------------------------*/
|
---|
102 | /* Solaris */
|
---|
103 | #if defined(HAVE_GETHRTIME) && defined(HAVE_HRTIME_T) && !defined(HAVE_TICK_COUNTER)
|
---|
104 | typedef hrtime_t CycleCounterTicks;
|
---|
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 CycleCounterTicks;
|
---|
117 |
|
---|
118 | static inline CycleCounterTicks getticks(void)
|
---|
119 | {
|
---|
120 | CycleCounterTicks t;
|
---|
121 | read_real_time(&t, TIMEBASE_SZ);
|
---|
122 | return t;
|
---|
123 | }
|
---|
124 |
|
---|
125 | static inline double elapsed(CycleCounterTicks t1, CycleCounterTicks t0) /* time in nanoseconds */
|
---|
126 | {
|
---|
127 | time_base_to_time(&t1, TIMEBASE_SZ);
|
---|
128 | time_base_to_time(&t0, TIMEBASE_SZ);
|
---|
129 | return ((t1.tb_high - t0.tb_high) * 1e9 + (t1.tb_low - 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(HAVE_TICK_COUNTER)
|
---|
140 | typedef unsigned long long CycleCounterTicks;
|
---|
141 |
|
---|
142 | static __inline__ CycleCounterTicks 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 |
|
---|
|
---|