5 const char default_parent_pattern
[] = "^sys_|^do_page_fault";
6 const char *parent_pattern
= default_parent_pattern
;
7 const char default_sort_order
[] = "comm,dso,symbol";
8 const char *sort_order
= default_sort_order
;
9 int sort__need_collapse
= 0;
10 int sort__has_parent
= 0;
11 int sort__has_sym
= 0;
12 int sort__branch_mode
= -1; /* -1 = means not set */
14 enum sort_type sort__first_dimension
;
16 LIST_HEAD(hist_entry__sort_list
);
18 static int repsep_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
24 n
= vsnprintf(bf
, size
, fmt
, ap
);
25 if (symbol_conf
.field_sep
&& n
> 0) {
29 sep
= strchr(sep
, *symbol_conf
.field_sep
);
42 static int64_t cmp_null(void *l
, void *r
)
55 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
57 return right
->thread
->pid
- left
->thread
->pid
;
60 static int hist_entry__thread_snprintf(struct hist_entry
*self
, char *bf
,
61 size_t size
, unsigned int width
)
63 return repsep_snprintf(bf
, size
, "%*s:%5d", width
- 6,
64 self
->thread
->comm
?: "", self
->thread
->pid
);
67 struct sort_entry sort_thread
= {
68 .se_header
= "Command: Pid",
69 .se_cmp
= sort__thread_cmp
,
70 .se_snprintf
= hist_entry__thread_snprintf
,
71 .se_width_idx
= HISTC_THREAD
,
77 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
79 return right
->thread
->pid
- left
->thread
->pid
;
83 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
85 char *comm_l
= left
->thread
->comm
;
86 char *comm_r
= right
->thread
->comm
;
88 if (!comm_l
|| !comm_r
)
89 return cmp_null(comm_l
, comm_r
);
91 return strcmp(comm_l
, comm_r
);
94 static int hist_entry__comm_snprintf(struct hist_entry
*self
, char *bf
,
95 size_t size
, unsigned int width
)
97 return repsep_snprintf(bf
, size
, "%*s", width
, self
->thread
->comm
);
100 struct sort_entry sort_comm
= {
101 .se_header
= "Command",
102 .se_cmp
= sort__comm_cmp
,
103 .se_collapse
= sort__comm_collapse
,
104 .se_snprintf
= hist_entry__comm_snprintf
,
105 .se_width_idx
= HISTC_COMM
,
110 static int64_t _sort__dso_cmp(struct map
*map_l
, struct map
*map_r
)
112 struct dso
*dso_l
= map_l
? map_l
->dso
: NULL
;
113 struct dso
*dso_r
= map_r
? map_r
->dso
: NULL
;
114 const char *dso_name_l
, *dso_name_r
;
116 if (!dso_l
|| !dso_r
)
117 return cmp_null(dso_l
, dso_r
);
120 dso_name_l
= dso_l
->long_name
;
121 dso_name_r
= dso_r
->long_name
;
123 dso_name_l
= dso_l
->short_name
;
124 dso_name_r
= dso_r
->short_name
;
127 return strcmp(dso_name_l
, dso_name_r
);
131 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
133 return _sort__dso_cmp(left
->ms
.map
, right
->ms
.map
);
136 static int _hist_entry__dso_snprintf(struct map
*map
, char *bf
,
137 size_t size
, unsigned int width
)
139 if (map
&& map
->dso
) {
140 const char *dso_name
= !verbose
? map
->dso
->short_name
:
142 return repsep_snprintf(bf
, size
, "%-*s", width
, dso_name
);
145 return repsep_snprintf(bf
, size
, "%-*s", width
, "[unknown]");
148 static int hist_entry__dso_snprintf(struct hist_entry
*self
, char *bf
,
149 size_t size
, unsigned int width
)
151 return _hist_entry__dso_snprintf(self
->ms
.map
, bf
, size
, width
);
154 struct sort_entry sort_dso
= {
155 .se_header
= "Shared Object",
156 .se_cmp
= sort__dso_cmp
,
157 .se_snprintf
= hist_entry__dso_snprintf
,
158 .se_width_idx
= HISTC_DSO
,
163 static int64_t _sort__sym_cmp(struct symbol
*sym_l
, struct symbol
*sym_r
)
167 if (!sym_l
|| !sym_r
)
168 return cmp_null(sym_l
, sym_r
);
176 return (int64_t)(ip_r
- ip_l
);
180 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
182 if (!left
->ms
.sym
&& !right
->ms
.sym
)
183 return right
->level
- left
->level
;
185 return _sort__sym_cmp(left
->ms
.sym
, right
->ms
.sym
);
188 static int _hist_entry__sym_snprintf(struct map
*map
, struct symbol
*sym
,
189 u64 ip
, char level
, char *bf
, size_t size
,
195 char o
= map
? dso__symtab_origin(map
->dso
) : '!';
196 ret
+= repsep_snprintf(bf
, size
, "%-#*llx %c ",
197 BITS_PER_LONG
/ 4, ip
, o
);
200 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "[%c] ", level
);
202 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-*s",
206 size_t len
= BITS_PER_LONG
/ 4;
207 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-#.*llx",
209 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-*s",
216 static int hist_entry__sym_snprintf(struct hist_entry
*self
, char *bf
,
217 size_t size
, unsigned int width
)
219 return _hist_entry__sym_snprintf(self
->ms
.map
, self
->ms
.sym
, self
->ip
,
220 self
->level
, bf
, size
, width
);
223 struct sort_entry sort_sym
= {
224 .se_header
= "Symbol",
225 .se_cmp
= sort__sym_cmp
,
226 .se_snprintf
= hist_entry__sym_snprintf
,
227 .se_width_idx
= HISTC_SYMBOL
,
233 sort__srcline_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
235 return (int64_t)(right
->ip
- left
->ip
);
238 static int hist_entry__srcline_snprintf(struct hist_entry
*self
, char *bf
,
240 unsigned int width __maybe_unused
)
243 char cmd
[PATH_MAX
+ 2], *path
= self
->srcline
, *nl
;
252 if (!strncmp(self
->ms
.map
->dso
->long_name
, "/tmp/perf-", 10))
255 snprintf(cmd
, sizeof(cmd
), "addr2line -e %s %016" PRIx64
,
256 self
->ms
.map
->dso
->long_name
, self
->ip
);
257 fp
= popen(cmd
, "r");
261 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
263 self
->srcline
= strdup(path
);
264 if (self
->srcline
== NULL
)
267 nl
= strchr(self
->srcline
, '\n');
270 path
= self
->srcline
;
274 return repsep_snprintf(bf
, size
, "%s", path
);
278 return repsep_snprintf(bf
, size
, "%-#*llx", BITS_PER_LONG
/ 4, self
->ip
);
281 struct sort_entry sort_srcline
= {
282 .se_header
= "Source:Line",
283 .se_cmp
= sort__srcline_cmp
,
284 .se_snprintf
= hist_entry__srcline_snprintf
,
285 .se_width_idx
= HISTC_SRCLINE
,
291 sort__parent_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
293 struct symbol
*sym_l
= left
->parent
;
294 struct symbol
*sym_r
= right
->parent
;
296 if (!sym_l
|| !sym_r
)
297 return cmp_null(sym_l
, sym_r
);
299 return strcmp(sym_l
->name
, sym_r
->name
);
302 static int hist_entry__parent_snprintf(struct hist_entry
*self
, char *bf
,
303 size_t size
, unsigned int width
)
305 return repsep_snprintf(bf
, size
, "%-*s", width
,
306 self
->parent
? self
->parent
->name
: "[other]");
309 struct sort_entry sort_parent
= {
310 .se_header
= "Parent symbol",
311 .se_cmp
= sort__parent_cmp
,
312 .se_snprintf
= hist_entry__parent_snprintf
,
313 .se_width_idx
= HISTC_PARENT
,
319 sort__cpu_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
321 return right
->cpu
- left
->cpu
;
324 static int hist_entry__cpu_snprintf(struct hist_entry
*self
, char *bf
,
325 size_t size
, unsigned int width
)
327 return repsep_snprintf(bf
, size
, "%*d", width
, self
->cpu
);
330 struct sort_entry sort_cpu
= {
332 .se_cmp
= sort__cpu_cmp
,
333 .se_snprintf
= hist_entry__cpu_snprintf
,
334 .se_width_idx
= HISTC_CPU
,
337 /* sort keys for branch stacks */
340 sort__dso_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
342 return _sort__dso_cmp(left
->branch_info
->from
.map
,
343 right
->branch_info
->from
.map
);
346 static int hist_entry__dso_from_snprintf(struct hist_entry
*self
, char *bf
,
347 size_t size
, unsigned int width
)
349 return _hist_entry__dso_snprintf(self
->branch_info
->from
.map
,
354 sort__dso_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
356 return _sort__dso_cmp(left
->branch_info
->to
.map
,
357 right
->branch_info
->to
.map
);
360 static int hist_entry__dso_to_snprintf(struct hist_entry
*self
, char *bf
,
361 size_t size
, unsigned int width
)
363 return _hist_entry__dso_snprintf(self
->branch_info
->to
.map
,
368 sort__sym_from_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
370 struct addr_map_symbol
*from_l
= &left
->branch_info
->from
;
371 struct addr_map_symbol
*from_r
= &right
->branch_info
->from
;
373 if (!from_l
->sym
&& !from_r
->sym
)
374 return right
->level
- left
->level
;
376 return _sort__sym_cmp(from_l
->sym
, from_r
->sym
);
380 sort__sym_to_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
382 struct addr_map_symbol
*to_l
= &left
->branch_info
->to
;
383 struct addr_map_symbol
*to_r
= &right
->branch_info
->to
;
385 if (!to_l
->sym
&& !to_r
->sym
)
386 return right
->level
- left
->level
;
388 return _sort__sym_cmp(to_l
->sym
, to_r
->sym
);
391 static int hist_entry__sym_from_snprintf(struct hist_entry
*self
, char *bf
,
392 size_t size
, unsigned int width
)
394 struct addr_map_symbol
*from
= &self
->branch_info
->from
;
395 return _hist_entry__sym_snprintf(from
->map
, from
->sym
, from
->addr
,
396 self
->level
, bf
, size
, width
);
400 static int hist_entry__sym_to_snprintf(struct hist_entry
*self
, char *bf
,
401 size_t size
, unsigned int width
)
403 struct addr_map_symbol
*to
= &self
->branch_info
->to
;
404 return _hist_entry__sym_snprintf(to
->map
, to
->sym
, to
->addr
,
405 self
->level
, bf
, size
, width
);
409 struct sort_entry sort_dso_from
= {
410 .se_header
= "Source Shared Object",
411 .se_cmp
= sort__dso_from_cmp
,
412 .se_snprintf
= hist_entry__dso_from_snprintf
,
413 .se_width_idx
= HISTC_DSO_FROM
,
416 struct sort_entry sort_dso_to
= {
417 .se_header
= "Target Shared Object",
418 .se_cmp
= sort__dso_to_cmp
,
419 .se_snprintf
= hist_entry__dso_to_snprintf
,
420 .se_width_idx
= HISTC_DSO_TO
,
423 struct sort_entry sort_sym_from
= {
424 .se_header
= "Source Symbol",
425 .se_cmp
= sort__sym_from_cmp
,
426 .se_snprintf
= hist_entry__sym_from_snprintf
,
427 .se_width_idx
= HISTC_SYMBOL_FROM
,
430 struct sort_entry sort_sym_to
= {
431 .se_header
= "Target Symbol",
432 .se_cmp
= sort__sym_to_cmp
,
433 .se_snprintf
= hist_entry__sym_to_snprintf
,
434 .se_width_idx
= HISTC_SYMBOL_TO
,
438 sort__mispredict_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
440 const unsigned char mp
= left
->branch_info
->flags
.mispred
!=
441 right
->branch_info
->flags
.mispred
;
442 const unsigned char p
= left
->branch_info
->flags
.predicted
!=
443 right
->branch_info
->flags
.predicted
;
448 static int hist_entry__mispredict_snprintf(struct hist_entry
*self
, char *bf
,
449 size_t size
, unsigned int width
){
450 static const char *out
= "N/A";
452 if (self
->branch_info
->flags
.predicted
)
454 else if (self
->branch_info
->flags
.mispred
)
457 return repsep_snprintf(bf
, size
, "%-*s", width
, out
);
460 struct sort_entry sort_mispredict
= {
461 .se_header
= "Branch Mispredicted",
462 .se_cmp
= sort__mispredict_cmp
,
463 .se_snprintf
= hist_entry__mispredict_snprintf
,
464 .se_width_idx
= HISTC_MISPREDICT
,
467 struct sort_dimension
{
469 struct sort_entry
*entry
;
473 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
475 static struct sort_dimension common_sort_dimensions
[] = {
476 DIM(SORT_PID
, "pid", sort_thread
),
477 DIM(SORT_COMM
, "comm", sort_comm
),
478 DIM(SORT_DSO
, "dso", sort_dso
),
479 DIM(SORT_SYM
, "symbol", sort_sym
),
480 DIM(SORT_PARENT
, "parent", sort_parent
),
481 DIM(SORT_CPU
, "cpu", sort_cpu
),
482 DIM(SORT_SRCLINE
, "srcline", sort_srcline
),
487 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
489 static struct sort_dimension bstack_sort_dimensions
[] = {
490 DIM(SORT_DSO_FROM
, "dso_from", sort_dso_from
),
491 DIM(SORT_DSO_TO
, "dso_to", sort_dso_to
),
492 DIM(SORT_SYM_FROM
, "symbol_from", sort_sym_from
),
493 DIM(SORT_SYM_TO
, "symbol_to", sort_sym_to
),
494 DIM(SORT_MISPREDICT
, "mispredict", sort_mispredict
),
499 int sort_dimension__add(const char *tok
)
503 for (i
= 0; i
< ARRAY_SIZE(common_sort_dimensions
); i
++) {
504 struct sort_dimension
*sd
= &common_sort_dimensions
[i
];
506 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
509 if (sd
->entry
== &sort_parent
) {
510 int ret
= regcomp(&parent_regex
, parent_pattern
, REG_EXTENDED
);
514 regerror(ret
, &parent_regex
, err
, sizeof(err
));
515 pr_err("Invalid regex: %s\n%s", parent_pattern
, err
);
518 sort__has_parent
= 1;
519 } else if (sd
->entry
== &sort_sym
) {
526 if (sd
->entry
->se_collapse
)
527 sort__need_collapse
= 1;
529 if (list_empty(&hist_entry__sort_list
))
530 sort__first_dimension
= i
;
532 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
538 for (i
= 0; i
< ARRAY_SIZE(bstack_sort_dimensions
); i
++) {
539 struct sort_dimension
*sd
= &bstack_sort_dimensions
[i
];
541 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
544 if (sort__branch_mode
!= 1)
547 if (sd
->entry
== &sort_sym_from
|| sd
->entry
== &sort_sym_to
)
553 if (sd
->entry
->se_collapse
)
554 sort__need_collapse
= 1;
556 if (list_empty(&hist_entry__sort_list
))
557 sort__first_dimension
= i
+ __SORT_BRANCH_STACK
;
559 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
568 int setup_sorting(void)
570 char *tmp
, *tok
, *str
= strdup(sort_order
);
574 error("Not enough memory to setup sort keys");
578 for (tok
= strtok_r(str
, ", ", &tmp
);
579 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
580 ret
= sort_dimension__add(tok
);
581 if (ret
== -EINVAL
) {
582 error("Invalid --sort key: `%s'", tok
);
584 } else if (ret
== -ESRCH
) {
585 error("Unknown --sort key: `%s'", tok
);
594 void sort_entry__setup_elide(struct sort_entry
*self
, struct strlist
*list
,
595 const char *list_name
, FILE *fp
)
597 if (list
&& strlist__nr_entries(list
) == 1) {
599 fprintf(fp
, "# %s: %s\n", list_name
,
600 strlist__entry(list
, 0)->s
);