blob: e14e9867617157aaee3407e4b52e536eec3e8438 [file] [log] [blame]
Ingo Molnarbf9e1872009-06-02 23:37:05 +02001/*
2 * builtin-report.c
3 *
4 * Builtin report command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
Ingo Molnar16f762a2009-05-27 09:10:38 +02008#include "builtin.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02009
Ingo Molnarbf9e1872009-06-02 23:37:05 +020010#include "util/util.h"
11
Ingo Molnar8fc03212009-06-04 15:19:47 +020012#include "util/color.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030013#include "util/list.h"
Ingo Molnara930d2c2009-05-27 09:50:13 +020014#include "util/cache.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030015#include "util/rbtree.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030016#include "util/symbol.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030017#include "util/string.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030018
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020019#include "perf.h"
20
21#include "util/parse-options.h"
22#include "util/parse-events.h"
23
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030024#define SHOW_KERNEL 1
25#define SHOW_USER 2
26#define SHOW_HV 4
27
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020028static char const *input_name = "perf.data";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020029static char *vmlinux = NULL;
Ingo Molnarbd741372009-06-04 14:13:04 +020030
31static char default_sort_order[] = "comm,dso";
32static char *sort_order = default_sort_order;
33
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030034static int input;
35static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
36
Ingo Molnar97b07b62009-05-26 18:48:58 +020037static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020038#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020039#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
Ingo Molnar35029732009-06-03 09:38:58 +020040
Ingo Molnar16f762a2009-05-27 09:10:38 +020041static int verbose;
Ingo Molnar75220602009-06-18 08:00:17 +020042#define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
43
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030044static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020045
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030046static unsigned long page_size;
47static unsigned long mmap_window = 32;
48
Ingo Molnarb25bcf22009-06-18 07:01:03 +020049static char *parent_pattern = "^sys_|^do_page_fault";
50static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020051
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030052struct ip_event {
53 struct perf_event_header header;
54 __u64 ip;
55 __u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020056 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030057};
Ingo Molnar75051722009-06-03 23:14:49 +020058
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030059struct mmap_event {
60 struct perf_event_header header;
61 __u32 pid, tid;
62 __u64 start;
63 __u64 len;
64 __u64 pgoff;
65 char filename[PATH_MAX];
66};
Ingo Molnar75051722009-06-03 23:14:49 +020067
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030068struct comm_event {
69 struct perf_event_header header;
Ingo Molnar75051722009-06-03 23:14:49 +020070 __u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030071 char comm[16];
72};
73
Peter Zijlstra62fc4452009-06-04 16:53:49 +020074struct fork_event {
75 struct perf_event_header header;
76 __u32 pid, ppid;
77};
78
Ingo Molnarb2fef072009-06-05 18:07:51 +020079struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030080 struct perf_event_header header;
Ingo Molnarb2fef072009-06-05 18:07:51 +020081 __u64 time;
82 __u64 id;
83 __u64 sample_period;
84};
85
86typedef union event_union {
87 struct perf_event_header header;
88 struct ip_event ip;
89 struct mmap_event mmap;
90 struct comm_event comm;
91 struct fork_event fork;
92 struct period_event period;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030093} event_t;
94
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030095static LIST_HEAD(dsos);
96static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +020097static struct dso *vdso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030098
99static void dsos__add(struct dso *dso)
100{
101 list_add_tail(&dso->node, &dsos);
102}
103
104static struct dso *dsos__find(const char *name)
105{
106 struct dso *pos;
107
108 list_for_each_entry(pos, &dsos, node)
109 if (strcmp(pos->name, name) == 0)
110 return pos;
111 return NULL;
112}
113
114static struct dso *dsos__findnew(const char *name)
115{
116 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200117 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300118
Ingo Molnar4593bba2009-06-02 15:34:25 +0200119 if (dso)
120 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300121
Ingo Molnar4593bba2009-06-02 15:34:25 +0200122 dso = dso__new(name, 0);
123 if (!dso)
124 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200125
Ingo Molnarbd741372009-06-04 14:13:04 +0200126 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200127 if (nr < 0) {
Ingo Molnar75220602009-06-18 08:00:17 +0200128 eprintf("Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200129 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300130 }
Ingo Molnar75220602009-06-18 08:00:17 +0200131 if (!nr)
132 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200133
134 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300135
136 return dso;
137
138out_delete_dso:
139 dso__delete(dso);
140 return NULL;
141}
142
Ingo Molnar16f762a2009-05-27 09:10:38 +0200143static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300144{
145 struct dso *pos;
146
147 list_for_each_entry(pos, &dsos, node)
148 dso__fprintf(pos, fp);
149}
150
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200151static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200152{
153 return dso__find_symbol(kernel_dso, ip);
154}
155
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200156static int load_kernel(void)
157{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300158 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200159
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300160 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200161 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300162 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200163
Ingo Molnarbd741372009-06-04 14:13:04 +0200164 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300165 if (err) {
166 dso__delete(kernel_dso);
167 kernel_dso = NULL;
168 } else
169 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200170
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200171 vdso = dso__new("[vdso]", 0);
172 if (!vdso)
173 return -1;
174
175 vdso->find_symbol = vdso__find_symbol;
176
177 dsos__add(vdso);
178
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300179 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200180}
181
Ingo Molnard80d3382009-06-03 23:14:49 +0200182static char __cwd[PATH_MAX];
183static char *cwd = __cwd;
184static int cwdlen;
185
186static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300187{
188 int n = 0;
189
190 while (pathname[n] == cwd[n] && n < cwdlen)
191 ++n;
192
193 return n;
194}
195
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300196struct map {
197 struct list_head node;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200198 __u64 start;
199 __u64 end;
200 __u64 pgoff;
201 __u64 (*map_ip)(struct map *, __u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300202 struct dso *dso;
203};
204
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200205static __u64 map__map_ip(struct map *map, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200206{
207 return ip - map->start + map->pgoff;
208}
209
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200210static __u64 vdso__map_ip(struct map *map, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200211{
212 return ip;
213}
214
Pekka Enberg80d496b2009-06-08 21:12:48 +0300215static inline int is_anon_memory(const char *filename)
216{
217 return strcmp(filename, "//anon") == 0;
218}
219
Ingo Molnard80d3382009-06-03 23:14:49 +0200220static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300221{
222 struct map *self = malloc(sizeof(*self));
223
224 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300225 const char *filename = event->filename;
226 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300227 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300228
229 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200230 int n = strcommon(filename);
231
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300232 if (n == cwdlen) {
233 snprintf(newfilename, sizeof(newfilename),
234 ".%s", filename + n);
235 filename = newfilename;
236 }
237 }
238
Pekka Enberg80d496b2009-06-08 21:12:48 +0300239 anon = is_anon_memory(filename);
240
241 if (anon) {
242 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
243 filename = newfilename;
244 }
245
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300246 self->start = event->start;
247 self->end = event->start + event->len;
248 self->pgoff = event->pgoff;
249
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300250 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300251 if (self->dso == NULL)
252 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200253
Pekka Enberg80d496b2009-06-08 21:12:48 +0300254 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200255 self->map_ip = vdso__map_ip;
256 else
257 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300258 }
259 return self;
260out_delete:
261 free(self);
262 return NULL;
263}
264
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200265static struct map *map__clone(struct map *self)
266{
267 struct map *map = malloc(sizeof(*self));
268
269 if (!map)
270 return NULL;
271
272 memcpy(map, self, sizeof(*self));
273
274 return map;
275}
276
277static int map__overlap(struct map *l, struct map *r)
278{
279 if (l->start > r->start) {
280 struct map *t = l;
281 l = r;
282 r = t;
283 }
284
285 if (l->end > r->start)
286 return 1;
287
288 return 0;
289}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300290
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300291static size_t map__fprintf(struct map *self, FILE *fp)
292{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200293 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300294 self->start, self->end, self->pgoff, self->dso->name);
295}
296
297
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300298struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300299 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300300 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300301 pid_t pid;
302 char *comm;
303};
304
305static struct thread *thread__new(pid_t pid)
306{
307 struct thread *self = malloc(sizeof(*self));
308
309 if (self != NULL) {
310 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200311 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200312 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200313 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300314 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300315 }
316
317 return self;
318}
319
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300320static int thread__set_comm(struct thread *self, const char *comm)
321{
Peter Zijlstra82292892009-06-03 12:37:36 +0200322 if (self->comm)
323 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300324 self->comm = strdup(comm);
325 return self->comm ? 0 : -ENOMEM;
326}
327
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300328static size_t thread__fprintf(struct thread *self, FILE *fp)
329{
330 struct map *pos;
331 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
332
333 list_for_each_entry(pos, &self->maps, node)
334 ret += map__fprintf(pos, fp);
335
336 return ret;
337}
338
339
Ingo Molnar16f762a2009-05-27 09:10:38 +0200340static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200341static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300342
343static struct thread *threads__findnew(pid_t pid)
344{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300345 struct rb_node **p = &threads.rb_node;
346 struct rb_node *parent = NULL;
347 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300348
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200349 /*
350 * Font-end cache - PID lookups come in blocks,
351 * so most of the time we dont have to look up
352 * the full rbtree:
353 */
354 if (last_match && last_match->pid == pid)
355 return last_match;
356
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300357 while (*p != NULL) {
358 parent = *p;
359 th = rb_entry(parent, struct thread, rb_node);
360
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200361 if (th->pid == pid) {
362 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300363 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200364 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300365
366 if (pid < th->pid)
367 p = &(*p)->rb_left;
368 else
369 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300370 }
371
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300372 th = thread__new(pid);
373 if (th != NULL) {
374 rb_link_node(&th->rb_node, parent, p);
375 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200376 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300377 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200378
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300379 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300380}
381
382static void thread__insert_map(struct thread *self, struct map *map)
383{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200384 struct map *pos, *tmp;
385
386 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
387 if (map__overlap(pos, map)) {
388 list_del_init(&pos->node);
389 /* XXX leaks dsos */
390 free(pos);
391 }
392 }
393
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300394 list_add_tail(&map->node, &self->maps);
395}
396
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200397static int thread__fork(struct thread *self, struct thread *parent)
398{
399 struct map *map;
400
401 if (self->comm)
402 free(self->comm);
403 self->comm = strdup(parent->comm);
404 if (!self->comm)
405 return -ENOMEM;
406
407 list_for_each_entry(map, &parent->maps, node) {
408 struct map *new = map__clone(map);
409 if (!new)
410 return -ENOMEM;
411 thread__insert_map(self, new);
412 }
413
414 return 0;
415}
416
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200417static struct map *thread__find_map(struct thread *self, __u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300418{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200419 struct map *pos;
420
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300421 if (self == NULL)
422 return NULL;
423
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300424 list_for_each_entry(pos, &self->maps, node)
425 if (ip >= pos->start && ip <= pos->end)
426 return pos;
427
428 return NULL;
429}
430
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300431static size_t threads__fprintf(FILE *fp)
432{
433 size_t ret = 0;
434 struct rb_node *nd;
435
436 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
437 struct thread *pos = rb_entry(nd, struct thread, rb_node);
438
439 ret += thread__fprintf(pos, fp);
440 }
441
442 return ret;
443}
444
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200445/*
446 * histogram, sorted on item, collects counts
447 */
448
449static struct rb_root hist;
450
451struct hist_entry {
452 struct rb_node rb_node;
453
454 struct thread *thread;
455 struct map *map;
456 struct dso *dso;
457 struct symbol *sym;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200458 struct symbol *parent;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200459 __u64 ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200460 char level;
461
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200462 __u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200463};
464
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200465/*
466 * configurable sorting bits
467 */
468
469struct sort_entry {
470 struct list_head list;
471
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200472 char *header;
473
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200474 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200475 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200476 size_t (*print)(FILE *fp, struct hist_entry *);
477};
478
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200479static int64_t cmp_null(void *l, void *r)
480{
481 if (!l && !r)
482 return 0;
483 else if (!l)
484 return -1;
485 else
486 return 1;
487}
488
Peter Zijlstra82292892009-06-03 12:37:36 +0200489/* --sort pid */
490
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200491static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200492sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
493{
494 return right->thread->pid - left->thread->pid;
495}
496
497static size_t
498sort__thread_print(FILE *fp, struct hist_entry *self)
499{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200500 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200501}
502
503static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200504 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200505 .cmp = sort__thread_cmp,
506 .print = sort__thread_print,
507};
508
Peter Zijlstra82292892009-06-03 12:37:36 +0200509/* --sort comm */
510
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200511static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200512sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
513{
Peter Zijlstra82292892009-06-03 12:37:36 +0200514 return right->thread->pid - left->thread->pid;
515}
516
517static int64_t
518sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
519{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200520 char *comm_l = left->thread->comm;
521 char *comm_r = right->thread->comm;
522
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200523 if (!comm_l || !comm_r)
524 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200525
526 return strcmp(comm_l, comm_r);
527}
528
529static size_t
530sort__comm_print(FILE *fp, struct hist_entry *self)
531{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200532 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200533}
534
535static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200536 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200537 .cmp = sort__comm_cmp,
538 .collapse = sort__comm_collapse,
539 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200540};
541
Peter Zijlstra82292892009-06-03 12:37:36 +0200542/* --sort dso */
543
Peter Zijlstra992444b2009-05-27 20:20:27 +0200544static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200545sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
546{
547 struct dso *dso_l = left->dso;
548 struct dso *dso_r = right->dso;
549
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200550 if (!dso_l || !dso_r)
551 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200552
553 return strcmp(dso_l->name, dso_r->name);
554}
555
556static size_t
557sort__dso_print(FILE *fp, struct hist_entry *self)
558{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200559 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200560 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200561
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200562 return fprintf(fp, "%016llx ", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200563}
564
565static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200566 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200567 .cmp = sort__dso_cmp,
568 .print = sort__dso_print,
569};
570
Peter Zijlstra82292892009-06-03 12:37:36 +0200571/* --sort symbol */
572
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200573static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200574sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300575{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200576 __u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200577
578 if (left->sym == right->sym)
579 return 0;
580
581 ip_l = left->sym ? left->sym->start : left->ip;
582 ip_r = right->sym ? right->sym->start : right->ip;
583
584 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300585}
586
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200587static size_t
588sort__sym_print(FILE *fp, struct hist_entry *self)
589{
590 size_t ret = 0;
591
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200592 if (verbose)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200593 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200594
Ingo Molnar8edd4282009-06-05 14:13:18 +0200595 if (self->sym) {
596 ret += fprintf(fp, "[%c] %s",
597 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
598 } else {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200599 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200600 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200601
602 return ret;
603}
604
605static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200606 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200607 .cmp = sort__sym_cmp,
608 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200609};
610
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200611/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200612
613static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200614sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200615{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200616 struct symbol *sym_l = left->parent;
617 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200618
619 if (!sym_l || !sym_r)
620 return cmp_null(sym_l, sym_r);
621
622 return strcmp(sym_l->name, sym_r->name);
623}
624
625static size_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200626sort__parent_print(FILE *fp, struct hist_entry *self)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200627{
628 size_t ret = 0;
629
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200630 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200631
632 return ret;
633}
634
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200635static struct sort_entry sort_parent = {
636 .header = "Parent symbol ",
637 .cmp = sort__parent_cmp,
638 .print = sort__parent_print,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200639};
640
Peter Zijlstra82292892009-06-03 12:37:36 +0200641static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200642static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200643
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200644struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200645 char *name;
646 struct sort_entry *entry;
647 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200648};
649
650static struct sort_dimension sort_dimensions[] = {
651 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200652 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200653 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200654 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200655 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200656};
657
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200658static LIST_HEAD(hist_entry__sort_list);
659
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200660static int sort_dimension__add(char *tok)
661{
662 int i;
663
664 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
665 struct sort_dimension *sd = &sort_dimensions[i];
666
667 if (sd->taken)
668 continue;
669
Ingo Molnar5352f352009-06-03 10:07:39 +0200670 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200671 continue;
672
Peter Zijlstra82292892009-06-03 12:37:36 +0200673 if (sd->entry->collapse)
674 sort__need_collapse = 1;
675
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200676 if (sd->entry == &sort_parent) {
677 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200678 if (ret) {
679 char err[BUFSIZ];
680
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200681 regerror(ret, &parent_regex, err, sizeof(err));
682 fprintf(stderr, "Invalid regex: %s\n%s",
683 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200684 exit(-1);
685 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200686 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200687 }
688
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200689 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
690 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200691
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200692 return 0;
693 }
694
695 return -ESRCH;
696}
697
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200698static int64_t
699hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
700{
701 struct sort_entry *se;
702 int64_t cmp = 0;
703
704 list_for_each_entry(se, &hist_entry__sort_list, list) {
705 cmp = se->cmp(left, right);
706 if (cmp)
707 break;
708 }
709
710 return cmp;
711}
712
Peter Zijlstra82292892009-06-03 12:37:36 +0200713static int64_t
714hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
715{
716 struct sort_entry *se;
717 int64_t cmp = 0;
718
719 list_for_each_entry(se, &hist_entry__sort_list, list) {
720 int64_t (*f)(struct hist_entry *, struct hist_entry *);
721
722 f = se->collapse ?: se->cmp;
723
724 cmp = f(left, right);
725 if (cmp)
726 break;
727 }
728
729 return cmp;
730}
731
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200732static size_t
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200733hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200734{
735 struct sort_entry *se;
736 size_t ret;
737
738 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200739 double percent = self->count * 100.0 / total_samples;
740 char *color = PERF_COLOR_NORMAL;
741
742 /*
Ingo Molnaraefcf372009-06-08 23:15:28 +0200743 * We color high-overhead entries in red, mid-overhead
744 * entries in green - and keep the low overhead places
745 * normal:
Ingo Molnar8fc03212009-06-04 15:19:47 +0200746 */
Ingo Molnaraefcf372009-06-08 23:15:28 +0200747 if (percent >= 5.0) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200748 color = PERF_COLOR_RED;
Ingo Molnaraefcf372009-06-08 23:15:28 +0200749 } else {
750 if (percent >= 0.5)
751 color = PERF_COLOR_GREEN;
752 }
Ingo Molnar8fc03212009-06-04 15:19:47 +0200753
754 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200755 (self->count * 100.0) / total_samples);
756 } else
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200757 ret = fprintf(fp, "%12Ld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200758
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200759 list_for_each_entry(se, &hist_entry__sort_list, list) {
760 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200761 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200762 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200763
764 ret += fprintf(fp, "\n");
765
766 return ret;
767}
768
769/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200770 *
771 */
772
773static struct symbol *
774resolve_symbol(struct thread *thread, struct map **mapp,
775 struct dso **dsop, __u64 *ipp)
776{
777 struct dso *dso = dsop ? *dsop : NULL;
778 struct map *map = mapp ? *mapp : NULL;
779 uint64_t ip = *ipp;
780
781 if (!thread)
782 return NULL;
783
784 if (dso)
785 goto got_dso;
786
787 if (map)
788 goto got_map;
789
790 map = thread__find_map(thread, ip);
791 if (map != NULL) {
792 if (mapp)
793 *mapp = map;
794got_map:
795 ip = map->map_ip(map, ip);
796 *ipp = ip;
797
798 dso = map->dso;
799 } else {
800 /*
801 * If this is outside of all known maps,
802 * and is a negative address, try to look it
803 * up in the kernel dso, as it might be a
804 * vsyscall (which executes in user-mode):
805 */
806 if ((long long)ip < 0)
807 dso = kernel_dso;
808 }
809 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
810
811 if (dsop)
812 *dsop = dso;
813
814 if (!dso)
815 return NULL;
816got_dso:
817 return dso->find_symbol(dso, ip);
818}
819
820static struct symbol *call__match(struct symbol *sym)
821{
822 if (!sym)
823 return NULL;
824
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200825 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200826 return sym;
827
828 return NULL;
829}
830
831/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200832 * collect histogram counts
833 */
834
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200835static int
836hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Ingo Molnar75220602009-06-18 08:00:17 +0200837 struct symbol *sym, __u64 ip, struct perf_callchain_entry *chain,
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200838 char level, __u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300839{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200840 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300841 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200842 struct hist_entry *he;
843 struct hist_entry entry = {
844 .thread = thread,
845 .map = map,
846 .dso = dso,
847 .sym = sym,
848 .ip = ip,
849 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200850 .count = count,
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200851 };
852 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300853
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200854 if (sort__has_parent && chain) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200855 int i, nr = chain->hv;
856 struct symbol *sym;
857 struct dso *dso;
858 __u64 ip;
859
860 for (i = 0; i < chain->kernel; i++) {
Ingo Molnar75220602009-06-18 08:00:17 +0200861 ip = chain->ip[nr + i];
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200862 dso = kernel_dso;
863 sym = resolve_symbol(thread, NULL, &dso, &ip);
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200864 entry.parent = call__match(sym);
865 if (entry.parent)
866 goto got_parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200867 }
868 nr += i;
869
870 for (i = 0; i < chain->user; i++) {
Ingo Molnar75220602009-06-18 08:00:17 +0200871 ip = chain->ip[nr + i];
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200872 sym = resolve_symbol(thread, NULL, NULL, &ip);
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200873 entry.parent = call__match(sym);
874 if (entry.parent)
875 goto got_parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200876 }
877 nr += i;
878 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200879got_parent:
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200880
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300881 while (*p != NULL) {
882 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200883 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300884
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200885 cmp = hist_entry__cmp(&entry, he);
886
887 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200888 he->count += count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200889 return 0;
890 }
891
892 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300893 p = &(*p)->rb_left;
894 else
895 p = &(*p)->rb_right;
896 }
897
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200898 he = malloc(sizeof(*he));
899 if (!he)
900 return -ENOMEM;
901 *he = entry;
902 rb_link_node(&he->rb_node, parent, p);
903 rb_insert_color(&he->rb_node, &hist);
904
905 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300906}
907
Peter Zijlstra82292892009-06-03 12:37:36 +0200908static void hist_entry__free(struct hist_entry *he)
909{
910 free(he);
911}
912
913/*
914 * collapse the histogram
915 */
916
917static struct rb_root collapse_hists;
918
919static void collapse__insert_entry(struct hist_entry *he)
920{
921 struct rb_node **p = &collapse_hists.rb_node;
922 struct rb_node *parent = NULL;
923 struct hist_entry *iter;
924 int64_t cmp;
925
926 while (*p != NULL) {
927 parent = *p;
928 iter = rb_entry(parent, struct hist_entry, rb_node);
929
930 cmp = hist_entry__collapse(iter, he);
931
932 if (!cmp) {
933 iter->count += he->count;
934 hist_entry__free(he);
935 return;
936 }
937
938 if (cmp < 0)
939 p = &(*p)->rb_left;
940 else
941 p = &(*p)->rb_right;
942 }
943
944 rb_link_node(&he->rb_node, parent, p);
945 rb_insert_color(&he->rb_node, &collapse_hists);
946}
947
948static void collapse__resort(void)
949{
950 struct rb_node *next;
951 struct hist_entry *n;
952
953 if (!sort__need_collapse)
954 return;
955
956 next = rb_first(&hist);
957 while (next) {
958 n = rb_entry(next, struct hist_entry, rb_node);
959 next = rb_next(&n->rb_node);
960
961 rb_erase(&n->rb_node, &hist);
962 collapse__insert_entry(n);
963 }
964}
965
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200966/*
967 * reverse the map, sort on count.
968 */
969
970static struct rb_root output_hists;
971
972static void output__insert_entry(struct hist_entry *he)
973{
974 struct rb_node **p = &output_hists.rb_node;
975 struct rb_node *parent = NULL;
976 struct hist_entry *iter;
977
978 while (*p != NULL) {
979 parent = *p;
980 iter = rb_entry(parent, struct hist_entry, rb_node);
981
982 if (he->count > iter->count)
983 p = &(*p)->rb_left;
984 else
985 p = &(*p)->rb_right;
986 }
987
988 rb_link_node(&he->rb_node, parent, p);
989 rb_insert_color(&he->rb_node, &output_hists);
990}
991
992static void output__resort(void)
993{
Peter Zijlstra82292892009-06-03 12:37:36 +0200994 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200995 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300996 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200997
Peter Zijlstra82292892009-06-03 12:37:36 +0200998 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300999 tree = &collapse_hists;
1000
1001 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001002
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001003 while (next) {
1004 n = rb_entry(next, struct hist_entry, rb_node);
1005 next = rb_next(&n->rb_node);
1006
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001007 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001008 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001009 }
1010}
1011
Ingo Molnar729ff5e22009-06-11 14:16:15 +02001012static size_t output__fprintf(FILE *fp, __u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001013{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001014 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001015 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001016 struct rb_node *nd;
1017 size_t ret = 0;
1018
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001019 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001020 fprintf(fp, "#\n");
Ingo Molnar2debbc82009-06-05 14:29:10 +02001021 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001022 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001023
1024 fprintf(fp, "# Overhead");
1025 list_for_each_entry(se, &hist_entry__sort_list, list)
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001026 fprintf(fp, " %s", se->header);
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001027 fprintf(fp, "\n");
1028
1029 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001030 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001031 int i;
1032
Ingo Molnar4593bba2009-06-02 15:34:25 +02001033 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001034 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001035 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001036 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001037 fprintf(fp, "\n");
1038
1039 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001040
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001041 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1042 pos = rb_entry(nd, struct hist_entry, rb_node);
1043 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001044 }
1045
Ingo Molnarbd741372009-06-04 14:13:04 +02001046 if (!strcmp(sort_order, default_sort_order)) {
1047 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001048 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001049 fprintf(fp, "#\n");
1050 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001051 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001052
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001053 return ret;
1054}
1055
Peter Zijlstra436224a2009-06-02 21:02:36 +02001056static void register_idle_thread(void)
1057{
1058 struct thread *thread = threads__findnew(0);
1059
1060 if (thread == NULL ||
1061 thread__set_comm(thread, "[idle]")) {
1062 fprintf(stderr, "problem inserting idle task.\n");
1063 exit(-1);
1064 }
1065}
1066
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001067static unsigned long total = 0,
1068 total_mmap = 0,
1069 total_comm = 0,
1070 total_fork = 0,
1071 total_unknown = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001072
Ingo Molnar75220602009-06-18 08:00:17 +02001073static int validate_chain(struct perf_callchain_entry *chain, event_t *event)
1074{
1075 unsigned int chain_size;
1076
1077 if (chain->nr > MAX_STACK_DEPTH)
1078 return -1;
1079 if (chain->hv > MAX_STACK_DEPTH)
1080 return -1;
1081 if (chain->kernel > MAX_STACK_DEPTH)
1082 return -1;
1083 if (chain->user > MAX_STACK_DEPTH)
1084 return -1;
1085 if (chain->hv + chain->kernel + chain->user != chain->nr)
1086 return -1;
1087
1088 chain_size = event->header.size;
1089 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1090
1091 if (chain->nr*sizeof(__u64) > chain_size)
1092 return -1;
1093
1094 return 0;
1095}
1096
Ingo Molnard80d3382009-06-03 23:14:49 +02001097static int
Ingo Molnar75051722009-06-03 23:14:49 +02001098process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1099{
1100 char level;
1101 int show = 0;
1102 struct dso *dso = NULL;
1103 struct thread *thread = threads__findnew(event->ip.pid);
Ingo Molnar729ff5e22009-06-11 14:16:15 +02001104 __u64 ip = event->ip.ip;
1105 __u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001106 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001107 void *more_data = event->ip.__more_data;
Ingo Molnar75220602009-06-18 08:00:17 +02001108 struct perf_callchain_entry *chain = NULL;
Ingo Molnar75051722009-06-03 23:14:49 +02001109
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001110 if (event->header.type & PERF_SAMPLE_PERIOD) {
1111 period = *(__u64 *)more_data;
1112 more_data += sizeof(__u64);
1113 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001114
Peter Zijlstra4502d772009-06-10 15:03:06 +02001115 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001116 (void *)(offset + head),
1117 (void *)(long)(event->header.size),
1118 event->header.misc,
1119 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001120 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001121 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001122
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001123 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
1124 int i;
1125
1126 chain = (void *)more_data;
1127
Ingo Molnar75220602009-06-18 08:00:17 +02001128 dprintf("... chain: u:%d, k:%d, nr:%d\n",
1129 chain->user,
1130 chain->kernel,
1131 chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001132
Ingo Molnar75220602009-06-18 08:00:17 +02001133 if (validate_chain(chain, event) < 0) {
1134 eprintf("call-chain problem with event, skipping it.\n");
1135 return 0;
1136 }
1137
1138 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001139 for (i = 0; i < chain->nr; i++)
Ingo Molnar75220602009-06-18 08:00:17 +02001140 dprintf("..... %2d: %016Lx\n", i, chain->ip[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001141 }
1142 }
1143
Ingo Molnar75051722009-06-03 23:14:49 +02001144 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1145
1146 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001147 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001148 event->header.type);
1149 return -1;
1150 }
1151
1152 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1153 show = SHOW_KERNEL;
1154 level = 'k';
1155
1156 dso = kernel_dso;
1157
1158 dprintf(" ...... dso: %s\n", dso->name);
1159
1160 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1161
1162 show = SHOW_USER;
1163 level = '.';
1164
Ingo Molnar75051722009-06-03 23:14:49 +02001165 } else {
1166 show = SHOW_HV;
1167 level = 'H';
1168 dprintf(" ...... dso: [hypervisor]\n");
1169 }
1170
1171 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001172 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001173
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001174 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001175 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001176 return -1;
1177 }
1178 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001179 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001180
1181 return 0;
1182}
1183
1184static int
1185process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1186{
1187 struct thread *thread = threads__findnew(event->mmap.pid);
1188 struct map *map = map__new(&event->mmap);
1189
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001190 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001191 (void *)(offset + head),
1192 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001193 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001194 (void *)(long)event->mmap.start,
1195 (void *)(long)event->mmap.len,
1196 (void *)(long)event->mmap.pgoff,
1197 event->mmap.filename);
1198
1199 if (thread == NULL || map == NULL) {
1200 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001201 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001202 }
1203
1204 thread__insert_map(thread, map);
1205 total_mmap++;
1206
1207 return 0;
1208}
1209
1210static int
1211process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1212{
1213 struct thread *thread = threads__findnew(event->comm.pid);
1214
1215 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1216 (void *)(offset + head),
1217 (void *)(long)(event->header.size),
1218 event->comm.comm, event->comm.pid);
1219
1220 if (thread == NULL ||
1221 thread__set_comm(thread, event->comm.comm)) {
1222 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1223 return -1;
1224 }
1225 total_comm++;
1226
1227 return 0;
1228}
1229
1230static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001231process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1232{
1233 struct thread *thread = threads__findnew(event->fork.pid);
1234 struct thread *parent = threads__findnew(event->fork.ppid);
1235
1236 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1237 (void *)(offset + head),
1238 (void *)(long)(event->header.size),
1239 event->fork.pid, event->fork.ppid);
1240
1241 if (!thread || !parent || thread__fork(thread, parent)) {
1242 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1243 return -1;
1244 }
1245 total_fork++;
1246
1247 return 0;
1248}
1249
1250static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001251process_period_event(event_t *event, unsigned long offset, unsigned long head)
1252{
1253 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1254 (void *)(offset + head),
1255 (void *)(long)(event->header.size),
1256 event->period.time,
1257 event->period.id,
1258 event->period.sample_period);
1259
1260 return 0;
1261}
1262
Ingo Molnar8465b052009-06-14 14:44:07 +02001263static void trace_event(event_t *event)
1264{
1265 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001266 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001267 int i, j;
1268
1269 if (!dump_trace)
1270 return;
1271
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001272 dprintf(".");
1273 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001274
1275 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001276 if ((i & 15) == 0) {
1277 dprintf(".");
1278 cdprintf(" %04x: ", i);
1279 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001280
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001281 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001282
1283 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001284 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001285 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001286 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001287 for (j = 0; j < (i & 15); j++) {
Peter Zijlstra5aa75a0f2009-06-15 20:11:41 +02001288 if (issane(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001289 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001290 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001291 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001292 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001293 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001294 }
1295 }
1296 dprintf(".\n");
1297}
1298
Ingo Molnarb2fef072009-06-05 18:07:51 +02001299static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001300process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001301{
Ingo Molnar8465b052009-06-14 14:44:07 +02001302 trace_event(event);
1303
Ingo Molnar75051722009-06-03 23:14:49 +02001304 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1305 return process_overflow_event(event, offset, head);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001306
Ingo Molnar75051722009-06-03 23:14:49 +02001307 switch (event->header.type) {
1308 case PERF_EVENT_MMAP:
1309 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001310
Ingo Molnar75051722009-06-03 23:14:49 +02001311 case PERF_EVENT_COMM:
1312 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001313
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001314 case PERF_EVENT_FORK:
1315 return process_fork_event(event, offset, head);
1316
Ingo Molnarb2fef072009-06-05 18:07:51 +02001317 case PERF_EVENT_PERIOD:
1318 return process_period_event(event, offset, head);
Ingo Molnard11444d2009-06-03 23:29:14 +02001319 /*
1320 * We dont process them right now but they are fine:
1321 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001322
Ingo Molnard11444d2009-06-03 23:29:14 +02001323 case PERF_EVENT_THROTTLE:
1324 case PERF_EVENT_UNTHROTTLE:
1325 return 0;
1326
Ingo Molnard80d3382009-06-03 23:14:49 +02001327 default:
1328 return -1;
1329 }
1330
1331 return 0;
1332}
1333
1334static int __cmd_report(void)
1335{
Ingo Molnar75051722009-06-03 23:14:49 +02001336 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001337 unsigned long offset = 0;
1338 unsigned long head = 0;
1339 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001340 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001341 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001342 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001343
1344 register_idle_thread();
1345
1346 input = open(input_name, O_RDONLY);
1347 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001348 fprintf(stderr, " failed to open file: %s", input_name);
1349 if (!strcmp(input_name, "perf.data"))
1350 fprintf(stderr, " (try 'perf record' first)");
1351 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001352 exit(-1);
1353 }
1354
1355 ret = fstat(input, &stat);
1356 if (ret < 0) {
1357 perror("failed to stat file");
1358 exit(-1);
1359 }
1360
1361 if (!stat.st_size) {
1362 fprintf(stderr, "zero-sized file, nothing to do!\n");
1363 exit(0);
1364 }
1365
1366 if (load_kernel() < 0) {
1367 perror("failed to load kernel symbols");
1368 return EXIT_FAILURE;
1369 }
1370
1371 if (!full_paths) {
1372 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1373 perror("failed to get the current directory");
1374 return EXIT_FAILURE;
1375 }
1376 cwdlen = strlen(cwd);
1377 } else {
1378 cwd = NULL;
1379 cwdlen = 0;
1380 }
1381remap:
1382 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1383 MAP_SHARED, input, offset);
1384 if (buf == MAP_FAILED) {
1385 perror("failed to mmap file");
1386 exit(-1);
1387 }
1388
1389more:
1390 event = (event_t *)(buf + head);
1391
1392 size = event->header.size;
1393 if (!size)
1394 size = 8;
1395
1396 if (head + event->header.size >= page_size * mmap_window) {
1397 unsigned long shift = page_size * (head / page_size);
1398 int ret;
1399
1400 ret = munmap(buf, page_size * mmap_window);
1401 assert(ret == 0);
1402
1403 offset += shift;
1404 head -= shift;
1405 goto remap;
1406 }
1407
1408 size = event->header.size;
1409
Ingo Molnar8465b052009-06-14 14:44:07 +02001410 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001411 (void *)(offset + head),
1412 (void *)(long)event->header.size,
1413 event->header.type);
1414
Ingo Molnard80d3382009-06-03 23:14:49 +02001415 if (!size || process_event(event, offset, head) < 0) {
1416
Ingo Molnar35029732009-06-03 09:38:58 +02001417 dprintf("%p [%p]: skipping unknown header type: %d\n",
1418 (void *)(offset + head),
1419 (void *)(long)(event->header.size),
1420 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001421
Ingo Molnar3e706112009-05-26 18:53:17 +02001422 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001423
1424 /*
1425 * assume we lost track of the stream, check alignment, and
1426 * increment a single u64 in the hope to catch on again 'soon'.
1427 */
1428
1429 if (unlikely(head & 7))
1430 head &= ~7ULL;
1431
1432 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001433 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001434
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001435 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001436
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001437 if (offset + head < stat.st_size)
1438 goto more;
1439
1440 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001441 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001442
Ingo Molnar35029732009-06-03 09:38:58 +02001443 dprintf(" IP events: %10ld\n", total);
1444 dprintf(" mmap events: %10ld\n", total_mmap);
1445 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001446 dprintf(" fork events: %10ld\n", total_fork);
Ingo Molnar35029732009-06-03 09:38:58 +02001447 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001448
Ingo Molnar35029732009-06-03 09:38:58 +02001449 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001450 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001451
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001452 if (verbose >= 3)
1453 threads__fprintf(stdout);
1454
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001455 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001456 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001457
Peter Zijlstra82292892009-06-03 12:37:36 +02001458 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001459 output__resort();
1460 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001461
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001462 return rc;
1463}
1464
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001465static const char * const report_usage[] = {
1466 "perf report [<options>] <command>",
1467 NULL
1468};
1469
1470static const struct option options[] = {
1471 OPT_STRING('i', "input", &input_name, "file",
1472 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001473 OPT_BOOLEAN('v', "verbose", &verbose,
1474 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001475 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1476 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001477 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001478 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001479 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001480 OPT_BOOLEAN('P', "full-paths", &full_paths,
1481 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001482 OPT_STRING('p', "parent", &parent_pattern, "regex",
1483 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001484 OPT_END()
1485};
1486
Ingo Molnar5352f352009-06-03 10:07:39 +02001487static void setup_sorting(void)
1488{
1489 char *tmp, *tok, *str = strdup(sort_order);
1490
1491 for (tok = strtok_r(str, ", ", &tmp);
1492 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1493 if (sort_dimension__add(tok) < 0) {
1494 error("Unknown --sort key: `%s'", tok);
1495 usage_with_options(report_usage, options);
1496 }
1497 }
1498
1499 free(str);
1500}
1501
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001502int cmd_report(int argc, const char **argv, const char *prefix)
1503{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001504 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001505
1506 page_size = getpagesize();
1507
Ingo Molnaredc52de2009-06-04 16:24:37 +02001508 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001509
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001510 setup_sorting();
1511
Ingo Molnaredc52de2009-06-04 16:24:37 +02001512 /*
1513 * Any (unrecognized) arguments left?
1514 */
1515 if (argc)
1516 usage_with_options(report_usage, options);
1517
Ingo Molnara930d2c2009-05-27 09:50:13 +02001518 setup_pager();
1519
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001520 return __cmd_report();
1521}