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