8 #include <traceevent/event-parse.h>
11 const char default_parent_pattern
[] = "^sys_|^do_page_fault";
12 const char *parent_pattern
= default_parent_pattern
;
13 const char default_sort_order
[] = "comm,dso,symbol";
14 const char default_branch_sort_order
[] = "comm,dso_from,symbol_from,symbol_to,cycles";
15 const char default_mem_sort_order
[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
16 const char default_top_sort_order
[] = "dso,symbol";
17 const char default_diff_sort_order
[] = "dso,symbol";
18 const char default_tracepoint_sort_order
[] = "trace";
19 const char *sort_order
;
20 const char *field_order
;
21 regex_t ignore_callees_regex
;
22 int have_ignore_callees
= 0;
23 int sort__need_collapse
= 0;
24 int sort__has_parent
= 0;
25 int sort__has_sym
= 0;
26 int sort__has_dso
= 0;
27 int sort__has_socket
= 0;
28 int sort__has_thread
= 0;
29 enum sort_mode sort__mode
= SORT_MODE__NORMAL
;
32 static int repsep_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
38 n
= vsnprintf(bf
, size
, fmt
, ap
);
39 if (symbol_conf
.field_sep
&& n
> 0) {
43 sep
= strchr(sep
, *symbol_conf
.field_sep
);
56 static int64_t cmp_null(const void *l
, const void *r
)
69 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
71 return right
->thread
->tid
- left
->thread
->tid
;
74 static int hist_entry__thread_snprintf(struct hist_entry
*he
, char *bf
,
75 size_t size
, unsigned int width
)
77 const char *comm
= thread__comm_str(he
->thread
);
79 width
= max(7U, width
) - 6;
80 return repsep_snprintf(bf
, size
, "%5d:%-*.*s", he
->thread
->tid
,
81 width
, width
, comm
?: "");
84 struct sort_entry sort_thread
= {
85 .se_header
= " Pid:Command",
86 .se_cmp
= sort__thread_cmp
,
87 .se_snprintf
= hist_entry__thread_snprintf
,
88 .se_width_idx
= HISTC_THREAD
,
94 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
96 /* Compare the addr that should be unique among comm */
97 return strcmp(comm__str(right
->comm
), comm__str(left
->comm
));
101 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
103 /* Compare the addr that should be unique among comm */
104 return strcmp(comm__str(right
->comm
), comm__str(left
->comm
));
108 sort__comm_sort(struct hist_entry
*left
, struct hist_entry
*right
)
110 return strcmp(comm__str(right
->comm
), comm__str(left
->comm
));
113 static int hist_entry__comm_snprintf(struct hist_entry
*he
, char *bf
,
114 size_t size
, unsigned int width
)
116 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, comm__str(he
->comm
));
119 struct sort_entry sort_comm
= {
120 .se_header
= "Command",
121 .se_cmp
= sort__comm_cmp
,
122 .se_collapse
= sort__comm_collapse
,
123 .se_sort
= sort__comm_sort
,
124 .se_snprintf
= hist_entry__comm_snprintf
,
125 .se_width_idx
= HISTC_COMM
,
130 static int64_t _sort__dso_cmp(struct map
*map_l
, struct map
*map_r
)
132 struct dso
*dso_l
= map_l
? map_l
->dso
: NULL
;
133 struct dso
*dso_r
= map_r
? map_r
->dso
: NULL
;
134 const char *dso_name_l
, *dso_name_r
;
136 if (!dso_l
|| !dso_r
)
137 return cmp_null(dso_r
, dso_l
);
140 dso_name_l
= dso_l
->long_name
;
141 dso_name_r
= dso_r
->long_name
;
143 dso_name_l
= dso_l
->short_name
;
144 dso_name_r
= dso_r
->short_name
;
147 return strcmp(dso_name_l
, dso_name_r
);
151 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
153 return _sort__dso_cmp(right
->ms
.map
, left
->ms
.map
);
156 static int _hist_entry__dso_snprintf(struct map
*map
, char *bf
,
157 size_t size
, unsigned int width
)
159 if (map
&& map
->dso
) {
160 const char *dso_name
= !verbose
? map
->dso
->short_name
:
162 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, dso_name
);
165 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "[unknown]");
168 static int hist_entry__dso_snprintf(struct hist_entry
*he
, char *bf
,
169 size_t size
, unsigned int width
)
171 return _hist_entry__dso_snprintf(he
->ms
.map
, bf
, size
, width
);
174 struct sort_entry sort_dso
= {
175 .se_header
= "Shared Object",
176 .se_cmp
= sort__dso_cmp
,
177 .se_snprintf
= hist_entry__dso_snprintf
,
178 .se_width_idx
= HISTC_DSO
,
183 static int64_t _sort__addr_cmp(u64 left_ip
, u64 right_ip
)
185 return (int64_t)(right_ip
- left_ip
);
188 static int64_t _sort__sym_cmp(struct symbol
*sym_l
, struct symbol
*sym_r
)
190 if (!sym_l
|| !sym_r
)
191 return cmp_null(sym_l
, sym_r
);
196 if (sym_l
->start
!= sym_r
->start
)
197 return (int64_t)(sym_r
->start
- sym_l
->start
);
199 return (int64_t)(sym_r
->end
- sym_l
->end
);
203 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
207 if (!left
->ms
.sym
&& !right
->ms
.sym
)
208 return _sort__addr_cmp(left
->ip
, right
->ip
);
211 * comparing symbol address alone is not enough since it's a
212 * relative address within a dso.
214 if (!sort__has_dso
) {
215 ret
= sort__dso_cmp(left
, right
);
220 return _sort__sym_cmp(left
->ms
.sym
, right
->ms
.sym
);
224 sort__sym_sort(struct hist_entry
*left
, struct hist_entry
*right
)
226 if (!left
->ms
.sym
|| !right
->ms
.sym
)
227 return cmp_null(left
->ms
.sym
, right
->ms
.sym
);
229 return strcmp(right
->ms
.sym
->name
, left
->ms
.sym
->name
);
232 static int _hist_entry__sym_snprintf(struct map
*map
, struct symbol
*sym
,
233 u64 ip
, char level
, char *bf
, size_t size
,
239 char o
= map
? dso__symtab_origin(map
->dso
) : '!';
240 ret
+= repsep_snprintf(bf
, size
, "%-#*llx %c ",
241 BITS_PER_LONG
/ 4 + 2, ip
, o
);
244 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "[%c] ", level
);
246 if (map
->type
== MAP__VARIABLE
) {
247 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%s", sym
->name
);
248 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "+0x%llx",
249 ip
- map
->unmap_ip(map
, sym
->start
));
250 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-*s",
253 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-*s",
258 size_t len
= BITS_PER_LONG
/ 4;
259 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-#.*llx",
261 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-*s",
271 static int hist_entry__sym_snprintf(struct hist_entry
*he
, char *bf
,
272 size_t size
, unsigned int width
)
274 return _hist_entry__sym_snprintf(he
->ms
.map
, he
->ms
.sym
, he
->ip
,
275 he
->level
, bf
, size
, width
);
278 struct sort_entry sort_sym
= {
279 .se_header
= "Symbol",
280 .se_cmp
= sort__sym_cmp
,
281 .se_sort
= sort__sym_sort
,
282 .se_snprintf
= hist_entry__sym_snprintf
,
283 .se_width_idx
= HISTC_SYMBOL
,
289 sort__srcline_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
291 if (!left
->srcline
) {
293 left
->srcline
= SRCLINE_UNKNOWN
;
295 struct map
*map
= left
->ms
.map
;
296 left
->srcline
= get_srcline(map
->dso
,
297 map__rip_2objdump(map
, left
->ip
),
301 if (!right
->srcline
) {
303 right
->srcline
= SRCLINE_UNKNOWN
;
305 struct map
*map
= right
->ms
.map
;
306 right
->srcline
= get_srcline(map
->dso
,
307 map__rip_2objdump(map
, right
->ip
),
308 right
->ms
.sym
, true);
311 return strcmp(right
->srcline
, left
->srcline
);
314 static int hist_entry__srcline_snprintf(struct hist_entry
*he
, char *bf
,
315 size_t size
, unsigned int width
)
317 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, he
->srcline
);
320 struct sort_entry sort_srcline
= {
321 .se_header
= "Source:Line",
322 .se_cmp
= sort__srcline_cmp
,
323 .se_snprintf
= hist_entry__srcline_snprintf
,
324 .se_width_idx
= HISTC_SRCLINE
,
329 static char no_srcfile
[1];
331 static char *get_srcfile(struct hist_entry
*e
)
334 struct map
*map
= e
->ms
.map
;
336 sf
= __get_srcline(map
->dso
, map__rip_2objdump(map
, e
->ip
),
337 e
->ms
.sym
, false, true);
338 if (!strcmp(sf
, SRCLINE_UNKNOWN
))
350 sort__srcfile_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
352 if (!left
->srcfile
) {
354 left
->srcfile
= no_srcfile
;
356 left
->srcfile
= get_srcfile(left
);
358 if (!right
->srcfile
) {
360 right
->srcfile
= no_srcfile
;
362 right
->srcfile
= get_srcfile(right
);
364 return strcmp(right
->srcfile
, left
->srcfile
);
367 static int hist_entry__srcfile_snprintf(struct hist_entry
*he
, char *bf
,
368 size_t size
, unsigned int width
)
370 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, he
->srcfile
);
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
,
383 sort__parent_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
385 struct symbol
*sym_l
= left
->parent
;
386 struct symbol
*sym_r
= right
->parent
;
388 if (!sym_l
|| !sym_r
)
389 return cmp_null(sym_l
, sym_r
);
391 return strcmp(sym_r
->name
, sym_l
->name
);
394 static int hist_entry__parent_snprintf(struct hist_entry
*he
, char *bf
,
395 size_t size
, unsigned int width
)
397 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
,
398 he
->parent
? he
->parent
->name
: "[other]");
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
,
411 sort__cpu_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
413 return right
->cpu
- left
->cpu
;
416 static int hist_entry__cpu_snprintf(struct hist_entry
*he
, char *bf
,
417 size_t size
, unsigned int width
)
419 return repsep_snprintf(bf
, size
, "%*.*d", width
, width
, he
->cpu
);
422 struct sort_entry sort_cpu
= {
424 .se_cmp
= sort__cpu_cmp
,
425 .se_snprintf
= hist_entry__cpu_snprintf
,
426 .se_width_idx
= HISTC_CPU
,
432 sort__socket_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
434 return right
->socket
- left
->socket
;
437 static int hist_entry__socket_snprintf(struct hist_entry
*he
, char *bf
,
438 size_t size
, unsigned int width
)
440 return repsep_snprintf(bf
, size
, "%*.*d", width
, width
-3, he
->socket
);
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
,
452 static char *get_trace_output(struct hist_entry
*he
)
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
,
461 evsel
= hists_to_evsel(he
->hists
);
463 trace_seq_init(&seq
);
464 if (symbol_conf
.raw_trace
) {
465 pevent_print_fields(&seq
, he
->raw_data
, he
->raw_size
,
468 pevent_event_info(&seq
, evsel
->tp_format
, &rec
);
474 sort__trace_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
476 struct perf_evsel
*evsel
;
478 evsel
= hists_to_evsel(left
->hists
);
479 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
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
);
487 hists__new_col_len(left
->hists
, HISTC_TRACE
, strlen(left
->trace_output
));
488 hists__new_col_len(right
->hists
, HISTC_TRACE
, strlen(right
->trace_output
));
490 return strcmp(right
->trace_output
, left
->trace_output
);
493 static int hist_entry__trace_snprintf(struct hist_entry
*he
, char *bf
,
494 size_t size
, unsigned int width
)
496 struct perf_evsel
*evsel
;
498 evsel
= hists_to_evsel(he
->hists
);
499 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
500 return scnprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
502 if (he
->trace_output
== NULL
)
503 he
->trace_output
= get_trace_output(he
);
504 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, he
->trace_output
);
507 struct sort_entry sort_trace
= {
508 .se_header
= "Trace output",
509 .se_cmp
= sort__trace_cmp
,
510 .se_snprintf
= hist_entry__trace_snprintf
,
511 .se_width_idx
= HISTC_TRACE
,
514 /* sort keys for branch stacks */
517 sort__dso_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
519 if (!left
->branch_info
|| !right
->branch_info
)
520 return cmp_null(left
->branch_info
, right
->branch_info
);
522 return _sort__dso_cmp(left
->branch_info
->from
.map
,
523 right
->branch_info
->from
.map
);
526 static int hist_entry__dso_from_snprintf(struct hist_entry
*he
, char *bf
,
527 size_t size
, unsigned int width
)
530 return _hist_entry__dso_snprintf(he
->branch_info
->from
.map
,
533 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
537 sort__dso_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
539 if (!left
->branch_info
|| !right
->branch_info
)
540 return cmp_null(left
->branch_info
, right
->branch_info
);
542 return _sort__dso_cmp(left
->branch_info
->to
.map
,
543 right
->branch_info
->to
.map
);
546 static int hist_entry__dso_to_snprintf(struct hist_entry
*he
, char *bf
,
547 size_t size
, unsigned int width
)
550 return _hist_entry__dso_snprintf(he
->branch_info
->to
.map
,
553 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
557 sort__sym_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
559 struct addr_map_symbol
*from_l
= &left
->branch_info
->from
;
560 struct addr_map_symbol
*from_r
= &right
->branch_info
->from
;
562 if (!left
->branch_info
|| !right
->branch_info
)
563 return cmp_null(left
->branch_info
, right
->branch_info
);
565 from_l
= &left
->branch_info
->from
;
566 from_r
= &right
->branch_info
->from
;
568 if (!from_l
->sym
&& !from_r
->sym
)
569 return _sort__addr_cmp(from_l
->addr
, from_r
->addr
);
571 return _sort__sym_cmp(from_l
->sym
, from_r
->sym
);
575 sort__sym_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
577 struct addr_map_symbol
*to_l
, *to_r
;
579 if (!left
->branch_info
|| !right
->branch_info
)
580 return cmp_null(left
->branch_info
, right
->branch_info
);
582 to_l
= &left
->branch_info
->to
;
583 to_r
= &right
->branch_info
->to
;
585 if (!to_l
->sym
&& !to_r
->sym
)
586 return _sort__addr_cmp(to_l
->addr
, to_r
->addr
);
588 return _sort__sym_cmp(to_l
->sym
, to_r
->sym
);
591 static int hist_entry__sym_from_snprintf(struct hist_entry
*he
, char *bf
,
592 size_t size
, unsigned int width
)
594 if (he
->branch_info
) {
595 struct addr_map_symbol
*from
= &he
->branch_info
->from
;
597 return _hist_entry__sym_snprintf(from
->map
, from
->sym
, from
->addr
,
598 he
->level
, bf
, size
, width
);
601 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
604 static int hist_entry__sym_to_snprintf(struct hist_entry
*he
, char *bf
,
605 size_t size
, unsigned int width
)
607 if (he
->branch_info
) {
608 struct addr_map_symbol
*to
= &he
->branch_info
->to
;
610 return _hist_entry__sym_snprintf(to
->map
, to
->sym
, to
->addr
,
611 he
->level
, bf
, size
, width
);
614 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, "N/A");
617 struct sort_entry sort_dso_from
= {
618 .se_header
= "Source Shared Object",
619 .se_cmp
= sort__dso_from_cmp
,
620 .se_snprintf
= hist_entry__dso_from_snprintf
,
621 .se_width_idx
= HISTC_DSO_FROM
,
624 struct sort_entry sort_dso_to
= {
625 .se_header
= "Target Shared Object",
626 .se_cmp
= sort__dso_to_cmp
,
627 .se_snprintf
= hist_entry__dso_to_snprintf
,
628 .se_width_idx
= HISTC_DSO_TO
,
631 struct sort_entry sort_sym_from
= {
632 .se_header
= "Source Symbol",
633 .se_cmp
= sort__sym_from_cmp
,
634 .se_snprintf
= hist_entry__sym_from_snprintf
,
635 .se_width_idx
= HISTC_SYMBOL_FROM
,
638 struct sort_entry sort_sym_to
= {
639 .se_header
= "Target Symbol",
640 .se_cmp
= sort__sym_to_cmp
,
641 .se_snprintf
= hist_entry__sym_to_snprintf
,
642 .se_width_idx
= HISTC_SYMBOL_TO
,
646 sort__mispredict_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
650 if (!left
->branch_info
|| !right
->branch_info
)
651 return cmp_null(left
->branch_info
, right
->branch_info
);
653 mp
= left
->branch_info
->flags
.mispred
!= right
->branch_info
->flags
.mispred
;
654 p
= left
->branch_info
->flags
.predicted
!= right
->branch_info
->flags
.predicted
;
658 static int hist_entry__mispredict_snprintf(struct hist_entry
*he
, char *bf
,
659 size_t size
, unsigned int width
){
660 static const char *out
= "N/A";
662 if (he
->branch_info
) {
663 if (he
->branch_info
->flags
.predicted
)
665 else if (he
->branch_info
->flags
.mispred
)
669 return repsep_snprintf(bf
, size
, "%-*.*s", width
, width
, out
);
673 sort__cycles_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
675 return left
->branch_info
->flags
.cycles
-
676 right
->branch_info
->flags
.cycles
;
679 static int hist_entry__cycles_snprintf(struct hist_entry
*he
, char *bf
,
680 size_t size
, unsigned int width
)
682 if (he
->branch_info
->flags
.cycles
== 0)
683 return repsep_snprintf(bf
, size
, "%-*s", width
, "-");
684 return repsep_snprintf(bf
, size
, "%-*hd", width
,
685 he
->branch_info
->flags
.cycles
);
688 struct sort_entry sort_cycles
= {
689 .se_header
= "Basic Block Cycles",
690 .se_cmp
= sort__cycles_cmp
,
691 .se_snprintf
= hist_entry__cycles_snprintf
,
692 .se_width_idx
= HISTC_CYCLES
,
695 /* --sort daddr_sym */
697 sort__daddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
699 uint64_t l
= 0, r
= 0;
702 l
= left
->mem_info
->daddr
.addr
;
704 r
= right
->mem_info
->daddr
.addr
;
706 return (int64_t)(r
- l
);
709 static int hist_entry__daddr_snprintf(struct hist_entry
*he
, char *bf
,
710 size_t size
, unsigned int width
)
713 struct map
*map
= NULL
;
714 struct symbol
*sym
= NULL
;
717 addr
= he
->mem_info
->daddr
.addr
;
718 map
= he
->mem_info
->daddr
.map
;
719 sym
= he
->mem_info
->daddr
.sym
;
721 return _hist_entry__sym_snprintf(map
, sym
, addr
, he
->level
, bf
, size
,
726 sort__iaddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
728 uint64_t l
= 0, r
= 0;
731 l
= left
->mem_info
->iaddr
.addr
;
733 r
= right
->mem_info
->iaddr
.addr
;
735 return (int64_t)(r
- l
);
738 static int hist_entry__iaddr_snprintf(struct hist_entry
*he
, char *bf
,
739 size_t size
, unsigned int width
)
742 struct map
*map
= NULL
;
743 struct symbol
*sym
= NULL
;
746 addr
= he
->mem_info
->iaddr
.addr
;
747 map
= he
->mem_info
->iaddr
.map
;
748 sym
= he
->mem_info
->iaddr
.sym
;
750 return _hist_entry__sym_snprintf(map
, sym
, addr
, he
->level
, bf
, size
,
755 sort__dso_daddr_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
757 struct map
*map_l
= NULL
;
758 struct map
*map_r
= NULL
;
761 map_l
= left
->mem_info
->daddr
.map
;
763 map_r
= right
->mem_info
->daddr
.map
;
765 return _sort__dso_cmp(map_l
, map_r
);
768 static int hist_entry__dso_daddr_snprintf(struct hist_entry
*he
, char *bf
,
769 size_t size
, unsigned int width
)
771 struct map
*map
= NULL
;
774 map
= he
->mem_info
->daddr
.map
;
776 return _hist_entry__dso_snprintf(map
, bf
, size
, width
);
780 sort__locked_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
782 union perf_mem_data_src data_src_l
;
783 union perf_mem_data_src data_src_r
;
786 data_src_l
= left
->mem_info
->data_src
;
788 data_src_l
.mem_lock
= PERF_MEM_LOCK_NA
;
791 data_src_r
= right
->mem_info
->data_src
;
793 data_src_r
.mem_lock
= PERF_MEM_LOCK_NA
;
795 return (int64_t)(data_src_r
.mem_lock
- data_src_l
.mem_lock
);
798 static int hist_entry__locked_snprintf(struct hist_entry
*he
, char *bf
,
799 size_t size
, unsigned int width
)
802 u64 mask
= PERF_MEM_LOCK_NA
;
805 mask
= he
->mem_info
->data_src
.mem_lock
;
807 if (mask
& PERF_MEM_LOCK_NA
)
809 else if (mask
& PERF_MEM_LOCK_LOCKED
)
814 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
818 sort__tlb_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
820 union perf_mem_data_src data_src_l
;
821 union perf_mem_data_src data_src_r
;
824 data_src_l
= left
->mem_info
->data_src
;
826 data_src_l
.mem_dtlb
= PERF_MEM_TLB_NA
;
829 data_src_r
= right
->mem_info
->data_src
;
831 data_src_r
.mem_dtlb
= PERF_MEM_TLB_NA
;
833 return (int64_t)(data_src_r
.mem_dtlb
- data_src_l
.mem_dtlb
);
836 static const char * const tlb_access
[] = {
845 #define NUM_TLB_ACCESS (sizeof(tlb_access)/sizeof(const char *))
847 static int hist_entry__tlb_snprintf(struct hist_entry
*he
, char *bf
,
848 size_t size
, unsigned int width
)
851 size_t sz
= sizeof(out
) - 1; /* -1 for null termination */
853 u64 m
= PERF_MEM_TLB_NA
;
859 m
= he
->mem_info
->data_src
.mem_dtlb
;
861 hit
= m
& PERF_MEM_TLB_HIT
;
862 miss
= m
& PERF_MEM_TLB_MISS
;
864 /* already taken care of */
865 m
&= ~(PERF_MEM_TLB_HIT
|PERF_MEM_TLB_MISS
);
867 for (i
= 0; m
&& i
< NUM_TLB_ACCESS
; i
++, m
>>= 1) {
874 strncat(out
, tlb_access
[i
], sz
- l
);
875 l
+= strlen(tlb_access
[i
]);
880 strncat(out
, " hit", sz
- l
);
882 strncat(out
, " miss", sz
- l
);
884 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
888 sort__lvl_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
890 union perf_mem_data_src data_src_l
;
891 union perf_mem_data_src data_src_r
;
894 data_src_l
= left
->mem_info
->data_src
;
896 data_src_l
.mem_lvl
= PERF_MEM_LVL_NA
;
899 data_src_r
= right
->mem_info
->data_src
;
901 data_src_r
.mem_lvl
= PERF_MEM_LVL_NA
;
903 return (int64_t)(data_src_r
.mem_lvl
- data_src_l
.mem_lvl
);
906 static const char * const mem_lvl
[] = {
915 "Remote RAM (1 hop)",
916 "Remote RAM (2 hops)",
917 "Remote Cache (1 hop)",
918 "Remote Cache (2 hops)",
922 #define NUM_MEM_LVL (sizeof(mem_lvl)/sizeof(const char *))
924 static int hist_entry__lvl_snprintf(struct hist_entry
*he
, char *bf
,
925 size_t size
, unsigned int width
)
928 size_t sz
= sizeof(out
) - 1; /* -1 for null termination */
930 u64 m
= PERF_MEM_LVL_NA
;
934 m
= he
->mem_info
->data_src
.mem_lvl
;
938 hit
= m
& PERF_MEM_LVL_HIT
;
939 miss
= m
& PERF_MEM_LVL_MISS
;
941 /* already taken care of */
942 m
&= ~(PERF_MEM_LVL_HIT
|PERF_MEM_LVL_MISS
);
944 for (i
= 0; m
&& i
< NUM_MEM_LVL
; i
++, m
>>= 1) {
951 strncat(out
, mem_lvl
[i
], sz
- l
);
952 l
+= strlen(mem_lvl
[i
]);
957 strncat(out
, " hit", sz
- l
);
959 strncat(out
, " miss", sz
- l
);
961 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
965 sort__snoop_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
967 union perf_mem_data_src data_src_l
;
968 union perf_mem_data_src data_src_r
;
971 data_src_l
= left
->mem_info
->data_src
;
973 data_src_l
.mem_snoop
= PERF_MEM_SNOOP_NA
;
976 data_src_r
= right
->mem_info
->data_src
;
978 data_src_r
.mem_snoop
= PERF_MEM_SNOOP_NA
;
980 return (int64_t)(data_src_r
.mem_snoop
- data_src_l
.mem_snoop
);
983 static const char * const snoop_access
[] = {
990 #define NUM_SNOOP_ACCESS (sizeof(snoop_access)/sizeof(const char *))
992 static int hist_entry__snoop_snprintf(struct hist_entry
*he
, char *bf
,
993 size_t size
, unsigned int width
)
996 size_t sz
= sizeof(out
) - 1; /* -1 for null termination */
998 u64 m
= PERF_MEM_SNOOP_NA
;
1003 m
= he
->mem_info
->data_src
.mem_snoop
;
1005 for (i
= 0; m
&& i
< NUM_SNOOP_ACCESS
; i
++, m
>>= 1) {
1009 strcat(out
, " or ");
1012 strncat(out
, snoop_access
[i
], sz
- l
);
1013 l
+= strlen(snoop_access
[i
]);
1019 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1022 static inline u64
cl_address(u64 address
)
1024 /* return the cacheline of the address */
1025 return (address
& ~(cacheline_size
- 1));
1029 sort__dcacheline_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1032 struct map
*l_map
, *r_map
;
1034 if (!left
->mem_info
) return -1;
1035 if (!right
->mem_info
) return 1;
1037 /* group event types together */
1038 if (left
->cpumode
> right
->cpumode
) return -1;
1039 if (left
->cpumode
< right
->cpumode
) return 1;
1041 l_map
= left
->mem_info
->daddr
.map
;
1042 r_map
= right
->mem_info
->daddr
.map
;
1044 /* if both are NULL, jump to sort on al_addr instead */
1045 if (!l_map
&& !r_map
)
1048 if (!l_map
) return -1;
1049 if (!r_map
) return 1;
1051 if (l_map
->maj
> r_map
->maj
) return -1;
1052 if (l_map
->maj
< r_map
->maj
) return 1;
1054 if (l_map
->min
> r_map
->min
) return -1;
1055 if (l_map
->min
< r_map
->min
) return 1;
1057 if (l_map
->ino
> r_map
->ino
) return -1;
1058 if (l_map
->ino
< r_map
->ino
) return 1;
1060 if (l_map
->ino_generation
> r_map
->ino_generation
) return -1;
1061 if (l_map
->ino_generation
< r_map
->ino_generation
) return 1;
1064 * Addresses with no major/minor numbers are assumed to be
1065 * anonymous in userspace. Sort those on pid then address.
1067 * The kernel and non-zero major/minor mapped areas are
1068 * assumed to be unity mapped. Sort those on address.
1071 if ((left
->cpumode
!= PERF_RECORD_MISC_KERNEL
) &&
1072 (!(l_map
->flags
& MAP_SHARED
)) &&
1073 !l_map
->maj
&& !l_map
->min
&& !l_map
->ino
&&
1074 !l_map
->ino_generation
) {
1075 /* userspace anonymous */
1077 if (left
->thread
->pid_
> right
->thread
->pid_
) return -1;
1078 if (left
->thread
->pid_
< right
->thread
->pid_
) return 1;
1082 /* al_addr does all the right addr - start + offset calculations */
1083 l
= cl_address(left
->mem_info
->daddr
.al_addr
);
1084 r
= cl_address(right
->mem_info
->daddr
.al_addr
);
1086 if (l
> r
) return -1;
1087 if (l
< r
) return 1;
1092 static int hist_entry__dcacheline_snprintf(struct hist_entry
*he
, char *bf
,
1093 size_t size
, unsigned int width
)
1097 struct map
*map
= NULL
;
1098 struct symbol
*sym
= NULL
;
1099 char level
= he
->level
;
1102 addr
= cl_address(he
->mem_info
->daddr
.al_addr
);
1103 map
= he
->mem_info
->daddr
.map
;
1104 sym
= he
->mem_info
->daddr
.sym
;
1106 /* print [s] for shared data mmaps */
1107 if ((he
->cpumode
!= PERF_RECORD_MISC_KERNEL
) &&
1108 map
&& (map
->type
== MAP__VARIABLE
) &&
1109 (map
->flags
& MAP_SHARED
) &&
1110 (map
->maj
|| map
->min
|| map
->ino
||
1111 map
->ino_generation
))
1116 return _hist_entry__sym_snprintf(map
, sym
, addr
, level
, bf
, size
,
1120 struct sort_entry sort_mispredict
= {
1121 .se_header
= "Branch Mispredicted",
1122 .se_cmp
= sort__mispredict_cmp
,
1123 .se_snprintf
= hist_entry__mispredict_snprintf
,
1124 .se_width_idx
= HISTC_MISPREDICT
,
1127 static u64
he_weight(struct hist_entry
*he
)
1129 return he
->stat
.nr_events
? he
->stat
.weight
/ he
->stat
.nr_events
: 0;
1133 sort__local_weight_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1135 return he_weight(left
) - he_weight(right
);
1138 static int hist_entry__local_weight_snprintf(struct hist_entry
*he
, char *bf
,
1139 size_t size
, unsigned int width
)
1141 return repsep_snprintf(bf
, size
, "%-*llu", width
, he_weight(he
));
1144 struct sort_entry sort_local_weight
= {
1145 .se_header
= "Local Weight",
1146 .se_cmp
= sort__local_weight_cmp
,
1147 .se_snprintf
= hist_entry__local_weight_snprintf
,
1148 .se_width_idx
= HISTC_LOCAL_WEIGHT
,
1152 sort__global_weight_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1154 return left
->stat
.weight
- right
->stat
.weight
;
1157 static int hist_entry__global_weight_snprintf(struct hist_entry
*he
, char *bf
,
1158 size_t size
, unsigned int width
)
1160 return repsep_snprintf(bf
, size
, "%-*llu", width
, he
->stat
.weight
);
1163 struct sort_entry sort_global_weight
= {
1164 .se_header
= "Weight",
1165 .se_cmp
= sort__global_weight_cmp
,
1166 .se_snprintf
= hist_entry__global_weight_snprintf
,
1167 .se_width_idx
= HISTC_GLOBAL_WEIGHT
,
1170 struct sort_entry sort_mem_daddr_sym
= {
1171 .se_header
= "Data Symbol",
1172 .se_cmp
= sort__daddr_cmp
,
1173 .se_snprintf
= hist_entry__daddr_snprintf
,
1174 .se_width_idx
= HISTC_MEM_DADDR_SYMBOL
,
1177 struct sort_entry sort_mem_iaddr_sym
= {
1178 .se_header
= "Code Symbol",
1179 .se_cmp
= sort__iaddr_cmp
,
1180 .se_snprintf
= hist_entry__iaddr_snprintf
,
1181 .se_width_idx
= HISTC_MEM_IADDR_SYMBOL
,
1184 struct sort_entry sort_mem_daddr_dso
= {
1185 .se_header
= "Data Object",
1186 .se_cmp
= sort__dso_daddr_cmp
,
1187 .se_snprintf
= hist_entry__dso_daddr_snprintf
,
1188 .se_width_idx
= HISTC_MEM_DADDR_SYMBOL
,
1191 struct sort_entry sort_mem_locked
= {
1192 .se_header
= "Locked",
1193 .se_cmp
= sort__locked_cmp
,
1194 .se_snprintf
= hist_entry__locked_snprintf
,
1195 .se_width_idx
= HISTC_MEM_LOCKED
,
1198 struct sort_entry sort_mem_tlb
= {
1199 .se_header
= "TLB access",
1200 .se_cmp
= sort__tlb_cmp
,
1201 .se_snprintf
= hist_entry__tlb_snprintf
,
1202 .se_width_idx
= HISTC_MEM_TLB
,
1205 struct sort_entry sort_mem_lvl
= {
1206 .se_header
= "Memory access",
1207 .se_cmp
= sort__lvl_cmp
,
1208 .se_snprintf
= hist_entry__lvl_snprintf
,
1209 .se_width_idx
= HISTC_MEM_LVL
,
1212 struct sort_entry sort_mem_snoop
= {
1213 .se_header
= "Snoop",
1214 .se_cmp
= sort__snoop_cmp
,
1215 .se_snprintf
= hist_entry__snoop_snprintf
,
1216 .se_width_idx
= HISTC_MEM_SNOOP
,
1219 struct sort_entry sort_mem_dcacheline
= {
1220 .se_header
= "Data Cacheline",
1221 .se_cmp
= sort__dcacheline_cmp
,
1222 .se_snprintf
= hist_entry__dcacheline_snprintf
,
1223 .se_width_idx
= HISTC_MEM_DCACHELINE
,
1227 sort__abort_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1229 if (!left
->branch_info
|| !right
->branch_info
)
1230 return cmp_null(left
->branch_info
, right
->branch_info
);
1232 return left
->branch_info
->flags
.abort
!=
1233 right
->branch_info
->flags
.abort
;
1236 static int hist_entry__abort_snprintf(struct hist_entry
*he
, char *bf
,
1237 size_t size
, unsigned int width
)
1239 static const char *out
= "N/A";
1241 if (he
->branch_info
) {
1242 if (he
->branch_info
->flags
.abort
)
1248 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1251 struct sort_entry sort_abort
= {
1252 .se_header
= "Transaction abort",
1253 .se_cmp
= sort__abort_cmp
,
1254 .se_snprintf
= hist_entry__abort_snprintf
,
1255 .se_width_idx
= HISTC_ABORT
,
1259 sort__in_tx_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1261 if (!left
->branch_info
|| !right
->branch_info
)
1262 return cmp_null(left
->branch_info
, right
->branch_info
);
1264 return left
->branch_info
->flags
.in_tx
!=
1265 right
->branch_info
->flags
.in_tx
;
1268 static int hist_entry__in_tx_snprintf(struct hist_entry
*he
, char *bf
,
1269 size_t size
, unsigned int width
)
1271 static const char *out
= "N/A";
1273 if (he
->branch_info
) {
1274 if (he
->branch_info
->flags
.in_tx
)
1280 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
1283 struct sort_entry sort_in_tx
= {
1284 .se_header
= "Branch in transaction",
1285 .se_cmp
= sort__in_tx_cmp
,
1286 .se_snprintf
= hist_entry__in_tx_snprintf
,
1287 .se_width_idx
= HISTC_IN_TX
,
1291 sort__transaction_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1293 return left
->transaction
- right
->transaction
;
1296 static inline char *add_str(char *p
, const char *str
)
1299 return p
+ strlen(str
);
1302 static struct txbit
{
1307 { PERF_TXN_ELISION
, "EL ", 0 },
1308 { PERF_TXN_TRANSACTION
, "TX ", 1 },
1309 { PERF_TXN_SYNC
, "SYNC ", 1 },
1310 { PERF_TXN_ASYNC
, "ASYNC ", 0 },
1311 { PERF_TXN_RETRY
, "RETRY ", 0 },
1312 { PERF_TXN_CONFLICT
, "CON ", 0 },
1313 { PERF_TXN_CAPACITY_WRITE
, "CAP-WRITE ", 1 },
1314 { PERF_TXN_CAPACITY_READ
, "CAP-READ ", 0 },
1318 int hist_entry__transaction_len(void)
1323 for (i
= 0; txbits
[i
].name
; i
++) {
1324 if (!txbits
[i
].skip_for_len
)
1325 len
+= strlen(txbits
[i
].name
);
1327 len
+= 4; /* :XX<space> */
1331 static int hist_entry__transaction_snprintf(struct hist_entry
*he
, char *bf
,
1332 size_t size
, unsigned int width
)
1334 u64 t
= he
->transaction
;
1340 for (i
= 0; txbits
[i
].name
; i
++)
1341 if (txbits
[i
].flag
& t
)
1342 p
= add_str(p
, txbits
[i
].name
);
1343 if (t
&& !(t
& (PERF_TXN_SYNC
|PERF_TXN_ASYNC
)))
1344 p
= add_str(p
, "NEITHER ");
1345 if (t
& PERF_TXN_ABORT_MASK
) {
1346 sprintf(p
, ":%" PRIx64
,
1347 (t
& PERF_TXN_ABORT_MASK
) >>
1348 PERF_TXN_ABORT_SHIFT
);
1352 return repsep_snprintf(bf
, size
, "%-*s", width
, buf
);
1355 struct sort_entry sort_transaction
= {
1356 .se_header
= "Transaction ",
1357 .se_cmp
= sort__transaction_cmp
,
1358 .se_snprintf
= hist_entry__transaction_snprintf
,
1359 .se_width_idx
= HISTC_TRANSACTION
,
1362 struct sort_dimension
{
1364 struct sort_entry
*entry
;
1368 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1370 static struct sort_dimension common_sort_dimensions
[] = {
1371 DIM(SORT_PID
, "pid", sort_thread
),
1372 DIM(SORT_COMM
, "comm", sort_comm
),
1373 DIM(SORT_DSO
, "dso", sort_dso
),
1374 DIM(SORT_SYM
, "symbol", sort_sym
),
1375 DIM(SORT_PARENT
, "parent", sort_parent
),
1376 DIM(SORT_CPU
, "cpu", sort_cpu
),
1377 DIM(SORT_SOCKET
, "socket", sort_socket
),
1378 DIM(SORT_SRCLINE
, "srcline", sort_srcline
),
1379 DIM(SORT_SRCFILE
, "srcfile", sort_srcfile
),
1380 DIM(SORT_LOCAL_WEIGHT
, "local_weight", sort_local_weight
),
1381 DIM(SORT_GLOBAL_WEIGHT
, "weight", sort_global_weight
),
1382 DIM(SORT_TRANSACTION
, "transaction", sort_transaction
),
1383 DIM(SORT_TRACE
, "trace", sort_trace
),
1388 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1390 static struct sort_dimension bstack_sort_dimensions
[] = {
1391 DIM(SORT_DSO_FROM
, "dso_from", sort_dso_from
),
1392 DIM(SORT_DSO_TO
, "dso_to", sort_dso_to
),
1393 DIM(SORT_SYM_FROM
, "symbol_from", sort_sym_from
),
1394 DIM(SORT_SYM_TO
, "symbol_to", sort_sym_to
),
1395 DIM(SORT_MISPREDICT
, "mispredict", sort_mispredict
),
1396 DIM(SORT_IN_TX
, "in_tx", sort_in_tx
),
1397 DIM(SORT_ABORT
, "abort", sort_abort
),
1398 DIM(SORT_CYCLES
, "cycles", sort_cycles
),
1403 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1405 static struct sort_dimension memory_sort_dimensions
[] = {
1406 DIM(SORT_MEM_DADDR_SYMBOL
, "symbol_daddr", sort_mem_daddr_sym
),
1407 DIM(SORT_MEM_IADDR_SYMBOL
, "symbol_iaddr", sort_mem_iaddr_sym
),
1408 DIM(SORT_MEM_DADDR_DSO
, "dso_daddr", sort_mem_daddr_dso
),
1409 DIM(SORT_MEM_LOCKED
, "locked", sort_mem_locked
),
1410 DIM(SORT_MEM_TLB
, "tlb", sort_mem_tlb
),
1411 DIM(SORT_MEM_LVL
, "mem", sort_mem_lvl
),
1412 DIM(SORT_MEM_SNOOP
, "snoop", sort_mem_snoop
),
1413 DIM(SORT_MEM_DCACHELINE
, "dcacheline", sort_mem_dcacheline
),
1418 struct hpp_dimension
{
1420 struct perf_hpp_fmt
*fmt
;
1424 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1426 static struct hpp_dimension hpp_sort_dimensions
[] = {
1427 DIM(PERF_HPP__OVERHEAD
, "overhead"),
1428 DIM(PERF_HPP__OVERHEAD_SYS
, "overhead_sys"),
1429 DIM(PERF_HPP__OVERHEAD_US
, "overhead_us"),
1430 DIM(PERF_HPP__OVERHEAD_GUEST_SYS
, "overhead_guest_sys"),
1431 DIM(PERF_HPP__OVERHEAD_GUEST_US
, "overhead_guest_us"),
1432 DIM(PERF_HPP__OVERHEAD_ACC
, "overhead_children"),
1433 DIM(PERF_HPP__SAMPLES
, "sample"),
1434 DIM(PERF_HPP__PERIOD
, "period"),
1439 struct hpp_sort_entry
{
1440 struct perf_hpp_fmt hpp
;
1441 struct sort_entry
*se
;
1444 void perf_hpp__reset_sort_width(struct perf_hpp_fmt
*fmt
, struct hists
*hists
)
1446 struct hpp_sort_entry
*hse
;
1448 if (!perf_hpp__is_sort_entry(fmt
))
1451 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1452 hists__new_col_len(hists
, hse
->se
->se_width_idx
, strlen(fmt
->name
));
1455 static int __sort__hpp_header(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1456 struct perf_evsel
*evsel
)
1458 struct hpp_sort_entry
*hse
;
1459 size_t len
= fmt
->user_len
;
1461 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1464 len
= hists__col_len(evsel__hists(evsel
), hse
->se
->se_width_idx
);
1466 return scnprintf(hpp
->buf
, hpp
->size
, "%-*.*s", len
, len
, fmt
->name
);
1469 static int __sort__hpp_width(struct perf_hpp_fmt
*fmt
,
1470 struct perf_hpp
*hpp __maybe_unused
,
1471 struct perf_evsel
*evsel
)
1473 struct hpp_sort_entry
*hse
;
1474 size_t len
= fmt
->user_len
;
1476 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1479 len
= hists__col_len(evsel__hists(evsel
), hse
->se
->se_width_idx
);
1484 static int __sort__hpp_entry(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1485 struct hist_entry
*he
)
1487 struct hpp_sort_entry
*hse
;
1488 size_t len
= fmt
->user_len
;
1490 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1493 len
= hists__col_len(he
->hists
, hse
->se
->se_width_idx
);
1495 return hse
->se
->se_snprintf(he
, hpp
->buf
, hpp
->size
, len
);
1498 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt
*fmt
,
1499 struct hist_entry
*a
, struct hist_entry
*b
)
1501 struct hpp_sort_entry
*hse
;
1503 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1504 return hse
->se
->se_cmp(a
, b
);
1507 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt
*fmt
,
1508 struct hist_entry
*a
, struct hist_entry
*b
)
1510 struct hpp_sort_entry
*hse
;
1511 int64_t (*collapse_fn
)(struct hist_entry
*, struct hist_entry
*);
1513 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1514 collapse_fn
= hse
->se
->se_collapse
?: hse
->se
->se_cmp
;
1515 return collapse_fn(a
, b
);
1518 static int64_t __sort__hpp_sort(struct perf_hpp_fmt
*fmt
,
1519 struct hist_entry
*a
, struct hist_entry
*b
)
1521 struct hpp_sort_entry
*hse
;
1522 int64_t (*sort_fn
)(struct hist_entry
*, struct hist_entry
*);
1524 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1525 sort_fn
= hse
->se
->se_sort
?: hse
->se
->se_cmp
;
1526 return sort_fn(a
, b
);
1529 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt
*format
)
1531 return format
->header
== __sort__hpp_header
;
1534 static bool __sort__hpp_equal(struct perf_hpp_fmt
*a
, struct perf_hpp_fmt
*b
)
1536 struct hpp_sort_entry
*hse_a
;
1537 struct hpp_sort_entry
*hse_b
;
1539 if (!perf_hpp__is_sort_entry(a
) || !perf_hpp__is_sort_entry(b
))
1542 hse_a
= container_of(a
, struct hpp_sort_entry
, hpp
);
1543 hse_b
= container_of(b
, struct hpp_sort_entry
, hpp
);
1545 return hse_a
->se
== hse_b
->se
;
1548 static void hse_free(struct perf_hpp_fmt
*fmt
)
1550 struct hpp_sort_entry
*hse
;
1552 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
1556 static struct hpp_sort_entry
*
1557 __sort_dimension__alloc_hpp(struct sort_dimension
*sd
)
1559 struct hpp_sort_entry
*hse
;
1561 hse
= malloc(sizeof(*hse
));
1563 pr_err("Memory allocation failed\n");
1567 hse
->se
= sd
->entry
;
1568 hse
->hpp
.name
= sd
->entry
->se_header
;
1569 hse
->hpp
.header
= __sort__hpp_header
;
1570 hse
->hpp
.width
= __sort__hpp_width
;
1571 hse
->hpp
.entry
= __sort__hpp_entry
;
1572 hse
->hpp
.color
= NULL
;
1574 hse
->hpp
.cmp
= __sort__hpp_cmp
;
1575 hse
->hpp
.collapse
= __sort__hpp_collapse
;
1576 hse
->hpp
.sort
= __sort__hpp_sort
;
1577 hse
->hpp
.equal
= __sort__hpp_equal
;
1578 hse
->hpp
.free
= hse_free
;
1580 INIT_LIST_HEAD(&hse
->hpp
.list
);
1581 INIT_LIST_HEAD(&hse
->hpp
.sort_list
);
1582 hse
->hpp
.elide
= false;
1584 hse
->hpp
.user_len
= 0;
1589 static void hpp_free(struct perf_hpp_fmt
*fmt
)
1594 static struct perf_hpp_fmt
*__hpp_dimension__alloc_hpp(struct hpp_dimension
*hd
)
1596 struct perf_hpp_fmt
*fmt
;
1598 fmt
= memdup(hd
->fmt
, sizeof(*fmt
));
1600 INIT_LIST_HEAD(&fmt
->list
);
1601 INIT_LIST_HEAD(&fmt
->sort_list
);
1602 fmt
->free
= hpp_free
;
1608 static int __sort_dimension__add_hpp_sort(struct sort_dimension
*sd
)
1610 struct hpp_sort_entry
*hse
= __sort_dimension__alloc_hpp(sd
);
1615 perf_hpp__register_sort_field(&hse
->hpp
);
1619 static int __sort_dimension__add_hpp_output(struct perf_hpp_list
*list
,
1620 struct sort_dimension
*sd
)
1622 struct hpp_sort_entry
*hse
= __sort_dimension__alloc_hpp(sd
);
1627 perf_hpp_list__column_register(list
, &hse
->hpp
);
1631 struct hpp_dynamic_entry
{
1632 struct perf_hpp_fmt hpp
;
1633 struct perf_evsel
*evsel
;
1634 struct format_field
*field
;
1635 unsigned dynamic_len
;
1639 static int hde_width(struct hpp_dynamic_entry
*hde
)
1641 if (!hde
->hpp
.len
) {
1642 int len
= hde
->dynamic_len
;
1643 int namelen
= strlen(hde
->field
->name
);
1644 int fieldlen
= hde
->field
->size
;
1649 if (!(hde
->field
->flags
& FIELD_IS_STRING
)) {
1650 /* length for print hex numbers */
1651 fieldlen
= hde
->field
->size
* 2 + 2;
1658 return hde
->hpp
.len
;
1661 static void update_dynamic_len(struct hpp_dynamic_entry
*hde
,
1662 struct hist_entry
*he
)
1665 struct format_field
*field
= hde
->field
;
1672 /* parse pretty print result and update max length */
1673 if (!he
->trace_output
)
1674 he
->trace_output
= get_trace_output(he
);
1676 namelen
= strlen(field
->name
);
1677 str
= he
->trace_output
;
1680 pos
= strchr(str
, ' ');
1683 pos
= str
+ strlen(str
);
1686 if (!strncmp(str
, field
->name
, namelen
)) {
1692 if (len
> hde
->dynamic_len
)
1693 hde
->dynamic_len
= len
;
1704 static int __sort__hde_header(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1705 struct perf_evsel
*evsel __maybe_unused
)
1707 struct hpp_dynamic_entry
*hde
;
1708 size_t len
= fmt
->user_len
;
1710 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1713 len
= hde_width(hde
);
1715 return scnprintf(hpp
->buf
, hpp
->size
, "%*.*s", len
, len
, hde
->field
->name
);
1718 static int __sort__hde_width(struct perf_hpp_fmt
*fmt
,
1719 struct perf_hpp
*hpp __maybe_unused
,
1720 struct perf_evsel
*evsel __maybe_unused
)
1722 struct hpp_dynamic_entry
*hde
;
1723 size_t len
= fmt
->user_len
;
1725 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1728 len
= hde_width(hde
);
1733 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt
*fmt
, struct hists
*hists
)
1735 struct hpp_dynamic_entry
*hde
;
1737 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1739 return hists_to_evsel(hists
) == hde
->evsel
;
1742 static int __sort__hde_entry(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1743 struct hist_entry
*he
)
1745 struct hpp_dynamic_entry
*hde
;
1746 size_t len
= fmt
->user_len
;
1748 struct format_field
*field
;
1753 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1756 len
= hde_width(hde
);
1762 namelen
= strlen(field
->name
);
1763 str
= he
->trace_output
;
1766 pos
= strchr(str
, ' ');
1769 pos
= str
+ strlen(str
);
1772 if (!strncmp(str
, field
->name
, namelen
)) {
1774 str
= strndup(str
, pos
- str
);
1777 return scnprintf(hpp
->buf
, hpp
->size
,
1778 "%*.*s", len
, len
, "ERROR");
1789 struct trace_seq seq
;
1791 trace_seq_init(&seq
);
1792 pevent_print_field(&seq
, he
->raw_data
, hde
->field
);
1796 ret
= scnprintf(hpp
->buf
, hpp
->size
, "%*.*s", len
, len
, str
);
1801 static int64_t __sort__hde_cmp(struct perf_hpp_fmt
*fmt
,
1802 struct hist_entry
*a
, struct hist_entry
*b
)
1804 struct hpp_dynamic_entry
*hde
;
1805 struct format_field
*field
;
1806 unsigned offset
, size
;
1808 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1811 if (field
->flags
& FIELD_IS_DYNAMIC
) {
1812 unsigned long long dyn
;
1814 pevent_read_number_field(field
, a
->raw_data
, &dyn
);
1815 offset
= dyn
& 0xffff;
1816 size
= (dyn
>> 16) & 0xffff;
1818 /* record max width for output */
1819 if (size
> hde
->dynamic_len
)
1820 hde
->dynamic_len
= size
;
1822 offset
= field
->offset
;
1825 update_dynamic_len(hde
, a
);
1826 update_dynamic_len(hde
, b
);
1829 return memcmp(a
->raw_data
+ offset
, b
->raw_data
+ offset
, size
);
1832 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt
*fmt
)
1834 return fmt
->cmp
== __sort__hde_cmp
;
1837 static void hde_free(struct perf_hpp_fmt
*fmt
)
1839 struct hpp_dynamic_entry
*hde
;
1841 hde
= container_of(fmt
, struct hpp_dynamic_entry
, hpp
);
1845 static struct hpp_dynamic_entry
*
1846 __alloc_dynamic_entry(struct perf_evsel
*evsel
, struct format_field
*field
)
1848 struct hpp_dynamic_entry
*hde
;
1850 hde
= malloc(sizeof(*hde
));
1852 pr_debug("Memory allocation failed\n");
1858 hde
->dynamic_len
= 0;
1860 hde
->hpp
.name
= field
->name
;
1861 hde
->hpp
.header
= __sort__hde_header
;
1862 hde
->hpp
.width
= __sort__hde_width
;
1863 hde
->hpp
.entry
= __sort__hde_entry
;
1864 hde
->hpp
.color
= NULL
;
1866 hde
->hpp
.cmp
= __sort__hde_cmp
;
1867 hde
->hpp
.collapse
= __sort__hde_cmp
;
1868 hde
->hpp
.sort
= __sort__hde_cmp
;
1869 hde
->hpp
.free
= hde_free
;
1871 INIT_LIST_HEAD(&hde
->hpp
.list
);
1872 INIT_LIST_HEAD(&hde
->hpp
.sort_list
);
1873 hde
->hpp
.elide
= false;
1875 hde
->hpp
.user_len
= 0;
1880 static int parse_field_name(char *str
, char **event
, char **field
, char **opt
)
1882 char *event_name
, *field_name
, *opt_name
;
1885 field_name
= strchr(str
, '.');
1888 *field_name
++ = '\0';
1894 opt_name
= strchr(field_name
, '/');
1898 *event
= event_name
;
1899 *field
= field_name
;
1905 /* find match evsel using a given event name. The event name can be:
1906 * 1. '%' + event index (e.g. '%1' for first event)
1907 * 2. full event name (e.g. sched:sched_switch)
1908 * 3. partial event name (should not contain ':')
1910 static struct perf_evsel
*find_evsel(struct perf_evlist
*evlist
, char *event_name
)
1912 struct perf_evsel
*evsel
= NULL
;
1913 struct perf_evsel
*pos
;
1917 if (event_name
[0] == '%') {
1918 int nr
= strtol(event_name
+1, NULL
, 0);
1920 if (nr
> evlist
->nr_entries
)
1923 evsel
= perf_evlist__first(evlist
);
1925 evsel
= perf_evsel__next(evsel
);
1930 full_name
= !!strchr(event_name
, ':');
1931 evlist__for_each(evlist
, pos
) {
1933 if (full_name
&& !strcmp(pos
->name
, event_name
))
1936 if (!full_name
&& strstr(pos
->name
, event_name
)) {
1938 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
1939 event_name
, evsel
->name
, pos
->name
);
1949 static int __dynamic_dimension__add(struct perf_evsel
*evsel
,
1950 struct format_field
*field
,
1953 struct hpp_dynamic_entry
*hde
;
1955 hde
= __alloc_dynamic_entry(evsel
, field
);
1959 hde
->raw_trace
= raw_trace
;
1961 perf_hpp__register_sort_field(&hde
->hpp
);
1965 static int add_evsel_fields(struct perf_evsel
*evsel
, bool raw_trace
)
1968 struct format_field
*field
;
1970 field
= evsel
->tp_format
->format
.fields
;
1972 ret
= __dynamic_dimension__add(evsel
, field
, raw_trace
);
1976 field
= field
->next
;
1981 static int add_all_dynamic_fields(struct perf_evlist
*evlist
, bool raw_trace
)
1984 struct perf_evsel
*evsel
;
1986 evlist__for_each(evlist
, evsel
) {
1987 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
1990 ret
= add_evsel_fields(evsel
, raw_trace
);
1997 static int add_all_matching_fields(struct perf_evlist
*evlist
,
1998 char *field_name
, bool raw_trace
)
2001 struct perf_evsel
*evsel
;
2002 struct format_field
*field
;
2004 evlist__for_each(evlist
, evsel
) {
2005 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
2008 field
= pevent_find_any_field(evsel
->tp_format
, field_name
);
2012 ret
= __dynamic_dimension__add(evsel
, field
, raw_trace
);
2019 static int add_dynamic_entry(struct perf_evlist
*evlist
, const char *tok
)
2021 char *str
, *event_name
, *field_name
, *opt_name
;
2022 struct perf_evsel
*evsel
;
2023 struct format_field
*field
;
2024 bool raw_trace
= symbol_conf
.raw_trace
;
2034 if (parse_field_name(str
, &event_name
, &field_name
, &opt_name
) < 0) {
2040 if (strcmp(opt_name
, "raw")) {
2041 pr_debug("unsupported field option %s\n", opt_name
);
2048 if (!strcmp(field_name
, "trace_fields")) {
2049 ret
= add_all_dynamic_fields(evlist
, raw_trace
);
2053 if (event_name
== NULL
) {
2054 ret
= add_all_matching_fields(evlist
, field_name
, raw_trace
);
2058 evsel
= find_evsel(evlist
, event_name
);
2059 if (evsel
== NULL
) {
2060 pr_debug("Cannot find event: %s\n", event_name
);
2065 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
) {
2066 pr_debug("%s is not a tracepoint event\n", event_name
);
2071 if (!strcmp(field_name
, "*")) {
2072 ret
= add_evsel_fields(evsel
, raw_trace
);
2074 field
= pevent_find_any_field(evsel
->tp_format
, field_name
);
2075 if (field
== NULL
) {
2076 pr_debug("Cannot find event field for %s.%s\n",
2077 event_name
, field_name
);
2081 ret
= __dynamic_dimension__add(evsel
, field
, raw_trace
);
2089 static int __sort_dimension__add(struct sort_dimension
*sd
)
2094 if (__sort_dimension__add_hpp_sort(sd
) < 0)
2097 if (sd
->entry
->se_collapse
)
2098 sort__need_collapse
= 1;
2105 static int __hpp_dimension__add(struct hpp_dimension
*hd
)
2107 struct perf_hpp_fmt
*fmt
;
2112 fmt
= __hpp_dimension__alloc_hpp(hd
);
2117 perf_hpp__register_sort_field(fmt
);
2121 static int __sort_dimension__add_output(struct perf_hpp_list
*list
,
2122 struct sort_dimension
*sd
)
2127 if (__sort_dimension__add_hpp_output(list
, sd
) < 0)
2134 static int __hpp_dimension__add_output(struct perf_hpp_list
*list
,
2135 struct hpp_dimension
*hd
)
2137 struct perf_hpp_fmt
*fmt
;
2142 fmt
= __hpp_dimension__alloc_hpp(hd
);
2147 perf_hpp_list__column_register(list
, fmt
);
2151 int hpp_dimension__add_output(unsigned col
)
2153 BUG_ON(col
>= PERF_HPP__MAX_INDEX
);
2154 return __hpp_dimension__add_output(&perf_hpp_list
, &hpp_sort_dimensions
[col
]);
2157 static int sort_dimension__add(const char *tok
,
2158 struct perf_evlist
*evlist __maybe_unused
)
2162 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++) {
2163 struct sort_dimension
*sd
= &common_sort_dimensions
[i
];
2165 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2168 if (sd
->entry
== &sort_parent
) {
2169 int ret
= regcomp(&parent_regex
, parent_pattern
, REG_EXTENDED
);
2173 regerror(ret
, &parent_regex
, err
, sizeof(err
));
2174 pr_err("Invalid regex: %s\n%s", parent_pattern
, err
);
2177 sort__has_parent
= 1;
2178 } else if (sd
->entry
== &sort_sym
) {
2181 * perf diff displays the performance difference amongst
2182 * two or more perf.data files. Those files could come
2183 * from different binaries. So we should not compare
2184 * their ips, but the name of symbol.
2186 if (sort__mode
== SORT_MODE__DIFF
)
2187 sd
->entry
->se_collapse
= sort__sym_sort
;
2189 } else if (sd
->entry
== &sort_dso
) {
2191 } else if (sd
->entry
== &sort_socket
) {
2192 sort__has_socket
= 1;
2193 } else if (sd
->entry
== &sort_thread
) {
2194 sort__has_thread
= 1;
2197 return __sort_dimension__add(sd
);
2200 for (i
= 0; i
< ARRAY_SIZE(hpp_sort_dimensions
); i
++) {
2201 struct hpp_dimension
*hd
= &hpp_sort_dimensions
[i
];
2203 if (strncasecmp(tok
, hd
->name
, strlen(tok
)))
2206 return __hpp_dimension__add(hd
);
2209 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++) {
2210 struct sort_dimension
*sd
= &bstack_sort_dimensions
[i
];
2212 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2215 if (sort__mode
!= SORT_MODE__BRANCH
)
2218 if (sd
->entry
== &sort_sym_from
|| sd
->entry
== &sort_sym_to
)
2221 __sort_dimension__add(sd
);
2225 for (i
= 0; i
< ARRAY_SIZE(memory_sort_dimensions
); i
++) {
2226 struct sort_dimension
*sd
= &memory_sort_dimensions
[i
];
2228 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2231 if (sort__mode
!= SORT_MODE__MEMORY
)
2234 if (sd
->entry
== &sort_mem_daddr_sym
)
2237 __sort_dimension__add(sd
);
2241 if (!add_dynamic_entry(evlist
, tok
))
2247 static int setup_sort_list(char *str
, struct perf_evlist
*evlist
)
2252 for (tok
= strtok_r(str
, ", ", &tmp
);
2253 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
2254 ret
= sort_dimension__add(tok
, evlist
);
2255 if (ret
== -EINVAL
) {
2256 error("Invalid --sort key: `%s'", tok
);
2258 } else if (ret
== -ESRCH
) {
2259 error("Unknown --sort key: `%s'", tok
);
2267 static const char *get_default_sort_order(struct perf_evlist
*evlist
)
2269 const char *default_sort_orders
[] = {
2271 default_branch_sort_order
,
2272 default_mem_sort_order
,
2273 default_top_sort_order
,
2274 default_diff_sort_order
,
2275 default_tracepoint_sort_order
,
2277 bool use_trace
= true;
2278 struct perf_evsel
*evsel
;
2280 BUG_ON(sort__mode
>= ARRAY_SIZE(default_sort_orders
));
2285 evlist__for_each(evlist
, evsel
) {
2286 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
) {
2293 sort__mode
= SORT_MODE__TRACEPOINT
;
2294 if (symbol_conf
.raw_trace
)
2295 return "trace_fields";
2298 return default_sort_orders
[sort__mode
];
2301 static int setup_sort_order(struct perf_evlist
*evlist
)
2303 char *new_sort_order
;
2306 * Append '+'-prefixed sort order to the default sort
2309 if (!sort_order
|| is_strict_order(sort_order
))
2312 if (sort_order
[1] == '\0') {
2313 error("Invalid --sort key: `+'");
2318 * We allocate new sort_order string, but we never free it,
2319 * because it's checked over the rest of the code.
2321 if (asprintf(&new_sort_order
, "%s,%s",
2322 get_default_sort_order(evlist
), sort_order
+ 1) < 0) {
2323 error("Not enough memory to set up --sort");
2327 sort_order
= new_sort_order
;
2332 * Adds 'pre,' prefix into 'str' is 'pre' is
2333 * not already part of 'str'.
2335 static char *prefix_if_not_in(const char *pre
, char *str
)
2339 if (!str
|| strstr(str
, pre
))
2342 if (asprintf(&n
, "%s,%s", pre
, str
) < 0)
2349 static char *setup_overhead(char *keys
)
2351 keys
= prefix_if_not_in("overhead", keys
);
2353 if (symbol_conf
.cumulate_callchain
)
2354 keys
= prefix_if_not_in("overhead_children", keys
);
2359 static int __setup_sorting(struct perf_evlist
*evlist
)
2362 const char *sort_keys
;
2365 ret
= setup_sort_order(evlist
);
2369 sort_keys
= sort_order
;
2370 if (sort_keys
== NULL
) {
2371 if (is_strict_order(field_order
)) {
2373 * If user specified field order but no sort order,
2374 * we'll honor it and not add default sort orders.
2379 sort_keys
= get_default_sort_order(evlist
);
2382 str
= strdup(sort_keys
);
2384 error("Not enough memory to setup sort keys");
2389 * Prepend overhead fields for backward compatibility.
2391 if (!is_strict_order(field_order
)) {
2392 str
= setup_overhead(str
);
2394 error("Not enough memory to setup overhead keys");
2399 ret
= setup_sort_list(str
, evlist
);
2405 void perf_hpp__set_elide(int idx
, bool elide
)
2407 struct perf_hpp_fmt
*fmt
;
2408 struct hpp_sort_entry
*hse
;
2410 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2411 if (!perf_hpp__is_sort_entry(fmt
))
2414 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
2415 if (hse
->se
->se_width_idx
== idx
) {
2422 static bool __get_elide(struct strlist
*list
, const char *list_name
, FILE *fp
)
2424 if (list
&& strlist__nr_entries(list
) == 1) {
2426 fprintf(fp
, "# %s: %s\n", list_name
,
2427 strlist__entry(list
, 0)->s
);
2433 static bool get_elide(int idx
, FILE *output
)
2437 return __get_elide(symbol_conf
.sym_list
, "symbol", output
);
2439 return __get_elide(symbol_conf
.dso_list
, "dso", output
);
2441 return __get_elide(symbol_conf
.comm_list
, "comm", output
);
2446 if (sort__mode
!= SORT_MODE__BRANCH
)
2450 case HISTC_SYMBOL_FROM
:
2451 return __get_elide(symbol_conf
.sym_from_list
, "sym_from", output
);
2452 case HISTC_SYMBOL_TO
:
2453 return __get_elide(symbol_conf
.sym_to_list
, "sym_to", output
);
2454 case HISTC_DSO_FROM
:
2455 return __get_elide(symbol_conf
.dso_from_list
, "dso_from", output
);
2457 return __get_elide(symbol_conf
.dso_to_list
, "dso_to", output
);
2465 void sort__setup_elide(FILE *output
)
2467 struct perf_hpp_fmt
*fmt
;
2468 struct hpp_sort_entry
*hse
;
2470 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2471 if (!perf_hpp__is_sort_entry(fmt
))
2474 hse
= container_of(fmt
, struct hpp_sort_entry
, hpp
);
2475 fmt
->elide
= get_elide(hse
->se
->se_width_idx
, output
);
2479 * It makes no sense to elide all of sort entries.
2480 * Just revert them to show up again.
2482 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2483 if (!perf_hpp__is_sort_entry(fmt
))
2490 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
2491 if (!perf_hpp__is_sort_entry(fmt
))
2498 static int output_field_add(struct perf_hpp_list
*list
, char *tok
)
2502 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++) {
2503 struct sort_dimension
*sd
= &common_sort_dimensions
[i
];
2505 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2508 return __sort_dimension__add_output(list
, sd
);
2511 for (i
= 0; i
< ARRAY_SIZE(hpp_sort_dimensions
); i
++) {
2512 struct hpp_dimension
*hd
= &hpp_sort_dimensions
[i
];
2514 if (strncasecmp(tok
, hd
->name
, strlen(tok
)))
2517 return __hpp_dimension__add_output(list
, hd
);
2520 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++) {
2521 struct sort_dimension
*sd
= &bstack_sort_dimensions
[i
];
2523 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2526 return __sort_dimension__add_output(list
, sd
);
2529 for (i
= 0; i
< ARRAY_SIZE(memory_sort_dimensions
); i
++) {
2530 struct sort_dimension
*sd
= &memory_sort_dimensions
[i
];
2532 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
2535 return __sort_dimension__add_output(list
, sd
);
2541 static int setup_output_list(struct perf_hpp_list
*list
, char *str
)
2546 for (tok
= strtok_r(str
, ", ", &tmp
);
2547 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
2548 ret
= output_field_add(list
, tok
);
2549 if (ret
== -EINVAL
) {
2550 error("Invalid --fields key: `%s'", tok
);
2552 } else if (ret
== -ESRCH
) {
2553 error("Unknown --fields key: `%s'", tok
);
2561 static void reset_dimensions(void)
2565 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++)
2566 common_sort_dimensions
[i
].taken
= 0;
2568 for (i
= 0; i
< ARRAY_SIZE(hpp_sort_dimensions
); i
++)
2569 hpp_sort_dimensions
[i
].taken
= 0;
2571 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++)
2572 bstack_sort_dimensions
[i
].taken
= 0;
2574 for (i
= 0; i
< ARRAY_SIZE(memory_sort_dimensions
); i
++)
2575 memory_sort_dimensions
[i
].taken
= 0;
2578 bool is_strict_order(const char *order
)
2580 return order
&& (*order
!= '+');
2583 static int __setup_output_field(void)
2588 if (field_order
== NULL
)
2591 strp
= str
= strdup(field_order
);
2593 error("Not enough memory to setup output fields");
2597 if (!is_strict_order(field_order
))
2600 if (!strlen(strp
)) {
2601 error("Invalid --fields key: `+'");
2605 ret
= setup_output_list(&perf_hpp_list
, strp
);
2612 int setup_sorting(struct perf_evlist
*evlist
)
2616 err
= __setup_sorting(evlist
);
2620 if (parent_pattern
!= default_parent_pattern
) {
2621 err
= sort_dimension__add("parent", evlist
);
2629 * perf diff doesn't use default hpp output fields.
2631 if (sort__mode
!= SORT_MODE__DIFF
)
2634 err
= __setup_output_field();
2638 /* copy sort keys to output fields */
2639 perf_hpp__setup_output_field();
2640 /* and then copy output fields to sort keys */
2641 perf_hpp__append_sort_keys();
2646 void reset_output_field(void)
2648 sort__need_collapse
= 0;
2649 sort__has_parent
= 0;
2657 perf_hpp__reset_output_field();