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