10 #include <traceevent/event-parse.h>
11 #include "mem-events.h"
12 #include <linux/kernel.h>
15 const char default_parent_pattern[] = "^sys_|^do_page_fault";
16 const char *parent_pattern = default_parent_pattern;
17 const char *default_sort_order = "comm,dso,symbol";
18 const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
19 const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
20 const char default_top_sort_order[] = "dso,symbol";
21 const char default_diff_sort_order[] = "dso,symbol";
22 const char default_tracepoint_sort_order[] = "trace";
23 const char *sort_order;
24 const char *field_order;
25 regex_t ignore_callees_regex;
26 int have_ignore_callees = 0;
27 enum sort_mode sort__mode = SORT_MODE__NORMAL;
30 * Replaces all occurrences of a char used with the:
32 * -t, --field-separator
34 * option, that uses a special separator character and don't pad with spaces,
35 * replacing all occurances of this separator in symbol names (and other
36 * output) with a '.' character, that thus it's the only non valid separator.
38 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
44 n = vsnprintf(bf, size, fmt, ap);
45 if (symbol_conf.field_sep && n > 0) {
49 sep = strchr(sep, *symbol_conf.field_sep);
62 static int64_t cmp_null(const void *l, const void *r)
75 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
77 return right->thread->tid - left->thread->tid;
80 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
81 size_t size, unsigned int width)
83 const char *comm = thread__comm_str(he->thread);
85 width = max(7U, width) - 8;
86 return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid,
87 width, width, comm ?: "");
90 static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
92 const struct thread *th = arg;
94 if (type != HIST_FILTER__THREAD)
97 return th && he->thread != th;
100 struct sort_entry sort_thread = {
101 .se_header = " Pid:Command",
102 .se_cmp = sort__thread_cmp,
103 .se_snprintf = hist_entry__thread_snprintf,
104 .se_filter = hist_entry__thread_filter,
105 .se_width_idx = HISTC_THREAD,
111 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
113 /* Compare the addr that should be unique among comm */
114 return strcmp(comm__str(right->comm), comm__str(left->comm));
118 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
120 /* Compare the addr that should be unique among comm */
121 return strcmp(comm__str(right->comm), comm__str(left->comm));
125 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
127 return strcmp(comm__str(right->comm), comm__str(left->comm));
130 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
131 size_t size, unsigned int width)
133 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
136 struct sort_entry sort_comm = {
137 .se_header = "Command",
138 .se_cmp = sort__comm_cmp,
139 .se_collapse = sort__comm_collapse,
140 .se_sort = sort__comm_sort,
141 .se_snprintf = hist_entry__comm_snprintf,
142 .se_filter = hist_entry__thread_filter,
143 .se_width_idx = HISTC_COMM,
148 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
150 struct dso *dso_l = map_l ? map_l->dso : NULL;
151 struct dso *dso_r = map_r ? map_r->dso : NULL;
152 const char *dso_name_l, *dso_name_r;
154 if (!dso_l || !dso_r)
155 return cmp_null(dso_r, dso_l);
158 dso_name_l = dso_l->long_name;
159 dso_name_r = dso_r->long_name;
161 dso_name_l = dso_l->short_name;
162 dso_name_r = dso_r->short_name;
165 return strcmp(dso_name_l, dso_name_r);
169 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
171 return _sort__dso_cmp(right->ms.map, left->ms.map);
174 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
175 size_t size, unsigned int width)
177 if (map && map->dso) {
178 const char *dso_name = verbose > 0 ? map->dso->long_name :
179 map->dso->short_name;
180 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
183 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
186 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
187 size_t size, unsigned int width)
189 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
192 static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
194 const struct dso *dso = arg;
196 if (type != HIST_FILTER__DSO)
199 return dso && (!he->ms.map || he->ms.map->dso != dso);
202 struct sort_entry sort_dso = {
203 .se_header = "Shared Object",
204 .se_cmp = sort__dso_cmp,
205 .se_snprintf = hist_entry__dso_snprintf,
206 .se_filter = hist_entry__dso_filter,
207 .se_width_idx = HISTC_DSO,
212 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
214 return (int64_t)(right_ip - left_ip);
217 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
219 if (!sym_l || !sym_r)
220 return cmp_null(sym_l, sym_r);
225 if (sym_l->start != sym_r->start)
226 return (int64_t)(sym_r->start - sym_l->start);
228 return (int64_t)(sym_r->end - sym_l->end);
232 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
236 if (!left->ms.sym && !right->ms.sym)
237 return _sort__addr_cmp(left->ip, right->ip);
240 * comparing symbol address alone is not enough since it's a
241 * relative address within a dso.
243 if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
244 ret = sort__dso_cmp(left, right);
249 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
253 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
255 if (!left->ms.sym || !right->ms.sym)
256 return cmp_null(left->ms.sym, right->ms.sym);
258 return strcmp(right->ms.sym->name, left->ms.sym->name);
261 static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
262 u64 ip, char level, char *bf, size_t size,
268 char o = map ? dso__symtab_origin(map->dso) : '!';
269 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
270 BITS_PER_LONG / 4 + 2, ip, o);
273 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
275 if (map->type == MAP__VARIABLE) {
276 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
277 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
278 ip - map->unmap_ip(map, sym->start));
280 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
285 size_t len = BITS_PER_LONG / 4;
286 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
293 static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
294 size_t size, unsigned int width)
296 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
297 he->level, bf, size, width);
300 static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
302 const char *sym = arg;
304 if (type != HIST_FILTER__SYMBOL)
307 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
310 struct sort_entry sort_sym = {
311 .se_header = "Symbol",
312 .se_cmp = sort__sym_cmp,
313 .se_sort = sort__sym_sort,
314 .se_snprintf = hist_entry__sym_snprintf,
315 .se_filter = hist_entry__sym_filter,
316 .se_width_idx = HISTC_SYMBOL,
321 char *hist_entry__get_srcline(struct hist_entry *he)
323 struct map *map = he->ms.map;
326 return SRCLINE_UNKNOWN;
328 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
329 he->ms.sym, true, true);
333 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
336 left->srcline = hist_entry__get_srcline(left);
338 right->srcline = hist_entry__get_srcline(right);
340 return strcmp(right->srcline, left->srcline);
343 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
344 size_t size, unsigned int width)
347 he->srcline = hist_entry__get_srcline(he);
349 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
352 struct sort_entry sort_srcline = {
353 .se_header = "Source:Line",
354 .se_cmp = sort__srcline_cmp,
355 .se_snprintf = hist_entry__srcline_snprintf,
356 .se_width_idx = HISTC_SRCLINE,
359 /* --sort srcline_from */
362 sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
364 if (!left->branch_info->srcline_from) {
365 struct map *map = left->branch_info->from.map;
367 left->branch_info->srcline_from = SRCLINE_UNKNOWN;
369 left->branch_info->srcline_from = get_srcline(map->dso,
370 map__rip_2objdump(map,
371 left->branch_info->from.al_addr),
372 left->branch_info->from.sym,
375 if (!right->branch_info->srcline_from) {
376 struct map *map = right->branch_info->from.map;
378 right->branch_info->srcline_from = SRCLINE_UNKNOWN;
380 right->branch_info->srcline_from = get_srcline(map->dso,
381 map__rip_2objdump(map,
382 right->branch_info->from.al_addr),
383 right->branch_info->from.sym,
386 return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
389 static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
390 size_t size, unsigned int width)
392 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
395 struct sort_entry sort_srcline_from = {
396 .se_header = "From Source:Line",
397 .se_cmp = sort__srcline_from_cmp,
398 .se_snprintf = hist_entry__srcline_from_snprintf,
399 .se_width_idx = HISTC_SRCLINE_FROM,
402 /* --sort srcline_to */
405 sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
407 if (!left->branch_info->srcline_to) {
408 struct map *map = left->branch_info->to.map;
410 left->branch_info->srcline_to = SRCLINE_UNKNOWN;
412 left->branch_info->srcline_to = get_srcline(map->dso,
413 map__rip_2objdump(map,
414 left->branch_info->to.al_addr),
415 left->branch_info->from.sym,
418 if (!right->branch_info->srcline_to) {
419 struct map *map = right->branch_info->to.map;
421 right->branch_info->srcline_to = SRCLINE_UNKNOWN;
423 right->branch_info->srcline_to = get_srcline(map->dso,
424 map__rip_2objdump(map,
425 right->branch_info->to.al_addr),
426 right->branch_info->to.sym,
429 return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
432 static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
433 size_t size, unsigned int width)
435 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
438 struct sort_entry sort_srcline_to = {
439 .se_header = "To Source:Line",
440 .se_cmp = sort__srcline_to_cmp,
441 .se_snprintf = hist_entry__srcline_to_snprintf,
442 .se_width_idx = HISTC_SRCLINE_TO,
447 static char no_srcfile[1];
449 static char *hist_entry__get_srcfile(struct hist_entry *e)
452 struct map *map = e->ms.map;
457 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
458 e->ms.sym, false, true, true);
459 if (!strcmp(sf, SRCLINE_UNKNOWN))
471 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
474 left->srcfile = hist_entry__get_srcfile(left);
476 right->srcfile = hist_entry__get_srcfile(right);
478 return strcmp(right->srcfile, left->srcfile);
481 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
482 size_t size, unsigned int width)
485 he->srcfile = hist_entry__get_srcfile(he);
487 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
490 struct sort_entry sort_srcfile = {
491 .se_header = "Source File",
492 .se_cmp = sort__srcfile_cmp,
493 .se_snprintf = hist_entry__srcfile_snprintf,
494 .se_width_idx = HISTC_SRCFILE,
500 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
502 struct symbol *sym_l = left->parent;
503 struct symbol *sym_r = right->parent;
505 if (!sym_l || !sym_r)
506 return cmp_null(sym_l, sym_r);
508 return strcmp(sym_r->name, sym_l->name);
511 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
512 size_t size, unsigned int width)
514 return repsep_snprintf(bf, size, "%-*.*s", width, width,
515 he->parent ? he->parent->name : "[other]");
518 struct sort_entry sort_parent = {
519 .se_header = "Parent symbol",
520 .se_cmp = sort__parent_cmp,
521 .se_snprintf = hist_entry__parent_snprintf,
522 .se_width_idx = HISTC_PARENT,
528 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
530 return right->cpu - left->cpu;
533 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
534 size_t size, unsigned int width)
536 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
539 struct sort_entry sort_cpu = {
541 .se_cmp = sort__cpu_cmp,
542 .se_snprintf = hist_entry__cpu_snprintf,
543 .se_width_idx = HISTC_CPU,
546 /* --sort cgroup_id */
548 static int64_t _sort__cgroup_dev_cmp(u64 left_dev, u64 right_dev)
550 return (int64_t)(right_dev - left_dev);
553 static int64_t _sort__cgroup_inode_cmp(u64 left_ino, u64 right_ino)
555 return (int64_t)(right_ino - left_ino);
559 sort__cgroup_id_cmp(struct hist_entry *left, struct hist_entry *right)
563 ret = _sort__cgroup_dev_cmp(right->cgroup_id.dev, left->cgroup_id.dev);
567 return _sort__cgroup_inode_cmp(right->cgroup_id.ino,
568 left->cgroup_id.ino);
571 static int hist_entry__cgroup_id_snprintf(struct hist_entry *he,
572 char *bf, size_t size,
573 unsigned int width __maybe_unused)
575 return repsep_snprintf(bf, size, "%lu/0x%lx", he->cgroup_id.dev,
579 struct sort_entry sort_cgroup_id = {
580 .se_header = "cgroup id (dev/inode)",
581 .se_cmp = sort__cgroup_id_cmp,
582 .se_snprintf = hist_entry__cgroup_id_snprintf,
583 .se_width_idx = HISTC_CGROUP_ID,
589 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
591 return right->socket - left->socket;
594 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
595 size_t size, unsigned int width)
597 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
600 static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
602 int sk = *(const int *)arg;
604 if (type != HIST_FILTER__SOCKET)
607 return sk >= 0 && he->socket != sk;
610 struct sort_entry sort_socket = {
611 .se_header = "Socket",
612 .se_cmp = sort__socket_cmp,
613 .se_snprintf = hist_entry__socket_snprintf,
614 .se_filter = hist_entry__socket_filter,
615 .se_width_idx = HISTC_SOCKET,
620 static char *get_trace_output(struct hist_entry *he)
622 struct trace_seq seq;
623 struct perf_evsel *evsel;
624 struct pevent_record rec = {
625 .data = he->raw_data,
626 .size = he->raw_size,
629 evsel = hists_to_evsel(he->hists);
631 trace_seq_init(&seq);
632 if (symbol_conf.raw_trace) {
633 pevent_print_fields(&seq, he->raw_data, he->raw_size,
636 pevent_event_info(&seq, evsel->tp_format, &rec);
639 * Trim the buffer, it starts at 4KB and we're not going to
640 * add anything more to this buffer.
642 return realloc(seq.buffer, seq.len + 1);
646 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
648 struct perf_evsel *evsel;
650 evsel = hists_to_evsel(left->hists);
651 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
654 if (left->trace_output == NULL)
655 left->trace_output = get_trace_output(left);
656 if (right->trace_output == NULL)
657 right->trace_output = get_trace_output(right);
659 return strcmp(right->trace_output, left->trace_output);
662 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
663 size_t size, unsigned int width)
665 struct perf_evsel *evsel;
667 evsel = hists_to_evsel(he->hists);
668 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
669 return scnprintf(bf, size, "%-.*s", width, "N/A");
671 if (he->trace_output == NULL)
672 he->trace_output = get_trace_output(he);
673 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
676 struct sort_entry sort_trace = {
677 .se_header = "Trace output",
678 .se_cmp = sort__trace_cmp,
679 .se_snprintf = hist_entry__trace_snprintf,
680 .se_width_idx = HISTC_TRACE,
683 /* sort keys for branch stacks */
686 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
688 if (!left->branch_info || !right->branch_info)
689 return cmp_null(left->branch_info, right->branch_info);
691 return _sort__dso_cmp(left->branch_info->from.map,
692 right->branch_info->from.map);
695 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
696 size_t size, unsigned int width)
699 return _hist_entry__dso_snprintf(he->branch_info->from.map,
702 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
705 static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
708 const struct dso *dso = arg;
710 if (type != HIST_FILTER__DSO)
713 return dso && (!he->branch_info || !he->branch_info->from.map ||
714 he->branch_info->from.map->dso != dso);
718 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
720 if (!left->branch_info || !right->branch_info)
721 return cmp_null(left->branch_info, right->branch_info);
723 return _sort__dso_cmp(left->branch_info->to.map,
724 right->branch_info->to.map);
727 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
728 size_t size, unsigned int width)
731 return _hist_entry__dso_snprintf(he->branch_info->to.map,
734 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
737 static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
740 const struct dso *dso = arg;
742 if (type != HIST_FILTER__DSO)
745 return dso && (!he->branch_info || !he->branch_info->to.map ||
746 he->branch_info->to.map->dso != dso);
750 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
752 struct addr_map_symbol *from_l = &left->branch_info->from;
753 struct addr_map_symbol *from_r = &right->branch_info->from;
755 if (!left->branch_info || !right->branch_info)
756 return cmp_null(left->branch_info, right->branch_info);
758 from_l = &left->branch_info->from;
759 from_r = &right->branch_info->from;
761 if (!from_l->sym && !from_r->sym)
762 return _sort__addr_cmp(from_l->addr, from_r->addr);
764 return _sort__sym_cmp(from_l->sym, from_r->sym);
768 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
770 struct addr_map_symbol *to_l, *to_r;
772 if (!left->branch_info || !right->branch_info)
773 return cmp_null(left->branch_info, right->branch_info);
775 to_l = &left->branch_info->to;
776 to_r = &right->branch_info->to;
778 if (!to_l->sym && !to_r->sym)
779 return _sort__addr_cmp(to_l->addr, to_r->addr);
781 return _sort__sym_cmp(to_l->sym, to_r->sym);
784 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
785 size_t size, unsigned int width)
787 if (he->branch_info) {
788 struct addr_map_symbol *from = &he->branch_info->from;
790 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
791 he->level, bf, size, width);
794 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
797 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
798 size_t size, unsigned int width)
800 if (he->branch_info) {
801 struct addr_map_symbol *to = &he->branch_info->to;
803 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
804 he->level, bf, size, width);
807 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
810 static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
813 const char *sym = arg;
815 if (type != HIST_FILTER__SYMBOL)
818 return sym && !(he->branch_info && he->branch_info->from.sym &&
819 strstr(he->branch_info->from.sym->name, sym));
822 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
825 const char *sym = arg;
827 if (type != HIST_FILTER__SYMBOL)
830 return sym && !(he->branch_info && he->branch_info->to.sym &&
831 strstr(he->branch_info->to.sym->name, sym));
834 struct sort_entry sort_dso_from = {
835 .se_header = "Source Shared Object",
836 .se_cmp = sort__dso_from_cmp,
837 .se_snprintf = hist_entry__dso_from_snprintf,
838 .se_filter = hist_entry__dso_from_filter,
839 .se_width_idx = HISTC_DSO_FROM,
842 struct sort_entry sort_dso_to = {
843 .se_header = "Target Shared Object",
844 .se_cmp = sort__dso_to_cmp,
845 .se_snprintf = hist_entry__dso_to_snprintf,
846 .se_filter = hist_entry__dso_to_filter,
847 .se_width_idx = HISTC_DSO_TO,
850 struct sort_entry sort_sym_from = {
851 .se_header = "Source Symbol",
852 .se_cmp = sort__sym_from_cmp,
853 .se_snprintf = hist_entry__sym_from_snprintf,
854 .se_filter = hist_entry__sym_from_filter,
855 .se_width_idx = HISTC_SYMBOL_FROM,
858 struct sort_entry sort_sym_to = {
859 .se_header = "Target Symbol",
860 .se_cmp = sort__sym_to_cmp,
861 .se_snprintf = hist_entry__sym_to_snprintf,
862 .se_filter = hist_entry__sym_to_filter,
863 .se_width_idx = HISTC_SYMBOL_TO,
867 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
871 if (!left->branch_info || !right->branch_info)
872 return cmp_null(left->branch_info, right->branch_info);
874 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
875 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
879 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
880 size_t size, unsigned int width){
881 static const char *out = "N/A";
883 if (he->branch_info) {
884 if (he->branch_info->flags.predicted)
886 else if (he->branch_info->flags.mispred)
890 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
894 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
896 if (!left->branch_info || !right->branch_info)
897 return cmp_null(left->branch_info, right->branch_info);
899 return left->branch_info->flags.cycles -
900 right->branch_info->flags.cycles;
903 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
904 size_t size, unsigned int width)
906 if (!he->branch_info)
907 return scnprintf(bf, size, "%-.*s", width, "N/A");
908 if (he->branch_info->flags.cycles == 0)
909 return repsep_snprintf(bf, size, "%-*s", width, "-");
910 return repsep_snprintf(bf, size, "%-*hd", width,
911 he->branch_info->flags.cycles);
914 struct sort_entry sort_cycles = {
915 .se_header = "Basic Block Cycles",
916 .se_cmp = sort__cycles_cmp,
917 .se_snprintf = hist_entry__cycles_snprintf,
918 .se_width_idx = HISTC_CYCLES,
921 /* --sort daddr_sym */
923 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
925 uint64_t l = 0, r = 0;
928 l = left->mem_info->daddr.addr;
930 r = right->mem_info->daddr.addr;
932 return (int64_t)(r - l);
935 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
936 size_t size, unsigned int width)
939 struct map *map = NULL;
940 struct symbol *sym = NULL;
943 addr = he->mem_info->daddr.addr;
944 map = he->mem_info->daddr.map;
945 sym = he->mem_info->daddr.sym;
947 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
952 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
954 uint64_t l = 0, r = 0;
957 l = left->mem_info->iaddr.addr;
959 r = right->mem_info->iaddr.addr;
961 return (int64_t)(r - l);
964 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
965 size_t size, unsigned int width)
968 struct map *map = NULL;
969 struct symbol *sym = NULL;
972 addr = he->mem_info->iaddr.addr;
973 map = he->mem_info->iaddr.map;
974 sym = he->mem_info->iaddr.sym;
976 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
981 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
983 struct map *map_l = NULL;
984 struct map *map_r = NULL;
987 map_l = left->mem_info->daddr.map;
989 map_r = right->mem_info->daddr.map;
991 return _sort__dso_cmp(map_l, map_r);
994 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
995 size_t size, unsigned int width)
997 struct map *map = NULL;
1000 map = he->mem_info->daddr.map;
1002 return _hist_entry__dso_snprintf(map, bf, size, width);
1006 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
1008 union perf_mem_data_src data_src_l;
1009 union perf_mem_data_src data_src_r;
1012 data_src_l = left->mem_info->data_src;
1014 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
1016 if (right->mem_info)
1017 data_src_r = right->mem_info->data_src;
1019 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
1021 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
1024 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
1025 size_t size, unsigned int width)
1029 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
1030 return repsep_snprintf(bf, size, "%.*s", width, out);
1034 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
1036 union perf_mem_data_src data_src_l;
1037 union perf_mem_data_src data_src_r;
1040 data_src_l = left->mem_info->data_src;
1042 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
1044 if (right->mem_info)
1045 data_src_r = right->mem_info->data_src;
1047 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
1049 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
1052 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
1053 size_t size, unsigned int width)
1057 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
1058 return repsep_snprintf(bf, size, "%-*s", width, out);
1062 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
1064 union perf_mem_data_src data_src_l;
1065 union perf_mem_data_src data_src_r;
1068 data_src_l = left->mem_info->data_src;
1070 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
1072 if (right->mem_info)
1073 data_src_r = right->mem_info->data_src;
1075 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
1077 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
1080 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
1081 size_t size, unsigned int width)
1085 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
1086 return repsep_snprintf(bf, size, "%-*s", width, out);
1090 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
1092 union perf_mem_data_src data_src_l;
1093 union perf_mem_data_src data_src_r;
1096 data_src_l = left->mem_info->data_src;
1098 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
1100 if (right->mem_info)
1101 data_src_r = right->mem_info->data_src;
1103 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
1105 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
1108 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
1109 size_t size, unsigned int width)
1113 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
1114 return repsep_snprintf(bf, size, "%-*s", width, out);
1118 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
1121 struct map *l_map, *r_map;
1123 if (!left->mem_info) return -1;
1124 if (!right->mem_info) return 1;
1126 /* group event types together */
1127 if (left->cpumode > right->cpumode) return -1;
1128 if (left->cpumode < right->cpumode) return 1;
1130 l_map = left->mem_info->daddr.map;
1131 r_map = right->mem_info->daddr.map;
1133 /* if both are NULL, jump to sort on al_addr instead */
1134 if (!l_map && !r_map)
1137 if (!l_map) return -1;
1138 if (!r_map) return 1;
1140 if (l_map->maj > r_map->maj) return -1;
1141 if (l_map->maj < r_map->maj) return 1;
1143 if (l_map->min > r_map->min) return -1;
1144 if (l_map->min < r_map->min) return 1;
1146 if (l_map->ino > r_map->ino) return -1;
1147 if (l_map->ino < r_map->ino) return 1;
1149 if (l_map->ino_generation > r_map->ino_generation) return -1;
1150 if (l_map->ino_generation < r_map->ino_generation) return 1;
1153 * Addresses with no major/minor numbers are assumed to be
1154 * anonymous in userspace. Sort those on pid then address.
1156 * The kernel and non-zero major/minor mapped areas are
1157 * assumed to be unity mapped. Sort those on address.
1160 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1161 (!(l_map->flags & MAP_SHARED)) &&
1162 !l_map->maj && !l_map->min && !l_map->ino &&
1163 !l_map->ino_generation) {
1164 /* userspace anonymous */
1166 if (left->thread->pid_ > right->thread->pid_) return -1;
1167 if (left->thread->pid_ < right->thread->pid_) return 1;
1171 /* al_addr does all the right addr - start + offset calculations */
1172 l = cl_address(left->mem_info->daddr.al_addr);
1173 r = cl_address(right->mem_info->daddr.al_addr);
1175 if (l > r) return -1;
1176 if (l < r) return 1;
1181 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1182 size_t size, unsigned int width)
1186 struct map *map = NULL;
1187 struct symbol *sym = NULL;
1188 char level = he->level;
1191 addr = cl_address(he->mem_info->daddr.al_addr);
1192 map = he->mem_info->daddr.map;
1193 sym = he->mem_info->daddr.sym;
1195 /* print [s] for shared data mmaps */
1196 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1197 map && (map->type == MAP__VARIABLE) &&
1198 (map->flags & MAP_SHARED) &&
1199 (map->maj || map->min || map->ino ||
1200 map->ino_generation))
1205 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1209 struct sort_entry sort_mispredict = {
1210 .se_header = "Branch Mispredicted",
1211 .se_cmp = sort__mispredict_cmp,
1212 .se_snprintf = hist_entry__mispredict_snprintf,
1213 .se_width_idx = HISTC_MISPREDICT,
1216 static u64 he_weight(struct hist_entry *he)
1218 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1222 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1224 return he_weight(left) - he_weight(right);
1227 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1228 size_t size, unsigned int width)
1230 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1233 struct sort_entry sort_local_weight = {
1234 .se_header = "Local Weight",
1235 .se_cmp = sort__local_weight_cmp,
1236 .se_snprintf = hist_entry__local_weight_snprintf,
1237 .se_width_idx = HISTC_LOCAL_WEIGHT,
1241 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1243 return left->stat.weight - right->stat.weight;
1246 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1247 size_t size, unsigned int width)
1249 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1252 struct sort_entry sort_global_weight = {
1253 .se_header = "Weight",
1254 .se_cmp = sort__global_weight_cmp,
1255 .se_snprintf = hist_entry__global_weight_snprintf,
1256 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1259 struct sort_entry sort_mem_daddr_sym = {
1260 .se_header = "Data Symbol",
1261 .se_cmp = sort__daddr_cmp,
1262 .se_snprintf = hist_entry__daddr_snprintf,
1263 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1266 struct sort_entry sort_mem_iaddr_sym = {
1267 .se_header = "Code Symbol",
1268 .se_cmp = sort__iaddr_cmp,
1269 .se_snprintf = hist_entry__iaddr_snprintf,
1270 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1273 struct sort_entry sort_mem_daddr_dso = {
1274 .se_header = "Data Object",
1275 .se_cmp = sort__dso_daddr_cmp,
1276 .se_snprintf = hist_entry__dso_daddr_snprintf,
1277 .se_width_idx = HISTC_MEM_DADDR_DSO,
1280 struct sort_entry sort_mem_locked = {
1281 .se_header = "Locked",
1282 .se_cmp = sort__locked_cmp,
1283 .se_snprintf = hist_entry__locked_snprintf,
1284 .se_width_idx = HISTC_MEM_LOCKED,
1287 struct sort_entry sort_mem_tlb = {
1288 .se_header = "TLB access",
1289 .se_cmp = sort__tlb_cmp,
1290 .se_snprintf = hist_entry__tlb_snprintf,
1291 .se_width_idx = HISTC_MEM_TLB,
1294 struct sort_entry sort_mem_lvl = {
1295 .se_header = "Memory access",
1296 .se_cmp = sort__lvl_cmp,
1297 .se_snprintf = hist_entry__lvl_snprintf,
1298 .se_width_idx = HISTC_MEM_LVL,
1301 struct sort_entry sort_mem_snoop = {
1302 .se_header = "Snoop",
1303 .se_cmp = sort__snoop_cmp,
1304 .se_snprintf = hist_entry__snoop_snprintf,
1305 .se_width_idx = HISTC_MEM_SNOOP,
1308 struct sort_entry sort_mem_dcacheline = {
1309 .se_header = "Data Cacheline",
1310 .se_cmp = sort__dcacheline_cmp,
1311 .se_snprintf = hist_entry__dcacheline_snprintf,
1312 .se_width_idx = HISTC_MEM_DCACHELINE,
1316 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1318 if (!left->branch_info || !right->branch_info)
1319 return cmp_null(left->branch_info, right->branch_info);
1321 return left->branch_info->flags.abort !=
1322 right->branch_info->flags.abort;
1325 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1326 size_t size, unsigned int width)
1328 static const char *out = "N/A";
1330 if (he->branch_info) {
1331 if (he->branch_info->flags.abort)
1337 return repsep_snprintf(bf, size, "%-*s", width, out);
1340 struct sort_entry sort_abort = {
1341 .se_header = "Transaction abort",
1342 .se_cmp = sort__abort_cmp,
1343 .se_snprintf = hist_entry__abort_snprintf,
1344 .se_width_idx = HISTC_ABORT,
1348 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1350 if (!left->branch_info || !right->branch_info)
1351 return cmp_null(left->branch_info, right->branch_info);
1353 return left->branch_info->flags.in_tx !=
1354 right->branch_info->flags.in_tx;
1357 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1358 size_t size, unsigned int width)
1360 static const char *out = "N/A";
1362 if (he->branch_info) {
1363 if (he->branch_info->flags.in_tx)
1369 return repsep_snprintf(bf, size, "%-*s", width, out);
1372 struct sort_entry sort_in_tx = {
1373 .se_header = "Branch in transaction",
1374 .se_cmp = sort__in_tx_cmp,
1375 .se_snprintf = hist_entry__in_tx_snprintf,
1376 .se_width_idx = HISTC_IN_TX,
1380 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1382 return left->transaction - right->transaction;
1385 static inline char *add_str(char *p, const char *str)
1388 return p + strlen(str);
1391 static struct txbit {
1396 { PERF_TXN_ELISION, "EL ", 0 },
1397 { PERF_TXN_TRANSACTION, "TX ", 1 },
1398 { PERF_TXN_SYNC, "SYNC ", 1 },
1399 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1400 { PERF_TXN_RETRY, "RETRY ", 0 },
1401 { PERF_TXN_CONFLICT, "CON ", 0 },
1402 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1403 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1407 int hist_entry__transaction_len(void)
1412 for (i = 0; txbits[i].name; i++) {
1413 if (!txbits[i].skip_for_len)
1414 len += strlen(txbits[i].name);
1416 len += 4; /* :XX<space> */
1420 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1421 size_t size, unsigned int width)
1423 u64 t = he->transaction;
1429 for (i = 0; txbits[i].name; i++)
1430 if (txbits[i].flag & t)
1431 p = add_str(p, txbits[i].name);
1432 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1433 p = add_str(p, "NEITHER ");
1434 if (t & PERF_TXN_ABORT_MASK) {
1435 sprintf(p, ":%" PRIx64,
1436 (t & PERF_TXN_ABORT_MASK) >>
1437 PERF_TXN_ABORT_SHIFT);
1441 return repsep_snprintf(bf, size, "%-*s", width, buf);
1444 struct sort_entry sort_transaction = {
1445 .se_header = "Transaction ",
1446 .se_cmp = sort__transaction_cmp,
1447 .se_snprintf = hist_entry__transaction_snprintf,
1448 .se_width_idx = HISTC_TRANSACTION,
1451 /* --sort symbol_size */
1453 static int64_t _sort__sym_size_cmp(struct symbol *sym_l, struct symbol *sym_r)
1455 int64_t size_l = sym_l != NULL ? symbol__size(sym_l) : 0;
1456 int64_t size_r = sym_r != NULL ? symbol__size(sym_r) : 0;
1458 return size_l < size_r ? -1 :
1459 size_l == size_r ? 0 : 1;
1463 sort__sym_size_cmp(struct hist_entry *left, struct hist_entry *right)
1465 return _sort__sym_size_cmp(right->ms.sym, left->ms.sym);
1468 static int _hist_entry__sym_size_snprintf(struct symbol *sym, char *bf,
1469 size_t bf_size, unsigned int width)
1472 return repsep_snprintf(bf, bf_size, "%*d", width, symbol__size(sym));
1474 return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1477 static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
1478 size_t size, unsigned int width)
1480 return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
1483 struct sort_entry sort_sym_size = {
1484 .se_header = "Symbol size",
1485 .se_cmp = sort__sym_size_cmp,
1486 .se_snprintf = hist_entry__sym_size_snprintf,
1487 .se_width_idx = HISTC_SYM_SIZE,
1491 struct sort_dimension {
1493 struct sort_entry *entry;
1497 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1499 static struct sort_dimension common_sort_dimensions[] = {
1500 DIM(SORT_PID, "pid", sort_thread),
1501 DIM(SORT_COMM, "comm", sort_comm),
1502 DIM(SORT_DSO, "dso", sort_dso),
1503 DIM(SORT_SYM, "symbol", sort_sym),
1504 DIM(SORT_PARENT, "parent", sort_parent),
1505 DIM(SORT_CPU, "cpu", sort_cpu),
1506 DIM(SORT_SOCKET, "socket", sort_socket),
1507 DIM(SORT_SRCLINE, "srcline", sort_srcline),
1508 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1509 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1510 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1511 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1512 DIM(SORT_TRACE, "trace", sort_trace),
1513 DIM(SORT_SYM_SIZE, "symbol_size", sort_sym_size),
1514 DIM(SORT_CGROUP_ID, "cgroup_id", sort_cgroup_id),
1519 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1521 static struct sort_dimension bstack_sort_dimensions[] = {
1522 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1523 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1524 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1525 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1526 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1527 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1528 DIM(SORT_ABORT, "abort", sort_abort),
1529 DIM(SORT_CYCLES, "cycles", sort_cycles),
1530 DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
1531 DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
1536 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1538 static struct sort_dimension memory_sort_dimensions[] = {
1539 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1540 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1541 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1542 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1543 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1544 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1545 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1546 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1551 struct hpp_dimension {
1553 struct perf_hpp_fmt *fmt;
1557 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1559 static struct hpp_dimension hpp_sort_dimensions[] = {
1560 DIM(PERF_HPP__OVERHEAD, "overhead"),
1561 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1562 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1563 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1564 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1565 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1566 DIM(PERF_HPP__SAMPLES, "sample"),
1567 DIM(PERF_HPP__PERIOD, "period"),
1572 struct hpp_sort_entry {
1573 struct perf_hpp_fmt hpp;
1574 struct sort_entry *se;
1577 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1579 struct hpp_sort_entry *hse;
1581 if (!perf_hpp__is_sort_entry(fmt))
1584 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1585 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1588 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1589 struct hists *hists, int line __maybe_unused,
1590 int *span __maybe_unused)
1592 struct hpp_sort_entry *hse;
1593 size_t len = fmt->user_len;
1595 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1598 len = hists__col_len(hists, hse->se->se_width_idx);
1600 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1603 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1604 struct perf_hpp *hpp __maybe_unused,
1605 struct hists *hists)
1607 struct hpp_sort_entry *hse;
1608 size_t len = fmt->user_len;
1610 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1613 len = hists__col_len(hists, hse->se->se_width_idx);
1618 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1619 struct hist_entry *he)
1621 struct hpp_sort_entry *hse;
1622 size_t len = fmt->user_len;
1624 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1627 len = hists__col_len(he->hists, hse->se->se_width_idx);
1629 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1632 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1633 struct hist_entry *a, struct hist_entry *b)
1635 struct hpp_sort_entry *hse;
1637 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1638 return hse->se->se_cmp(a, b);
1641 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1642 struct hist_entry *a, struct hist_entry *b)
1644 struct hpp_sort_entry *hse;
1645 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1647 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1648 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1649 return collapse_fn(a, b);
1652 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1653 struct hist_entry *a, struct hist_entry *b)
1655 struct hpp_sort_entry *hse;
1656 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1658 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1659 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1660 return sort_fn(a, b);
1663 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1665 return format->header == __sort__hpp_header;
1668 #define MK_SORT_ENTRY_CHK(key) \
1669 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1671 struct hpp_sort_entry *hse; \
1673 if (!perf_hpp__is_sort_entry(fmt)) \
1676 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1677 return hse->se == &sort_ ## key ; \
1680 MK_SORT_ENTRY_CHK(trace)
1681 MK_SORT_ENTRY_CHK(srcline)
1682 MK_SORT_ENTRY_CHK(srcfile)
1683 MK_SORT_ENTRY_CHK(thread)
1684 MK_SORT_ENTRY_CHK(comm)
1685 MK_SORT_ENTRY_CHK(dso)
1686 MK_SORT_ENTRY_CHK(sym)
1689 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1691 struct hpp_sort_entry *hse_a;
1692 struct hpp_sort_entry *hse_b;
1694 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1697 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1698 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1700 return hse_a->se == hse_b->se;
1703 static void hse_free(struct perf_hpp_fmt *fmt)
1705 struct hpp_sort_entry *hse;
1707 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1711 static struct hpp_sort_entry *
1712 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
1714 struct hpp_sort_entry *hse;
1716 hse = malloc(sizeof(*hse));
1718 pr_err("Memory allocation failed\n");
1722 hse->se = sd->entry;
1723 hse->hpp.name = sd->entry->se_header;
1724 hse->hpp.header = __sort__hpp_header;
1725 hse->hpp.width = __sort__hpp_width;
1726 hse->hpp.entry = __sort__hpp_entry;
1727 hse->hpp.color = NULL;
1729 hse->hpp.cmp = __sort__hpp_cmp;
1730 hse->hpp.collapse = __sort__hpp_collapse;
1731 hse->hpp.sort = __sort__hpp_sort;
1732 hse->hpp.equal = __sort__hpp_equal;
1733 hse->hpp.free = hse_free;
1735 INIT_LIST_HEAD(&hse->hpp.list);
1736 INIT_LIST_HEAD(&hse->hpp.sort_list);
1737 hse->hpp.elide = false;
1739 hse->hpp.user_len = 0;
1740 hse->hpp.level = level;
1745 static void hpp_free(struct perf_hpp_fmt *fmt)
1750 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1753 struct perf_hpp_fmt *fmt;
1755 fmt = memdup(hd->fmt, sizeof(*fmt));
1757 INIT_LIST_HEAD(&fmt->list);
1758 INIT_LIST_HEAD(&fmt->sort_list);
1759 fmt->free = hpp_free;
1766 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1768 struct perf_hpp_fmt *fmt;
1769 struct hpp_sort_entry *hse;
1773 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1774 if (!perf_hpp__is_sort_entry(fmt))
1777 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1778 if (hse->se->se_filter == NULL)
1782 * hist entry is filtered if any of sort key in the hpp list
1783 * is applied. But it should skip non-matched filter types.
1785 r = hse->se->se_filter(he, type, arg);
1796 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1797 struct perf_hpp_list *list,
1800 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
1805 perf_hpp_list__register_sort_field(list, &hse->hpp);
1809 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1810 struct perf_hpp_list *list)
1812 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
1817 perf_hpp_list__column_register(list, &hse->hpp);
1821 struct hpp_dynamic_entry {
1822 struct perf_hpp_fmt hpp;
1823 struct perf_evsel *evsel;
1824 struct format_field *field;
1825 unsigned dynamic_len;
1829 static int hde_width(struct hpp_dynamic_entry *hde)
1831 if (!hde->hpp.len) {
1832 int len = hde->dynamic_len;
1833 int namelen = strlen(hde->field->name);
1834 int fieldlen = hde->field->size;
1839 if (!(hde->field->flags & FIELD_IS_STRING)) {
1840 /* length for print hex numbers */
1841 fieldlen = hde->field->size * 2 + 2;
1848 return hde->hpp.len;
1851 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1852 struct hist_entry *he)
1855 struct format_field *field = hde->field;
1862 /* parse pretty print result and update max length */
1863 if (!he->trace_output)
1864 he->trace_output = get_trace_output(he);
1866 namelen = strlen(field->name);
1867 str = he->trace_output;
1870 pos = strchr(str, ' ');
1873 pos = str + strlen(str);
1876 if (!strncmp(str, field->name, namelen)) {
1882 if (len > hde->dynamic_len)
1883 hde->dynamic_len = len;
1894 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1895 struct hists *hists __maybe_unused,
1896 int line __maybe_unused,
1897 int *span __maybe_unused)
1899 struct hpp_dynamic_entry *hde;
1900 size_t len = fmt->user_len;
1902 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1905 len = hde_width(hde);
1907 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1910 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1911 struct perf_hpp *hpp __maybe_unused,
1912 struct hists *hists __maybe_unused)
1914 struct hpp_dynamic_entry *hde;
1915 size_t len = fmt->user_len;
1917 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1920 len = hde_width(hde);
1925 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1927 struct hpp_dynamic_entry *hde;
1929 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1931 return hists_to_evsel(hists) == hde->evsel;
1934 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1935 struct hist_entry *he)
1937 struct hpp_dynamic_entry *hde;
1938 size_t len = fmt->user_len;
1940 struct format_field *field;
1945 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1948 len = hde_width(hde);
1953 if (!he->trace_output)
1954 he->trace_output = get_trace_output(he);
1957 namelen = strlen(field->name);
1958 str = he->trace_output;
1961 pos = strchr(str, ' ');
1964 pos = str + strlen(str);
1967 if (!strncmp(str, field->name, namelen)) {
1969 str = strndup(str, pos - str);
1972 return scnprintf(hpp->buf, hpp->size,
1973 "%*.*s", len, len, "ERROR");
1984 struct trace_seq seq;
1986 trace_seq_init(&seq);
1987 pevent_print_field(&seq, he->raw_data, hde->field);
1991 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1996 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1997 struct hist_entry *a, struct hist_entry *b)
1999 struct hpp_dynamic_entry *hde;
2000 struct format_field *field;
2001 unsigned offset, size;
2003 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2006 update_dynamic_len(hde, a);
2011 if (field->flags & FIELD_IS_DYNAMIC) {
2012 unsigned long long dyn;
2014 pevent_read_number_field(field, a->raw_data, &dyn);
2015 offset = dyn & 0xffff;
2016 size = (dyn >> 16) & 0xffff;
2018 /* record max width for output */
2019 if (size > hde->dynamic_len)
2020 hde->dynamic_len = size;
2022 offset = field->offset;
2026 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
2029 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
2031 return fmt->cmp == __sort__hde_cmp;
2034 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2036 struct hpp_dynamic_entry *hde_a;
2037 struct hpp_dynamic_entry *hde_b;
2039 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
2042 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
2043 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
2045 return hde_a->field == hde_b->field;
2048 static void hde_free(struct perf_hpp_fmt *fmt)
2050 struct hpp_dynamic_entry *hde;
2052 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2056 static struct hpp_dynamic_entry *
2057 __alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
2060 struct hpp_dynamic_entry *hde;
2062 hde = malloc(sizeof(*hde));
2064 pr_debug("Memory allocation failed\n");
2070 hde->dynamic_len = 0;
2072 hde->hpp.name = field->name;
2073 hde->hpp.header = __sort__hde_header;
2074 hde->hpp.width = __sort__hde_width;
2075 hde->hpp.entry = __sort__hde_entry;
2076 hde->hpp.color = NULL;
2078 hde->hpp.cmp = __sort__hde_cmp;
2079 hde->hpp.collapse = __sort__hde_cmp;
2080 hde->hpp.sort = __sort__hde_cmp;
2081 hde->hpp.equal = __sort__hde_equal;
2082 hde->hpp.free = hde_free;
2084 INIT_LIST_HEAD(&hde->hpp.list);
2085 INIT_LIST_HEAD(&hde->hpp.sort_list);
2086 hde->hpp.elide = false;
2088 hde->hpp.user_len = 0;
2089 hde->hpp.level = level;
2094 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
2096 struct perf_hpp_fmt *new_fmt = NULL;
2098 if (perf_hpp__is_sort_entry(fmt)) {
2099 struct hpp_sort_entry *hse, *new_hse;
2101 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2102 new_hse = memdup(hse, sizeof(*hse));
2104 new_fmt = &new_hse->hpp;
2105 } else if (perf_hpp__is_dynamic_entry(fmt)) {
2106 struct hpp_dynamic_entry *hde, *new_hde;
2108 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2109 new_hde = memdup(hde, sizeof(*hde));
2111 new_fmt = &new_hde->hpp;
2113 new_fmt = memdup(fmt, sizeof(*fmt));
2116 INIT_LIST_HEAD(&new_fmt->list);
2117 INIT_LIST_HEAD(&new_fmt->sort_list);
2122 static int parse_field_name(char *str, char **event, char **field, char **opt)
2124 char *event_name, *field_name, *opt_name;
2127 field_name = strchr(str, '.');
2130 *field_name++ = '\0';
2136 opt_name = strchr(field_name, '/');
2140 *event = event_name;
2141 *field = field_name;
2147 /* find match evsel using a given event name. The event name can be:
2148 * 1. '%' + event index (e.g. '%1' for first event)
2149 * 2. full event name (e.g. sched:sched_switch)
2150 * 3. partial event name (should not contain ':')
2152 static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
2154 struct perf_evsel *evsel = NULL;
2155 struct perf_evsel *pos;
2159 if (event_name[0] == '%') {
2160 int nr = strtol(event_name+1, NULL, 0);
2162 if (nr > evlist->nr_entries)
2165 evsel = perf_evlist__first(evlist);
2167 evsel = perf_evsel__next(evsel);
2172 full_name = !!strchr(event_name, ':');
2173 evlist__for_each_entry(evlist, pos) {
2175 if (full_name && !strcmp(pos->name, event_name))
2178 if (!full_name && strstr(pos->name, event_name)) {
2180 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2181 event_name, evsel->name, pos->name);
2191 static int __dynamic_dimension__add(struct perf_evsel *evsel,
2192 struct format_field *field,
2193 bool raw_trace, int level)
2195 struct hpp_dynamic_entry *hde;
2197 hde = __alloc_dynamic_entry(evsel, field, level);
2201 hde->raw_trace = raw_trace;
2203 perf_hpp__register_sort_field(&hde->hpp);
2207 static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
2210 struct format_field *field;
2212 field = evsel->tp_format->format.fields;
2214 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2218 field = field->next;
2223 static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2227 struct perf_evsel *evsel;
2229 evlist__for_each_entry(evlist, evsel) {
2230 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2233 ret = add_evsel_fields(evsel, raw_trace, level);
2240 static int add_all_matching_fields(struct perf_evlist *evlist,
2241 char *field_name, bool raw_trace, int level)
2244 struct perf_evsel *evsel;
2245 struct format_field *field;
2247 evlist__for_each_entry(evlist, evsel) {
2248 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2251 field = pevent_find_any_field(evsel->tp_format, field_name);
2255 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2262 static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2265 char *str, *event_name, *field_name, *opt_name;
2266 struct perf_evsel *evsel;
2267 struct format_field *field;
2268 bool raw_trace = symbol_conf.raw_trace;
2278 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2284 if (strcmp(opt_name, "raw")) {
2285 pr_debug("unsupported field option %s\n", opt_name);
2292 if (!strcmp(field_name, "trace_fields")) {
2293 ret = add_all_dynamic_fields(evlist, raw_trace, level);
2297 if (event_name == NULL) {
2298 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2302 evsel = find_evsel(evlist, event_name);
2303 if (evsel == NULL) {
2304 pr_debug("Cannot find event: %s\n", event_name);
2309 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2310 pr_debug("%s is not a tracepoint event\n", event_name);
2315 if (!strcmp(field_name, "*")) {
2316 ret = add_evsel_fields(evsel, raw_trace, level);
2318 field = pevent_find_any_field(evsel->tp_format, field_name);
2319 if (field == NULL) {
2320 pr_debug("Cannot find event field for %s.%s\n",
2321 event_name, field_name);
2325 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2333 static int __sort_dimension__add(struct sort_dimension *sd,
2334 struct perf_hpp_list *list,
2340 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2343 if (sd->entry->se_collapse)
2344 list->need_collapse = 1;
2351 static int __hpp_dimension__add(struct hpp_dimension *hd,
2352 struct perf_hpp_list *list,
2355 struct perf_hpp_fmt *fmt;
2360 fmt = __hpp_dimension__alloc_hpp(hd, level);
2365 perf_hpp_list__register_sort_field(list, fmt);
2369 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2370 struct sort_dimension *sd)
2375 if (__sort_dimension__add_hpp_output(sd, list) < 0)
2382 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2383 struct hpp_dimension *hd)
2385 struct perf_hpp_fmt *fmt;
2390 fmt = __hpp_dimension__alloc_hpp(hd, 0);
2395 perf_hpp_list__column_register(list, fmt);
2399 int hpp_dimension__add_output(unsigned col)
2401 BUG_ON(col >= PERF_HPP__MAX_INDEX);
2402 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2405 int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2406 struct perf_evlist *evlist,
2411 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2412 struct sort_dimension *sd = &common_sort_dimensions[i];
2414 if (strncasecmp(tok, sd->name, strlen(tok)))
2417 if (sd->entry == &sort_parent) {
2418 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2422 regerror(ret, &parent_regex, err, sizeof(err));
2423 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2427 } else if (sd->entry == &sort_sym) {
2430 * perf diff displays the performance difference amongst
2431 * two or more perf.data files. Those files could come
2432 * from different binaries. So we should not compare
2433 * their ips, but the name of symbol.
2435 if (sort__mode == SORT_MODE__DIFF)
2436 sd->entry->se_collapse = sort__sym_sort;
2438 } else if (sd->entry == &sort_dso) {
2440 } else if (sd->entry == &sort_socket) {
2442 } else if (sd->entry == &sort_thread) {
2444 } else if (sd->entry == &sort_comm) {
2448 return __sort_dimension__add(sd, list, level);
2451 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2452 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2454 if (strncasecmp(tok, hd->name, strlen(tok)))
2457 return __hpp_dimension__add(hd, list, level);
2460 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2461 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2463 if (strncasecmp(tok, sd->name, strlen(tok)))
2466 if (sort__mode != SORT_MODE__BRANCH)
2469 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2472 __sort_dimension__add(sd, list, level);
2476 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2477 struct sort_dimension *sd = &memory_sort_dimensions[i];
2479 if (strncasecmp(tok, sd->name, strlen(tok)))
2482 if (sort__mode != SORT_MODE__MEMORY)
2485 if (sd->entry == &sort_mem_dcacheline && cacheline_size == 0)
2488 if (sd->entry == &sort_mem_daddr_sym)
2491 __sort_dimension__add(sd, list, level);
2495 if (!add_dynamic_entry(evlist, tok, level))
2501 static int setup_sort_list(struct perf_hpp_list *list, char *str,
2502 struct perf_evlist *evlist)
2508 bool in_group = false;
2512 tmp = strpbrk(str, "{}, ");
2517 next_level = level + 1;
2521 else if (*tmp == '}')
2529 ret = sort_dimension__add(list, tok, evlist, level);
2530 if (ret == -EINVAL) {
2531 if (!cacheline_size && !strncasecmp(tok, "dcacheline", strlen(tok)))
2532 error("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2534 error("Invalid --sort key: `%s'", tok);
2536 } else if (ret == -ESRCH) {
2537 error("Unknown --sort key: `%s'", tok);
2548 static const char *get_default_sort_order(struct perf_evlist *evlist)
2550 const char *default_sort_orders[] = {
2552 default_branch_sort_order,
2553 default_mem_sort_order,
2554 default_top_sort_order,
2555 default_diff_sort_order,
2556 default_tracepoint_sort_order,
2558 bool use_trace = true;
2559 struct perf_evsel *evsel;
2561 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2566 evlist__for_each_entry(evlist, evsel) {
2567 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2574 sort__mode = SORT_MODE__TRACEPOINT;
2575 if (symbol_conf.raw_trace)
2576 return "trace_fields";
2579 return default_sort_orders[sort__mode];
2582 static int setup_sort_order(struct perf_evlist *evlist)
2584 char *new_sort_order;
2587 * Append '+'-prefixed sort order to the default sort
2590 if (!sort_order || is_strict_order(sort_order))
2593 if (sort_order[1] == '\0') {
2594 error("Invalid --sort key: `+'");
2599 * We allocate new sort_order string, but we never free it,
2600 * because it's checked over the rest of the code.
2602 if (asprintf(&new_sort_order, "%s,%s",
2603 get_default_sort_order(evlist), sort_order + 1) < 0) {
2604 error("Not enough memory to set up --sort");
2608 sort_order = new_sort_order;
2613 * Adds 'pre,' prefix into 'str' is 'pre' is
2614 * not already part of 'str'.
2616 static char *prefix_if_not_in(const char *pre, char *str)
2620 if (!str || strstr(str, pre))
2623 if (asprintf(&n, "%s,%s", pre, str) < 0)
2630 static char *setup_overhead(char *keys)
2632 if (sort__mode == SORT_MODE__DIFF)
2635 keys = prefix_if_not_in("overhead", keys);
2637 if (symbol_conf.cumulate_callchain)
2638 keys = prefix_if_not_in("overhead_children", keys);
2643 static int __setup_sorting(struct perf_evlist *evlist)
2646 const char *sort_keys;
2649 ret = setup_sort_order(evlist);
2653 sort_keys = sort_order;
2654 if (sort_keys == NULL) {
2655 if (is_strict_order(field_order)) {
2657 * If user specified field order but no sort order,
2658 * we'll honor it and not add default sort orders.
2663 sort_keys = get_default_sort_order(evlist);
2666 str = strdup(sort_keys);
2668 error("Not enough memory to setup sort keys");
2673 * Prepend overhead fields for backward compatibility.
2675 if (!is_strict_order(field_order)) {
2676 str = setup_overhead(str);
2678 error("Not enough memory to setup overhead keys");
2683 ret = setup_sort_list(&perf_hpp_list, str, evlist);
2689 void perf_hpp__set_elide(int idx, bool elide)
2691 struct perf_hpp_fmt *fmt;
2692 struct hpp_sort_entry *hse;
2694 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2695 if (!perf_hpp__is_sort_entry(fmt))
2698 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2699 if (hse->se->se_width_idx == idx) {
2706 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
2708 if (list && strlist__nr_entries(list) == 1) {
2710 fprintf(fp, "# %s: %s\n", list_name,
2711 strlist__entry(list, 0)->s);
2717 static bool get_elide(int idx, FILE *output)
2721 return __get_elide(symbol_conf.sym_list, "symbol", output);
2723 return __get_elide(symbol_conf.dso_list, "dso", output);
2725 return __get_elide(symbol_conf.comm_list, "comm", output);
2730 if (sort__mode != SORT_MODE__BRANCH)
2734 case HISTC_SYMBOL_FROM:
2735 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2736 case HISTC_SYMBOL_TO:
2737 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2738 case HISTC_DSO_FROM:
2739 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2741 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2749 void sort__setup_elide(FILE *output)
2751 struct perf_hpp_fmt *fmt;
2752 struct hpp_sort_entry *hse;
2754 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2755 if (!perf_hpp__is_sort_entry(fmt))
2758 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2759 fmt->elide = get_elide(hse->se->se_width_idx, output);
2763 * It makes no sense to elide all of sort entries.
2764 * Just revert them to show up again.
2766 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2767 if (!perf_hpp__is_sort_entry(fmt))
2774 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2775 if (!perf_hpp__is_sort_entry(fmt))
2782 int output_field_add(struct perf_hpp_list *list, char *tok)
2786 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2787 struct sort_dimension *sd = &common_sort_dimensions[i];
2789 if (strncasecmp(tok, sd->name, strlen(tok)))
2792 return __sort_dimension__add_output(list, sd);
2795 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2796 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2798 if (strncasecmp(tok, hd->name, strlen(tok)))
2801 return __hpp_dimension__add_output(list, hd);
2804 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2805 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2807 if (strncasecmp(tok, sd->name, strlen(tok)))
2810 return __sort_dimension__add_output(list, sd);
2813 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2814 struct sort_dimension *sd = &memory_sort_dimensions[i];
2816 if (strncasecmp(tok, sd->name, strlen(tok)))
2819 return __sort_dimension__add_output(list, sd);
2825 static int setup_output_list(struct perf_hpp_list *list, char *str)
2830 for (tok = strtok_r(str, ", ", &tmp);
2831 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2832 ret = output_field_add(list, tok);
2833 if (ret == -EINVAL) {
2834 error("Invalid --fields key: `%s'", tok);
2836 } else if (ret == -ESRCH) {
2837 error("Unknown --fields key: `%s'", tok);
2845 void reset_dimensions(void)
2849 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2850 common_sort_dimensions[i].taken = 0;
2852 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2853 hpp_sort_dimensions[i].taken = 0;
2855 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2856 bstack_sort_dimensions[i].taken = 0;
2858 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2859 memory_sort_dimensions[i].taken = 0;
2862 bool is_strict_order(const char *order)
2864 return order && (*order != '+');
2867 static int __setup_output_field(void)
2872 if (field_order == NULL)
2875 strp = str = strdup(field_order);
2877 error("Not enough memory to setup output fields");
2881 if (!is_strict_order(field_order))
2884 if (!strlen(strp)) {
2885 error("Invalid --fields key: `+'");
2889 ret = setup_output_list(&perf_hpp_list, strp);
2896 int setup_sorting(struct perf_evlist *evlist)
2900 err = __setup_sorting(evlist);
2904 if (parent_pattern != default_parent_pattern) {
2905 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
2913 * perf diff doesn't use default hpp output fields.
2915 if (sort__mode != SORT_MODE__DIFF)
2918 err = __setup_output_field();
2922 /* copy sort keys to output fields */
2923 perf_hpp__setup_output_field(&perf_hpp_list);
2924 /* and then copy output fields to sort keys */
2925 perf_hpp__append_sort_keys(&perf_hpp_list);
2927 /* setup hists-specific output fields */
2928 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2934 void reset_output_field(void)
2936 perf_hpp_list.need_collapse = 0;
2937 perf_hpp_list.parent = 0;
2938 perf_hpp_list.sym = 0;
2939 perf_hpp_list.dso = 0;
2945 perf_hpp__reset_output_field(&perf_hpp_list);