blob: 7ad62393aa883b26a6c4c750bffa1ba467ff15c9 [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{
Namhyung Kimfb29a332012-12-27 18:11:40 +090063 return repsep_snprintf(bf, size, "%*s:%5d", width - 6,
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
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900100struct 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,
106};
107
108/* --sort dso */
109
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100110static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
John Kacurdd68ada2009-09-24 18:02:49 +0200111{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100112 struct dso *dso_l = map_l ? map_l->dso : NULL;
113 struct dso *dso_r = map_r ? map_r->dso : NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300114 const char *dso_name_l, *dso_name_r;
John Kacurdd68ada2009-09-24 18:02:49 +0200115
116 if (!dso_l || !dso_r)
117 return cmp_null(dso_l, dso_r);
118
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300119 if (verbose) {
120 dso_name_l = dso_l->long_name;
121 dso_name_r = dso_r->long_name;
122 } else {
123 dso_name_l = dso_l->short_name;
124 dso_name_r = dso_r->short_name;
125 }
126
127 return strcmp(dso_name_l, dso_name_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200128}
129
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100130static 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
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100136static int _hist_entry__dso_snprintf(struct map *map, char *bf,
137 size_t size, unsigned int width)
138{
139 if (map && map->dso) {
140 const char *dso_name = !verbose ? map->dso->short_name :
141 map->dso->long_name;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300142 return repsep_snprintf(bf, size, "%-*s", width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300143 }
John Kacurdd68ada2009-09-24 18:02:49 +0200144
Ian Munsie1437a302010-12-06 13:37:04 +1100145 return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200146}
147
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100148static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
149 size_t size, unsigned int width)
150{
151 return _hist_entry__dso_snprintf(self->ms.map, bf, size, width);
152}
153
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900154struct sort_entry sort_dso = {
155 .se_header = "Shared Object",
156 .se_cmp = sort__dso_cmp,
157 .se_snprintf = hist_entry__dso_snprintf,
158 .se_width_idx = HISTC_DSO,
159};
160
161/* --sort symbol */
162
163static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r,
164 u64 ip_l, u64 ip_r)
165{
166 if (!sym_l || !sym_r)
167 return cmp_null(sym_l, sym_r);
168
169 if (sym_l == sym_r)
170 return 0;
171
172 ip_l = sym_l->start;
173 ip_r = sym_r->start;
174
175 return (int64_t)(ip_r - ip_l);
176}
177
178static int64_t
179sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
180{
181 u64 ip_l, ip_r;
182
183 if (!left->ms.sym && !right->ms.sym)
184 return right->level - left->level;
185
186 if (!left->ms.sym || !right->ms.sym)
187 return cmp_null(left->ms.sym, right->ms.sym);
188
189 if (left->ms.sym == right->ms.sym)
190 return 0;
191
192 ip_l = left->ms.sym->start;
193 ip_r = right->ms.sym->start;
194
195 return _sort__sym_cmp(left->ms.sym, right->ms.sym, ip_l, ip_r);
196}
197
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100198static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
199 u64 ip, char level, char *bf, size_t size,
Namhyung Kim43355522012-12-27 18:11:39 +0900200 unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100201{
202 size_t ret = 0;
203
204 if (verbose) {
205 char o = map ? dso__symtab_origin(map->dso) : '!';
206 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
207 BITS_PER_LONG / 4, ip, o);
208 }
209
210 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
211 if (sym)
212 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
213 width - ret,
214 sym->name);
215 else {
216 size_t len = BITS_PER_LONG / 4;
217 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
218 len, ip);
219 ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
220 width - ret, "");
221 }
222
223 return ret;
224}
225
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100226static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900227 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100228{
229 return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
230 self->level, bf, size, width);
231}
John Kacurdd68ada2009-09-24 18:02:49 +0200232
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200233struct sort_entry sort_sym = {
234 .se_header = "Symbol",
235 .se_cmp = sort__sym_cmp,
236 .se_snprintf = hist_entry__sym_snprintf,
237 .se_width_idx = HISTC_SYMBOL,
238};
John Kacurdd68ada2009-09-24 18:02:49 +0200239
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300240/* --sort srcline */
241
242static int64_t
243sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
244{
245 return (int64_t)(right->ip - left->ip);
246}
247
248static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300249 size_t size,
250 unsigned int width __maybe_unused)
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300251{
252 FILE *fp;
253 char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
254 size_t line_len;
255
256 if (path != NULL)
257 goto out_path;
258
Namhyung Kimffe10c62012-10-15 12:39:42 +0900259 if (!self->ms.map)
260 goto out_ip;
261
Namhyung Kim88481b62012-10-15 12:39:43 +0900262 if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
263 goto out_ip;
264
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300265 snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
266 self->ms.map->dso->long_name, self->ip);
267 fp = popen(cmd, "r");
268 if (!fp)
269 goto out_ip;
270
271 if (getline(&path, &line_len, fp) < 0 || !line_len)
272 goto out_ip;
273 fclose(fp);
274 self->srcline = strdup(path);
275 if (self->srcline == NULL)
276 goto out_ip;
277
278 nl = strchr(self->srcline, '\n');
279 if (nl != NULL)
280 *nl = '\0';
281 path = self->srcline;
282out_path:
283 return repsep_snprintf(bf, size, "%s", path);
284out_ip:
285 return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
286}
287
288struct sort_entry sort_srcline = {
289 .se_header = "Source:Line",
290 .se_cmp = sort__srcline_cmp,
291 .se_snprintf = hist_entry__srcline_snprintf,
292 .se_width_idx = HISTC_SRCLINE,
293};
294
John Kacurdd68ada2009-09-24 18:02:49 +0200295/* --sort parent */
296
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200297static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200298sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
299{
300 struct symbol *sym_l = left->parent;
301 struct symbol *sym_r = right->parent;
302
303 if (!sym_l || !sym_r)
304 return cmp_null(sym_l, sym_r);
305
306 return strcmp(sym_l->name, sym_r->name);
307}
308
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300309static int hist_entry__parent_snprintf(struct hist_entry *self, char *bf,
310 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200311{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300312 return repsep_snprintf(bf, size, "%-*s", width,
John Kacurdd68ada2009-09-24 18:02:49 +0200313 self->parent ? self->parent->name : "[other]");
314}
315
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200316struct sort_entry sort_parent = {
317 .se_header = "Parent symbol",
318 .se_cmp = sort__parent_cmp,
319 .se_snprintf = hist_entry__parent_snprintf,
320 .se_width_idx = HISTC_PARENT,
321};
322
Arun Sharmaf60f3592010-06-04 11:27:10 -0300323/* --sort cpu */
324
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200325static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300326sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
327{
328 return right->cpu - left->cpu;
329}
330
331static int hist_entry__cpu_snprintf(struct hist_entry *self, char *bf,
332 size_t size, unsigned int width)
333{
Namhyung Kimdccf1802012-12-27 18:11:41 +0900334 return repsep_snprintf(bf, size, "%*d", width, self->cpu);
Arun Sharmaf60f3592010-06-04 11:27:10 -0300335}
336
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200337struct sort_entry sort_cpu = {
338 .se_header = "CPU",
339 .se_cmp = sort__cpu_cmp,
340 .se_snprintf = hist_entry__cpu_snprintf,
341 .se_width_idx = HISTC_CPU,
342};
343
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900344/* sort keys for branch stacks */
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
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100360static int64_t
361sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
362{
363 return _sort__dso_cmp(left->branch_info->to.map,
364 right->branch_info->to.map);
365}
366
367static int hist_entry__dso_to_snprintf(struct hist_entry *self, char *bf,
368 size_t size, unsigned int width)
369{
370 return _hist_entry__dso_snprintf(self->branch_info->to.map,
371 bf, size, width);
372}
373
374static int64_t
375sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
376{
377 struct addr_map_symbol *from_l = &left->branch_info->from;
378 struct addr_map_symbol *from_r = &right->branch_info->from;
379
380 if (!from_l->sym && !from_r->sym)
381 return right->level - left->level;
382
383 return _sort__sym_cmp(from_l->sym, from_r->sym, from_l->addr,
384 from_r->addr);
385}
386
387static int64_t
388sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
389{
390 struct addr_map_symbol *to_l = &left->branch_info->to;
391 struct addr_map_symbol *to_r = &right->branch_info->to;
392
393 if (!to_l->sym && !to_r->sym)
394 return right->level - left->level;
395
396 return _sort__sym_cmp(to_l->sym, to_r->sym, to_l->addr, to_r->addr);
397}
398
399static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900400 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100401{
402 struct addr_map_symbol *from = &self->branch_info->from;
403 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
404 self->level, bf, size, width);
405
406}
407
408static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900409 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100410{
411 struct addr_map_symbol *to = &self->branch_info->to;
412 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
413 self->level, bf, size, width);
414
415}
416
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900417struct sort_entry sort_dso_from = {
418 .se_header = "Source Shared Object",
419 .se_cmp = sort__dso_from_cmp,
420 .se_snprintf = hist_entry__dso_from_snprintf,
421 .se_width_idx = HISTC_DSO_FROM,
422};
423
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100424struct sort_entry sort_dso_to = {
425 .se_header = "Target Shared Object",
426 .se_cmp = sort__dso_to_cmp,
427 .se_snprintf = hist_entry__dso_to_snprintf,
428 .se_width_idx = HISTC_DSO_TO,
429};
430
431struct sort_entry sort_sym_from = {
432 .se_header = "Source Symbol",
433 .se_cmp = sort__sym_from_cmp,
434 .se_snprintf = hist_entry__sym_from_snprintf,
435 .se_width_idx = HISTC_SYMBOL_FROM,
436};
437
438struct sort_entry sort_sym_to = {
439 .se_header = "Target Symbol",
440 .se_cmp = sort__sym_to_cmp,
441 .se_snprintf = hist_entry__sym_to_snprintf,
442 .se_width_idx = HISTC_SYMBOL_TO,
443};
444
445static int64_t
446sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
447{
448 const unsigned char mp = left->branch_info->flags.mispred !=
449 right->branch_info->flags.mispred;
450 const unsigned char p = left->branch_info->flags.predicted !=
451 right->branch_info->flags.predicted;
452
453 return mp || p;
454}
455
456static int hist_entry__mispredict_snprintf(struct hist_entry *self, char *bf,
457 size_t size, unsigned int width){
458 static const char *out = "N/A";
459
460 if (self->branch_info->flags.predicted)
461 out = "N";
462 else if (self->branch_info->flags.mispred)
463 out = "Y";
464
465 return repsep_snprintf(bf, size, "%-*s", width, out);
466}
467
468struct sort_entry sort_mispredict = {
469 .se_header = "Branch Mispredicted",
470 .se_cmp = sort__mispredict_cmp,
471 .se_snprintf = hist_entry__mispredict_snprintf,
472 .se_width_idx = HISTC_MISPREDICT,
473};
474
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200475struct sort_dimension {
476 const char *name;
477 struct sort_entry *entry;
478 int taken;
479};
480
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100481#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
482
Namhyung Kimfc5871e2012-12-27 18:11:46 +0900483static struct sort_dimension common_sort_dimensions[] = {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100484 DIM(SORT_PID, "pid", sort_thread),
485 DIM(SORT_COMM, "comm", sort_comm),
486 DIM(SORT_DSO, "dso", sort_dso),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100487 DIM(SORT_SYM, "symbol", sort_sym),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100488 DIM(SORT_PARENT, "parent", sort_parent),
489 DIM(SORT_CPU, "cpu", sort_cpu),
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300490 DIM(SORT_SRCLINE, "srcline", sort_srcline),
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200491};
492
Namhyung Kimfc5871e2012-12-27 18:11:46 +0900493#undef DIM
494
495#define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
496
497static struct sort_dimension bstack_sort_dimensions[] = {
498 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
499 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
500 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
501 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
502 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
503};
504
505#undef DIM
506
John Kacurdd68ada2009-09-24 18:02:49 +0200507int sort_dimension__add(const char *tok)
508{
509 unsigned int i;
510
Namhyung Kimfc5871e2012-12-27 18:11:46 +0900511 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
512 struct sort_dimension *sd = &common_sort_dimensions[i];
John Kacurdd68ada2009-09-24 18:02:49 +0200513
John Kacurdd68ada2009-09-24 18:02:49 +0200514 if (strncasecmp(tok, sd->name, strlen(tok)))
515 continue;
Namhyung Kimfc5871e2012-12-27 18:11:46 +0900516
John Kacurdd68ada2009-09-24 18:02:49 +0200517 if (sd->entry == &sort_parent) {
518 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
519 if (ret) {
520 char err[BUFSIZ];
521
522 regerror(ret, &parent_regex, err, sizeof(err));
Arnaldo Carvalho de Melo2aefa4f2010-04-02 12:30:57 -0300523 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
524 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +0200525 }
526 sort__has_parent = 1;
Namhyung Kimfc5871e2012-12-27 18:11:46 +0900527 } else if (sd->entry == &sort_sym) {
Namhyung Kim1af556402012-09-14 17:35:27 +0900528 sort__has_sym = 1;
John Kacurdd68ada2009-09-24 18:02:49 +0200529 }
530
Frederic Weisbeckerfd8ea212011-06-29 23:08:14 +0200531 if (sd->taken)
532 return 0;
533
534 if (sd->entry->se_collapse)
535 sort__need_collapse = 1;
536
Namhyung Kim6f38cf22012-12-27 18:11:45 +0900537 if (list_empty(&hist_entry__sort_list))
538 sort__first_dimension = i;
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200539
John Kacurdd68ada2009-09-24 18:02:49 +0200540 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
541 sd->taken = 1;
542
543 return 0;
544 }
Namhyung Kimfc5871e2012-12-27 18:11:46 +0900545
546 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
547 struct sort_dimension *sd = &bstack_sort_dimensions[i];
548
549 if (strncasecmp(tok, sd->name, strlen(tok)))
550 continue;
551
552 if (sort__branch_mode != 1)
553 return -EINVAL;
554
555 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
556 sort__has_sym = 1;
557
558 if (sd->taken)
559 return 0;
560
561 if (sd->entry->se_collapse)
562 sort__need_collapse = 1;
563
564 if (list_empty(&hist_entry__sort_list))
565 sort__first_dimension = i + __SORT_BRANCH_STACK;
566
567 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
568 sd->taken = 1;
569
570 return 0;
571 }
572
John Kacurdd68ada2009-09-24 18:02:49 +0200573 return -ESRCH;
574}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -0200575
576void setup_sorting(const char * const usagestr[], const struct option *opts)
577{
578 char *tmp, *tok, *str = strdup(sort_order);
579
580 for (tok = strtok_r(str, ", ", &tmp);
581 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Namhyung Kimfc5871e2012-12-27 18:11:46 +0900582 int ret = sort_dimension__add(tok);
583 if (ret == -EINVAL) {
584 error("Invalid --sort key: `%s'", tok);
585 usage_with_options(usagestr, opts);
586 } else if (ret == -ESRCH) {
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -0200587 error("Unknown --sort key: `%s'", tok);
588 usage_with_options(usagestr, opts);
589 }
590 }
591
592 free(str);
593}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200594
595void sort_entry__setup_elide(struct sort_entry *self, struct strlist *list,
596 const char *list_name, FILE *fp)
597{
598 if (list && strlist__nr_entries(list) == 1) {
599 if (fp != NULL)
600 fprintf(fp, "# %s: %s\n", list_name,
601 strlist__entry(list, 0)->s);
602 self->elide = true;
603 }
604}