From Jason Merrill <jason@cygnus.com>:
[deliverable/binutils-gdb.git] / gprof / hist.c
1 /*
2 * Histogram related operations.
3 */
4 #include <stdio.h>
5 #include "libiberty.h"
6 #include "gprof.h"
7 #include "core.h"
8 #include "gmon_io.h"
9 #include "gmon_out.h"
10 #include "hist.h"
11 #include "symtab.h"
12 #include "sym_ids.h"
13 #include "utils.h"
14
15 static void scale_and_align_entries PARAMS ((void));
16
17 /* declarations of automatically generated functions to output blurbs: */
18 extern void flat_blurb PARAMS ((FILE * fp));
19
20 bfd_vma s_lowpc; /* lowest address in .text */
21 bfd_vma s_highpc = 0; /* highest address in .text */
22 bfd_vma lowpc, highpc; /* same, but expressed in UNITs */
23 int hist_num_bins = 0; /* number of histogram samples */
24 int *hist_sample = 0; /* histogram samples (shorts in the file!) */
25 double hist_scale;
26 char hist_dimension[sizeof (((struct gmon_hist_hdr *) 0)->dimen) + 1] =
27 "seconds";
28 char hist_dimension_abbrev = 's';
29
30 static double accum_time; /* accumulated time so far for print_line() */
31 static double total_time; /* total time for all routines */
32 /*
33 * Table of SI prefixes for powers of 10 (used to automatically
34 * scale some of the values in the flat profile).
35 */
36 const struct
37 {
38 char prefix;
39 double scale;
40 }
41 SItab[] =
42 {
43 {
44 'T', 1e-12
45 }
46 , /* tera */
47 {
48 'G', 1e-09
49 }
50 , /* giga */
51 {
52 'M', 1e-06
53 }
54 , /* mega */
55 {
56 'K', 1e-03
57 }
58 , /* kilo */
59 {
60 ' ', 1e-00
61 }
62 ,
63 {
64 'm', 1e+03
65 }
66 , /* milli */
67 {
68 'u', 1e+06
69 }
70 , /* micro */
71 {
72 'n', 1e+09
73 }
74 , /* nano */
75 {
76 'p', 1e+12
77 }
78 , /* pico */
79 {
80 'f', 1e+15
81 }
82 , /* femto */
83 {
84 'a', 1e+18
85 }
86 , /* ato */
87 };
88
89 /*
90 * Read the histogram from file IFP. FILENAME is the name of IFP and
91 * is provided for formatting error messages only.
92 */
93 void
94 DEFUN (hist_read_rec, (ifp, filename), FILE * ifp AND const char *filename)
95 {
96 struct gmon_hist_hdr hdr;
97 bfd_vma n_lowpc, n_highpc;
98 int i, ncnt, profrate;
99 UNIT count;
100
101 if (fread (&hdr, sizeof (hdr), 1, ifp) != 1)
102 {
103 fprintf (stderr, "%s: %s: unexpected end of file\n",
104 whoami, filename);
105 done (1);
106 }
107
108 n_lowpc = (bfd_vma) get_vma (core_bfd, (bfd_byte *) hdr.low_pc);
109 n_highpc = (bfd_vma) get_vma (core_bfd, (bfd_byte *) hdr.high_pc);
110 ncnt = bfd_get_32 (core_bfd, (bfd_byte *) hdr.hist_size);
111 profrate = bfd_get_32 (core_bfd, (bfd_byte *) hdr.prof_rate);
112 strncpy (hist_dimension, hdr.dimen, sizeof (hdr.dimen));
113 hist_dimension[sizeof (hdr.dimen)] = '\0';
114 hist_dimension_abbrev = hdr.dimen_abbrev;
115
116 if (!s_highpc)
117 {
118
119 /* this is the first histogram record: */
120
121 s_lowpc = n_lowpc;
122 s_highpc = n_highpc;
123 lowpc = (bfd_vma) n_lowpc / sizeof (UNIT);
124 highpc = (bfd_vma) n_highpc / sizeof (UNIT);
125 hist_num_bins = ncnt;
126 hz = profrate;
127 }
128
129 DBG (SAMPLEDEBUG,
130 printf ("[hist_read_rec] n_lowpc 0x%lx n_highpc 0x%lx ncnt %d\n",
131 n_lowpc, n_highpc, ncnt);
132 printf ("[hist_read_rec] s_lowpc 0x%lx s_highpc 0x%lx nsamples %d\n",
133 s_lowpc, s_highpc, hist_num_bins);
134 printf ("[hist_read_rec] lowpc 0x%lx highpc 0x%lx\n",
135 lowpc, highpc));
136
137 if (n_lowpc != s_lowpc || n_highpc != s_highpc
138 || ncnt != hist_num_bins || hz != profrate)
139 {
140 fprintf (stderr, "%s: `%s' is incompatible with first gmon file\n",
141 whoami, filename);
142 done (1);
143 }
144
145 if (!hist_sample)
146 {
147 hist_sample = (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0]));
148 memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0]));
149 }
150
151 for (i = 0; i < hist_num_bins; ++i)
152 {
153 if (fread (&count[0], sizeof (count), 1, ifp) != 1)
154 {
155 fprintf (stderr,
156 "%s: %s: unexpected EOF after reading %d of %d samples\n",
157 whoami, filename, i, hist_num_bins);
158 done (1);
159 }
160 hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) & count[0]);
161 }
162 }
163
164
165 /*
166 * Write execution histogram to file OFP. FILENAME is the name
167 * of OFP and is provided for formatting error-messages only.
168 */
169 void
170 DEFUN (hist_write_hist, (ofp, filename), FILE * ofp AND const char *filename)
171 {
172 struct gmon_hist_hdr hdr;
173 unsigned char tag;
174 UNIT count;
175 int i;
176
177 /* write header: */
178
179 tag = GMON_TAG_TIME_HIST;
180 put_vma (core_bfd, s_lowpc, (bfd_byte *) hdr.low_pc);
181 put_vma (core_bfd, s_highpc, (bfd_byte *) hdr.high_pc);
182 bfd_put_32 (core_bfd, hist_num_bins, (bfd_byte *) hdr.hist_size);
183 bfd_put_32 (core_bfd, hz, (bfd_byte *) hdr.prof_rate);
184 strncpy (hdr.dimen, hist_dimension, sizeof (hdr.dimen));
185 hdr.dimen_abbrev = hist_dimension_abbrev;
186
187 if (fwrite (&tag, sizeof (tag), 1, ofp) != 1
188 || fwrite (&hdr, sizeof (hdr), 1, ofp) != 1)
189 {
190 perror (filename);
191 done (1);
192 }
193
194 for (i = 0; i < hist_num_bins; ++i)
195 {
196 bfd_put_16 (core_bfd, hist_sample[i], (bfd_byte *) & count[0]);
197 if (fwrite (&count[0], sizeof (count), 1, ofp) != 1)
198 {
199 perror (filename);
200 done (1);
201 }
202 }
203 }
204
205
206 /*
207 * Calculate scaled entry point addresses (to save time in
208 * hist_assign_samples), and, on architectures that have procedure
209 * entry masks at the start of a function, possibly push the scaled
210 * entry points over the procedure entry mask, if it turns out that
211 * the entry point is in one bin and the code for a routine is in the
212 * next bin.
213 */
214 static void
215 scale_and_align_entries ()
216 {
217 Sym *sym;
218 bfd_vma bin_of_entry;
219 bfd_vma bin_of_code;
220
221 for (sym = symtab.base; sym < symtab.limit; sym++)
222 {
223 sym->hist.scaled_addr = sym->addr / sizeof (UNIT);
224 bin_of_entry = (sym->hist.scaled_addr - lowpc) / hist_scale;
225 bin_of_code = (sym->hist.scaled_addr + UNITS_TO_CODE - lowpc) / hist_scale;
226 if (bin_of_entry < bin_of_code)
227 {
228 DBG (SAMPLEDEBUG,
229 printf ("[scale_and_align_entries] pushing 0x%lx to 0x%lx\n",
230 sym->hist.scaled_addr,
231 sym->hist.scaled_addr + UNITS_TO_CODE));
232 sym->hist.scaled_addr += UNITS_TO_CODE;
233 }
234 }
235 }
236
237
238 /*
239 * Assign samples to the symbol to which they belong.
240 *
241 * Histogram bin I covers some address range [BIN_LOWPC,BIN_HIGH_PC)
242 * which may overlap one more symbol address ranges. If a symbol
243 * overlaps with the bin's address range by O percent, then O percent
244 * of the bin's count is credited to that symbol.
245 *
246 * There are three cases as to where BIN_LOW_PC and BIN_HIGH_PC can be
247 * with respect to the symbol's address range [SYM_LOW_PC,
248 * SYM_HIGH_PC) as shown in the following diagram. OVERLAP computes
249 * the distance (in UNITs) between the arrows, the fraction of the
250 * sample that is to be credited to the symbol which starts at
251 * SYM_LOW_PC.
252 *
253 * sym_low_pc sym_high_pc
254 * | |
255 * v v
256 *
257 * +-----------------------------------------------+
258 * | |
259 * | ->| |<- ->| |<- ->| |<- |
260 * | | | | | |
261 * +---------+ +---------+ +---------+
262 *
263 * ^ ^ ^ ^ ^ ^
264 * | | | | | |
265 * bin_low_pc bin_high_pc bin_low_pc bin_high_pc bin_low_pc bin_high_pc
266 *
267 * For the VAX we assert that samples will never fall in the first two
268 * bytes of any routine, since that is the entry mask, thus we call
269 * scale_and_align_entries() to adjust the entry points if the entry
270 * mask falls in one bin but the code for the routine doesn't start
271 * until the next bin. In conjunction with the alignment of routine
272 * addresses, this should allow us to have only one sample for every
273 * four bytes of text space and never have any overlap (the two end
274 * cases, above).
275 */
276 void
277 DEFUN_VOID (hist_assign_samples)
278 {
279 bfd_vma bin_low_pc, bin_high_pc;
280 bfd_vma sym_low_pc, sym_high_pc;
281 bfd_vma overlap, addr;
282 int bin_count, i, j;
283 double time, credit;
284
285 /* read samples and assign to symbols: */
286 hist_scale = highpc - lowpc;
287 hist_scale /= hist_num_bins;
288 scale_and_align_entries ();
289
290 /* iterate over all sample bins: */
291
292 for (i = 0, j = 1; i < hist_num_bins; ++i)
293 {
294 bin_count = hist_sample[i];
295 if (!bin_count)
296 {
297 continue;
298 }
299 bin_low_pc = lowpc + (bfd_vma) (hist_scale * i);
300 bin_high_pc = lowpc + (bfd_vma) (hist_scale * (i + 1));
301 time = bin_count;
302 DBG (SAMPLEDEBUG,
303 printf (
304 "[assign_samples] bin_low_pc=0x%lx, bin_high_pc=0x%lx, bin_count=%d\n",
305 sizeof (UNIT) * bin_low_pc, sizeof (UNIT) * bin_high_pc,
306 bin_count));
307 total_time += time;
308
309 /* credit all symbols that are covered by bin I: */
310
311 for (j = j - 1; j < symtab.len; ++j)
312 {
313 sym_low_pc = symtab.base[j].hist.scaled_addr;
314 sym_high_pc = symtab.base[j + 1].hist.scaled_addr;
315 /*
316 * If high end of bin is below entry address, go for next
317 * bin:
318 */
319 if (bin_high_pc < sym_low_pc)
320 {
321 break;
322 }
323 /*
324 * If low end of bin is above high end of symbol, go for
325 * next symbol.
326 */
327 if (bin_low_pc >= sym_high_pc)
328 {
329 continue;
330 }
331 overlap =
332 MIN (bin_high_pc, sym_high_pc) - MAX (bin_low_pc, sym_low_pc);
333 if (overlap > 0)
334 {
335 DBG (SAMPLEDEBUG,
336 printf (
337 "[assign_samples] [0x%lx,0x%lx) %s gets %f ticks %ld overlap\n",
338 symtab.base[j].addr, sizeof (UNIT) * sym_high_pc,
339 symtab.base[j].name, overlap * time / hist_scale,
340 overlap));
341 addr = symtab.base[j].addr;
342 credit = overlap * time / hist_scale;
343 /*
344 * Credit symbol if it appears in INCL_FLAT or that
345 * table is empty and it does not appear it in
346 * EXCL_FLAT.
347 */
348 if (sym_lookup (&syms[INCL_FLAT], addr)
349 || (syms[INCL_FLAT].len == 0
350 && !sym_lookup (&syms[EXCL_FLAT], addr)))
351 {
352 symtab.base[j].hist.time += credit;
353 }
354 else
355 {
356 total_time -= credit;
357 }
358 }
359 }
360 }
361 DBG (SAMPLEDEBUG, printf ("[assign_samples] total_time %f\n",
362 total_time));
363 }
364
365
366 /*
367 * Print header for flag histogram profile:
368 */
369 static void
370 DEFUN (print_header, (prefix), const char prefix)
371 {
372 char unit[64];
373
374 sprintf (unit, "%c%c/call", prefix, hist_dimension_abbrev);
375
376 if (bsd_style_output)
377 {
378 printf ("\ngranularity: each sample hit covers %ld byte(s)",
379 (long) hist_scale * sizeof (UNIT));
380 if (total_time > 0.0)
381 {
382 printf (" for %.2f%% of %.2f %s\n\n",
383 100.0 / total_time, total_time / hz, hist_dimension);
384 }
385 }
386 else
387 {
388 printf ("\nEach sample counts as %g %s.\n", 1.0 / hz, hist_dimension);
389 }
390
391 if (total_time <= 0.0)
392 {
393 printf (" no time accumulated\n\n");
394 /* this doesn't hurt since all the numerators will be zero: */
395 total_time = 1.0;
396 }
397
398 printf ("%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
399 "% ", "cumulative", "self ", "", "self ", "total ", "");
400 printf ("%5.5s %9.9s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
401 "time", hist_dimension, hist_dimension, "calls", unit, unit,
402 "name");
403 }
404
405
406 static void
407 DEFUN (print_line, (sym, scale), Sym * sym AND double scale)
408 {
409 if (ignore_zeros && sym->ncalls == 0 && sym->hist.time == 0)
410 {
411 return;
412 }
413
414 accum_time += sym->hist.time;
415 if (bsd_style_output)
416 {
417 printf ("%5.1f %10.2f %8.2f",
418 total_time > 0.0 ? 100 * sym->hist.time / total_time : 0.0,
419 accum_time / hz, sym->hist.time / hz);
420 }
421 else
422 {
423 printf ("%6.2f %9.2f %8.2f",
424 total_time > 0.0 ? 100 * sym->hist.time / total_time : 0.0,
425 accum_time / hz, sym->hist.time / hz);
426 }
427 if (sym->ncalls)
428 {
429 printf (" %8d %8.2f %8.2f ",
430 sym->ncalls, scale * sym->hist.time / hz / sym->ncalls,
431 scale * (sym->hist.time + sym->cg.child_time) / hz / sym->ncalls);
432 }
433 else
434 {
435 printf (" %8.8s %8.8s %8.8s ", "", "", "");
436 }
437 if (bsd_style_output)
438 {
439 print_name (sym);
440 }
441 else
442 {
443 print_name_only (sym);
444 }
445 printf ("\n");
446 }
447
448
449 /*
450 * Compare LP and RP. The primary comparison key is execution time,
451 * the secondary is number of invocation, and the tertiary is the
452 * lexicographic order of the function names.
453 */
454 static int
455 DEFUN (cmp_time, (lp, rp), const PTR lp AND const PTR rp)
456 {
457 const Sym *left = *(const Sym **) lp;
458 const Sym *right = *(const Sym **) rp;
459 double time_diff;
460 long call_diff;
461
462 time_diff = right->hist.time - left->hist.time;
463 if (time_diff > 0.0)
464 {
465 return 1;
466 }
467 if (time_diff < 0.0)
468 {
469 return -1;
470 }
471
472 call_diff = right->ncalls - left->ncalls;
473 if (call_diff > 0)
474 {
475 return 1;
476 }
477 if (call_diff < 0)
478 {
479 return -1;
480 }
481
482 return strcmp (left->name, right->name);
483 }
484
485
486 /*
487 * Print the flat histogram profile.
488 */
489 void
490 DEFUN_VOID (hist_print)
491 {
492 Sym **time_sorted_syms, *top_dog, *sym;
493 int index, log_scale;
494 double top_time, time;
495 bfd_vma addr;
496
497 if (first_output)
498 {
499 first_output = FALSE;
500 }
501 else
502 {
503 printf ("\f\n");
504 }
505
506 accum_time = 0.0;
507 if (bsd_style_output)
508 {
509 if (print_descriptions)
510 {
511 printf ("\n\n\nflat profile:\n");
512 flat_blurb (stdout);
513 }
514 }
515 else
516 {
517 printf ("Flat profile:\n");
518 }
519 /*
520 * Sort the symbol table by time (call-count and name as secondary
521 * and tertiary keys):
522 */
523 time_sorted_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
524 for (index = 0; index < symtab.len; ++index)
525 {
526 time_sorted_syms[index] = &symtab.base[index];
527 }
528 qsort (time_sorted_syms, symtab.len, sizeof (Sym *), cmp_time);
529
530 if (bsd_style_output)
531 {
532 log_scale = 5; /* milli-seconds is BSD-default */
533 }
534 else
535 {
536 /*
537 * Search for symbol with highest per-call execution time and
538 * scale accordingly:
539 */
540 log_scale = 0;
541 top_dog = 0;
542 top_time = 0.0;
543 for (index = 0; index < symtab.len; ++index)
544 {
545 sym = time_sorted_syms[index];
546 if (sym->ncalls)
547 {
548 time = (sym->hist.time + sym->cg.child_time) / sym->ncalls;
549 if (time > top_time)
550 {
551 top_dog = sym;
552 top_time = time;
553 }
554 }
555 }
556 if (top_dog && top_dog->ncalls && top_time > 0.0)
557 {
558 top_time /= hz;
559 while (SItab[log_scale].scale * top_time < 1000.0
560 && log_scale < sizeof (SItab) / sizeof (SItab[0]) - 1)
561 {
562 ++log_scale;
563 }
564 }
565 }
566
567 /*
568 * For now, the dimension is always seconds. In the future, we
569 * may also want to support other (pseudo-)dimensions (such as
570 * I-cache misses etc.).
571 */
572 print_header (SItab[log_scale].prefix);
573 for (index = 0; index < symtab.len; ++index)
574 {
575 addr = time_sorted_syms[index]->addr;
576 /*
577 * Print symbol if its in INCL_FLAT table or that table
578 * is empty and the symbol is not in EXCL_FLAT.
579 */
580 if (sym_lookup (&syms[INCL_FLAT], addr)
581 || (syms[INCL_FLAT].len == 0
582 && !sym_lookup (&syms[EXCL_FLAT], addr)))
583 {
584 print_line (time_sorted_syms[index], SItab[log_scale].scale);
585 }
586 }
587 free (time_sorted_syms);
588
589 if (print_descriptions && !bsd_style_output)
590 {
591 flat_blurb (stdout);
592 }
593 }
This page took 0.041809 seconds and 4 git commands to generate.