Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / sort.c
blobd42339df20f8d706091cbc8aff817776b00563ac
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <regex.h>
5 #include <stdlib.h>
6 #include <linux/mman.h>
7 #include <linux/time64.h>
8 #include "debug.h"
9 #include "dso.h"
10 #include "sort.h"
11 #include "hist.h"
12 #include "cacheline.h"
13 #include "comm.h"
14 #include "map.h"
15 #include "maps.h"
16 #include "symbol.h"
17 #include "map_symbol.h"
18 #include "branch.h"
19 #include "thread.h"
20 #include "evsel.h"
21 #include "evlist.h"
22 #include "srcline.h"
23 #include "strlist.h"
24 #include "strbuf.h"
25 #include <traceevent/event-parse.h>
26 #include "mem-events.h"
27 #include "annotate.h"
28 #include "time-utils.h"
29 #include "cgroup.h"
30 #include "machine.h"
31 #include <linux/kernel.h>
32 #include <linux/string.h>
34 regex_t parent_regex;
35 const char default_parent_pattern[] = "^sys_|^do_page_fault";
36 const char *parent_pattern = default_parent_pattern;
37 const char *default_sort_order = "comm,dso,symbol";
38 const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
39 const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
40 const char default_top_sort_order[] = "dso,symbol";
41 const char default_diff_sort_order[] = "dso,symbol";
42 const char default_tracepoint_sort_order[] = "trace";
43 const char *sort_order;
44 const char *field_order;
45 regex_t ignore_callees_regex;
46 int have_ignore_callees = 0;
47 enum sort_mode sort__mode = SORT_MODE__NORMAL;
50 * Replaces all occurrences of a char used with the:
52 * -t, --field-separator
54 * option, that uses a special separator character and don't pad with spaces,
55 * replacing all occurrences of this separator in symbol names (and other
56 * output) with a '.' character, that thus it's the only non valid separator.
58 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
60 int n;
61 va_list ap;
63 va_start(ap, fmt);
64 n = vsnprintf(bf, size, fmt, ap);
65 if (symbol_conf.field_sep && n > 0) {
66 char *sep = bf;
68 while (1) {
69 sep = strchr(sep, *symbol_conf.field_sep);
70 if (sep == NULL)
71 break;
72 *sep = '.';
75 va_end(ap);
77 if (n >= (int)size)
78 return size - 1;
79 return n;
82 static int64_t cmp_null(const void *l, const void *r)
84 if (!l && !r)
85 return 0;
86 else if (!l)
87 return -1;
88 else
89 return 1;
92 /* --sort pid */
94 static int64_t
95 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
97 return right->thread->tid - left->thread->tid;
100 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
101 size_t size, unsigned int width)
103 const char *comm = thread__comm_str(he->thread);
105 width = max(7U, width) - 8;
106 return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid,
107 width, width, comm ?: "");
110 static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
112 const struct thread *th = arg;
114 if (type != HIST_FILTER__THREAD)
115 return -1;
117 return th && he->thread != th;
120 struct sort_entry sort_thread = {
121 .se_header = " Pid:Command",
122 .se_cmp = sort__thread_cmp,
123 .se_snprintf = hist_entry__thread_snprintf,
124 .se_filter = hist_entry__thread_filter,
125 .se_width_idx = HISTC_THREAD,
128 /* --sort comm */
131 * We can't use pointer comparison in functions below,
132 * because it gives different results based on pointer
133 * values, which could break some sorting assumptions.
135 static int64_t
136 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
138 return strcmp(comm__str(right->comm), comm__str(left->comm));
141 static int64_t
142 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
144 return strcmp(comm__str(right->comm), comm__str(left->comm));
147 static int64_t
148 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
150 return strcmp(comm__str(right->comm), comm__str(left->comm));
153 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
154 size_t size, unsigned int width)
156 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
159 struct sort_entry sort_comm = {
160 .se_header = "Command",
161 .se_cmp = sort__comm_cmp,
162 .se_collapse = sort__comm_collapse,
163 .se_sort = sort__comm_sort,
164 .se_snprintf = hist_entry__comm_snprintf,
165 .se_filter = hist_entry__thread_filter,
166 .se_width_idx = HISTC_COMM,
169 /* --sort dso */
171 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
173 struct dso *dso_l = map_l ? map_l->dso : NULL;
174 struct dso *dso_r = map_r ? map_r->dso : NULL;
175 const char *dso_name_l, *dso_name_r;
177 if (!dso_l || !dso_r)
178 return cmp_null(dso_r, dso_l);
180 if (verbose > 0) {
181 dso_name_l = dso_l->long_name;
182 dso_name_r = dso_r->long_name;
183 } else {
184 dso_name_l = dso_l->short_name;
185 dso_name_r = dso_r->short_name;
188 return strcmp(dso_name_l, dso_name_r);
191 static int64_t
192 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
194 return _sort__dso_cmp(right->ms.map, left->ms.map);
197 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
198 size_t size, unsigned int width)
200 if (map && map->dso) {
201 const char *dso_name = verbose > 0 ? map->dso->long_name :
202 map->dso->short_name;
203 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
206 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
209 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
210 size_t size, unsigned int width)
212 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
215 static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
217 const struct dso *dso = arg;
219 if (type != HIST_FILTER__DSO)
220 return -1;
222 return dso && (!he->ms.map || he->ms.map->dso != dso);
225 struct sort_entry sort_dso = {
226 .se_header = "Shared Object",
227 .se_cmp = sort__dso_cmp,
228 .se_snprintf = hist_entry__dso_snprintf,
229 .se_filter = hist_entry__dso_filter,
230 .se_width_idx = HISTC_DSO,
233 /* --sort symbol */
235 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
237 return (int64_t)(right_ip - left_ip);
240 int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
242 if (!sym_l || !sym_r)
243 return cmp_null(sym_l, sym_r);
245 if (sym_l == sym_r)
246 return 0;
248 if (sym_l->inlined || sym_r->inlined) {
249 int ret = strcmp(sym_l->name, sym_r->name);
251 if (ret)
252 return ret;
253 if ((sym_l->start <= sym_r->end) && (sym_l->end >= sym_r->start))
254 return 0;
257 if (sym_l->start != sym_r->start)
258 return (int64_t)(sym_r->start - sym_l->start);
260 return (int64_t)(sym_r->end - sym_l->end);
263 static int64_t
264 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
266 int64_t ret;
268 if (!left->ms.sym && !right->ms.sym)
269 return _sort__addr_cmp(left->ip, right->ip);
272 * comparing symbol address alone is not enough since it's a
273 * relative address within a dso.
275 if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
276 ret = sort__dso_cmp(left, right);
277 if (ret != 0)
278 return ret;
281 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
284 static int64_t
285 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
287 if (!left->ms.sym || !right->ms.sym)
288 return cmp_null(left->ms.sym, right->ms.sym);
290 return strcmp(right->ms.sym->name, left->ms.sym->name);
293 static int _hist_entry__sym_snprintf(struct map_symbol *ms,
294 u64 ip, char level, char *bf, size_t size,
295 unsigned int width)
297 struct symbol *sym = ms->sym;
298 struct map *map = ms->map;
299 size_t ret = 0;
301 if (verbose > 0) {
302 char o = map ? dso__symtab_origin(map->dso) : '!';
303 u64 rip = ip;
305 if (map && map->dso && map->dso->kernel
306 && map->dso->adjust_symbols)
307 rip = map->unmap_ip(map, ip);
309 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
310 BITS_PER_LONG / 4 + 2, rip, o);
313 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
314 if (sym && map) {
315 if (sym->type == STT_OBJECT) {
316 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
317 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
318 ip - map->unmap_ip(map, sym->start));
319 } else {
320 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
321 width - ret,
322 sym->name);
323 if (sym->inlined)
324 ret += repsep_snprintf(bf + ret, size - ret,
325 " (inlined)");
327 } else {
328 size_t len = BITS_PER_LONG / 4;
329 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
330 len, ip);
333 return ret;
336 int hist_entry__sym_snprintf(struct hist_entry *he, char *bf, size_t size, unsigned int width)
338 return _hist_entry__sym_snprintf(&he->ms, he->ip,
339 he->level, bf, size, width);
342 static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
344 const char *sym = arg;
346 if (type != HIST_FILTER__SYMBOL)
347 return -1;
349 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
352 struct sort_entry sort_sym = {
353 .se_header = "Symbol",
354 .se_cmp = sort__sym_cmp,
355 .se_sort = sort__sym_sort,
356 .se_snprintf = hist_entry__sym_snprintf,
357 .se_filter = hist_entry__sym_filter,
358 .se_width_idx = HISTC_SYMBOL,
361 /* --sort srcline */
363 char *hist_entry__srcline(struct hist_entry *he)
365 return map__srcline(he->ms.map, he->ip, he->ms.sym);
368 static int64_t
369 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
371 if (!left->srcline)
372 left->srcline = hist_entry__srcline(left);
373 if (!right->srcline)
374 right->srcline = hist_entry__srcline(right);
376 return strcmp(right->srcline, left->srcline);
379 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
380 size_t size, unsigned int width)
382 if (!he->srcline)
383 he->srcline = hist_entry__srcline(he);
385 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
388 struct sort_entry sort_srcline = {
389 .se_header = "Source:Line",
390 .se_cmp = sort__srcline_cmp,
391 .se_snprintf = hist_entry__srcline_snprintf,
392 .se_width_idx = HISTC_SRCLINE,
395 /* --sort srcline_from */
397 static char *addr_map_symbol__srcline(struct addr_map_symbol *ams)
399 return map__srcline(ams->ms.map, ams->al_addr, ams->ms.sym);
402 static int64_t
403 sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
405 if (!left->branch_info->srcline_from)
406 left->branch_info->srcline_from = addr_map_symbol__srcline(&left->branch_info->from);
408 if (!right->branch_info->srcline_from)
409 right->branch_info->srcline_from = addr_map_symbol__srcline(&right->branch_info->from);
411 return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
414 static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
415 size_t size, unsigned int width)
417 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
420 struct sort_entry sort_srcline_from = {
421 .se_header = "From Source:Line",
422 .se_cmp = sort__srcline_from_cmp,
423 .se_snprintf = hist_entry__srcline_from_snprintf,
424 .se_width_idx = HISTC_SRCLINE_FROM,
427 /* --sort srcline_to */
429 static int64_t
430 sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
432 if (!left->branch_info->srcline_to)
433 left->branch_info->srcline_to = addr_map_symbol__srcline(&left->branch_info->to);
435 if (!right->branch_info->srcline_to)
436 right->branch_info->srcline_to = addr_map_symbol__srcline(&right->branch_info->to);
438 return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
441 static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
442 size_t size, unsigned int width)
444 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
447 struct sort_entry sort_srcline_to = {
448 .se_header = "To Source:Line",
449 .se_cmp = sort__srcline_to_cmp,
450 .se_snprintf = hist_entry__srcline_to_snprintf,
451 .se_width_idx = HISTC_SRCLINE_TO,
454 static int hist_entry__sym_ipc_snprintf(struct hist_entry *he, char *bf,
455 size_t size, unsigned int width)
458 struct symbol *sym = he->ms.sym;
459 struct annotation *notes;
460 double ipc = 0.0, coverage = 0.0;
461 char tmp[64];
463 if (!sym)
464 return repsep_snprintf(bf, size, "%-*s", width, "-");
466 notes = symbol__annotation(sym);
468 if (notes->hit_cycles)
469 ipc = notes->hit_insn / ((double)notes->hit_cycles);
471 if (notes->total_insn) {
472 coverage = notes->cover_insn * 100.0 /
473 ((double)notes->total_insn);
476 snprintf(tmp, sizeof(tmp), "%-5.2f [%5.1f%%]", ipc, coverage);
477 return repsep_snprintf(bf, size, "%-*s", width, tmp);
480 struct sort_entry sort_sym_ipc = {
481 .se_header = "IPC [IPC Coverage]",
482 .se_cmp = sort__sym_cmp,
483 .se_snprintf = hist_entry__sym_ipc_snprintf,
484 .se_width_idx = HISTC_SYMBOL_IPC,
487 static int hist_entry__sym_ipc_null_snprintf(struct hist_entry *he
488 __maybe_unused,
489 char *bf, size_t size,
490 unsigned int width)
492 char tmp[64];
494 snprintf(tmp, sizeof(tmp), "%-5s %2s", "-", "-");
495 return repsep_snprintf(bf, size, "%-*s", width, tmp);
498 struct sort_entry sort_sym_ipc_null = {
499 .se_header = "IPC [IPC Coverage]",
500 .se_cmp = sort__sym_cmp,
501 .se_snprintf = hist_entry__sym_ipc_null_snprintf,
502 .se_width_idx = HISTC_SYMBOL_IPC,
505 /* --sort srcfile */
507 static char no_srcfile[1];
509 static char *hist_entry__get_srcfile(struct hist_entry *e)
511 char *sf, *p;
512 struct map *map = e->ms.map;
514 if (!map)
515 return no_srcfile;
517 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
518 e->ms.sym, false, true, true, e->ip);
519 if (!strcmp(sf, SRCLINE_UNKNOWN))
520 return no_srcfile;
521 p = strchr(sf, ':');
522 if (p && *sf) {
523 *p = 0;
524 return sf;
526 free(sf);
527 return no_srcfile;
530 static int64_t
531 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
533 if (!left->srcfile)
534 left->srcfile = hist_entry__get_srcfile(left);
535 if (!right->srcfile)
536 right->srcfile = hist_entry__get_srcfile(right);
538 return strcmp(right->srcfile, left->srcfile);
541 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
542 size_t size, unsigned int width)
544 if (!he->srcfile)
545 he->srcfile = hist_entry__get_srcfile(he);
547 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
550 struct sort_entry sort_srcfile = {
551 .se_header = "Source File",
552 .se_cmp = sort__srcfile_cmp,
553 .se_snprintf = hist_entry__srcfile_snprintf,
554 .se_width_idx = HISTC_SRCFILE,
557 /* --sort parent */
559 static int64_t
560 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
562 struct symbol *sym_l = left->parent;
563 struct symbol *sym_r = right->parent;
565 if (!sym_l || !sym_r)
566 return cmp_null(sym_l, sym_r);
568 return strcmp(sym_r->name, sym_l->name);
571 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
572 size_t size, unsigned int width)
574 return repsep_snprintf(bf, size, "%-*.*s", width, width,
575 he->parent ? he->parent->name : "[other]");
578 struct sort_entry sort_parent = {
579 .se_header = "Parent symbol",
580 .se_cmp = sort__parent_cmp,
581 .se_snprintf = hist_entry__parent_snprintf,
582 .se_width_idx = HISTC_PARENT,
585 /* --sort cpu */
587 static int64_t
588 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
590 return right->cpu - left->cpu;
593 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
594 size_t size, unsigned int width)
596 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
599 struct sort_entry sort_cpu = {
600 .se_header = "CPU",
601 .se_cmp = sort__cpu_cmp,
602 .se_snprintf = hist_entry__cpu_snprintf,
603 .se_width_idx = HISTC_CPU,
606 /* --sort cgroup_id */
608 static int64_t _sort__cgroup_dev_cmp(u64 left_dev, u64 right_dev)
610 return (int64_t)(right_dev - left_dev);
613 static int64_t _sort__cgroup_inode_cmp(u64 left_ino, u64 right_ino)
615 return (int64_t)(right_ino - left_ino);
618 static int64_t
619 sort__cgroup_id_cmp(struct hist_entry *left, struct hist_entry *right)
621 int64_t ret;
623 ret = _sort__cgroup_dev_cmp(right->cgroup_id.dev, left->cgroup_id.dev);
624 if (ret != 0)
625 return ret;
627 return _sort__cgroup_inode_cmp(right->cgroup_id.ino,
628 left->cgroup_id.ino);
631 static int hist_entry__cgroup_id_snprintf(struct hist_entry *he,
632 char *bf, size_t size,
633 unsigned int width __maybe_unused)
635 return repsep_snprintf(bf, size, "%lu/0x%lx", he->cgroup_id.dev,
636 he->cgroup_id.ino);
639 struct sort_entry sort_cgroup_id = {
640 .se_header = "cgroup id (dev/inode)",
641 .se_cmp = sort__cgroup_id_cmp,
642 .se_snprintf = hist_entry__cgroup_id_snprintf,
643 .se_width_idx = HISTC_CGROUP_ID,
646 /* --sort cgroup */
648 static int64_t
649 sort__cgroup_cmp(struct hist_entry *left, struct hist_entry *right)
651 return right->cgroup - left->cgroup;
654 static int hist_entry__cgroup_snprintf(struct hist_entry *he,
655 char *bf, size_t size,
656 unsigned int width __maybe_unused)
658 const char *cgrp_name = "N/A";
660 if (he->cgroup) {
661 struct cgroup *cgrp = cgroup__find(he->ms.maps->machine->env,
662 he->cgroup);
663 if (cgrp != NULL)
664 cgrp_name = cgrp->name;
665 else
666 cgrp_name = "unknown";
669 return repsep_snprintf(bf, size, "%s", cgrp_name);
672 struct sort_entry sort_cgroup = {
673 .se_header = "Cgroup",
674 .se_cmp = sort__cgroup_cmp,
675 .se_snprintf = hist_entry__cgroup_snprintf,
676 .se_width_idx = HISTC_CGROUP,
679 /* --sort socket */
681 static int64_t
682 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
684 return right->socket - left->socket;
687 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
688 size_t size, unsigned int width)
690 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
693 static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
695 int sk = *(const int *)arg;
697 if (type != HIST_FILTER__SOCKET)
698 return -1;
700 return sk >= 0 && he->socket != sk;
703 struct sort_entry sort_socket = {
704 .se_header = "Socket",
705 .se_cmp = sort__socket_cmp,
706 .se_snprintf = hist_entry__socket_snprintf,
707 .se_filter = hist_entry__socket_filter,
708 .se_width_idx = HISTC_SOCKET,
711 /* --sort time */
713 static int64_t
714 sort__time_cmp(struct hist_entry *left, struct hist_entry *right)
716 return right->time - left->time;
719 static int hist_entry__time_snprintf(struct hist_entry *he, char *bf,
720 size_t size, unsigned int width)
722 char he_time[32];
724 if (symbol_conf.nanosecs)
725 timestamp__scnprintf_nsec(he->time, he_time,
726 sizeof(he_time));
727 else
728 timestamp__scnprintf_usec(he->time, he_time,
729 sizeof(he_time));
731 return repsep_snprintf(bf, size, "%-.*s", width, he_time);
734 struct sort_entry sort_time = {
735 .se_header = "Time",
736 .se_cmp = sort__time_cmp,
737 .se_snprintf = hist_entry__time_snprintf,
738 .se_width_idx = HISTC_TIME,
741 /* --sort trace */
743 static char *get_trace_output(struct hist_entry *he)
745 struct trace_seq seq;
746 struct evsel *evsel;
747 struct tep_record rec = {
748 .data = he->raw_data,
749 .size = he->raw_size,
752 evsel = hists_to_evsel(he->hists);
754 trace_seq_init(&seq);
755 if (symbol_conf.raw_trace) {
756 tep_print_fields(&seq, he->raw_data, he->raw_size,
757 evsel->tp_format);
758 } else {
759 tep_print_event(evsel->tp_format->tep,
760 &seq, &rec, "%s", TEP_PRINT_INFO);
763 * Trim the buffer, it starts at 4KB and we're not going to
764 * add anything more to this buffer.
766 return realloc(seq.buffer, seq.len + 1);
769 static int64_t
770 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
772 struct evsel *evsel;
774 evsel = hists_to_evsel(left->hists);
775 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
776 return 0;
778 if (left->trace_output == NULL)
779 left->trace_output = get_trace_output(left);
780 if (right->trace_output == NULL)
781 right->trace_output = get_trace_output(right);
783 return strcmp(right->trace_output, left->trace_output);
786 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
787 size_t size, unsigned int width)
789 struct evsel *evsel;
791 evsel = hists_to_evsel(he->hists);
792 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
793 return scnprintf(bf, size, "%-.*s", width, "N/A");
795 if (he->trace_output == NULL)
796 he->trace_output = get_trace_output(he);
797 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
800 struct sort_entry sort_trace = {
801 .se_header = "Trace output",
802 .se_cmp = sort__trace_cmp,
803 .se_snprintf = hist_entry__trace_snprintf,
804 .se_width_idx = HISTC_TRACE,
807 /* sort keys for branch stacks */
809 static int64_t
810 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
812 if (!left->branch_info || !right->branch_info)
813 return cmp_null(left->branch_info, right->branch_info);
815 return _sort__dso_cmp(left->branch_info->from.ms.map,
816 right->branch_info->from.ms.map);
819 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
820 size_t size, unsigned int width)
822 if (he->branch_info)
823 return _hist_entry__dso_snprintf(he->branch_info->from.ms.map,
824 bf, size, width);
825 else
826 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
829 static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
830 const void *arg)
832 const struct dso *dso = arg;
834 if (type != HIST_FILTER__DSO)
835 return -1;
837 return dso && (!he->branch_info || !he->branch_info->from.ms.map ||
838 he->branch_info->from.ms.map->dso != dso);
841 static int64_t
842 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
844 if (!left->branch_info || !right->branch_info)
845 return cmp_null(left->branch_info, right->branch_info);
847 return _sort__dso_cmp(left->branch_info->to.ms.map,
848 right->branch_info->to.ms.map);
851 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
852 size_t size, unsigned int width)
854 if (he->branch_info)
855 return _hist_entry__dso_snprintf(he->branch_info->to.ms.map,
856 bf, size, width);
857 else
858 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
861 static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
862 const void *arg)
864 const struct dso *dso = arg;
866 if (type != HIST_FILTER__DSO)
867 return -1;
869 return dso && (!he->branch_info || !he->branch_info->to.ms.map ||
870 he->branch_info->to.ms.map->dso != dso);
873 static int64_t
874 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
876 struct addr_map_symbol *from_l = &left->branch_info->from;
877 struct addr_map_symbol *from_r = &right->branch_info->from;
879 if (!left->branch_info || !right->branch_info)
880 return cmp_null(left->branch_info, right->branch_info);
882 from_l = &left->branch_info->from;
883 from_r = &right->branch_info->from;
885 if (!from_l->ms.sym && !from_r->ms.sym)
886 return _sort__addr_cmp(from_l->addr, from_r->addr);
888 return _sort__sym_cmp(from_l->ms.sym, from_r->ms.sym);
891 static int64_t
892 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
894 struct addr_map_symbol *to_l, *to_r;
896 if (!left->branch_info || !right->branch_info)
897 return cmp_null(left->branch_info, right->branch_info);
899 to_l = &left->branch_info->to;
900 to_r = &right->branch_info->to;
902 if (!to_l->ms.sym && !to_r->ms.sym)
903 return _sort__addr_cmp(to_l->addr, to_r->addr);
905 return _sort__sym_cmp(to_l->ms.sym, to_r->ms.sym);
908 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
909 size_t size, unsigned int width)
911 if (he->branch_info) {
912 struct addr_map_symbol *from = &he->branch_info->from;
914 return _hist_entry__sym_snprintf(&from->ms, from->al_addr,
915 he->level, bf, size, width);
918 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
921 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
922 size_t size, unsigned int width)
924 if (he->branch_info) {
925 struct addr_map_symbol *to = &he->branch_info->to;
927 return _hist_entry__sym_snprintf(&to->ms, to->al_addr,
928 he->level, bf, size, width);
931 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
934 static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
935 const void *arg)
937 const char *sym = arg;
939 if (type != HIST_FILTER__SYMBOL)
940 return -1;
942 return sym && !(he->branch_info && he->branch_info->from.ms.sym &&
943 strstr(he->branch_info->from.ms.sym->name, sym));
946 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
947 const void *arg)
949 const char *sym = arg;
951 if (type != HIST_FILTER__SYMBOL)
952 return -1;
954 return sym && !(he->branch_info && he->branch_info->to.ms.sym &&
955 strstr(he->branch_info->to.ms.sym->name, sym));
958 struct sort_entry sort_dso_from = {
959 .se_header = "Source Shared Object",
960 .se_cmp = sort__dso_from_cmp,
961 .se_snprintf = hist_entry__dso_from_snprintf,
962 .se_filter = hist_entry__dso_from_filter,
963 .se_width_idx = HISTC_DSO_FROM,
966 struct sort_entry sort_dso_to = {
967 .se_header = "Target Shared Object",
968 .se_cmp = sort__dso_to_cmp,
969 .se_snprintf = hist_entry__dso_to_snprintf,
970 .se_filter = hist_entry__dso_to_filter,
971 .se_width_idx = HISTC_DSO_TO,
974 struct sort_entry sort_sym_from = {
975 .se_header = "Source Symbol",
976 .se_cmp = sort__sym_from_cmp,
977 .se_snprintf = hist_entry__sym_from_snprintf,
978 .se_filter = hist_entry__sym_from_filter,
979 .se_width_idx = HISTC_SYMBOL_FROM,
982 struct sort_entry sort_sym_to = {
983 .se_header = "Target Symbol",
984 .se_cmp = sort__sym_to_cmp,
985 .se_snprintf = hist_entry__sym_to_snprintf,
986 .se_filter = hist_entry__sym_to_filter,
987 .se_width_idx = HISTC_SYMBOL_TO,
990 static int64_t
991 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
993 unsigned char mp, p;
995 if (!left->branch_info || !right->branch_info)
996 return cmp_null(left->branch_info, right->branch_info);
998 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
999 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
1000 return mp || p;
1003 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
1004 size_t size, unsigned int width){
1005 static const char *out = "N/A";
1007 if (he->branch_info) {
1008 if (he->branch_info->flags.predicted)
1009 out = "N";
1010 else if (he->branch_info->flags.mispred)
1011 out = "Y";
1014 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
1017 static int64_t
1018 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
1020 if (!left->branch_info || !right->branch_info)
1021 return cmp_null(left->branch_info, right->branch_info);
1023 return left->branch_info->flags.cycles -
1024 right->branch_info->flags.cycles;
1027 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
1028 size_t size, unsigned int width)
1030 if (!he->branch_info)
1031 return scnprintf(bf, size, "%-.*s", width, "N/A");
1032 if (he->branch_info->flags.cycles == 0)
1033 return repsep_snprintf(bf, size, "%-*s", width, "-");
1034 return repsep_snprintf(bf, size, "%-*hd", width,
1035 he->branch_info->flags.cycles);
1038 struct sort_entry sort_cycles = {
1039 .se_header = "Basic Block Cycles",
1040 .se_cmp = sort__cycles_cmp,
1041 .se_snprintf = hist_entry__cycles_snprintf,
1042 .se_width_idx = HISTC_CYCLES,
1045 /* --sort daddr_sym */
1046 int64_t
1047 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1049 uint64_t l = 0, r = 0;
1051 if (left->mem_info)
1052 l = left->mem_info->daddr.addr;
1053 if (right->mem_info)
1054 r = right->mem_info->daddr.addr;
1056 return (int64_t)(r - l);
1059 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
1060 size_t size, unsigned int width)
1062 uint64_t addr = 0;
1063 struct map_symbol *ms = NULL;
1065 if (he->mem_info) {
1066 addr = he->mem_info->daddr.addr;
1067 ms = &he->mem_info->daddr.ms;
1069 return _hist_entry__sym_snprintf(ms, addr, he->level, bf, size, width);
1072 int64_t
1073 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
1075 uint64_t l = 0, r = 0;
1077 if (left->mem_info)
1078 l = left->mem_info->iaddr.addr;
1079 if (right->mem_info)
1080 r = right->mem_info->iaddr.addr;
1082 return (int64_t)(r - l);
1085 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
1086 size_t size, unsigned int width)
1088 uint64_t addr = 0;
1089 struct map_symbol *ms = NULL;
1091 if (he->mem_info) {
1092 addr = he->mem_info->iaddr.addr;
1093 ms = &he->mem_info->iaddr.ms;
1095 return _hist_entry__sym_snprintf(ms, addr, he->level, bf, size, width);
1098 static int64_t
1099 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1101 struct map *map_l = NULL;
1102 struct map *map_r = NULL;
1104 if (left->mem_info)
1105 map_l = left->mem_info->daddr.ms.map;
1106 if (right->mem_info)
1107 map_r = right->mem_info->daddr.ms.map;
1109 return _sort__dso_cmp(map_l, map_r);
1112 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
1113 size_t size, unsigned int width)
1115 struct map *map = NULL;
1117 if (he->mem_info)
1118 map = he->mem_info->daddr.ms.map;
1120 return _hist_entry__dso_snprintf(map, bf, size, width);
1123 static int64_t
1124 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
1126 union perf_mem_data_src data_src_l;
1127 union perf_mem_data_src data_src_r;
1129 if (left->mem_info)
1130 data_src_l = left->mem_info->data_src;
1131 else
1132 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
1134 if (right->mem_info)
1135 data_src_r = right->mem_info->data_src;
1136 else
1137 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
1139 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
1142 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
1143 size_t size, unsigned int width)
1145 char out[10];
1147 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
1148 return repsep_snprintf(bf, size, "%.*s", width, out);
1151 static int64_t
1152 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
1154 union perf_mem_data_src data_src_l;
1155 union perf_mem_data_src data_src_r;
1157 if (left->mem_info)
1158 data_src_l = left->mem_info->data_src;
1159 else
1160 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
1162 if (right->mem_info)
1163 data_src_r = right->mem_info->data_src;
1164 else
1165 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
1167 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
1170 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
1171 size_t size, unsigned int width)
1173 char out[64];
1175 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
1176 return repsep_snprintf(bf, size, "%-*s", width, out);
1179 static int64_t
1180 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
1182 union perf_mem_data_src data_src_l;
1183 union perf_mem_data_src data_src_r;
1185 if (left->mem_info)
1186 data_src_l = left->mem_info->data_src;
1187 else
1188 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
1190 if (right->mem_info)
1191 data_src_r = right->mem_info->data_src;
1192 else
1193 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
1195 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
1198 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
1199 size_t size, unsigned int width)
1201 char out[64];
1203 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
1204 return repsep_snprintf(bf, size, "%-*s", width, out);
1207 static int64_t
1208 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
1210 union perf_mem_data_src data_src_l;
1211 union perf_mem_data_src data_src_r;
1213 if (left->mem_info)
1214 data_src_l = left->mem_info->data_src;
1215 else
1216 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
1218 if (right->mem_info)
1219 data_src_r = right->mem_info->data_src;
1220 else
1221 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
1223 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
1226 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
1227 size_t size, unsigned int width)
1229 char out[64];
1231 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
1232 return repsep_snprintf(bf, size, "%-*s", width, out);
1235 int64_t
1236 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
1238 u64 l, r;
1239 struct map *l_map, *r_map;
1240 int rc;
1242 if (!left->mem_info) return -1;
1243 if (!right->mem_info) return 1;
1245 /* group event types together */
1246 if (left->cpumode > right->cpumode) return -1;
1247 if (left->cpumode < right->cpumode) return 1;
1249 l_map = left->mem_info->daddr.ms.map;
1250 r_map = right->mem_info->daddr.ms.map;
1252 /* if both are NULL, jump to sort on al_addr instead */
1253 if (!l_map && !r_map)
1254 goto addr;
1256 if (!l_map) return -1;
1257 if (!r_map) return 1;
1259 rc = dso__cmp_id(l_map->dso, r_map->dso);
1260 if (rc)
1261 return rc;
1263 * Addresses with no major/minor numbers are assumed to be
1264 * anonymous in userspace. Sort those on pid then address.
1266 * The kernel and non-zero major/minor mapped areas are
1267 * assumed to be unity mapped. Sort those on address.
1270 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1271 (!(l_map->flags & MAP_SHARED)) &&
1272 !l_map->dso->id.maj && !l_map->dso->id.min &&
1273 !l_map->dso->id.ino && !l_map->dso->id.ino_generation) {
1274 /* userspace anonymous */
1276 if (left->thread->pid_ > right->thread->pid_) return -1;
1277 if (left->thread->pid_ < right->thread->pid_) return 1;
1280 addr:
1281 /* al_addr does all the right addr - start + offset calculations */
1282 l = cl_address(left->mem_info->daddr.al_addr);
1283 r = cl_address(right->mem_info->daddr.al_addr);
1285 if (l > r) return -1;
1286 if (l < r) return 1;
1288 return 0;
1291 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1292 size_t size, unsigned int width)
1295 uint64_t addr = 0;
1296 struct map_symbol *ms = NULL;
1297 char level = he->level;
1299 if (he->mem_info) {
1300 struct map *map = he->mem_info->daddr.ms.map;
1302 addr = cl_address(he->mem_info->daddr.al_addr);
1303 ms = &he->mem_info->daddr.ms;
1305 /* print [s] for shared data mmaps */
1306 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1307 map && !(map->prot & PROT_EXEC) &&
1308 (map->flags & MAP_SHARED) &&
1309 (map->dso->id.maj || map->dso->id.min ||
1310 map->dso->id.ino || map->dso->id.ino_generation))
1311 level = 's';
1312 else if (!map)
1313 level = 'X';
1315 return _hist_entry__sym_snprintf(ms, addr, level, bf, size, width);
1318 struct sort_entry sort_mispredict = {
1319 .se_header = "Branch Mispredicted",
1320 .se_cmp = sort__mispredict_cmp,
1321 .se_snprintf = hist_entry__mispredict_snprintf,
1322 .se_width_idx = HISTC_MISPREDICT,
1325 static u64 he_weight(struct hist_entry *he)
1327 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1330 static int64_t
1331 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1333 return he_weight(left) - he_weight(right);
1336 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1337 size_t size, unsigned int width)
1339 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1342 struct sort_entry sort_local_weight = {
1343 .se_header = "Local Weight",
1344 .se_cmp = sort__local_weight_cmp,
1345 .se_snprintf = hist_entry__local_weight_snprintf,
1346 .se_width_idx = HISTC_LOCAL_WEIGHT,
1349 static int64_t
1350 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1352 return left->stat.weight - right->stat.weight;
1355 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1356 size_t size, unsigned int width)
1358 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1361 struct sort_entry sort_global_weight = {
1362 .se_header = "Weight",
1363 .se_cmp = sort__global_weight_cmp,
1364 .se_snprintf = hist_entry__global_weight_snprintf,
1365 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1368 struct sort_entry sort_mem_daddr_sym = {
1369 .se_header = "Data Symbol",
1370 .se_cmp = sort__daddr_cmp,
1371 .se_snprintf = hist_entry__daddr_snprintf,
1372 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1375 struct sort_entry sort_mem_iaddr_sym = {
1376 .se_header = "Code Symbol",
1377 .se_cmp = sort__iaddr_cmp,
1378 .se_snprintf = hist_entry__iaddr_snprintf,
1379 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1382 struct sort_entry sort_mem_daddr_dso = {
1383 .se_header = "Data Object",
1384 .se_cmp = sort__dso_daddr_cmp,
1385 .se_snprintf = hist_entry__dso_daddr_snprintf,
1386 .se_width_idx = HISTC_MEM_DADDR_DSO,
1389 struct sort_entry sort_mem_locked = {
1390 .se_header = "Locked",
1391 .se_cmp = sort__locked_cmp,
1392 .se_snprintf = hist_entry__locked_snprintf,
1393 .se_width_idx = HISTC_MEM_LOCKED,
1396 struct sort_entry sort_mem_tlb = {
1397 .se_header = "TLB access",
1398 .se_cmp = sort__tlb_cmp,
1399 .se_snprintf = hist_entry__tlb_snprintf,
1400 .se_width_idx = HISTC_MEM_TLB,
1403 struct sort_entry sort_mem_lvl = {
1404 .se_header = "Memory access",
1405 .se_cmp = sort__lvl_cmp,
1406 .se_snprintf = hist_entry__lvl_snprintf,
1407 .se_width_idx = HISTC_MEM_LVL,
1410 struct sort_entry sort_mem_snoop = {
1411 .se_header = "Snoop",
1412 .se_cmp = sort__snoop_cmp,
1413 .se_snprintf = hist_entry__snoop_snprintf,
1414 .se_width_idx = HISTC_MEM_SNOOP,
1417 struct sort_entry sort_mem_dcacheline = {
1418 .se_header = "Data Cacheline",
1419 .se_cmp = sort__dcacheline_cmp,
1420 .se_snprintf = hist_entry__dcacheline_snprintf,
1421 .se_width_idx = HISTC_MEM_DCACHELINE,
1424 static int64_t
1425 sort__phys_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1427 uint64_t l = 0, r = 0;
1429 if (left->mem_info)
1430 l = left->mem_info->daddr.phys_addr;
1431 if (right->mem_info)
1432 r = right->mem_info->daddr.phys_addr;
1434 return (int64_t)(r - l);
1437 static int hist_entry__phys_daddr_snprintf(struct hist_entry *he, char *bf,
1438 size_t size, unsigned int width)
1440 uint64_t addr = 0;
1441 size_t ret = 0;
1442 size_t len = BITS_PER_LONG / 4;
1444 addr = he->mem_info->daddr.phys_addr;
1446 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", he->level);
1448 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", len, addr);
1450 ret += repsep_snprintf(bf + ret, size - ret, "%-*s", width - ret, "");
1452 if (ret > width)
1453 bf[width] = '\0';
1455 return width;
1458 struct sort_entry sort_mem_phys_daddr = {
1459 .se_header = "Data Physical Address",
1460 .se_cmp = sort__phys_daddr_cmp,
1461 .se_snprintf = hist_entry__phys_daddr_snprintf,
1462 .se_width_idx = HISTC_MEM_PHYS_DADDR,
1465 static int64_t
1466 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1468 if (!left->branch_info || !right->branch_info)
1469 return cmp_null(left->branch_info, right->branch_info);
1471 return left->branch_info->flags.abort !=
1472 right->branch_info->flags.abort;
1475 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1476 size_t size, unsigned int width)
1478 static const char *out = "N/A";
1480 if (he->branch_info) {
1481 if (he->branch_info->flags.abort)
1482 out = "A";
1483 else
1484 out = ".";
1487 return repsep_snprintf(bf, size, "%-*s", width, out);
1490 struct sort_entry sort_abort = {
1491 .se_header = "Transaction abort",
1492 .se_cmp = sort__abort_cmp,
1493 .se_snprintf = hist_entry__abort_snprintf,
1494 .se_width_idx = HISTC_ABORT,
1497 static int64_t
1498 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1500 if (!left->branch_info || !right->branch_info)
1501 return cmp_null(left->branch_info, right->branch_info);
1503 return left->branch_info->flags.in_tx !=
1504 right->branch_info->flags.in_tx;
1507 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1508 size_t size, unsigned int width)
1510 static const char *out = "N/A";
1512 if (he->branch_info) {
1513 if (he->branch_info->flags.in_tx)
1514 out = "T";
1515 else
1516 out = ".";
1519 return repsep_snprintf(bf, size, "%-*s", width, out);
1522 struct sort_entry sort_in_tx = {
1523 .se_header = "Branch in transaction",
1524 .se_cmp = sort__in_tx_cmp,
1525 .se_snprintf = hist_entry__in_tx_snprintf,
1526 .se_width_idx = HISTC_IN_TX,
1529 static int64_t
1530 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1532 return left->transaction - right->transaction;
1535 static inline char *add_str(char *p, const char *str)
1537 strcpy(p, str);
1538 return p + strlen(str);
1541 static struct txbit {
1542 unsigned flag;
1543 const char *name;
1544 int skip_for_len;
1545 } txbits[] = {
1546 { PERF_TXN_ELISION, "EL ", 0 },
1547 { PERF_TXN_TRANSACTION, "TX ", 1 },
1548 { PERF_TXN_SYNC, "SYNC ", 1 },
1549 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1550 { PERF_TXN_RETRY, "RETRY ", 0 },
1551 { PERF_TXN_CONFLICT, "CON ", 0 },
1552 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1553 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1554 { 0, NULL, 0 }
1557 int hist_entry__transaction_len(void)
1559 int i;
1560 int len = 0;
1562 for (i = 0; txbits[i].name; i++) {
1563 if (!txbits[i].skip_for_len)
1564 len += strlen(txbits[i].name);
1566 len += 4; /* :XX<space> */
1567 return len;
1570 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1571 size_t size, unsigned int width)
1573 u64 t = he->transaction;
1574 char buf[128];
1575 char *p = buf;
1576 int i;
1578 buf[0] = 0;
1579 for (i = 0; txbits[i].name; i++)
1580 if (txbits[i].flag & t)
1581 p = add_str(p, txbits[i].name);
1582 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1583 p = add_str(p, "NEITHER ");
1584 if (t & PERF_TXN_ABORT_MASK) {
1585 sprintf(p, ":%" PRIx64,
1586 (t & PERF_TXN_ABORT_MASK) >>
1587 PERF_TXN_ABORT_SHIFT);
1588 p += strlen(p);
1591 return repsep_snprintf(bf, size, "%-*s", width, buf);
1594 struct sort_entry sort_transaction = {
1595 .se_header = "Transaction ",
1596 .se_cmp = sort__transaction_cmp,
1597 .se_snprintf = hist_entry__transaction_snprintf,
1598 .se_width_idx = HISTC_TRANSACTION,
1601 /* --sort symbol_size */
1603 static int64_t _sort__sym_size_cmp(struct symbol *sym_l, struct symbol *sym_r)
1605 int64_t size_l = sym_l != NULL ? symbol__size(sym_l) : 0;
1606 int64_t size_r = sym_r != NULL ? symbol__size(sym_r) : 0;
1608 return size_l < size_r ? -1 :
1609 size_l == size_r ? 0 : 1;
1612 static int64_t
1613 sort__sym_size_cmp(struct hist_entry *left, struct hist_entry *right)
1615 return _sort__sym_size_cmp(right->ms.sym, left->ms.sym);
1618 static int _hist_entry__sym_size_snprintf(struct symbol *sym, char *bf,
1619 size_t bf_size, unsigned int width)
1621 if (sym)
1622 return repsep_snprintf(bf, bf_size, "%*d", width, symbol__size(sym));
1624 return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1627 static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
1628 size_t size, unsigned int width)
1630 return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
1633 struct sort_entry sort_sym_size = {
1634 .se_header = "Symbol size",
1635 .se_cmp = sort__sym_size_cmp,
1636 .se_snprintf = hist_entry__sym_size_snprintf,
1637 .se_width_idx = HISTC_SYM_SIZE,
1640 /* --sort dso_size */
1642 static int64_t _sort__dso_size_cmp(struct map *map_l, struct map *map_r)
1644 int64_t size_l = map_l != NULL ? map__size(map_l) : 0;
1645 int64_t size_r = map_r != NULL ? map__size(map_r) : 0;
1647 return size_l < size_r ? -1 :
1648 size_l == size_r ? 0 : 1;
1651 static int64_t
1652 sort__dso_size_cmp(struct hist_entry *left, struct hist_entry *right)
1654 return _sort__dso_size_cmp(right->ms.map, left->ms.map);
1657 static int _hist_entry__dso_size_snprintf(struct map *map, char *bf,
1658 size_t bf_size, unsigned int width)
1660 if (map && map->dso)
1661 return repsep_snprintf(bf, bf_size, "%*d", width,
1662 map__size(map));
1664 return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1667 static int hist_entry__dso_size_snprintf(struct hist_entry *he, char *bf,
1668 size_t size, unsigned int width)
1670 return _hist_entry__dso_size_snprintf(he->ms.map, bf, size, width);
1673 struct sort_entry sort_dso_size = {
1674 .se_header = "DSO size",
1675 .se_cmp = sort__dso_size_cmp,
1676 .se_snprintf = hist_entry__dso_size_snprintf,
1677 .se_width_idx = HISTC_DSO_SIZE,
1681 struct sort_dimension {
1682 const char *name;
1683 struct sort_entry *entry;
1684 int taken;
1687 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1689 static struct sort_dimension common_sort_dimensions[] = {
1690 DIM(SORT_PID, "pid", sort_thread),
1691 DIM(SORT_COMM, "comm", sort_comm),
1692 DIM(SORT_DSO, "dso", sort_dso),
1693 DIM(SORT_SYM, "symbol", sort_sym),
1694 DIM(SORT_PARENT, "parent", sort_parent),
1695 DIM(SORT_CPU, "cpu", sort_cpu),
1696 DIM(SORT_SOCKET, "socket", sort_socket),
1697 DIM(SORT_SRCLINE, "srcline", sort_srcline),
1698 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1699 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1700 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1701 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1702 DIM(SORT_TRACE, "trace", sort_trace),
1703 DIM(SORT_SYM_SIZE, "symbol_size", sort_sym_size),
1704 DIM(SORT_DSO_SIZE, "dso_size", sort_dso_size),
1705 DIM(SORT_CGROUP, "cgroup", sort_cgroup),
1706 DIM(SORT_CGROUP_ID, "cgroup_id", sort_cgroup_id),
1707 DIM(SORT_SYM_IPC_NULL, "ipc_null", sort_sym_ipc_null),
1708 DIM(SORT_TIME, "time", sort_time),
1711 #undef DIM
1713 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1715 static struct sort_dimension bstack_sort_dimensions[] = {
1716 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1717 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1718 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1719 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1720 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1721 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1722 DIM(SORT_ABORT, "abort", sort_abort),
1723 DIM(SORT_CYCLES, "cycles", sort_cycles),
1724 DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
1725 DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
1726 DIM(SORT_SYM_IPC, "ipc_lbr", sort_sym_ipc),
1729 #undef DIM
1731 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1733 static struct sort_dimension memory_sort_dimensions[] = {
1734 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1735 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1736 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1737 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1738 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1739 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1740 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1741 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1742 DIM(SORT_MEM_PHYS_DADDR, "phys_daddr", sort_mem_phys_daddr),
1745 #undef DIM
1747 struct hpp_dimension {
1748 const char *name;
1749 struct perf_hpp_fmt *fmt;
1750 int taken;
1753 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1755 static struct hpp_dimension hpp_sort_dimensions[] = {
1756 DIM(PERF_HPP__OVERHEAD, "overhead"),
1757 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1758 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1759 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1760 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1761 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1762 DIM(PERF_HPP__SAMPLES, "sample"),
1763 DIM(PERF_HPP__PERIOD, "period"),
1766 #undef DIM
1768 struct hpp_sort_entry {
1769 struct perf_hpp_fmt hpp;
1770 struct sort_entry *se;
1773 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1775 struct hpp_sort_entry *hse;
1777 if (!perf_hpp__is_sort_entry(fmt))
1778 return;
1780 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1781 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1784 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1785 struct hists *hists, int line __maybe_unused,
1786 int *span __maybe_unused)
1788 struct hpp_sort_entry *hse;
1789 size_t len = fmt->user_len;
1791 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1793 if (!len)
1794 len = hists__col_len(hists, hse->se->se_width_idx);
1796 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1799 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1800 struct perf_hpp *hpp __maybe_unused,
1801 struct hists *hists)
1803 struct hpp_sort_entry *hse;
1804 size_t len = fmt->user_len;
1806 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1808 if (!len)
1809 len = hists__col_len(hists, hse->se->se_width_idx);
1811 return len;
1814 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1815 struct hist_entry *he)
1817 struct hpp_sort_entry *hse;
1818 size_t len = fmt->user_len;
1820 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1822 if (!len)
1823 len = hists__col_len(he->hists, hse->se->se_width_idx);
1825 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1828 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1829 struct hist_entry *a, struct hist_entry *b)
1831 struct hpp_sort_entry *hse;
1833 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1834 return hse->se->se_cmp(a, b);
1837 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1838 struct hist_entry *a, struct hist_entry *b)
1840 struct hpp_sort_entry *hse;
1841 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1843 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1844 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1845 return collapse_fn(a, b);
1848 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1849 struct hist_entry *a, struct hist_entry *b)
1851 struct hpp_sort_entry *hse;
1852 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1854 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1855 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1856 return sort_fn(a, b);
1859 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1861 return format->header == __sort__hpp_header;
1864 #define MK_SORT_ENTRY_CHK(key) \
1865 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1867 struct hpp_sort_entry *hse; \
1869 if (!perf_hpp__is_sort_entry(fmt)) \
1870 return false; \
1872 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1873 return hse->se == &sort_ ## key ; \
1876 MK_SORT_ENTRY_CHK(trace)
1877 MK_SORT_ENTRY_CHK(srcline)
1878 MK_SORT_ENTRY_CHK(srcfile)
1879 MK_SORT_ENTRY_CHK(thread)
1880 MK_SORT_ENTRY_CHK(comm)
1881 MK_SORT_ENTRY_CHK(dso)
1882 MK_SORT_ENTRY_CHK(sym)
1885 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1887 struct hpp_sort_entry *hse_a;
1888 struct hpp_sort_entry *hse_b;
1890 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1891 return false;
1893 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1894 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1896 return hse_a->se == hse_b->se;
1899 static void hse_free(struct perf_hpp_fmt *fmt)
1901 struct hpp_sort_entry *hse;
1903 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1904 free(hse);
1907 static struct hpp_sort_entry *
1908 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
1910 struct hpp_sort_entry *hse;
1912 hse = malloc(sizeof(*hse));
1913 if (hse == NULL) {
1914 pr_err("Memory allocation failed\n");
1915 return NULL;
1918 hse->se = sd->entry;
1919 hse->hpp.name = sd->entry->se_header;
1920 hse->hpp.header = __sort__hpp_header;
1921 hse->hpp.width = __sort__hpp_width;
1922 hse->hpp.entry = __sort__hpp_entry;
1923 hse->hpp.color = NULL;
1925 hse->hpp.cmp = __sort__hpp_cmp;
1926 hse->hpp.collapse = __sort__hpp_collapse;
1927 hse->hpp.sort = __sort__hpp_sort;
1928 hse->hpp.equal = __sort__hpp_equal;
1929 hse->hpp.free = hse_free;
1931 INIT_LIST_HEAD(&hse->hpp.list);
1932 INIT_LIST_HEAD(&hse->hpp.sort_list);
1933 hse->hpp.elide = false;
1934 hse->hpp.len = 0;
1935 hse->hpp.user_len = 0;
1936 hse->hpp.level = level;
1938 return hse;
1941 static void hpp_free(struct perf_hpp_fmt *fmt)
1943 free(fmt);
1946 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1947 int level)
1949 struct perf_hpp_fmt *fmt;
1951 fmt = memdup(hd->fmt, sizeof(*fmt));
1952 if (fmt) {
1953 INIT_LIST_HEAD(&fmt->list);
1954 INIT_LIST_HEAD(&fmt->sort_list);
1955 fmt->free = hpp_free;
1956 fmt->level = level;
1959 return fmt;
1962 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1964 struct perf_hpp_fmt *fmt;
1965 struct hpp_sort_entry *hse;
1966 int ret = -1;
1967 int r;
1969 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1970 if (!perf_hpp__is_sort_entry(fmt))
1971 continue;
1973 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1974 if (hse->se->se_filter == NULL)
1975 continue;
1978 * hist entry is filtered if any of sort key in the hpp list
1979 * is applied. But it should skip non-matched filter types.
1981 r = hse->se->se_filter(he, type, arg);
1982 if (r >= 0) {
1983 if (ret < 0)
1984 ret = 0;
1985 ret |= r;
1989 return ret;
1992 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1993 struct perf_hpp_list *list,
1994 int level)
1996 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
1998 if (hse == NULL)
1999 return -1;
2001 perf_hpp_list__register_sort_field(list, &hse->hpp);
2002 return 0;
2005 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
2006 struct perf_hpp_list *list)
2008 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
2010 if (hse == NULL)
2011 return -1;
2013 perf_hpp_list__column_register(list, &hse->hpp);
2014 return 0;
2017 struct hpp_dynamic_entry {
2018 struct perf_hpp_fmt hpp;
2019 struct evsel *evsel;
2020 struct tep_format_field *field;
2021 unsigned dynamic_len;
2022 bool raw_trace;
2025 static int hde_width(struct hpp_dynamic_entry *hde)
2027 if (!hde->hpp.len) {
2028 int len = hde->dynamic_len;
2029 int namelen = strlen(hde->field->name);
2030 int fieldlen = hde->field->size;
2032 if (namelen > len)
2033 len = namelen;
2035 if (!(hde->field->flags & TEP_FIELD_IS_STRING)) {
2036 /* length for print hex numbers */
2037 fieldlen = hde->field->size * 2 + 2;
2039 if (fieldlen > len)
2040 len = fieldlen;
2042 hde->hpp.len = len;
2044 return hde->hpp.len;
2047 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
2048 struct hist_entry *he)
2050 char *str, *pos;
2051 struct tep_format_field *field = hde->field;
2052 size_t namelen;
2053 bool last = false;
2055 if (hde->raw_trace)
2056 return;
2058 /* parse pretty print result and update max length */
2059 if (!he->trace_output)
2060 he->trace_output = get_trace_output(he);
2062 namelen = strlen(field->name);
2063 str = he->trace_output;
2065 while (str) {
2066 pos = strchr(str, ' ');
2067 if (pos == NULL) {
2068 last = true;
2069 pos = str + strlen(str);
2072 if (!strncmp(str, field->name, namelen)) {
2073 size_t len;
2075 str += namelen + 1;
2076 len = pos - str;
2078 if (len > hde->dynamic_len)
2079 hde->dynamic_len = len;
2080 break;
2083 if (last)
2084 str = NULL;
2085 else
2086 str = pos + 1;
2090 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2091 struct hists *hists __maybe_unused,
2092 int line __maybe_unused,
2093 int *span __maybe_unused)
2095 struct hpp_dynamic_entry *hde;
2096 size_t len = fmt->user_len;
2098 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2100 if (!len)
2101 len = hde_width(hde);
2103 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
2106 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
2107 struct perf_hpp *hpp __maybe_unused,
2108 struct hists *hists __maybe_unused)
2110 struct hpp_dynamic_entry *hde;
2111 size_t len = fmt->user_len;
2113 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2115 if (!len)
2116 len = hde_width(hde);
2118 return len;
2121 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
2123 struct hpp_dynamic_entry *hde;
2125 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2127 return hists_to_evsel(hists) == hde->evsel;
2130 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2131 struct hist_entry *he)
2133 struct hpp_dynamic_entry *hde;
2134 size_t len = fmt->user_len;
2135 char *str, *pos;
2136 struct tep_format_field *field;
2137 size_t namelen;
2138 bool last = false;
2139 int ret;
2141 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2143 if (!len)
2144 len = hde_width(hde);
2146 if (hde->raw_trace)
2147 goto raw_field;
2149 if (!he->trace_output)
2150 he->trace_output = get_trace_output(he);
2152 field = hde->field;
2153 namelen = strlen(field->name);
2154 str = he->trace_output;
2156 while (str) {
2157 pos = strchr(str, ' ');
2158 if (pos == NULL) {
2159 last = true;
2160 pos = str + strlen(str);
2163 if (!strncmp(str, field->name, namelen)) {
2164 str += namelen + 1;
2165 str = strndup(str, pos - str);
2167 if (str == NULL)
2168 return scnprintf(hpp->buf, hpp->size,
2169 "%*.*s", len, len, "ERROR");
2170 break;
2173 if (last)
2174 str = NULL;
2175 else
2176 str = pos + 1;
2179 if (str == NULL) {
2180 struct trace_seq seq;
2181 raw_field:
2182 trace_seq_init(&seq);
2183 tep_print_field(&seq, he->raw_data, hde->field);
2184 str = seq.buffer;
2187 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
2188 free(str);
2189 return ret;
2192 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
2193 struct hist_entry *a, struct hist_entry *b)
2195 struct hpp_dynamic_entry *hde;
2196 struct tep_format_field *field;
2197 unsigned offset, size;
2199 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2201 if (b == NULL) {
2202 update_dynamic_len(hde, a);
2203 return 0;
2206 field = hde->field;
2207 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2208 unsigned long long dyn;
2210 tep_read_number_field(field, a->raw_data, &dyn);
2211 offset = dyn & 0xffff;
2212 size = (dyn >> 16) & 0xffff;
2214 /* record max width for output */
2215 if (size > hde->dynamic_len)
2216 hde->dynamic_len = size;
2217 } else {
2218 offset = field->offset;
2219 size = field->size;
2222 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
2225 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
2227 return fmt->cmp == __sort__hde_cmp;
2230 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2232 struct hpp_dynamic_entry *hde_a;
2233 struct hpp_dynamic_entry *hde_b;
2235 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
2236 return false;
2238 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
2239 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
2241 return hde_a->field == hde_b->field;
2244 static void hde_free(struct perf_hpp_fmt *fmt)
2246 struct hpp_dynamic_entry *hde;
2248 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2249 free(hde);
2252 static struct hpp_dynamic_entry *
2253 __alloc_dynamic_entry(struct evsel *evsel, struct tep_format_field *field,
2254 int level)
2256 struct hpp_dynamic_entry *hde;
2258 hde = malloc(sizeof(*hde));
2259 if (hde == NULL) {
2260 pr_debug("Memory allocation failed\n");
2261 return NULL;
2264 hde->evsel = evsel;
2265 hde->field = field;
2266 hde->dynamic_len = 0;
2268 hde->hpp.name = field->name;
2269 hde->hpp.header = __sort__hde_header;
2270 hde->hpp.width = __sort__hde_width;
2271 hde->hpp.entry = __sort__hde_entry;
2272 hde->hpp.color = NULL;
2274 hde->hpp.cmp = __sort__hde_cmp;
2275 hde->hpp.collapse = __sort__hde_cmp;
2276 hde->hpp.sort = __sort__hde_cmp;
2277 hde->hpp.equal = __sort__hde_equal;
2278 hde->hpp.free = hde_free;
2280 INIT_LIST_HEAD(&hde->hpp.list);
2281 INIT_LIST_HEAD(&hde->hpp.sort_list);
2282 hde->hpp.elide = false;
2283 hde->hpp.len = 0;
2284 hde->hpp.user_len = 0;
2285 hde->hpp.level = level;
2287 return hde;
2290 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
2292 struct perf_hpp_fmt *new_fmt = NULL;
2294 if (perf_hpp__is_sort_entry(fmt)) {
2295 struct hpp_sort_entry *hse, *new_hse;
2297 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2298 new_hse = memdup(hse, sizeof(*hse));
2299 if (new_hse)
2300 new_fmt = &new_hse->hpp;
2301 } else if (perf_hpp__is_dynamic_entry(fmt)) {
2302 struct hpp_dynamic_entry *hde, *new_hde;
2304 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2305 new_hde = memdup(hde, sizeof(*hde));
2306 if (new_hde)
2307 new_fmt = &new_hde->hpp;
2308 } else {
2309 new_fmt = memdup(fmt, sizeof(*fmt));
2312 INIT_LIST_HEAD(&new_fmt->list);
2313 INIT_LIST_HEAD(&new_fmt->sort_list);
2315 return new_fmt;
2318 static int parse_field_name(char *str, char **event, char **field, char **opt)
2320 char *event_name, *field_name, *opt_name;
2322 event_name = str;
2323 field_name = strchr(str, '.');
2325 if (field_name) {
2326 *field_name++ = '\0';
2327 } else {
2328 event_name = NULL;
2329 field_name = str;
2332 opt_name = strchr(field_name, '/');
2333 if (opt_name)
2334 *opt_name++ = '\0';
2336 *event = event_name;
2337 *field = field_name;
2338 *opt = opt_name;
2340 return 0;
2343 /* find match evsel using a given event name. The event name can be:
2344 * 1. '%' + event index (e.g. '%1' for first event)
2345 * 2. full event name (e.g. sched:sched_switch)
2346 * 3. partial event name (should not contain ':')
2348 static struct evsel *find_evsel(struct evlist *evlist, char *event_name)
2350 struct evsel *evsel = NULL;
2351 struct evsel *pos;
2352 bool full_name;
2354 /* case 1 */
2355 if (event_name[0] == '%') {
2356 int nr = strtol(event_name+1, NULL, 0);
2358 if (nr > evlist->core.nr_entries)
2359 return NULL;
2361 evsel = evlist__first(evlist);
2362 while (--nr > 0)
2363 evsel = evsel__next(evsel);
2365 return evsel;
2368 full_name = !!strchr(event_name, ':');
2369 evlist__for_each_entry(evlist, pos) {
2370 /* case 2 */
2371 if (full_name && !strcmp(pos->name, event_name))
2372 return pos;
2373 /* case 3 */
2374 if (!full_name && strstr(pos->name, event_name)) {
2375 if (evsel) {
2376 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2377 event_name, evsel->name, pos->name);
2378 return NULL;
2380 evsel = pos;
2384 return evsel;
2387 static int __dynamic_dimension__add(struct evsel *evsel,
2388 struct tep_format_field *field,
2389 bool raw_trace, int level)
2391 struct hpp_dynamic_entry *hde;
2393 hde = __alloc_dynamic_entry(evsel, field, level);
2394 if (hde == NULL)
2395 return -ENOMEM;
2397 hde->raw_trace = raw_trace;
2399 perf_hpp__register_sort_field(&hde->hpp);
2400 return 0;
2403 static int add_evsel_fields(struct evsel *evsel, bool raw_trace, int level)
2405 int ret;
2406 struct tep_format_field *field;
2408 field = evsel->tp_format->format.fields;
2409 while (field) {
2410 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2411 if (ret < 0)
2412 return ret;
2414 field = field->next;
2416 return 0;
2419 static int add_all_dynamic_fields(struct evlist *evlist, bool raw_trace,
2420 int level)
2422 int ret;
2423 struct evsel *evsel;
2425 evlist__for_each_entry(evlist, evsel) {
2426 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2427 continue;
2429 ret = add_evsel_fields(evsel, raw_trace, level);
2430 if (ret < 0)
2431 return ret;
2433 return 0;
2436 static int add_all_matching_fields(struct evlist *evlist,
2437 char *field_name, bool raw_trace, int level)
2439 int ret = -ESRCH;
2440 struct evsel *evsel;
2441 struct tep_format_field *field;
2443 evlist__for_each_entry(evlist, evsel) {
2444 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2445 continue;
2447 field = tep_find_any_field(evsel->tp_format, field_name);
2448 if (field == NULL)
2449 continue;
2451 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2452 if (ret < 0)
2453 break;
2455 return ret;
2458 static int add_dynamic_entry(struct evlist *evlist, const char *tok,
2459 int level)
2461 char *str, *event_name, *field_name, *opt_name;
2462 struct evsel *evsel;
2463 struct tep_format_field *field;
2464 bool raw_trace = symbol_conf.raw_trace;
2465 int ret = 0;
2467 if (evlist == NULL)
2468 return -ENOENT;
2470 str = strdup(tok);
2471 if (str == NULL)
2472 return -ENOMEM;
2474 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2475 ret = -EINVAL;
2476 goto out;
2479 if (opt_name) {
2480 if (strcmp(opt_name, "raw")) {
2481 pr_debug("unsupported field option %s\n", opt_name);
2482 ret = -EINVAL;
2483 goto out;
2485 raw_trace = true;
2488 if (!strcmp(field_name, "trace_fields")) {
2489 ret = add_all_dynamic_fields(evlist, raw_trace, level);
2490 goto out;
2493 if (event_name == NULL) {
2494 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2495 goto out;
2498 evsel = find_evsel(evlist, event_name);
2499 if (evsel == NULL) {
2500 pr_debug("Cannot find event: %s\n", event_name);
2501 ret = -ENOENT;
2502 goto out;
2505 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2506 pr_debug("%s is not a tracepoint event\n", event_name);
2507 ret = -EINVAL;
2508 goto out;
2511 if (!strcmp(field_name, "*")) {
2512 ret = add_evsel_fields(evsel, raw_trace, level);
2513 } else {
2514 field = tep_find_any_field(evsel->tp_format, field_name);
2515 if (field == NULL) {
2516 pr_debug("Cannot find event field for %s.%s\n",
2517 event_name, field_name);
2518 return -ENOENT;
2521 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2524 out:
2525 free(str);
2526 return ret;
2529 static int __sort_dimension__add(struct sort_dimension *sd,
2530 struct perf_hpp_list *list,
2531 int level)
2533 if (sd->taken)
2534 return 0;
2536 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2537 return -1;
2539 if (sd->entry->se_collapse)
2540 list->need_collapse = 1;
2542 sd->taken = 1;
2544 return 0;
2547 static int __hpp_dimension__add(struct hpp_dimension *hd,
2548 struct perf_hpp_list *list,
2549 int level)
2551 struct perf_hpp_fmt *fmt;
2553 if (hd->taken)
2554 return 0;
2556 fmt = __hpp_dimension__alloc_hpp(hd, level);
2557 if (!fmt)
2558 return -1;
2560 hd->taken = 1;
2561 perf_hpp_list__register_sort_field(list, fmt);
2562 return 0;
2565 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2566 struct sort_dimension *sd)
2568 if (sd->taken)
2569 return 0;
2571 if (__sort_dimension__add_hpp_output(sd, list) < 0)
2572 return -1;
2574 sd->taken = 1;
2575 return 0;
2578 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2579 struct hpp_dimension *hd)
2581 struct perf_hpp_fmt *fmt;
2583 if (hd->taken)
2584 return 0;
2586 fmt = __hpp_dimension__alloc_hpp(hd, 0);
2587 if (!fmt)
2588 return -1;
2590 hd->taken = 1;
2591 perf_hpp_list__column_register(list, fmt);
2592 return 0;
2595 int hpp_dimension__add_output(unsigned col)
2597 BUG_ON(col >= PERF_HPP__MAX_INDEX);
2598 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2601 int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2602 struct evlist *evlist,
2603 int level)
2605 unsigned int i;
2607 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2608 struct sort_dimension *sd = &common_sort_dimensions[i];
2610 if (strncasecmp(tok, sd->name, strlen(tok)))
2611 continue;
2613 if (sd->entry == &sort_parent) {
2614 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2615 if (ret) {
2616 char err[BUFSIZ];
2618 regerror(ret, &parent_regex, err, sizeof(err));
2619 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2620 return -EINVAL;
2622 list->parent = 1;
2623 } else if (sd->entry == &sort_sym) {
2624 list->sym = 1;
2626 * perf diff displays the performance difference amongst
2627 * two or more perf.data files. Those files could come
2628 * from different binaries. So we should not compare
2629 * their ips, but the name of symbol.
2631 if (sort__mode == SORT_MODE__DIFF)
2632 sd->entry->se_collapse = sort__sym_sort;
2634 } else if (sd->entry == &sort_dso) {
2635 list->dso = 1;
2636 } else if (sd->entry == &sort_socket) {
2637 list->socket = 1;
2638 } else if (sd->entry == &sort_thread) {
2639 list->thread = 1;
2640 } else if (sd->entry == &sort_comm) {
2641 list->comm = 1;
2644 return __sort_dimension__add(sd, list, level);
2647 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2648 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2650 if (strncasecmp(tok, hd->name, strlen(tok)))
2651 continue;
2653 return __hpp_dimension__add(hd, list, level);
2656 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2657 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2659 if (strncasecmp(tok, sd->name, strlen(tok)))
2660 continue;
2662 if (sort__mode != SORT_MODE__BRANCH)
2663 return -EINVAL;
2665 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2666 list->sym = 1;
2668 __sort_dimension__add(sd, list, level);
2669 return 0;
2672 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2673 struct sort_dimension *sd = &memory_sort_dimensions[i];
2675 if (strncasecmp(tok, sd->name, strlen(tok)))
2676 continue;
2678 if (sort__mode != SORT_MODE__MEMORY)
2679 return -EINVAL;
2681 if (sd->entry == &sort_mem_dcacheline && cacheline_size() == 0)
2682 return -EINVAL;
2684 if (sd->entry == &sort_mem_daddr_sym)
2685 list->sym = 1;
2687 __sort_dimension__add(sd, list, level);
2688 return 0;
2691 if (!add_dynamic_entry(evlist, tok, level))
2692 return 0;
2694 return -ESRCH;
2697 static int setup_sort_list(struct perf_hpp_list *list, char *str,
2698 struct evlist *evlist)
2700 char *tmp, *tok;
2701 int ret = 0;
2702 int level = 0;
2703 int next_level = 1;
2704 bool in_group = false;
2706 do {
2707 tok = str;
2708 tmp = strpbrk(str, "{}, ");
2709 if (tmp) {
2710 if (in_group)
2711 next_level = level;
2712 else
2713 next_level = level + 1;
2715 if (*tmp == '{')
2716 in_group = true;
2717 else if (*tmp == '}')
2718 in_group = false;
2720 *tmp = '\0';
2721 str = tmp + 1;
2724 if (*tok) {
2725 ret = sort_dimension__add(list, tok, evlist, level);
2726 if (ret == -EINVAL) {
2727 if (!cacheline_size() && !strncasecmp(tok, "dcacheline", strlen(tok)))
2728 ui__error("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2729 else
2730 ui__error("Invalid --sort key: `%s'", tok);
2731 break;
2732 } else if (ret == -ESRCH) {
2733 ui__error("Unknown --sort key: `%s'", tok);
2734 break;
2738 level = next_level;
2739 } while (tmp);
2741 return ret;
2744 static const char *get_default_sort_order(struct evlist *evlist)
2746 const char *default_sort_orders[] = {
2747 default_sort_order,
2748 default_branch_sort_order,
2749 default_mem_sort_order,
2750 default_top_sort_order,
2751 default_diff_sort_order,
2752 default_tracepoint_sort_order,
2754 bool use_trace = true;
2755 struct evsel *evsel;
2757 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2759 if (evlist == NULL || perf_evlist__empty(evlist))
2760 goto out_no_evlist;
2762 evlist__for_each_entry(evlist, evsel) {
2763 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2764 use_trace = false;
2765 break;
2769 if (use_trace) {
2770 sort__mode = SORT_MODE__TRACEPOINT;
2771 if (symbol_conf.raw_trace)
2772 return "trace_fields";
2774 out_no_evlist:
2775 return default_sort_orders[sort__mode];
2778 static int setup_sort_order(struct evlist *evlist)
2780 char *new_sort_order;
2783 * Append '+'-prefixed sort order to the default sort
2784 * order string.
2786 if (!sort_order || is_strict_order(sort_order))
2787 return 0;
2789 if (sort_order[1] == '\0') {
2790 ui__error("Invalid --sort key: `+'");
2791 return -EINVAL;
2795 * We allocate new sort_order string, but we never free it,
2796 * because it's checked over the rest of the code.
2798 if (asprintf(&new_sort_order, "%s,%s",
2799 get_default_sort_order(evlist), sort_order + 1) < 0) {
2800 pr_err("Not enough memory to set up --sort");
2801 return -ENOMEM;
2804 sort_order = new_sort_order;
2805 return 0;
2809 * Adds 'pre,' prefix into 'str' is 'pre' is
2810 * not already part of 'str'.
2812 static char *prefix_if_not_in(const char *pre, char *str)
2814 char *n;
2816 if (!str || strstr(str, pre))
2817 return str;
2819 if (asprintf(&n, "%s,%s", pre, str) < 0)
2820 n = NULL;
2822 free(str);
2823 return n;
2826 static char *setup_overhead(char *keys)
2828 if (sort__mode == SORT_MODE__DIFF)
2829 return keys;
2831 keys = prefix_if_not_in("overhead", keys);
2833 if (symbol_conf.cumulate_callchain)
2834 keys = prefix_if_not_in("overhead_children", keys);
2836 return keys;
2839 static int __setup_sorting(struct evlist *evlist)
2841 char *str;
2842 const char *sort_keys;
2843 int ret = 0;
2845 ret = setup_sort_order(evlist);
2846 if (ret)
2847 return ret;
2849 sort_keys = sort_order;
2850 if (sort_keys == NULL) {
2851 if (is_strict_order(field_order)) {
2853 * If user specified field order but no sort order,
2854 * we'll honor it and not add default sort orders.
2856 return 0;
2859 sort_keys = get_default_sort_order(evlist);
2862 str = strdup(sort_keys);
2863 if (str == NULL) {
2864 pr_err("Not enough memory to setup sort keys");
2865 return -ENOMEM;
2869 * Prepend overhead fields for backward compatibility.
2871 if (!is_strict_order(field_order)) {
2872 str = setup_overhead(str);
2873 if (str == NULL) {
2874 pr_err("Not enough memory to setup overhead keys");
2875 return -ENOMEM;
2879 ret = setup_sort_list(&perf_hpp_list, str, evlist);
2881 free(str);
2882 return ret;
2885 void perf_hpp__set_elide(int idx, bool elide)
2887 struct perf_hpp_fmt *fmt;
2888 struct hpp_sort_entry *hse;
2890 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2891 if (!perf_hpp__is_sort_entry(fmt))
2892 continue;
2894 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2895 if (hse->se->se_width_idx == idx) {
2896 fmt->elide = elide;
2897 break;
2902 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
2904 if (list && strlist__nr_entries(list) == 1) {
2905 if (fp != NULL)
2906 fprintf(fp, "# %s: %s\n", list_name,
2907 strlist__entry(list, 0)->s);
2908 return true;
2910 return false;
2913 static bool get_elide(int idx, FILE *output)
2915 switch (idx) {
2916 case HISTC_SYMBOL:
2917 return __get_elide(symbol_conf.sym_list, "symbol", output);
2918 case HISTC_DSO:
2919 return __get_elide(symbol_conf.dso_list, "dso", output);
2920 case HISTC_COMM:
2921 return __get_elide(symbol_conf.comm_list, "comm", output);
2922 default:
2923 break;
2926 if (sort__mode != SORT_MODE__BRANCH)
2927 return false;
2929 switch (idx) {
2930 case HISTC_SYMBOL_FROM:
2931 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2932 case HISTC_SYMBOL_TO:
2933 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2934 case HISTC_DSO_FROM:
2935 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2936 case HISTC_DSO_TO:
2937 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2938 default:
2939 break;
2942 return false;
2945 void sort__setup_elide(FILE *output)
2947 struct perf_hpp_fmt *fmt;
2948 struct hpp_sort_entry *hse;
2950 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2951 if (!perf_hpp__is_sort_entry(fmt))
2952 continue;
2954 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2955 fmt->elide = get_elide(hse->se->se_width_idx, output);
2959 * It makes no sense to elide all of sort entries.
2960 * Just revert them to show up again.
2962 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2963 if (!perf_hpp__is_sort_entry(fmt))
2964 continue;
2966 if (!fmt->elide)
2967 return;
2970 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2971 if (!perf_hpp__is_sort_entry(fmt))
2972 continue;
2974 fmt->elide = false;
2978 int output_field_add(struct perf_hpp_list *list, char *tok)
2980 unsigned int i;
2982 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2983 struct sort_dimension *sd = &common_sort_dimensions[i];
2985 if (strncasecmp(tok, sd->name, strlen(tok)))
2986 continue;
2988 return __sort_dimension__add_output(list, sd);
2991 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2992 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2994 if (strncasecmp(tok, hd->name, strlen(tok)))
2995 continue;
2997 return __hpp_dimension__add_output(list, hd);
3000 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
3001 struct sort_dimension *sd = &bstack_sort_dimensions[i];
3003 if (strncasecmp(tok, sd->name, strlen(tok)))
3004 continue;
3006 if (sort__mode != SORT_MODE__MEMORY)
3007 return -EINVAL;
3009 return __sort_dimension__add_output(list, sd);
3012 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
3013 struct sort_dimension *sd = &memory_sort_dimensions[i];
3015 if (strncasecmp(tok, sd->name, strlen(tok)))
3016 continue;
3018 if (sort__mode != SORT_MODE__BRANCH)
3019 return -EINVAL;
3021 return __sort_dimension__add_output(list, sd);
3024 return -ESRCH;
3027 static int setup_output_list(struct perf_hpp_list *list, char *str)
3029 char *tmp, *tok;
3030 int ret = 0;
3032 for (tok = strtok_r(str, ", ", &tmp);
3033 tok; tok = strtok_r(NULL, ", ", &tmp)) {
3034 ret = output_field_add(list, tok);
3035 if (ret == -EINVAL) {
3036 ui__error("Invalid --fields key: `%s'", tok);
3037 break;
3038 } else if (ret == -ESRCH) {
3039 ui__error("Unknown --fields key: `%s'", tok);
3040 break;
3044 return ret;
3047 void reset_dimensions(void)
3049 unsigned int i;
3051 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
3052 common_sort_dimensions[i].taken = 0;
3054 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
3055 hpp_sort_dimensions[i].taken = 0;
3057 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
3058 bstack_sort_dimensions[i].taken = 0;
3060 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
3061 memory_sort_dimensions[i].taken = 0;
3064 bool is_strict_order(const char *order)
3066 return order && (*order != '+');
3069 static int __setup_output_field(void)
3071 char *str, *strp;
3072 int ret = -EINVAL;
3074 if (field_order == NULL)
3075 return 0;
3077 strp = str = strdup(field_order);
3078 if (str == NULL) {
3079 pr_err("Not enough memory to setup output fields");
3080 return -ENOMEM;
3083 if (!is_strict_order(field_order))
3084 strp++;
3086 if (!strlen(strp)) {
3087 ui__error("Invalid --fields key: `+'");
3088 goto out;
3091 ret = setup_output_list(&perf_hpp_list, strp);
3093 out:
3094 free(str);
3095 return ret;
3098 int setup_sorting(struct evlist *evlist)
3100 int err;
3102 err = __setup_sorting(evlist);
3103 if (err < 0)
3104 return err;
3106 if (parent_pattern != default_parent_pattern) {
3107 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
3108 if (err < 0)
3109 return err;
3112 reset_dimensions();
3115 * perf diff doesn't use default hpp output fields.
3117 if (sort__mode != SORT_MODE__DIFF)
3118 perf_hpp__init();
3120 err = __setup_output_field();
3121 if (err < 0)
3122 return err;
3124 /* copy sort keys to output fields */
3125 perf_hpp__setup_output_field(&perf_hpp_list);
3126 /* and then copy output fields to sort keys */
3127 perf_hpp__append_sort_keys(&perf_hpp_list);
3129 /* setup hists-specific output fields */
3130 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
3131 return -1;
3133 return 0;
3136 void reset_output_field(void)
3138 perf_hpp_list.need_collapse = 0;
3139 perf_hpp_list.parent = 0;
3140 perf_hpp_list.sym = 0;
3141 perf_hpp_list.dso = 0;
3143 field_order = NULL;
3144 sort_order = NULL;
3146 reset_dimensions();
3147 perf_hpp__reset_output_field(&perf_hpp_list);
3150 #define INDENT (3*8 + 1)
3152 static void add_key(struct strbuf *sb, const char *str, int *llen)
3154 if (*llen >= 75) {
3155 strbuf_addstr(sb, "\n\t\t\t ");
3156 *llen = INDENT;
3158 strbuf_addf(sb, " %s", str);
3159 *llen += strlen(str) + 1;
3162 static void add_sort_string(struct strbuf *sb, struct sort_dimension *s, int n,
3163 int *llen)
3165 int i;
3167 for (i = 0; i < n; i++)
3168 add_key(sb, s[i].name, llen);
3171 static void add_hpp_sort_string(struct strbuf *sb, struct hpp_dimension *s, int n,
3172 int *llen)
3174 int i;
3176 for (i = 0; i < n; i++)
3177 add_key(sb, s[i].name, llen);
3180 const char *sort_help(const char *prefix)
3182 struct strbuf sb;
3183 char *s;
3184 int len = strlen(prefix) + INDENT;
3186 strbuf_init(&sb, 300);
3187 strbuf_addstr(&sb, prefix);
3188 add_hpp_sort_string(&sb, hpp_sort_dimensions,
3189 ARRAY_SIZE(hpp_sort_dimensions), &len);
3190 add_sort_string(&sb, common_sort_dimensions,
3191 ARRAY_SIZE(common_sort_dimensions), &len);
3192 add_sort_string(&sb, bstack_sort_dimensions,
3193 ARRAY_SIZE(bstack_sort_dimensions), &len);
3194 add_sort_string(&sb, memory_sort_dimensions,
3195 ARRAY_SIZE(memory_sort_dimensions), &len);
3196 s = strbuf_detach(&sb, NULL);
3197 strbuf_release(&sb);
3198 return s;