ce5d40456c07632f16a0e5b582c7aea0c941176e
[cascardo/linux.git] / tools / perf / util / sort.c
1 #include "sort.h"
2 #include "hist.h"
3
4 regex_t         parent_regex;
5 const char      default_parent_pattern[] = "^sys_|^do_page_fault";
6 const char      *parent_pattern = default_parent_pattern;
7 const char      default_sort_order[] = "comm,dso,symbol";
8 const char      *sort_order = default_sort_order;
9 int             sort__need_collapse = 0;
10 int             sort__has_parent = 0;
11 int             sort__has_sym = 0;
12 int             sort__branch_mode = -1; /* -1 = means not set */
13
14 enum sort_type  sort__first_dimension;
15
16 LIST_HEAD(hist_entry__sort_list);
17
18 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
19 {
20         int n;
21         va_list ap;
22
23         va_start(ap, fmt);
24         n = vsnprintf(bf, size, fmt, ap);
25         if (symbol_conf.field_sep && n > 0) {
26                 char *sep = bf;
27
28                 while (1) {
29                         sep = strchr(sep, *symbol_conf.field_sep);
30                         if (sep == NULL)
31                                 break;
32                         *sep = '.';
33                 }
34         }
35         va_end(ap);
36
37         if (n >= (int)size)
38                 return size - 1;
39         return n;
40 }
41
42 static int64_t cmp_null(void *l, void *r)
43 {
44         if (!l && !r)
45                 return 0;
46         else if (!l)
47                 return -1;
48         else
49                 return 1;
50 }
51
52 /* --sort pid */
53
54 static int64_t
55 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
56 {
57         return right->thread->pid - left->thread->pid;
58 }
59
60 static int hist_entry__thread_snprintf(struct hist_entry *self, char *bf,
61                                        size_t size, unsigned int width)
62 {
63         return repsep_snprintf(bf, size, "%*s:%5d", width,
64                               self->thread->comm ?: "", self->thread->pid);
65 }
66
67 struct sort_entry sort_thread = {
68         .se_header      = "Command:  Pid",
69         .se_cmp         = sort__thread_cmp,
70         .se_snprintf    = hist_entry__thread_snprintf,
71         .se_width_idx   = HISTC_THREAD,
72 };
73
74 /* --sort comm */
75
76 static int64_t
77 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
78 {
79         return right->thread->pid - left->thread->pid;
80 }
81
82 static int64_t
83 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
84 {
85         char *comm_l = left->thread->comm;
86         char *comm_r = right->thread->comm;
87
88         if (!comm_l || !comm_r)
89                 return cmp_null(comm_l, comm_r);
90
91         return strcmp(comm_l, comm_r);
92 }
93
94 static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf,
95                                      size_t size, unsigned int width)
96 {
97         return repsep_snprintf(bf, size, "%*s", width, self->thread->comm);
98 }
99
100 struct sort_entry sort_comm = {
101         .se_header      = "Command",
102         .se_cmp         = sort__comm_cmp,
103         .se_collapse    = sort__comm_collapse,
104         .se_snprintf    = hist_entry__comm_snprintf,
105         .se_width_idx   = HISTC_COMM,
106 };
107
108 /* --sort dso */
109
110 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
111 {
112         struct dso *dso_l = map_l ? map_l->dso : NULL;
113         struct dso *dso_r = map_r ? map_r->dso : NULL;
114         const char *dso_name_l, *dso_name_r;
115
116         if (!dso_l || !dso_r)
117                 return cmp_null(dso_l, dso_r);
118
119         if (verbose) {
120                 dso_name_l = dso_l->long_name;
121                 dso_name_r = dso_r->long_name;
122         } else {
123                 dso_name_l = dso_l->short_name;
124                 dso_name_r = dso_r->short_name;
125         }
126
127         return strcmp(dso_name_l, dso_name_r);
128 }
129
130 static int64_t
131 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
132 {
133         return _sort__dso_cmp(left->ms.map, right->ms.map);
134 }
135
136 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
137                                      size_t size, unsigned int width)
138 {
139         if (map && map->dso) {
140                 const char *dso_name = !verbose ? map->dso->short_name :
141                         map->dso->long_name;
142                 return repsep_snprintf(bf, size, "%-*s", width, dso_name);
143         }
144
145         return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
146 }
147
148 static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
149                                     size_t size, unsigned int width)
150 {
151         return _hist_entry__dso_snprintf(self->ms.map, bf, size, width);
152 }
153
154 struct sort_entry sort_dso = {
155         .se_header      = "Shared Object",
156         .se_cmp         = sort__dso_cmp,
157         .se_snprintf    = hist_entry__dso_snprintf,
158         .se_width_idx   = HISTC_DSO,
159 };
160
161 /* --sort symbol */
162
163 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r,
164                               u64 ip_l, u64 ip_r)
165 {
166         if (!sym_l || !sym_r)
167                 return cmp_null(sym_l, sym_r);
168
169         if (sym_l == sym_r)
170                 return 0;
171
172         ip_l = sym_l->start;
173         ip_r = sym_r->start;
174
175         return (int64_t)(ip_r - ip_l);
176 }
177
178 static int64_t
179 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
180 {
181         u64 ip_l, ip_r;
182
183         if (!left->ms.sym && !right->ms.sym)
184                 return right->level - left->level;
185
186         if (!left->ms.sym || !right->ms.sym)
187                 return cmp_null(left->ms.sym, right->ms.sym);
188
189         if (left->ms.sym == right->ms.sym)
190                 return 0;
191
192         ip_l = left->ms.sym->start;
193         ip_r = right->ms.sym->start;
194
195         return _sort__sym_cmp(left->ms.sym, right->ms.sym, ip_l, ip_r);
196 }
197
198 static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
199                                      u64 ip, char level, char *bf, size_t size,
200                                      unsigned int width __maybe_unused)
201 {
202         size_t ret = 0;
203
204         if (verbose) {
205                 char o = map ? dso__symtab_origin(map->dso) : '!';
206                 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
207                                        BITS_PER_LONG / 4, ip, o);
208         }
209
210         ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
211         if (sym)
212                 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
213                                        width - ret,
214                                        sym->name);
215         else {
216                 size_t len = BITS_PER_LONG / 4;
217                 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
218                                        len, ip);
219                 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
220                                        width - ret, "");
221         }
222
223         return ret;
224 }
225
226 static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
227                                     size_t size,
228                                     unsigned int width __maybe_unused)
229 {
230         return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
231                                          self->level, bf, size, width);
232 }
233
234 struct sort_entry sort_sym = {
235         .se_header      = "Symbol",
236         .se_cmp         = sort__sym_cmp,
237         .se_snprintf    = hist_entry__sym_snprintf,
238         .se_width_idx   = HISTC_SYMBOL,
239 };
240
241 /* --sort srcline */
242
243 static int64_t
244 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
245 {
246         return (int64_t)(right->ip - left->ip);
247 }
248
249 static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
250                                         size_t size,
251                                         unsigned int width __maybe_unused)
252 {
253         FILE *fp;
254         char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
255         size_t line_len;
256
257         if (path != NULL)
258                 goto out_path;
259
260         if (!self->ms.map)
261                 goto out_ip;
262
263         if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
264                 goto out_ip;
265
266         snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
267                  self->ms.map->dso->long_name, self->ip);
268         fp = popen(cmd, "r");
269         if (!fp)
270                 goto out_ip;
271
272         if (getline(&path, &line_len, fp) < 0 || !line_len)
273                 goto out_ip;
274         fclose(fp);
275         self->srcline = strdup(path);
276         if (self->srcline == NULL)
277                 goto out_ip;
278
279         nl = strchr(self->srcline, '\n');
280         if (nl != NULL)
281                 *nl = '\0';
282         path = self->srcline;
283 out_path:
284         return repsep_snprintf(bf, size, "%s", path);
285 out_ip:
286         return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
287 }
288
289 struct sort_entry sort_srcline = {
290         .se_header      = "Source:Line",
291         .se_cmp         = sort__srcline_cmp,
292         .se_snprintf    = hist_entry__srcline_snprintf,
293         .se_width_idx   = HISTC_SRCLINE,
294 };
295
296 /* --sort parent */
297
298 static int64_t
299 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
300 {
301         struct symbol *sym_l = left->parent;
302         struct symbol *sym_r = right->parent;
303
304         if (!sym_l || !sym_r)
305                 return cmp_null(sym_l, sym_r);
306
307         return strcmp(sym_l->name, sym_r->name);
308 }
309
310 static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
311                                        size_t size, unsigned int width)
312 {
313         return repsep_snprintf(bf, size, "%-*s", width,
314                               self->parent ? self->parent->name : "[other]");
315 }
316
317 struct sort_entry sort_parent = {
318         .se_header      = "Parent symbol",
319         .se_cmp         = sort__parent_cmp,
320         .se_snprintf    = hist_entry__parent_snprintf,
321         .se_width_idx   = HISTC_PARENT,
322 };
323
324 /* --sort cpu */
325
326 static int64_t
327 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
328 {
329         return right->cpu - left->cpu;
330 }
331
332 static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
333                                        size_t size, unsigned int width)
334 {
335         return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
336 }
337
338 struct sort_entry sort_cpu = {
339         .se_header      = "CPU",
340         .se_cmp         = sort__cpu_cmp,
341         .se_snprintf    = hist_entry__cpu_snprintf,
342         .se_width_idx   = HISTC_CPU,
343 };
344
345 /* sort keys for branch stacks */
346
347 static int64_t
348 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
349 {
350         return _sort__dso_cmp(left->branch_info->from.map,
351                               right->branch_info->from.map);
352 }
353
354 static int hist_entry__dso_from_snprintf(struct hist_entry *self, char *bf,
355                                     size_t size, unsigned int width)
356 {
357         return _hist_entry__dso_snprintf(self->branch_info->from.map,
358                                          bf, size, width);
359 }
360
361 static int64_t
362 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
363 {
364         return _sort__dso_cmp(left->branch_info->to.map,
365                               right->branch_info->to.map);
366 }
367
368 static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf,
369                                        size_t size, unsigned int width)
370 {
371         return _hist_entry__dso_snprintf(self->branch_info->to.map,
372                                          bf, size, width);
373 }
374
375 static int64_t
376 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
377 {
378         struct addr_map_symbol *from_l = &left->branch_info->from;
379         struct addr_map_symbol *from_r = &right->branch_info->from;
380
381         if (!from_l->sym && !from_r->sym)
382                 return right->level - left->level;
383
384         return _sort__sym_cmp(from_l->sym, from_r->sym, from_l->addr,
385                              from_r->addr);
386 }
387
388 static int64_t
389 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
390 {
391         struct addr_map_symbol *to_l = &left->branch_info->to;
392         struct addr_map_symbol *to_r = &right->branch_info->to;
393
394         if (!to_l->sym && !to_r->sym)
395                 return right->level - left->level;
396
397         return _sort__sym_cmp(to_l->sym, to_r->sym, to_l->addr, to_r->addr);
398 }
399
400 static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
401                                         size_t size,
402                                         unsigned int width __maybe_unused)
403 {
404         struct addr_map_symbol *from = &self->branch_info->from;
405         return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
406                                          self->level, bf, size, width);
407
408 }
409
410 static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
411                                        size_t size,
412                                        unsigned int width __maybe_unused)
413 {
414         struct addr_map_symbol *to = &self->branch_info->to;
415         return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
416                                          self->level, bf, size, width);
417
418 }
419
420 struct sort_entry sort_dso_from = {
421         .se_header      = "Source Shared Object",
422         .se_cmp         = sort__dso_from_cmp,
423         .se_snprintf    = hist_entry__dso_from_snprintf,
424         .se_width_idx   = HISTC_DSO_FROM,
425 };
426
427 struct sort_entry sort_dso_to = {
428         .se_header      = "Target Shared Object",
429         .se_cmp         = sort__dso_to_cmp,
430         .se_snprintf    = hist_entry__dso_to_snprintf,
431         .se_width_idx   = HISTC_DSO_TO,
432 };
433
434 struct sort_entry sort_sym_from = {
435         .se_header      = "Source Symbol",
436         .se_cmp         = sort__sym_from_cmp,
437         .se_snprintf    = hist_entry__sym_from_snprintf,
438         .se_width_idx   = HISTC_SYMBOL_FROM,
439 };
440
441 struct sort_entry sort_sym_to = {
442         .se_header      = "Target Symbol",
443         .se_cmp         = sort__sym_to_cmp,
444         .se_snprintf    = hist_entry__sym_to_snprintf,
445         .se_width_idx   = HISTC_SYMBOL_TO,
446 };
447
448 static int64_t
449 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
450 {
451         const unsigned char mp = left->branch_info->flags.mispred !=
452                                         right->branch_info->flags.mispred;
453         const unsigned char p = left->branch_info->flags.predicted !=
454                                         right->branch_info->flags.predicted;
455
456         return mp || p;
457 }
458
459 static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
460                                     size_t size, unsigned int width){
461         static const char *out = "N/A";
462
463         if (self->branch_info->flags.predicted)
464                 out = "N";
465         else if (self->branch_info->flags.mispred)
466                 out = "Y";
467
468         return repsep_snprintf(bf, size, "%-*s", width, out);
469 }
470
471 struct sort_entry sort_mispredict = {
472         .se_header      = "Branch Mispredicted",
473         .se_cmp         = sort__mispredict_cmp,
474         .se_snprintf    = hist_entry__mispredict_snprintf,
475         .se_width_idx   = HISTC_MISPREDICT,
476 };
477
478 struct sort_dimension {
479         const char              *name;
480         struct sort_entry       *entry;
481         int                     taken;
482 };
483
484 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
485
486 static struct sort_dimension sort_dimensions[] = {
487         DIM(SORT_PID, "pid", sort_thread),
488         DIM(SORT_COMM, "comm", sort_comm),
489         DIM(SORT_DSO, "dso", sort_dso),
490         DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
491         DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
492         DIM(SORT_SYM, "symbol", sort_sym),
493         DIM(SORT_SYM_TO, "symbol_from", sort_sym_from),
494         DIM(SORT_SYM_FROM, "symbol_to", sort_sym_to),
495         DIM(SORT_PARENT, "parent", sort_parent),
496         DIM(SORT_CPU, "cpu", sort_cpu),
497         DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
498         DIM(SORT_SRCLINE, "srcline", sort_srcline),
499 };
500
501 int sort_dimension__add(const char *tok)
502 {
503         unsigned int i;
504
505         for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
506                 struct sort_dimension *sd = &sort_dimensions[i];
507
508                 if (strncasecmp(tok, sd->name, strlen(tok)))
509                         continue;
510                 if (sd->entry == &sort_parent) {
511                         int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
512                         if (ret) {
513                                 char err[BUFSIZ];
514
515                                 regerror(ret, &parent_regex, err, sizeof(err));
516                                 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
517                                 return -EINVAL;
518                         }
519                         sort__has_parent = 1;
520                 } else if (sd->entry == &sort_sym ||
521                            sd->entry == &sort_sym_from ||
522                            sd->entry == &sort_sym_to) {
523                         sort__has_sym = 1;
524                 }
525
526                 if (sd->taken)
527                         return 0;
528
529                 if (sd->entry->se_collapse)
530                         sort__need_collapse = 1;
531
532                 if (list_empty(&hist_entry__sort_list)) {
533                         if (!strcmp(sd->name, "pid"))
534                                 sort__first_dimension = SORT_PID;
535                         else if (!strcmp(sd->name, "comm"))
536                                 sort__first_dimension = SORT_COMM;
537                         else if (!strcmp(sd->name, "dso"))
538                                 sort__first_dimension = SORT_DSO;
539                         else if (!strcmp(sd->name, "symbol"))
540                                 sort__first_dimension = SORT_SYM;
541                         else if (!strcmp(sd->name, "parent"))
542                                 sort__first_dimension = SORT_PARENT;
543                         else if (!strcmp(sd->name, "cpu"))
544                                 sort__first_dimension = SORT_CPU;
545                         else if (!strcmp(sd->name, "symbol_from"))
546                                 sort__first_dimension = SORT_SYM_FROM;
547                         else if (!strcmp(sd->name, "symbol_to"))
548                                 sort__first_dimension = SORT_SYM_TO;
549                         else if (!strcmp(sd->name, "dso_from"))
550                                 sort__first_dimension = SORT_DSO_FROM;
551                         else if (!strcmp(sd->name, "dso_to"))
552                                 sort__first_dimension = SORT_DSO_TO;
553                         else if (!strcmp(sd->name, "mispredict"))
554                                 sort__first_dimension = SORT_MISPREDICT;
555                 }
556
557                 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
558                 sd->taken = 1;
559
560                 return 0;
561         }
562         return -ESRCH;
563 }
564
565 void setup_sorting(const char * const usagestr[], const struct option *opts)
566 {
567         char *tmp, *tok, *str = strdup(sort_order);
568
569         for (tok = strtok_r(str, ", ", &tmp);
570                         tok; tok = strtok_r(NULL, ", ", &tmp)) {
571                 if (sort_dimension__add(tok) < 0) {
572                         error("Unknown --sort key: `%s'", tok);
573                         usage_with_options(usagestr, opts);
574                 }
575         }
576
577         free(str);
578 }
579
580 void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
581                              const char *list_name, FILE *fp)
582 {
583         if (list && strlist__nr_entries(list) == 1) {
584                 if (fp != NULL)
585                         fprintf(fp, "# %s: %s\n", list_name,
586                                 strlist__entry(list, 0)->s);
587                 self->elide = true;
588         }
589 }