chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / powerpc / powerpc64 / hp-timing.h
1 /* High precision, low overhead timing functions.  powerpc64 version.
2    Copyright (C) 2005, 2008 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 #ifndef _HP_TIMING_H
22 #define _HP_TIMING_H    1
23
24 #include <string.h>
25 #include <sys/param.h>
26 #include <stdio-common/_itoa.h>
27 #include <atomic.h>
28
29 /* The macros defined here use the powerpc 64-bit time base register.
30    The time base is nominally clocked at 1/8th the CPU clock, but this
31    can vary.
32
33    The list of macros we need includes the following:
34
35    - HP_TIMING_AVAIL: test for availability.
36
37    - HP_TIMING_INLINE: this macro is non-zero if the functionality is not
38      implemented using function calls but instead uses some inlined code
39      which might simply consist of a few assembler instructions.  We have to
40      know this since we might want to use the macros here in places where we
41      cannot make function calls.
42
43    - hp_timing_t: This is the type for variables used to store the time
44      values.
45
46    - HP_TIMING_ZERO: clear `hp_timing_t' object.
47
48    - HP_TIMING_NOW: place timestamp for current time in variable given as
49      parameter.
50
51    - HP_TIMING_DIFF_INIT: do whatever is necessary to be able to use the
52      HP_TIMING_DIFF macro.
53
54    - HP_TIMING_DIFF: compute difference between two times and store it
55      in a third.  Source and destination might overlap.
56
57    - HP_TIMING_ACCUM: add time difference to another variable.  This might
58      be a bit more complicated to implement for some platforms as the
59      operation should be thread-safe and 64bit arithmetic on 32bit platforms
60      is not.
61
62    - HP_TIMING_ACCUM_NT: this is the variant for situations where we know
63      there are no threads involved.
64
65    - HP_TIMING_PRINT: write decimal representation of the timing value into
66      the given string.  This operation need not be inline even though
67      HP_TIMING_INLINE is specified.
68
69 */
70
71 /* We always assume having the timestamp register.  */
72 #define HP_TIMING_AVAIL         (1)
73
74 /* We indeed have inlined functions.  */
75 #define HP_TIMING_INLINE        (1)
76
77 /* We use 64bit values for the times.  */
78 typedef unsigned long long int hp_timing_t;
79
80 /* Set timestamp value to zero.  */
81 #define HP_TIMING_ZERO(Var)     (Var) = (0)
82
83 /* That's quite simple.  Use the `mftb' instruction.  Note that the value
84    might not be 100% accurate since there might be some more instructions
85    running in this moment.  This could be changed by using a barrier like
86    'lwsync' right before the `mftb' instruciton.  But we are not interested
87    in accurate clock cycles here so we don't do this.  */
88 #ifdef _ARCH_PWR4
89 #define HP_TIMING_NOW(Var)      __asm__ __volatile__ ("mfspr %0,268" : "=r" (Var))
90 #else
91 #define HP_TIMING_NOW(Var)      __asm__ __volatile__ ("mftb %0" : "=r" (Var))
92 #endif
93
94 /* Use two 'mftb' instructions in a row to find out how long it takes.
95    On current POWER4, POWER5, and 970 processors mftb take ~10 cycles.  */
96 #define HP_TIMING_DIFF_INIT() \
97   do {                                                                        \
98     if (GLRO(dl_hp_timing_overhead) == 0)                                     \
99       {                                                                       \
100         int __cnt = 5;                                                        \
101         GLRO(dl_hp_timing_overhead) = ~0ull;                                  \
102         do                                                                    \
103           {                                                                   \
104             hp_timing_t __t1, __t2;                                           \
105             HP_TIMING_NOW (__t1);                                             \
106             HP_TIMING_NOW (__t2);                                             \
107             if (__t2 - __t1 < GLRO(dl_hp_timing_overhead))                    \
108               GLRO(dl_hp_timing_overhead) = __t2 - __t1;                      \
109           }                                                                   \
110         while (--__cnt > 0);                                                  \
111       }                                                                       \
112   } while (0)
113
114 /* It's simple arithmetic in 64-bit.  */
115 #define HP_TIMING_DIFF(Diff, Start, End)        (Diff) = ((End) - (Start))
116
117 /* We need to insure that this add is atomic in threaded environments.  We use
118    __arch_atomic_exchange_and_add_64 from atomic.h to get thread safety.  */
119 #define HP_TIMING_ACCUM(Sum, Diff) \
120   do {                                                                        \
121     hp_timing_t __diff = (Diff) - GLRO(dl_hp_timing_overhead);                \
122     __arch_atomic_exchange_and_add_64 (&(Sum), __diff);                       \
123   } while (0)
124
125 /* No threads, no extra work.  */
126 #define HP_TIMING_ACCUM_NT(Sum, Diff)   (Sum) += (Diff)
127
128 /* Print the time value.  */
129 #define HP_TIMING_PRINT(Buf, Len, Val) \
130   do {                                                                        \
131     char __buf[20];                                                           \
132     char *__cp = _itoa (Val, __buf + sizeof (__buf), 10, 0);                  \
133     size_t __len = (Len);                                                     \
134     char *__dest = (Buf);                                                     \
135     while (__len-- > 0 && __cp < __buf + sizeof (__buf))                      \
136       *__dest++ = *__cp++;                                                    \
137     memcpy (__dest, " ticks", MIN (__len, sizeof (" ticks")));  \
138   } while (0)
139
140 #endif  /* hp-timing.h */