perf sort: Get rid of unnecessary __maybe_unused
[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;
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{
a4e3b956 63 return repsep_snprintf(bf, size, "%*s:%5d", width,
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
b5387528
RAV
163static 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
53985a7b
SL
172 ip_l = sym_l->start;
173 ip_r = sym_r->start;
b5387528
RAV
174
175 return (int64_t)(ip_r - ip_l);
176}
177
14d1ac74
NK
178static int64_t
179sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
b5387528 180{
14d1ac74 181 u64 ip_l, ip_r;
439d473b 182
14d1ac74
NK
183 if (!left->ms.sym && !right->ms.sym)
184 return right->level - left->level;
dd68ada2 185
14d1ac74
NK
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);
b5387528
RAV
196}
197
198static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
199 u64 ip, char level, char *bf, size_t size,
43355522 200 unsigned int width)
b5387528
RAV
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);
439d473b 208 }
dd68ada2 209
b5387528
RAV
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;
dd68ada2
JK
224}
225
b5387528 226static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
43355522 227 size_t size, unsigned int width)
b5387528
RAV
228{
229 return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
230 self->level, bf, size, width);
231}
dd68ada2 232
872a878f
FW
233struct sort_entry sort_sym = {
234 .se_header = "Symbol",
235 .se_cmp = sort__sym_cmp,
236 .se_snprintf = hist_entry__sym_snprintf,
237 .se_width_idx = HISTC_SYMBOL,
238};
dd68ada2 239
409a8be6
ACM
240/* --sort srcline */
241
242static int64_t
243sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
244{
245 return (int64_t)(right->ip - left->ip);
246}
247
248static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
1d037ca1
IT
249 size_t size,
250 unsigned int width __maybe_unused)
409a8be6
ACM
251{
252 FILE *fp;
253 char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
254 size_t line_len;
255
256 if (path != NULL)
257 goto out_path;
258
ffe10c6f
NK
259 if (!self->ms.map)
260 goto out_ip;
261
88481b6b
NK
262 if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
263 goto out_ip;
264
409a8be6
ACM
265 snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
266 self->ms.map->dso->long_name, self->ip);
267 fp = popen(cmd, "r");
268 if (!fp)
269 goto out_ip;
270
271 if (getline(&path, &line_len, fp) < 0 || !line_len)
272 goto out_ip;
273 fclose(fp);
274 self->srcline = strdup(path);
275 if (self->srcline == NULL)
276 goto out_ip;
277
278 nl = strchr(self->srcline, '\n');
279 if (nl != NULL)
280 *nl = '\0';
281 path = self->srcline;
282out_path:
283 return repsep_snprintf(bf, size, "%s", path);
284out_ip:
285 return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
286}
287
288struct sort_entry sort_srcline = {
289 .se_header = "Source:Line",
290 .se_cmp = sort__srcline_cmp,
291 .se_snprintf = hist_entry__srcline_snprintf,
292 .se_width_idx = HISTC_SRCLINE,
293};
294
dd68ada2
JK
295/* --sort parent */
296
872a878f 297static int64_t
dd68ada2
JK
298sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
299{
300 struct symbol *sym_l = left->parent;
301 struct symbol *sym_r = right->parent;
302
303 if (!sym_l || !sym_r)
304 return cmp_null(sym_l, sym_r);
305
306 return strcmp(sym_l->name, sym_r->name);
307}
308
a4e3b956
ACM
309static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
310 size_t size, unsigned int width)
dd68ada2 311{
a4e3b956 312 return repsep_snprintf(bf, size, "%-*s", width,
dd68ada2
JK
313 self->parent ? self->parent->name : "[other]");
314}
315
872a878f
FW
316struct sort_entry sort_parent = {
317 .se_header = "Parent symbol",
318 .se_cmp = sort__parent_cmp,
319 .se_snprintf = hist_entry__parent_snprintf,
320 .se_width_idx = HISTC_PARENT,
321};
322
f60f3593
AS
323/* --sort cpu */
324
872a878f 325static int64_t
f60f3593
AS
326sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
327{
328 return right->cpu - left->cpu;
329}
330
331static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
332 size_t size, unsigned int width)
333{
334 return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
335}
336
872a878f
FW
337struct sort_entry sort_cpu = {
338 .se_header = "CPU",
339 .se_cmp = sort__cpu_cmp,
340 .se_snprintf = hist_entry__cpu_snprintf,
341 .se_width_idx = HISTC_CPU,
342};
343
14d1ac74
NK
344/* sort keys for branch stacks */
345
b5387528
RAV
346static int64_t
347sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
348{
349 return _sort__dso_cmp(left->branch_info->from.map,
350 right->branch_info->from.map);
351}
352
353static int hist_entry__dso_from_snprintf(struct hist_entry *self, char *bf,
354 size_t size, unsigned int width)
355{
356 return _hist_entry__dso_snprintf(self->branch_info->from.map,
357 bf, size, width);
358}
359
b5387528
RAV
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,
43355522 400 size_t size, unsigned int width)
b5387528
RAV
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,
43355522 409 size_t size, unsigned int width)
b5387528
RAV
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
14d1ac74
NK
417struct sort_entry sort_dso_from = {
418 .se_header = "Source Shared Object",
419 .se_cmp = sort__dso_from_cmp,
420 .se_snprintf = hist_entry__dso_from_snprintf,
421 .se_width_idx = HISTC_DSO_FROM,
422};
423
b5387528
RAV
424struct sort_entry sort_dso_to = {
425 .se_header = "Target Shared Object",
426 .se_cmp = sort__dso_to_cmp,
427 .se_snprintf = hist_entry__dso_to_snprintf,
428 .se_width_idx = HISTC_DSO_TO,
429};
430
431struct sort_entry sort_sym_from = {
432 .se_header = "Source Symbol",
433 .se_cmp = sort__sym_from_cmp,
434 .se_snprintf = hist_entry__sym_from_snprintf,
435 .se_width_idx = HISTC_SYMBOL_FROM,
436};
437
438struct sort_entry sort_sym_to = {
439 .se_header = "Target Symbol",
440 .se_cmp = sort__sym_to_cmp,
441 .se_snprintf = hist_entry__sym_to_snprintf,
442 .se_width_idx = HISTC_SYMBOL_TO,
443};
444
445static int64_t
446sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
447{
448 const unsigned char mp = left->branch_info->flags.mispred !=
449 right->branch_info->flags.mispred;
450 const unsigned char p = left->branch_info->flags.predicted !=
451 right->branch_info->flags.predicted;
452
453 return mp || p;
454}
455
456static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
457 size_t size, unsigned int width){
458 static const char *out = "N/A";
459
460 if (self->branch_info->flags.predicted)
461 out = "N";
462 else if (self->branch_info->flags.mispred)
463 out = "Y";
464
465 return repsep_snprintf(bf, size, "%-*s", width, out);
466}
467
468struct sort_entry sort_mispredict = {
469 .se_header = "Branch Mispredicted",
470 .se_cmp = sort__mispredict_cmp,
471 .se_snprintf = hist_entry__mispredict_snprintf,
472 .se_width_idx = HISTC_MISPREDICT,
473};
474
872a878f
FW
475struct sort_dimension {
476 const char *name;
477 struct sort_entry *entry;
478 int taken;
479};
480
b5387528
RAV
481#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
482
872a878f 483static struct sort_dimension sort_dimensions[] = {
b5387528
RAV
484 DIM(SORT_PID, "pid", sort_thread),
485 DIM(SORT_COMM, "comm", sort_comm),
486 DIM(SORT_DSO, "dso", sort_dso),
487 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
488 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
489 DIM(SORT_SYM, "symbol", sort_sym),
490 DIM(SORT_SYM_TO, "symbol_from", sort_sym_from),
491 DIM(SORT_SYM_FROM, "symbol_to", sort_sym_to),
492 DIM(SORT_PARENT, "parent", sort_parent),
493 DIM(SORT_CPU, "cpu", sort_cpu),
494 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
409a8be6 495 DIM(SORT_SRCLINE, "srcline", sort_srcline),
872a878f
FW
496};
497
dd68ada2
JK
498int sort_dimension__add(const char *tok)
499{
500 unsigned int i;
501
502 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
503 struct sort_dimension *sd = &sort_dimensions[i];
504
dd68ada2
JK
505 if (strncasecmp(tok, sd->name, strlen(tok)))
506 continue;
dd68ada2
JK
507 if (sd->entry == &sort_parent) {
508 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
509 if (ret) {
510 char err[BUFSIZ];
511
512 regerror(ret, &parent_regex, err, sizeof(err));
2aefa4f7
ACM
513 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
514 return -EINVAL;
dd68ada2
JK
515 }
516 sort__has_parent = 1;
1af55640
NK
517 } else if (sd->entry == &sort_sym ||
518 sd->entry == &sort_sym_from ||
519 sd->entry == &sort_sym_to) {
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
a4fb581b
FW
529 if (list_empty(&hist_entry__sort_list)) {
530 if (!strcmp(sd->name, "pid"))
531 sort__first_dimension = SORT_PID;
532 else if (!strcmp(sd->name, "comm"))
533 sort__first_dimension = SORT_COMM;
534 else if (!strcmp(sd->name, "dso"))
535 sort__first_dimension = SORT_DSO;
536 else if (!strcmp(sd->name, "symbol"))
537 sort__first_dimension = SORT_SYM;
538 else if (!strcmp(sd->name, "parent"))
539 sort__first_dimension = SORT_PARENT;
f60f3593
AS
540 else if (!strcmp(sd->name, "cpu"))
541 sort__first_dimension = SORT_CPU;
b5387528
RAV
542 else if (!strcmp(sd->name, "symbol_from"))
543 sort__first_dimension = SORT_SYM_FROM;
544 else if (!strcmp(sd->name, "symbol_to"))
545 sort__first_dimension = SORT_SYM_TO;
546 else if (!strcmp(sd->name, "dso_from"))
547 sort__first_dimension = SORT_DSO_FROM;
548 else if (!strcmp(sd->name, "dso_to"))
549 sort__first_dimension = SORT_DSO_TO;
550 else if (!strcmp(sd->name, "mispredict"))
551 sort__first_dimension = SORT_MISPREDICT;
a4fb581b 552 }
af0a6fa4 553
dd68ada2
JK
554 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
555 sd->taken = 1;
556
557 return 0;
558 }
dd68ada2
JK
559 return -ESRCH;
560}
c8829c7a
ACM
561
562void setup_sorting(const char * const usagestr[], const struct option *opts)
563{
564 char *tmp, *tok, *str = strdup(sort_order);
565
566 for (tok = strtok_r(str, ", ", &tmp);
567 tok; tok = strtok_r(NULL, ", ", &tmp)) {
568 if (sort_dimension__add(tok) < 0) {
569 error("Unknown --sort key: `%s'", tok);
570 usage_with_options(usagestr, opts);
571 }
572 }
573
574 free(str);
575}
c351c281
ACM
576
577void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
578 const char *list_name, FILE *fp)
579{
580 if (list && strlist__nr_entries(list) == 1) {
581 if (fp != NULL)
582 fprintf(fp, "# %s: %s\n", list_name,
583 strlist__entry(list, 0)->s);
584 self->elide = true;
585 }
586}
This page took 0.179385 seconds and 5 git commands to generate.