]>
Commit | Line | Data |
---|---|---|
d6d3f033 RX |
1 | /* Routines required for instrumenting a program. */ |
2 | /* Compile this one with gcc. */ | |
a945c346 | 3 | /* Copyright (C) 1989-2024 Free Software Foundation, Inc. |
d6d3f033 RX |
4 | |
5 | This file is part of GCC. | |
6 | ||
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 | |
10 | version. | |
11 | ||
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 | |
15 | for more details. | |
16 | ||
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. | |
20 | ||
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/>. */ | |
25 | ||
40d6b753 | 26 | #include "libgcov.h" |
d6d3f033 RX |
27 | |
28 | #if defined(inhibit_libc) | |
29 | /* If libc and its header files are not available, provide dummy functions. */ | |
30 | ||
31 | #ifdef L_gcov_merge_add | |
32 | void __gcov_merge_add (gcov_type *counters __attribute__ ((unused)), | |
33 | unsigned n_counters __attribute__ ((unused))) {} | |
34 | #endif | |
35 | ||
08a52331 JK |
36 | #ifdef L_gcov_merge_ior |
37 | void __gcov_merge_ior (gcov_type *counters __attribute__ ((unused)), | |
38 | unsigned n_counters __attribute__ ((unused))) {} | |
39 | #endif | |
40 | ||
596341c7 ML |
41 | #ifdef L_gcov_merge_topn |
42 | void __gcov_merge_topn (gcov_type *counters __attribute__ ((unused)), | |
43 | unsigned n_counters __attribute__ ((unused))) {} | |
d6d3f033 RX |
44 | #endif |
45 | ||
d6d3f033 RX |
46 | #else |
47 | ||
48 | #ifdef L_gcov_merge_add | |
49 | /* The profile merging function that just adds the counters. It is given | |
50 | an array COUNTERS of N_COUNTERS old counters and it reads the same number | |
51 | of counters from the gcov file. */ | |
52 | void | |
53 | __gcov_merge_add (gcov_type *counters, unsigned n_counters) | |
54 | { | |
55 | for (; n_counters; counters++, n_counters--) | |
c77556a5 | 56 | *counters += gcov_get_counter (); |
d6d3f033 RX |
57 | } |
58 | #endif /* L_gcov_merge_add */ | |
59 | ||
60 | #ifdef L_gcov_merge_ior | |
61 | /* The profile merging function that just adds the counters. It is given | |
62 | an array COUNTERS of N_COUNTERS old counters and it reads the same number | |
63 | of counters from the gcov file. */ | |
64 | void | |
65 | __gcov_merge_ior (gcov_type *counters, unsigned n_counters) | |
66 | { | |
67 | for (; n_counters; counters++, n_counters--) | |
c77556a5 | 68 | *counters |= gcov_get_counter_target (); |
d6d3f033 RX |
69 | } |
70 | #endif | |
71 | ||
72 | #ifdef L_gcov_merge_time_profile | |
73 | /* Time profiles are merged so that minimum from all valid (greater than zero) | |
74 | is stored. There could be a fork that creates new counters. To have | |
75 | the profile stable, we chosen to pick the smallest function visit time. */ | |
76 | void | |
77 | __gcov_merge_time_profile (gcov_type *counters, unsigned n_counters) | |
78 | { | |
79 | unsigned int i; | |
80 | gcov_type value; | |
81 | ||
82 | for (i = 0; i < n_counters; i++) | |
83 | { | |
c77556a5 | 84 | value = gcov_get_counter_target (); |
d6d3f033 RX |
85 | |
86 | if (value && (!counters[i] || value < counters[i])) | |
87 | counters[i] = value; | |
88 | } | |
89 | } | |
90 | #endif /* L_gcov_merge_time_profile */ | |
91 | ||
596341c7 | 92 | #ifdef L_gcov_merge_topn |
92d41717 | 93 | |
d6d3f033 RX |
94 | /* The profile merging function for choosing the most common value. |
95 | It is given an array COUNTERS of N_COUNTERS old counters and it | |
96 | reads the same number of counters from the gcov file. The counters | |
92d41717 | 97 | are split into pairs where the members of the tuple have |
d6d3f033 RX |
98 | meanings: |
99 | ||
100 | -- the stored candidate on the most common value of the measured entity | |
101 | -- counter | |
5089df53 ML |
102 | |
103 | We use -TOTAL for situation when merging dropped some values. | |
104 | The information is used for -fprofile-reproducible flag. | |
92d41717 | 105 | */ |
871e5ada | 106 | |
d6d3f033 | 107 | void |
596341c7 | 108 | __gcov_merge_topn (gcov_type *counters, unsigned n_counters) |
d6d3f033 | 109 | { |
871e5ada | 110 | gcc_assert (!(n_counters % GCOV_TOPN_MEM_COUNTERS)); |
d6d3f033 | 111 | |
871e5ada ML |
112 | for (unsigned i = 0; i < (n_counters / GCOV_TOPN_MEM_COUNTERS); i++) |
113 | { | |
114 | /* First value is number of total executions of the profiler. */ | |
115 | gcov_type all = gcov_get_counter_ignore_scaling (-1); | |
116 | gcov_type n = gcov_get_counter_ignore_scaling (-1); | |
117 | ||
5089df53 ML |
118 | unsigned full = all < 0; |
119 | gcov_type *total = &counters[GCOV_TOPN_MEM_COUNTERS * i]; | |
120 | *total += full ? -all : all; | |
871e5ada ML |
121 | |
122 | for (unsigned j = 0; j < n; j++) | |
123 | { | |
124 | gcov_type value = gcov_get_counter_target (); | |
125 | gcov_type count = gcov_get_counter_ignore_scaling (-1); | |
126 | ||
127 | // TODO: we should use atomic here | |
5089df53 ML |
128 | full |= gcov_topn_add_value (counters + GCOV_TOPN_MEM_COUNTERS * i, |
129 | value, count, 0, 0); | |
871e5ada | 130 | } |
5089df53 ML |
131 | |
132 | if (full) | |
133 | *total = -(*total); | |
871e5ada | 134 | } |
d6d3f033 | 135 | } |
596341c7 | 136 | #endif /* L_gcov_merge_topn */ |
d6d3f033 | 137 | |
d6d3f033 | 138 | #endif /* inhibit_libc */ |