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
->map
? left
->map
->dso
: NULL
;
133 struct dso
*dso_r
= right
->map
? right
->map
->dso
: NULL
;
134 const char *dso_name_l
, *dso_name_r
;
136 if (!dso_l
|| !dso_r
)
137 return cmp_null(dso_l
, dso_r
);
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_print(FILE *fp
, struct hist_entry
*self
, unsigned int width
)
153 if (self
->map
&& self
->map
->dso
) {
154 const char *dso_name
= !verbose
? self
->map
->dso
->short_name
:
155 self
->map
->dso
->long_name
;
156 return repsep_fprintf(fp
, "%-*s", width
, dso_name
);
159 return repsep_fprintf(fp
, "%*llx", width
, (u64
)self
->ip
);
165 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
169 if (left
->sym
== right
->sym
)
172 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
173 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
175 return (int64_t)(ip_r
- ip_l
);
180 sort__sym_print(FILE *fp
, struct hist_entry
*self
, unsigned int width __used
)
185 char o
= self
->map
? dso__symtab_origin(self
->map
->dso
) : '!';
186 ret
+= repsep_fprintf(fp
, "%#018llx %c ", (u64
)self
->ip
, o
);
189 ret
+= repsep_fprintf(fp
, "[%c] ", self
->level
);
191 ret
+= repsep_fprintf(fp
, "%s", self
->sym
->name
);
193 ret
+= repsep_fprintf(fp
, "%#016llx", (u64
)self
->ip
);
201 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
203 return right
->thread
->pid
- left
->thread
->pid
;
207 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
209 char *comm_l
= left
->thread
->comm
;
210 char *comm_r
= right
->thread
->comm
;
212 if (!comm_l
|| !comm_r
)
213 return cmp_null(comm_l
, comm_r
);
215 return strcmp(comm_l
, comm_r
);
221 sort__parent_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
223 struct symbol
*sym_l
= left
->parent
;
224 struct symbol
*sym_r
= right
->parent
;
226 if (!sym_l
|| !sym_r
)
227 return cmp_null(sym_l
, sym_r
);
229 return strcmp(sym_l
->name
, sym_r
->name
);
233 sort__parent_print(FILE *fp
, struct hist_entry
*self
, unsigned int width
)
235 return repsep_fprintf(fp
, "%-*s", width
,
236 self
->parent
? self
->parent
->name
: "[other]");
239 int sort_dimension__add(const char *tok
)
243 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
244 struct sort_dimension
*sd
= &sort_dimensions
[i
];
249 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
252 if (sd
->entry
->collapse
)
253 sort__need_collapse
= 1;
255 if (sd
->entry
== &sort_parent
) {
256 int ret
= regcomp(&parent_regex
, parent_pattern
, REG_EXTENDED
);
260 regerror(ret
, &parent_regex
, err
, sizeof(err
));
261 fprintf(stderr
, "Invalid regex: %s\n%s",
262 parent_pattern
, err
);
265 sort__has_parent
= 1;
268 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);