treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / perf / util / sort.c
blobab0cfd790ad00dddbf06a5e6b0baafe573614f9a
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <regex.h>
5 #include <stdlib.h>
6 #include <linux/mman.h>
7 #include <linux/time64.h>
8 #include "debug.h"
9 #include "dso.h"
10 #include "sort.h"
11 #include "hist.h"
12 #include "cacheline.h"
13 #include "comm.h"
14 #include "map.h"
15 #include "symbol.h"
16 #include "map_symbol.h"
17 #include "branch.h"
18 #include "thread.h"
19 #include "evsel.h"
20 #include "evlist.h"
21 #include "srcline.h"
22 #include "strlist.h"
23 #include "strbuf.h"
24 #include <traceevent/event-parse.h>
25 #include "mem-events.h"
26 #include "annotate.h"
27 #include "time-utils.h"
28 #include <linux/kernel.h>
29 #include <linux/string.h>
31 regex_t parent_regex;
32 const char default_parent_pattern[] = "^sys_|^do_page_fault";
33 const char *parent_pattern = default_parent_pattern;
34 const char *default_sort_order = "comm,dso,symbol";
35 const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
36 const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
37 const char default_top_sort_order[] = "dso,symbol";
38 const char default_diff_sort_order[] = "dso,symbol";
39 const char default_tracepoint_sort_order[] = "trace";
40 const char *sort_order;
41 const char *field_order;
42 regex_t ignore_callees_regex;
43 int have_ignore_callees = 0;
44 enum sort_mode sort__mode = SORT_MODE__NORMAL;
47 * Replaces all occurrences of a char used with the:
49 * -t, --field-separator
51 * option, that uses a special separator character and don't pad with spaces,
52 * replacing all occurrences of this separator in symbol names (and other
53 * output) with a '.' character, that thus it's the only non valid separator.
55 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
57 int n;
58 va_list ap;
60 va_start(ap, fmt);
61 n = vsnprintf(bf, size, fmt, ap);
62 if (symbol_conf.field_sep && n > 0) {
63 char *sep = bf;
65 while (1) {
66 sep = strchr(sep, *symbol_conf.field_sep);
67 if (sep == NULL)
68 break;
69 *sep = '.';
72 va_end(ap);
74 if (n >= (int)size)
75 return size - 1;
76 return n;
79 static int64_t cmp_null(const void *l, const void *r)
81 if (!l && !r)
82 return 0;
83 else if (!l)
84 return -1;
85 else
86 return 1;
89 /* --sort pid */
91 static int64_t
92 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
94 return right->thread->tid - left->thread->tid;
97 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
98 size_t size, unsigned int width)
100 const char *comm = thread__comm_str(he->thread);
102 width = max(7U, width) - 8;
103 return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid,
104 width, width, comm ?: "");
107 static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
109 const struct thread *th = arg;
111 if (type != HIST_FILTER__THREAD)
112 return -1;
114 return th && he->thread != th;
117 struct sort_entry sort_thread = {
118 .se_header = " Pid:Command",
119 .se_cmp = sort__thread_cmp,
120 .se_snprintf = hist_entry__thread_snprintf,
121 .se_filter = hist_entry__thread_filter,
122 .se_width_idx = HISTC_THREAD,
125 /* --sort comm */
128 * We can't use pointer comparison in functions below,
129 * because it gives different results based on pointer
130 * values, which could break some sorting assumptions.
132 static int64_t
133 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
135 return strcmp(comm__str(right->comm), comm__str(left->comm));
138 static int64_t
139 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
141 return strcmp(comm__str(right->comm), comm__str(left->comm));
144 static int64_t
145 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
147 return strcmp(comm__str(right->comm), comm__str(left->comm));
150 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
151 size_t size, unsigned int width)
153 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
156 struct sort_entry sort_comm = {
157 .se_header = "Command",
158 .se_cmp = sort__comm_cmp,
159 .se_collapse = sort__comm_collapse,
160 .se_sort = sort__comm_sort,
161 .se_snprintf = hist_entry__comm_snprintf,
162 .se_filter = hist_entry__thread_filter,
163 .se_width_idx = HISTC_COMM,
166 /* --sort dso */
168 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
170 struct dso *dso_l = map_l ? map_l->dso : NULL;
171 struct dso *dso_r = map_r ? map_r->dso : NULL;
172 const char *dso_name_l, *dso_name_r;
174 if (!dso_l || !dso_r)
175 return cmp_null(dso_r, dso_l);
177 if (verbose > 0) {
178 dso_name_l = dso_l->long_name;
179 dso_name_r = dso_r->long_name;
180 } else {
181 dso_name_l = dso_l->short_name;
182 dso_name_r = dso_r->short_name;
185 return strcmp(dso_name_l, dso_name_r);
188 static int64_t
189 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
191 return _sort__dso_cmp(right->ms.map, left->ms.map);
194 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
195 size_t size, unsigned int width)
197 if (map && map->dso) {
198 const char *dso_name = verbose > 0 ? map->dso->long_name :
199 map->dso->short_name;
200 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
203 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
206 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
207 size_t size, unsigned int width)
209 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
212 static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
214 const struct dso *dso = arg;
216 if (type != HIST_FILTER__DSO)
217 return -1;
219 return dso && (!he->ms.map || he->ms.map->dso != dso);
222 struct sort_entry sort_dso = {
223 .se_header = "Shared Object",
224 .se_cmp = sort__dso_cmp,
225 .se_snprintf = hist_entry__dso_snprintf,
226 .se_filter = hist_entry__dso_filter,
227 .se_width_idx = HISTC_DSO,
230 /* --sort symbol */
232 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
234 return (int64_t)(right_ip - left_ip);
237 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
239 if (!sym_l || !sym_r)
240 return cmp_null(sym_l, sym_r);
242 if (sym_l == sym_r)
243 return 0;
245 if (sym_l->inlined || sym_r->inlined) {
246 int ret = strcmp(sym_l->name, sym_r->name);
248 if (ret)
249 return ret;
250 if ((sym_l->start <= sym_r->end) && (sym_l->end >= sym_r->start))
251 return 0;
254 if (sym_l->start != sym_r->start)
255 return (int64_t)(sym_r->start - sym_l->start);
257 return (int64_t)(sym_r->end - sym_l->end);
260 static int64_t
261 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
263 int64_t ret;
265 if (!left->ms.sym && !right->ms.sym)
266 return _sort__addr_cmp(left->ip, right->ip);
269 * comparing symbol address alone is not enough since it's a
270 * relative address within a dso.
272 if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
273 ret = sort__dso_cmp(left, right);
274 if (ret != 0)
275 return ret;
278 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
281 static int64_t
282 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
284 if (!left->ms.sym || !right->ms.sym)
285 return cmp_null(left->ms.sym, right->ms.sym);
287 return strcmp(right->ms.sym->name, left->ms.sym->name);
290 static int _hist_entry__sym_snprintf(struct map_symbol *ms,
291 u64 ip, char level, char *bf, size_t size,
292 unsigned int width)
294 struct symbol *sym = ms->sym;
295 struct map *map = ms->map;
296 size_t ret = 0;
298 if (verbose > 0) {
299 char o = map ? dso__symtab_origin(map->dso) : '!';
300 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
301 BITS_PER_LONG / 4 + 2, ip, o);
304 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
305 if (sym && map) {
306 if (sym->type == STT_OBJECT) {
307 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
308 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
309 ip - map->unmap_ip(map, sym->start));
310 } else {
311 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
312 width - ret,
313 sym->name);
314 if (sym->inlined)
315 ret += repsep_snprintf(bf + ret, size - ret,
316 " (inlined)");
318 } else {
319 size_t len = BITS_PER_LONG / 4;
320 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
321 len, ip);
324 return ret;
327 int hist_entry__sym_snprintf(struct hist_entry *he, char *bf, size_t size, unsigned int width)
329 return _hist_entry__sym_snprintf(&he->ms, he->ip,
330 he->level, bf, size, width);
333 static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
335 const char *sym = arg;
337 if (type != HIST_FILTER__SYMBOL)
338 return -1;
340 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
343 struct sort_entry sort_sym = {
344 .se_header = "Symbol",
345 .se_cmp = sort__sym_cmp,
346 .se_sort = sort__sym_sort,
347 .se_snprintf = hist_entry__sym_snprintf,
348 .se_filter = hist_entry__sym_filter,
349 .se_width_idx = HISTC_SYMBOL,
352 /* --sort srcline */
354 char *hist_entry__srcline(struct hist_entry *he)
356 return map__srcline(he->ms.map, he->ip, he->ms.sym);
359 static int64_t
360 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
362 if (!left->srcline)
363 left->srcline = hist_entry__srcline(left);
364 if (!right->srcline)
365 right->srcline = hist_entry__srcline(right);
367 return strcmp(right->srcline, left->srcline);
370 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
371 size_t size, unsigned int width)
373 if (!he->srcline)
374 he->srcline = hist_entry__srcline(he);
376 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
379 struct sort_entry sort_srcline = {
380 .se_header = "Source:Line",
381 .se_cmp = sort__srcline_cmp,
382 .se_snprintf = hist_entry__srcline_snprintf,
383 .se_width_idx = HISTC_SRCLINE,
386 /* --sort srcline_from */
388 static char *addr_map_symbol__srcline(struct addr_map_symbol *ams)
390 return map__srcline(ams->ms.map, ams->al_addr, ams->ms.sym);
393 static int64_t
394 sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
396 if (!left->branch_info->srcline_from)
397 left->branch_info->srcline_from = addr_map_symbol__srcline(&left->branch_info->from);
399 if (!right->branch_info->srcline_from)
400 right->branch_info->srcline_from = addr_map_symbol__srcline(&right->branch_info->from);
402 return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
405 static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
406 size_t size, unsigned int width)
408 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
411 struct sort_entry sort_srcline_from = {
412 .se_header = "From Source:Line",
413 .se_cmp = sort__srcline_from_cmp,
414 .se_snprintf = hist_entry__srcline_from_snprintf,
415 .se_width_idx = HISTC_SRCLINE_FROM,
418 /* --sort srcline_to */
420 static int64_t
421 sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
423 if (!left->branch_info->srcline_to)
424 left->branch_info->srcline_to = addr_map_symbol__srcline(&left->branch_info->to);
426 if (!right->branch_info->srcline_to)
427 right->branch_info->srcline_to = addr_map_symbol__srcline(&right->branch_info->to);
429 return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
432 static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
433 size_t size, unsigned int width)
435 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
438 struct sort_entry sort_srcline_to = {
439 .se_header = "To Source:Line",
440 .se_cmp = sort__srcline_to_cmp,
441 .se_snprintf = hist_entry__srcline_to_snprintf,
442 .se_width_idx = HISTC_SRCLINE_TO,
445 static int hist_entry__sym_ipc_snprintf(struct hist_entry *he, char *bf,
446 size_t size, unsigned int width)
449 struct symbol *sym = he->ms.sym;
450 struct annotation *notes;
451 double ipc = 0.0, coverage = 0.0;
452 char tmp[64];
454 if (!sym)
455 return repsep_snprintf(bf, size, "%-*s", width, "-");
457 notes = symbol__annotation(sym);
459 if (notes->hit_cycles)
460 ipc = notes->hit_insn / ((double)notes->hit_cycles);
462 if (notes->total_insn) {
463 coverage = notes->cover_insn * 100.0 /
464 ((double)notes->total_insn);
467 snprintf(tmp, sizeof(tmp), "%-5.2f [%5.1f%%]", ipc, coverage);
468 return repsep_snprintf(bf, size, "%-*s", width, tmp);
471 struct sort_entry sort_sym_ipc = {
472 .se_header = "IPC [IPC Coverage]",
473 .se_cmp = sort__sym_cmp,
474 .se_snprintf = hist_entry__sym_ipc_snprintf,
475 .se_width_idx = HISTC_SYMBOL_IPC,
478 static int hist_entry__sym_ipc_null_snprintf(struct hist_entry *he
479 __maybe_unused,
480 char *bf, size_t size,
481 unsigned int width)
483 char tmp[64];
485 snprintf(tmp, sizeof(tmp), "%-5s %2s", "-", "-");
486 return repsep_snprintf(bf, size, "%-*s", width, tmp);
489 struct sort_entry sort_sym_ipc_null = {
490 .se_header = "IPC [IPC Coverage]",
491 .se_cmp = sort__sym_cmp,
492 .se_snprintf = hist_entry__sym_ipc_null_snprintf,
493 .se_width_idx = HISTC_SYMBOL_IPC,
496 /* --sort srcfile */
498 static char no_srcfile[1];
500 static char *hist_entry__get_srcfile(struct hist_entry *e)
502 char *sf, *p;
503 struct map *map = e->ms.map;
505 if (!map)
506 return no_srcfile;
508 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
509 e->ms.sym, false, true, true, e->ip);
510 if (!strcmp(sf, SRCLINE_UNKNOWN))
511 return no_srcfile;
512 p = strchr(sf, ':');
513 if (p && *sf) {
514 *p = 0;
515 return sf;
517 free(sf);
518 return no_srcfile;
521 static int64_t
522 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
524 if (!left->srcfile)
525 left->srcfile = hist_entry__get_srcfile(left);
526 if (!right->srcfile)
527 right->srcfile = hist_entry__get_srcfile(right);
529 return strcmp(right->srcfile, left->srcfile);
532 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
533 size_t size, unsigned int width)
535 if (!he->srcfile)
536 he->srcfile = hist_entry__get_srcfile(he);
538 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
541 struct sort_entry sort_srcfile = {
542 .se_header = "Source File",
543 .se_cmp = sort__srcfile_cmp,
544 .se_snprintf = hist_entry__srcfile_snprintf,
545 .se_width_idx = HISTC_SRCFILE,
548 /* --sort parent */
550 static int64_t
551 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
553 struct symbol *sym_l = left->parent;
554 struct symbol *sym_r = right->parent;
556 if (!sym_l || !sym_r)
557 return cmp_null(sym_l, sym_r);
559 return strcmp(sym_r->name, sym_l->name);
562 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
563 size_t size, unsigned int width)
565 return repsep_snprintf(bf, size, "%-*.*s", width, width,
566 he->parent ? he->parent->name : "[other]");
569 struct sort_entry sort_parent = {
570 .se_header = "Parent symbol",
571 .se_cmp = sort__parent_cmp,
572 .se_snprintf = hist_entry__parent_snprintf,
573 .se_width_idx = HISTC_PARENT,
576 /* --sort cpu */
578 static int64_t
579 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
581 return right->cpu - left->cpu;
584 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
585 size_t size, unsigned int width)
587 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
590 struct sort_entry sort_cpu = {
591 .se_header = "CPU",
592 .se_cmp = sort__cpu_cmp,
593 .se_snprintf = hist_entry__cpu_snprintf,
594 .se_width_idx = HISTC_CPU,
597 /* --sort cgroup_id */
599 static int64_t _sort__cgroup_dev_cmp(u64 left_dev, u64 right_dev)
601 return (int64_t)(right_dev - left_dev);
604 static int64_t _sort__cgroup_inode_cmp(u64 left_ino, u64 right_ino)
606 return (int64_t)(right_ino - left_ino);
609 static int64_t
610 sort__cgroup_id_cmp(struct hist_entry *left, struct hist_entry *right)
612 int64_t ret;
614 ret = _sort__cgroup_dev_cmp(right->cgroup_id.dev, left->cgroup_id.dev);
615 if (ret != 0)
616 return ret;
618 return _sort__cgroup_inode_cmp(right->cgroup_id.ino,
619 left->cgroup_id.ino);
622 static int hist_entry__cgroup_id_snprintf(struct hist_entry *he,
623 char *bf, size_t size,
624 unsigned int width __maybe_unused)
626 return repsep_snprintf(bf, size, "%lu/0x%lx", he->cgroup_id.dev,
627 he->cgroup_id.ino);
630 struct sort_entry sort_cgroup_id = {
631 .se_header = "cgroup id (dev/inode)",
632 .se_cmp = sort__cgroup_id_cmp,
633 .se_snprintf = hist_entry__cgroup_id_snprintf,
634 .se_width_idx = HISTC_CGROUP_ID,
637 /* --sort socket */
639 static int64_t
640 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
642 return right->socket - left->socket;
645 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
646 size_t size, unsigned int width)
648 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
651 static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
653 int sk = *(const int *)arg;
655 if (type != HIST_FILTER__SOCKET)
656 return -1;
658 return sk >= 0 && he->socket != sk;
661 struct sort_entry sort_socket = {
662 .se_header = "Socket",
663 .se_cmp = sort__socket_cmp,
664 .se_snprintf = hist_entry__socket_snprintf,
665 .se_filter = hist_entry__socket_filter,
666 .se_width_idx = HISTC_SOCKET,
669 /* --sort time */
671 static int64_t
672 sort__time_cmp(struct hist_entry *left, struct hist_entry *right)
674 return right->time - left->time;
677 static int hist_entry__time_snprintf(struct hist_entry *he, char *bf,
678 size_t size, unsigned int width)
680 char he_time[32];
682 if (symbol_conf.nanosecs)
683 timestamp__scnprintf_nsec(he->time, he_time,
684 sizeof(he_time));
685 else
686 timestamp__scnprintf_usec(he->time, he_time,
687 sizeof(he_time));
689 return repsep_snprintf(bf, size, "%-.*s", width, he_time);
692 struct sort_entry sort_time = {
693 .se_header = "Time",
694 .se_cmp = sort__time_cmp,
695 .se_snprintf = hist_entry__time_snprintf,
696 .se_width_idx = HISTC_TIME,
699 /* --sort trace */
701 static char *get_trace_output(struct hist_entry *he)
703 struct trace_seq seq;
704 struct evsel *evsel;
705 struct tep_record rec = {
706 .data = he->raw_data,
707 .size = he->raw_size,
710 evsel = hists_to_evsel(he->hists);
712 trace_seq_init(&seq);
713 if (symbol_conf.raw_trace) {
714 tep_print_fields(&seq, he->raw_data, he->raw_size,
715 evsel->tp_format);
716 } else {
717 tep_print_event(evsel->tp_format->tep,
718 &seq, &rec, "%s", TEP_PRINT_INFO);
721 * Trim the buffer, it starts at 4KB and we're not going to
722 * add anything more to this buffer.
724 return realloc(seq.buffer, seq.len + 1);
727 static int64_t
728 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
730 struct evsel *evsel;
732 evsel = hists_to_evsel(left->hists);
733 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
734 return 0;
736 if (left->trace_output == NULL)
737 left->trace_output = get_trace_output(left);
738 if (right->trace_output == NULL)
739 right->trace_output = get_trace_output(right);
741 return strcmp(right->trace_output, left->trace_output);
744 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
745 size_t size, unsigned int width)
747 struct evsel *evsel;
749 evsel = hists_to_evsel(he->hists);
750 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
751 return scnprintf(bf, size, "%-.*s", width, "N/A");
753 if (he->trace_output == NULL)
754 he->trace_output = get_trace_output(he);
755 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
758 struct sort_entry sort_trace = {
759 .se_header = "Trace output",
760 .se_cmp = sort__trace_cmp,
761 .se_snprintf = hist_entry__trace_snprintf,
762 .se_width_idx = HISTC_TRACE,
765 /* sort keys for branch stacks */
767 static int64_t
768 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
770 if (!left->branch_info || !right->branch_info)
771 return cmp_null(left->branch_info, right->branch_info);
773 return _sort__dso_cmp(left->branch_info->from.ms.map,
774 right->branch_info->from.ms.map);
777 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
778 size_t size, unsigned int width)
780 if (he->branch_info)
781 return _hist_entry__dso_snprintf(he->branch_info->from.ms.map,
782 bf, size, width);
783 else
784 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
787 static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
788 const void *arg)
790 const struct dso *dso = arg;
792 if (type != HIST_FILTER__DSO)
793 return -1;
795 return dso && (!he->branch_info || !he->branch_info->from.ms.map ||
796 he->branch_info->from.ms.map->dso != dso);
799 static int64_t
800 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
802 if (!left->branch_info || !right->branch_info)
803 return cmp_null(left->branch_info, right->branch_info);
805 return _sort__dso_cmp(left->branch_info->to.ms.map,
806 right->branch_info->to.ms.map);
809 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
810 size_t size, unsigned int width)
812 if (he->branch_info)
813 return _hist_entry__dso_snprintf(he->branch_info->to.ms.map,
814 bf, size, width);
815 else
816 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
819 static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
820 const void *arg)
822 const struct dso *dso = arg;
824 if (type != HIST_FILTER__DSO)
825 return -1;
827 return dso && (!he->branch_info || !he->branch_info->to.ms.map ||
828 he->branch_info->to.ms.map->dso != dso);
831 static int64_t
832 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
834 struct addr_map_symbol *from_l = &left->branch_info->from;
835 struct addr_map_symbol *from_r = &right->branch_info->from;
837 if (!left->branch_info || !right->branch_info)
838 return cmp_null(left->branch_info, right->branch_info);
840 from_l = &left->branch_info->from;
841 from_r = &right->branch_info->from;
843 if (!from_l->ms.sym && !from_r->ms.sym)
844 return _sort__addr_cmp(from_l->addr, from_r->addr);
846 return _sort__sym_cmp(from_l->ms.sym, from_r->ms.sym);
849 static int64_t
850 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
852 struct addr_map_symbol *to_l, *to_r;
854 if (!left->branch_info || !right->branch_info)
855 return cmp_null(left->branch_info, right->branch_info);
857 to_l = &left->branch_info->to;
858 to_r = &right->branch_info->to;
860 if (!to_l->ms.sym && !to_r->ms.sym)
861 return _sort__addr_cmp(to_l->addr, to_r->addr);
863 return _sort__sym_cmp(to_l->ms.sym, to_r->ms.sym);
866 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
867 size_t size, unsigned int width)
869 if (he->branch_info) {
870 struct addr_map_symbol *from = &he->branch_info->from;
872 return _hist_entry__sym_snprintf(&from->ms, from->addr, he->level, bf, size, width);
875 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
878 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
879 size_t size, unsigned int width)
881 if (he->branch_info) {
882 struct addr_map_symbol *to = &he->branch_info->to;
884 return _hist_entry__sym_snprintf(&to->ms, to->addr, he->level, bf, size, width);
887 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
890 static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
891 const void *arg)
893 const char *sym = arg;
895 if (type != HIST_FILTER__SYMBOL)
896 return -1;
898 return sym && !(he->branch_info && he->branch_info->from.ms.sym &&
899 strstr(he->branch_info->from.ms.sym->name, sym));
902 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
903 const void *arg)
905 const char *sym = arg;
907 if (type != HIST_FILTER__SYMBOL)
908 return -1;
910 return sym && !(he->branch_info && he->branch_info->to.ms.sym &&
911 strstr(he->branch_info->to.ms.sym->name, sym));
914 struct sort_entry sort_dso_from = {
915 .se_header = "Source Shared Object",
916 .se_cmp = sort__dso_from_cmp,
917 .se_snprintf = hist_entry__dso_from_snprintf,
918 .se_filter = hist_entry__dso_from_filter,
919 .se_width_idx = HISTC_DSO_FROM,
922 struct sort_entry sort_dso_to = {
923 .se_header = "Target Shared Object",
924 .se_cmp = sort__dso_to_cmp,
925 .se_snprintf = hist_entry__dso_to_snprintf,
926 .se_filter = hist_entry__dso_to_filter,
927 .se_width_idx = HISTC_DSO_TO,
930 struct sort_entry sort_sym_from = {
931 .se_header = "Source Symbol",
932 .se_cmp = sort__sym_from_cmp,
933 .se_snprintf = hist_entry__sym_from_snprintf,
934 .se_filter = hist_entry__sym_from_filter,
935 .se_width_idx = HISTC_SYMBOL_FROM,
938 struct sort_entry sort_sym_to = {
939 .se_header = "Target Symbol",
940 .se_cmp = sort__sym_to_cmp,
941 .se_snprintf = hist_entry__sym_to_snprintf,
942 .se_filter = hist_entry__sym_to_filter,
943 .se_width_idx = HISTC_SYMBOL_TO,
946 static int64_t
947 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
949 unsigned char mp, p;
951 if (!left->branch_info || !right->branch_info)
952 return cmp_null(left->branch_info, right->branch_info);
954 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
955 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
956 return mp || p;
959 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
960 size_t size, unsigned int width){
961 static const char *out = "N/A";
963 if (he->branch_info) {
964 if (he->branch_info->flags.predicted)
965 out = "N";
966 else if (he->branch_info->flags.mispred)
967 out = "Y";
970 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
973 static int64_t
974 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
976 if (!left->branch_info || !right->branch_info)
977 return cmp_null(left->branch_info, right->branch_info);
979 return left->branch_info->flags.cycles -
980 right->branch_info->flags.cycles;
983 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
984 size_t size, unsigned int width)
986 if (!he->branch_info)
987 return scnprintf(bf, size, "%-.*s", width, "N/A");
988 if (he->branch_info->flags.cycles == 0)
989 return repsep_snprintf(bf, size, "%-*s", width, "-");
990 return repsep_snprintf(bf, size, "%-*hd", width,
991 he->branch_info->flags.cycles);
994 struct sort_entry sort_cycles = {
995 .se_header = "Basic Block Cycles",
996 .se_cmp = sort__cycles_cmp,
997 .se_snprintf = hist_entry__cycles_snprintf,
998 .se_width_idx = HISTC_CYCLES,
1001 /* --sort daddr_sym */
1002 int64_t
1003 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1005 uint64_t l = 0, r = 0;
1007 if (left->mem_info)
1008 l = left->mem_info->daddr.addr;
1009 if (right->mem_info)
1010 r = right->mem_info->daddr.addr;
1012 return (int64_t)(r - l);
1015 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
1016 size_t size, unsigned int width)
1018 uint64_t addr = 0;
1019 struct map_symbol *ms = NULL;
1021 if (he->mem_info) {
1022 addr = he->mem_info->daddr.addr;
1023 ms = &he->mem_info->daddr.ms;
1025 return _hist_entry__sym_snprintf(ms, addr, he->level, bf, size, width);
1028 int64_t
1029 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
1031 uint64_t l = 0, r = 0;
1033 if (left->mem_info)
1034 l = left->mem_info->iaddr.addr;
1035 if (right->mem_info)
1036 r = right->mem_info->iaddr.addr;
1038 return (int64_t)(r - l);
1041 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
1042 size_t size, unsigned int width)
1044 uint64_t addr = 0;
1045 struct map_symbol *ms = NULL;
1047 if (he->mem_info) {
1048 addr = he->mem_info->iaddr.addr;
1049 ms = &he->mem_info->iaddr.ms;
1051 return _hist_entry__sym_snprintf(ms, addr, he->level, bf, size, width);
1054 static int64_t
1055 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1057 struct map *map_l = NULL;
1058 struct map *map_r = NULL;
1060 if (left->mem_info)
1061 map_l = left->mem_info->daddr.ms.map;
1062 if (right->mem_info)
1063 map_r = right->mem_info->daddr.ms.map;
1065 return _sort__dso_cmp(map_l, map_r);
1068 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
1069 size_t size, unsigned int width)
1071 struct map *map = NULL;
1073 if (he->mem_info)
1074 map = he->mem_info->daddr.ms.map;
1076 return _hist_entry__dso_snprintf(map, bf, size, width);
1079 static int64_t
1080 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
1082 union perf_mem_data_src data_src_l;
1083 union perf_mem_data_src data_src_r;
1085 if (left->mem_info)
1086 data_src_l = left->mem_info->data_src;
1087 else
1088 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
1090 if (right->mem_info)
1091 data_src_r = right->mem_info->data_src;
1092 else
1093 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
1095 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
1098 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
1099 size_t size, unsigned int width)
1101 char out[10];
1103 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
1104 return repsep_snprintf(bf, size, "%.*s", width, out);
1107 static int64_t
1108 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
1110 union perf_mem_data_src data_src_l;
1111 union perf_mem_data_src data_src_r;
1113 if (left->mem_info)
1114 data_src_l = left->mem_info->data_src;
1115 else
1116 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
1118 if (right->mem_info)
1119 data_src_r = right->mem_info->data_src;
1120 else
1121 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
1123 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
1126 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
1127 size_t size, unsigned int width)
1129 char out[64];
1131 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
1132 return repsep_snprintf(bf, size, "%-*s", width, out);
1135 static int64_t
1136 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
1138 union perf_mem_data_src data_src_l;
1139 union perf_mem_data_src data_src_r;
1141 if (left->mem_info)
1142 data_src_l = left->mem_info->data_src;
1143 else
1144 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
1146 if (right->mem_info)
1147 data_src_r = right->mem_info->data_src;
1148 else
1149 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
1151 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
1154 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
1155 size_t size, unsigned int width)
1157 char out[64];
1159 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
1160 return repsep_snprintf(bf, size, "%-*s", width, out);
1163 static int64_t
1164 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
1166 union perf_mem_data_src data_src_l;
1167 union perf_mem_data_src data_src_r;
1169 if (left->mem_info)
1170 data_src_l = left->mem_info->data_src;
1171 else
1172 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
1174 if (right->mem_info)
1175 data_src_r = right->mem_info->data_src;
1176 else
1177 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
1179 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
1182 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
1183 size_t size, unsigned int width)
1185 char out[64];
1187 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
1188 return repsep_snprintf(bf, size, "%-*s", width, out);
1191 int64_t
1192 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
1194 u64 l, r;
1195 struct map *l_map, *r_map;
1196 int rc;
1198 if (!left->mem_info) return -1;
1199 if (!right->mem_info) return 1;
1201 /* group event types together */
1202 if (left->cpumode > right->cpumode) return -1;
1203 if (left->cpumode < right->cpumode) return 1;
1205 l_map = left->mem_info->daddr.ms.map;
1206 r_map = right->mem_info->daddr.ms.map;
1208 /* if both are NULL, jump to sort on al_addr instead */
1209 if (!l_map && !r_map)
1210 goto addr;
1212 if (!l_map) return -1;
1213 if (!r_map) return 1;
1215 rc = dso__cmp_id(l_map->dso, r_map->dso);
1216 if (rc)
1217 return rc;
1219 * Addresses with no major/minor numbers are assumed to be
1220 * anonymous in userspace. Sort those on pid then address.
1222 * The kernel and non-zero major/minor mapped areas are
1223 * assumed to be unity mapped. Sort those on address.
1226 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1227 (!(l_map->flags & MAP_SHARED)) &&
1228 !l_map->dso->id.maj && !l_map->dso->id.min &&
1229 !l_map->dso->id.ino && !l_map->dso->id.ino_generation) {
1230 /* userspace anonymous */
1232 if (left->thread->pid_ > right->thread->pid_) return -1;
1233 if (left->thread->pid_ < right->thread->pid_) return 1;
1236 addr:
1237 /* al_addr does all the right addr - start + offset calculations */
1238 l = cl_address(left->mem_info->daddr.al_addr);
1239 r = cl_address(right->mem_info->daddr.al_addr);
1241 if (l > r) return -1;
1242 if (l < r) return 1;
1244 return 0;
1247 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1248 size_t size, unsigned int width)
1251 uint64_t addr = 0;
1252 struct map_symbol *ms = NULL;
1253 char level = he->level;
1255 if (he->mem_info) {
1256 struct map *map = he->mem_info->daddr.ms.map;
1258 addr = cl_address(he->mem_info->daddr.al_addr);
1259 ms = &he->mem_info->daddr.ms;
1261 /* print [s] for shared data mmaps */
1262 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1263 map && !(map->prot & PROT_EXEC) &&
1264 (map->flags & MAP_SHARED) &&
1265 (map->dso->id.maj || map->dso->id.min ||
1266 map->dso->id.ino || map->dso->id.ino_generation))
1267 level = 's';
1268 else if (!map)
1269 level = 'X';
1271 return _hist_entry__sym_snprintf(ms, addr, level, bf, size, width);
1274 struct sort_entry sort_mispredict = {
1275 .se_header = "Branch Mispredicted",
1276 .se_cmp = sort__mispredict_cmp,
1277 .se_snprintf = hist_entry__mispredict_snprintf,
1278 .se_width_idx = HISTC_MISPREDICT,
1281 static u64 he_weight(struct hist_entry *he)
1283 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1286 static int64_t
1287 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1289 return he_weight(left) - he_weight(right);
1292 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1293 size_t size, unsigned int width)
1295 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1298 struct sort_entry sort_local_weight = {
1299 .se_header = "Local Weight",
1300 .se_cmp = sort__local_weight_cmp,
1301 .se_snprintf = hist_entry__local_weight_snprintf,
1302 .se_width_idx = HISTC_LOCAL_WEIGHT,
1305 static int64_t
1306 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1308 return left->stat.weight - right->stat.weight;
1311 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1312 size_t size, unsigned int width)
1314 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1317 struct sort_entry sort_global_weight = {
1318 .se_header = "Weight",
1319 .se_cmp = sort__global_weight_cmp,
1320 .se_snprintf = hist_entry__global_weight_snprintf,
1321 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1324 struct sort_entry sort_mem_daddr_sym = {
1325 .se_header = "Data Symbol",
1326 .se_cmp = sort__daddr_cmp,
1327 .se_snprintf = hist_entry__daddr_snprintf,
1328 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1331 struct sort_entry sort_mem_iaddr_sym = {
1332 .se_header = "Code Symbol",
1333 .se_cmp = sort__iaddr_cmp,
1334 .se_snprintf = hist_entry__iaddr_snprintf,
1335 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1338 struct sort_entry sort_mem_daddr_dso = {
1339 .se_header = "Data Object",
1340 .se_cmp = sort__dso_daddr_cmp,
1341 .se_snprintf = hist_entry__dso_daddr_snprintf,
1342 .se_width_idx = HISTC_MEM_DADDR_DSO,
1345 struct sort_entry sort_mem_locked = {
1346 .se_header = "Locked",
1347 .se_cmp = sort__locked_cmp,
1348 .se_snprintf = hist_entry__locked_snprintf,
1349 .se_width_idx = HISTC_MEM_LOCKED,
1352 struct sort_entry sort_mem_tlb = {
1353 .se_header = "TLB access",
1354 .se_cmp = sort__tlb_cmp,
1355 .se_snprintf = hist_entry__tlb_snprintf,
1356 .se_width_idx = HISTC_MEM_TLB,
1359 struct sort_entry sort_mem_lvl = {
1360 .se_header = "Memory access",
1361 .se_cmp = sort__lvl_cmp,
1362 .se_snprintf = hist_entry__lvl_snprintf,
1363 .se_width_idx = HISTC_MEM_LVL,
1366 struct sort_entry sort_mem_snoop = {
1367 .se_header = "Snoop",
1368 .se_cmp = sort__snoop_cmp,
1369 .se_snprintf = hist_entry__snoop_snprintf,
1370 .se_width_idx = HISTC_MEM_SNOOP,
1373 struct sort_entry sort_mem_dcacheline = {
1374 .se_header = "Data Cacheline",
1375 .se_cmp = sort__dcacheline_cmp,
1376 .se_snprintf = hist_entry__dcacheline_snprintf,
1377 .se_width_idx = HISTC_MEM_DCACHELINE,
1380 static int64_t
1381 sort__phys_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1383 uint64_t l = 0, r = 0;
1385 if (left->mem_info)
1386 l = left->mem_info->daddr.phys_addr;
1387 if (right->mem_info)
1388 r = right->mem_info->daddr.phys_addr;
1390 return (int64_t)(r - l);
1393 static int hist_entry__phys_daddr_snprintf(struct hist_entry *he, char *bf,
1394 size_t size, unsigned int width)
1396 uint64_t addr = 0;
1397 size_t ret = 0;
1398 size_t len = BITS_PER_LONG / 4;
1400 addr = he->mem_info->daddr.phys_addr;
1402 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", he->level);
1404 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", len, addr);
1406 ret += repsep_snprintf(bf + ret, size - ret, "%-*s", width - ret, "");
1408 if (ret > width)
1409 bf[width] = '\0';
1411 return width;
1414 struct sort_entry sort_mem_phys_daddr = {
1415 .se_header = "Data Physical Address",
1416 .se_cmp = sort__phys_daddr_cmp,
1417 .se_snprintf = hist_entry__phys_daddr_snprintf,
1418 .se_width_idx = HISTC_MEM_PHYS_DADDR,
1421 static int64_t
1422 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1424 if (!left->branch_info || !right->branch_info)
1425 return cmp_null(left->branch_info, right->branch_info);
1427 return left->branch_info->flags.abort !=
1428 right->branch_info->flags.abort;
1431 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1432 size_t size, unsigned int width)
1434 static const char *out = "N/A";
1436 if (he->branch_info) {
1437 if (he->branch_info->flags.abort)
1438 out = "A";
1439 else
1440 out = ".";
1443 return repsep_snprintf(bf, size, "%-*s", width, out);
1446 struct sort_entry sort_abort = {
1447 .se_header = "Transaction abort",
1448 .se_cmp = sort__abort_cmp,
1449 .se_snprintf = hist_entry__abort_snprintf,
1450 .se_width_idx = HISTC_ABORT,
1453 static int64_t
1454 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1456 if (!left->branch_info || !right->branch_info)
1457 return cmp_null(left->branch_info, right->branch_info);
1459 return left->branch_info->flags.in_tx !=
1460 right->branch_info->flags.in_tx;
1463 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1464 size_t size, unsigned int width)
1466 static const char *out = "N/A";
1468 if (he->branch_info) {
1469 if (he->branch_info->flags.in_tx)
1470 out = "T";
1471 else
1472 out = ".";
1475 return repsep_snprintf(bf, size, "%-*s", width, out);
1478 struct sort_entry sort_in_tx = {
1479 .se_header = "Branch in transaction",
1480 .se_cmp = sort__in_tx_cmp,
1481 .se_snprintf = hist_entry__in_tx_snprintf,
1482 .se_width_idx = HISTC_IN_TX,
1485 static int64_t
1486 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1488 return left->transaction - right->transaction;
1491 static inline char *add_str(char *p, const char *str)
1493 strcpy(p, str);
1494 return p + strlen(str);
1497 static struct txbit {
1498 unsigned flag;
1499 const char *name;
1500 int skip_for_len;
1501 } txbits[] = {
1502 { PERF_TXN_ELISION, "EL ", 0 },
1503 { PERF_TXN_TRANSACTION, "TX ", 1 },
1504 { PERF_TXN_SYNC, "SYNC ", 1 },
1505 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1506 { PERF_TXN_RETRY, "RETRY ", 0 },
1507 { PERF_TXN_CONFLICT, "CON ", 0 },
1508 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1509 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1510 { 0, NULL, 0 }
1513 int hist_entry__transaction_len(void)
1515 int i;
1516 int len = 0;
1518 for (i = 0; txbits[i].name; i++) {
1519 if (!txbits[i].skip_for_len)
1520 len += strlen(txbits[i].name);
1522 len += 4; /* :XX<space> */
1523 return len;
1526 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1527 size_t size, unsigned int width)
1529 u64 t = he->transaction;
1530 char buf[128];
1531 char *p = buf;
1532 int i;
1534 buf[0] = 0;
1535 for (i = 0; txbits[i].name; i++)
1536 if (txbits[i].flag & t)
1537 p = add_str(p, txbits[i].name);
1538 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1539 p = add_str(p, "NEITHER ");
1540 if (t & PERF_TXN_ABORT_MASK) {
1541 sprintf(p, ":%" PRIx64,
1542 (t & PERF_TXN_ABORT_MASK) >>
1543 PERF_TXN_ABORT_SHIFT);
1544 p += strlen(p);
1547 return repsep_snprintf(bf, size, "%-*s", width, buf);
1550 struct sort_entry sort_transaction = {
1551 .se_header = "Transaction ",
1552 .se_cmp = sort__transaction_cmp,
1553 .se_snprintf = hist_entry__transaction_snprintf,
1554 .se_width_idx = HISTC_TRANSACTION,
1557 /* --sort symbol_size */
1559 static int64_t _sort__sym_size_cmp(struct symbol *sym_l, struct symbol *sym_r)
1561 int64_t size_l = sym_l != NULL ? symbol__size(sym_l) : 0;
1562 int64_t size_r = sym_r != NULL ? symbol__size(sym_r) : 0;
1564 return size_l < size_r ? -1 :
1565 size_l == size_r ? 0 : 1;
1568 static int64_t
1569 sort__sym_size_cmp(struct hist_entry *left, struct hist_entry *right)
1571 return _sort__sym_size_cmp(right->ms.sym, left->ms.sym);
1574 static int _hist_entry__sym_size_snprintf(struct symbol *sym, char *bf,
1575 size_t bf_size, unsigned int width)
1577 if (sym)
1578 return repsep_snprintf(bf, bf_size, "%*d", width, symbol__size(sym));
1580 return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1583 static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
1584 size_t size, unsigned int width)
1586 return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
1589 struct sort_entry sort_sym_size = {
1590 .se_header = "Symbol size",
1591 .se_cmp = sort__sym_size_cmp,
1592 .se_snprintf = hist_entry__sym_size_snprintf,
1593 .se_width_idx = HISTC_SYM_SIZE,
1596 /* --sort dso_size */
1598 static int64_t _sort__dso_size_cmp(struct map *map_l, struct map *map_r)
1600 int64_t size_l = map_l != NULL ? map__size(map_l) : 0;
1601 int64_t size_r = map_r != NULL ? map__size(map_r) : 0;
1603 return size_l < size_r ? -1 :
1604 size_l == size_r ? 0 : 1;
1607 static int64_t
1608 sort__dso_size_cmp(struct hist_entry *left, struct hist_entry *right)
1610 return _sort__dso_size_cmp(right->ms.map, left->ms.map);
1613 static int _hist_entry__dso_size_snprintf(struct map *map, char *bf,
1614 size_t bf_size, unsigned int width)
1616 if (map && map->dso)
1617 return repsep_snprintf(bf, bf_size, "%*d", width,
1618 map__size(map));
1620 return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1623 static int hist_entry__dso_size_snprintf(struct hist_entry *he, char *bf,
1624 size_t size, unsigned int width)
1626 return _hist_entry__dso_size_snprintf(he->ms.map, bf, size, width);
1629 struct sort_entry sort_dso_size = {
1630 .se_header = "DSO size",
1631 .se_cmp = sort__dso_size_cmp,
1632 .se_snprintf = hist_entry__dso_size_snprintf,
1633 .se_width_idx = HISTC_DSO_SIZE,
1637 struct sort_dimension {
1638 const char *name;
1639 struct sort_entry *entry;
1640 int taken;
1643 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1645 static struct sort_dimension common_sort_dimensions[] = {
1646 DIM(SORT_PID, "pid", sort_thread),
1647 DIM(SORT_COMM, "comm", sort_comm),
1648 DIM(SORT_DSO, "dso", sort_dso),
1649 DIM(SORT_SYM, "symbol", sort_sym),
1650 DIM(SORT_PARENT, "parent", sort_parent),
1651 DIM(SORT_CPU, "cpu", sort_cpu),
1652 DIM(SORT_SOCKET, "socket", sort_socket),
1653 DIM(SORT_SRCLINE, "srcline", sort_srcline),
1654 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1655 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1656 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1657 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1658 DIM(SORT_TRACE, "trace", sort_trace),
1659 DIM(SORT_SYM_SIZE, "symbol_size", sort_sym_size),
1660 DIM(SORT_DSO_SIZE, "dso_size", sort_dso_size),
1661 DIM(SORT_CGROUP_ID, "cgroup_id", sort_cgroup_id),
1662 DIM(SORT_SYM_IPC_NULL, "ipc_null", sort_sym_ipc_null),
1663 DIM(SORT_TIME, "time", sort_time),
1666 #undef DIM
1668 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1670 static struct sort_dimension bstack_sort_dimensions[] = {
1671 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1672 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1673 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1674 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1675 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1676 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1677 DIM(SORT_ABORT, "abort", sort_abort),
1678 DIM(SORT_CYCLES, "cycles", sort_cycles),
1679 DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
1680 DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
1681 DIM(SORT_SYM_IPC, "ipc_lbr", sort_sym_ipc),
1684 #undef DIM
1686 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1688 static struct sort_dimension memory_sort_dimensions[] = {
1689 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1690 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1691 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1692 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1693 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1694 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1695 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1696 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1697 DIM(SORT_MEM_PHYS_DADDR, "phys_daddr", sort_mem_phys_daddr),
1700 #undef DIM
1702 struct hpp_dimension {
1703 const char *name;
1704 struct perf_hpp_fmt *fmt;
1705 int taken;
1708 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1710 static struct hpp_dimension hpp_sort_dimensions[] = {
1711 DIM(PERF_HPP__OVERHEAD, "overhead"),
1712 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1713 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1714 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1715 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1716 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1717 DIM(PERF_HPP__SAMPLES, "sample"),
1718 DIM(PERF_HPP__PERIOD, "period"),
1721 #undef DIM
1723 struct hpp_sort_entry {
1724 struct perf_hpp_fmt hpp;
1725 struct sort_entry *se;
1728 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1730 struct hpp_sort_entry *hse;
1732 if (!perf_hpp__is_sort_entry(fmt))
1733 return;
1735 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1736 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1739 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1740 struct hists *hists, int line __maybe_unused,
1741 int *span __maybe_unused)
1743 struct hpp_sort_entry *hse;
1744 size_t len = fmt->user_len;
1746 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1748 if (!len)
1749 len = hists__col_len(hists, hse->se->se_width_idx);
1751 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1754 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1755 struct perf_hpp *hpp __maybe_unused,
1756 struct hists *hists)
1758 struct hpp_sort_entry *hse;
1759 size_t len = fmt->user_len;
1761 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1763 if (!len)
1764 len = hists__col_len(hists, hse->se->se_width_idx);
1766 return len;
1769 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1770 struct hist_entry *he)
1772 struct hpp_sort_entry *hse;
1773 size_t len = fmt->user_len;
1775 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1777 if (!len)
1778 len = hists__col_len(he->hists, hse->se->se_width_idx);
1780 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1783 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1784 struct hist_entry *a, struct hist_entry *b)
1786 struct hpp_sort_entry *hse;
1788 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1789 return hse->se->se_cmp(a, b);
1792 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1793 struct hist_entry *a, struct hist_entry *b)
1795 struct hpp_sort_entry *hse;
1796 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1798 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1799 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1800 return collapse_fn(a, b);
1803 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1804 struct hist_entry *a, struct hist_entry *b)
1806 struct hpp_sort_entry *hse;
1807 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1809 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1810 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1811 return sort_fn(a, b);
1814 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1816 return format->header == __sort__hpp_header;
1819 #define MK_SORT_ENTRY_CHK(key) \
1820 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1822 struct hpp_sort_entry *hse; \
1824 if (!perf_hpp__is_sort_entry(fmt)) \
1825 return false; \
1827 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1828 return hse->se == &sort_ ## key ; \
1831 MK_SORT_ENTRY_CHK(trace)
1832 MK_SORT_ENTRY_CHK(srcline)
1833 MK_SORT_ENTRY_CHK(srcfile)
1834 MK_SORT_ENTRY_CHK(thread)
1835 MK_SORT_ENTRY_CHK(comm)
1836 MK_SORT_ENTRY_CHK(dso)
1837 MK_SORT_ENTRY_CHK(sym)
1840 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1842 struct hpp_sort_entry *hse_a;
1843 struct hpp_sort_entry *hse_b;
1845 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1846 return false;
1848 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1849 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1851 return hse_a->se == hse_b->se;
1854 static void hse_free(struct perf_hpp_fmt *fmt)
1856 struct hpp_sort_entry *hse;
1858 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1859 free(hse);
1862 static struct hpp_sort_entry *
1863 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
1865 struct hpp_sort_entry *hse;
1867 hse = malloc(sizeof(*hse));
1868 if (hse == NULL) {
1869 pr_err("Memory allocation failed\n");
1870 return NULL;
1873 hse->se = sd->entry;
1874 hse->hpp.name = sd->entry->se_header;
1875 hse->hpp.header = __sort__hpp_header;
1876 hse->hpp.width = __sort__hpp_width;
1877 hse->hpp.entry = __sort__hpp_entry;
1878 hse->hpp.color = NULL;
1880 hse->hpp.cmp = __sort__hpp_cmp;
1881 hse->hpp.collapse = __sort__hpp_collapse;
1882 hse->hpp.sort = __sort__hpp_sort;
1883 hse->hpp.equal = __sort__hpp_equal;
1884 hse->hpp.free = hse_free;
1886 INIT_LIST_HEAD(&hse->hpp.list);
1887 INIT_LIST_HEAD(&hse->hpp.sort_list);
1888 hse->hpp.elide = false;
1889 hse->hpp.len = 0;
1890 hse->hpp.user_len = 0;
1891 hse->hpp.level = level;
1893 return hse;
1896 static void hpp_free(struct perf_hpp_fmt *fmt)
1898 free(fmt);
1901 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1902 int level)
1904 struct perf_hpp_fmt *fmt;
1906 fmt = memdup(hd->fmt, sizeof(*fmt));
1907 if (fmt) {
1908 INIT_LIST_HEAD(&fmt->list);
1909 INIT_LIST_HEAD(&fmt->sort_list);
1910 fmt->free = hpp_free;
1911 fmt->level = level;
1914 return fmt;
1917 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1919 struct perf_hpp_fmt *fmt;
1920 struct hpp_sort_entry *hse;
1921 int ret = -1;
1922 int r;
1924 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1925 if (!perf_hpp__is_sort_entry(fmt))
1926 continue;
1928 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1929 if (hse->se->se_filter == NULL)
1930 continue;
1933 * hist entry is filtered if any of sort key in the hpp list
1934 * is applied. But it should skip non-matched filter types.
1936 r = hse->se->se_filter(he, type, arg);
1937 if (r >= 0) {
1938 if (ret < 0)
1939 ret = 0;
1940 ret |= r;
1944 return ret;
1947 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1948 struct perf_hpp_list *list,
1949 int level)
1951 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
1953 if (hse == NULL)
1954 return -1;
1956 perf_hpp_list__register_sort_field(list, &hse->hpp);
1957 return 0;
1960 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1961 struct perf_hpp_list *list)
1963 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
1965 if (hse == NULL)
1966 return -1;
1968 perf_hpp_list__column_register(list, &hse->hpp);
1969 return 0;
1972 struct hpp_dynamic_entry {
1973 struct perf_hpp_fmt hpp;
1974 struct evsel *evsel;
1975 struct tep_format_field *field;
1976 unsigned dynamic_len;
1977 bool raw_trace;
1980 static int hde_width(struct hpp_dynamic_entry *hde)
1982 if (!hde->hpp.len) {
1983 int len = hde->dynamic_len;
1984 int namelen = strlen(hde->field->name);
1985 int fieldlen = hde->field->size;
1987 if (namelen > len)
1988 len = namelen;
1990 if (!(hde->field->flags & TEP_FIELD_IS_STRING)) {
1991 /* length for print hex numbers */
1992 fieldlen = hde->field->size * 2 + 2;
1994 if (fieldlen > len)
1995 len = fieldlen;
1997 hde->hpp.len = len;
1999 return hde->hpp.len;
2002 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
2003 struct hist_entry *he)
2005 char *str, *pos;
2006 struct tep_format_field *field = hde->field;
2007 size_t namelen;
2008 bool last = false;
2010 if (hde->raw_trace)
2011 return;
2013 /* parse pretty print result and update max length */
2014 if (!he->trace_output)
2015 he->trace_output = get_trace_output(he);
2017 namelen = strlen(field->name);
2018 str = he->trace_output;
2020 while (str) {
2021 pos = strchr(str, ' ');
2022 if (pos == NULL) {
2023 last = true;
2024 pos = str + strlen(str);
2027 if (!strncmp(str, field->name, namelen)) {
2028 size_t len;
2030 str += namelen + 1;
2031 len = pos - str;
2033 if (len > hde->dynamic_len)
2034 hde->dynamic_len = len;
2035 break;
2038 if (last)
2039 str = NULL;
2040 else
2041 str = pos + 1;
2045 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2046 struct hists *hists __maybe_unused,
2047 int line __maybe_unused,
2048 int *span __maybe_unused)
2050 struct hpp_dynamic_entry *hde;
2051 size_t len = fmt->user_len;
2053 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2055 if (!len)
2056 len = hde_width(hde);
2058 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
2061 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
2062 struct perf_hpp *hpp __maybe_unused,
2063 struct hists *hists __maybe_unused)
2065 struct hpp_dynamic_entry *hde;
2066 size_t len = fmt->user_len;
2068 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2070 if (!len)
2071 len = hde_width(hde);
2073 return len;
2076 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
2078 struct hpp_dynamic_entry *hde;
2080 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2082 return hists_to_evsel(hists) == hde->evsel;
2085 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2086 struct hist_entry *he)
2088 struct hpp_dynamic_entry *hde;
2089 size_t len = fmt->user_len;
2090 char *str, *pos;
2091 struct tep_format_field *field;
2092 size_t namelen;
2093 bool last = false;
2094 int ret;
2096 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2098 if (!len)
2099 len = hde_width(hde);
2101 if (hde->raw_trace)
2102 goto raw_field;
2104 if (!he->trace_output)
2105 he->trace_output = get_trace_output(he);
2107 field = hde->field;
2108 namelen = strlen(field->name);
2109 str = he->trace_output;
2111 while (str) {
2112 pos = strchr(str, ' ');
2113 if (pos == NULL) {
2114 last = true;
2115 pos = str + strlen(str);
2118 if (!strncmp(str, field->name, namelen)) {
2119 str += namelen + 1;
2120 str = strndup(str, pos - str);
2122 if (str == NULL)
2123 return scnprintf(hpp->buf, hpp->size,
2124 "%*.*s", len, len, "ERROR");
2125 break;
2128 if (last)
2129 str = NULL;
2130 else
2131 str = pos + 1;
2134 if (str == NULL) {
2135 struct trace_seq seq;
2136 raw_field:
2137 trace_seq_init(&seq);
2138 tep_print_field(&seq, he->raw_data, hde->field);
2139 str = seq.buffer;
2142 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
2143 free(str);
2144 return ret;
2147 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
2148 struct hist_entry *a, struct hist_entry *b)
2150 struct hpp_dynamic_entry *hde;
2151 struct tep_format_field *field;
2152 unsigned offset, size;
2154 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2156 if (b == NULL) {
2157 update_dynamic_len(hde, a);
2158 return 0;
2161 field = hde->field;
2162 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2163 unsigned long long dyn;
2165 tep_read_number_field(field, a->raw_data, &dyn);
2166 offset = dyn & 0xffff;
2167 size = (dyn >> 16) & 0xffff;
2169 /* record max width for output */
2170 if (size > hde->dynamic_len)
2171 hde->dynamic_len = size;
2172 } else {
2173 offset = field->offset;
2174 size = field->size;
2177 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
2180 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
2182 return fmt->cmp == __sort__hde_cmp;
2185 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2187 struct hpp_dynamic_entry *hde_a;
2188 struct hpp_dynamic_entry *hde_b;
2190 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
2191 return false;
2193 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
2194 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
2196 return hde_a->field == hde_b->field;
2199 static void hde_free(struct perf_hpp_fmt *fmt)
2201 struct hpp_dynamic_entry *hde;
2203 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2204 free(hde);
2207 static struct hpp_dynamic_entry *
2208 __alloc_dynamic_entry(struct evsel *evsel, struct tep_format_field *field,
2209 int level)
2211 struct hpp_dynamic_entry *hde;
2213 hde = malloc(sizeof(*hde));
2214 if (hde == NULL) {
2215 pr_debug("Memory allocation failed\n");
2216 return NULL;
2219 hde->evsel = evsel;
2220 hde->field = field;
2221 hde->dynamic_len = 0;
2223 hde->hpp.name = field->name;
2224 hde->hpp.header = __sort__hde_header;
2225 hde->hpp.width = __sort__hde_width;
2226 hde->hpp.entry = __sort__hde_entry;
2227 hde->hpp.color = NULL;
2229 hde->hpp.cmp = __sort__hde_cmp;
2230 hde->hpp.collapse = __sort__hde_cmp;
2231 hde->hpp.sort = __sort__hde_cmp;
2232 hde->hpp.equal = __sort__hde_equal;
2233 hde->hpp.free = hde_free;
2235 INIT_LIST_HEAD(&hde->hpp.list);
2236 INIT_LIST_HEAD(&hde->hpp.sort_list);
2237 hde->hpp.elide = false;
2238 hde->hpp.len = 0;
2239 hde->hpp.user_len = 0;
2240 hde->hpp.level = level;
2242 return hde;
2245 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
2247 struct perf_hpp_fmt *new_fmt = NULL;
2249 if (perf_hpp__is_sort_entry(fmt)) {
2250 struct hpp_sort_entry *hse, *new_hse;
2252 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2253 new_hse = memdup(hse, sizeof(*hse));
2254 if (new_hse)
2255 new_fmt = &new_hse->hpp;
2256 } else if (perf_hpp__is_dynamic_entry(fmt)) {
2257 struct hpp_dynamic_entry *hde, *new_hde;
2259 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2260 new_hde = memdup(hde, sizeof(*hde));
2261 if (new_hde)
2262 new_fmt = &new_hde->hpp;
2263 } else {
2264 new_fmt = memdup(fmt, sizeof(*fmt));
2267 INIT_LIST_HEAD(&new_fmt->list);
2268 INIT_LIST_HEAD(&new_fmt->sort_list);
2270 return new_fmt;
2273 static int parse_field_name(char *str, char **event, char **field, char **opt)
2275 char *event_name, *field_name, *opt_name;
2277 event_name = str;
2278 field_name = strchr(str, '.');
2280 if (field_name) {
2281 *field_name++ = '\0';
2282 } else {
2283 event_name = NULL;
2284 field_name = str;
2287 opt_name = strchr(field_name, '/');
2288 if (opt_name)
2289 *opt_name++ = '\0';
2291 *event = event_name;
2292 *field = field_name;
2293 *opt = opt_name;
2295 return 0;
2298 /* find match evsel using a given event name. The event name can be:
2299 * 1. '%' + event index (e.g. '%1' for first event)
2300 * 2. full event name (e.g. sched:sched_switch)
2301 * 3. partial event name (should not contain ':')
2303 static struct evsel *find_evsel(struct evlist *evlist, char *event_name)
2305 struct evsel *evsel = NULL;
2306 struct evsel *pos;
2307 bool full_name;
2309 /* case 1 */
2310 if (event_name[0] == '%') {
2311 int nr = strtol(event_name+1, NULL, 0);
2313 if (nr > evlist->core.nr_entries)
2314 return NULL;
2316 evsel = evlist__first(evlist);
2317 while (--nr > 0)
2318 evsel = perf_evsel__next(evsel);
2320 return evsel;
2323 full_name = !!strchr(event_name, ':');
2324 evlist__for_each_entry(evlist, pos) {
2325 /* case 2 */
2326 if (full_name && !strcmp(pos->name, event_name))
2327 return pos;
2328 /* case 3 */
2329 if (!full_name && strstr(pos->name, event_name)) {
2330 if (evsel) {
2331 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2332 event_name, evsel->name, pos->name);
2333 return NULL;
2335 evsel = pos;
2339 return evsel;
2342 static int __dynamic_dimension__add(struct evsel *evsel,
2343 struct tep_format_field *field,
2344 bool raw_trace, int level)
2346 struct hpp_dynamic_entry *hde;
2348 hde = __alloc_dynamic_entry(evsel, field, level);
2349 if (hde == NULL)
2350 return -ENOMEM;
2352 hde->raw_trace = raw_trace;
2354 perf_hpp__register_sort_field(&hde->hpp);
2355 return 0;
2358 static int add_evsel_fields(struct evsel *evsel, bool raw_trace, int level)
2360 int ret;
2361 struct tep_format_field *field;
2363 field = evsel->tp_format->format.fields;
2364 while (field) {
2365 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2366 if (ret < 0)
2367 return ret;
2369 field = field->next;
2371 return 0;
2374 static int add_all_dynamic_fields(struct evlist *evlist, bool raw_trace,
2375 int level)
2377 int ret;
2378 struct evsel *evsel;
2380 evlist__for_each_entry(evlist, evsel) {
2381 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2382 continue;
2384 ret = add_evsel_fields(evsel, raw_trace, level);
2385 if (ret < 0)
2386 return ret;
2388 return 0;
2391 static int add_all_matching_fields(struct evlist *evlist,
2392 char *field_name, bool raw_trace, int level)
2394 int ret = -ESRCH;
2395 struct evsel *evsel;
2396 struct tep_format_field *field;
2398 evlist__for_each_entry(evlist, evsel) {
2399 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2400 continue;
2402 field = tep_find_any_field(evsel->tp_format, field_name);
2403 if (field == NULL)
2404 continue;
2406 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2407 if (ret < 0)
2408 break;
2410 return ret;
2413 static int add_dynamic_entry(struct evlist *evlist, const char *tok,
2414 int level)
2416 char *str, *event_name, *field_name, *opt_name;
2417 struct evsel *evsel;
2418 struct tep_format_field *field;
2419 bool raw_trace = symbol_conf.raw_trace;
2420 int ret = 0;
2422 if (evlist == NULL)
2423 return -ENOENT;
2425 str = strdup(tok);
2426 if (str == NULL)
2427 return -ENOMEM;
2429 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2430 ret = -EINVAL;
2431 goto out;
2434 if (opt_name) {
2435 if (strcmp(opt_name, "raw")) {
2436 pr_debug("unsupported field option %s\n", opt_name);
2437 ret = -EINVAL;
2438 goto out;
2440 raw_trace = true;
2443 if (!strcmp(field_name, "trace_fields")) {
2444 ret = add_all_dynamic_fields(evlist, raw_trace, level);
2445 goto out;
2448 if (event_name == NULL) {
2449 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2450 goto out;
2453 evsel = find_evsel(evlist, event_name);
2454 if (evsel == NULL) {
2455 pr_debug("Cannot find event: %s\n", event_name);
2456 ret = -ENOENT;
2457 goto out;
2460 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2461 pr_debug("%s is not a tracepoint event\n", event_name);
2462 ret = -EINVAL;
2463 goto out;
2466 if (!strcmp(field_name, "*")) {
2467 ret = add_evsel_fields(evsel, raw_trace, level);
2468 } else {
2469 field = tep_find_any_field(evsel->tp_format, field_name);
2470 if (field == NULL) {
2471 pr_debug("Cannot find event field for %s.%s\n",
2472 event_name, field_name);
2473 return -ENOENT;
2476 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2479 out:
2480 free(str);
2481 return ret;
2484 static int __sort_dimension__add(struct sort_dimension *sd,
2485 struct perf_hpp_list *list,
2486 int level)
2488 if (sd->taken)
2489 return 0;
2491 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2492 return -1;
2494 if (sd->entry->se_collapse)
2495 list->need_collapse = 1;
2497 sd->taken = 1;
2499 return 0;
2502 static int __hpp_dimension__add(struct hpp_dimension *hd,
2503 struct perf_hpp_list *list,
2504 int level)
2506 struct perf_hpp_fmt *fmt;
2508 if (hd->taken)
2509 return 0;
2511 fmt = __hpp_dimension__alloc_hpp(hd, level);
2512 if (!fmt)
2513 return -1;
2515 hd->taken = 1;
2516 perf_hpp_list__register_sort_field(list, fmt);
2517 return 0;
2520 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2521 struct sort_dimension *sd)
2523 if (sd->taken)
2524 return 0;
2526 if (__sort_dimension__add_hpp_output(sd, list) < 0)
2527 return -1;
2529 sd->taken = 1;
2530 return 0;
2533 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2534 struct hpp_dimension *hd)
2536 struct perf_hpp_fmt *fmt;
2538 if (hd->taken)
2539 return 0;
2541 fmt = __hpp_dimension__alloc_hpp(hd, 0);
2542 if (!fmt)
2543 return -1;
2545 hd->taken = 1;
2546 perf_hpp_list__column_register(list, fmt);
2547 return 0;
2550 int hpp_dimension__add_output(unsigned col)
2552 BUG_ON(col >= PERF_HPP__MAX_INDEX);
2553 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2556 int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2557 struct evlist *evlist,
2558 int level)
2560 unsigned int i;
2562 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2563 struct sort_dimension *sd = &common_sort_dimensions[i];
2565 if (strncasecmp(tok, sd->name, strlen(tok)))
2566 continue;
2568 if (sd->entry == &sort_parent) {
2569 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2570 if (ret) {
2571 char err[BUFSIZ];
2573 regerror(ret, &parent_regex, err, sizeof(err));
2574 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2575 return -EINVAL;
2577 list->parent = 1;
2578 } else if (sd->entry == &sort_sym) {
2579 list->sym = 1;
2581 * perf diff displays the performance difference amongst
2582 * two or more perf.data files. Those files could come
2583 * from different binaries. So we should not compare
2584 * their ips, but the name of symbol.
2586 if (sort__mode == SORT_MODE__DIFF)
2587 sd->entry->se_collapse = sort__sym_sort;
2589 } else if (sd->entry == &sort_dso) {
2590 list->dso = 1;
2591 } else if (sd->entry == &sort_socket) {
2592 list->socket = 1;
2593 } else if (sd->entry == &sort_thread) {
2594 list->thread = 1;
2595 } else if (sd->entry == &sort_comm) {
2596 list->comm = 1;
2599 return __sort_dimension__add(sd, list, level);
2602 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2603 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2605 if (strncasecmp(tok, hd->name, strlen(tok)))
2606 continue;
2608 return __hpp_dimension__add(hd, list, level);
2611 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2612 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2614 if (strncasecmp(tok, sd->name, strlen(tok)))
2615 continue;
2617 if (sort__mode != SORT_MODE__BRANCH)
2618 return -EINVAL;
2620 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2621 list->sym = 1;
2623 __sort_dimension__add(sd, list, level);
2624 return 0;
2627 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2628 struct sort_dimension *sd = &memory_sort_dimensions[i];
2630 if (strncasecmp(tok, sd->name, strlen(tok)))
2631 continue;
2633 if (sort__mode != SORT_MODE__MEMORY)
2634 return -EINVAL;
2636 if (sd->entry == &sort_mem_dcacheline && cacheline_size() == 0)
2637 return -EINVAL;
2639 if (sd->entry == &sort_mem_daddr_sym)
2640 list->sym = 1;
2642 __sort_dimension__add(sd, list, level);
2643 return 0;
2646 if (!add_dynamic_entry(evlist, tok, level))
2647 return 0;
2649 return -ESRCH;
2652 static int setup_sort_list(struct perf_hpp_list *list, char *str,
2653 struct evlist *evlist)
2655 char *tmp, *tok;
2656 int ret = 0;
2657 int level = 0;
2658 int next_level = 1;
2659 bool in_group = false;
2661 do {
2662 tok = str;
2663 tmp = strpbrk(str, "{}, ");
2664 if (tmp) {
2665 if (in_group)
2666 next_level = level;
2667 else
2668 next_level = level + 1;
2670 if (*tmp == '{')
2671 in_group = true;
2672 else if (*tmp == '}')
2673 in_group = false;
2675 *tmp = '\0';
2676 str = tmp + 1;
2679 if (*tok) {
2680 ret = sort_dimension__add(list, tok, evlist, level);
2681 if (ret == -EINVAL) {
2682 if (!cacheline_size() && !strncasecmp(tok, "dcacheline", strlen(tok)))
2683 ui__error("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2684 else
2685 ui__error("Invalid --sort key: `%s'", tok);
2686 break;
2687 } else if (ret == -ESRCH) {
2688 ui__error("Unknown --sort key: `%s'", tok);
2689 break;
2693 level = next_level;
2694 } while (tmp);
2696 return ret;
2699 static const char *get_default_sort_order(struct evlist *evlist)
2701 const char *default_sort_orders[] = {
2702 default_sort_order,
2703 default_branch_sort_order,
2704 default_mem_sort_order,
2705 default_top_sort_order,
2706 default_diff_sort_order,
2707 default_tracepoint_sort_order,
2709 bool use_trace = true;
2710 struct evsel *evsel;
2712 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2714 if (evlist == NULL || perf_evlist__empty(evlist))
2715 goto out_no_evlist;
2717 evlist__for_each_entry(evlist, evsel) {
2718 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2719 use_trace = false;
2720 break;
2724 if (use_trace) {
2725 sort__mode = SORT_MODE__TRACEPOINT;
2726 if (symbol_conf.raw_trace)
2727 return "trace_fields";
2729 out_no_evlist:
2730 return default_sort_orders[sort__mode];
2733 static int setup_sort_order(struct evlist *evlist)
2735 char *new_sort_order;
2738 * Append '+'-prefixed sort order to the default sort
2739 * order string.
2741 if (!sort_order || is_strict_order(sort_order))
2742 return 0;
2744 if (sort_order[1] == '\0') {
2745 ui__error("Invalid --sort key: `+'");
2746 return -EINVAL;
2750 * We allocate new sort_order string, but we never free it,
2751 * because it's checked over the rest of the code.
2753 if (asprintf(&new_sort_order, "%s,%s",
2754 get_default_sort_order(evlist), sort_order + 1) < 0) {
2755 pr_err("Not enough memory to set up --sort");
2756 return -ENOMEM;
2759 sort_order = new_sort_order;
2760 return 0;
2764 * Adds 'pre,' prefix into 'str' is 'pre' is
2765 * not already part of 'str'.
2767 static char *prefix_if_not_in(const char *pre, char *str)
2769 char *n;
2771 if (!str || strstr(str, pre))
2772 return str;
2774 if (asprintf(&n, "%s,%s", pre, str) < 0)
2775 return NULL;
2777 free(str);
2778 return n;
2781 static char *setup_overhead(char *keys)
2783 if (sort__mode == SORT_MODE__DIFF)
2784 return keys;
2786 keys = prefix_if_not_in("overhead", keys);
2788 if (symbol_conf.cumulate_callchain)
2789 keys = prefix_if_not_in("overhead_children", keys);
2791 return keys;
2794 static int __setup_sorting(struct evlist *evlist)
2796 char *str;
2797 const char *sort_keys;
2798 int ret = 0;
2800 ret = setup_sort_order(evlist);
2801 if (ret)
2802 return ret;
2804 sort_keys = sort_order;
2805 if (sort_keys == NULL) {
2806 if (is_strict_order(field_order)) {
2808 * If user specified field order but no sort order,
2809 * we'll honor it and not add default sort orders.
2811 return 0;
2814 sort_keys = get_default_sort_order(evlist);
2817 str = strdup(sort_keys);
2818 if (str == NULL) {
2819 pr_err("Not enough memory to setup sort keys");
2820 return -ENOMEM;
2824 * Prepend overhead fields for backward compatibility.
2826 if (!is_strict_order(field_order)) {
2827 str = setup_overhead(str);
2828 if (str == NULL) {
2829 pr_err("Not enough memory to setup overhead keys");
2830 return -ENOMEM;
2834 ret = setup_sort_list(&perf_hpp_list, str, evlist);
2836 free(str);
2837 return ret;
2840 void perf_hpp__set_elide(int idx, bool elide)
2842 struct perf_hpp_fmt *fmt;
2843 struct hpp_sort_entry *hse;
2845 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2846 if (!perf_hpp__is_sort_entry(fmt))
2847 continue;
2849 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2850 if (hse->se->se_width_idx == idx) {
2851 fmt->elide = elide;
2852 break;
2857 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
2859 if (list && strlist__nr_entries(list) == 1) {
2860 if (fp != NULL)
2861 fprintf(fp, "# %s: %s\n", list_name,
2862 strlist__entry(list, 0)->s);
2863 return true;
2865 return false;
2868 static bool get_elide(int idx, FILE *output)
2870 switch (idx) {
2871 case HISTC_SYMBOL:
2872 return __get_elide(symbol_conf.sym_list, "symbol", output);
2873 case HISTC_DSO:
2874 return __get_elide(symbol_conf.dso_list, "dso", output);
2875 case HISTC_COMM:
2876 return __get_elide(symbol_conf.comm_list, "comm", output);
2877 default:
2878 break;
2881 if (sort__mode != SORT_MODE__BRANCH)
2882 return false;
2884 switch (idx) {
2885 case HISTC_SYMBOL_FROM:
2886 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2887 case HISTC_SYMBOL_TO:
2888 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2889 case HISTC_DSO_FROM:
2890 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2891 case HISTC_DSO_TO:
2892 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2893 default:
2894 break;
2897 return false;
2900 void sort__setup_elide(FILE *output)
2902 struct perf_hpp_fmt *fmt;
2903 struct hpp_sort_entry *hse;
2905 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2906 if (!perf_hpp__is_sort_entry(fmt))
2907 continue;
2909 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2910 fmt->elide = get_elide(hse->se->se_width_idx, output);
2914 * It makes no sense to elide all of sort entries.
2915 * Just revert them to show up again.
2917 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2918 if (!perf_hpp__is_sort_entry(fmt))
2919 continue;
2921 if (!fmt->elide)
2922 return;
2925 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2926 if (!perf_hpp__is_sort_entry(fmt))
2927 continue;
2929 fmt->elide = false;
2933 int output_field_add(struct perf_hpp_list *list, char *tok)
2935 unsigned int i;
2937 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2938 struct sort_dimension *sd = &common_sort_dimensions[i];
2940 if (strncasecmp(tok, sd->name, strlen(tok)))
2941 continue;
2943 return __sort_dimension__add_output(list, sd);
2946 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2947 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2949 if (strncasecmp(tok, hd->name, strlen(tok)))
2950 continue;
2952 return __hpp_dimension__add_output(list, hd);
2955 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2956 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2958 if (strncasecmp(tok, sd->name, strlen(tok)))
2959 continue;
2961 if (sort__mode != SORT_MODE__MEMORY)
2962 return -EINVAL;
2964 return __sort_dimension__add_output(list, sd);
2967 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2968 struct sort_dimension *sd = &memory_sort_dimensions[i];
2970 if (strncasecmp(tok, sd->name, strlen(tok)))
2971 continue;
2973 if (sort__mode != SORT_MODE__BRANCH)
2974 return -EINVAL;
2976 return __sort_dimension__add_output(list, sd);
2979 return -ESRCH;
2982 static int setup_output_list(struct perf_hpp_list *list, char *str)
2984 char *tmp, *tok;
2985 int ret = 0;
2987 for (tok = strtok_r(str, ", ", &tmp);
2988 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2989 ret = output_field_add(list, tok);
2990 if (ret == -EINVAL) {
2991 ui__error("Invalid --fields key: `%s'", tok);
2992 break;
2993 } else if (ret == -ESRCH) {
2994 ui__error("Unknown --fields key: `%s'", tok);
2995 break;
2999 return ret;
3002 void reset_dimensions(void)
3004 unsigned int i;
3006 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
3007 common_sort_dimensions[i].taken = 0;
3009 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
3010 hpp_sort_dimensions[i].taken = 0;
3012 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
3013 bstack_sort_dimensions[i].taken = 0;
3015 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
3016 memory_sort_dimensions[i].taken = 0;
3019 bool is_strict_order(const char *order)
3021 return order && (*order != '+');
3024 static int __setup_output_field(void)
3026 char *str, *strp;
3027 int ret = -EINVAL;
3029 if (field_order == NULL)
3030 return 0;
3032 strp = str = strdup(field_order);
3033 if (str == NULL) {
3034 pr_err("Not enough memory to setup output fields");
3035 return -ENOMEM;
3038 if (!is_strict_order(field_order))
3039 strp++;
3041 if (!strlen(strp)) {
3042 ui__error("Invalid --fields key: `+'");
3043 goto out;
3046 ret = setup_output_list(&perf_hpp_list, strp);
3048 out:
3049 free(str);
3050 return ret;
3053 int setup_sorting(struct evlist *evlist)
3055 int err;
3057 err = __setup_sorting(evlist);
3058 if (err < 0)
3059 return err;
3061 if (parent_pattern != default_parent_pattern) {
3062 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
3063 if (err < 0)
3064 return err;
3067 reset_dimensions();
3070 * perf diff doesn't use default hpp output fields.
3072 if (sort__mode != SORT_MODE__DIFF)
3073 perf_hpp__init();
3075 err = __setup_output_field();
3076 if (err < 0)
3077 return err;
3079 /* copy sort keys to output fields */
3080 perf_hpp__setup_output_field(&perf_hpp_list);
3081 /* and then copy output fields to sort keys */
3082 perf_hpp__append_sort_keys(&perf_hpp_list);
3084 /* setup hists-specific output fields */
3085 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
3086 return -1;
3088 return 0;
3091 void reset_output_field(void)
3093 perf_hpp_list.need_collapse = 0;
3094 perf_hpp_list.parent = 0;
3095 perf_hpp_list.sym = 0;
3096 perf_hpp_list.dso = 0;
3098 field_order = NULL;
3099 sort_order = NULL;
3101 reset_dimensions();
3102 perf_hpp__reset_output_field(&perf_hpp_list);
3105 #define INDENT (3*8 + 1)
3107 static void add_key(struct strbuf *sb, const char *str, int *llen)
3109 if (*llen >= 75) {
3110 strbuf_addstr(sb, "\n\t\t\t ");
3111 *llen = INDENT;
3113 strbuf_addf(sb, " %s", str);
3114 *llen += strlen(str) + 1;
3117 static void add_sort_string(struct strbuf *sb, struct sort_dimension *s, int n,
3118 int *llen)
3120 int i;
3122 for (i = 0; i < n; i++)
3123 add_key(sb, s[i].name, llen);
3126 static void add_hpp_sort_string(struct strbuf *sb, struct hpp_dimension *s, int n,
3127 int *llen)
3129 int i;
3131 for (i = 0; i < n; i++)
3132 add_key(sb, s[i].name, llen);
3135 const char *sort_help(const char *prefix)
3137 struct strbuf sb;
3138 char *s;
3139 int len = strlen(prefix) + INDENT;
3141 strbuf_init(&sb, 300);
3142 strbuf_addstr(&sb, prefix);
3143 add_hpp_sort_string(&sb, hpp_sort_dimensions,
3144 ARRAY_SIZE(hpp_sort_dimensions), &len);
3145 add_sort_string(&sb, common_sort_dimensions,
3146 ARRAY_SIZE(common_sort_dimensions), &len);
3147 add_sort_string(&sb, bstack_sort_dimensions,
3148 ARRAY_SIZE(bstack_sort_dimensions), &len);
3149 add_sort_string(&sb, memory_sort_dimensions,
3150 ARRAY_SIZE(memory_sort_dimensions), &len);
3151 s = strbuf_detach(&sb, NULL);
3152 strbuf_release(&sb);
3153 return s;