]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/perf/util/sort.c
perf tools: Including missing inttypes.h header
[karo-tx-linux.git] / tools / perf / util / sort.c
1 #include <inttypes.h>
2 #include <sys/mman.h>
3 #include "sort.h"
4 #include "hist.h"
5 #include "comm.h"
6 #include "symbol.h"
7 #include "evsel.h"
8 #include "evlist.h"
9 #include <traceevent/event-parse.h>
10 #include "mem-events.h"
11 #include <linux/kernel.h>
12
13 regex_t         parent_regex;
14 const char      default_parent_pattern[] = "^sys_|^do_page_fault";
15 const char      *parent_pattern = default_parent_pattern;
16 const char      *default_sort_order = "comm,dso,symbol";
17 const char      default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
18 const char      default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
19 const char      default_top_sort_order[] = "dso,symbol";
20 const char      default_diff_sort_order[] = "dso,symbol";
21 const char      default_tracepoint_sort_order[] = "trace";
22 const char      *sort_order;
23 const char      *field_order;
24 regex_t         ignore_callees_regex;
25 int             have_ignore_callees = 0;
26 enum sort_mode  sort__mode = SORT_MODE__NORMAL;
27
28 /*
29  * Replaces all occurrences of a char used with the:
30  *
31  * -t, --field-separator
32  *
33  * option, that uses a special separator character and don't pad with spaces,
34  * replacing all occurances of this separator in symbol names (and other
35  * output) with a '.' character, that thus it's the only non valid separator.
36 */
37 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
38 {
39         int n;
40         va_list ap;
41
42         va_start(ap, fmt);
43         n = vsnprintf(bf, size, fmt, ap);
44         if (symbol_conf.field_sep && n > 0) {
45                 char *sep = bf;
46
47                 while (1) {
48                         sep = strchr(sep, *symbol_conf.field_sep);
49                         if (sep == NULL)
50                                 break;
51                         *sep = '.';
52                 }
53         }
54         va_end(ap);
55
56         if (n >= (int)size)
57                 return size - 1;
58         return n;
59 }
60
61 static int64_t cmp_null(const void *l, const void *r)
62 {
63         if (!l && !r)
64                 return 0;
65         else if (!l)
66                 return -1;
67         else
68                 return 1;
69 }
70
71 /* --sort pid */
72
73 static int64_t
74 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
75 {
76         return right->thread->tid - left->thread->tid;
77 }
78
79 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
80                                        size_t size, unsigned int width)
81 {
82         const char *comm = thread__comm_str(he->thread);
83
84         width = max(7U, width) - 8;
85         return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid,
86                                width, width, comm ?: "");
87 }
88
89 static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
90 {
91         const struct thread *th = arg;
92
93         if (type != HIST_FILTER__THREAD)
94                 return -1;
95
96         return th && he->thread != th;
97 }
98
99 struct sort_entry sort_thread = {
100         .se_header      = "    Pid:Command",
101         .se_cmp         = sort__thread_cmp,
102         .se_snprintf    = hist_entry__thread_snprintf,
103         .se_filter      = hist_entry__thread_filter,
104         .se_width_idx   = HISTC_THREAD,
105 };
106
107 /* --sort comm */
108
109 static int64_t
110 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
111 {
112         /* Compare the addr that should be unique among comm */
113         return strcmp(comm__str(right->comm), comm__str(left->comm));
114 }
115
116 static int64_t
117 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
118 {
119         /* Compare the addr that should be unique among comm */
120         return strcmp(comm__str(right->comm), comm__str(left->comm));
121 }
122
123 static int64_t
124 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
125 {
126         return strcmp(comm__str(right->comm), comm__str(left->comm));
127 }
128
129 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
130                                      size_t size, unsigned int width)
131 {
132         return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
133 }
134
135 struct sort_entry sort_comm = {
136         .se_header      = "Command",
137         .se_cmp         = sort__comm_cmp,
138         .se_collapse    = sort__comm_collapse,
139         .se_sort        = sort__comm_sort,
140         .se_snprintf    = hist_entry__comm_snprintf,
141         .se_filter      = hist_entry__thread_filter,
142         .se_width_idx   = HISTC_COMM,
143 };
144
145 /* --sort dso */
146
147 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
148 {
149         struct dso *dso_l = map_l ? map_l->dso : NULL;
150         struct dso *dso_r = map_r ? map_r->dso : NULL;
151         const char *dso_name_l, *dso_name_r;
152
153         if (!dso_l || !dso_r)
154                 return cmp_null(dso_r, dso_l);
155
156         if (verbose > 0) {
157                 dso_name_l = dso_l->long_name;
158                 dso_name_r = dso_r->long_name;
159         } else {
160                 dso_name_l = dso_l->short_name;
161                 dso_name_r = dso_r->short_name;
162         }
163
164         return strcmp(dso_name_l, dso_name_r);
165 }
166
167 static int64_t
168 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
169 {
170         return _sort__dso_cmp(right->ms.map, left->ms.map);
171 }
172
173 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
174                                      size_t size, unsigned int width)
175 {
176         if (map && map->dso) {
177                 const char *dso_name = verbose > 0 ? map->dso->long_name :
178                         map->dso->short_name;
179                 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
180         }
181
182         return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
183 }
184
185 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
186                                     size_t size, unsigned int width)
187 {
188         return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
189 }
190
191 static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
192 {
193         const struct dso *dso = arg;
194
195         if (type != HIST_FILTER__DSO)
196                 return -1;
197
198         return dso && (!he->ms.map || he->ms.map->dso != dso);
199 }
200
201 struct sort_entry sort_dso = {
202         .se_header      = "Shared Object",
203         .se_cmp         = sort__dso_cmp,
204         .se_snprintf    = hist_entry__dso_snprintf,
205         .se_filter      = hist_entry__dso_filter,
206         .se_width_idx   = HISTC_DSO,
207 };
208
209 /* --sort symbol */
210
211 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
212 {
213         return (int64_t)(right_ip - left_ip);
214 }
215
216 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
217 {
218         if (!sym_l || !sym_r)
219                 return cmp_null(sym_l, sym_r);
220
221         if (sym_l == sym_r)
222                 return 0;
223
224         if (sym_l->start != sym_r->start)
225                 return (int64_t)(sym_r->start - sym_l->start);
226
227         return (int64_t)(sym_r->end - sym_l->end);
228 }
229
230 static int64_t
231 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
232 {
233         int64_t ret;
234
235         if (!left->ms.sym && !right->ms.sym)
236                 return _sort__addr_cmp(left->ip, right->ip);
237
238         /*
239          * comparing symbol address alone is not enough since it's a
240          * relative address within a dso.
241          */
242         if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
243                 ret = sort__dso_cmp(left, right);
244                 if (ret != 0)
245                         return ret;
246         }
247
248         return _sort__sym_cmp(left->ms.sym, right->ms.sym);
249 }
250
251 static int64_t
252 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
253 {
254         if (!left->ms.sym || !right->ms.sym)
255                 return cmp_null(left->ms.sym, right->ms.sym);
256
257         return strcmp(right->ms.sym->name, left->ms.sym->name);
258 }
259
260 static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
261                                      u64 ip, char level, char *bf, size_t size,
262                                      unsigned int width)
263 {
264         size_t ret = 0;
265
266         if (verbose > 0) {
267                 char o = map ? dso__symtab_origin(map->dso) : '!';
268                 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
269                                        BITS_PER_LONG / 4 + 2, ip, o);
270         }
271
272         ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
273         if (sym && map) {
274                 if (map->type == MAP__VARIABLE) {
275                         ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
276                         ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
277                                         ip - map->unmap_ip(map, sym->start));
278                 } else {
279                         ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
280                                                width - ret,
281                                                sym->name);
282                 }
283         } else {
284                 size_t len = BITS_PER_LONG / 4;
285                 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
286                                        len, ip);
287         }
288
289         return ret;
290 }
291
292 static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
293                                     size_t size, unsigned int width)
294 {
295         return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
296                                          he->level, bf, size, width);
297 }
298
299 static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
300 {
301         const char *sym = arg;
302
303         if (type != HIST_FILTER__SYMBOL)
304                 return -1;
305
306         return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
307 }
308
309 struct sort_entry sort_sym = {
310         .se_header      = "Symbol",
311         .se_cmp         = sort__sym_cmp,
312         .se_sort        = sort__sym_sort,
313         .se_snprintf    = hist_entry__sym_snprintf,
314         .se_filter      = hist_entry__sym_filter,
315         .se_width_idx   = HISTC_SYMBOL,
316 };
317
318 /* --sort srcline */
319
320 char *hist_entry__get_srcline(struct hist_entry *he)
321 {
322         struct map *map = he->ms.map;
323
324         if (!map)
325                 return SRCLINE_UNKNOWN;
326
327         return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
328                            he->ms.sym, true, true);
329 }
330
331 static int64_t
332 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
333 {
334         if (!left->srcline)
335                 left->srcline = hist_entry__get_srcline(left);
336         if (!right->srcline)
337                 right->srcline = hist_entry__get_srcline(right);
338
339         return strcmp(right->srcline, left->srcline);
340 }
341
342 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
343                                         size_t size, unsigned int width)
344 {
345         if (!he->srcline)
346                 he->srcline = hist_entry__get_srcline(he);
347
348         return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
349 }
350
351 struct sort_entry sort_srcline = {
352         .se_header      = "Source:Line",
353         .se_cmp         = sort__srcline_cmp,
354         .se_snprintf    = hist_entry__srcline_snprintf,
355         .se_width_idx   = HISTC_SRCLINE,
356 };
357
358 /* --sort srcline_from */
359
360 static int64_t
361 sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
362 {
363         if (!left->branch_info->srcline_from) {
364                 struct map *map = left->branch_info->from.map;
365                 if (!map)
366                         left->branch_info->srcline_from = SRCLINE_UNKNOWN;
367                 else
368                         left->branch_info->srcline_from = get_srcline(map->dso,
369                                            map__rip_2objdump(map,
370                                                              left->branch_info->from.al_addr),
371                                                          left->branch_info->from.sym,
372                                                          true, true);
373         }
374         if (!right->branch_info->srcline_from) {
375                 struct map *map = right->branch_info->from.map;
376                 if (!map)
377                         right->branch_info->srcline_from = SRCLINE_UNKNOWN;
378                 else
379                         right->branch_info->srcline_from = get_srcline(map->dso,
380                                              map__rip_2objdump(map,
381                                                                right->branch_info->from.al_addr),
382                                                      right->branch_info->from.sym,
383                                                      true, true);
384         }
385         return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
386 }
387
388 static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
389                                         size_t size, unsigned int width)
390 {
391         return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
392 }
393
394 struct sort_entry sort_srcline_from = {
395         .se_header      = "From Source:Line",
396         .se_cmp         = sort__srcline_from_cmp,
397         .se_snprintf    = hist_entry__srcline_from_snprintf,
398         .se_width_idx   = HISTC_SRCLINE_FROM,
399 };
400
401 /* --sort srcline_to */
402
403 static int64_t
404 sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
405 {
406         if (!left->branch_info->srcline_to) {
407                 struct map *map = left->branch_info->to.map;
408                 if (!map)
409                         left->branch_info->srcline_to = SRCLINE_UNKNOWN;
410                 else
411                         left->branch_info->srcline_to = get_srcline(map->dso,
412                                            map__rip_2objdump(map,
413                                                              left->branch_info->to.al_addr),
414                                                          left->branch_info->from.sym,
415                                                          true, true);
416         }
417         if (!right->branch_info->srcline_to) {
418                 struct map *map = right->branch_info->to.map;
419                 if (!map)
420                         right->branch_info->srcline_to = SRCLINE_UNKNOWN;
421                 else
422                         right->branch_info->srcline_to = get_srcline(map->dso,
423                                              map__rip_2objdump(map,
424                                                                right->branch_info->to.al_addr),
425                                                      right->branch_info->to.sym,
426                                                      true, true);
427         }
428         return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
429 }
430
431 static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
432                                         size_t size, unsigned int width)
433 {
434         return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
435 }
436
437 struct sort_entry sort_srcline_to = {
438         .se_header      = "To Source:Line",
439         .se_cmp         = sort__srcline_to_cmp,
440         .se_snprintf    = hist_entry__srcline_to_snprintf,
441         .se_width_idx   = HISTC_SRCLINE_TO,
442 };
443
444 /* --sort srcfile */
445
446 static char no_srcfile[1];
447
448 static char *hist_entry__get_srcfile(struct hist_entry *e)
449 {
450         char *sf, *p;
451         struct map *map = e->ms.map;
452
453         if (!map)
454                 return no_srcfile;
455
456         sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
457                          e->ms.sym, false, true, true);
458         if (!strcmp(sf, SRCLINE_UNKNOWN))
459                 return no_srcfile;
460         p = strchr(sf, ':');
461         if (p && *sf) {
462                 *p = 0;
463                 return sf;
464         }
465         free(sf);
466         return no_srcfile;
467 }
468
469 static int64_t
470 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
471 {
472         if (!left->srcfile)
473                 left->srcfile = hist_entry__get_srcfile(left);
474         if (!right->srcfile)
475                 right->srcfile = hist_entry__get_srcfile(right);
476
477         return strcmp(right->srcfile, left->srcfile);
478 }
479
480 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
481                                         size_t size, unsigned int width)
482 {
483         if (!he->srcfile)
484                 he->srcfile = hist_entry__get_srcfile(he);
485
486         return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
487 }
488
489 struct sort_entry sort_srcfile = {
490         .se_header      = "Source File",
491         .se_cmp         = sort__srcfile_cmp,
492         .se_snprintf    = hist_entry__srcfile_snprintf,
493         .se_width_idx   = HISTC_SRCFILE,
494 };
495
496 /* --sort parent */
497
498 static int64_t
499 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
500 {
501         struct symbol *sym_l = left->parent;
502         struct symbol *sym_r = right->parent;
503
504         if (!sym_l || !sym_r)
505                 return cmp_null(sym_l, sym_r);
506
507         return strcmp(sym_r->name, sym_l->name);
508 }
509
510 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
511                                        size_t size, unsigned int width)
512 {
513         return repsep_snprintf(bf, size, "%-*.*s", width, width,
514                               he->parent ? he->parent->name : "[other]");
515 }
516
517 struct sort_entry sort_parent = {
518         .se_header      = "Parent symbol",
519         .se_cmp         = sort__parent_cmp,
520         .se_snprintf    = hist_entry__parent_snprintf,
521         .se_width_idx   = HISTC_PARENT,
522 };
523
524 /* --sort cpu */
525
526 static int64_t
527 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
528 {
529         return right->cpu - left->cpu;
530 }
531
532 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
533                                     size_t size, unsigned int width)
534 {
535         return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
536 }
537
538 struct sort_entry sort_cpu = {
539         .se_header      = "CPU",
540         .se_cmp         = sort__cpu_cmp,
541         .se_snprintf    = hist_entry__cpu_snprintf,
542         .se_width_idx   = HISTC_CPU,
543 };
544
545 /* --sort cgroup_id */
546
547 static int64_t _sort__cgroup_dev_cmp(u64 left_dev, u64 right_dev)
548 {
549         return (int64_t)(right_dev - left_dev);
550 }
551
552 static int64_t _sort__cgroup_inode_cmp(u64 left_ino, u64 right_ino)
553 {
554         return (int64_t)(right_ino - left_ino);
555 }
556
557 static int64_t
558 sort__cgroup_id_cmp(struct hist_entry *left, struct hist_entry *right)
559 {
560         int64_t ret;
561
562         ret = _sort__cgroup_dev_cmp(right->cgroup_id.dev, left->cgroup_id.dev);
563         if (ret != 0)
564                 return ret;
565
566         return _sort__cgroup_inode_cmp(right->cgroup_id.ino,
567                                        left->cgroup_id.ino);
568 }
569
570 static int hist_entry__cgroup_id_snprintf(struct hist_entry *he,
571                                           char *bf, size_t size,
572                                           unsigned int width __maybe_unused)
573 {
574         return repsep_snprintf(bf, size, "%lu/0x%lx", he->cgroup_id.dev,
575                                he->cgroup_id.ino);
576 }
577
578 struct sort_entry sort_cgroup_id = {
579         .se_header      = "cgroup id (dev/inode)",
580         .se_cmp         = sort__cgroup_id_cmp,
581         .se_snprintf    = hist_entry__cgroup_id_snprintf,
582         .se_width_idx   = HISTC_CGROUP_ID,
583 };
584
585 /* --sort socket */
586
587 static int64_t
588 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
589 {
590         return right->socket - left->socket;
591 }
592
593 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
594                                     size_t size, unsigned int width)
595 {
596         return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
597 }
598
599 static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
600 {
601         int sk = *(const int *)arg;
602
603         if (type != HIST_FILTER__SOCKET)
604                 return -1;
605
606         return sk >= 0 && he->socket != sk;
607 }
608
609 struct sort_entry sort_socket = {
610         .se_header      = "Socket",
611         .se_cmp         = sort__socket_cmp,
612         .se_snprintf    = hist_entry__socket_snprintf,
613         .se_filter      = hist_entry__socket_filter,
614         .se_width_idx   = HISTC_SOCKET,
615 };
616
617 /* --sort trace */
618
619 static char *get_trace_output(struct hist_entry *he)
620 {
621         struct trace_seq seq;
622         struct perf_evsel *evsel;
623         struct pevent_record rec = {
624                 .data = he->raw_data,
625                 .size = he->raw_size,
626         };
627
628         evsel = hists_to_evsel(he->hists);
629
630         trace_seq_init(&seq);
631         if (symbol_conf.raw_trace) {
632                 pevent_print_fields(&seq, he->raw_data, he->raw_size,
633                                     evsel->tp_format);
634         } else {
635                 pevent_event_info(&seq, evsel->tp_format, &rec);
636         }
637         /*
638          * Trim the buffer, it starts at 4KB and we're not going to
639          * add anything more to this buffer.
640          */
641         return realloc(seq.buffer, seq.len + 1);
642 }
643
644 static int64_t
645 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
646 {
647         struct perf_evsel *evsel;
648
649         evsel = hists_to_evsel(left->hists);
650         if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
651                 return 0;
652
653         if (left->trace_output == NULL)
654                 left->trace_output = get_trace_output(left);
655         if (right->trace_output == NULL)
656                 right->trace_output = get_trace_output(right);
657
658         return strcmp(right->trace_output, left->trace_output);
659 }
660
661 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
662                                     size_t size, unsigned int width)
663 {
664         struct perf_evsel *evsel;
665
666         evsel = hists_to_evsel(he->hists);
667         if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
668                 return scnprintf(bf, size, "%-.*s", width, "N/A");
669
670         if (he->trace_output == NULL)
671                 he->trace_output = get_trace_output(he);
672         return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
673 }
674
675 struct sort_entry sort_trace = {
676         .se_header      = "Trace output",
677         .se_cmp         = sort__trace_cmp,
678         .se_snprintf    = hist_entry__trace_snprintf,
679         .se_width_idx   = HISTC_TRACE,
680 };
681
682 /* sort keys for branch stacks */
683
684 static int64_t
685 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
686 {
687         if (!left->branch_info || !right->branch_info)
688                 return cmp_null(left->branch_info, right->branch_info);
689
690         return _sort__dso_cmp(left->branch_info->from.map,
691                               right->branch_info->from.map);
692 }
693
694 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
695                                     size_t size, unsigned int width)
696 {
697         if (he->branch_info)
698                 return _hist_entry__dso_snprintf(he->branch_info->from.map,
699                                                  bf, size, width);
700         else
701                 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
702 }
703
704 static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
705                                        const void *arg)
706 {
707         const struct dso *dso = arg;
708
709         if (type != HIST_FILTER__DSO)
710                 return -1;
711
712         return dso && (!he->branch_info || !he->branch_info->from.map ||
713                        he->branch_info->from.map->dso != dso);
714 }
715
716 static int64_t
717 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
718 {
719         if (!left->branch_info || !right->branch_info)
720                 return cmp_null(left->branch_info, right->branch_info);
721
722         return _sort__dso_cmp(left->branch_info->to.map,
723                               right->branch_info->to.map);
724 }
725
726 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
727                                        size_t size, unsigned int width)
728 {
729         if (he->branch_info)
730                 return _hist_entry__dso_snprintf(he->branch_info->to.map,
731                                                  bf, size, width);
732         else
733                 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
734 }
735
736 static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
737                                      const void *arg)
738 {
739         const struct dso *dso = arg;
740
741         if (type != HIST_FILTER__DSO)
742                 return -1;
743
744         return dso && (!he->branch_info || !he->branch_info->to.map ||
745                        he->branch_info->to.map->dso != dso);
746 }
747
748 static int64_t
749 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
750 {
751         struct addr_map_symbol *from_l = &left->branch_info->from;
752         struct addr_map_symbol *from_r = &right->branch_info->from;
753
754         if (!left->branch_info || !right->branch_info)
755                 return cmp_null(left->branch_info, right->branch_info);
756
757         from_l = &left->branch_info->from;
758         from_r = &right->branch_info->from;
759
760         if (!from_l->sym && !from_r->sym)
761                 return _sort__addr_cmp(from_l->addr, from_r->addr);
762
763         return _sort__sym_cmp(from_l->sym, from_r->sym);
764 }
765
766 static int64_t
767 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
768 {
769         struct addr_map_symbol *to_l, *to_r;
770
771         if (!left->branch_info || !right->branch_info)
772                 return cmp_null(left->branch_info, right->branch_info);
773
774         to_l = &left->branch_info->to;
775         to_r = &right->branch_info->to;
776
777         if (!to_l->sym && !to_r->sym)
778                 return _sort__addr_cmp(to_l->addr, to_r->addr);
779
780         return _sort__sym_cmp(to_l->sym, to_r->sym);
781 }
782
783 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
784                                          size_t size, unsigned int width)
785 {
786         if (he->branch_info) {
787                 struct addr_map_symbol *from = &he->branch_info->from;
788
789                 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
790                                                  he->level, bf, size, width);
791         }
792
793         return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
794 }
795
796 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
797                                        size_t size, unsigned int width)
798 {
799         if (he->branch_info) {
800                 struct addr_map_symbol *to = &he->branch_info->to;
801
802                 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
803                                                  he->level, bf, size, width);
804         }
805
806         return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
807 }
808
809 static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
810                                        const void *arg)
811 {
812         const char *sym = arg;
813
814         if (type != HIST_FILTER__SYMBOL)
815                 return -1;
816
817         return sym && !(he->branch_info && he->branch_info->from.sym &&
818                         strstr(he->branch_info->from.sym->name, sym));
819 }
820
821 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
822                                        const void *arg)
823 {
824         const char *sym = arg;
825
826         if (type != HIST_FILTER__SYMBOL)
827                 return -1;
828
829         return sym && !(he->branch_info && he->branch_info->to.sym &&
830                         strstr(he->branch_info->to.sym->name, sym));
831 }
832
833 struct sort_entry sort_dso_from = {
834         .se_header      = "Source Shared Object",
835         .se_cmp         = sort__dso_from_cmp,
836         .se_snprintf    = hist_entry__dso_from_snprintf,
837         .se_filter      = hist_entry__dso_from_filter,
838         .se_width_idx   = HISTC_DSO_FROM,
839 };
840
841 struct sort_entry sort_dso_to = {
842         .se_header      = "Target Shared Object",
843         .se_cmp         = sort__dso_to_cmp,
844         .se_snprintf    = hist_entry__dso_to_snprintf,
845         .se_filter      = hist_entry__dso_to_filter,
846         .se_width_idx   = HISTC_DSO_TO,
847 };
848
849 struct sort_entry sort_sym_from = {
850         .se_header      = "Source Symbol",
851         .se_cmp         = sort__sym_from_cmp,
852         .se_snprintf    = hist_entry__sym_from_snprintf,
853         .se_filter      = hist_entry__sym_from_filter,
854         .se_width_idx   = HISTC_SYMBOL_FROM,
855 };
856
857 struct sort_entry sort_sym_to = {
858         .se_header      = "Target Symbol",
859         .se_cmp         = sort__sym_to_cmp,
860         .se_snprintf    = hist_entry__sym_to_snprintf,
861         .se_filter      = hist_entry__sym_to_filter,
862         .se_width_idx   = HISTC_SYMBOL_TO,
863 };
864
865 static int64_t
866 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
867 {
868         unsigned char mp, p;
869
870         if (!left->branch_info || !right->branch_info)
871                 return cmp_null(left->branch_info, right->branch_info);
872
873         mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
874         p  = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
875         return mp || p;
876 }
877
878 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
879                                     size_t size, unsigned int width){
880         static const char *out = "N/A";
881
882         if (he->branch_info) {
883                 if (he->branch_info->flags.predicted)
884                         out = "N";
885                 else if (he->branch_info->flags.mispred)
886                         out = "Y";
887         }
888
889         return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
890 }
891
892 static int64_t
893 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
894 {
895         if (!left->branch_info || !right->branch_info)
896                 return cmp_null(left->branch_info, right->branch_info);
897
898         return left->branch_info->flags.cycles -
899                 right->branch_info->flags.cycles;
900 }
901
902 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
903                                     size_t size, unsigned int width)
904 {
905         if (!he->branch_info)
906                 return scnprintf(bf, size, "%-.*s", width, "N/A");
907         if (he->branch_info->flags.cycles == 0)
908                 return repsep_snprintf(bf, size, "%-*s", width, "-");
909         return repsep_snprintf(bf, size, "%-*hd", width,
910                                he->branch_info->flags.cycles);
911 }
912
913 struct sort_entry sort_cycles = {
914         .se_header      = "Basic Block Cycles",
915         .se_cmp         = sort__cycles_cmp,
916         .se_snprintf    = hist_entry__cycles_snprintf,
917         .se_width_idx   = HISTC_CYCLES,
918 };
919
920 /* --sort daddr_sym */
921 int64_t
922 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
923 {
924         uint64_t l = 0, r = 0;
925
926         if (left->mem_info)
927                 l = left->mem_info->daddr.addr;
928         if (right->mem_info)
929                 r = right->mem_info->daddr.addr;
930
931         return (int64_t)(r - l);
932 }
933
934 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
935                                     size_t size, unsigned int width)
936 {
937         uint64_t addr = 0;
938         struct map *map = NULL;
939         struct symbol *sym = NULL;
940
941         if (he->mem_info) {
942                 addr = he->mem_info->daddr.addr;
943                 map = he->mem_info->daddr.map;
944                 sym = he->mem_info->daddr.sym;
945         }
946         return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
947                                          width);
948 }
949
950 int64_t
951 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
952 {
953         uint64_t l = 0, r = 0;
954
955         if (left->mem_info)
956                 l = left->mem_info->iaddr.addr;
957         if (right->mem_info)
958                 r = right->mem_info->iaddr.addr;
959
960         return (int64_t)(r - l);
961 }
962
963 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
964                                     size_t size, unsigned int width)
965 {
966         uint64_t addr = 0;
967         struct map *map = NULL;
968         struct symbol *sym = NULL;
969
970         if (he->mem_info) {
971                 addr = he->mem_info->iaddr.addr;
972                 map  = he->mem_info->iaddr.map;
973                 sym  = he->mem_info->iaddr.sym;
974         }
975         return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
976                                          width);
977 }
978
979 static int64_t
980 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
981 {
982         struct map *map_l = NULL;
983         struct map *map_r = NULL;
984
985         if (left->mem_info)
986                 map_l = left->mem_info->daddr.map;
987         if (right->mem_info)
988                 map_r = right->mem_info->daddr.map;
989
990         return _sort__dso_cmp(map_l, map_r);
991 }
992
993 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
994                                     size_t size, unsigned int width)
995 {
996         struct map *map = NULL;
997
998         if (he->mem_info)
999                 map = he->mem_info->daddr.map;
1000
1001         return _hist_entry__dso_snprintf(map, bf, size, width);
1002 }
1003
1004 static int64_t
1005 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
1006 {
1007         union perf_mem_data_src data_src_l;
1008         union perf_mem_data_src data_src_r;
1009
1010         if (left->mem_info)
1011                 data_src_l = left->mem_info->data_src;
1012         else
1013                 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
1014
1015         if (right->mem_info)
1016                 data_src_r = right->mem_info->data_src;
1017         else
1018                 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
1019
1020         return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
1021 }
1022
1023 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
1024                                     size_t size, unsigned int width)
1025 {
1026         char out[10];
1027
1028         perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
1029         return repsep_snprintf(bf, size, "%.*s", width, out);
1030 }
1031
1032 static int64_t
1033 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
1034 {
1035         union perf_mem_data_src data_src_l;
1036         union perf_mem_data_src data_src_r;
1037
1038         if (left->mem_info)
1039                 data_src_l = left->mem_info->data_src;
1040         else
1041                 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
1042
1043         if (right->mem_info)
1044                 data_src_r = right->mem_info->data_src;
1045         else
1046                 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
1047
1048         return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
1049 }
1050
1051 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
1052                                     size_t size, unsigned int width)
1053 {
1054         char out[64];
1055
1056         perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
1057         return repsep_snprintf(bf, size, "%-*s", width, out);
1058 }
1059
1060 static int64_t
1061 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
1062 {
1063         union perf_mem_data_src data_src_l;
1064         union perf_mem_data_src data_src_r;
1065
1066         if (left->mem_info)
1067                 data_src_l = left->mem_info->data_src;
1068         else
1069                 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
1070
1071         if (right->mem_info)
1072                 data_src_r = right->mem_info->data_src;
1073         else
1074                 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
1075
1076         return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
1077 }
1078
1079 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
1080                                     size_t size, unsigned int width)
1081 {
1082         char out[64];
1083
1084         perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
1085         return repsep_snprintf(bf, size, "%-*s", width, out);
1086 }
1087
1088 static int64_t
1089 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
1090 {
1091         union perf_mem_data_src data_src_l;
1092         union perf_mem_data_src data_src_r;
1093
1094         if (left->mem_info)
1095                 data_src_l = left->mem_info->data_src;
1096         else
1097                 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
1098
1099         if (right->mem_info)
1100                 data_src_r = right->mem_info->data_src;
1101         else
1102                 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
1103
1104         return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
1105 }
1106
1107 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
1108                                     size_t size, unsigned int width)
1109 {
1110         char out[64];
1111
1112         perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
1113         return repsep_snprintf(bf, size, "%-*s", width, out);
1114 }
1115
1116 int64_t
1117 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
1118 {
1119         u64 l, r;
1120         struct map *l_map, *r_map;
1121
1122         if (!left->mem_info)  return -1;
1123         if (!right->mem_info) return 1;
1124
1125         /* group event types together */
1126         if (left->cpumode > right->cpumode) return -1;
1127         if (left->cpumode < right->cpumode) return 1;
1128
1129         l_map = left->mem_info->daddr.map;
1130         r_map = right->mem_info->daddr.map;
1131
1132         /* if both are NULL, jump to sort on al_addr instead */
1133         if (!l_map && !r_map)
1134                 goto addr;
1135
1136         if (!l_map) return -1;
1137         if (!r_map) return 1;
1138
1139         if (l_map->maj > r_map->maj) return -1;
1140         if (l_map->maj < r_map->maj) return 1;
1141
1142         if (l_map->min > r_map->min) return -1;
1143         if (l_map->min < r_map->min) return 1;
1144
1145         if (l_map->ino > r_map->ino) return -1;
1146         if (l_map->ino < r_map->ino) return 1;
1147
1148         if (l_map->ino_generation > r_map->ino_generation) return -1;
1149         if (l_map->ino_generation < r_map->ino_generation) return 1;
1150
1151         /*
1152          * Addresses with no major/minor numbers are assumed to be
1153          * anonymous in userspace.  Sort those on pid then address.
1154          *
1155          * The kernel and non-zero major/minor mapped areas are
1156          * assumed to be unity mapped.  Sort those on address.
1157          */
1158
1159         if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1160             (!(l_map->flags & MAP_SHARED)) &&
1161             !l_map->maj && !l_map->min && !l_map->ino &&
1162             !l_map->ino_generation) {
1163                 /* userspace anonymous */
1164
1165                 if (left->thread->pid_ > right->thread->pid_) return -1;
1166                 if (left->thread->pid_ < right->thread->pid_) return 1;
1167         }
1168
1169 addr:
1170         /* al_addr does all the right addr - start + offset calculations */
1171         l = cl_address(left->mem_info->daddr.al_addr);
1172         r = cl_address(right->mem_info->daddr.al_addr);
1173
1174         if (l > r) return -1;
1175         if (l < r) return 1;
1176
1177         return 0;
1178 }
1179
1180 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1181                                           size_t size, unsigned int width)
1182 {
1183
1184         uint64_t addr = 0;
1185         struct map *map = NULL;
1186         struct symbol *sym = NULL;
1187         char level = he->level;
1188
1189         if (he->mem_info) {
1190                 addr = cl_address(he->mem_info->daddr.al_addr);
1191                 map = he->mem_info->daddr.map;
1192                 sym = he->mem_info->daddr.sym;
1193
1194                 /* print [s] for shared data mmaps */
1195                 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1196                      map && (map->type == MAP__VARIABLE) &&
1197                     (map->flags & MAP_SHARED) &&
1198                     (map->maj || map->min || map->ino ||
1199                      map->ino_generation))
1200                         level = 's';
1201                 else if (!map)
1202                         level = 'X';
1203         }
1204         return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1205                                          width);
1206 }
1207
1208 struct sort_entry sort_mispredict = {
1209         .se_header      = "Branch Mispredicted",
1210         .se_cmp         = sort__mispredict_cmp,
1211         .se_snprintf    = hist_entry__mispredict_snprintf,
1212         .se_width_idx   = HISTC_MISPREDICT,
1213 };
1214
1215 static u64 he_weight(struct hist_entry *he)
1216 {
1217         return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1218 }
1219
1220 static int64_t
1221 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1222 {
1223         return he_weight(left) - he_weight(right);
1224 }
1225
1226 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1227                                     size_t size, unsigned int width)
1228 {
1229         return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1230 }
1231
1232 struct sort_entry sort_local_weight = {
1233         .se_header      = "Local Weight",
1234         .se_cmp         = sort__local_weight_cmp,
1235         .se_snprintf    = hist_entry__local_weight_snprintf,
1236         .se_width_idx   = HISTC_LOCAL_WEIGHT,
1237 };
1238
1239 static int64_t
1240 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1241 {
1242         return left->stat.weight - right->stat.weight;
1243 }
1244
1245 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1246                                               size_t size, unsigned int width)
1247 {
1248         return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1249 }
1250
1251 struct sort_entry sort_global_weight = {
1252         .se_header      = "Weight",
1253         .se_cmp         = sort__global_weight_cmp,
1254         .se_snprintf    = hist_entry__global_weight_snprintf,
1255         .se_width_idx   = HISTC_GLOBAL_WEIGHT,
1256 };
1257
1258 struct sort_entry sort_mem_daddr_sym = {
1259         .se_header      = "Data Symbol",
1260         .se_cmp         = sort__daddr_cmp,
1261         .se_snprintf    = hist_entry__daddr_snprintf,
1262         .se_width_idx   = HISTC_MEM_DADDR_SYMBOL,
1263 };
1264
1265 struct sort_entry sort_mem_iaddr_sym = {
1266         .se_header      = "Code Symbol",
1267         .se_cmp         = sort__iaddr_cmp,
1268         .se_snprintf    = hist_entry__iaddr_snprintf,
1269         .se_width_idx   = HISTC_MEM_IADDR_SYMBOL,
1270 };
1271
1272 struct sort_entry sort_mem_daddr_dso = {
1273         .se_header      = "Data Object",
1274         .se_cmp         = sort__dso_daddr_cmp,
1275         .se_snprintf    = hist_entry__dso_daddr_snprintf,
1276         .se_width_idx   = HISTC_MEM_DADDR_DSO,
1277 };
1278
1279 struct sort_entry sort_mem_locked = {
1280         .se_header      = "Locked",
1281         .se_cmp         = sort__locked_cmp,
1282         .se_snprintf    = hist_entry__locked_snprintf,
1283         .se_width_idx   = HISTC_MEM_LOCKED,
1284 };
1285
1286 struct sort_entry sort_mem_tlb = {
1287         .se_header      = "TLB access",
1288         .se_cmp         = sort__tlb_cmp,
1289         .se_snprintf    = hist_entry__tlb_snprintf,
1290         .se_width_idx   = HISTC_MEM_TLB,
1291 };
1292
1293 struct sort_entry sort_mem_lvl = {
1294         .se_header      = "Memory access",
1295         .se_cmp         = sort__lvl_cmp,
1296         .se_snprintf    = hist_entry__lvl_snprintf,
1297         .se_width_idx   = HISTC_MEM_LVL,
1298 };
1299
1300 struct sort_entry sort_mem_snoop = {
1301         .se_header      = "Snoop",
1302         .se_cmp         = sort__snoop_cmp,
1303         .se_snprintf    = hist_entry__snoop_snprintf,
1304         .se_width_idx   = HISTC_MEM_SNOOP,
1305 };
1306
1307 struct sort_entry sort_mem_dcacheline = {
1308         .se_header      = "Data Cacheline",
1309         .se_cmp         = sort__dcacheline_cmp,
1310         .se_snprintf    = hist_entry__dcacheline_snprintf,
1311         .se_width_idx   = HISTC_MEM_DCACHELINE,
1312 };
1313
1314 static int64_t
1315 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1316 {
1317         if (!left->branch_info || !right->branch_info)
1318                 return cmp_null(left->branch_info, right->branch_info);
1319
1320         return left->branch_info->flags.abort !=
1321                 right->branch_info->flags.abort;
1322 }
1323
1324 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1325                                     size_t size, unsigned int width)
1326 {
1327         static const char *out = "N/A";
1328
1329         if (he->branch_info) {
1330                 if (he->branch_info->flags.abort)
1331                         out = "A";
1332                 else
1333                         out = ".";
1334         }
1335
1336         return repsep_snprintf(bf, size, "%-*s", width, out);
1337 }
1338
1339 struct sort_entry sort_abort = {
1340         .se_header      = "Transaction abort",
1341         .se_cmp         = sort__abort_cmp,
1342         .se_snprintf    = hist_entry__abort_snprintf,
1343         .se_width_idx   = HISTC_ABORT,
1344 };
1345
1346 static int64_t
1347 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1348 {
1349         if (!left->branch_info || !right->branch_info)
1350                 return cmp_null(left->branch_info, right->branch_info);
1351
1352         return left->branch_info->flags.in_tx !=
1353                 right->branch_info->flags.in_tx;
1354 }
1355
1356 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1357                                     size_t size, unsigned int width)
1358 {
1359         static const char *out = "N/A";
1360
1361         if (he->branch_info) {
1362                 if (he->branch_info->flags.in_tx)
1363                         out = "T";
1364                 else
1365                         out = ".";
1366         }
1367
1368         return repsep_snprintf(bf, size, "%-*s", width, out);
1369 }
1370
1371 struct sort_entry sort_in_tx = {
1372         .se_header      = "Branch in transaction",
1373         .se_cmp         = sort__in_tx_cmp,
1374         .se_snprintf    = hist_entry__in_tx_snprintf,
1375         .se_width_idx   = HISTC_IN_TX,
1376 };
1377
1378 static int64_t
1379 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1380 {
1381         return left->transaction - right->transaction;
1382 }
1383
1384 static inline char *add_str(char *p, const char *str)
1385 {
1386         strcpy(p, str);
1387         return p + strlen(str);
1388 }
1389
1390 static struct txbit {
1391         unsigned flag;
1392         const char *name;
1393         int skip_for_len;
1394 } txbits[] = {
1395         { PERF_TXN_ELISION,        "EL ",        0 },
1396         { PERF_TXN_TRANSACTION,    "TX ",        1 },
1397         { PERF_TXN_SYNC,           "SYNC ",      1 },
1398         { PERF_TXN_ASYNC,          "ASYNC ",     0 },
1399         { PERF_TXN_RETRY,          "RETRY ",     0 },
1400         { PERF_TXN_CONFLICT,       "CON ",       0 },
1401         { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1402         { PERF_TXN_CAPACITY_READ,  "CAP-READ ",  0 },
1403         { 0, NULL, 0 }
1404 };
1405
1406 int hist_entry__transaction_len(void)
1407 {
1408         int i;
1409         int len = 0;
1410
1411         for (i = 0; txbits[i].name; i++) {
1412                 if (!txbits[i].skip_for_len)
1413                         len += strlen(txbits[i].name);
1414         }
1415         len += 4; /* :XX<space> */
1416         return len;
1417 }
1418
1419 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1420                                             size_t size, unsigned int width)
1421 {
1422         u64 t = he->transaction;
1423         char buf[128];
1424         char *p = buf;
1425         int i;
1426
1427         buf[0] = 0;
1428         for (i = 0; txbits[i].name; i++)
1429                 if (txbits[i].flag & t)
1430                         p = add_str(p, txbits[i].name);
1431         if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1432                 p = add_str(p, "NEITHER ");
1433         if (t & PERF_TXN_ABORT_MASK) {
1434                 sprintf(p, ":%" PRIx64,
1435                         (t & PERF_TXN_ABORT_MASK) >>
1436                         PERF_TXN_ABORT_SHIFT);
1437                 p += strlen(p);
1438         }
1439
1440         return repsep_snprintf(bf, size, "%-*s", width, buf);
1441 }
1442
1443 struct sort_entry sort_transaction = {
1444         .se_header      = "Transaction                ",
1445         .se_cmp         = sort__transaction_cmp,
1446         .se_snprintf    = hist_entry__transaction_snprintf,
1447         .se_width_idx   = HISTC_TRANSACTION,
1448 };
1449
1450 /* --sort symbol_size */
1451
1452 static int64_t _sort__sym_size_cmp(struct symbol *sym_l, struct symbol *sym_r)
1453 {
1454         int64_t size_l = sym_l != NULL ? symbol__size(sym_l) : 0;
1455         int64_t size_r = sym_r != NULL ? symbol__size(sym_r) : 0;
1456
1457         return size_l < size_r ? -1 :
1458                 size_l == size_r ? 0 : 1;
1459 }
1460
1461 static int64_t
1462 sort__sym_size_cmp(struct hist_entry *left, struct hist_entry *right)
1463 {
1464         return _sort__sym_size_cmp(right->ms.sym, left->ms.sym);
1465 }
1466
1467 static int _hist_entry__sym_size_snprintf(struct symbol *sym, char *bf,
1468                                           size_t bf_size, unsigned int width)
1469 {
1470         if (sym)
1471                 return repsep_snprintf(bf, bf_size, "%*d", width, symbol__size(sym));
1472
1473         return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1474 }
1475
1476 static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
1477                                          size_t size, unsigned int width)
1478 {
1479         return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
1480 }
1481
1482 struct sort_entry sort_sym_size = {
1483         .se_header      = "Symbol size",
1484         .se_cmp         = sort__sym_size_cmp,
1485         .se_snprintf    = hist_entry__sym_size_snprintf,
1486         .se_width_idx   = HISTC_SYM_SIZE,
1487 };
1488
1489
1490 struct sort_dimension {
1491         const char              *name;
1492         struct sort_entry       *entry;
1493         int                     taken;
1494 };
1495
1496 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1497
1498 static struct sort_dimension common_sort_dimensions[] = {
1499         DIM(SORT_PID, "pid", sort_thread),
1500         DIM(SORT_COMM, "comm", sort_comm),
1501         DIM(SORT_DSO, "dso", sort_dso),
1502         DIM(SORT_SYM, "symbol", sort_sym),
1503         DIM(SORT_PARENT, "parent", sort_parent),
1504         DIM(SORT_CPU, "cpu", sort_cpu),
1505         DIM(SORT_SOCKET, "socket", sort_socket),
1506         DIM(SORT_SRCLINE, "srcline", sort_srcline),
1507         DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1508         DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1509         DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1510         DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1511         DIM(SORT_TRACE, "trace", sort_trace),
1512         DIM(SORT_SYM_SIZE, "symbol_size", sort_sym_size),
1513         DIM(SORT_CGROUP_ID, "cgroup_id", sort_cgroup_id),
1514 };
1515
1516 #undef DIM
1517
1518 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1519
1520 static struct sort_dimension bstack_sort_dimensions[] = {
1521         DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1522         DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1523         DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1524         DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1525         DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1526         DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1527         DIM(SORT_ABORT, "abort", sort_abort),
1528         DIM(SORT_CYCLES, "cycles", sort_cycles),
1529         DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
1530         DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
1531 };
1532
1533 #undef DIM
1534
1535 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1536
1537 static struct sort_dimension memory_sort_dimensions[] = {
1538         DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1539         DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1540         DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1541         DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1542         DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1543         DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1544         DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1545         DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1546 };
1547
1548 #undef DIM
1549
1550 struct hpp_dimension {
1551         const char              *name;
1552         struct perf_hpp_fmt     *fmt;
1553         int                     taken;
1554 };
1555
1556 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1557
1558 static struct hpp_dimension hpp_sort_dimensions[] = {
1559         DIM(PERF_HPP__OVERHEAD, "overhead"),
1560         DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1561         DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1562         DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1563         DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1564         DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1565         DIM(PERF_HPP__SAMPLES, "sample"),
1566         DIM(PERF_HPP__PERIOD, "period"),
1567 };
1568
1569 #undef DIM
1570
1571 struct hpp_sort_entry {
1572         struct perf_hpp_fmt hpp;
1573         struct sort_entry *se;
1574 };
1575
1576 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1577 {
1578         struct hpp_sort_entry *hse;
1579
1580         if (!perf_hpp__is_sort_entry(fmt))
1581                 return;
1582
1583         hse = container_of(fmt, struct hpp_sort_entry, hpp);
1584         hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1585 }
1586
1587 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1588                               struct hists *hists, int line __maybe_unused,
1589                               int *span __maybe_unused)
1590 {
1591         struct hpp_sort_entry *hse;
1592         size_t len = fmt->user_len;
1593
1594         hse = container_of(fmt, struct hpp_sort_entry, hpp);
1595
1596         if (!len)
1597                 len = hists__col_len(hists, hse->se->se_width_idx);
1598
1599         return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1600 }
1601
1602 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1603                              struct perf_hpp *hpp __maybe_unused,
1604                              struct hists *hists)
1605 {
1606         struct hpp_sort_entry *hse;
1607         size_t len = fmt->user_len;
1608
1609         hse = container_of(fmt, struct hpp_sort_entry, hpp);
1610
1611         if (!len)
1612                 len = hists__col_len(hists, hse->se->se_width_idx);
1613
1614         return len;
1615 }
1616
1617 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1618                              struct hist_entry *he)
1619 {
1620         struct hpp_sort_entry *hse;
1621         size_t len = fmt->user_len;
1622
1623         hse = container_of(fmt, struct hpp_sort_entry, hpp);
1624
1625         if (!len)
1626                 len = hists__col_len(he->hists, hse->se->se_width_idx);
1627
1628         return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1629 }
1630
1631 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1632                                struct hist_entry *a, struct hist_entry *b)
1633 {
1634         struct hpp_sort_entry *hse;
1635
1636         hse = container_of(fmt, struct hpp_sort_entry, hpp);
1637         return hse->se->se_cmp(a, b);
1638 }
1639
1640 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1641                                     struct hist_entry *a, struct hist_entry *b)
1642 {
1643         struct hpp_sort_entry *hse;
1644         int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1645
1646         hse = container_of(fmt, struct hpp_sort_entry, hpp);
1647         collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1648         return collapse_fn(a, b);
1649 }
1650
1651 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1652                                 struct hist_entry *a, struct hist_entry *b)
1653 {
1654         struct hpp_sort_entry *hse;
1655         int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1656
1657         hse = container_of(fmt, struct hpp_sort_entry, hpp);
1658         sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1659         return sort_fn(a, b);
1660 }
1661
1662 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1663 {
1664         return format->header == __sort__hpp_header;
1665 }
1666
1667 #define MK_SORT_ENTRY_CHK(key)                                  \
1668 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt)   \
1669 {                                                               \
1670         struct hpp_sort_entry *hse;                             \
1671                                                                 \
1672         if (!perf_hpp__is_sort_entry(fmt))                      \
1673                 return false;                                   \
1674                                                                 \
1675         hse = container_of(fmt, struct hpp_sort_entry, hpp);    \
1676         return hse->se == &sort_ ## key ;                       \
1677 }
1678
1679 MK_SORT_ENTRY_CHK(trace)
1680 MK_SORT_ENTRY_CHK(srcline)
1681 MK_SORT_ENTRY_CHK(srcfile)
1682 MK_SORT_ENTRY_CHK(thread)
1683 MK_SORT_ENTRY_CHK(comm)
1684 MK_SORT_ENTRY_CHK(dso)
1685 MK_SORT_ENTRY_CHK(sym)
1686
1687
1688 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1689 {
1690         struct hpp_sort_entry *hse_a;
1691         struct hpp_sort_entry *hse_b;
1692
1693         if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1694                 return false;
1695
1696         hse_a = container_of(a, struct hpp_sort_entry, hpp);
1697         hse_b = container_of(b, struct hpp_sort_entry, hpp);
1698
1699         return hse_a->se == hse_b->se;
1700 }
1701
1702 static void hse_free(struct perf_hpp_fmt *fmt)
1703 {
1704         struct hpp_sort_entry *hse;
1705
1706         hse = container_of(fmt, struct hpp_sort_entry, hpp);
1707         free(hse);
1708 }
1709
1710 static struct hpp_sort_entry *
1711 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
1712 {
1713         struct hpp_sort_entry *hse;
1714
1715         hse = malloc(sizeof(*hse));
1716         if (hse == NULL) {
1717                 pr_err("Memory allocation failed\n");
1718                 return NULL;
1719         }
1720
1721         hse->se = sd->entry;
1722         hse->hpp.name = sd->entry->se_header;
1723         hse->hpp.header = __sort__hpp_header;
1724         hse->hpp.width = __sort__hpp_width;
1725         hse->hpp.entry = __sort__hpp_entry;
1726         hse->hpp.color = NULL;
1727
1728         hse->hpp.cmp = __sort__hpp_cmp;
1729         hse->hpp.collapse = __sort__hpp_collapse;
1730         hse->hpp.sort = __sort__hpp_sort;
1731         hse->hpp.equal = __sort__hpp_equal;
1732         hse->hpp.free = hse_free;
1733
1734         INIT_LIST_HEAD(&hse->hpp.list);
1735         INIT_LIST_HEAD(&hse->hpp.sort_list);
1736         hse->hpp.elide = false;
1737         hse->hpp.len = 0;
1738         hse->hpp.user_len = 0;
1739         hse->hpp.level = level;
1740
1741         return hse;
1742 }
1743
1744 static void hpp_free(struct perf_hpp_fmt *fmt)
1745 {
1746         free(fmt);
1747 }
1748
1749 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1750                                                        int level)
1751 {
1752         struct perf_hpp_fmt *fmt;
1753
1754         fmt = memdup(hd->fmt, sizeof(*fmt));
1755         if (fmt) {
1756                 INIT_LIST_HEAD(&fmt->list);
1757                 INIT_LIST_HEAD(&fmt->sort_list);
1758                 fmt->free = hpp_free;
1759                 fmt->level = level;
1760         }
1761
1762         return fmt;
1763 }
1764
1765 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1766 {
1767         struct perf_hpp_fmt *fmt;
1768         struct hpp_sort_entry *hse;
1769         int ret = -1;
1770         int r;
1771
1772         perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1773                 if (!perf_hpp__is_sort_entry(fmt))
1774                         continue;
1775
1776                 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1777                 if (hse->se->se_filter == NULL)
1778                         continue;
1779
1780                 /*
1781                  * hist entry is filtered if any of sort key in the hpp list
1782                  * is applied.  But it should skip non-matched filter types.
1783                  */
1784                 r = hse->se->se_filter(he, type, arg);
1785                 if (r >= 0) {
1786                         if (ret < 0)
1787                                 ret = 0;
1788                         ret |= r;
1789                 }
1790         }
1791
1792         return ret;
1793 }
1794
1795 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1796                                           struct perf_hpp_list *list,
1797                                           int level)
1798 {
1799         struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
1800
1801         if (hse == NULL)
1802                 return -1;
1803
1804         perf_hpp_list__register_sort_field(list, &hse->hpp);
1805         return 0;
1806 }
1807
1808 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1809                                             struct perf_hpp_list *list)
1810 {
1811         struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
1812
1813         if (hse == NULL)
1814                 return -1;
1815
1816         perf_hpp_list__column_register(list, &hse->hpp);
1817         return 0;
1818 }
1819
1820 struct hpp_dynamic_entry {
1821         struct perf_hpp_fmt hpp;
1822         struct perf_evsel *evsel;
1823         struct format_field *field;
1824         unsigned dynamic_len;
1825         bool raw_trace;
1826 };
1827
1828 static int hde_width(struct hpp_dynamic_entry *hde)
1829 {
1830         if (!hde->hpp.len) {
1831                 int len = hde->dynamic_len;
1832                 int namelen = strlen(hde->field->name);
1833                 int fieldlen = hde->field->size;
1834
1835                 if (namelen > len)
1836                         len = namelen;
1837
1838                 if (!(hde->field->flags & FIELD_IS_STRING)) {
1839                         /* length for print hex numbers */
1840                         fieldlen = hde->field->size * 2 + 2;
1841                 }
1842                 if (fieldlen > len)
1843                         len = fieldlen;
1844
1845                 hde->hpp.len = len;
1846         }
1847         return hde->hpp.len;
1848 }
1849
1850 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1851                                struct hist_entry *he)
1852 {
1853         char *str, *pos;
1854         struct format_field *field = hde->field;
1855         size_t namelen;
1856         bool last = false;
1857
1858         if (hde->raw_trace)
1859                 return;
1860
1861         /* parse pretty print result and update max length */
1862         if (!he->trace_output)
1863                 he->trace_output = get_trace_output(he);
1864
1865         namelen = strlen(field->name);
1866         str = he->trace_output;
1867
1868         while (str) {
1869                 pos = strchr(str, ' ');
1870                 if (pos == NULL) {
1871                         last = true;
1872                         pos = str + strlen(str);
1873                 }
1874
1875                 if (!strncmp(str, field->name, namelen)) {
1876                         size_t len;
1877
1878                         str += namelen + 1;
1879                         len = pos - str;
1880
1881                         if (len > hde->dynamic_len)
1882                                 hde->dynamic_len = len;
1883                         break;
1884                 }
1885
1886                 if (last)
1887                         str = NULL;
1888                 else
1889                         str = pos + 1;
1890         }
1891 }
1892
1893 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1894                               struct hists *hists __maybe_unused,
1895                               int line __maybe_unused,
1896                               int *span __maybe_unused)
1897 {
1898         struct hpp_dynamic_entry *hde;
1899         size_t len = fmt->user_len;
1900
1901         hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1902
1903         if (!len)
1904                 len = hde_width(hde);
1905
1906         return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1907 }
1908
1909 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1910                              struct perf_hpp *hpp __maybe_unused,
1911                              struct hists *hists __maybe_unused)
1912 {
1913         struct hpp_dynamic_entry *hde;
1914         size_t len = fmt->user_len;
1915
1916         hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1917
1918         if (!len)
1919                 len = hde_width(hde);
1920
1921         return len;
1922 }
1923
1924 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1925 {
1926         struct hpp_dynamic_entry *hde;
1927
1928         hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1929
1930         return hists_to_evsel(hists) == hde->evsel;
1931 }
1932
1933 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1934                              struct hist_entry *he)
1935 {
1936         struct hpp_dynamic_entry *hde;
1937         size_t len = fmt->user_len;
1938         char *str, *pos;
1939         struct format_field *field;
1940         size_t namelen;
1941         bool last = false;
1942         int ret;
1943
1944         hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1945
1946         if (!len)
1947                 len = hde_width(hde);
1948
1949         if (hde->raw_trace)
1950                 goto raw_field;
1951
1952         if (!he->trace_output)
1953                 he->trace_output = get_trace_output(he);
1954
1955         field = hde->field;
1956         namelen = strlen(field->name);
1957         str = he->trace_output;
1958
1959         while (str) {
1960                 pos = strchr(str, ' ');
1961                 if (pos == NULL) {
1962                         last = true;
1963                         pos = str + strlen(str);
1964                 }
1965
1966                 if (!strncmp(str, field->name, namelen)) {
1967                         str += namelen + 1;
1968                         str = strndup(str, pos - str);
1969
1970                         if (str == NULL)
1971                                 return scnprintf(hpp->buf, hpp->size,
1972                                                  "%*.*s", len, len, "ERROR");
1973                         break;
1974                 }
1975
1976                 if (last)
1977                         str = NULL;
1978                 else
1979                         str = pos + 1;
1980         }
1981
1982         if (str == NULL) {
1983                 struct trace_seq seq;
1984 raw_field:
1985                 trace_seq_init(&seq);
1986                 pevent_print_field(&seq, he->raw_data, hde->field);
1987                 str = seq.buffer;
1988         }
1989
1990         ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1991         free(str);
1992         return ret;
1993 }
1994
1995 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1996                                struct hist_entry *a, struct hist_entry *b)
1997 {
1998         struct hpp_dynamic_entry *hde;
1999         struct format_field *field;
2000         unsigned offset, size;
2001
2002         hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2003
2004         if (b == NULL) {
2005                 update_dynamic_len(hde, a);
2006                 return 0;
2007         }
2008
2009         field = hde->field;
2010         if (field->flags & FIELD_IS_DYNAMIC) {
2011                 unsigned long long dyn;
2012
2013                 pevent_read_number_field(field, a->raw_data, &dyn);
2014                 offset = dyn & 0xffff;
2015                 size = (dyn >> 16) & 0xffff;
2016
2017                 /* record max width for output */
2018                 if (size > hde->dynamic_len)
2019                         hde->dynamic_len = size;
2020         } else {
2021                 offset = field->offset;
2022                 size = field->size;
2023         }
2024
2025         return memcmp(a->raw_data + offset, b->raw_data + offset, size);
2026 }
2027
2028 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
2029 {
2030         return fmt->cmp == __sort__hde_cmp;
2031 }
2032
2033 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2034 {
2035         struct hpp_dynamic_entry *hde_a;
2036         struct hpp_dynamic_entry *hde_b;
2037
2038         if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
2039                 return false;
2040
2041         hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
2042         hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
2043
2044         return hde_a->field == hde_b->field;
2045 }
2046
2047 static void hde_free(struct perf_hpp_fmt *fmt)
2048 {
2049         struct hpp_dynamic_entry *hde;
2050
2051         hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2052         free(hde);
2053 }
2054
2055 static struct hpp_dynamic_entry *
2056 __alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
2057                       int level)
2058 {
2059         struct hpp_dynamic_entry *hde;
2060
2061         hde = malloc(sizeof(*hde));
2062         if (hde == NULL) {
2063                 pr_debug("Memory allocation failed\n");
2064                 return NULL;
2065         }
2066
2067         hde->evsel = evsel;
2068         hde->field = field;
2069         hde->dynamic_len = 0;
2070
2071         hde->hpp.name = field->name;
2072         hde->hpp.header = __sort__hde_header;
2073         hde->hpp.width  = __sort__hde_width;
2074         hde->hpp.entry  = __sort__hde_entry;
2075         hde->hpp.color  = NULL;
2076
2077         hde->hpp.cmp = __sort__hde_cmp;
2078         hde->hpp.collapse = __sort__hde_cmp;
2079         hde->hpp.sort = __sort__hde_cmp;
2080         hde->hpp.equal = __sort__hde_equal;
2081         hde->hpp.free = hde_free;
2082
2083         INIT_LIST_HEAD(&hde->hpp.list);
2084         INIT_LIST_HEAD(&hde->hpp.sort_list);
2085         hde->hpp.elide = false;
2086         hde->hpp.len = 0;
2087         hde->hpp.user_len = 0;
2088         hde->hpp.level = level;
2089
2090         return hde;
2091 }
2092
2093 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
2094 {
2095         struct perf_hpp_fmt *new_fmt = NULL;
2096
2097         if (perf_hpp__is_sort_entry(fmt)) {
2098                 struct hpp_sort_entry *hse, *new_hse;
2099
2100                 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2101                 new_hse = memdup(hse, sizeof(*hse));
2102                 if (new_hse)
2103                         new_fmt = &new_hse->hpp;
2104         } else if (perf_hpp__is_dynamic_entry(fmt)) {
2105                 struct hpp_dynamic_entry *hde, *new_hde;
2106
2107                 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2108                 new_hde = memdup(hde, sizeof(*hde));
2109                 if (new_hde)
2110                         new_fmt = &new_hde->hpp;
2111         } else {
2112                 new_fmt = memdup(fmt, sizeof(*fmt));
2113         }
2114
2115         INIT_LIST_HEAD(&new_fmt->list);
2116         INIT_LIST_HEAD(&new_fmt->sort_list);
2117
2118         return new_fmt;
2119 }
2120
2121 static int parse_field_name(char *str, char **event, char **field, char **opt)
2122 {
2123         char *event_name, *field_name, *opt_name;
2124
2125         event_name = str;
2126         field_name = strchr(str, '.');
2127
2128         if (field_name) {
2129                 *field_name++ = '\0';
2130         } else {
2131                 event_name = NULL;
2132                 field_name = str;
2133         }
2134
2135         opt_name = strchr(field_name, '/');
2136         if (opt_name)
2137                 *opt_name++ = '\0';
2138
2139         *event = event_name;
2140         *field = field_name;
2141         *opt   = opt_name;
2142
2143         return 0;
2144 }
2145
2146 /* find match evsel using a given event name.  The event name can be:
2147  *   1. '%' + event index (e.g. '%1' for first event)
2148  *   2. full event name (e.g. sched:sched_switch)
2149  *   3. partial event name (should not contain ':')
2150  */
2151 static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
2152 {
2153         struct perf_evsel *evsel = NULL;
2154         struct perf_evsel *pos;
2155         bool full_name;
2156
2157         /* case 1 */
2158         if (event_name[0] == '%') {
2159                 int nr = strtol(event_name+1, NULL, 0);
2160
2161                 if (nr > evlist->nr_entries)
2162                         return NULL;
2163
2164                 evsel = perf_evlist__first(evlist);
2165                 while (--nr > 0)
2166                         evsel = perf_evsel__next(evsel);
2167
2168                 return evsel;
2169         }
2170
2171         full_name = !!strchr(event_name, ':');
2172         evlist__for_each_entry(evlist, pos) {
2173                 /* case 2 */
2174                 if (full_name && !strcmp(pos->name, event_name))
2175                         return pos;
2176                 /* case 3 */
2177                 if (!full_name && strstr(pos->name, event_name)) {
2178                         if (evsel) {
2179                                 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2180                                          event_name, evsel->name, pos->name);
2181                                 return NULL;
2182                         }
2183                         evsel = pos;
2184                 }
2185         }
2186
2187         return evsel;
2188 }
2189
2190 static int __dynamic_dimension__add(struct perf_evsel *evsel,
2191                                     struct format_field *field,
2192                                     bool raw_trace, int level)
2193 {
2194         struct hpp_dynamic_entry *hde;
2195
2196         hde = __alloc_dynamic_entry(evsel, field, level);
2197         if (hde == NULL)
2198                 return -ENOMEM;
2199
2200         hde->raw_trace = raw_trace;
2201
2202         perf_hpp__register_sort_field(&hde->hpp);
2203         return 0;
2204 }
2205
2206 static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
2207 {
2208         int ret;
2209         struct format_field *field;
2210
2211         field = evsel->tp_format->format.fields;
2212         while (field) {
2213                 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2214                 if (ret < 0)
2215                         return ret;
2216
2217                 field = field->next;
2218         }
2219         return 0;
2220 }
2221
2222 static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2223                                   int level)
2224 {
2225         int ret;
2226         struct perf_evsel *evsel;
2227
2228         evlist__for_each_entry(evlist, evsel) {
2229                 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2230                         continue;
2231
2232                 ret = add_evsel_fields(evsel, raw_trace, level);
2233                 if (ret < 0)
2234                         return ret;
2235         }
2236         return 0;
2237 }
2238
2239 static int add_all_matching_fields(struct perf_evlist *evlist,
2240                                    char *field_name, bool raw_trace, int level)
2241 {
2242         int ret = -ESRCH;
2243         struct perf_evsel *evsel;
2244         struct format_field *field;
2245
2246         evlist__for_each_entry(evlist, evsel) {
2247                 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2248                         continue;
2249
2250                 field = pevent_find_any_field(evsel->tp_format, field_name);
2251                 if (field == NULL)
2252                         continue;
2253
2254                 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2255                 if (ret < 0)
2256                         break;
2257         }
2258         return ret;
2259 }
2260
2261 static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2262                              int level)
2263 {
2264         char *str, *event_name, *field_name, *opt_name;
2265         struct perf_evsel *evsel;
2266         struct format_field *field;
2267         bool raw_trace = symbol_conf.raw_trace;
2268         int ret = 0;
2269
2270         if (evlist == NULL)
2271                 return -ENOENT;
2272
2273         str = strdup(tok);
2274         if (str == NULL)
2275                 return -ENOMEM;
2276
2277         if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2278                 ret = -EINVAL;
2279                 goto out;
2280         }
2281
2282         if (opt_name) {
2283                 if (strcmp(opt_name, "raw")) {
2284                         pr_debug("unsupported field option %s\n", opt_name);
2285                         ret = -EINVAL;
2286                         goto out;
2287                 }
2288                 raw_trace = true;
2289         }
2290
2291         if (!strcmp(field_name, "trace_fields")) {
2292                 ret = add_all_dynamic_fields(evlist, raw_trace, level);
2293                 goto out;
2294         }
2295
2296         if (event_name == NULL) {
2297                 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2298                 goto out;
2299         }
2300
2301         evsel = find_evsel(evlist, event_name);
2302         if (evsel == NULL) {
2303                 pr_debug("Cannot find event: %s\n", event_name);
2304                 ret = -ENOENT;
2305                 goto out;
2306         }
2307
2308         if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2309                 pr_debug("%s is not a tracepoint event\n", event_name);
2310                 ret = -EINVAL;
2311                 goto out;
2312         }
2313
2314         if (!strcmp(field_name, "*")) {
2315                 ret = add_evsel_fields(evsel, raw_trace, level);
2316         } else {
2317                 field = pevent_find_any_field(evsel->tp_format, field_name);
2318                 if (field == NULL) {
2319                         pr_debug("Cannot find event field for %s.%s\n",
2320                                  event_name, field_name);
2321                         return -ENOENT;
2322                 }
2323
2324                 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2325         }
2326
2327 out:
2328         free(str);
2329         return ret;
2330 }
2331
2332 static int __sort_dimension__add(struct sort_dimension *sd,
2333                                  struct perf_hpp_list *list,
2334                                  int level)
2335 {
2336         if (sd->taken)
2337                 return 0;
2338
2339         if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2340                 return -1;
2341
2342         if (sd->entry->se_collapse)
2343                 list->need_collapse = 1;
2344
2345         sd->taken = 1;
2346
2347         return 0;
2348 }
2349
2350 static int __hpp_dimension__add(struct hpp_dimension *hd,
2351                                 struct perf_hpp_list *list,
2352                                 int level)
2353 {
2354         struct perf_hpp_fmt *fmt;
2355
2356         if (hd->taken)
2357                 return 0;
2358
2359         fmt = __hpp_dimension__alloc_hpp(hd, level);
2360         if (!fmt)
2361                 return -1;
2362
2363         hd->taken = 1;
2364         perf_hpp_list__register_sort_field(list, fmt);
2365         return 0;
2366 }
2367
2368 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2369                                         struct sort_dimension *sd)
2370 {
2371         if (sd->taken)
2372                 return 0;
2373
2374         if (__sort_dimension__add_hpp_output(sd, list) < 0)
2375                 return -1;
2376
2377         sd->taken = 1;
2378         return 0;
2379 }
2380
2381 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2382                                        struct hpp_dimension *hd)
2383 {
2384         struct perf_hpp_fmt *fmt;
2385
2386         if (hd->taken)
2387                 return 0;
2388
2389         fmt = __hpp_dimension__alloc_hpp(hd, 0);
2390         if (!fmt)
2391                 return -1;
2392
2393         hd->taken = 1;
2394         perf_hpp_list__column_register(list, fmt);
2395         return 0;
2396 }
2397
2398 int hpp_dimension__add_output(unsigned col)
2399 {
2400         BUG_ON(col >= PERF_HPP__MAX_INDEX);
2401         return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2402 }
2403
2404 int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2405                         struct perf_evlist *evlist,
2406                         int level)
2407 {
2408         unsigned int i;
2409
2410         for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2411                 struct sort_dimension *sd = &common_sort_dimensions[i];
2412
2413                 if (strncasecmp(tok, sd->name, strlen(tok)))
2414                         continue;
2415
2416                 if (sd->entry == &sort_parent) {
2417                         int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2418                         if (ret) {
2419                                 char err[BUFSIZ];
2420
2421                                 regerror(ret, &parent_regex, err, sizeof(err));
2422                                 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2423                                 return -EINVAL;
2424                         }
2425                         list->parent = 1;
2426                 } else if (sd->entry == &sort_sym) {
2427                         list->sym = 1;
2428                         /*
2429                          * perf diff displays the performance difference amongst
2430                          * two or more perf.data files. Those files could come
2431                          * from different binaries. So we should not compare
2432                          * their ips, but the name of symbol.
2433                          */
2434                         if (sort__mode == SORT_MODE__DIFF)
2435                                 sd->entry->se_collapse = sort__sym_sort;
2436
2437                 } else if (sd->entry == &sort_dso) {
2438                         list->dso = 1;
2439                 } else if (sd->entry == &sort_socket) {
2440                         list->socket = 1;
2441                 } else if (sd->entry == &sort_thread) {
2442                         list->thread = 1;
2443                 } else if (sd->entry == &sort_comm) {
2444                         list->comm = 1;
2445                 }
2446
2447                 return __sort_dimension__add(sd, list, level);
2448         }
2449
2450         for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2451                 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2452
2453                 if (strncasecmp(tok, hd->name, strlen(tok)))
2454                         continue;
2455
2456                 return __hpp_dimension__add(hd, list, level);
2457         }
2458
2459         for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2460                 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2461
2462                 if (strncasecmp(tok, sd->name, strlen(tok)))
2463                         continue;
2464
2465                 if (sort__mode != SORT_MODE__BRANCH)
2466                         return -EINVAL;
2467
2468                 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2469                         list->sym = 1;
2470
2471                 __sort_dimension__add(sd, list, level);
2472                 return 0;
2473         }
2474
2475         for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2476                 struct sort_dimension *sd = &memory_sort_dimensions[i];
2477
2478                 if (strncasecmp(tok, sd->name, strlen(tok)))
2479                         continue;
2480
2481                 if (sort__mode != SORT_MODE__MEMORY)
2482                         return -EINVAL;
2483
2484                 if (sd->entry == &sort_mem_dcacheline && cacheline_size == 0)
2485                         return -EINVAL;
2486
2487                 if (sd->entry == &sort_mem_daddr_sym)
2488                         list->sym = 1;
2489
2490                 __sort_dimension__add(sd, list, level);
2491                 return 0;
2492         }
2493
2494         if (!add_dynamic_entry(evlist, tok, level))
2495                 return 0;
2496
2497         return -ESRCH;
2498 }
2499
2500 static int setup_sort_list(struct perf_hpp_list *list, char *str,
2501                            struct perf_evlist *evlist)
2502 {
2503         char *tmp, *tok;
2504         int ret = 0;
2505         int level = 0;
2506         int next_level = 1;
2507         bool in_group = false;
2508
2509         do {
2510                 tok = str;
2511                 tmp = strpbrk(str, "{}, ");
2512                 if (tmp) {
2513                         if (in_group)
2514                                 next_level = level;
2515                         else
2516                                 next_level = level + 1;
2517
2518                         if (*tmp == '{')
2519                                 in_group = true;
2520                         else if (*tmp == '}')
2521                                 in_group = false;
2522
2523                         *tmp = '\0';
2524                         str = tmp + 1;
2525                 }
2526
2527                 if (*tok) {
2528                         ret = sort_dimension__add(list, tok, evlist, level);
2529                         if (ret == -EINVAL) {
2530                                 if (!cacheline_size && !strncasecmp(tok, "dcacheline", strlen(tok)))
2531                                         error("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2532                                 else
2533                                         error("Invalid --sort key: `%s'", tok);
2534                                 break;
2535                         } else if (ret == -ESRCH) {
2536                                 error("Unknown --sort key: `%s'", tok);
2537                                 break;
2538                         }
2539                 }
2540
2541                 level = next_level;
2542         } while (tmp);
2543
2544         return ret;
2545 }
2546
2547 static const char *get_default_sort_order(struct perf_evlist *evlist)
2548 {
2549         const char *default_sort_orders[] = {
2550                 default_sort_order,
2551                 default_branch_sort_order,
2552                 default_mem_sort_order,
2553                 default_top_sort_order,
2554                 default_diff_sort_order,
2555                 default_tracepoint_sort_order,
2556         };
2557         bool use_trace = true;
2558         struct perf_evsel *evsel;
2559
2560         BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2561
2562         if (evlist == NULL)
2563                 goto out_no_evlist;
2564
2565         evlist__for_each_entry(evlist, evsel) {
2566                 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2567                         use_trace = false;
2568                         break;
2569                 }
2570         }
2571
2572         if (use_trace) {
2573                 sort__mode = SORT_MODE__TRACEPOINT;
2574                 if (symbol_conf.raw_trace)
2575                         return "trace_fields";
2576         }
2577 out_no_evlist:
2578         return default_sort_orders[sort__mode];
2579 }
2580
2581 static int setup_sort_order(struct perf_evlist *evlist)
2582 {
2583         char *new_sort_order;
2584
2585         /*
2586          * Append '+'-prefixed sort order to the default sort
2587          * order string.
2588          */
2589         if (!sort_order || is_strict_order(sort_order))
2590                 return 0;
2591
2592         if (sort_order[1] == '\0') {
2593                 error("Invalid --sort key: `+'");
2594                 return -EINVAL;
2595         }
2596
2597         /*
2598          * We allocate new sort_order string, but we never free it,
2599          * because it's checked over the rest of the code.
2600          */
2601         if (asprintf(&new_sort_order, "%s,%s",
2602                      get_default_sort_order(evlist), sort_order + 1) < 0) {
2603                 error("Not enough memory to set up --sort");
2604                 return -ENOMEM;
2605         }
2606
2607         sort_order = new_sort_order;
2608         return 0;
2609 }
2610
2611 /*
2612  * Adds 'pre,' prefix into 'str' is 'pre' is
2613  * not already part of 'str'.
2614  */
2615 static char *prefix_if_not_in(const char *pre, char *str)
2616 {
2617         char *n;
2618
2619         if (!str || strstr(str, pre))
2620                 return str;
2621
2622         if (asprintf(&n, "%s,%s", pre, str) < 0)
2623                 return NULL;
2624
2625         free(str);
2626         return n;
2627 }
2628
2629 static char *setup_overhead(char *keys)
2630 {
2631         if (sort__mode == SORT_MODE__DIFF)
2632                 return keys;
2633
2634         keys = prefix_if_not_in("overhead", keys);
2635
2636         if (symbol_conf.cumulate_callchain)
2637                 keys = prefix_if_not_in("overhead_children", keys);
2638
2639         return keys;
2640 }
2641
2642 static int __setup_sorting(struct perf_evlist *evlist)
2643 {
2644         char *str;
2645         const char *sort_keys;
2646         int ret = 0;
2647
2648         ret = setup_sort_order(evlist);
2649         if (ret)
2650                 return ret;
2651
2652         sort_keys = sort_order;
2653         if (sort_keys == NULL) {
2654                 if (is_strict_order(field_order)) {
2655                         /*
2656                          * If user specified field order but no sort order,
2657                          * we'll honor it and not add default sort orders.
2658                          */
2659                         return 0;
2660                 }
2661
2662                 sort_keys = get_default_sort_order(evlist);
2663         }
2664
2665         str = strdup(sort_keys);
2666         if (str == NULL) {
2667                 error("Not enough memory to setup sort keys");
2668                 return -ENOMEM;
2669         }
2670
2671         /*
2672          * Prepend overhead fields for backward compatibility.
2673          */
2674         if (!is_strict_order(field_order)) {
2675                 str = setup_overhead(str);
2676                 if (str == NULL) {
2677                         error("Not enough memory to setup overhead keys");
2678                         return -ENOMEM;
2679                 }
2680         }
2681
2682         ret = setup_sort_list(&perf_hpp_list, str, evlist);
2683
2684         free(str);
2685         return ret;
2686 }
2687
2688 void perf_hpp__set_elide(int idx, bool elide)
2689 {
2690         struct perf_hpp_fmt *fmt;
2691         struct hpp_sort_entry *hse;
2692
2693         perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2694                 if (!perf_hpp__is_sort_entry(fmt))
2695                         continue;
2696
2697                 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2698                 if (hse->se->se_width_idx == idx) {
2699                         fmt->elide = elide;
2700                         break;
2701                 }
2702         }
2703 }
2704
2705 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
2706 {
2707         if (list && strlist__nr_entries(list) == 1) {
2708                 if (fp != NULL)
2709                         fprintf(fp, "# %s: %s\n", list_name,
2710                                 strlist__entry(list, 0)->s);
2711                 return true;
2712         }
2713         return false;
2714 }
2715
2716 static bool get_elide(int idx, FILE *output)
2717 {
2718         switch (idx) {
2719         case HISTC_SYMBOL:
2720                 return __get_elide(symbol_conf.sym_list, "symbol", output);
2721         case HISTC_DSO:
2722                 return __get_elide(symbol_conf.dso_list, "dso", output);
2723         case HISTC_COMM:
2724                 return __get_elide(symbol_conf.comm_list, "comm", output);
2725         default:
2726                 break;
2727         }
2728
2729         if (sort__mode != SORT_MODE__BRANCH)
2730                 return false;
2731
2732         switch (idx) {
2733         case HISTC_SYMBOL_FROM:
2734                 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2735         case HISTC_SYMBOL_TO:
2736                 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2737         case HISTC_DSO_FROM:
2738                 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2739         case HISTC_DSO_TO:
2740                 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2741         default:
2742                 break;
2743         }
2744
2745         return false;
2746 }
2747
2748 void sort__setup_elide(FILE *output)
2749 {
2750         struct perf_hpp_fmt *fmt;
2751         struct hpp_sort_entry *hse;
2752
2753         perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2754                 if (!perf_hpp__is_sort_entry(fmt))
2755                         continue;
2756
2757                 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2758                 fmt->elide = get_elide(hse->se->se_width_idx, output);
2759         }
2760
2761         /*
2762          * It makes no sense to elide all of sort entries.
2763          * Just revert them to show up again.
2764          */
2765         perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2766                 if (!perf_hpp__is_sort_entry(fmt))
2767                         continue;
2768
2769                 if (!fmt->elide)
2770                         return;
2771         }
2772
2773         perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2774                 if (!perf_hpp__is_sort_entry(fmt))
2775                         continue;
2776
2777                 fmt->elide = false;
2778         }
2779 }
2780
2781 int output_field_add(struct perf_hpp_list *list, char *tok)
2782 {
2783         unsigned int i;
2784
2785         for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2786                 struct sort_dimension *sd = &common_sort_dimensions[i];
2787
2788                 if (strncasecmp(tok, sd->name, strlen(tok)))
2789                         continue;
2790
2791                 return __sort_dimension__add_output(list, sd);
2792         }
2793
2794         for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2795                 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2796
2797                 if (strncasecmp(tok, hd->name, strlen(tok)))
2798                         continue;
2799
2800                 return __hpp_dimension__add_output(list, hd);
2801         }
2802
2803         for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2804                 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2805
2806                 if (strncasecmp(tok, sd->name, strlen(tok)))
2807                         continue;
2808
2809                 return __sort_dimension__add_output(list, sd);
2810         }
2811
2812         for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2813                 struct sort_dimension *sd = &memory_sort_dimensions[i];
2814
2815                 if (strncasecmp(tok, sd->name, strlen(tok)))
2816                         continue;
2817
2818                 return __sort_dimension__add_output(list, sd);
2819         }
2820
2821         return -ESRCH;
2822 }
2823
2824 static int setup_output_list(struct perf_hpp_list *list, char *str)
2825 {
2826         char *tmp, *tok;
2827         int ret = 0;
2828
2829         for (tok = strtok_r(str, ", ", &tmp);
2830                         tok; tok = strtok_r(NULL, ", ", &tmp)) {
2831                 ret = output_field_add(list, tok);
2832                 if (ret == -EINVAL) {
2833                         error("Invalid --fields key: `%s'", tok);
2834                         break;
2835                 } else if (ret == -ESRCH) {
2836                         error("Unknown --fields key: `%s'", tok);
2837                         break;
2838                 }
2839         }
2840
2841         return ret;
2842 }
2843
2844 void reset_dimensions(void)
2845 {
2846         unsigned int i;
2847
2848         for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2849                 common_sort_dimensions[i].taken = 0;
2850
2851         for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2852                 hpp_sort_dimensions[i].taken = 0;
2853
2854         for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2855                 bstack_sort_dimensions[i].taken = 0;
2856
2857         for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2858                 memory_sort_dimensions[i].taken = 0;
2859 }
2860
2861 bool is_strict_order(const char *order)
2862 {
2863         return order && (*order != '+');
2864 }
2865
2866 static int __setup_output_field(void)
2867 {
2868         char *str, *strp;
2869         int ret = -EINVAL;
2870
2871         if (field_order == NULL)
2872                 return 0;
2873
2874         strp = str = strdup(field_order);
2875         if (str == NULL) {
2876                 error("Not enough memory to setup output fields");
2877                 return -ENOMEM;
2878         }
2879
2880         if (!is_strict_order(field_order))
2881                 strp++;
2882
2883         if (!strlen(strp)) {
2884                 error("Invalid --fields key: `+'");
2885                 goto out;
2886         }
2887
2888         ret = setup_output_list(&perf_hpp_list, strp);
2889
2890 out:
2891         free(str);
2892         return ret;
2893 }
2894
2895 int setup_sorting(struct perf_evlist *evlist)
2896 {
2897         int err;
2898
2899         err = __setup_sorting(evlist);
2900         if (err < 0)
2901                 return err;
2902
2903         if (parent_pattern != default_parent_pattern) {
2904                 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
2905                 if (err < 0)
2906                         return err;
2907         }
2908
2909         reset_dimensions();
2910
2911         /*
2912          * perf diff doesn't use default hpp output fields.
2913          */
2914         if (sort__mode != SORT_MODE__DIFF)
2915                 perf_hpp__init();
2916
2917         err = __setup_output_field();
2918         if (err < 0)
2919                 return err;
2920
2921         /* copy sort keys to output fields */
2922         perf_hpp__setup_output_field(&perf_hpp_list);
2923         /* and then copy output fields to sort keys */
2924         perf_hpp__append_sort_keys(&perf_hpp_list);
2925
2926         /* setup hists-specific output fields */
2927         if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2928                 return -1;
2929
2930         return 0;
2931 }
2932
2933 void reset_output_field(void)
2934 {
2935         perf_hpp_list.need_collapse = 0;
2936         perf_hpp_list.parent = 0;
2937         perf_hpp_list.sym = 0;
2938         perf_hpp_list.dso = 0;
2939
2940         field_order = NULL;
2941         sort_order = NULL;
2942
2943         reset_dimensions();
2944         perf_hpp__reset_output_field(&perf_hpp_list);
2945 }