14 struct callchain_param callchain_param
= {
15 .mode
= CHAIN_GRAPH_REL
,
19 u16
hists__col_len(struct hists
*self
, enum hist_column col
)
21 return self
->col_len
[col
];
24 void hists__set_col_len(struct hists
*self
, enum hist_column col
, u16 len
)
26 self
->col_len
[col
] = len
;
29 bool hists__new_col_len(struct hists
*self
, enum hist_column col
, u16 len
)
31 if (len
> hists__col_len(self
, col
)) {
32 hists__set_col_len(self
, col
, len
);
38 static void hists__reset_col_len(struct hists
*self
)
42 for (col
= 0; col
< HISTC_NR_COLS
; ++col
)
43 hists__set_col_len(self
, col
, 0);
46 static void hists__calc_col_len(struct hists
*self
, struct hist_entry
*h
)
51 hists__new_col_len(self
, HISTC_SYMBOL
, h
->ms
.sym
->namelen
);
53 len
= thread__comm_len(h
->thread
);
54 if (hists__new_col_len(self
, HISTC_COMM
, len
))
55 hists__set_col_len(self
, HISTC_THREAD
, len
+ 6);
58 len
= dso__name_len(h
->ms
.map
->dso
);
59 hists__new_col_len(self
, HISTC_DSO
, len
);
63 static void hist_entry__add_cpumode_period(struct hist_entry
*self
,
64 unsigned int cpumode
, u64 period
)
67 case PERF_RECORD_MISC_KERNEL
:
68 self
->period_sys
+= period
;
70 case PERF_RECORD_MISC_USER
:
71 self
->period_us
+= period
;
73 case PERF_RECORD_MISC_GUEST_KERNEL
:
74 self
->period_guest_sys
+= period
;
76 case PERF_RECORD_MISC_GUEST_USER
:
77 self
->period_guest_us
+= period
;
85 * histogram, sorted on item, collects periods
88 static struct hist_entry
*hist_entry__new(struct hist_entry
*template)
90 size_t callchain_size
= symbol_conf
.use_callchain
? sizeof(struct callchain_node
) : 0;
91 struct hist_entry
*self
= malloc(sizeof(*self
) + callchain_size
);
97 self
->ms
.map
->referenced
= true;
98 if (symbol_conf
.use_callchain
)
99 callchain_init(self
->callchain
);
105 static void hists__inc_nr_entries(struct hists
*self
, struct hist_entry
*h
)
108 hists__calc_col_len(self
, h
);
113 static u8
symbol__parent_filter(const struct symbol
*parent
)
115 if (symbol_conf
.exclude_other
&& parent
== NULL
)
116 return 1 << HIST_FILTER__PARENT
;
120 struct hist_entry
*__hists__add_entry(struct hists
*self
,
121 struct addr_location
*al
,
122 struct symbol
*sym_parent
, u64 period
)
124 struct rb_node
**p
= &self
->entries
.rb_node
;
125 struct rb_node
*parent
= NULL
;
126 struct hist_entry
*he
;
127 struct hist_entry entry
= {
128 .thread
= al
->thread
,
137 .parent
= sym_parent
,
138 .filtered
= symbol__parent_filter(sym_parent
),
144 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
146 cmp
= hist_entry__cmp(&entry
, he
);
149 he
->period
+= period
;
160 he
= hist_entry__new(&entry
);
163 rb_link_node(&he
->rb_node
, parent
, p
);
164 rb_insert_color(&he
->rb_node
, &self
->entries
);
165 hists__inc_nr_entries(self
, he
);
167 hist_entry__add_cpumode_period(he
, al
->cpumode
, period
);
172 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
174 struct sort_entry
*se
;
177 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
178 cmp
= se
->se_cmp(left
, right
);
187 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
189 struct sort_entry
*se
;
192 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
193 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
195 f
= se
->se_collapse
?: se
->se_cmp
;
197 cmp
= f(left
, right
);
205 void hist_entry__free(struct hist_entry
*he
)
211 * collapse the histogram
214 static bool collapse__insert_entry(struct rb_root
*root
, struct hist_entry
*he
)
216 struct rb_node
**p
= &root
->rb_node
;
217 struct rb_node
*parent
= NULL
;
218 struct hist_entry
*iter
;
223 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
225 cmp
= hist_entry__collapse(iter
, he
);
228 iter
->period
+= he
->period
;
229 hist_entry__free(he
);
239 rb_link_node(&he
->rb_node
, parent
, p
);
240 rb_insert_color(&he
->rb_node
, root
);
244 void hists__collapse_resort(struct hists
*self
)
247 struct rb_node
*next
;
248 struct hist_entry
*n
;
250 if (!sort__need_collapse
)
254 next
= rb_first(&self
->entries
);
255 self
->nr_entries
= 0;
256 hists__reset_col_len(self
);
259 n
= rb_entry(next
, struct hist_entry
, rb_node
);
260 next
= rb_next(&n
->rb_node
);
262 rb_erase(&n
->rb_node
, &self
->entries
);
263 if (collapse__insert_entry(&tmp
, n
))
264 hists__inc_nr_entries(self
, n
);
271 * reverse the map, sort on period.
274 static void __hists__insert_output_entry(struct rb_root
*entries
,
275 struct hist_entry
*he
,
276 u64 min_callchain_hits
)
278 struct rb_node
**p
= &entries
->rb_node
;
279 struct rb_node
*parent
= NULL
;
280 struct hist_entry
*iter
;
282 if (symbol_conf
.use_callchain
)
283 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
284 min_callchain_hits
, &callchain_param
);
288 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
290 if (he
->period
> iter
->period
)
296 rb_link_node(&he
->rb_node
, parent
, p
);
297 rb_insert_color(&he
->rb_node
, entries
);
300 void hists__output_resort(struct hists
*self
)
303 struct rb_node
*next
;
304 struct hist_entry
*n
;
305 u64 min_callchain_hits
;
307 min_callchain_hits
= self
->stats
.total_period
* (callchain_param
.min_percent
/ 100);
310 next
= rb_first(&self
->entries
);
312 self
->nr_entries
= 0;
313 hists__reset_col_len(self
);
316 n
= rb_entry(next
, struct hist_entry
, rb_node
);
317 next
= rb_next(&n
->rb_node
);
319 rb_erase(&n
->rb_node
, &self
->entries
);
320 __hists__insert_output_entry(&tmp
, n
, min_callchain_hits
);
321 hists__inc_nr_entries(self
, n
);
327 static size_t callchain__fprintf_left_margin(FILE *fp
, int left_margin
)
330 int ret
= fprintf(fp
, " ");
332 for (i
= 0; i
< left_margin
; i
++)
333 ret
+= fprintf(fp
, " ");
338 static size_t ipchain__fprintf_graph_line(FILE *fp
, int depth
, int depth_mask
,
342 size_t ret
= callchain__fprintf_left_margin(fp
, left_margin
);
344 for (i
= 0; i
< depth
; i
++)
345 if (depth_mask
& (1 << i
))
346 ret
+= fprintf(fp
, "| ");
348 ret
+= fprintf(fp
, " ");
350 ret
+= fprintf(fp
, "\n");
355 static size_t ipchain__fprintf_graph(FILE *fp
, struct callchain_list
*chain
,
356 int depth
, int depth_mask
, int period
,
357 u64 total_samples
, int hits
,
363 ret
+= callchain__fprintf_left_margin(fp
, left_margin
);
364 for (i
= 0; i
< depth
; i
++) {
365 if (depth_mask
& (1 << i
))
366 ret
+= fprintf(fp
, "|");
368 ret
+= fprintf(fp
, " ");
369 if (!period
&& i
== depth
- 1) {
372 percent
= hits
* 100.0 / total_samples
;
373 ret
+= percent_color_fprintf(fp
, "--%2.2f%%-- ", percent
);
375 ret
+= fprintf(fp
, "%s", " ");
378 ret
+= fprintf(fp
, "%s\n", chain
->ms
.sym
->name
);
380 ret
+= fprintf(fp
, "%p\n", (void *)(long)chain
->ip
);
385 static struct symbol
*rem_sq_bracket
;
386 static struct callchain_list rem_hits
;
388 static void init_rem_hits(void)
390 rem_sq_bracket
= malloc(sizeof(*rem_sq_bracket
) + 6);
391 if (!rem_sq_bracket
) {
392 fprintf(stderr
, "Not enough memory to display remaining hits\n");
396 strcpy(rem_sq_bracket
->name
, "[...]");
397 rem_hits
.ms
.sym
= rem_sq_bracket
;
400 static size_t __callchain__fprintf_graph(FILE *fp
, struct callchain_node
*self
,
401 u64 total_samples
, int depth
,
402 int depth_mask
, int left_margin
)
404 struct rb_node
*node
, *next
;
405 struct callchain_node
*child
;
406 struct callchain_list
*chain
;
407 int new_depth_mask
= depth_mask
;
412 uint entries_printed
= 0;
414 if (callchain_param
.mode
== CHAIN_GRAPH_REL
)
415 new_total
= self
->children_hit
;
417 new_total
= total_samples
;
419 remaining
= new_total
;
421 node
= rb_first(&self
->rb_root
);
425 child
= rb_entry(node
, struct callchain_node
, rb_node
);
426 cumul
= cumul_hits(child
);
430 * The depth mask manages the output of pipes that show
431 * the depth. We don't want to keep the pipes of the current
432 * level for the last child of this depth.
433 * Except if we have remaining filtered hits. They will
434 * supersede the last child
436 next
= rb_next(node
);
437 if (!next
&& (callchain_param
.mode
!= CHAIN_GRAPH_REL
|| !remaining
))
438 new_depth_mask
&= ~(1 << (depth
- 1));
441 * But we keep the older depth mask for the line separator
442 * to keep the level link until we reach the last child
444 ret
+= ipchain__fprintf_graph_line(fp
, depth
, depth_mask
,
447 list_for_each_entry(chain
, &child
->val
, list
) {
448 ret
+= ipchain__fprintf_graph(fp
, chain
, depth
,
454 ret
+= __callchain__fprintf_graph(fp
, child
, new_total
,
456 new_depth_mask
| (1 << depth
),
459 if (++entries_printed
== callchain_param
.print_limit
)
463 if (callchain_param
.mode
== CHAIN_GRAPH_REL
&&
464 remaining
&& remaining
!= new_total
) {
469 new_depth_mask
&= ~(1 << (depth
- 1));
471 ret
+= ipchain__fprintf_graph(fp
, &rem_hits
, depth
,
472 new_depth_mask
, 0, new_total
,
473 remaining
, left_margin
);
479 static size_t callchain__fprintf_graph(FILE *fp
, struct callchain_node
*self
,
480 u64 total_samples
, int left_margin
)
482 struct callchain_list
*chain
;
483 bool printed
= false;
486 u32 entries_printed
= 0;
488 list_for_each_entry(chain
, &self
->val
, list
) {
489 if (!i
++ && sort__first_dimension
== SORT_SYM
)
493 ret
+= callchain__fprintf_left_margin(fp
, left_margin
);
494 ret
+= fprintf(fp
, "|\n");
495 ret
+= callchain__fprintf_left_margin(fp
, left_margin
);
496 ret
+= fprintf(fp
, "---");
501 ret
+= callchain__fprintf_left_margin(fp
, left_margin
);
504 ret
+= fprintf(fp
, " %s\n", chain
->ms
.sym
->name
);
506 ret
+= fprintf(fp
, " %p\n", (void *)(long)chain
->ip
);
508 if (++entries_printed
== callchain_param
.print_limit
)
512 ret
+= __callchain__fprintf_graph(fp
, self
, total_samples
, 1, 1, left_margin
);
517 static size_t callchain__fprintf_flat(FILE *fp
, struct callchain_node
*self
,
520 struct callchain_list
*chain
;
526 ret
+= callchain__fprintf_flat(fp
, self
->parent
, total_samples
);
529 list_for_each_entry(chain
, &self
->val
, list
) {
530 if (chain
->ip
>= PERF_CONTEXT_MAX
)
533 ret
+= fprintf(fp
, " %s\n", chain
->ms
.sym
->name
);
535 ret
+= fprintf(fp
, " %p\n",
536 (void *)(long)chain
->ip
);
542 static size_t hist_entry_callchain__fprintf(FILE *fp
, struct hist_entry
*self
,
543 u64 total_samples
, int left_margin
)
545 struct rb_node
*rb_node
;
546 struct callchain_node
*chain
;
548 u32 entries_printed
= 0;
550 rb_node
= rb_first(&self
->sorted_chain
);
554 chain
= rb_entry(rb_node
, struct callchain_node
, rb_node
);
555 percent
= chain
->hit
* 100.0 / total_samples
;
556 switch (callchain_param
.mode
) {
558 ret
+= percent_color_fprintf(fp
, " %6.2f%%\n",
560 ret
+= callchain__fprintf_flat(fp
, chain
, total_samples
);
562 case CHAIN_GRAPH_ABS
: /* Falldown */
563 case CHAIN_GRAPH_REL
:
564 ret
+= callchain__fprintf_graph(fp
, chain
, total_samples
,
570 ret
+= fprintf(fp
, "\n");
571 if (++entries_printed
== callchain_param
.print_limit
)
573 rb_node
= rb_next(rb_node
);
579 int hist_entry__snprintf(struct hist_entry
*self
, char *s
, size_t size
,
580 struct hists
*hists
, struct hists
*pair_hists
,
581 bool show_displacement
, long displacement
,
582 bool color
, u64 session_total
)
584 struct sort_entry
*se
;
585 u64 period
, total
, period_sys
, period_us
, period_guest_sys
, period_guest_us
;
586 const char *sep
= symbol_conf
.field_sep
;
589 if (symbol_conf
.exclude_other
&& !self
->parent
)
593 period
= self
->pair
? self
->pair
->period
: 0;
594 total
= pair_hists
->stats
.total_period
;
595 period_sys
= self
->pair
? self
->pair
->period_sys
: 0;
596 period_us
= self
->pair
? self
->pair
->period_us
: 0;
597 period_guest_sys
= self
->pair
? self
->pair
->period_guest_sys
: 0;
598 period_guest_us
= self
->pair
? self
->pair
->period_guest_us
: 0;
600 period
= self
->period
;
601 total
= session_total
;
602 period_sys
= self
->period_sys
;
603 period_us
= self
->period_us
;
604 period_guest_sys
= self
->period_guest_sys
;
605 period_guest_us
= self
->period_guest_us
;
610 ret
= percent_color_snprintf(s
, size
,
611 sep
? "%.2f" : " %6.2f%%",
612 (period
* 100.0) / total
);
614 ret
= snprintf(s
, size
, sep
? "%.2f" : " %6.2f%%",
615 (period
* 100.0) / total
);
616 if (symbol_conf
.show_cpu_utilization
) {
617 ret
+= percent_color_snprintf(s
+ ret
, size
- ret
,
618 sep
? "%.2f" : " %6.2f%%",
619 (period_sys
* 100.0) / total
);
620 ret
+= percent_color_snprintf(s
+ ret
, size
- ret
,
621 sep
? "%.2f" : " %6.2f%%",
622 (period_us
* 100.0) / total
);
624 ret
+= percent_color_snprintf(s
+ ret
,
626 sep
? "%.2f" : " %6.2f%%",
627 (period_guest_sys
* 100.0) /
629 ret
+= percent_color_snprintf(s
+ ret
,
631 sep
? "%.2f" : " %6.2f%%",
632 (period_guest_us
* 100.0) /
637 ret
= snprintf(s
, size
, sep
? "%lld" : "%12lld ", period
);
639 if (symbol_conf
.show_nr_samples
) {
641 ret
+= snprintf(s
+ ret
, size
- ret
, "%c%lld", *sep
, period
);
643 ret
+= snprintf(s
+ ret
, size
- ret
, "%11lld", period
);
648 double old_percent
= 0, new_percent
= 0, diff
;
651 old_percent
= (period
* 100.0) / total
;
652 if (session_total
> 0)
653 new_percent
= (self
->period
* 100.0) / session_total
;
655 diff
= new_percent
- old_percent
;
657 if (fabs(diff
) >= 0.01)
658 snprintf(bf
, sizeof(bf
), "%+4.2F%%", diff
);
660 snprintf(bf
, sizeof(bf
), " ");
663 ret
+= snprintf(s
+ ret
, size
- ret
, "%c%s", *sep
, bf
);
665 ret
+= snprintf(s
+ ret
, size
- ret
, "%11.11s", bf
);
667 if (show_displacement
) {
669 snprintf(bf
, sizeof(bf
), "%+4ld", displacement
);
671 snprintf(bf
, sizeof(bf
), " ");
674 ret
+= snprintf(s
+ ret
, size
- ret
, "%c%s", *sep
, bf
);
676 ret
+= snprintf(s
+ ret
, size
- ret
, "%6.6s", bf
);
680 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
684 ret
+= snprintf(s
+ ret
, size
- ret
, "%s", sep
?: " ");
685 ret
+= se
->se_snprintf(self
, s
+ ret
, size
- ret
,
686 hists__col_len(hists
, se
->se_width_idx
));
692 int hist_entry__fprintf(struct hist_entry
*self
, struct hists
*hists
,
693 struct hists
*pair_hists
, bool show_displacement
,
694 long displacement
, FILE *fp
, u64 session_total
)
697 hist_entry__snprintf(self
, bf
, sizeof(bf
), hists
, pair_hists
,
698 show_displacement
, displacement
,
699 true, session_total
);
700 return fprintf(fp
, "%s\n", bf
);
703 static size_t hist_entry__fprintf_callchain(struct hist_entry
*self
,
704 struct hists
*hists
, FILE *fp
,
709 if (sort__first_dimension
== SORT_COMM
) {
710 struct sort_entry
*se
= list_first_entry(&hist_entry__sort_list
,
712 left_margin
= hists__col_len(hists
, se
->se_width_idx
);
713 left_margin
-= thread__comm_len(self
->thread
);
716 return hist_entry_callchain__fprintf(fp
, self
, session_total
,
720 size_t hists__fprintf(struct hists
*self
, struct hists
*pair
,
721 bool show_displacement
, FILE *fp
)
723 struct sort_entry
*se
;
726 unsigned long position
= 1;
727 long displacement
= 0;
729 const char *sep
= symbol_conf
.field_sep
;
730 const char *col_width
= symbol_conf
.col_width_list_str
;
734 fprintf(fp
, "# %s", pair
? "Baseline" : "Overhead");
736 if (symbol_conf
.show_nr_samples
) {
738 fprintf(fp
, "%cSamples", *sep
);
740 fputs(" Samples ", fp
);
743 if (symbol_conf
.show_cpu_utilization
) {
745 ret
+= fprintf(fp
, "%csys", *sep
);
746 ret
+= fprintf(fp
, "%cus", *sep
);
748 ret
+= fprintf(fp
, "%cguest sys", *sep
);
749 ret
+= fprintf(fp
, "%cguest us", *sep
);
752 ret
+= fprintf(fp
, " sys ");
753 ret
+= fprintf(fp
, " us ");
755 ret
+= fprintf(fp
, " guest sys ");
756 ret
+= fprintf(fp
, " guest us ");
763 ret
+= fprintf(fp
, "%cDelta", *sep
);
765 ret
+= fprintf(fp
, " Delta ");
767 if (show_displacement
) {
769 ret
+= fprintf(fp
, "%cDisplacement", *sep
);
771 ret
+= fprintf(fp
, " Displ");
775 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
779 fprintf(fp
, "%c%s", *sep
, se
->se_header
);
782 width
= strlen(se
->se_header
);
783 if (symbol_conf
.col_width_list_str
) {
785 hists__set_col_len(self
, se
->se_width_idx
,
787 col_width
= strchr(col_width
, ',');
792 if (!hists__new_col_len(self
, se
->se_width_idx
, width
))
793 width
= hists__col_len(self
, se
->se_width_idx
);
794 fprintf(fp
, " %*s", width
, se
->se_header
);
801 fprintf(fp
, "# ........");
802 if (symbol_conf
.show_nr_samples
)
803 fprintf(fp
, " ..........");
805 fprintf(fp
, " ..........");
806 if (show_displacement
)
807 fprintf(fp
, " .....");
809 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
816 width
= hists__col_len(self
, se
->se_width_idx
);
818 width
= strlen(se
->se_header
);
819 for (i
= 0; i
< width
; i
++)
823 fprintf(fp
, "\n#\n");
826 for (nd
= rb_first(&self
->entries
); nd
; nd
= rb_next(nd
)) {
827 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
829 if (show_displacement
) {
831 displacement
= ((long)h
->pair
->position
-
837 ret
+= hist_entry__fprintf(h
, self
, pair
, show_displacement
,
838 displacement
, fp
, self
->stats
.total_period
);
840 if (symbol_conf
.use_callchain
)
841 ret
+= hist_entry__fprintf_callchain(h
, self
, fp
,
842 self
->stats
.total_period
);
843 if (h
->ms
.map
== NULL
&& verbose
> 1) {
844 __map_groups__fprintf_maps(&h
->thread
->mg
,
845 MAP__FUNCTION
, verbose
, fp
);
846 fprintf(fp
, "%.10s end\n", graph_dotted_line
);
850 free(rem_sq_bracket
);
856 * See hists__fprintf to match the column widths
858 unsigned int hists__sort_list_width(struct hists
*self
)
860 struct sort_entry
*se
;
861 int ret
= 9; /* total % */
863 if (symbol_conf
.show_cpu_utilization
) {
864 ret
+= 7; /* count_sys % */
865 ret
+= 6; /* count_us % */
867 ret
+= 13; /* count_guest_sys % */
868 ret
+= 12; /* count_guest_us % */
872 if (symbol_conf
.show_nr_samples
)
875 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
877 ret
+= 2 + hists__col_len(self
, se
->se_width_idx
);
882 static void hists__remove_entry_filter(struct hists
*self
, struct hist_entry
*h
,
883 enum hist_filter filter
)
885 h
->filtered
&= ~(1 << filter
);
891 self
->nr_entries
+= h
->nr_rows
;
893 self
->stats
.total_period
+= h
->period
;
894 self
->stats
.nr_events
[PERF_RECORD_SAMPLE
] += h
->nr_events
;
896 hists__calc_col_len(self
, h
);
899 void hists__filter_by_dso(struct hists
*self
, const struct dso
*dso
)
903 self
->nr_entries
= self
->stats
.total_period
= 0;
904 self
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
905 hists__reset_col_len(self
);
907 for (nd
= rb_first(&self
->entries
); nd
; nd
= rb_next(nd
)) {
908 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
910 if (symbol_conf
.exclude_other
&& !h
->parent
)
913 if (dso
!= NULL
&& (h
->ms
.map
== NULL
|| h
->ms
.map
->dso
!= dso
)) {
914 h
->filtered
|= (1 << HIST_FILTER__DSO
);
918 hists__remove_entry_filter(self
, h
, HIST_FILTER__DSO
);
922 void hists__filter_by_thread(struct hists
*self
, const struct thread
*thread
)
926 self
->nr_entries
= self
->stats
.total_period
= 0;
927 self
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
928 hists__reset_col_len(self
);
930 for (nd
= rb_first(&self
->entries
); nd
; nd
= rb_next(nd
)) {
931 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
933 if (thread
!= NULL
&& h
->thread
!= thread
) {
934 h
->filtered
|= (1 << HIST_FILTER__THREAD
);
938 hists__remove_entry_filter(self
, h
, HIST_FILTER__THREAD
);
942 static int symbol__alloc_hist(struct symbol
*self
)
944 struct sym_priv
*priv
= symbol__priv(self
);
945 const int size
= (sizeof(*priv
->hist
) +
946 (self
->end
- self
->start
) * sizeof(u64
));
948 priv
->hist
= zalloc(size
);
949 return priv
->hist
== NULL
? -1 : 0;
952 int hist_entry__inc_addr_samples(struct hist_entry
*self
, u64 ip
)
954 unsigned int sym_size
, offset
;
955 struct symbol
*sym
= self
->ms
.sym
;
956 struct sym_priv
*priv
;
959 if (!sym
|| !self
->ms
.map
)
962 priv
= symbol__priv(sym
);
963 if (priv
->hist
== NULL
&& symbol__alloc_hist(sym
) < 0)
966 sym_size
= sym
->end
- sym
->start
;
967 offset
= ip
- sym
->start
;
969 pr_debug3("%s: ip=%#Lx\n", __func__
, self
->ms
.map
->unmap_ip(self
->ms
.map
, ip
));
971 if (offset
>= sym_size
)
978 pr_debug3("%#Lx %s: period++ [ip: %#Lx, %#Lx] => %Ld\n", self
->ms
.sym
->start
,
979 self
->ms
.sym
->name
, ip
, ip
- self
->ms
.sym
->start
, h
->ip
[offset
]);
983 static struct objdump_line
*objdump_line__new(s64 offset
, char *line
)
985 struct objdump_line
*self
= malloc(sizeof(*self
));
988 self
->offset
= offset
;
995 void objdump_line__free(struct objdump_line
*self
)
1001 static void objdump__add_line(struct list_head
*head
, struct objdump_line
*line
)
1003 list_add_tail(&line
->node
, head
);
1006 struct objdump_line
*objdump__get_next_ip_line(struct list_head
*head
,
1007 struct objdump_line
*pos
)
1009 list_for_each_entry_continue(pos
, head
, node
)
1010 if (pos
->offset
>= 0)
1016 static int hist_entry__parse_objdump_line(struct hist_entry
*self
, FILE *file
,
1017 struct list_head
*head
)
1019 struct symbol
*sym
= self
->ms
.sym
;
1020 struct objdump_line
*objdump_line
;
1021 char *line
= NULL
, *tmp
, *tmp2
, *c
;
1023 s64 line_ip
, offset
= -1;
1025 if (getline(&line
, &line_len
, file
) < 0)
1031 while (line_len
!= 0 && isspace(line
[line_len
- 1]))
1032 line
[--line_len
] = '\0';
1034 c
= strchr(line
, '\n');
1041 * Strip leading spaces:
1052 * Parse hexa addresses followed by ':'
1054 line_ip
= strtoull(tmp
, &tmp2
, 16);
1055 if (*tmp2
!= ':' || tmp
== tmp2
|| tmp2
[1] == '\0')
1059 if (line_ip
!= -1) {
1060 u64 start
= map__rip_2objdump(self
->ms
.map
, sym
->start
),
1061 end
= map__rip_2objdump(self
->ms
.map
, sym
->end
);
1063 offset
= line_ip
- start
;
1064 if (offset
< 0 || (u64
)line_ip
> end
)
1068 objdump_line
= objdump_line__new(offset
, line
);
1069 if (objdump_line
== NULL
) {
1073 objdump__add_line(head
, objdump_line
);
1078 int hist_entry__annotate(struct hist_entry
*self
, struct list_head
*head
)
1080 struct symbol
*sym
= self
->ms
.sym
;
1081 struct map
*map
= self
->ms
.map
;
1082 struct dso
*dso
= map
->dso
;
1083 char *filename
= dso__build_id_filename(dso
, NULL
, 0);
1084 bool free_filename
= true;
1085 char command
[PATH_MAX
* 2];
1090 if (filename
== NULL
) {
1091 if (dso
->has_build_id
) {
1092 pr_err("Can't annotate %s: not enough memory\n",
1097 } else if (readlink(filename
, command
, sizeof(command
)) < 0 ||
1098 strstr(command
, "[kernel.kallsyms]") ||
1099 access(filename
, R_OK
)) {
1103 * If we don't have build-ids or the build-id file isn't in the
1104 * cache, or is just a kallsyms file, well, lets hope that this
1105 * DSO is the same as when 'perf record' ran.
1107 filename
= dso
->long_name
;
1108 free_filename
= false;
1111 if (dso
->origin
== DSO__ORIG_KERNEL
) {
1112 if (dso
->annotate_warned
)
1113 goto out_free_filename
;
1115 dso
->annotate_warned
= 1;
1116 pr_err("Can't annotate %s: No vmlinux file was found in the "
1117 "path\n", sym
->name
);
1118 goto out_free_filename
;
1121 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__
,
1122 filename
, sym
->name
, map
->unmap_ip(map
, sym
->start
),
1123 map
->unmap_ip(map
, sym
->end
));
1125 len
= sym
->end
- sym
->start
;
1127 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1128 dso
, dso
->long_name
, sym
, sym
->name
);
1130 snprintf(command
, sizeof(command
),
1131 "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS -C %s|grep -v %s|expand",
1132 map__rip_2objdump(map
, sym
->start
),
1133 map__rip_2objdump(map
, sym
->end
),
1134 filename
, filename
);
1136 pr_debug("Executing: %s\n", command
);
1138 file
= popen(command
, "r");
1140 goto out_free_filename
;
1143 if (hist_entry__parse_objdump_line(self
, file
, head
) < 0)
1153 void hists__inc_nr_events(struct hists
*self
, u32 type
)
1155 ++self
->stats
.nr_events
[0];
1156 ++self
->stats
.nr_events
[type
];
1159 size_t hists__fprintf_nr_events(struct hists
*self
, FILE *fp
)
1164 for (i
= 0; i
< PERF_RECORD_HEADER_MAX
; ++i
) {
1165 if (!event__name
[i
])
1167 ret
+= fprintf(fp
, "%10s events: %10d\n",
1168 event__name
[i
], self
->stats
.nr_events
[i
]);