blob: dd68f115d3927702d695d6a419fb2218d5db6fac [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
146 if (sym_l)
147 ip_l = sym_l->start;
148 if (sym_r)
149 ip_r = sym_r->start;
150
151 return (int64_t)(ip_r - ip_l);
152}
153
154static int _hist_entry__dso_snprintf(struct map *map, char *bf,
155 size_t size, unsigned int width)
156{
157 if (map && map->dso) {
158 const char *dso_name = !verbose ? map->dso->short_name :
159 map->dso->long_name;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300160 return repsep_snprintf(bf, size, "%-*s", width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300161 }
John Kacurdd68ada2009-09-24 18:02:49 +0200162
Ian Munsie1437a302010-12-06 13:37:04 +1100163 return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200164}
165
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100166static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
167 size_t size, unsigned int width)
168{
169 return _hist_entry__dso_snprintf(self->ms.map, bf, size, width);
170}
171
172static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
173 u64 ip, char level, char *bf, size_t size,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300174 unsigned int width __maybe_unused)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100175{
176 size_t ret = 0;
177
178 if (verbose) {
179 char o = map ? dso__symtab_origin(map->dso) : '!';
180 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
181 BITS_PER_LONG / 4, ip, o);
182 }
183
184 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
185 if (sym)
186 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
187 width - ret,
188 sym->name);
189 else {
190 size_t len = BITS_PER_LONG / 4;
191 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
192 len, ip);
193 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
194 width - ret, "");
195 }
196
197 return ret;
198}
199
200
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200201struct sort_entry sort_dso = {
202 .se_header = "Shared Object",
203 .se_cmp = sort__dso_cmp,
204 .se_snprintf = hist_entry__dso_snprintf,
205 .se_width_idx = HISTC_DSO,
206};
207
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100208static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300209 size_t size,
210 unsigned int width __maybe_unused)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100211{
212 return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
213 self->level, bf, size, width);
214}
John Kacurdd68ada2009-09-24 18:02:49 +0200215
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100216/* --sort symbol */
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200217static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200218sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
219{
220 u64 ip_l, ip_r;
221
Anton Blanchard6bb8f312011-08-31 11:51:45 +1000222 if (!left->ms.sym && !right->ms.sym)
223 return right->level - left->level;
224
225 if (!left->ms.sym || !right->ms.sym)
226 return cmp_null(left->ms.sym, right->ms.sym);
227
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300228 if (left->ms.sym == right->ms.sym)
John Kacurdd68ada2009-09-24 18:02:49 +0200229 return 0;
230
Anton Blanchard6bb8f312011-08-31 11:51:45 +1000231 ip_l = left->ms.sym->start;
232 ip_r = right->ms.sym->start;
John Kacurdd68ada2009-09-24 18:02:49 +0200233
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100234 return _sort__sym_cmp(left->ms.sym, right->ms.sym, ip_l, ip_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200235}
236
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200237struct sort_entry sort_sym = {
238 .se_header = "Symbol",
239 .se_cmp = sort__sym_cmp,
240 .se_snprintf = hist_entry__sym_snprintf,
241 .se_width_idx = HISTC_SYMBOL,
242};
John Kacurdd68ada2009-09-24 18:02:49 +0200243
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300244/* --sort srcline */
245
246static int64_t
247sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
248{
249 return (int64_t)(right->ip - left->ip);
250}
251
252static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300253 size_t size,
254 unsigned int width __maybe_unused)
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300255{
256 FILE *fp;
257 char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
258 size_t line_len;
259
260 if (path != NULL)
261 goto out_path;
262
Namhyung Kimffe10c62012-10-15 12:39:42 +0900263 if (!self->ms.map)
264 goto out_ip;
265
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300266 snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
267 self->ms.map->dso->long_name, self->ip);
268 fp = popen(cmd, "r");
269 if (!fp)
270 goto out_ip;
271
272 if (getline(&path, &line_len, fp) < 0 || !line_len)
273 goto out_ip;
274 fclose(fp);
275 self->srcline = strdup(path);
276 if (self->srcline == NULL)
277 goto out_ip;
278
279 nl = strchr(self->srcline, '\n');
280 if (nl != NULL)
281 *nl = '\0';
282 path = self->srcline;
283out_path:
284 return repsep_snprintf(bf, size, "%s", path);
285out_ip:
286 return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
287}
288
289struct sort_entry sort_srcline = {
290 .se_header = "Source:Line",
291 .se_cmp = sort__srcline_cmp,
292 .se_snprintf = hist_entry__srcline_snprintf,
293 .se_width_idx = HISTC_SRCLINE,
294};
295
John Kacurdd68ada2009-09-24 18:02:49 +0200296/* --sort parent */
297
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200298static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200299sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
300{
301 struct symbol *sym_l = left->parent;
302 struct symbol *sym_r = right->parent;
303
304 if (!sym_l || !sym_r)
305 return cmp_null(sym_l, sym_r);
306
307 return strcmp(sym_l->name, sym_r->name);
308}
309
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300310static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
311 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200312{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300313 return repsep_snprintf(bf, size, "%-*s", width,
John Kacurdd68ada2009-09-24 18:02:49 +0200314 self->parent ? self->parent->name : "[other]");
315}
316
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200317struct sort_entry sort_parent = {
318 .se_header = "Parent symbol",
319 .se_cmp = sort__parent_cmp,
320 .se_snprintf = hist_entry__parent_snprintf,
321 .se_width_idx = HISTC_PARENT,
322};
323
Arun Sharmaf60f3592010-06-04 11:27:10 -0300324/* --sort cpu */
325
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200326static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300327sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
328{
329 return right->cpu - left->cpu;
330}
331
332static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
333 size_t size, unsigned int width)
334{
335 return repsep_snprintf(bf, size, "%-*d", width, self->cpu);
336}
337
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200338struct sort_entry sort_cpu = {
339 .se_header = "CPU",
340 .se_cmp = sort__cpu_cmp,
341 .se_snprintf = hist_entry__cpu_snprintf,
342 .se_width_idx = HISTC_CPU,
343};
344
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100345static int64_t
346sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
347{
348 return _sort__dso_cmp(left->branch_info->from.map,
349 right->branch_info->from.map);
350}
351
352static int hist_entry__dso_from_snprintf(struct hist_entry *self, char *bf,
353 size_t size, unsigned int width)
354{
355 return _hist_entry__dso_snprintf(self->branch_info->from.map,
356 bf, size, width);
357}
358
359struct sort_entry sort_dso_from = {
360 .se_header = "Source Shared Object",
361 .se_cmp = sort__dso_from_cmp,
362 .se_snprintf = hist_entry__dso_from_snprintf,
363 .se_width_idx = HISTC_DSO_FROM,
364};
365
366static int64_t
367sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
368{
369 return _sort__dso_cmp(left->branch_info->to.map,
370 right->branch_info->to.map);
371}
372
373static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf,
374 size_t size, unsigned int width)
375{
376 return _hist_entry__dso_snprintf(self->branch_info->to.map,
377 bf, size, width);
378}
379
380static int64_t
381sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
382{
383 struct addr_map_symbol *from_l = &left->branch_info->from;
384 struct addr_map_symbol *from_r = &right->branch_info->from;
385
386 if (!from_l->sym && !from_r->sym)
387 return right->level - left->level;
388
389 return _sort__sym_cmp(from_l->sym, from_r->sym, from_l->addr,
390 from_r->addr);
391}
392
393static int64_t
394sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
395{
396 struct addr_map_symbol *to_l = &left->branch_info->to;
397 struct addr_map_symbol *to_r = &right->branch_info->to;
398
399 if (!to_l->sym && !to_r->sym)
400 return right->level - left->level;
401
402 return _sort__sym_cmp(to_l->sym, to_r->sym, to_l->addr, to_r->addr);
403}
404
405static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300406 size_t size,
407 unsigned int width __maybe_unused)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100408{
409 struct addr_map_symbol *from = &self->branch_info->from;
410 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
411 self->level, bf, size, width);
412
413}
414
415static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300416 size_t size,
417 unsigned int width __maybe_unused)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100418{
419 struct addr_map_symbol *to = &self->branch_info->to;
420 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
421 self->level, bf, size, width);
422
423}
424
425struct sort_entry sort_dso_to = {
426 .se_header = "Target Shared Object",
427 .se_cmp = sort__dso_to_cmp,
428 .se_snprintf = hist_entry__dso_to_snprintf,
429 .se_width_idx = HISTC_DSO_TO,
430};
431
432struct sort_entry sort_sym_from = {
433 .se_header = "Source Symbol",
434 .se_cmp = sort__sym_from_cmp,
435 .se_snprintf = hist_entry__sym_from_snprintf,
436 .se_width_idx = HISTC_SYMBOL_FROM,
437};
438
439struct sort_entry sort_sym_to = {
440 .se_header = "Target Symbol",
441 .se_cmp = sort__sym_to_cmp,
442 .se_snprintf = hist_entry__sym_to_snprintf,
443 .se_width_idx = HISTC_SYMBOL_TO,
444};
445
446static int64_t
447sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
448{
449 const unsigned char mp = left->branch_info->flags.mispred !=
450 right->branch_info->flags.mispred;
451 const unsigned char p = left->branch_info->flags.predicted !=
452 right->branch_info->flags.predicted;
453
454 return mp || p;
455}
456
457static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
458 size_t size, unsigned int width){
459 static const char *out = "N/A";
460
461 if (self->branch_info->flags.predicted)
462 out = "N";
463 else if (self->branch_info->flags.mispred)
464 out = "Y";
465
466 return repsep_snprintf(bf, size, "%-*s", width, out);
467}
468
469struct sort_entry sort_mispredict = {
470 .se_header = "Branch Mispredicted",
471 .se_cmp = sort__mispredict_cmp,
472 .se_snprintf = hist_entry__mispredict_snprintf,
473 .se_width_idx = HISTC_MISPREDICT,
474};
475
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200476struct sort_dimension {
477 const char *name;
478 struct sort_entry *entry;
479 int taken;
480};
481
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100482#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
483
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200484static struct sort_dimension sort_dimensions[] = {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100485 DIM(SORT_PID, "pid", sort_thread),
486 DIM(SORT_COMM, "comm", sort_comm),
487 DIM(SORT_DSO, "dso", sort_dso),
488 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
489 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
490 DIM(SORT_SYM, "symbol", sort_sym),
491 DIM(SORT_SYM_TO, "symbol_from", sort_sym_from),
492 DIM(SORT_SYM_FROM, "symbol_to", sort_sym_to),
493 DIM(SORT_PARENT, "parent", sort_parent),
494 DIM(SORT_CPU, "cpu", sort_cpu),
495 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300496 DIM(SORT_SRCLINE, "srcline", sort_srcline),
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200497};
498
John Kacurdd68ada2009-09-24 18:02:49 +0200499int sort_dimension__add(const char *tok)
500{
501 unsigned int i;
502
503 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
504 struct sort_dimension *sd = &sort_dimensions[i];
505
John Kacurdd68ada2009-09-24 18:02:49 +0200506 if (strncasecmp(tok, sd->name, strlen(tok)))
507 continue;
John Kacurdd68ada2009-09-24 18:02:49 +0200508 if (sd->entry == &sort_parent) {
509 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
510 if (ret) {
511 char err[BUFSIZ];
512
513 regerror(ret, &parent_regex, err, sizeof(err));
Arnaldo Carvalho de Melo2aefa4f2010-04-02 12:30:57 -0300514 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
515 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +0200516 }
517 sort__has_parent = 1;
Namhyung Kim1af556402012-09-14 17:35:27 +0900518 } else if (sd->entry == &sort_sym ||
519 sd->entry == &sort_sym_from ||
520 sd->entry == &sort_sym_to) {
521 sort__has_sym = 1;
John Kacurdd68ada2009-09-24 18:02:49 +0200522 }
523
Frederic Weisbeckerfd8ea212011-06-29 23:08:14 +0200524 if (sd->taken)
525 return 0;
526
527 if (sd->entry->se_collapse)
528 sort__need_collapse = 1;
529
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200530 if (list_empty(&hist_entry__sort_list)) {
531 if (!strcmp(sd->name, "pid"))
532 sort__first_dimension = SORT_PID;
533 else if (!strcmp(sd->name, "comm"))
534 sort__first_dimension = SORT_COMM;
535 else if (!strcmp(sd->name, "dso"))
536 sort__first_dimension = SORT_DSO;
537 else if (!strcmp(sd->name, "symbol"))
538 sort__first_dimension = SORT_SYM;
539 else if (!strcmp(sd->name, "parent"))
540 sort__first_dimension = SORT_PARENT;
Arun Sharmaf60f3592010-06-04 11:27:10 -0300541 else if (!strcmp(sd->name, "cpu"))
542 sort__first_dimension = SORT_CPU;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100543 else if (!strcmp(sd->name, "symbol_from"))
544 sort__first_dimension = SORT_SYM_FROM;
545 else if (!strcmp(sd->name, "symbol_to"))
546 sort__first_dimension = SORT_SYM_TO;
547 else if (!strcmp(sd->name, "dso_from"))
548 sort__first_dimension = SORT_DSO_FROM;
549 else if (!strcmp(sd->name, "dso_to"))
550 sort__first_dimension = SORT_DSO_TO;
551 else if (!strcmp(sd->name, "mispredict"))
552 sort__first_dimension = SORT_MISPREDICT;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200553 }
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200554
John Kacurdd68ada2009-09-24 18:02:49 +0200555 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
556 sd->taken = 1;
557
558 return 0;
559 }
John Kacurdd68ada2009-09-24 18:02:49 +0200560 return -ESRCH;
561}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -0200562
563void setup_sorting(const char * const usagestr[], const struct option *opts)
564{
565 char *tmp, *tok, *str = strdup(sort_order);
566
567 for (tok = strtok_r(str, ", ", &tmp);
568 tok; tok = strtok_r(NULL, ", ", &tmp)) {
569 if (sort_dimension__add(tok) < 0) {
570 error("Unknown --sort key: `%s'", tok);
571 usage_with_options(usagestr, opts);
572 }
573 }
574
575 free(str);
576}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200577
578void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
579 const char *list_name, FILE *fp)
580{
581 if (list && strlist__nr_entries(list) == 1) {
582 if (fp != NULL)
583 fprintf(fp, "# %s: %s\n", list_name,
584 strlist__entry(list, 0)->s);
585 self->elide = true;
586 }
587}