perf tools: Introduce perf_mem__snp_scnprintf function
[deliverable/linux.git] / tools / perf / util / sort.c
1 #include <sys/mman.h>
2 #include "sort.h"
3 #include "hist.h"
4 #include "comm.h"
5 #include "symbol.h"
6 #include "evsel.h"
7 #include "evlist.h"
8 #include <traceevent/event-parse.h>
9 #include "mem-events.h"
10
11 regex_t parent_regex;
12 const char default_parent_pattern[] = "^sys_|^do_page_fault";
13 const char *parent_pattern = default_parent_pattern;
14 const char default_sort_order[] = "comm,dso,symbol";
15 const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
16 const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
17 const char default_top_sort_order[] = "dso,symbol";
18 const char default_diff_sort_order[] = "dso,symbol";
19 const char default_tracepoint_sort_order[] = "trace";
20 const char *sort_order;
21 const char *field_order;
22 regex_t ignore_callees_regex;
23 int have_ignore_callees = 0;
24 int sort__need_collapse = 0;
25 int sort__has_parent = 0;
26 int sort__has_sym = 0;
27 int sort__has_dso = 0;
28 int sort__has_socket = 0;
29 int sort__has_thread = 0;
30 enum sort_mode sort__mode = SORT_MODE__NORMAL;
31
32 /*
33 * Replaces all occurrences of a char used with the:
34 *
35 * -t, --field-separator
36 *
37 * option, that uses a special separator character and don't pad with spaces,
38 * replacing all occurances of this separator in symbol names (and other
39 * output) with a '.' character, that thus it's the only non valid separator.
40 */
41 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
42 {
43 int n;
44 va_list ap;
45
46 va_start(ap, fmt);
47 n = vsnprintf(bf, size, fmt, ap);
48 if (symbol_conf.field_sep && n > 0) {
49 char *sep = bf;
50
51 while (1) {
52 sep = strchr(sep, *symbol_conf.field_sep);
53 if (sep == NULL)
54 break;
55 *sep = '.';
56 }
57 }
58 va_end(ap);
59
60 if (n >= (int)size)
61 return size - 1;
62 return n;
63 }
64
65 static int64_t cmp_null(const void *l, const void *r)
66 {
67 if (!l && !r)
68 return 0;
69 else if (!l)
70 return -1;
71 else
72 return 1;
73 }
74
75 /* --sort pid */
76
77 static int64_t
78 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
79 {
80 return right->thread->tid - left->thread->tid;
81 }
82
83 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
84 size_t size, unsigned int width)
85 {
86 const char *comm = thread__comm_str(he->thread);
87
88 width = max(7U, width) - 6;
89 return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
90 width, width, comm ?: "");
91 }
92
93 struct sort_entry sort_thread = {
94 .se_header = " Pid:Command",
95 .se_cmp = sort__thread_cmp,
96 .se_snprintf = hist_entry__thread_snprintf,
97 .se_width_idx = HISTC_THREAD,
98 };
99
100 /* --sort comm */
101
102 static int64_t
103 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
104 {
105 /* Compare the addr that should be unique among comm */
106 return strcmp(comm__str(right->comm), comm__str(left->comm));
107 }
108
109 static int64_t
110 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
111 {
112 /* Compare the addr that should be unique among comm */
113 return strcmp(comm__str(right->comm), comm__str(left->comm));
114 }
115
116 static int64_t
117 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
118 {
119 return strcmp(comm__str(right->comm), comm__str(left->comm));
120 }
121
122 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
123 size_t size, unsigned int width)
124 {
125 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
126 }
127
128 struct sort_entry sort_comm = {
129 .se_header = "Command",
130 .se_cmp = sort__comm_cmp,
131 .se_collapse = sort__comm_collapse,
132 .se_sort = sort__comm_sort,
133 .se_snprintf = hist_entry__comm_snprintf,
134 .se_width_idx = HISTC_COMM,
135 };
136
137 /* --sort dso */
138
139 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
140 {
141 struct dso *dso_l = map_l ? map_l->dso : NULL;
142 struct dso *dso_r = map_r ? map_r->dso : NULL;
143 const char *dso_name_l, *dso_name_r;
144
145 if (!dso_l || !dso_r)
146 return cmp_null(dso_r, dso_l);
147
148 if (verbose) {
149 dso_name_l = dso_l->long_name;
150 dso_name_r = dso_r->long_name;
151 } else {
152 dso_name_l = dso_l->short_name;
153 dso_name_r = dso_r->short_name;
154 }
155
156 return strcmp(dso_name_l, dso_name_r);
157 }
158
159 static int64_t
160 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
161 {
162 return _sort__dso_cmp(right->ms.map, left->ms.map);
163 }
164
165 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
166 size_t size, unsigned int width)
167 {
168 if (map && map->dso) {
169 const char *dso_name = !verbose ? map->dso->short_name :
170 map->dso->long_name;
171 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
172 }
173
174 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
175 }
176
177 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
178 size_t size, unsigned int width)
179 {
180 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
181 }
182
183 struct sort_entry sort_dso = {
184 .se_header = "Shared Object",
185 .se_cmp = sort__dso_cmp,
186 .se_snprintf = hist_entry__dso_snprintf,
187 .se_width_idx = HISTC_DSO,
188 };
189
190 /* --sort symbol */
191
192 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
193 {
194 return (int64_t)(right_ip - left_ip);
195 }
196
197 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
198 {
199 if (!sym_l || !sym_r)
200 return cmp_null(sym_l, sym_r);
201
202 if (sym_l == sym_r)
203 return 0;
204
205 if (sym_l->start != sym_r->start)
206 return (int64_t)(sym_r->start - sym_l->start);
207
208 return (int64_t)(sym_r->end - sym_l->end);
209 }
210
211 static int64_t
212 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
213 {
214 int64_t ret;
215
216 if (!left->ms.sym && !right->ms.sym)
217 return _sort__addr_cmp(left->ip, right->ip);
218
219 /*
220 * comparing symbol address alone is not enough since it's a
221 * relative address within a dso.
222 */
223 if (!sort__has_dso) {
224 ret = sort__dso_cmp(left, right);
225 if (ret != 0)
226 return ret;
227 }
228
229 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
230 }
231
232 static int64_t
233 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
234 {
235 if (!left->ms.sym || !right->ms.sym)
236 return cmp_null(left->ms.sym, right->ms.sym);
237
238 return strcmp(right->ms.sym->name, left->ms.sym->name);
239 }
240
241 static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
242 u64 ip, char level, char *bf, size_t size,
243 unsigned int width)
244 {
245 size_t ret = 0;
246
247 if (verbose) {
248 char o = map ? dso__symtab_origin(map->dso) : '!';
249 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
250 BITS_PER_LONG / 4 + 2, ip, o);
251 }
252
253 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
254 if (sym && map) {
255 if (map->type == MAP__VARIABLE) {
256 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
257 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
258 ip - map->unmap_ip(map, sym->start));
259 } else {
260 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
261 width - ret,
262 sym->name);
263 }
264 } else {
265 size_t len = BITS_PER_LONG / 4;
266 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
267 len, ip);
268 }
269
270 return ret;
271 }
272
273 static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
274 size_t size, unsigned int width)
275 {
276 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
277 he->level, bf, size, width);
278 }
279
280 struct sort_entry sort_sym = {
281 .se_header = "Symbol",
282 .se_cmp = sort__sym_cmp,
283 .se_sort = sort__sym_sort,
284 .se_snprintf = hist_entry__sym_snprintf,
285 .se_width_idx = HISTC_SYMBOL,
286 };
287
288 /* --sort srcline */
289
290 static char *hist_entry__get_srcline(struct hist_entry *he)
291 {
292 struct map *map = he->ms.map;
293
294 if (!map)
295 return SRCLINE_UNKNOWN;
296
297 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
298 he->ms.sym, true);
299 }
300
301 static int64_t
302 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
303 {
304 if (!left->srcline)
305 left->srcline = hist_entry__get_srcline(left);
306 if (!right->srcline)
307 right->srcline = hist_entry__get_srcline(right);
308
309 return strcmp(right->srcline, left->srcline);
310 }
311
312 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
313 size_t size, unsigned int width)
314 {
315 if (!he->srcline)
316 he->srcline = hist_entry__get_srcline(he);
317
318 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
319 }
320
321 struct sort_entry sort_srcline = {
322 .se_header = "Source:Line",
323 .se_cmp = sort__srcline_cmp,
324 .se_snprintf = hist_entry__srcline_snprintf,
325 .se_width_idx = HISTC_SRCLINE,
326 };
327
328 /* --sort srcfile */
329
330 static char no_srcfile[1];
331
332 static char *hist_entry__get_srcfile(struct hist_entry *e)
333 {
334 char *sf, *p;
335 struct map *map = e->ms.map;
336
337 if (!map)
338 return no_srcfile;
339
340 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
341 e->ms.sym, false, true);
342 if (!strcmp(sf, SRCLINE_UNKNOWN))
343 return no_srcfile;
344 p = strchr(sf, ':');
345 if (p && *sf) {
346 *p = 0;
347 return sf;
348 }
349 free(sf);
350 return no_srcfile;
351 }
352
353 static int64_t
354 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
355 {
356 if (!left->srcfile)
357 left->srcfile = hist_entry__get_srcfile(left);
358 if (!right->srcfile)
359 right->srcfile = hist_entry__get_srcfile(right);
360
361 return strcmp(right->srcfile, left->srcfile);
362 }
363
364 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
365 size_t size, unsigned int width)
366 {
367 if (!he->srcfile)
368 he->srcfile = hist_entry__get_srcfile(he);
369
370 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
371 }
372
373 struct sort_entry sort_srcfile = {
374 .se_header = "Source File",
375 .se_cmp = sort__srcfile_cmp,
376 .se_snprintf = hist_entry__srcfile_snprintf,
377 .se_width_idx = HISTC_SRCFILE,
378 };
379
380 /* --sort parent */
381
382 static int64_t
383 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
384 {
385 struct symbol *sym_l = left->parent;
386 struct symbol *sym_r = right->parent;
387
388 if (!sym_l || !sym_r)
389 return cmp_null(sym_l, sym_r);
390
391 return strcmp(sym_r->name, sym_l->name);
392 }
393
394 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
395 size_t size, unsigned int width)
396 {
397 return repsep_snprintf(bf, size, "%-*.*s", width, width,
398 he->parent ? he->parent->name : "[other]");
399 }
400
401 struct sort_entry sort_parent = {
402 .se_header = "Parent symbol",
403 .se_cmp = sort__parent_cmp,
404 .se_snprintf = hist_entry__parent_snprintf,
405 .se_width_idx = HISTC_PARENT,
406 };
407
408 /* --sort cpu */
409
410 static int64_t
411 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
412 {
413 return right->cpu - left->cpu;
414 }
415
416 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
417 size_t size, unsigned int width)
418 {
419 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
420 }
421
422 struct sort_entry sort_cpu = {
423 .se_header = "CPU",
424 .se_cmp = sort__cpu_cmp,
425 .se_snprintf = hist_entry__cpu_snprintf,
426 .se_width_idx = HISTC_CPU,
427 };
428
429 /* --sort socket */
430
431 static int64_t
432 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
433 {
434 return right->socket - left->socket;
435 }
436
437 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
438 size_t size, unsigned int width)
439 {
440 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
441 }
442
443 struct sort_entry sort_socket = {
444 .se_header = "Socket",
445 .se_cmp = sort__socket_cmp,
446 .se_snprintf = hist_entry__socket_snprintf,
447 .se_width_idx = HISTC_SOCKET,
448 };
449
450 /* --sort trace */
451
452 static char *get_trace_output(struct hist_entry *he)
453 {
454 struct trace_seq seq;
455 struct perf_evsel *evsel;
456 struct pevent_record rec = {
457 .data = he->raw_data,
458 .size = he->raw_size,
459 };
460
461 evsel = hists_to_evsel(he->hists);
462
463 trace_seq_init(&seq);
464 if (symbol_conf.raw_trace) {
465 pevent_print_fields(&seq, he->raw_data, he->raw_size,
466 evsel->tp_format);
467 } else {
468 pevent_event_info(&seq, evsel->tp_format, &rec);
469 }
470 return seq.buffer;
471 }
472
473 static int64_t
474 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
475 {
476 struct perf_evsel *evsel;
477
478 evsel = hists_to_evsel(left->hists);
479 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
480 return 0;
481
482 if (left->trace_output == NULL)
483 left->trace_output = get_trace_output(left);
484 if (right->trace_output == NULL)
485 right->trace_output = get_trace_output(right);
486
487 return strcmp(right->trace_output, left->trace_output);
488 }
489
490 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
491 size_t size, unsigned int width)
492 {
493 struct perf_evsel *evsel;
494
495 evsel = hists_to_evsel(he->hists);
496 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
497 return scnprintf(bf, size, "%-.*s", width, "N/A");
498
499 if (he->trace_output == NULL)
500 he->trace_output = get_trace_output(he);
501 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
502 }
503
504 struct sort_entry sort_trace = {
505 .se_header = "Trace output",
506 .se_cmp = sort__trace_cmp,
507 .se_snprintf = hist_entry__trace_snprintf,
508 .se_width_idx = HISTC_TRACE,
509 };
510
511 /* sort keys for branch stacks */
512
513 static int64_t
514 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
515 {
516 if (!left->branch_info || !right->branch_info)
517 return cmp_null(left->branch_info, right->branch_info);
518
519 return _sort__dso_cmp(left->branch_info->from.map,
520 right->branch_info->from.map);
521 }
522
523 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
524 size_t size, unsigned int width)
525 {
526 if (he->branch_info)
527 return _hist_entry__dso_snprintf(he->branch_info->from.map,
528 bf, size, width);
529 else
530 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
531 }
532
533 static int64_t
534 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
535 {
536 if (!left->branch_info || !right->branch_info)
537 return cmp_null(left->branch_info, right->branch_info);
538
539 return _sort__dso_cmp(left->branch_info->to.map,
540 right->branch_info->to.map);
541 }
542
543 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
544 size_t size, unsigned int width)
545 {
546 if (he->branch_info)
547 return _hist_entry__dso_snprintf(he->branch_info->to.map,
548 bf, size, width);
549 else
550 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
551 }
552
553 static int64_t
554 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
555 {
556 struct addr_map_symbol *from_l = &left->branch_info->from;
557 struct addr_map_symbol *from_r = &right->branch_info->from;
558
559 if (!left->branch_info || !right->branch_info)
560 return cmp_null(left->branch_info, right->branch_info);
561
562 from_l = &left->branch_info->from;
563 from_r = &right->branch_info->from;
564
565 if (!from_l->sym && !from_r->sym)
566 return _sort__addr_cmp(from_l->addr, from_r->addr);
567
568 return _sort__sym_cmp(from_l->sym, from_r->sym);
569 }
570
571 static int64_t
572 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
573 {
574 struct addr_map_symbol *to_l, *to_r;
575
576 if (!left->branch_info || !right->branch_info)
577 return cmp_null(left->branch_info, right->branch_info);
578
579 to_l = &left->branch_info->to;
580 to_r = &right->branch_info->to;
581
582 if (!to_l->sym && !to_r->sym)
583 return _sort__addr_cmp(to_l->addr, to_r->addr);
584
585 return _sort__sym_cmp(to_l->sym, to_r->sym);
586 }
587
588 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
589 size_t size, unsigned int width)
590 {
591 if (he->branch_info) {
592 struct addr_map_symbol *from = &he->branch_info->from;
593
594 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
595 he->level, bf, size, width);
596 }
597
598 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
599 }
600
601 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
602 size_t size, unsigned int width)
603 {
604 if (he->branch_info) {
605 struct addr_map_symbol *to = &he->branch_info->to;
606
607 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
608 he->level, bf, size, width);
609 }
610
611 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
612 }
613
614 struct sort_entry sort_dso_from = {
615 .se_header = "Source Shared Object",
616 .se_cmp = sort__dso_from_cmp,
617 .se_snprintf = hist_entry__dso_from_snprintf,
618 .se_width_idx = HISTC_DSO_FROM,
619 };
620
621 struct sort_entry sort_dso_to = {
622 .se_header = "Target Shared Object",
623 .se_cmp = sort__dso_to_cmp,
624 .se_snprintf = hist_entry__dso_to_snprintf,
625 .se_width_idx = HISTC_DSO_TO,
626 };
627
628 struct sort_entry sort_sym_from = {
629 .se_header = "Source Symbol",
630 .se_cmp = sort__sym_from_cmp,
631 .se_snprintf = hist_entry__sym_from_snprintf,
632 .se_width_idx = HISTC_SYMBOL_FROM,
633 };
634
635 struct sort_entry sort_sym_to = {
636 .se_header = "Target Symbol",
637 .se_cmp = sort__sym_to_cmp,
638 .se_snprintf = hist_entry__sym_to_snprintf,
639 .se_width_idx = HISTC_SYMBOL_TO,
640 };
641
642 static int64_t
643 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
644 {
645 unsigned char mp, p;
646
647 if (!left->branch_info || !right->branch_info)
648 return cmp_null(left->branch_info, right->branch_info);
649
650 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
651 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
652 return mp || p;
653 }
654
655 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
656 size_t size, unsigned int width){
657 static const char *out = "N/A";
658
659 if (he->branch_info) {
660 if (he->branch_info->flags.predicted)
661 out = "N";
662 else if (he->branch_info->flags.mispred)
663 out = "Y";
664 }
665
666 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
667 }
668
669 static int64_t
670 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
671 {
672 return left->branch_info->flags.cycles -
673 right->branch_info->flags.cycles;
674 }
675
676 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
677 size_t size, unsigned int width)
678 {
679 if (he->branch_info->flags.cycles == 0)
680 return repsep_snprintf(bf, size, "%-*s", width, "-");
681 return repsep_snprintf(bf, size, "%-*hd", width,
682 he->branch_info->flags.cycles);
683 }
684
685 struct sort_entry sort_cycles = {
686 .se_header = "Basic Block Cycles",
687 .se_cmp = sort__cycles_cmp,
688 .se_snprintf = hist_entry__cycles_snprintf,
689 .se_width_idx = HISTC_CYCLES,
690 };
691
692 /* --sort daddr_sym */
693 static int64_t
694 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
695 {
696 uint64_t l = 0, r = 0;
697
698 if (left->mem_info)
699 l = left->mem_info->daddr.addr;
700 if (right->mem_info)
701 r = right->mem_info->daddr.addr;
702
703 return (int64_t)(r - l);
704 }
705
706 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
707 size_t size, unsigned int width)
708 {
709 uint64_t addr = 0;
710 struct map *map = NULL;
711 struct symbol *sym = NULL;
712
713 if (he->mem_info) {
714 addr = he->mem_info->daddr.addr;
715 map = he->mem_info->daddr.map;
716 sym = he->mem_info->daddr.sym;
717 }
718 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
719 width);
720 }
721
722 static int64_t
723 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
724 {
725 uint64_t l = 0, r = 0;
726
727 if (left->mem_info)
728 l = left->mem_info->iaddr.addr;
729 if (right->mem_info)
730 r = right->mem_info->iaddr.addr;
731
732 return (int64_t)(r - l);
733 }
734
735 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
736 size_t size, unsigned int width)
737 {
738 uint64_t addr = 0;
739 struct map *map = NULL;
740 struct symbol *sym = NULL;
741
742 if (he->mem_info) {
743 addr = he->mem_info->iaddr.addr;
744 map = he->mem_info->iaddr.map;
745 sym = he->mem_info->iaddr.sym;
746 }
747 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
748 width);
749 }
750
751 static int64_t
752 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
753 {
754 struct map *map_l = NULL;
755 struct map *map_r = NULL;
756
757 if (left->mem_info)
758 map_l = left->mem_info->daddr.map;
759 if (right->mem_info)
760 map_r = right->mem_info->daddr.map;
761
762 return _sort__dso_cmp(map_l, map_r);
763 }
764
765 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
766 size_t size, unsigned int width)
767 {
768 struct map *map = NULL;
769
770 if (he->mem_info)
771 map = he->mem_info->daddr.map;
772
773 return _hist_entry__dso_snprintf(map, bf, size, width);
774 }
775
776 static int64_t
777 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
778 {
779 union perf_mem_data_src data_src_l;
780 union perf_mem_data_src data_src_r;
781
782 if (left->mem_info)
783 data_src_l = left->mem_info->data_src;
784 else
785 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
786
787 if (right->mem_info)
788 data_src_r = right->mem_info->data_src;
789 else
790 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
791
792 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
793 }
794
795 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
796 size_t size, unsigned int width)
797 {
798 const char *out;
799 u64 mask = PERF_MEM_LOCK_NA;
800
801 if (he->mem_info)
802 mask = he->mem_info->data_src.mem_lock;
803
804 if (mask & PERF_MEM_LOCK_NA)
805 out = "N/A";
806 else if (mask & PERF_MEM_LOCK_LOCKED)
807 out = "Yes";
808 else
809 out = "No";
810
811 return repsep_snprintf(bf, size, "%.*s", width, out);
812 }
813
814 static int64_t
815 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
816 {
817 union perf_mem_data_src data_src_l;
818 union perf_mem_data_src data_src_r;
819
820 if (left->mem_info)
821 data_src_l = left->mem_info->data_src;
822 else
823 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
824
825 if (right->mem_info)
826 data_src_r = right->mem_info->data_src;
827 else
828 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
829
830 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
831 }
832
833 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
834 size_t size, unsigned int width)
835 {
836 char out[64];
837
838 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
839 return repsep_snprintf(bf, size, "%-*s", width, out);
840 }
841
842 static int64_t
843 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
844 {
845 union perf_mem_data_src data_src_l;
846 union perf_mem_data_src data_src_r;
847
848 if (left->mem_info)
849 data_src_l = left->mem_info->data_src;
850 else
851 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
852
853 if (right->mem_info)
854 data_src_r = right->mem_info->data_src;
855 else
856 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
857
858 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
859 }
860
861 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
862 size_t size, unsigned int width)
863 {
864 char out[64];
865
866 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
867 return repsep_snprintf(bf, size, "%-*s", width, out);
868 }
869
870 static int64_t
871 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
872 {
873 union perf_mem_data_src data_src_l;
874 union perf_mem_data_src data_src_r;
875
876 if (left->mem_info)
877 data_src_l = left->mem_info->data_src;
878 else
879 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
880
881 if (right->mem_info)
882 data_src_r = right->mem_info->data_src;
883 else
884 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
885
886 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
887 }
888
889 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
890 size_t size, unsigned int width)
891 {
892 char out[64];
893
894 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
895 return repsep_snprintf(bf, size, "%-*s", width, out);
896 }
897
898 static int64_t
899 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
900 {
901 u64 l, r;
902 struct map *l_map, *r_map;
903
904 if (!left->mem_info) return -1;
905 if (!right->mem_info) return 1;
906
907 /* group event types together */
908 if (left->cpumode > right->cpumode) return -1;
909 if (left->cpumode < right->cpumode) return 1;
910
911 l_map = left->mem_info->daddr.map;
912 r_map = right->mem_info->daddr.map;
913
914 /* if both are NULL, jump to sort on al_addr instead */
915 if (!l_map && !r_map)
916 goto addr;
917
918 if (!l_map) return -1;
919 if (!r_map) return 1;
920
921 if (l_map->maj > r_map->maj) return -1;
922 if (l_map->maj < r_map->maj) return 1;
923
924 if (l_map->min > r_map->min) return -1;
925 if (l_map->min < r_map->min) return 1;
926
927 if (l_map->ino > r_map->ino) return -1;
928 if (l_map->ino < r_map->ino) return 1;
929
930 if (l_map->ino_generation > r_map->ino_generation) return -1;
931 if (l_map->ino_generation < r_map->ino_generation) return 1;
932
933 /*
934 * Addresses with no major/minor numbers are assumed to be
935 * anonymous in userspace. Sort those on pid then address.
936 *
937 * The kernel and non-zero major/minor mapped areas are
938 * assumed to be unity mapped. Sort those on address.
939 */
940
941 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
942 (!(l_map->flags & MAP_SHARED)) &&
943 !l_map->maj && !l_map->min && !l_map->ino &&
944 !l_map->ino_generation) {
945 /* userspace anonymous */
946
947 if (left->thread->pid_ > right->thread->pid_) return -1;
948 if (left->thread->pid_ < right->thread->pid_) return 1;
949 }
950
951 addr:
952 /* al_addr does all the right addr - start + offset calculations */
953 l = cl_address(left->mem_info->daddr.al_addr);
954 r = cl_address(right->mem_info->daddr.al_addr);
955
956 if (l > r) return -1;
957 if (l < r) return 1;
958
959 return 0;
960 }
961
962 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
963 size_t size, unsigned int width)
964 {
965
966 uint64_t addr = 0;
967 struct map *map = NULL;
968 struct symbol *sym = NULL;
969 char level = he->level;
970
971 if (he->mem_info) {
972 addr = cl_address(he->mem_info->daddr.al_addr);
973 map = he->mem_info->daddr.map;
974 sym = he->mem_info->daddr.sym;
975
976 /* print [s] for shared data mmaps */
977 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
978 map && (map->type == MAP__VARIABLE) &&
979 (map->flags & MAP_SHARED) &&
980 (map->maj || map->min || map->ino ||
981 map->ino_generation))
982 level = 's';
983 else if (!map)
984 level = 'X';
985 }
986 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
987 width);
988 }
989
990 struct sort_entry sort_mispredict = {
991 .se_header = "Branch Mispredicted",
992 .se_cmp = sort__mispredict_cmp,
993 .se_snprintf = hist_entry__mispredict_snprintf,
994 .se_width_idx = HISTC_MISPREDICT,
995 };
996
997 static u64 he_weight(struct hist_entry *he)
998 {
999 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1000 }
1001
1002 static int64_t
1003 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1004 {
1005 return he_weight(left) - he_weight(right);
1006 }
1007
1008 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1009 size_t size, unsigned int width)
1010 {
1011 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1012 }
1013
1014 struct sort_entry sort_local_weight = {
1015 .se_header = "Local Weight",
1016 .se_cmp = sort__local_weight_cmp,
1017 .se_snprintf = hist_entry__local_weight_snprintf,
1018 .se_width_idx = HISTC_LOCAL_WEIGHT,
1019 };
1020
1021 static int64_t
1022 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1023 {
1024 return left->stat.weight - right->stat.weight;
1025 }
1026
1027 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1028 size_t size, unsigned int width)
1029 {
1030 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1031 }
1032
1033 struct sort_entry sort_global_weight = {
1034 .se_header = "Weight",
1035 .se_cmp = sort__global_weight_cmp,
1036 .se_snprintf = hist_entry__global_weight_snprintf,
1037 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1038 };
1039
1040 struct sort_entry sort_mem_daddr_sym = {
1041 .se_header = "Data Symbol",
1042 .se_cmp = sort__daddr_cmp,
1043 .se_snprintf = hist_entry__daddr_snprintf,
1044 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1045 };
1046
1047 struct sort_entry sort_mem_iaddr_sym = {
1048 .se_header = "Code Symbol",
1049 .se_cmp = sort__iaddr_cmp,
1050 .se_snprintf = hist_entry__iaddr_snprintf,
1051 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1052 };
1053
1054 struct sort_entry sort_mem_daddr_dso = {
1055 .se_header = "Data Object",
1056 .se_cmp = sort__dso_daddr_cmp,
1057 .se_snprintf = hist_entry__dso_daddr_snprintf,
1058 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1059 };
1060
1061 struct sort_entry sort_mem_locked = {
1062 .se_header = "Locked",
1063 .se_cmp = sort__locked_cmp,
1064 .se_snprintf = hist_entry__locked_snprintf,
1065 .se_width_idx = HISTC_MEM_LOCKED,
1066 };
1067
1068 struct sort_entry sort_mem_tlb = {
1069 .se_header = "TLB access",
1070 .se_cmp = sort__tlb_cmp,
1071 .se_snprintf = hist_entry__tlb_snprintf,
1072 .se_width_idx = HISTC_MEM_TLB,
1073 };
1074
1075 struct sort_entry sort_mem_lvl = {
1076 .se_header = "Memory access",
1077 .se_cmp = sort__lvl_cmp,
1078 .se_snprintf = hist_entry__lvl_snprintf,
1079 .se_width_idx = HISTC_MEM_LVL,
1080 };
1081
1082 struct sort_entry sort_mem_snoop = {
1083 .se_header = "Snoop",
1084 .se_cmp = sort__snoop_cmp,
1085 .se_snprintf = hist_entry__snoop_snprintf,
1086 .se_width_idx = HISTC_MEM_SNOOP,
1087 };
1088
1089 struct sort_entry sort_mem_dcacheline = {
1090 .se_header = "Data Cacheline",
1091 .se_cmp = sort__dcacheline_cmp,
1092 .se_snprintf = hist_entry__dcacheline_snprintf,
1093 .se_width_idx = HISTC_MEM_DCACHELINE,
1094 };
1095
1096 static int64_t
1097 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1098 {
1099 if (!left->branch_info || !right->branch_info)
1100 return cmp_null(left->branch_info, right->branch_info);
1101
1102 return left->branch_info->flags.abort !=
1103 right->branch_info->flags.abort;
1104 }
1105
1106 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1107 size_t size, unsigned int width)
1108 {
1109 static const char *out = "N/A";
1110
1111 if (he->branch_info) {
1112 if (he->branch_info->flags.abort)
1113 out = "A";
1114 else
1115 out = ".";
1116 }
1117
1118 return repsep_snprintf(bf, size, "%-*s", width, out);
1119 }
1120
1121 struct sort_entry sort_abort = {
1122 .se_header = "Transaction abort",
1123 .se_cmp = sort__abort_cmp,
1124 .se_snprintf = hist_entry__abort_snprintf,
1125 .se_width_idx = HISTC_ABORT,
1126 };
1127
1128 static int64_t
1129 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1130 {
1131 if (!left->branch_info || !right->branch_info)
1132 return cmp_null(left->branch_info, right->branch_info);
1133
1134 return left->branch_info->flags.in_tx !=
1135 right->branch_info->flags.in_tx;
1136 }
1137
1138 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1139 size_t size, unsigned int width)
1140 {
1141 static const char *out = "N/A";
1142
1143 if (he->branch_info) {
1144 if (he->branch_info->flags.in_tx)
1145 out = "T";
1146 else
1147 out = ".";
1148 }
1149
1150 return repsep_snprintf(bf, size, "%-*s", width, out);
1151 }
1152
1153 struct sort_entry sort_in_tx = {
1154 .se_header = "Branch in transaction",
1155 .se_cmp = sort__in_tx_cmp,
1156 .se_snprintf = hist_entry__in_tx_snprintf,
1157 .se_width_idx = HISTC_IN_TX,
1158 };
1159
1160 static int64_t
1161 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1162 {
1163 return left->transaction - right->transaction;
1164 }
1165
1166 static inline char *add_str(char *p, const char *str)
1167 {
1168 strcpy(p, str);
1169 return p + strlen(str);
1170 }
1171
1172 static struct txbit {
1173 unsigned flag;
1174 const char *name;
1175 int skip_for_len;
1176 } txbits[] = {
1177 { PERF_TXN_ELISION, "EL ", 0 },
1178 { PERF_TXN_TRANSACTION, "TX ", 1 },
1179 { PERF_TXN_SYNC, "SYNC ", 1 },
1180 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1181 { PERF_TXN_RETRY, "RETRY ", 0 },
1182 { PERF_TXN_CONFLICT, "CON ", 0 },
1183 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1184 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1185 { 0, NULL, 0 }
1186 };
1187
1188 int hist_entry__transaction_len(void)
1189 {
1190 int i;
1191 int len = 0;
1192
1193 for (i = 0; txbits[i].name; i++) {
1194 if (!txbits[i].skip_for_len)
1195 len += strlen(txbits[i].name);
1196 }
1197 len += 4; /* :XX<space> */
1198 return len;
1199 }
1200
1201 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1202 size_t size, unsigned int width)
1203 {
1204 u64 t = he->transaction;
1205 char buf[128];
1206 char *p = buf;
1207 int i;
1208
1209 buf[0] = 0;
1210 for (i = 0; txbits[i].name; i++)
1211 if (txbits[i].flag & t)
1212 p = add_str(p, txbits[i].name);
1213 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1214 p = add_str(p, "NEITHER ");
1215 if (t & PERF_TXN_ABORT_MASK) {
1216 sprintf(p, ":%" PRIx64,
1217 (t & PERF_TXN_ABORT_MASK) >>
1218 PERF_TXN_ABORT_SHIFT);
1219 p += strlen(p);
1220 }
1221
1222 return repsep_snprintf(bf, size, "%-*s", width, buf);
1223 }
1224
1225 struct sort_entry sort_transaction = {
1226 .se_header = "Transaction ",
1227 .se_cmp = sort__transaction_cmp,
1228 .se_snprintf = hist_entry__transaction_snprintf,
1229 .se_width_idx = HISTC_TRANSACTION,
1230 };
1231
1232 struct sort_dimension {
1233 const char *name;
1234 struct sort_entry *entry;
1235 int taken;
1236 };
1237
1238 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1239
1240 static struct sort_dimension common_sort_dimensions[] = {
1241 DIM(SORT_PID, "pid", sort_thread),
1242 DIM(SORT_COMM, "comm", sort_comm),
1243 DIM(SORT_DSO, "dso", sort_dso),
1244 DIM(SORT_SYM, "symbol", sort_sym),
1245 DIM(SORT_PARENT, "parent", sort_parent),
1246 DIM(SORT_CPU, "cpu", sort_cpu),
1247 DIM(SORT_SOCKET, "socket", sort_socket),
1248 DIM(SORT_SRCLINE, "srcline", sort_srcline),
1249 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1250 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1251 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1252 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1253 DIM(SORT_TRACE, "trace", sort_trace),
1254 };
1255
1256 #undef DIM
1257
1258 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1259
1260 static struct sort_dimension bstack_sort_dimensions[] = {
1261 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1262 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1263 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1264 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1265 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1266 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1267 DIM(SORT_ABORT, "abort", sort_abort),
1268 DIM(SORT_CYCLES, "cycles", sort_cycles),
1269 };
1270
1271 #undef DIM
1272
1273 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1274
1275 static struct sort_dimension memory_sort_dimensions[] = {
1276 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1277 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1278 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1279 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1280 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1281 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1282 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1283 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1284 };
1285
1286 #undef DIM
1287
1288 struct hpp_dimension {
1289 const char *name;
1290 struct perf_hpp_fmt *fmt;
1291 int taken;
1292 };
1293
1294 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1295
1296 static struct hpp_dimension hpp_sort_dimensions[] = {
1297 DIM(PERF_HPP__OVERHEAD, "overhead"),
1298 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1299 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1300 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1301 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1302 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1303 DIM(PERF_HPP__SAMPLES, "sample"),
1304 DIM(PERF_HPP__PERIOD, "period"),
1305 };
1306
1307 #undef DIM
1308
1309 struct hpp_sort_entry {
1310 struct perf_hpp_fmt hpp;
1311 struct sort_entry *se;
1312 };
1313
1314 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1315 {
1316 struct hpp_sort_entry *hse;
1317
1318 if (!perf_hpp__is_sort_entry(fmt))
1319 return;
1320
1321 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1322 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1323 }
1324
1325 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1326 struct perf_evsel *evsel)
1327 {
1328 struct hpp_sort_entry *hse;
1329 size_t len = fmt->user_len;
1330
1331 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1332
1333 if (!len)
1334 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
1335
1336 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1337 }
1338
1339 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1340 struct perf_hpp *hpp __maybe_unused,
1341 struct perf_evsel *evsel)
1342 {
1343 struct hpp_sort_entry *hse;
1344 size_t len = fmt->user_len;
1345
1346 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1347
1348 if (!len)
1349 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
1350
1351 return len;
1352 }
1353
1354 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1355 struct hist_entry *he)
1356 {
1357 struct hpp_sort_entry *hse;
1358 size_t len = fmt->user_len;
1359
1360 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1361
1362 if (!len)
1363 len = hists__col_len(he->hists, hse->se->se_width_idx);
1364
1365 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1366 }
1367
1368 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1369 struct hist_entry *a, struct hist_entry *b)
1370 {
1371 struct hpp_sort_entry *hse;
1372
1373 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1374 return hse->se->se_cmp(a, b);
1375 }
1376
1377 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1378 struct hist_entry *a, struct hist_entry *b)
1379 {
1380 struct hpp_sort_entry *hse;
1381 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1382
1383 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1384 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1385 return collapse_fn(a, b);
1386 }
1387
1388 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1389 struct hist_entry *a, struct hist_entry *b)
1390 {
1391 struct hpp_sort_entry *hse;
1392 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1393
1394 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1395 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1396 return sort_fn(a, b);
1397 }
1398
1399 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1400 {
1401 return format->header == __sort__hpp_header;
1402 }
1403
1404 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1405 {
1406 struct hpp_sort_entry *hse_a;
1407 struct hpp_sort_entry *hse_b;
1408
1409 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1410 return false;
1411
1412 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1413 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1414
1415 return hse_a->se == hse_b->se;
1416 }
1417
1418 static void hse_free(struct perf_hpp_fmt *fmt)
1419 {
1420 struct hpp_sort_entry *hse;
1421
1422 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1423 free(hse);
1424 }
1425
1426 static struct hpp_sort_entry *
1427 __sort_dimension__alloc_hpp(struct sort_dimension *sd)
1428 {
1429 struct hpp_sort_entry *hse;
1430
1431 hse = malloc(sizeof(*hse));
1432 if (hse == NULL) {
1433 pr_err("Memory allocation failed\n");
1434 return NULL;
1435 }
1436
1437 hse->se = sd->entry;
1438 hse->hpp.name = sd->entry->se_header;
1439 hse->hpp.header = __sort__hpp_header;
1440 hse->hpp.width = __sort__hpp_width;
1441 hse->hpp.entry = __sort__hpp_entry;
1442 hse->hpp.color = NULL;
1443
1444 hse->hpp.cmp = __sort__hpp_cmp;
1445 hse->hpp.collapse = __sort__hpp_collapse;
1446 hse->hpp.sort = __sort__hpp_sort;
1447 hse->hpp.equal = __sort__hpp_equal;
1448 hse->hpp.free = hse_free;
1449
1450 INIT_LIST_HEAD(&hse->hpp.list);
1451 INIT_LIST_HEAD(&hse->hpp.sort_list);
1452 hse->hpp.elide = false;
1453 hse->hpp.len = 0;
1454 hse->hpp.user_len = 0;
1455
1456 return hse;
1457 }
1458
1459 static void hpp_free(struct perf_hpp_fmt *fmt)
1460 {
1461 free(fmt);
1462 }
1463
1464 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd)
1465 {
1466 struct perf_hpp_fmt *fmt;
1467
1468 fmt = memdup(hd->fmt, sizeof(*fmt));
1469 if (fmt) {
1470 INIT_LIST_HEAD(&fmt->list);
1471 INIT_LIST_HEAD(&fmt->sort_list);
1472 fmt->free = hpp_free;
1473 }
1474
1475 return fmt;
1476 }
1477
1478 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd)
1479 {
1480 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
1481
1482 if (hse == NULL)
1483 return -1;
1484
1485 perf_hpp__register_sort_field(&hse->hpp);
1486 return 0;
1487 }
1488
1489 static int __sort_dimension__add_hpp_output(struct perf_hpp_list *list,
1490 struct sort_dimension *sd)
1491 {
1492 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
1493
1494 if (hse == NULL)
1495 return -1;
1496
1497 perf_hpp_list__column_register(list, &hse->hpp);
1498 return 0;
1499 }
1500
1501 struct hpp_dynamic_entry {
1502 struct perf_hpp_fmt hpp;
1503 struct perf_evsel *evsel;
1504 struct format_field *field;
1505 unsigned dynamic_len;
1506 bool raw_trace;
1507 };
1508
1509 static int hde_width(struct hpp_dynamic_entry *hde)
1510 {
1511 if (!hde->hpp.len) {
1512 int len = hde->dynamic_len;
1513 int namelen = strlen(hde->field->name);
1514 int fieldlen = hde->field->size;
1515
1516 if (namelen > len)
1517 len = namelen;
1518
1519 if (!(hde->field->flags & FIELD_IS_STRING)) {
1520 /* length for print hex numbers */
1521 fieldlen = hde->field->size * 2 + 2;
1522 }
1523 if (fieldlen > len)
1524 len = fieldlen;
1525
1526 hde->hpp.len = len;
1527 }
1528 return hde->hpp.len;
1529 }
1530
1531 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1532 struct hist_entry *he)
1533 {
1534 char *str, *pos;
1535 struct format_field *field = hde->field;
1536 size_t namelen;
1537 bool last = false;
1538
1539 if (hde->raw_trace)
1540 return;
1541
1542 /* parse pretty print result and update max length */
1543 if (!he->trace_output)
1544 he->trace_output = get_trace_output(he);
1545
1546 namelen = strlen(field->name);
1547 str = he->trace_output;
1548
1549 while (str) {
1550 pos = strchr(str, ' ');
1551 if (pos == NULL) {
1552 last = true;
1553 pos = str + strlen(str);
1554 }
1555
1556 if (!strncmp(str, field->name, namelen)) {
1557 size_t len;
1558
1559 str += namelen + 1;
1560 len = pos - str;
1561
1562 if (len > hde->dynamic_len)
1563 hde->dynamic_len = len;
1564 break;
1565 }
1566
1567 if (last)
1568 str = NULL;
1569 else
1570 str = pos + 1;
1571 }
1572 }
1573
1574 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1575 struct perf_evsel *evsel __maybe_unused)
1576 {
1577 struct hpp_dynamic_entry *hde;
1578 size_t len = fmt->user_len;
1579
1580 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1581
1582 if (!len)
1583 len = hde_width(hde);
1584
1585 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1586 }
1587
1588 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1589 struct perf_hpp *hpp __maybe_unused,
1590 struct perf_evsel *evsel __maybe_unused)
1591 {
1592 struct hpp_dynamic_entry *hde;
1593 size_t len = fmt->user_len;
1594
1595 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1596
1597 if (!len)
1598 len = hde_width(hde);
1599
1600 return len;
1601 }
1602
1603 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1604 {
1605 struct hpp_dynamic_entry *hde;
1606
1607 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1608
1609 return hists_to_evsel(hists) == hde->evsel;
1610 }
1611
1612 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1613 struct hist_entry *he)
1614 {
1615 struct hpp_dynamic_entry *hde;
1616 size_t len = fmt->user_len;
1617 char *str, *pos;
1618 struct format_field *field;
1619 size_t namelen;
1620 bool last = false;
1621 int ret;
1622
1623 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1624
1625 if (!len)
1626 len = hde_width(hde);
1627
1628 if (hde->raw_trace)
1629 goto raw_field;
1630
1631 field = hde->field;
1632 namelen = strlen(field->name);
1633 str = he->trace_output;
1634
1635 while (str) {
1636 pos = strchr(str, ' ');
1637 if (pos == NULL) {
1638 last = true;
1639 pos = str + strlen(str);
1640 }
1641
1642 if (!strncmp(str, field->name, namelen)) {
1643 str += namelen + 1;
1644 str = strndup(str, pos - str);
1645
1646 if (str == NULL)
1647 return scnprintf(hpp->buf, hpp->size,
1648 "%*.*s", len, len, "ERROR");
1649 break;
1650 }
1651
1652 if (last)
1653 str = NULL;
1654 else
1655 str = pos + 1;
1656 }
1657
1658 if (str == NULL) {
1659 struct trace_seq seq;
1660 raw_field:
1661 trace_seq_init(&seq);
1662 pevent_print_field(&seq, he->raw_data, hde->field);
1663 str = seq.buffer;
1664 }
1665
1666 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1667 free(str);
1668 return ret;
1669 }
1670
1671 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1672 struct hist_entry *a, struct hist_entry *b)
1673 {
1674 struct hpp_dynamic_entry *hde;
1675 struct format_field *field;
1676 unsigned offset, size;
1677
1678 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1679
1680 field = hde->field;
1681 if (field->flags & FIELD_IS_DYNAMIC) {
1682 unsigned long long dyn;
1683
1684 pevent_read_number_field(field, a->raw_data, &dyn);
1685 offset = dyn & 0xffff;
1686 size = (dyn >> 16) & 0xffff;
1687
1688 /* record max width for output */
1689 if (size > hde->dynamic_len)
1690 hde->dynamic_len = size;
1691 } else {
1692 offset = field->offset;
1693 size = field->size;
1694
1695 update_dynamic_len(hde, a);
1696 update_dynamic_len(hde, b);
1697 }
1698
1699 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
1700 }
1701
1702 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
1703 {
1704 return fmt->cmp == __sort__hde_cmp;
1705 }
1706
1707 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1708 {
1709 struct hpp_dynamic_entry *hde_a;
1710 struct hpp_dynamic_entry *hde_b;
1711
1712 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
1713 return false;
1714
1715 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
1716 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
1717
1718 return hde_a->field == hde_b->field;
1719 }
1720
1721 static void hde_free(struct perf_hpp_fmt *fmt)
1722 {
1723 struct hpp_dynamic_entry *hde;
1724
1725 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1726 free(hde);
1727 }
1728
1729 static struct hpp_dynamic_entry *
1730 __alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field)
1731 {
1732 struct hpp_dynamic_entry *hde;
1733
1734 hde = malloc(sizeof(*hde));
1735 if (hde == NULL) {
1736 pr_debug("Memory allocation failed\n");
1737 return NULL;
1738 }
1739
1740 hde->evsel = evsel;
1741 hde->field = field;
1742 hde->dynamic_len = 0;
1743
1744 hde->hpp.name = field->name;
1745 hde->hpp.header = __sort__hde_header;
1746 hde->hpp.width = __sort__hde_width;
1747 hde->hpp.entry = __sort__hde_entry;
1748 hde->hpp.color = NULL;
1749
1750 hde->hpp.cmp = __sort__hde_cmp;
1751 hde->hpp.collapse = __sort__hde_cmp;
1752 hde->hpp.sort = __sort__hde_cmp;
1753 hde->hpp.equal = __sort__hde_equal;
1754 hde->hpp.free = hde_free;
1755
1756 INIT_LIST_HEAD(&hde->hpp.list);
1757 INIT_LIST_HEAD(&hde->hpp.sort_list);
1758 hde->hpp.elide = false;
1759 hde->hpp.len = 0;
1760 hde->hpp.user_len = 0;
1761
1762 return hde;
1763 }
1764
1765 static int parse_field_name(char *str, char **event, char **field, char **opt)
1766 {
1767 char *event_name, *field_name, *opt_name;
1768
1769 event_name = str;
1770 field_name = strchr(str, '.');
1771
1772 if (field_name) {
1773 *field_name++ = '\0';
1774 } else {
1775 event_name = NULL;
1776 field_name = str;
1777 }
1778
1779 opt_name = strchr(field_name, '/');
1780 if (opt_name)
1781 *opt_name++ = '\0';
1782
1783 *event = event_name;
1784 *field = field_name;
1785 *opt = opt_name;
1786
1787 return 0;
1788 }
1789
1790 /* find match evsel using a given event name. The event name can be:
1791 * 1. '%' + event index (e.g. '%1' for first event)
1792 * 2. full event name (e.g. sched:sched_switch)
1793 * 3. partial event name (should not contain ':')
1794 */
1795 static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
1796 {
1797 struct perf_evsel *evsel = NULL;
1798 struct perf_evsel *pos;
1799 bool full_name;
1800
1801 /* case 1 */
1802 if (event_name[0] == '%') {
1803 int nr = strtol(event_name+1, NULL, 0);
1804
1805 if (nr > evlist->nr_entries)
1806 return NULL;
1807
1808 evsel = perf_evlist__first(evlist);
1809 while (--nr > 0)
1810 evsel = perf_evsel__next(evsel);
1811
1812 return evsel;
1813 }
1814
1815 full_name = !!strchr(event_name, ':');
1816 evlist__for_each(evlist, pos) {
1817 /* case 2 */
1818 if (full_name && !strcmp(pos->name, event_name))
1819 return pos;
1820 /* case 3 */
1821 if (!full_name && strstr(pos->name, event_name)) {
1822 if (evsel) {
1823 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
1824 event_name, evsel->name, pos->name);
1825 return NULL;
1826 }
1827 evsel = pos;
1828 }
1829 }
1830
1831 return evsel;
1832 }
1833
1834 static int __dynamic_dimension__add(struct perf_evsel *evsel,
1835 struct format_field *field,
1836 bool raw_trace)
1837 {
1838 struct hpp_dynamic_entry *hde;
1839
1840 hde = __alloc_dynamic_entry(evsel, field);
1841 if (hde == NULL)
1842 return -ENOMEM;
1843
1844 hde->raw_trace = raw_trace;
1845
1846 perf_hpp__register_sort_field(&hde->hpp);
1847 return 0;
1848 }
1849
1850 static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace)
1851 {
1852 int ret;
1853 struct format_field *field;
1854
1855 field = evsel->tp_format->format.fields;
1856 while (field) {
1857 ret = __dynamic_dimension__add(evsel, field, raw_trace);
1858 if (ret < 0)
1859 return ret;
1860
1861 field = field->next;
1862 }
1863 return 0;
1864 }
1865
1866 static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace)
1867 {
1868 int ret;
1869 struct perf_evsel *evsel;
1870
1871 evlist__for_each(evlist, evsel) {
1872 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
1873 continue;
1874
1875 ret = add_evsel_fields(evsel, raw_trace);
1876 if (ret < 0)
1877 return ret;
1878 }
1879 return 0;
1880 }
1881
1882 static int add_all_matching_fields(struct perf_evlist *evlist,
1883 char *field_name, bool raw_trace)
1884 {
1885 int ret = -ESRCH;
1886 struct perf_evsel *evsel;
1887 struct format_field *field;
1888
1889 evlist__for_each(evlist, evsel) {
1890 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
1891 continue;
1892
1893 field = pevent_find_any_field(evsel->tp_format, field_name);
1894 if (field == NULL)
1895 continue;
1896
1897 ret = __dynamic_dimension__add(evsel, field, raw_trace);
1898 if (ret < 0)
1899 break;
1900 }
1901 return ret;
1902 }
1903
1904 static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok)
1905 {
1906 char *str, *event_name, *field_name, *opt_name;
1907 struct perf_evsel *evsel;
1908 struct format_field *field;
1909 bool raw_trace = symbol_conf.raw_trace;
1910 int ret = 0;
1911
1912 if (evlist == NULL)
1913 return -ENOENT;
1914
1915 str = strdup(tok);
1916 if (str == NULL)
1917 return -ENOMEM;
1918
1919 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
1920 ret = -EINVAL;
1921 goto out;
1922 }
1923
1924 if (opt_name) {
1925 if (strcmp(opt_name, "raw")) {
1926 pr_debug("unsupported field option %s\n", opt_name);
1927 ret = -EINVAL;
1928 goto out;
1929 }
1930 raw_trace = true;
1931 }
1932
1933 if (!strcmp(field_name, "trace_fields")) {
1934 ret = add_all_dynamic_fields(evlist, raw_trace);
1935 goto out;
1936 }
1937
1938 if (event_name == NULL) {
1939 ret = add_all_matching_fields(evlist, field_name, raw_trace);
1940 goto out;
1941 }
1942
1943 evsel = find_evsel(evlist, event_name);
1944 if (evsel == NULL) {
1945 pr_debug("Cannot find event: %s\n", event_name);
1946 ret = -ENOENT;
1947 goto out;
1948 }
1949
1950 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1951 pr_debug("%s is not a tracepoint event\n", event_name);
1952 ret = -EINVAL;
1953 goto out;
1954 }
1955
1956 if (!strcmp(field_name, "*")) {
1957 ret = add_evsel_fields(evsel, raw_trace);
1958 } else {
1959 field = pevent_find_any_field(evsel->tp_format, field_name);
1960 if (field == NULL) {
1961 pr_debug("Cannot find event field for %s.%s\n",
1962 event_name, field_name);
1963 return -ENOENT;
1964 }
1965
1966 ret = __dynamic_dimension__add(evsel, field, raw_trace);
1967 }
1968
1969 out:
1970 free(str);
1971 return ret;
1972 }
1973
1974 static int __sort_dimension__add(struct sort_dimension *sd)
1975 {
1976 if (sd->taken)
1977 return 0;
1978
1979 if (__sort_dimension__add_hpp_sort(sd) < 0)
1980 return -1;
1981
1982 if (sd->entry->se_collapse)
1983 sort__need_collapse = 1;
1984
1985 sd->taken = 1;
1986
1987 return 0;
1988 }
1989
1990 static int __hpp_dimension__add(struct hpp_dimension *hd)
1991 {
1992 struct perf_hpp_fmt *fmt;
1993
1994 if (hd->taken)
1995 return 0;
1996
1997 fmt = __hpp_dimension__alloc_hpp(hd);
1998 if (!fmt)
1999 return -1;
2000
2001 hd->taken = 1;
2002 perf_hpp__register_sort_field(fmt);
2003 return 0;
2004 }
2005
2006 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2007 struct sort_dimension *sd)
2008 {
2009 if (sd->taken)
2010 return 0;
2011
2012 if (__sort_dimension__add_hpp_output(list, sd) < 0)
2013 return -1;
2014
2015 sd->taken = 1;
2016 return 0;
2017 }
2018
2019 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2020 struct hpp_dimension *hd)
2021 {
2022 struct perf_hpp_fmt *fmt;
2023
2024 if (hd->taken)
2025 return 0;
2026
2027 fmt = __hpp_dimension__alloc_hpp(hd);
2028 if (!fmt)
2029 return -1;
2030
2031 hd->taken = 1;
2032 perf_hpp_list__column_register(list, fmt);
2033 return 0;
2034 }
2035
2036 int hpp_dimension__add_output(unsigned col)
2037 {
2038 BUG_ON(col >= PERF_HPP__MAX_INDEX);
2039 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2040 }
2041
2042 static int sort_dimension__add(const char *tok,
2043 struct perf_evlist *evlist __maybe_unused)
2044 {
2045 unsigned int i;
2046
2047 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2048 struct sort_dimension *sd = &common_sort_dimensions[i];
2049
2050 if (strncasecmp(tok, sd->name, strlen(tok)))
2051 continue;
2052
2053 if (sd->entry == &sort_parent) {
2054 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2055 if (ret) {
2056 char err[BUFSIZ];
2057
2058 regerror(ret, &parent_regex, err, sizeof(err));
2059 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2060 return -EINVAL;
2061 }
2062 sort__has_parent = 1;
2063 } else if (sd->entry == &sort_sym) {
2064 sort__has_sym = 1;
2065 /*
2066 * perf diff displays the performance difference amongst
2067 * two or more perf.data files. Those files could come
2068 * from different binaries. So we should not compare
2069 * their ips, but the name of symbol.
2070 */
2071 if (sort__mode == SORT_MODE__DIFF)
2072 sd->entry->se_collapse = sort__sym_sort;
2073
2074 } else if (sd->entry == &sort_dso) {
2075 sort__has_dso = 1;
2076 } else if (sd->entry == &sort_socket) {
2077 sort__has_socket = 1;
2078 } else if (sd->entry == &sort_thread) {
2079 sort__has_thread = 1;
2080 }
2081
2082 return __sort_dimension__add(sd);
2083 }
2084
2085 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2086 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2087
2088 if (strncasecmp(tok, hd->name, strlen(tok)))
2089 continue;
2090
2091 return __hpp_dimension__add(hd);
2092 }
2093
2094 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2095 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2096
2097 if (strncasecmp(tok, sd->name, strlen(tok)))
2098 continue;
2099
2100 if (sort__mode != SORT_MODE__BRANCH)
2101 return -EINVAL;
2102
2103 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2104 sort__has_sym = 1;
2105
2106 __sort_dimension__add(sd);
2107 return 0;
2108 }
2109
2110 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2111 struct sort_dimension *sd = &memory_sort_dimensions[i];
2112
2113 if (strncasecmp(tok, sd->name, strlen(tok)))
2114 continue;
2115
2116 if (sort__mode != SORT_MODE__MEMORY)
2117 return -EINVAL;
2118
2119 if (sd->entry == &sort_mem_daddr_sym)
2120 sort__has_sym = 1;
2121
2122 __sort_dimension__add(sd);
2123 return 0;
2124 }
2125
2126 if (!add_dynamic_entry(evlist, tok))
2127 return 0;
2128
2129 return -ESRCH;
2130 }
2131
2132 static int setup_sort_list(char *str, struct perf_evlist *evlist)
2133 {
2134 char *tmp, *tok;
2135 int ret = 0;
2136
2137 for (tok = strtok_r(str, ", ", &tmp);
2138 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2139 ret = sort_dimension__add(tok, evlist);
2140 if (ret == -EINVAL) {
2141 error("Invalid --sort key: `%s'", tok);
2142 break;
2143 } else if (ret == -ESRCH) {
2144 error("Unknown --sort key: `%s'", tok);
2145 break;
2146 }
2147 }
2148
2149 return ret;
2150 }
2151
2152 static const char *get_default_sort_order(struct perf_evlist *evlist)
2153 {
2154 const char *default_sort_orders[] = {
2155 default_sort_order,
2156 default_branch_sort_order,
2157 default_mem_sort_order,
2158 default_top_sort_order,
2159 default_diff_sort_order,
2160 default_tracepoint_sort_order,
2161 };
2162 bool use_trace = true;
2163 struct perf_evsel *evsel;
2164
2165 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2166
2167 if (evlist == NULL)
2168 goto out_no_evlist;
2169
2170 evlist__for_each(evlist, evsel) {
2171 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2172 use_trace = false;
2173 break;
2174 }
2175 }
2176
2177 if (use_trace) {
2178 sort__mode = SORT_MODE__TRACEPOINT;
2179 if (symbol_conf.raw_trace)
2180 return "trace_fields";
2181 }
2182 out_no_evlist:
2183 return default_sort_orders[sort__mode];
2184 }
2185
2186 static int setup_sort_order(struct perf_evlist *evlist)
2187 {
2188 char *new_sort_order;
2189
2190 /*
2191 * Append '+'-prefixed sort order to the default sort
2192 * order string.
2193 */
2194 if (!sort_order || is_strict_order(sort_order))
2195 return 0;
2196
2197 if (sort_order[1] == '\0') {
2198 error("Invalid --sort key: `+'");
2199 return -EINVAL;
2200 }
2201
2202 /*
2203 * We allocate new sort_order string, but we never free it,
2204 * because it's checked over the rest of the code.
2205 */
2206 if (asprintf(&new_sort_order, "%s,%s",
2207 get_default_sort_order(evlist), sort_order + 1) < 0) {
2208 error("Not enough memory to set up --sort");
2209 return -ENOMEM;
2210 }
2211
2212 sort_order = new_sort_order;
2213 return 0;
2214 }
2215
2216 /*
2217 * Adds 'pre,' prefix into 'str' is 'pre' is
2218 * not already part of 'str'.
2219 */
2220 static char *prefix_if_not_in(const char *pre, char *str)
2221 {
2222 char *n;
2223
2224 if (!str || strstr(str, pre))
2225 return str;
2226
2227 if (asprintf(&n, "%s,%s", pre, str) < 0)
2228 return NULL;
2229
2230 free(str);
2231 return n;
2232 }
2233
2234 static char *setup_overhead(char *keys)
2235 {
2236 keys = prefix_if_not_in("overhead", keys);
2237
2238 if (symbol_conf.cumulate_callchain)
2239 keys = prefix_if_not_in("overhead_children", keys);
2240
2241 return keys;
2242 }
2243
2244 static int __setup_sorting(struct perf_evlist *evlist)
2245 {
2246 char *str;
2247 const char *sort_keys;
2248 int ret = 0;
2249
2250 ret = setup_sort_order(evlist);
2251 if (ret)
2252 return ret;
2253
2254 sort_keys = sort_order;
2255 if (sort_keys == NULL) {
2256 if (is_strict_order(field_order)) {
2257 /*
2258 * If user specified field order but no sort order,
2259 * we'll honor it and not add default sort orders.
2260 */
2261 return 0;
2262 }
2263
2264 sort_keys = get_default_sort_order(evlist);
2265 }
2266
2267 str = strdup(sort_keys);
2268 if (str == NULL) {
2269 error("Not enough memory to setup sort keys");
2270 return -ENOMEM;
2271 }
2272
2273 /*
2274 * Prepend overhead fields for backward compatibility.
2275 */
2276 if (!is_strict_order(field_order)) {
2277 str = setup_overhead(str);
2278 if (str == NULL) {
2279 error("Not enough memory to setup overhead keys");
2280 return -ENOMEM;
2281 }
2282 }
2283
2284 ret = setup_sort_list(str, evlist);
2285
2286 free(str);
2287 return ret;
2288 }
2289
2290 void perf_hpp__set_elide(int idx, bool elide)
2291 {
2292 struct perf_hpp_fmt *fmt;
2293 struct hpp_sort_entry *hse;
2294
2295 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2296 if (!perf_hpp__is_sort_entry(fmt))
2297 continue;
2298
2299 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2300 if (hse->se->se_width_idx == idx) {
2301 fmt->elide = elide;
2302 break;
2303 }
2304 }
2305 }
2306
2307 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
2308 {
2309 if (list && strlist__nr_entries(list) == 1) {
2310 if (fp != NULL)
2311 fprintf(fp, "# %s: %s\n", list_name,
2312 strlist__entry(list, 0)->s);
2313 return true;
2314 }
2315 return false;
2316 }
2317
2318 static bool get_elide(int idx, FILE *output)
2319 {
2320 switch (idx) {
2321 case HISTC_SYMBOL:
2322 return __get_elide(symbol_conf.sym_list, "symbol", output);
2323 case HISTC_DSO:
2324 return __get_elide(symbol_conf.dso_list, "dso", output);
2325 case HISTC_COMM:
2326 return __get_elide(symbol_conf.comm_list, "comm", output);
2327 default:
2328 break;
2329 }
2330
2331 if (sort__mode != SORT_MODE__BRANCH)
2332 return false;
2333
2334 switch (idx) {
2335 case HISTC_SYMBOL_FROM:
2336 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2337 case HISTC_SYMBOL_TO:
2338 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2339 case HISTC_DSO_FROM:
2340 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2341 case HISTC_DSO_TO:
2342 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2343 default:
2344 break;
2345 }
2346
2347 return false;
2348 }
2349
2350 void sort__setup_elide(FILE *output)
2351 {
2352 struct perf_hpp_fmt *fmt;
2353 struct hpp_sort_entry *hse;
2354
2355 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2356 if (!perf_hpp__is_sort_entry(fmt))
2357 continue;
2358
2359 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2360 fmt->elide = get_elide(hse->se->se_width_idx, output);
2361 }
2362
2363 /*
2364 * It makes no sense to elide all of sort entries.
2365 * Just revert them to show up again.
2366 */
2367 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2368 if (!perf_hpp__is_sort_entry(fmt))
2369 continue;
2370
2371 if (!fmt->elide)
2372 return;
2373 }
2374
2375 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2376 if (!perf_hpp__is_sort_entry(fmt))
2377 continue;
2378
2379 fmt->elide = false;
2380 }
2381 }
2382
2383 static int output_field_add(struct perf_hpp_list *list, char *tok)
2384 {
2385 unsigned int i;
2386
2387 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2388 struct sort_dimension *sd = &common_sort_dimensions[i];
2389
2390 if (strncasecmp(tok, sd->name, strlen(tok)))
2391 continue;
2392
2393 return __sort_dimension__add_output(list, sd);
2394 }
2395
2396 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2397 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2398
2399 if (strncasecmp(tok, hd->name, strlen(tok)))
2400 continue;
2401
2402 return __hpp_dimension__add_output(list, hd);
2403 }
2404
2405 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2406 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2407
2408 if (strncasecmp(tok, sd->name, strlen(tok)))
2409 continue;
2410
2411 return __sort_dimension__add_output(list, sd);
2412 }
2413
2414 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2415 struct sort_dimension *sd = &memory_sort_dimensions[i];
2416
2417 if (strncasecmp(tok, sd->name, strlen(tok)))
2418 continue;
2419
2420 return __sort_dimension__add_output(list, sd);
2421 }
2422
2423 return -ESRCH;
2424 }
2425
2426 static int setup_output_list(struct perf_hpp_list *list, char *str)
2427 {
2428 char *tmp, *tok;
2429 int ret = 0;
2430
2431 for (tok = strtok_r(str, ", ", &tmp);
2432 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2433 ret = output_field_add(list, tok);
2434 if (ret == -EINVAL) {
2435 error("Invalid --fields key: `%s'", tok);
2436 break;
2437 } else if (ret == -ESRCH) {
2438 error("Unknown --fields key: `%s'", tok);
2439 break;
2440 }
2441 }
2442
2443 return ret;
2444 }
2445
2446 static void reset_dimensions(void)
2447 {
2448 unsigned int i;
2449
2450 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2451 common_sort_dimensions[i].taken = 0;
2452
2453 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2454 hpp_sort_dimensions[i].taken = 0;
2455
2456 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2457 bstack_sort_dimensions[i].taken = 0;
2458
2459 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2460 memory_sort_dimensions[i].taken = 0;
2461 }
2462
2463 bool is_strict_order(const char *order)
2464 {
2465 return order && (*order != '+');
2466 }
2467
2468 static int __setup_output_field(void)
2469 {
2470 char *str, *strp;
2471 int ret = -EINVAL;
2472
2473 if (field_order == NULL)
2474 return 0;
2475
2476 strp = str = strdup(field_order);
2477 if (str == NULL) {
2478 error("Not enough memory to setup output fields");
2479 return -ENOMEM;
2480 }
2481
2482 if (!is_strict_order(field_order))
2483 strp++;
2484
2485 if (!strlen(strp)) {
2486 error("Invalid --fields key: `+'");
2487 goto out;
2488 }
2489
2490 ret = setup_output_list(&perf_hpp_list, strp);
2491
2492 out:
2493 free(str);
2494 return ret;
2495 }
2496
2497 int setup_sorting(struct perf_evlist *evlist)
2498 {
2499 int err;
2500
2501 err = __setup_sorting(evlist);
2502 if (err < 0)
2503 return err;
2504
2505 if (parent_pattern != default_parent_pattern) {
2506 err = sort_dimension__add("parent", evlist);
2507 if (err < 0)
2508 return err;
2509 }
2510
2511 reset_dimensions();
2512
2513 /*
2514 * perf diff doesn't use default hpp output fields.
2515 */
2516 if (sort__mode != SORT_MODE__DIFF)
2517 perf_hpp__init();
2518
2519 err = __setup_output_field();
2520 if (err < 0)
2521 return err;
2522
2523 /* copy sort keys to output fields */
2524 perf_hpp__setup_output_field(&perf_hpp_list);
2525 /* and then copy output fields to sort keys */
2526 perf_hpp__append_sort_keys(&perf_hpp_list);
2527
2528 return 0;
2529 }
2530
2531 void reset_output_field(void)
2532 {
2533 sort__need_collapse = 0;
2534 sort__has_parent = 0;
2535 sort__has_sym = 0;
2536 sort__has_dso = 0;
2537
2538 field_order = NULL;
2539 sort_order = NULL;
2540
2541 reset_dimensions();
2542 perf_hpp__reset_output_field(&perf_hpp_list);
2543 }
This page took 0.117911 seconds and 5 git commands to generate.