perf hists browser: Fix first column printing
[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,
1d037ca1 173 unsigned int width __maybe_unused)
b5387528
RAV
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 207static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
1d037ca1
IT
208 size_t size,
209 unsigned int width __maybe_unused)
b5387528
RAV
210{
211 return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
212 self->level, bf, size, width);
213}
dd68ada2 214
b5387528 215/* --sort symbol */
872a878f 216static int64_t
dd68ada2
JK
217sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
218{
219 u64 ip_l, ip_r;
220
6bb8f311
AB
221 if (!left->ms.sym && !right->ms.sym)
222 return right->level - left->level;
223
224 if (!left->ms.sym || !right->ms.sym)
225 return cmp_null(left->ms.sym, right->ms.sym);
226
59fd5306 227 if (left->ms.sym == right->ms.sym)
dd68ada2
JK
228 return 0;
229
6bb8f311
AB
230 ip_l = left->ms.sym->start;
231 ip_r = right->ms.sym->start;
dd68ada2 232
b5387528 233 return _sort__sym_cmp(left->ms.sym, right->ms.sym, ip_l, ip_r);
dd68ada2
JK
234}
235
872a878f
FW
236struct sort_entry sort_sym = {
237 .se_header = "Symbol",
238 .se_cmp = sort__sym_cmp,
239 .se_snprintf = hist_entry__sym_snprintf,
240 .se_width_idx = HISTC_SYMBOL,
241};
dd68ada2 242
409a8be6
ACM
243/* --sort srcline */
244
245static int64_t
246sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
247{
248 return (int64_t)(right->ip - left->ip);
249}
250
251static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
1d037ca1
IT
252 size_t size,
253 unsigned int width __maybe_unused)
409a8be6
ACM
254{
255 FILE *fp;
256 char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
257 size_t line_len;
258
259 if (path != NULL)
260 goto out_path;
261
262 snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
263 self->ms.map->dso->long_name, self->ip);
264 fp = popen(cmd, "r");
265 if (!fp)
266 goto out_ip;
267
268 if (getline(&path, &line_len, fp) < 0 || !line_len)
269 goto out_ip;
270 fclose(fp);
271 self->srcline = strdup(path);
272 if (self->srcline == NULL)
273 goto out_ip;
274
275 nl = strchr(self->srcline, '\n');
276 if (nl != NULL)
277 *nl = '\0';
278 path = self->srcline;
279out_path:
280 return repsep_snprintf(bf, size, "%s", path);
281out_ip:
282 return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
283}
284
285struct sort_entry sort_srcline = {
286 .se_header = "Source:Line",
287 .se_cmp = sort__srcline_cmp,
288 .se_snprintf = hist_entry__srcline_snprintf,
289 .se_width_idx = HISTC_SRCLINE,
290};
291
dd68ada2
JK
292/* --sort parent */
293
872a878f 294static int64_t
dd68ada2
JK
295sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
296{
297 struct symbol *sym_l = left->parent;
298 struct symbol *sym_r = right->parent;
299
300 if (!sym_l || !sym_r)
301 return cmp_null(sym_l, sym_r);
302
303 return strcmp(sym_l->name, sym_r->name);
304}
305
a4e3b956
ACM
306static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
307 size_t size, unsigned int width)
dd68ada2 308{
a4e3b956 309 return repsep_snprintf(bf, size, "%-*s", width,
dd68ada2
JK
310 self->parent ? self->parent->name : "[other]");
311}
312
872a878f
FW
313struct sort_entry sort_parent = {
314 .se_header = "Parent symbol",
315 .se_cmp = sort__parent_cmp,
316 .se_snprintf = hist_entry__parent_snprintf,
317 .se_width_idx = HISTC_PARENT,
318};
319
f60f3593
AS
320/* --sort cpu */
321
872a878f 322static int64_t
f60f3593
AS
323sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
324{
325 return right->cpu - left->cpu;
326}
327
328static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
329 size_t size, unsigned int width)
330{
331 return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
332}
333
872a878f
FW
334struct sort_entry sort_cpu = {
335 .se_header = "CPU",
336 .se_cmp = sort__cpu_cmp,
337 .se_snprintf = hist_entry__cpu_snprintf,
338 .se_width_idx = HISTC_CPU,
339};
340
b5387528
RAV
341static int64_t
342sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
343{
344 return _sort__dso_cmp(left->branch_info->from.map,
345 right->branch_info->from.map);
346}
347
348static int hist_entry__dso_from_snprintf(struct hist_entry *self, char *bf,
349 size_t size, unsigned int width)
350{
351 return _hist_entry__dso_snprintf(self->branch_info->from.map,
352 bf, size, width);
353}
354
355struct sort_entry sort_dso_from = {
356 .se_header = "Source Shared Object",
357 .se_cmp = sort__dso_from_cmp,
358 .se_snprintf = hist_entry__dso_from_snprintf,
359 .se_width_idx = HISTC_DSO_FROM,
360};
361
362static int64_t
363sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
364{
365 return _sort__dso_cmp(left->branch_info->to.map,
366 right->branch_info->to.map);
367}
368
369static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf,
370 size_t size, unsigned int width)
371{
372 return _hist_entry__dso_snprintf(self->branch_info->to.map,
373 bf, size, width);
374}
375
376static int64_t
377sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
378{
379 struct addr_map_symbol *from_l = &left->branch_info->from;
380 struct addr_map_symbol *from_r = &right->branch_info->from;
381
382 if (!from_l->sym && !from_r->sym)
383 return right->level - left->level;
384
385 return _sort__sym_cmp(from_l->sym, from_r->sym, from_l->addr,
386 from_r->addr);
387}
388
389static int64_t
390sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
391{
392 struct addr_map_symbol *to_l = &left->branch_info->to;
393 struct addr_map_symbol *to_r = &right->branch_info->to;
394
395 if (!to_l->sym && !to_r->sym)
396 return right->level - left->level;
397
398 return _sort__sym_cmp(to_l->sym, to_r->sym, to_l->addr, to_r->addr);
399}
400
401static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
1d037ca1
IT
402 size_t size,
403 unsigned int width __maybe_unused)
b5387528
RAV
404{
405 struct addr_map_symbol *from = &self->branch_info->from;
406 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
407 self->level, bf, size, width);
408
409}
410
411static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
1d037ca1
IT
412 size_t size,
413 unsigned int width __maybe_unused)
b5387528
RAV
414{
415 struct addr_map_symbol *to = &self->branch_info->to;
416 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
417 self->level, bf, size, width);
418
419}
420
421struct sort_entry sort_dso_to = {
422 .se_header = "Target Shared Object",
423 .se_cmp = sort__dso_to_cmp,
424 .se_snprintf = hist_entry__dso_to_snprintf,
425 .se_width_idx = HISTC_DSO_TO,
426};
427
428struct sort_entry sort_sym_from = {
429 .se_header = "Source Symbol",
430 .se_cmp = sort__sym_from_cmp,
431 .se_snprintf = hist_entry__sym_from_snprintf,
432 .se_width_idx = HISTC_SYMBOL_FROM,
433};
434
435struct sort_entry sort_sym_to = {
436 .se_header = "Target Symbol",
437 .se_cmp = sort__sym_to_cmp,
438 .se_snprintf = hist_entry__sym_to_snprintf,
439 .se_width_idx = HISTC_SYMBOL_TO,
440};
441
442static int64_t
443sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
444{
445 const unsigned char mp = left->branch_info->flags.mispred !=
446 right->branch_info->flags.mispred;
447 const unsigned char p = left->branch_info->flags.predicted !=
448 right->branch_info->flags.predicted;
449
450 return mp || p;
451}
452
453static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
454 size_t size, unsigned int width){
455 static const char *out = "N/A";
456
457 if (self->branch_info->flags.predicted)
458 out = "N";
459 else if (self->branch_info->flags.mispred)
460 out = "Y";
461
462 return repsep_snprintf(bf, size, "%-*s", width, out);
463}
464
465struct sort_entry sort_mispredict = {
466 .se_header = "Branch Mispredicted",
467 .se_cmp = sort__mispredict_cmp,
468 .se_snprintf = hist_entry__mispredict_snprintf,
469 .se_width_idx = HISTC_MISPREDICT,
470};
471
872a878f
FW
472struct sort_dimension {
473 const char *name;
474 struct sort_entry *entry;
475 int taken;
476};
477
b5387528
RAV
478#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
479
872a878f 480static struct sort_dimension sort_dimensions[] = {
b5387528
RAV
481 DIM(SORT_PID, "pid", sort_thread),
482 DIM(SORT_COMM, "comm", sort_comm),
483 DIM(SORT_DSO, "dso", sort_dso),
484 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
485 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
486 DIM(SORT_SYM, "symbol", sort_sym),
487 DIM(SORT_SYM_TO, "symbol_from", sort_sym_from),
488 DIM(SORT_SYM_FROM, "symbol_to", sort_sym_to),
489 DIM(SORT_PARENT, "parent", sort_parent),
490 DIM(SORT_CPU, "cpu", sort_cpu),
491 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
409a8be6 492 DIM(SORT_SRCLINE, "srcline", sort_srcline),
872a878f
FW
493};
494
dd68ada2
JK
495int sort_dimension__add(const char *tok)
496{
497 unsigned int i;
498
499 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
500 struct sort_dimension *sd = &sort_dimensions[i];
501
dd68ada2
JK
502 if (strncasecmp(tok, sd->name, strlen(tok)))
503 continue;
dd68ada2
JK
504 if (sd->entry == &sort_parent) {
505 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
506 if (ret) {
507 char err[BUFSIZ];
508
509 regerror(ret, &parent_regex, err, sizeof(err));
2aefa4f7
ACM
510 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
511 return -EINVAL;
dd68ada2
JK
512 }
513 sort__has_parent = 1;
514 }
515
fd8ea212
FW
516 if (sd->taken)
517 return 0;
518
519 if (sd->entry->se_collapse)
520 sort__need_collapse = 1;
521
a4fb581b
FW
522 if (list_empty(&hist_entry__sort_list)) {
523 if (!strcmp(sd->name, "pid"))
524 sort__first_dimension = SORT_PID;
525 else if (!strcmp(sd->name, "comm"))
526 sort__first_dimension = SORT_COMM;
527 else if (!strcmp(sd->name, "dso"))
528 sort__first_dimension = SORT_DSO;
529 else if (!strcmp(sd->name, "symbol"))
530 sort__first_dimension = SORT_SYM;
531 else if (!strcmp(sd->name, "parent"))
532 sort__first_dimension = SORT_PARENT;
f60f3593
AS
533 else if (!strcmp(sd->name, "cpu"))
534 sort__first_dimension = SORT_CPU;
b5387528
RAV
535 else if (!strcmp(sd->name, "symbol_from"))
536 sort__first_dimension = SORT_SYM_FROM;
537 else if (!strcmp(sd->name, "symbol_to"))
538 sort__first_dimension = SORT_SYM_TO;
539 else if (!strcmp(sd->name, "dso_from"))
540 sort__first_dimension = SORT_DSO_FROM;
541 else if (!strcmp(sd->name, "dso_to"))
542 sort__first_dimension = SORT_DSO_TO;
543 else if (!strcmp(sd->name, "mispredict"))
544 sort__first_dimension = SORT_MISPREDICT;
a4fb581b 545 }
af0a6fa4 546
dd68ada2
JK
547 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
548 sd->taken = 1;
549
550 return 0;
551 }
dd68ada2
JK
552 return -ESRCH;
553}
c8829c7a
ACM
554
555void setup_sorting(const char * const usagestr[], const struct option *opts)
556{
557 char *tmp, *tok, *str = strdup(sort_order);
558
559 for (tok = strtok_r(str, ", ", &tmp);
560 tok; tok = strtok_r(NULL, ", ", &tmp)) {
561 if (sort_dimension__add(tok) < 0) {
562 error("Unknown --sort key: `%s'", tok);
563 usage_with_options(usagestr, opts);
564 }
565 }
566
567 free(str);
568}
c351c281
ACM
569
570void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
571 const char *list_name, FILE *fp)
572{
573 if (list && strlist__nr_entries(list) == 1) {
574 if (fp != NULL)
575 fprintf(fp, "# %s: %s\n", list_name,
576 strlist__entry(list, 0)->s);
577 self->elide = true;
578 }
579}
This page took 0.184776 seconds and 5 git commands to generate.