blob: dbaded90312c94e25f50252ab9eed334b46592eb [file] [log] [blame]
John Kacurdd68ada2009-09-24 18:02:49 +02001#include "sort.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03002#include "hist.h"
John Kacurdd68ada2009-09-24 18:02:49 +02003
4regex_t parent_regex;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -03005const char default_parent_pattern[] = "^sys_|^do_page_fault";
6const char *parent_pattern = default_parent_pattern;
7const char default_sort_order[] = "comm,dso,symbol";
8const char *sort_order = default_sort_order;
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +02009int sort__need_collapse = 0;
10int sort__has_parent = 0;
Namhyung Kim1af556402012-09-14 17:35:27 +090011int sort__has_sym = 0;
Stephane Eranian993ac882012-03-08 23:47:47 +010012int sort__branch_mode = -1; /* -1 = means not set */
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020013
14enum sort_type sort__first_dimension;
John Kacurdd68ada2009-09-24 18:02:49 +020015
John Kacurdd68ada2009-09-24 18:02:49 +020016LIST_HEAD(hist_entry__sort_list);
17
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030018static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
John Kacurdd68ada2009-09-24 18:02:49 +020019{
20 int n;
21 va_list ap;
22
23 va_start(ap, fmt);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030024 n = vsnprintf(bf, size, fmt, ap);
Jiri Olsa0ca0c132012-09-06 17:46:56 +020025 if (symbol_conf.field_sep && n > 0) {
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030026 char *sep = bf;
John Kacurdd68ada2009-09-24 18:02:49 +020027
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030028 while (1) {
Jiri Olsa0ca0c132012-09-06 17:46:56 +020029 sep = strchr(sep, *symbol_conf.field_sep);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030030 if (sep == NULL)
31 break;
32 *sep = '.';
John Kacurdd68ada2009-09-24 18:02:49 +020033 }
John Kacurdd68ada2009-09-24 18:02:49 +020034 }
35 va_end(ap);
Anton Blanchardb8327962012-03-07 11:42:49 +110036
37 if (n >= (int)size)
38 return size - 1;
John Kacurdd68ada2009-09-24 18:02:49 +020039 return n;
40}
41
Frederic Weisbecker872a8782011-06-29 03:14:52 +020042static int64_t cmp_null(void *l, void *r)
43{
44 if (!l && !r)
45 return 0;
46 else if (!l)
47 return -1;
48 else
49 return 1;
50}
51
52/* --sort pid */
53
54static int64_t
55sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
56{
57 return right->thread->pid - left->thread->pid;
58}
59
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030060static int hist_entry__thread_snprintf(struct hist_entry *self, char *bf,
61 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020062{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030063 return repsep_snprintf(bf, size, "%*s:%5d", width,
John Kacurdd68ada2009-09-24 18:02:49 +020064 self->thread->comm ?: "", self->thread->pid);
65}
66
Frederic Weisbecker872a8782011-06-29 03:14:52 +020067struct 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,
72};
73
74/* --sort comm */
75
76static int64_t
77sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
78{
79 return right->thread->pid - left->thread->pid;
80}
81
82static int64_t
83sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
84{
85 char *comm_l = left->thread->comm;
86 char *comm_r = right->thread->comm;
87
88 if (!comm_l || !comm_r)
89 return cmp_null(comm_l, comm_r);
90
91 return strcmp(comm_l, comm_r);
92}
93
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030094static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf,
95 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020096{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030097 return repsep_snprintf(bf, size, "%*s", width, self->thread->comm);
John Kacurdd68ada2009-09-24 18:02:49 +020098}
99
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100100static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
John Kacurdd68ada2009-09-24 18:02:49 +0200101{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100102 struct dso *dso_l = map_l ? map_l->dso : NULL;
103 struct dso *dso_r = map_r ? map_r->dso : NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300104 const char *dso_name_l, *dso_name_r;
John Kacurdd68ada2009-09-24 18:02:49 +0200105
106 if (!dso_l || !dso_r)
107 return cmp_null(dso_l, dso_r);
108
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300109 if (verbose) {
110 dso_name_l = dso_l->long_name;
111 dso_name_r = dso_r->long_name;
112 } else {
113 dso_name_l = dso_l->short_name;
114 dso_name_r = dso_r->short_name;
115 }
116
117 return strcmp(dso_name_l, dso_name_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200118}
119
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100120struct sort_entry sort_comm = {
121 .se_header = "Command",
122 .se_cmp = sort__comm_cmp,
123 .se_collapse = sort__comm_collapse,
124 .se_snprintf = hist_entry__comm_snprintf,
125 .se_width_idx = HISTC_COMM,
126};
127
128/* --sort dso */
129
130static int64_t
131sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
John Kacurdd68ada2009-09-24 18:02:49 +0200132{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100133 return _sort__dso_cmp(left->ms.map, right->ms.map);
134}
135
136
137static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r,
138 u64 ip_l, u64 ip_r)
139{
140 if (!sym_l || !sym_r)
141 return cmp_null(sym_l, sym_r);
142
143 if (sym_l == sym_r)
144 return 0;
145
Sasha Levin53985a72012-12-20 14:11:20 -0500146 ip_l = sym_l->start;
147 ip_r = sym_r->start;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100148
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;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300158 return repsep_snprintf(bf, size, "%-*s", width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300159 }
John Kacurdd68ada2009-09-24 18:02:49 +0200160
Ian Munsie1437a302010-12-06 13:37:04 +1100161 return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200162}
163
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100164static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
165 size_t size, unsigned int width)
166{
167 return _hist_entry__dso_snprintf(self->ms.map, bf, size, width);
168}
169
170static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
171 u64 ip, char level, char *bf, size_t size,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300172 unsigned int width __maybe_unused)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100173{
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);
180 }
181
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;
196}
197
198
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200199struct sort_entry sort_dso = {
200 .se_header = "Shared Object",
201 .se_cmp = sort__dso_cmp,
202 .se_snprintf = hist_entry__dso_snprintf,
203 .se_width_idx = HISTC_DSO,
204};
205
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100206static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300207 size_t size,
208 unsigned int width __maybe_unused)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100209{
210 return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
211 self->level, bf, size, width);
212}
John Kacurdd68ada2009-09-24 18:02:49 +0200213
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100214/* --sort symbol */
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200215static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200216sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
217{
218 u64 ip_l, ip_r;
219
Anton Blanchard6bb8f312011-08-31 11:51:45 +1000220 if (!left->ms.sym && !right->ms.sym)
221 return right->level - left->level;
222
223 if (!left->ms.sym || !right->ms.sym)
224 return cmp_null(left->ms.sym, right->ms.sym);
225
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300226 if (left->ms.sym == right->ms.sym)
John Kacurdd68ada2009-09-24 18:02:49 +0200227 return 0;
228
Anton Blanchard6bb8f312011-08-31 11:51:45 +1000229 ip_l = left->ms.sym->start;
230 ip_r = right->ms.sym->start;
John Kacurdd68ada2009-09-24 18:02:49 +0200231
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100232 return _sort__sym_cmp(left->ms.sym, right->ms.sym, ip_l, ip_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200233}
234
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200235struct sort_entry sort_sym = {
236 .se_header = "Symbol",
237 .se_cmp = sort__sym_cmp,
238 .se_snprintf = hist_entry__sym_snprintf,
239 .se_width_idx = HISTC_SYMBOL,
240};
John Kacurdd68ada2009-09-24 18:02:49 +0200241
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300242/* --sort srcline */
243
244static int64_t
245sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
246{
247 return (int64_t)(right->ip - left->ip);
248}
249
250static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300251 size_t size,
252 unsigned int width __maybe_unused)
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300253{
254 FILE *fp;
255 char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
256 size_t line_len;
257
258 if (path != NULL)
259 goto out_path;
260
Namhyung Kimffe10c62012-10-15 12:39:42 +0900261 if (!self->ms.map)
262 goto out_ip;
263
Namhyung Kim88481b62012-10-15 12:39:43 +0900264 if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
265 goto out_ip;
266
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300267 snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
268 self->ms.map->dso->long_name, self->ip);
269 fp = popen(cmd, "r");
270 if (!fp)
271 goto out_ip;
272
273 if (getline(&path, &line_len, fp) < 0 || !line_len)
274 goto out_ip;
275 fclose(fp);
276 self->srcline = strdup(path);
277 if (self->srcline == NULL)
278 goto out_ip;
279
280 nl = strchr(self->srcline, '\n');
281 if (nl != NULL)
282 *nl = '\0';
283 path = self->srcline;
284out_path:
285 return repsep_snprintf(bf, size, "%s", path);
286out_ip:
287 return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
288}
289
290struct sort_entry sort_srcline = {
291 .se_header = "Source:Line",
292 .se_cmp = sort__srcline_cmp,
293 .se_snprintf = hist_entry__srcline_snprintf,
294 .se_width_idx = HISTC_SRCLINE,
295};
296
John Kacurdd68ada2009-09-24 18:02:49 +0200297/* --sort parent */
298
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200299static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200300sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
301{
302 struct symbol *sym_l = left->parent;
303 struct symbol *sym_r = right->parent;
304
305 if (!sym_l || !sym_r)
306 return cmp_null(sym_l, sym_r);
307
308 return strcmp(sym_l->name, sym_r->name);
309}
310
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300311static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
312 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200313{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300314 return repsep_snprintf(bf, size, "%-*s", width,
John Kacurdd68ada2009-09-24 18:02:49 +0200315 self->parent ? self->parent->name : "[other]");
316}
317
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200318struct sort_entry sort_parent = {
319 .se_header = "Parent symbol",
320 .se_cmp = sort__parent_cmp,
321 .se_snprintf = hist_entry__parent_snprintf,
322 .se_width_idx = HISTC_PARENT,
323};
324
Arun Sharmaf60f3592010-06-04 11:27:10 -0300325/* --sort cpu */
326
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200327static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300328sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
329{
330 return right->cpu - left->cpu;
331}
332
333static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
334 size_t size, unsigned int width)
335{
336 return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
337}
338
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200339struct sort_entry sort_cpu = {
340 .se_header = "CPU",
341 .se_cmp = sort__cpu_cmp,
342 .se_snprintf = hist_entry__cpu_snprintf,
343 .se_width_idx = HISTC_CPU,
344};
345
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100346static int64_t
347sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
348{
349 return _sort__dso_cmp(left->branch_info->from.map,
350 right->branch_info->from.map);
351}
352
353static int hist_entry__dso_from_snprintf(struct hist_entry *self, char *bf,
354 size_t size, unsigned int width)
355{
356 return _hist_entry__dso_snprintf(self->branch_info->from.map,
357 bf, size, width);
358}
359
360struct sort_entry sort_dso_from = {
361 .se_header = "Source Shared Object",
362 .se_cmp = sort__dso_from_cmp,
363 .se_snprintf = hist_entry__dso_from_snprintf,
364 .se_width_idx = HISTC_DSO_FROM,
365};
366
367static int64_t
368sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
369{
370 return _sort__dso_cmp(left->branch_info->to.map,
371 right->branch_info->to.map);
372}
373
374static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf,
375 size_t size, unsigned int width)
376{
377 return _hist_entry__dso_snprintf(self->branch_info->to.map,
378 bf, size, width);
379}
380
381static int64_t
382sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
383{
384 struct addr_map_symbol *from_l = &left->branch_info->from;
385 struct addr_map_symbol *from_r = &right->branch_info->from;
386
387 if (!from_l->sym && !from_r->sym)
388 return right->level - left->level;
389
390 return _sort__sym_cmp(from_l->sym, from_r->sym, from_l->addr,
391 from_r->addr);
392}
393
394static int64_t
395sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
396{
397 struct addr_map_symbol *to_l = &left->branch_info->to;
398 struct addr_map_symbol *to_r = &right->branch_info->to;
399
400 if (!to_l->sym && !to_r->sym)
401 return right->level - left->level;
402
403 return _sort__sym_cmp(to_l->sym, to_r->sym, to_l->addr, to_r->addr);
404}
405
406static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300407 size_t size,
408 unsigned int width __maybe_unused)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100409{
410 struct addr_map_symbol *from = &self->branch_info->from;
411 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
412 self->level, bf, size, width);
413
414}
415
416static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300417 size_t size,
418 unsigned int width __maybe_unused)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100419{
420 struct addr_map_symbol *to = &self->branch_info->to;
421 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
422 self->level, bf, size, width);
423
424}
425
426struct sort_entry sort_dso_to = {
427 .se_header = "Target Shared Object",
428 .se_cmp = sort__dso_to_cmp,
429 .se_snprintf = hist_entry__dso_to_snprintf,
430 .se_width_idx = HISTC_DSO_TO,
431};
432
433struct sort_entry sort_sym_from = {
434 .se_header = "Source Symbol",
435 .se_cmp = sort__sym_from_cmp,
436 .se_snprintf = hist_entry__sym_from_snprintf,
437 .se_width_idx = HISTC_SYMBOL_FROM,
438};
439
440struct sort_entry sort_sym_to = {
441 .se_header = "Target Symbol",
442 .se_cmp = sort__sym_to_cmp,
443 .se_snprintf = hist_entry__sym_to_snprintf,
444 .se_width_idx = HISTC_SYMBOL_TO,
445};
446
447static int64_t
448sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
449{
450 const unsigned char mp = left->branch_info->flags.mispred !=
451 right->branch_info->flags.mispred;
452 const unsigned char p = left->branch_info->flags.predicted !=
453 right->branch_info->flags.predicted;
454
455 return mp || p;
456}
457
458static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
459 size_t size, unsigned int width){
460 static const char *out = "N/A";
461
462 if (self->branch_info->flags.predicted)
463 out = "N";
464 else if (self->branch_info->flags.mispred)
465 out = "Y";
466
467 return repsep_snprintf(bf, size, "%-*s", width, out);
468}
469
470struct sort_entry sort_mispredict = {
471 .se_header = "Branch Mispredicted",
472 .se_cmp = sort__mispredict_cmp,
473 .se_snprintf = hist_entry__mispredict_snprintf,
474 .se_width_idx = HISTC_MISPREDICT,
475};
476
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200477struct sort_dimension {
478 const char *name;
479 struct sort_entry *entry;
480 int taken;
481};
482
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100483#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
484
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200485static struct sort_dimension sort_dimensions[] = {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100486 DIM(SORT_PID, "pid", sort_thread),
487 DIM(SORT_COMM, "comm", sort_comm),
488 DIM(SORT_DSO, "dso", sort_dso),
489 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
490 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
491 DIM(SORT_SYM, "symbol", sort_sym),
492 DIM(SORT_SYM_TO, "symbol_from", sort_sym_from),
493 DIM(SORT_SYM_FROM, "symbol_to", sort_sym_to),
494 DIM(SORT_PARENT, "parent", sort_parent),
495 DIM(SORT_CPU, "cpu", sort_cpu),
496 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300497 DIM(SORT_SRCLINE, "srcline", sort_srcline),
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200498};
499
John Kacurdd68ada2009-09-24 18:02:49 +0200500int sort_dimension__add(const char *tok)
501{
502 unsigned int i;
503
504 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
505 struct sort_dimension *sd = &sort_dimensions[i];
506
John Kacurdd68ada2009-09-24 18:02:49 +0200507 if (strncasecmp(tok, sd->name, strlen(tok)))
508 continue;
John Kacurdd68ada2009-09-24 18:02:49 +0200509 if (sd->entry == &sort_parent) {
510 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
511 if (ret) {
512 char err[BUFSIZ];
513
514 regerror(ret, &parent_regex, err, sizeof(err));
Arnaldo Carvalho de Melo2aefa4f2010-04-02 12:30:57 -0300515 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
516 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +0200517 }
518 sort__has_parent = 1;
Namhyung Kim1af556402012-09-14 17:35:27 +0900519 } else if (sd->entry == &sort_sym ||
520 sd->entry == &sort_sym_from ||
521 sd->entry == &sort_sym_to) {
522 sort__has_sym = 1;
John Kacurdd68ada2009-09-24 18:02:49 +0200523 }
524
Frederic Weisbeckerfd8ea212011-06-29 23:08:14 +0200525 if (sd->taken)
526 return 0;
527
528 if (sd->entry->se_collapse)
529 sort__need_collapse = 1;
530
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200531 if (list_empty(&hist_entry__sort_list)) {
532 if (!strcmp(sd->name, "pid"))
533 sort__first_dimension = SORT_PID;
534 else if (!strcmp(sd->name, "comm"))
535 sort__first_dimension = SORT_COMM;
536 else if (!strcmp(sd->name, "dso"))
537 sort__first_dimension = SORT_DSO;
538 else if (!strcmp(sd->name, "symbol"))
539 sort__first_dimension = SORT_SYM;
540 else if (!strcmp(sd->name, "parent"))
541 sort__first_dimension = SORT_PARENT;
Arun Sharmaf60f3592010-06-04 11:27:10 -0300542 else if (!strcmp(sd->name, "cpu"))
543 sort__first_dimension = SORT_CPU;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100544 else if (!strcmp(sd->name, "symbol_from"))
545 sort__first_dimension = SORT_SYM_FROM;
546 else if (!strcmp(sd->name, "symbol_to"))
547 sort__first_dimension = SORT_SYM_TO;
548 else if (!strcmp(sd->name, "dso_from"))
549 sort__first_dimension = SORT_DSO_FROM;
550 else if (!strcmp(sd->name, "dso_to"))
551 sort__first_dimension = SORT_DSO_TO;
552 else if (!strcmp(sd->name, "mispredict"))
553 sort__first_dimension = SORT_MISPREDICT;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200554 }
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200555
John Kacurdd68ada2009-09-24 18:02:49 +0200556 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
557 sd->taken = 1;
558
559 return 0;
560 }
John Kacurdd68ada2009-09-24 18:02:49 +0200561 return -ESRCH;
562}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -0200563
564void setup_sorting(const char * const usagestr[], const struct option *opts)
565{
566 char *tmp, *tok, *str = strdup(sort_order);
567
568 for (tok = strtok_r(str, ", ", &tmp);
569 tok; tok = strtok_r(NULL, ", ", &tmp)) {
570 if (sort_dimension__add(tok) < 0) {
571 error("Unknown --sort key: `%s'", tok);
572 usage_with_options(usagestr, opts);
573 }
574 }
575
576 free(str);
577}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200578
579void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
580 const char *list_name, FILE *fp)
581{
582 if (list && strlist__nr_entries(list) == 1) {
583 if (fp != NULL)
584 fprintf(fp, "# %s: %s\n", list_name,
585 strlist__entry(list, 0)->s);
586 self->elide = true;
587 }
588}