1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/mman.h>
15 #include <traceevent/event-parse.h>
16 #include "mem-events.h"
18 #include <linux/kernel.h>
21 const char default_parent_pattern
[] = "^sys_|^do_page_fault";
22 const char *parent_pattern
= default_parent_pattern
;
23 const char *default_sort_order
= "comm,dso,symbol";
24 const char default_branch_sort_order
[] = "comm,dso_from,symbol_from,symbol_to,cycles";
25 const char default_mem_sort_order
[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
26 const char default_top_sort_order
[] = "dso,symbol";
27 const char default_diff_sort_order
[] = "dso,symbol";
28 const char default_tracepoint_sort_order
[] = "trace";
29 const char *sort_order
;
30 const char *field_order
;
31 regex_t ignore_callees_regex
;
32 int have_ignore_callees
= 0;
33 enum sort_mode sort__mode
= SORT_MODE__NORMAL
;
36 * Replaces all occurrences of a char used with the:
38 * -t, --field-separator
40 * option, that uses a special separator character and don't pad with spaces,
41 * replacing all occurrences of this separator in symbol names (and other
42 * output) with a '.' character, that thus it's the only non valid separator.
44 static int repsep_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
50 n
= vsnprintf(bf
, size
, fmt
, ap
);
51 if (symbol_conf
.field_sep
&& n
> 0) {
55 sep
= strchr(sep
, *symbol_conf
.field_sep
);
68 static int64_t cmp_null(const void *l
, const void *r
)
81 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
83 return right
->thread
->tid
- left
->thread
->tid
;
86 static int hist_entry__thread_snprintf(struct hist_entry
*he
, char *bf
,
87 size_t size
, unsigned int width
)
89 const char *comm
= thread__comm_str(he
->thread
);
91 width
= max(7U, width
) - 8;
92 return repsep_snprintf(bf
, size
, "%7d:%-*.*s", he
->thread
->tid
,
93 width
, width
, comm
?: "");
96 static int hist_entry__thread_filter(struct hist_entry
*he
, int type
, const void *arg
)
98 const struct thread
*th
= arg
;
100 if (type
!= HIST_FILTER__THREAD
)
103 return th
&& he
->thread
!= th
;
106 struct sort_entry sort_thread
= {
107 .se_header
= " Pid:Command",
108 .se_cmp
= sort__thread_cmp
,
109 .se_snprintf
= hist_entry__thread_snprintf
,
110 .se_filter
= hist_entry__thread_filter
,
111 .se_width_idx
= HISTC_THREAD
,
117 * We can't use pointer comparison in functions below,
118 * because it gives different results based on pointer
119 * values, which could break some sorting assumptions.
122 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
124 return strcmp(comm__str(right
->comm
), comm__str(left
->comm
));
128 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
130 return strcmp(comm__str(right
->comm
), comm__str(left
->comm
));
134 sort__comm_sort(struct hist_entry
*left
, struct hist_entry
*right
)
136 return strcmp(comm__str(right
->comm
), comm__str(left
->comm
));
139 static int hist_entry__comm_snprintf(struct hist_entry
*he
, char *bf
,
140 size_t size
, unsigned int width
)
142 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, comm__str(he
->comm
));
145 struct sort_entry sort_comm
= {
146 .se_header
= "Command",
147 .se_cmp
= sort__comm_cmp
,
148 .se_collapse
= sort__comm_collapse
,
149 .se_sort
= sort__comm_sort
,
150 .se_snprintf
= hist_entry__comm_snprintf
,
151 .se_filter
= hist_entry__thread_filter
,
152 .se_width_idx
= HISTC_COMM
,
157 static int64_t _sort__dso_cmp(struct map
*map_l
, struct map
*map_r
)
159 struct dso
*dso_l
= map_l
? map_l
->dso
: NULL
;
160 struct dso
*dso_r
= map_r
? map_r
->dso
: NULL
;
161 const char *dso_name_l
, *dso_name_r
;
163 if (!dso_l
|| !dso_r
)
164 return cmp_null(dso_r
, dso_l
);
167 dso_name_l
= dso_l
->long_name
;
168 dso_name_r
= dso_r
->long_name
;
170 dso_name_l
= dso_l
->short_name
;
171 dso_name_r
= dso_r
->short_name
;
174 return strcmp(dso_name_l
, dso_name_r
);
178 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
180 return _sort__dso_cmp(right
->ms
.map
, left
->ms
.map
);
183 static int _hist_entry__dso_snprintf(struct map
*map
, char *bf
,
184 size_t size
, unsigned int width
)
186 if (map
&& map
->dso
) {
187 const char *dso_name
= verbose
> 0 ? map
->dso
->long_name
:
188 map
->dso
->short_name
;
189 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, dso_name
);
192 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "[unknown]");
195 static int hist_entry__dso_snprintf(struct hist_entry
*he
, char *bf
,
196 size_t size
, unsigned int width
)
198 return _hist_entry__dso_snprintf(he
->ms
.map
, bf
, size
, width
);
201 static int hist_entry__dso_filter(struct hist_entry
*he
, int type
, const void *arg
)
203 const struct dso
*dso
= arg
;
205 if (type
!= HIST_FILTER__DSO
)
208 return dso
&& (!he
->ms
.map
|| he
->ms
.map
->dso
!= dso
);
211 struct sort_entry sort_dso
= {
212 .se_header
= "Shared Object",
213 .se_cmp
= sort__dso_cmp
,
214 .se_snprintf
= hist_entry__dso_snprintf
,
215 .se_filter
= hist_entry__dso_filter
,
216 .se_width_idx
= HISTC_DSO
,
221 static int64_t _sort__addr_cmp(u64 left_ip
, u64 right_ip
)
223 return (int64_t)(right_ip
- left_ip
);
226 static int64_t _sort__sym_cmp(struct symbol
*sym_l
, struct symbol
*sym_r
)
228 if (!sym_l
|| !sym_r
)
229 return cmp_null(sym_l
, sym_r
);
234 if (sym_l
->inlined
|| sym_r
->inlined
) {
235 int ret
= strcmp(sym_l
->name
, sym_r
->name
);
239 if ((sym_l
->start
<= sym_r
->end
) && (sym_l
->end
>= sym_r
->start
))
243 if (sym_l
->start
!= sym_r
->start
)
244 return (int64_t)(sym_r
->start
- sym_l
->start
);
246 return (int64_t)(sym_r
->end
- sym_l
->end
);
250 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
254 if (!left
->ms
.sym
&& !right
->ms
.sym
)
255 return _sort__addr_cmp(left
->ip
, right
->ip
);
258 * comparing symbol address alone is not enough since it's a
259 * relative address within a dso.
261 if (!hists__has(left
->hists
, dso
) || hists__has(right
->hists
, dso
)) {
262 ret
= sort__dso_cmp(left
, right
);
267 return _sort__sym_cmp(left
->ms
.sym
, right
->ms
.sym
);
271 sort__sym_sort(struct hist_entry
*left
, struct hist_entry
*right
)
273 if (!left
->ms
.sym
|| !right
->ms
.sym
)
274 return cmp_null(left
->ms
.sym
, right
->ms
.sym
);
276 return strcmp(right
->ms
.sym
->name
, left
->ms
.sym
->name
);
279 static int _hist_entry__sym_snprintf(struct map
*map
, struct symbol
*sym
,
280 u64 ip
, char level
, char *bf
, size_t size
,
286 char o
= map
? dso__symtab_origin(map
->dso
) : '!';
287 ret
+= repsep_snprintf(bf
, size
, "%-#*llx %c ",
288 BITS_PER_LONG
/ 4 + 2, ip
, o
);
291 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "[%c] ", level
);
293 if (sym
->type
== STT_OBJECT
) {
294 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%s", sym
->name
);
295 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "+0x%llx",
296 ip
- map
->unmap_ip(map
, sym
->start
));
298 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%.*s",
302 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
,
306 size_t len
= BITS_PER_LONG
/ 4;
307 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-#.*llx",
314 static int hist_entry__sym_snprintf(struct hist_entry
*he
, char *bf
,
315 size_t size
, unsigned int width
)
317 return _hist_entry__sym_snprintf(he
->ms
.map
, he
->ms
.sym
, he
->ip
,
318 he
->level
, bf
, size
, width
);
321 static int hist_entry__sym_filter(struct hist_entry
*he
, int type
, const void *arg
)
323 const char *sym
= arg
;
325 if (type
!= HIST_FILTER__SYMBOL
)
328 return sym
&& (!he
->ms
.sym
|| !strstr(he
->ms
.sym
->name
, sym
));
331 struct sort_entry sort_sym
= {
332 .se_header
= "Symbol",
333 .se_cmp
= sort__sym_cmp
,
334 .se_sort
= sort__sym_sort
,
335 .se_snprintf
= hist_entry__sym_snprintf
,
336 .se_filter
= hist_entry__sym_filter
,
337 .se_width_idx
= HISTC_SYMBOL
,
342 char *hist_entry__srcline(struct hist_entry
*he
)
344 return map__srcline(he
->ms
.map
, he
->ip
, he
->ms
.sym
);
348 sort__srcline_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
351 left
->srcline
= hist_entry__srcline(left
);
353 right
->srcline
= hist_entry__srcline(right
);
355 return strcmp(right
->srcline
, left
->srcline
);
358 static int hist_entry__srcline_snprintf(struct hist_entry
*he
, char *bf
,
359 size_t size
, unsigned int width
)
362 he
->srcline
= hist_entry__srcline(he
);
364 return repsep_snprintf(bf
, size
, "%-.*s", width
, he
->srcline
);
367 struct sort_entry sort_srcline
= {
368 .se_header
= "Source:Line",
369 .se_cmp
= sort__srcline_cmp
,
370 .se_snprintf
= hist_entry__srcline_snprintf
,
371 .se_width_idx
= HISTC_SRCLINE
,
374 /* --sort srcline_from */
376 static char *addr_map_symbol__srcline(struct addr_map_symbol
*ams
)
378 return map__srcline(ams
->map
, ams
->al_addr
, ams
->sym
);
382 sort__srcline_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
384 if (!left
->branch_info
->srcline_from
)
385 left
->branch_info
->srcline_from
= addr_map_symbol__srcline(&left
->branch_info
->from
);
387 if (!right
->branch_info
->srcline_from
)
388 right
->branch_info
->srcline_from
= addr_map_symbol__srcline(&right
->branch_info
->from
);
390 return strcmp(right
->branch_info
->srcline_from
, left
->branch_info
->srcline_from
);
393 static int hist_entry__srcline_from_snprintf(struct hist_entry
*he
, char *bf
,
394 size_t size
, unsigned int width
)
396 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, he
->branch_info
->srcline_from
);
399 struct sort_entry sort_srcline_from
= {
400 .se_header
= "From Source:Line",
401 .se_cmp
= sort__srcline_from_cmp
,
402 .se_snprintf
= hist_entry__srcline_from_snprintf
,
403 .se_width_idx
= HISTC_SRCLINE_FROM
,
406 /* --sort srcline_to */
409 sort__srcline_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
411 if (!left
->branch_info
->srcline_to
)
412 left
->branch_info
->srcline_to
= addr_map_symbol__srcline(&left
->branch_info
->to
);
414 if (!right
->branch_info
->srcline_to
)
415 right
->branch_info
->srcline_to
= addr_map_symbol__srcline(&right
->branch_info
->to
);
417 return strcmp(right
->branch_info
->srcline_to
, left
->branch_info
->srcline_to
);
420 static int hist_entry__srcline_to_snprintf(struct hist_entry
*he
, char *bf
,
421 size_t size
, unsigned int width
)
423 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, he
->branch_info
->srcline_to
);
426 struct sort_entry sort_srcline_to
= {
427 .se_header
= "To Source:Line",
428 .se_cmp
= sort__srcline_to_cmp
,
429 .se_snprintf
= hist_entry__srcline_to_snprintf
,
430 .se_width_idx
= HISTC_SRCLINE_TO
,
433 static int hist_entry__sym_ipc_snprintf(struct hist_entry
*he
, char *bf
,
434 size_t size
, unsigned int width
)
437 struct symbol
*sym
= he
->ms
.sym
;
438 struct annotation
*notes
;
439 double ipc
= 0.0, coverage
= 0.0;
443 return repsep_snprintf(bf
, size
, "%-*s", width
, "-");
445 notes
= symbol__annotation(sym
);
447 if (notes
->hit_cycles
)
448 ipc
= notes
->hit_insn
/ ((double)notes
->hit_cycles
);
450 if (notes
->total_insn
) {
451 coverage
= notes
->cover_insn
* 100.0 /
452 ((double)notes
->total_insn
);
455 snprintf(tmp
, sizeof(tmp
), "%-5.2f [%5.1f%%]", ipc
, coverage
);
456 return repsep_snprintf(bf
, size
, "%-*s", width
, tmp
);
459 struct sort_entry sort_sym_ipc
= {
460 .se_header
= "IPC [IPC Coverage]",
461 .se_cmp
= sort__sym_cmp
,
462 .se_snprintf
= hist_entry__sym_ipc_snprintf
,
463 .se_width_idx
= HISTC_SYMBOL_IPC
,
466 static int hist_entry__sym_ipc_null_snprintf(struct hist_entry
*he
468 char *bf
, size_t size
,
473 snprintf(tmp
, sizeof(tmp
), "%-5s %2s", "-", "-");
474 return repsep_snprintf(bf
, size
, "%-*s", width
, tmp
);
477 struct sort_entry sort_sym_ipc_null
= {
478 .se_header
= "IPC [IPC Coverage]",
479 .se_cmp
= sort__sym_cmp
,
480 .se_snprintf
= hist_entry__sym_ipc_null_snprintf
,
481 .se_width_idx
= HISTC_SYMBOL_IPC
,
486 static char no_srcfile
[1];
488 static char *hist_entry__get_srcfile(struct hist_entry
*e
)
491 struct map
*map
= e
->ms
.map
;
496 sf
= __get_srcline(map
->dso
, map__rip_2objdump(map
, e
->ip
),
497 e
->ms
.sym
, false, true, true, e
->ip
);
498 if (!strcmp(sf
, SRCLINE_UNKNOWN
))
510 sort__srcfile_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
513 left
->srcfile
= hist_entry__get_srcfile(left
);
515 right
->srcfile
= hist_entry__get_srcfile(right
);
517 return strcmp(right
->srcfile
, left
->srcfile
);
520 static int hist_entry__srcfile_snprintf(struct hist_entry
*he
, char *bf
,
521 size_t size
, unsigned int width
)
524 he
->srcfile
= hist_entry__get_srcfile(he
);
526 return repsep_snprintf(bf
, size
, "%-.*s", width
, he
->srcfile
);
529 struct sort_entry sort_srcfile
= {
530 .se_header
= "Source File",
531 .se_cmp
= sort__srcfile_cmp
,
532 .se_snprintf
= hist_entry__srcfile_snprintf
,
533 .se_width_idx
= HISTC_SRCFILE
,
539 sort__parent_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
541 struct symbol
*sym_l
= left
->parent
;
542 struct symbol
*sym_r
= right
->parent
;
544 if (!sym_l
|| !sym_r
)
545 return cmp_null(sym_l
, sym_r
);
547 return strcmp(sym_r
->name
, sym_l
->name
);
550 static int hist_entry__parent_snprintf(struct hist_entry
*he
, char *bf
,
551 size_t size
, unsigned int width
)
553 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
,
554 he
->parent
? he
->parent
->name
: "[other]");
557 struct sort_entry sort_parent
= {
558 .se_header
= "Parent symbol",
559 .se_cmp
= sort__parent_cmp
,
560 .se_snprintf
= hist_entry__parent_snprintf
,
561 .se_width_idx
= HISTC_PARENT
,
567 sort__cpu_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
569 return right
->cpu
- left
->cpu
;
572 static int hist_entry__cpu_snprintf(struct hist_entry
*he
, char *bf
,
573 size_t size
, unsigned int width
)
575 return repsep_snprintf(bf
, size
, "%*.*d", width
, width
, he
->cpu
);
578 struct sort_entry sort_cpu
= {
580 .se_cmp
= sort__cpu_cmp
,
581 .se_snprintf
= hist_entry__cpu_snprintf
,
582 .se_width_idx
= HISTC_CPU
,
585 /* --sort cgroup_id */
587 static int64_t _sort__cgroup_dev_cmp(u64 left_dev
, u64 right_dev
)
589 return (int64_t)(right_dev
- left_dev
);
592 static int64_t _sort__cgroup_inode_cmp(u64 left_ino
, u64 right_ino
)
594 return (int64_t)(right_ino
- left_ino
);
598 sort__cgroup_id_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
602 ret
= _sort__cgroup_dev_cmp(right
->cgroup_id
.dev
, left
->cgroup_id
.dev
);
606 return _sort__cgroup_inode_cmp(right
->cgroup_id
.ino
,
607 left
->cgroup_id
.ino
);
610 static int hist_entry__cgroup_id_snprintf(struct hist_entry
*he
,
611 char *bf
, size_t size
,
612 unsigned int width __maybe_unused
)
614 return repsep_snprintf(bf
, size
, "%lu/0x%lx", he
->cgroup_id
.dev
,
618 struct sort_entry sort_cgroup_id
= {
619 .se_header
= "cgroup id (dev/inode)",
620 .se_cmp
= sort__cgroup_id_cmp
,
621 .se_snprintf
= hist_entry__cgroup_id_snprintf
,
622 .se_width_idx
= HISTC_CGROUP_ID
,
628 sort__socket_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
630 return right
->socket
- left
->socket
;
633 static int hist_entry__socket_snprintf(struct hist_entry
*he
, char *bf
,
634 size_t size
, unsigned int width
)
636 return repsep_snprintf(bf
, size
, "%*.*d", width
, width
-3, he
->socket
);
639 static int hist_entry__socket_filter(struct hist_entry
*he
, int type
, const void *arg
)
641 int sk
= *(const int *)arg
;
643 if (type
!= HIST_FILTER__SOCKET
)
646 return sk
>= 0 && he
->socket
!= sk
;
649 struct sort_entry sort_socket
= {
650 .se_header
= "Socket",
651 .se_cmp
= sort__socket_cmp
,
652 .se_snprintf
= hist_entry__socket_snprintf
,
653 .se_filter
= hist_entry__socket_filter
,
654 .se_width_idx
= HISTC_SOCKET
,
659 static char *get_trace_output(struct hist_entry
*he
)
661 struct trace_seq seq
;
662 struct perf_evsel
*evsel
;
663 struct tep_record rec
= {
664 .data
= he
->raw_data
,
665 .size
= he
->raw_size
,
668 evsel
= hists_to_evsel(he
->hists
);
670 trace_seq_init(&seq
);
671 if (symbol_conf
.raw_trace
) {
672 tep_print_fields(&seq
, he
->raw_data
, he
->raw_size
,
675 tep_event_info(&seq
, evsel
->tp_format
, &rec
);
678 * Trim the buffer, it starts at 4KB and we're not going to
679 * add anything more to this buffer.
681 return realloc(seq
.buffer
, seq
.len
+ 1);
685 sort__trace_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
687 struct perf_evsel
*evsel
;
689 evsel
= hists_to_evsel(left
->hists
);
690 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
693 if (left
->trace_output
== NULL
)
694 left
->trace_output
= get_trace_output(left
);
695 if (right
->trace_output
== NULL
)
696 right
->trace_output
= get_trace_output(right
);
698 return strcmp(right
->trace_output
, left
->trace_output
);
701 static int hist_entry__trace_snprintf(struct hist_entry
*he
, char *bf
,
702 size_t size
, unsigned int width
)
704 struct perf_evsel
*evsel
;
706 evsel
= hists_to_evsel(he
->hists
);
707 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
708 return scnprintf(bf
, size
, "%-.*s", width
, "N/A");
710 if (he
->trace_output
== NULL
)
711 he
->trace_output
= get_trace_output(he
);
712 return repsep_snprintf(bf
, size
, "%-.*s", width
, he
->trace_output
);
715 struct sort_entry sort_trace
= {
716 .se_header
= "Trace output",
717 .se_cmp
= sort__trace_cmp
,
718 .se_snprintf
= hist_entry__trace_snprintf
,
719 .se_width_idx
= HISTC_TRACE
,
722 /* sort keys for branch stacks */
725 sort__dso_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
727 if (!left
->branch_info
|| !right
->branch_info
)
728 return cmp_null(left
->branch_info
, right
->branch_info
);
730 return _sort__dso_cmp(left
->branch_info
->from
.map
,
731 right
->branch_info
->from
.map
);
734 static int hist_entry__dso_from_snprintf(struct hist_entry
*he
, char *bf
,
735 size_t size
, unsigned int width
)
738 return _hist_entry__dso_snprintf(he
->branch_info
->from
.map
,
741 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
744 static int hist_entry__dso_from_filter(struct hist_entry
*he
, int type
,
747 const struct dso
*dso
= arg
;
749 if (type
!= HIST_FILTER__DSO
)
752 return dso
&& (!he
->branch_info
|| !he
->branch_info
->from
.map
||
753 he
->branch_info
->from
.map
->dso
!= dso
);
757 sort__dso_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
759 if (!left
->branch_info
|| !right
->branch_info
)
760 return cmp_null(left
->branch_info
, right
->branch_info
);
762 return _sort__dso_cmp(left
->branch_info
->to
.map
,
763 right
->branch_info
->to
.map
);
766 static int hist_entry__dso_to_snprintf(struct hist_entry
*he
, char *bf
,
767 size_t size
, unsigned int width
)
770 return _hist_entry__dso_snprintf(he
->branch_info
->to
.map
,
773 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
776 static int hist_entry__dso_to_filter(struct hist_entry
*he
, int type
,
779 const struct dso
*dso
= arg
;
781 if (type
!= HIST_FILTER__DSO
)
784 return dso
&& (!he
->branch_info
|| !he
->branch_info
->to
.map
||
785 he
->branch_info
->to
.map
->dso
!= dso
);
789 sort__sym_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
791 struct addr_map_symbol
*from_l
= &left
->branch_info
->from
;
792 struct addr_map_symbol
*from_r
= &right
->branch_info
->from
;
794 if (!left
->branch_info
|| !right
->branch_info
)
795 return cmp_null(left
->branch_info
, right
->branch_info
);
797 from_l
= &left
->branch_info
->from
;
798 from_r
= &right
->branch_info
->from
;
800 if (!from_l
->sym
&& !from_r
->sym
)
801 return _sort__addr_cmp(from_l
->addr
, from_r
->addr
);
803 return _sort__sym_cmp(from_l
->sym
, from_r
->sym
);
807 sort__sym_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
809 struct addr_map_symbol
*to_l
, *to_r
;
811 if (!left
->branch_info
|| !right
->branch_info
)
812 return cmp_null(left
->branch_info
, right
->branch_info
);
814 to_l
= &left
->branch_info
->to
;
815 to_r
= &right
->branch_info
->to
;
817 if (!to_l
->sym
&& !to_r
->sym
)
818 return _sort__addr_cmp(to_l
->addr
, to_r
->addr
);
820 return _sort__sym_cmp(to_l
->sym
, to_r
->sym
);
823 static int hist_entry__sym_from_snprintf(struct hist_entry
*he
, char *bf
,
824 size_t size
, unsigned int width
)
826 if (he
->branch_info
) {
827 struct addr_map_symbol
*from
= &he
->branch_info
->from
;
829 return _hist_entry__sym_snprintf(from
->map
, from
->sym
, from
->addr
,
830 he
->level
, bf
, size
, width
);
833 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
836 static int hist_entry__sym_to_snprintf(struct hist_entry
*he
, char *bf
,
837 size_t size
, unsigned int width
)
839 if (he
->branch_info
) {
840 struct addr_map_symbol
*to
= &he
->branch_info
->to
;
842 return _hist_entry__sym_snprintf(to
->map
, to
->sym
, to
->addr
,
843 he
->level
, bf
, size
, width
);
846 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
849 static int hist_entry__sym_from_filter(struct hist_entry
*he
, int type
,
852 const char *sym
= arg
;
854 if (type
!= HIST_FILTER__SYMBOL
)
857 return sym
&& !(he
->branch_info
&& he
->branch_info
->from
.sym
&&
858 strstr(he
->branch_info
->from
.sym
->name
, sym
));
861 static int hist_entry__sym_to_filter(struct hist_entry
*he
, int type
,
864 const char *sym
= arg
;
866 if (type
!= HIST_FILTER__SYMBOL
)
869 return sym
&& !(he
->branch_info
&& he
->branch_info
->to
.sym
&&
870 strstr(he
->branch_info
->to
.sym
->name
, sym
));
873 struct sort_entry sort_dso_from
= {
874 .se_header
= "Source Shared Object",
875 .se_cmp
= sort__dso_from_cmp
,
876 .se_snprintf
= hist_entry__dso_from_snprintf
,
877 .se_filter
= hist_entry__dso_from_filter
,
878 .se_width_idx
= HISTC_DSO_FROM
,
881 struct sort_entry sort_dso_to
= {
882 .se_header
= "Target Shared Object",
883 .se_cmp
= sort__dso_to_cmp
,
884 .se_snprintf
= hist_entry__dso_to_snprintf
,
885 .se_filter
= hist_entry__dso_to_filter
,
886 .se_width_idx
= HISTC_DSO_TO
,
889 struct sort_entry sort_sym_from
= {
890 .se_header
= "Source Symbol",
891 .se_cmp
= sort__sym_from_cmp
,
892 .se_snprintf
= hist_entry__sym_from_snprintf
,
893 .se_filter
= hist_entry__sym_from_filter
,
894 .se_width_idx
= HISTC_SYMBOL_FROM
,
897 struct sort_entry sort_sym_to
= {
898 .se_header
= "Target Symbol",
899 .se_cmp
= sort__sym_to_cmp
,
900 .se_snprintf
= hist_entry__sym_to_snprintf
,
901 .se_filter
= hist_entry__sym_to_filter
,
902 .se_width_idx
= HISTC_SYMBOL_TO
,
906 sort__mispredict_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
910 if (!left
->branch_info
|| !right
->branch_info
)
911 return cmp_null(left
->branch_info
, right
->branch_info
);
913 mp
= left
->branch_info
->flags
.mispred
!= right
->branch_info
->flags
.mispred
;
914 p
= left
->branch_info
->flags
.predicted
!= right
->branch_info
->flags
.predicted
;
918 static int hist_entry__mispredict_snprintf(struct hist_entry
*he
, char *bf
,
919 size_t size
, unsigned int width
){
920 static const char *out
= "N/A";
922 if (he
->branch_info
) {
923 if (he
->branch_info
->flags
.predicted
)
925 else if (he
->branch_info
->flags
.mispred
)
929 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, out
);
933 sort__cycles_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
935 if (!left
->branch_info
|| !right
->branch_info
)
936 return cmp_null(left
->branch_info
, right
->branch_info
);
938 return left
->branch_info
->flags
.cycles
-
939 right
->branch_info
->flags
.cycles
;
942 static int hist_entry__cycles_snprintf(struct hist_entry
*he
, char *bf
,
943 size_t size
, unsigned int width
)
945 if (!he
->branch_info
)
946 return scnprintf(bf
, size
, "%-.*s", width
, "N/A");
947 if (he
->branch_info
->flags
.cycles
== 0)
948 return repsep_snprintf(bf
, size
, "%-*s", width
, "-");
949 return repsep_snprintf(bf
, size
, "%-*hd", width
,
950 he
->branch_info
->flags
.cycles
);
953 struct sort_entry sort_cycles
= {
954 .se_header
= "Basic Block Cycles",
955 .se_cmp
= sort__cycles_cmp
,
956 .se_snprintf
= hist_entry__cycles_snprintf
,
957 .se_width_idx
= HISTC_CYCLES
,
960 /* --sort daddr_sym */
962 sort__daddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
964 uint64_t l
= 0, r
= 0;
967 l
= left
->mem_info
->daddr
.addr
;
969 r
= right
->mem_info
->daddr
.addr
;
971 return (int64_t)(r
- l
);
974 static int hist_entry__daddr_snprintf(struct hist_entry
*he
, char *bf
,
975 size_t size
, unsigned int width
)
978 struct map
*map
= NULL
;
979 struct symbol
*sym
= NULL
;
982 addr
= he
->mem_info
->daddr
.addr
;
983 map
= he
->mem_info
->daddr
.map
;
984 sym
= he
->mem_info
->daddr
.sym
;
986 return _hist_entry__sym_snprintf(map
, sym
, addr
, he
->level
, bf
, size
,
991 sort__iaddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
993 uint64_t l
= 0, r
= 0;
996 l
= left
->mem_info
->iaddr
.addr
;
998 r
= right
->mem_info
->iaddr
.addr
;
1000 return (int64_t)(r
- l
);
1003 static int hist_entry__iaddr_snprintf(struct hist_entry
*he
, char *bf
,
1004 size_t size
, unsigned int width
)
1007 struct map
*map
= NULL
;
1008 struct symbol
*sym
= NULL
;
1011 addr
= he
->mem_info
->iaddr
.addr
;
1012 map
= he
->mem_info
->iaddr
.map
;
1013 sym
= he
->mem_info
->iaddr
.sym
;
1015 return _hist_entry__sym_snprintf(map
, sym
, addr
, he
->level
, bf
, size
,
1020 sort__dso_daddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1022 struct map
*map_l
= NULL
;
1023 struct map
*map_r
= NULL
;
1026 map_l
= left
->mem_info
->daddr
.map
;
1027 if (right
->mem_info
)
1028 map_r
= right
->mem_info
->daddr
.map
;
1030 return _sort__dso_cmp(map_l
, map_r
);
1033 static int hist_entry__dso_daddr_snprintf(struct hist_entry
*he
, char *bf
,
1034 size_t size
, unsigned int width
)
1036 struct map
*map
= NULL
;
1039 map
= he
->mem_info
->daddr
.map
;
1041 return _hist_entry__dso_snprintf(map
, bf
, size
, width
);
1045 sort__locked_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1047 union perf_mem_data_src data_src_l
;
1048 union perf_mem_data_src data_src_r
;
1051 data_src_l
= left
->mem_info
->data_src
;
1053 data_src_l
.mem_lock
= PERF_MEM_LOCK_NA
;
1055 if (right
->mem_info
)
1056 data_src_r
= right
->mem_info
->data_src
;
1058 data_src_r
.mem_lock
= PERF_MEM_LOCK_NA
;
1060 return (int64_t)(data_src_r
.mem_lock
- data_src_l
.mem_lock
);
1063 static int hist_entry__locked_snprintf(struct hist_entry
*he
, char *bf
,
1064 size_t size
, unsigned int width
)
1068 perf_mem__lck_scnprintf(out
, sizeof(out
), he
->mem_info
);
1069 return repsep_snprintf(bf
, size
, "%.*s", width
, out
);
1073 sort__tlb_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1075 union perf_mem_data_src data_src_l
;
1076 union perf_mem_data_src data_src_r
;
1079 data_src_l
= left
->mem_info
->data_src
;
1081 data_src_l
.mem_dtlb
= PERF_MEM_TLB_NA
;
1083 if (right
->mem_info
)
1084 data_src_r
= right
->mem_info
->data_src
;
1086 data_src_r
.mem_dtlb
= PERF_MEM_TLB_NA
;
1088 return (int64_t)(data_src_r
.mem_dtlb
- data_src_l
.mem_dtlb
);
1091 static int hist_entry__tlb_snprintf(struct hist_entry
*he
, char *bf
,
1092 size_t size
, unsigned int width
)
1096 perf_mem__tlb_scnprintf(out
, sizeof(out
), he
->mem_info
);
1097 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1101 sort__lvl_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1103 union perf_mem_data_src data_src_l
;
1104 union perf_mem_data_src data_src_r
;
1107 data_src_l
= left
->mem_info
->data_src
;
1109 data_src_l
.mem_lvl
= PERF_MEM_LVL_NA
;
1111 if (right
->mem_info
)
1112 data_src_r
= right
->mem_info
->data_src
;
1114 data_src_r
.mem_lvl
= PERF_MEM_LVL_NA
;
1116 return (int64_t)(data_src_r
.mem_lvl
- data_src_l
.mem_lvl
);
1119 static int hist_entry__lvl_snprintf(struct hist_entry
*he
, char *bf
,
1120 size_t size
, unsigned int width
)
1124 perf_mem__lvl_scnprintf(out
, sizeof(out
), he
->mem_info
);
1125 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1129 sort__snoop_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1131 union perf_mem_data_src data_src_l
;
1132 union perf_mem_data_src data_src_r
;
1135 data_src_l
= left
->mem_info
->data_src
;
1137 data_src_l
.mem_snoop
= PERF_MEM_SNOOP_NA
;
1139 if (right
->mem_info
)
1140 data_src_r
= right
->mem_info
->data_src
;
1142 data_src_r
.mem_snoop
= PERF_MEM_SNOOP_NA
;
1144 return (int64_t)(data_src_r
.mem_snoop
- data_src_l
.mem_snoop
);
1147 static int hist_entry__snoop_snprintf(struct hist_entry
*he
, char *bf
,
1148 size_t size
, unsigned int width
)
1152 perf_mem__snp_scnprintf(out
, sizeof(out
), he
->mem_info
);
1153 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1157 sort__dcacheline_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1160 struct map
*l_map
, *r_map
;
1162 if (!left
->mem_info
) return -1;
1163 if (!right
->mem_info
) return 1;
1165 /* group event types together */
1166 if (left
->cpumode
> right
->cpumode
) return -1;
1167 if (left
->cpumode
< right
->cpumode
) return 1;
1169 l_map
= left
->mem_info
->daddr
.map
;
1170 r_map
= right
->mem_info
->daddr
.map
;
1172 /* if both are NULL, jump to sort on al_addr instead */
1173 if (!l_map
&& !r_map
)
1176 if (!l_map
) return -1;
1177 if (!r_map
) return 1;
1179 if (l_map
->maj
> r_map
->maj
) return -1;
1180 if (l_map
->maj
< r_map
->maj
) return 1;
1182 if (l_map
->min
> r_map
->min
) return -1;
1183 if (l_map
->min
< r_map
->min
) return 1;
1185 if (l_map
->ino
> r_map
->ino
) return -1;
1186 if (l_map
->ino
< r_map
->ino
) return 1;
1188 if (l_map
->ino_generation
> r_map
->ino_generation
) return -1;
1189 if (l_map
->ino_generation
< r_map
->ino_generation
) return 1;
1192 * Addresses with no major/minor numbers are assumed to be
1193 * anonymous in userspace. Sort those on pid then address.
1195 * The kernel and non-zero major/minor mapped areas are
1196 * assumed to be unity mapped. Sort those on address.
1199 if ((left
->cpumode
!= PERF_RECORD_MISC_KERNEL
) &&
1200 (!(l_map
->flags
& MAP_SHARED
)) &&
1201 !l_map
->maj
&& !l_map
->min
&& !l_map
->ino
&&
1202 !l_map
->ino_generation
) {
1203 /* userspace anonymous */
1205 if (left
->thread
->pid_
> right
->thread
->pid_
) return -1;
1206 if (left
->thread
->pid_
< right
->thread
->pid_
) return 1;
1210 /* al_addr does all the right addr - start + offset calculations */
1211 l
= cl_address(left
->mem_info
->daddr
.al_addr
);
1212 r
= cl_address(right
->mem_info
->daddr
.al_addr
);
1214 if (l
> r
) return -1;
1215 if (l
< r
) return 1;
1220 static int hist_entry__dcacheline_snprintf(struct hist_entry
*he
, char *bf
,
1221 size_t size
, unsigned int width
)
1225 struct map
*map
= NULL
;
1226 struct symbol
*sym
= NULL
;
1227 char level
= he
->level
;
1230 addr
= cl_address(he
->mem_info
->daddr
.al_addr
);
1231 map
= he
->mem_info
->daddr
.map
;
1232 sym
= he
->mem_info
->daddr
.sym
;
1234 /* print [s] for shared data mmaps */
1235 if ((he
->cpumode
!= PERF_RECORD_MISC_KERNEL
) &&
1236 map
&& !(map
->prot
& PROT_EXEC
) &&
1237 (map
->flags
& MAP_SHARED
) &&
1238 (map
->maj
|| map
->min
|| map
->ino
||
1239 map
->ino_generation
))
1244 return _hist_entry__sym_snprintf(map
, sym
, addr
, level
, bf
, size
,
1248 struct sort_entry sort_mispredict
= {
1249 .se_header
= "Branch Mispredicted",
1250 .se_cmp
= sort__mispredict_cmp
,
1251 .se_snprintf
= hist_entry__mispredict_snprintf
,
1252 .se_width_idx
= HISTC_MISPREDICT
,
1255 static u64
he_weight(struct hist_entry
*he
)
1257 return he
->stat
.nr_events
? he
->stat
.weight
/ he
->stat
.nr_events
: 0;
1261 sort__local_weight_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1263 return he_weight(left
) - he_weight(right
);
1266 static int hist_entry__local_weight_snprintf(struct hist_entry
*he
, char *bf
,
1267 size_t size
, unsigned int width
)
1269 return repsep_snprintf(bf
, size
, "%-*llu", width
, he_weight(he
));
1272 struct sort_entry sort_local_weight
= {
1273 .se_header
= "Local Weight",
1274 .se_cmp
= sort__local_weight_cmp
,
1275 .se_snprintf
= hist_entry__local_weight_snprintf
,
1276 .se_width_idx
= HISTC_LOCAL_WEIGHT
,
1280 sort__global_weight_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1282 return left
->stat
.weight
- right
->stat
.weight
;
1285 static int hist_entry__global_weight_snprintf(struct hist_entry
*he
, char *bf
,
1286 size_t size
, unsigned int width
)
1288 return repsep_snprintf(bf
, size
, "%-*llu", width
, he
->stat
.weight
);
1291 struct sort_entry sort_global_weight
= {
1292 .se_header
= "Weight",
1293 .se_cmp
= sort__global_weight_cmp
,
1294 .se_snprintf
= hist_entry__global_weight_snprintf
,
1295 .se_width_idx
= HISTC_GLOBAL_WEIGHT
,
1298 struct sort_entry sort_mem_daddr_sym
= {
1299 .se_header
= "Data Symbol",
1300 .se_cmp
= sort__daddr_cmp
,
1301 .se_snprintf
= hist_entry__daddr_snprintf
,
1302 .se_width_idx
= HISTC_MEM_DADDR_SYMBOL
,
1305 struct sort_entry sort_mem_iaddr_sym
= {
1306 .se_header
= "Code Symbol",
1307 .se_cmp
= sort__iaddr_cmp
,
1308 .se_snprintf
= hist_entry__iaddr_snprintf
,
1309 .se_width_idx
= HISTC_MEM_IADDR_SYMBOL
,
1312 struct sort_entry sort_mem_daddr_dso
= {
1313 .se_header
= "Data Object",
1314 .se_cmp
= sort__dso_daddr_cmp
,
1315 .se_snprintf
= hist_entry__dso_daddr_snprintf
,
1316 .se_width_idx
= HISTC_MEM_DADDR_DSO
,
1319 struct sort_entry sort_mem_locked
= {
1320 .se_header
= "Locked",
1321 .se_cmp
= sort__locked_cmp
,
1322 .se_snprintf
= hist_entry__locked_snprintf
,
1323 .se_width_idx
= HISTC_MEM_LOCKED
,
1326 struct sort_entry sort_mem_tlb
= {
1327 .se_header
= "TLB access",
1328 .se_cmp
= sort__tlb_cmp
,
1329 .se_snprintf
= hist_entry__tlb_snprintf
,
1330 .se_width_idx
= HISTC_MEM_TLB
,
1333 struct sort_entry sort_mem_lvl
= {
1334 .se_header
= "Memory access",
1335 .se_cmp
= sort__lvl_cmp
,
1336 .se_snprintf
= hist_entry__lvl_snprintf
,
1337 .se_width_idx
= HISTC_MEM_LVL
,
1340 struct sort_entry sort_mem_snoop
= {
1341 .se_header
= "Snoop",
1342 .se_cmp
= sort__snoop_cmp
,
1343 .se_snprintf
= hist_entry__snoop_snprintf
,
1344 .se_width_idx
= HISTC_MEM_SNOOP
,
1347 struct sort_entry sort_mem_dcacheline
= {
1348 .se_header
= "Data Cacheline",
1349 .se_cmp
= sort__dcacheline_cmp
,
1350 .se_snprintf
= hist_entry__dcacheline_snprintf
,
1351 .se_width_idx
= HISTC_MEM_DCACHELINE
,
1355 sort__phys_daddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1357 uint64_t l
= 0, r
= 0;
1360 l
= left
->mem_info
->daddr
.phys_addr
;
1361 if (right
->mem_info
)
1362 r
= right
->mem_info
->daddr
.phys_addr
;
1364 return (int64_t)(r
- l
);
1367 static int hist_entry__phys_daddr_snprintf(struct hist_entry
*he
, char *bf
,
1368 size_t size
, unsigned int width
)
1372 size_t len
= BITS_PER_LONG
/ 4;
1374 addr
= he
->mem_info
->daddr
.phys_addr
;
1376 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "[%c] ", he
->level
);
1378 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-#.*llx", len
, addr
);
1380 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-*s", width
- ret
, "");
1388 struct sort_entry sort_mem_phys_daddr
= {
1389 .se_header
= "Data Physical Address",
1390 .se_cmp
= sort__phys_daddr_cmp
,
1391 .se_snprintf
= hist_entry__phys_daddr_snprintf
,
1392 .se_width_idx
= HISTC_MEM_PHYS_DADDR
,
1396 sort__abort_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1398 if (!left
->branch_info
|| !right
->branch_info
)
1399 return cmp_null(left
->branch_info
, right
->branch_info
);
1401 return left
->branch_info
->flags
.abort
!=
1402 right
->branch_info
->flags
.abort
;
1405 static int hist_entry__abort_snprintf(struct hist_entry
*he
, char *bf
,
1406 size_t size
, unsigned int width
)
1408 static const char *out
= "N/A";
1410 if (he
->branch_info
) {
1411 if (he
->branch_info
->flags
.abort
)
1417 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1420 struct sort_entry sort_abort
= {
1421 .se_header
= "Transaction abort",
1422 .se_cmp
= sort__abort_cmp
,
1423 .se_snprintf
= hist_entry__abort_snprintf
,
1424 .se_width_idx
= HISTC_ABORT
,
1428 sort__in_tx_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1430 if (!left
->branch_info
|| !right
->branch_info
)
1431 return cmp_null(left
->branch_info
, right
->branch_info
);
1433 return left
->branch_info
->flags
.in_tx
!=
1434 right
->branch_info
->flags
.in_tx
;
1437 static int hist_entry__in_tx_snprintf(struct hist_entry
*he
, char *bf
,
1438 size_t size
, unsigned int width
)
1440 static const char *out
= "N/A";
1442 if (he
->branch_info
) {
1443 if (he
->branch_info
->flags
.in_tx
)
1449 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1452 struct sort_entry sort_in_tx
= {
1453 .se_header
= "Branch in transaction",
1454 .se_cmp
= sort__in_tx_cmp
,
1455 .se_snprintf
= hist_entry__in_tx_snprintf
,
1456 .se_width_idx
= HISTC_IN_TX
,
1460 sort__transaction_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1462 return left
->transaction
- right
->transaction
;
1465 static inline char *add_str(char *p
, const char *str
)
1468 return p
+ strlen(str
);
1471 static struct txbit
{
1476 { PERF_TXN_ELISION
, "EL ", 0 },
1477 { PERF_TXN_TRANSACTION
, "TX ", 1 },
1478 { PERF_TXN_SYNC
, "SYNC ", 1 },
1479 { PERF_TXN_ASYNC
, "ASYNC ", 0 },
1480 { PERF_TXN_RETRY
, "RETRY ", 0 },
1481 { PERF_TXN_CONFLICT
, "CON ", 0 },
1482 { PERF_TXN_CAPACITY_WRITE
, "CAP-WRITE ", 1 },
1483 { PERF_TXN_CAPACITY_READ
, "CAP-READ ", 0 },
1487 int hist_entry__transaction_len(void)
1492 for (i
= 0; txbits
[i
].name
; i
++) {
1493 if (!txbits
[i
].skip_for_len
)
1494 len
+= strlen(txbits
[i
].name
);
1496 len
+= 4; /* :XX<space> */
1500 static int hist_entry__transaction_snprintf(struct hist_entry
*he
, char *bf
,
1501 size_t size
, unsigned int width
)
1503 u64 t
= he
->transaction
;
1509 for (i
= 0; txbits
[i
].name
; i
++)
1510 if (txbits
[i
].flag
& t
)
1511 p
= add_str(p
, txbits
[i
].name
);
1512 if (t
&& !(t
& (PERF_TXN_SYNC
|PERF_TXN_ASYNC
)))
1513 p
= add_str(p
, "NEITHER ");
1514 if (t
& PERF_TXN_ABORT_MASK
) {
1515 sprintf(p
, ":%" PRIx64
,
1516 (t
& PERF_TXN_ABORT_MASK
) >>
1517 PERF_TXN_ABORT_SHIFT
);
1521 return repsep_snprintf(bf
, size
, "%-*s", width
, buf
);
1524 struct sort_entry sort_transaction
= {
1525 .se_header
= "Transaction ",
1526 .se_cmp
= sort__transaction_cmp
,
1527 .se_snprintf
= hist_entry__transaction_snprintf
,
1528 .se_width_idx
= HISTC_TRANSACTION
,
1531 /* --sort symbol_size */
1533 static int64_t _sort__sym_size_cmp(struct symbol
*sym_l
, struct symbol
*sym_r
)
1535 int64_t size_l
= sym_l
!= NULL
? symbol__size(sym_l
) : 0;
1536 int64_t size_r
= sym_r
!= NULL
? symbol__size(sym_r
) : 0;
1538 return size_l
< size_r
? -1 :
1539 size_l
== size_r
? 0 : 1;
1543 sort__sym_size_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1545 return _sort__sym_size_cmp(right
->ms
.sym
, left
->ms
.sym
);
1548 static int _hist_entry__sym_size_snprintf(struct symbol
*sym
, char *bf
,
1549 size_t bf_size
, unsigned int width
)
1552 return repsep_snprintf(bf
, bf_size
, "%*d", width
, symbol__size(sym
));
1554 return repsep_snprintf(bf
, bf_size
, "%*s", width
, "unknown");
1557 static int hist_entry__sym_size_snprintf(struct hist_entry
*he
, char *bf
,
1558 size_t size
, unsigned int width
)
1560 return _hist_entry__sym_size_snprintf(he
->ms
.sym
, bf
, size
, width
);
1563 struct sort_entry sort_sym_size
= {
1564 .se_header
= "Symbol size",
1565 .se_cmp
= sort__sym_size_cmp
,
1566 .se_snprintf
= hist_entry__sym_size_snprintf
,
1567 .se_width_idx
= HISTC_SYM_SIZE
,
1570 /* --sort dso_size */
1572 static int64_t _sort__dso_size_cmp(struct map
*map_l
, struct map
*map_r
)
1574 int64_t size_l
= map_l
!= NULL
? map__size(map_l
) : 0;
1575 int64_t size_r
= map_r
!= NULL
? map__size(map_r
) : 0;
1577 return size_l
< size_r
? -1 :
1578 size_l
== size_r
? 0 : 1;
1582 sort__dso_size_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1584 return _sort__dso_size_cmp(right
->ms
.map
, left
->ms
.map
);
1587 static int _hist_entry__dso_size_snprintf(struct map
*map
, char *bf
,
1588 size_t bf_size
, unsigned int width
)
1590 if (map
&& map
->dso
)
1591 return repsep_snprintf(bf
, bf_size
, "%*d", width
,
1594 return repsep_snprintf(bf
, bf_size
, "%*s", width
, "unknown");
1597 static int hist_entry__dso_size_snprintf(struct hist_entry
*he
, char *bf
,
1598 size_t size
, unsigned int width
)
1600 return _hist_entry__dso_size_snprintf(he
->ms
.map
, bf
, size
, width
);
1603 struct sort_entry sort_dso_size
= {
1604 .se_header
= "DSO size",
1605 .se_cmp
= sort__dso_size_cmp
,
1606 .se_snprintf
= hist_entry__dso_size_snprintf
,
1607 .se_width_idx
= HISTC_DSO_SIZE
,
1611 struct sort_dimension
{
1613 struct sort_entry
*entry
;
1617 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1619 static struct sort_dimension common_sort_dimensions
[] = {
1620 DIM(SORT_PID
, "pid", sort_thread
),
1621 DIM(SORT_COMM
, "comm", sort_comm
),
1622 DIM(SORT_DSO
, "dso", sort_dso
),
1623 DIM(SORT_SYM
, "symbol", sort_sym
),
1624 DIM(SORT_PARENT
, "parent", sort_parent
),
1625 DIM(SORT_CPU
, "cpu", sort_cpu
),
1626 DIM(SORT_SOCKET
, "socket", sort_socket
),
1627 DIM(SORT_SRCLINE
, "srcline", sort_srcline
),
1628 DIM(SORT_SRCFILE
, "srcfile", sort_srcfile
),
1629 DIM(SORT_LOCAL_WEIGHT
, "local_weight", sort_local_weight
),
1630 DIM(SORT_GLOBAL_WEIGHT
, "weight", sort_global_weight
),
1631 DIM(SORT_TRANSACTION
, "transaction", sort_transaction
),
1632 DIM(SORT_TRACE
, "trace", sort_trace
),
1633 DIM(SORT_SYM_SIZE
, "symbol_size", sort_sym_size
),
1634 DIM(SORT_DSO_SIZE
, "dso_size", sort_dso_size
),
1635 DIM(SORT_CGROUP_ID
, "cgroup_id", sort_cgroup_id
),
1636 DIM(SORT_SYM_IPC_NULL
, "ipc_null", sort_sym_ipc_null
),
1641 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1643 static struct sort_dimension bstack_sort_dimensions
[] = {
1644 DIM(SORT_DSO_FROM
, "dso_from", sort_dso_from
),
1645 DIM(SORT_DSO_TO
, "dso_to", sort_dso_to
),
1646 DIM(SORT_SYM_FROM
, "symbol_from", sort_sym_from
),
1647 DIM(SORT_SYM_TO
, "symbol_to", sort_sym_to
),
1648 DIM(SORT_MISPREDICT
, "mispredict", sort_mispredict
),
1649 DIM(SORT_IN_TX
, "in_tx", sort_in_tx
),
1650 DIM(SORT_ABORT
, "abort", sort_abort
),
1651 DIM(SORT_CYCLES
, "cycles", sort_cycles
),
1652 DIM(SORT_SRCLINE_FROM
, "srcline_from", sort_srcline_from
),
1653 DIM(SORT_SRCLINE_TO
, "srcline_to", sort_srcline_to
),
1654 DIM(SORT_SYM_IPC
, "ipc_lbr", sort_sym_ipc
),
1659 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1661 static struct sort_dimension memory_sort_dimensions
[] = {
1662 DIM(SORT_MEM_DADDR_SYMBOL
, "symbol_daddr", sort_mem_daddr_sym
),
1663 DIM(SORT_MEM_IADDR_SYMBOL
, "symbol_iaddr", sort_mem_iaddr_sym
),
1664 DIM(SORT_MEM_DADDR_DSO
, "dso_daddr", sort_mem_daddr_dso
),
1665 DIM(SORT_MEM_LOCKED
, "locked", sort_mem_locked
),
1666 DIM(SORT_MEM_TLB
, "tlb", sort_mem_tlb
),
1667 DIM(SORT_MEM_LVL
, "mem", sort_mem_lvl
),
1668 DIM(SORT_MEM_SNOOP
, "snoop", sort_mem_snoop
),
1669 DIM(SORT_MEM_DCACHELINE
, "dcacheline", sort_mem_dcacheline
),
1670 DIM(SORT_MEM_PHYS_DADDR
, "phys_daddr", sort_mem_phys_daddr
),
1675 struct hpp_dimension
{
1677 struct perf_hpp_fmt
*fmt
;
1681 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1683 static struct hpp_dimension hpp_sort_dimensions
[] = {
1684 DIM(PERF_HPP__OVERHEAD
, "overhead"),
1685 DIM(PERF_HPP__OVERHEAD_SYS
, "overhead_sys"),
1686 DIM(PERF_HPP__OVERHEAD_US
, "overhead_us"),
1687 DIM(PERF_HPP__OVERHEAD_GUEST_SYS
, "overhead_guest_sys"),
1688 DIM(PERF_HPP__OVERHEAD_GUEST_US
, "overhead_guest_us"),
1689 DIM(PERF_HPP__OVERHEAD_ACC
, "overhead_children"),
1690 DIM(PERF_HPP__SAMPLES
, "sample"),
1691 DIM(PERF_HPP__PERIOD
, "period"),
1696 struct hpp_sort_entry
{
1697 struct perf_hpp_fmt hpp
;
1698 struct sort_entry
*se
;
1701 void perf_hpp__reset_sort_width(struct perf_hpp_fmt
*fmt
, struct hists
*hists
)
1703 struct hpp_sort_entry
*hse
;
1705 if (!perf_hpp__is_sort_entry(fmt
))
1708 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1709 hists__new_col_len(hists
, hse
->se
->se_width_idx
, strlen(fmt
->name
));
1712 static int __sort__hpp_header(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1713 struct hists
*hists
, int line __maybe_unused
,
1714 int *span __maybe_unused
)
1716 struct hpp_sort_entry
*hse
;
1717 size_t len
= fmt
->user_len
;
1719 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1722 len
= hists__col_len(hists
, hse
->se
->se_width_idx
);
1724 return scnprintf(hpp
->buf
, hpp
->size
, "%-*.*s", len
, len
, fmt
->name
);
1727 static int __sort__hpp_width(struct perf_hpp_fmt
*fmt
,
1728 struct perf_hpp
*hpp __maybe_unused
,
1729 struct hists
*hists
)
1731 struct hpp_sort_entry
*hse
;
1732 size_t len
= fmt
->user_len
;
1734 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1737 len
= hists__col_len(hists
, hse
->se
->se_width_idx
);
1742 static int __sort__hpp_entry(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1743 struct hist_entry
*he
)
1745 struct hpp_sort_entry
*hse
;
1746 size_t len
= fmt
->user_len
;
1748 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1751 len
= hists__col_len(he
->hists
, hse
->se
->se_width_idx
);
1753 return hse
->se
->se_snprintf(he
, hpp
->buf
, hpp
->size
, len
);
1756 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt
*fmt
,
1757 struct hist_entry
*a
, struct hist_entry
*b
)
1759 struct hpp_sort_entry
*hse
;
1761 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1762 return hse
->se
->se_cmp(a
, b
);
1765 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt
*fmt
,
1766 struct hist_entry
*a
, struct hist_entry
*b
)
1768 struct hpp_sort_entry
*hse
;
1769 int64_t (*collapse_fn
)(struct hist_entry
*, struct hist_entry
*);
1771 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1772 collapse_fn
= hse
->se
->se_collapse
?: hse
->se
->se_cmp
;
1773 return collapse_fn(a
, b
);
1776 static int64_t __sort__hpp_sort(struct perf_hpp_fmt
*fmt
,
1777 struct hist_entry
*a
, struct hist_entry
*b
)
1779 struct hpp_sort_entry
*hse
;
1780 int64_t (*sort_fn
)(struct hist_entry
*, struct hist_entry
*);
1782 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1783 sort_fn
= hse
->se
->se_sort
?: hse
->se
->se_cmp
;
1784 return sort_fn(a
, b
);
1787 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt
*format
)
1789 return format
->header
== __sort__hpp_header
;
1792 #define MK_SORT_ENTRY_CHK(key) \
1793 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1795 struct hpp_sort_entry *hse; \
1797 if (!perf_hpp__is_sort_entry(fmt)) \
1800 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1801 return hse->se == &sort_ ## key ; \
1804 MK_SORT_ENTRY_CHK(trace
)
1805 MK_SORT_ENTRY_CHK(srcline
)
1806 MK_SORT_ENTRY_CHK(srcfile
)
1807 MK_SORT_ENTRY_CHK(thread
)
1808 MK_SORT_ENTRY_CHK(comm
)
1809 MK_SORT_ENTRY_CHK(dso
)
1810 MK_SORT_ENTRY_CHK(sym
)
1813 static bool __sort__hpp_equal(struct perf_hpp_fmt
*a
, struct perf_hpp_fmt
*b
)
1815 struct hpp_sort_entry
*hse_a
;
1816 struct hpp_sort_entry
*hse_b
;
1818 if (!perf_hpp__is_sort_entry(a
) || !perf_hpp__is_sort_entry(b
))
1821 hse_a
= container_of(a
, struct hpp_sort_entry
, hpp
);
1822 hse_b
= container_of(b
, struct hpp_sort_entry
, hpp
);
1824 return hse_a
->se
== hse_b
->se
;
1827 static void hse_free(struct perf_hpp_fmt
*fmt
)
1829 struct hpp_sort_entry
*hse
;
1831 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1835 static struct hpp_sort_entry
*
1836 __sort_dimension__alloc_hpp(struct sort_dimension
*sd
, int level
)
1838 struct hpp_sort_entry
*hse
;
1840 hse
= malloc(sizeof(*hse
));
1842 pr_err("Memory allocation failed\n");
1846 hse
->se
= sd
->entry
;
1847 hse
->hpp
.name
= sd
->entry
->se_header
;
1848 hse
->hpp
.header
= __sort__hpp_header
;
1849 hse
->hpp
.width
= __sort__hpp_width
;
1850 hse
->hpp
.entry
= __sort__hpp_entry
;
1851 hse
->hpp
.color
= NULL
;
1853 hse
->hpp
.cmp
= __sort__hpp_cmp
;
1854 hse
->hpp
.collapse
= __sort__hpp_collapse
;
1855 hse
->hpp
.sort
= __sort__hpp_sort
;
1856 hse
->hpp
.equal
= __sort__hpp_equal
;
1857 hse
->hpp
.free
= hse_free
;
1859 INIT_LIST_HEAD(&hse
->hpp
.list
);
1860 INIT_LIST_HEAD(&hse
->hpp
.sort_list
);
1861 hse
->hpp
.elide
= false;
1863 hse
->hpp
.user_len
= 0;
1864 hse
->hpp
.level
= level
;
1869 static void hpp_free(struct perf_hpp_fmt
*fmt
)
1874 static struct perf_hpp_fmt
*__hpp_dimension__alloc_hpp(struct hpp_dimension
*hd
,
1877 struct perf_hpp_fmt
*fmt
;
1879 fmt
= memdup(hd
->fmt
, sizeof(*fmt
));
1881 INIT_LIST_HEAD(&fmt
->list
);
1882 INIT_LIST_HEAD(&fmt
->sort_list
);
1883 fmt
->free
= hpp_free
;
1890 int hist_entry__filter(struct hist_entry
*he
, int type
, const void *arg
)
1892 struct perf_hpp_fmt
*fmt
;
1893 struct hpp_sort_entry
*hse
;
1897 perf_hpp_list__for_each_format(he
->hpp_list
, fmt
) {
1898 if (!perf_hpp__is_sort_entry(fmt
))
1901 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1902 if (hse
->se
->se_filter
== NULL
)
1906 * hist entry is filtered if any of sort key in the hpp list
1907 * is applied. But it should skip non-matched filter types.
1909 r
= hse
->se
->se_filter(he
, type
, arg
);
1920 static int __sort_dimension__add_hpp_sort(struct sort_dimension
*sd
,
1921 struct perf_hpp_list
*list
,
1924 struct hpp_sort_entry
*hse
= __sort_dimension__alloc_hpp(sd
, level
);
1929 perf_hpp_list__register_sort_field(list
, &hse
->hpp
);
1933 static int __sort_dimension__add_hpp_output(struct sort_dimension
*sd
,
1934 struct perf_hpp_list
*list
)
1936 struct hpp_sort_entry
*hse
= __sort_dimension__alloc_hpp(sd
, 0);
1941 perf_hpp_list__column_register(list
, &hse
->hpp
);
1945 struct hpp_dynamic_entry
{
1946 struct perf_hpp_fmt hpp
;
1947 struct perf_evsel
*evsel
;
1948 struct tep_format_field
*field
;
1949 unsigned dynamic_len
;
1953 static int hde_width(struct hpp_dynamic_entry
*hde
)
1955 if (!hde
->hpp
.len
) {
1956 int len
= hde
->dynamic_len
;
1957 int namelen
= strlen(hde
->field
->name
);
1958 int fieldlen
= hde
->field
->size
;
1963 if (!(hde
->field
->flags
& TEP_FIELD_IS_STRING
)) {
1964 /* length for print hex numbers */
1965 fieldlen
= hde
->field
->size
* 2 + 2;
1972 return hde
->hpp
.len
;
1975 static void update_dynamic_len(struct hpp_dynamic_entry
*hde
,
1976 struct hist_entry
*he
)
1979 struct tep_format_field
*field
= hde
->field
;
1986 /* parse pretty print result and update max length */
1987 if (!he
->trace_output
)
1988 he
->trace_output
= get_trace_output(he
);
1990 namelen
= strlen(field
->name
);
1991 str
= he
->trace_output
;
1994 pos
= strchr(str
, ' ');
1997 pos
= str
+ strlen(str
);
2000 if (!strncmp(str
, field
->name
, namelen
)) {
2006 if (len
> hde
->dynamic_len
)
2007 hde
->dynamic_len
= len
;
2018 static int __sort__hde_header(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
2019 struct hists
*hists __maybe_unused
,
2020 int line __maybe_unused
,
2021 int *span __maybe_unused
)
2023 struct hpp_dynamic_entry
*hde
;
2024 size_t len
= fmt
->user_len
;
2026 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2029 len
= hde_width(hde
);
2031 return scnprintf(hpp
->buf
, hpp
->size
, "%*.*s", len
, len
, hde
->field
->name
);
2034 static int __sort__hde_width(struct perf_hpp_fmt
*fmt
,
2035 struct perf_hpp
*hpp __maybe_unused
,
2036 struct hists
*hists __maybe_unused
)
2038 struct hpp_dynamic_entry
*hde
;
2039 size_t len
= fmt
->user_len
;
2041 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2044 len
= hde_width(hde
);
2049 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt
*fmt
, struct hists
*hists
)
2051 struct hpp_dynamic_entry
*hde
;
2053 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2055 return hists_to_evsel(hists
) == hde
->evsel
;
2058 static int __sort__hde_entry(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
2059 struct hist_entry
*he
)
2061 struct hpp_dynamic_entry
*hde
;
2062 size_t len
= fmt
->user_len
;
2064 struct tep_format_field
*field
;
2069 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2072 len
= hde_width(hde
);
2077 if (!he
->trace_output
)
2078 he
->trace_output
= get_trace_output(he
);
2081 namelen
= strlen(field
->name
);
2082 str
= he
->trace_output
;
2085 pos
= strchr(str
, ' ');
2088 pos
= str
+ strlen(str
);
2091 if (!strncmp(str
, field
->name
, namelen
)) {
2093 str
= strndup(str
, pos
- str
);
2096 return scnprintf(hpp
->buf
, hpp
->size
,
2097 "%*.*s", len
, len
, "ERROR");
2108 struct trace_seq seq
;
2110 trace_seq_init(&seq
);
2111 tep_print_field(&seq
, he
->raw_data
, hde
->field
);
2115 ret
= scnprintf(hpp
->buf
, hpp
->size
, "%*.*s", len
, len
, str
);
2120 static int64_t __sort__hde_cmp(struct perf_hpp_fmt
*fmt
,
2121 struct hist_entry
*a
, struct hist_entry
*b
)
2123 struct hpp_dynamic_entry
*hde
;
2124 struct tep_format_field
*field
;
2125 unsigned offset
, size
;
2127 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2130 update_dynamic_len(hde
, a
);
2135 if (field
->flags
& TEP_FIELD_IS_DYNAMIC
) {
2136 unsigned long long dyn
;
2138 tep_read_number_field(field
, a
->raw_data
, &dyn
);
2139 offset
= dyn
& 0xffff;
2140 size
= (dyn
>> 16) & 0xffff;
2142 /* record max width for output */
2143 if (size
> hde
->dynamic_len
)
2144 hde
->dynamic_len
= size
;
2146 offset
= field
->offset
;
2150 return memcmp(a
->raw_data
+ offset
, b
->raw_data
+ offset
, size
);
2153 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt
*fmt
)
2155 return fmt
->cmp
== __sort__hde_cmp
;
2158 static bool __sort__hde_equal(struct perf_hpp_fmt
*a
, struct perf_hpp_fmt
*b
)
2160 struct hpp_dynamic_entry
*hde_a
;
2161 struct hpp_dynamic_entry
*hde_b
;
2163 if (!perf_hpp__is_dynamic_entry(a
) || !perf_hpp__is_dynamic_entry(b
))
2166 hde_a
= container_of(a
, struct hpp_dynamic_entry
, hpp
);
2167 hde_b
= container_of(b
, struct hpp_dynamic_entry
, hpp
);
2169 return hde_a
->field
== hde_b
->field
;
2172 static void hde_free(struct perf_hpp_fmt
*fmt
)
2174 struct hpp_dynamic_entry
*hde
;
2176 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2180 static struct hpp_dynamic_entry
*
2181 __alloc_dynamic_entry(struct perf_evsel
*evsel
, struct tep_format_field
*field
,
2184 struct hpp_dynamic_entry
*hde
;
2186 hde
= malloc(sizeof(*hde
));
2188 pr_debug("Memory allocation failed\n");
2194 hde
->dynamic_len
= 0;
2196 hde
->hpp
.name
= field
->name
;
2197 hde
->hpp
.header
= __sort__hde_header
;
2198 hde
->hpp
.width
= __sort__hde_width
;
2199 hde
->hpp
.entry
= __sort__hde_entry
;
2200 hde
->hpp
.color
= NULL
;
2202 hde
->hpp
.cmp
= __sort__hde_cmp
;
2203 hde
->hpp
.collapse
= __sort__hde_cmp
;
2204 hde
->hpp
.sort
= __sort__hde_cmp
;
2205 hde
->hpp
.equal
= __sort__hde_equal
;
2206 hde
->hpp
.free
= hde_free
;
2208 INIT_LIST_HEAD(&hde
->hpp
.list
);
2209 INIT_LIST_HEAD(&hde
->hpp
.sort_list
);
2210 hde
->hpp
.elide
= false;
2212 hde
->hpp
.user_len
= 0;
2213 hde
->hpp
.level
= level
;
2218 struct perf_hpp_fmt
*perf_hpp_fmt__dup(struct perf_hpp_fmt
*fmt
)
2220 struct perf_hpp_fmt
*new_fmt
= NULL
;
2222 if (perf_hpp__is_sort_entry(fmt
)) {
2223 struct hpp_sort_entry
*hse
, *new_hse
;
2225 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
2226 new_hse
= memdup(hse
, sizeof(*hse
));
2228 new_fmt
= &new_hse
->hpp
;
2229 } else if (perf_hpp__is_dynamic_entry(fmt
)) {
2230 struct hpp_dynamic_entry
*hde
, *new_hde
;
2232 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
2233 new_hde
= memdup(hde
, sizeof(*hde
));
2235 new_fmt
= &new_hde
->hpp
;
2237 new_fmt
= memdup(fmt
, sizeof(*fmt
));
2240 INIT_LIST_HEAD(&new_fmt
->list
);
2241 INIT_LIST_HEAD(&new_fmt
->sort_list
);
2246 static int parse_field_name(char *str
, char **event
, char **field
, char **opt
)
2248 char *event_name
, *field_name
, *opt_name
;
2251 field_name
= strchr(str
, '.');
2254 *field_name
++ = '\0';
2260 opt_name
= strchr(field_name
, '/');
2264 *event
= event_name
;
2265 *field
= field_name
;
2271 /* find match evsel using a given event name. The event name can be:
2272 * 1. '%' + event index (e.g. '%1' for first event)
2273 * 2. full event name (e.g. sched:sched_switch)
2274 * 3. partial event name (should not contain ':')
2276 static struct perf_evsel
*find_evsel(struct perf_evlist
*evlist
, char *event_name
)
2278 struct perf_evsel
*evsel
= NULL
;
2279 struct perf_evsel
*pos
;
2283 if (event_name
[0] == '%') {
2284 int nr
= strtol(event_name
+1, NULL
, 0);
2286 if (nr
> evlist
->nr_entries
)
2289 evsel
= perf_evlist__first(evlist
);
2291 evsel
= perf_evsel__next(evsel
);
2296 full_name
= !!strchr(event_name
, ':');
2297 evlist__for_each_entry(evlist
, pos
) {
2299 if (full_name
&& !strcmp(pos
->name
, event_name
))
2302 if (!full_name
&& strstr(pos
->name
, event_name
)) {
2304 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2305 event_name
, evsel
->name
, pos
->name
);
2315 static int __dynamic_dimension__add(struct perf_evsel
*evsel
,
2316 struct tep_format_field
*field
,
2317 bool raw_trace
, int level
)
2319 struct hpp_dynamic_entry
*hde
;
2321 hde
= __alloc_dynamic_entry(evsel
, field
, level
);
2325 hde
->raw_trace
= raw_trace
;
2327 perf_hpp__register_sort_field(&hde
->hpp
);
2331 static int add_evsel_fields(struct perf_evsel
*evsel
, bool raw_trace
, int level
)
2334 struct tep_format_field
*field
;
2336 field
= evsel
->tp_format
->format
.fields
;
2338 ret
= __dynamic_dimension__add(evsel
, field
, raw_trace
, level
);
2342 field
= field
->next
;
2347 static int add_all_dynamic_fields(struct perf_evlist
*evlist
, bool raw_trace
,
2351 struct perf_evsel
*evsel
;
2353 evlist__for_each_entry(evlist
, evsel
) {
2354 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
2357 ret
= add_evsel_fields(evsel
, raw_trace
, level
);
2364 static int add_all_matching_fields(struct perf_evlist
*evlist
,
2365 char *field_name
, bool raw_trace
, int level
)
2368 struct perf_evsel
*evsel
;
2369 struct tep_format_field
*field
;
2371 evlist__for_each_entry(evlist
, evsel
) {
2372 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
2375 field
= tep_find_any_field(evsel
->tp_format
, field_name
);
2379 ret
= __dynamic_dimension__add(evsel
, field
, raw_trace
, level
);
2386 static int add_dynamic_entry(struct perf_evlist
*evlist
, const char *tok
,
2389 char *str
, *event_name
, *field_name
, *opt_name
;
2390 struct perf_evsel
*evsel
;
2391 struct tep_format_field
*field
;
2392 bool raw_trace
= symbol_conf
.raw_trace
;
2402 if (parse_field_name(str
, &event_name
, &field_name
, &opt_name
) < 0) {
2408 if (strcmp(opt_name
, "raw")) {
2409 pr_debug("unsupported field option %s\n", opt_name
);
2416 if (!strcmp(field_name
, "trace_fields")) {
2417 ret
= add_all_dynamic_fields(evlist
, raw_trace
, level
);
2421 if (event_name
== NULL
) {
2422 ret
= add_all_matching_fields(evlist
, field_name
, raw_trace
, level
);
2426 evsel
= find_evsel(evlist
, event_name
);
2427 if (evsel
== NULL
) {
2428 pr_debug("Cannot find event: %s\n", event_name
);
2433 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
) {
2434 pr_debug("%s is not a tracepoint event\n", event_name
);
2439 if (!strcmp(field_name
, "*")) {
2440 ret
= add_evsel_fields(evsel
, raw_trace
, level
);
2442 field
= tep_find_any_field(evsel
->tp_format
, field_name
);
2443 if (field
== NULL
) {
2444 pr_debug("Cannot find event field for %s.%s\n",
2445 event_name
, field_name
);
2449 ret
= __dynamic_dimension__add(evsel
, field
, raw_trace
, level
);
2457 static int __sort_dimension__add(struct sort_dimension
*sd
,
2458 struct perf_hpp_list
*list
,
2464 if (__sort_dimension__add_hpp_sort(sd
, list
, level
) < 0)
2467 if (sd
->entry
->se_collapse
)
2468 list
->need_collapse
= 1;
2475 static int __hpp_dimension__add(struct hpp_dimension
*hd
,
2476 struct perf_hpp_list
*list
,
2479 struct perf_hpp_fmt
*fmt
;
2484 fmt
= __hpp_dimension__alloc_hpp(hd
, level
);
2489 perf_hpp_list__register_sort_field(list
, fmt
);
2493 static int __sort_dimension__add_output(struct perf_hpp_list
*list
,
2494 struct sort_dimension
*sd
)
2499 if (__sort_dimension__add_hpp_output(sd
, list
) < 0)
2506 static int __hpp_dimension__add_output(struct perf_hpp_list
*list
,
2507 struct hpp_dimension
*hd
)
2509 struct perf_hpp_fmt
*fmt
;
2514 fmt
= __hpp_dimension__alloc_hpp(hd
, 0);
2519 perf_hpp_list__column_register(list
, fmt
);
2523 int hpp_dimension__add_output(unsigned col
)
2525 BUG_ON(col
>= PERF_HPP__MAX_INDEX
);
2526 return __hpp_dimension__add_output(&perf_hpp_list
, &hpp_sort_dimensions
[col
]);
2529 int sort_dimension__add(struct perf_hpp_list
*list
, const char *tok
,
2530 struct perf_evlist
*evlist
,
2535 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++) {
2536 struct sort_dimension
*sd
= &common_sort_dimensions
[i
];
2538 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2541 if (sd
->entry
== &sort_parent
) {
2542 int ret
= regcomp(&parent_regex
, parent_pattern
, REG_EXTENDED
);
2546 regerror(ret
, &parent_regex
, err
, sizeof(err
));
2547 pr_err("Invalid regex: %s\n%s", parent_pattern
, err
);
2551 } else if (sd
->entry
== &sort_sym
) {
2554 * perf diff displays the performance difference amongst
2555 * two or more perf.data files. Those files could come
2556 * from different binaries. So we should not compare
2557 * their ips, but the name of symbol.
2559 if (sort__mode
== SORT_MODE__DIFF
)
2560 sd
->entry
->se_collapse
= sort__sym_sort
;
2562 } else if (sd
->entry
== &sort_dso
) {
2564 } else if (sd
->entry
== &sort_socket
) {
2566 } else if (sd
->entry
== &sort_thread
) {
2568 } else if (sd
->entry
== &sort_comm
) {
2572 return __sort_dimension__add(sd
, list
, level
);
2575 for (i
= 0; i
< ARRAY_SIZE(hpp_sort_dimensions
); i
++) {
2576 struct hpp_dimension
*hd
= &hpp_sort_dimensions
[i
];
2578 if (strncasecmp(tok
, hd
->name
, strlen(tok
)))
2581 return __hpp_dimension__add(hd
, list
, level
);
2584 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++) {
2585 struct sort_dimension
*sd
= &bstack_sort_dimensions
[i
];
2587 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2590 if (sort__mode
!= SORT_MODE__BRANCH
)
2593 if (sd
->entry
== &sort_sym_from
|| sd
->entry
== &sort_sym_to
)
2596 __sort_dimension__add(sd
, list
, level
);
2600 for (i
= 0; i
< ARRAY_SIZE(memory_sort_dimensions
); i
++) {
2601 struct sort_dimension
*sd
= &memory_sort_dimensions
[i
];
2603 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2606 if (sort__mode
!= SORT_MODE__MEMORY
)
2609 if (sd
->entry
== &sort_mem_dcacheline
&& cacheline_size() == 0)
2612 if (sd
->entry
== &sort_mem_daddr_sym
)
2615 __sort_dimension__add(sd
, list
, level
);
2619 if (!add_dynamic_entry(evlist
, tok
, level
))
2625 static int setup_sort_list(struct perf_hpp_list
*list
, char *str
,
2626 struct perf_evlist
*evlist
)
2632 bool in_group
= false;
2636 tmp
= strpbrk(str
, "{}, ");
2641 next_level
= level
+ 1;
2645 else if (*tmp
== '}')
2653 ret
= sort_dimension__add(list
, tok
, evlist
, level
);
2654 if (ret
== -EINVAL
) {
2655 if (!cacheline_size() && !strncasecmp(tok
, "dcacheline", strlen(tok
)))
2656 pr_err("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2658 pr_err("Invalid --sort key: `%s'", tok
);
2660 } else if (ret
== -ESRCH
) {
2661 pr_err("Unknown --sort key: `%s'", tok
);
2672 static const char *get_default_sort_order(struct perf_evlist
*evlist
)
2674 const char *default_sort_orders
[] = {
2676 default_branch_sort_order
,
2677 default_mem_sort_order
,
2678 default_top_sort_order
,
2679 default_diff_sort_order
,
2680 default_tracepoint_sort_order
,
2682 bool use_trace
= true;
2683 struct perf_evsel
*evsel
;
2685 BUG_ON(sort__mode
>= ARRAY_SIZE(default_sort_orders
));
2687 if (evlist
== NULL
|| perf_evlist__empty(evlist
))
2690 evlist__for_each_entry(evlist
, evsel
) {
2691 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
) {
2698 sort__mode
= SORT_MODE__TRACEPOINT
;
2699 if (symbol_conf
.raw_trace
)
2700 return "trace_fields";
2703 return default_sort_orders
[sort__mode
];
2706 static int setup_sort_order(struct perf_evlist
*evlist
)
2708 char *new_sort_order
;
2711 * Append '+'-prefixed sort order to the default sort
2714 if (!sort_order
|| is_strict_order(sort_order
))
2717 if (sort_order
[1] == '\0') {
2718 pr_err("Invalid --sort key: `+'");
2723 * We allocate new sort_order string, but we never free it,
2724 * because it's checked over the rest of the code.
2726 if (asprintf(&new_sort_order
, "%s,%s",
2727 get_default_sort_order(evlist
), sort_order
+ 1) < 0) {
2728 pr_err("Not enough memory to set up --sort");
2732 sort_order
= new_sort_order
;
2737 * Adds 'pre,' prefix into 'str' is 'pre' is
2738 * not already part of 'str'.
2740 static char *prefix_if_not_in(const char *pre
, char *str
)
2744 if (!str
|| strstr(str
, pre
))
2747 if (asprintf(&n
, "%s,%s", pre
, str
) < 0)
2754 static char *setup_overhead(char *keys
)
2756 if (sort__mode
== SORT_MODE__DIFF
)
2759 keys
= prefix_if_not_in("overhead", keys
);
2761 if (symbol_conf
.cumulate_callchain
)
2762 keys
= prefix_if_not_in("overhead_children", keys
);
2767 static int __setup_sorting(struct perf_evlist
*evlist
)
2770 const char *sort_keys
;
2773 ret
= setup_sort_order(evlist
);
2777 sort_keys
= sort_order
;
2778 if (sort_keys
== NULL
) {
2779 if (is_strict_order(field_order
)) {
2781 * If user specified field order but no sort order,
2782 * we'll honor it and not add default sort orders.
2787 sort_keys
= get_default_sort_order(evlist
);
2790 str
= strdup(sort_keys
);
2792 pr_err("Not enough memory to setup sort keys");
2797 * Prepend overhead fields for backward compatibility.
2799 if (!is_strict_order(field_order
)) {
2800 str
= setup_overhead(str
);
2802 pr_err("Not enough memory to setup overhead keys");
2807 ret
= setup_sort_list(&perf_hpp_list
, str
, evlist
);
2813 void perf_hpp__set_elide(int idx
, bool elide
)
2815 struct perf_hpp_fmt
*fmt
;
2816 struct hpp_sort_entry
*hse
;
2818 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2819 if (!perf_hpp__is_sort_entry(fmt
))
2822 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
2823 if (hse
->se
->se_width_idx
== idx
) {
2830 static bool __get_elide(struct strlist
*list
, const char *list_name
, FILE *fp
)
2832 if (list
&& strlist__nr_entries(list
) == 1) {
2834 fprintf(fp
, "# %s: %s\n", list_name
,
2835 strlist__entry(list
, 0)->s
);
2841 static bool get_elide(int idx
, FILE *output
)
2845 return __get_elide(symbol_conf
.sym_list
, "symbol", output
);
2847 return __get_elide(symbol_conf
.dso_list
, "dso", output
);
2849 return __get_elide(symbol_conf
.comm_list
, "comm", output
);
2854 if (sort__mode
!= SORT_MODE__BRANCH
)
2858 case HISTC_SYMBOL_FROM
:
2859 return __get_elide(symbol_conf
.sym_from_list
, "sym_from", output
);
2860 case HISTC_SYMBOL_TO
:
2861 return __get_elide(symbol_conf
.sym_to_list
, "sym_to", output
);
2862 case HISTC_DSO_FROM
:
2863 return __get_elide(symbol_conf
.dso_from_list
, "dso_from", output
);
2865 return __get_elide(symbol_conf
.dso_to_list
, "dso_to", output
);
2873 void sort__setup_elide(FILE *output
)
2875 struct perf_hpp_fmt
*fmt
;
2876 struct hpp_sort_entry
*hse
;
2878 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2879 if (!perf_hpp__is_sort_entry(fmt
))
2882 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
2883 fmt
->elide
= get_elide(hse
->se
->se_width_idx
, output
);
2887 * It makes no sense to elide all of sort entries.
2888 * Just revert them to show up again.
2890 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2891 if (!perf_hpp__is_sort_entry(fmt
))
2898 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2899 if (!perf_hpp__is_sort_entry(fmt
))
2906 int output_field_add(struct perf_hpp_list
*list
, char *tok
)
2910 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++) {
2911 struct sort_dimension
*sd
= &common_sort_dimensions
[i
];
2913 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2916 return __sort_dimension__add_output(list
, sd
);
2919 for (i
= 0; i
< ARRAY_SIZE(hpp_sort_dimensions
); i
++) {
2920 struct hpp_dimension
*hd
= &hpp_sort_dimensions
[i
];
2922 if (strncasecmp(tok
, hd
->name
, strlen(tok
)))
2925 return __hpp_dimension__add_output(list
, hd
);
2928 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++) {
2929 struct sort_dimension
*sd
= &bstack_sort_dimensions
[i
];
2931 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2934 return __sort_dimension__add_output(list
, sd
);
2937 for (i
= 0; i
< ARRAY_SIZE(memory_sort_dimensions
); i
++) {
2938 struct sort_dimension
*sd
= &memory_sort_dimensions
[i
];
2940 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2943 return __sort_dimension__add_output(list
, sd
);
2949 static int setup_output_list(struct perf_hpp_list
*list
, char *str
)
2954 for (tok
= strtok_r(str
, ", ", &tmp
);
2955 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
2956 ret
= output_field_add(list
, tok
);
2957 if (ret
== -EINVAL
) {
2958 ui__error("Invalid --fields key: `%s'", tok
);
2960 } else if (ret
== -ESRCH
) {
2961 ui__error("Unknown --fields key: `%s'", tok
);
2969 void reset_dimensions(void)
2973 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++)
2974 common_sort_dimensions
[i
].taken
= 0;
2976 for (i
= 0; i
< ARRAY_SIZE(hpp_sort_dimensions
); i
++)
2977 hpp_sort_dimensions
[i
].taken
= 0;
2979 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++)
2980 bstack_sort_dimensions
[i
].taken
= 0;
2982 for (i
= 0; i
< ARRAY_SIZE(memory_sort_dimensions
); i
++)
2983 memory_sort_dimensions
[i
].taken
= 0;
2986 bool is_strict_order(const char *order
)
2988 return order
&& (*order
!= '+');
2991 static int __setup_output_field(void)
2996 if (field_order
== NULL
)
2999 strp
= str
= strdup(field_order
);
3001 pr_err("Not enough memory to setup output fields");
3005 if (!is_strict_order(field_order
))
3008 if (!strlen(strp
)) {
3009 pr_err("Invalid --fields key: `+'");
3013 ret
= setup_output_list(&perf_hpp_list
, strp
);
3020 int setup_sorting(struct perf_evlist
*evlist
)
3024 err
= __setup_sorting(evlist
);
3028 if (parent_pattern
!= default_parent_pattern
) {
3029 err
= sort_dimension__add(&perf_hpp_list
, "parent", evlist
, -1);
3037 * perf diff doesn't use default hpp output fields.
3039 if (sort__mode
!= SORT_MODE__DIFF
)
3042 err
= __setup_output_field();
3046 /* copy sort keys to output fields */
3047 perf_hpp__setup_output_field(&perf_hpp_list
);
3048 /* and then copy output fields to sort keys */
3049 perf_hpp__append_sort_keys(&perf_hpp_list
);
3051 /* setup hists-specific output fields */
3052 if (perf_hpp__setup_hists_formats(&perf_hpp_list
, evlist
) < 0)
3058 void reset_output_field(void)
3060 perf_hpp_list
.need_collapse
= 0;
3061 perf_hpp_list
.parent
= 0;
3062 perf_hpp_list
.sym
= 0;
3063 perf_hpp_list
.dso
= 0;
3069 perf_hpp__reset_output_field(&perf_hpp_list
);