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