2 * Histogram related operations.
15 #define UNITS_TO_CODE (offset_to_code / sizeof(UNIT))
17 static void scale_and_align_entries
PARAMS ((void));
19 /* declarations of automatically generated functions to output blurbs: */
20 extern void flat_blurb
PARAMS ((FILE * fp
));
22 bfd_vma s_lowpc
; /* lowest address in .text */
23 bfd_vma s_highpc
= 0; /* highest address in .text */
24 bfd_vma lowpc
, highpc
; /* same, but expressed in UNITs */
25 int hist_num_bins
= 0; /* number of histogram samples */
26 int *hist_sample
= 0; /* histogram samples (shorts in the file!) */
28 char hist_dimension
[sizeof (((struct gmon_hist_hdr
*) 0)->dimen
) + 1] =
30 char hist_dimension_abbrev
= 's';
32 static double accum_time
; /* accumulated time so far for print_line() */
33 static double total_time
; /* total time for all routines */
35 * Table of SI prefixes for powers of 10 (used to automatically
36 * scale some of the values in the flat profile).
92 * Read the histogram from file IFP. FILENAME is the name of IFP and
93 * is provided for formatting error messages only.
96 DEFUN (hist_read_rec
, (ifp
, filename
), FILE * ifp AND
const char *filename
)
98 struct gmon_hist_hdr hdr
;
99 bfd_vma n_lowpc
, n_highpc
;
100 int i
, ncnt
, profrate
;
103 if (fread (&hdr
, sizeof (hdr
), 1, ifp
) != 1)
105 fprintf (stderr
, "%s: %s: unexpected end of file\n",
110 n_lowpc
= (bfd_vma
) get_vma (core_bfd
, (bfd_byte
*) hdr
.low_pc
);
111 n_highpc
= (bfd_vma
) get_vma (core_bfd
, (bfd_byte
*) hdr
.high_pc
);
112 ncnt
= bfd_get_32 (core_bfd
, (bfd_byte
*) hdr
.hist_size
);
113 profrate
= bfd_get_32 (core_bfd
, (bfd_byte
*) hdr
.prof_rate
);
114 strncpy (hist_dimension
, hdr
.dimen
, sizeof (hdr
.dimen
));
115 hist_dimension
[sizeof (hdr
.dimen
)] = '\0';
116 hist_dimension_abbrev
= hdr
.dimen_abbrev
;
121 /* this is the first histogram record: */
125 lowpc
= (bfd_vma
) n_lowpc
/ sizeof (UNIT
);
126 highpc
= (bfd_vma
) n_highpc
/ sizeof (UNIT
);
127 hist_num_bins
= ncnt
;
132 printf ("[hist_read_rec] n_lowpc 0x%lx n_highpc 0x%lx ncnt %d\n",
133 n_lowpc
, n_highpc
, ncnt
);
134 printf ("[hist_read_rec] s_lowpc 0x%lx s_highpc 0x%lx nsamples %d\n",
135 s_lowpc
, s_highpc
, hist_num_bins
);
136 printf ("[hist_read_rec] lowpc 0x%lx highpc 0x%lx\n",
139 if (n_lowpc
!= s_lowpc
|| n_highpc
!= s_highpc
140 || ncnt
!= hist_num_bins
|| hz
!= profrate
)
142 fprintf (stderr
, "%s: `%s' is incompatible with first gmon file\n",
149 hist_sample
= (int *) xmalloc (hist_num_bins
* sizeof (hist_sample
[0]));
150 memset (hist_sample
, 0, hist_num_bins
* sizeof (hist_sample
[0]));
153 for (i
= 0; i
< hist_num_bins
; ++i
)
155 if (fread (&count
[0], sizeof (count
), 1, ifp
) != 1)
158 "%s: %s: unexpected EOF after reading %d of %d samples\n",
159 whoami
, filename
, i
, hist_num_bins
);
162 hist_sample
[i
] += bfd_get_16 (core_bfd
, (bfd_byte
*) & count
[0]);
168 * Write execution histogram to file OFP. FILENAME is the name
169 * of OFP and is provided for formatting error-messages only.
172 DEFUN (hist_write_hist
, (ofp
, filename
), FILE * ofp AND
const char *filename
)
174 struct gmon_hist_hdr hdr
;
181 tag
= GMON_TAG_TIME_HIST
;
182 put_vma (core_bfd
, s_lowpc
, (bfd_byte
*) hdr
.low_pc
);
183 put_vma (core_bfd
, s_highpc
, (bfd_byte
*) hdr
.high_pc
);
184 bfd_put_32 (core_bfd
, hist_num_bins
, (bfd_byte
*) hdr
.hist_size
);
185 bfd_put_32 (core_bfd
, hz
, (bfd_byte
*) hdr
.prof_rate
);
186 strncpy (hdr
.dimen
, hist_dimension
, sizeof (hdr
.dimen
));
187 hdr
.dimen_abbrev
= hist_dimension_abbrev
;
189 if (fwrite (&tag
, sizeof (tag
), 1, ofp
) != 1
190 || fwrite (&hdr
, sizeof (hdr
), 1, ofp
) != 1)
196 for (i
= 0; i
< hist_num_bins
; ++i
)
198 bfd_put_16 (core_bfd
, hist_sample
[i
], (bfd_byte
*) & count
[0]);
199 if (fwrite (&count
[0], sizeof (count
), 1, ofp
) != 1)
209 * Calculate scaled entry point addresses (to save time in
210 * hist_assign_samples), and, on architectures that have procedure
211 * entry masks at the start of a function, possibly push the scaled
212 * entry points over the procedure entry mask, if it turns out that
213 * the entry point is in one bin and the code for a routine is in the
217 scale_and_align_entries ()
220 bfd_vma bin_of_entry
;
223 for (sym
= symtab
.base
; sym
< symtab
.limit
; sym
++)
225 sym
->hist
.scaled_addr
= sym
->addr
/ sizeof (UNIT
);
226 bin_of_entry
= (sym
->hist
.scaled_addr
- lowpc
) / hist_scale
;
227 bin_of_code
= (sym
->hist
.scaled_addr
+ UNITS_TO_CODE
- lowpc
) / hist_scale
;
228 if (bin_of_entry
< bin_of_code
)
231 printf ("[scale_and_align_entries] pushing 0x%lx to 0x%lx\n",
232 sym
->hist
.scaled_addr
,
233 sym
->hist
.scaled_addr
+ UNITS_TO_CODE
));
234 sym
->hist
.scaled_addr
+= UNITS_TO_CODE
;
241 * Assign samples to the symbol to which they belong.
243 * Histogram bin I covers some address range [BIN_LOWPC,BIN_HIGH_PC)
244 * which may overlap one more symbol address ranges. If a symbol
245 * overlaps with the bin's address range by O percent, then O percent
246 * of the bin's count is credited to that symbol.
248 * There are three cases as to where BIN_LOW_PC and BIN_HIGH_PC can be
249 * with respect to the symbol's address range [SYM_LOW_PC,
250 * SYM_HIGH_PC) as shown in the following diagram. OVERLAP computes
251 * the distance (in UNITs) between the arrows, the fraction of the
252 * sample that is to be credited to the symbol which starts at
255 * sym_low_pc sym_high_pc
259 * +-----------------------------------------------+
261 * | ->| |<- ->| |<- ->| |<- |
263 * +---------+ +---------+ +---------+
267 * bin_low_pc bin_high_pc bin_low_pc bin_high_pc bin_low_pc bin_high_pc
269 * For the VAX we assert that samples will never fall in the first two
270 * bytes of any routine, since that is the entry mask, thus we call
271 * scale_and_align_entries() to adjust the entry points if the entry
272 * mask falls in one bin but the code for the routine doesn't start
273 * until the next bin. In conjunction with the alignment of routine
274 * addresses, this should allow us to have only one sample for every
275 * four bytes of text space and never have any overlap (the two end
279 DEFUN_VOID (hist_assign_samples
)
281 bfd_vma bin_low_pc
, bin_high_pc
;
282 bfd_vma sym_low_pc
, sym_high_pc
;
283 bfd_vma overlap
, addr
;
287 /* read samples and assign to symbols: */
288 hist_scale
= highpc
- lowpc
;
289 hist_scale
/= hist_num_bins
;
290 scale_and_align_entries ();
292 /* iterate over all sample bins: */
294 for (i
= 0, j
= 1; i
< hist_num_bins
; ++i
)
296 bin_count
= hist_sample
[i
];
301 bin_low_pc
= lowpc
+ (bfd_vma
) (hist_scale
* i
);
302 bin_high_pc
= lowpc
+ (bfd_vma
) (hist_scale
* (i
+ 1));
306 "[assign_samples] bin_low_pc=0x%lx, bin_high_pc=0x%lx, bin_count=%d\n",
307 sizeof (UNIT
) * bin_low_pc
, sizeof (UNIT
) * bin_high_pc
,
311 /* credit all symbols that are covered by bin I: */
313 for (j
= j
- 1; j
< symtab
.len
; ++j
)
315 sym_low_pc
= symtab
.base
[j
].hist
.scaled_addr
;
316 sym_high_pc
= symtab
.base
[j
+ 1].hist
.scaled_addr
;
318 * If high end of bin is below entry address, go for next
321 if (bin_high_pc
< sym_low_pc
)
326 * If low end of bin is above high end of symbol, go for
329 if (bin_low_pc
>= sym_high_pc
)
334 MIN (bin_high_pc
, sym_high_pc
) - MAX (bin_low_pc
, sym_low_pc
);
339 "[assign_samples] [0x%lx,0x%lx) %s gets %f ticks %ld overlap\n",
340 symtab
.base
[j
].addr
, sizeof (UNIT
) * sym_high_pc
,
341 symtab
.base
[j
].name
, overlap
* time
/ hist_scale
,
343 addr
= symtab
.base
[j
].addr
;
344 credit
= overlap
* time
/ hist_scale
;
346 * Credit symbol if it appears in INCL_FLAT or that
347 * table is empty and it does not appear it in
350 if (sym_lookup (&syms
[INCL_FLAT
], addr
)
351 || (syms
[INCL_FLAT
].len
== 0
352 && !sym_lookup (&syms
[EXCL_FLAT
], addr
)))
354 symtab
.base
[j
].hist
.time
+= credit
;
358 total_time
-= credit
;
363 DBG (SAMPLEDEBUG
, printf ("[assign_samples] total_time %f\n",
369 * Print header for flag histogram profile:
372 DEFUN (print_header
, (prefix
), const char prefix
)
376 sprintf (unit
, "%c%c/call", prefix
, hist_dimension_abbrev
);
378 if (bsd_style_output
)
380 printf ("\ngranularity: each sample hit covers %ld byte(s)",
381 (long) hist_scale
* sizeof (UNIT
));
382 if (total_time
> 0.0)
384 printf (" for %.2f%% of %.2f %s\n\n",
385 100.0 / total_time
, total_time
/ hz
, hist_dimension
);
390 printf ("\nEach sample counts as %g %s.\n", 1.0 / hz
, hist_dimension
);
393 if (total_time
<= 0.0)
395 printf (" no time accumulated\n\n");
396 /* this doesn't hurt since all the numerators will be zero: */
400 printf ("%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
401 "% ", "cumulative", "self ", "", "self ", "total ", "");
402 printf ("%5.5s %9.9s %8.8s %8.8s %8.8s %8.8s %-8.8s\n",
403 "time", hist_dimension
, hist_dimension
, "calls", unit
, unit
,
409 DEFUN (print_line
, (sym
, scale
), Sym
* sym AND
double scale
)
411 if (ignore_zeros
&& sym
->ncalls
== 0 && sym
->hist
.time
== 0)
416 accum_time
+= sym
->hist
.time
;
417 if (bsd_style_output
)
419 printf ("%5.1f %10.2f %8.2f",
420 total_time
> 0.0 ? 100 * sym
->hist
.time
/ total_time
: 0.0,
421 accum_time
/ hz
, sym
->hist
.time
/ hz
);
425 printf ("%6.2f %9.2f %8.2f",
426 total_time
> 0.0 ? 100 * sym
->hist
.time
/ total_time
: 0.0,
427 accum_time
/ hz
, sym
->hist
.time
/ hz
);
431 printf (" %8d %8.2f %8.2f ",
432 sym
->ncalls
, scale
* sym
->hist
.time
/ hz
/ sym
->ncalls
,
433 scale
* (sym
->hist
.time
+ sym
->cg
.child_time
) / hz
/ sym
->ncalls
);
437 printf (" %8.8s %8.8s %8.8s ", "", "", "");
439 if (bsd_style_output
)
445 print_name_only (sym
);
452 * Compare LP and RP. The primary comparison key is execution time,
453 * the secondary is number of invocation, and the tertiary is the
454 * lexicographic order of the function names.
457 DEFUN (cmp_time
, (lp
, rp
), const PTR lp AND
const PTR rp
)
459 const Sym
*left
= *(const Sym
**) lp
;
460 const Sym
*right
= *(const Sym
**) rp
;
464 time_diff
= right
->hist
.time
- left
->hist
.time
;
474 call_diff
= right
->ncalls
- left
->ncalls
;
484 return strcmp (left
->name
, right
->name
);
489 * Print the flat histogram profile.
492 DEFUN_VOID (hist_print
)
494 Sym
**time_sorted_syms
, *top_dog
, *sym
;
495 int index
, log_scale
;
496 double top_time
, time
;
501 first_output
= FALSE
;
509 if (bsd_style_output
)
511 if (print_descriptions
)
513 printf ("\n\n\nflat profile:\n");
519 printf ("Flat profile:\n");
522 * Sort the symbol table by time (call-count and name as secondary
523 * and tertiary keys):
525 time_sorted_syms
= (Sym
**) xmalloc (symtab
.len
* sizeof (Sym
*));
526 for (index
= 0; index
< symtab
.len
; ++index
)
528 time_sorted_syms
[index
] = &symtab
.base
[index
];
530 qsort (time_sorted_syms
, symtab
.len
, sizeof (Sym
*), cmp_time
);
532 if (bsd_style_output
)
534 log_scale
= 5; /* milli-seconds is BSD-default */
539 * Search for symbol with highest per-call execution time and
545 for (index
= 0; index
< symtab
.len
; ++index
)
547 sym
= time_sorted_syms
[index
];
550 time
= (sym
->hist
.time
+ sym
->cg
.child_time
) / sym
->ncalls
;
558 if (top_dog
&& top_dog
->ncalls
&& top_time
> 0.0)
561 while (SItab
[log_scale
].scale
* top_time
< 1000.0
562 && log_scale
< sizeof (SItab
) / sizeof (SItab
[0]) - 1)
570 * For now, the dimension is always seconds. In the future, we
571 * may also want to support other (pseudo-)dimensions (such as
572 * I-cache misses etc.).
574 print_header (SItab
[log_scale
].prefix
);
575 for (index
= 0; index
< symtab
.len
; ++index
)
577 addr
= time_sorted_syms
[index
]->addr
;
579 * Print symbol if its in INCL_FLAT table or that table
580 * is empty and the symbol is not in EXCL_FLAT.
582 if (sym_lookup (&syms
[INCL_FLAT
], addr
)
583 || (syms
[INCL_FLAT
].len
== 0
584 && !sym_lookup (&syms
[EXCL_FLAT
], addr
)))
586 print_line (time_sorted_syms
[index
], SItab
[log_scale
].scale
);
589 free (time_sorted_syms
);
591 if (print_descriptions
&& !bsd_style_output
)
This page took 0.040548 seconds and 4 git commands to generate.