1 |
207 |
jeremybenn |
/* High precision, low overhead timing functions. i686 version.
|
2 |
|
|
Copyright (C) 1998 Free Software Foundation, Inc.
|
3 |
|
|
This file is part of the GNU C Library.
|
4 |
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
|
5 |
|
|
|
6 |
|
|
The GNU C Library is free software; you can redistribute it and/or
|
7 |
|
|
modify it under the terms of the GNU Lesser General Public
|
8 |
|
|
License as published by the Free Software Foundation; either
|
9 |
|
|
version 2.1 of the License, or (at your option) any later version.
|
10 |
|
|
|
11 |
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
Lesser General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU Lesser General Public
|
17 |
|
|
License along with the GNU C Library; if not, write to the Free
|
18 |
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
19 |
|
|
02111-1307 USA. */
|
20 |
|
|
|
21 |
|
|
/* Modified for newlib by Jeff Johnston - June 27, 2002 */
|
22 |
|
|
|
23 |
|
|
#ifndef _HP_TIMING_H
|
24 |
|
|
#define _HP_TIMING_H 1
|
25 |
|
|
|
26 |
|
|
#include <string.h>
|
27 |
|
|
#include <stdio.h>
|
28 |
|
|
#include <sys/param.h>
|
29 |
|
|
|
30 |
|
|
#ifdef __i686__
|
31 |
|
|
|
32 |
|
|
/* The macros defined here use the timestamp counter in i586 and up versions
|
33 |
|
|
of the x86 processors. They provide a very accurate way to measure the
|
34 |
|
|
time with very little overhead. The time values themself have no real
|
35 |
|
|
meaning, only differences are interesting.
|
36 |
|
|
|
37 |
|
|
This version is for the i686 processors. The difference to the i586
|
38 |
|
|
version is that the timerstamp register is unconditionally used. This is
|
39 |
|
|
not the case for the i586 version where we have to perform runtime test
|
40 |
|
|
whether the processor really has this capability. We have to make this
|
41 |
|
|
distinction since the sysdeps/i386/i586 code is supposed to work on all
|
42 |
|
|
platforms while the i686 already contains i686-specific code.
|
43 |
|
|
|
44 |
|
|
The list of macros we need includes the following:
|
45 |
|
|
|
46 |
|
|
- HP_TIMING_AVAIL: test for availability.
|
47 |
|
|
|
48 |
|
|
- HP_TIMING_INLINE: this macro is non-zero if the functionality is not
|
49 |
|
|
implemented using function calls but instead uses some inlined code
|
50 |
|
|
which might simply consist of a few assembler instructions. We have to
|
51 |
|
|
know this since we might want to use the macros here in places where we
|
52 |
|
|
cannot make function calls.
|
53 |
|
|
|
54 |
|
|
- hp_timing_t: This is the type for variables used to store the time
|
55 |
|
|
values.
|
56 |
|
|
|
57 |
|
|
- HP_TIMING_ZERO: clear `hp_timing_t' object.
|
58 |
|
|
|
59 |
|
|
- HP_TIMING_NOW: place timestamp for current time in variable given as
|
60 |
|
|
parameter.
|
61 |
|
|
|
62 |
|
|
- HP_TIMING_DIFF_INIT: do whatever is necessary to be able to use the
|
63 |
|
|
HP_TIMING_DIFF macro.
|
64 |
|
|
|
65 |
|
|
- HP_TIMING_DIFF: compute difference between two times and store it
|
66 |
|
|
in a third. Source and destination might overlap.
|
67 |
|
|
|
68 |
|
|
- HP_TIMING_ACCUM: add time difference to another variable. This might
|
69 |
|
|
be a bit more complicated to implement for some platforms as the
|
70 |
|
|
operation should be thread-safe and 64bit arithmetic on 32bit platforms
|
71 |
|
|
is not.
|
72 |
|
|
|
73 |
|
|
- HP_TIMING_ACCUM_NT: this is the variant for situations where we know
|
74 |
|
|
there are no threads involved.
|
75 |
|
|
|
76 |
|
|
- HP_TIMING_PRINT: write decimal representation of the timing value into
|
77 |
|
|
the given string. This operation need not be inline even though
|
78 |
|
|
HP_TIMING_INLINE is specified.
|
79 |
|
|
|
80 |
|
|
*/
|
81 |
|
|
|
82 |
|
|
/* We always assume having the timestamp register. */
|
83 |
|
|
#define HP_TIMING_AVAIL (1)
|
84 |
|
|
|
85 |
|
|
/* We indeed have inlined functions. */
|
86 |
|
|
#define HP_TIMING_INLINE (1)
|
87 |
|
|
|
88 |
|
|
/* We use 64bit values for the times. */
|
89 |
|
|
typedef unsigned long long int hp_timing_t;
|
90 |
|
|
|
91 |
|
|
/* Internal variable used to store the overhead of the measurement
|
92 |
|
|
opcodes. */
|
93 |
|
|
extern hp_timing_t __libc_hp_timing_overhead;
|
94 |
|
|
|
95 |
|
|
/* Set timestamp value to zero. */
|
96 |
|
|
#define HP_TIMING_ZERO(Var) (Var) = (0)
|
97 |
|
|
|
98 |
|
|
/* That's quite simple. Use the `rdtsc' instruction. Note that the value
|
99 |
|
|
might not be 100% accurate since there might be some more instructions
|
100 |
|
|
running in this moment. This could be changed by using a barrier like
|
101 |
|
|
'cpuid' right before the `rdtsc' instruciton. But we are not interested
|
102 |
|
|
in accurate clock cycles here so we don't do this. */
|
103 |
|
|
#define HP_TIMING_NOW(Var) __asm__ __volatile__ ("rdtsc" : "=A" (Var))
|
104 |
|
|
|
105 |
|
|
/* Use two 'rdtsc' instructions in a row to find out how long it takes. */
|
106 |
|
|
#define HP_TIMING_DIFF_INIT() \
|
107 |
|
|
do { \
|
108 |
|
|
int __cnt = 5; \
|
109 |
|
|
__libc_hp_timing_overhead = ~0ull; \
|
110 |
|
|
do \
|
111 |
|
|
{ \
|
112 |
|
|
hp_timing_t __t1, __t2; \
|
113 |
|
|
HP_TIMING_NOW (__t1); \
|
114 |
|
|
HP_TIMING_NOW (__t2); \
|
115 |
|
|
if (__t2 - __t1 < __libc_hp_timing_overhead) \
|
116 |
|
|
__libc_hp_timing_overhead = __t2 - __t1; \
|
117 |
|
|
} \
|
118 |
|
|
while (--__cnt > 0); \
|
119 |
|
|
} while (0)
|
120 |
|
|
|
121 |
|
|
/* It's simple arithmetic for us. */
|
122 |
|
|
#define HP_TIMING_DIFF(Diff, Start, End) (Diff) = ((End) - (Start))
|
123 |
|
|
|
124 |
|
|
/* We have to jump through hoops to get this correctly implemented. */
|
125 |
|
|
#define HP_TIMING_ACCUM(Sum, Diff) \
|
126 |
|
|
do { \
|
127 |
|
|
char __not_done; \
|
128 |
|
|
hp_timing_t __oldval = (Sum); \
|
129 |
|
|
hp_timing_t __diff = (Diff) - __libc_hp_timing_overhead; \
|
130 |
|
|
do \
|
131 |
|
|
{ \
|
132 |
|
|
hp_timing_t __newval = __oldval + __diff; \
|
133 |
|
|
int __temp0, __temp1; \
|
134 |
|
|
__asm__ __volatile__ ("xchgl %4, %%ebx\n\t" \
|
135 |
|
|
"lock; cmpxchg8b %1\n\t" \
|
136 |
|
|
"sete %0\n\t" \
|
137 |
|
|
"movl %4, %%ebx" \
|
138 |
|
|
: "=q" (__not_done), "=m" (Sum), \
|
139 |
|
|
"=A" (__oldval), "=c" (__temp0), \
|
140 |
|
|
"=SD" (__temp1) \
|
141 |
|
|
: "1" (Sum), "2" (__oldval), \
|
142 |
|
|
"3" (__newval >> 32), \
|
143 |
|
|
"4" (__newval & 0xffffffff) \
|
144 |
|
|
: "memory"); \
|
145 |
|
|
} \
|
146 |
|
|
while (__not_done); \
|
147 |
|
|
} while (0)
|
148 |
|
|
|
149 |
|
|
/* No threads, no extra work. */
|
150 |
|
|
#define HP_TIMING_ACCUM_NT(Sum, Diff) (Sum) += (Diff)
|
151 |
|
|
|
152 |
|
|
/* Print the time value. */
|
153 |
|
|
#define HP_TIMING_PRINT(Buf, Len, Val) \
|
154 |
|
|
do { \
|
155 |
|
|
char __buf[20]; \
|
156 |
|
|
char *__cp = __buf + sizeof (__buf); \
|
157 |
|
|
int __len = (Len); \
|
158 |
|
|
char *__dest = (Buf); \
|
159 |
|
|
do { \
|
160 |
|
|
*--__cp = Val % 10; \
|
161 |
|
|
Val /= 10; \
|
162 |
|
|
} while (Val > 0); \
|
163 |
|
|
while (__len-- > 0 && __cp < __buf + sizeof (__buf)) \
|
164 |
|
|
*__dest++ = *__cp++; \
|
165 |
|
|
memcpy (__dest, " clock cycles", MIN (__len, sizeof (" clock cycles"))); \
|
166 |
|
|
} while (0)
|
167 |
|
|
|
168 |
|
|
#else /* !__i686__ */
|
169 |
|
|
|
170 |
|
|
/* Provide dummy definitions. */
|
171 |
|
|
#define HP_TIMING_AVAIL (0)
|
172 |
|
|
#define HP_TIMING_INLINE (0)
|
173 |
|
|
typedef int hp_timing_t;
|
174 |
|
|
#define HP_TIMING_ZERO(Var)
|
175 |
|
|
#define HP_TIMING_NOW(var)
|
176 |
|
|
#define HP_TIMING_DIFF_INIT()
|
177 |
|
|
#define HP_TIMING_DIFF(Diff, Start, End)
|
178 |
|
|
#define HP_TIMING_ACCUM(Sum, Diff)
|
179 |
|
|
#define HP_TIMING_ACCUM_NT(Sum, Diff)
|
180 |
|
|
#define HP_TIMING_PRINT(Buf, Len, Val)
|
181 |
|
|
|
182 |
|
|
/* Since this implementation is not available we tell the user about it. */
|
183 |
|
|
#define HP_TIMING_NONAVAIL 1
|
184 |
|
|
|
185 |
|
|
#endif
|
186 |
|
|
|
187 |
|
|
#endif /* hp-timing.h */
|