4 char default_parent_pattern[] = "^sys_|^do_page_fault";
5 char *parent_pattern = default_parent_pattern;
6 char default_sort_order[] = "comm,dso,symbol";
7 char *sort_order = default_sort_order;
8 int sort__need_collapse = 0;
9 int sort__has_parent = 0;
10 int sort_by_sym_first;
12 unsigned int dsos__col_width;
13 unsigned int comms__col_width;
14 unsigned int threads__col_width;
15 static unsigned int parent_symbol__col_width;
18 LIST_HEAD(hist_entry__sort_list);
20 struct sort_entry sort_thread = {
21 .header = "Command: Pid",
22 .cmp = sort__thread_cmp,
23 .print = sort__thread_print,
24 .width = &threads__col_width,
27 struct sort_entry sort_comm = {
29 .cmp = sort__comm_cmp,
30 .collapse = sort__comm_collapse,
31 .print = sort__comm_print,
32 .width = &comms__col_width,
35 struct sort_entry sort_dso = {
36 .header = "Shared Object",
38 .print = sort__dso_print,
39 .width = &dsos__col_width,
42 struct sort_entry sort_sym = {
45 .print = sort__sym_print,
48 struct sort_entry sort_parent = {
49 .header = "Parent symbol",
50 .cmp = sort__parent_cmp,
51 .print = sort__parent_print,
52 .width = &parent_symbol__col_width,
55 struct sort_dimension {
57 struct sort_entry *entry;
61 static struct sort_dimension sort_dimensions[] = {
62 { .name = "pid", .entry = &sort_thread, },
63 { .name = "comm", .entry = &sort_comm, },
64 { .name = "dso", .entry = &sort_dso, },
65 { .name = "symbol", .entry = &sort_sym, },
66 { .name = "parent", .entry = &sort_parent, },
69 int64_t cmp_null(void *l, void *r)
82 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
84 return right->thread->pid - left->thread->pid;
87 int repsep_fprintf(FILE *fp, const char *fmt, ...)
94 n = vfprintf(fp, fmt, ap);
97 n = vasprintf(&bf, fmt, ap);
102 sep = strchr(sep, *field_sep);
116 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
118 return repsep_fprintf(fp, "%*s:%5d", width - 6,
119 self->thread->comm ?: "", self->thread->pid);
123 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
125 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
131 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
133 struct dso *dso_l = left->map ? left->map->dso : NULL;
134 struct dso *dso_r = right->map ? right->map->dso : NULL;
135 const char *dso_name_l, *dso_name_r;
137 if (!dso_l || !dso_r)
138 return cmp_null(dso_l, dso_r);
141 dso_name_l = dso_l->long_name;
142 dso_name_r = dso_r->long_name;
144 dso_name_l = dso_l->short_name;
145 dso_name_r = dso_r->short_name;
148 return strcmp(dso_name_l, dso_name_r);
152 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
154 if (self->map && self->map->dso) {
155 const char *dso_name = !verbose ? self->map->dso->short_name :
156 self->map->dso->long_name;
157 return repsep_fprintf(fp, "%-*s", width, dso_name);
160 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
166 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
170 if (left->sym == right->sym)
173 ip_l = left->sym ? left->sym->start : left->ip;
174 ip_r = right->sym ? right->sym->start : right->ip;
176 return (int64_t)(ip_r - ip_l);
181 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
186 char o = self->map ? dso__symtab_origin(self->map->dso) : '!';
187 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip, o);
190 ret += repsep_fprintf(fp, "[%c] ", self->level);
192 ret += repsep_fprintf(fp, "%s", self->sym->name);
194 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
202 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
204 return right->thread->pid - left->thread->pid;
208 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
210 char *comm_l = left->thread->comm;
211 char *comm_r = right->thread->comm;
213 if (!comm_l || !comm_r)
214 return cmp_null(comm_l, comm_r);
216 return strcmp(comm_l, comm_r);
222 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
224 struct symbol *sym_l = left->parent;
225 struct symbol *sym_r = right->parent;
227 if (!sym_l || !sym_r)
228 return cmp_null(sym_l, sym_r);
230 return strcmp(sym_l->name, sym_r->name);
234 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
236 return repsep_fprintf(fp, "%-*s", width,
237 self->parent ? self->parent->name : "[other]");
240 int sort_dimension__add(const char *tok)
244 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
245 struct sort_dimension *sd = &sort_dimensions[i];
250 if (strncasecmp(tok, sd->name, strlen(tok)))
253 if (sd->entry->collapse)
254 sort__need_collapse = 1;
256 if (sd->entry == &sort_parent) {
257 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
261 regerror(ret, &parent_regex, err, sizeof(err));
262 fprintf(stderr, "Invalid regex: %s\n%s",
263 parent_pattern, err);
266 sort__has_parent = 1;
269 if (list_empty(&hist_entry__sort_list) &&
270 !strcmp(sd->name, "symbol"))
271 sort_by_sym_first = true;
273 list_add_tail(&sd->entry->list, &hist_entry__sort_list);