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