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