4 const char default_parent_pattern
[] = "^sys_|^do_page_fault";
5 const char *parent_pattern
= default_parent_pattern
;
6 const char default_sort_order
[] = "comm,dso,symbol";
7 const char *sort_order
= default_sort_order
;
8 int sort__need_collapse
= 0;
9 int sort__has_parent
= 0;
11 enum sort_type sort__first_dimension
;
13 unsigned int dsos__col_width
;
14 unsigned int comms__col_width
;
15 unsigned int threads__col_width
;
16 static unsigned int parent_symbol__col_width
;
19 LIST_HEAD(hist_entry__sort_list
);
21 static int hist_entry__thread_snprintf(struct hist_entry
*self
, char *bf
,
22 size_t size
, unsigned int width
);
23 static int hist_entry__comm_snprintf(struct hist_entry
*self
, char *bf
,
24 size_t size
, unsigned int width
);
25 static int hist_entry__dso_snprintf(struct hist_entry
*self
, char *bf
,
26 size_t size
, unsigned int width
);
27 static int hist_entry__sym_snprintf(struct hist_entry
*self
, char *bf
,
28 size_t size
, unsigned int width
);
29 static int hist_entry__parent_snprintf(struct hist_entry
*self
, char *bf
,
30 size_t size
, unsigned int width
);
32 struct sort_entry sort_thread
= {
33 .se_header
= "Command: Pid",
34 .se_cmp
= sort__thread_cmp
,
35 .se_snprintf
= hist_entry__thread_snprintf
,
36 .se_width
= &threads__col_width
,
39 struct sort_entry sort_comm
= {
40 .se_header
= "Command",
41 .se_cmp
= sort__comm_cmp
,
42 .se_collapse
= sort__comm_collapse
,
43 .se_snprintf
= hist_entry__comm_snprintf
,
44 .se_width
= &comms__col_width
,
47 struct sort_entry sort_dso
= {
48 .se_header
= "Shared Object",
49 .se_cmp
= sort__dso_cmp
,
50 .se_snprintf
= hist_entry__dso_snprintf
,
51 .se_width
= &dsos__col_width
,
54 struct sort_entry sort_sym
= {
55 .se_header
= "Symbol",
56 .se_cmp
= sort__sym_cmp
,
57 .se_snprintf
= hist_entry__sym_snprintf
,
60 struct sort_entry sort_parent
= {
61 .se_header
= "Parent symbol",
62 .se_cmp
= sort__parent_cmp
,
63 .se_snprintf
= hist_entry__parent_snprintf
,
64 .se_width
= &parent_symbol__col_width
,
67 struct sort_dimension
{
69 struct sort_entry
*entry
;
73 static struct sort_dimension sort_dimensions
[] = {
74 { .name
= "pid", .entry
= &sort_thread
, },
75 { .name
= "comm", .entry
= &sort_comm
, },
76 { .name
= "dso", .entry
= &sort_dso
, },
77 { .name
= "symbol", .entry
= &sort_sym
, },
78 { .name
= "parent", .entry
= &sort_parent
, },
81 int64_t cmp_null(void *l
, void *r
)
94 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
96 return right
->thread
->pid
- left
->thread
->pid
;
99 static int repsep_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
105 n
= vsnprintf(bf
, size
, fmt
, ap
);
106 if (field_sep
&& n
> 0) {
110 sep
= strchr(sep
, *field_sep
);
120 static int hist_entry__thread_snprintf(struct hist_entry
*self
, char *bf
,
121 size_t size
, unsigned int width
)
123 return repsep_snprintf(bf
, size
, "%*s:%5d", width
,
124 self
->thread
->comm
?: "", self
->thread
->pid
);
127 static int hist_entry__comm_snprintf(struct hist_entry
*self
, char *bf
,
128 size_t size
, unsigned int width
)
130 return repsep_snprintf(bf
, size
, "%*s", width
, self
->thread
->comm
);
136 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
138 struct dso
*dso_l
= left
->ms
.map
? left
->ms
.map
->dso
: NULL
;
139 struct dso
*dso_r
= right
->ms
.map
? right
->ms
.map
->dso
: NULL
;
140 const char *dso_name_l
, *dso_name_r
;
142 if (!dso_l
|| !dso_r
)
143 return cmp_null(dso_l
, dso_r
);
146 dso_name_l
= dso_l
->long_name
;
147 dso_name_r
= dso_r
->long_name
;
149 dso_name_l
= dso_l
->short_name
;
150 dso_name_r
= dso_r
->short_name
;
153 return strcmp(dso_name_l
, dso_name_r
);
156 static int hist_entry__dso_snprintf(struct hist_entry
*self
, char *bf
,
157 size_t size
, unsigned int width
)
159 if (self
->ms
.map
&& self
->ms
.map
->dso
) {
160 const char *dso_name
= !verbose
? self
->ms
.map
->dso
->short_name
:
161 self
->ms
.map
->dso
->long_name
;
162 return repsep_snprintf(bf
, size
, "%-*s", width
, dso_name
);
165 return repsep_snprintf(bf
, size
, "%*Lx", width
, self
->ip
);
171 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
175 if (left
->ms
.sym
== right
->ms
.sym
)
178 ip_l
= left
->ms
.sym
? left
->ms
.sym
->start
: left
->ip
;
179 ip_r
= right
->ms
.sym
? right
->ms
.sym
->start
: right
->ip
;
181 return (int64_t)(ip_r
- ip_l
);
184 static int hist_entry__sym_snprintf(struct hist_entry
*self
, char *bf
,
185 size_t size
, unsigned int width __used
)
190 char o
= self
->ms
.map
? dso__symtab_origin(self
->ms
.map
->dso
) : '!';
191 ret
+= repsep_snprintf(bf
, size
, "%#018llx %c ", self
->ip
, o
);
194 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "[%c] ", self
->level
);
196 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%s",
199 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%#016llx", self
->ip
);
207 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
209 return right
->thread
->pid
- left
->thread
->pid
;
213 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
215 char *comm_l
= left
->thread
->comm
;
216 char *comm_r
= right
->thread
->comm
;
218 if (!comm_l
|| !comm_r
)
219 return cmp_null(comm_l
, comm_r
);
221 return strcmp(comm_l
, comm_r
);
227 sort__parent_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
229 struct symbol
*sym_l
= left
->parent
;
230 struct symbol
*sym_r
= right
->parent
;
232 if (!sym_l
|| !sym_r
)
233 return cmp_null(sym_l
, sym_r
);
235 return strcmp(sym_l
->name
, sym_r
->name
);
238 static int hist_entry__parent_snprintf(struct hist_entry
*self
, char *bf
,
239 size_t size
, unsigned int width
)
241 return repsep_snprintf(bf
, size
, "%-*s", width
,
242 self
->parent
? self
->parent
->name
: "[other]");
245 int sort_dimension__add(const char *tok
)
249 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
250 struct sort_dimension
*sd
= &sort_dimensions
[i
];
255 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
258 if (sd
->entry
->se_collapse
)
259 sort__need_collapse
= 1;
261 if (sd
->entry
== &sort_parent
) {
262 int ret
= regcomp(&parent_regex
, parent_pattern
, REG_EXTENDED
);
266 regerror(ret
, &parent_regex
, err
, sizeof(err
));
267 pr_err("Invalid regex: %s\n%s", parent_pattern
, err
);
270 sort__has_parent
= 1;
273 if (list_empty(&hist_entry__sort_list
)) {
274 if (!strcmp(sd
->name
, "pid"))
275 sort__first_dimension
= SORT_PID
;
276 else if (!strcmp(sd
->name
, "comm"))
277 sort__first_dimension
= SORT_COMM
;
278 else if (!strcmp(sd
->name
, "dso"))
279 sort__first_dimension
= SORT_DSO
;
280 else if (!strcmp(sd
->name
, "symbol"))
281 sort__first_dimension
= SORT_SYM
;
282 else if (!strcmp(sd
->name
, "parent"))
283 sort__first_dimension
= SORT_PARENT
;
286 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
295 void setup_sorting(const char * const usagestr
[], const struct option
*opts
)
297 char *tmp
, *tok
, *str
= strdup(sort_order
);
299 for (tok
= strtok_r(str
, ", ", &tmp
);
300 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
301 if (sort_dimension__add(tok
) < 0) {
302 error("Unknown --sort key: `%s'", tok
);
303 usage_with_options(usagestr
, opts
);
310 void sort_entry__setup_elide(struct sort_entry
*self
, struct strlist
*list
,
311 const char *list_name
, FILE *fp
)
313 if (list
&& strlist__nr_entries(list
) == 1) {
315 fprintf(fp
, "# %s: %s\n", list_name
,
316 strlist__entry(list
, 0)->s
);