1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/mman.h>
7 #include <linux/time64.h>
12 #include "cacheline.h"
17 #include "map_symbol.h"
25 #include <traceevent/event-parse.h>
26 #include "mem-events.h"
29 #include "time-utils.h"
32 #include <linux/kernel.h>
33 #include <linux/string.h>
36 const char default_parent_pattern[] = "^sys_|^do_page_fault";
37 const char *parent_pattern = default_parent_pattern;
38 const char *default_sort_order = "comm,dso,symbol";
39 const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
40 const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked,blocked,local_ins_lat,p_stage_cyc";
41 const char default_top_sort_order[] = "dso,symbol";
42 const char default_diff_sort_order[] = "dso,symbol";
43 const char default_tracepoint_sort_order[] = "trace";
44 const char *sort_order;
45 const char *field_order;
46 regex_t ignore_callees_regex;
47 int have_ignore_callees = 0;
48 enum sort_mode sort__mode = SORT_MODE__NORMAL;
49 const char *dynamic_headers[] = {"local_ins_lat", "p_stage_cyc"};
50 const char *arch_specific_sort_keys[] = {"p_stage_cyc"};
53 * Replaces all occurrences of a char used with the:
55 * -t, --field-separator
57 * option, that uses a special separator character and don't pad with spaces,
58 * replacing all occurrences of this separator in symbol names (and other
59 * output) with a '.' character, that thus it's the only non valid separator.
61 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
67 n = vsnprintf(bf, size, fmt, ap);
68 if (symbol_conf.field_sep && n > 0) {
72 sep = strchr(sep, *symbol_conf.field_sep);
85 static int64_t cmp_null(const void *l, const void *r)
98 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
100 return right->thread->tid - left->thread->tid;
103 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
104 size_t size, unsigned int width)
106 const char *comm = thread__comm_str(he->thread);
108 width = max(7U, width) - 8;
109 return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid,
110 width, width, comm ?: "");
113 static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
115 const struct thread *th = arg;
117 if (type != HIST_FILTER__THREAD)
120 return th && he->thread != th;
123 struct sort_entry sort_thread = {
124 .se_header = " Pid:Command",
125 .se_cmp = sort__thread_cmp,
126 .se_snprintf = hist_entry__thread_snprintf,
127 .se_filter = hist_entry__thread_filter,
128 .se_width_idx = HISTC_THREAD,
134 * We can't use pointer comparison in functions below,
135 * because it gives different results based on pointer
136 * values, which could break some sorting assumptions.
139 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
141 return strcmp(comm__str(right->comm), comm__str(left->comm));
145 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
147 return strcmp(comm__str(right->comm), comm__str(left->comm));
151 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
153 return strcmp(comm__str(right->comm), comm__str(left->comm));
156 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
157 size_t size, unsigned int width)
159 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
162 struct sort_entry sort_comm = {
163 .se_header = "Command",
164 .se_cmp = sort__comm_cmp,
165 .se_collapse = sort__comm_collapse,
166 .se_sort = sort__comm_sort,
167 .se_snprintf = hist_entry__comm_snprintf,
168 .se_filter = hist_entry__thread_filter,
169 .se_width_idx = HISTC_COMM,
174 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
176 struct dso *dso_l = map_l ? map_l->dso : NULL;
177 struct dso *dso_r = map_r ? map_r->dso : NULL;
178 const char *dso_name_l, *dso_name_r;
180 if (!dso_l || !dso_r)
181 return cmp_null(dso_r, dso_l);
184 dso_name_l = dso_l->long_name;
185 dso_name_r = dso_r->long_name;
187 dso_name_l = dso_l->short_name;
188 dso_name_r = dso_r->short_name;
191 return strcmp(dso_name_l, dso_name_r);
195 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
197 return _sort__dso_cmp(right->ms.map, left->ms.map);
200 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
201 size_t size, unsigned int width)
203 if (map && map->dso) {
204 const char *dso_name = verbose > 0 ? map->dso->long_name :
205 map->dso->short_name;
206 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
209 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
212 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
213 size_t size, unsigned int width)
215 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
218 static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
220 const struct dso *dso = arg;
222 if (type != HIST_FILTER__DSO)
225 return dso && (!he->ms.map || he->ms.map->dso != dso);
228 struct sort_entry sort_dso = {
229 .se_header = "Shared Object",
230 .se_cmp = sort__dso_cmp,
231 .se_snprintf = hist_entry__dso_snprintf,
232 .se_filter = hist_entry__dso_filter,
233 .se_width_idx = HISTC_DSO,
238 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
240 return (int64_t)(right_ip - left_ip);
243 int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
245 if (!sym_l || !sym_r)
246 return cmp_null(sym_l, sym_r);
251 if (sym_l->inlined || sym_r->inlined) {
252 int ret = strcmp(sym_l->name, sym_r->name);
256 if ((sym_l->start <= sym_r->end) && (sym_l->end >= sym_r->start))
260 if (sym_l->start != sym_r->start)
261 return (int64_t)(sym_r->start - sym_l->start);
263 return (int64_t)(sym_r->end - sym_l->end);
267 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
271 if (!left->ms.sym && !right->ms.sym)
272 return _sort__addr_cmp(left->ip, right->ip);
275 * comparing symbol address alone is not enough since it's a
276 * relative address within a dso.
278 if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
279 ret = sort__dso_cmp(left, right);
284 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
288 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
290 if (!left->ms.sym || !right->ms.sym)
291 return cmp_null(left->ms.sym, right->ms.sym);
293 return strcmp(right->ms.sym->name, left->ms.sym->name);
296 static int _hist_entry__sym_snprintf(struct map_symbol *ms,
297 u64 ip, char level, char *bf, size_t size,
300 struct symbol *sym = ms->sym;
301 struct map *map = ms->map;
305 char o = map ? dso__symtab_origin(map->dso) : '!';
308 if (map && map->dso && map->dso->kernel
309 && map->dso->adjust_symbols)
310 rip = map->unmap_ip(map, ip);
312 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
313 BITS_PER_LONG / 4 + 2, rip, o);
316 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
318 if (sym->type == STT_OBJECT) {
319 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
320 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
321 ip - map->unmap_ip(map, sym->start));
323 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
327 ret += repsep_snprintf(bf + ret, size - ret,
331 size_t len = BITS_PER_LONG / 4;
332 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
339 int hist_entry__sym_snprintf(struct hist_entry *he, char *bf, size_t size, unsigned int width)
341 return _hist_entry__sym_snprintf(&he->ms, he->ip,
342 he->level, bf, size, width);
345 static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
347 const char *sym = arg;
349 if (type != HIST_FILTER__SYMBOL)
352 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
355 struct sort_entry sort_sym = {
356 .se_header = "Symbol",
357 .se_cmp = sort__sym_cmp,
358 .se_sort = sort__sym_sort,
359 .se_snprintf = hist_entry__sym_snprintf,
360 .se_filter = hist_entry__sym_filter,
361 .se_width_idx = HISTC_SYMBOL,
366 char *hist_entry__srcline(struct hist_entry *he)
368 return map__srcline(he->ms.map, he->ip, he->ms.sym);
372 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
375 left->srcline = hist_entry__srcline(left);
377 right->srcline = hist_entry__srcline(right);
379 return strcmp(right->srcline, left->srcline);
382 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
383 size_t size, unsigned int width)
386 he->srcline = hist_entry__srcline(he);
388 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
391 struct sort_entry sort_srcline = {
392 .se_header = "Source:Line",
393 .se_cmp = sort__srcline_cmp,
394 .se_snprintf = hist_entry__srcline_snprintf,
395 .se_width_idx = HISTC_SRCLINE,
398 /* --sort srcline_from */
400 static char *addr_map_symbol__srcline(struct addr_map_symbol *ams)
402 return map__srcline(ams->ms.map, ams->al_addr, ams->ms.sym);
406 sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
408 if (!left->branch_info->srcline_from)
409 left->branch_info->srcline_from = addr_map_symbol__srcline(&left->branch_info->from);
411 if (!right->branch_info->srcline_from)
412 right->branch_info->srcline_from = addr_map_symbol__srcline(&right->branch_info->from);
414 return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
417 static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
418 size_t size, unsigned int width)
420 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
423 struct sort_entry sort_srcline_from = {
424 .se_header = "From Source:Line",
425 .se_cmp = sort__srcline_from_cmp,
426 .se_snprintf = hist_entry__srcline_from_snprintf,
427 .se_width_idx = HISTC_SRCLINE_FROM,
430 /* --sort srcline_to */
433 sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
435 if (!left->branch_info->srcline_to)
436 left->branch_info->srcline_to = addr_map_symbol__srcline(&left->branch_info->to);
438 if (!right->branch_info->srcline_to)
439 right->branch_info->srcline_to = addr_map_symbol__srcline(&right->branch_info->to);
441 return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
444 static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
445 size_t size, unsigned int width)
447 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
450 struct sort_entry sort_srcline_to = {
451 .se_header = "To Source:Line",
452 .se_cmp = sort__srcline_to_cmp,
453 .se_snprintf = hist_entry__srcline_to_snprintf,
454 .se_width_idx = HISTC_SRCLINE_TO,
457 static int hist_entry__sym_ipc_snprintf(struct hist_entry *he, char *bf,
458 size_t size, unsigned int width)
461 struct symbol *sym = he->ms.sym;
462 struct annotation *notes;
463 double ipc = 0.0, coverage = 0.0;
467 return repsep_snprintf(bf, size, "%-*s", width, "-");
469 notes = symbol__annotation(sym);
471 if (notes->hit_cycles)
472 ipc = notes->hit_insn / ((double)notes->hit_cycles);
474 if (notes->total_insn) {
475 coverage = notes->cover_insn * 100.0 /
476 ((double)notes->total_insn);
479 snprintf(tmp, sizeof(tmp), "%-5.2f [%5.1f%%]", ipc, coverage);
480 return repsep_snprintf(bf, size, "%-*s", width, tmp);
483 struct sort_entry sort_sym_ipc = {
484 .se_header = "IPC [IPC Coverage]",
485 .se_cmp = sort__sym_cmp,
486 .se_snprintf = hist_entry__sym_ipc_snprintf,
487 .se_width_idx = HISTC_SYMBOL_IPC,
490 static int hist_entry__sym_ipc_null_snprintf(struct hist_entry *he
492 char *bf, size_t size,
497 snprintf(tmp, sizeof(tmp), "%-5s %2s", "-", "-");
498 return repsep_snprintf(bf, size, "%-*s", width, tmp);
501 struct sort_entry sort_sym_ipc_null = {
502 .se_header = "IPC [IPC Coverage]",
503 .se_cmp = sort__sym_cmp,
504 .se_snprintf = hist_entry__sym_ipc_null_snprintf,
505 .se_width_idx = HISTC_SYMBOL_IPC,
510 static char no_srcfile[1];
512 static char *hist_entry__get_srcfile(struct hist_entry *e)
515 struct map *map = e->ms.map;
520 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
521 e->ms.sym, false, true, true, e->ip);
522 if (!strcmp(sf, SRCLINE_UNKNOWN))
534 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
537 left->srcfile = hist_entry__get_srcfile(left);
539 right->srcfile = hist_entry__get_srcfile(right);
541 return strcmp(right->srcfile, left->srcfile);
544 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
545 size_t size, unsigned int width)
548 he->srcfile = hist_entry__get_srcfile(he);
550 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
553 struct sort_entry sort_srcfile = {
554 .se_header = "Source File",
555 .se_cmp = sort__srcfile_cmp,
556 .se_snprintf = hist_entry__srcfile_snprintf,
557 .se_width_idx = HISTC_SRCFILE,
563 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
565 struct symbol *sym_l = left->parent;
566 struct symbol *sym_r = right->parent;
568 if (!sym_l || !sym_r)
569 return cmp_null(sym_l, sym_r);
571 return strcmp(sym_r->name, sym_l->name);
574 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
575 size_t size, unsigned int width)
577 return repsep_snprintf(bf, size, "%-*.*s", width, width,
578 he->parent ? he->parent->name : "[other]");
581 struct sort_entry sort_parent = {
582 .se_header = "Parent symbol",
583 .se_cmp = sort__parent_cmp,
584 .se_snprintf = hist_entry__parent_snprintf,
585 .se_width_idx = HISTC_PARENT,
591 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
593 return right->cpu - left->cpu;
596 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
597 size_t size, unsigned int width)
599 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
602 struct sort_entry sort_cpu = {
604 .se_cmp = sort__cpu_cmp,
605 .se_snprintf = hist_entry__cpu_snprintf,
606 .se_width_idx = HISTC_CPU,
609 /* --sort cgroup_id */
611 static int64_t _sort__cgroup_dev_cmp(u64 left_dev, u64 right_dev)
613 return (int64_t)(right_dev - left_dev);
616 static int64_t _sort__cgroup_inode_cmp(u64 left_ino, u64 right_ino)
618 return (int64_t)(right_ino - left_ino);
622 sort__cgroup_id_cmp(struct hist_entry *left, struct hist_entry *right)
626 ret = _sort__cgroup_dev_cmp(right->cgroup_id.dev, left->cgroup_id.dev);
630 return _sort__cgroup_inode_cmp(right->cgroup_id.ino,
631 left->cgroup_id.ino);
634 static int hist_entry__cgroup_id_snprintf(struct hist_entry *he,
635 char *bf, size_t size,
636 unsigned int width __maybe_unused)
638 return repsep_snprintf(bf, size, "%lu/0x%lx", he->cgroup_id.dev,
642 struct sort_entry sort_cgroup_id = {
643 .se_header = "cgroup id (dev/inode)",
644 .se_cmp = sort__cgroup_id_cmp,
645 .se_snprintf = hist_entry__cgroup_id_snprintf,
646 .se_width_idx = HISTC_CGROUP_ID,
652 sort__cgroup_cmp(struct hist_entry *left, struct hist_entry *right)
654 return right->cgroup - left->cgroup;
657 static int hist_entry__cgroup_snprintf(struct hist_entry *he,
658 char *bf, size_t size,
659 unsigned int width __maybe_unused)
661 const char *cgrp_name = "N/A";
664 struct cgroup *cgrp = cgroup__find(he->ms.maps->machine->env,
667 cgrp_name = cgrp->name;
669 cgrp_name = "unknown";
672 return repsep_snprintf(bf, size, "%s", cgrp_name);
675 struct sort_entry sort_cgroup = {
676 .se_header = "Cgroup",
677 .se_cmp = sort__cgroup_cmp,
678 .se_snprintf = hist_entry__cgroup_snprintf,
679 .se_width_idx = HISTC_CGROUP,
685 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
687 return right->socket - left->socket;
690 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
691 size_t size, unsigned int width)
693 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
696 static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
698 int sk = *(const int *)arg;
700 if (type != HIST_FILTER__SOCKET)
703 return sk >= 0 && he->socket != sk;
706 struct sort_entry sort_socket = {
707 .se_header = "Socket",
708 .se_cmp = sort__socket_cmp,
709 .se_snprintf = hist_entry__socket_snprintf,
710 .se_filter = hist_entry__socket_filter,
711 .se_width_idx = HISTC_SOCKET,
717 sort__time_cmp(struct hist_entry *left, struct hist_entry *right)
719 return right->time - left->time;
722 static int hist_entry__time_snprintf(struct hist_entry *he, char *bf,
723 size_t size, unsigned int width)
727 if (symbol_conf.nanosecs)
728 timestamp__scnprintf_nsec(he->time, he_time,
731 timestamp__scnprintf_usec(he->time, he_time,
734 return repsep_snprintf(bf, size, "%-.*s", width, he_time);
737 struct sort_entry sort_time = {
739 .se_cmp = sort__time_cmp,
740 .se_snprintf = hist_entry__time_snprintf,
741 .se_width_idx = HISTC_TIME,
746 static char *get_trace_output(struct hist_entry *he)
748 struct trace_seq seq;
750 struct tep_record rec = {
751 .data = he->raw_data,
752 .size = he->raw_size,
755 evsel = hists_to_evsel(he->hists);
757 trace_seq_init(&seq);
758 if (symbol_conf.raw_trace) {
759 tep_print_fields(&seq, he->raw_data, he->raw_size,
762 tep_print_event(evsel->tp_format->tep,
763 &seq, &rec, "%s", TEP_PRINT_INFO);
766 * Trim the buffer, it starts at 4KB and we're not going to
767 * add anything more to this buffer.
769 return realloc(seq.buffer, seq.len + 1);
773 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
777 evsel = hists_to_evsel(left->hists);
778 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
781 if (left->trace_output == NULL)
782 left->trace_output = get_trace_output(left);
783 if (right->trace_output == NULL)
784 right->trace_output = get_trace_output(right);
786 return strcmp(right->trace_output, left->trace_output);
789 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
790 size_t size, unsigned int width)
794 evsel = hists_to_evsel(he->hists);
795 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
796 return scnprintf(bf, size, "%-.*s", width, "N/A");
798 if (he->trace_output == NULL)
799 he->trace_output = get_trace_output(he);
800 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
803 struct sort_entry sort_trace = {
804 .se_header = "Trace output",
805 .se_cmp = sort__trace_cmp,
806 .se_snprintf = hist_entry__trace_snprintf,
807 .se_width_idx = HISTC_TRACE,
810 /* sort keys for branch stacks */
813 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
815 if (!left->branch_info || !right->branch_info)
816 return cmp_null(left->branch_info, right->branch_info);
818 return _sort__dso_cmp(left->branch_info->from.ms.map,
819 right->branch_info->from.ms.map);
822 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
823 size_t size, unsigned int width)
826 return _hist_entry__dso_snprintf(he->branch_info->from.ms.map,
829 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
832 static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
835 const struct dso *dso = arg;
837 if (type != HIST_FILTER__DSO)
840 return dso && (!he->branch_info || !he->branch_info->from.ms.map ||
841 he->branch_info->from.ms.map->dso != dso);
845 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
847 if (!left->branch_info || !right->branch_info)
848 return cmp_null(left->branch_info, right->branch_info);
850 return _sort__dso_cmp(left->branch_info->to.ms.map,
851 right->branch_info->to.ms.map);
854 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
855 size_t size, unsigned int width)
858 return _hist_entry__dso_snprintf(he->branch_info->to.ms.map,
861 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
864 static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
867 const struct dso *dso = arg;
869 if (type != HIST_FILTER__DSO)
872 return dso && (!he->branch_info || !he->branch_info->to.ms.map ||
873 he->branch_info->to.ms.map->dso != dso);
877 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
879 struct addr_map_symbol *from_l = &left->branch_info->from;
880 struct addr_map_symbol *from_r = &right->branch_info->from;
882 if (!left->branch_info || !right->branch_info)
883 return cmp_null(left->branch_info, right->branch_info);
885 from_l = &left->branch_info->from;
886 from_r = &right->branch_info->from;
888 if (!from_l->ms.sym && !from_r->ms.sym)
889 return _sort__addr_cmp(from_l->addr, from_r->addr);
891 return _sort__sym_cmp(from_l->ms.sym, from_r->ms.sym);
895 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
897 struct addr_map_symbol *to_l, *to_r;
899 if (!left->branch_info || !right->branch_info)
900 return cmp_null(left->branch_info, right->branch_info);
902 to_l = &left->branch_info->to;
903 to_r = &right->branch_info->to;
905 if (!to_l->ms.sym && !to_r->ms.sym)
906 return _sort__addr_cmp(to_l->addr, to_r->addr);
908 return _sort__sym_cmp(to_l->ms.sym, to_r->ms.sym);
911 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
912 size_t size, unsigned int width)
914 if (he->branch_info) {
915 struct addr_map_symbol *from = &he->branch_info->from;
917 return _hist_entry__sym_snprintf(&from->ms, from->al_addr,
918 he->level, bf, size, width);
921 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
924 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
925 size_t size, unsigned int width)
927 if (he->branch_info) {
928 struct addr_map_symbol *to = &he->branch_info->to;
930 return _hist_entry__sym_snprintf(&to->ms, to->al_addr,
931 he->level, bf, size, width);
934 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
937 static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
940 const char *sym = arg;
942 if (type != HIST_FILTER__SYMBOL)
945 return sym && !(he->branch_info && he->branch_info->from.ms.sym &&
946 strstr(he->branch_info->from.ms.sym->name, sym));
949 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
952 const char *sym = arg;
954 if (type != HIST_FILTER__SYMBOL)
957 return sym && !(he->branch_info && he->branch_info->to.ms.sym &&
958 strstr(he->branch_info->to.ms.sym->name, sym));
961 struct sort_entry sort_dso_from = {
962 .se_header = "Source Shared Object",
963 .se_cmp = sort__dso_from_cmp,
964 .se_snprintf = hist_entry__dso_from_snprintf,
965 .se_filter = hist_entry__dso_from_filter,
966 .se_width_idx = HISTC_DSO_FROM,
969 struct sort_entry sort_dso_to = {
970 .se_header = "Target Shared Object",
971 .se_cmp = sort__dso_to_cmp,
972 .se_snprintf = hist_entry__dso_to_snprintf,
973 .se_filter = hist_entry__dso_to_filter,
974 .se_width_idx = HISTC_DSO_TO,
977 struct sort_entry sort_sym_from = {
978 .se_header = "Source Symbol",
979 .se_cmp = sort__sym_from_cmp,
980 .se_snprintf = hist_entry__sym_from_snprintf,
981 .se_filter = hist_entry__sym_from_filter,
982 .se_width_idx = HISTC_SYMBOL_FROM,
985 struct sort_entry sort_sym_to = {
986 .se_header = "Target Symbol",
987 .se_cmp = sort__sym_to_cmp,
988 .se_snprintf = hist_entry__sym_to_snprintf,
989 .se_filter = hist_entry__sym_to_filter,
990 .se_width_idx = HISTC_SYMBOL_TO,
994 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
998 if (!left->branch_info || !right->branch_info)
999 return cmp_null(left->branch_info, right->branch_info);
1001 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
1002 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
1006 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
1007 size_t size, unsigned int width){
1008 static const char *out = "N/A";
1010 if (he->branch_info) {
1011 if (he->branch_info->flags.predicted)
1013 else if (he->branch_info->flags.mispred)
1017 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
1021 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
1023 if (!left->branch_info || !right->branch_info)
1024 return cmp_null(left->branch_info, right->branch_info);
1026 return left->branch_info->flags.cycles -
1027 right->branch_info->flags.cycles;
1030 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
1031 size_t size, unsigned int width)
1033 if (!he->branch_info)
1034 return scnprintf(bf, size, "%-.*s", width, "N/A");
1035 if (he->branch_info->flags.cycles == 0)
1036 return repsep_snprintf(bf, size, "%-*s", width, "-");
1037 return repsep_snprintf(bf, size, "%-*hd", width,
1038 he->branch_info->flags.cycles);
1041 struct sort_entry sort_cycles = {
1042 .se_header = "Basic Block Cycles",
1043 .se_cmp = sort__cycles_cmp,
1044 .se_snprintf = hist_entry__cycles_snprintf,
1045 .se_width_idx = HISTC_CYCLES,
1048 /* --sort daddr_sym */
1050 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1052 uint64_t l = 0, r = 0;
1055 l = left->mem_info->daddr.addr;
1056 if (right->mem_info)
1057 r = right->mem_info->daddr.addr;
1059 return (int64_t)(r - l);
1062 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
1063 size_t size, unsigned int width)
1066 struct map_symbol *ms = NULL;
1069 addr = he->mem_info->daddr.addr;
1070 ms = &he->mem_info->daddr.ms;
1072 return _hist_entry__sym_snprintf(ms, addr, he->level, bf, size, width);
1076 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
1078 uint64_t l = 0, r = 0;
1081 l = left->mem_info->iaddr.addr;
1082 if (right->mem_info)
1083 r = right->mem_info->iaddr.addr;
1085 return (int64_t)(r - l);
1088 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
1089 size_t size, unsigned int width)
1092 struct map_symbol *ms = NULL;
1095 addr = he->mem_info->iaddr.addr;
1096 ms = &he->mem_info->iaddr.ms;
1098 return _hist_entry__sym_snprintf(ms, addr, he->level, bf, size, width);
1102 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1104 struct map *map_l = NULL;
1105 struct map *map_r = NULL;
1108 map_l = left->mem_info->daddr.ms.map;
1109 if (right->mem_info)
1110 map_r = right->mem_info->daddr.ms.map;
1112 return _sort__dso_cmp(map_l, map_r);
1115 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
1116 size_t size, unsigned int width)
1118 struct map *map = NULL;
1121 map = he->mem_info->daddr.ms.map;
1123 return _hist_entry__dso_snprintf(map, bf, size, width);
1127 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
1129 union perf_mem_data_src data_src_l;
1130 union perf_mem_data_src data_src_r;
1133 data_src_l = left->mem_info->data_src;
1135 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
1137 if (right->mem_info)
1138 data_src_r = right->mem_info->data_src;
1140 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
1142 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
1145 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
1146 size_t size, unsigned int width)
1150 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
1151 return repsep_snprintf(bf, size, "%.*s", width, out);
1155 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
1157 union perf_mem_data_src data_src_l;
1158 union perf_mem_data_src data_src_r;
1161 data_src_l = left->mem_info->data_src;
1163 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
1165 if (right->mem_info)
1166 data_src_r = right->mem_info->data_src;
1168 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
1170 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
1173 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
1174 size_t size, unsigned int width)
1178 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
1179 return repsep_snprintf(bf, size, "%-*s", width, out);
1183 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
1185 union perf_mem_data_src data_src_l;
1186 union perf_mem_data_src data_src_r;
1189 data_src_l = left->mem_info->data_src;
1191 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
1193 if (right->mem_info)
1194 data_src_r = right->mem_info->data_src;
1196 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
1198 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
1201 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
1202 size_t size, unsigned int width)
1206 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
1207 return repsep_snprintf(bf, size, "%-*s", width, out);
1211 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
1213 union perf_mem_data_src data_src_l;
1214 union perf_mem_data_src data_src_r;
1217 data_src_l = left->mem_info->data_src;
1219 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
1221 if (right->mem_info)
1222 data_src_r = right->mem_info->data_src;
1224 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
1226 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
1229 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
1230 size_t size, unsigned int width)
1234 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
1235 return repsep_snprintf(bf, size, "%-*s", width, out);
1239 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
1242 struct map *l_map, *r_map;
1245 if (!left->mem_info) return -1;
1246 if (!right->mem_info) return 1;
1248 /* group event types together */
1249 if (left->cpumode > right->cpumode) return -1;
1250 if (left->cpumode < right->cpumode) return 1;
1252 l_map = left->mem_info->daddr.ms.map;
1253 r_map = right->mem_info->daddr.ms.map;
1255 /* if both are NULL, jump to sort on al_addr instead */
1256 if (!l_map && !r_map)
1259 if (!l_map) return -1;
1260 if (!r_map) return 1;
1262 rc = dso__cmp_id(l_map->dso, r_map->dso);
1266 * Addresses with no major/minor numbers are assumed to be
1267 * anonymous in userspace. Sort those on pid then address.
1269 * The kernel and non-zero major/minor mapped areas are
1270 * assumed to be unity mapped. Sort those on address.
1273 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1274 (!(l_map->flags & MAP_SHARED)) &&
1275 !l_map->dso->id.maj && !l_map->dso->id.min &&
1276 !l_map->dso->id.ino && !l_map->dso->id.ino_generation) {
1277 /* userspace anonymous */
1279 if (left->thread->pid_ > right->thread->pid_) return -1;
1280 if (left->thread->pid_ < right->thread->pid_) return 1;
1284 /* al_addr does all the right addr - start + offset calculations */
1285 l = cl_address(left->mem_info->daddr.al_addr);
1286 r = cl_address(right->mem_info->daddr.al_addr);
1288 if (l > r) return -1;
1289 if (l < r) return 1;
1294 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1295 size_t size, unsigned int width)
1299 struct map_symbol *ms = NULL;
1300 char level = he->level;
1303 struct map *map = he->mem_info->daddr.ms.map;
1305 addr = cl_address(he->mem_info->daddr.al_addr);
1306 ms = &he->mem_info->daddr.ms;
1308 /* print [s] for shared data mmaps */
1309 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1310 map && !(map->prot & PROT_EXEC) &&
1311 (map->flags & MAP_SHARED) &&
1312 (map->dso->id.maj || map->dso->id.min ||
1313 map->dso->id.ino || map->dso->id.ino_generation))
1318 return _hist_entry__sym_snprintf(ms, addr, level, bf, size, width);
1321 struct sort_entry sort_mispredict = {
1322 .se_header = "Branch Mispredicted",
1323 .se_cmp = sort__mispredict_cmp,
1324 .se_snprintf = hist_entry__mispredict_snprintf,
1325 .se_width_idx = HISTC_MISPREDICT,
1328 static u64 he_weight(struct hist_entry *he)
1330 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1334 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1336 return he_weight(left) - he_weight(right);
1339 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1340 size_t size, unsigned int width)
1342 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1345 struct sort_entry sort_local_weight = {
1346 .se_header = "Local Weight",
1347 .se_cmp = sort__local_weight_cmp,
1348 .se_snprintf = hist_entry__local_weight_snprintf,
1349 .se_width_idx = HISTC_LOCAL_WEIGHT,
1353 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1355 return left->stat.weight - right->stat.weight;
1358 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1359 size_t size, unsigned int width)
1361 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1364 struct sort_entry sort_global_weight = {
1365 .se_header = "Weight",
1366 .se_cmp = sort__global_weight_cmp,
1367 .se_snprintf = hist_entry__global_weight_snprintf,
1368 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1371 static u64 he_ins_lat(struct hist_entry *he)
1373 return he->stat.nr_events ? he->stat.ins_lat / he->stat.nr_events : 0;
1377 sort__local_ins_lat_cmp(struct hist_entry *left, struct hist_entry *right)
1379 return he_ins_lat(left) - he_ins_lat(right);
1382 static int hist_entry__local_ins_lat_snprintf(struct hist_entry *he, char *bf,
1383 size_t size, unsigned int width)
1385 return repsep_snprintf(bf, size, "%-*u", width, he_ins_lat(he));
1388 struct sort_entry sort_local_ins_lat = {
1389 .se_header = "Local INSTR Latency",
1390 .se_cmp = sort__local_ins_lat_cmp,
1391 .se_snprintf = hist_entry__local_ins_lat_snprintf,
1392 .se_width_idx = HISTC_LOCAL_INS_LAT,
1396 sort__global_ins_lat_cmp(struct hist_entry *left, struct hist_entry *right)
1398 return left->stat.ins_lat - right->stat.ins_lat;
1401 static int hist_entry__global_ins_lat_snprintf(struct hist_entry *he, char *bf,
1402 size_t size, unsigned int width)
1404 return repsep_snprintf(bf, size, "%-*u", width, he->stat.ins_lat);
1407 struct sort_entry sort_global_ins_lat = {
1408 .se_header = "INSTR Latency",
1409 .se_cmp = sort__global_ins_lat_cmp,
1410 .se_snprintf = hist_entry__global_ins_lat_snprintf,
1411 .se_width_idx = HISTC_GLOBAL_INS_LAT,
1415 sort__global_p_stage_cyc_cmp(struct hist_entry *left, struct hist_entry *right)
1417 return left->stat.p_stage_cyc - right->stat.p_stage_cyc;
1420 static int hist_entry__p_stage_cyc_snprintf(struct hist_entry *he, char *bf,
1421 size_t size, unsigned int width)
1423 return repsep_snprintf(bf, size, "%-*u", width, he->stat.p_stage_cyc);
1426 struct sort_entry sort_p_stage_cyc = {
1427 .se_header = "Pipeline Stage Cycle",
1428 .se_cmp = sort__global_p_stage_cyc_cmp,
1429 .se_snprintf = hist_entry__p_stage_cyc_snprintf,
1430 .se_width_idx = HISTC_P_STAGE_CYC,
1433 struct sort_entry sort_mem_daddr_sym = {
1434 .se_header = "Data Symbol",
1435 .se_cmp = sort__daddr_cmp,
1436 .se_snprintf = hist_entry__daddr_snprintf,
1437 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1440 struct sort_entry sort_mem_iaddr_sym = {
1441 .se_header = "Code Symbol",
1442 .se_cmp = sort__iaddr_cmp,
1443 .se_snprintf = hist_entry__iaddr_snprintf,
1444 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1447 struct sort_entry sort_mem_daddr_dso = {
1448 .se_header = "Data Object",
1449 .se_cmp = sort__dso_daddr_cmp,
1450 .se_snprintf = hist_entry__dso_daddr_snprintf,
1451 .se_width_idx = HISTC_MEM_DADDR_DSO,
1454 struct sort_entry sort_mem_locked = {
1455 .se_header = "Locked",
1456 .se_cmp = sort__locked_cmp,
1457 .se_snprintf = hist_entry__locked_snprintf,
1458 .se_width_idx = HISTC_MEM_LOCKED,
1461 struct sort_entry sort_mem_tlb = {
1462 .se_header = "TLB access",
1463 .se_cmp = sort__tlb_cmp,
1464 .se_snprintf = hist_entry__tlb_snprintf,
1465 .se_width_idx = HISTC_MEM_TLB,
1468 struct sort_entry sort_mem_lvl = {
1469 .se_header = "Memory access",
1470 .se_cmp = sort__lvl_cmp,
1471 .se_snprintf = hist_entry__lvl_snprintf,
1472 .se_width_idx = HISTC_MEM_LVL,
1475 struct sort_entry sort_mem_snoop = {
1476 .se_header = "Snoop",
1477 .se_cmp = sort__snoop_cmp,
1478 .se_snprintf = hist_entry__snoop_snprintf,
1479 .se_width_idx = HISTC_MEM_SNOOP,
1482 struct sort_entry sort_mem_dcacheline = {
1483 .se_header = "Data Cacheline",
1484 .se_cmp = sort__dcacheline_cmp,
1485 .se_snprintf = hist_entry__dcacheline_snprintf,
1486 .se_width_idx = HISTC_MEM_DCACHELINE,
1490 sort__blocked_cmp(struct hist_entry *left, struct hist_entry *right)
1492 union perf_mem_data_src data_src_l;
1493 union perf_mem_data_src data_src_r;
1496 data_src_l = left->mem_info->data_src;
1498 data_src_l.mem_blk = PERF_MEM_BLK_NA;
1500 if (right->mem_info)
1501 data_src_r = right->mem_info->data_src;
1503 data_src_r.mem_blk = PERF_MEM_BLK_NA;
1505 return (int64_t)(data_src_r.mem_blk - data_src_l.mem_blk);
1508 static int hist_entry__blocked_snprintf(struct hist_entry *he, char *bf,
1509 size_t size, unsigned int width)
1513 perf_mem__blk_scnprintf(out, sizeof(out), he->mem_info);
1514 return repsep_snprintf(bf, size, "%.*s", width, out);
1517 struct sort_entry sort_mem_blocked = {
1518 .se_header = "Blocked",
1519 .se_cmp = sort__blocked_cmp,
1520 .se_snprintf = hist_entry__blocked_snprintf,
1521 .se_width_idx = HISTC_MEM_BLOCKED,
1525 sort__phys_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1527 uint64_t l = 0, r = 0;
1530 l = left->mem_info->daddr.phys_addr;
1531 if (right->mem_info)
1532 r = right->mem_info->daddr.phys_addr;
1534 return (int64_t)(r - l);
1537 static int hist_entry__phys_daddr_snprintf(struct hist_entry *he, char *bf,
1538 size_t size, unsigned int width)
1542 size_t len = BITS_PER_LONG / 4;
1544 addr = he->mem_info->daddr.phys_addr;
1546 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", he->level);
1548 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", len, addr);
1550 ret += repsep_snprintf(bf + ret, size - ret, "%-*s", width - ret, "");
1558 struct sort_entry sort_mem_phys_daddr = {
1559 .se_header = "Data Physical Address",
1560 .se_cmp = sort__phys_daddr_cmp,
1561 .se_snprintf = hist_entry__phys_daddr_snprintf,
1562 .se_width_idx = HISTC_MEM_PHYS_DADDR,
1566 sort__data_page_size_cmp(struct hist_entry *left, struct hist_entry *right)
1568 uint64_t l = 0, r = 0;
1571 l = left->mem_info->daddr.data_page_size;
1572 if (right->mem_info)
1573 r = right->mem_info->daddr.data_page_size;
1575 return (int64_t)(r - l);
1578 static int hist_entry__data_page_size_snprintf(struct hist_entry *he, char *bf,
1579 size_t size, unsigned int width)
1581 char str[PAGE_SIZE_NAME_LEN];
1583 return repsep_snprintf(bf, size, "%-*s", width,
1584 get_page_size_name(he->mem_info->daddr.data_page_size, str));
1587 struct sort_entry sort_mem_data_page_size = {
1588 .se_header = "Data Page Size",
1589 .se_cmp = sort__data_page_size_cmp,
1590 .se_snprintf = hist_entry__data_page_size_snprintf,
1591 .se_width_idx = HISTC_MEM_DATA_PAGE_SIZE,
1595 sort__code_page_size_cmp(struct hist_entry *left, struct hist_entry *right)
1597 uint64_t l = left->code_page_size;
1598 uint64_t r = right->code_page_size;
1600 return (int64_t)(r - l);
1603 static int hist_entry__code_page_size_snprintf(struct hist_entry *he, char *bf,
1604 size_t size, unsigned int width)
1606 char str[PAGE_SIZE_NAME_LEN];
1608 return repsep_snprintf(bf, size, "%-*s", width,
1609 get_page_size_name(he->code_page_size, str));
1612 struct sort_entry sort_code_page_size = {
1613 .se_header = "Code Page Size",
1614 .se_cmp = sort__code_page_size_cmp,
1615 .se_snprintf = hist_entry__code_page_size_snprintf,
1616 .se_width_idx = HISTC_CODE_PAGE_SIZE,
1620 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1622 if (!left->branch_info || !right->branch_info)
1623 return cmp_null(left->branch_info, right->branch_info);
1625 return left->branch_info->flags.abort !=
1626 right->branch_info->flags.abort;
1629 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1630 size_t size, unsigned int width)
1632 static const char *out = "N/A";
1634 if (he->branch_info) {
1635 if (he->branch_info->flags.abort)
1641 return repsep_snprintf(bf, size, "%-*s", width, out);
1644 struct sort_entry sort_abort = {
1645 .se_header = "Transaction abort",
1646 .se_cmp = sort__abort_cmp,
1647 .se_snprintf = hist_entry__abort_snprintf,
1648 .se_width_idx = HISTC_ABORT,
1652 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1654 if (!left->branch_info || !right->branch_info)
1655 return cmp_null(left->branch_info, right->branch_info);
1657 return left->branch_info->flags.in_tx !=
1658 right->branch_info->flags.in_tx;
1661 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1662 size_t size, unsigned int width)
1664 static const char *out = "N/A";
1666 if (he->branch_info) {
1667 if (he->branch_info->flags.in_tx)
1673 return repsep_snprintf(bf, size, "%-*s", width, out);
1676 struct sort_entry sort_in_tx = {
1677 .se_header = "Branch in transaction",
1678 .se_cmp = sort__in_tx_cmp,
1679 .se_snprintf = hist_entry__in_tx_snprintf,
1680 .se_width_idx = HISTC_IN_TX,
1684 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1686 return left->transaction - right->transaction;
1689 static inline char *add_str(char *p, const char *str)
1692 return p + strlen(str);
1695 static struct txbit {
1700 { PERF_TXN_ELISION, "EL ", 0 },
1701 { PERF_TXN_TRANSACTION, "TX ", 1 },
1702 { PERF_TXN_SYNC, "SYNC ", 1 },
1703 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1704 { PERF_TXN_RETRY, "RETRY ", 0 },
1705 { PERF_TXN_CONFLICT, "CON ", 0 },
1706 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1707 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1711 int hist_entry__transaction_len(void)
1716 for (i = 0; txbits[i].name; i++) {
1717 if (!txbits[i].skip_for_len)
1718 len += strlen(txbits[i].name);
1720 len += 4; /* :XX<space> */
1724 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1725 size_t size, unsigned int width)
1727 u64 t = he->transaction;
1733 for (i = 0; txbits[i].name; i++)
1734 if (txbits[i].flag & t)
1735 p = add_str(p, txbits[i].name);
1736 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1737 p = add_str(p, "NEITHER ");
1738 if (t & PERF_TXN_ABORT_MASK) {
1739 sprintf(p, ":%" PRIx64,
1740 (t & PERF_TXN_ABORT_MASK) >>
1741 PERF_TXN_ABORT_SHIFT);
1745 return repsep_snprintf(bf, size, "%-*s", width, buf);
1748 struct sort_entry sort_transaction = {
1749 .se_header = "Transaction ",
1750 .se_cmp = sort__transaction_cmp,
1751 .se_snprintf = hist_entry__transaction_snprintf,
1752 .se_width_idx = HISTC_TRANSACTION,
1755 /* --sort symbol_size */
1757 static int64_t _sort__sym_size_cmp(struct symbol *sym_l, struct symbol *sym_r)
1759 int64_t size_l = sym_l != NULL ? symbol__size(sym_l) : 0;
1760 int64_t size_r = sym_r != NULL ? symbol__size(sym_r) : 0;
1762 return size_l < size_r ? -1 :
1763 size_l == size_r ? 0 : 1;
1767 sort__sym_size_cmp(struct hist_entry *left, struct hist_entry *right)
1769 return _sort__sym_size_cmp(right->ms.sym, left->ms.sym);
1772 static int _hist_entry__sym_size_snprintf(struct symbol *sym, char *bf,
1773 size_t bf_size, unsigned int width)
1776 return repsep_snprintf(bf, bf_size, "%*d", width, symbol__size(sym));
1778 return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1781 static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
1782 size_t size, unsigned int width)
1784 return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
1787 struct sort_entry sort_sym_size = {
1788 .se_header = "Symbol size",
1789 .se_cmp = sort__sym_size_cmp,
1790 .se_snprintf = hist_entry__sym_size_snprintf,
1791 .se_width_idx = HISTC_SYM_SIZE,
1794 /* --sort dso_size */
1796 static int64_t _sort__dso_size_cmp(struct map *map_l, struct map *map_r)
1798 int64_t size_l = map_l != NULL ? map__size(map_l) : 0;
1799 int64_t size_r = map_r != NULL ? map__size(map_r) : 0;
1801 return size_l < size_r ? -1 :
1802 size_l == size_r ? 0 : 1;
1806 sort__dso_size_cmp(struct hist_entry *left, struct hist_entry *right)
1808 return _sort__dso_size_cmp(right->ms.map, left->ms.map);
1811 static int _hist_entry__dso_size_snprintf(struct map *map, char *bf,
1812 size_t bf_size, unsigned int width)
1814 if (map && map->dso)
1815 return repsep_snprintf(bf, bf_size, "%*d", width,
1818 return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1821 static int hist_entry__dso_size_snprintf(struct hist_entry *he, char *bf,
1822 size_t size, unsigned int width)
1824 return _hist_entry__dso_size_snprintf(he->ms.map, bf, size, width);
1827 struct sort_entry sort_dso_size = {
1828 .se_header = "DSO size",
1829 .se_cmp = sort__dso_size_cmp,
1830 .se_snprintf = hist_entry__dso_size_snprintf,
1831 .se_width_idx = HISTC_DSO_SIZE,
1835 struct sort_dimension {
1837 struct sort_entry *entry;
1841 int __weak arch_support_sort_key(const char *sort_key __maybe_unused)
1846 const char * __weak arch_perf_header_entry(const char *se_header)
1851 static void sort_dimension_add_dynamic_header(struct sort_dimension *sd)
1853 sd->entry->se_header = arch_perf_header_entry(sd->entry->se_header);
1856 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1858 static struct sort_dimension common_sort_dimensions[] = {
1859 DIM(SORT_PID, "pid", sort_thread),
1860 DIM(SORT_COMM, "comm", sort_comm),
1861 DIM(SORT_DSO, "dso", sort_dso),
1862 DIM(SORT_SYM, "symbol", sort_sym),
1863 DIM(SORT_PARENT, "parent", sort_parent),
1864 DIM(SORT_CPU, "cpu", sort_cpu),
1865 DIM(SORT_SOCKET, "socket", sort_socket),
1866 DIM(SORT_SRCLINE, "srcline", sort_srcline),
1867 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1868 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1869 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1870 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1871 DIM(SORT_TRACE, "trace", sort_trace),
1872 DIM(SORT_SYM_SIZE, "symbol_size", sort_sym_size),
1873 DIM(SORT_DSO_SIZE, "dso_size", sort_dso_size),
1874 DIM(SORT_CGROUP, "cgroup", sort_cgroup),
1875 DIM(SORT_CGROUP_ID, "cgroup_id", sort_cgroup_id),
1876 DIM(SORT_SYM_IPC_NULL, "ipc_null", sort_sym_ipc_null),
1877 DIM(SORT_TIME, "time", sort_time),
1878 DIM(SORT_CODE_PAGE_SIZE, "code_page_size", sort_code_page_size),
1879 DIM(SORT_LOCAL_INS_LAT, "local_ins_lat", sort_local_ins_lat),
1880 DIM(SORT_GLOBAL_INS_LAT, "ins_lat", sort_global_ins_lat),
1881 DIM(SORT_PIPELINE_STAGE_CYC, "p_stage_cyc", sort_p_stage_cyc),
1886 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1888 static struct sort_dimension bstack_sort_dimensions[] = {
1889 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1890 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1891 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1892 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1893 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1894 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1895 DIM(SORT_ABORT, "abort", sort_abort),
1896 DIM(SORT_CYCLES, "cycles", sort_cycles),
1897 DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
1898 DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
1899 DIM(SORT_SYM_IPC, "ipc_lbr", sort_sym_ipc),
1904 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1906 static struct sort_dimension memory_sort_dimensions[] = {
1907 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1908 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1909 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1910 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1911 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1912 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1913 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1914 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1915 DIM(SORT_MEM_PHYS_DADDR, "phys_daddr", sort_mem_phys_daddr),
1916 DIM(SORT_MEM_DATA_PAGE_SIZE, "data_page_size", sort_mem_data_page_size),
1917 DIM(SORT_MEM_BLOCKED, "blocked", sort_mem_blocked),
1922 struct hpp_dimension {
1924 struct perf_hpp_fmt *fmt;
1928 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1930 static struct hpp_dimension hpp_sort_dimensions[] = {
1931 DIM(PERF_HPP__OVERHEAD, "overhead"),
1932 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1933 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1934 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1935 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1936 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1937 DIM(PERF_HPP__SAMPLES, "sample"),
1938 DIM(PERF_HPP__PERIOD, "period"),
1943 struct hpp_sort_entry {
1944 struct perf_hpp_fmt hpp;
1945 struct sort_entry *se;
1948 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1950 struct hpp_sort_entry *hse;
1952 if (!perf_hpp__is_sort_entry(fmt))
1955 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1956 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1959 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1960 struct hists *hists, int line __maybe_unused,
1961 int *span __maybe_unused)
1963 struct hpp_sort_entry *hse;
1964 size_t len = fmt->user_len;
1966 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1969 len = hists__col_len(hists, hse->se->se_width_idx);
1971 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1974 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1975 struct perf_hpp *hpp __maybe_unused,
1976 struct hists *hists)
1978 struct hpp_sort_entry *hse;
1979 size_t len = fmt->user_len;
1981 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1984 len = hists__col_len(hists, hse->se->se_width_idx);
1989 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1990 struct hist_entry *he)
1992 struct hpp_sort_entry *hse;
1993 size_t len = fmt->user_len;
1995 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1998 len = hists__col_len(he->hists, hse->se->se_width_idx);
2000 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
2003 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
2004 struct hist_entry *a, struct hist_entry *b)
2006 struct hpp_sort_entry *hse;
2008 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2009 return hse->se->se_cmp(a, b);
2012 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
2013 struct hist_entry *a, struct hist_entry *b)
2015 struct hpp_sort_entry *hse;
2016 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
2018 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2019 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
2020 return collapse_fn(a, b);
2023 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
2024 struct hist_entry *a, struct hist_entry *b)
2026 struct hpp_sort_entry *hse;
2027 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
2029 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2030 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
2031 return sort_fn(a, b);
2034 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
2036 return format->header == __sort__hpp_header;
2039 #define MK_SORT_ENTRY_CHK(key) \
2040 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
2042 struct hpp_sort_entry *hse; \
2044 if (!perf_hpp__is_sort_entry(fmt)) \
2047 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
2048 return hse->se == &sort_ ## key ; \
2051 MK_SORT_ENTRY_CHK(trace)
2052 MK_SORT_ENTRY_CHK(srcline)
2053 MK_SORT_ENTRY_CHK(srcfile)
2054 MK_SORT_ENTRY_CHK(thread)
2055 MK_SORT_ENTRY_CHK(comm)
2056 MK_SORT_ENTRY_CHK(dso)
2057 MK_SORT_ENTRY_CHK(sym)
2060 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2062 struct hpp_sort_entry *hse_a;
2063 struct hpp_sort_entry *hse_b;
2065 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
2068 hse_a = container_of(a, struct hpp_sort_entry, hpp);
2069 hse_b = container_of(b, struct hpp_sort_entry, hpp);
2071 return hse_a->se == hse_b->se;
2074 static void hse_free(struct perf_hpp_fmt *fmt)
2076 struct hpp_sort_entry *hse;
2078 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2082 static struct hpp_sort_entry *
2083 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
2085 struct hpp_sort_entry *hse;
2087 hse = malloc(sizeof(*hse));
2089 pr_err("Memory allocation failed\n");
2093 hse->se = sd->entry;
2094 hse->hpp.name = sd->entry->se_header;
2095 hse->hpp.header = __sort__hpp_header;
2096 hse->hpp.width = __sort__hpp_width;
2097 hse->hpp.entry = __sort__hpp_entry;
2098 hse->hpp.color = NULL;
2100 hse->hpp.cmp = __sort__hpp_cmp;
2101 hse->hpp.collapse = __sort__hpp_collapse;
2102 hse->hpp.sort = __sort__hpp_sort;
2103 hse->hpp.equal = __sort__hpp_equal;
2104 hse->hpp.free = hse_free;
2106 INIT_LIST_HEAD(&hse->hpp.list);
2107 INIT_LIST_HEAD(&hse->hpp.sort_list);
2108 hse->hpp.elide = false;
2110 hse->hpp.user_len = 0;
2111 hse->hpp.level = level;
2116 static void hpp_free(struct perf_hpp_fmt *fmt)
2121 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
2124 struct perf_hpp_fmt *fmt;
2126 fmt = memdup(hd->fmt, sizeof(*fmt));
2128 INIT_LIST_HEAD(&fmt->list);
2129 INIT_LIST_HEAD(&fmt->sort_list);
2130 fmt->free = hpp_free;
2137 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
2139 struct perf_hpp_fmt *fmt;
2140 struct hpp_sort_entry *hse;
2144 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
2145 if (!perf_hpp__is_sort_entry(fmt))
2148 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2149 if (hse->se->se_filter == NULL)
2153 * hist entry is filtered if any of sort key in the hpp list
2154 * is applied. But it should skip non-matched filter types.
2156 r = hse->se->se_filter(he, type, arg);
2167 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
2168 struct perf_hpp_list *list,
2171 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
2176 perf_hpp_list__register_sort_field(list, &hse->hpp);
2180 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
2181 struct perf_hpp_list *list)
2183 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
2188 perf_hpp_list__column_register(list, &hse->hpp);
2192 struct hpp_dynamic_entry {
2193 struct perf_hpp_fmt hpp;
2194 struct evsel *evsel;
2195 struct tep_format_field *field;
2196 unsigned dynamic_len;
2200 static int hde_width(struct hpp_dynamic_entry *hde)
2202 if (!hde->hpp.len) {
2203 int len = hde->dynamic_len;
2204 int namelen = strlen(hde->field->name);
2205 int fieldlen = hde->field->size;
2210 if (!(hde->field->flags & TEP_FIELD_IS_STRING)) {
2211 /* length for print hex numbers */
2212 fieldlen = hde->field->size * 2 + 2;
2219 return hde->hpp.len;
2222 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
2223 struct hist_entry *he)
2226 struct tep_format_field *field = hde->field;
2233 /* parse pretty print result and update max length */
2234 if (!he->trace_output)
2235 he->trace_output = get_trace_output(he);
2237 namelen = strlen(field->name);
2238 str = he->trace_output;
2241 pos = strchr(str, ' ');
2244 pos = str + strlen(str);
2247 if (!strncmp(str, field->name, namelen)) {
2253 if (len > hde->dynamic_len)
2254 hde->dynamic_len = len;
2265 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2266 struct hists *hists __maybe_unused,
2267 int line __maybe_unused,
2268 int *span __maybe_unused)
2270 struct hpp_dynamic_entry *hde;
2271 size_t len = fmt->user_len;
2273 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2276 len = hde_width(hde);
2278 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
2281 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
2282 struct perf_hpp *hpp __maybe_unused,
2283 struct hists *hists __maybe_unused)
2285 struct hpp_dynamic_entry *hde;
2286 size_t len = fmt->user_len;
2288 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2291 len = hde_width(hde);
2296 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
2298 struct hpp_dynamic_entry *hde;
2300 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2302 return hists_to_evsel(hists) == hde->evsel;
2305 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2306 struct hist_entry *he)
2308 struct hpp_dynamic_entry *hde;
2309 size_t len = fmt->user_len;
2311 struct tep_format_field *field;
2316 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2319 len = hde_width(hde);
2324 if (!he->trace_output)
2325 he->trace_output = get_trace_output(he);
2328 namelen = strlen(field->name);
2329 str = he->trace_output;
2332 pos = strchr(str, ' ');
2335 pos = str + strlen(str);
2338 if (!strncmp(str, field->name, namelen)) {
2340 str = strndup(str, pos - str);
2343 return scnprintf(hpp->buf, hpp->size,
2344 "%*.*s", len, len, "ERROR");
2355 struct trace_seq seq;
2357 trace_seq_init(&seq);
2358 tep_print_field(&seq, he->raw_data, hde->field);
2362 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
2367 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
2368 struct hist_entry *a, struct hist_entry *b)
2370 struct hpp_dynamic_entry *hde;
2371 struct tep_format_field *field;
2372 unsigned offset, size;
2374 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2377 update_dynamic_len(hde, a);
2382 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2383 unsigned long long dyn;
2385 tep_read_number_field(field, a->raw_data, &dyn);
2386 offset = dyn & 0xffff;
2387 size = (dyn >> 16) & 0xffff;
2389 /* record max width for output */
2390 if (size > hde->dynamic_len)
2391 hde->dynamic_len = size;
2393 offset = field->offset;
2397 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
2400 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
2402 return fmt->cmp == __sort__hde_cmp;
2405 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2407 struct hpp_dynamic_entry *hde_a;
2408 struct hpp_dynamic_entry *hde_b;
2410 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
2413 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
2414 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
2416 return hde_a->field == hde_b->field;
2419 static void hde_free(struct perf_hpp_fmt *fmt)
2421 struct hpp_dynamic_entry *hde;
2423 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2427 static struct hpp_dynamic_entry *
2428 __alloc_dynamic_entry(struct evsel *evsel, struct tep_format_field *field,
2431 struct hpp_dynamic_entry *hde;
2433 hde = malloc(sizeof(*hde));
2435 pr_debug("Memory allocation failed\n");
2441 hde->dynamic_len = 0;
2443 hde->hpp.name = field->name;
2444 hde->hpp.header = __sort__hde_header;
2445 hde->hpp.width = __sort__hde_width;
2446 hde->hpp.entry = __sort__hde_entry;
2447 hde->hpp.color = NULL;
2449 hde->hpp.cmp = __sort__hde_cmp;
2450 hde->hpp.collapse = __sort__hde_cmp;
2451 hde->hpp.sort = __sort__hde_cmp;
2452 hde->hpp.equal = __sort__hde_equal;
2453 hde->hpp.free = hde_free;
2455 INIT_LIST_HEAD(&hde->hpp.list);
2456 INIT_LIST_HEAD(&hde->hpp.sort_list);
2457 hde->hpp.elide = false;
2459 hde->hpp.user_len = 0;
2460 hde->hpp.level = level;
2465 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
2467 struct perf_hpp_fmt *new_fmt = NULL;
2469 if (perf_hpp__is_sort_entry(fmt)) {
2470 struct hpp_sort_entry *hse, *new_hse;
2472 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2473 new_hse = memdup(hse, sizeof(*hse));
2475 new_fmt = &new_hse->hpp;
2476 } else if (perf_hpp__is_dynamic_entry(fmt)) {
2477 struct hpp_dynamic_entry *hde, *new_hde;
2479 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2480 new_hde = memdup(hde, sizeof(*hde));
2482 new_fmt = &new_hde->hpp;
2484 new_fmt = memdup(fmt, sizeof(*fmt));
2487 INIT_LIST_HEAD(&new_fmt->list);
2488 INIT_LIST_HEAD(&new_fmt->sort_list);
2493 static int parse_field_name(char *str, char **event, char **field, char **opt)
2495 char *event_name, *field_name, *opt_name;
2498 field_name = strchr(str, '.');
2501 *field_name++ = '\0';
2507 opt_name = strchr(field_name, '/');
2511 *event = event_name;
2512 *field = field_name;
2518 /* find match evsel using a given event name. The event name can be:
2519 * 1. '%' + event index (e.g. '%1' for first event)
2520 * 2. full event name (e.g. sched:sched_switch)
2521 * 3. partial event name (should not contain ':')
2523 static struct evsel *find_evsel(struct evlist *evlist, char *event_name)
2525 struct evsel *evsel = NULL;
2530 if (event_name[0] == '%') {
2531 int nr = strtol(event_name+1, NULL, 0);
2533 if (nr > evlist->core.nr_entries)
2536 evsel = evlist__first(evlist);
2538 evsel = evsel__next(evsel);
2543 full_name = !!strchr(event_name, ':');
2544 evlist__for_each_entry(evlist, pos) {
2546 if (full_name && !strcmp(pos->name, event_name))
2549 if (!full_name && strstr(pos->name, event_name)) {
2551 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2552 event_name, evsel->name, pos->name);
2562 static int __dynamic_dimension__add(struct evsel *evsel,
2563 struct tep_format_field *field,
2564 bool raw_trace, int level)
2566 struct hpp_dynamic_entry *hde;
2568 hde = __alloc_dynamic_entry(evsel, field, level);
2572 hde->raw_trace = raw_trace;
2574 perf_hpp__register_sort_field(&hde->hpp);
2578 static int add_evsel_fields(struct evsel *evsel, bool raw_trace, int level)
2581 struct tep_format_field *field;
2583 field = evsel->tp_format->format.fields;
2585 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2589 field = field->next;
2594 static int add_all_dynamic_fields(struct evlist *evlist, bool raw_trace,
2598 struct evsel *evsel;
2600 evlist__for_each_entry(evlist, evsel) {
2601 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2604 ret = add_evsel_fields(evsel, raw_trace, level);
2611 static int add_all_matching_fields(struct evlist *evlist,
2612 char *field_name, bool raw_trace, int level)
2615 struct evsel *evsel;
2616 struct tep_format_field *field;
2618 evlist__for_each_entry(evlist, evsel) {
2619 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2622 field = tep_find_any_field(evsel->tp_format, field_name);
2626 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2633 static int add_dynamic_entry(struct evlist *evlist, const char *tok,
2636 char *str, *event_name, *field_name, *opt_name;
2637 struct evsel *evsel;
2638 struct tep_format_field *field;
2639 bool raw_trace = symbol_conf.raw_trace;
2649 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2655 if (strcmp(opt_name, "raw")) {
2656 pr_debug("unsupported field option %s\n", opt_name);
2663 if (!strcmp(field_name, "trace_fields")) {
2664 ret = add_all_dynamic_fields(evlist, raw_trace, level);
2668 if (event_name == NULL) {
2669 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2673 evsel = find_evsel(evlist, event_name);
2674 if (evsel == NULL) {
2675 pr_debug("Cannot find event: %s\n", event_name);
2680 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2681 pr_debug("%s is not a tracepoint event\n", event_name);
2686 if (!strcmp(field_name, "*")) {
2687 ret = add_evsel_fields(evsel, raw_trace, level);
2689 field = tep_find_any_field(evsel->tp_format, field_name);
2690 if (field == NULL) {
2691 pr_debug("Cannot find event field for %s.%s\n",
2692 event_name, field_name);
2696 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2704 static int __sort_dimension__add(struct sort_dimension *sd,
2705 struct perf_hpp_list *list,
2711 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2714 if (sd->entry->se_collapse)
2715 list->need_collapse = 1;
2722 static int __hpp_dimension__add(struct hpp_dimension *hd,
2723 struct perf_hpp_list *list,
2726 struct perf_hpp_fmt *fmt;
2731 fmt = __hpp_dimension__alloc_hpp(hd, level);
2736 perf_hpp_list__register_sort_field(list, fmt);
2740 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2741 struct sort_dimension *sd)
2746 if (__sort_dimension__add_hpp_output(sd, list) < 0)
2753 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2754 struct hpp_dimension *hd)
2756 struct perf_hpp_fmt *fmt;
2761 fmt = __hpp_dimension__alloc_hpp(hd, 0);
2766 perf_hpp_list__column_register(list, fmt);
2770 int hpp_dimension__add_output(unsigned col)
2772 BUG_ON(col >= PERF_HPP__MAX_INDEX);
2773 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2776 int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2777 struct evlist *evlist,
2783 * Check to see if there are any arch specific
2784 * sort dimensions not applicable for the current
2785 * architecture. If so, Skip that sort key since
2786 * we don't want to display it in the output fields.
2788 for (j = 0; j < ARRAY_SIZE(arch_specific_sort_keys); j++) {
2789 if (!strcmp(arch_specific_sort_keys[j], tok) &&
2790 !arch_support_sort_key(tok)) {
2795 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2796 struct sort_dimension *sd = &common_sort_dimensions[i];
2798 if (strncasecmp(tok, sd->name, strlen(tok)))
2801 for (j = 0; j < ARRAY_SIZE(dynamic_headers); j++) {
2802 if (!strcmp(dynamic_headers[j], sd->name))
2803 sort_dimension_add_dynamic_header(sd);
2806 if (sd->entry == &sort_parent) {
2807 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2811 regerror(ret, &parent_regex, err, sizeof(err));
2812 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2816 } else if (sd->entry == &sort_sym) {
2819 * perf diff displays the performance difference amongst
2820 * two or more perf.data files. Those files could come
2821 * from different binaries. So we should not compare
2822 * their ips, but the name of symbol.
2824 if (sort__mode == SORT_MODE__DIFF)
2825 sd->entry->se_collapse = sort__sym_sort;
2827 } else if (sd->entry == &sort_dso) {
2829 } else if (sd->entry == &sort_socket) {
2831 } else if (sd->entry == &sort_thread) {
2833 } else if (sd->entry == &sort_comm) {
2837 return __sort_dimension__add(sd, list, level);
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(hd, list, level);
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 if (sort__mode != SORT_MODE__BRANCH)
2858 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2861 __sort_dimension__add(sd, list, level);
2865 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2866 struct sort_dimension *sd = &memory_sort_dimensions[i];
2868 if (strncasecmp(tok, sd->name, strlen(tok)))
2871 if (sort__mode != SORT_MODE__MEMORY)
2874 if (sd->entry == &sort_mem_dcacheline && cacheline_size() == 0)
2877 if (sd->entry == &sort_mem_daddr_sym)
2880 __sort_dimension__add(sd, list, level);
2884 if (!add_dynamic_entry(evlist, tok, level))
2890 static int setup_sort_list(struct perf_hpp_list *list, char *str,
2891 struct evlist *evlist)
2897 bool in_group = false;
2901 tmp = strpbrk(str, "{}, ");
2906 next_level = level + 1;
2910 else if (*tmp == '}')
2918 ret = sort_dimension__add(list, tok, evlist, level);
2919 if (ret == -EINVAL) {
2920 if (!cacheline_size() && !strncasecmp(tok, "dcacheline", strlen(tok)))
2921 ui__error("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2923 ui__error("Invalid --sort key: `%s'", tok);
2925 } else if (ret == -ESRCH) {
2926 ui__error("Unknown --sort key: `%s'", tok);
2937 static const char *get_default_sort_order(struct evlist *evlist)
2939 const char *default_sort_orders[] = {
2941 default_branch_sort_order,
2942 default_mem_sort_order,
2943 default_top_sort_order,
2944 default_diff_sort_order,
2945 default_tracepoint_sort_order,
2947 bool use_trace = true;
2948 struct evsel *evsel;
2950 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2952 if (evlist == NULL || evlist__empty(evlist))
2955 evlist__for_each_entry(evlist, evsel) {
2956 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2963 sort__mode = SORT_MODE__TRACEPOINT;
2964 if (symbol_conf.raw_trace)
2965 return "trace_fields";
2968 return default_sort_orders[sort__mode];
2971 static int setup_sort_order(struct evlist *evlist)
2973 char *new_sort_order;
2976 * Append '+'-prefixed sort order to the default sort
2979 if (!sort_order || is_strict_order(sort_order))
2982 if (sort_order[1] == '\0') {
2983 ui__error("Invalid --sort key: `+'");
2988 * We allocate new sort_order string, but we never free it,
2989 * because it's checked over the rest of the code.
2991 if (asprintf(&new_sort_order, "%s,%s",
2992 get_default_sort_order(evlist), sort_order + 1) < 0) {
2993 pr_err("Not enough memory to set up --sort");
2997 sort_order = new_sort_order;
3002 * Adds 'pre,' prefix into 'str' is 'pre' is
3003 * not already part of 'str'.
3005 static char *prefix_if_not_in(const char *pre, char *str)
3009 if (!str || strstr(str, pre))
3012 if (asprintf(&n, "%s,%s", pre, str) < 0)
3019 static char *setup_overhead(char *keys)
3021 if (sort__mode == SORT_MODE__DIFF)
3024 keys = prefix_if_not_in("overhead", keys);
3026 if (symbol_conf.cumulate_callchain)
3027 keys = prefix_if_not_in("overhead_children", keys);
3032 static int __setup_sorting(struct evlist *evlist)
3035 const char *sort_keys;
3038 ret = setup_sort_order(evlist);
3042 sort_keys = sort_order;
3043 if (sort_keys == NULL) {
3044 if (is_strict_order(field_order)) {
3046 * If user specified field order but no sort order,
3047 * we'll honor it and not add default sort orders.
3052 sort_keys = get_default_sort_order(evlist);
3055 str = strdup(sort_keys);
3057 pr_err("Not enough memory to setup sort keys");
3062 * Prepend overhead fields for backward compatibility.
3064 if (!is_strict_order(field_order)) {
3065 str = setup_overhead(str);
3067 pr_err("Not enough memory to setup overhead keys");
3072 ret = setup_sort_list(&perf_hpp_list, str, evlist);
3078 void perf_hpp__set_elide(int idx, bool elide)
3080 struct perf_hpp_fmt *fmt;
3081 struct hpp_sort_entry *hse;
3083 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
3084 if (!perf_hpp__is_sort_entry(fmt))
3087 hse = container_of(fmt, struct hpp_sort_entry, hpp);
3088 if (hse->se->se_width_idx == idx) {
3095 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
3097 if (list && strlist__nr_entries(list) == 1) {
3099 fprintf(fp, "# %s: %s\n", list_name,
3100 strlist__entry(list, 0)->s);
3106 static bool get_elide(int idx, FILE *output)
3110 return __get_elide(symbol_conf.sym_list, "symbol", output);
3112 return __get_elide(symbol_conf.dso_list, "dso", output);
3114 return __get_elide(symbol_conf.comm_list, "comm", output);
3119 if (sort__mode != SORT_MODE__BRANCH)
3123 case HISTC_SYMBOL_FROM:
3124 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
3125 case HISTC_SYMBOL_TO:
3126 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
3127 case HISTC_DSO_FROM:
3128 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
3130 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
3138 void sort__setup_elide(FILE *output)
3140 struct perf_hpp_fmt *fmt;
3141 struct hpp_sort_entry *hse;
3143 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
3144 if (!perf_hpp__is_sort_entry(fmt))
3147 hse = container_of(fmt, struct hpp_sort_entry, hpp);
3148 fmt->elide = get_elide(hse->se->se_width_idx, output);
3152 * It makes no sense to elide all of sort entries.
3153 * Just revert them to show up again.
3155 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
3156 if (!perf_hpp__is_sort_entry(fmt))
3163 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
3164 if (!perf_hpp__is_sort_entry(fmt))
3171 int output_field_add(struct perf_hpp_list *list, char *tok)
3175 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
3176 struct sort_dimension *sd = &common_sort_dimensions[i];
3178 if (strncasecmp(tok, sd->name, strlen(tok)))
3181 return __sort_dimension__add_output(list, sd);
3184 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
3185 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
3187 if (strncasecmp(tok, hd->name, strlen(tok)))
3190 return __hpp_dimension__add_output(list, hd);
3193 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
3194 struct sort_dimension *sd = &bstack_sort_dimensions[i];
3196 if (strncasecmp(tok, sd->name, strlen(tok)))
3199 if (sort__mode != SORT_MODE__BRANCH)
3202 return __sort_dimension__add_output(list, sd);
3205 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
3206 struct sort_dimension *sd = &memory_sort_dimensions[i];
3208 if (strncasecmp(tok, sd->name, strlen(tok)))
3211 if (sort__mode != SORT_MODE__MEMORY)
3214 return __sort_dimension__add_output(list, sd);
3220 static int setup_output_list(struct perf_hpp_list *list, char *str)
3225 for (tok = strtok_r(str, ", ", &tmp);
3226 tok; tok = strtok_r(NULL, ", ", &tmp)) {
3227 ret = output_field_add(list, tok);
3228 if (ret == -EINVAL) {
3229 ui__error("Invalid --fields key: `%s'", tok);
3231 } else if (ret == -ESRCH) {
3232 ui__error("Unknown --fields key: `%s'", tok);
3240 void reset_dimensions(void)
3244 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
3245 common_sort_dimensions[i].taken = 0;
3247 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
3248 hpp_sort_dimensions[i].taken = 0;
3250 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
3251 bstack_sort_dimensions[i].taken = 0;
3253 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
3254 memory_sort_dimensions[i].taken = 0;
3257 bool is_strict_order(const char *order)
3259 return order && (*order != '+');
3262 static int __setup_output_field(void)
3267 if (field_order == NULL)
3270 strp = str = strdup(field_order);
3272 pr_err("Not enough memory to setup output fields");
3276 if (!is_strict_order(field_order))
3279 if (!strlen(strp)) {
3280 ui__error("Invalid --fields key: `+'");
3284 ret = setup_output_list(&perf_hpp_list, strp);
3291 int setup_sorting(struct evlist *evlist)
3295 err = __setup_sorting(evlist);
3299 if (parent_pattern != default_parent_pattern) {
3300 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
3308 * perf diff doesn't use default hpp output fields.
3310 if (sort__mode != SORT_MODE__DIFF)
3313 err = __setup_output_field();
3317 /* copy sort keys to output fields */
3318 perf_hpp__setup_output_field(&perf_hpp_list);
3319 /* and then copy output fields to sort keys */
3320 perf_hpp__append_sort_keys(&perf_hpp_list);
3322 /* setup hists-specific output fields */
3323 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
3329 void reset_output_field(void)
3331 perf_hpp_list.need_collapse = 0;
3332 perf_hpp_list.parent = 0;
3333 perf_hpp_list.sym = 0;
3334 perf_hpp_list.dso = 0;
3340 perf_hpp__reset_output_field(&perf_hpp_list);
3343 #define INDENT (3*8 + 1)
3345 static void add_key(struct strbuf *sb, const char *str, int *llen)
3348 strbuf_addstr(sb, "\n\t\t\t ");
3351 strbuf_addf(sb, " %s", str);
3352 *llen += strlen(str) + 1;
3355 static void add_sort_string(struct strbuf *sb, struct sort_dimension *s, int n,
3360 for (i = 0; i < n; i++)
3361 add_key(sb, s[i].name, llen);
3364 static void add_hpp_sort_string(struct strbuf *sb, struct hpp_dimension *s, int n,
3369 for (i = 0; i < n; i++)
3370 add_key(sb, s[i].name, llen);
3373 const char *sort_help(const char *prefix)
3377 int len = strlen(prefix) + INDENT;
3379 strbuf_init(&sb, 300);
3380 strbuf_addstr(&sb, prefix);
3381 add_hpp_sort_string(&sb, hpp_sort_dimensions,
3382 ARRAY_SIZE(hpp_sort_dimensions), &len);
3383 add_sort_string(&sb, common_sort_dimensions,
3384 ARRAY_SIZE(common_sort_dimensions), &len);
3385 add_sort_string(&sb, bstack_sort_dimensions,
3386 ARRAY_SIZE(bstack_sort_dimensions), &len);
3387 add_sort_string(&sb, memory_sort_dimensions,
3388 ARRAY_SIZE(memory_sort_dimensions), &len);
3389 s = strbuf_detach(&sb, NULL);
3390 strbuf_release(&sb);