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