aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorJohn Kacur <jkacur@redhat.com>2009-09-24 12:02:49 -0400
committerIngo Molnar <mingo@elte.hu>2009-09-24 15:27:52 -0400
commitdd68ada2d417e57b848822a1407b5317a54136c5 (patch)
tree8d7cc54e3bcf063038532d88ac1f94a54b6650a5 /tools
parent8b40f521cf1c9750eab0c04da9075e7484675e9c (diff)
perf tools: Create util/sort.and use it
Create util/sort.[ch] and move common functionality for builtin-report.c and builtin-annotate.c there, and make use of it. Signed-off-by: John Kacur <jkacur@redhat.com> LKML-Reference: <alpine.LFD.2.00.0909241758390.11383@localhost.localdomain> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/Makefile2
-rw-r--r--tools/perf/builtin-annotate.c211
-rw-r--r--tools/perf/builtin-report.c307
-rw-r--r--tools/perf/util/sort.c268
-rw-r--r--tools/perf/util/sort.h93
5 files changed, 373 insertions, 508 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index b5f1953b6144..0a9e5aede318 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -339,6 +339,7 @@ LIB_H += util/symbol.h
339LIB_H += util/module.h 339LIB_H += util/module.h
340LIB_H += util/color.h 340LIB_H += util/color.h
341LIB_H += util/values.h 341LIB_H += util/values.h
342LIB_H += util/sort.h
342 343
343LIB_OBJS += util/abspath.o 344LIB_OBJS += util/abspath.o
344LIB_OBJS += util/alias.o 345LIB_OBJS += util/alias.o
@@ -374,6 +375,7 @@ LIB_OBJS += util/trace-event-parse.o
374LIB_OBJS += util/trace-event-read.o 375LIB_OBJS += util/trace-event-read.o
375LIB_OBJS += util/trace-event-info.o 376LIB_OBJS += util/trace-event-info.o
376LIB_OBJS += util/svghelper.o 377LIB_OBJS += util/svghelper.o
378LIB_OBJS += util/sort.o
377 379
378BUILTIN_OBJS += builtin-annotate.o 380BUILTIN_OBJS += builtin-annotate.o
379BUILTIN_OBJS += builtin-help.o 381BUILTIN_OBJS += builtin-help.o
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index a33087328bd4..059c565b31ea 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -22,12 +22,10 @@
22#include "util/parse-options.h" 22#include "util/parse-options.h"
23#include "util/parse-events.h" 23#include "util/parse-events.h"
24#include "util/thread.h" 24#include "util/thread.h"
25#include "util/sort.h"
25 26
26static char const *input_name = "perf.data"; 27static char const *input_name = "perf.data";
27 28
28static char default_sort_order[] = "comm,symbol";
29static char *sort_order = default_sort_order;
30
31static int force; 29static int force;
32static int input; 30static int input;
33static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; 31static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
@@ -55,207 +53,6 @@ struct sym_ext {
55 53
56static struct rb_root hist; 54static struct rb_root hist;
57 55
58struct hist_entry {
59 struct rb_node rb_node;
60
61 struct thread *thread;
62 struct map *map;
63 struct dso *dso;
64 struct symbol *sym;
65 u64 ip;
66 char level;
67
68 uint32_t count;
69};
70
71/*
72 * configurable sorting bits
73 */
74
75struct sort_entry {
76 struct list_head list;
77
78 const char *header;
79
80 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
81 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
82 size_t (*print)(FILE *fp, struct hist_entry *);
83};
84
85static int64_t cmp_null(void *l, void *r)
86{
87 if (!l && !r)
88 return 0;
89 else if (!l)
90 return -1;
91 else
92 return 1;
93}
94
95/* --sort pid */
96
97static int64_t
98sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
99{
100 return right->thread->pid - left->thread->pid;
101}
102
103static size_t
104sort__thread_print(FILE *fp, struct hist_entry *self)
105{
106 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
107}
108
109static struct sort_entry sort_thread = {
110 .header = " Command: Pid",
111 .cmp = sort__thread_cmp,
112 .print = sort__thread_print,
113};
114
115/* --sort comm */
116
117static int64_t
118sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
119{
120 return right->thread->pid - left->thread->pid;
121}
122
123static int64_t
124sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
125{
126 char *comm_l = left->thread->comm;
127 char *comm_r = right->thread->comm;
128
129 if (!comm_l || !comm_r)
130 return cmp_null(comm_l, comm_r);
131
132 return strcmp(comm_l, comm_r);
133}
134
135static size_t
136sort__comm_print(FILE *fp, struct hist_entry *self)
137{
138 return fprintf(fp, "%16s", self->thread->comm);
139}
140
141static struct sort_entry sort_comm = {
142 .header = " Command",
143 .cmp = sort__comm_cmp,
144 .collapse = sort__comm_collapse,
145 .print = sort__comm_print,
146};
147
148/* --sort dso */
149
150static int64_t
151sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
152{
153 struct dso *dso_l = left->dso;
154 struct dso *dso_r = right->dso;
155
156 if (!dso_l || !dso_r)
157 return cmp_null(dso_l, dso_r);
158
159 return strcmp(dso_l->name, dso_r->name);
160}
161
162static size_t
163sort__dso_print(FILE *fp, struct hist_entry *self)
164{
165 if (self->dso)
166 return fprintf(fp, "%-25s", self->dso->name);
167
168 return fprintf(fp, "%016llx ", (u64)self->ip);
169}
170
171static struct sort_entry sort_dso = {
172 .header = "Shared Object ",
173 .cmp = sort__dso_cmp,
174 .print = sort__dso_print,
175};
176
177/* --sort symbol */
178
179static int64_t
180sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
181{
182 u64 ip_l, ip_r;
183
184 if (left->sym == right->sym)
185 return 0;
186
187 ip_l = left->sym ? left->sym->start : left->ip;
188 ip_r = right->sym ? right->sym->start : right->ip;
189
190 return (int64_t)(ip_r - ip_l);
191}
192
193static size_t
194sort__sym_print(FILE *fp, struct hist_entry *self)
195{
196 size_t ret = 0;
197
198 if (verbose)
199 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
200
201 if (self->sym) {
202 ret += fprintf(fp, "[%c] %s",
203 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
204 } else {
205 ret += fprintf(fp, "%#016llx", (u64)self->ip);
206 }
207
208 return ret;
209}
210
211static struct sort_entry sort_sym = {
212 .header = "Symbol",
213 .cmp = sort__sym_cmp,
214 .print = sort__sym_print,
215};
216
217static int sort__need_collapse = 0;
218
219struct sort_dimension {
220 const char *name;
221 struct sort_entry *entry;
222 int taken;
223};
224
225static struct sort_dimension sort_dimensions[] = {
226 { .name = "pid", .entry = &sort_thread, },
227 { .name = "comm", .entry = &sort_comm, },
228 { .name = "dso", .entry = &sort_dso, },
229 { .name = "symbol", .entry = &sort_sym, },
230};
231
232static LIST_HEAD(hist_entry__sort_list);
233
234static int sort_dimension__add(char *tok)
235{
236 unsigned int i;
237
238 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
239 struct sort_dimension *sd = &sort_dimensions[i];
240
241 if (sd->taken)
242 continue;
243
244 if (strncasecmp(tok, sd->name, strlen(tok)))
245 continue;
246
247 if (sd->entry->collapse)
248 sort__need_collapse = 1;
249
250 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
251 sd->taken = 1;
252
253 return 0;
254 }
255
256 return -ESRCH;
257}
258
259static int64_t 56static int64_t
260hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) 57hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
261{ 58{
@@ -1137,5 +934,11 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1137 934
1138 setup_pager(); 935 setup_pager();
1139 936
937 if (field_sep && *field_sep == '.') {
938 fputs("'.' is the only non valid --field-separator argument\n",
939 stderr);
940 exit(129);
941 }
942
1140 return __cmd_annotate(); 943 return __cmd_annotate();
1141} 944}
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 19669c20088e..7b43504900ff 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -27,15 +27,13 @@
27#include "util/parse-events.h" 27#include "util/parse-events.h"
28 28
29#include "util/thread.h" 29#include "util/thread.h"
30#include "util/sort.h"
30 31
31static char const *input_name = "perf.data"; 32static char const *input_name = "perf.data";
32 33
33static char default_sort_order[] = "comm,dso,symbol";
34static char *sort_order = default_sort_order;
35static char *dso_list_str, *comm_list_str, *sym_list_str, 34static char *dso_list_str, *comm_list_str, *sym_list_str,
36 *col_width_list_str; 35 *col_width_list_str;
37static struct strlist *dso_list, *comm_list, *sym_list; 36static struct strlist *dso_list, *comm_list, *sym_list;
38static char *field_sep;
39 37
40static int force; 38static int force;
41static int input; 39static int input;
@@ -53,10 +51,6 @@ static char *pretty_printing_style = default_pretty_printing_style;
53static unsigned long page_size; 51static unsigned long page_size;
54static unsigned long mmap_window = 32; 52static unsigned long mmap_window = 32;
55 53
56static char default_parent_pattern[] = "^sys_|^do_page_fault";
57static char *parent_pattern = default_parent_pattern;
58static regex_t parent_regex;
59
60static int exclude_other = 1; 54static int exclude_other = 1;
61 55
62static char callchain_default_opt[] = "fractal,0.5"; 56static char callchain_default_opt[] = "fractal,0.5";
@@ -80,304 +74,8 @@ struct callchain_param callchain_param = {
80 74
81static u64 sample_type; 75static u64 sample_type;
82 76
83static int repsep_fprintf(FILE *fp, const char *fmt, ...)
84{
85 int n;
86 va_list ap;
87
88 va_start(ap, fmt);
89 if (!field_sep)
90 n = vfprintf(fp, fmt, ap);
91 else {
92 char *bf = NULL;
93 n = vasprintf(&bf, fmt, ap);
94 if (n > 0) {
95 char *sep = bf;
96
97 while (1) {
98 sep = strchr(sep, *field_sep);
99 if (sep == NULL)
100 break;
101 *sep = '.';
102 }
103 }
104 fputs(bf, fp);
105 free(bf);
106 }
107 va_end(ap);
108 return n;
109}
110
111static unsigned int dsos__col_width,
112 comms__col_width,
113 threads__col_width;
114
115/*
116 * histogram, sorted on item, collects counts
117 */
118
119static struct rb_root hist; 77static struct rb_root hist;
120 78
121struct hist_entry {
122 struct rb_node rb_node;
123
124 struct thread *thread;
125 struct map *map;
126 struct dso *dso;
127 struct symbol *sym;
128 struct symbol *parent;
129 u64 ip;
130 char level;
131 struct callchain_node callchain;
132 struct rb_root sorted_chain;
133
134 u64 count;
135};
136
137/*
138 * configurable sorting bits
139 */
140
141struct sort_entry {
142 struct list_head list;
143
144 const char *header;
145
146 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
147 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
148 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
149 unsigned int *width;
150 bool elide;
151};
152
153static int64_t cmp_null(void *l, void *r)
154{
155 if (!l && !r)
156 return 0;
157 else if (!l)
158 return -1;
159 else
160 return 1;
161}
162
163/* --sort pid */
164
165static int64_t
166sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
167{
168 return right->thread->pid - left->thread->pid;
169}
170
171static size_t
172sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
173{
174 return repsep_fprintf(fp, "%*s:%5d", width - 6,
175 self->thread->comm ?: "", self->thread->pid);
176}
177
178static struct sort_entry sort_thread = {
179 .header = "Command: Pid",
180 .cmp = sort__thread_cmp,
181 .print = sort__thread_print,
182 .width = &threads__col_width,
183};
184
185/* --sort comm */
186
187static int64_t
188sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
189{
190 return right->thread->pid - left->thread->pid;
191}
192
193static int64_t
194sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
195{
196 char *comm_l = left->thread->comm;
197 char *comm_r = right->thread->comm;
198
199 if (!comm_l || !comm_r)
200 return cmp_null(comm_l, comm_r);
201
202 return strcmp(comm_l, comm_r);
203}
204
205static size_t
206sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
207{
208 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
209}
210
211static struct sort_entry sort_comm = {
212 .header = "Command",
213 .cmp = sort__comm_cmp,
214 .collapse = sort__comm_collapse,
215 .print = sort__comm_print,
216 .width = &comms__col_width,
217};
218
219/* --sort dso */
220
221static int64_t
222sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
223{
224 struct dso *dso_l = left->dso;
225 struct dso *dso_r = right->dso;
226
227 if (!dso_l || !dso_r)
228 return cmp_null(dso_l, dso_r);
229
230 return strcmp(dso_l->name, dso_r->name);
231}
232
233static size_t
234sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
235{
236 if (self->dso)
237 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
238
239 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
240}
241
242static struct sort_entry sort_dso = {
243 .header = "Shared Object",
244 .cmp = sort__dso_cmp,
245 .print = sort__dso_print,
246 .width = &dsos__col_width,
247};
248
249/* --sort symbol */
250
251static int64_t
252sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
253{
254 u64 ip_l, ip_r;
255
256 if (left->sym == right->sym)
257 return 0;
258
259 ip_l = left->sym ? left->sym->start : left->ip;
260 ip_r = right->sym ? right->sym->start : right->ip;
261
262 return (int64_t)(ip_r - ip_l);
263}
264
265static size_t
266sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
267{
268 size_t ret = 0;
269
270 if (verbose)
271 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
272 dso__symtab_origin(self->dso));
273
274 ret += repsep_fprintf(fp, "[%c] ", self->level);
275 if (self->sym) {
276 ret += repsep_fprintf(fp, "%s", self->sym->name);
277
278 if (self->sym->module)
279 ret += repsep_fprintf(fp, "\t[%s]",
280 self->sym->module->name);
281 } else {
282 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
283 }
284
285 return ret;
286}
287
288static struct sort_entry sort_sym = {
289 .header = "Symbol",
290 .cmp = sort__sym_cmp,
291 .print = sort__sym_print,
292};
293
294/* --sort parent */
295
296static int64_t
297sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
298{
299 struct symbol *sym_l = left->parent;
300 struct symbol *sym_r = right->parent;
301
302 if (!sym_l || !sym_r)
303 return cmp_null(sym_l, sym_r);
304
305 return strcmp(sym_l->name, sym_r->name);
306}
307
308static size_t
309sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
310{
311 return repsep_fprintf(fp, "%-*s", width,
312 self->parent ? self->parent->name : "[other]");
313}
314
315static unsigned int parent_symbol__col_width;
316
317static struct sort_entry sort_parent = {
318 .header = "Parent symbol",
319 .cmp = sort__parent_cmp,
320 .print = sort__parent_print,
321 .width = &parent_symbol__col_width,
322};
323
324static int sort__need_collapse = 0;
325static int sort__has_parent = 0;
326
327struct sort_dimension {
328 const char *name;
329 struct sort_entry *entry;
330 int taken;
331};
332
333static struct sort_dimension sort_dimensions[] = {
334 { .name = "pid", .entry = &sort_thread, },
335 { .name = "comm", .entry = &sort_comm, },
336 { .name = "dso", .entry = &sort_dso, },
337 { .name = "symbol", .entry = &sort_sym, },
338 { .name = "parent", .entry = &sort_parent, },
339};
340
341static LIST_HEAD(hist_entry__sort_list);
342
343static int sort_dimension__add(const char *tok)
344{
345 unsigned int i;
346
347 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
348 struct sort_dimension *sd = &sort_dimensions[i];
349
350 if (sd->taken)
351 continue;
352
353 if (strncasecmp(tok, sd->name, strlen(tok)))
354 continue;
355
356 if (sd->entry->collapse)
357 sort__need_collapse = 1;
358
359 if (sd->entry == &sort_parent) {
360 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
361 if (ret) {
362 char err[BUFSIZ];
363
364 regerror(ret, &parent_regex, err, sizeof(err));
365 fprintf(stderr, "Invalid regex: %s\n%s",
366 parent_pattern, err);
367 exit(-1);
368 }
369 sort__has_parent = 1;
370 }
371
372 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
373 sd->taken = 1;
374
375 return 0;
376 }
377
378 return -ESRCH;
379}
380
381static int64_t 79static int64_t
382hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) 80hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
383{ 81{
@@ -1606,7 +1304,8 @@ setup:
1606 return 0; 1304 return 0;
1607} 1305}
1608 1306
1609static const char * const report_usage[] = { 1307//static const char * const report_usage[] = {
1308const char * const report_usage[] = {
1610 "perf report [<options>] <command>", 1309 "perf report [<options>] <command>",
1611 NULL 1310 NULL
1612}; 1311};
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
new file mode 100644
index 000000000000..50e75abb1fdd
--- /dev/null
+++ b/tools/perf/util/sort.c
@@ -0,0 +1,268 @@
1#include "sort.h"
2
3regex_t parent_regex;
4char default_parent_pattern[] = "^sys_|^do_page_fault";
5char *parent_pattern = default_parent_pattern;
6char default_sort_order[] = "comm,dso,symbol";
7char *sort_order = default_sort_order;
8int sort__need_collapse = 0;
9int sort__has_parent = 0;
10
11unsigned int dsos__col_width;
12unsigned int comms__col_width;
13unsigned int threads__col_width;
14static unsigned int parent_symbol__col_width;
15char * field_sep;
16
17LIST_HEAD(hist_entry__sort_list);
18
19struct sort_entry sort_thread = {
20 .header = "Command: Pid",
21 .cmp = sort__thread_cmp,
22 .print = sort__thread_print,
23 .width = &threads__col_width,
24};
25
26struct sort_entry sort_comm = {
27 .header = "Command",
28 .cmp = sort__comm_cmp,
29 .collapse = sort__comm_collapse,
30 .print = sort__comm_print,
31 .width = &comms__col_width,
32};
33
34struct sort_entry sort_dso = {
35 .header = "Shared Object",
36 .cmp = sort__dso_cmp,
37 .print = sort__dso_print,
38 .width = &dsos__col_width,
39};
40
41struct sort_entry sort_sym = {
42 .header = "Symbol",
43 .cmp = sort__sym_cmp,
44 .print = sort__sym_print,
45};
46
47struct sort_entry sort_parent = {
48 .header = "Parent symbol",
49 .cmp = sort__parent_cmp,
50 .print = sort__parent_print,
51 .width = &parent_symbol__col_width,
52};
53
54struct sort_dimension {
55 const char *name;
56 struct sort_entry *entry;
57 int taken;
58};
59
60static struct sort_dimension sort_dimensions[] = {
61 { .name = "pid", .entry = &sort_thread, },
62 { .name = "comm", .entry = &sort_comm, },
63 { .name = "dso", .entry = &sort_dso, },
64 { .name = "symbol", .entry = &sort_sym, },
65 { .name = "parent", .entry = &sort_parent, },
66};
67
68int64_t cmp_null(void *l, void *r)
69{
70 if (!l && !r)
71 return 0;
72 else if (!l)
73 return -1;
74 else
75 return 1;
76}
77
78/* --sort pid */
79
80int64_t
81sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
82{
83 return right->thread->pid - left->thread->pid;
84}
85
86int repsep_fprintf(FILE *fp, const char *fmt, ...)
87{
88 int n;
89 va_list ap;
90
91 va_start(ap, fmt);
92 if (!field_sep)
93 n = vfprintf(fp, fmt, ap);
94 else {
95 char *bf = NULL;
96 n = vasprintf(&bf, fmt, ap);
97 if (n > 0) {
98 char *sep = bf;
99
100 while (1) {
101 sep = strchr(sep, *field_sep);
102 if (sep == NULL)
103 break;
104 *sep = '.';
105 }
106 }
107 fputs(bf, fp);
108 free(bf);
109 }
110 va_end(ap);
111 return n;
112}
113
114size_t
115sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
116{
117 return repsep_fprintf(fp, "%*s:%5d", width - 6,
118 self->thread->comm ?: "", self->thread->pid);
119}
120
121size_t
122sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
123{
124 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
125}
126
127/* --sort dso */
128
129int64_t
130sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
131{
132 struct dso *dso_l = left->dso;
133 struct dso *dso_r = right->dso;
134
135 if (!dso_l || !dso_r)
136 return cmp_null(dso_l, dso_r);
137
138 return strcmp(dso_l->name, dso_r->name);
139}
140
141size_t
142sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
143{
144 if (self->dso)
145 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
146
147 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
148}
149
150/* --sort symbol */
151
152int64_t
153sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
154{
155 u64 ip_l, ip_r;
156
157 if (left->sym == right->sym)
158 return 0;
159
160 ip_l = left->sym ? left->sym->start : left->ip;
161 ip_r = right->sym ? right->sym->start : right->ip;
162
163 return (int64_t)(ip_r - ip_l);
164}
165
166
167size_t
168sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
169{
170 size_t ret = 0;
171
172 if (verbose)
173 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
174 dso__symtab_origin(self->dso));
175
176 ret += repsep_fprintf(fp, "[%c] ", self->level);
177 if (self->sym) {
178 ret += repsep_fprintf(fp, "%s", self->sym->name);
179
180 if (self->sym->module)
181 ret += repsep_fprintf(fp, "\t[%s]",
182 self->sym->module->name);
183 } else {
184 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
185 }
186
187 return ret;
188}
189
190/* --sort comm */
191
192int64_t
193sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
194{
195 return right->thread->pid - left->thread->pid;
196}
197
198int64_t
199sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
200{
201 char *comm_l = left->thread->comm;
202 char *comm_r = right->thread->comm;
203
204 if (!comm_l || !comm_r)
205 return cmp_null(comm_l, comm_r);
206
207 return strcmp(comm_l, comm_r);
208}
209
210/* --sort parent */
211
212int64_t
213sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
214{
215 struct symbol *sym_l = left->parent;
216 struct symbol *sym_r = right->parent;
217
218 if (!sym_l || !sym_r)
219 return cmp_null(sym_l, sym_r);
220
221 return strcmp(sym_l->name, sym_r->name);
222}
223
224size_t
225sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
226{
227 return repsep_fprintf(fp, "%-*s", width,
228 self->parent ? self->parent->name : "[other]");
229}
230
231int sort_dimension__add(const char *tok)
232{
233 unsigned int i;
234
235 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
236 struct sort_dimension *sd = &sort_dimensions[i];
237
238 if (sd->taken)
239 continue;
240
241 if (strncasecmp(tok, sd->name, strlen(tok)))
242 continue;
243
244 if (sd->entry->collapse)
245 sort__need_collapse = 1;
246
247 if (sd->entry == &sort_parent) {
248 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
249 if (ret) {
250 char err[BUFSIZ];
251
252 regerror(ret, &parent_regex, err, sizeof(err));
253 fprintf(stderr, "Invalid regex: %s\n%s",
254 parent_pattern, err);
255 exit(-1);
256 }
257 sort__has_parent = 1;
258 }
259
260 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
261 sd->taken = 1;
262
263 return 0;
264 }
265
266 return -ESRCH;
267}
268
diff --git a/tools/perf/util/sort.h b/tools/perf/util/sort.h
new file mode 100644
index 000000000000..4684fd6d5c4a
--- /dev/null
+++ b/tools/perf/util/sort.h
@@ -0,0 +1,93 @@
1#ifndef __PERF_SORT_H
2#define __PERF_SORT_H
3#include "../builtin.h"
4
5#include "util.h"
6
7#include "color.h"
8#include <linux/list.h>
9#include "cache.h"
10#include <linux/rbtree.h>
11#include "symbol.h"
12#include "string.h"
13#include "callchain.h"
14#include "strlist.h"
15#include "values.h"
16
17#include "../perf.h"
18#include "debug.h"
19#include "header.h"
20
21#include "parse-options.h"
22#include "parse-events.h"
23
24#include "thread.h"
25#include "sort.h"
26
27extern regex_t parent_regex;
28extern char *sort_order;
29extern char default_parent_pattern[];
30extern char *parent_pattern;
31extern char default_sort_order[];
32extern int sort__need_collapse;
33extern int sort__has_parent;
34extern char *field_sep;
35extern struct sort_entry sort_comm;
36extern struct sort_entry sort_dso;
37extern struct sort_entry sort_sym;
38extern struct sort_entry sort_parent;
39extern unsigned int dsos__col_width;
40extern unsigned int comms__col_width;
41extern unsigned int threads__col_width;
42
43struct hist_entry {
44 struct rb_node rb_node;
45
46 struct thread *thread;
47 struct map *map;
48 struct dso *dso;
49 struct symbol *sym;
50 struct symbol *parent;
51 u64 ip;
52 char level;
53 struct callchain_node callchain;
54 struct rb_root sorted_chain;
55
56 u64 count;
57};
58
59/*
60 * configurable sorting bits
61 */
62
63struct sort_entry {
64 struct list_head list;
65
66 const char *header;
67
68 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
69 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
70 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
71 unsigned int *width;
72 bool elide;
73};
74
75extern struct sort_entry sort_thread;
76extern struct list_head hist_entry__sort_list;
77
78extern int repsep_fprintf(FILE *fp, const char *fmt, ...);
79extern size_t sort__thread_print(FILE *, struct hist_entry *, unsigned int);
80extern size_t sort__comm_print(FILE *, struct hist_entry *, unsigned int);
81extern size_t sort__dso_print(FILE *, struct hist_entry *, unsigned int);
82extern size_t sort__sym_print(FILE *, struct hist_entry *, unsigned int __used);
83extern int64_t cmp_null(void *, void *);
84extern int64_t sort__thread_cmp(struct hist_entry *, struct hist_entry *);
85extern int64_t sort__comm_cmp(struct hist_entry *, struct hist_entry *);
86extern int64_t sort__comm_collapse(struct hist_entry *, struct hist_entry *);
87extern int64_t sort__dso_cmp(struct hist_entry *, struct hist_entry *);
88extern int64_t sort__sym_cmp(struct hist_entry *, struct hist_entry *);
89extern int64_t sort__parent_cmp(struct hist_entry *, struct hist_entry *);
90extern size_t sort__parent_print(FILE *, struct hist_entry *, unsigned int);
91extern int sort_dimension__add(const char *);
92
93#endif /* __PERF_SORT_H */