1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2016 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
27 #if !defined(inhibit_libc)
29 #ifdef L_gcov_interval_profiler
30 /* If VALUE is in interval <START, START + STEPS - 1>, then increases the
31 corresponding counter in COUNTERS. If the VALUE is above or below
32 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
36 __gcov_interval_profiler (gcov_type
*counters
, gcov_type value
,
37 int start
, unsigned steps
)
39 gcov_type delta
= value
- start
;
41 counters
[steps
+ 1]++;
42 else if (delta
>= steps
)
49 #ifdef L_gcov_interval_profiler_atomic
50 /* If VALUE is in interval <START, START + STEPS - 1>, then increases the
51 corresponding counter in COUNTERS. If the VALUE is above or below
52 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
53 instead. Function is thread-safe. */
56 __gcov_interval_profiler_atomic (gcov_type
*counters
, gcov_type value
,
57 int start
, unsigned steps
)
59 gcov_type delta
= value
- start
;
61 __atomic_fetch_add (&counters
[steps
+ 1], 1, MEMMODEL_RELAXED
);
62 else if (delta
>= steps
)
63 __atomic_fetch_add (&counters
[steps
], 1, MEMMODEL_RELAXED
);
65 __atomic_fetch_add (&counters
[delta
], 1, MEMMODEL_RELAXED
);
69 #ifdef L_gcov_pow2_profiler
70 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
71 COUNTERS[0] is incremented. */
74 __gcov_pow2_profiler (gcov_type
*counters
, gcov_type value
)
76 if (value
== 0 || (value
& (value
- 1)))
83 #ifdef L_gcov_pow2_profiler_atomic
84 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
85 COUNTERS[0] is incremented. Function is thread-safe. */
88 __gcov_pow2_profiler_atomic (gcov_type
*counters
, gcov_type value
)
90 if (value
== 0 || (value
& (value
- 1)))
91 __atomic_fetch_add (&counters
[0], 1, MEMMODEL_RELAXED
);
93 __atomic_fetch_add (&counters
[1], 1, MEMMODEL_RELAXED
);
98 /* Tries to determine the most common value among its inputs. Checks if the
99 value stored in COUNTERS[0] matches VALUE. If this is the case, COUNTERS[1]
100 is incremented. If this is not the case and COUNTERS[1] is not zero,
101 COUNTERS[1] is decremented. Otherwise COUNTERS[1] is set to one and
102 VALUE is stored to COUNTERS[0]. This algorithm guarantees that if this
103 function is called more than 50% of the time with one value, this value
104 will be in COUNTERS[0] in the end.
106 In any case, COUNTERS[2] is incremented. If USE_ATOMIC is set to 1,
107 COUNTERS[2] is updated with an atomic instruction. */
110 __gcov_one_value_profiler_body (gcov_type
*counters
, gcov_type value
,
113 if (value
== counters
[0])
115 else if (counters
[1] == 0)
124 __atomic_fetch_add (&counters
[2], 1, MEMMODEL_RELAXED
);
129 #ifdef L_gcov_one_value_profiler
131 __gcov_one_value_profiler (gcov_type
*counters
, gcov_type value
)
133 __gcov_one_value_profiler_body (counters
, value
, 0);
137 #ifdef L_gcov_one_value_profiler_atomic
139 /* Update one value profilers (COUNTERS) for a given VALUE.
141 CAVEAT: Following function is not thread-safe, only total number
142 of executions (COUNTERS[2]) is update with an atomic instruction.
143 Problem is that one cannot atomically update two counters
144 (COUNTERS[0] and COUNTERS[1]), for more information please read
145 following email thread:
146 https://gcc.gnu.org/ml/gcc-patches/2016-08/msg00024.html. */
149 __gcov_one_value_profiler_atomic (gcov_type
*counters
, gcov_type value
)
151 __gcov_one_value_profiler_body (counters
, value
, 1);
155 #ifdef L_gcov_indirect_call_topn_profiler
156 /* Tries to keep track the most frequent N values in the counters where
157 N is specified by parameter TOPN_VAL. To track top N values, 2*N counter
159 counter[0] --- the accumative count of the number of times one entry in
160 in the counters gets evicted/replaced due to limited capacity.
161 When this value reaches a threshold, the bottom N values are
163 counter[1] through counter[2*N] records the top 2*N values collected so far.
164 Each value is represented by two entries: count[2*i+1] is the ith value, and
165 count[2*i+2] is the number of times the value is seen. */
168 __gcov_topn_value_profiler_body (gcov_type
*counters
, gcov_type value
)
170 unsigned i
, found
= 0, have_zero_count
= 0;
172 gcov_type
*lfu_entry
= &counters
[1];
173 gcov_type
*value_array
= &counters
[1];
174 gcov_type
*num_eviction
= &counters
[0];
175 gcov_unsigned_t topn_val
= GCOV_ICALL_TOPN_VAL
;
177 /* There are 2*topn_val values tracked, each value takes two slots in the
179 for (i
= 0; i
< (topn_val
<< 2); i
+= 2)
181 entry
= &value_array
[i
];
182 if (entry
[0] == value
)
188 else if (entry
[1] == 0)
193 else if (entry
[1] < lfu_entry
[1])
200 /* lfu_entry is either an empty entry or an entry
201 with lowest count, which will be evicted. */
202 lfu_entry
[0] = value
;
205 #define GCOV_ICALL_COUNTER_CLEAR_THRESHOLD 3000
207 /* Too many evictions -- time to clear bottom entries to
208 avoid hot values bumping each other out. */
210 && ++*num_eviction
>= GCOV_ICALL_COUNTER_CLEAR_THRESHOLD
)
215 = (gcov_type
*)alloca (topn_val
* sizeof (gcov_type
));
219 for (i
= 0; i
< topn_val
; i
++)
222 /* Find the largest topn_val values from the group of
223 2*topn_val values and put them into tmp_cnts. */
225 for (i
= 0; i
< 2 * topn_val
; i
+= 2)
228 for (j
= 0; j
< topn_val
; j
++)
230 if (!p
|| tmp_cnts
[j
] < *p
)
233 if (value_array
[i
+ 1] > *p
)
234 *p
= value_array
[i
+ 1];
238 for (j
= 1; j
< topn_val
; j
++)
240 if (tmp_cnts
[j
] < minv
)
243 /* Zero out low value entries. */
244 for (i
= 0; i
< 2 * topn_val
; i
+= 2)
246 if (value_array
[i
+ 1] < minv
)
249 value_array
[i
+ 1] = 0;
255 /* These two variables are used to actually track caller and callee. Keep
256 them in TLS memory so races are not common (they are written to often).
257 The variables are set directly by GCC instrumented code, so declaration
258 here must match one in tree-profile.c. */
260 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
263 gcov_type
*__gcov_indirect_call_topn_counters ATTRIBUTE_HIDDEN
;
265 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
268 void *__gcov_indirect_call_topn_callee ATTRIBUTE_HIDDEN
;
270 #ifdef TARGET_VTABLE_USES_DESCRIPTORS
271 #define VTABLE_USES_DESCRIPTORS 1
273 #define VTABLE_USES_DESCRIPTORS 0
276 /* This fucntion is instrumented at function entry to track topn indirect
277 calls to CUR_FUNC. */
280 __gcov_indirect_call_topn_profiler (gcov_type value
, void* cur_func
)
282 void *callee_func
= __gcov_indirect_call_topn_callee
;
283 /* If the C++ virtual tables contain function descriptors then one
284 function may have multiple descriptors and we need to dereference
285 the descriptors to see if they point to the same function. */
286 if (cur_func
== callee_func
287 || (VTABLE_USES_DESCRIPTORS
&& callee_func
288 && *(void **) cur_func
== *(void **) callee_func
))
289 __gcov_topn_value_profiler_body (__gcov_indirect_call_topn_counters
, value
);
293 #ifdef L_gcov_indirect_call_profiler_v2
295 /* These two variables are used to actually track caller and callee. Keep
296 them in TLS memory so races are not common (they are written to often).
297 The variables are set directly by GCC instrumented code, so declaration
298 here must match one in tree-profile.c */
300 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
303 void * __gcov_indirect_call_callee
;
304 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
307 gcov_type
* __gcov_indirect_call_counters
;
309 /* By default, the C++ compiler will use function addresses in the
310 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
311 tells the compiler to use function descriptors instead. The value
312 of this macro says how many words wide the descriptor is (normally 2).
314 It is assumed that the address of a function descriptor may be treated
315 as a pointer to a function. */
317 /* Tries to determine the most common value among its inputs. */
319 __gcov_indirect_call_profiler_v2 (gcov_type value
, void* cur_func
)
321 /* If the C++ virtual tables contain function descriptors then one
322 function may have multiple descriptors and we need to dereference
323 the descriptors to see if they point to the same function. */
324 if (cur_func
== __gcov_indirect_call_callee
325 || (__LIBGCC_VTABLE_USES_DESCRIPTORS__
&& __gcov_indirect_call_callee
326 && *(void **) cur_func
== *(void **) __gcov_indirect_call_callee
))
327 __gcov_one_value_profiler_body (__gcov_indirect_call_counters
, value
, 0);
331 #ifdef L_gcov_time_profiler
333 /* Counter for first visit of each function. */
334 static gcov_type function_counter
;
336 /* Sets corresponding COUNTERS if there is no value. */
339 __gcov_time_profiler (gcov_type
* counters
)
342 counters
[0] = ++function_counter
;
345 /* Sets corresponding COUNTERS if there is no value.
346 Function is thread-safe. */
349 __gcov_time_profiler_atomic (gcov_type
* counters
)
352 counters
[0] = __atomic_add_fetch (&function_counter
, 1, MEMMODEL_RELAXED
);
357 #ifdef L_gcov_average_profiler
358 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
362 __gcov_average_profiler (gcov_type
*counters
, gcov_type value
)
364 counters
[0] += value
;
369 #ifdef L_gcov_average_profiler_atomic
370 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
371 to saturate up. Function is thread-safe. */
374 __gcov_average_profiler_atomic (gcov_type
*counters
, gcov_type value
)
376 __atomic_fetch_add (&counters
[0], value
, MEMMODEL_RELAXED
);
377 __atomic_fetch_add (&counters
[1], 1, MEMMODEL_RELAXED
);
381 #ifdef L_gcov_ior_profiler
382 /* Bitwise-OR VALUE into COUNTER. */
385 __gcov_ior_profiler (gcov_type
*counters
, gcov_type value
)
391 #ifdef L_gcov_ior_profiler_atomic
392 /* Bitwise-OR VALUE into COUNTER. Function is thread-safe. */
395 __gcov_ior_profiler_atomic (gcov_type
*counters
, gcov_type value
)
397 __atomic_fetch_or (&counters
[0], value
, MEMMODEL_RELAXED
);
402 #endif /* inhibit_libc */