8 #include <traceevent/event-parse.h>
9 #include "mem-events.h"
12 const char default_parent_pattern[] = "^sys_|^do_page_fault";
13 const char *parent_pattern = default_parent_pattern;
14 const char default_sort_order[] = "comm,dso,symbol";
15 const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
16 const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
17 const char default_top_sort_order[] = "dso,symbol";
18 const char default_diff_sort_order[] = "dso,symbol";
19 const char default_tracepoint_sort_order[] = "trace";
20 const char *sort_order;
21 const char *field_order;
22 regex_t ignore_callees_regex;
23 int have_ignore_callees = 0;
24 enum sort_mode sort__mode = SORT_MODE__NORMAL;
27 * Replaces all occurrences of a char used with the:
29 * -t, --field-separator
31 * option, that uses a special separator character and don't pad with spaces,
32 * replacing all occurances of this separator in symbol names (and other
33 * output) with a '.' character, that thus it's the only non valid separator.
35 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
41 n = vsnprintf(bf, size, fmt, ap);
42 if (symbol_conf.field_sep && n > 0) {
46 sep = strchr(sep, *symbol_conf.field_sep);
59 static int64_t cmp_null(const void *l, const void *r)
72 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
74 return right->thread->tid - left->thread->tid;
77 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
78 size_t size, unsigned int width)
80 const char *comm = thread__comm_str(he->thread);
82 width = max(7U, width) - 6;
83 return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
84 width, width, comm ?: "");
87 static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
89 const struct thread *th = arg;
91 if (type != HIST_FILTER__THREAD)
94 return th && he->thread != th;
97 struct sort_entry sort_thread = {
98 .se_header = " Pid:Command",
99 .se_cmp = sort__thread_cmp,
100 .se_snprintf = hist_entry__thread_snprintf,
101 .se_filter = hist_entry__thread_filter,
102 .se_width_idx = HISTC_THREAD,
108 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
110 /* Compare the addr that should be unique among comm */
111 return strcmp(comm__str(right->comm), comm__str(left->comm));
115 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
117 /* Compare the addr that should be unique among comm */
118 return strcmp(comm__str(right->comm), comm__str(left->comm));
122 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
124 return strcmp(comm__str(right->comm), comm__str(left->comm));
127 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
128 size_t size, unsigned int width)
130 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
133 struct sort_entry sort_comm = {
134 .se_header = "Command",
135 .se_cmp = sort__comm_cmp,
136 .se_collapse = sort__comm_collapse,
137 .se_sort = sort__comm_sort,
138 .se_snprintf = hist_entry__comm_snprintf,
139 .se_filter = hist_entry__thread_filter,
140 .se_width_idx = HISTC_COMM,
145 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
147 struct dso *dso_l = map_l ? map_l->dso : NULL;
148 struct dso *dso_r = map_r ? map_r->dso : NULL;
149 const char *dso_name_l, *dso_name_r;
151 if (!dso_l || !dso_r)
152 return cmp_null(dso_r, dso_l);
155 dso_name_l = dso_l->long_name;
156 dso_name_r = dso_r->long_name;
158 dso_name_l = dso_l->short_name;
159 dso_name_r = dso_r->short_name;
162 return strcmp(dso_name_l, dso_name_r);
166 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
168 return _sort__dso_cmp(right->ms.map, left->ms.map);
171 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
172 size_t size, unsigned int width)
174 if (map && map->dso) {
175 const char *dso_name = !verbose ? map->dso->short_name :
177 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
180 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
183 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
184 size_t size, unsigned int width)
186 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
189 static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
191 const struct dso *dso = arg;
193 if (type != HIST_FILTER__DSO)
196 return dso && (!he->ms.map || he->ms.map->dso != dso);
199 struct sort_entry sort_dso = {
200 .se_header = "Shared Object",
201 .se_cmp = sort__dso_cmp,
202 .se_snprintf = hist_entry__dso_snprintf,
203 .se_filter = hist_entry__dso_filter,
204 .se_width_idx = HISTC_DSO,
209 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
211 return (int64_t)(right_ip - left_ip);
214 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
216 if (!sym_l || !sym_r)
217 return cmp_null(sym_l, sym_r);
222 if (sym_l->start != sym_r->start)
223 return (int64_t)(sym_r->start - sym_l->start);
225 return (int64_t)(sym_r->end - sym_l->end);
229 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
233 if (!left->ms.sym && !right->ms.sym)
234 return _sort__addr_cmp(left->ip, right->ip);
237 * comparing symbol address alone is not enough since it's a
238 * relative address within a dso.
240 if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
241 ret = sort__dso_cmp(left, right);
246 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
250 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
252 if (!left->ms.sym || !right->ms.sym)
253 return cmp_null(left->ms.sym, right->ms.sym);
255 return strcmp(right->ms.sym->name, left->ms.sym->name);
258 static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
259 u64 ip, char level, char *bf, size_t size,
265 char o = map ? dso__symtab_origin(map->dso) : '!';
266 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
267 BITS_PER_LONG / 4 + 2, ip, o);
270 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
272 if (map->type == MAP__VARIABLE) {
273 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
274 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
275 ip - map->unmap_ip(map, sym->start));
277 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
282 size_t len = BITS_PER_LONG / 4;
283 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
290 static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
291 size_t size, unsigned int width)
293 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
294 he->level, bf, size, width);
297 static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
299 const char *sym = arg;
301 if (type != HIST_FILTER__SYMBOL)
304 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
307 struct sort_entry sort_sym = {
308 .se_header = "Symbol",
309 .se_cmp = sort__sym_cmp,
310 .se_sort = sort__sym_sort,
311 .se_snprintf = hist_entry__sym_snprintf,
312 .se_filter = hist_entry__sym_filter,
313 .se_width_idx = HISTC_SYMBOL,
318 static char *hist_entry__get_srcline(struct hist_entry *he)
320 struct map *map = he->ms.map;
323 return SRCLINE_UNKNOWN;
325 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
330 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
333 left->srcline = hist_entry__get_srcline(left);
335 right->srcline = hist_entry__get_srcline(right);
337 return strcmp(right->srcline, left->srcline);
340 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
341 size_t size, unsigned int width)
344 he->srcline = hist_entry__get_srcline(he);
346 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
349 struct sort_entry sort_srcline = {
350 .se_header = "Source:Line",
351 .se_cmp = sort__srcline_cmp,
352 .se_snprintf = hist_entry__srcline_snprintf,
353 .se_width_idx = HISTC_SRCLINE,
358 static char no_srcfile[1];
360 static char *hist_entry__get_srcfile(struct hist_entry *e)
363 struct map *map = e->ms.map;
368 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
369 e->ms.sym, false, true);
370 if (!strcmp(sf, SRCLINE_UNKNOWN))
382 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
385 left->srcfile = hist_entry__get_srcfile(left);
387 right->srcfile = hist_entry__get_srcfile(right);
389 return strcmp(right->srcfile, left->srcfile);
392 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
393 size_t size, unsigned int width)
396 he->srcfile = hist_entry__get_srcfile(he);
398 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
401 struct sort_entry sort_srcfile = {
402 .se_header = "Source File",
403 .se_cmp = sort__srcfile_cmp,
404 .se_snprintf = hist_entry__srcfile_snprintf,
405 .se_width_idx = HISTC_SRCFILE,
411 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
413 struct symbol *sym_l = left->parent;
414 struct symbol *sym_r = right->parent;
416 if (!sym_l || !sym_r)
417 return cmp_null(sym_l, sym_r);
419 return strcmp(sym_r->name, sym_l->name);
422 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
423 size_t size, unsigned int width)
425 return repsep_snprintf(bf, size, "%-*.*s", width, width,
426 he->parent ? he->parent->name : "[other]");
429 struct sort_entry sort_parent = {
430 .se_header = "Parent symbol",
431 .se_cmp = sort__parent_cmp,
432 .se_snprintf = hist_entry__parent_snprintf,
433 .se_width_idx = HISTC_PARENT,
439 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
441 return right->cpu - left->cpu;
444 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
445 size_t size, unsigned int width)
447 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
450 struct sort_entry sort_cpu = {
452 .se_cmp = sort__cpu_cmp,
453 .se_snprintf = hist_entry__cpu_snprintf,
454 .se_width_idx = HISTC_CPU,
460 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
462 return right->socket - left->socket;
465 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
466 size_t size, unsigned int width)
468 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
471 static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
473 int sk = *(const int *)arg;
475 if (type != HIST_FILTER__SOCKET)
478 return sk >= 0 && he->socket != sk;
481 struct sort_entry sort_socket = {
482 .se_header = "Socket",
483 .se_cmp = sort__socket_cmp,
484 .se_snprintf = hist_entry__socket_snprintf,
485 .se_filter = hist_entry__socket_filter,
486 .se_width_idx = HISTC_SOCKET,
491 static char *get_trace_output(struct hist_entry *he)
493 struct trace_seq seq;
494 struct perf_evsel *evsel;
495 struct pevent_record rec = {
496 .data = he->raw_data,
497 .size = he->raw_size,
500 evsel = hists_to_evsel(he->hists);
502 trace_seq_init(&seq);
503 if (symbol_conf.raw_trace) {
504 pevent_print_fields(&seq, he->raw_data, he->raw_size,
507 pevent_event_info(&seq, evsel->tp_format, &rec);
513 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
515 struct perf_evsel *evsel;
517 evsel = hists_to_evsel(left->hists);
518 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
521 if (left->trace_output == NULL)
522 left->trace_output = get_trace_output(left);
523 if (right->trace_output == NULL)
524 right->trace_output = get_trace_output(right);
526 return strcmp(right->trace_output, left->trace_output);
529 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
530 size_t size, unsigned int width)
532 struct perf_evsel *evsel;
534 evsel = hists_to_evsel(he->hists);
535 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
536 return scnprintf(bf, size, "%-.*s", width, "N/A");
538 if (he->trace_output == NULL)
539 he->trace_output = get_trace_output(he);
540 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
543 struct sort_entry sort_trace = {
544 .se_header = "Trace output",
545 .se_cmp = sort__trace_cmp,
546 .se_snprintf = hist_entry__trace_snprintf,
547 .se_width_idx = HISTC_TRACE,
550 /* sort keys for branch stacks */
553 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
555 if (!left->branch_info || !right->branch_info)
556 return cmp_null(left->branch_info, right->branch_info);
558 return _sort__dso_cmp(left->branch_info->from.map,
559 right->branch_info->from.map);
562 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
563 size_t size, unsigned int width)
566 return _hist_entry__dso_snprintf(he->branch_info->from.map,
569 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
572 static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
575 const struct dso *dso = arg;
577 if (type != HIST_FILTER__DSO)
580 return dso && (!he->branch_info || !he->branch_info->from.map ||
581 he->branch_info->from.map->dso != dso);
585 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
587 if (!left->branch_info || !right->branch_info)
588 return cmp_null(left->branch_info, right->branch_info);
590 return _sort__dso_cmp(left->branch_info->to.map,
591 right->branch_info->to.map);
594 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
595 size_t size, unsigned int width)
598 return _hist_entry__dso_snprintf(he->branch_info->to.map,
601 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
604 static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
607 const struct dso *dso = arg;
609 if (type != HIST_FILTER__DSO)
612 return dso && (!he->branch_info || !he->branch_info->to.map ||
613 he->branch_info->to.map->dso != dso);
617 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
619 struct addr_map_symbol *from_l = &left->branch_info->from;
620 struct addr_map_symbol *from_r = &right->branch_info->from;
622 if (!left->branch_info || !right->branch_info)
623 return cmp_null(left->branch_info, right->branch_info);
625 from_l = &left->branch_info->from;
626 from_r = &right->branch_info->from;
628 if (!from_l->sym && !from_r->sym)
629 return _sort__addr_cmp(from_l->addr, from_r->addr);
631 return _sort__sym_cmp(from_l->sym, from_r->sym);
635 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
637 struct addr_map_symbol *to_l, *to_r;
639 if (!left->branch_info || !right->branch_info)
640 return cmp_null(left->branch_info, right->branch_info);
642 to_l = &left->branch_info->to;
643 to_r = &right->branch_info->to;
645 if (!to_l->sym && !to_r->sym)
646 return _sort__addr_cmp(to_l->addr, to_r->addr);
648 return _sort__sym_cmp(to_l->sym, to_r->sym);
651 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
652 size_t size, unsigned int width)
654 if (he->branch_info) {
655 struct addr_map_symbol *from = &he->branch_info->from;
657 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
658 he->level, bf, size, width);
661 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
664 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
665 size_t size, unsigned int width)
667 if (he->branch_info) {
668 struct addr_map_symbol *to = &he->branch_info->to;
670 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
671 he->level, bf, size, width);
674 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
677 static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
680 const char *sym = arg;
682 if (type != HIST_FILTER__SYMBOL)
685 return sym && !(he->branch_info && he->branch_info->from.sym &&
686 strstr(he->branch_info->from.sym->name, sym));
689 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
692 const char *sym = arg;
694 if (type != HIST_FILTER__SYMBOL)
697 return sym && !(he->branch_info && he->branch_info->to.sym &&
698 strstr(he->branch_info->to.sym->name, sym));
701 struct sort_entry sort_dso_from = {
702 .se_header = "Source Shared Object",
703 .se_cmp = sort__dso_from_cmp,
704 .se_snprintf = hist_entry__dso_from_snprintf,
705 .se_filter = hist_entry__dso_from_filter,
706 .se_width_idx = HISTC_DSO_FROM,
709 struct sort_entry sort_dso_to = {
710 .se_header = "Target Shared Object",
711 .se_cmp = sort__dso_to_cmp,
712 .se_snprintf = hist_entry__dso_to_snprintf,
713 .se_filter = hist_entry__dso_to_filter,
714 .se_width_idx = HISTC_DSO_TO,
717 struct sort_entry sort_sym_from = {
718 .se_header = "Source Symbol",
719 .se_cmp = sort__sym_from_cmp,
720 .se_snprintf = hist_entry__sym_from_snprintf,
721 .se_filter = hist_entry__sym_from_filter,
722 .se_width_idx = HISTC_SYMBOL_FROM,
725 struct sort_entry sort_sym_to = {
726 .se_header = "Target Symbol",
727 .se_cmp = sort__sym_to_cmp,
728 .se_snprintf = hist_entry__sym_to_snprintf,
729 .se_filter = hist_entry__sym_to_filter,
730 .se_width_idx = HISTC_SYMBOL_TO,
734 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
738 if (!left->branch_info || !right->branch_info)
739 return cmp_null(left->branch_info, right->branch_info);
741 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
742 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
746 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
747 size_t size, unsigned int width){
748 static const char *out = "N/A";
750 if (he->branch_info) {
751 if (he->branch_info->flags.predicted)
753 else if (he->branch_info->flags.mispred)
757 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
761 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
763 return left->branch_info->flags.cycles -
764 right->branch_info->flags.cycles;
767 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
768 size_t size, unsigned int width)
770 if (he->branch_info->flags.cycles == 0)
771 return repsep_snprintf(bf, size, "%-*s", width, "-");
772 return repsep_snprintf(bf, size, "%-*hd", width,
773 he->branch_info->flags.cycles);
776 struct sort_entry sort_cycles = {
777 .se_header = "Basic Block Cycles",
778 .se_cmp = sort__cycles_cmp,
779 .se_snprintf = hist_entry__cycles_snprintf,
780 .se_width_idx = HISTC_CYCLES,
783 /* --sort daddr_sym */
785 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
787 uint64_t l = 0, r = 0;
790 l = left->mem_info->daddr.addr;
792 r = right->mem_info->daddr.addr;
794 return (int64_t)(r - l);
797 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
798 size_t size, unsigned int width)
801 struct map *map = NULL;
802 struct symbol *sym = NULL;
805 addr = he->mem_info->daddr.addr;
806 map = he->mem_info->daddr.map;
807 sym = he->mem_info->daddr.sym;
809 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
814 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
816 uint64_t l = 0, r = 0;
819 l = left->mem_info->iaddr.addr;
821 r = right->mem_info->iaddr.addr;
823 return (int64_t)(r - l);
826 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
827 size_t size, unsigned int width)
830 struct map *map = NULL;
831 struct symbol *sym = NULL;
834 addr = he->mem_info->iaddr.addr;
835 map = he->mem_info->iaddr.map;
836 sym = he->mem_info->iaddr.sym;
838 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
843 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
845 struct map *map_l = NULL;
846 struct map *map_r = NULL;
849 map_l = left->mem_info->daddr.map;
851 map_r = right->mem_info->daddr.map;
853 return _sort__dso_cmp(map_l, map_r);
856 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
857 size_t size, unsigned int width)
859 struct map *map = NULL;
862 map = he->mem_info->daddr.map;
864 return _hist_entry__dso_snprintf(map, bf, size, width);
868 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
870 union perf_mem_data_src data_src_l;
871 union perf_mem_data_src data_src_r;
874 data_src_l = left->mem_info->data_src;
876 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
879 data_src_r = right->mem_info->data_src;
881 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
883 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
886 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
887 size_t size, unsigned int width)
891 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
892 return repsep_snprintf(bf, size, "%.*s", width, out);
896 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
898 union perf_mem_data_src data_src_l;
899 union perf_mem_data_src data_src_r;
902 data_src_l = left->mem_info->data_src;
904 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
907 data_src_r = right->mem_info->data_src;
909 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
911 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
914 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
915 size_t size, unsigned int width)
919 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
920 return repsep_snprintf(bf, size, "%-*s", width, out);
924 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
926 union perf_mem_data_src data_src_l;
927 union perf_mem_data_src data_src_r;
930 data_src_l = left->mem_info->data_src;
932 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
935 data_src_r = right->mem_info->data_src;
937 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
939 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
942 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
943 size_t size, unsigned int width)
947 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
948 return repsep_snprintf(bf, size, "%-*s", width, out);
952 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
954 union perf_mem_data_src data_src_l;
955 union perf_mem_data_src data_src_r;
958 data_src_l = left->mem_info->data_src;
960 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
963 data_src_r = right->mem_info->data_src;
965 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
967 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
970 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
971 size_t size, unsigned int width)
975 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
976 return repsep_snprintf(bf, size, "%-*s", width, out);
980 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
983 struct map *l_map, *r_map;
985 if (!left->mem_info) return -1;
986 if (!right->mem_info) return 1;
988 /* group event types together */
989 if (left->cpumode > right->cpumode) return -1;
990 if (left->cpumode < right->cpumode) return 1;
992 l_map = left->mem_info->daddr.map;
993 r_map = right->mem_info->daddr.map;
995 /* if both are NULL, jump to sort on al_addr instead */
996 if (!l_map && !r_map)
999 if (!l_map) return -1;
1000 if (!r_map) return 1;
1002 if (l_map->maj > r_map->maj) return -1;
1003 if (l_map->maj < r_map->maj) return 1;
1005 if (l_map->min > r_map->min) return -1;
1006 if (l_map->min < r_map->min) return 1;
1008 if (l_map->ino > r_map->ino) return -1;
1009 if (l_map->ino < r_map->ino) return 1;
1011 if (l_map->ino_generation > r_map->ino_generation) return -1;
1012 if (l_map->ino_generation < r_map->ino_generation) return 1;
1015 * Addresses with no major/minor numbers are assumed to be
1016 * anonymous in userspace. Sort those on pid then address.
1018 * The kernel and non-zero major/minor mapped areas are
1019 * assumed to be unity mapped. Sort those on address.
1022 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1023 (!(l_map->flags & MAP_SHARED)) &&
1024 !l_map->maj && !l_map->min && !l_map->ino &&
1025 !l_map->ino_generation) {
1026 /* userspace anonymous */
1028 if (left->thread->pid_ > right->thread->pid_) return -1;
1029 if (left->thread->pid_ < right->thread->pid_) return 1;
1033 /* al_addr does all the right addr - start + offset calculations */
1034 l = cl_address(left->mem_info->daddr.al_addr);
1035 r = cl_address(right->mem_info->daddr.al_addr);
1037 if (l > r) return -1;
1038 if (l < r) return 1;
1043 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1044 size_t size, unsigned int width)
1048 struct map *map = NULL;
1049 struct symbol *sym = NULL;
1050 char level = he->level;
1053 addr = cl_address(he->mem_info->daddr.al_addr);
1054 map = he->mem_info->daddr.map;
1055 sym = he->mem_info->daddr.sym;
1057 /* print [s] for shared data mmaps */
1058 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1059 map && (map->type == MAP__VARIABLE) &&
1060 (map->flags & MAP_SHARED) &&
1061 (map->maj || map->min || map->ino ||
1062 map->ino_generation))
1067 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1071 struct sort_entry sort_mispredict = {
1072 .se_header = "Branch Mispredicted",
1073 .se_cmp = sort__mispredict_cmp,
1074 .se_snprintf = hist_entry__mispredict_snprintf,
1075 .se_width_idx = HISTC_MISPREDICT,
1078 static u64 he_weight(struct hist_entry *he)
1080 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1084 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1086 return he_weight(left) - he_weight(right);
1089 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1090 size_t size, unsigned int width)
1092 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1095 struct sort_entry sort_local_weight = {
1096 .se_header = "Local Weight",
1097 .se_cmp = sort__local_weight_cmp,
1098 .se_snprintf = hist_entry__local_weight_snprintf,
1099 .se_width_idx = HISTC_LOCAL_WEIGHT,
1103 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1105 return left->stat.weight - right->stat.weight;
1108 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1109 size_t size, unsigned int width)
1111 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1114 struct sort_entry sort_global_weight = {
1115 .se_header = "Weight",
1116 .se_cmp = sort__global_weight_cmp,
1117 .se_snprintf = hist_entry__global_weight_snprintf,
1118 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1121 struct sort_entry sort_mem_daddr_sym = {
1122 .se_header = "Data Symbol",
1123 .se_cmp = sort__daddr_cmp,
1124 .se_snprintf = hist_entry__daddr_snprintf,
1125 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1128 struct sort_entry sort_mem_iaddr_sym = {
1129 .se_header = "Code Symbol",
1130 .se_cmp = sort__iaddr_cmp,
1131 .se_snprintf = hist_entry__iaddr_snprintf,
1132 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1135 struct sort_entry sort_mem_daddr_dso = {
1136 .se_header = "Data Object",
1137 .se_cmp = sort__dso_daddr_cmp,
1138 .se_snprintf = hist_entry__dso_daddr_snprintf,
1139 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1142 struct sort_entry sort_mem_locked = {
1143 .se_header = "Locked",
1144 .se_cmp = sort__locked_cmp,
1145 .se_snprintf = hist_entry__locked_snprintf,
1146 .se_width_idx = HISTC_MEM_LOCKED,
1149 struct sort_entry sort_mem_tlb = {
1150 .se_header = "TLB access",
1151 .se_cmp = sort__tlb_cmp,
1152 .se_snprintf = hist_entry__tlb_snprintf,
1153 .se_width_idx = HISTC_MEM_TLB,
1156 struct sort_entry sort_mem_lvl = {
1157 .se_header = "Memory access",
1158 .se_cmp = sort__lvl_cmp,
1159 .se_snprintf = hist_entry__lvl_snprintf,
1160 .se_width_idx = HISTC_MEM_LVL,
1163 struct sort_entry sort_mem_snoop = {
1164 .se_header = "Snoop",
1165 .se_cmp = sort__snoop_cmp,
1166 .se_snprintf = hist_entry__snoop_snprintf,
1167 .se_width_idx = HISTC_MEM_SNOOP,
1170 struct sort_entry sort_mem_dcacheline = {
1171 .se_header = "Data Cacheline",
1172 .se_cmp = sort__dcacheline_cmp,
1173 .se_snprintf = hist_entry__dcacheline_snprintf,
1174 .se_width_idx = HISTC_MEM_DCACHELINE,
1178 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1180 if (!left->branch_info || !right->branch_info)
1181 return cmp_null(left->branch_info, right->branch_info);
1183 return left->branch_info->flags.abort !=
1184 right->branch_info->flags.abort;
1187 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1188 size_t size, unsigned int width)
1190 static const char *out = "N/A";
1192 if (he->branch_info) {
1193 if (he->branch_info->flags.abort)
1199 return repsep_snprintf(bf, size, "%-*s", width, out);
1202 struct sort_entry sort_abort = {
1203 .se_header = "Transaction abort",
1204 .se_cmp = sort__abort_cmp,
1205 .se_snprintf = hist_entry__abort_snprintf,
1206 .se_width_idx = HISTC_ABORT,
1210 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1212 if (!left->branch_info || !right->branch_info)
1213 return cmp_null(left->branch_info, right->branch_info);
1215 return left->branch_info->flags.in_tx !=
1216 right->branch_info->flags.in_tx;
1219 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1220 size_t size, unsigned int width)
1222 static const char *out = "N/A";
1224 if (he->branch_info) {
1225 if (he->branch_info->flags.in_tx)
1231 return repsep_snprintf(bf, size, "%-*s", width, out);
1234 struct sort_entry sort_in_tx = {
1235 .se_header = "Branch in transaction",
1236 .se_cmp = sort__in_tx_cmp,
1237 .se_snprintf = hist_entry__in_tx_snprintf,
1238 .se_width_idx = HISTC_IN_TX,
1242 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1244 return left->transaction - right->transaction;
1247 static inline char *add_str(char *p, const char *str)
1250 return p + strlen(str);
1253 static struct txbit {
1258 { PERF_TXN_ELISION, "EL ", 0 },
1259 { PERF_TXN_TRANSACTION, "TX ", 1 },
1260 { PERF_TXN_SYNC, "SYNC ", 1 },
1261 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1262 { PERF_TXN_RETRY, "RETRY ", 0 },
1263 { PERF_TXN_CONFLICT, "CON ", 0 },
1264 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1265 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1269 int hist_entry__transaction_len(void)
1274 for (i = 0; txbits[i].name; i++) {
1275 if (!txbits[i].skip_for_len)
1276 len += strlen(txbits[i].name);
1278 len += 4; /* :XX<space> */
1282 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1283 size_t size, unsigned int width)
1285 u64 t = he->transaction;
1291 for (i = 0; txbits[i].name; i++)
1292 if (txbits[i].flag & t)
1293 p = add_str(p, txbits[i].name);
1294 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1295 p = add_str(p, "NEITHER ");
1296 if (t & PERF_TXN_ABORT_MASK) {
1297 sprintf(p, ":%" PRIx64,
1298 (t & PERF_TXN_ABORT_MASK) >>
1299 PERF_TXN_ABORT_SHIFT);
1303 return repsep_snprintf(bf, size, "%-*s", width, buf);
1306 struct sort_entry sort_transaction = {
1307 .se_header = "Transaction ",
1308 .se_cmp = sort__transaction_cmp,
1309 .se_snprintf = hist_entry__transaction_snprintf,
1310 .se_width_idx = HISTC_TRANSACTION,
1313 struct sort_dimension {
1315 struct sort_entry *entry;
1319 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1321 static struct sort_dimension common_sort_dimensions[] = {
1322 DIM(SORT_PID, "pid", sort_thread),
1323 DIM(SORT_COMM, "comm", sort_comm),
1324 DIM(SORT_DSO, "dso", sort_dso),
1325 DIM(SORT_SYM, "symbol", sort_sym),
1326 DIM(SORT_PARENT, "parent", sort_parent),
1327 DIM(SORT_CPU, "cpu", sort_cpu),
1328 DIM(SORT_SOCKET, "socket", sort_socket),
1329 DIM(SORT_SRCLINE, "srcline", sort_srcline),
1330 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1331 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1332 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1333 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1334 DIM(SORT_TRACE, "trace", sort_trace),
1339 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1341 static struct sort_dimension bstack_sort_dimensions[] = {
1342 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1343 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1344 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1345 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1346 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1347 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1348 DIM(SORT_ABORT, "abort", sort_abort),
1349 DIM(SORT_CYCLES, "cycles", sort_cycles),
1354 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1356 static struct sort_dimension memory_sort_dimensions[] = {
1357 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1358 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1359 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1360 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1361 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1362 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1363 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1364 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1369 struct hpp_dimension {
1371 struct perf_hpp_fmt *fmt;
1375 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1377 static struct hpp_dimension hpp_sort_dimensions[] = {
1378 DIM(PERF_HPP__OVERHEAD, "overhead"),
1379 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1380 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1381 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1382 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1383 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1384 DIM(PERF_HPP__SAMPLES, "sample"),
1385 DIM(PERF_HPP__PERIOD, "period"),
1390 struct hpp_sort_entry {
1391 struct perf_hpp_fmt hpp;
1392 struct sort_entry *se;
1395 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1397 struct hpp_sort_entry *hse;
1399 if (!perf_hpp__is_sort_entry(fmt))
1402 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1403 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1406 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1407 struct perf_evsel *evsel)
1409 struct hpp_sort_entry *hse;
1410 size_t len = fmt->user_len;
1412 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1415 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
1417 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1420 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1421 struct perf_hpp *hpp __maybe_unused,
1422 struct perf_evsel *evsel)
1424 struct hpp_sort_entry *hse;
1425 size_t len = fmt->user_len;
1427 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1430 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
1435 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1436 struct hist_entry *he)
1438 struct hpp_sort_entry *hse;
1439 size_t len = fmt->user_len;
1441 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1444 len = hists__col_len(he->hists, hse->se->se_width_idx);
1446 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1449 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1450 struct hist_entry *a, struct hist_entry *b)
1452 struct hpp_sort_entry *hse;
1454 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1455 return hse->se->se_cmp(a, b);
1458 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1459 struct hist_entry *a, struct hist_entry *b)
1461 struct hpp_sort_entry *hse;
1462 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1464 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1465 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1466 return collapse_fn(a, b);
1469 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1470 struct hist_entry *a, struct hist_entry *b)
1472 struct hpp_sort_entry *hse;
1473 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1475 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1476 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1477 return sort_fn(a, b);
1480 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1482 return format->header == __sort__hpp_header;
1485 #define MK_SORT_ENTRY_CHK(key) \
1486 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1488 struct hpp_sort_entry *hse; \
1490 if (!perf_hpp__is_sort_entry(fmt)) \
1493 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1494 return hse->se == &sort_ ## key ; \
1497 MK_SORT_ENTRY_CHK(trace)
1498 MK_SORT_ENTRY_CHK(srcline)
1499 MK_SORT_ENTRY_CHK(srcfile)
1500 MK_SORT_ENTRY_CHK(thread)
1501 MK_SORT_ENTRY_CHK(comm)
1502 MK_SORT_ENTRY_CHK(dso)
1503 MK_SORT_ENTRY_CHK(sym)
1506 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1508 struct hpp_sort_entry *hse_a;
1509 struct hpp_sort_entry *hse_b;
1511 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1514 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1515 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1517 return hse_a->se == hse_b->se;
1520 static void hse_free(struct perf_hpp_fmt *fmt)
1522 struct hpp_sort_entry *hse;
1524 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1528 static struct hpp_sort_entry *
1529 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
1531 struct hpp_sort_entry *hse;
1533 hse = malloc(sizeof(*hse));
1535 pr_err("Memory allocation failed\n");
1539 hse->se = sd->entry;
1540 hse->hpp.name = sd->entry->se_header;
1541 hse->hpp.header = __sort__hpp_header;
1542 hse->hpp.width = __sort__hpp_width;
1543 hse->hpp.entry = __sort__hpp_entry;
1544 hse->hpp.color = NULL;
1546 hse->hpp.cmp = __sort__hpp_cmp;
1547 hse->hpp.collapse = __sort__hpp_collapse;
1548 hse->hpp.sort = __sort__hpp_sort;
1549 hse->hpp.equal = __sort__hpp_equal;
1550 hse->hpp.free = hse_free;
1552 INIT_LIST_HEAD(&hse->hpp.list);
1553 INIT_LIST_HEAD(&hse->hpp.sort_list);
1554 hse->hpp.elide = false;
1556 hse->hpp.user_len = 0;
1557 hse->hpp.level = level;
1562 static void hpp_free(struct perf_hpp_fmt *fmt)
1567 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1570 struct perf_hpp_fmt *fmt;
1572 fmt = memdup(hd->fmt, sizeof(*fmt));
1574 INIT_LIST_HEAD(&fmt->list);
1575 INIT_LIST_HEAD(&fmt->sort_list);
1576 fmt->free = hpp_free;
1583 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1585 struct perf_hpp_fmt *fmt;
1586 struct hpp_sort_entry *hse;
1590 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1591 if (!perf_hpp__is_sort_entry(fmt))
1594 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1595 if (hse->se->se_filter == NULL)
1599 * hist entry is filtered if any of sort key in the hpp list
1600 * is applied. But it should skip non-matched filter types.
1602 r = hse->se->se_filter(he, type, arg);
1613 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1614 struct perf_hpp_list *list,
1617 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
1622 perf_hpp_list__register_sort_field(list, &hse->hpp);
1626 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1627 struct perf_hpp_list *list)
1629 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
1634 perf_hpp_list__column_register(list, &hse->hpp);
1638 struct hpp_dynamic_entry {
1639 struct perf_hpp_fmt hpp;
1640 struct perf_evsel *evsel;
1641 struct format_field *field;
1642 unsigned dynamic_len;
1646 static int hde_width(struct hpp_dynamic_entry *hde)
1648 if (!hde->hpp.len) {
1649 int len = hde->dynamic_len;
1650 int namelen = strlen(hde->field->name);
1651 int fieldlen = hde->field->size;
1656 if (!(hde->field->flags & FIELD_IS_STRING)) {
1657 /* length for print hex numbers */
1658 fieldlen = hde->field->size * 2 + 2;
1665 return hde->hpp.len;
1668 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1669 struct hist_entry *he)
1672 struct format_field *field = hde->field;
1679 /* parse pretty print result and update max length */
1680 if (!he->trace_output)
1681 he->trace_output = get_trace_output(he);
1683 namelen = strlen(field->name);
1684 str = he->trace_output;
1687 pos = strchr(str, ' ');
1690 pos = str + strlen(str);
1693 if (!strncmp(str, field->name, namelen)) {
1699 if (len > hde->dynamic_len)
1700 hde->dynamic_len = len;
1711 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1712 struct perf_evsel *evsel __maybe_unused)
1714 struct hpp_dynamic_entry *hde;
1715 size_t len = fmt->user_len;
1717 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1720 len = hde_width(hde);
1722 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1725 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1726 struct perf_hpp *hpp __maybe_unused,
1727 struct perf_evsel *evsel __maybe_unused)
1729 struct hpp_dynamic_entry *hde;
1730 size_t len = fmt->user_len;
1732 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1735 len = hde_width(hde);
1740 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1742 struct hpp_dynamic_entry *hde;
1744 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1746 return hists_to_evsel(hists) == hde->evsel;
1749 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1750 struct hist_entry *he)
1752 struct hpp_dynamic_entry *hde;
1753 size_t len = fmt->user_len;
1755 struct format_field *field;
1760 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1763 len = hde_width(hde);
1768 if (!he->trace_output)
1769 he->trace_output = get_trace_output(he);
1772 namelen = strlen(field->name);
1773 str = he->trace_output;
1776 pos = strchr(str, ' ');
1779 pos = str + strlen(str);
1782 if (!strncmp(str, field->name, namelen)) {
1784 str = strndup(str, pos - str);
1787 return scnprintf(hpp->buf, hpp->size,
1788 "%*.*s", len, len, "ERROR");
1799 struct trace_seq seq;
1801 trace_seq_init(&seq);
1802 pevent_print_field(&seq, he->raw_data, hde->field);
1806 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1811 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1812 struct hist_entry *a, struct hist_entry *b)
1814 struct hpp_dynamic_entry *hde;
1815 struct format_field *field;
1816 unsigned offset, size;
1818 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1821 update_dynamic_len(hde, a);
1826 if (field->flags & FIELD_IS_DYNAMIC) {
1827 unsigned long long dyn;
1829 pevent_read_number_field(field, a->raw_data, &dyn);
1830 offset = dyn & 0xffff;
1831 size = (dyn >> 16) & 0xffff;
1833 /* record max width for output */
1834 if (size > hde->dynamic_len)
1835 hde->dynamic_len = size;
1837 offset = field->offset;
1841 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
1844 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
1846 return fmt->cmp == __sort__hde_cmp;
1849 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1851 struct hpp_dynamic_entry *hde_a;
1852 struct hpp_dynamic_entry *hde_b;
1854 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
1857 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
1858 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
1860 return hde_a->field == hde_b->field;
1863 static void hde_free(struct perf_hpp_fmt *fmt)
1865 struct hpp_dynamic_entry *hde;
1867 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1871 static struct hpp_dynamic_entry *
1872 __alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
1875 struct hpp_dynamic_entry *hde;
1877 hde = malloc(sizeof(*hde));
1879 pr_debug("Memory allocation failed\n");
1885 hde->dynamic_len = 0;
1887 hde->hpp.name = field->name;
1888 hde->hpp.header = __sort__hde_header;
1889 hde->hpp.width = __sort__hde_width;
1890 hde->hpp.entry = __sort__hde_entry;
1891 hde->hpp.color = NULL;
1893 hde->hpp.cmp = __sort__hde_cmp;
1894 hde->hpp.collapse = __sort__hde_cmp;
1895 hde->hpp.sort = __sort__hde_cmp;
1896 hde->hpp.equal = __sort__hde_equal;
1897 hde->hpp.free = hde_free;
1899 INIT_LIST_HEAD(&hde->hpp.list);
1900 INIT_LIST_HEAD(&hde->hpp.sort_list);
1901 hde->hpp.elide = false;
1903 hde->hpp.user_len = 0;
1904 hde->hpp.level = level;
1909 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
1911 struct perf_hpp_fmt *new_fmt = NULL;
1913 if (perf_hpp__is_sort_entry(fmt)) {
1914 struct hpp_sort_entry *hse, *new_hse;
1916 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1917 new_hse = memdup(hse, sizeof(*hse));
1919 new_fmt = &new_hse->hpp;
1920 } else if (perf_hpp__is_dynamic_entry(fmt)) {
1921 struct hpp_dynamic_entry *hde, *new_hde;
1923 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1924 new_hde = memdup(hde, sizeof(*hde));
1926 new_fmt = &new_hde->hpp;
1928 new_fmt = memdup(fmt, sizeof(*fmt));
1931 INIT_LIST_HEAD(&new_fmt->list);
1932 INIT_LIST_HEAD(&new_fmt->sort_list);
1937 static int parse_field_name(char *str, char **event, char **field, char **opt)
1939 char *event_name, *field_name, *opt_name;
1942 field_name = strchr(str, '.');
1945 *field_name++ = '\0';
1951 opt_name = strchr(field_name, '/');
1955 *event = event_name;
1956 *field = field_name;
1962 /* find match evsel using a given event name. The event name can be:
1963 * 1. '%' + event index (e.g. '%1' for first event)
1964 * 2. full event name (e.g. sched:sched_switch)
1965 * 3. partial event name (should not contain ':')
1967 static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
1969 struct perf_evsel *evsel = NULL;
1970 struct perf_evsel *pos;
1974 if (event_name[0] == '%') {
1975 int nr = strtol(event_name+1, NULL, 0);
1977 if (nr > evlist->nr_entries)
1980 evsel = perf_evlist__first(evlist);
1982 evsel = perf_evsel__next(evsel);
1987 full_name = !!strchr(event_name, ':');
1988 evlist__for_each(evlist, pos) {
1990 if (full_name && !strcmp(pos->name, event_name))
1993 if (!full_name && strstr(pos->name, event_name)) {
1995 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
1996 event_name, evsel->name, pos->name);
2006 static int __dynamic_dimension__add(struct perf_evsel *evsel,
2007 struct format_field *field,
2008 bool raw_trace, int level)
2010 struct hpp_dynamic_entry *hde;
2012 hde = __alloc_dynamic_entry(evsel, field, level);
2016 hde->raw_trace = raw_trace;
2018 perf_hpp__register_sort_field(&hde->hpp);
2022 static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
2025 struct format_field *field;
2027 field = evsel->tp_format->format.fields;
2029 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2033 field = field->next;
2038 static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2042 struct perf_evsel *evsel;
2044 evlist__for_each(evlist, evsel) {
2045 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2048 ret = add_evsel_fields(evsel, raw_trace, level);
2055 static int add_all_matching_fields(struct perf_evlist *evlist,
2056 char *field_name, bool raw_trace, int level)
2059 struct perf_evsel *evsel;
2060 struct format_field *field;
2062 evlist__for_each(evlist, evsel) {
2063 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2066 field = pevent_find_any_field(evsel->tp_format, field_name);
2070 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2077 static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2080 char *str, *event_name, *field_name, *opt_name;
2081 struct perf_evsel *evsel;
2082 struct format_field *field;
2083 bool raw_trace = symbol_conf.raw_trace;
2093 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2099 if (strcmp(opt_name, "raw")) {
2100 pr_debug("unsupported field option %s\n", opt_name);
2107 if (!strcmp(field_name, "trace_fields")) {
2108 ret = add_all_dynamic_fields(evlist, raw_trace, level);
2112 if (event_name == NULL) {
2113 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2117 evsel = find_evsel(evlist, event_name);
2118 if (evsel == NULL) {
2119 pr_debug("Cannot find event: %s\n", event_name);
2124 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2125 pr_debug("%s is not a tracepoint event\n", event_name);
2130 if (!strcmp(field_name, "*")) {
2131 ret = add_evsel_fields(evsel, raw_trace, level);
2133 field = pevent_find_any_field(evsel->tp_format, field_name);
2134 if (field == NULL) {
2135 pr_debug("Cannot find event field for %s.%s\n",
2136 event_name, field_name);
2140 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2148 static int __sort_dimension__add(struct sort_dimension *sd,
2149 struct perf_hpp_list *list,
2155 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2158 if (sd->entry->se_collapse)
2159 list->need_collapse = 1;
2166 static int __hpp_dimension__add(struct hpp_dimension *hd,
2167 struct perf_hpp_list *list,
2170 struct perf_hpp_fmt *fmt;
2175 fmt = __hpp_dimension__alloc_hpp(hd, level);
2180 perf_hpp_list__register_sort_field(list, fmt);
2184 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2185 struct sort_dimension *sd)
2190 if (__sort_dimension__add_hpp_output(sd, list) < 0)
2197 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2198 struct hpp_dimension *hd)
2200 struct perf_hpp_fmt *fmt;
2205 fmt = __hpp_dimension__alloc_hpp(hd, 0);
2210 perf_hpp_list__column_register(list, fmt);
2214 int hpp_dimension__add_output(unsigned col)
2216 BUG_ON(col >= PERF_HPP__MAX_INDEX);
2217 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2220 static int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2221 struct perf_evlist *evlist,
2226 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2227 struct sort_dimension *sd = &common_sort_dimensions[i];
2229 if (strncasecmp(tok, sd->name, strlen(tok)))
2232 if (sd->entry == &sort_parent) {
2233 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2237 regerror(ret, &parent_regex, err, sizeof(err));
2238 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2242 } else if (sd->entry == &sort_sym) {
2245 * perf diff displays the performance difference amongst
2246 * two or more perf.data files. Those files could come
2247 * from different binaries. So we should not compare
2248 * their ips, but the name of symbol.
2250 if (sort__mode == SORT_MODE__DIFF)
2251 sd->entry->se_collapse = sort__sym_sort;
2253 } else if (sd->entry == &sort_dso) {
2255 } else if (sd->entry == &sort_socket) {
2257 } else if (sd->entry == &sort_thread) {
2259 } else if (sd->entry == &sort_comm) {
2263 return __sort_dimension__add(sd, list, level);
2266 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2267 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2269 if (strncasecmp(tok, hd->name, strlen(tok)))
2272 return __hpp_dimension__add(hd, list, level);
2275 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2276 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2278 if (strncasecmp(tok, sd->name, strlen(tok)))
2281 if (sort__mode != SORT_MODE__BRANCH)
2284 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2287 __sort_dimension__add(sd, list, level);
2291 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2292 struct sort_dimension *sd = &memory_sort_dimensions[i];
2294 if (strncasecmp(tok, sd->name, strlen(tok)))
2297 if (sort__mode != SORT_MODE__MEMORY)
2300 if (sd->entry == &sort_mem_daddr_sym)
2303 __sort_dimension__add(sd, list, level);
2307 if (!add_dynamic_entry(evlist, tok, level))
2313 static int setup_sort_list(struct perf_hpp_list *list, char *str,
2314 struct perf_evlist *evlist)
2320 bool in_group = false;
2324 tmp = strpbrk(str, "{}, ");
2329 next_level = level + 1;
2333 else if (*tmp == '}')
2341 ret = sort_dimension__add(list, tok, evlist, level);
2342 if (ret == -EINVAL) {
2343 error("Invalid --sort key: `%s'", tok);
2345 } else if (ret == -ESRCH) {
2346 error("Unknown --sort key: `%s'", tok);
2357 static const char *get_default_sort_order(struct perf_evlist *evlist)
2359 const char *default_sort_orders[] = {
2361 default_branch_sort_order,
2362 default_mem_sort_order,
2363 default_top_sort_order,
2364 default_diff_sort_order,
2365 default_tracepoint_sort_order,
2367 bool use_trace = true;
2368 struct perf_evsel *evsel;
2370 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2375 evlist__for_each(evlist, evsel) {
2376 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2383 sort__mode = SORT_MODE__TRACEPOINT;
2384 if (symbol_conf.raw_trace)
2385 return "trace_fields";
2388 return default_sort_orders[sort__mode];
2391 static int setup_sort_order(struct perf_evlist *evlist)
2393 char *new_sort_order;
2396 * Append '+'-prefixed sort order to the default sort
2399 if (!sort_order || is_strict_order(sort_order))
2402 if (sort_order[1] == '\0') {
2403 error("Invalid --sort key: `+'");
2408 * We allocate new sort_order string, but we never free it,
2409 * because it's checked over the rest of the code.
2411 if (asprintf(&new_sort_order, "%s,%s",
2412 get_default_sort_order(evlist), sort_order + 1) < 0) {
2413 error("Not enough memory to set up --sort");
2417 sort_order = new_sort_order;
2422 * Adds 'pre,' prefix into 'str' is 'pre' is
2423 * not already part of 'str'.
2425 static char *prefix_if_not_in(const char *pre, char *str)
2429 if (!str || strstr(str, pre))
2432 if (asprintf(&n, "%s,%s", pre, str) < 0)
2439 static char *setup_overhead(char *keys)
2441 if (sort__mode == SORT_MODE__DIFF)
2444 keys = prefix_if_not_in("overhead", keys);
2446 if (symbol_conf.cumulate_callchain)
2447 keys = prefix_if_not_in("overhead_children", keys);
2452 static int __setup_sorting(struct perf_evlist *evlist)
2455 const char *sort_keys;
2458 ret = setup_sort_order(evlist);
2462 sort_keys = sort_order;
2463 if (sort_keys == NULL) {
2464 if (is_strict_order(field_order)) {
2466 * If user specified field order but no sort order,
2467 * we'll honor it and not add default sort orders.
2472 sort_keys = get_default_sort_order(evlist);
2475 str = strdup(sort_keys);
2477 error("Not enough memory to setup sort keys");
2482 * Prepend overhead fields for backward compatibility.
2484 if (!is_strict_order(field_order)) {
2485 str = setup_overhead(str);
2487 error("Not enough memory to setup overhead keys");
2492 ret = setup_sort_list(&perf_hpp_list, str, evlist);
2498 void perf_hpp__set_elide(int idx, bool elide)
2500 struct perf_hpp_fmt *fmt;
2501 struct hpp_sort_entry *hse;
2503 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2504 if (!perf_hpp__is_sort_entry(fmt))
2507 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2508 if (hse->se->se_width_idx == idx) {
2515 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
2517 if (list && strlist__nr_entries(list) == 1) {
2519 fprintf(fp, "# %s: %s\n", list_name,
2520 strlist__entry(list, 0)->s);
2526 static bool get_elide(int idx, FILE *output)
2530 return __get_elide(symbol_conf.sym_list, "symbol", output);
2532 return __get_elide(symbol_conf.dso_list, "dso", output);
2534 return __get_elide(symbol_conf.comm_list, "comm", output);
2539 if (sort__mode != SORT_MODE__BRANCH)
2543 case HISTC_SYMBOL_FROM:
2544 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2545 case HISTC_SYMBOL_TO:
2546 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2547 case HISTC_DSO_FROM:
2548 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2550 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2558 void sort__setup_elide(FILE *output)
2560 struct perf_hpp_fmt *fmt;
2561 struct hpp_sort_entry *hse;
2563 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2564 if (!perf_hpp__is_sort_entry(fmt))
2567 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2568 fmt->elide = get_elide(hse->se->se_width_idx, output);
2572 * It makes no sense to elide all of sort entries.
2573 * Just revert them to show up again.
2575 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2576 if (!perf_hpp__is_sort_entry(fmt))
2583 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2584 if (!perf_hpp__is_sort_entry(fmt))
2591 static int output_field_add(struct perf_hpp_list *list, char *tok)
2595 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2596 struct sort_dimension *sd = &common_sort_dimensions[i];
2598 if (strncasecmp(tok, sd->name, strlen(tok)))
2601 return __sort_dimension__add_output(list, sd);
2604 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2605 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2607 if (strncasecmp(tok, hd->name, strlen(tok)))
2610 return __hpp_dimension__add_output(list, hd);
2613 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2614 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2616 if (strncasecmp(tok, sd->name, strlen(tok)))
2619 return __sort_dimension__add_output(list, sd);
2622 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2623 struct sort_dimension *sd = &memory_sort_dimensions[i];
2625 if (strncasecmp(tok, sd->name, strlen(tok)))
2628 return __sort_dimension__add_output(list, sd);
2634 static int setup_output_list(struct perf_hpp_list *list, char *str)
2639 for (tok = strtok_r(str, ", ", &tmp);
2640 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2641 ret = output_field_add(list, tok);
2642 if (ret == -EINVAL) {
2643 error("Invalid --fields key: `%s'", tok);
2645 } else if (ret == -ESRCH) {
2646 error("Unknown --fields key: `%s'", tok);
2654 static void reset_dimensions(void)
2658 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2659 common_sort_dimensions[i].taken = 0;
2661 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2662 hpp_sort_dimensions[i].taken = 0;
2664 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2665 bstack_sort_dimensions[i].taken = 0;
2667 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2668 memory_sort_dimensions[i].taken = 0;
2671 bool is_strict_order(const char *order)
2673 return order && (*order != '+');
2676 static int __setup_output_field(void)
2681 if (field_order == NULL)
2684 strp = str = strdup(field_order);
2686 error("Not enough memory to setup output fields");
2690 if (!is_strict_order(field_order))
2693 if (!strlen(strp)) {
2694 error("Invalid --fields key: `+'");
2698 ret = setup_output_list(&perf_hpp_list, strp);
2705 int setup_sorting(struct perf_evlist *evlist)
2709 err = __setup_sorting(evlist);
2713 if (parent_pattern != default_parent_pattern) {
2714 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
2722 * perf diff doesn't use default hpp output fields.
2724 if (sort__mode != SORT_MODE__DIFF)
2727 err = __setup_output_field();
2731 /* copy sort keys to output fields */
2732 perf_hpp__setup_output_field(&perf_hpp_list);
2733 /* and then copy output fields to sort keys */
2734 perf_hpp__append_sort_keys(&perf_hpp_list);
2736 /* setup hists-specific output fields */
2737 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2743 void reset_output_field(void)
2745 perf_hpp_list.need_collapse = 0;
2746 perf_hpp_list.parent = 0;
2747 perf_hpp_list.sym = 0;
2748 perf_hpp_list.dso = 0;
2754 perf_hpp__reset_output_field(&perf_hpp_list);