2 * Histogram related operations.
15 /* declarations of automatically generated functions to output blurbs: */
16 extern void flat_blurb
PARAMS ((FILE * fp
));
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!) */
24 char hist_dimension
[sizeof (((struct gmon_hist_hdr
*) 0)->dimen
) + 1] =
26 char hist_dimension_abbrev
= 's';
28 static double accum_time
; /* accumulated time so far for print_line() */
29 static double total_time
; /* total time for all routines */
31 * Table of SI prefixes for powers of 10 (used to automatically
32 * scale some of the values in the flat profile).
88 * Read the histogram from file IFP. FILENAME is the name of IFP and
89 * is provided for formatting error messages only.
92 DEFUN (hist_read_rec
, (ifp
, filename
), FILE * ifp AND
const char *filename
)
94 struct gmon_hist_hdr hdr
;
95 bfd_vma n_lowpc
, n_highpc
;
96 int i
, ncnt
, profrate
;
99 if (fread (&hdr
, sizeof (hdr
), 1, ifp
) != 1)
101 fprintf (stderr
, "%s: %s: unexpected end of file\n",
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
;
117 /* this is the first histogram record: */
121 lowpc
= (bfd_vma
) n_lowpc
/ sizeof (UNIT
);
122 highpc
= (bfd_vma
) n_highpc
/ sizeof (UNIT
);
123 hist_num_bins
= ncnt
;
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",
135 if (n_lowpc
!= s_lowpc
|| n_highpc
!= s_highpc
136 || ncnt
!= hist_num_bins
|| hz
!= profrate
)
138 fprintf (stderr
, "%s: `%s' is incompatible with first gmon file\n",
145 hist_sample
= (int *) xmalloc (hist_num_bins
* sizeof (hist_sample
[0]));
146 memset (hist_sample
, 0, hist_num_bins
* sizeof (hist_sample
[0]));
149 for (i
= 0; i
< hist_num_bins
; ++i
)
151 if (fread (&count
[0], sizeof (count
), 1, ifp
) != 1)
154 "%s: %s: unexpected EOF after reading %d of %d samples\n",
155 whoami
, filename
, i
, hist_num_bins
);
158 hist_sample
[i
] += bfd_get_16 (core_bfd
, (bfd_byte
*) & count
[0]);
164 * Write execution histogram to file OFP. FILENAME is the name
165 * of OFP and is provided for formatting error-messages only.
168 DEFUN (hist_write_hist
, (ofp
, filename
), FILE * ofp AND
const char *filename
)
170 struct gmon_hist_hdr hdr
;
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
;
185 if (fwrite (&tag
, sizeof (tag
), 1, ofp
) != 1
186 || fwrite (&hdr
, sizeof (hdr
), 1, ofp
) != 1)
192 for (i
= 0; i
< hist_num_bins
; ++i
)
194 bfd_put_16 (core_bfd
, hist_sample
[i
], (bfd_byte
*) & count
[0]);
195 if (fwrite (&count
[0], sizeof (count
), 1, ofp
) != 1)
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
213 scale_and_align_entries ()
216 bfd_vma bin_of_entry
;
219 for (sym
= symtab
.base
; sym
< symtab
.limit
; sym
++)
221 sym
->hist
.scaled_addr
= sym
->addr
/ sizeof (UNIT
);
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
)
227 printf ("[scale_and_align_entries] pushing 0x%lx to 0x%lx\n",
228 sym
->hist
.scaled_addr
,
229 sym
->hist
.scaled_addr
+ UNITS_TO_CODE
));
230 sym
->hist
.scaled_addr
+= UNITS_TO_CODE
;
237 * Assign samples to the symbol to which they belong.
239 * Histogram bin I covers some address range [BIN_LOWPC,BIN_HIGH_PC)
240 * which may overlap one more symbol address ranges. If a symbol
241 * overlaps with the bin's address range by O percent, then O percent
242 * of the bin's count is credited to that symbol.
244 * There are three cases as to where BIN_LOW_PC and BIN_HIGH_PC can be
245 * with respect to the symbol's address range [SYM_LOW_PC,
246 * SYM_HIGH_PC) as shown in the following diagram. OVERLAP computes
247 * the distance (in UNITs) between the arrows, the fraction of the
248 * sample that is to be credited to the symbol which starts at
251 * sym_low_pc sym_high_pc
255 * +-----------------------------------------------+
257 * | ->| |<- ->| |<- ->| |<- |
259 * +---------+ +---------+ +---------+
263 * bin_low_pc bin_high_pc bin_low_pc bin_high_pc bin_low_pc bin_high_pc
265 * For the VAX we assert that samples will never fall in the first two
266 * bytes of any routine, since that is the entry mask, thus we call
267 * scale_and_align_entries() to adjust the entry points if the entry
268 * mask falls in one bin but the code for the routine doesn't start
269 * until the next bin. In conjunction with the alignment of routine
270 * addresses, this should allow us to have only one sample for every
271 * four bytes of text space and never have any overlap (the two end
275 DEFUN_VOID (hist_assign_samples
)
277 bfd_vma bin_low_pc
, bin_high_pc
;
278 bfd_vma sym_low_pc
, sym_high_pc
;
279 bfd_vma overlap
, addr
;
283 /* read samples and assign to symbols: */
284 hist_scale
= highpc
- lowpc
;
285 hist_scale
/= hist_num_bins
;
286 scale_and_align_entries ();
288 /* iterate over all sample bins: */
290 for (i
= 0, j
= 1; i
< hist_num_bins
; ++i
)
292 bin_count
= hist_sample
[i
];
297 bin_low_pc
= lowpc
+ (bfd_vma
) (hist_scale
* i
);
298 bin_high_pc
= lowpc
+ (bfd_vma
) (hist_scale
* (i
+ 1));
302 "[assign_samples] bin_low_pc=0x%lx, bin_high_pc=0x%lx, bin_count=%d\n",
303 sizeof (UNIT
) * bin_low_pc
, sizeof (UNIT
) * bin_high_pc
,
307 /* credit all symbols that are covered by bin I: */
309 for (j
= j
- 1; j
< symtab
.len
; ++j
)
311 sym_low_pc
= symtab
.base
[j
].hist
.scaled_addr
;
312 sym_high_pc
= symtab
.base
[j
+ 1].hist
.scaled_addr
;
314 * If high end of bin is below entry address, go for next
317 if (bin_high_pc
< sym_low_pc
)
322 * If low end of bin is above high end of symbol, go for
325 if (bin_low_pc
>= sym_high_pc
)
330 MIN (bin_high_pc
, sym_high_pc
) - MAX (bin_low_pc
, sym_low_pc
);
335 "[assign_samples] [0x%lx,0x%lx) %s gets %f ticks %ld overlap\n",
336 symtab
.base
[j
].addr
, sizeof (UNIT
) * sym_high_pc
,
337 symtab
.base
[j
].name
, overlap
* time
/ hist_scale
,
339 addr
= symtab
.base
[j
].addr
;
340 credit
= overlap
* time
/ hist_scale
;
342 * Credit symbol if it appears in INCL_FLAT or that
343 * table is empty and it does not appear it in
346 if (sym_lookup (&syms
[INCL_FLAT
], addr
)
347 || (syms
[INCL_FLAT
].len
== 0
348 && !sym_lookup (&syms
[EXCL_FLAT
], addr
)))
350 symtab
.base
[j
].hist
.time
+= credit
;
354 total_time
-= credit
;
359 DBG (SAMPLEDEBUG
, printf ("[assign_samples] total_time %f\n",
365 * Print header for flag histogram profile:
368 DEFUN (print_header
, (prefix
), const char prefix
)
372 sprintf (unit
, "%c%c/call", prefix
, hist_dimension_abbrev
);
374 if (bsd_style_output
)
376 printf ("\ngranularity: each sample hit covers %ld byte(s)",
377 (long) hist_scale
* sizeof (UNIT
));
378 if (total_time
> 0.0)
380 printf (" for %.2f%% of %.2f %s\n\n",
381 100.0 / total_time
, total_time
/ hz
, hist_dimension
);
386 printf ("\nEach sample counts as %g %s.\n", 1.0 / hz
, hist_dimension
);
389 if (total_time
<= 0.0)
391 printf (" no time accumulated\n\n");
392 /* this doesn't hurt since all the numerators will be zero: */
396 printf ("%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
397 "% ", "cumulative", "self ", "", "self ", "total ", "");
398 printf ("%5.5s %9.9s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
399 "time", hist_dimension
, hist_dimension
, "calls", unit
, unit
,
405 DEFUN (print_line
, (sym
, scale
), Sym
* sym AND
double scale
)
407 if (ignore_zeros
&& sym
->ncalls
== 0 && sym
->hist
.time
== 0)
412 accum_time
+= sym
->hist
.time
;
413 if (bsd_style_output
)
415 printf ("%5.1f %10.2f %8.2f",
416 total_time
> 0.0 ? 100 * sym
->hist
.time
/ total_time
: 0.0,
417 accum_time
/ hz
, sym
->hist
.time
/ hz
);
421 printf ("%6.2f %9.2f %8.2f",
422 total_time
> 0.0 ? 100 * sym
->hist
.time
/ total_time
: 0.0,
423 accum_time
/ hz
, sym
->hist
.time
/ hz
);
427 printf (" %8d %8.2f %8.2f ",
428 sym
->ncalls
, scale
* sym
->hist
.time
/ hz
/ sym
->ncalls
,
429 scale
* (sym
->hist
.time
+ sym
->cg
.child_time
) / hz
/ sym
->ncalls
);
433 printf (" %8.8s %8.8s %8.8s ", "", "", "");
435 if (bsd_style_output
)
441 print_name_only (sym
);
448 * Compare LP and RP. The primary comparison key is execution time,
449 * the secondary is number of invocation, and the tertiary is the
450 * lexicographic order of the function names.
453 DEFUN (cmp_time
, (lp
, rp
), const PTR lp AND
const PTR rp
)
455 const Sym
*left
= *(const Sym
**) lp
;
456 const Sym
*right
= *(const Sym
**) rp
;
460 time_diff
= right
->hist
.time
- left
->hist
.time
;
470 call_diff
= right
->ncalls
- left
->ncalls
;
480 return strcmp (left
->name
, right
->name
);
485 * Print the flat histogram profile.
488 DEFUN_VOID (hist_print
)
490 Sym
**time_sorted_syms
, *top_dog
, *sym
;
491 int index
, log_scale
;
492 double top_time
, time
;
497 first_output
= FALSE
;
505 if (bsd_style_output
)
507 if (print_descriptions
)
509 printf ("\n\n\nflat profile:\n");
515 printf ("Flat profile:\n");
518 * Sort the symbol table by time (call-count and name as secondary
519 * and tertiary keys):
521 time_sorted_syms
= (Sym
**) xmalloc (symtab
.len
* sizeof (Sym
*));
522 for (index
= 0; index
< symtab
.len
; ++index
)
524 time_sorted_syms
[index
] = &symtab
.base
[index
];
526 qsort (time_sorted_syms
, symtab
.len
, sizeof (Sym
*), cmp_time
);
528 if (bsd_style_output
)
530 log_scale
= 5; /* milli-seconds is BSD-default */
535 * Search for symbol with highest per-call execution time and
541 for (index
= 0; index
< symtab
.len
; ++index
)
543 sym
= time_sorted_syms
[index
];
546 time
= (sym
->hist
.time
+ sym
->cg
.child_time
) / sym
->ncalls
;
554 if (top_dog
&& top_dog
->ncalls
&& top_time
> 0.0)
557 while (SItab
[log_scale
].scale
* top_time
< 1000.0
558 && log_scale
< sizeof (SItab
) / sizeof (SItab
[0]) - 1)
566 * For now, the dimension is always seconds. In the future, we
567 * may also want to support other (pseudo-)dimensions (such as
568 * I-cache misses etc.).
570 print_header (SItab
[log_scale
].prefix
);
571 for (index
= 0; index
< symtab
.len
; ++index
)
573 addr
= time_sorted_syms
[index
]->addr
;
575 * Print symbol if its in INCL_FLAT table or that table
576 * is empty and the symbol is not in EXCL_FLAT.
578 if (sym_lookup (&syms
[INCL_FLAT
], addr
)
579 || (syms
[INCL_FLAT
].len
== 0
580 && !sym_lookup (&syms
[EXCL_FLAT
], addr
)))
582 print_line (time_sorted_syms
[index
], SItab
[log_scale
].scale
);
585 free (time_sorted_syms
);
587 if (print_descriptions
&& !bsd_style_output
)
This page took 0.053008 seconds and 4 git commands to generate.