perf: Add PERF_RECORD_MISC_MMAP_DATA to RECORD_MMAP
[linux-block.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;
1af55640 11int sort__has_sym = 0;
993ac88d 12int sort__branch_mode = -1; /* -1 = means not set */
a4fb581b
FW
13
14enum sort_type sort__first_dimension;
dd68ada2 15
dd68ada2
JK
16LIST_HEAD(hist_entry__sort_list);
17
a4e3b956 18static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
dd68ada2
JK
19{
20 int n;
21 va_list ap;
22
23 va_start(ap, fmt);
a4e3b956 24 n = vsnprintf(bf, size, fmt, ap);
0ca0c130 25 if (symbol_conf.field_sep && n > 0) {
a4e3b956
ACM
26 char *sep = bf;
27
28 while (1) {
0ca0c130 29 sep = strchr(sep, *symbol_conf.field_sep);
a4e3b956
ACM
30 if (sep == NULL)
31 break;
32 *sep = '.';
dd68ada2 33 }
dd68ada2
JK
34 }
35 va_end(ap);
b832796c
AB
36
37 if (n >= (int)size)
38 return size - 1;
dd68ada2
JK
39 return n;
40}
41
872a878f
FW
42static 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
54static int64_t
55sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
56{
57 return right->thread->pid - left->thread->pid;
58}
59
a4e3b956
ACM
60static int hist_entry__thread_snprintf(struct hist_entry *self, char *bf,
61 size_t size, unsigned int width)
dd68ada2 62{
fb29a338 63 return repsep_snprintf(bf, size, "%*s:%5d", width - 6,
dd68ada2
JK
64 self->thread->comm ?: "", self->thread->pid);
65}
66
872a878f
FW
67struct 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
76static int64_t
77sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
78{
79 return right->thread->pid - left->thread->pid;
80}
81
82static int64_t
83sort__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
a4e3b956
ACM
94static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf,
95 size_t size, unsigned int width)
dd68ada2 96{
a4e3b956 97 return repsep_snprintf(bf, size, "%*s", width, self->thread->comm);
dd68ada2
JK
98}
99
14d1ac74
NK
100struct 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
b5387528
RAV
110static 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
872a878f 130static int64_t
dd68ada2
JK
131sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
132{
b5387528
RAV
133 return _sort__dso_cmp(left->ms.map, right->ms.map);
134}
dd68ada2 135
14d1ac74
NK
136static 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
148static 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
154struct 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 */
dd68ada2 162
51f27d14 163static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
b5387528 164{
51f27d14
NK
165 u64 ip_l, ip_r;
166
b5387528
RAV
167 if (!sym_l || !sym_r)
168 return cmp_null(sym_l, sym_r);
169
170 if (sym_l == sym_r)
171 return 0;
172
53985a7b
SL
173 ip_l = sym_l->start;
174 ip_r = sym_r->start;
b5387528
RAV
175
176 return (int64_t)(ip_r - ip_l);
177}
178
14d1ac74
NK
179static int64_t
180sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
b5387528 181{
14d1ac74
NK
182 if (!left->ms.sym && !right->ms.sym)
183 return right->level - left->level;
dd68ada2 184
51f27d14 185 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
b5387528
RAV
186}
187
188static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
189 u64 ip, char level, char *bf, size_t size,
43355522 190 unsigned int width)
b5387528
RAV
191{
192 size_t ret = 0;
193
194 if (verbose) {
195 char o = map ? dso__symtab_origin(map->dso) : '!';
196 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
197 BITS_PER_LONG / 4, ip, o);
439d473b 198 }
dd68ada2 199
b5387528
RAV
200 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
201 if (sym)
202 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
203 width - ret,
204 sym->name);
205 else {
206 size_t len = BITS_PER_LONG / 4;
207 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
208 len, ip);
209 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
210 width - ret, "");
211 }
212
213 return ret;
dd68ada2
JK
214}
215
b5387528 216static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
43355522 217 size_t size, unsigned int width)
b5387528
RAV
218{
219 return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
220 self->level, bf, size, width);
221}
dd68ada2 222
872a878f
FW
223struct sort_entry sort_sym = {
224 .se_header = "Symbol",
225 .se_cmp = sort__sym_cmp,
226 .se_snprintf = hist_entry__sym_snprintf,
227 .se_width_idx = HISTC_SYMBOL,
228};
dd68ada2 229
409a8be6
ACM
230/* --sort srcline */
231
232static int64_t
233sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
234{
235 return (int64_t)(right->ip - left->ip);
236}
237
238static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
1d037ca1
IT
239 size_t size,
240 unsigned int width __maybe_unused)
409a8be6 241{
8eb44dd7 242 FILE *fp = NULL;
409a8be6
ACM
243 char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
244 size_t line_len;
245
246 if (path != NULL)
247 goto out_path;
248
ffe10c6f
NK
249 if (!self->ms.map)
250 goto out_ip;
251
88481b6b
NK
252 if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
253 goto out_ip;
254
409a8be6
ACM
255 snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
256 self->ms.map->dso->long_name, self->ip);
257 fp = popen(cmd, "r");
258 if (!fp)
259 goto out_ip;
260
261 if (getline(&path, &line_len, fp) < 0 || !line_len)
262 goto out_ip;
409a8be6
ACM
263 self->srcline = strdup(path);
264 if (self->srcline == NULL)
265 goto out_ip;
266
267 nl = strchr(self->srcline, '\n');
268 if (nl != NULL)
269 *nl = '\0';
270 path = self->srcline;
271out_path:
8eb44dd7
TJ
272 if (fp)
273 pclose(fp);
409a8be6
ACM
274 return repsep_snprintf(bf, size, "%s", path);
275out_ip:
8eb44dd7
TJ
276 if (fp)
277 pclose(fp);
409a8be6
ACM
278 return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
279}
280
281struct sort_entry sort_srcline = {
282 .se_header = "Source:Line",
283 .se_cmp = sort__srcline_cmp,
284 .se_snprintf = hist_entry__srcline_snprintf,
285 .se_width_idx = HISTC_SRCLINE,
286};
287
dd68ada2
JK
288/* --sort parent */
289
872a878f 290static int64_t
dd68ada2
JK
291sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
292{
293 struct symbol *sym_l = left->parent;
294 struct symbol *sym_r = right->parent;
295
296 if (!sym_l || !sym_r)
297 return cmp_null(sym_l, sym_r);
298
299 return strcmp(sym_l->name, sym_r->name);
300}
301
a4e3b956
ACM
302static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
303 size_t size, unsigned int width)
dd68ada2 304{
a4e3b956 305 return repsep_snprintf(bf, size, "%-*s", width,
dd68ada2
JK
306 self->parent ? self->parent->name : "[other]");
307}
308
872a878f
FW
309struct sort_entry sort_parent = {
310 .se_header = "Parent symbol",
311 .se_cmp = sort__parent_cmp,
312 .se_snprintf = hist_entry__parent_snprintf,
313 .se_width_idx = HISTC_PARENT,
314};
315
f60f3593
AS
316/* --sort cpu */
317
872a878f 318static int64_t
f60f3593
AS
319sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
320{
321 return right->cpu - left->cpu;
322}
323
324static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
325 size_t size, unsigned int width)
326{
dccf1805 327 return repsep_snprintf(bf, size, "%*d", width, self->cpu);
f60f3593
AS
328}
329
872a878f
FW
330struct sort_entry sort_cpu = {
331 .se_header = "CPU",
332 .se_cmp = sort__cpu_cmp,
333 .se_snprintf = hist_entry__cpu_snprintf,
334 .se_width_idx = HISTC_CPU,
335};
336
14d1ac74
NK
337/* sort keys for branch stacks */
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
b5387528
RAV
353static int64_t
354sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
355{
356 return _sort__dso_cmp(left->branch_info->to.map,
357 right->branch_info->to.map);
358}
359
360static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf,
361 size_t size, unsigned int width)
362{
363 return _hist_entry__dso_snprintf(self->branch_info->to.map,
364 bf, size, width);
365}
366
367static int64_t
368sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
369{
370 struct addr_map_symbol *from_l = &left->branch_info->from;
371 struct addr_map_symbol *from_r = &right->branch_info->from;
372
373 if (!from_l->sym && !from_r->sym)
374 return right->level - left->level;
375
51f27d14 376 return _sort__sym_cmp(from_l->sym, from_r->sym);
b5387528
RAV
377}
378
379static int64_t
380sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
381{
382 struct addr_map_symbol *to_l = &left->branch_info->to;
383 struct addr_map_symbol *to_r = &right->branch_info->to;
384
385 if (!to_l->sym && !to_r->sym)
386 return right->level - left->level;
387
51f27d14 388 return _sort__sym_cmp(to_l->sym, to_r->sym);
b5387528
RAV
389}
390
391static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
43355522 392 size_t size, unsigned int width)
b5387528
RAV
393{
394 struct addr_map_symbol *from = &self->branch_info->from;
395 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
396 self->level, bf, size, width);
397
398}
399
400static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
43355522 401 size_t size, unsigned int width)
b5387528
RAV
402{
403 struct addr_map_symbol *to = &self->branch_info->to;
404 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
405 self->level, bf, size, width);
406
407}
408
14d1ac74
NK
409struct sort_entry sort_dso_from = {
410 .se_header = "Source Shared Object",
411 .se_cmp = sort__dso_from_cmp,
412 .se_snprintf = hist_entry__dso_from_snprintf,
413 .se_width_idx = HISTC_DSO_FROM,
414};
415
b5387528
RAV
416struct sort_entry sort_dso_to = {
417 .se_header = "Target Shared Object",
418 .se_cmp = sort__dso_to_cmp,
419 .se_snprintf = hist_entry__dso_to_snprintf,
420 .se_width_idx = HISTC_DSO_TO,
421};
422
423struct sort_entry sort_sym_from = {
424 .se_header = "Source Symbol",
425 .se_cmp = sort__sym_from_cmp,
426 .se_snprintf = hist_entry__sym_from_snprintf,
427 .se_width_idx = HISTC_SYMBOL_FROM,
428};
429
430struct sort_entry sort_sym_to = {
431 .se_header = "Target Symbol",
432 .se_cmp = sort__sym_to_cmp,
433 .se_snprintf = hist_entry__sym_to_snprintf,
434 .se_width_idx = HISTC_SYMBOL_TO,
435};
436
437static int64_t
438sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
439{
440 const unsigned char mp = left->branch_info->flags.mispred !=
441 right->branch_info->flags.mispred;
442 const unsigned char p = left->branch_info->flags.predicted !=
443 right->branch_info->flags.predicted;
444
445 return mp || p;
446}
447
448static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
449 size_t size, unsigned int width){
450 static const char *out = "N/A";
451
452 if (self->branch_info->flags.predicted)
453 out = "N";
454 else if (self->branch_info->flags.mispred)
455 out = "Y";
456
457 return repsep_snprintf(bf, size, "%-*s", width, out);
458}
459
460struct sort_entry sort_mispredict = {
461 .se_header = "Branch Mispredicted",
462 .se_cmp = sort__mispredict_cmp,
463 .se_snprintf = hist_entry__mispredict_snprintf,
464 .se_width_idx = HISTC_MISPREDICT,
465};
466
872a878f
FW
467struct sort_dimension {
468 const char *name;
469 struct sort_entry *entry;
470 int taken;
471};
472
b5387528
RAV
473#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
474
fc5871ed 475static struct sort_dimension common_sort_dimensions[] = {
b5387528
RAV
476 DIM(SORT_PID, "pid", sort_thread),
477 DIM(SORT_COMM, "comm", sort_comm),
478 DIM(SORT_DSO, "dso", sort_dso),
b5387528 479 DIM(SORT_SYM, "symbol", sort_sym),
b5387528
RAV
480 DIM(SORT_PARENT, "parent", sort_parent),
481 DIM(SORT_CPU, "cpu", sort_cpu),
409a8be6 482 DIM(SORT_SRCLINE, "srcline", sort_srcline),
872a878f
FW
483};
484
fc5871ed
NK
485#undef DIM
486
487#define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
488
489static struct sort_dimension bstack_sort_dimensions[] = {
490 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
491 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
492 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
493 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
494 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
495};
496
497#undef DIM
498
dd68ada2
JK
499int sort_dimension__add(const char *tok)
500{
501 unsigned int i;
502
fc5871ed
NK
503 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
504 struct sort_dimension *sd = &common_sort_dimensions[i];
dd68ada2 505
dd68ada2
JK
506 if (strncasecmp(tok, sd->name, strlen(tok)))
507 continue;
fc5871ed 508
dd68ada2
JK
509 if (sd->entry == &sort_parent) {
510 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
511 if (ret) {
512 char err[BUFSIZ];
513
514 regerror(ret, &parent_regex, err, sizeof(err));
2aefa4f7
ACM
515 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
516 return -EINVAL;
dd68ada2
JK
517 }
518 sort__has_parent = 1;
fc5871ed 519 } else if (sd->entry == &sort_sym) {
1af55640 520 sort__has_sym = 1;
dd68ada2
JK
521 }
522
fd8ea212
FW
523 if (sd->taken)
524 return 0;
525
526 if (sd->entry->se_collapse)
527 sort__need_collapse = 1;
528
6f38cf25
NK
529 if (list_empty(&hist_entry__sort_list))
530 sort__first_dimension = i;
af0a6fa4 531
dd68ada2
JK
532 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
533 sd->taken = 1;
534
535 return 0;
536 }
fc5871ed
NK
537
538 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
539 struct sort_dimension *sd = &bstack_sort_dimensions[i];
540
541 if (strncasecmp(tok, sd->name, strlen(tok)))
542 continue;
543
544 if (sort__branch_mode != 1)
545 return -EINVAL;
546
547 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
548 sort__has_sym = 1;
549
550 if (sd->taken)
551 return 0;
552
553 if (sd->entry->se_collapse)
554 sort__need_collapse = 1;
555
556 if (list_empty(&hist_entry__sort_list))
557 sort__first_dimension = i + __SORT_BRANCH_STACK;
558
559 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
560 sd->taken = 1;
561
562 return 0;
563 }
564
dd68ada2
JK
565 return -ESRCH;
566}
c8829c7a 567
55309985 568int setup_sorting(void)
c8829c7a
ACM
569{
570 char *tmp, *tok, *str = strdup(sort_order);
55309985 571 int ret = 0;
c8829c7a 572
5936f54d
NK
573 if (str == NULL) {
574 error("Not enough memory to setup sort keys");
575 return -ENOMEM;
576 }
577
c8829c7a
ACM
578 for (tok = strtok_r(str, ", ", &tmp);
579 tok; tok = strtok_r(NULL, ", ", &tmp)) {
55309985 580 ret = sort_dimension__add(tok);
fc5871ed
NK
581 if (ret == -EINVAL) {
582 error("Invalid --sort key: `%s'", tok);
55309985 583 break;
fc5871ed 584 } else if (ret == -ESRCH) {
c8829c7a 585 error("Unknown --sort key: `%s'", tok);
55309985 586 break;
c8829c7a
ACM
587 }
588 }
589
590 free(str);
55309985 591 return ret;
c8829c7a 592}
c351c281
ACM
593
594void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
595 const char *list_name, FILE *fp)
596{
597 if (list && strlist__nr_entries(list) == 1) {
598 if (fp != NULL)
599 fprintf(fp, "# %s: %s\n", list_name,
600 strlist__entry(list, 0)->s);
601 self->elide = true;
602 }
603}