1 // SPDX-License-Identifier: GPL-2.0
14 #include <traceevent/event-parse.h>
15 #include "mem-events.h"
16 #include <linux/kernel.h>
19 const char default_parent_pattern[] = "^sys_|^do_page_fault";
20 const char *parent_pattern = default_parent_pattern;
21 const char *default_sort_order = "comm,dso,symbol";
22 const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
23 const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
24 const char default_top_sort_order[] = "dso,symbol";
25 const char default_diff_sort_order[] = "dso,symbol";
26 const char default_tracepoint_sort_order[] = "trace";
27 const char *sort_order;
28 const char *field_order;
29 regex_t ignore_callees_regex;
30 int have_ignore_callees = 0;
31 enum sort_mode sort__mode = SORT_MODE__NORMAL;
34 * Replaces all occurrences of a char used with the:
36 * -t, --field-separator
38 * option, that uses a special separator character and don't pad with spaces,
39 * replacing all occurances of this separator in symbol names (and other
40 * output) with a '.' character, that thus it's the only non valid separator.
42 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
48 n = vsnprintf(bf, size, fmt, ap);
49 if (symbol_conf.field_sep && n > 0) {
53 sep = strchr(sep, *symbol_conf.field_sep);
66 static int64_t cmp_null(const void *l, const void *r)
79 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
81 return right->thread->tid - left->thread->tid;
84 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
85 size_t size, unsigned int width)
87 const char *comm = thread__comm_str(he->thread);
89 width = max(7U, width) - 8;
90 return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid,
91 width, width, comm ?: "");
94 static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
96 const struct thread *th = arg;
98 if (type != HIST_FILTER__THREAD)
101 return th && he->thread != th;
104 struct sort_entry sort_thread = {
105 .se_header = " Pid:Command",
106 .se_cmp = sort__thread_cmp,
107 .se_snprintf = hist_entry__thread_snprintf,
108 .se_filter = hist_entry__thread_filter,
109 .se_width_idx = HISTC_THREAD,
115 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
117 /* Compare the addr that should be unique among comm */
118 return strcmp(comm__str(right->comm), comm__str(left->comm));
122 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
124 /* Compare the addr that should be unique among comm */
125 return strcmp(comm__str(right->comm), comm__str(left->comm));
129 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
131 return strcmp(comm__str(right->comm), comm__str(left->comm));
134 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
135 size_t size, unsigned int width)
137 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
140 struct sort_entry sort_comm = {
141 .se_header = "Command",
142 .se_cmp = sort__comm_cmp,
143 .se_collapse = sort__comm_collapse,
144 .se_sort = sort__comm_sort,
145 .se_snprintf = hist_entry__comm_snprintf,
146 .se_filter = hist_entry__thread_filter,
147 .se_width_idx = HISTC_COMM,
152 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
154 struct dso *dso_l = map_l ? map_l->dso : NULL;
155 struct dso *dso_r = map_r ? map_r->dso : NULL;
156 const char *dso_name_l, *dso_name_r;
158 if (!dso_l || !dso_r)
159 return cmp_null(dso_r, dso_l);
162 dso_name_l = dso_l->long_name;
163 dso_name_r = dso_r->long_name;
165 dso_name_l = dso_l->short_name;
166 dso_name_r = dso_r->short_name;
169 return strcmp(dso_name_l, dso_name_r);
173 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
175 return _sort__dso_cmp(right->ms.map, left->ms.map);
178 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
179 size_t size, unsigned int width)
181 if (map && map->dso) {
182 const char *dso_name = verbose > 0 ? map->dso->long_name :
183 map->dso->short_name;
184 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
187 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
190 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
191 size_t size, unsigned int width)
193 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
196 static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
198 const struct dso *dso = arg;
200 if (type != HIST_FILTER__DSO)
203 return dso && (!he->ms.map || he->ms.map->dso != dso);
206 struct sort_entry sort_dso = {
207 .se_header = "Shared Object",
208 .se_cmp = sort__dso_cmp,
209 .se_snprintf = hist_entry__dso_snprintf,
210 .se_filter = hist_entry__dso_filter,
211 .se_width_idx = HISTC_DSO,
216 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
218 return (int64_t)(right_ip - left_ip);
221 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
223 if (!sym_l || !sym_r)
224 return cmp_null(sym_l, sym_r);
229 if (sym_l->start != sym_r->start)
230 return (int64_t)(sym_r->start - sym_l->start);
232 return (int64_t)(sym_r->end - sym_l->end);
236 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
240 if (!left->ms.sym && !right->ms.sym)
241 return _sort__addr_cmp(left->ip, right->ip);
244 * comparing symbol address alone is not enough since it's a
245 * relative address within a dso.
247 if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
248 ret = sort__dso_cmp(left, right);
253 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
257 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
259 if (!left->ms.sym || !right->ms.sym)
260 return cmp_null(left->ms.sym, right->ms.sym);
262 return strcmp(right->ms.sym->name, left->ms.sym->name);
265 static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
266 u64 ip, char level, char *bf, size_t size,
272 char o = map ? dso__symtab_origin(map->dso) : '!';
273 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
274 BITS_PER_LONG / 4 + 2, ip, o);
277 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
279 if (map->type == MAP__VARIABLE) {
280 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
281 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
282 ip - map->unmap_ip(map, sym->start));
284 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
289 size_t len = BITS_PER_LONG / 4;
290 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
297 static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
298 size_t size, unsigned int width)
300 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
301 he->level, bf, size, width);
304 static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
306 const char *sym = arg;
308 if (type != HIST_FILTER__SYMBOL)
311 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
314 struct sort_entry sort_sym = {
315 .se_header = "Symbol",
316 .se_cmp = sort__sym_cmp,
317 .se_sort = sort__sym_sort,
318 .se_snprintf = hist_entry__sym_snprintf,
319 .se_filter = hist_entry__sym_filter,
320 .se_width_idx = HISTC_SYMBOL,
325 char *hist_entry__get_srcline(struct hist_entry *he)
327 struct map *map = he->ms.map;
330 return SRCLINE_UNKNOWN;
332 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
333 he->ms.sym, true, true);
337 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
340 left->srcline = hist_entry__get_srcline(left);
342 right->srcline = hist_entry__get_srcline(right);
344 return strcmp(right->srcline, left->srcline);
347 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
348 size_t size, unsigned int width)
351 he->srcline = hist_entry__get_srcline(he);
353 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
356 struct sort_entry sort_srcline = {
357 .se_header = "Source:Line",
358 .se_cmp = sort__srcline_cmp,
359 .se_snprintf = hist_entry__srcline_snprintf,
360 .se_width_idx = HISTC_SRCLINE,
363 /* --sort srcline_from */
366 sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
368 if (!left->branch_info->srcline_from) {
369 struct map *map = left->branch_info->from.map;
371 left->branch_info->srcline_from = SRCLINE_UNKNOWN;
373 left->branch_info->srcline_from = get_srcline(map->dso,
374 map__rip_2objdump(map,
375 left->branch_info->from.al_addr),
376 left->branch_info->from.sym,
379 if (!right->branch_info->srcline_from) {
380 struct map *map = right->branch_info->from.map;
382 right->branch_info->srcline_from = SRCLINE_UNKNOWN;
384 right->branch_info->srcline_from = get_srcline(map->dso,
385 map__rip_2objdump(map,
386 right->branch_info->from.al_addr),
387 right->branch_info->from.sym,
390 return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
393 static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
394 size_t size, unsigned int width)
396 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
399 struct sort_entry sort_srcline_from = {
400 .se_header = "From Source:Line",
401 .se_cmp = sort__srcline_from_cmp,
402 .se_snprintf = hist_entry__srcline_from_snprintf,
403 .se_width_idx = HISTC_SRCLINE_FROM,
406 /* --sort srcline_to */
409 sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
411 if (!left->branch_info->srcline_to) {
412 struct map *map = left->branch_info->to.map;
414 left->branch_info->srcline_to = SRCLINE_UNKNOWN;
416 left->branch_info->srcline_to = get_srcline(map->dso,
417 map__rip_2objdump(map,
418 left->branch_info->to.al_addr),
419 left->branch_info->from.sym,
422 if (!right->branch_info->srcline_to) {
423 struct map *map = right->branch_info->to.map;
425 right->branch_info->srcline_to = SRCLINE_UNKNOWN;
427 right->branch_info->srcline_to = get_srcline(map->dso,
428 map__rip_2objdump(map,
429 right->branch_info->to.al_addr),
430 right->branch_info->to.sym,
433 return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
436 static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
437 size_t size, unsigned int width)
439 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
442 struct sort_entry sort_srcline_to = {
443 .se_header = "To Source:Line",
444 .se_cmp = sort__srcline_to_cmp,
445 .se_snprintf = hist_entry__srcline_to_snprintf,
446 .se_width_idx = HISTC_SRCLINE_TO,
451 static char no_srcfile[1];
453 static char *hist_entry__get_srcfile(struct hist_entry *e)
456 struct map *map = e->ms.map;
461 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
462 e->ms.sym, false, true, true);
463 if (!strcmp(sf, SRCLINE_UNKNOWN))
475 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
478 left->srcfile = hist_entry__get_srcfile(left);
480 right->srcfile = hist_entry__get_srcfile(right);
482 return strcmp(right->srcfile, left->srcfile);
485 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
486 size_t size, unsigned int width)
489 he->srcfile = hist_entry__get_srcfile(he);
491 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
494 struct sort_entry sort_srcfile = {
495 .se_header = "Source File",
496 .se_cmp = sort__srcfile_cmp,
497 .se_snprintf = hist_entry__srcfile_snprintf,
498 .se_width_idx = HISTC_SRCFILE,
504 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
506 struct symbol *sym_l = left->parent;
507 struct symbol *sym_r = right->parent;
509 if (!sym_l || !sym_r)
510 return cmp_null(sym_l, sym_r);
512 return strcmp(sym_r->name, sym_l->name);
515 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
516 size_t size, unsigned int width)
518 return repsep_snprintf(bf, size, "%-*.*s", width, width,
519 he->parent ? he->parent->name : "[other]");
522 struct sort_entry sort_parent = {
523 .se_header = "Parent symbol",
524 .se_cmp = sort__parent_cmp,
525 .se_snprintf = hist_entry__parent_snprintf,
526 .se_width_idx = HISTC_PARENT,
532 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
534 return right->cpu - left->cpu;
537 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
538 size_t size, unsigned int width)
540 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
543 struct sort_entry sort_cpu = {
545 .se_cmp = sort__cpu_cmp,
546 .se_snprintf = hist_entry__cpu_snprintf,
547 .se_width_idx = HISTC_CPU,
550 /* --sort cgroup_id */
552 static int64_t _sort__cgroup_dev_cmp(u64 left_dev, u64 right_dev)
554 return (int64_t)(right_dev - left_dev);
557 static int64_t _sort__cgroup_inode_cmp(u64 left_ino, u64 right_ino)
559 return (int64_t)(right_ino - left_ino);
563 sort__cgroup_id_cmp(struct hist_entry *left, struct hist_entry *right)
567 ret = _sort__cgroup_dev_cmp(right->cgroup_id.dev, left->cgroup_id.dev);
571 return _sort__cgroup_inode_cmp(right->cgroup_id.ino,
572 left->cgroup_id.ino);
575 static int hist_entry__cgroup_id_snprintf(struct hist_entry *he,
576 char *bf, size_t size,
577 unsigned int width __maybe_unused)
579 return repsep_snprintf(bf, size, "%lu/0x%lx", he->cgroup_id.dev,
583 struct sort_entry sort_cgroup_id = {
584 .se_header = "cgroup id (dev/inode)",
585 .se_cmp = sort__cgroup_id_cmp,
586 .se_snprintf = hist_entry__cgroup_id_snprintf,
587 .se_width_idx = HISTC_CGROUP_ID,
593 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
595 return right->socket - left->socket;
598 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
599 size_t size, unsigned int width)
601 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
604 static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
606 int sk = *(const int *)arg;
608 if (type != HIST_FILTER__SOCKET)
611 return sk >= 0 && he->socket != sk;
614 struct sort_entry sort_socket = {
615 .se_header = "Socket",
616 .se_cmp = sort__socket_cmp,
617 .se_snprintf = hist_entry__socket_snprintf,
618 .se_filter = hist_entry__socket_filter,
619 .se_width_idx = HISTC_SOCKET,
624 static char *get_trace_output(struct hist_entry *he)
626 struct trace_seq seq;
627 struct perf_evsel *evsel;
628 struct pevent_record rec = {
629 .data = he->raw_data,
630 .size = he->raw_size,
633 evsel = hists_to_evsel(he->hists);
635 trace_seq_init(&seq);
636 if (symbol_conf.raw_trace) {
637 pevent_print_fields(&seq, he->raw_data, he->raw_size,
640 pevent_event_info(&seq, evsel->tp_format, &rec);
643 * Trim the buffer, it starts at 4KB and we're not going to
644 * add anything more to this buffer.
646 return realloc(seq.buffer, seq.len + 1);
650 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
652 struct perf_evsel *evsel;
654 evsel = hists_to_evsel(left->hists);
655 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
658 if (left->trace_output == NULL)
659 left->trace_output = get_trace_output(left);
660 if (right->trace_output == NULL)
661 right->trace_output = get_trace_output(right);
663 return strcmp(right->trace_output, left->trace_output);
666 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
667 size_t size, unsigned int width)
669 struct perf_evsel *evsel;
671 evsel = hists_to_evsel(he->hists);
672 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
673 return scnprintf(bf, size, "%-.*s", width, "N/A");
675 if (he->trace_output == NULL)
676 he->trace_output = get_trace_output(he);
677 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
680 struct sort_entry sort_trace = {
681 .se_header = "Trace output",
682 .se_cmp = sort__trace_cmp,
683 .se_snprintf = hist_entry__trace_snprintf,
684 .se_width_idx = HISTC_TRACE,
687 /* sort keys for branch stacks */
690 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
692 if (!left->branch_info || !right->branch_info)
693 return cmp_null(left->branch_info, right->branch_info);
695 return _sort__dso_cmp(left->branch_info->from.map,
696 right->branch_info->from.map);
699 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
700 size_t size, unsigned int width)
703 return _hist_entry__dso_snprintf(he->branch_info->from.map,
706 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
709 static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
712 const struct dso *dso = arg;
714 if (type != HIST_FILTER__DSO)
717 return dso && (!he->branch_info || !he->branch_info->from.map ||
718 he->branch_info->from.map->dso != dso);
722 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
724 if (!left->branch_info || !right->branch_info)
725 return cmp_null(left->branch_info, right->branch_info);
727 return _sort__dso_cmp(left->branch_info->to.map,
728 right->branch_info->to.map);
731 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
732 size_t size, unsigned int width)
735 return _hist_entry__dso_snprintf(he->branch_info->to.map,
738 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
741 static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
744 const struct dso *dso = arg;
746 if (type != HIST_FILTER__DSO)
749 return dso && (!he->branch_info || !he->branch_info->to.map ||
750 he->branch_info->to.map->dso != dso);
754 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
756 struct addr_map_symbol *from_l, *from_r;
758 if (!left->branch_info || !right->branch_info)
759 return cmp_null(left->branch_info, right->branch_info);
761 from_l = &left->branch_info->from;
762 from_r = &right->branch_info->from;
764 if (!from_l->sym && !from_r->sym)
765 return _sort__addr_cmp(from_l->addr, from_r->addr);
767 return _sort__sym_cmp(from_l->sym, from_r->sym);
771 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
773 struct addr_map_symbol *to_l, *to_r;
775 if (!left->branch_info || !right->branch_info)
776 return cmp_null(left->branch_info, right->branch_info);
778 to_l = &left->branch_info->to;
779 to_r = &right->branch_info->to;
781 if (!to_l->sym && !to_r->sym)
782 return _sort__addr_cmp(to_l->addr, to_r->addr);
784 return _sort__sym_cmp(to_l->sym, to_r->sym);
787 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
788 size_t size, unsigned int width)
790 if (he->branch_info) {
791 struct addr_map_symbol *from = &he->branch_info->from;
793 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
794 he->level, bf, size, width);
797 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
800 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
801 size_t size, unsigned int width)
803 if (he->branch_info) {
804 struct addr_map_symbol *to = &he->branch_info->to;
806 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
807 he->level, bf, size, width);
810 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
813 static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
816 const char *sym = arg;
818 if (type != HIST_FILTER__SYMBOL)
821 return sym && !(he->branch_info && he->branch_info->from.sym &&
822 strstr(he->branch_info->from.sym->name, sym));
825 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
828 const char *sym = arg;
830 if (type != HIST_FILTER__SYMBOL)
833 return sym && !(he->branch_info && he->branch_info->to.sym &&
834 strstr(he->branch_info->to.sym->name, sym));
837 struct sort_entry sort_dso_from = {
838 .se_header = "Source Shared Object",
839 .se_cmp = sort__dso_from_cmp,
840 .se_snprintf = hist_entry__dso_from_snprintf,
841 .se_filter = hist_entry__dso_from_filter,
842 .se_width_idx = HISTC_DSO_FROM,
845 struct sort_entry sort_dso_to = {
846 .se_header = "Target Shared Object",
847 .se_cmp = sort__dso_to_cmp,
848 .se_snprintf = hist_entry__dso_to_snprintf,
849 .se_filter = hist_entry__dso_to_filter,
850 .se_width_idx = HISTC_DSO_TO,
853 struct sort_entry sort_sym_from = {
854 .se_header = "Source Symbol",
855 .se_cmp = sort__sym_from_cmp,
856 .se_snprintf = hist_entry__sym_from_snprintf,
857 .se_filter = hist_entry__sym_from_filter,
858 .se_width_idx = HISTC_SYMBOL_FROM,
861 struct sort_entry sort_sym_to = {
862 .se_header = "Target Symbol",
863 .se_cmp = sort__sym_to_cmp,
864 .se_snprintf = hist_entry__sym_to_snprintf,
865 .se_filter = hist_entry__sym_to_filter,
866 .se_width_idx = HISTC_SYMBOL_TO,
870 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
874 if (!left->branch_info || !right->branch_info)
875 return cmp_null(left->branch_info, right->branch_info);
877 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
878 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
882 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
883 size_t size, unsigned int width){
884 static const char *out = "N/A";
886 if (he->branch_info) {
887 if (he->branch_info->flags.predicted)
889 else if (he->branch_info->flags.mispred)
893 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
897 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
899 if (!left->branch_info || !right->branch_info)
900 return cmp_null(left->branch_info, right->branch_info);
902 return left->branch_info->flags.cycles -
903 right->branch_info->flags.cycles;
906 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
907 size_t size, unsigned int width)
909 if (!he->branch_info)
910 return scnprintf(bf, size, "%-.*s", width, "N/A");
911 if (he->branch_info->flags.cycles == 0)
912 return repsep_snprintf(bf, size, "%-*s", width, "-");
913 return repsep_snprintf(bf, size, "%-*hd", width,
914 he->branch_info->flags.cycles);
917 struct sort_entry sort_cycles = {
918 .se_header = "Basic Block Cycles",
919 .se_cmp = sort__cycles_cmp,
920 .se_snprintf = hist_entry__cycles_snprintf,
921 .se_width_idx = HISTC_CYCLES,
924 /* --sort daddr_sym */
926 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
928 uint64_t l = 0, r = 0;
931 l = left->mem_info->daddr.addr;
933 r = right->mem_info->daddr.addr;
935 return (int64_t)(r - l);
938 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
939 size_t size, unsigned int width)
942 struct map *map = NULL;
943 struct symbol *sym = NULL;
946 addr = he->mem_info->daddr.addr;
947 map = he->mem_info->daddr.map;
948 sym = he->mem_info->daddr.sym;
950 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
955 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
957 uint64_t l = 0, r = 0;
960 l = left->mem_info->iaddr.addr;
962 r = right->mem_info->iaddr.addr;
964 return (int64_t)(r - l);
967 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
968 size_t size, unsigned int width)
971 struct map *map = NULL;
972 struct symbol *sym = NULL;
975 addr = he->mem_info->iaddr.addr;
976 map = he->mem_info->iaddr.map;
977 sym = he->mem_info->iaddr.sym;
979 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
984 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
986 struct map *map_l = NULL;
987 struct map *map_r = NULL;
990 map_l = left->mem_info->daddr.map;
992 map_r = right->mem_info->daddr.map;
994 return _sort__dso_cmp(map_l, map_r);
997 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
998 size_t size, unsigned int width)
1000 struct map *map = NULL;
1003 map = he->mem_info->daddr.map;
1005 return _hist_entry__dso_snprintf(map, bf, size, width);
1009 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
1011 union perf_mem_data_src data_src_l;
1012 union perf_mem_data_src data_src_r;
1015 data_src_l = left->mem_info->data_src;
1017 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
1019 if (right->mem_info)
1020 data_src_r = right->mem_info->data_src;
1022 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
1024 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
1027 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
1028 size_t size, unsigned int width)
1032 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
1033 return repsep_snprintf(bf, size, "%.*s", width, out);
1037 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
1039 union perf_mem_data_src data_src_l;
1040 union perf_mem_data_src data_src_r;
1043 data_src_l = left->mem_info->data_src;
1045 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
1047 if (right->mem_info)
1048 data_src_r = right->mem_info->data_src;
1050 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
1052 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
1055 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
1056 size_t size, unsigned int width)
1060 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
1061 return repsep_snprintf(bf, size, "%-*s", width, out);
1065 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
1067 union perf_mem_data_src data_src_l;
1068 union perf_mem_data_src data_src_r;
1071 data_src_l = left->mem_info->data_src;
1073 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
1075 if (right->mem_info)
1076 data_src_r = right->mem_info->data_src;
1078 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
1080 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
1083 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
1084 size_t size, unsigned int width)
1088 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
1089 return repsep_snprintf(bf, size, "%-*s", width, out);
1093 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
1095 union perf_mem_data_src data_src_l;
1096 union perf_mem_data_src data_src_r;
1099 data_src_l = left->mem_info->data_src;
1101 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
1103 if (right->mem_info)
1104 data_src_r = right->mem_info->data_src;
1106 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
1108 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
1111 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
1112 size_t size, unsigned int width)
1116 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
1117 return repsep_snprintf(bf, size, "%-*s", width, out);
1121 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
1124 struct map *l_map, *r_map;
1126 if (!left->mem_info) return -1;
1127 if (!right->mem_info) return 1;
1129 /* group event types together */
1130 if (left->cpumode > right->cpumode) return -1;
1131 if (left->cpumode < right->cpumode) return 1;
1133 l_map = left->mem_info->daddr.map;
1134 r_map = right->mem_info->daddr.map;
1136 /* if both are NULL, jump to sort on al_addr instead */
1137 if (!l_map && !r_map)
1140 if (!l_map) return -1;
1141 if (!r_map) return 1;
1143 if (l_map->maj > r_map->maj) return -1;
1144 if (l_map->maj < r_map->maj) return 1;
1146 if (l_map->min > r_map->min) return -1;
1147 if (l_map->min < r_map->min) return 1;
1149 if (l_map->ino > r_map->ino) return -1;
1150 if (l_map->ino < r_map->ino) return 1;
1152 if (l_map->ino_generation > r_map->ino_generation) return -1;
1153 if (l_map->ino_generation < r_map->ino_generation) return 1;
1156 * Addresses with no major/minor numbers are assumed to be
1157 * anonymous in userspace. Sort those on pid then address.
1159 * The kernel and non-zero major/minor mapped areas are
1160 * assumed to be unity mapped. Sort those on address.
1163 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1164 (!(l_map->flags & MAP_SHARED)) &&
1165 !l_map->maj && !l_map->min && !l_map->ino &&
1166 !l_map->ino_generation) {
1167 /* userspace anonymous */
1169 if (left->thread->pid_ > right->thread->pid_) return -1;
1170 if (left->thread->pid_ < right->thread->pid_) return 1;
1174 /* al_addr does all the right addr - start + offset calculations */
1175 l = cl_address(left->mem_info->daddr.al_addr);
1176 r = cl_address(right->mem_info->daddr.al_addr);
1178 if (l > r) return -1;
1179 if (l < r) return 1;
1184 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1185 size_t size, unsigned int width)
1189 struct map *map = NULL;
1190 struct symbol *sym = NULL;
1191 char level = he->level;
1194 addr = cl_address(he->mem_info->daddr.al_addr);
1195 map = he->mem_info->daddr.map;
1196 sym = he->mem_info->daddr.sym;
1198 /* print [s] for shared data mmaps */
1199 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1200 map && (map->type == MAP__VARIABLE) &&
1201 (map->flags & MAP_SHARED) &&
1202 (map->maj || map->min || map->ino ||
1203 map->ino_generation))
1208 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1212 struct sort_entry sort_mispredict = {
1213 .se_header = "Branch Mispredicted",
1214 .se_cmp = sort__mispredict_cmp,
1215 .se_snprintf = hist_entry__mispredict_snprintf,
1216 .se_width_idx = HISTC_MISPREDICT,
1219 static u64 he_weight(struct hist_entry *he)
1221 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1225 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1227 return he_weight(left) - he_weight(right);
1230 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1231 size_t size, unsigned int width)
1233 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1236 struct sort_entry sort_local_weight = {
1237 .se_header = "Local Weight",
1238 .se_cmp = sort__local_weight_cmp,
1239 .se_snprintf = hist_entry__local_weight_snprintf,
1240 .se_width_idx = HISTC_LOCAL_WEIGHT,
1244 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1246 return left->stat.weight - right->stat.weight;
1249 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1250 size_t size, unsigned int width)
1252 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1255 struct sort_entry sort_global_weight = {
1256 .se_header = "Weight",
1257 .se_cmp = sort__global_weight_cmp,
1258 .se_snprintf = hist_entry__global_weight_snprintf,
1259 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1262 struct sort_entry sort_mem_daddr_sym = {
1263 .se_header = "Data Symbol",
1264 .se_cmp = sort__daddr_cmp,
1265 .se_snprintf = hist_entry__daddr_snprintf,
1266 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1269 struct sort_entry sort_mem_iaddr_sym = {
1270 .se_header = "Code Symbol",
1271 .se_cmp = sort__iaddr_cmp,
1272 .se_snprintf = hist_entry__iaddr_snprintf,
1273 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1276 struct sort_entry sort_mem_daddr_dso = {
1277 .se_header = "Data Object",
1278 .se_cmp = sort__dso_daddr_cmp,
1279 .se_snprintf = hist_entry__dso_daddr_snprintf,
1280 .se_width_idx = HISTC_MEM_DADDR_DSO,
1283 struct sort_entry sort_mem_locked = {
1284 .se_header = "Locked",
1285 .se_cmp = sort__locked_cmp,
1286 .se_snprintf = hist_entry__locked_snprintf,
1287 .se_width_idx = HISTC_MEM_LOCKED,
1290 struct sort_entry sort_mem_tlb = {
1291 .se_header = "TLB access",
1292 .se_cmp = sort__tlb_cmp,
1293 .se_snprintf = hist_entry__tlb_snprintf,
1294 .se_width_idx = HISTC_MEM_TLB,
1297 struct sort_entry sort_mem_lvl = {
1298 .se_header = "Memory access",
1299 .se_cmp = sort__lvl_cmp,
1300 .se_snprintf = hist_entry__lvl_snprintf,
1301 .se_width_idx = HISTC_MEM_LVL,
1304 struct sort_entry sort_mem_snoop = {
1305 .se_header = "Snoop",
1306 .se_cmp = sort__snoop_cmp,
1307 .se_snprintf = hist_entry__snoop_snprintf,
1308 .se_width_idx = HISTC_MEM_SNOOP,
1311 struct sort_entry sort_mem_dcacheline = {
1312 .se_header = "Data Cacheline",
1313 .se_cmp = sort__dcacheline_cmp,
1314 .se_snprintf = hist_entry__dcacheline_snprintf,
1315 .se_width_idx = HISTC_MEM_DCACHELINE,
1319 sort__phys_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1321 uint64_t l = 0, r = 0;
1324 l = left->mem_info->daddr.phys_addr;
1325 if (right->mem_info)
1326 r = right->mem_info->daddr.phys_addr;
1328 return (int64_t)(r - l);
1331 static int hist_entry__phys_daddr_snprintf(struct hist_entry *he, char *bf,
1332 size_t size, unsigned int width)
1336 size_t len = BITS_PER_LONG / 4;
1338 addr = he->mem_info->daddr.phys_addr;
1340 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", he->level);
1342 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", len, addr);
1344 ret += repsep_snprintf(bf + ret, size - ret, "%-*s", width - ret, "");
1352 struct sort_entry sort_mem_phys_daddr = {
1353 .se_header = "Data Physical Address",
1354 .se_cmp = sort__phys_daddr_cmp,
1355 .se_snprintf = hist_entry__phys_daddr_snprintf,
1356 .se_width_idx = HISTC_MEM_PHYS_DADDR,
1360 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1362 if (!left->branch_info || !right->branch_info)
1363 return cmp_null(left->branch_info, right->branch_info);
1365 return left->branch_info->flags.abort !=
1366 right->branch_info->flags.abort;
1369 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1370 size_t size, unsigned int width)
1372 static const char *out = "N/A";
1374 if (he->branch_info) {
1375 if (he->branch_info->flags.abort)
1381 return repsep_snprintf(bf, size, "%-*s", width, out);
1384 struct sort_entry sort_abort = {
1385 .se_header = "Transaction abort",
1386 .se_cmp = sort__abort_cmp,
1387 .se_snprintf = hist_entry__abort_snprintf,
1388 .se_width_idx = HISTC_ABORT,
1392 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1394 if (!left->branch_info || !right->branch_info)
1395 return cmp_null(left->branch_info, right->branch_info);
1397 return left->branch_info->flags.in_tx !=
1398 right->branch_info->flags.in_tx;
1401 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1402 size_t size, unsigned int width)
1404 static const char *out = "N/A";
1406 if (he->branch_info) {
1407 if (he->branch_info->flags.in_tx)
1413 return repsep_snprintf(bf, size, "%-*s", width, out);
1416 struct sort_entry sort_in_tx = {
1417 .se_header = "Branch in transaction",
1418 .se_cmp = sort__in_tx_cmp,
1419 .se_snprintf = hist_entry__in_tx_snprintf,
1420 .se_width_idx = HISTC_IN_TX,
1424 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1426 return left->transaction - right->transaction;
1429 static inline char *add_str(char *p, const char *str)
1432 return p + strlen(str);
1435 static struct txbit {
1440 { PERF_TXN_ELISION, "EL ", 0 },
1441 { PERF_TXN_TRANSACTION, "TX ", 1 },
1442 { PERF_TXN_SYNC, "SYNC ", 1 },
1443 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1444 { PERF_TXN_RETRY, "RETRY ", 0 },
1445 { PERF_TXN_CONFLICT, "CON ", 0 },
1446 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1447 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1451 int hist_entry__transaction_len(void)
1456 for (i = 0; txbits[i].name; i++) {
1457 if (!txbits[i].skip_for_len)
1458 len += strlen(txbits[i].name);
1460 len += 4; /* :XX<space> */
1464 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1465 size_t size, unsigned int width)
1467 u64 t = he->transaction;
1473 for (i = 0; txbits[i].name; i++)
1474 if (txbits[i].flag & t)
1475 p = add_str(p, txbits[i].name);
1476 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1477 p = add_str(p, "NEITHER ");
1478 if (t & PERF_TXN_ABORT_MASK) {
1479 sprintf(p, ":%" PRIx64,
1480 (t & PERF_TXN_ABORT_MASK) >>
1481 PERF_TXN_ABORT_SHIFT);
1485 return repsep_snprintf(bf, size, "%-*s", width, buf);
1488 struct sort_entry sort_transaction = {
1489 .se_header = "Transaction ",
1490 .se_cmp = sort__transaction_cmp,
1491 .se_snprintf = hist_entry__transaction_snprintf,
1492 .se_width_idx = HISTC_TRANSACTION,
1495 /* --sort symbol_size */
1497 static int64_t _sort__sym_size_cmp(struct symbol *sym_l, struct symbol *sym_r)
1499 int64_t size_l = sym_l != NULL ? symbol__size(sym_l) : 0;
1500 int64_t size_r = sym_r != NULL ? symbol__size(sym_r) : 0;
1502 return size_l < size_r ? -1 :
1503 size_l == size_r ? 0 : 1;
1507 sort__sym_size_cmp(struct hist_entry *left, struct hist_entry *right)
1509 return _sort__sym_size_cmp(right->ms.sym, left->ms.sym);
1512 static int _hist_entry__sym_size_snprintf(struct symbol *sym, char *bf,
1513 size_t bf_size, unsigned int width)
1516 return repsep_snprintf(bf, bf_size, "%*d", width, symbol__size(sym));
1518 return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1521 static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
1522 size_t size, unsigned int width)
1524 return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
1527 struct sort_entry sort_sym_size = {
1528 .se_header = "Symbol size",
1529 .se_cmp = sort__sym_size_cmp,
1530 .se_snprintf = hist_entry__sym_size_snprintf,
1531 .se_width_idx = HISTC_SYM_SIZE,
1535 struct sort_dimension {
1537 struct sort_entry *entry;
1541 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1543 static struct sort_dimension common_sort_dimensions[] = {
1544 DIM(SORT_PID, "pid", sort_thread),
1545 DIM(SORT_COMM, "comm", sort_comm),
1546 DIM(SORT_DSO, "dso", sort_dso),
1547 DIM(SORT_SYM, "symbol", sort_sym),
1548 DIM(SORT_PARENT, "parent", sort_parent),
1549 DIM(SORT_CPU, "cpu", sort_cpu),
1550 DIM(SORT_SOCKET, "socket", sort_socket),
1551 DIM(SORT_SRCLINE, "srcline", sort_srcline),
1552 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1553 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1554 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1555 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1556 DIM(SORT_TRACE, "trace", sort_trace),
1557 DIM(SORT_SYM_SIZE, "symbol_size", sort_sym_size),
1558 DIM(SORT_CGROUP_ID, "cgroup_id", sort_cgroup_id),
1563 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1565 static struct sort_dimension bstack_sort_dimensions[] = {
1566 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1567 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1568 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1569 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1570 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1571 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1572 DIM(SORT_ABORT, "abort", sort_abort),
1573 DIM(SORT_CYCLES, "cycles", sort_cycles),
1574 DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
1575 DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
1580 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1582 static struct sort_dimension memory_sort_dimensions[] = {
1583 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1584 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1585 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1586 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1587 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1588 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1589 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1590 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1591 DIM(SORT_MEM_PHYS_DADDR, "phys_daddr", sort_mem_phys_daddr),
1596 struct hpp_dimension {
1598 struct perf_hpp_fmt *fmt;
1602 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1604 static struct hpp_dimension hpp_sort_dimensions[] = {
1605 DIM(PERF_HPP__OVERHEAD, "overhead"),
1606 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1607 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1608 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1609 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1610 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1611 DIM(PERF_HPP__SAMPLES, "sample"),
1612 DIM(PERF_HPP__PERIOD, "period"),
1617 struct hpp_sort_entry {
1618 struct perf_hpp_fmt hpp;
1619 struct sort_entry *se;
1622 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1624 struct hpp_sort_entry *hse;
1626 if (!perf_hpp__is_sort_entry(fmt))
1629 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1630 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1633 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1634 struct hists *hists, int line __maybe_unused,
1635 int *span __maybe_unused)
1637 struct hpp_sort_entry *hse;
1638 size_t len = fmt->user_len;
1640 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1643 len = hists__col_len(hists, hse->se->se_width_idx);
1645 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1648 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1649 struct perf_hpp *hpp __maybe_unused,
1650 struct hists *hists)
1652 struct hpp_sort_entry *hse;
1653 size_t len = fmt->user_len;
1655 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1658 len = hists__col_len(hists, hse->se->se_width_idx);
1663 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1664 struct hist_entry *he)
1666 struct hpp_sort_entry *hse;
1667 size_t len = fmt->user_len;
1669 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1672 len = hists__col_len(he->hists, hse->se->se_width_idx);
1674 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1677 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1678 struct hist_entry *a, struct hist_entry *b)
1680 struct hpp_sort_entry *hse;
1682 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1683 return hse->se->se_cmp(a, b);
1686 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1687 struct hist_entry *a, struct hist_entry *b)
1689 struct hpp_sort_entry *hse;
1690 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1692 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1693 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1694 return collapse_fn(a, b);
1697 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1698 struct hist_entry *a, struct hist_entry *b)
1700 struct hpp_sort_entry *hse;
1701 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1703 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1704 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1705 return sort_fn(a, b);
1708 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1710 return format->header == __sort__hpp_header;
1713 #define MK_SORT_ENTRY_CHK(key) \
1714 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1716 struct hpp_sort_entry *hse; \
1718 if (!perf_hpp__is_sort_entry(fmt)) \
1721 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1722 return hse->se == &sort_ ## key ; \
1725 MK_SORT_ENTRY_CHK(trace)
1726 MK_SORT_ENTRY_CHK(srcline)
1727 MK_SORT_ENTRY_CHK(srcfile)
1728 MK_SORT_ENTRY_CHK(thread)
1729 MK_SORT_ENTRY_CHK(comm)
1730 MK_SORT_ENTRY_CHK(dso)
1731 MK_SORT_ENTRY_CHK(sym)
1734 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1736 struct hpp_sort_entry *hse_a;
1737 struct hpp_sort_entry *hse_b;
1739 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1742 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1743 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1745 return hse_a->se == hse_b->se;
1748 static void hse_free(struct perf_hpp_fmt *fmt)
1750 struct hpp_sort_entry *hse;
1752 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1756 static struct hpp_sort_entry *
1757 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
1759 struct hpp_sort_entry *hse;
1761 hse = malloc(sizeof(*hse));
1763 pr_err("Memory allocation failed\n");
1767 hse->se = sd->entry;
1768 hse->hpp.name = sd->entry->se_header;
1769 hse->hpp.header = __sort__hpp_header;
1770 hse->hpp.width = __sort__hpp_width;
1771 hse->hpp.entry = __sort__hpp_entry;
1772 hse->hpp.color = NULL;
1774 hse->hpp.cmp = __sort__hpp_cmp;
1775 hse->hpp.collapse = __sort__hpp_collapse;
1776 hse->hpp.sort = __sort__hpp_sort;
1777 hse->hpp.equal = __sort__hpp_equal;
1778 hse->hpp.free = hse_free;
1780 INIT_LIST_HEAD(&hse->hpp.list);
1781 INIT_LIST_HEAD(&hse->hpp.sort_list);
1782 hse->hpp.elide = false;
1784 hse->hpp.user_len = 0;
1785 hse->hpp.level = level;
1790 static void hpp_free(struct perf_hpp_fmt *fmt)
1795 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1798 struct perf_hpp_fmt *fmt;
1800 fmt = memdup(hd->fmt, sizeof(*fmt));
1802 INIT_LIST_HEAD(&fmt->list);
1803 INIT_LIST_HEAD(&fmt->sort_list);
1804 fmt->free = hpp_free;
1811 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1813 struct perf_hpp_fmt *fmt;
1814 struct hpp_sort_entry *hse;
1818 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1819 if (!perf_hpp__is_sort_entry(fmt))
1822 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1823 if (hse->se->se_filter == NULL)
1827 * hist entry is filtered if any of sort key in the hpp list
1828 * is applied. But it should skip non-matched filter types.
1830 r = hse->se->se_filter(he, type, arg);
1841 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1842 struct perf_hpp_list *list,
1845 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
1850 perf_hpp_list__register_sort_field(list, &hse->hpp);
1854 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1855 struct perf_hpp_list *list)
1857 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
1862 perf_hpp_list__column_register(list, &hse->hpp);
1866 struct hpp_dynamic_entry {
1867 struct perf_hpp_fmt hpp;
1868 struct perf_evsel *evsel;
1869 struct format_field *field;
1870 unsigned dynamic_len;
1874 static int hde_width(struct hpp_dynamic_entry *hde)
1876 if (!hde->hpp.len) {
1877 int len = hde->dynamic_len;
1878 int namelen = strlen(hde->field->name);
1879 int fieldlen = hde->field->size;
1884 if (!(hde->field->flags & FIELD_IS_STRING)) {
1885 /* length for print hex numbers */
1886 fieldlen = hde->field->size * 2 + 2;
1893 return hde->hpp.len;
1896 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1897 struct hist_entry *he)
1900 struct format_field *field = hde->field;
1907 /* parse pretty print result and update max length */
1908 if (!he->trace_output)
1909 he->trace_output = get_trace_output(he);
1911 namelen = strlen(field->name);
1912 str = he->trace_output;
1915 pos = strchr(str, ' ');
1918 pos = str + strlen(str);
1921 if (!strncmp(str, field->name, namelen)) {
1927 if (len > hde->dynamic_len)
1928 hde->dynamic_len = len;
1939 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1940 struct hists *hists __maybe_unused,
1941 int line __maybe_unused,
1942 int *span __maybe_unused)
1944 struct hpp_dynamic_entry *hde;
1945 size_t len = fmt->user_len;
1947 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1950 len = hde_width(hde);
1952 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1955 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1956 struct perf_hpp *hpp __maybe_unused,
1957 struct hists *hists __maybe_unused)
1959 struct hpp_dynamic_entry *hde;
1960 size_t len = fmt->user_len;
1962 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1965 len = hde_width(hde);
1970 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1972 struct hpp_dynamic_entry *hde;
1974 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1976 return hists_to_evsel(hists) == hde->evsel;
1979 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1980 struct hist_entry *he)
1982 struct hpp_dynamic_entry *hde;
1983 size_t len = fmt->user_len;
1985 struct format_field *field;
1990 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1993 len = hde_width(hde);
1998 if (!he->trace_output)
1999 he->trace_output = get_trace_output(he);
2002 namelen = strlen(field->name);
2003 str = he->trace_output;
2006 pos = strchr(str, ' ');
2009 pos = str + strlen(str);
2012 if (!strncmp(str, field->name, namelen)) {
2014 str = strndup(str, pos - str);
2017 return scnprintf(hpp->buf, hpp->size,
2018 "%*.*s", len, len, "ERROR");
2029 struct trace_seq seq;
2031 trace_seq_init(&seq);
2032 pevent_print_field(&seq, he->raw_data, hde->field);
2036 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
2041 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
2042 struct hist_entry *a, struct hist_entry *b)
2044 struct hpp_dynamic_entry *hde;
2045 struct format_field *field;
2046 unsigned offset, size;
2048 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2051 update_dynamic_len(hde, a);
2056 if (field->flags & FIELD_IS_DYNAMIC) {
2057 unsigned long long dyn;
2059 pevent_read_number_field(field, a->raw_data, &dyn);
2060 offset = dyn & 0xffff;
2061 size = (dyn >> 16) & 0xffff;
2063 /* record max width for output */
2064 if (size > hde->dynamic_len)
2065 hde->dynamic_len = size;
2067 offset = field->offset;
2071 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
2074 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
2076 return fmt->cmp == __sort__hde_cmp;
2079 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2081 struct hpp_dynamic_entry *hde_a;
2082 struct hpp_dynamic_entry *hde_b;
2084 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
2087 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
2088 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
2090 return hde_a->field == hde_b->field;
2093 static void hde_free(struct perf_hpp_fmt *fmt)
2095 struct hpp_dynamic_entry *hde;
2097 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2101 static struct hpp_dynamic_entry *
2102 __alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
2105 struct hpp_dynamic_entry *hde;
2107 hde = malloc(sizeof(*hde));
2109 pr_debug("Memory allocation failed\n");
2115 hde->dynamic_len = 0;
2117 hde->hpp.name = field->name;
2118 hde->hpp.header = __sort__hde_header;
2119 hde->hpp.width = __sort__hde_width;
2120 hde->hpp.entry = __sort__hde_entry;
2121 hde->hpp.color = NULL;
2123 hde->hpp.cmp = __sort__hde_cmp;
2124 hde->hpp.collapse = __sort__hde_cmp;
2125 hde->hpp.sort = __sort__hde_cmp;
2126 hde->hpp.equal = __sort__hde_equal;
2127 hde->hpp.free = hde_free;
2129 INIT_LIST_HEAD(&hde->hpp.list);
2130 INIT_LIST_HEAD(&hde->hpp.sort_list);
2131 hde->hpp.elide = false;
2133 hde->hpp.user_len = 0;
2134 hde->hpp.level = level;
2139 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
2141 struct perf_hpp_fmt *new_fmt = NULL;
2143 if (perf_hpp__is_sort_entry(fmt)) {
2144 struct hpp_sort_entry *hse, *new_hse;
2146 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2147 new_hse = memdup(hse, sizeof(*hse));
2149 new_fmt = &new_hse->hpp;
2150 } else if (perf_hpp__is_dynamic_entry(fmt)) {
2151 struct hpp_dynamic_entry *hde, *new_hde;
2153 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2154 new_hde = memdup(hde, sizeof(*hde));
2156 new_fmt = &new_hde->hpp;
2158 new_fmt = memdup(fmt, sizeof(*fmt));
2161 INIT_LIST_HEAD(&new_fmt->list);
2162 INIT_LIST_HEAD(&new_fmt->sort_list);
2167 static int parse_field_name(char *str, char **event, char **field, char **opt)
2169 char *event_name, *field_name, *opt_name;
2172 field_name = strchr(str, '.');
2175 *field_name++ = '\0';
2181 opt_name = strchr(field_name, '/');
2185 *event = event_name;
2186 *field = field_name;
2192 /* find match evsel using a given event name. The event name can be:
2193 * 1. '%' + event index (e.g. '%1' for first event)
2194 * 2. full event name (e.g. sched:sched_switch)
2195 * 3. partial event name (should not contain ':')
2197 static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
2199 struct perf_evsel *evsel = NULL;
2200 struct perf_evsel *pos;
2204 if (event_name[0] == '%') {
2205 int nr = strtol(event_name+1, NULL, 0);
2207 if (nr > evlist->nr_entries)
2210 evsel = perf_evlist__first(evlist);
2212 evsel = perf_evsel__next(evsel);
2217 full_name = !!strchr(event_name, ':');
2218 evlist__for_each_entry(evlist, pos) {
2220 if (full_name && !strcmp(pos->name, event_name))
2223 if (!full_name && strstr(pos->name, event_name)) {
2225 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2226 event_name, evsel->name, pos->name);
2236 static int __dynamic_dimension__add(struct perf_evsel *evsel,
2237 struct format_field *field,
2238 bool raw_trace, int level)
2240 struct hpp_dynamic_entry *hde;
2242 hde = __alloc_dynamic_entry(evsel, field, level);
2246 hde->raw_trace = raw_trace;
2248 perf_hpp__register_sort_field(&hde->hpp);
2252 static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
2255 struct format_field *field;
2257 field = evsel->tp_format->format.fields;
2259 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2263 field = field->next;
2268 static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2272 struct perf_evsel *evsel;
2274 evlist__for_each_entry(evlist, evsel) {
2275 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2278 ret = add_evsel_fields(evsel, raw_trace, level);
2285 static int add_all_matching_fields(struct perf_evlist *evlist,
2286 char *field_name, bool raw_trace, int level)
2289 struct perf_evsel *evsel;
2290 struct format_field *field;
2292 evlist__for_each_entry(evlist, evsel) {
2293 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2296 field = pevent_find_any_field(evsel->tp_format, field_name);
2300 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2307 static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2310 char *str, *event_name, *field_name, *opt_name;
2311 struct perf_evsel *evsel;
2312 struct format_field *field;
2313 bool raw_trace = symbol_conf.raw_trace;
2323 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2329 if (strcmp(opt_name, "raw")) {
2330 pr_debug("unsupported field option %s\n", opt_name);
2337 if (!strcmp(field_name, "trace_fields")) {
2338 ret = add_all_dynamic_fields(evlist, raw_trace, level);
2342 if (event_name == NULL) {
2343 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2347 evsel = find_evsel(evlist, event_name);
2348 if (evsel == NULL) {
2349 pr_debug("Cannot find event: %s\n", event_name);
2354 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2355 pr_debug("%s is not a tracepoint event\n", event_name);
2360 if (!strcmp(field_name, "*")) {
2361 ret = add_evsel_fields(evsel, raw_trace, level);
2363 field = pevent_find_any_field(evsel->tp_format, field_name);
2364 if (field == NULL) {
2365 pr_debug("Cannot find event field for %s.%s\n",
2366 event_name, field_name);
2370 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2378 static int __sort_dimension__add(struct sort_dimension *sd,
2379 struct perf_hpp_list *list,
2385 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2388 if (sd->entry->se_collapse)
2389 list->need_collapse = 1;
2396 static int __hpp_dimension__add(struct hpp_dimension *hd,
2397 struct perf_hpp_list *list,
2400 struct perf_hpp_fmt *fmt;
2405 fmt = __hpp_dimension__alloc_hpp(hd, level);
2410 perf_hpp_list__register_sort_field(list, fmt);
2414 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2415 struct sort_dimension *sd)
2420 if (__sort_dimension__add_hpp_output(sd, list) < 0)
2427 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2428 struct hpp_dimension *hd)
2430 struct perf_hpp_fmt *fmt;
2435 fmt = __hpp_dimension__alloc_hpp(hd, 0);
2440 perf_hpp_list__column_register(list, fmt);
2444 int hpp_dimension__add_output(unsigned col)
2446 BUG_ON(col >= PERF_HPP__MAX_INDEX);
2447 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2450 int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2451 struct perf_evlist *evlist,
2456 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2457 struct sort_dimension *sd = &common_sort_dimensions[i];
2459 if (strncasecmp(tok, sd->name, strlen(tok)))
2462 if (sd->entry == &sort_parent) {
2463 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2467 regerror(ret, &parent_regex, err, sizeof(err));
2468 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2472 } else if (sd->entry == &sort_sym) {
2475 * perf diff displays the performance difference amongst
2476 * two or more perf.data files. Those files could come
2477 * from different binaries. So we should not compare
2478 * their ips, but the name of symbol.
2480 if (sort__mode == SORT_MODE__DIFF)
2481 sd->entry->se_collapse = sort__sym_sort;
2483 } else if (sd->entry == &sort_dso) {
2485 } else if (sd->entry == &sort_socket) {
2487 } else if (sd->entry == &sort_thread) {
2489 } else if (sd->entry == &sort_comm) {
2493 return __sort_dimension__add(sd, list, level);
2496 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2497 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2499 if (strncasecmp(tok, hd->name, strlen(tok)))
2502 return __hpp_dimension__add(hd, list, level);
2505 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2506 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2508 if (strncasecmp(tok, sd->name, strlen(tok)))
2511 if (sort__mode != SORT_MODE__BRANCH)
2514 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2517 __sort_dimension__add(sd, list, level);
2521 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2522 struct sort_dimension *sd = &memory_sort_dimensions[i];
2524 if (strncasecmp(tok, sd->name, strlen(tok)))
2527 if (sort__mode != SORT_MODE__MEMORY)
2530 if (sd->entry == &sort_mem_dcacheline && cacheline_size == 0)
2533 if (sd->entry == &sort_mem_daddr_sym)
2536 __sort_dimension__add(sd, list, level);
2540 if (!add_dynamic_entry(evlist, tok, level))
2546 static int setup_sort_list(struct perf_hpp_list *list, char *str,
2547 struct perf_evlist *evlist)
2553 bool in_group = false;
2557 tmp = strpbrk(str, "{}, ");
2562 next_level = level + 1;
2566 else if (*tmp == '}')
2574 ret = sort_dimension__add(list, tok, evlist, level);
2575 if (ret == -EINVAL) {
2576 if (!cacheline_size && !strncasecmp(tok, "dcacheline", strlen(tok)))
2577 pr_err("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2579 pr_err("Invalid --sort key: `%s'", tok);
2581 } else if (ret == -ESRCH) {
2582 pr_err("Unknown --sort key: `%s'", tok);
2593 static const char *get_default_sort_order(struct perf_evlist *evlist)
2595 const char *default_sort_orders[] = {
2597 default_branch_sort_order,
2598 default_mem_sort_order,
2599 default_top_sort_order,
2600 default_diff_sort_order,
2601 default_tracepoint_sort_order,
2603 bool use_trace = true;
2604 struct perf_evsel *evsel;
2606 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2608 if (evlist == NULL || perf_evlist__empty(evlist))
2611 evlist__for_each_entry(evlist, evsel) {
2612 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2619 sort__mode = SORT_MODE__TRACEPOINT;
2620 if (symbol_conf.raw_trace)
2621 return "trace_fields";
2624 return default_sort_orders[sort__mode];
2627 static int setup_sort_order(struct perf_evlist *evlist)
2629 char *new_sort_order;
2632 * Append '+'-prefixed sort order to the default sort
2635 if (!sort_order || is_strict_order(sort_order))
2638 if (sort_order[1] == '\0') {
2639 pr_err("Invalid --sort key: `+'");
2644 * We allocate new sort_order string, but we never free it,
2645 * because it's checked over the rest of the code.
2647 if (asprintf(&new_sort_order, "%s,%s",
2648 get_default_sort_order(evlist), sort_order + 1) < 0) {
2649 pr_err("Not enough memory to set up --sort");
2653 sort_order = new_sort_order;
2658 * Adds 'pre,' prefix into 'str' is 'pre' is
2659 * not already part of 'str'.
2661 static char *prefix_if_not_in(const char *pre, char *str)
2665 if (!str || strstr(str, pre))
2668 if (asprintf(&n, "%s,%s", pre, str) < 0)
2675 static char *setup_overhead(char *keys)
2677 if (sort__mode == SORT_MODE__DIFF)
2680 keys = prefix_if_not_in("overhead", keys);
2682 if (symbol_conf.cumulate_callchain)
2683 keys = prefix_if_not_in("overhead_children", keys);
2688 static int __setup_sorting(struct perf_evlist *evlist)
2691 const char *sort_keys;
2694 ret = setup_sort_order(evlist);
2698 sort_keys = sort_order;
2699 if (sort_keys == NULL) {
2700 if (is_strict_order(field_order)) {
2702 * If user specified field order but no sort order,
2703 * we'll honor it and not add default sort orders.
2708 sort_keys = get_default_sort_order(evlist);
2711 str = strdup(sort_keys);
2713 pr_err("Not enough memory to setup sort keys");
2718 * Prepend overhead fields for backward compatibility.
2720 if (!is_strict_order(field_order)) {
2721 str = setup_overhead(str);
2723 pr_err("Not enough memory to setup overhead keys");
2728 ret = setup_sort_list(&perf_hpp_list, str, evlist);
2734 void perf_hpp__set_elide(int idx, bool elide)
2736 struct perf_hpp_fmt *fmt;
2737 struct hpp_sort_entry *hse;
2739 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2740 if (!perf_hpp__is_sort_entry(fmt))
2743 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2744 if (hse->se->se_width_idx == idx) {
2751 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
2753 if (list && strlist__nr_entries(list) == 1) {
2755 fprintf(fp, "# %s: %s\n", list_name,
2756 strlist__entry(list, 0)->s);
2762 static bool get_elide(int idx, FILE *output)
2766 return __get_elide(symbol_conf.sym_list, "symbol", output);
2768 return __get_elide(symbol_conf.dso_list, "dso", output);
2770 return __get_elide(symbol_conf.comm_list, "comm", output);
2775 if (sort__mode != SORT_MODE__BRANCH)
2779 case HISTC_SYMBOL_FROM:
2780 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2781 case HISTC_SYMBOL_TO:
2782 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2783 case HISTC_DSO_FROM:
2784 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2786 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2794 void sort__setup_elide(FILE *output)
2796 struct perf_hpp_fmt *fmt;
2797 struct hpp_sort_entry *hse;
2799 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2800 if (!perf_hpp__is_sort_entry(fmt))
2803 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2804 fmt->elide = get_elide(hse->se->se_width_idx, output);
2808 * It makes no sense to elide all of sort entries.
2809 * Just revert them to show up again.
2811 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2812 if (!perf_hpp__is_sort_entry(fmt))
2819 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2820 if (!perf_hpp__is_sort_entry(fmt))
2827 int output_field_add(struct perf_hpp_list *list, char *tok)
2831 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2832 struct sort_dimension *sd = &common_sort_dimensions[i];
2834 if (strncasecmp(tok, sd->name, strlen(tok)))
2837 return __sort_dimension__add_output(list, sd);
2840 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2841 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2843 if (strncasecmp(tok, hd->name, strlen(tok)))
2846 return __hpp_dimension__add_output(list, hd);
2849 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2850 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2852 if (strncasecmp(tok, sd->name, strlen(tok)))
2855 return __sort_dimension__add_output(list, sd);
2858 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2859 struct sort_dimension *sd = &memory_sort_dimensions[i];
2861 if (strncasecmp(tok, sd->name, strlen(tok)))
2864 return __sort_dimension__add_output(list, sd);
2870 static int setup_output_list(struct perf_hpp_list *list, char *str)
2875 for (tok = strtok_r(str, ", ", &tmp);
2876 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2877 ret = output_field_add(list, tok);
2878 if (ret == -EINVAL) {
2879 pr_err("Invalid --fields key: `%s'", tok);
2881 } else if (ret == -ESRCH) {
2882 pr_err("Unknown --fields key: `%s'", tok);
2890 void reset_dimensions(void)
2894 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2895 common_sort_dimensions[i].taken = 0;
2897 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2898 hpp_sort_dimensions[i].taken = 0;
2900 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2901 bstack_sort_dimensions[i].taken = 0;
2903 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2904 memory_sort_dimensions[i].taken = 0;
2907 bool is_strict_order(const char *order)
2909 return order && (*order != '+');
2912 static int __setup_output_field(void)
2917 if (field_order == NULL)
2920 strp = str = strdup(field_order);
2922 pr_err("Not enough memory to setup output fields");
2926 if (!is_strict_order(field_order))
2929 if (!strlen(strp)) {
2930 pr_err("Invalid --fields key: `+'");
2934 ret = setup_output_list(&perf_hpp_list, strp);
2941 int setup_sorting(struct perf_evlist *evlist)
2945 err = __setup_sorting(evlist);
2949 if (parent_pattern != default_parent_pattern) {
2950 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
2958 * perf diff doesn't use default hpp output fields.
2960 if (sort__mode != SORT_MODE__DIFF)
2963 err = __setup_output_field();
2967 /* copy sort keys to output fields */
2968 perf_hpp__setup_output_field(&perf_hpp_list);
2969 /* and then copy output fields to sort keys */
2970 perf_hpp__append_sort_keys(&perf_hpp_list);
2972 /* setup hists-specific output fields */
2973 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2979 void reset_output_field(void)
2981 perf_hpp_list.need_collapse = 0;
2982 perf_hpp_list.parent = 0;
2983 perf_hpp_list.sym = 0;
2984 perf_hpp_list.dso = 0;
2990 perf_hpp__reset_output_field(&perf_hpp_list);