* Makefile.in (.m.c): Strip out directory name from function name.
[deliverable/binutils-gdb.git] / gprof / hist.c
CommitLineData
5489fcc3
KR
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: */
12516a37 16extern void flat_blurb PARAMS ((FILE * fp));
5489fcc3
KR
17
18bfd_vma s_lowpc; /* lowest address in .text */
19bfd_vma s_highpc = 0; /* highest address in .text */
20bfd_vma lowpc, highpc; /* same, but expressed in UNITs */
21int hist_num_bins = 0; /* number of histogram samples */
22int *hist_sample = 0; /* histogram samples (shorts in the file!) */
23double hist_scale;
12516a37
KR
24char hist_dimension[sizeof (((struct gmon_hist_hdr *) 0)->dimen) + 1] =
25 "seconds";
5489fcc3
KR
26char hist_dimension_abbrev = 's';
27
28static double accum_time; /* accumulated time so far for print_line() */
29static 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 */
12516a37
KR
34const struct
35 {
5489fcc3
KR
36 char prefix;
37 double scale;
12516a37
KR
38 }
39SItab[] =
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 */
5489fcc3
KR
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 */
91void
12516a37 92DEFUN (hist_read_rec, (ifp, filename), FILE * ifp AND const char *filename)
5489fcc3 93{
12516a37
KR
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);
03c35bcb 104 }
12516a37
KR
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;
03c35bcb 125 }
12516a37
KR
126
127 DBG (SAMPLEDEBUG,
128 printf ("[hist_read_rec] n_lowpc 0x%lx n_highpc 0x%lx ncnt %d\n",
5489fcc3 129 n_lowpc, n_highpc, ncnt);
12516a37 130 printf ("[hist_read_rec] s_lowpc 0x%lx s_highpc 0x%lx nsamples %d\n",
5489fcc3 131 s_lowpc, s_highpc, hist_num_bins);
12516a37 132 printf ("[hist_read_rec] lowpc 0x%lx highpc 0x%lx\n",
5489fcc3
KR
133 lowpc, highpc));
134
12516a37
KR
135 if (n_lowpc != s_lowpc || n_highpc != s_highpc
136 || ncnt != hist_num_bins || hz != profrate)
5489fcc3 137 {
12516a37
KR
138 fprintf (stderr, "%s: `%s' is incompatible with first gmon file\n",
139 whoami, filename);
140 done (1);
03c35bcb 141 }
5489fcc3 142
12516a37
KR
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]));
03c35bcb 147 }
5489fcc3 148
12516a37
KR
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);
03c35bcb 157 }
12516a37 158 hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) & count[0]);
03c35bcb
KR
159 }
160}
5489fcc3
KR
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 */
167void
12516a37 168DEFUN (hist_write_hist, (ofp, filename), FILE * ofp AND const char *filename)
5489fcc3 169{
12516a37
KR
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);
03c35bcb 190 }
12516a37
KR
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);
03c35bcb
KR
199 }
200 }
201}
5489fcc3
KR
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 */
212static void
fcc14c40 213scale_and_align_entries ()
5489fcc3 214{
12516a37 215 Sym *sym;
12516a37
KR
216 bfd_vma bin_of_entry;
217 bfd_vma bin_of_code;
5489fcc3 218
12516a37
KR
219 for (sym = symtab.base; sym < symtab.limit; sym++)
220 {
221 sym->hist.scaled_addr = sym->addr / sizeof (UNIT);
12516a37
KR
222 bin_of_entry = (sym->hist.scaled_addr - lowpc) / hist_scale;
223 bin_of_code = (sym->hist.scaled_addr + UNITS_TO_CODE - lowpc) / hist_scale;
224 if (bin_of_entry < bin_of_code)
225 {
226 DBG (SAMPLEDEBUG,
227 printf ("[scale_and_align_entries] pushing 0x%lx to 0x%lx\n",
fcc14c40
KR
228 sym->hist.scaled_addr, sym->scaled_addr + UNITS_TO_CODE));
229 sym->scaled_addr += UNITS_TO_CODE;
03c35bcb 230 }
03c35bcb
KR
231 }
232}
5489fcc3
KR
233
234
235/*
236 * Assign samples to the symbol to which they belong.
237 *
238 * Histogram bin I covers some address range [BIN_LOWPC,BIN_HIGH_PC)
239 * which may overlap one more symbol address ranges. If a symbol
240 * overlaps with the bin's address range by O percent, then O percent
241 * of the bin's count is credited to that symbol.
242 *
243 * There are three cases as to where BIN_LOW_PC and BIN_HIGH_PC can be
244 * with respect to the symbol's address range [SYM_LOW_PC,
245 * SYM_HIGH_PC) as shown in the following diagram. OVERLAP computes
246 * the distance (in UNITs) between the arrows, the fraction of the
247 * sample that is to be credited to the symbol which starts at
248 * SYM_LOW_PC.
249 *
12516a37
KR
250 * sym_low_pc sym_high_pc
251 * | |
252 * v v
5489fcc3 253 *
12516a37
KR
254 * +-----------------------------------------------+
255 * | |
256 * | ->| |<- ->| |<- ->| |<- |
257 * | | | | | |
258 * +---------+ +---------+ +---------+
5489fcc3 259 *
12516a37
KR
260 * ^ ^ ^ ^ ^ ^
261 * | | | | | |
5489fcc3
KR
262 * bin_low_pc bin_high_pc bin_low_pc bin_high_pc bin_low_pc bin_high_pc
263 *
264 * For the VAX we assert that samples will never fall in the first two
265 * bytes of any routine, since that is the entry mask, thus we call
266 * scale_and_align_entries() to adjust the entry points if the entry
267 * mask falls in one bin but the code for the routine doesn't start
268 * until the next bin. In conjunction with the alignment of routine
269 * addresses, this should allow us to have only one sample for every
270 * four bytes of text space and never have any overlap (the two end
271 * cases, above).
272 */
273void
12516a37 274DEFUN_VOID (hist_assign_samples)
5489fcc3 275{
12516a37
KR
276 bfd_vma bin_low_pc, bin_high_pc;
277 bfd_vma sym_low_pc, sym_high_pc;
278 bfd_vma overlap, addr;
279 int bin_count, i, j;
280 double time, credit;
281
282 /* read samples and assign to symbols: */
283 hist_scale = highpc - lowpc;
284 hist_scale /= hist_num_bins;
285 scale_and_align_entries ();
286
287 /* iterate over all sample bins: */
288
289 for (i = 0, j = 1; i < hist_num_bins; ++i)
290 {
291 bin_count = hist_sample[i];
292 if (!bin_count)
293 {
294 continue;
03c35bcb 295 }
12516a37
KR
296 bin_low_pc = lowpc + (bfd_vma) (hist_scale * i);
297 bin_high_pc = lowpc + (bfd_vma) (hist_scale * (i + 1));
298 time = bin_count;
299 DBG (SAMPLEDEBUG,
300 printf (
301 "[assign_samples] bin_low_pc=0x%lx, bin_high_pc=0x%lx, bin_count=%d\n",
302 sizeof (UNIT) * bin_low_pc, sizeof (UNIT) * bin_high_pc,
303 bin_count));
304 total_time += time;
305
306 /* credit all symbols that are covered by bin I: */
307
308 for (j = j - 1; j < symtab.len; ++j)
309 {
310 sym_low_pc = symtab.base[j].hist.scaled_addr;
311 sym_high_pc = symtab.base[j + 1].hist.scaled_addr;
312 /*
313 * If high end of bin is below entry address, go for next
314 * bin:
315 */
316 if (bin_high_pc < sym_low_pc)
317 {
318 break;
03c35bcb 319 }
12516a37
KR
320 /*
321 * If low end of bin is above high end of symbol, go for
322 * next symbol.
323 */
324 if (bin_low_pc >= sym_high_pc)
325 {
326 continue;
03c35bcb 327 }
12516a37
KR
328 overlap =
329 MIN (bin_high_pc, sym_high_pc) - MAX (bin_low_pc, sym_low_pc);
330 if (overlap > 0)
331 {
332 DBG (SAMPLEDEBUG,
333 printf (
334 "[assign_samples] [0x%lx,0x%lx) %s gets %f ticks %ld overlap\n",
335 symtab.base[j].addr, sizeof (UNIT) * sym_high_pc,
336 symtab.base[j].name, overlap * time / hist_scale,
337 overlap));
338 addr = symtab.base[j].addr;
339 credit = overlap * time / hist_scale;
340 /*
341 * Credit symbol if it appears in INCL_FLAT or that
342 * table is empty and it does not appear it in
343 * EXCL_FLAT.
344 */
345 if (sym_lookup (&syms[INCL_FLAT], addr)
346 || (syms[INCL_FLAT].len == 0
347 && !sym_lookup (&syms[EXCL_FLAT], addr)))
5489fcc3 348 {
12516a37
KR
349 symtab.base[j].hist.time += credit;
350 }
351 else
352 {
353 total_time -= credit;
03c35bcb
KR
354 }
355 }
356 }
357 }
12516a37 358 DBG (SAMPLEDEBUG, printf ("[assign_samples] total_time %f\n",
5489fcc3 359 total_time));
03c35bcb 360}
5489fcc3
KR
361
362
363/*
364 * Print header for flag histogram profile:
365 */
366static void
12516a37 367DEFUN (print_header, (prefix), const char prefix)
5489fcc3 368{
12516a37
KR
369 char unit[64];
370
371 sprintf (unit, "%c%c/call", prefix, hist_dimension_abbrev);
372
373 if (bsd_style_output)
374 {
375 printf ("\ngranularity: each sample hit covers %ld byte(s)",
376 (long) hist_scale * sizeof (UNIT));
377 if (total_time > 0.0)
378 {
379 printf (" for %.2f%% of %.2f %s\n\n",
380 100.0 / total_time, total_time / hz, hist_dimension);
03c35bcb 381 }
12516a37
KR
382 }
383 else
384 {
385 printf ("\nEach sample counts as %g %s.\n", 1.0 / hz, hist_dimension);
03c35bcb 386 }
12516a37
KR
387
388 if (total_time <= 0.0)
389 {
390 printf (" no time accumulated\n\n");
391 /* this doesn't hurt since all the numerators will be zero: */
392 total_time = 1.0;
03c35bcb 393 }
12516a37
KR
394
395 printf ("%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
396 "% ", "cumulative", "self ", "", "self ", "total ", "");
397 printf ("%5.5s %9.9s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
398 "time", hist_dimension, hist_dimension, "calls", unit, unit,
399 "name");
03c35bcb 400}
5489fcc3
KR
401
402
403static void
12516a37 404DEFUN (print_line, (sym, scale), Sym * sym AND double scale)
5489fcc3 405{
12516a37
KR
406 if (ignore_zeros && sym->ncalls == 0 && sym->hist.time == 0)
407 {
408 return;
03c35bcb 409 }
12516a37
KR
410
411 accum_time += sym->hist.time;
412 if (bsd_style_output)
413 {
414 printf ("%5.1f %10.2f %8.2f",
415 total_time > 0.0 ? 100 * sym->hist.time / total_time : 0.0,
416 accum_time / hz, sym->hist.time / hz);
417 }
418 else
419 {
420 printf ("%6.2f %9.2f %8.2f",
421 total_time > 0.0 ? 100 * sym->hist.time / total_time : 0.0,
422 accum_time / hz, sym->hist.time / hz);
03c35bcb 423 }
12516a37
KR
424 if (sym->ncalls)
425 {
426 printf (" %8d %8.2f %8.2f ",
427 sym->ncalls, scale * sym->hist.time / hz / sym->ncalls,
428 scale * (sym->hist.time + sym->cg.child_time) / hz / sym->ncalls);
429 }
430 else
431 {
432 printf (" %8.8s %8.8s %8.8s ", "", "", "");
03c35bcb 433 }
12516a37
KR
434 if (bsd_style_output)
435 {
436 print_name (sym);
437 }
438 else
439 {
440 print_name_only (sym);
03c35bcb 441 }
12516a37 442 printf ("\n");
03c35bcb 443}
5489fcc3
KR
444
445
446/*
447 * Compare LP and RP. The primary comparison key is execution time,
448 * the secondary is number of invocation, and the tertiary is the
449 * lexicographic order of the function names.
450 */
451static int
12516a37 452DEFUN (cmp_time, (lp, rp), const PTR lp AND const PTR rp)
5489fcc3 453{
12516a37
KR
454 const Sym *left = *(const Sym **) lp;
455 const Sym *right = *(const Sym **) rp;
456 double time_diff;
457 long call_diff;
458
459 time_diff = right->hist.time - left->hist.time;
460 if (time_diff > 0.0)
461 {
462 return 1;
03c35bcb 463 }
12516a37
KR
464 if (time_diff < 0.0)
465 {
466 return -1;
03c35bcb 467 }
12516a37
KR
468
469 call_diff = right->ncalls - left->ncalls;
470 if (call_diff > 0)
471 {
472 return 1;
03c35bcb 473 }
12516a37
KR
474 if (call_diff < 0)
475 {
476 return -1;
03c35bcb 477 }
12516a37
KR
478
479 return strcmp (left->name, right->name);
03c35bcb 480}
5489fcc3
KR
481
482
483/*
484 * Print the flat histogram profile.
485 */
486void
12516a37 487DEFUN_VOID (hist_print)
5489fcc3 488{
12516a37
KR
489 Sym **time_sorted_syms, *top_dog, *sym;
490 int index, log_scale;
491 double top_time, time;
492 bfd_vma addr;
493
494 if (first_output)
495 {
496 first_output = FALSE;
497 }
498 else
499 {
500 printf ("\f\n");
03c35bcb 501 }
12516a37
KR
502
503 accum_time = 0.0;
504 if (bsd_style_output)
505 {
506 if (print_descriptions)
507 {
508 printf ("\n\n\nflat profile:\n");
509 flat_blurb (stdout);
03c35bcb 510 }
12516a37
KR
511 }
512 else
513 {
514 printf ("Flat profile:\n");
03c35bcb 515 }
12516a37
KR
516 /*
517 * Sort the symbol table by time (call-count and name as secondary
518 * and tertiary keys):
519 */
520 time_sorted_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
521 for (index = 0; index < symtab.len; ++index)
522 {
523 time_sorted_syms[index] = &symtab.base[index];
03c35bcb 524 }
12516a37
KR
525 qsort (time_sorted_syms, symtab.len, sizeof (Sym *), cmp_time);
526
527 if (bsd_style_output)
528 {
529 log_scale = 5; /* milli-seconds is BSD-default */
530 }
531 else
532 {
533 /*
534 * Search for symbol with highest per-call execution time and
535 * scale accordingly:
536 */
537 log_scale = 0;
538 top_dog = 0;
539 top_time = 0.0;
540 for (index = 0; index < symtab.len; ++index)
541 {
542 sym = time_sorted_syms[index];
543 if (sym->ncalls)
544 {
545 time = (sym->hist.time + sym->cg.child_time) / sym->ncalls;
546 if (time > top_time)
547 {
548 top_dog = sym;
549 top_time = time;
03c35bcb
KR
550 }
551 }
552 }
12516a37
KR
553 if (top_dog && top_dog->ncalls && top_time > 0.0)
554 {
555 top_time /= hz;
556 while (SItab[log_scale].scale * top_time < 1000.0
557 && log_scale < sizeof (SItab) / sizeof (SItab[0]) - 1)
5489fcc3 558 {
12516a37 559 ++log_scale;
03c35bcb
KR
560 }
561 }
562 }
12516a37
KR
563
564 /*
565 * For now, the dimension is always seconds. In the future, we
566 * may also want to support other (pseudo-)dimensions (such as
567 * I-cache misses etc.).
568 */
569 print_header (SItab[log_scale].prefix);
570 for (index = 0; index < symtab.len; ++index)
571 {
572 addr = time_sorted_syms[index]->addr;
573 /*
574 * Print symbol if its in INCL_FLAT table or that table
575 * is empty and the symbol is not in EXCL_FLAT.
576 */
577 if (sym_lookup (&syms[INCL_FLAT], addr)
578 || (syms[INCL_FLAT].len == 0
579 && !sym_lookup (&syms[EXCL_FLAT], addr)))
5489fcc3 580 {
12516a37 581 print_line (time_sorted_syms[index], SItab[log_scale].scale);
03c35bcb
KR
582 }
583 }
12516a37 584 free (time_sorted_syms);
5489fcc3 585
12516a37
KR
586 if (print_descriptions && !bsd_style_output)
587 {
588 flat_blurb (stdout);
03c35bcb
KR
589 }
590}
This page took 0.090021 seconds and 4 git commands to generate.