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