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