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_root
) : 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 hists__collapse_insert_entry(struct hists
*self
,
215 struct rb_root
*root
,
216 struct hist_entry
*he
)
218 struct rb_node
**p
= &root
->rb_node
;
219 struct rb_node
*parent
= NULL
;
220 struct hist_entry
*iter
;
225 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
227 cmp
= hist_entry__collapse(iter
, he
);
230 iter
->period
+= he
->period
;
231 if (symbol_conf
.use_callchain
) {
232 callchain_cursor_reset(&self
->callchain_cursor
);
233 callchain_merge(&self
->callchain_cursor
, iter
->callchain
,
236 hist_entry__free(he
);
246 rb_link_node(&he
->rb_node
, parent
, p
);
247 rb_insert_color(&he
->rb_node
, root
);
251 void hists__collapse_resort(struct hists
*self
)
254 struct rb_node
*next
;
255 struct hist_entry
*n
;
257 if (!sort__need_collapse
)
261 next
= rb_first(&self
->entries
);
262 self
->nr_entries
= 0;
263 hists__reset_col_len(self
);
266 n
= rb_entry(next
, struct hist_entry
, rb_node
);
267 next
= rb_next(&n
->rb_node
);
269 rb_erase(&n
->rb_node
, &self
->entries
);
270 if (hists__collapse_insert_entry(self
, &tmp
, n
))
271 hists__inc_nr_entries(self
, n
);
278 * reverse the map, sort on period.
281 static void __hists__insert_output_entry(struct rb_root
*entries
,
282 struct hist_entry
*he
,
283 u64 min_callchain_hits
)
285 struct rb_node
**p
= &entries
->rb_node
;
286 struct rb_node
*parent
= NULL
;
287 struct hist_entry
*iter
;
289 if (symbol_conf
.use_callchain
)
290 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
291 min_callchain_hits
, &callchain_param
);
295 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
297 if (he
->period
> iter
->period
)
303 rb_link_node(&he
->rb_node
, parent
, p
);
304 rb_insert_color(&he
->rb_node
, entries
);
307 void hists__output_resort(struct hists
*self
)
310 struct rb_node
*next
;
311 struct hist_entry
*n
;
312 u64 min_callchain_hits
;
314 min_callchain_hits
= self
->stats
.total_period
* (callchain_param
.min_percent
/ 100);
317 next
= rb_first(&self
->entries
);
319 self
->nr_entries
= 0;
320 hists__reset_col_len(self
);
323 n
= rb_entry(next
, struct hist_entry
, rb_node
);
324 next
= rb_next(&n
->rb_node
);
326 rb_erase(&n
->rb_node
, &self
->entries
);
327 __hists__insert_output_entry(&tmp
, n
, min_callchain_hits
);
328 hists__inc_nr_entries(self
, n
);
334 static size_t callchain__fprintf_left_margin(FILE *fp
, int left_margin
)
337 int ret
= fprintf(fp
, " ");
339 for (i
= 0; i
< left_margin
; i
++)
340 ret
+= fprintf(fp
, " ");
345 static size_t ipchain__fprintf_graph_line(FILE *fp
, int depth
, int depth_mask
,
349 size_t ret
= callchain__fprintf_left_margin(fp
, left_margin
);
351 for (i
= 0; i
< depth
; i
++)
352 if (depth_mask
& (1 << i
))
353 ret
+= fprintf(fp
, "| ");
355 ret
+= fprintf(fp
, " ");
357 ret
+= fprintf(fp
, "\n");
362 static size_t ipchain__fprintf_graph(FILE *fp
, struct callchain_list
*chain
,
363 int depth
, int depth_mask
, int period
,
364 u64 total_samples
, u64 hits
,
370 ret
+= callchain__fprintf_left_margin(fp
, left_margin
);
371 for (i
= 0; i
< depth
; i
++) {
372 if (depth_mask
& (1 << i
))
373 ret
+= fprintf(fp
, "|");
375 ret
+= fprintf(fp
, " ");
376 if (!period
&& i
== depth
- 1) {
379 percent
= hits
* 100.0 / total_samples
;
380 ret
+= percent_color_fprintf(fp
, "--%2.2f%%-- ", percent
);
382 ret
+= fprintf(fp
, "%s", " ");
385 ret
+= fprintf(fp
, "%s\n", chain
->ms
.sym
->name
);
387 ret
+= fprintf(fp
, "%p\n", (void *)(long)chain
->ip
);
392 static struct symbol
*rem_sq_bracket
;
393 static struct callchain_list rem_hits
;
395 static void init_rem_hits(void)
397 rem_sq_bracket
= malloc(sizeof(*rem_sq_bracket
) + 6);
398 if (!rem_sq_bracket
) {
399 fprintf(stderr
, "Not enough memory to display remaining hits\n");
403 strcpy(rem_sq_bracket
->name
, "[...]");
404 rem_hits
.ms
.sym
= rem_sq_bracket
;
407 static size_t __callchain__fprintf_graph(FILE *fp
, struct callchain_node
*self
,
408 u64 total_samples
, int depth
,
409 int depth_mask
, int left_margin
)
411 struct rb_node
*node
, *next
;
412 struct callchain_node
*child
;
413 struct callchain_list
*chain
;
414 int new_depth_mask
= depth_mask
;
419 uint entries_printed
= 0;
421 if (callchain_param
.mode
== CHAIN_GRAPH_REL
)
422 new_total
= self
->children_hit
;
424 new_total
= total_samples
;
426 remaining
= new_total
;
428 node
= rb_first(&self
->rb_root
);
432 child
= rb_entry(node
, struct callchain_node
, rb_node
);
433 cumul
= cumul_hits(child
);
437 * The depth mask manages the output of pipes that show
438 * the depth. We don't want to keep the pipes of the current
439 * level for the last child of this depth.
440 * Except if we have remaining filtered hits. They will
441 * supersede the last child
443 next
= rb_next(node
);
444 if (!next
&& (callchain_param
.mode
!= CHAIN_GRAPH_REL
|| !remaining
))
445 new_depth_mask
&= ~(1 << (depth
- 1));
448 * But we keep the older depth mask for the line separator
449 * to keep the level link until we reach the last child
451 ret
+= ipchain__fprintf_graph_line(fp
, depth
, depth_mask
,
454 list_for_each_entry(chain
, &child
->val
, list
) {
455 ret
+= ipchain__fprintf_graph(fp
, chain
, depth
,
461 ret
+= __callchain__fprintf_graph(fp
, child
, new_total
,
463 new_depth_mask
| (1 << depth
),
466 if (++entries_printed
== callchain_param
.print_limit
)
470 if (callchain_param
.mode
== CHAIN_GRAPH_REL
&&
471 remaining
&& remaining
!= new_total
) {
476 new_depth_mask
&= ~(1 << (depth
- 1));
478 ret
+= ipchain__fprintf_graph(fp
, &rem_hits
, depth
,
479 new_depth_mask
, 0, new_total
,
480 remaining
, left_margin
);
486 static size_t callchain__fprintf_graph(FILE *fp
, struct callchain_node
*self
,
487 u64 total_samples
, int left_margin
)
489 struct callchain_list
*chain
;
490 bool printed
= false;
493 u32 entries_printed
= 0;
495 list_for_each_entry(chain
, &self
->val
, list
) {
496 if (!i
++ && sort__first_dimension
== SORT_SYM
)
500 ret
+= callchain__fprintf_left_margin(fp
, left_margin
);
501 ret
+= fprintf(fp
, "|\n");
502 ret
+= callchain__fprintf_left_margin(fp
, left_margin
);
503 ret
+= fprintf(fp
, "---");
508 ret
+= callchain__fprintf_left_margin(fp
, left_margin
);
511 ret
+= fprintf(fp
, " %s\n", chain
->ms
.sym
->name
);
513 ret
+= fprintf(fp
, " %p\n", (void *)(long)chain
->ip
);
515 if (++entries_printed
== callchain_param
.print_limit
)
519 ret
+= __callchain__fprintf_graph(fp
, self
, total_samples
, 1, 1, left_margin
);
524 static size_t callchain__fprintf_flat(FILE *fp
, struct callchain_node
*self
,
527 struct callchain_list
*chain
;
533 ret
+= callchain__fprintf_flat(fp
, self
->parent
, total_samples
);
536 list_for_each_entry(chain
, &self
->val
, list
) {
537 if (chain
->ip
>= PERF_CONTEXT_MAX
)
540 ret
+= fprintf(fp
, " %s\n", chain
->ms
.sym
->name
);
542 ret
+= fprintf(fp
, " %p\n",
543 (void *)(long)chain
->ip
);
549 static size_t hist_entry_callchain__fprintf(FILE *fp
, struct hist_entry
*self
,
550 u64 total_samples
, int left_margin
)
552 struct rb_node
*rb_node
;
553 struct callchain_node
*chain
;
555 u32 entries_printed
= 0;
557 rb_node
= rb_first(&self
->sorted_chain
);
561 chain
= rb_entry(rb_node
, struct callchain_node
, rb_node
);
562 percent
= chain
->hit
* 100.0 / total_samples
;
563 switch (callchain_param
.mode
) {
565 ret
+= percent_color_fprintf(fp
, " %6.2f%%\n",
567 ret
+= callchain__fprintf_flat(fp
, chain
, total_samples
);
569 case CHAIN_GRAPH_ABS
: /* Falldown */
570 case CHAIN_GRAPH_REL
:
571 ret
+= callchain__fprintf_graph(fp
, chain
, total_samples
,
577 ret
+= fprintf(fp
, "\n");
578 if (++entries_printed
== callchain_param
.print_limit
)
580 rb_node
= rb_next(rb_node
);
586 int hist_entry__snprintf(struct hist_entry
*self
, char *s
, size_t size
,
587 struct hists
*hists
, struct hists
*pair_hists
,
588 bool show_displacement
, long displacement
,
589 bool color
, u64 session_total
)
591 struct sort_entry
*se
;
592 u64 period
, total
, period_sys
, period_us
, period_guest_sys
, period_guest_us
;
593 const char *sep
= symbol_conf
.field_sep
;
596 if (symbol_conf
.exclude_other
&& !self
->parent
)
600 period
= self
->pair
? self
->pair
->period
: 0;
601 total
= pair_hists
->stats
.total_period
;
602 period_sys
= self
->pair
? self
->pair
->period_sys
: 0;
603 period_us
= self
->pair
? self
->pair
->period_us
: 0;
604 period_guest_sys
= self
->pair
? self
->pair
->period_guest_sys
: 0;
605 period_guest_us
= self
->pair
? self
->pair
->period_guest_us
: 0;
607 period
= self
->period
;
608 total
= session_total
;
609 period_sys
= self
->period_sys
;
610 period_us
= self
->period_us
;
611 period_guest_sys
= self
->period_guest_sys
;
612 period_guest_us
= self
->period_guest_us
;
617 ret
= percent_color_snprintf(s
, size
,
618 sep
? "%.2f" : " %6.2f%%",
619 (period
* 100.0) / total
);
621 ret
= snprintf(s
, size
, sep
? "%.2f" : " %6.2f%%",
622 (period
* 100.0) / total
);
623 if (symbol_conf
.show_cpu_utilization
) {
624 ret
+= percent_color_snprintf(s
+ ret
, size
- ret
,
625 sep
? "%.2f" : " %6.2f%%",
626 (period_sys
* 100.0) / total
);
627 ret
+= percent_color_snprintf(s
+ ret
, size
- ret
,
628 sep
? "%.2f" : " %6.2f%%",
629 (period_us
* 100.0) / total
);
631 ret
+= percent_color_snprintf(s
+ ret
,
633 sep
? "%.2f" : " %6.2f%%",
634 (period_guest_sys
* 100.0) /
636 ret
+= percent_color_snprintf(s
+ ret
,
638 sep
? "%.2f" : " %6.2f%%",
639 (period_guest_us
* 100.0) /
644 ret
= snprintf(s
, size
, sep
? "%" PRIu64
: "%12" PRIu64
" ", period
);
646 if (symbol_conf
.show_nr_samples
) {
648 ret
+= snprintf(s
+ ret
, size
- ret
, "%c%" PRIu64
, *sep
, period
);
650 ret
+= snprintf(s
+ ret
, size
- ret
, "%11" PRIu64
, period
);
655 double old_percent
= 0, new_percent
= 0, diff
;
658 old_percent
= (period
* 100.0) / total
;
659 if (session_total
> 0)
660 new_percent
= (self
->period
* 100.0) / session_total
;
662 diff
= new_percent
- old_percent
;
664 if (fabs(diff
) >= 0.01)
665 snprintf(bf
, sizeof(bf
), "%+4.2F%%", diff
);
667 snprintf(bf
, sizeof(bf
), " ");
670 ret
+= snprintf(s
+ ret
, size
- ret
, "%c%s", *sep
, bf
);
672 ret
+= snprintf(s
+ ret
, size
- ret
, "%11.11s", bf
);
674 if (show_displacement
) {
676 snprintf(bf
, sizeof(bf
), "%+4ld", displacement
);
678 snprintf(bf
, sizeof(bf
), " ");
681 ret
+= snprintf(s
+ ret
, size
- ret
, "%c%s", *sep
, bf
);
683 ret
+= snprintf(s
+ ret
, size
- ret
, "%6.6s", bf
);
687 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
691 ret
+= snprintf(s
+ ret
, size
- ret
, "%s", sep
?: " ");
692 ret
+= se
->se_snprintf(self
, s
+ ret
, size
- ret
,
693 hists__col_len(hists
, se
->se_width_idx
));
699 int hist_entry__fprintf(struct hist_entry
*self
, struct hists
*hists
,
700 struct hists
*pair_hists
, bool show_displacement
,
701 long displacement
, FILE *fp
, u64 session_total
)
704 hist_entry__snprintf(self
, bf
, sizeof(bf
), hists
, pair_hists
,
705 show_displacement
, displacement
,
706 true, session_total
);
707 return fprintf(fp
, "%s\n", bf
);
710 static size_t hist_entry__fprintf_callchain(struct hist_entry
*self
,
711 struct hists
*hists
, FILE *fp
,
716 if (sort__first_dimension
== SORT_COMM
) {
717 struct sort_entry
*se
= list_first_entry(&hist_entry__sort_list
,
719 left_margin
= hists__col_len(hists
, se
->se_width_idx
);
720 left_margin
-= thread__comm_len(self
->thread
);
723 return hist_entry_callchain__fprintf(fp
, self
, session_total
,
727 size_t hists__fprintf(struct hists
*self
, struct hists
*pair
,
728 bool show_displacement
, FILE *fp
)
730 struct sort_entry
*se
;
733 unsigned long position
= 1;
734 long displacement
= 0;
736 const char *sep
= symbol_conf
.field_sep
;
737 const char *col_width
= symbol_conf
.col_width_list_str
;
741 fprintf(fp
, "# %s", pair
? "Baseline" : "Overhead");
743 if (symbol_conf
.show_nr_samples
) {
745 fprintf(fp
, "%cSamples", *sep
);
747 fputs(" Samples ", fp
);
750 if (symbol_conf
.show_cpu_utilization
) {
752 ret
+= fprintf(fp
, "%csys", *sep
);
753 ret
+= fprintf(fp
, "%cus", *sep
);
755 ret
+= fprintf(fp
, "%cguest sys", *sep
);
756 ret
+= fprintf(fp
, "%cguest us", *sep
);
759 ret
+= fprintf(fp
, " sys ");
760 ret
+= fprintf(fp
, " us ");
762 ret
+= fprintf(fp
, " guest sys ");
763 ret
+= fprintf(fp
, " guest us ");
770 ret
+= fprintf(fp
, "%cDelta", *sep
);
772 ret
+= fprintf(fp
, " Delta ");
774 if (show_displacement
) {
776 ret
+= fprintf(fp
, "%cDisplacement", *sep
);
778 ret
+= fprintf(fp
, " Displ");
782 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
786 fprintf(fp
, "%c%s", *sep
, se
->se_header
);
789 width
= strlen(se
->se_header
);
790 if (symbol_conf
.col_width_list_str
) {
792 hists__set_col_len(self
, se
->se_width_idx
,
794 col_width
= strchr(col_width
, ',');
799 if (!hists__new_col_len(self
, se
->se_width_idx
, width
))
800 width
= hists__col_len(self
, se
->se_width_idx
);
801 fprintf(fp
, " %*s", width
, se
->se_header
);
808 fprintf(fp
, "# ........");
809 if (symbol_conf
.show_nr_samples
)
810 fprintf(fp
, " ..........");
812 fprintf(fp
, " ..........");
813 if (show_displacement
)
814 fprintf(fp
, " .....");
816 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
823 width
= hists__col_len(self
, se
->se_width_idx
);
825 width
= strlen(se
->se_header
);
826 for (i
= 0; i
< width
; i
++)
830 fprintf(fp
, "\n#\n");
833 for (nd
= rb_first(&self
->entries
); nd
; nd
= rb_next(nd
)) {
834 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
836 if (show_displacement
) {
838 displacement
= ((long)h
->pair
->position
-
844 ret
+= hist_entry__fprintf(h
, self
, pair
, show_displacement
,
845 displacement
, fp
, self
->stats
.total_period
);
847 if (symbol_conf
.use_callchain
)
848 ret
+= hist_entry__fprintf_callchain(h
, self
, fp
,
849 self
->stats
.total_period
);
850 if (h
->ms
.map
== NULL
&& verbose
> 1) {
851 __map_groups__fprintf_maps(&h
->thread
->mg
,
852 MAP__FUNCTION
, verbose
, fp
);
853 fprintf(fp
, "%.10s end\n", graph_dotted_line
);
857 free(rem_sq_bracket
);
863 * See hists__fprintf to match the column widths
865 unsigned int hists__sort_list_width(struct hists
*self
)
867 struct sort_entry
*se
;
868 int ret
= 9; /* total % */
870 if (symbol_conf
.show_cpu_utilization
) {
871 ret
+= 7; /* count_sys % */
872 ret
+= 6; /* count_us % */
874 ret
+= 13; /* count_guest_sys % */
875 ret
+= 12; /* count_guest_us % */
879 if (symbol_conf
.show_nr_samples
)
882 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
884 ret
+= 2 + hists__col_len(self
, se
->se_width_idx
);
886 if (verbose
) /* Addr + origin */
887 ret
+= 3 + BITS_PER_LONG
/ 4;
892 static void hists__remove_entry_filter(struct hists
*self
, struct hist_entry
*h
,
893 enum hist_filter filter
)
895 h
->filtered
&= ~(1 << filter
);
901 self
->nr_entries
+= h
->nr_rows
;
903 self
->stats
.total_period
+= h
->period
;
904 self
->stats
.nr_events
[PERF_RECORD_SAMPLE
] += h
->nr_events
;
906 hists__calc_col_len(self
, h
);
909 void hists__filter_by_dso(struct hists
*self
, const struct dso
*dso
)
913 self
->nr_entries
= self
->stats
.total_period
= 0;
914 self
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
915 hists__reset_col_len(self
);
917 for (nd
= rb_first(&self
->entries
); nd
; nd
= rb_next(nd
)) {
918 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
920 if (symbol_conf
.exclude_other
&& !h
->parent
)
923 if (dso
!= NULL
&& (h
->ms
.map
== NULL
|| h
->ms
.map
->dso
!= dso
)) {
924 h
->filtered
|= (1 << HIST_FILTER__DSO
);
928 hists__remove_entry_filter(self
, h
, HIST_FILTER__DSO
);
932 void hists__filter_by_thread(struct hists
*self
, const struct thread
*thread
)
936 self
->nr_entries
= self
->stats
.total_period
= 0;
937 self
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
938 hists__reset_col_len(self
);
940 for (nd
= rb_first(&self
->entries
); nd
; nd
= rb_next(nd
)) {
941 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
943 if (thread
!= NULL
&& h
->thread
!= thread
) {
944 h
->filtered
|= (1 << HIST_FILTER__THREAD
);
948 hists__remove_entry_filter(self
, h
, HIST_FILTER__THREAD
);
952 static int symbol__alloc_hist(struct symbol
*self
)
954 struct sym_priv
*priv
= symbol__priv(self
);
955 const int size
= (sizeof(*priv
->hist
) +
956 (self
->end
- self
->start
) * sizeof(u64
));
958 priv
->hist
= zalloc(size
);
959 return priv
->hist
== NULL
? -1 : 0;
962 int hist_entry__inc_addr_samples(struct hist_entry
*self
, u64 ip
)
964 unsigned int sym_size
, offset
;
965 struct symbol
*sym
= self
->ms
.sym
;
966 struct sym_priv
*priv
;
969 if (!sym
|| !self
->ms
.map
)
972 priv
= symbol__priv(sym
);
973 if (priv
->hist
== NULL
&& symbol__alloc_hist(sym
) < 0)
976 sym_size
= sym
->end
- sym
->start
;
977 offset
= ip
- sym
->start
;
979 pr_debug3("%s: ip=%#" PRIx64
"\n", __func__
, self
->ms
.map
->unmap_ip(self
->ms
.map
, ip
));
981 if (offset
>= sym_size
)
988 pr_debug3("%#" PRIx64
" %s: period++ [ip: %#" PRIx64
", %#" PRIx64
989 "] => %" PRIu64
"\n", self
->ms
.sym
->start
, self
->ms
.sym
->name
,
990 ip
, ip
- self
->ms
.sym
->start
, h
->ip
[offset
]);
994 static struct objdump_line
*objdump_line__new(s64 offset
, char *line
, size_t privsize
)
996 struct objdump_line
*self
= malloc(sizeof(*self
) + privsize
);
999 self
->offset
= offset
;
1006 void objdump_line__free(struct objdump_line
*self
)
1012 static void objdump__add_line(struct list_head
*head
, struct objdump_line
*line
)
1014 list_add_tail(&line
->node
, head
);
1017 struct objdump_line
*objdump__get_next_ip_line(struct list_head
*head
,
1018 struct objdump_line
*pos
)
1020 list_for_each_entry_continue(pos
, head
, node
)
1021 if (pos
->offset
>= 0)
1027 static int hist_entry__parse_objdump_line(struct hist_entry
*self
, FILE *file
,
1028 struct list_head
*head
, size_t privsize
)
1030 struct symbol
*sym
= self
->ms
.sym
;
1031 struct objdump_line
*objdump_line
;
1032 char *line
= NULL
, *tmp
, *tmp2
, *c
;
1034 s64 line_ip
, offset
= -1;
1036 if (getline(&line
, &line_len
, file
) < 0)
1042 while (line_len
!= 0 && isspace(line
[line_len
- 1]))
1043 line
[--line_len
] = '\0';
1045 c
= strchr(line
, '\n');
1052 * Strip leading spaces:
1063 * Parse hexa addresses followed by ':'
1065 line_ip
= strtoull(tmp
, &tmp2
, 16);
1066 if (*tmp2
!= ':' || tmp
== tmp2
|| tmp2
[1] == '\0')
1070 if (line_ip
!= -1) {
1071 u64 start
= map__rip_2objdump(self
->ms
.map
, sym
->start
),
1072 end
= map__rip_2objdump(self
->ms
.map
, sym
->end
);
1074 offset
= line_ip
- start
;
1075 if (offset
< 0 || (u64
)line_ip
> end
)
1079 objdump_line
= objdump_line__new(offset
, line
, privsize
);
1080 if (objdump_line
== NULL
) {
1084 objdump__add_line(head
, objdump_line
);
1089 int hist_entry__annotate(struct hist_entry
*self
, struct list_head
*head
,
1092 struct symbol
*sym
= self
->ms
.sym
;
1093 struct map
*map
= self
->ms
.map
;
1094 struct dso
*dso
= map
->dso
;
1095 char *filename
= dso__build_id_filename(dso
, NULL
, 0);
1096 bool free_filename
= true;
1097 char command
[PATH_MAX
* 2];
1101 char symfs_filename
[PATH_MAX
];
1104 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
1105 symbol_conf
.symfs
, filename
);
1108 if (filename
== NULL
) {
1109 if (dso
->has_build_id
) {
1110 pr_err("Can't annotate %s: not enough memory\n",
1115 } else if (readlink(symfs_filename
, command
, sizeof(command
)) < 0 ||
1116 strstr(command
, "[kernel.kallsyms]") ||
1117 access(symfs_filename
, R_OK
)) {
1121 * If we don't have build-ids or the build-id file isn't in the
1122 * cache, or is just a kallsyms file, well, lets hope that this
1123 * DSO is the same as when 'perf record' ran.
1125 filename
= dso
->long_name
;
1126 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
1127 symbol_conf
.symfs
, filename
);
1128 free_filename
= false;
1131 if (dso
->origin
== DSO__ORIG_KERNEL
) {
1132 if (dso
->annotate_warned
)
1133 goto out_free_filename
;
1135 dso
->annotate_warned
= 1;
1136 pr_err("Can't annotate %s: No vmlinux file was found in the "
1137 "path\n", sym
->name
);
1138 goto out_free_filename
;
1141 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64
", end=%#" PRIx64
"\n", __func__
,
1142 filename
, sym
->name
, map
->unmap_ip(map
, sym
->start
),
1143 map
->unmap_ip(map
, sym
->end
));
1145 len
= sym
->end
- sym
->start
;
1147 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1148 dso
, dso
->long_name
, sym
, sym
->name
);
1150 snprintf(command
, sizeof(command
),
1151 "objdump --start-address=0x%016" PRIx64
" --stop-address=0x%016" PRIx64
" -dS -C %s|grep -v %s|expand",
1152 map__rip_2objdump(map
, sym
->start
),
1153 map__rip_2objdump(map
, sym
->end
),
1154 symfs_filename
, filename
);
1156 pr_debug("Executing: %s\n", command
);
1158 file
= popen(command
, "r");
1160 goto out_free_filename
;
1163 if (hist_entry__parse_objdump_line(self
, file
, head
, privsize
) < 0)
1173 void hists__inc_nr_events(struct hists
*self
, u32 type
)
1175 ++self
->stats
.nr_events
[0];
1176 ++self
->stats
.nr_events
[type
];
1179 size_t hists__fprintf_nr_events(struct hists
*self
, FILE *fp
)
1184 for (i
= 0; i
< PERF_RECORD_HEADER_MAX
; ++i
) {
1185 const char *name
= event__get_event_name(i
);
1187 if (!strcmp(name
, "UNKNOWN"))
1190 ret
+= fprintf(fp
, "%16s events: %10d\n", name
,
1191 self
->stats
.nr_events
[i
]);