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