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;
12 enum sort_type sort__first_dimension
;
16 LIST_HEAD(hist_entry__sort_list
);
18 static int repsep_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
24 n
= vsnprintf(bf
, size
, fmt
, ap
);
25 if (field_sep
&& n
> 0) {
29 sep
= strchr(sep
, *field_sep
);
42 static int64_t cmp_null(void *l
, void *r
)
55 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
57 return right
->thread
->pid
- left
->thread
->pid
;
60 static int hist_entry__thread_snprintf(struct hist_entry
*self
, char *bf
,
61 size_t size
, unsigned int width
)
63 return repsep_snprintf(bf
, size
, "%*s:%5d", width
,
64 self
->thread
->comm
?: "", self
->thread
->pid
);
67 struct sort_entry sort_thread
= {
68 .se_header
= "Command: Pid",
69 .se_cmp
= sort__thread_cmp
,
70 .se_snprintf
= hist_entry__thread_snprintf
,
71 .se_width_idx
= HISTC_THREAD
,
77 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
79 return right
->thread
->pid
- left
->thread
->pid
;
83 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
85 char *comm_l
= left
->thread
->comm
;
86 char *comm_r
= right
->thread
->comm
;
88 if (!comm_l
|| !comm_r
)
89 return cmp_null(comm_l
, comm_r
);
91 return strcmp(comm_l
, comm_r
);
94 static int hist_entry__comm_snprintf(struct hist_entry
*self
, char *bf
,
95 size_t size
, unsigned int width
)
97 return repsep_snprintf(bf
, size
, "%*s", width
, self
->thread
->comm
);
100 struct sort_entry sort_comm
= {
101 .se_header
= "Command",
102 .se_cmp
= sort__comm_cmp
,
103 .se_collapse
= sort__comm_collapse
,
104 .se_snprintf
= hist_entry__comm_snprintf
,
105 .se_width_idx
= HISTC_COMM
,
111 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
113 struct dso
*dso_l
= left
->ms
.map
? left
->ms
.map
->dso
: NULL
;
114 struct dso
*dso_r
= right
->ms
.map
? right
->ms
.map
->dso
: NULL
;
115 const char *dso_name_l
, *dso_name_r
;
117 if (!dso_l
|| !dso_r
)
118 return cmp_null(dso_l
, dso_r
);
121 dso_name_l
= dso_l
->long_name
;
122 dso_name_r
= dso_r
->long_name
;
124 dso_name_l
= dso_l
->short_name
;
125 dso_name_r
= dso_r
->short_name
;
128 return strcmp(dso_name_l
, dso_name_r
);
131 static int hist_entry__dso_snprintf(struct hist_entry
*self
, char *bf
,
132 size_t size
, unsigned int width
)
134 if (self
->ms
.map
&& self
->ms
.map
->dso
) {
135 const char *dso_name
= !verbose
? self
->ms
.map
->dso
->short_name
:
136 self
->ms
.map
->dso
->long_name
;
137 return repsep_snprintf(bf
, size
, "%-*s", width
, dso_name
);
140 return repsep_snprintf(bf
, size
, "%-*s", width
, "[unknown]");
143 struct sort_entry sort_dso
= {
144 .se_header
= "Shared Object",
145 .se_cmp
= sort__dso_cmp
,
146 .se_snprintf
= hist_entry__dso_snprintf
,
147 .se_width_idx
= HISTC_DSO
,
153 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
157 if (!left
->ms
.sym
&& !right
->ms
.sym
)
158 return right
->level
- left
->level
;
160 if (!left
->ms
.sym
|| !right
->ms
.sym
)
161 return cmp_null(left
->ms
.sym
, right
->ms
.sym
);
163 if (left
->ms
.sym
== right
->ms
.sym
)
166 ip_l
= left
->ms
.sym
->start
;
167 ip_r
= right
->ms
.sym
->start
;
169 return (int64_t)(ip_r
- ip_l
);
172 static int hist_entry__sym_snprintf(struct hist_entry
*self
, char *bf
,
173 size_t size
, unsigned int width __used
)
178 char o
= self
->ms
.map
? dso__symtab_origin(self
->ms
.map
->dso
) : '!';
179 ret
+= repsep_snprintf(bf
, size
, "%-#*llx %c ",
180 BITS_PER_LONG
/ 4, self
->ip
, o
);
184 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "[%c] ", self
->level
);
187 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%s",
190 ret
+= repsep_snprintf(bf
+ ret
, size
- ret
, "%-#*llx",
191 BITS_PER_LONG
/ 4, self
->ip
);
196 struct sort_entry sort_sym
= {
197 .se_header
= "Symbol",
198 .se_cmp
= sort__sym_cmp
,
199 .se_snprintf
= hist_entry__sym_snprintf
,
200 .se_width_idx
= HISTC_SYMBOL
,
206 sort__parent_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
208 struct symbol
*sym_l
= left
->parent
;
209 struct symbol
*sym_r
= right
->parent
;
211 if (!sym_l
|| !sym_r
)
212 return cmp_null(sym_l
, sym_r
);
214 return strcmp(sym_l
->name
, sym_r
->name
);
217 static int hist_entry__parent_snprintf(struct hist_entry
*self
, char *bf
,
218 size_t size
, unsigned int width
)
220 return repsep_snprintf(bf
, size
, "%-*s", width
,
221 self
->parent
? self
->parent
->name
: "[other]");
224 struct sort_entry sort_parent
= {
225 .se_header
= "Parent symbol",
226 .se_cmp
= sort__parent_cmp
,
227 .se_snprintf
= hist_entry__parent_snprintf
,
228 .se_width_idx
= HISTC_PARENT
,
234 sort__cpu_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
236 return right
->cpu
- left
->cpu
;
239 static int hist_entry__cpu_snprintf(struct hist_entry
*self
, char *bf
,
240 size_t size
, unsigned int width
)
242 return repsep_snprintf(bf
, size
, "%-*d", width
, self
->cpu
);
245 struct sort_entry sort_cpu
= {
247 .se_cmp
= sort__cpu_cmp
,
248 .se_snprintf
= hist_entry__cpu_snprintf
,
249 .se_width_idx
= HISTC_CPU
,
252 struct sort_dimension
{
254 struct sort_entry
*entry
;
258 static struct sort_dimension sort_dimensions
[] = {
259 { .name
= "pid", .entry
= &sort_thread
, },
260 { .name
= "comm", .entry
= &sort_comm
, },
261 { .name
= "dso", .entry
= &sort_dso
, },
262 { .name
= "symbol", .entry
= &sort_sym
, },
263 { .name
= "parent", .entry
= &sort_parent
, },
264 { .name
= "cpu", .entry
= &sort_cpu
, },
267 int sort_dimension__add(const char *tok
)
271 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
272 struct sort_dimension
*sd
= &sort_dimensions
[i
];
274 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
277 if (sd
->entry
== &sort_parent
) {
278 int ret
= regcomp(&parent_regex
, parent_pattern
, REG_EXTENDED
);
282 regerror(ret
, &parent_regex
, err
, sizeof(err
));
283 pr_err("Invalid regex: %s\n%s", parent_pattern
, err
);
286 sort__has_parent
= 1;
292 if (sd
->entry
->se_collapse
)
293 sort__need_collapse
= 1;
295 if (list_empty(&hist_entry__sort_list
)) {
296 if (!strcmp(sd
->name
, "pid"))
297 sort__first_dimension
= SORT_PID
;
298 else if (!strcmp(sd
->name
, "comm"))
299 sort__first_dimension
= SORT_COMM
;
300 else if (!strcmp(sd
->name
, "dso"))
301 sort__first_dimension
= SORT_DSO
;
302 else if (!strcmp(sd
->name
, "symbol"))
303 sort__first_dimension
= SORT_SYM
;
304 else if (!strcmp(sd
->name
, "parent"))
305 sort__first_dimension
= SORT_PARENT
;
306 else if (!strcmp(sd
->name
, "cpu"))
307 sort__first_dimension
= SORT_CPU
;
310 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
319 void setup_sorting(const char * const usagestr
[], const struct option
*opts
)
321 char *tmp
, *tok
, *str
= strdup(sort_order
);
323 for (tok
= strtok_r(str
, ", ", &tmp
);
324 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
325 if (sort_dimension__add(tok
) < 0) {
326 error("Unknown --sort key: `%s'", tok
);
327 usage_with_options(usagestr
, opts
);
334 void sort_entry__setup_elide(struct sort_entry
*self
, struct strlist
*list
,
335 const char *list_name
, FILE *fp
)
337 if (list
&& strlist__nr_entries(list
) == 1) {
339 fprintf(fp
, "# %s: %s\n", list_name
,
340 strlist__entry(list
, 0)->s
);