13 #include <traceevent/event-parse.h>
14 #include "mem-events.h"
15 #include <linux/kernel.h>
18 const char default_parent_pattern
[] = "^sys_|^do_page_fault";
19 const char *parent_pattern
= default_parent_pattern
;
20 const char *default_sort_order
= "comm,dso,symbol";
21 const char default_branch_sort_order
[] = "comm,dso_from,symbol_from,symbol_to,cycles";
22 const char default_mem_sort_order
[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
23 const char default_top_sort_order
[] = "dso,symbol";
24 const char default_diff_sort_order
[] = "dso,symbol";
25 const char default_tracepoint_sort_order
[] = "trace";
26 const char *sort_order
;
27 const char *field_order
;
28 regex_t ignore_callees_regex
;
29 int have_ignore_callees
= 0;
30 enum sort_mode sort__mode
= SORT_MODE__NORMAL
;
33 * Replaces all occurrences of a char used with the:
35 * -t, --field-separator
37 * option, that uses a special separator character and don't pad with spaces,
38 * replacing all occurances of this separator in symbol names (and other
39 * output) with a '.' character, that thus it's the only non valid separator.
41 static int repsep_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
47 n
= vsnprintf(bf
, size
, fmt
, ap
);
48 if (symbol_conf
.field_sep
&& n
> 0) {
52 sep
= strchr(sep
, *symbol_conf
.field_sep
);
65 static int64_t cmp_null(const void *l
, const void *r
)
78 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
80 return right
->thread
->tid
- left
->thread
->tid
;
83 static int hist_entry__thread_snprintf(struct hist_entry
*he
, char *bf
,
84 size_t size
, unsigned int width
)
86 const char *comm
= thread__comm_str(he
->thread
);
88 width
= max(7U, width
) - 8;
89 return repsep_snprintf(bf
, size
, "%7d:%-*.*s", he
->thread
->tid
,
90 width
, width
, comm
?: "");
93 static int hist_entry__thread_filter(struct hist_entry
*he
, int type
, const void *arg
)
95 const struct thread
*th
= arg
;
97 if (type
!= HIST_FILTER__THREAD
)
100 return th
&& he
->thread
!= th
;
103 struct sort_entry sort_thread
= {
104 .se_header
= " Pid:Command",
105 .se_cmp
= sort__thread_cmp
,
106 .se_snprintf
= hist_entry__thread_snprintf
,
107 .se_filter
= hist_entry__thread_filter
,
108 .se_width_idx
= HISTC_THREAD
,
114 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
116 /* Compare the addr that should be unique among comm */
117 return strcmp(comm__str(right
->comm
), comm__str(left
->comm
));
121 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
123 /* Compare the addr that should be unique among comm */
124 return strcmp(comm__str(right
->comm
), comm__str(left
->comm
));
128 sort__comm_sort(struct hist_entry
*left
, struct hist_entry
*right
)
130 return strcmp(comm__str(right
->comm
), comm__str(left
->comm
));
133 static int hist_entry__comm_snprintf(struct hist_entry
*he
, char *bf
,
134 size_t size
, unsigned int width
)
136 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, comm__str(he
->comm
));
139 struct sort_entry sort_comm
= {
140 .se_header
= "Command",
141 .se_cmp
= sort__comm_cmp
,
142 .se_collapse
= sort__comm_collapse
,
143 .se_sort
= sort__comm_sort
,
144 .se_snprintf
= hist_entry__comm_snprintf
,
145 .se_filter
= hist_entry__thread_filter
,
146 .se_width_idx
= HISTC_COMM
,
151 static int64_t _sort__dso_cmp(struct map
*map_l
, struct map
*map_r
)
153 struct dso
*dso_l
= map_l
? map_l
->dso
: NULL
;
154 struct dso
*dso_r
= map_r
? map_r
->dso
: NULL
;
155 const char *dso_name_l
, *dso_name_r
;
157 if (!dso_l
|| !dso_r
)
158 return cmp_null(dso_r
, dso_l
);
161 dso_name_l
= dso_l
->long_name
;
162 dso_name_r
= dso_r
->long_name
;
164 dso_name_l
= dso_l
->short_name
;
165 dso_name_r
= dso_r
->short_name
;
168 return strcmp(dso_name_l
, dso_name_r
);
172 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
174 return _sort__dso_cmp(right
->ms
.map
, left
->ms
.map
);
177 static int _hist_entry__dso_snprintf(struct map
*map
, char *bf
,
178 size_t size
, unsigned int width
)
180 if (map
&& map
->dso
) {
181 const char *dso_name
= verbose
> 0 ? map
->dso
->long_name
:
182 map
->dso
->short_name
;
183 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, dso_name
);
186 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "[unknown]");
189 static int hist_entry__dso_snprintf(struct hist_entry
*he
, char *bf
,
190 size_t size
, unsigned int width
)
192 return _hist_entry__dso_snprintf(he
->ms
.map
, bf
, size
, width
);
195 static int hist_entry__dso_filter(struct hist_entry
*he
, int type
, const void *arg
)
197 const struct dso
*dso
= arg
;
199 if (type
!= HIST_FILTER__DSO
)
202 return dso
&& (!he
->ms
.map
|| he
->ms
.map
->dso
!= dso
);
205 struct sort_entry sort_dso
= {
206 .se_header
= "Shared Object",
207 .se_cmp
= sort__dso_cmp
,
208 .se_snprintf
= hist_entry__dso_snprintf
,
209 .se_filter
= hist_entry__dso_filter
,
210 .se_width_idx
= HISTC_DSO
,
215 static int64_t _sort__addr_cmp(u64 left_ip
, u64 right_ip
)
217 return (int64_t)(right_ip
- left_ip
);
220 static int64_t _sort__sym_cmp(struct symbol
*sym_l
, struct symbol
*sym_r
)
222 if (!sym_l
|| !sym_r
)
223 return cmp_null(sym_l
, sym_r
);
228 if (sym_l
->start
!= sym_r
->start
)
229 return (int64_t)(sym_r
->start
- sym_l
->start
);
231 return (int64_t)(sym_r
->end
- sym_l
->end
);
235 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
239 if (!left
->ms
.sym
&& !right
->ms
.sym
)
240 return _sort__addr_cmp(left
->ip
, right
->ip
);
243 * comparing symbol address alone is not enough since it's a
244 * relative address within a dso.
246 if (!hists__has(left
->hists
, dso
) || hists__has(right
->hists
, dso
)) {
247 ret
= sort__dso_cmp(left
, right
);
252 return _sort__sym_cmp(left
->ms
.sym
, right
->ms
.sym
);
256 sort__sym_sort(struct hist_entry
*left
, struct hist_entry
*right
)
258 if (!left
->ms
.sym
|| !right
->ms
.sym
)
259 return cmp_null(left
->ms
.sym
, right
->ms
.sym
);
261 return strcmp(right
->ms
.sym
->name
, left
->ms
.sym
->name
);
264 static int _hist_entry__sym_snprintf(struct map
*map
, struct symbol
*sym
,
265 u64 ip
, char level
, char *bf
, size_t size
,
271 char o
= map
? dso__symtab_origin(map
->dso
) : '!';
272 ret
+= repsep_snprintf(bf
, size
, "%-#*llx %c ",
273 BITS_PER_LONG
/ 4 + 2, ip
, o
);
276 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "[%c] ", level
);
278 if (map
->type
== MAP__VARIABLE
) {
279 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%s", sym
->name
);
280 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "+0x%llx",
281 ip
- map
->unmap_ip(map
, sym
->start
));
283 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%.*s",
288 size_t len
= BITS_PER_LONG
/ 4;
289 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-#.*llx",
296 static int hist_entry__sym_snprintf(struct hist_entry
*he
, char *bf
,
297 size_t size
, unsigned int width
)
299 return _hist_entry__sym_snprintf(he
->ms
.map
, he
->ms
.sym
, he
->ip
,
300 he
->level
, bf
, size
, width
);
303 static int hist_entry__sym_filter(struct hist_entry
*he
, int type
, const void *arg
)
305 const char *sym
= arg
;
307 if (type
!= HIST_FILTER__SYMBOL
)
310 return sym
&& (!he
->ms
.sym
|| !strstr(he
->ms
.sym
->name
, sym
));
313 struct sort_entry sort_sym
= {
314 .se_header
= "Symbol",
315 .se_cmp
= sort__sym_cmp
,
316 .se_sort
= sort__sym_sort
,
317 .se_snprintf
= hist_entry__sym_snprintf
,
318 .se_filter
= hist_entry__sym_filter
,
319 .se_width_idx
= HISTC_SYMBOL
,
324 char *hist_entry__get_srcline(struct hist_entry
*he
)
326 struct map
*map
= he
->ms
.map
;
329 return SRCLINE_UNKNOWN
;
331 return get_srcline(map
->dso
, map__rip_2objdump(map
, he
->ip
),
332 he
->ms
.sym
, true, true);
336 sort__srcline_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
339 left
->srcline
= hist_entry__get_srcline(left
);
341 right
->srcline
= hist_entry__get_srcline(right
);
343 return strcmp(right
->srcline
, left
->srcline
);
346 static int hist_entry__srcline_snprintf(struct hist_entry
*he
, char *bf
,
347 size_t size
, unsigned int width
)
350 he
->srcline
= hist_entry__get_srcline(he
);
352 return repsep_snprintf(bf
, size
, "%-.*s", width
, he
->srcline
);
355 struct sort_entry sort_srcline
= {
356 .se_header
= "Source:Line",
357 .se_cmp
= sort__srcline_cmp
,
358 .se_snprintf
= hist_entry__srcline_snprintf
,
359 .se_width_idx
= HISTC_SRCLINE
,
362 /* --sort srcline_from */
365 sort__srcline_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
367 if (!left
->branch_info
->srcline_from
) {
368 struct map
*map
= left
->branch_info
->from
.map
;
370 left
->branch_info
->srcline_from
= SRCLINE_UNKNOWN
;
372 left
->branch_info
->srcline_from
= get_srcline(map
->dso
,
373 map__rip_2objdump(map
,
374 left
->branch_info
->from
.al_addr
),
375 left
->branch_info
->from
.sym
,
378 if (!right
->branch_info
->srcline_from
) {
379 struct map
*map
= right
->branch_info
->from
.map
;
381 right
->branch_info
->srcline_from
= SRCLINE_UNKNOWN
;
383 right
->branch_info
->srcline_from
= get_srcline(map
->dso
,
384 map__rip_2objdump(map
,
385 right
->branch_info
->from
.al_addr
),
386 right
->branch_info
->from
.sym
,
389 return strcmp(right
->branch_info
->srcline_from
, left
->branch_info
->srcline_from
);
392 static int hist_entry__srcline_from_snprintf(struct hist_entry
*he
, char *bf
,
393 size_t size
, unsigned int width
)
395 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, he
->branch_info
->srcline_from
);
398 struct sort_entry sort_srcline_from
= {
399 .se_header
= "From Source:Line",
400 .se_cmp
= sort__srcline_from_cmp
,
401 .se_snprintf
= hist_entry__srcline_from_snprintf
,
402 .se_width_idx
= HISTC_SRCLINE_FROM
,
405 /* --sort srcline_to */
408 sort__srcline_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
410 if (!left
->branch_info
->srcline_to
) {
411 struct map
*map
= left
->branch_info
->to
.map
;
413 left
->branch_info
->srcline_to
= SRCLINE_UNKNOWN
;
415 left
->branch_info
->srcline_to
= get_srcline(map
->dso
,
416 map__rip_2objdump(map
,
417 left
->branch_info
->to
.al_addr
),
418 left
->branch_info
->from
.sym
,
421 if (!right
->branch_info
->srcline_to
) {
422 struct map
*map
= right
->branch_info
->to
.map
;
424 right
->branch_info
->srcline_to
= SRCLINE_UNKNOWN
;
426 right
->branch_info
->srcline_to
= get_srcline(map
->dso
,
427 map__rip_2objdump(map
,
428 right
->branch_info
->to
.al_addr
),
429 right
->branch_info
->to
.sym
,
432 return strcmp(right
->branch_info
->srcline_to
, left
->branch_info
->srcline_to
);
435 static int hist_entry__srcline_to_snprintf(struct hist_entry
*he
, char *bf
,
436 size_t size
, unsigned int width
)
438 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, he
->branch_info
->srcline_to
);
441 struct sort_entry sort_srcline_to
= {
442 .se_header
= "To Source:Line",
443 .se_cmp
= sort__srcline_to_cmp
,
444 .se_snprintf
= hist_entry__srcline_to_snprintf
,
445 .se_width_idx
= HISTC_SRCLINE_TO
,
450 static char no_srcfile
[1];
452 static char *hist_entry__get_srcfile(struct hist_entry
*e
)
455 struct map
*map
= e
->ms
.map
;
460 sf
= __get_srcline(map
->dso
, map__rip_2objdump(map
, e
->ip
),
461 e
->ms
.sym
, false, true, true);
462 if (!strcmp(sf
, SRCLINE_UNKNOWN
))
474 sort__srcfile_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
477 left
->srcfile
= hist_entry__get_srcfile(left
);
479 right
->srcfile
= hist_entry__get_srcfile(right
);
481 return strcmp(right
->srcfile
, left
->srcfile
);
484 static int hist_entry__srcfile_snprintf(struct hist_entry
*he
, char *bf
,
485 size_t size
, unsigned int width
)
488 he
->srcfile
= hist_entry__get_srcfile(he
);
490 return repsep_snprintf(bf
, size
, "%-.*s", width
, he
->srcfile
);
493 struct sort_entry sort_srcfile
= {
494 .se_header
= "Source File",
495 .se_cmp
= sort__srcfile_cmp
,
496 .se_snprintf
= hist_entry__srcfile_snprintf
,
497 .se_width_idx
= HISTC_SRCFILE
,
503 sort__parent_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
505 struct symbol
*sym_l
= left
->parent
;
506 struct symbol
*sym_r
= right
->parent
;
508 if (!sym_l
|| !sym_r
)
509 return cmp_null(sym_l
, sym_r
);
511 return strcmp(sym_r
->name
, sym_l
->name
);
514 static int hist_entry__parent_snprintf(struct hist_entry
*he
, char *bf
,
515 size_t size
, unsigned int width
)
517 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
,
518 he
->parent
? he
->parent
->name
: "[other]");
521 struct sort_entry sort_parent
= {
522 .se_header
= "Parent symbol",
523 .se_cmp
= sort__parent_cmp
,
524 .se_snprintf
= hist_entry__parent_snprintf
,
525 .se_width_idx
= HISTC_PARENT
,
531 sort__cpu_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
533 return right
->cpu
- left
->cpu
;
536 static int hist_entry__cpu_snprintf(struct hist_entry
*he
, char *bf
,
537 size_t size
, unsigned int width
)
539 return repsep_snprintf(bf
, size
, "%*.*d", width
, width
, he
->cpu
);
542 struct sort_entry sort_cpu
= {
544 .se_cmp
= sort__cpu_cmp
,
545 .se_snprintf
= hist_entry__cpu_snprintf
,
546 .se_width_idx
= HISTC_CPU
,
549 /* --sort cgroup_id */
551 static int64_t _sort__cgroup_dev_cmp(u64 left_dev
, u64 right_dev
)
553 return (int64_t)(right_dev
- left_dev
);
556 static int64_t _sort__cgroup_inode_cmp(u64 left_ino
, u64 right_ino
)
558 return (int64_t)(right_ino
- left_ino
);
562 sort__cgroup_id_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
566 ret
= _sort__cgroup_dev_cmp(right
->cgroup_id
.dev
, left
->cgroup_id
.dev
);
570 return _sort__cgroup_inode_cmp(right
->cgroup_id
.ino
,
571 left
->cgroup_id
.ino
);
574 static int hist_entry__cgroup_id_snprintf(struct hist_entry
*he
,
575 char *bf
, size_t size
,
576 unsigned int width __maybe_unused
)
578 return repsep_snprintf(bf
, size
, "%lu/0x%lx", he
->cgroup_id
.dev
,
582 struct sort_entry sort_cgroup_id
= {
583 .se_header
= "cgroup id (dev/inode)",
584 .se_cmp
= sort__cgroup_id_cmp
,
585 .se_snprintf
= hist_entry__cgroup_id_snprintf
,
586 .se_width_idx
= HISTC_CGROUP_ID
,
592 sort__socket_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
594 return right
->socket
- left
->socket
;
597 static int hist_entry__socket_snprintf(struct hist_entry
*he
, char *bf
,
598 size_t size
, unsigned int width
)
600 return repsep_snprintf(bf
, size
, "%*.*d", width
, width
-3, he
->socket
);
603 static int hist_entry__socket_filter(struct hist_entry
*he
, int type
, const void *arg
)
605 int sk
= *(const int *)arg
;
607 if (type
!= HIST_FILTER__SOCKET
)
610 return sk
>= 0 && he
->socket
!= sk
;
613 struct sort_entry sort_socket
= {
614 .se_header
= "Socket",
615 .se_cmp
= sort__socket_cmp
,
616 .se_snprintf
= hist_entry__socket_snprintf
,
617 .se_filter
= hist_entry__socket_filter
,
618 .se_width_idx
= HISTC_SOCKET
,
623 static char *get_trace_output(struct hist_entry
*he
)
625 struct trace_seq seq
;
626 struct perf_evsel
*evsel
;
627 struct pevent_record rec
= {
628 .data
= he
->raw_data
,
629 .size
= he
->raw_size
,
632 evsel
= hists_to_evsel(he
->hists
);
634 trace_seq_init(&seq
);
635 if (symbol_conf
.raw_trace
) {
636 pevent_print_fields(&seq
, he
->raw_data
, he
->raw_size
,
639 pevent_event_info(&seq
, evsel
->tp_format
, &rec
);
642 * Trim the buffer, it starts at 4KB and we're not going to
643 * add anything more to this buffer.
645 return realloc(seq
.buffer
, seq
.len
+ 1);
649 sort__trace_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
651 struct perf_evsel
*evsel
;
653 evsel
= hists_to_evsel(left
->hists
);
654 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
657 if (left
->trace_output
== NULL
)
658 left
->trace_output
= get_trace_output(left
);
659 if (right
->trace_output
== NULL
)
660 right
->trace_output
= get_trace_output(right
);
662 return strcmp(right
->trace_output
, left
->trace_output
);
665 static int hist_entry__trace_snprintf(struct hist_entry
*he
, char *bf
,
666 size_t size
, unsigned int width
)
668 struct perf_evsel
*evsel
;
670 evsel
= hists_to_evsel(he
->hists
);
671 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
672 return scnprintf(bf
, size
, "%-.*s", width
, "N/A");
674 if (he
->trace_output
== NULL
)
675 he
->trace_output
= get_trace_output(he
);
676 return repsep_snprintf(bf
, size
, "%-.*s", width
, he
->trace_output
);
679 struct sort_entry sort_trace
= {
680 .se_header
= "Trace output",
681 .se_cmp
= sort__trace_cmp
,
682 .se_snprintf
= hist_entry__trace_snprintf
,
683 .se_width_idx
= HISTC_TRACE
,
686 /* sort keys for branch stacks */
689 sort__dso_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
691 if (!left
->branch_info
|| !right
->branch_info
)
692 return cmp_null(left
->branch_info
, right
->branch_info
);
694 return _sort__dso_cmp(left
->branch_info
->from
.map
,
695 right
->branch_info
->from
.map
);
698 static int hist_entry__dso_from_snprintf(struct hist_entry
*he
, char *bf
,
699 size_t size
, unsigned int width
)
702 return _hist_entry__dso_snprintf(he
->branch_info
->from
.map
,
705 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
708 static int hist_entry__dso_from_filter(struct hist_entry
*he
, int type
,
711 const struct dso
*dso
= arg
;
713 if (type
!= HIST_FILTER__DSO
)
716 return dso
&& (!he
->branch_info
|| !he
->branch_info
->from
.map
||
717 he
->branch_info
->from
.map
->dso
!= dso
);
721 sort__dso_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
723 if (!left
->branch_info
|| !right
->branch_info
)
724 return cmp_null(left
->branch_info
, right
->branch_info
);
726 return _sort__dso_cmp(left
->branch_info
->to
.map
,
727 right
->branch_info
->to
.map
);
730 static int hist_entry__dso_to_snprintf(struct hist_entry
*he
, char *bf
,
731 size_t size
, unsigned int width
)
734 return _hist_entry__dso_snprintf(he
->branch_info
->to
.map
,
737 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
740 static int hist_entry__dso_to_filter(struct hist_entry
*he
, int type
,
743 const struct dso
*dso
= arg
;
745 if (type
!= HIST_FILTER__DSO
)
748 return dso
&& (!he
->branch_info
|| !he
->branch_info
->to
.map
||
749 he
->branch_info
->to
.map
->dso
!= dso
);
753 sort__sym_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
755 struct addr_map_symbol
*from_l
= &left
->branch_info
->from
;
756 struct addr_map_symbol
*from_r
= &right
->branch_info
->from
;
758 if (!left
->branch_info
|| !right
->branch_info
)
759 return cmp_null(left
->branch_info
, right
->branch_info
);
761 from_l
= &left
->branch_info
->from
;
762 from_r
= &right
->branch_info
->from
;
764 if (!from_l
->sym
&& !from_r
->sym
)
765 return _sort__addr_cmp(from_l
->addr
, from_r
->addr
);
767 return _sort__sym_cmp(from_l
->sym
, from_r
->sym
);
771 sort__sym_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
773 struct addr_map_symbol
*to_l
, *to_r
;
775 if (!left
->branch_info
|| !right
->branch_info
)
776 return cmp_null(left
->branch_info
, right
->branch_info
);
778 to_l
= &left
->branch_info
->to
;
779 to_r
= &right
->branch_info
->to
;
781 if (!to_l
->sym
&& !to_r
->sym
)
782 return _sort__addr_cmp(to_l
->addr
, to_r
->addr
);
784 return _sort__sym_cmp(to_l
->sym
, to_r
->sym
);
787 static int hist_entry__sym_from_snprintf(struct hist_entry
*he
, char *bf
,
788 size_t size
, unsigned int width
)
790 if (he
->branch_info
) {
791 struct addr_map_symbol
*from
= &he
->branch_info
->from
;
793 return _hist_entry__sym_snprintf(from
->map
, from
->sym
, from
->addr
,
794 he
->level
, bf
, size
, width
);
797 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
800 static int hist_entry__sym_to_snprintf(struct hist_entry
*he
, char *bf
,
801 size_t size
, unsigned int width
)
803 if (he
->branch_info
) {
804 struct addr_map_symbol
*to
= &he
->branch_info
->to
;
806 return _hist_entry__sym_snprintf(to
->map
, to
->sym
, to
->addr
,
807 he
->level
, bf
, size
, width
);
810 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
813 static int hist_entry__sym_from_filter(struct hist_entry
*he
, int type
,
816 const char *sym
= arg
;
818 if (type
!= HIST_FILTER__SYMBOL
)
821 return sym
&& !(he
->branch_info
&& he
->branch_info
->from
.sym
&&
822 strstr(he
->branch_info
->from
.sym
->name
, sym
));
825 static int hist_entry__sym_to_filter(struct hist_entry
*he
, int type
,
828 const char *sym
= arg
;
830 if (type
!= HIST_FILTER__SYMBOL
)
833 return sym
&& !(he
->branch_info
&& he
->branch_info
->to
.sym
&&
834 strstr(he
->branch_info
->to
.sym
->name
, sym
));
837 struct sort_entry sort_dso_from
= {
838 .se_header
= "Source Shared Object",
839 .se_cmp
= sort__dso_from_cmp
,
840 .se_snprintf
= hist_entry__dso_from_snprintf
,
841 .se_filter
= hist_entry__dso_from_filter
,
842 .se_width_idx
= HISTC_DSO_FROM
,
845 struct sort_entry sort_dso_to
= {
846 .se_header
= "Target Shared Object",
847 .se_cmp
= sort__dso_to_cmp
,
848 .se_snprintf
= hist_entry__dso_to_snprintf
,
849 .se_filter
= hist_entry__dso_to_filter
,
850 .se_width_idx
= HISTC_DSO_TO
,
853 struct sort_entry sort_sym_from
= {
854 .se_header
= "Source Symbol",
855 .se_cmp
= sort__sym_from_cmp
,
856 .se_snprintf
= hist_entry__sym_from_snprintf
,
857 .se_filter
= hist_entry__sym_from_filter
,
858 .se_width_idx
= HISTC_SYMBOL_FROM
,
861 struct sort_entry sort_sym_to
= {
862 .se_header
= "Target Symbol",
863 .se_cmp
= sort__sym_to_cmp
,
864 .se_snprintf
= hist_entry__sym_to_snprintf
,
865 .se_filter
= hist_entry__sym_to_filter
,
866 .se_width_idx
= HISTC_SYMBOL_TO
,
870 sort__mispredict_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
874 if (!left
->branch_info
|| !right
->branch_info
)
875 return cmp_null(left
->branch_info
, right
->branch_info
);
877 mp
= left
->branch_info
->flags
.mispred
!= right
->branch_info
->flags
.mispred
;
878 p
= left
->branch_info
->flags
.predicted
!= right
->branch_info
->flags
.predicted
;
882 static int hist_entry__mispredict_snprintf(struct hist_entry
*he
, char *bf
,
883 size_t size
, unsigned int width
){
884 static const char *out
= "N/A";
886 if (he
->branch_info
) {
887 if (he
->branch_info
->flags
.predicted
)
889 else if (he
->branch_info
->flags
.mispred
)
893 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, out
);
897 sort__cycles_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
899 if (!left
->branch_info
|| !right
->branch_info
)
900 return cmp_null(left
->branch_info
, right
->branch_info
);
902 return left
->branch_info
->flags
.cycles
-
903 right
->branch_info
->flags
.cycles
;
906 static int hist_entry__cycles_snprintf(struct hist_entry
*he
, char *bf
,
907 size_t size
, unsigned int width
)
909 if (!he
->branch_info
)
910 return scnprintf(bf
, size
, "%-.*s", width
, "N/A");
911 if (he
->branch_info
->flags
.cycles
== 0)
912 return repsep_snprintf(bf
, size
, "%-*s", width
, "-");
913 return repsep_snprintf(bf
, size
, "%-*hd", width
,
914 he
->branch_info
->flags
.cycles
);
917 struct sort_entry sort_cycles
= {
918 .se_header
= "Basic Block Cycles",
919 .se_cmp
= sort__cycles_cmp
,
920 .se_snprintf
= hist_entry__cycles_snprintf
,
921 .se_width_idx
= HISTC_CYCLES
,
924 /* --sort daddr_sym */
926 sort__daddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
928 uint64_t l
= 0, r
= 0;
931 l
= left
->mem_info
->daddr
.addr
;
933 r
= right
->mem_info
->daddr
.addr
;
935 return (int64_t)(r
- l
);
938 static int hist_entry__daddr_snprintf(struct hist_entry
*he
, char *bf
,
939 size_t size
, unsigned int width
)
942 struct map
*map
= NULL
;
943 struct symbol
*sym
= NULL
;
946 addr
= he
->mem_info
->daddr
.addr
;
947 map
= he
->mem_info
->daddr
.map
;
948 sym
= he
->mem_info
->daddr
.sym
;
950 return _hist_entry__sym_snprintf(map
, sym
, addr
, he
->level
, bf
, size
,
955 sort__iaddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
957 uint64_t l
= 0, r
= 0;
960 l
= left
->mem_info
->iaddr
.addr
;
962 r
= right
->mem_info
->iaddr
.addr
;
964 return (int64_t)(r
- l
);
967 static int hist_entry__iaddr_snprintf(struct hist_entry
*he
, char *bf
,
968 size_t size
, unsigned int width
)
971 struct map
*map
= NULL
;
972 struct symbol
*sym
= NULL
;
975 addr
= he
->mem_info
->iaddr
.addr
;
976 map
= he
->mem_info
->iaddr
.map
;
977 sym
= he
->mem_info
->iaddr
.sym
;
979 return _hist_entry__sym_snprintf(map
, sym
, addr
, he
->level
, bf
, size
,
984 sort__dso_daddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
986 struct map
*map_l
= NULL
;
987 struct map
*map_r
= NULL
;
990 map_l
= left
->mem_info
->daddr
.map
;
992 map_r
= right
->mem_info
->daddr
.map
;
994 return _sort__dso_cmp(map_l
, map_r
);
997 static int hist_entry__dso_daddr_snprintf(struct hist_entry
*he
, char *bf
,
998 size_t size
, unsigned int width
)
1000 struct map
*map
= NULL
;
1003 map
= he
->mem_info
->daddr
.map
;
1005 return _hist_entry__dso_snprintf(map
, bf
, size
, width
);
1009 sort__locked_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1011 union perf_mem_data_src data_src_l
;
1012 union perf_mem_data_src data_src_r
;
1015 data_src_l
= left
->mem_info
->data_src
;
1017 data_src_l
.mem_lock
= PERF_MEM_LOCK_NA
;
1019 if (right
->mem_info
)
1020 data_src_r
= right
->mem_info
->data_src
;
1022 data_src_r
.mem_lock
= PERF_MEM_LOCK_NA
;
1024 return (int64_t)(data_src_r
.mem_lock
- data_src_l
.mem_lock
);
1027 static int hist_entry__locked_snprintf(struct hist_entry
*he
, char *bf
,
1028 size_t size
, unsigned int width
)
1032 perf_mem__lck_scnprintf(out
, sizeof(out
), he
->mem_info
);
1033 return repsep_snprintf(bf
, size
, "%.*s", width
, out
);
1037 sort__tlb_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1039 union perf_mem_data_src data_src_l
;
1040 union perf_mem_data_src data_src_r
;
1043 data_src_l
= left
->mem_info
->data_src
;
1045 data_src_l
.mem_dtlb
= PERF_MEM_TLB_NA
;
1047 if (right
->mem_info
)
1048 data_src_r
= right
->mem_info
->data_src
;
1050 data_src_r
.mem_dtlb
= PERF_MEM_TLB_NA
;
1052 return (int64_t)(data_src_r
.mem_dtlb
- data_src_l
.mem_dtlb
);
1055 static int hist_entry__tlb_snprintf(struct hist_entry
*he
, char *bf
,
1056 size_t size
, unsigned int width
)
1060 perf_mem__tlb_scnprintf(out
, sizeof(out
), he
->mem_info
);
1061 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1065 sort__lvl_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1067 union perf_mem_data_src data_src_l
;
1068 union perf_mem_data_src data_src_r
;
1071 data_src_l
= left
->mem_info
->data_src
;
1073 data_src_l
.mem_lvl
= PERF_MEM_LVL_NA
;
1075 if (right
->mem_info
)
1076 data_src_r
= right
->mem_info
->data_src
;
1078 data_src_r
.mem_lvl
= PERF_MEM_LVL_NA
;
1080 return (int64_t)(data_src_r
.mem_lvl
- data_src_l
.mem_lvl
);
1083 static int hist_entry__lvl_snprintf(struct hist_entry
*he
, char *bf
,
1084 size_t size
, unsigned int width
)
1088 perf_mem__lvl_scnprintf(out
, sizeof(out
), he
->mem_info
);
1089 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1093 sort__snoop_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1095 union perf_mem_data_src data_src_l
;
1096 union perf_mem_data_src data_src_r
;
1099 data_src_l
= left
->mem_info
->data_src
;
1101 data_src_l
.mem_snoop
= PERF_MEM_SNOOP_NA
;
1103 if (right
->mem_info
)
1104 data_src_r
= right
->mem_info
->data_src
;
1106 data_src_r
.mem_snoop
= PERF_MEM_SNOOP_NA
;
1108 return (int64_t)(data_src_r
.mem_snoop
- data_src_l
.mem_snoop
);
1111 static int hist_entry__snoop_snprintf(struct hist_entry
*he
, char *bf
,
1112 size_t size
, unsigned int width
)
1116 perf_mem__snp_scnprintf(out
, sizeof(out
), he
->mem_info
);
1117 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1121 sort__dcacheline_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1124 struct map
*l_map
, *r_map
;
1126 if (!left
->mem_info
) return -1;
1127 if (!right
->mem_info
) return 1;
1129 /* group event types together */
1130 if (left
->cpumode
> right
->cpumode
) return -1;
1131 if (left
->cpumode
< right
->cpumode
) return 1;
1133 l_map
= left
->mem_info
->daddr
.map
;
1134 r_map
= right
->mem_info
->daddr
.map
;
1136 /* if both are NULL, jump to sort on al_addr instead */
1137 if (!l_map
&& !r_map
)
1140 if (!l_map
) return -1;
1141 if (!r_map
) return 1;
1143 if (l_map
->maj
> r_map
->maj
) return -1;
1144 if (l_map
->maj
< r_map
->maj
) return 1;
1146 if (l_map
->min
> r_map
->min
) return -1;
1147 if (l_map
->min
< r_map
->min
) return 1;
1149 if (l_map
->ino
> r_map
->ino
) return -1;
1150 if (l_map
->ino
< r_map
->ino
) return 1;
1152 if (l_map
->ino_generation
> r_map
->ino_generation
) return -1;
1153 if (l_map
->ino_generation
< r_map
->ino_generation
) return 1;
1156 * Addresses with no major/minor numbers are assumed to be
1157 * anonymous in userspace. Sort those on pid then address.
1159 * The kernel and non-zero major/minor mapped areas are
1160 * assumed to be unity mapped. Sort those on address.
1163 if ((left
->cpumode
!= PERF_RECORD_MISC_KERNEL
) &&
1164 (!(l_map
->flags
& MAP_SHARED
)) &&
1165 !l_map
->maj
&& !l_map
->min
&& !l_map
->ino
&&
1166 !l_map
->ino_generation
) {
1167 /* userspace anonymous */
1169 if (left
->thread
->pid_
> right
->thread
->pid_
) return -1;
1170 if (left
->thread
->pid_
< right
->thread
->pid_
) return 1;
1174 /* al_addr does all the right addr - start + offset calculations */
1175 l
= cl_address(left
->mem_info
->daddr
.al_addr
);
1176 r
= cl_address(right
->mem_info
->daddr
.al_addr
);
1178 if (l
> r
) return -1;
1179 if (l
< r
) return 1;
1184 static int hist_entry__dcacheline_snprintf(struct hist_entry
*he
, char *bf
,
1185 size_t size
, unsigned int width
)
1189 struct map
*map
= NULL
;
1190 struct symbol
*sym
= NULL
;
1191 char level
= he
->level
;
1194 addr
= cl_address(he
->mem_info
->daddr
.al_addr
);
1195 map
= he
->mem_info
->daddr
.map
;
1196 sym
= he
->mem_info
->daddr
.sym
;
1198 /* print [s] for shared data mmaps */
1199 if ((he
->cpumode
!= PERF_RECORD_MISC_KERNEL
) &&
1200 map
&& (map
->type
== MAP__VARIABLE
) &&
1201 (map
->flags
& MAP_SHARED
) &&
1202 (map
->maj
|| map
->min
|| map
->ino
||
1203 map
->ino_generation
))
1208 return _hist_entry__sym_snprintf(map
, sym
, addr
, level
, bf
, size
,
1212 struct sort_entry sort_mispredict
= {
1213 .se_header
= "Branch Mispredicted",
1214 .se_cmp
= sort__mispredict_cmp
,
1215 .se_snprintf
= hist_entry__mispredict_snprintf
,
1216 .se_width_idx
= HISTC_MISPREDICT
,
1219 static u64
he_weight(struct hist_entry
*he
)
1221 return he
->stat
.nr_events
? he
->stat
.weight
/ he
->stat
.nr_events
: 0;
1225 sort__local_weight_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1227 return he_weight(left
) - he_weight(right
);
1230 static int hist_entry__local_weight_snprintf(struct hist_entry
*he
, char *bf
,
1231 size_t size
, unsigned int width
)
1233 return repsep_snprintf(bf
, size
, "%-*llu", width
, he_weight(he
));
1236 struct sort_entry sort_local_weight
= {
1237 .se_header
= "Local Weight",
1238 .se_cmp
= sort__local_weight_cmp
,
1239 .se_snprintf
= hist_entry__local_weight_snprintf
,
1240 .se_width_idx
= HISTC_LOCAL_WEIGHT
,
1244 sort__global_weight_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1246 return left
->stat
.weight
- right
->stat
.weight
;
1249 static int hist_entry__global_weight_snprintf(struct hist_entry
*he
, char *bf
,
1250 size_t size
, unsigned int width
)
1252 return repsep_snprintf(bf
, size
, "%-*llu", width
, he
->stat
.weight
);
1255 struct sort_entry sort_global_weight
= {
1256 .se_header
= "Weight",
1257 .se_cmp
= sort__global_weight_cmp
,
1258 .se_snprintf
= hist_entry__global_weight_snprintf
,
1259 .se_width_idx
= HISTC_GLOBAL_WEIGHT
,
1262 struct sort_entry sort_mem_daddr_sym
= {
1263 .se_header
= "Data Symbol",
1264 .se_cmp
= sort__daddr_cmp
,
1265 .se_snprintf
= hist_entry__daddr_snprintf
,
1266 .se_width_idx
= HISTC_MEM_DADDR_SYMBOL
,
1269 struct sort_entry sort_mem_iaddr_sym
= {
1270 .se_header
= "Code Symbol",
1271 .se_cmp
= sort__iaddr_cmp
,
1272 .se_snprintf
= hist_entry__iaddr_snprintf
,
1273 .se_width_idx
= HISTC_MEM_IADDR_SYMBOL
,
1276 struct sort_entry sort_mem_daddr_dso
= {
1277 .se_header
= "Data Object",
1278 .se_cmp
= sort__dso_daddr_cmp
,
1279 .se_snprintf
= hist_entry__dso_daddr_snprintf
,
1280 .se_width_idx
= HISTC_MEM_DADDR_DSO
,
1283 struct sort_entry sort_mem_locked
= {
1284 .se_header
= "Locked",
1285 .se_cmp
= sort__locked_cmp
,
1286 .se_snprintf
= hist_entry__locked_snprintf
,
1287 .se_width_idx
= HISTC_MEM_LOCKED
,
1290 struct sort_entry sort_mem_tlb
= {
1291 .se_header
= "TLB access",
1292 .se_cmp
= sort__tlb_cmp
,
1293 .se_snprintf
= hist_entry__tlb_snprintf
,
1294 .se_width_idx
= HISTC_MEM_TLB
,
1297 struct sort_entry sort_mem_lvl
= {
1298 .se_header
= "Memory access",
1299 .se_cmp
= sort__lvl_cmp
,
1300 .se_snprintf
= hist_entry__lvl_snprintf
,
1301 .se_width_idx
= HISTC_MEM_LVL
,
1304 struct sort_entry sort_mem_snoop
= {
1305 .se_header
= "Snoop",
1306 .se_cmp
= sort__snoop_cmp
,
1307 .se_snprintf
= hist_entry__snoop_snprintf
,
1308 .se_width_idx
= HISTC_MEM_SNOOP
,
1311 struct sort_entry sort_mem_dcacheline
= {
1312 .se_header
= "Data Cacheline",
1313 .se_cmp
= sort__dcacheline_cmp
,
1314 .se_snprintf
= hist_entry__dcacheline_snprintf
,
1315 .se_width_idx
= HISTC_MEM_DCACHELINE
,
1319 sort__abort_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1321 if (!left
->branch_info
|| !right
->branch_info
)
1322 return cmp_null(left
->branch_info
, right
->branch_info
);
1324 return left
->branch_info
->flags
.abort
!=
1325 right
->branch_info
->flags
.abort
;
1328 static int hist_entry__abort_snprintf(struct hist_entry
*he
, char *bf
,
1329 size_t size
, unsigned int width
)
1331 static const char *out
= "N/A";
1333 if (he
->branch_info
) {
1334 if (he
->branch_info
->flags
.abort
)
1340 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1343 struct sort_entry sort_abort
= {
1344 .se_header
= "Transaction abort",
1345 .se_cmp
= sort__abort_cmp
,
1346 .se_snprintf
= hist_entry__abort_snprintf
,
1347 .se_width_idx
= HISTC_ABORT
,
1351 sort__in_tx_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1353 if (!left
->branch_info
|| !right
->branch_info
)
1354 return cmp_null(left
->branch_info
, right
->branch_info
);
1356 return left
->branch_info
->flags
.in_tx
!=
1357 right
->branch_info
->flags
.in_tx
;
1360 static int hist_entry__in_tx_snprintf(struct hist_entry
*he
, char *bf
,
1361 size_t size
, unsigned int width
)
1363 static const char *out
= "N/A";
1365 if (he
->branch_info
) {
1366 if (he
->branch_info
->flags
.in_tx
)
1372 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1375 struct sort_entry sort_in_tx
= {
1376 .se_header
= "Branch in transaction",
1377 .se_cmp
= sort__in_tx_cmp
,
1378 .se_snprintf
= hist_entry__in_tx_snprintf
,
1379 .se_width_idx
= HISTC_IN_TX
,
1383 sort__transaction_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1385 return left
->transaction
- right
->transaction
;
1388 static inline char *add_str(char *p
, const char *str
)
1391 return p
+ strlen(str
);
1394 static struct txbit
{
1399 { PERF_TXN_ELISION
, "EL ", 0 },
1400 { PERF_TXN_TRANSACTION
, "TX ", 1 },
1401 { PERF_TXN_SYNC
, "SYNC ", 1 },
1402 { PERF_TXN_ASYNC
, "ASYNC ", 0 },
1403 { PERF_TXN_RETRY
, "RETRY ", 0 },
1404 { PERF_TXN_CONFLICT
, "CON ", 0 },
1405 { PERF_TXN_CAPACITY_WRITE
, "CAP-WRITE ", 1 },
1406 { PERF_TXN_CAPACITY_READ
, "CAP-READ ", 0 },
1410 int hist_entry__transaction_len(void)
1415 for (i
= 0; txbits
[i
].name
; i
++) {
1416 if (!txbits
[i
].skip_for_len
)
1417 len
+= strlen(txbits
[i
].name
);
1419 len
+= 4; /* :XX<space> */
1423 static int hist_entry__transaction_snprintf(struct hist_entry
*he
, char *bf
,
1424 size_t size
, unsigned int width
)
1426 u64 t
= he
->transaction
;
1432 for (i
= 0; txbits
[i
].name
; i
++)
1433 if (txbits
[i
].flag
& t
)
1434 p
= add_str(p
, txbits
[i
].name
);
1435 if (t
&& !(t
& (PERF_TXN_SYNC
|PERF_TXN_ASYNC
)))
1436 p
= add_str(p
, "NEITHER ");
1437 if (t
& PERF_TXN_ABORT_MASK
) {
1438 sprintf(p
, ":%" PRIx64
,
1439 (t
& PERF_TXN_ABORT_MASK
) >>
1440 PERF_TXN_ABORT_SHIFT
);
1444 return repsep_snprintf(bf
, size
, "%-*s", width
, buf
);
1447 struct sort_entry sort_transaction
= {
1448 .se_header
= "Transaction ",
1449 .se_cmp
= sort__transaction_cmp
,
1450 .se_snprintf
= hist_entry__transaction_snprintf
,
1451 .se_width_idx
= HISTC_TRANSACTION
,
1454 /* --sort symbol_size */
1456 static int64_t _sort__sym_size_cmp(struct symbol
*sym_l
, struct symbol
*sym_r
)
1458 int64_t size_l
= sym_l
!= NULL
? symbol__size(sym_l
) : 0;
1459 int64_t size_r
= sym_r
!= NULL
? symbol__size(sym_r
) : 0;
1461 return size_l
< size_r
? -1 :
1462 size_l
== size_r
? 0 : 1;
1466 sort__sym_size_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1468 return _sort__sym_size_cmp(right
->ms
.sym
, left
->ms
.sym
);
1471 static int _hist_entry__sym_size_snprintf(struct symbol
*sym
, char *bf
,
1472 size_t bf_size
, unsigned int width
)
1475 return repsep_snprintf(bf
, bf_size
, "%*d", width
, symbol__size(sym
));
1477 return repsep_snprintf(bf
, bf_size
, "%*s", width
, "unknown");
1480 static int hist_entry__sym_size_snprintf(struct hist_entry
*he
, char *bf
,
1481 size_t size
, unsigned int width
)
1483 return _hist_entry__sym_size_snprintf(he
->ms
.sym
, bf
, size
, width
);
1486 struct sort_entry sort_sym_size
= {
1487 .se_header
= "Symbol size",
1488 .se_cmp
= sort__sym_size_cmp
,
1489 .se_snprintf
= hist_entry__sym_size_snprintf
,
1490 .se_width_idx
= HISTC_SYM_SIZE
,
1494 struct sort_dimension
{
1496 struct sort_entry
*entry
;
1500 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1502 static struct sort_dimension common_sort_dimensions
[] = {
1503 DIM(SORT_PID
, "pid", sort_thread
),
1504 DIM(SORT_COMM
, "comm", sort_comm
),
1505 DIM(SORT_DSO
, "dso", sort_dso
),
1506 DIM(SORT_SYM
, "symbol", sort_sym
),
1507 DIM(SORT_PARENT
, "parent", sort_parent
),
1508 DIM(SORT_CPU
, "cpu", sort_cpu
),
1509 DIM(SORT_SOCKET
, "socket", sort_socket
),
1510 DIM(SORT_SRCLINE
, "srcline", sort_srcline
),
1511 DIM(SORT_SRCFILE
, "srcfile", sort_srcfile
),
1512 DIM(SORT_LOCAL_WEIGHT
, "local_weight", sort_local_weight
),
1513 DIM(SORT_GLOBAL_WEIGHT
, "weight", sort_global_weight
),
1514 DIM(SORT_TRANSACTION
, "transaction", sort_transaction
),
1515 DIM(SORT_TRACE
, "trace", sort_trace
),
1516 DIM(SORT_SYM_SIZE
, "symbol_size", sort_sym_size
),
1517 DIM(SORT_CGROUP_ID
, "cgroup_id", sort_cgroup_id
),
1522 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1524 static struct sort_dimension bstack_sort_dimensions
[] = {
1525 DIM(SORT_DSO_FROM
, "dso_from", sort_dso_from
),
1526 DIM(SORT_DSO_TO
, "dso_to", sort_dso_to
),
1527 DIM(SORT_SYM_FROM
, "symbol_from", sort_sym_from
),
1528 DIM(SORT_SYM_TO
, "symbol_to", sort_sym_to
),
1529 DIM(SORT_MISPREDICT
, "mispredict", sort_mispredict
),
1530 DIM(SORT_IN_TX
, "in_tx", sort_in_tx
),
1531 DIM(SORT_ABORT
, "abort", sort_abort
),
1532 DIM(SORT_CYCLES
, "cycles", sort_cycles
),
1533 DIM(SORT_SRCLINE_FROM
, "srcline_from", sort_srcline_from
),
1534 DIM(SORT_SRCLINE_TO
, "srcline_to", sort_srcline_to
),
1539 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1541 static struct sort_dimension memory_sort_dimensions
[] = {
1542 DIM(SORT_MEM_DADDR_SYMBOL
, "symbol_daddr", sort_mem_daddr_sym
),
1543 DIM(SORT_MEM_IADDR_SYMBOL
, "symbol_iaddr", sort_mem_iaddr_sym
),
1544 DIM(SORT_MEM_DADDR_DSO
, "dso_daddr", sort_mem_daddr_dso
),
1545 DIM(SORT_MEM_LOCKED
, "locked", sort_mem_locked
),
1546 DIM(SORT_MEM_TLB
, "tlb", sort_mem_tlb
),
1547 DIM(SORT_MEM_LVL
, "mem", sort_mem_lvl
),
1548 DIM(SORT_MEM_SNOOP
, "snoop", sort_mem_snoop
),
1549 DIM(SORT_MEM_DCACHELINE
, "dcacheline", sort_mem_dcacheline
),
1554 struct hpp_dimension
{
1556 struct perf_hpp_fmt
*fmt
;
1560 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1562 static struct hpp_dimension hpp_sort_dimensions
[] = {
1563 DIM(PERF_HPP__OVERHEAD
, "overhead"),
1564 DIM(PERF_HPP__OVERHEAD_SYS
, "overhead_sys"),
1565 DIM(PERF_HPP__OVERHEAD_US
, "overhead_us"),
1566 DIM(PERF_HPP__OVERHEAD_GUEST_SYS
, "overhead_guest_sys"),
1567 DIM(PERF_HPP__OVERHEAD_GUEST_US
, "overhead_guest_us"),
1568 DIM(PERF_HPP__OVERHEAD_ACC
, "overhead_children"),
1569 DIM(PERF_HPP__SAMPLES
, "sample"),
1570 DIM(PERF_HPP__PERIOD
, "period"),
1575 struct hpp_sort_entry
{
1576 struct perf_hpp_fmt hpp
;
1577 struct sort_entry
*se
;
1580 void perf_hpp__reset_sort_width(struct perf_hpp_fmt
*fmt
, struct hists
*hists
)
1582 struct hpp_sort_entry
*hse
;
1584 if (!perf_hpp__is_sort_entry(fmt
))
1587 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1588 hists__new_col_len(hists
, hse
->se
->se_width_idx
, strlen(fmt
->name
));
1591 static int __sort__hpp_header(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1592 struct hists
*hists
, int line __maybe_unused
,
1593 int *span __maybe_unused
)
1595 struct hpp_sort_entry
*hse
;
1596 size_t len
= fmt
->user_len
;
1598 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1601 len
= hists__col_len(hists
, hse
->se
->se_width_idx
);
1603 return scnprintf(hpp
->buf
, hpp
->size
, "%-*.*s", len
, len
, fmt
->name
);
1606 static int __sort__hpp_width(struct perf_hpp_fmt
*fmt
,
1607 struct perf_hpp
*hpp __maybe_unused
,
1608 struct hists
*hists
)
1610 struct hpp_sort_entry
*hse
;
1611 size_t len
= fmt
->user_len
;
1613 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1616 len
= hists__col_len(hists
, hse
->se
->se_width_idx
);
1621 static int __sort__hpp_entry(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1622 struct hist_entry
*he
)
1624 struct hpp_sort_entry
*hse
;
1625 size_t len
= fmt
->user_len
;
1627 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1630 len
= hists__col_len(he
->hists
, hse
->se
->se_width_idx
);
1632 return hse
->se
->se_snprintf(he
, hpp
->buf
, hpp
->size
, len
);
1635 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt
*fmt
,
1636 struct hist_entry
*a
, struct hist_entry
*b
)
1638 struct hpp_sort_entry
*hse
;
1640 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1641 return hse
->se
->se_cmp(a
, b
);
1644 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt
*fmt
,
1645 struct hist_entry
*a
, struct hist_entry
*b
)
1647 struct hpp_sort_entry
*hse
;
1648 int64_t (*collapse_fn
)(struct hist_entry
*, struct hist_entry
*);
1650 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1651 collapse_fn
= hse
->se
->se_collapse
?: hse
->se
->se_cmp
;
1652 return collapse_fn(a
, b
);
1655 static int64_t __sort__hpp_sort(struct perf_hpp_fmt
*fmt
,
1656 struct hist_entry
*a
, struct hist_entry
*b
)
1658 struct hpp_sort_entry
*hse
;
1659 int64_t (*sort_fn
)(struct hist_entry
*, struct hist_entry
*);
1661 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1662 sort_fn
= hse
->se
->se_sort
?: hse
->se
->se_cmp
;
1663 return sort_fn(a
, b
);
1666 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt
*format
)
1668 return format
->header
== __sort__hpp_header
;
1671 #define MK_SORT_ENTRY_CHK(key) \
1672 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1674 struct hpp_sort_entry *hse; \
1676 if (!perf_hpp__is_sort_entry(fmt)) \
1679 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1680 return hse->se == &sort_ ## key ; \
1683 MK_SORT_ENTRY_CHK(trace
)
1684 MK_SORT_ENTRY_CHK(srcline
)
1685 MK_SORT_ENTRY_CHK(srcfile
)
1686 MK_SORT_ENTRY_CHK(thread
)
1687 MK_SORT_ENTRY_CHK(comm
)
1688 MK_SORT_ENTRY_CHK(dso
)
1689 MK_SORT_ENTRY_CHK(sym
)
1692 static bool __sort__hpp_equal(struct perf_hpp_fmt
*a
, struct perf_hpp_fmt
*b
)
1694 struct hpp_sort_entry
*hse_a
;
1695 struct hpp_sort_entry
*hse_b
;
1697 if (!perf_hpp__is_sort_entry(a
) || !perf_hpp__is_sort_entry(b
))
1700 hse_a
= container_of(a
, struct hpp_sort_entry
, hpp
);
1701 hse_b
= container_of(b
, struct hpp_sort_entry
, hpp
);
1703 return hse_a
->se
== hse_b
->se
;
1706 static void hse_free(struct perf_hpp_fmt
*fmt
)
1708 struct hpp_sort_entry
*hse
;
1710 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1714 static struct hpp_sort_entry
*
1715 __sort_dimension__alloc_hpp(struct sort_dimension
*sd
, int level
)
1717 struct hpp_sort_entry
*hse
;
1719 hse
= malloc(sizeof(*hse
));
1721 pr_err("Memory allocation failed\n");
1725 hse
->se
= sd
->entry
;
1726 hse
->hpp
.name
= sd
->entry
->se_header
;
1727 hse
->hpp
.header
= __sort__hpp_header
;
1728 hse
->hpp
.width
= __sort__hpp_width
;
1729 hse
->hpp
.entry
= __sort__hpp_entry
;
1730 hse
->hpp
.color
= NULL
;
1732 hse
->hpp
.cmp
= __sort__hpp_cmp
;
1733 hse
->hpp
.collapse
= __sort__hpp_collapse
;
1734 hse
->hpp
.sort
= __sort__hpp_sort
;
1735 hse
->hpp
.equal
= __sort__hpp_equal
;
1736 hse
->hpp
.free
= hse_free
;
1738 INIT_LIST_HEAD(&hse
->hpp
.list
);
1739 INIT_LIST_HEAD(&hse
->hpp
.sort_list
);
1740 hse
->hpp
.elide
= false;
1742 hse
->hpp
.user_len
= 0;
1743 hse
->hpp
.level
= level
;
1748 static void hpp_free(struct perf_hpp_fmt
*fmt
)
1753 static struct perf_hpp_fmt
*__hpp_dimension__alloc_hpp(struct hpp_dimension
*hd
,
1756 struct perf_hpp_fmt
*fmt
;
1758 fmt
= memdup(hd
->fmt
, sizeof(*fmt
));
1760 INIT_LIST_HEAD(&fmt
->list
);
1761 INIT_LIST_HEAD(&fmt
->sort_list
);
1762 fmt
->free
= hpp_free
;
1769 int hist_entry__filter(struct hist_entry
*he
, int type
, const void *arg
)
1771 struct perf_hpp_fmt
*fmt
;
1772 struct hpp_sort_entry
*hse
;
1776 perf_hpp_list__for_each_format(he
->hpp_list
, fmt
) {
1777 if (!perf_hpp__is_sort_entry(fmt
))
1780 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1781 if (hse
->se
->se_filter
== NULL
)
1785 * hist entry is filtered if any of sort key in the hpp list
1786 * is applied. But it should skip non-matched filter types.
1788 r
= hse
->se
->se_filter(he
, type
, arg
);
1799 static int __sort_dimension__add_hpp_sort(struct sort_dimension
*sd
,
1800 struct perf_hpp_list
*list
,
1803 struct hpp_sort_entry
*hse
= __sort_dimension__alloc_hpp(sd
, level
);
1808 perf_hpp_list__register_sort_field(list
, &hse
->hpp
);
1812 static int __sort_dimension__add_hpp_output(struct sort_dimension
*sd
,
1813 struct perf_hpp_list
*list
)
1815 struct hpp_sort_entry
*hse
= __sort_dimension__alloc_hpp(sd
, 0);
1820 perf_hpp_list__column_register(list
, &hse
->hpp
);
1824 struct hpp_dynamic_entry
{
1825 struct perf_hpp_fmt hpp
;
1826 struct perf_evsel
*evsel
;
1827 struct format_field
*field
;
1828 unsigned dynamic_len
;
1832 static int hde_width(struct hpp_dynamic_entry
*hde
)
1834 if (!hde
->hpp
.len
) {
1835 int len
= hde
->dynamic_len
;
1836 int namelen
= strlen(hde
->field
->name
);
1837 int fieldlen
= hde
->field
->size
;
1842 if (!(hde
->field
->flags
& FIELD_IS_STRING
)) {
1843 /* length for print hex numbers */
1844 fieldlen
= hde
->field
->size
* 2 + 2;
1851 return hde
->hpp
.len
;
1854 static void update_dynamic_len(struct hpp_dynamic_entry
*hde
,
1855 struct hist_entry
*he
)
1858 struct format_field
*field
= hde
->field
;
1865 /* parse pretty print result and update max length */
1866 if (!he
->trace_output
)
1867 he
->trace_output
= get_trace_output(he
);
1869 namelen
= strlen(field
->name
);
1870 str
= he
->trace_output
;
1873 pos
= strchr(str
, ' ');
1876 pos
= str
+ strlen(str
);
1879 if (!strncmp(str
, field
->name
, namelen
)) {
1885 if (len
> hde
->dynamic_len
)
1886 hde
->dynamic_len
= len
;
1897 static int __sort__hde_header(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1898 struct hists
*hists __maybe_unused
,
1899 int line __maybe_unused
,
1900 int *span __maybe_unused
)
1902 struct hpp_dynamic_entry
*hde
;
1903 size_t len
= fmt
->user_len
;
1905 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1908 len
= hde_width(hde
);
1910 return scnprintf(hpp
->buf
, hpp
->size
, "%*.*s", len
, len
, hde
->field
->name
);
1913 static int __sort__hde_width(struct perf_hpp_fmt
*fmt
,
1914 struct perf_hpp
*hpp __maybe_unused
,
1915 struct hists
*hists __maybe_unused
)
1917 struct hpp_dynamic_entry
*hde
;
1918 size_t len
= fmt
->user_len
;
1920 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1923 len
= hde_width(hde
);
1928 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt
*fmt
, struct hists
*hists
)
1930 struct hpp_dynamic_entry
*hde
;
1932 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1934 return hists_to_evsel(hists
) == hde
->evsel
;
1937 static int __sort__hde_entry(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1938 struct hist_entry
*he
)
1940 struct hpp_dynamic_entry
*hde
;
1941 size_t len
= fmt
->user_len
;
1943 struct format_field
*field
;
1948 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1951 len
= hde_width(hde
);
1956 if (!he
->trace_output
)
1957 he
->trace_output
= get_trace_output(he
);
1960 namelen
= strlen(field
->name
);
1961 str
= he
->trace_output
;
1964 pos
= strchr(str
, ' ');
1967 pos
= str
+ strlen(str
);
1970 if (!strncmp(str
, field
->name
, namelen
)) {
1972 str
= strndup(str
, pos
- str
);
1975 return scnprintf(hpp
->buf
, hpp
->size
,
1976 "%*.*s", len
, len
, "ERROR");
1987 struct trace_seq seq
;
1989 trace_seq_init(&seq
);
1990 pevent_print_field(&seq
, he
->raw_data
, hde
->field
);
1994 ret
= scnprintf(hpp
->buf
, hpp
->size
, "%*.*s", len
, len
, str
);
1999 static int64_t __sort__hde_cmp(struct perf_hpp_fmt
*fmt
,
2000 struct hist_entry
*a
, struct hist_entry
*b
)
2002 struct hpp_dynamic_entry
*hde
;
2003 struct format_field
*field
;
2004 unsigned offset
, size
;
2006 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2009 update_dynamic_len(hde
, a
);
2014 if (field
->flags
& FIELD_IS_DYNAMIC
) {
2015 unsigned long long dyn
;
2017 pevent_read_number_field(field
, a
->raw_data
, &dyn
);
2018 offset
= dyn
& 0xffff;
2019 size
= (dyn
>> 16) & 0xffff;
2021 /* record max width for output */
2022 if (size
> hde
->dynamic_len
)
2023 hde
->dynamic_len
= size
;
2025 offset
= field
->offset
;
2029 return memcmp(a
->raw_data
+ offset
, b
->raw_data
+ offset
, size
);
2032 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt
*fmt
)
2034 return fmt
->cmp
== __sort__hde_cmp
;
2037 static bool __sort__hde_equal(struct perf_hpp_fmt
*a
, struct perf_hpp_fmt
*b
)
2039 struct hpp_dynamic_entry
*hde_a
;
2040 struct hpp_dynamic_entry
*hde_b
;
2042 if (!perf_hpp__is_dynamic_entry(a
) || !perf_hpp__is_dynamic_entry(b
))
2045 hde_a
= container_of(a
, struct hpp_dynamic_entry
, hpp
);
2046 hde_b
= container_of(b
, struct hpp_dynamic_entry
, hpp
);
2048 return hde_a
->field
== hde_b
->field
;
2051 static void hde_free(struct perf_hpp_fmt
*fmt
)
2053 struct hpp_dynamic_entry
*hde
;
2055 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2059 static struct hpp_dynamic_entry
*
2060 __alloc_dynamic_entry(struct perf_evsel
*evsel
, struct format_field
*field
,
2063 struct hpp_dynamic_entry
*hde
;
2065 hde
= malloc(sizeof(*hde
));
2067 pr_debug("Memory allocation failed\n");
2073 hde
->dynamic_len
= 0;
2075 hde
->hpp
.name
= field
->name
;
2076 hde
->hpp
.header
= __sort__hde_header
;
2077 hde
->hpp
.width
= __sort__hde_width
;
2078 hde
->hpp
.entry
= __sort__hde_entry
;
2079 hde
->hpp
.color
= NULL
;
2081 hde
->hpp
.cmp
= __sort__hde_cmp
;
2082 hde
->hpp
.collapse
= __sort__hde_cmp
;
2083 hde
->hpp
.sort
= __sort__hde_cmp
;
2084 hde
->hpp
.equal
= __sort__hde_equal
;
2085 hde
->hpp
.free
= hde_free
;
2087 INIT_LIST_HEAD(&hde
->hpp
.list
);
2088 INIT_LIST_HEAD(&hde
->hpp
.sort_list
);
2089 hde
->hpp
.elide
= false;
2091 hde
->hpp
.user_len
= 0;
2092 hde
->hpp
.level
= level
;
2097 struct perf_hpp_fmt
*perf_hpp_fmt__dup(struct perf_hpp_fmt
*fmt
)
2099 struct perf_hpp_fmt
*new_fmt
= NULL
;
2101 if (perf_hpp__is_sort_entry(fmt
)) {
2102 struct hpp_sort_entry
*hse
, *new_hse
;
2104 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
2105 new_hse
= memdup(hse
, sizeof(*hse
));
2107 new_fmt
= &new_hse
->hpp
;
2108 } else if (perf_hpp__is_dynamic_entry(fmt
)) {
2109 struct hpp_dynamic_entry
*hde
, *new_hde
;
2111 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2112 new_hde
= memdup(hde
, sizeof(*hde
));
2114 new_fmt
= &new_hde
->hpp
;
2116 new_fmt
= memdup(fmt
, sizeof(*fmt
));
2119 INIT_LIST_HEAD(&new_fmt
->list
);
2120 INIT_LIST_HEAD(&new_fmt
->sort_list
);
2125 static int parse_field_name(char *str
, char **event
, char **field
, char **opt
)
2127 char *event_name
, *field_name
, *opt_name
;
2130 field_name
= strchr(str
, '.');
2133 *field_name
++ = '\0';
2139 opt_name
= strchr(field_name
, '/');
2143 *event
= event_name
;
2144 *field
= field_name
;
2150 /* find match evsel using a given event name. The event name can be:
2151 * 1. '%' + event index (e.g. '%1' for first event)
2152 * 2. full event name (e.g. sched:sched_switch)
2153 * 3. partial event name (should not contain ':')
2155 static struct perf_evsel
*find_evsel(struct perf_evlist
*evlist
, char *event_name
)
2157 struct perf_evsel
*evsel
= NULL
;
2158 struct perf_evsel
*pos
;
2162 if (event_name
[0] == '%') {
2163 int nr
= strtol(event_name
+1, NULL
, 0);
2165 if (nr
> evlist
->nr_entries
)
2168 evsel
= perf_evlist__first(evlist
);
2170 evsel
= perf_evsel__next(evsel
);
2175 full_name
= !!strchr(event_name
, ':');
2176 evlist__for_each_entry(evlist
, pos
) {
2178 if (full_name
&& !strcmp(pos
->name
, event_name
))
2181 if (!full_name
&& strstr(pos
->name
, event_name
)) {
2183 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2184 event_name
, evsel
->name
, pos
->name
);
2194 static int __dynamic_dimension__add(struct perf_evsel
*evsel
,
2195 struct format_field
*field
,
2196 bool raw_trace
, int level
)
2198 struct hpp_dynamic_entry
*hde
;
2200 hde
= __alloc_dynamic_entry(evsel
, field
, level
);
2204 hde
->raw_trace
= raw_trace
;
2206 perf_hpp__register_sort_field(&hde
->hpp
);
2210 static int add_evsel_fields(struct perf_evsel
*evsel
, bool raw_trace
, int level
)
2213 struct format_field
*field
;
2215 field
= evsel
->tp_format
->format
.fields
;
2217 ret
= __dynamic_dimension__add(evsel
, field
, raw_trace
, level
);
2221 field
= field
->next
;
2226 static int add_all_dynamic_fields(struct perf_evlist
*evlist
, bool raw_trace
,
2230 struct perf_evsel
*evsel
;
2232 evlist__for_each_entry(evlist
, evsel
) {
2233 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
2236 ret
= add_evsel_fields(evsel
, raw_trace
, level
);
2243 static int add_all_matching_fields(struct perf_evlist
*evlist
,
2244 char *field_name
, bool raw_trace
, int level
)
2247 struct perf_evsel
*evsel
;
2248 struct format_field
*field
;
2250 evlist__for_each_entry(evlist
, evsel
) {
2251 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
2254 field
= pevent_find_any_field(evsel
->tp_format
, field_name
);
2258 ret
= __dynamic_dimension__add(evsel
, field
, raw_trace
, level
);
2265 static int add_dynamic_entry(struct perf_evlist
*evlist
, const char *tok
,
2268 char *str
, *event_name
, *field_name
, *opt_name
;
2269 struct perf_evsel
*evsel
;
2270 struct format_field
*field
;
2271 bool raw_trace
= symbol_conf
.raw_trace
;
2281 if (parse_field_name(str
, &event_name
, &field_name
, &opt_name
) < 0) {
2287 if (strcmp(opt_name
, "raw")) {
2288 pr_debug("unsupported field option %s\n", opt_name
);
2295 if (!strcmp(field_name
, "trace_fields")) {
2296 ret
= add_all_dynamic_fields(evlist
, raw_trace
, level
);
2300 if (event_name
== NULL
) {
2301 ret
= add_all_matching_fields(evlist
, field_name
, raw_trace
, level
);
2305 evsel
= find_evsel(evlist
, event_name
);
2306 if (evsel
== NULL
) {
2307 pr_debug("Cannot find event: %s\n", event_name
);
2312 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
) {
2313 pr_debug("%s is not a tracepoint event\n", event_name
);
2318 if (!strcmp(field_name
, "*")) {
2319 ret
= add_evsel_fields(evsel
, raw_trace
, level
);
2321 field
= pevent_find_any_field(evsel
->tp_format
, field_name
);
2322 if (field
== NULL
) {
2323 pr_debug("Cannot find event field for %s.%s\n",
2324 event_name
, field_name
);
2328 ret
= __dynamic_dimension__add(evsel
, field
, raw_trace
, level
);
2336 static int __sort_dimension__add(struct sort_dimension
*sd
,
2337 struct perf_hpp_list
*list
,
2343 if (__sort_dimension__add_hpp_sort(sd
, list
, level
) < 0)
2346 if (sd
->entry
->se_collapse
)
2347 list
->need_collapse
= 1;
2354 static int __hpp_dimension__add(struct hpp_dimension
*hd
,
2355 struct perf_hpp_list
*list
,
2358 struct perf_hpp_fmt
*fmt
;
2363 fmt
= __hpp_dimension__alloc_hpp(hd
, level
);
2368 perf_hpp_list__register_sort_field(list
, fmt
);
2372 static int __sort_dimension__add_output(struct perf_hpp_list
*list
,
2373 struct sort_dimension
*sd
)
2378 if (__sort_dimension__add_hpp_output(sd
, list
) < 0)
2385 static int __hpp_dimension__add_output(struct perf_hpp_list
*list
,
2386 struct hpp_dimension
*hd
)
2388 struct perf_hpp_fmt
*fmt
;
2393 fmt
= __hpp_dimension__alloc_hpp(hd
, 0);
2398 perf_hpp_list__column_register(list
, fmt
);
2402 int hpp_dimension__add_output(unsigned col
)
2404 BUG_ON(col
>= PERF_HPP__MAX_INDEX
);
2405 return __hpp_dimension__add_output(&perf_hpp_list
, &hpp_sort_dimensions
[col
]);
2408 int sort_dimension__add(struct perf_hpp_list
*list
, const char *tok
,
2409 struct perf_evlist
*evlist
,
2414 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++) {
2415 struct sort_dimension
*sd
= &common_sort_dimensions
[i
];
2417 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2420 if (sd
->entry
== &sort_parent
) {
2421 int ret
= regcomp(&parent_regex
, parent_pattern
, REG_EXTENDED
);
2425 regerror(ret
, &parent_regex
, err
, sizeof(err
));
2426 pr_err("Invalid regex: %s\n%s", parent_pattern
, err
);
2430 } else if (sd
->entry
== &sort_sym
) {
2433 * perf diff displays the performance difference amongst
2434 * two or more perf.data files. Those files could come
2435 * from different binaries. So we should not compare
2436 * their ips, but the name of symbol.
2438 if (sort__mode
== SORT_MODE__DIFF
)
2439 sd
->entry
->se_collapse
= sort__sym_sort
;
2441 } else if (sd
->entry
== &sort_dso
) {
2443 } else if (sd
->entry
== &sort_socket
) {
2445 } else if (sd
->entry
== &sort_thread
) {
2447 } else if (sd
->entry
== &sort_comm
) {
2451 return __sort_dimension__add(sd
, list
, level
);
2454 for (i
= 0; i
< ARRAY_SIZE(hpp_sort_dimensions
); i
++) {
2455 struct hpp_dimension
*hd
= &hpp_sort_dimensions
[i
];
2457 if (strncasecmp(tok
, hd
->name
, strlen(tok
)))
2460 return __hpp_dimension__add(hd
, list
, level
);
2463 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++) {
2464 struct sort_dimension
*sd
= &bstack_sort_dimensions
[i
];
2466 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2469 if (sort__mode
!= SORT_MODE__BRANCH
)
2472 if (sd
->entry
== &sort_sym_from
|| sd
->entry
== &sort_sym_to
)
2475 __sort_dimension__add(sd
, list
, level
);
2479 for (i
= 0; i
< ARRAY_SIZE(memory_sort_dimensions
); i
++) {
2480 struct sort_dimension
*sd
= &memory_sort_dimensions
[i
];
2482 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2485 if (sort__mode
!= SORT_MODE__MEMORY
)
2488 if (sd
->entry
== &sort_mem_dcacheline
&& cacheline_size
== 0)
2491 if (sd
->entry
== &sort_mem_daddr_sym
)
2494 __sort_dimension__add(sd
, list
, level
);
2498 if (!add_dynamic_entry(evlist
, tok
, level
))
2504 static int setup_sort_list(struct perf_hpp_list
*list
, char *str
,
2505 struct perf_evlist
*evlist
)
2511 bool in_group
= false;
2515 tmp
= strpbrk(str
, "{}, ");
2520 next_level
= level
+ 1;
2524 else if (*tmp
== '}')
2532 ret
= sort_dimension__add(list
, tok
, evlist
, level
);
2533 if (ret
== -EINVAL
) {
2534 if (!cacheline_size
&& !strncasecmp(tok
, "dcacheline", strlen(tok
)))
2535 pr_err("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2537 pr_err("Invalid --sort key: `%s'", tok
);
2539 } else if (ret
== -ESRCH
) {
2540 pr_err("Unknown --sort key: `%s'", tok
);
2551 static const char *get_default_sort_order(struct perf_evlist
*evlist
)
2553 const char *default_sort_orders
[] = {
2555 default_branch_sort_order
,
2556 default_mem_sort_order
,
2557 default_top_sort_order
,
2558 default_diff_sort_order
,
2559 default_tracepoint_sort_order
,
2561 bool use_trace
= true;
2562 struct perf_evsel
*evsel
;
2564 BUG_ON(sort__mode
>= ARRAY_SIZE(default_sort_orders
));
2569 evlist__for_each_entry(evlist
, evsel
) {
2570 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
) {
2577 sort__mode
= SORT_MODE__TRACEPOINT
;
2578 if (symbol_conf
.raw_trace
)
2579 return "trace_fields";
2582 return default_sort_orders
[sort__mode
];
2585 static int setup_sort_order(struct perf_evlist
*evlist
)
2587 char *new_sort_order
;
2590 * Append '+'-prefixed sort order to the default sort
2593 if (!sort_order
|| is_strict_order(sort_order
))
2596 if (sort_order
[1] == '\0') {
2597 pr_err("Invalid --sort key: `+'");
2602 * We allocate new sort_order string, but we never free it,
2603 * because it's checked over the rest of the code.
2605 if (asprintf(&new_sort_order
, "%s,%s",
2606 get_default_sort_order(evlist
), sort_order
+ 1) < 0) {
2607 pr_err("Not enough memory to set up --sort");
2611 sort_order
= new_sort_order
;
2616 * Adds 'pre,' prefix into 'str' is 'pre' is
2617 * not already part of 'str'.
2619 static char *prefix_if_not_in(const char *pre
, char *str
)
2623 if (!str
|| strstr(str
, pre
))
2626 if (asprintf(&n
, "%s,%s", pre
, str
) < 0)
2633 static char *setup_overhead(char *keys
)
2635 if (sort__mode
== SORT_MODE__DIFF
)
2638 keys
= prefix_if_not_in("overhead", keys
);
2640 if (symbol_conf
.cumulate_callchain
)
2641 keys
= prefix_if_not_in("overhead_children", keys
);
2646 static int __setup_sorting(struct perf_evlist
*evlist
)
2649 const char *sort_keys
;
2652 ret
= setup_sort_order(evlist
);
2656 sort_keys
= sort_order
;
2657 if (sort_keys
== NULL
) {
2658 if (is_strict_order(field_order
)) {
2660 * If user specified field order but no sort order,
2661 * we'll honor it and not add default sort orders.
2666 sort_keys
= get_default_sort_order(evlist
);
2669 str
= strdup(sort_keys
);
2671 pr_err("Not enough memory to setup sort keys");
2676 * Prepend overhead fields for backward compatibility.
2678 if (!is_strict_order(field_order
)) {
2679 str
= setup_overhead(str
);
2681 pr_err("Not enough memory to setup overhead keys");
2686 ret
= setup_sort_list(&perf_hpp_list
, str
, evlist
);
2692 void perf_hpp__set_elide(int idx
, bool elide
)
2694 struct perf_hpp_fmt
*fmt
;
2695 struct hpp_sort_entry
*hse
;
2697 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2698 if (!perf_hpp__is_sort_entry(fmt
))
2701 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
2702 if (hse
->se
->se_width_idx
== idx
) {
2709 static bool __get_elide(struct strlist
*list
, const char *list_name
, FILE *fp
)
2711 if (list
&& strlist__nr_entries(list
) == 1) {
2713 fprintf(fp
, "# %s: %s\n", list_name
,
2714 strlist__entry(list
, 0)->s
);
2720 static bool get_elide(int idx
, FILE *output
)
2724 return __get_elide(symbol_conf
.sym_list
, "symbol", output
);
2726 return __get_elide(symbol_conf
.dso_list
, "dso", output
);
2728 return __get_elide(symbol_conf
.comm_list
, "comm", output
);
2733 if (sort__mode
!= SORT_MODE__BRANCH
)
2737 case HISTC_SYMBOL_FROM
:
2738 return __get_elide(symbol_conf
.sym_from_list
, "sym_from", output
);
2739 case HISTC_SYMBOL_TO
:
2740 return __get_elide(symbol_conf
.sym_to_list
, "sym_to", output
);
2741 case HISTC_DSO_FROM
:
2742 return __get_elide(symbol_conf
.dso_from_list
, "dso_from", output
);
2744 return __get_elide(symbol_conf
.dso_to_list
, "dso_to", output
);
2752 void sort__setup_elide(FILE *output
)
2754 struct perf_hpp_fmt
*fmt
;
2755 struct hpp_sort_entry
*hse
;
2757 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2758 if (!perf_hpp__is_sort_entry(fmt
))
2761 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
2762 fmt
->elide
= get_elide(hse
->se
->se_width_idx
, output
);
2766 * It makes no sense to elide all of sort entries.
2767 * Just revert them to show up again.
2769 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2770 if (!perf_hpp__is_sort_entry(fmt
))
2777 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2778 if (!perf_hpp__is_sort_entry(fmt
))
2785 int output_field_add(struct perf_hpp_list
*list
, char *tok
)
2789 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++) {
2790 struct sort_dimension
*sd
= &common_sort_dimensions
[i
];
2792 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2795 return __sort_dimension__add_output(list
, sd
);
2798 for (i
= 0; i
< ARRAY_SIZE(hpp_sort_dimensions
); i
++) {
2799 struct hpp_dimension
*hd
= &hpp_sort_dimensions
[i
];
2801 if (strncasecmp(tok
, hd
->name
, strlen(tok
)))
2804 return __hpp_dimension__add_output(list
, hd
);
2807 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++) {
2808 struct sort_dimension
*sd
= &bstack_sort_dimensions
[i
];
2810 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2813 return __sort_dimension__add_output(list
, sd
);
2816 for (i
= 0; i
< ARRAY_SIZE(memory_sort_dimensions
); i
++) {
2817 struct sort_dimension
*sd
= &memory_sort_dimensions
[i
];
2819 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2822 return __sort_dimension__add_output(list
, sd
);
2828 static int setup_output_list(struct perf_hpp_list
*list
, char *str
)
2833 for (tok
= strtok_r(str
, ", ", &tmp
);
2834 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
2835 ret
= output_field_add(list
, tok
);
2836 if (ret
== -EINVAL
) {
2837 pr_err("Invalid --fields key: `%s'", tok
);
2839 } else if (ret
== -ESRCH
) {
2840 pr_err("Unknown --fields key: `%s'", tok
);
2848 void reset_dimensions(void)
2852 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++)
2853 common_sort_dimensions
[i
].taken
= 0;
2855 for (i
= 0; i
< ARRAY_SIZE(hpp_sort_dimensions
); i
++)
2856 hpp_sort_dimensions
[i
].taken
= 0;
2858 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++)
2859 bstack_sort_dimensions
[i
].taken
= 0;
2861 for (i
= 0; i
< ARRAY_SIZE(memory_sort_dimensions
); i
++)
2862 memory_sort_dimensions
[i
].taken
= 0;
2865 bool is_strict_order(const char *order
)
2867 return order
&& (*order
!= '+');
2870 static int __setup_output_field(void)
2875 if (field_order
== NULL
)
2878 strp
= str
= strdup(field_order
);
2880 pr_err("Not enough memory to setup output fields");
2884 if (!is_strict_order(field_order
))
2887 if (!strlen(strp
)) {
2888 pr_err("Invalid --fields key: `+'");
2892 ret
= setup_output_list(&perf_hpp_list
, strp
);
2899 int setup_sorting(struct perf_evlist
*evlist
)
2903 err
= __setup_sorting(evlist
);
2907 if (parent_pattern
!= default_parent_pattern
) {
2908 err
= sort_dimension__add(&perf_hpp_list
, "parent", evlist
, -1);
2916 * perf diff doesn't use default hpp output fields.
2918 if (sort__mode
!= SORT_MODE__DIFF
)
2921 err
= __setup_output_field();
2925 /* copy sort keys to output fields */
2926 perf_hpp__setup_output_field(&perf_hpp_list
);
2927 /* and then copy output fields to sort keys */
2928 perf_hpp__append_sort_keys(&perf_hpp_list
);
2930 /* setup hists-specific output fields */
2931 if (perf_hpp__setup_hists_formats(&perf_hpp_list
, evlist
) < 0)
2937 void reset_output_field(void)
2939 perf_hpp_list
.need_collapse
= 0;
2940 perf_hpp_list
.parent
= 0;
2941 perf_hpp_list
.sym
= 0;
2942 perf_hpp_list
.dso
= 0;
2948 perf_hpp__reset_output_field(&perf_hpp_list
);