annotate libgcc/libgcov-profiler.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Routines required for instrumenting a program. */
kono
parents:
diff changeset
2 /* Compile this one with gcc. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
3 /* Copyright (C) 1989-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
10 version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
15 for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 #include "libgcov.h"
kono
parents:
diff changeset
27 #if !defined(inhibit_libc)
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 /* Detect whether target can support atomic update of profilers. */
kono
parents:
diff changeset
30 #if __SIZEOF_LONG_LONG__ == 4 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
kono
parents:
diff changeset
31 #define GCOV_SUPPORTS_ATOMIC 1
kono
parents:
diff changeset
32 #else
kono
parents:
diff changeset
33 #if __SIZEOF_LONG_LONG__ == 8 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
kono
parents:
diff changeset
34 #define GCOV_SUPPORTS_ATOMIC 1
kono
parents:
diff changeset
35 #else
kono
parents:
diff changeset
36 #define GCOV_SUPPORTS_ATOMIC 0
kono
parents:
diff changeset
37 #endif
kono
parents:
diff changeset
38 #endif
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 #ifdef L_gcov_interval_profiler
kono
parents:
diff changeset
41 /* If VALUE is in interval <START, START + STEPS - 1>, then increases the
kono
parents:
diff changeset
42 corresponding counter in COUNTERS. If the VALUE is above or below
kono
parents:
diff changeset
43 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
kono
parents:
diff changeset
44 instead. */
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 void
kono
parents:
diff changeset
47 __gcov_interval_profiler (gcov_type *counters, gcov_type value,
kono
parents:
diff changeset
48 int start, unsigned steps)
kono
parents:
diff changeset
49 {
kono
parents:
diff changeset
50 gcov_type delta = value - start;
kono
parents:
diff changeset
51 if (delta < 0)
kono
parents:
diff changeset
52 counters[steps + 1]++;
kono
parents:
diff changeset
53 else if (delta >= steps)
kono
parents:
diff changeset
54 counters[steps]++;
kono
parents:
diff changeset
55 else
kono
parents:
diff changeset
56 counters[delta]++;
kono
parents:
diff changeset
57 }
kono
parents:
diff changeset
58 #endif
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 #if defined(L_gcov_interval_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
kono
parents:
diff changeset
61 /* If VALUE is in interval <START, START + STEPS - 1>, then increases the
kono
parents:
diff changeset
62 corresponding counter in COUNTERS. If the VALUE is above or below
kono
parents:
diff changeset
63 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
kono
parents:
diff changeset
64 instead. Function is thread-safe. */
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 void
kono
parents:
diff changeset
67 __gcov_interval_profiler_atomic (gcov_type *counters, gcov_type value,
kono
parents:
diff changeset
68 int start, unsigned steps)
kono
parents:
diff changeset
69 {
kono
parents:
diff changeset
70 gcov_type delta = value - start;
kono
parents:
diff changeset
71 if (delta < 0)
kono
parents:
diff changeset
72 __atomic_fetch_add (&counters[steps + 1], 1, __ATOMIC_RELAXED);
kono
parents:
diff changeset
73 else if (delta >= steps)
kono
parents:
diff changeset
74 __atomic_fetch_add (&counters[steps], 1, __ATOMIC_RELAXED);
kono
parents:
diff changeset
75 else
kono
parents:
diff changeset
76 __atomic_fetch_add (&counters[delta], 1, __ATOMIC_RELAXED);
kono
parents:
diff changeset
77 }
kono
parents:
diff changeset
78 #endif
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 #ifdef L_gcov_pow2_profiler
kono
parents:
diff changeset
81 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
kono
parents:
diff changeset
82 COUNTERS[0] is incremented. */
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 void
kono
parents:
diff changeset
85 __gcov_pow2_profiler (gcov_type *counters, gcov_type value)
kono
parents:
diff changeset
86 {
kono
parents:
diff changeset
87 if (value == 0 || (value & (value - 1)))
kono
parents:
diff changeset
88 counters[0]++;
kono
parents:
diff changeset
89 else
kono
parents:
diff changeset
90 counters[1]++;
kono
parents:
diff changeset
91 }
kono
parents:
diff changeset
92 #endif
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 #if defined(L_gcov_pow2_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
kono
parents:
diff changeset
95 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
kono
parents:
diff changeset
96 COUNTERS[0] is incremented. Function is thread-safe. */
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 void
kono
parents:
diff changeset
99 __gcov_pow2_profiler_atomic (gcov_type *counters, gcov_type value)
kono
parents:
diff changeset
100 {
kono
parents:
diff changeset
101 if (value == 0 || (value & (value - 1)))
kono
parents:
diff changeset
102 __atomic_fetch_add (&counters[0], 1, __ATOMIC_RELAXED);
kono
parents:
diff changeset
103 else
kono
parents:
diff changeset
104 __atomic_fetch_add (&counters[1], 1, __ATOMIC_RELAXED);
kono
parents:
diff changeset
105 }
kono
parents:
diff changeset
106 #endif
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
109 /* Tries to determine N most commons value among its inputs. */
111
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 static inline void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
112 __gcov_topn_values_profiler_body (gcov_type *counters, gcov_type value,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
113 int use_atomic)
111
kono
parents:
diff changeset
114 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
115 if (use_atomic)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
116 __atomic_fetch_add (&counters[0], 1, __ATOMIC_RELAXED);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
117 else
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
118 counters[0]++;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
119
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
120 ++counters;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
121
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
122 /* First try to find an existing value. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
123 int empty_counter = -1;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
124
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
125 for (unsigned i = 0; i < GCOV_TOPN_VALUES; i++)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
126 if (value == counters[2 * i])
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
127 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
128 if (use_atomic)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
129 __atomic_fetch_add (&counters[2 * i + 1], GCOV_TOPN_VALUES,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
130 __ATOMIC_RELAXED);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
131 else
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
132 counters[2 * i + 1] += GCOV_TOPN_VALUES;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
133 return;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
134 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
135 else if (counters[2 * i + 1] <= 0)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
136 empty_counter = i;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
137
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
138 /* Find an empty slot for a new value. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
139 if (empty_counter != -1)
111
kono
parents:
diff changeset
140 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
141 counters[2 * empty_counter] = value;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
142 counters[2 * empty_counter + 1] = GCOV_TOPN_VALUES;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
143 return;
111
kono
parents:
diff changeset
144 }
kono
parents:
diff changeset
145
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
146 /* We haven't found an empty slot, then decrement all
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
147 counter values by one. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
148 for (unsigned i = 0; i < GCOV_TOPN_VALUES; i++)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
149 if (use_atomic)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
150 __atomic_fetch_sub (&counters[2 * i + 1], 1, __ATOMIC_RELAXED);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
151 else
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
152 counters[2 * i + 1]--;
111
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
155 #ifdef L_gcov_topn_values_profiler
111
kono
parents:
diff changeset
156 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
157 __gcov_topn_values_profiler (gcov_type *counters, gcov_type value)
111
kono
parents:
diff changeset
158 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
159 __gcov_topn_values_profiler_body (counters, value, 0);
111
kono
parents:
diff changeset
160 }
kono
parents:
diff changeset
161 #endif
kono
parents:
diff changeset
162
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
163 #if defined(L_gcov_topn_values_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
111
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 /* Update one value profilers (COUNTERS) for a given VALUE.
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 CAVEAT: Following function is not thread-safe, only total number
kono
parents:
diff changeset
168 of executions (COUNTERS[2]) is update with an atomic instruction.
kono
parents:
diff changeset
169 Problem is that one cannot atomically update two counters
kono
parents:
diff changeset
170 (COUNTERS[0] and COUNTERS[1]), for more information please read
kono
parents:
diff changeset
171 following email thread:
kono
parents:
diff changeset
172 https://gcc.gnu.org/ml/gcc-patches/2016-08/msg00024.html. */
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
175 __gcov_topn_values_profiler_atomic (gcov_type *counters, gcov_type value)
111
kono
parents:
diff changeset
176 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
177 __gcov_topn_values_profiler_body (counters, value, 1);
111
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179 #endif
kono
parents:
diff changeset
180
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
181 #ifdef L_gcov_indirect_call_profiler_v4
111
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 /* These two variables are used to actually track caller and callee. Keep
kono
parents:
diff changeset
184 them in TLS memory so races are not common (they are written to often).
kono
parents:
diff changeset
185 The variables are set directly by GCC instrumented code, so declaration
kono
parents:
diff changeset
186 here must match one in tree-profile.c */
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
kono
parents:
diff changeset
189 __thread
kono
parents:
diff changeset
190 #endif
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
191 struct indirect_call_tuple __gcov_indirect_call;
111
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 /* By default, the C++ compiler will use function addresses in the
kono
parents:
diff changeset
194 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
kono
parents:
diff changeset
195 tells the compiler to use function descriptors instead. The value
kono
parents:
diff changeset
196 of this macro says how many words wide the descriptor is (normally 2).
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 It is assumed that the address of a function descriptor may be treated
kono
parents:
diff changeset
199 as a pointer to a function. */
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 /* Tries to determine the most common value among its inputs. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
202 static inline void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
203 __gcov_indirect_call_profiler_body (gcov_type value, void *cur_func,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
204 int use_atomic)
111
kono
parents:
diff changeset
205 {
kono
parents:
diff changeset
206 /* If the C++ virtual tables contain function descriptors then one
kono
parents:
diff changeset
207 function may have multiple descriptors and we need to dereference
kono
parents:
diff changeset
208 the descriptors to see if they point to the same function. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
209 if (cur_func == __gcov_indirect_call.callee
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
210 || (__LIBGCC_VTABLE_USES_DESCRIPTORS__
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
211 && *(void **) cur_func == *(void **) __gcov_indirect_call.callee))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
212 __gcov_topn_values_profiler_body (__gcov_indirect_call.counters, value,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
213 use_atomic);
111
kono
parents:
diff changeset
214
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
215 __gcov_indirect_call.callee = NULL;
111
kono
parents:
diff changeset
216 }
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
217
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
218 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
219 __gcov_indirect_call_profiler_v4 (gcov_type value, void *cur_func)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
220 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
221 __gcov_indirect_call_profiler_body (value, cur_func, 0);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
222 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
223
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
224 #if GCOV_SUPPORTS_ATOMIC
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
225 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
226 __gcov_indirect_call_profiler_v4_atomic (gcov_type value, void *cur_func)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
227 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
228 __gcov_indirect_call_profiler_body (value, cur_func, 1);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
229 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
230 #endif
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
231
111
kono
parents:
diff changeset
232 #endif
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 #ifdef L_gcov_time_profiler
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 /* Counter for first visit of each function. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
237 gcov_type __gcov_time_profiler_counter ATTRIBUTE_HIDDEN;
111
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 #endif
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 #ifdef L_gcov_average_profiler
kono
parents:
diff changeset
242 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
kono
parents:
diff changeset
243 to saturate up. */
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 void
kono
parents:
diff changeset
246 __gcov_average_profiler (gcov_type *counters, gcov_type value)
kono
parents:
diff changeset
247 {
kono
parents:
diff changeset
248 counters[0] += value;
kono
parents:
diff changeset
249 counters[1] ++;
kono
parents:
diff changeset
250 }
kono
parents:
diff changeset
251 #endif
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 #if defined(L_gcov_average_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
kono
parents:
diff changeset
254 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
kono
parents:
diff changeset
255 to saturate up. Function is thread-safe. */
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 void
kono
parents:
diff changeset
258 __gcov_average_profiler_atomic (gcov_type *counters, gcov_type value)
kono
parents:
diff changeset
259 {
kono
parents:
diff changeset
260 __atomic_fetch_add (&counters[0], value, __ATOMIC_RELAXED);
kono
parents:
diff changeset
261 __atomic_fetch_add (&counters[1], 1, __ATOMIC_RELAXED);
kono
parents:
diff changeset
262 }
kono
parents:
diff changeset
263 #endif
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 #ifdef L_gcov_ior_profiler
kono
parents:
diff changeset
266 /* Bitwise-OR VALUE into COUNTER. */
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 void
kono
parents:
diff changeset
269 __gcov_ior_profiler (gcov_type *counters, gcov_type value)
kono
parents:
diff changeset
270 {
kono
parents:
diff changeset
271 *counters |= value;
kono
parents:
diff changeset
272 }
kono
parents:
diff changeset
273 #endif
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 #if defined(L_gcov_ior_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
kono
parents:
diff changeset
276 /* Bitwise-OR VALUE into COUNTER. Function is thread-safe. */
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 void
kono
parents:
diff changeset
279 __gcov_ior_profiler_atomic (gcov_type *counters, gcov_type value)
kono
parents:
diff changeset
280 {
kono
parents:
diff changeset
281 __atomic_fetch_or (&counters[0], value, __ATOMIC_RELAXED);
kono
parents:
diff changeset
282 }
kono
parents:
diff changeset
283 #endif
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 #endif /* inhibit_libc */