annotate libgcc/libgcov-util.c @ 120:f93fa5091070

fix conv1.c
author mir3636
date Thu, 08 Mar 2018 14:53:42 +0900
parents 04ced10e8804
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Utility functions for reading gcda files into in-memory
kono
parents:
diff changeset
2 gcov_info structures and offline profile processing. */
kono
parents:
diff changeset
3 /* Copyright (C) 2014-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
4 Contributed by Rong Xu <xur@google.com>.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 This file is part of GCC.
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
9 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
10 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
11 version.
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
16 for more details.
kono
parents:
diff changeset
17
kono
parents:
diff changeset
18 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
19 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
20 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
23 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
25 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 #define IN_GCOV_TOOL 1
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 #include "libgcov.h"
kono
parents:
diff changeset
31 #include "intl.h"
kono
parents:
diff changeset
32 #include "diagnostic.h"
kono
parents:
diff changeset
33 #include "version.h"
kono
parents:
diff changeset
34 #include "demangle.h"
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 /* Borrowed from basic-block.h. */
kono
parents:
diff changeset
37 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 extern gcov_position_t gcov_position();
kono
parents:
diff changeset
40 extern int gcov_is_error();
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 /* Verbose mode for debug. */
kono
parents:
diff changeset
43 static int verbose;
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 /* Set verbose flag. */
kono
parents:
diff changeset
46 void gcov_set_verbose (void)
kono
parents:
diff changeset
47 {
kono
parents:
diff changeset
48 verbose = 1;
kono
parents:
diff changeset
49 }
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 /* The following part is to read Gcda and reconstruct GCOV_INFO. */
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 #include "obstack.h"
kono
parents:
diff changeset
54 #include <unistd.h>
kono
parents:
diff changeset
55 #ifdef HAVE_FTW_H
kono
parents:
diff changeset
56 #include <ftw.h>
kono
parents:
diff changeset
57 #endif
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 static void tag_function (unsigned, unsigned);
kono
parents:
diff changeset
60 static void tag_blocks (unsigned, unsigned);
kono
parents:
diff changeset
61 static void tag_arcs (unsigned, unsigned);
kono
parents:
diff changeset
62 static void tag_lines (unsigned, unsigned);
kono
parents:
diff changeset
63 static void tag_counters (unsigned, unsigned);
kono
parents:
diff changeset
64 static void tag_summary (unsigned, unsigned);
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 /* The gcov_info for the first module. */
kono
parents:
diff changeset
67 static struct gcov_info *curr_gcov_info;
kono
parents:
diff changeset
68 /* The gcov_info being processed. */
kono
parents:
diff changeset
69 static struct gcov_info *gcov_info_head;
kono
parents:
diff changeset
70 /* This variable contains all the functions in current module. */
kono
parents:
diff changeset
71 static struct obstack fn_info;
kono
parents:
diff changeset
72 /* The function being processed. */
kono
parents:
diff changeset
73 static struct gcov_fn_info *curr_fn_info;
kono
parents:
diff changeset
74 /* The number of functions seen so far. */
kono
parents:
diff changeset
75 static unsigned num_fn_info;
kono
parents:
diff changeset
76 /* This variable contains all the counters for current module. */
kono
parents:
diff changeset
77 static int k_ctrs_mask[GCOV_COUNTERS];
kono
parents:
diff changeset
78 /* The kind of counters that have been seen. */
kono
parents:
diff changeset
79 static struct gcov_ctr_info k_ctrs[GCOV_COUNTERS];
kono
parents:
diff changeset
80 /* Number of kind of counters that have been seen. */
kono
parents:
diff changeset
81 static int k_ctrs_types;
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 /* Merge functions for counters. */
kono
parents:
diff changeset
84 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) __gcov_merge ## FN_TYPE,
kono
parents:
diff changeset
85 static gcov_merge_fn ctr_merge_functions[GCOV_COUNTERS] = {
kono
parents:
diff changeset
86 #include "gcov-counter.def"
kono
parents:
diff changeset
87 };
kono
parents:
diff changeset
88 #undef DEF_GCOV_COUNTER
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 /* Set the ctrs field in gcov_fn_info object FN_INFO. */
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 static void
kono
parents:
diff changeset
93 set_fn_ctrs (struct gcov_fn_info *fn_info)
kono
parents:
diff changeset
94 {
kono
parents:
diff changeset
95 int j = 0, i;
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 for (i = 0; i < GCOV_COUNTERS; i++)
kono
parents:
diff changeset
98 {
kono
parents:
diff changeset
99 if (k_ctrs_mask[i] == 0)
kono
parents:
diff changeset
100 continue;
kono
parents:
diff changeset
101 fn_info->ctrs[j].num = k_ctrs[i].num;
kono
parents:
diff changeset
102 fn_info->ctrs[j].values = k_ctrs[i].values;
kono
parents:
diff changeset
103 j++;
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105 if (k_ctrs_types == 0)
kono
parents:
diff changeset
106 k_ctrs_types = j;
kono
parents:
diff changeset
107 else
kono
parents:
diff changeset
108 gcc_assert (j == k_ctrs_types);
kono
parents:
diff changeset
109 }
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 /* For each tag in gcda file, we have an entry here.
kono
parents:
diff changeset
112 TAG is the tag value; NAME is the tag name; and
kono
parents:
diff changeset
113 PROC is the handler function. */
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 typedef struct tag_format
kono
parents:
diff changeset
116 {
kono
parents:
diff changeset
117 unsigned tag;
kono
parents:
diff changeset
118 char const *name;
kono
parents:
diff changeset
119 void (*proc) (unsigned, unsigned);
kono
parents:
diff changeset
120 } tag_format_t;
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 /* Handler table for various Tags. */
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 static const tag_format_t tag_table[] =
kono
parents:
diff changeset
125 {
kono
parents:
diff changeset
126 {0, "NOP", NULL},
kono
parents:
diff changeset
127 {0, "UNKNOWN", NULL},
kono
parents:
diff changeset
128 {0, "COUNTERS", tag_counters},
kono
parents:
diff changeset
129 {GCOV_TAG_FUNCTION, "FUNCTION", tag_function},
kono
parents:
diff changeset
130 {GCOV_TAG_BLOCKS, "BLOCKS", tag_blocks},
kono
parents:
diff changeset
131 {GCOV_TAG_ARCS, "ARCS", tag_arcs},
kono
parents:
diff changeset
132 {GCOV_TAG_LINES, "LINES", tag_lines},
kono
parents:
diff changeset
133 {GCOV_TAG_OBJECT_SUMMARY, "OBJECT_SUMMARY", tag_summary},
kono
parents:
diff changeset
134 {GCOV_TAG_PROGRAM_SUMMARY, "PROGRAM_SUMMARY", tag_summary},
kono
parents:
diff changeset
135 {0, NULL, NULL}
kono
parents:
diff changeset
136 };
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 /* Handler for reading function tag. */
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 static void
kono
parents:
diff changeset
141 tag_function (unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 int i;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 /* write out previous fn_info. */
kono
parents:
diff changeset
146 if (num_fn_info)
kono
parents:
diff changeset
147 {
kono
parents:
diff changeset
148 set_fn_ctrs (curr_fn_info);
kono
parents:
diff changeset
149 obstack_ptr_grow (&fn_info, curr_fn_info);
kono
parents:
diff changeset
150 }
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 /* Here we over allocate a bit, using GCOV_COUNTERS instead of the actual active
kono
parents:
diff changeset
153 counter types. */
kono
parents:
diff changeset
154 curr_fn_info = (struct gcov_fn_info *) xcalloc (sizeof (struct gcov_fn_info)
kono
parents:
diff changeset
155 + GCOV_COUNTERS * sizeof (struct gcov_ctr_info), 1);
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 for (i = 0; i < GCOV_COUNTERS; i++)
kono
parents:
diff changeset
158 k_ctrs[i].num = 0;
kono
parents:
diff changeset
159 k_ctrs_types = 0;
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 curr_fn_info->key = curr_gcov_info;
kono
parents:
diff changeset
162 curr_fn_info->ident = gcov_read_unsigned ();
kono
parents:
diff changeset
163 curr_fn_info->lineno_checksum = gcov_read_unsigned ();
kono
parents:
diff changeset
164 curr_fn_info->cfg_checksum = gcov_read_unsigned ();
kono
parents:
diff changeset
165 num_fn_info++;
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 if (verbose)
kono
parents:
diff changeset
168 fnotice (stdout, "tag one function id=%d\n", curr_fn_info->ident);
kono
parents:
diff changeset
169 }
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 /* Handler for reading block tag. */
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 static void
kono
parents:
diff changeset
174 tag_blocks (unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
175 {
kono
parents:
diff changeset
176 /* TBD: gcov-tool currently does not handle gcno files. Assert here. */
kono
parents:
diff changeset
177 gcc_unreachable ();
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 /* Handler for reading flow arc tag. */
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 static void
kono
parents:
diff changeset
183 tag_arcs (unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
184 {
kono
parents:
diff changeset
185 /* TBD: gcov-tool currently does not handle gcno files. Assert here. */
kono
parents:
diff changeset
186 gcc_unreachable ();
kono
parents:
diff changeset
187 }
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 /* Handler for reading line tag. */
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 static void
kono
parents:
diff changeset
192 tag_lines (unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
193 {
kono
parents:
diff changeset
194 /* TBD: gcov-tool currently does not handle gcno files. Assert here. */
kono
parents:
diff changeset
195 gcc_unreachable ();
kono
parents:
diff changeset
196 }
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 /* Handler for reading counters array tag with value as TAG and length of LENGTH. */
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 static void
kono
parents:
diff changeset
201 tag_counters (unsigned tag, unsigned length)
kono
parents:
diff changeset
202 {
kono
parents:
diff changeset
203 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
kono
parents:
diff changeset
204 gcov_type *values;
kono
parents:
diff changeset
205 unsigned ix;
kono
parents:
diff changeset
206 unsigned tag_ix;
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 tag_ix = GCOV_COUNTER_FOR_TAG (tag);
kono
parents:
diff changeset
209 gcc_assert (tag_ix < GCOV_COUNTERS);
kono
parents:
diff changeset
210 k_ctrs_mask [tag_ix] = 1;
kono
parents:
diff changeset
211 gcc_assert (k_ctrs[tag_ix].num == 0);
kono
parents:
diff changeset
212 k_ctrs[tag_ix].num = n_counts;
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 k_ctrs[tag_ix].values = values = (gcov_type *) xmalloc (n_counts * sizeof (gcov_type));
kono
parents:
diff changeset
215 gcc_assert (values);
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 for (ix = 0; ix != n_counts; ix++)
kono
parents:
diff changeset
218 values[ix] = gcov_read_counter ();
kono
parents:
diff changeset
219 }
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 /* Handler for reading summary tag. */
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 static void
kono
parents:
diff changeset
224 tag_summary (unsigned tag ATTRIBUTE_UNUSED, unsigned length ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
225 {
kono
parents:
diff changeset
226 struct gcov_summary summary;
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 gcov_read_summary (&summary);
kono
parents:
diff changeset
229 }
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 /* This function is called at the end of reading a gcda file.
kono
parents:
diff changeset
232 It flushes the contents in curr_fn_info to gcov_info object OBJ_INFO. */
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 static void
kono
parents:
diff changeset
235 read_gcda_finalize (struct gcov_info *obj_info)
kono
parents:
diff changeset
236 {
kono
parents:
diff changeset
237 int i;
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 set_fn_ctrs (curr_fn_info);
kono
parents:
diff changeset
240 obstack_ptr_grow (&fn_info, curr_fn_info);
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 /* We set the following fields: merge, n_functions, and functions. */
kono
parents:
diff changeset
243 obj_info->n_functions = num_fn_info;
kono
parents:
diff changeset
244 obj_info->functions = (const struct gcov_fn_info**) obstack_finish (&fn_info);
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 /* wrap all the counter array. */
kono
parents:
diff changeset
247 for (i=0; i< GCOV_COUNTERS; i++)
kono
parents:
diff changeset
248 {
kono
parents:
diff changeset
249 if (k_ctrs_mask[i])
kono
parents:
diff changeset
250 obj_info->merge[i] = ctr_merge_functions[i];
kono
parents:
diff changeset
251 }
kono
parents:
diff changeset
252 }
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 /* Read the content of a gcda file FILENAME, and return a gcov_info data structure.
kono
parents:
diff changeset
255 Program level summary CURRENT_SUMMARY will also be updated. */
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 static struct gcov_info *
kono
parents:
diff changeset
258 read_gcda_file (const char *filename)
kono
parents:
diff changeset
259 {
kono
parents:
diff changeset
260 unsigned tags[4];
kono
parents:
diff changeset
261 unsigned depth = 0;
kono
parents:
diff changeset
262 unsigned magic, version;
kono
parents:
diff changeset
263 struct gcov_info *obj_info;
kono
parents:
diff changeset
264 int i;
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 for (i=0; i< GCOV_COUNTERS; i++)
kono
parents:
diff changeset
267 k_ctrs_mask[i] = 0;
kono
parents:
diff changeset
268 k_ctrs_types = 0;
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 if (!gcov_open (filename))
kono
parents:
diff changeset
271 {
kono
parents:
diff changeset
272 fnotice (stderr, "%s:cannot open\n", filename);
kono
parents:
diff changeset
273 return NULL;
kono
parents:
diff changeset
274 }
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 /* Read magic. */
kono
parents:
diff changeset
277 magic = gcov_read_unsigned ();
kono
parents:
diff changeset
278 if (magic != GCOV_DATA_MAGIC)
kono
parents:
diff changeset
279 {
kono
parents:
diff changeset
280 fnotice (stderr, "%s:not a gcov data file\n", filename);
kono
parents:
diff changeset
281 gcov_close ();
kono
parents:
diff changeset
282 return NULL;
kono
parents:
diff changeset
283 }
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 /* Read version. */
kono
parents:
diff changeset
286 version = gcov_read_unsigned ();
kono
parents:
diff changeset
287 if (version != GCOV_VERSION)
kono
parents:
diff changeset
288 {
kono
parents:
diff changeset
289 fnotice (stderr, "%s:incorrect gcov version %d vs %d \n", filename, version, GCOV_VERSION);
kono
parents:
diff changeset
290 gcov_close ();
kono
parents:
diff changeset
291 return NULL;
kono
parents:
diff changeset
292 }
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 /* Instantiate a gcov_info object. */
kono
parents:
diff changeset
295 curr_gcov_info = obj_info = (struct gcov_info *) xcalloc (sizeof (struct gcov_info) +
kono
parents:
diff changeset
296 sizeof (struct gcov_ctr_info) * GCOV_COUNTERS, 1);
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 obj_info->version = version;
kono
parents:
diff changeset
299 obstack_init (&fn_info);
kono
parents:
diff changeset
300 num_fn_info = 0;
kono
parents:
diff changeset
301 curr_fn_info = 0;
kono
parents:
diff changeset
302 {
kono
parents:
diff changeset
303 size_t len = strlen (filename) + 1;
kono
parents:
diff changeset
304 char *str_dup = (char*) xmalloc (len);
kono
parents:
diff changeset
305
kono
parents:
diff changeset
306 memcpy (str_dup, filename, len);
kono
parents:
diff changeset
307 obj_info->filename = str_dup;
kono
parents:
diff changeset
308 }
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 /* Read stamp. */
kono
parents:
diff changeset
311 obj_info->stamp = gcov_read_unsigned ();
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 while (1)
kono
parents:
diff changeset
314 {
kono
parents:
diff changeset
315 gcov_position_t base;
kono
parents:
diff changeset
316 unsigned tag, length;
kono
parents:
diff changeset
317 tag_format_t const *format;
kono
parents:
diff changeset
318 unsigned tag_depth;
kono
parents:
diff changeset
319 int error;
kono
parents:
diff changeset
320 unsigned mask;
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 tag = gcov_read_unsigned ();
kono
parents:
diff changeset
323 if (!tag)
kono
parents:
diff changeset
324 break;
kono
parents:
diff changeset
325 length = gcov_read_unsigned ();
kono
parents:
diff changeset
326 base = gcov_position ();
kono
parents:
diff changeset
327 mask = GCOV_TAG_MASK (tag) >> 1;
kono
parents:
diff changeset
328 for (tag_depth = 4; mask; mask >>= 8)
kono
parents:
diff changeset
329 {
kono
parents:
diff changeset
330 if (((mask & 0xff) != 0xff))
kono
parents:
diff changeset
331 {
kono
parents:
diff changeset
332 warning (0, "%s:tag `%x' is invalid\n", filename, tag);
kono
parents:
diff changeset
333 break;
kono
parents:
diff changeset
334 }
kono
parents:
diff changeset
335 tag_depth--;
kono
parents:
diff changeset
336 }
kono
parents:
diff changeset
337 for (format = tag_table; format->name; format++)
kono
parents:
diff changeset
338 if (format->tag == tag)
kono
parents:
diff changeset
339 goto found;
kono
parents:
diff changeset
340 format = &tag_table[GCOV_TAG_IS_COUNTER (tag) ? 2 : 1];
kono
parents:
diff changeset
341 found:;
kono
parents:
diff changeset
342 if (tag)
kono
parents:
diff changeset
343 {
kono
parents:
diff changeset
344 if (depth && depth < tag_depth)
kono
parents:
diff changeset
345 {
kono
parents:
diff changeset
346 if (!GCOV_TAG_IS_SUBTAG (tags[depth - 1], tag))
kono
parents:
diff changeset
347 warning (0, "%s:tag `%x' is incorrectly nested\n",
kono
parents:
diff changeset
348 filename, tag);
kono
parents:
diff changeset
349 }
kono
parents:
diff changeset
350 depth = tag_depth;
kono
parents:
diff changeset
351 tags[depth - 1] = tag;
kono
parents:
diff changeset
352 }
kono
parents:
diff changeset
353
kono
parents:
diff changeset
354 if (format->proc)
kono
parents:
diff changeset
355 {
kono
parents:
diff changeset
356 unsigned long actual_length;
kono
parents:
diff changeset
357
kono
parents:
diff changeset
358 (*format->proc) (tag, length);
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 actual_length = gcov_position () - base;
kono
parents:
diff changeset
361 if (actual_length > length)
kono
parents:
diff changeset
362 warning (0, "%s:record size mismatch %lu bytes overread\n",
kono
parents:
diff changeset
363 filename, actual_length - length);
kono
parents:
diff changeset
364 else if (length > actual_length)
kono
parents:
diff changeset
365 warning (0, "%s:record size mismatch %lu bytes unread\n",
kono
parents:
diff changeset
366 filename, length - actual_length);
kono
parents:
diff changeset
367 }
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 gcov_sync (base, length);
kono
parents:
diff changeset
370 if ((error = gcov_is_error ()))
kono
parents:
diff changeset
371 {
kono
parents:
diff changeset
372 warning (0, error < 0 ? "%s:counter overflow at %lu\n" :
kono
parents:
diff changeset
373 "%s:read error at %lu\n", filename,
kono
parents:
diff changeset
374 (long unsigned) gcov_position ());
kono
parents:
diff changeset
375 break;
kono
parents:
diff changeset
376 }
kono
parents:
diff changeset
377 }
kono
parents:
diff changeset
378
kono
parents:
diff changeset
379 read_gcda_finalize (obj_info);
kono
parents:
diff changeset
380 gcov_close ();
kono
parents:
diff changeset
381
kono
parents:
diff changeset
382 return obj_info;
kono
parents:
diff changeset
383 }
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385 #ifdef HAVE_FTW_H
kono
parents:
diff changeset
386 /* This will be called by ftw(). It opens and read a gcda file FILENAME.
kono
parents:
diff changeset
387 Return a non-zero value to stop the tree walk. */
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 static int
kono
parents:
diff changeset
390 ftw_read_file (const char *filename,
kono
parents:
diff changeset
391 const struct stat *status ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
392 int type)
kono
parents:
diff changeset
393 {
kono
parents:
diff changeset
394 int filename_len;
kono
parents:
diff changeset
395 int suffix_len;
kono
parents:
diff changeset
396 struct gcov_info *obj_info;
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 /* Only read regular files. */
kono
parents:
diff changeset
399 if (type != FTW_F)
kono
parents:
diff changeset
400 return 0;
kono
parents:
diff changeset
401
kono
parents:
diff changeset
402 filename_len = strlen (filename);
kono
parents:
diff changeset
403 suffix_len = strlen (GCOV_DATA_SUFFIX);
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 if (filename_len <= suffix_len)
kono
parents:
diff changeset
406 return 0;
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408 if (strcmp(filename + filename_len - suffix_len, GCOV_DATA_SUFFIX))
kono
parents:
diff changeset
409 return 0;
kono
parents:
diff changeset
410
kono
parents:
diff changeset
411 if (verbose)
kono
parents:
diff changeset
412 fnotice (stderr, "reading file: %s\n", filename);
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 obj_info = read_gcda_file (filename);
kono
parents:
diff changeset
415 if (!obj_info)
kono
parents:
diff changeset
416 return 0;
kono
parents:
diff changeset
417
kono
parents:
diff changeset
418 obj_info->next = gcov_info_head;
kono
parents:
diff changeset
419 gcov_info_head = obj_info;
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 return 0;
kono
parents:
diff changeset
422 }
kono
parents:
diff changeset
423 #endif
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 /* Initializer for reading a profile dir. */
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 static inline void
kono
parents:
diff changeset
428 read_profile_dir_init (void)
kono
parents:
diff changeset
429 {
kono
parents:
diff changeset
430 gcov_info_head = 0;
kono
parents:
diff changeset
431 }
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 /* Driver for read a profile directory and convert into gcov_info list in memory.
kono
parents:
diff changeset
434 Return NULL on error,
kono
parents:
diff changeset
435 Return the head of gcov_info list on success. */
kono
parents:
diff changeset
436
kono
parents:
diff changeset
437 struct gcov_info *
kono
parents:
diff changeset
438 gcov_read_profile_dir (const char* dir_name, int recompute_summary ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
439 {
kono
parents:
diff changeset
440 char *pwd;
kono
parents:
diff changeset
441 int ret;
kono
parents:
diff changeset
442
kono
parents:
diff changeset
443 read_profile_dir_init ();
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 if (access (dir_name, R_OK) != 0)
kono
parents:
diff changeset
446 {
kono
parents:
diff changeset
447 fnotice (stderr, "cannot access directory %s\n", dir_name);
kono
parents:
diff changeset
448 return NULL;
kono
parents:
diff changeset
449 }
kono
parents:
diff changeset
450 pwd = getcwd (NULL, 0);
kono
parents:
diff changeset
451 gcc_assert (pwd);
kono
parents:
diff changeset
452 ret = chdir (dir_name);
kono
parents:
diff changeset
453 if (ret !=0)
kono
parents:
diff changeset
454 {
kono
parents:
diff changeset
455 fnotice (stderr, "%s is not a directory\n", dir_name);
kono
parents:
diff changeset
456 return NULL;
kono
parents:
diff changeset
457 }
kono
parents:
diff changeset
458 #ifdef HAVE_FTW_H
kono
parents:
diff changeset
459 ftw (".", ftw_read_file, 50);
kono
parents:
diff changeset
460 #endif
kono
parents:
diff changeset
461 ret = chdir (pwd);
kono
parents:
diff changeset
462 free (pwd);
kono
parents:
diff changeset
463
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 return gcov_info_head;;
kono
parents:
diff changeset
466 }
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 /* This part of the code is to merge profile counters. These
kono
parents:
diff changeset
469 variables are set in merge_wrapper and to be used by
kono
parents:
diff changeset
470 global function gcov_read_counter_mem() and gcov_get_merge_weight. */
kono
parents:
diff changeset
471
kono
parents:
diff changeset
472 /* We save the counter value address to this variable. */
kono
parents:
diff changeset
473 static gcov_type *gcov_value_buf;
kono
parents:
diff changeset
474
kono
parents:
diff changeset
475 /* The number of counter values to be read by current merging. */
kono
parents:
diff changeset
476 static gcov_unsigned_t gcov_value_buf_size;
kono
parents:
diff changeset
477
kono
parents:
diff changeset
478 /* The index of counter values being read. */
kono
parents:
diff changeset
479 static gcov_unsigned_t gcov_value_buf_pos;
kono
parents:
diff changeset
480
kono
parents:
diff changeset
481 /* The weight of current merging. */
kono
parents:
diff changeset
482 static unsigned gcov_merge_weight;
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 /* Read a counter value from gcov_value_buf array. */
kono
parents:
diff changeset
485
kono
parents:
diff changeset
486 gcov_type
kono
parents:
diff changeset
487 gcov_read_counter_mem (void)
kono
parents:
diff changeset
488 {
kono
parents:
diff changeset
489 gcov_type ret;
kono
parents:
diff changeset
490 gcc_assert (gcov_value_buf_pos < gcov_value_buf_size);
kono
parents:
diff changeset
491 ret = *(gcov_value_buf + gcov_value_buf_pos);
kono
parents:
diff changeset
492 ++gcov_value_buf_pos;
kono
parents:
diff changeset
493 return ret;
kono
parents:
diff changeset
494 }
kono
parents:
diff changeset
495
kono
parents:
diff changeset
496 /* Return the recorded merge weight. */
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 unsigned
kono
parents:
diff changeset
499 gcov_get_merge_weight (void)
kono
parents:
diff changeset
500 {
kono
parents:
diff changeset
501 return gcov_merge_weight;
kono
parents:
diff changeset
502 }
kono
parents:
diff changeset
503
kono
parents:
diff changeset
504 /* A wrapper function for merge functions. It sets up the
kono
parents:
diff changeset
505 value buffer and weights and then calls the merge function. */
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 static void
kono
parents:
diff changeset
508 merge_wrapper (gcov_merge_fn f, gcov_type *v1, gcov_unsigned_t n,
kono
parents:
diff changeset
509 gcov_type *v2, unsigned w)
kono
parents:
diff changeset
510 {
kono
parents:
diff changeset
511 gcov_value_buf = v2;
kono
parents:
diff changeset
512 gcov_value_buf_pos = 0;
kono
parents:
diff changeset
513 gcov_value_buf_size = n;
kono
parents:
diff changeset
514 gcov_merge_weight = w;
kono
parents:
diff changeset
515 (*f) (v1, n);
kono
parents:
diff changeset
516 }
kono
parents:
diff changeset
517
kono
parents:
diff changeset
518 /* Offline tool to manipulate profile data.
kono
parents:
diff changeset
519 This tool targets on matched profiles. But it has some tolerance on
kono
parents:
diff changeset
520 unmatched profiles.
kono
parents:
diff changeset
521 When merging p1 to p2 (p2 is the dst),
kono
parents:
diff changeset
522 * m.gcda in p1 but not in p2: append m.gcda to p2 with specified weight;
kono
parents:
diff changeset
523 emit warning
kono
parents:
diff changeset
524 * m.gcda in p2 but not in p1: keep m.gcda in p2 and multiply by
kono
parents:
diff changeset
525 specified weight; emit warning.
kono
parents:
diff changeset
526 * m.gcda in both p1 and p2:
kono
parents:
diff changeset
527 ** p1->m.gcda->f checksum matches p2->m.gcda->f: simple merge.
kono
parents:
diff changeset
528 ** p1->m.gcda->f checksum does not matches p2->m.gcda->f: keep
kono
parents:
diff changeset
529 p2->m.gcda->f and
kono
parents:
diff changeset
530 drop p1->m.gcda->f. A warning is emitted. */
kono
parents:
diff changeset
531
kono
parents:
diff changeset
532 /* Add INFO2's counter to INFO1, multiplying by weight W. */
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 static int
kono
parents:
diff changeset
535 gcov_merge (struct gcov_info *info1, struct gcov_info *info2, int w)
kono
parents:
diff changeset
536 {
kono
parents:
diff changeset
537 unsigned f_ix;
kono
parents:
diff changeset
538 unsigned n_functions = info1->n_functions;
kono
parents:
diff changeset
539 int has_mismatch = 0;
kono
parents:
diff changeset
540
kono
parents:
diff changeset
541 gcc_assert (info2->n_functions == n_functions);
kono
parents:
diff changeset
542 for (f_ix = 0; f_ix < n_functions; f_ix++)
kono
parents:
diff changeset
543 {
kono
parents:
diff changeset
544 unsigned t_ix;
kono
parents:
diff changeset
545 const struct gcov_fn_info *gfi_ptr1 = info1->functions[f_ix];
kono
parents:
diff changeset
546 const struct gcov_fn_info *gfi_ptr2 = info2->functions[f_ix];
kono
parents:
diff changeset
547 const struct gcov_ctr_info *ci_ptr1, *ci_ptr2;
kono
parents:
diff changeset
548
kono
parents:
diff changeset
549 if (!gfi_ptr1 || gfi_ptr1->key != info1)
kono
parents:
diff changeset
550 continue;
kono
parents:
diff changeset
551 if (!gfi_ptr2 || gfi_ptr2->key != info2)
kono
parents:
diff changeset
552 continue;
kono
parents:
diff changeset
553
kono
parents:
diff changeset
554 if (gfi_ptr1->cfg_checksum != gfi_ptr2->cfg_checksum)
kono
parents:
diff changeset
555 {
kono
parents:
diff changeset
556 fnotice (stderr, "in %s, cfg_checksum mismatch, skipping\n",
kono
parents:
diff changeset
557 info1->filename);
kono
parents:
diff changeset
558 has_mismatch = 1;
kono
parents:
diff changeset
559 continue;
kono
parents:
diff changeset
560 }
kono
parents:
diff changeset
561 ci_ptr1 = gfi_ptr1->ctrs;
kono
parents:
diff changeset
562 ci_ptr2 = gfi_ptr2->ctrs;
kono
parents:
diff changeset
563 for (t_ix = 0; t_ix != GCOV_COUNTERS; t_ix++)
kono
parents:
diff changeset
564 {
kono
parents:
diff changeset
565 gcov_merge_fn merge1 = info1->merge[t_ix];
kono
parents:
diff changeset
566 gcov_merge_fn merge2 = info2->merge[t_ix];
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 gcc_assert (merge1 == merge2);
kono
parents:
diff changeset
569 if (!merge1)
kono
parents:
diff changeset
570 continue;
kono
parents:
diff changeset
571 gcc_assert (ci_ptr1->num == ci_ptr2->num);
kono
parents:
diff changeset
572 merge_wrapper (merge1, ci_ptr1->values, ci_ptr1->num, ci_ptr2->values, w);
kono
parents:
diff changeset
573 ci_ptr1++;
kono
parents:
diff changeset
574 ci_ptr2++;
kono
parents:
diff changeset
575 }
kono
parents:
diff changeset
576 }
kono
parents:
diff changeset
577
kono
parents:
diff changeset
578 return has_mismatch;
kono
parents:
diff changeset
579 }
kono
parents:
diff changeset
580
kono
parents:
diff changeset
581 /* Find and return the match gcov_info object for INFO from ARRAY.
kono
parents:
diff changeset
582 SIZE is the length of ARRAY.
kono
parents:
diff changeset
583 Return NULL if there is no match. */
kono
parents:
diff changeset
584
kono
parents:
diff changeset
585 static struct gcov_info *
kono
parents:
diff changeset
586 find_match_gcov_info (struct gcov_info **array, int size,
kono
parents:
diff changeset
587 struct gcov_info *info)
kono
parents:
diff changeset
588 {
kono
parents:
diff changeset
589 struct gcov_info *gi_ptr;
kono
parents:
diff changeset
590 struct gcov_info *ret = NULL;
kono
parents:
diff changeset
591 int i;
kono
parents:
diff changeset
592
kono
parents:
diff changeset
593 for (i = 0; i < size; i++)
kono
parents:
diff changeset
594 {
kono
parents:
diff changeset
595 gi_ptr = array[i];
kono
parents:
diff changeset
596 if (gi_ptr == 0)
kono
parents:
diff changeset
597 continue;
kono
parents:
diff changeset
598 if (!strcmp (gi_ptr->filename, info->filename))
kono
parents:
diff changeset
599 {
kono
parents:
diff changeset
600 ret = gi_ptr;
kono
parents:
diff changeset
601 array[i] = 0;
kono
parents:
diff changeset
602 break;
kono
parents:
diff changeset
603 }
kono
parents:
diff changeset
604 }
kono
parents:
diff changeset
605
kono
parents:
diff changeset
606 if (ret && ret->n_functions != info->n_functions)
kono
parents:
diff changeset
607 {
kono
parents:
diff changeset
608 fnotice (stderr, "mismatched profiles in %s (%d functions"
kono
parents:
diff changeset
609 " vs %d functions)\n",
kono
parents:
diff changeset
610 ret->filename,
kono
parents:
diff changeset
611 ret->n_functions,
kono
parents:
diff changeset
612 info->n_functions);
kono
parents:
diff changeset
613 ret = NULL;
kono
parents:
diff changeset
614 }
kono
parents:
diff changeset
615 return ret;
kono
parents:
diff changeset
616 }
kono
parents:
diff changeset
617
kono
parents:
diff changeset
618 /* Merge the list of gcov_info objects from SRC_PROFILE to TGT_PROFILE.
kono
parents:
diff changeset
619 Return 0 on success: without mismatch.
kono
parents:
diff changeset
620 Reutrn 1 on error. */
kono
parents:
diff changeset
621
kono
parents:
diff changeset
622 int
kono
parents:
diff changeset
623 gcov_profile_merge (struct gcov_info *tgt_profile, struct gcov_info *src_profile,
kono
parents:
diff changeset
624 int w1, int w2)
kono
parents:
diff changeset
625 {
kono
parents:
diff changeset
626 struct gcov_info *gi_ptr;
kono
parents:
diff changeset
627 struct gcov_info **tgt_infos;
kono
parents:
diff changeset
628 struct gcov_info *tgt_tail;
kono
parents:
diff changeset
629 struct gcov_info **in_src_not_tgt;
kono
parents:
diff changeset
630 unsigned tgt_cnt = 0, src_cnt = 0;
kono
parents:
diff changeset
631 unsigned unmatch_info_cnt = 0;
kono
parents:
diff changeset
632 unsigned int i;
kono
parents:
diff changeset
633
kono
parents:
diff changeset
634 for (gi_ptr = tgt_profile; gi_ptr; gi_ptr = gi_ptr->next)
kono
parents:
diff changeset
635 tgt_cnt++;
kono
parents:
diff changeset
636 for (gi_ptr = src_profile; gi_ptr; gi_ptr = gi_ptr->next)
kono
parents:
diff changeset
637 src_cnt++;
kono
parents:
diff changeset
638 tgt_infos = (struct gcov_info **) xmalloc (sizeof (struct gcov_info *)
kono
parents:
diff changeset
639 * tgt_cnt);
kono
parents:
diff changeset
640 gcc_assert (tgt_infos);
kono
parents:
diff changeset
641 in_src_not_tgt = (struct gcov_info **) xmalloc (sizeof (struct gcov_info *)
kono
parents:
diff changeset
642 * src_cnt);
kono
parents:
diff changeset
643 gcc_assert (in_src_not_tgt);
kono
parents:
diff changeset
644
kono
parents:
diff changeset
645 for (gi_ptr = tgt_profile, i = 0; gi_ptr; gi_ptr = gi_ptr->next, i++)
kono
parents:
diff changeset
646 tgt_infos[i] = gi_ptr;
kono
parents:
diff changeset
647
kono
parents:
diff changeset
648 tgt_tail = tgt_infos[tgt_cnt - 1];
kono
parents:
diff changeset
649
kono
parents:
diff changeset
650 /* First pass on tgt_profile, we multiply w1 to all counters. */
kono
parents:
diff changeset
651 if (w1 > 1)
kono
parents:
diff changeset
652 {
kono
parents:
diff changeset
653 for (i = 0; i < tgt_cnt; i++)
kono
parents:
diff changeset
654 gcov_merge (tgt_infos[i], tgt_infos[i], w1-1);
kono
parents:
diff changeset
655 }
kono
parents:
diff changeset
656
kono
parents:
diff changeset
657 /* Second pass, add src_profile to the tgt_profile. */
kono
parents:
diff changeset
658 for (gi_ptr = src_profile; gi_ptr; gi_ptr = gi_ptr->next)
kono
parents:
diff changeset
659 {
kono
parents:
diff changeset
660 struct gcov_info *gi_ptr1;
kono
parents:
diff changeset
661
kono
parents:
diff changeset
662 gi_ptr1 = find_match_gcov_info (tgt_infos, tgt_cnt, gi_ptr);
kono
parents:
diff changeset
663 if (gi_ptr1 == NULL)
kono
parents:
diff changeset
664 {
kono
parents:
diff changeset
665 in_src_not_tgt[unmatch_info_cnt++] = gi_ptr;
kono
parents:
diff changeset
666 continue;
kono
parents:
diff changeset
667 }
kono
parents:
diff changeset
668 gcov_merge (gi_ptr1, gi_ptr, w2);
kono
parents:
diff changeset
669 }
kono
parents:
diff changeset
670
kono
parents:
diff changeset
671 /* For modules in src but not in tgt. We adjust the counter and append. */
kono
parents:
diff changeset
672 for (i = 0; i < unmatch_info_cnt; i++)
kono
parents:
diff changeset
673 {
kono
parents:
diff changeset
674 gi_ptr = in_src_not_tgt[i];
kono
parents:
diff changeset
675 gcov_merge (gi_ptr, gi_ptr, w2 - 1);
kono
parents:
diff changeset
676 gi_ptr->next = NULL;
kono
parents:
diff changeset
677 tgt_tail->next = gi_ptr;
kono
parents:
diff changeset
678 tgt_tail = gi_ptr;
kono
parents:
diff changeset
679 }
kono
parents:
diff changeset
680
kono
parents:
diff changeset
681 return 0;
kono
parents:
diff changeset
682 }
kono
parents:
diff changeset
683
kono
parents:
diff changeset
684 typedef gcov_type (*counter_op_fn) (gcov_type, void*, void*);
kono
parents:
diff changeset
685
kono
parents:
diff changeset
686 /* Performing FN upon arc counters. */
kono
parents:
diff changeset
687
kono
parents:
diff changeset
688 static void
kono
parents:
diff changeset
689 __gcov_add_counter_op (gcov_type *counters, unsigned n_counters,
kono
parents:
diff changeset
690 counter_op_fn fn, void *data1, void *data2)
kono
parents:
diff changeset
691 {
kono
parents:
diff changeset
692 for (; n_counters; counters++, n_counters--)
kono
parents:
diff changeset
693 {
kono
parents:
diff changeset
694 gcov_type val = *counters;
kono
parents:
diff changeset
695 *counters = fn(val, data1, data2);
kono
parents:
diff changeset
696 }
kono
parents:
diff changeset
697 }
kono
parents:
diff changeset
698
kono
parents:
diff changeset
699 /* Performing FN upon ior counters. */
kono
parents:
diff changeset
700
kono
parents:
diff changeset
701 static void
kono
parents:
diff changeset
702 __gcov_ior_counter_op (gcov_type *counters ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
703 unsigned n_counters ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
704 counter_op_fn fn ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
705 void *data1 ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
706 void *data2 ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
707 {
kono
parents:
diff changeset
708 /* Do nothing. */
kono
parents:
diff changeset
709 }
kono
parents:
diff changeset
710
kono
parents:
diff changeset
711 /* Performing FN upon time-profile counters. */
kono
parents:
diff changeset
712
kono
parents:
diff changeset
713 static void
kono
parents:
diff changeset
714 __gcov_time_profile_counter_op (gcov_type *counters ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
715 unsigned n_counters ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
716 counter_op_fn fn ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
717 void *data1 ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
718 void *data2 ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
719 {
kono
parents:
diff changeset
720 /* Do nothing. */
kono
parents:
diff changeset
721 }
kono
parents:
diff changeset
722
kono
parents:
diff changeset
723 /* Performing FN upon single counters. */
kono
parents:
diff changeset
724
kono
parents:
diff changeset
725 static void
kono
parents:
diff changeset
726 __gcov_single_counter_op (gcov_type *counters, unsigned n_counters,
kono
parents:
diff changeset
727 counter_op_fn fn, void *data1, void *data2)
kono
parents:
diff changeset
728 {
kono
parents:
diff changeset
729 unsigned i, n_measures;
kono
parents:
diff changeset
730
kono
parents:
diff changeset
731 gcc_assert (!(n_counters % 3));
kono
parents:
diff changeset
732 n_measures = n_counters / 3;
kono
parents:
diff changeset
733 for (i = 0; i < n_measures; i++, counters += 3)
kono
parents:
diff changeset
734 {
kono
parents:
diff changeset
735 counters[1] = fn (counters[1], data1, data2);
kono
parents:
diff changeset
736 counters[2] = fn (counters[2], data1, data2);
kono
parents:
diff changeset
737 }
kono
parents:
diff changeset
738 }
kono
parents:
diff changeset
739
kono
parents:
diff changeset
740 /* Performing FN upon indirect-call profile counters. */
kono
parents:
diff changeset
741
kono
parents:
diff changeset
742 static void
kono
parents:
diff changeset
743 __gcov_icall_topn_counter_op (gcov_type *counters, unsigned n_counters,
kono
parents:
diff changeset
744 counter_op_fn fn, void *data1, void *data2)
kono
parents:
diff changeset
745 {
kono
parents:
diff changeset
746 unsigned i;
kono
parents:
diff changeset
747
kono
parents:
diff changeset
748 gcc_assert (!(n_counters % GCOV_ICALL_TOPN_NCOUNTS));
kono
parents:
diff changeset
749 for (i = 0; i < n_counters; i += GCOV_ICALL_TOPN_NCOUNTS)
kono
parents:
diff changeset
750 {
kono
parents:
diff changeset
751 unsigned j;
kono
parents:
diff changeset
752 gcov_type *value_array = &counters[i + 1];
kono
parents:
diff changeset
753
kono
parents:
diff changeset
754 for (j = 0; j < GCOV_ICALL_TOPN_NCOUNTS - 1; j += 2)
kono
parents:
diff changeset
755 value_array[j + 1] = fn (value_array[j + 1], data1, data2);
kono
parents:
diff changeset
756 }
kono
parents:
diff changeset
757 }
kono
parents:
diff changeset
758
kono
parents:
diff changeset
759 /* Scaling the counter value V by multiplying *(float*) DATA1. */
kono
parents:
diff changeset
760
kono
parents:
diff changeset
761 static gcov_type
kono
parents:
diff changeset
762 fp_scale (gcov_type v, void *data1, void *data2 ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
763 {
kono
parents:
diff changeset
764 float f = *(float *) data1;
kono
parents:
diff changeset
765 return (gcov_type) (v * f);
kono
parents:
diff changeset
766 }
kono
parents:
diff changeset
767
kono
parents:
diff changeset
768 /* Scaling the counter value V by multiplying DATA2/DATA1. */
kono
parents:
diff changeset
769
kono
parents:
diff changeset
770 static gcov_type
kono
parents:
diff changeset
771 int_scale (gcov_type v, void *data1, void *data2)
kono
parents:
diff changeset
772 {
kono
parents:
diff changeset
773 int n = *(int *) data1;
kono
parents:
diff changeset
774 int d = *(int *) data2;
kono
parents:
diff changeset
775 return (gcov_type) ( RDIV (v,d) * n);
kono
parents:
diff changeset
776 }
kono
parents:
diff changeset
777
kono
parents:
diff changeset
778 /* Type of function used to process counters. */
kono
parents:
diff changeset
779 typedef void (*gcov_counter_fn) (gcov_type *, gcov_unsigned_t,
kono
parents:
diff changeset
780 counter_op_fn, void *, void *);
kono
parents:
diff changeset
781
kono
parents:
diff changeset
782 /* Function array to process profile counters. */
kono
parents:
diff changeset
783 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) \
kono
parents:
diff changeset
784 __gcov ## FN_TYPE ## _counter_op,
kono
parents:
diff changeset
785 static gcov_counter_fn ctr_functions[GCOV_COUNTERS] = {
kono
parents:
diff changeset
786 #include "gcov-counter.def"
kono
parents:
diff changeset
787 };
kono
parents:
diff changeset
788 #undef DEF_GCOV_COUNTER
kono
parents:
diff changeset
789
kono
parents:
diff changeset
790 /* Driver for scaling profile counters. */
kono
parents:
diff changeset
791
kono
parents:
diff changeset
792 int
kono
parents:
diff changeset
793 gcov_profile_scale (struct gcov_info *profile, float scale_factor, int n, int d)
kono
parents:
diff changeset
794 {
kono
parents:
diff changeset
795 struct gcov_info *gi_ptr;
kono
parents:
diff changeset
796 unsigned f_ix;
kono
parents:
diff changeset
797
kono
parents:
diff changeset
798 if (verbose)
kono
parents:
diff changeset
799 fnotice (stdout, "scale_factor is %f or %d/%d\n", scale_factor, n, d);
kono
parents:
diff changeset
800
kono
parents:
diff changeset
801 /* Scaling the counters. */
kono
parents:
diff changeset
802 for (gi_ptr = profile; gi_ptr; gi_ptr = gi_ptr->next)
kono
parents:
diff changeset
803 for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
kono
parents:
diff changeset
804 {
kono
parents:
diff changeset
805 unsigned t_ix;
kono
parents:
diff changeset
806 const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
kono
parents:
diff changeset
807 const struct gcov_ctr_info *ci_ptr;
kono
parents:
diff changeset
808
kono
parents:
diff changeset
809 if (!gfi_ptr || gfi_ptr->key != gi_ptr)
kono
parents:
diff changeset
810 continue;
kono
parents:
diff changeset
811
kono
parents:
diff changeset
812 ci_ptr = gfi_ptr->ctrs;
kono
parents:
diff changeset
813 for (t_ix = 0; t_ix != GCOV_COUNTERS; t_ix++)
kono
parents:
diff changeset
814 {
kono
parents:
diff changeset
815 gcov_merge_fn merge = gi_ptr->merge[t_ix];
kono
parents:
diff changeset
816
kono
parents:
diff changeset
817 if (!merge)
kono
parents:
diff changeset
818 continue;
kono
parents:
diff changeset
819 if (d == 0)
kono
parents:
diff changeset
820 (*ctr_functions[t_ix]) (ci_ptr->values, ci_ptr->num,
kono
parents:
diff changeset
821 fp_scale, &scale_factor, NULL);
kono
parents:
diff changeset
822 else
kono
parents:
diff changeset
823 (*ctr_functions[t_ix]) (ci_ptr->values, ci_ptr->num,
kono
parents:
diff changeset
824 int_scale, &n, &d);
kono
parents:
diff changeset
825 ci_ptr++;
kono
parents:
diff changeset
826 }
kono
parents:
diff changeset
827 }
kono
parents:
diff changeset
828
kono
parents:
diff changeset
829 return 0;
kono
parents:
diff changeset
830 }
kono
parents:
diff changeset
831
kono
parents:
diff changeset
832 /* Driver to normalize profile counters. */
kono
parents:
diff changeset
833
kono
parents:
diff changeset
834 int
kono
parents:
diff changeset
835 gcov_profile_normalize (struct gcov_info *profile, gcov_type max_val)
kono
parents:
diff changeset
836 {
kono
parents:
diff changeset
837 struct gcov_info *gi_ptr;
kono
parents:
diff changeset
838 gcov_type curr_max_val = 0;
kono
parents:
diff changeset
839 unsigned f_ix;
kono
parents:
diff changeset
840 unsigned int i;
kono
parents:
diff changeset
841 float scale_factor;
kono
parents:
diff changeset
842
kono
parents:
diff changeset
843 /* Find the largest count value. */
kono
parents:
diff changeset
844 for (gi_ptr = profile; gi_ptr; gi_ptr = gi_ptr->next)
kono
parents:
diff changeset
845 for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
kono
parents:
diff changeset
846 {
kono
parents:
diff changeset
847 unsigned t_ix;
kono
parents:
diff changeset
848 const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
kono
parents:
diff changeset
849 const struct gcov_ctr_info *ci_ptr;
kono
parents:
diff changeset
850
kono
parents:
diff changeset
851 if (!gfi_ptr || gfi_ptr->key != gi_ptr)
kono
parents:
diff changeset
852 continue;
kono
parents:
diff changeset
853
kono
parents:
diff changeset
854 ci_ptr = gfi_ptr->ctrs;
kono
parents:
diff changeset
855 for (t_ix = 0; t_ix < 1; t_ix++)
kono
parents:
diff changeset
856 {
kono
parents:
diff changeset
857 for (i = 0; i < ci_ptr->num; i++)
kono
parents:
diff changeset
858 if (ci_ptr->values[i] > curr_max_val)
kono
parents:
diff changeset
859 curr_max_val = ci_ptr->values[i];
kono
parents:
diff changeset
860 ci_ptr++;
kono
parents:
diff changeset
861 }
kono
parents:
diff changeset
862 }
kono
parents:
diff changeset
863
kono
parents:
diff changeset
864 scale_factor = (float)max_val / curr_max_val;
kono
parents:
diff changeset
865 if (verbose)
kono
parents:
diff changeset
866 fnotice (stdout, "max_val is %" PRId64 "\n", curr_max_val);
kono
parents:
diff changeset
867
kono
parents:
diff changeset
868 return gcov_profile_scale (profile, scale_factor, 0, 0);
kono
parents:
diff changeset
869 }
kono
parents:
diff changeset
870
kono
parents:
diff changeset
871 /* The following variables are defined in gcc/gcov-tool.c. */
kono
parents:
diff changeset
872 extern int overlap_func_level;
kono
parents:
diff changeset
873 extern int overlap_obj_level;
kono
parents:
diff changeset
874 extern int overlap_hot_only;
kono
parents:
diff changeset
875 extern int overlap_use_fullname;
kono
parents:
diff changeset
876 extern double overlap_hot_threshold;
kono
parents:
diff changeset
877
kono
parents:
diff changeset
878 /* Compute the overlap score of two values. The score is defined as:
kono
parents:
diff changeset
879 min (V1/SUM_1, V2/SUM_2) */
kono
parents:
diff changeset
880
kono
parents:
diff changeset
881 static double
kono
parents:
diff changeset
882 calculate_2_entries (const unsigned long v1, const unsigned long v2,
kono
parents:
diff changeset
883 const double sum_1, const double sum_2)
kono
parents:
diff changeset
884 {
kono
parents:
diff changeset
885 double val1 = (sum_1 == 0.0 ? 0.0 : v1/sum_1);
kono
parents:
diff changeset
886 double val2 = (sum_2 == 0.0 ? 0.0 : v2/sum_2);
kono
parents:
diff changeset
887
kono
parents:
diff changeset
888 if (val2 < val1)
kono
parents:
diff changeset
889 val1 = val2;
kono
parents:
diff changeset
890
kono
parents:
diff changeset
891 return val1;
kono
parents:
diff changeset
892 }
kono
parents:
diff changeset
893
kono
parents:
diff changeset
894 /* Compute the overlap score between GCOV_INFO1 and GCOV_INFO2.
kono
parents:
diff changeset
895 SUM_1 is the sum_all for profile1 where GCOV_INFO1 belongs.
kono
parents:
diff changeset
896 SUM_2 is the sum_all for profile2 where GCOV_INFO2 belongs.
kono
parents:
diff changeset
897 This function also updates cumulative score CUM_1_RESULT and
kono
parents:
diff changeset
898 CUM_2_RESULT. */
kono
parents:
diff changeset
899
kono
parents:
diff changeset
900 static double
kono
parents:
diff changeset
901 compute_one_gcov (const struct gcov_info *gcov_info1,
kono
parents:
diff changeset
902 const struct gcov_info *gcov_info2,
kono
parents:
diff changeset
903 const double sum_1, const double sum_2,
kono
parents:
diff changeset
904 double *cum_1_result, double *cum_2_result)
kono
parents:
diff changeset
905 {
kono
parents:
diff changeset
906 unsigned f_ix;
kono
parents:
diff changeset
907 double ret = 0;
kono
parents:
diff changeset
908 double cum_1 = 0, cum_2 = 0;
kono
parents:
diff changeset
909 const struct gcov_info *gcov_info = 0;
kono
parents:
diff changeset
910 double *cum_p;
kono
parents:
diff changeset
911 double sum;
kono
parents:
diff changeset
912
kono
parents:
diff changeset
913 gcc_assert (gcov_info1 || gcov_info2);
kono
parents:
diff changeset
914 if (!gcov_info1)
kono
parents:
diff changeset
915 {
kono
parents:
diff changeset
916 gcov_info = gcov_info2;
kono
parents:
diff changeset
917 cum_p = cum_2_result;
kono
parents:
diff changeset
918 sum = sum_2;
kono
parents:
diff changeset
919 *cum_1_result = 0;
kono
parents:
diff changeset
920 } else
kono
parents:
diff changeset
921 if (!gcov_info2)
kono
parents:
diff changeset
922 {
kono
parents:
diff changeset
923 gcov_info = gcov_info1;
kono
parents:
diff changeset
924 cum_p = cum_1_result;
kono
parents:
diff changeset
925 sum = sum_1;
kono
parents:
diff changeset
926 *cum_2_result = 0;
kono
parents:
diff changeset
927 }
kono
parents:
diff changeset
928
kono
parents:
diff changeset
929 if (gcov_info)
kono
parents:
diff changeset
930 {
kono
parents:
diff changeset
931 for (f_ix = 0; f_ix < gcov_info->n_functions; f_ix++)
kono
parents:
diff changeset
932 {
kono
parents:
diff changeset
933 unsigned t_ix;
kono
parents:
diff changeset
934 const struct gcov_fn_info *gfi_ptr = gcov_info->functions[f_ix];
kono
parents:
diff changeset
935 if (!gfi_ptr || gfi_ptr->key != gcov_info)
kono
parents:
diff changeset
936 continue;
kono
parents:
diff changeset
937 const struct gcov_ctr_info *ci_ptr = gfi_ptr->ctrs;
kono
parents:
diff changeset
938 for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
kono
parents:
diff changeset
939 {
kono
parents:
diff changeset
940 unsigned c_num;
kono
parents:
diff changeset
941
kono
parents:
diff changeset
942 if (!gcov_info->merge[t_ix])
kono
parents:
diff changeset
943 continue;
kono
parents:
diff changeset
944
kono
parents:
diff changeset
945 for (c_num = 0; c_num < ci_ptr->num; c_num++)
kono
parents:
diff changeset
946 {
kono
parents:
diff changeset
947 cum_1 += ci_ptr->values[c_num] / sum;
kono
parents:
diff changeset
948 }
kono
parents:
diff changeset
949 ci_ptr++;
kono
parents:
diff changeset
950 }
kono
parents:
diff changeset
951 }
kono
parents:
diff changeset
952 *cum_p = cum_1;
kono
parents:
diff changeset
953 return 0.0;
kono
parents:
diff changeset
954 }
kono
parents:
diff changeset
955
kono
parents:
diff changeset
956 for (f_ix = 0; f_ix < gcov_info1->n_functions; f_ix++)
kono
parents:
diff changeset
957 {
kono
parents:
diff changeset
958 unsigned t_ix;
kono
parents:
diff changeset
959 double func_cum_1 = 0.0;
kono
parents:
diff changeset
960 double func_cum_2 = 0.0;
kono
parents:
diff changeset
961 double func_val = 0.0;
kono
parents:
diff changeset
962 int nonzero = 0;
kono
parents:
diff changeset
963 int hot = 0;
kono
parents:
diff changeset
964 const struct gcov_fn_info *gfi_ptr1 = gcov_info1->functions[f_ix];
kono
parents:
diff changeset
965 const struct gcov_fn_info *gfi_ptr2 = gcov_info2->functions[f_ix];
kono
parents:
diff changeset
966
kono
parents:
diff changeset
967 if (!gfi_ptr1 || gfi_ptr1->key != gcov_info1)
kono
parents:
diff changeset
968 continue;
kono
parents:
diff changeset
969 if (!gfi_ptr2 || gfi_ptr2->key != gcov_info2)
kono
parents:
diff changeset
970 continue;
kono
parents:
diff changeset
971
kono
parents:
diff changeset
972 const struct gcov_ctr_info *ci_ptr1 = gfi_ptr1->ctrs;
kono
parents:
diff changeset
973 const struct gcov_ctr_info *ci_ptr2 = gfi_ptr2->ctrs;
kono
parents:
diff changeset
974 for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
kono
parents:
diff changeset
975 {
kono
parents:
diff changeset
976 unsigned c_num;
kono
parents:
diff changeset
977
kono
parents:
diff changeset
978 if (!gcov_info1->merge[t_ix])
kono
parents:
diff changeset
979 continue;
kono
parents:
diff changeset
980
kono
parents:
diff changeset
981 for (c_num = 0; c_num < ci_ptr1->num; c_num++)
kono
parents:
diff changeset
982 {
kono
parents:
diff changeset
983 if (ci_ptr1->values[c_num] | ci_ptr2->values[c_num])
kono
parents:
diff changeset
984 {
kono
parents:
diff changeset
985 func_val += calculate_2_entries (ci_ptr1->values[c_num],
kono
parents:
diff changeset
986 ci_ptr2->values[c_num],
kono
parents:
diff changeset
987 sum_1, sum_2);
kono
parents:
diff changeset
988
kono
parents:
diff changeset
989 func_cum_1 += ci_ptr1->values[c_num] / sum_1;
kono
parents:
diff changeset
990 func_cum_2 += ci_ptr2->values[c_num] / sum_2;
kono
parents:
diff changeset
991 nonzero = 1;
kono
parents:
diff changeset
992 if (ci_ptr1->values[c_num] / sum_1 >= overlap_hot_threshold ||
kono
parents:
diff changeset
993 ci_ptr2->values[c_num] / sum_2 >= overlap_hot_threshold)
kono
parents:
diff changeset
994 hot = 1;
kono
parents:
diff changeset
995 }
kono
parents:
diff changeset
996 }
kono
parents:
diff changeset
997 ci_ptr1++;
kono
parents:
diff changeset
998 ci_ptr2++;
kono
parents:
diff changeset
999 }
kono
parents:
diff changeset
1000 ret += func_val;
kono
parents:
diff changeset
1001 cum_1 += func_cum_1;
kono
parents:
diff changeset
1002 cum_2 += func_cum_2;
kono
parents:
diff changeset
1003 if (overlap_func_level && nonzero && (!overlap_hot_only || hot))
kono
parents:
diff changeset
1004 {
kono
parents:
diff changeset
1005 printf(" \tfunc_id=%10d \toverlap =%6.5f%% (%5.5f%% %5.5f%%)\n",
kono
parents:
diff changeset
1006 gfi_ptr1->ident, func_val*100, func_cum_1*100, func_cum_2*100);
kono
parents:
diff changeset
1007 }
kono
parents:
diff changeset
1008 }
kono
parents:
diff changeset
1009 *cum_1_result = cum_1;
kono
parents:
diff changeset
1010 *cum_2_result = cum_2;
kono
parents:
diff changeset
1011 return ret;
kono
parents:
diff changeset
1012 }
kono
parents:
diff changeset
1013
kono
parents:
diff changeset
1014 /* Test if all counter values in this GCOV_INFO are cold.
kono
parents:
diff changeset
1015 "Cold" is defined as the counter value being less than
kono
parents:
diff changeset
1016 or equal to THRESHOLD. */
kono
parents:
diff changeset
1017
kono
parents:
diff changeset
1018 static bool
kono
parents:
diff changeset
1019 gcov_info_count_all_cold (const struct gcov_info *gcov_info,
kono
parents:
diff changeset
1020 gcov_type threshold)
kono
parents:
diff changeset
1021 {
kono
parents:
diff changeset
1022 unsigned f_ix;
kono
parents:
diff changeset
1023
kono
parents:
diff changeset
1024 for (f_ix = 0; f_ix < gcov_info->n_functions; f_ix++)
kono
parents:
diff changeset
1025 {
kono
parents:
diff changeset
1026 unsigned t_ix;
kono
parents:
diff changeset
1027 const struct gcov_fn_info *gfi_ptr = gcov_info->functions[f_ix];
kono
parents:
diff changeset
1028
kono
parents:
diff changeset
1029 if (!gfi_ptr || gfi_ptr->key != gcov_info)
kono
parents:
diff changeset
1030 continue;
kono
parents:
diff changeset
1031 const struct gcov_ctr_info *ci_ptr = gfi_ptr->ctrs;
kono
parents:
diff changeset
1032 for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
kono
parents:
diff changeset
1033 {
kono
parents:
diff changeset
1034 unsigned c_num;
kono
parents:
diff changeset
1035
kono
parents:
diff changeset
1036 if (!gcov_info->merge[t_ix])
kono
parents:
diff changeset
1037 continue;
kono
parents:
diff changeset
1038
kono
parents:
diff changeset
1039 for (c_num = 0; c_num < ci_ptr->num; c_num++)
kono
parents:
diff changeset
1040 {
kono
parents:
diff changeset
1041 if (ci_ptr->values[c_num] > threshold)
kono
parents:
diff changeset
1042 return false;
kono
parents:
diff changeset
1043 }
kono
parents:
diff changeset
1044 ci_ptr++;
kono
parents:
diff changeset
1045 }
kono
parents:
diff changeset
1046 }
kono
parents:
diff changeset
1047
kono
parents:
diff changeset
1048 return true;
kono
parents:
diff changeset
1049 }
kono
parents:
diff changeset
1050
kono
parents:
diff changeset
1051 /* Test if all counter values in this GCOV_INFO are 0. */
kono
parents:
diff changeset
1052
kono
parents:
diff changeset
1053 static bool
kono
parents:
diff changeset
1054 gcov_info_count_all_zero (const struct gcov_info *gcov_info)
kono
parents:
diff changeset
1055 {
kono
parents:
diff changeset
1056 return gcov_info_count_all_cold (gcov_info, 0);
kono
parents:
diff changeset
1057 }
kono
parents:
diff changeset
1058
kono
parents:
diff changeset
1059 /* A pair of matched GCOV_INFO.
kono
parents:
diff changeset
1060 The flag is a bitvector:
kono
parents:
diff changeset
1061 b0: obj1's all counts are 0;
kono
parents:
diff changeset
1062 b1: obj1's all counts are cold (but no 0);
kono
parents:
diff changeset
1063 b2: obj1 is hot;
kono
parents:
diff changeset
1064 b3: no obj1 to match obj2;
kono
parents:
diff changeset
1065 b4: obj2's all counts are 0;
kono
parents:
diff changeset
1066 b5: obj2's all counts are cold (but no 0);
kono
parents:
diff changeset
1067 b6: obj2 is hot;
kono
parents:
diff changeset
1068 b7: no obj2 to match obj1;
kono
parents:
diff changeset
1069 */
kono
parents:
diff changeset
1070 struct overlap_t {
kono
parents:
diff changeset
1071 const struct gcov_info *obj1;
kono
parents:
diff changeset
1072 const struct gcov_info *obj2;
kono
parents:
diff changeset
1073 char flag;
kono
parents:
diff changeset
1074 };
kono
parents:
diff changeset
1075
kono
parents:
diff changeset
1076 #define FLAG_BOTH_ZERO(flag) ((flag & 0x1) && (flag & 0x10))
kono
parents:
diff changeset
1077 #define FLAG_BOTH_COLD(flag) ((flag & 0x2) && (flag & 0x20))
kono
parents:
diff changeset
1078 #define FLAG_ONE_HOT(flag) ((flag & 0x4) || (flag & 0x40))
kono
parents:
diff changeset
1079
kono
parents:
diff changeset
1080 /* Cumlative overlap dscore for profile1 and profile2. */
kono
parents:
diff changeset
1081 static double overlap_sum_1, overlap_sum_2;
kono
parents:
diff changeset
1082
kono
parents:
diff changeset
1083 /* sum_all for profile1 and profile2. */
kono
parents:
diff changeset
1084 static gcov_type p1_sum_all, p2_sum_all;
kono
parents:
diff changeset
1085
kono
parents:
diff changeset
1086 /* run_max for profile1 and profile2. */
kono
parents:
diff changeset
1087 static gcov_type p1_run_max, p2_run_max;
kono
parents:
diff changeset
1088
kono
parents:
diff changeset
1089 /* The number of gcda files in the profiles. */
kono
parents:
diff changeset
1090 static unsigned gcda_files[2];
kono
parents:
diff changeset
1091
kono
parents:
diff changeset
1092 /* The number of unique gcda files in the profiles
kono
parents:
diff changeset
1093 (not existing in the other profile). */
kono
parents:
diff changeset
1094 static unsigned unique_gcda_files[2];
kono
parents:
diff changeset
1095
kono
parents:
diff changeset
1096 /* The number of gcda files that all counter values are 0. */
kono
parents:
diff changeset
1097 static unsigned zero_gcda_files[2];
kono
parents:
diff changeset
1098
kono
parents:
diff changeset
1099 /* The number of gcda files that all counter values are cold (but not 0). */
kono
parents:
diff changeset
1100 static unsigned cold_gcda_files[2];
kono
parents:
diff changeset
1101
kono
parents:
diff changeset
1102 /* The number of gcda files that includes hot counter values. */
kono
parents:
diff changeset
1103 static unsigned hot_gcda_files[2];
kono
parents:
diff changeset
1104
kono
parents:
diff changeset
1105 /* The number of gcda files with hot count value in either profiles. */
kono
parents:
diff changeset
1106 static unsigned both_hot_cnt;
kono
parents:
diff changeset
1107
kono
parents:
diff changeset
1108 /* The number of gcda files with all counts cold (but not 0) in
kono
parents:
diff changeset
1109 both profiles. */
kono
parents:
diff changeset
1110 static unsigned both_cold_cnt;
kono
parents:
diff changeset
1111
kono
parents:
diff changeset
1112 /* The number of gcda files with all counts 0 in both profiles. */
kono
parents:
diff changeset
1113 static unsigned both_zero_cnt;
kono
parents:
diff changeset
1114
kono
parents:
diff changeset
1115 /* Extract the basename of the filename NAME. */
kono
parents:
diff changeset
1116
kono
parents:
diff changeset
1117 static char *
kono
parents:
diff changeset
1118 extract_file_basename (const char *name)
kono
parents:
diff changeset
1119 {
kono
parents:
diff changeset
1120 char *str;
kono
parents:
diff changeset
1121 int len = 0;
kono
parents:
diff changeset
1122 char *path = xstrdup (name);
kono
parents:
diff changeset
1123 char sep_str[2];
kono
parents:
diff changeset
1124
kono
parents:
diff changeset
1125 sep_str[0] = DIR_SEPARATOR;
kono
parents:
diff changeset
1126 sep_str[1] = 0;
kono
parents:
diff changeset
1127 str = strstr(path, sep_str);
kono
parents:
diff changeset
1128 do{
kono
parents:
diff changeset
1129 len = strlen(str) + 1;
kono
parents:
diff changeset
1130 path = &path[strlen(path) - len + 2];
kono
parents:
diff changeset
1131 str = strstr(path, sep_str);
kono
parents:
diff changeset
1132 } while(str);
kono
parents:
diff changeset
1133
kono
parents:
diff changeset
1134 return path;
kono
parents:
diff changeset
1135 }
kono
parents:
diff changeset
1136
kono
parents:
diff changeset
1137 /* Utility function to get the filename. */
kono
parents:
diff changeset
1138
kono
parents:
diff changeset
1139 static const char *
kono
parents:
diff changeset
1140 get_file_basename (const char *name)
kono
parents:
diff changeset
1141 {
kono
parents:
diff changeset
1142 if (overlap_use_fullname)
kono
parents:
diff changeset
1143 return name;
kono
parents:
diff changeset
1144 return extract_file_basename (name);
kono
parents:
diff changeset
1145 }
kono
parents:
diff changeset
1146
kono
parents:
diff changeset
1147 /* A utility function to set the flag for the gcda files. */
kono
parents:
diff changeset
1148
kono
parents:
diff changeset
1149 static void
kono
parents:
diff changeset
1150 set_flag (struct overlap_t *e)
kono
parents:
diff changeset
1151 {
kono
parents:
diff changeset
1152 char flag = 0;
kono
parents:
diff changeset
1153
kono
parents:
diff changeset
1154 if (!e->obj1)
kono
parents:
diff changeset
1155 {
kono
parents:
diff changeset
1156 unique_gcda_files[1]++;
kono
parents:
diff changeset
1157 flag = 0x8;
kono
parents:
diff changeset
1158 }
kono
parents:
diff changeset
1159 else
kono
parents:
diff changeset
1160 {
kono
parents:
diff changeset
1161 gcda_files[0]++;
kono
parents:
diff changeset
1162 if (gcov_info_count_all_zero (e->obj1))
kono
parents:
diff changeset
1163 {
kono
parents:
diff changeset
1164 zero_gcda_files[0]++;
kono
parents:
diff changeset
1165 flag = 0x1;
kono
parents:
diff changeset
1166 }
kono
parents:
diff changeset
1167 else
kono
parents:
diff changeset
1168 if (gcov_info_count_all_cold (e->obj1, overlap_sum_1
kono
parents:
diff changeset
1169 * overlap_hot_threshold))
kono
parents:
diff changeset
1170 {
kono
parents:
diff changeset
1171 cold_gcda_files[0]++;
kono
parents:
diff changeset
1172 flag = 0x2;
kono
parents:
diff changeset
1173 }
kono
parents:
diff changeset
1174 else
kono
parents:
diff changeset
1175 {
kono
parents:
diff changeset
1176 hot_gcda_files[0]++;
kono
parents:
diff changeset
1177 flag = 0x4;
kono
parents:
diff changeset
1178 }
kono
parents:
diff changeset
1179 }
kono
parents:
diff changeset
1180
kono
parents:
diff changeset
1181 if (!e->obj2)
kono
parents:
diff changeset
1182 {
kono
parents:
diff changeset
1183 unique_gcda_files[0]++;
kono
parents:
diff changeset
1184 flag |= (0x8 << 4);
kono
parents:
diff changeset
1185 }
kono
parents:
diff changeset
1186 else
kono
parents:
diff changeset
1187 {
kono
parents:
diff changeset
1188 gcda_files[1]++;
kono
parents:
diff changeset
1189 if (gcov_info_count_all_zero (e->obj2))
kono
parents:
diff changeset
1190 {
kono
parents:
diff changeset
1191 zero_gcda_files[1]++;
kono
parents:
diff changeset
1192 flag |= (0x1 << 4);
kono
parents:
diff changeset
1193 }
kono
parents:
diff changeset
1194 else
kono
parents:
diff changeset
1195 if (gcov_info_count_all_cold (e->obj2, overlap_sum_2
kono
parents:
diff changeset
1196 * overlap_hot_threshold))
kono
parents:
diff changeset
1197 {
kono
parents:
diff changeset
1198 cold_gcda_files[1]++;
kono
parents:
diff changeset
1199 flag |= (0x2 << 4);
kono
parents:
diff changeset
1200 }
kono
parents:
diff changeset
1201 else
kono
parents:
diff changeset
1202 {
kono
parents:
diff changeset
1203 hot_gcda_files[1]++;
kono
parents:
diff changeset
1204 flag |= (0x4 << 4);
kono
parents:
diff changeset
1205 }
kono
parents:
diff changeset
1206 }
kono
parents:
diff changeset
1207
kono
parents:
diff changeset
1208 gcc_assert (flag);
kono
parents:
diff changeset
1209 e->flag = flag;
kono
parents:
diff changeset
1210 }
kono
parents:
diff changeset
1211
kono
parents:
diff changeset
1212 /* Test if INFO1 and INFO2 are from the matched source file.
kono
parents:
diff changeset
1213 Return 1 if they match; return 0 otherwise. */
kono
parents:
diff changeset
1214
kono
parents:
diff changeset
1215 static int
kono
parents:
diff changeset
1216 matched_gcov_info (const struct gcov_info *info1, const struct gcov_info *info2)
kono
parents:
diff changeset
1217 {
kono
parents:
diff changeset
1218 /* For FDO, we have to match the name. This can be expensive.
kono
parents:
diff changeset
1219 Maybe we should use hash here. */
kono
parents:
diff changeset
1220 if (strcmp (info1->filename, info2->filename))
kono
parents:
diff changeset
1221 return 0;
kono
parents:
diff changeset
1222
kono
parents:
diff changeset
1223 if (info1->n_functions != info2->n_functions)
kono
parents:
diff changeset
1224 {
kono
parents:
diff changeset
1225 fnotice (stderr, "mismatched profiles in %s (%d functions"
kono
parents:
diff changeset
1226 " vs %d functions)\n",
kono
parents:
diff changeset
1227 info1->filename,
kono
parents:
diff changeset
1228 info1->n_functions,
kono
parents:
diff changeset
1229 info2->n_functions);
kono
parents:
diff changeset
1230 return 0;
kono
parents:
diff changeset
1231 }
kono
parents:
diff changeset
1232 return 1;
kono
parents:
diff changeset
1233 }
kono
parents:
diff changeset
1234
kono
parents:
diff changeset
1235 /* Defined in libgcov-driver.c. */
kono
parents:
diff changeset
1236 extern gcov_unsigned_t compute_summary (struct gcov_info *,
kono
parents:
diff changeset
1237 struct gcov_summary *, size_t *);
kono
parents:
diff changeset
1238
kono
parents:
diff changeset
1239 /* Compute the overlap score of two profiles with the head of GCOV_LIST1 and
kono
parents:
diff changeset
1240 GCOV_LIST1. Return a number ranging from [0.0, 1.0], with 0.0 meaning no
kono
parents:
diff changeset
1241 match and 1.0 meaning a perfect match. */
kono
parents:
diff changeset
1242
kono
parents:
diff changeset
1243 static double
kono
parents:
diff changeset
1244 calculate_overlap (struct gcov_info *gcov_list1,
kono
parents:
diff changeset
1245 struct gcov_info *gcov_list2)
kono
parents:
diff changeset
1246 {
kono
parents:
diff changeset
1247 struct gcov_summary this_prg;
kono
parents:
diff changeset
1248 unsigned list1_cnt = 0, list2_cnt= 0, all_cnt;
kono
parents:
diff changeset
1249 unsigned int i, j;
kono
parents:
diff changeset
1250 size_t max_length;
kono
parents:
diff changeset
1251 const struct gcov_info *gi_ptr;
kono
parents:
diff changeset
1252 struct overlap_t *all_infos;
kono
parents:
diff changeset
1253
kono
parents:
diff changeset
1254 compute_summary (gcov_list1, &this_prg, &max_length);
kono
parents:
diff changeset
1255 overlap_sum_1 = (double) (this_prg.ctrs[0].sum_all);
kono
parents:
diff changeset
1256 p1_sum_all = this_prg.ctrs[0].sum_all;
kono
parents:
diff changeset
1257 p1_run_max = this_prg.ctrs[0].run_max;
kono
parents:
diff changeset
1258 compute_summary (gcov_list2, &this_prg, &max_length);
kono
parents:
diff changeset
1259 overlap_sum_2 = (double) (this_prg.ctrs[0].sum_all);
kono
parents:
diff changeset
1260 p2_sum_all = this_prg.ctrs[0].sum_all;
kono
parents:
diff changeset
1261 p2_run_max = this_prg.ctrs[0].run_max;
kono
parents:
diff changeset
1262
kono
parents:
diff changeset
1263 for (gi_ptr = gcov_list1; gi_ptr; gi_ptr = gi_ptr->next)
kono
parents:
diff changeset
1264 list1_cnt++;
kono
parents:
diff changeset
1265 for (gi_ptr = gcov_list2; gi_ptr; gi_ptr = gi_ptr->next)
kono
parents:
diff changeset
1266 list2_cnt++;
kono
parents:
diff changeset
1267 all_cnt = list1_cnt + list2_cnt;
kono
parents:
diff changeset
1268 all_infos = (struct overlap_t *) xmalloc (sizeof (struct overlap_t)
kono
parents:
diff changeset
1269 * all_cnt * 2);
kono
parents:
diff changeset
1270 gcc_assert (all_infos);
kono
parents:
diff changeset
1271
kono
parents:
diff changeset
1272 i = 0;
kono
parents:
diff changeset
1273 for (gi_ptr = gcov_list1; gi_ptr; gi_ptr = gi_ptr->next, i++)
kono
parents:
diff changeset
1274 {
kono
parents:
diff changeset
1275 all_infos[i].obj1 = gi_ptr;
kono
parents:
diff changeset
1276 all_infos[i].obj2 = 0;
kono
parents:
diff changeset
1277 }
kono
parents:
diff changeset
1278
kono
parents:
diff changeset
1279 for (gi_ptr = gcov_list2; gi_ptr; gi_ptr = gi_ptr->next, i++)
kono
parents:
diff changeset
1280 {
kono
parents:
diff changeset
1281 all_infos[i].obj1 = 0;
kono
parents:
diff changeset
1282 all_infos[i].obj2 = gi_ptr;
kono
parents:
diff changeset
1283 }
kono
parents:
diff changeset
1284
kono
parents:
diff changeset
1285 for (i = list1_cnt; i < all_cnt; i++)
kono
parents:
diff changeset
1286 {
kono
parents:
diff changeset
1287 if (all_infos[i].obj2 == 0)
kono
parents:
diff changeset
1288 continue;
kono
parents:
diff changeset
1289 for (j = 0; j < list1_cnt; j++)
kono
parents:
diff changeset
1290 {
kono
parents:
diff changeset
1291 if (all_infos[j].obj2 != 0)
kono
parents:
diff changeset
1292 continue;
kono
parents:
diff changeset
1293 if (matched_gcov_info (all_infos[i].obj2, all_infos[j].obj1))
kono
parents:
diff changeset
1294 {
kono
parents:
diff changeset
1295 all_infos[j].obj2 = all_infos[i].obj2;
kono
parents:
diff changeset
1296 all_infos[i].obj2 = 0;
kono
parents:
diff changeset
1297 break;
kono
parents:
diff changeset
1298 }
kono
parents:
diff changeset
1299 }
kono
parents:
diff changeset
1300 }
kono
parents:
diff changeset
1301
kono
parents:
diff changeset
1302 for (i = 0; i < all_cnt; i++)
kono
parents:
diff changeset
1303 if (all_infos[i].obj1 || all_infos[i].obj2)
kono
parents:
diff changeset
1304 {
kono
parents:
diff changeset
1305 set_flag (all_infos + i);
kono
parents:
diff changeset
1306 if (FLAG_ONE_HOT (all_infos[i].flag))
kono
parents:
diff changeset
1307 both_hot_cnt++;
kono
parents:
diff changeset
1308 if (FLAG_BOTH_COLD(all_infos[i].flag))
kono
parents:
diff changeset
1309 both_cold_cnt++;
kono
parents:
diff changeset
1310 if (FLAG_BOTH_ZERO(all_infos[i].flag))
kono
parents:
diff changeset
1311 both_zero_cnt++;
kono
parents:
diff changeset
1312 }
kono
parents:
diff changeset
1313
kono
parents:
diff changeset
1314 double prg_val = 0;
kono
parents:
diff changeset
1315 double sum_val = 0;
kono
parents:
diff changeset
1316 double sum_cum_1 = 0;
kono
parents:
diff changeset
1317 double sum_cum_2 = 0;
kono
parents:
diff changeset
1318
kono
parents:
diff changeset
1319 for (i = 0; i < all_cnt; i++)
kono
parents:
diff changeset
1320 {
kono
parents:
diff changeset
1321 double val;
kono
parents:
diff changeset
1322 double cum_1, cum_2;
kono
parents:
diff changeset
1323 const char *filename;
kono
parents:
diff changeset
1324
kono
parents:
diff changeset
1325 if (all_infos[i].obj1 == 0 && all_infos[i].obj2 == 0)
kono
parents:
diff changeset
1326 continue;
kono
parents:
diff changeset
1327 if (FLAG_BOTH_ZERO (all_infos[i].flag))
kono
parents:
diff changeset
1328 continue;
kono
parents:
diff changeset
1329
kono
parents:
diff changeset
1330 if (all_infos[i].obj1)
kono
parents:
diff changeset
1331 filename = get_file_basename (all_infos[i].obj1->filename);
kono
parents:
diff changeset
1332 else
kono
parents:
diff changeset
1333 filename = get_file_basename (all_infos[i].obj2->filename);
kono
parents:
diff changeset
1334
kono
parents:
diff changeset
1335 if (overlap_func_level)
kono
parents:
diff changeset
1336 printf("\n processing %36s:\n", filename);
kono
parents:
diff changeset
1337
kono
parents:
diff changeset
1338 val = compute_one_gcov (all_infos[i].obj1, all_infos[i].obj2,
kono
parents:
diff changeset
1339 overlap_sum_1, overlap_sum_2, &cum_1, &cum_2);
kono
parents:
diff changeset
1340
kono
parents:
diff changeset
1341 if (overlap_obj_level && (!overlap_hot_only || FLAG_ONE_HOT (all_infos[i].flag)))
kono
parents:
diff changeset
1342 {
kono
parents:
diff changeset
1343 printf(" obj=%36s overlap = %6.2f%% (%5.2f%% %5.2f%%)\n",
kono
parents:
diff changeset
1344 filename, val*100, cum_1*100, cum_2*100);
kono
parents:
diff changeset
1345 sum_val += val;
kono
parents:
diff changeset
1346 sum_cum_1 += cum_1;
kono
parents:
diff changeset
1347 sum_cum_2 += cum_2;
kono
parents:
diff changeset
1348 }
kono
parents:
diff changeset
1349
kono
parents:
diff changeset
1350 prg_val += val;
kono
parents:
diff changeset
1351
kono
parents:
diff changeset
1352 }
kono
parents:
diff changeset
1353
kono
parents:
diff changeset
1354 if (overlap_obj_level)
kono
parents:
diff changeset
1355 printf(" SUM:%36s overlap = %6.2f%% (%5.2f%% %5.2f%%)\n",
kono
parents:
diff changeset
1356 "", sum_val*100, sum_cum_1*100, sum_cum_2*100);
kono
parents:
diff changeset
1357
kono
parents:
diff changeset
1358 printf (" Statistics:\n"
kono
parents:
diff changeset
1359 " profile1_# profile2_# overlap_#\n");
kono
parents:
diff changeset
1360 printf (" gcda files: %12u\t%12u\t%12u\n", gcda_files[0], gcda_files[1],
kono
parents:
diff changeset
1361 gcda_files[0]-unique_gcda_files[0]);
kono
parents:
diff changeset
1362 printf (" unique files: %12u\t%12u\n", unique_gcda_files[0],
kono
parents:
diff changeset
1363 unique_gcda_files[1]);
kono
parents:
diff changeset
1364 printf (" hot files: %12u\t%12u\t%12u\n", hot_gcda_files[0],
kono
parents:
diff changeset
1365 hot_gcda_files[1], both_hot_cnt);
kono
parents:
diff changeset
1366 printf (" cold files: %12u\t%12u\t%12u\n", cold_gcda_files[0],
kono
parents:
diff changeset
1367 cold_gcda_files[1], both_cold_cnt);
kono
parents:
diff changeset
1368 printf (" zero files: %12u\t%12u\t%12u\n", zero_gcda_files[0],
kono
parents:
diff changeset
1369 zero_gcda_files[1], both_zero_cnt);
kono
parents:
diff changeset
1370 printf (" sum_all: %12" PRId64 "\t%12" PRId64 "\n",
kono
parents:
diff changeset
1371 p1_sum_all, p2_sum_all);
kono
parents:
diff changeset
1372 printf (" run_max: %12" PRId64 "\t%12" PRId64 "\n",
kono
parents:
diff changeset
1373 p1_run_max, p2_run_max);
kono
parents:
diff changeset
1374
kono
parents:
diff changeset
1375 return prg_val;
kono
parents:
diff changeset
1376 }
kono
parents:
diff changeset
1377
kono
parents:
diff changeset
1378 /* Compute the overlap score of two lists of gcov_info objects PROFILE1 and
kono
parents:
diff changeset
1379 PROFILE2.
kono
parents:
diff changeset
1380 Return 0 on success: without mismatch. Reutrn 1 on error. */
kono
parents:
diff changeset
1381
kono
parents:
diff changeset
1382 int
kono
parents:
diff changeset
1383 gcov_profile_overlap (struct gcov_info *profile1, struct gcov_info *profile2)
kono
parents:
diff changeset
1384 {
kono
parents:
diff changeset
1385 double result;
kono
parents:
diff changeset
1386
kono
parents:
diff changeset
1387 result = calculate_overlap (profile1, profile2);
kono
parents:
diff changeset
1388
kono
parents:
diff changeset
1389 if (result > 0)
kono
parents:
diff changeset
1390 {
kono
parents:
diff changeset
1391 printf("\nProgram level overlap result is %3.2f%%\n\n", result*100);
kono
parents:
diff changeset
1392 return 0;
kono
parents:
diff changeset
1393 }
kono
parents:
diff changeset
1394 return 1;
kono
parents:
diff changeset
1395 }