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 int sort__has_thread = 0;
25 int sort__has_comm = 0;
26 enum sort_mode sort__mode = SORT_MODE__NORMAL;
29 * Replaces all occurrences of a char used with the:
31 * -t, --field-separator
33 * option, that uses a special separator character and don't pad with spaces,
34 * replacing all occurances of this separator in symbol names (and other
35 * output) with a '.' character, that thus it's the only non valid separator.
37 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
43 n = vsnprintf(bf, size, fmt, ap);
44 if (symbol_conf.field_sep && n > 0) {
48 sep = strchr(sep, *symbol_conf.field_sep);
61 static int64_t cmp_null(const void *l, const void *r)
74 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
76 return right->thread->tid - left->thread->tid;
79 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
80 size_t size, unsigned int width)
82 const char *comm = thread__comm_str(he->thread);
84 width = max(7U, width) - 6;
85 return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
86 width, width, comm ?: "");
89 static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
91 const struct thread *th = arg;
93 if (type != HIST_FILTER__THREAD)
96 return th && he->thread != th;
99 struct sort_entry sort_thread = {
100 .se_header = " Pid:Command",
101 .se_cmp = sort__thread_cmp,
102 .se_snprintf = hist_entry__thread_snprintf,
103 .se_filter = hist_entry__thread_filter,
104 .se_width_idx = HISTC_THREAD,
110 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
112 /* Compare the addr that should be unique among comm */
113 return strcmp(comm__str(right->comm), comm__str(left->comm));
117 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
119 /* Compare the addr that should be unique among comm */
120 return strcmp(comm__str(right->comm), comm__str(left->comm));
124 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
126 return strcmp(comm__str(right->comm), comm__str(left->comm));
129 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
130 size_t size, unsigned int width)
132 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
135 struct sort_entry sort_comm = {
136 .se_header = "Command",
137 .se_cmp = sort__comm_cmp,
138 .se_collapse = sort__comm_collapse,
139 .se_sort = sort__comm_sort,
140 .se_snprintf = hist_entry__comm_snprintf,
141 .se_filter = hist_entry__thread_filter,
142 .se_width_idx = HISTC_COMM,
147 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
149 struct dso *dso_l = map_l ? map_l->dso : NULL;
150 struct dso *dso_r = map_r ? map_r->dso : NULL;
151 const char *dso_name_l, *dso_name_r;
153 if (!dso_l || !dso_r)
154 return cmp_null(dso_r, dso_l);
157 dso_name_l = dso_l->long_name;
158 dso_name_r = dso_r->long_name;
160 dso_name_l = dso_l->short_name;
161 dso_name_r = dso_r->short_name;
164 return strcmp(dso_name_l, dso_name_r);
168 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
170 return _sort__dso_cmp(right->ms.map, left->ms.map);
173 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
174 size_t size, unsigned int width)
176 if (map && map->dso) {
177 const char *dso_name = !verbose ? map->dso->short_name :
179 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
182 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
185 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
186 size_t size, unsigned int width)
188 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
191 static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
193 const struct dso *dso = arg;
195 if (type != HIST_FILTER__DSO)
198 return dso && (!he->ms.map || he->ms.map->dso != dso);
201 struct sort_entry sort_dso = {
202 .se_header = "Shared Object",
203 .se_cmp = sort__dso_cmp,
204 .se_snprintf = hist_entry__dso_snprintf,
205 .se_filter = hist_entry__dso_filter,
206 .se_width_idx = HISTC_DSO,
211 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
213 return (int64_t)(right_ip - left_ip);
216 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
218 if (!sym_l || !sym_r)
219 return cmp_null(sym_l, sym_r);
224 if (sym_l->start != sym_r->start)
225 return (int64_t)(sym_r->start - sym_l->start);
227 return (int64_t)(sym_r->end - sym_l->end);
231 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
235 if (!left->ms.sym && !right->ms.sym)
236 return _sort__addr_cmp(left->ip, right->ip);
239 * comparing symbol address alone is not enough since it's a
240 * relative address within a dso.
242 if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
243 ret = sort__dso_cmp(left, right);
248 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
252 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
254 if (!left->ms.sym || !right->ms.sym)
255 return cmp_null(left->ms.sym, right->ms.sym);
257 return strcmp(right->ms.sym->name, left->ms.sym->name);
260 static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
261 u64 ip, char level, char *bf, size_t size,
267 char o = map ? dso__symtab_origin(map->dso) : '!';
268 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
269 BITS_PER_LONG / 4 + 2, ip, o);
272 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
274 if (map->type == MAP__VARIABLE) {
275 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
276 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
277 ip - map->unmap_ip(map, sym->start));
279 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
284 size_t len = BITS_PER_LONG / 4;
285 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
292 static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
293 size_t size, unsigned int width)
295 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
296 he->level, bf, size, width);
299 static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
301 const char *sym = arg;
303 if (type != HIST_FILTER__SYMBOL)
306 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
309 struct sort_entry sort_sym = {
310 .se_header = "Symbol",
311 .se_cmp = sort__sym_cmp,
312 .se_sort = sort__sym_sort,
313 .se_snprintf = hist_entry__sym_snprintf,
314 .se_filter = hist_entry__sym_filter,
315 .se_width_idx = HISTC_SYMBOL,
320 static char *hist_entry__get_srcline(struct hist_entry *he)
322 struct map *map = he->ms.map;
325 return SRCLINE_UNKNOWN;
327 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
332 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
335 left->srcline = hist_entry__get_srcline(left);
337 right->srcline = hist_entry__get_srcline(right);
339 return strcmp(right->srcline, left->srcline);
342 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
343 size_t size, unsigned int width)
346 he->srcline = hist_entry__get_srcline(he);
348 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
351 struct sort_entry sort_srcline = {
352 .se_header = "Source:Line",
353 .se_cmp = sort__srcline_cmp,
354 .se_snprintf = hist_entry__srcline_snprintf,
355 .se_width_idx = HISTC_SRCLINE,
360 static char no_srcfile[1];
362 static char *hist_entry__get_srcfile(struct hist_entry *e)
365 struct map *map = e->ms.map;
370 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
371 e->ms.sym, false, true);
372 if (!strcmp(sf, SRCLINE_UNKNOWN))
384 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
387 left->srcfile = hist_entry__get_srcfile(left);
389 right->srcfile = hist_entry__get_srcfile(right);
391 return strcmp(right->srcfile, left->srcfile);
394 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
395 size_t size, unsigned int width)
398 he->srcfile = hist_entry__get_srcfile(he);
400 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
403 struct sort_entry sort_srcfile = {
404 .se_header = "Source File",
405 .se_cmp = sort__srcfile_cmp,
406 .se_snprintf = hist_entry__srcfile_snprintf,
407 .se_width_idx = HISTC_SRCFILE,
413 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
415 struct symbol *sym_l = left->parent;
416 struct symbol *sym_r = right->parent;
418 if (!sym_l || !sym_r)
419 return cmp_null(sym_l, sym_r);
421 return strcmp(sym_r->name, sym_l->name);
424 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
425 size_t size, unsigned int width)
427 return repsep_snprintf(bf, size, "%-*.*s", width, width,
428 he->parent ? he->parent->name : "[other]");
431 struct sort_entry sort_parent = {
432 .se_header = "Parent symbol",
433 .se_cmp = sort__parent_cmp,
434 .se_snprintf = hist_entry__parent_snprintf,
435 .se_width_idx = HISTC_PARENT,
441 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
443 return right->cpu - left->cpu;
446 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
447 size_t size, unsigned int width)
449 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
452 struct sort_entry sort_cpu = {
454 .se_cmp = sort__cpu_cmp,
455 .se_snprintf = hist_entry__cpu_snprintf,
456 .se_width_idx = HISTC_CPU,
462 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
464 return right->socket - left->socket;
467 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
468 size_t size, unsigned int width)
470 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
473 static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
475 int sk = *(const int *)arg;
477 if (type != HIST_FILTER__SOCKET)
480 return sk >= 0 && he->socket != sk;
483 struct sort_entry sort_socket = {
484 .se_header = "Socket",
485 .se_cmp = sort__socket_cmp,
486 .se_snprintf = hist_entry__socket_snprintf,
487 .se_filter = hist_entry__socket_filter,
488 .se_width_idx = HISTC_SOCKET,
493 static char *get_trace_output(struct hist_entry *he)
495 struct trace_seq seq;
496 struct perf_evsel *evsel;
497 struct pevent_record rec = {
498 .data = he->raw_data,
499 .size = he->raw_size,
502 evsel = hists_to_evsel(he->hists);
504 trace_seq_init(&seq);
505 if (symbol_conf.raw_trace) {
506 pevent_print_fields(&seq, he->raw_data, he->raw_size,
509 pevent_event_info(&seq, evsel->tp_format, &rec);
515 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
517 struct perf_evsel *evsel;
519 evsel = hists_to_evsel(left->hists);
520 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
523 if (left->trace_output == NULL)
524 left->trace_output = get_trace_output(left);
525 if (right->trace_output == NULL)
526 right->trace_output = get_trace_output(right);
528 return strcmp(right->trace_output, left->trace_output);
531 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
532 size_t size, unsigned int width)
534 struct perf_evsel *evsel;
536 evsel = hists_to_evsel(he->hists);
537 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
538 return scnprintf(bf, size, "%-.*s", width, "N/A");
540 if (he->trace_output == NULL)
541 he->trace_output = get_trace_output(he);
542 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
545 struct sort_entry sort_trace = {
546 .se_header = "Trace output",
547 .se_cmp = sort__trace_cmp,
548 .se_snprintf = hist_entry__trace_snprintf,
549 .se_width_idx = HISTC_TRACE,
552 /* sort keys for branch stacks */
555 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
557 if (!left->branch_info || !right->branch_info)
558 return cmp_null(left->branch_info, right->branch_info);
560 return _sort__dso_cmp(left->branch_info->from.map,
561 right->branch_info->from.map);
564 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
565 size_t size, unsigned int width)
568 return _hist_entry__dso_snprintf(he->branch_info->from.map,
571 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
574 static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
577 const struct dso *dso = arg;
579 if (type != HIST_FILTER__DSO)
582 return dso && (!he->branch_info || !he->branch_info->from.map ||
583 he->branch_info->from.map->dso != dso);
587 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
589 if (!left->branch_info || !right->branch_info)
590 return cmp_null(left->branch_info, right->branch_info);
592 return _sort__dso_cmp(left->branch_info->to.map,
593 right->branch_info->to.map);
596 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
597 size_t size, unsigned int width)
600 return _hist_entry__dso_snprintf(he->branch_info->to.map,
603 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
606 static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
609 const struct dso *dso = arg;
611 if (type != HIST_FILTER__DSO)
614 return dso && (!he->branch_info || !he->branch_info->to.map ||
615 he->branch_info->to.map->dso != dso);
619 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
621 struct addr_map_symbol *from_l = &left->branch_info->from;
622 struct addr_map_symbol *from_r = &right->branch_info->from;
624 if (!left->branch_info || !right->branch_info)
625 return cmp_null(left->branch_info, right->branch_info);
627 from_l = &left->branch_info->from;
628 from_r = &right->branch_info->from;
630 if (!from_l->sym && !from_r->sym)
631 return _sort__addr_cmp(from_l->addr, from_r->addr);
633 return _sort__sym_cmp(from_l->sym, from_r->sym);
637 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
639 struct addr_map_symbol *to_l, *to_r;
641 if (!left->branch_info || !right->branch_info)
642 return cmp_null(left->branch_info, right->branch_info);
644 to_l = &left->branch_info->to;
645 to_r = &right->branch_info->to;
647 if (!to_l->sym && !to_r->sym)
648 return _sort__addr_cmp(to_l->addr, to_r->addr);
650 return _sort__sym_cmp(to_l->sym, to_r->sym);
653 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
654 size_t size, unsigned int width)
656 if (he->branch_info) {
657 struct addr_map_symbol *from = &he->branch_info->from;
659 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
660 he->level, bf, size, width);
663 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
666 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
667 size_t size, unsigned int width)
669 if (he->branch_info) {
670 struct addr_map_symbol *to = &he->branch_info->to;
672 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
673 he->level, bf, size, width);
676 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
679 static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
682 const char *sym = arg;
684 if (type != HIST_FILTER__SYMBOL)
687 return sym && !(he->branch_info && he->branch_info->from.sym &&
688 strstr(he->branch_info->from.sym->name, sym));
691 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
694 const char *sym = arg;
696 if (type != HIST_FILTER__SYMBOL)
699 return sym && !(he->branch_info && he->branch_info->to.sym &&
700 strstr(he->branch_info->to.sym->name, sym));
703 struct sort_entry sort_dso_from = {
704 .se_header = "Source Shared Object",
705 .se_cmp = sort__dso_from_cmp,
706 .se_snprintf = hist_entry__dso_from_snprintf,
707 .se_filter = hist_entry__dso_from_filter,
708 .se_width_idx = HISTC_DSO_FROM,
711 struct sort_entry sort_dso_to = {
712 .se_header = "Target Shared Object",
713 .se_cmp = sort__dso_to_cmp,
714 .se_snprintf = hist_entry__dso_to_snprintf,
715 .se_filter = hist_entry__dso_to_filter,
716 .se_width_idx = HISTC_DSO_TO,
719 struct sort_entry sort_sym_from = {
720 .se_header = "Source Symbol",
721 .se_cmp = sort__sym_from_cmp,
722 .se_snprintf = hist_entry__sym_from_snprintf,
723 .se_filter = hist_entry__sym_from_filter,
724 .se_width_idx = HISTC_SYMBOL_FROM,
727 struct sort_entry sort_sym_to = {
728 .se_header = "Target Symbol",
729 .se_cmp = sort__sym_to_cmp,
730 .se_snprintf = hist_entry__sym_to_snprintf,
731 .se_filter = hist_entry__sym_to_filter,
732 .se_width_idx = HISTC_SYMBOL_TO,
736 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
740 if (!left->branch_info || !right->branch_info)
741 return cmp_null(left->branch_info, right->branch_info);
743 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
744 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
748 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
749 size_t size, unsigned int width){
750 static const char *out = "N/A";
752 if (he->branch_info) {
753 if (he->branch_info->flags.predicted)
755 else if (he->branch_info->flags.mispred)
759 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
763 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
765 return left->branch_info->flags.cycles -
766 right->branch_info->flags.cycles;
769 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
770 size_t size, unsigned int width)
772 if (he->branch_info->flags.cycles == 0)
773 return repsep_snprintf(bf, size, "%-*s", width, "-");
774 return repsep_snprintf(bf, size, "%-*hd", width,
775 he->branch_info->flags.cycles);
778 struct sort_entry sort_cycles = {
779 .se_header = "Basic Block Cycles",
780 .se_cmp = sort__cycles_cmp,
781 .se_snprintf = hist_entry__cycles_snprintf,
782 .se_width_idx = HISTC_CYCLES,
785 /* --sort daddr_sym */
787 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
789 uint64_t l = 0, r = 0;
792 l = left->mem_info->daddr.addr;
794 r = right->mem_info->daddr.addr;
796 return (int64_t)(r - l);
799 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
800 size_t size, unsigned int width)
803 struct map *map = NULL;
804 struct symbol *sym = NULL;
807 addr = he->mem_info->daddr.addr;
808 map = he->mem_info->daddr.map;
809 sym = he->mem_info->daddr.sym;
811 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
816 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
818 uint64_t l = 0, r = 0;
821 l = left->mem_info->iaddr.addr;
823 r = right->mem_info->iaddr.addr;
825 return (int64_t)(r - l);
828 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
829 size_t size, unsigned int width)
832 struct map *map = NULL;
833 struct symbol *sym = NULL;
836 addr = he->mem_info->iaddr.addr;
837 map = he->mem_info->iaddr.map;
838 sym = he->mem_info->iaddr.sym;
840 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
845 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
847 struct map *map_l = NULL;
848 struct map *map_r = NULL;
851 map_l = left->mem_info->daddr.map;
853 map_r = right->mem_info->daddr.map;
855 return _sort__dso_cmp(map_l, map_r);
858 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
859 size_t size, unsigned int width)
861 struct map *map = NULL;
864 map = he->mem_info->daddr.map;
866 return _hist_entry__dso_snprintf(map, bf, size, width);
870 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
872 union perf_mem_data_src data_src_l;
873 union perf_mem_data_src data_src_r;
876 data_src_l = left->mem_info->data_src;
878 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
881 data_src_r = right->mem_info->data_src;
883 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
885 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
888 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
889 size_t size, unsigned int width)
893 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
894 return repsep_snprintf(bf, size, "%.*s", width, out);
898 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
900 union perf_mem_data_src data_src_l;
901 union perf_mem_data_src data_src_r;
904 data_src_l = left->mem_info->data_src;
906 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
909 data_src_r = right->mem_info->data_src;
911 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
913 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
916 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
917 size_t size, unsigned int width)
921 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
922 return repsep_snprintf(bf, size, "%-*s", width, out);
926 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
928 union perf_mem_data_src data_src_l;
929 union perf_mem_data_src data_src_r;
932 data_src_l = left->mem_info->data_src;
934 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
937 data_src_r = right->mem_info->data_src;
939 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
941 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
944 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
945 size_t size, unsigned int width)
949 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
950 return repsep_snprintf(bf, size, "%-*s", width, out);
954 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
956 union perf_mem_data_src data_src_l;
957 union perf_mem_data_src data_src_r;
960 data_src_l = left->mem_info->data_src;
962 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
965 data_src_r = right->mem_info->data_src;
967 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
969 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
972 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
973 size_t size, unsigned int width)
977 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
978 return repsep_snprintf(bf, size, "%-*s", width, out);
982 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
985 struct map *l_map, *r_map;
987 if (!left->mem_info) return -1;
988 if (!right->mem_info) return 1;
990 /* group event types together */
991 if (left->cpumode > right->cpumode) return -1;
992 if (left->cpumode < right->cpumode) return 1;
994 l_map = left->mem_info->daddr.map;
995 r_map = right->mem_info->daddr.map;
997 /* if both are NULL, jump to sort on al_addr instead */
998 if (!l_map && !r_map)
1001 if (!l_map) return -1;
1002 if (!r_map) return 1;
1004 if (l_map->maj > r_map->maj) return -1;
1005 if (l_map->maj < r_map->maj) return 1;
1007 if (l_map->min > r_map->min) return -1;
1008 if (l_map->min < r_map->min) return 1;
1010 if (l_map->ino > r_map->ino) return -1;
1011 if (l_map->ino < r_map->ino) return 1;
1013 if (l_map->ino_generation > r_map->ino_generation) return -1;
1014 if (l_map->ino_generation < r_map->ino_generation) return 1;
1017 * Addresses with no major/minor numbers are assumed to be
1018 * anonymous in userspace. Sort those on pid then address.
1020 * The kernel and non-zero major/minor mapped areas are
1021 * assumed to be unity mapped. Sort those on address.
1024 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1025 (!(l_map->flags & MAP_SHARED)) &&
1026 !l_map->maj && !l_map->min && !l_map->ino &&
1027 !l_map->ino_generation) {
1028 /* userspace anonymous */
1030 if (left->thread->pid_ > right->thread->pid_) return -1;
1031 if (left->thread->pid_ < right->thread->pid_) return 1;
1035 /* al_addr does all the right addr - start + offset calculations */
1036 l = cl_address(left->mem_info->daddr.al_addr);
1037 r = cl_address(right->mem_info->daddr.al_addr);
1039 if (l > r) return -1;
1040 if (l < r) return 1;
1045 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1046 size_t size, unsigned int width)
1050 struct map *map = NULL;
1051 struct symbol *sym = NULL;
1052 char level = he->level;
1055 addr = cl_address(he->mem_info->daddr.al_addr);
1056 map = he->mem_info->daddr.map;
1057 sym = he->mem_info->daddr.sym;
1059 /* print [s] for shared data mmaps */
1060 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1061 map && (map->type == MAP__VARIABLE) &&
1062 (map->flags & MAP_SHARED) &&
1063 (map->maj || map->min || map->ino ||
1064 map->ino_generation))
1069 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1073 struct sort_entry sort_mispredict = {
1074 .se_header = "Branch Mispredicted",
1075 .se_cmp = sort__mispredict_cmp,
1076 .se_snprintf = hist_entry__mispredict_snprintf,
1077 .se_width_idx = HISTC_MISPREDICT,
1080 static u64 he_weight(struct hist_entry *he)
1082 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1086 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1088 return he_weight(left) - he_weight(right);
1091 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1092 size_t size, unsigned int width)
1094 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1097 struct sort_entry sort_local_weight = {
1098 .se_header = "Local Weight",
1099 .se_cmp = sort__local_weight_cmp,
1100 .se_snprintf = hist_entry__local_weight_snprintf,
1101 .se_width_idx = HISTC_LOCAL_WEIGHT,
1105 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1107 return left->stat.weight - right->stat.weight;
1110 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1111 size_t size, unsigned int width)
1113 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1116 struct sort_entry sort_global_weight = {
1117 .se_header = "Weight",
1118 .se_cmp = sort__global_weight_cmp,
1119 .se_snprintf = hist_entry__global_weight_snprintf,
1120 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1123 struct sort_entry sort_mem_daddr_sym = {
1124 .se_header = "Data Symbol",
1125 .se_cmp = sort__daddr_cmp,
1126 .se_snprintf = hist_entry__daddr_snprintf,
1127 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1130 struct sort_entry sort_mem_iaddr_sym = {
1131 .se_header = "Code Symbol",
1132 .se_cmp = sort__iaddr_cmp,
1133 .se_snprintf = hist_entry__iaddr_snprintf,
1134 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1137 struct sort_entry sort_mem_daddr_dso = {
1138 .se_header = "Data Object",
1139 .se_cmp = sort__dso_daddr_cmp,
1140 .se_snprintf = hist_entry__dso_daddr_snprintf,
1141 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1144 struct sort_entry sort_mem_locked = {
1145 .se_header = "Locked",
1146 .se_cmp = sort__locked_cmp,
1147 .se_snprintf = hist_entry__locked_snprintf,
1148 .se_width_idx = HISTC_MEM_LOCKED,
1151 struct sort_entry sort_mem_tlb = {
1152 .se_header = "TLB access",
1153 .se_cmp = sort__tlb_cmp,
1154 .se_snprintf = hist_entry__tlb_snprintf,
1155 .se_width_idx = HISTC_MEM_TLB,
1158 struct sort_entry sort_mem_lvl = {
1159 .se_header = "Memory access",
1160 .se_cmp = sort__lvl_cmp,
1161 .se_snprintf = hist_entry__lvl_snprintf,
1162 .se_width_idx = HISTC_MEM_LVL,
1165 struct sort_entry sort_mem_snoop = {
1166 .se_header = "Snoop",
1167 .se_cmp = sort__snoop_cmp,
1168 .se_snprintf = hist_entry__snoop_snprintf,
1169 .se_width_idx = HISTC_MEM_SNOOP,
1172 struct sort_entry sort_mem_dcacheline = {
1173 .se_header = "Data Cacheline",
1174 .se_cmp = sort__dcacheline_cmp,
1175 .se_snprintf = hist_entry__dcacheline_snprintf,
1176 .se_width_idx = HISTC_MEM_DCACHELINE,
1180 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1182 if (!left->branch_info || !right->branch_info)
1183 return cmp_null(left->branch_info, right->branch_info);
1185 return left->branch_info->flags.abort !=
1186 right->branch_info->flags.abort;
1189 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1190 size_t size, unsigned int width)
1192 static const char *out = "N/A";
1194 if (he->branch_info) {
1195 if (he->branch_info->flags.abort)
1201 return repsep_snprintf(bf, size, "%-*s", width, out);
1204 struct sort_entry sort_abort = {
1205 .se_header = "Transaction abort",
1206 .se_cmp = sort__abort_cmp,
1207 .se_snprintf = hist_entry__abort_snprintf,
1208 .se_width_idx = HISTC_ABORT,
1212 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1214 if (!left->branch_info || !right->branch_info)
1215 return cmp_null(left->branch_info, right->branch_info);
1217 return left->branch_info->flags.in_tx !=
1218 right->branch_info->flags.in_tx;
1221 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1222 size_t size, unsigned int width)
1224 static const char *out = "N/A";
1226 if (he->branch_info) {
1227 if (he->branch_info->flags.in_tx)
1233 return repsep_snprintf(bf, size, "%-*s", width, out);
1236 struct sort_entry sort_in_tx = {
1237 .se_header = "Branch in transaction",
1238 .se_cmp = sort__in_tx_cmp,
1239 .se_snprintf = hist_entry__in_tx_snprintf,
1240 .se_width_idx = HISTC_IN_TX,
1244 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1246 return left->transaction - right->transaction;
1249 static inline char *add_str(char *p, const char *str)
1252 return p + strlen(str);
1255 static struct txbit {
1260 { PERF_TXN_ELISION, "EL ", 0 },
1261 { PERF_TXN_TRANSACTION, "TX ", 1 },
1262 { PERF_TXN_SYNC, "SYNC ", 1 },
1263 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1264 { PERF_TXN_RETRY, "RETRY ", 0 },
1265 { PERF_TXN_CONFLICT, "CON ", 0 },
1266 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1267 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1271 int hist_entry__transaction_len(void)
1276 for (i = 0; txbits[i].name; i++) {
1277 if (!txbits[i].skip_for_len)
1278 len += strlen(txbits[i].name);
1280 len += 4; /* :XX<space> */
1284 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1285 size_t size, unsigned int width)
1287 u64 t = he->transaction;
1293 for (i = 0; txbits[i].name; i++)
1294 if (txbits[i].flag & t)
1295 p = add_str(p, txbits[i].name);
1296 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1297 p = add_str(p, "NEITHER ");
1298 if (t & PERF_TXN_ABORT_MASK) {
1299 sprintf(p, ":%" PRIx64,
1300 (t & PERF_TXN_ABORT_MASK) >>
1301 PERF_TXN_ABORT_SHIFT);
1305 return repsep_snprintf(bf, size, "%-*s", width, buf);
1308 struct sort_entry sort_transaction = {
1309 .se_header = "Transaction ",
1310 .se_cmp = sort__transaction_cmp,
1311 .se_snprintf = hist_entry__transaction_snprintf,
1312 .se_width_idx = HISTC_TRANSACTION,
1315 struct sort_dimension {
1317 struct sort_entry *entry;
1321 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1323 static struct sort_dimension common_sort_dimensions[] = {
1324 DIM(SORT_PID, "pid", sort_thread),
1325 DIM(SORT_COMM, "comm", sort_comm),
1326 DIM(SORT_DSO, "dso", sort_dso),
1327 DIM(SORT_SYM, "symbol", sort_sym),
1328 DIM(SORT_PARENT, "parent", sort_parent),
1329 DIM(SORT_CPU, "cpu", sort_cpu),
1330 DIM(SORT_SOCKET, "socket", sort_socket),
1331 DIM(SORT_SRCLINE, "srcline", sort_srcline),
1332 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1333 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1334 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1335 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1336 DIM(SORT_TRACE, "trace", sort_trace),
1341 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1343 static struct sort_dimension bstack_sort_dimensions[] = {
1344 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1345 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1346 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1347 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1348 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1349 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1350 DIM(SORT_ABORT, "abort", sort_abort),
1351 DIM(SORT_CYCLES, "cycles", sort_cycles),
1356 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1358 static struct sort_dimension memory_sort_dimensions[] = {
1359 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1360 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1361 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1362 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1363 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1364 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1365 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1366 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1371 struct hpp_dimension {
1373 struct perf_hpp_fmt *fmt;
1377 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1379 static struct hpp_dimension hpp_sort_dimensions[] = {
1380 DIM(PERF_HPP__OVERHEAD, "overhead"),
1381 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1382 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1383 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1384 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1385 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1386 DIM(PERF_HPP__SAMPLES, "sample"),
1387 DIM(PERF_HPP__PERIOD, "period"),
1392 struct hpp_sort_entry {
1393 struct perf_hpp_fmt hpp;
1394 struct sort_entry *se;
1397 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1399 struct hpp_sort_entry *hse;
1401 if (!perf_hpp__is_sort_entry(fmt))
1404 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1405 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1408 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1409 struct perf_evsel *evsel)
1411 struct hpp_sort_entry *hse;
1412 size_t len = fmt->user_len;
1414 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1417 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
1419 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1422 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1423 struct perf_hpp *hpp __maybe_unused,
1424 struct perf_evsel *evsel)
1426 struct hpp_sort_entry *hse;
1427 size_t len = fmt->user_len;
1429 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1432 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
1437 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1438 struct hist_entry *he)
1440 struct hpp_sort_entry *hse;
1441 size_t len = fmt->user_len;
1443 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1446 len = hists__col_len(he->hists, hse->se->se_width_idx);
1448 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1451 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1452 struct hist_entry *a, struct hist_entry *b)
1454 struct hpp_sort_entry *hse;
1456 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1457 return hse->se->se_cmp(a, b);
1460 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1461 struct hist_entry *a, struct hist_entry *b)
1463 struct hpp_sort_entry *hse;
1464 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1466 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1467 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1468 return collapse_fn(a, b);
1471 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1472 struct hist_entry *a, struct hist_entry *b)
1474 struct hpp_sort_entry *hse;
1475 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1477 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1478 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1479 return sort_fn(a, b);
1482 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1484 return format->header == __sort__hpp_header;
1487 #define MK_SORT_ENTRY_CHK(key) \
1488 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1490 struct hpp_sort_entry *hse; \
1492 if (!perf_hpp__is_sort_entry(fmt)) \
1495 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1496 return hse->se == &sort_ ## key ; \
1499 MK_SORT_ENTRY_CHK(trace)
1500 MK_SORT_ENTRY_CHK(srcline)
1501 MK_SORT_ENTRY_CHK(srcfile)
1502 MK_SORT_ENTRY_CHK(thread)
1503 MK_SORT_ENTRY_CHK(comm)
1504 MK_SORT_ENTRY_CHK(dso)
1505 MK_SORT_ENTRY_CHK(sym)
1508 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1510 struct hpp_sort_entry *hse_a;
1511 struct hpp_sort_entry *hse_b;
1513 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1516 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1517 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1519 return hse_a->se == hse_b->se;
1522 static void hse_free(struct perf_hpp_fmt *fmt)
1524 struct hpp_sort_entry *hse;
1526 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1530 static struct hpp_sort_entry *
1531 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
1533 struct hpp_sort_entry *hse;
1535 hse = malloc(sizeof(*hse));
1537 pr_err("Memory allocation failed\n");
1541 hse->se = sd->entry;
1542 hse->hpp.name = sd->entry->se_header;
1543 hse->hpp.header = __sort__hpp_header;
1544 hse->hpp.width = __sort__hpp_width;
1545 hse->hpp.entry = __sort__hpp_entry;
1546 hse->hpp.color = NULL;
1548 hse->hpp.cmp = __sort__hpp_cmp;
1549 hse->hpp.collapse = __sort__hpp_collapse;
1550 hse->hpp.sort = __sort__hpp_sort;
1551 hse->hpp.equal = __sort__hpp_equal;
1552 hse->hpp.free = hse_free;
1554 INIT_LIST_HEAD(&hse->hpp.list);
1555 INIT_LIST_HEAD(&hse->hpp.sort_list);
1556 hse->hpp.elide = false;
1558 hse->hpp.user_len = 0;
1559 hse->hpp.level = level;
1564 static void hpp_free(struct perf_hpp_fmt *fmt)
1569 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1572 struct perf_hpp_fmt *fmt;
1574 fmt = memdup(hd->fmt, sizeof(*fmt));
1576 INIT_LIST_HEAD(&fmt->list);
1577 INIT_LIST_HEAD(&fmt->sort_list);
1578 fmt->free = hpp_free;
1585 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1587 struct perf_hpp_fmt *fmt;
1588 struct hpp_sort_entry *hse;
1592 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1593 if (!perf_hpp__is_sort_entry(fmt))
1596 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1597 if (hse->se->se_filter == NULL)
1601 * hist entry is filtered if any of sort key in the hpp list
1602 * is applied. But it should skip non-matched filter types.
1604 r = hse->se->se_filter(he, type, arg);
1615 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1616 struct perf_hpp_list *list,
1619 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
1624 perf_hpp_list__register_sort_field(list, &hse->hpp);
1628 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1629 struct perf_hpp_list *list)
1631 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
1636 perf_hpp_list__column_register(list, &hse->hpp);
1640 struct hpp_dynamic_entry {
1641 struct perf_hpp_fmt hpp;
1642 struct perf_evsel *evsel;
1643 struct format_field *field;
1644 unsigned dynamic_len;
1648 static int hde_width(struct hpp_dynamic_entry *hde)
1650 if (!hde->hpp.len) {
1651 int len = hde->dynamic_len;
1652 int namelen = strlen(hde->field->name);
1653 int fieldlen = hde->field->size;
1658 if (!(hde->field->flags & FIELD_IS_STRING)) {
1659 /* length for print hex numbers */
1660 fieldlen = hde->field->size * 2 + 2;
1667 return hde->hpp.len;
1670 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1671 struct hist_entry *he)
1674 struct format_field *field = hde->field;
1681 /* parse pretty print result and update max length */
1682 if (!he->trace_output)
1683 he->trace_output = get_trace_output(he);
1685 namelen = strlen(field->name);
1686 str = he->trace_output;
1689 pos = strchr(str, ' ');
1692 pos = str + strlen(str);
1695 if (!strncmp(str, field->name, namelen)) {
1701 if (len > hde->dynamic_len)
1702 hde->dynamic_len = len;
1713 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1714 struct perf_evsel *evsel __maybe_unused)
1716 struct hpp_dynamic_entry *hde;
1717 size_t len = fmt->user_len;
1719 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1722 len = hde_width(hde);
1724 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1727 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1728 struct perf_hpp *hpp __maybe_unused,
1729 struct perf_evsel *evsel __maybe_unused)
1731 struct hpp_dynamic_entry *hde;
1732 size_t len = fmt->user_len;
1734 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1737 len = hde_width(hde);
1742 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1744 struct hpp_dynamic_entry *hde;
1746 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1748 return hists_to_evsel(hists) == hde->evsel;
1751 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1752 struct hist_entry *he)
1754 struct hpp_dynamic_entry *hde;
1755 size_t len = fmt->user_len;
1757 struct format_field *field;
1762 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1765 len = hde_width(hde);
1770 if (!he->trace_output)
1771 he->trace_output = get_trace_output(he);
1774 namelen = strlen(field->name);
1775 str = he->trace_output;
1778 pos = strchr(str, ' ');
1781 pos = str + strlen(str);
1784 if (!strncmp(str, field->name, namelen)) {
1786 str = strndup(str, pos - str);
1789 return scnprintf(hpp->buf, hpp->size,
1790 "%*.*s", len, len, "ERROR");
1801 struct trace_seq seq;
1803 trace_seq_init(&seq);
1804 pevent_print_field(&seq, he->raw_data, hde->field);
1808 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1813 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1814 struct hist_entry *a, struct hist_entry *b)
1816 struct hpp_dynamic_entry *hde;
1817 struct format_field *field;
1818 unsigned offset, size;
1820 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1823 update_dynamic_len(hde, a);
1828 if (field->flags & FIELD_IS_DYNAMIC) {
1829 unsigned long long dyn;
1831 pevent_read_number_field(field, a->raw_data, &dyn);
1832 offset = dyn & 0xffff;
1833 size = (dyn >> 16) & 0xffff;
1835 /* record max width for output */
1836 if (size > hde->dynamic_len)
1837 hde->dynamic_len = size;
1839 offset = field->offset;
1843 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
1846 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
1848 return fmt->cmp == __sort__hde_cmp;
1851 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1853 struct hpp_dynamic_entry *hde_a;
1854 struct hpp_dynamic_entry *hde_b;
1856 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
1859 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
1860 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
1862 return hde_a->field == hde_b->field;
1865 static void hde_free(struct perf_hpp_fmt *fmt)
1867 struct hpp_dynamic_entry *hde;
1869 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1873 static struct hpp_dynamic_entry *
1874 __alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
1877 struct hpp_dynamic_entry *hde;
1879 hde = malloc(sizeof(*hde));
1881 pr_debug("Memory allocation failed\n");
1887 hde->dynamic_len = 0;
1889 hde->hpp.name = field->name;
1890 hde->hpp.header = __sort__hde_header;
1891 hde->hpp.width = __sort__hde_width;
1892 hde->hpp.entry = __sort__hde_entry;
1893 hde->hpp.color = NULL;
1895 hde->hpp.cmp = __sort__hde_cmp;
1896 hde->hpp.collapse = __sort__hde_cmp;
1897 hde->hpp.sort = __sort__hde_cmp;
1898 hde->hpp.equal = __sort__hde_equal;
1899 hde->hpp.free = hde_free;
1901 INIT_LIST_HEAD(&hde->hpp.list);
1902 INIT_LIST_HEAD(&hde->hpp.sort_list);
1903 hde->hpp.elide = false;
1905 hde->hpp.user_len = 0;
1906 hde->hpp.level = level;
1911 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
1913 struct perf_hpp_fmt *new_fmt = NULL;
1915 if (perf_hpp__is_sort_entry(fmt)) {
1916 struct hpp_sort_entry *hse, *new_hse;
1918 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1919 new_hse = memdup(hse, sizeof(*hse));
1921 new_fmt = &new_hse->hpp;
1922 } else if (perf_hpp__is_dynamic_entry(fmt)) {
1923 struct hpp_dynamic_entry *hde, *new_hde;
1925 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1926 new_hde = memdup(hde, sizeof(*hde));
1928 new_fmt = &new_hde->hpp;
1930 new_fmt = memdup(fmt, sizeof(*fmt));
1933 INIT_LIST_HEAD(&new_fmt->list);
1934 INIT_LIST_HEAD(&new_fmt->sort_list);
1939 static int parse_field_name(char *str, char **event, char **field, char **opt)
1941 char *event_name, *field_name, *opt_name;
1944 field_name = strchr(str, '.');
1947 *field_name++ = '\0';
1953 opt_name = strchr(field_name, '/');
1957 *event = event_name;
1958 *field = field_name;
1964 /* find match evsel using a given event name. The event name can be:
1965 * 1. '%' + event index (e.g. '%1' for first event)
1966 * 2. full event name (e.g. sched:sched_switch)
1967 * 3. partial event name (should not contain ':')
1969 static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
1971 struct perf_evsel *evsel = NULL;
1972 struct perf_evsel *pos;
1976 if (event_name[0] == '%') {
1977 int nr = strtol(event_name+1, NULL, 0);
1979 if (nr > evlist->nr_entries)
1982 evsel = perf_evlist__first(evlist);
1984 evsel = perf_evsel__next(evsel);
1989 full_name = !!strchr(event_name, ':');
1990 evlist__for_each(evlist, pos) {
1992 if (full_name && !strcmp(pos->name, event_name))
1995 if (!full_name && strstr(pos->name, event_name)) {
1997 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
1998 event_name, evsel->name, pos->name);
2008 static int __dynamic_dimension__add(struct perf_evsel *evsel,
2009 struct format_field *field,
2010 bool raw_trace, int level)
2012 struct hpp_dynamic_entry *hde;
2014 hde = __alloc_dynamic_entry(evsel, field, level);
2018 hde->raw_trace = raw_trace;
2020 perf_hpp__register_sort_field(&hde->hpp);
2024 static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
2027 struct format_field *field;
2029 field = evsel->tp_format->format.fields;
2031 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2035 field = field->next;
2040 static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2044 struct perf_evsel *evsel;
2046 evlist__for_each(evlist, evsel) {
2047 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2050 ret = add_evsel_fields(evsel, raw_trace, level);
2057 static int add_all_matching_fields(struct perf_evlist *evlist,
2058 char *field_name, bool raw_trace, int level)
2061 struct perf_evsel *evsel;
2062 struct format_field *field;
2064 evlist__for_each(evlist, evsel) {
2065 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2068 field = pevent_find_any_field(evsel->tp_format, field_name);
2072 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2079 static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2082 char *str, *event_name, *field_name, *opt_name;
2083 struct perf_evsel *evsel;
2084 struct format_field *field;
2085 bool raw_trace = symbol_conf.raw_trace;
2095 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2101 if (strcmp(opt_name, "raw")) {
2102 pr_debug("unsupported field option %s\n", opt_name);
2109 if (!strcmp(field_name, "trace_fields")) {
2110 ret = add_all_dynamic_fields(evlist, raw_trace, level);
2114 if (event_name == NULL) {
2115 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2119 evsel = find_evsel(evlist, event_name);
2120 if (evsel == NULL) {
2121 pr_debug("Cannot find event: %s\n", event_name);
2126 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2127 pr_debug("%s is not a tracepoint event\n", event_name);
2132 if (!strcmp(field_name, "*")) {
2133 ret = add_evsel_fields(evsel, raw_trace, level);
2135 field = pevent_find_any_field(evsel->tp_format, field_name);
2136 if (field == NULL) {
2137 pr_debug("Cannot find event field for %s.%s\n",
2138 event_name, field_name);
2142 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2150 static int __sort_dimension__add(struct sort_dimension *sd,
2151 struct perf_hpp_list *list,
2157 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2160 if (sd->entry->se_collapse)
2161 list->need_collapse = 1;
2168 static int __hpp_dimension__add(struct hpp_dimension *hd,
2169 struct perf_hpp_list *list,
2172 struct perf_hpp_fmt *fmt;
2177 fmt = __hpp_dimension__alloc_hpp(hd, level);
2182 perf_hpp_list__register_sort_field(list, fmt);
2186 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2187 struct sort_dimension *sd)
2192 if (__sort_dimension__add_hpp_output(sd, list) < 0)
2199 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2200 struct hpp_dimension *hd)
2202 struct perf_hpp_fmt *fmt;
2207 fmt = __hpp_dimension__alloc_hpp(hd, 0);
2212 perf_hpp_list__column_register(list, fmt);
2216 int hpp_dimension__add_output(unsigned col)
2218 BUG_ON(col >= PERF_HPP__MAX_INDEX);
2219 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2222 static int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2223 struct perf_evlist *evlist,
2228 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2229 struct sort_dimension *sd = &common_sort_dimensions[i];
2231 if (strncasecmp(tok, sd->name, strlen(tok)))
2234 if (sd->entry == &sort_parent) {
2235 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2239 regerror(ret, &parent_regex, err, sizeof(err));
2240 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2244 } else if (sd->entry == &sort_sym) {
2247 * perf diff displays the performance difference amongst
2248 * two or more perf.data files. Those files could come
2249 * from different binaries. So we should not compare
2250 * their ips, but the name of symbol.
2252 if (sort__mode == SORT_MODE__DIFF)
2253 sd->entry->se_collapse = sort__sym_sort;
2255 } else if (sd->entry == &sort_dso) {
2257 } else if (sd->entry == &sort_socket) {
2259 } else if (sd->entry == &sort_thread) {
2260 sort__has_thread = 1;
2261 } else if (sd->entry == &sort_comm) {
2265 return __sort_dimension__add(sd, list, level);
2268 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2269 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2271 if (strncasecmp(tok, hd->name, strlen(tok)))
2274 return __hpp_dimension__add(hd, list, level);
2277 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2278 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2280 if (strncasecmp(tok, sd->name, strlen(tok)))
2283 if (sort__mode != SORT_MODE__BRANCH)
2286 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2289 __sort_dimension__add(sd, list, level);
2293 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2294 struct sort_dimension *sd = &memory_sort_dimensions[i];
2296 if (strncasecmp(tok, sd->name, strlen(tok)))
2299 if (sort__mode != SORT_MODE__MEMORY)
2302 if (sd->entry == &sort_mem_daddr_sym)
2305 __sort_dimension__add(sd, list, level);
2309 if (!add_dynamic_entry(evlist, tok, level))
2315 static int setup_sort_list(struct perf_hpp_list *list, char *str,
2316 struct perf_evlist *evlist)
2322 bool in_group = false;
2326 tmp = strpbrk(str, "{}, ");
2331 next_level = level + 1;
2335 else if (*tmp == '}')
2343 ret = sort_dimension__add(list, tok, evlist, level);
2344 if (ret == -EINVAL) {
2345 error("Invalid --sort key: `%s'", tok);
2347 } else if (ret == -ESRCH) {
2348 error("Unknown --sort key: `%s'", tok);
2359 static const char *get_default_sort_order(struct perf_evlist *evlist)
2361 const char *default_sort_orders[] = {
2363 default_branch_sort_order,
2364 default_mem_sort_order,
2365 default_top_sort_order,
2366 default_diff_sort_order,
2367 default_tracepoint_sort_order,
2369 bool use_trace = true;
2370 struct perf_evsel *evsel;
2372 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2377 evlist__for_each(evlist, evsel) {
2378 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2385 sort__mode = SORT_MODE__TRACEPOINT;
2386 if (symbol_conf.raw_trace)
2387 return "trace_fields";
2390 return default_sort_orders[sort__mode];
2393 static int setup_sort_order(struct perf_evlist *evlist)
2395 char *new_sort_order;
2398 * Append '+'-prefixed sort order to the default sort
2401 if (!sort_order || is_strict_order(sort_order))
2404 if (sort_order[1] == '\0') {
2405 error("Invalid --sort key: `+'");
2410 * We allocate new sort_order string, but we never free it,
2411 * because it's checked over the rest of the code.
2413 if (asprintf(&new_sort_order, "%s,%s",
2414 get_default_sort_order(evlist), sort_order + 1) < 0) {
2415 error("Not enough memory to set up --sort");
2419 sort_order = new_sort_order;
2424 * Adds 'pre,' prefix into 'str' is 'pre' is
2425 * not already part of 'str'.
2427 static char *prefix_if_not_in(const char *pre, char *str)
2431 if (!str || strstr(str, pre))
2434 if (asprintf(&n, "%s,%s", pre, str) < 0)
2441 static char *setup_overhead(char *keys)
2443 keys = prefix_if_not_in("overhead", keys);
2445 if (symbol_conf.cumulate_callchain)
2446 keys = prefix_if_not_in("overhead_children", keys);
2451 static int __setup_sorting(struct perf_evlist *evlist)
2454 const char *sort_keys;
2457 ret = setup_sort_order(evlist);
2461 sort_keys = sort_order;
2462 if (sort_keys == NULL) {
2463 if (is_strict_order(field_order)) {
2465 * If user specified field order but no sort order,
2466 * we'll honor it and not add default sort orders.
2471 sort_keys = get_default_sort_order(evlist);
2474 str = strdup(sort_keys);
2476 error("Not enough memory to setup sort keys");
2481 * Prepend overhead fields for backward compatibility.
2483 if (!is_strict_order(field_order)) {
2484 str = setup_overhead(str);
2486 error("Not enough memory to setup overhead keys");
2491 ret = setup_sort_list(&perf_hpp_list, str, evlist);
2497 void perf_hpp__set_elide(int idx, bool elide)
2499 struct perf_hpp_fmt *fmt;
2500 struct hpp_sort_entry *hse;
2502 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2503 if (!perf_hpp__is_sort_entry(fmt))
2506 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2507 if (hse->se->se_width_idx == idx) {
2514 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
2516 if (list && strlist__nr_entries(list) == 1) {
2518 fprintf(fp, "# %s: %s\n", list_name,
2519 strlist__entry(list, 0)->s);
2525 static bool get_elide(int idx, FILE *output)
2529 return __get_elide(symbol_conf.sym_list, "symbol", output);
2531 return __get_elide(symbol_conf.dso_list, "dso", output);
2533 return __get_elide(symbol_conf.comm_list, "comm", output);
2538 if (sort__mode != SORT_MODE__BRANCH)
2542 case HISTC_SYMBOL_FROM:
2543 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2544 case HISTC_SYMBOL_TO:
2545 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2546 case HISTC_DSO_FROM:
2547 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2549 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2557 void sort__setup_elide(FILE *output)
2559 struct perf_hpp_fmt *fmt;
2560 struct hpp_sort_entry *hse;
2562 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2563 if (!perf_hpp__is_sort_entry(fmt))
2566 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2567 fmt->elide = get_elide(hse->se->se_width_idx, output);
2571 * It makes no sense to elide all of sort entries.
2572 * Just revert them to show up again.
2574 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2575 if (!perf_hpp__is_sort_entry(fmt))
2582 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2583 if (!perf_hpp__is_sort_entry(fmt))
2590 static int output_field_add(struct perf_hpp_list *list, char *tok)
2594 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2595 struct sort_dimension *sd = &common_sort_dimensions[i];
2597 if (strncasecmp(tok, sd->name, strlen(tok)))
2600 return __sort_dimension__add_output(list, sd);
2603 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2604 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2606 if (strncasecmp(tok, hd->name, strlen(tok)))
2609 return __hpp_dimension__add_output(list, hd);
2612 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2613 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2615 if (strncasecmp(tok, sd->name, strlen(tok)))
2618 return __sort_dimension__add_output(list, sd);
2621 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2622 struct sort_dimension *sd = &memory_sort_dimensions[i];
2624 if (strncasecmp(tok, sd->name, strlen(tok)))
2627 return __sort_dimension__add_output(list, sd);
2633 static int setup_output_list(struct perf_hpp_list *list, char *str)
2638 for (tok = strtok_r(str, ", ", &tmp);
2639 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2640 ret = output_field_add(list, tok);
2641 if (ret == -EINVAL) {
2642 error("Invalid --fields key: `%s'", tok);
2644 } else if (ret == -ESRCH) {
2645 error("Unknown --fields key: `%s'", tok);
2653 static void reset_dimensions(void)
2657 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2658 common_sort_dimensions[i].taken = 0;
2660 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2661 hpp_sort_dimensions[i].taken = 0;
2663 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2664 bstack_sort_dimensions[i].taken = 0;
2666 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2667 memory_sort_dimensions[i].taken = 0;
2670 bool is_strict_order(const char *order)
2672 return order && (*order != '+');
2675 static int __setup_output_field(void)
2680 if (field_order == NULL)
2683 strp = str = strdup(field_order);
2685 error("Not enough memory to setup output fields");
2689 if (!is_strict_order(field_order))
2692 if (!strlen(strp)) {
2693 error("Invalid --fields key: `+'");
2697 ret = setup_output_list(&perf_hpp_list, strp);
2704 int setup_sorting(struct perf_evlist *evlist)
2708 err = __setup_sorting(evlist);
2712 if (parent_pattern != default_parent_pattern) {
2713 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
2721 * perf diff doesn't use default hpp output fields.
2723 if (sort__mode != SORT_MODE__DIFF)
2726 err = __setup_output_field();
2730 /* copy sort keys to output fields */
2731 perf_hpp__setup_output_field(&perf_hpp_list);
2732 /* and then copy output fields to sort keys */
2733 perf_hpp__append_sort_keys(&perf_hpp_list);
2735 /* setup hists-specific output fields */
2736 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2742 void reset_output_field(void)
2744 perf_hpp_list.need_collapse = 0;
2745 perf_hpp_list.parent = 0;
2746 perf_hpp_list.sym = 0;
2747 perf_hpp_list.dso = 0;
2753 perf_hpp__reset_output_field(&perf_hpp_list);