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