diff options
Diffstat (limited to 'tools/perf/util/sort.c')
-rw-r--r-- | tools/perf/util/sort.c | 290 |
1 files changed, 239 insertions, 51 deletions
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index 16da30d8d765..a27237430c5f 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c | |||
@@ -8,6 +8,7 @@ const char default_sort_order[] = "comm,dso,symbol"; | |||
8 | const char *sort_order = default_sort_order; | 8 | const char *sort_order = default_sort_order; |
9 | int sort__need_collapse = 0; | 9 | int sort__need_collapse = 0; |
10 | int sort__has_parent = 0; | 10 | int sort__has_parent = 0; |
11 | int sort__branch_mode = -1; /* -1 = means not set */ | ||
11 | 12 | ||
12 | enum sort_type sort__first_dimension; | 13 | enum sort_type sort__first_dimension; |
13 | 14 | ||
@@ -33,6 +34,9 @@ static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...) | |||
33 | } | 34 | } |
34 | } | 35 | } |
35 | va_end(ap); | 36 | va_end(ap); |
37 | |||
38 | if (n >= (int)size) | ||
39 | return size - 1; | ||
36 | return n; | 40 | return n; |
37 | } | 41 | } |
38 | 42 | ||
@@ -94,6 +98,26 @@ static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf, | |||
94 | return repsep_snprintf(bf, size, "%*s", width, self->thread->comm); | 98 | return repsep_snprintf(bf, size, "%*s", width, self->thread->comm); |
95 | } | 99 | } |
96 | 100 | ||
101 | static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r) | ||
102 | { | ||
103 | struct dso *dso_l = map_l ? map_l->dso : NULL; | ||
104 | struct dso *dso_r = map_r ? map_r->dso : NULL; | ||
105 | const char *dso_name_l, *dso_name_r; | ||
106 | |||
107 | if (!dso_l || !dso_r) | ||
108 | return cmp_null(dso_l, dso_r); | ||
109 | |||
110 | if (verbose) { | ||
111 | dso_name_l = dso_l->long_name; | ||
112 | dso_name_r = dso_r->long_name; | ||
113 | } else { | ||
114 | dso_name_l = dso_l->short_name; | ||
115 | dso_name_r = dso_r->short_name; | ||
116 | } | ||
117 | |||
118 | return strcmp(dso_name_l, dso_name_r); | ||
119 | } | ||
120 | |||
97 | struct sort_entry sort_comm = { | 121 | struct sort_entry sort_comm = { |
98 | .se_header = "Command", | 122 | .se_header = "Command", |
99 | .se_cmp = sort__comm_cmp, | 123 | .se_cmp = sort__comm_cmp, |
@@ -107,36 +131,74 @@ struct sort_entry sort_comm = { | |||
107 | static int64_t | 131 | static int64_t |
108 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) | 132 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) |
109 | { | 133 | { |
110 | struct dso *dso_l = left->ms.map ? left->ms.map->dso : NULL; | 134 | return _sort__dso_cmp(left->ms.map, right->ms.map); |
111 | struct dso *dso_r = right->ms.map ? right->ms.map->dso : NULL; | 135 | } |
112 | const char *dso_name_l, *dso_name_r; | ||
113 | 136 | ||
114 | if (!dso_l || !dso_r) | ||
115 | return cmp_null(dso_l, dso_r); | ||
116 | 137 | ||
117 | if (verbose) { | 138 | static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r, |
118 | dso_name_l = dso_l->long_name; | 139 | u64 ip_l, u64 ip_r) |
119 | dso_name_r = dso_r->long_name; | 140 | { |
120 | } else { | 141 | if (!sym_l || !sym_r) |
121 | dso_name_l = dso_l->short_name; | 142 | return cmp_null(sym_l, sym_r); |
122 | dso_name_r = dso_r->short_name; | 143 | |
144 | if (sym_l == sym_r) | ||
145 | return 0; | ||
146 | |||
147 | if (sym_l) | ||
148 | ip_l = sym_l->start; | ||
149 | if (sym_r) | ||
150 | ip_r = sym_r->start; | ||
151 | |||
152 | return (int64_t)(ip_r - ip_l); | ||
153 | } | ||
154 | |||
155 | static int _hist_entry__dso_snprintf(struct map *map, char *bf, | ||
156 | size_t size, unsigned int width) | ||
157 | { | ||
158 | if (map && map->dso) { | ||
159 | const char *dso_name = !verbose ? map->dso->short_name : | ||
160 | map->dso->long_name; | ||
161 | return repsep_snprintf(bf, size, "%-*s", width, dso_name); | ||
123 | } | 162 | } |
124 | 163 | ||
125 | return strcmp(dso_name_l, dso_name_r); | 164 | return repsep_snprintf(bf, size, "%-*s", width, "[unknown]"); |
126 | } | 165 | } |
127 | 166 | ||
128 | static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf, | 167 | static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf, |
129 | size_t size, unsigned int width) | 168 | size_t size, unsigned int width) |
130 | { | 169 | { |
131 | if (self->ms.map && self->ms.map->dso) { | 170 | return _hist_entry__dso_snprintf(self->ms.map, bf, size, width); |
132 | const char *dso_name = !verbose ? self->ms.map->dso->short_name : | 171 | } |
133 | self->ms.map->dso->long_name; | 172 | |
134 | return repsep_snprintf(bf, size, "%-*s", width, dso_name); | 173 | static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym, |
174 | u64 ip, char level, char *bf, size_t size, | ||
175 | unsigned int width __used) | ||
176 | { | ||
177 | size_t ret = 0; | ||
178 | |||
179 | if (verbose) { | ||
180 | char o = map ? dso__symtab_origin(map->dso) : '!'; | ||
181 | ret += repsep_snprintf(bf, size, "%-#*llx %c ", | ||
182 | BITS_PER_LONG / 4, ip, o); | ||
135 | } | 183 | } |
136 | 184 | ||
137 | return repsep_snprintf(bf, size, "%-*s", width, "[unknown]"); | 185 | ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level); |
186 | if (sym) | ||
187 | ret += repsep_snprintf(bf + ret, size - ret, "%-*s", | ||
188 | width - ret, | ||
189 | sym->name); | ||
190 | else { | ||
191 | size_t len = BITS_PER_LONG / 4; | ||
192 | ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", | ||
193 | len, ip); | ||
194 | ret += repsep_snprintf(bf + ret, size - ret, "%-*s", | ||
195 | width - ret, ""); | ||
196 | } | ||
197 | |||
198 | return ret; | ||
138 | } | 199 | } |
139 | 200 | ||
201 | |||
140 | struct sort_entry sort_dso = { | 202 | struct sort_entry sort_dso = { |
141 | .se_header = "Shared Object", | 203 | .se_header = "Shared Object", |
142 | .se_cmp = sort__dso_cmp, | 204 | .se_cmp = sort__dso_cmp, |
@@ -144,8 +206,14 @@ struct sort_entry sort_dso = { | |||
144 | .se_width_idx = HISTC_DSO, | 206 | .se_width_idx = HISTC_DSO, |
145 | }; | 207 | }; |
146 | 208 | ||
147 | /* --sort symbol */ | 209 | static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf, |
210 | size_t size, unsigned int width __used) | ||
211 | { | ||
212 | return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip, | ||
213 | self->level, bf, size, width); | ||
214 | } | ||
148 | 215 | ||
216 | /* --sort symbol */ | ||
149 | static int64_t | 217 | static int64_t |
150 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) | 218 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) |
151 | { | 219 | { |
@@ -163,31 +231,7 @@ sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) | |||
163 | ip_l = left->ms.sym->start; | 231 | ip_l = left->ms.sym->start; |
164 | ip_r = right->ms.sym->start; | 232 | ip_r = right->ms.sym->start; |
165 | 233 | ||
166 | return (int64_t)(ip_r - ip_l); | 234 | return _sort__sym_cmp(left->ms.sym, right->ms.sym, ip_l, ip_r); |
167 | } | ||
168 | |||
169 | static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf, | ||
170 | size_t size, unsigned int width __used) | ||
171 | { | ||
172 | size_t ret = 0; | ||
173 | |||
174 | if (verbose) { | ||
175 | char o = self->ms.map ? dso__symtab_origin(self->ms.map->dso) : '!'; | ||
176 | ret += repsep_snprintf(bf, size, "%-#*llx %c ", | ||
177 | BITS_PER_LONG / 4, self->ip, o); | ||
178 | } | ||
179 | |||
180 | if (!sort_dso.elide) | ||
181 | ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", self->level); | ||
182 | |||
183 | if (self->ms.sym) | ||
184 | ret += repsep_snprintf(bf + ret, size - ret, "%s", | ||
185 | self->ms.sym->name); | ||
186 | else | ||
187 | ret += repsep_snprintf(bf + ret, size - ret, "%-#*llx", | ||
188 | BITS_PER_LONG / 4, self->ip); | ||
189 | |||
190 | return ret; | ||
191 | } | 235 | } |
192 | 236 | ||
193 | struct sort_entry sort_sym = { | 237 | struct sort_entry sort_sym = { |
@@ -246,19 +290,155 @@ struct sort_entry sort_cpu = { | |||
246 | .se_width_idx = HISTC_CPU, | 290 | .se_width_idx = HISTC_CPU, |
247 | }; | 291 | }; |
248 | 292 | ||
293 | static int64_t | ||
294 | sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right) | ||
295 | { | ||
296 | return _sort__dso_cmp(left->branch_info->from.map, | ||
297 | right->branch_info->from.map); | ||
298 | } | ||
299 | |||
300 | static int hist_entry__dso_from_snprintf(struct hist_entry *self, char *bf, | ||
301 | size_t size, unsigned int width) | ||
302 | { | ||
303 | return _hist_entry__dso_snprintf(self->branch_info->from.map, | ||
304 | bf, size, width); | ||
305 | } | ||
306 | |||
307 | struct sort_entry sort_dso_from = { | ||
308 | .se_header = "Source Shared Object", | ||
309 | .se_cmp = sort__dso_from_cmp, | ||
310 | .se_snprintf = hist_entry__dso_from_snprintf, | ||
311 | .se_width_idx = HISTC_DSO_FROM, | ||
312 | }; | ||
313 | |||
314 | static int64_t | ||
315 | sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right) | ||
316 | { | ||
317 | return _sort__dso_cmp(left->branch_info->to.map, | ||
318 | right->branch_info->to.map); | ||
319 | } | ||
320 | |||
321 | static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf, | ||
322 | size_t size, unsigned int width) | ||
323 | { | ||
324 | return _hist_entry__dso_snprintf(self->branch_info->to.map, | ||
325 | bf, size, width); | ||
326 | } | ||
327 | |||
328 | static int64_t | ||
329 | sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right) | ||
330 | { | ||
331 | struct addr_map_symbol *from_l = &left->branch_info->from; | ||
332 | struct addr_map_symbol *from_r = &right->branch_info->from; | ||
333 | |||
334 | if (!from_l->sym && !from_r->sym) | ||
335 | return right->level - left->level; | ||
336 | |||
337 | return _sort__sym_cmp(from_l->sym, from_r->sym, from_l->addr, | ||
338 | from_r->addr); | ||
339 | } | ||
340 | |||
341 | static int64_t | ||
342 | sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right) | ||
343 | { | ||
344 | struct addr_map_symbol *to_l = &left->branch_info->to; | ||
345 | struct addr_map_symbol *to_r = &right->branch_info->to; | ||
346 | |||
347 | if (!to_l->sym && !to_r->sym) | ||
348 | return right->level - left->level; | ||
349 | |||
350 | return _sort__sym_cmp(to_l->sym, to_r->sym, to_l->addr, to_r->addr); | ||
351 | } | ||
352 | |||
353 | static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf, | ||
354 | size_t size, unsigned int width __used) | ||
355 | { | ||
356 | struct addr_map_symbol *from = &self->branch_info->from; | ||
357 | return _hist_entry__sym_snprintf(from->map, from->sym, from->addr, | ||
358 | self->level, bf, size, width); | ||
359 | |||
360 | } | ||
361 | |||
362 | static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf, | ||
363 | size_t size, unsigned int width __used) | ||
364 | { | ||
365 | struct addr_map_symbol *to = &self->branch_info->to; | ||
366 | return _hist_entry__sym_snprintf(to->map, to->sym, to->addr, | ||
367 | self->level, bf, size, width); | ||
368 | |||
369 | } | ||
370 | |||
371 | struct sort_entry sort_dso_to = { | ||
372 | .se_header = "Target Shared Object", | ||
373 | .se_cmp = sort__dso_to_cmp, | ||
374 | .se_snprintf = hist_entry__dso_to_snprintf, | ||
375 | .se_width_idx = HISTC_DSO_TO, | ||
376 | }; | ||
377 | |||
378 | struct sort_entry sort_sym_from = { | ||
379 | .se_header = "Source Symbol", | ||
380 | .se_cmp = sort__sym_from_cmp, | ||
381 | .se_snprintf = hist_entry__sym_from_snprintf, | ||
382 | .se_width_idx = HISTC_SYMBOL_FROM, | ||
383 | }; | ||
384 | |||
385 | struct sort_entry sort_sym_to = { | ||
386 | .se_header = "Target Symbol", | ||
387 | .se_cmp = sort__sym_to_cmp, | ||
388 | .se_snprintf = hist_entry__sym_to_snprintf, | ||
389 | .se_width_idx = HISTC_SYMBOL_TO, | ||
390 | }; | ||
391 | |||
392 | static int64_t | ||
393 | sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right) | ||
394 | { | ||
395 | const unsigned char mp = left->branch_info->flags.mispred != | ||
396 | right->branch_info->flags.mispred; | ||
397 | const unsigned char p = left->branch_info->flags.predicted != | ||
398 | right->branch_info->flags.predicted; | ||
399 | |||
400 | return mp || p; | ||
401 | } | ||
402 | |||
403 | static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf, | ||
404 | size_t size, unsigned int width){ | ||
405 | static const char *out = "N/A"; | ||
406 | |||
407 | if (self->branch_info->flags.predicted) | ||
408 | out = "N"; | ||
409 | else if (self->branch_info->flags.mispred) | ||
410 | out = "Y"; | ||
411 | |||
412 | return repsep_snprintf(bf, size, "%-*s", width, out); | ||
413 | } | ||
414 | |||
415 | struct sort_entry sort_mispredict = { | ||
416 | .se_header = "Branch Mispredicted", | ||
417 | .se_cmp = sort__mispredict_cmp, | ||
418 | .se_snprintf = hist_entry__mispredict_snprintf, | ||
419 | .se_width_idx = HISTC_MISPREDICT, | ||
420 | }; | ||
421 | |||
249 | struct sort_dimension { | 422 | struct sort_dimension { |
250 | const char *name; | 423 | const char *name; |
251 | struct sort_entry *entry; | 424 | struct sort_entry *entry; |
252 | int taken; | 425 | int taken; |
253 | }; | 426 | }; |
254 | 427 | ||
428 | #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) } | ||
429 | |||
255 | static struct sort_dimension sort_dimensions[] = { | 430 | static struct sort_dimension sort_dimensions[] = { |
256 | { .name = "pid", .entry = &sort_thread, }, | 431 | DIM(SORT_PID, "pid", sort_thread), |
257 | { .name = "comm", .entry = &sort_comm, }, | 432 | DIM(SORT_COMM, "comm", sort_comm), |
258 | { .name = "dso", .entry = &sort_dso, }, | 433 | DIM(SORT_DSO, "dso", sort_dso), |
259 | { .name = "symbol", .entry = &sort_sym, }, | 434 | DIM(SORT_DSO_FROM, "dso_from", sort_dso_from), |
260 | { .name = "parent", .entry = &sort_parent, }, | 435 | DIM(SORT_DSO_TO, "dso_to", sort_dso_to), |
261 | { .name = "cpu", .entry = &sort_cpu, }, | 436 | DIM(SORT_SYM, "symbol", sort_sym), |
437 | DIM(SORT_SYM_TO, "symbol_from", sort_sym_from), | ||
438 | DIM(SORT_SYM_FROM, "symbol_to", sort_sym_to), | ||
439 | DIM(SORT_PARENT, "parent", sort_parent), | ||
440 | DIM(SORT_CPU, "cpu", sort_cpu), | ||
441 | DIM(SORT_MISPREDICT, "mispredict", sort_mispredict), | ||
262 | }; | 442 | }; |
263 | 443 | ||
264 | int sort_dimension__add(const char *tok) | 444 | int sort_dimension__add(const char *tok) |
@@ -270,7 +450,6 @@ int sort_dimension__add(const char *tok) | |||
270 | 450 | ||
271 | if (strncasecmp(tok, sd->name, strlen(tok))) | 451 | if (strncasecmp(tok, sd->name, strlen(tok))) |
272 | continue; | 452 | continue; |
273 | |||
274 | if (sd->entry == &sort_parent) { | 453 | if (sd->entry == &sort_parent) { |
275 | int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED); | 454 | int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED); |
276 | if (ret) { | 455 | if (ret) { |
@@ -302,6 +481,16 @@ int sort_dimension__add(const char *tok) | |||
302 | sort__first_dimension = SORT_PARENT; | 481 | sort__first_dimension = SORT_PARENT; |
303 | else if (!strcmp(sd->name, "cpu")) | 482 | else if (!strcmp(sd->name, "cpu")) |
304 | sort__first_dimension = SORT_CPU; | 483 | sort__first_dimension = SORT_CPU; |
484 | else if (!strcmp(sd->name, "symbol_from")) | ||
485 | sort__first_dimension = SORT_SYM_FROM; | ||
486 | else if (!strcmp(sd->name, "symbol_to")) | ||
487 | sort__first_dimension = SORT_SYM_TO; | ||
488 | else if (!strcmp(sd->name, "dso_from")) | ||
489 | sort__first_dimension = SORT_DSO_FROM; | ||
490 | else if (!strcmp(sd->name, "dso_to")) | ||
491 | sort__first_dimension = SORT_DSO_TO; | ||
492 | else if (!strcmp(sd->name, "mispredict")) | ||
493 | sort__first_dimension = SORT_MISPREDICT; | ||
305 | } | 494 | } |
306 | 495 | ||
307 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); | 496 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); |
@@ -309,7 +498,6 @@ int sort_dimension__add(const char *tok) | |||
309 | 498 | ||
310 | return 0; | 499 | return 0; |
311 | } | 500 | } |
312 | |||
313 | return -ESRCH; | 501 | return -ESRCH; |
314 | } | 502 | } |
315 | 503 | ||