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 bool sort__branch_mode;
13 enum sort_type sort__first_dimension;
17 LIST_HEAD(hist_entry__sort_list);
19 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
25 n = vsnprintf(bf, size, fmt, ap);
26 if (field_sep && n > 0) {
30 sep = strchr(sep, *field_sep);
40 static int64_t cmp_null(void *l, void *r)
53 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
55 return right->thread->pid - left->thread->pid;
58 static int hist_entry__thread_snprintf(struct hist_entry *self, char *bf,
59 size_t size, unsigned int width)
61 return repsep_snprintf(bf, size, "%*s:%5d", width,
62 self->thread->comm ?: "", self->thread->pid);
65 struct sort_entry sort_thread = {
66 .se_header = "Command: Pid",
67 .se_cmp = sort__thread_cmp,
68 .se_snprintf = hist_entry__thread_snprintf,
69 .se_width_idx = HISTC_THREAD,
75 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
77 return right->thread->pid - left->thread->pid;
81 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
83 char *comm_l = left->thread->comm;
84 char *comm_r = right->thread->comm;
86 if (!comm_l || !comm_r)
87 return cmp_null(comm_l, comm_r);
89 return strcmp(comm_l, comm_r);
92 static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf,
93 size_t size, unsigned int width)
95 return repsep_snprintf(bf, size, "%*s", width, self->thread->comm);
98 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
100 struct dso *dso_l = map_l ? map_l->dso : NULL;
101 struct dso *dso_r = map_r ? map_r->dso : NULL;
102 const char *dso_name_l, *dso_name_r;
104 if (!dso_l || !dso_r)
105 return cmp_null(dso_l, dso_r);
108 dso_name_l = dso_l->long_name;
109 dso_name_r = dso_r->long_name;
111 dso_name_l = dso_l->short_name;
112 dso_name_r = dso_r->short_name;
115 return strcmp(dso_name_l, dso_name_r);
118 struct sort_entry sort_comm = {
119 .se_header = "Command",
120 .se_cmp = sort__comm_cmp,
121 .se_collapse = sort__comm_collapse,
122 .se_snprintf = hist_entry__comm_snprintf,
123 .se_width_idx = HISTC_COMM,
129 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
131 return _sort__dso_cmp(left->ms.map, right->ms.map);
135 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r,
138 if (!sym_l || !sym_r)
139 return cmp_null(sym_l, sym_r);
149 return (int64_t)(ip_r - ip_l);
152 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
153 size_t size, unsigned int width)
155 if (map && map->dso) {
156 const char *dso_name = !verbose ? map->dso->short_name :
158 return repsep_snprintf(bf, size, "%-*s", width, dso_name);
161 return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
164 static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
165 size_t size, unsigned int width)
167 return _hist_entry__dso_snprintf(self->ms.map, bf, size, width);
170 static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
171 u64 ip, char level, char *bf, size_t size,
172 unsigned int width __used)
177 char o = map ? dso__symtab_origin(map->dso) : '!';
178 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
179 BITS_PER_LONG / 4, ip, o);
182 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
184 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
188 size_t len = BITS_PER_LONG / 4;
189 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
191 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
199 struct sort_entry sort_dso = {
200 .se_header = "Shared Object",
201 .se_cmp = sort__dso_cmp,
202 .se_snprintf = hist_entry__dso_snprintf,
203 .se_width_idx = HISTC_DSO,
206 static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
207 size_t size, unsigned int width __used)
209 return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
210 self->level, bf, size, width);
215 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
219 if (!left->ms.sym && !right->ms.sym)
220 return right->level - left->level;
222 if (!left->ms.sym || !right->ms.sym)
223 return cmp_null(left->ms.sym, right->ms.sym);
225 if (left->ms.sym == right->ms.sym)
228 ip_l = left->ms.sym->start;
229 ip_r = right->ms.sym->start;
231 return _sort__sym_cmp(left->ms.sym, right->ms.sym, ip_l, ip_r);
234 struct sort_entry sort_sym = {
235 .se_header = "Symbol",
236 .se_cmp = sort__sym_cmp,
237 .se_snprintf = hist_entry__sym_snprintf,
238 .se_width_idx = HISTC_SYMBOL,
244 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
246 struct symbol *sym_l = left->parent;
247 struct symbol *sym_r = right->parent;
249 if (!sym_l || !sym_r)
250 return cmp_null(sym_l, sym_r);
252 return strcmp(sym_l->name, sym_r->name);
255 static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
256 size_t size, unsigned int width)
258 return repsep_snprintf(bf, size, "%-*s", width,
259 self->parent ? self->parent->name : "[other]");
262 struct sort_entry sort_parent = {
263 .se_header = "Parent symbol",
264 .se_cmp = sort__parent_cmp,
265 .se_snprintf = hist_entry__parent_snprintf,
266 .se_width_idx = HISTC_PARENT,
272 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
274 return right->cpu - left->cpu;
277 static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
278 size_t size, unsigned int width)
280 return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
283 struct sort_entry sort_cpu = {
285 .se_cmp = sort__cpu_cmp,
286 .se_snprintf = hist_entry__cpu_snprintf,
287 .se_width_idx = HISTC_CPU,
291 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
293 return _sort__dso_cmp(left->branch_info->from.map,
294 right->branch_info->from.map);
297 static int hist_entry__dso_from_snprintf(struct hist_entry *self, char *bf,
298 size_t size, unsigned int width)
300 return _hist_entry__dso_snprintf(self->branch_info->from.map,
304 struct sort_entry sort_dso_from = {
305 .se_header = "Source Shared Object",
306 .se_cmp = sort__dso_from_cmp,
307 .se_snprintf = hist_entry__dso_from_snprintf,
308 .se_width_idx = HISTC_DSO_FROM,
312 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
314 return _sort__dso_cmp(left->branch_info->to.map,
315 right->branch_info->to.map);
318 static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf,
319 size_t size, unsigned int width)
321 return _hist_entry__dso_snprintf(self->branch_info->to.map,
326 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
328 struct addr_map_symbol *from_l = &left->branch_info->from;
329 struct addr_map_symbol *from_r = &right->branch_info->from;
331 if (!from_l->sym && !from_r->sym)
332 return right->level - left->level;
334 return _sort__sym_cmp(from_l->sym, from_r->sym, from_l->addr,
339 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
341 struct addr_map_symbol *to_l = &left->branch_info->to;
342 struct addr_map_symbol *to_r = &right->branch_info->to;
344 if (!to_l->sym && !to_r->sym)
345 return right->level - left->level;
347 return _sort__sym_cmp(to_l->sym, to_r->sym, to_l->addr, to_r->addr);
350 static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
351 size_t size, unsigned int width __used)
353 struct addr_map_symbol *from = &self->branch_info->from;
354 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
355 self->level, bf, size, width);
359 static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
360 size_t size, unsigned int width __used)
362 struct addr_map_symbol *to = &self->branch_info->to;
363 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
364 self->level, bf, size, width);
368 struct sort_entry sort_dso_to = {
369 .se_header = "Target Shared Object",
370 .se_cmp = sort__dso_to_cmp,
371 .se_snprintf = hist_entry__dso_to_snprintf,
372 .se_width_idx = HISTC_DSO_TO,
375 struct sort_entry sort_sym_from = {
376 .se_header = "Source Symbol",
377 .se_cmp = sort__sym_from_cmp,
378 .se_snprintf = hist_entry__sym_from_snprintf,
379 .se_width_idx = HISTC_SYMBOL_FROM,
382 struct sort_entry sort_sym_to = {
383 .se_header = "Target Symbol",
384 .se_cmp = sort__sym_to_cmp,
385 .se_snprintf = hist_entry__sym_to_snprintf,
386 .se_width_idx = HISTC_SYMBOL_TO,
390 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
392 const unsigned char mp = left->branch_info->flags.mispred !=
393 right->branch_info->flags.mispred;
394 const unsigned char p = left->branch_info->flags.predicted !=
395 right->branch_info->flags.predicted;
400 static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
401 size_t size, unsigned int width){
402 static const char *out = "N/A";
404 if (self->branch_info->flags.predicted)
406 else if (self->branch_info->flags.mispred)
409 return repsep_snprintf(bf, size, "%-*s", width, out);
412 struct sort_entry sort_mispredict = {
413 .se_header = "Branch Mispredicted",
414 .se_cmp = sort__mispredict_cmp,
415 .se_snprintf = hist_entry__mispredict_snprintf,
416 .se_width_idx = HISTC_MISPREDICT,
419 struct sort_dimension {
421 struct sort_entry *entry;
425 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
427 static struct sort_dimension sort_dimensions[] = {
428 DIM(SORT_PID, "pid", sort_thread),
429 DIM(SORT_COMM, "comm", sort_comm),
430 DIM(SORT_DSO, "dso", sort_dso),
431 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
432 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
433 DIM(SORT_SYM, "symbol", sort_sym),
434 DIM(SORT_SYM_TO, "symbol_from", sort_sym_from),
435 DIM(SORT_SYM_FROM, "symbol_to", sort_sym_to),
436 DIM(SORT_PARENT, "parent", sort_parent),
437 DIM(SORT_CPU, "cpu", sort_cpu),
438 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
441 int sort_dimension__add(const char *tok)
445 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
446 struct sort_dimension *sd = &sort_dimensions[i];
448 if (strncasecmp(tok, sd->name, strlen(tok)))
450 if (sd->entry == &sort_parent) {
451 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
455 regerror(ret, &parent_regex, err, sizeof(err));
456 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
459 sort__has_parent = 1;
465 if (sd->entry->se_collapse)
466 sort__need_collapse = 1;
468 if (list_empty(&hist_entry__sort_list)) {
469 if (!strcmp(sd->name, "pid"))
470 sort__first_dimension = SORT_PID;
471 else if (!strcmp(sd->name, "comm"))
472 sort__first_dimension = SORT_COMM;
473 else if (!strcmp(sd->name, "dso"))
474 sort__first_dimension = SORT_DSO;
475 else if (!strcmp(sd->name, "symbol"))
476 sort__first_dimension = SORT_SYM;
477 else if (!strcmp(sd->name, "parent"))
478 sort__first_dimension = SORT_PARENT;
479 else if (!strcmp(sd->name, "cpu"))
480 sort__first_dimension = SORT_CPU;
481 else if (!strcmp(sd->name, "symbol_from"))
482 sort__first_dimension = SORT_SYM_FROM;
483 else if (!strcmp(sd->name, "symbol_to"))
484 sort__first_dimension = SORT_SYM_TO;
485 else if (!strcmp(sd->name, "dso_from"))
486 sort__first_dimension = SORT_DSO_FROM;
487 else if (!strcmp(sd->name, "dso_to"))
488 sort__first_dimension = SORT_DSO_TO;
489 else if (!strcmp(sd->name, "mispredict"))
490 sort__first_dimension = SORT_MISPREDICT;
493 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
501 void setup_sorting(const char * const usagestr[], const struct option *opts)
503 char *tmp, *tok, *str = strdup(sort_order);
505 for (tok = strtok_r(str, ", ", &tmp);
506 tok; tok = strtok_r(NULL, ", ", &tmp)) {
507 if (sort_dimension__add(tok) < 0) {
508 error("Unknown --sort key: `%s'", tok);
509 usage_with_options(usagestr, opts);
516 void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
517 const char *list_name, FILE *fp)
519 if (list && strlist__nr_entries(list) == 1) {
521 fprintf(fp, "# %s: %s\n", list_name,
522 strlist__entry(list, 0)->s);