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;
11 unsigned int dsos__col_width;
12 unsigned int comms__col_width;
13 unsigned int threads__col_width;
14 static unsigned int parent_symbol__col_width;
17 LIST_HEAD(hist_entry__sort_list);
19 struct sort_entry sort_thread = {
20 .header = "Command: Pid",
21 .cmp = sort__thread_cmp,
22 .print = sort__thread_print,
23 .width = &threads__col_width,
26 struct sort_entry sort_comm = {
28 .cmp = sort__comm_cmp,
29 .collapse = sort__comm_collapse,
30 .print = sort__comm_print,
31 .width = &comms__col_width,
34 struct sort_entry sort_dso = {
35 .header = "Shared Object",
37 .print = sort__dso_print,
38 .width = &dsos__col_width,
41 struct sort_entry sort_sym = {
44 .print = sort__sym_print,
47 struct sort_entry sort_parent = {
48 .header = "Parent symbol",
49 .cmp = sort__parent_cmp,
50 .print = sort__parent_print,
51 .width = &parent_symbol__col_width,
54 struct sort_dimension {
56 struct sort_entry *entry;
60 static struct sort_dimension sort_dimensions[] = {
61 { .name = "pid", .entry = &sort_thread, },
62 { .name = "comm", .entry = &sort_comm, },
63 { .name = "dso", .entry = &sort_dso, },
64 { .name = "symbol", .entry = &sort_sym, },
65 { .name = "parent", .entry = &sort_parent, },
68 int64_t cmp_null(void *l, void *r)
81 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
83 return right->thread->pid - left->thread->pid;
86 int repsep_fprintf(FILE *fp, const char *fmt, ...)
93 n = vfprintf(fp, fmt, ap);
96 n = vasprintf(&bf, fmt, ap);
101 sep = strchr(sep, *field_sep);
115 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
117 return repsep_fprintf(fp, "%*s:%5d", width - 6,
118 self->thread->comm ?: "", self->thread->pid);
122 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
124 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
130 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
132 struct dso *dso_l = left->dso;
133 struct dso *dso_r = right->dso;
135 if (!dso_l || !dso_r)
136 return cmp_null(dso_l, dso_r);
138 return strcmp(dso_l->name, dso_r->name);
142 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
145 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
147 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
153 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
157 if (left->sym == right->sym)
160 ip_l = left->sym ? left->sym->start : left->ip;
161 ip_r = right->sym ? right->sym->start : right->ip;
163 return (int64_t)(ip_r - ip_l);
168 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
173 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
174 dso__symtab_origin(self->dso));
176 ret += repsep_fprintf(fp, "[%c] ", self->level);
178 ret += repsep_fprintf(fp, "%s", self->sym->name);
180 if (self->sym->module)
181 ret += repsep_fprintf(fp, "\t[%s]",
182 self->sym->module->name);
184 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
193 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
195 return right->thread->pid - left->thread->pid;
199 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
201 char *comm_l = left->thread->comm;
202 char *comm_r = right->thread->comm;
204 if (!comm_l || !comm_r)
205 return cmp_null(comm_l, comm_r);
207 return strcmp(comm_l, comm_r);
213 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
215 struct symbol *sym_l = left->parent;
216 struct symbol *sym_r = right->parent;
218 if (!sym_l || !sym_r)
219 return cmp_null(sym_l, sym_r);
221 return strcmp(sym_l->name, sym_r->name);
225 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
227 return repsep_fprintf(fp, "%-*s", width,
228 self->parent ? self->parent->name : "[other]");
231 int sort_dimension__add(const char *tok)
235 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
236 struct sort_dimension *sd = &sort_dimensions[i];
241 if (strncasecmp(tok, sd->name, strlen(tok)))
244 if (sd->entry->collapse)
245 sort__need_collapse = 1;
247 if (sd->entry == &sort_parent) {
248 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
252 regerror(ret, &parent_regex, err, sizeof(err));
253 fprintf(stderr, "Invalid regex: %s\n%s",
254 parent_pattern, err);
257 sort__has_parent = 1;
260 list_add_tail(&sd->entry->list, &hist_entry__sort_list);