diff options
author | John Kacur <jkacur@redhat.com> | 2009-09-24 12:02:49 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2009-09-24 15:27:52 -0400 |
commit | dd68ada2d417e57b848822a1407b5317a54136c5 (patch) | |
tree | 8d7cc54e3bcf063038532d88ac1f94a54b6650a5 | |
parent | 8b40f521cf1c9750eab0c04da9075e7484675e9c (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>
-rw-r--r-- | tools/perf/Makefile | 2 | ||||
-rw-r--r-- | tools/perf/builtin-annotate.c | 211 | ||||
-rw-r--r-- | tools/perf/builtin-report.c | 307 | ||||
-rw-r--r-- | tools/perf/util/sort.c | 268 | ||||
-rw-r--r-- | tools/perf/util/sort.h | 93 |
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 | |||
339 | LIB_H += util/module.h | 339 | LIB_H += util/module.h |
340 | LIB_H += util/color.h | 340 | LIB_H += util/color.h |
341 | LIB_H += util/values.h | 341 | LIB_H += util/values.h |
342 | LIB_H += util/sort.h | ||
342 | 343 | ||
343 | LIB_OBJS += util/abspath.o | 344 | LIB_OBJS += util/abspath.o |
344 | LIB_OBJS += util/alias.o | 345 | LIB_OBJS += util/alias.o |
@@ -374,6 +375,7 @@ LIB_OBJS += util/trace-event-parse.o | |||
374 | LIB_OBJS += util/trace-event-read.o | 375 | LIB_OBJS += util/trace-event-read.o |
375 | LIB_OBJS += util/trace-event-info.o | 376 | LIB_OBJS += util/trace-event-info.o |
376 | LIB_OBJS += util/svghelper.o | 377 | LIB_OBJS += util/svghelper.o |
378 | LIB_OBJS += util/sort.o | ||
377 | 379 | ||
378 | BUILTIN_OBJS += builtin-annotate.o | 380 | BUILTIN_OBJS += builtin-annotate.o |
379 | BUILTIN_OBJS += builtin-help.o | 381 | BUILTIN_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 | ||
26 | static char const *input_name = "perf.data"; | 27 | static char const *input_name = "perf.data"; |
27 | 28 | ||
28 | static char default_sort_order[] = "comm,symbol"; | ||
29 | static char *sort_order = default_sort_order; | ||
30 | |||
31 | static int force; | 29 | static int force; |
32 | static int input; | 30 | static int input; |
33 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; | 31 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
@@ -55,207 +53,6 @@ struct sym_ext { | |||
55 | 53 | ||
56 | static struct rb_root hist; | 54 | static struct rb_root hist; |
57 | 55 | ||
58 | struct 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 | |||
75 | struct 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 | |||
85 | static 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 | |||
97 | static int64_t | ||
98 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) | ||
99 | { | ||
100 | return right->thread->pid - left->thread->pid; | ||
101 | } | ||
102 | |||
103 | static size_t | ||
104 | sort__thread_print(FILE *fp, struct hist_entry *self) | ||
105 | { | ||
106 | return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid); | ||
107 | } | ||
108 | |||
109 | static 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 | |||
117 | static int64_t | ||
118 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | ||
119 | { | ||
120 | return right->thread->pid - left->thread->pid; | ||
121 | } | ||
122 | |||
123 | static int64_t | ||
124 | sort__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 | |||
135 | static size_t | ||
136 | sort__comm_print(FILE *fp, struct hist_entry *self) | ||
137 | { | ||
138 | return fprintf(fp, "%16s", self->thread->comm); | ||
139 | } | ||
140 | |||
141 | static 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 | |||
150 | static int64_t | ||
151 | sort__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 | |||
162 | static size_t | ||
163 | sort__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 | |||
171 | static 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 | |||
179 | static int64_t | ||
180 | sort__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 | |||
193 | static size_t | ||
194 | sort__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 | |||
211 | static struct sort_entry sort_sym = { | ||
212 | .header = "Symbol", | ||
213 | .cmp = sort__sym_cmp, | ||
214 | .print = sort__sym_print, | ||
215 | }; | ||
216 | |||
217 | static int sort__need_collapse = 0; | ||
218 | |||
219 | struct sort_dimension { | ||
220 | const char *name; | ||
221 | struct sort_entry *entry; | ||
222 | int taken; | ||
223 | }; | ||
224 | |||
225 | static 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 | |||
232 | static LIST_HEAD(hist_entry__sort_list); | ||
233 | |||
234 | static 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 | |||
259 | static int64_t | 56 | static int64_t |
260 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | 57 | hist_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 | ||
31 | static char const *input_name = "perf.data"; | 32 | static char const *input_name = "perf.data"; |
32 | 33 | ||
33 | static char default_sort_order[] = "comm,dso,symbol"; | ||
34 | static char *sort_order = default_sort_order; | ||
35 | static char *dso_list_str, *comm_list_str, *sym_list_str, | 34 | static char *dso_list_str, *comm_list_str, *sym_list_str, |
36 | *col_width_list_str; | 35 | *col_width_list_str; |
37 | static struct strlist *dso_list, *comm_list, *sym_list; | 36 | static struct strlist *dso_list, *comm_list, *sym_list; |
38 | static char *field_sep; | ||
39 | 37 | ||
40 | static int force; | 38 | static int force; |
41 | static int input; | 39 | static int input; |
@@ -53,10 +51,6 @@ static char *pretty_printing_style = default_pretty_printing_style; | |||
53 | static unsigned long page_size; | 51 | static unsigned long page_size; |
54 | static unsigned long mmap_window = 32; | 52 | static unsigned long mmap_window = 32; |
55 | 53 | ||
56 | static char default_parent_pattern[] = "^sys_|^do_page_fault"; | ||
57 | static char *parent_pattern = default_parent_pattern; | ||
58 | static regex_t parent_regex; | ||
59 | |||
60 | static int exclude_other = 1; | 54 | static int exclude_other = 1; |
61 | 55 | ||
62 | static char callchain_default_opt[] = "fractal,0.5"; | 56 | static char callchain_default_opt[] = "fractal,0.5"; |
@@ -80,304 +74,8 @@ struct callchain_param callchain_param = { | |||
80 | 74 | ||
81 | static u64 sample_type; | 75 | static u64 sample_type; |
82 | 76 | ||
83 | static 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 | |||
111 | static unsigned int dsos__col_width, | ||
112 | comms__col_width, | ||
113 | threads__col_width; | ||
114 | |||
115 | /* | ||
116 | * histogram, sorted on item, collects counts | ||
117 | */ | ||
118 | |||
119 | static struct rb_root hist; | 77 | static struct rb_root hist; |
120 | 78 | ||
121 | struct 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 | |||
141 | struct 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 | |||
153 | static 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 | |||
165 | static int64_t | ||
166 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) | ||
167 | { | ||
168 | return right->thread->pid - left->thread->pid; | ||
169 | } | ||
170 | |||
171 | static size_t | ||
172 | sort__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 | |||
178 | static 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 | |||
187 | static int64_t | ||
188 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | ||
189 | { | ||
190 | return right->thread->pid - left->thread->pid; | ||
191 | } | ||
192 | |||
193 | static int64_t | ||
194 | sort__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 | |||
205 | static size_t | ||
206 | sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width) | ||
207 | { | ||
208 | return repsep_fprintf(fp, "%*s", width, self->thread->comm); | ||
209 | } | ||
210 | |||
211 | static 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 | |||
221 | static int64_t | ||
222 | sort__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 | |||
233 | static size_t | ||
234 | sort__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 | |||
242 | static 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 | |||
251 | static int64_t | ||
252 | sort__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 | |||
265 | static size_t | ||
266 | sort__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 | |||
288 | static struct sort_entry sort_sym = { | ||
289 | .header = "Symbol", | ||
290 | .cmp = sort__sym_cmp, | ||
291 | .print = sort__sym_print, | ||
292 | }; | ||
293 | |||
294 | /* --sort parent */ | ||
295 | |||
296 | static int64_t | ||
297 | sort__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 | |||
308 | static size_t | ||
309 | sort__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 | |||
315 | static unsigned int parent_symbol__col_width; | ||
316 | |||
317 | static 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 | |||
324 | static int sort__need_collapse = 0; | ||
325 | static int sort__has_parent = 0; | ||
326 | |||
327 | struct sort_dimension { | ||
328 | const char *name; | ||
329 | struct sort_entry *entry; | ||
330 | int taken; | ||
331 | }; | ||
332 | |||
333 | static 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 | |||
341 | static LIST_HEAD(hist_entry__sort_list); | ||
342 | |||
343 | static 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 | |||
381 | static int64_t | 79 | static int64_t |
382 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | 80 | hist_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 | ||
1609 | static const char * const report_usage[] = { | 1307 | //static const char * const report_usage[] = { |
1308 | const 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 | |||
3 | regex_t parent_regex; | ||
4 | char default_parent_pattern[] = "^sys_|^do_page_fault"; | ||
5 | char *parent_pattern = default_parent_pattern; | ||
6 | char default_sort_order[] = "comm,dso,symbol"; | ||
7 | char *sort_order = default_sort_order; | ||
8 | int sort__need_collapse = 0; | ||
9 | int sort__has_parent = 0; | ||
10 | |||
11 | unsigned int dsos__col_width; | ||
12 | unsigned int comms__col_width; | ||
13 | unsigned int threads__col_width; | ||
14 | static unsigned int parent_symbol__col_width; | ||
15 | char * field_sep; | ||
16 | |||
17 | LIST_HEAD(hist_entry__sort_list); | ||
18 | |||
19 | struct 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 | |||
26 | struct 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 | |||
34 | struct 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 | |||
41 | struct sort_entry sort_sym = { | ||
42 | .header = "Symbol", | ||
43 | .cmp = sort__sym_cmp, | ||
44 | .print = sort__sym_print, | ||
45 | }; | ||
46 | |||
47 | struct 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 | |||
54 | struct sort_dimension { | ||
55 | const char *name; | ||
56 | struct sort_entry *entry; | ||
57 | int taken; | ||
58 | }; | ||
59 | |||
60 | static 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 | |||
68 | int64_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 | |||
80 | int64_t | ||
81 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) | ||
82 | { | ||
83 | return right->thread->pid - left->thread->pid; | ||
84 | } | ||
85 | |||
86 | int 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 | |||
114 | size_t | ||
115 | sort__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 | |||
121 | size_t | ||
122 | sort__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 | |||
129 | int64_t | ||
130 | sort__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 | |||
141 | size_t | ||
142 | sort__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 | |||
152 | int64_t | ||
153 | sort__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 | |||
167 | size_t | ||
168 | sort__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 | |||
192 | int64_t | ||
193 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | ||
194 | { | ||
195 | return right->thread->pid - left->thread->pid; | ||
196 | } | ||
197 | |||
198 | int64_t | ||
199 | sort__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 | |||
212 | int64_t | ||
213 | sort__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 | |||
224 | size_t | ||
225 | sort__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 | |||
231 | int 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 | |||
27 | extern regex_t parent_regex; | ||
28 | extern char *sort_order; | ||
29 | extern char default_parent_pattern[]; | ||
30 | extern char *parent_pattern; | ||
31 | extern char default_sort_order[]; | ||
32 | extern int sort__need_collapse; | ||
33 | extern int sort__has_parent; | ||
34 | extern char *field_sep; | ||
35 | extern struct sort_entry sort_comm; | ||
36 | extern struct sort_entry sort_dso; | ||
37 | extern struct sort_entry sort_sym; | ||
38 | extern struct sort_entry sort_parent; | ||
39 | extern unsigned int dsos__col_width; | ||
40 | extern unsigned int comms__col_width; | ||
41 | extern unsigned int threads__col_width; | ||
42 | |||
43 | struct 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 | |||
63 | struct 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 | |||
75 | extern struct sort_entry sort_thread; | ||
76 | extern struct list_head hist_entry__sort_list; | ||
77 | |||
78 | extern int repsep_fprintf(FILE *fp, const char *fmt, ...); | ||
79 | extern size_t sort__thread_print(FILE *, struct hist_entry *, unsigned int); | ||
80 | extern size_t sort__comm_print(FILE *, struct hist_entry *, unsigned int); | ||
81 | extern size_t sort__dso_print(FILE *, struct hist_entry *, unsigned int); | ||
82 | extern size_t sort__sym_print(FILE *, struct hist_entry *, unsigned int __used); | ||
83 | extern int64_t cmp_null(void *, void *); | ||
84 | extern int64_t sort__thread_cmp(struct hist_entry *, struct hist_entry *); | ||
85 | extern int64_t sort__comm_cmp(struct hist_entry *, struct hist_entry *); | ||
86 | extern int64_t sort__comm_collapse(struct hist_entry *, struct hist_entry *); | ||
87 | extern int64_t sort__dso_cmp(struct hist_entry *, struct hist_entry *); | ||
88 | extern int64_t sort__sym_cmp(struct hist_entry *, struct hist_entry *); | ||
89 | extern int64_t sort__parent_cmp(struct hist_entry *, struct hist_entry *); | ||
90 | extern size_t sort__parent_print(FILE *, struct hist_entry *, unsigned int); | ||
91 | extern int sort_dimension__add(const char *); | ||
92 | |||
93 | #endif /* __PERF_SORT_H */ | ||