blob: 86981bd08f6510f6851bdc0bdbed19c63e7b97ce [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 Molnarb8e6d822009-06-18 14:32:19 +020049static char default_parent_pattern[] = "^sys_|^do_page_fault";
50static char *parent_pattern = default_parent_pattern;
Ingo Molnarb25bcf22009-06-18 07:01:03 +020051static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020052
Ingo Molnarb8e6d822009-06-18 14:32:19 +020053static int exclude_other = 1;
54
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030055struct ip_event {
56 struct perf_event_header header;
57 __u64 ip;
58 __u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020059 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030060};
Ingo Molnar75051722009-06-03 23:14:49 +020061
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030062struct mmap_event {
63 struct perf_event_header header;
64 __u32 pid, tid;
65 __u64 start;
66 __u64 len;
67 __u64 pgoff;
68 char filename[PATH_MAX];
69};
Ingo Molnar75051722009-06-03 23:14:49 +020070
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030071struct comm_event {
72 struct perf_event_header header;
Ingo Molnar75051722009-06-03 23:14:49 +020073 __u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030074 char comm[16];
75};
76
Peter Zijlstra62fc4452009-06-04 16:53:49 +020077struct fork_event {
78 struct perf_event_header header;
79 __u32 pid, ppid;
80};
81
Ingo Molnarb2fef072009-06-05 18:07:51 +020082struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030083 struct perf_event_header header;
Ingo Molnarb2fef072009-06-05 18:07:51 +020084 __u64 time;
85 __u64 id;
86 __u64 sample_period;
87};
88
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020089struct lost_event {
90 struct perf_event_header header;
91 __u64 id;
92 __u64 lost;
93};
94
Ingo Molnarb2fef072009-06-05 18:07:51 +020095typedef union event_union {
96 struct perf_event_header header;
97 struct ip_event ip;
98 struct mmap_event mmap;
99 struct comm_event comm;
100 struct fork_event fork;
101 struct period_event period;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200102 struct lost_event lost;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300103} event_t;
104
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300105static LIST_HEAD(dsos);
106static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200107static struct dso *vdso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300108
109static void dsos__add(struct dso *dso)
110{
111 list_add_tail(&dso->node, &dsos);
112}
113
114static struct dso *dsos__find(const char *name)
115{
116 struct dso *pos;
117
118 list_for_each_entry(pos, &dsos, node)
119 if (strcmp(pos->name, name) == 0)
120 return pos;
121 return NULL;
122}
123
124static struct dso *dsos__findnew(const char *name)
125{
126 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200127 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300128
Ingo Molnar4593bba2009-06-02 15:34:25 +0200129 if (dso)
130 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300131
Ingo Molnar4593bba2009-06-02 15:34:25 +0200132 dso = dso__new(name, 0);
133 if (!dso)
134 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200135
Ingo Molnarbd741372009-06-04 14:13:04 +0200136 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200137 if (nr < 0) {
Ingo Molnar75220602009-06-18 08:00:17 +0200138 eprintf("Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200139 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300140 }
Ingo Molnar75220602009-06-18 08:00:17 +0200141 if (!nr)
142 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200143
144 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300145
146 return dso;
147
148out_delete_dso:
149 dso__delete(dso);
150 return NULL;
151}
152
Ingo Molnar16f762a2009-05-27 09:10:38 +0200153static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300154{
155 struct dso *pos;
156
157 list_for_each_entry(pos, &dsos, node)
158 dso__fprintf(pos, fp);
159}
160
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200161static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200162{
163 return dso__find_symbol(kernel_dso, ip);
164}
165
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200166static int load_kernel(void)
167{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300168 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200169
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300170 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200171 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300172 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200173
Ingo Molnarbd741372009-06-04 14:13:04 +0200174 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300175 if (err) {
176 dso__delete(kernel_dso);
177 kernel_dso = NULL;
178 } else
179 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200180
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200181 vdso = dso__new("[vdso]", 0);
182 if (!vdso)
183 return -1;
184
185 vdso->find_symbol = vdso__find_symbol;
186
187 dsos__add(vdso);
188
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300189 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200190}
191
Ingo Molnard80d3382009-06-03 23:14:49 +0200192static char __cwd[PATH_MAX];
193static char *cwd = __cwd;
194static int cwdlen;
195
196static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300197{
198 int n = 0;
199
200 while (pathname[n] == cwd[n] && n < cwdlen)
201 ++n;
202
203 return n;
204}
205
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300206struct map {
207 struct list_head node;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200208 __u64 start;
209 __u64 end;
210 __u64 pgoff;
211 __u64 (*map_ip)(struct map *, __u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300212 struct dso *dso;
213};
214
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200215static __u64 map__map_ip(struct map *map, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200216{
217 return ip - map->start + map->pgoff;
218}
219
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200220static __u64 vdso__map_ip(struct map *map, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200221{
222 return ip;
223}
224
Pekka Enberg80d496b2009-06-08 21:12:48 +0300225static inline int is_anon_memory(const char *filename)
226{
227 return strcmp(filename, "//anon") == 0;
228}
229
Ingo Molnard80d3382009-06-03 23:14:49 +0200230static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300231{
232 struct map *self = malloc(sizeof(*self));
233
234 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300235 const char *filename = event->filename;
236 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300237 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300238
239 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200240 int n = strcommon(filename);
241
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300242 if (n == cwdlen) {
243 snprintf(newfilename, sizeof(newfilename),
244 ".%s", filename + n);
245 filename = newfilename;
246 }
247 }
248
Pekka Enberg80d496b2009-06-08 21:12:48 +0300249 anon = is_anon_memory(filename);
250
251 if (anon) {
252 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
253 filename = newfilename;
254 }
255
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300256 self->start = event->start;
257 self->end = event->start + event->len;
258 self->pgoff = event->pgoff;
259
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300260 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300261 if (self->dso == NULL)
262 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200263
Pekka Enberg80d496b2009-06-08 21:12:48 +0300264 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200265 self->map_ip = vdso__map_ip;
266 else
267 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300268 }
269 return self;
270out_delete:
271 free(self);
272 return NULL;
273}
274
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200275static struct map *map__clone(struct map *self)
276{
277 struct map *map = malloc(sizeof(*self));
278
279 if (!map)
280 return NULL;
281
282 memcpy(map, self, sizeof(*self));
283
284 return map;
285}
286
287static int map__overlap(struct map *l, struct map *r)
288{
289 if (l->start > r->start) {
290 struct map *t = l;
291 l = r;
292 r = t;
293 }
294
295 if (l->end > r->start)
296 return 1;
297
298 return 0;
299}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300300
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300301static size_t map__fprintf(struct map *self, FILE *fp)
302{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200303 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300304 self->start, self->end, self->pgoff, self->dso->name);
305}
306
307
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300308struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300309 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300310 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300311 pid_t pid;
312 char *comm;
313};
314
315static struct thread *thread__new(pid_t pid)
316{
317 struct thread *self = malloc(sizeof(*self));
318
319 if (self != NULL) {
320 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200321 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200322 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200323 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300324 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300325 }
326
327 return self;
328}
329
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300330static int thread__set_comm(struct thread *self, const char *comm)
331{
Peter Zijlstra82292892009-06-03 12:37:36 +0200332 if (self->comm)
333 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300334 self->comm = strdup(comm);
335 return self->comm ? 0 : -ENOMEM;
336}
337
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300338static size_t thread__fprintf(struct thread *self, FILE *fp)
339{
340 struct map *pos;
341 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
342
343 list_for_each_entry(pos, &self->maps, node)
344 ret += map__fprintf(pos, fp);
345
346 return ret;
347}
348
349
Ingo Molnar16f762a2009-05-27 09:10:38 +0200350static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200351static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300352
353static struct thread *threads__findnew(pid_t pid)
354{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300355 struct rb_node **p = &threads.rb_node;
356 struct rb_node *parent = NULL;
357 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300358
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200359 /*
360 * Font-end cache - PID lookups come in blocks,
361 * so most of the time we dont have to look up
362 * the full rbtree:
363 */
364 if (last_match && last_match->pid == pid)
365 return last_match;
366
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300367 while (*p != NULL) {
368 parent = *p;
369 th = rb_entry(parent, struct thread, rb_node);
370
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200371 if (th->pid == pid) {
372 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300373 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200374 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300375
376 if (pid < th->pid)
377 p = &(*p)->rb_left;
378 else
379 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300380 }
381
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300382 th = thread__new(pid);
383 if (th != NULL) {
384 rb_link_node(&th->rb_node, parent, p);
385 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200386 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300387 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200388
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300389 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300390}
391
392static void thread__insert_map(struct thread *self, struct map *map)
393{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200394 struct map *pos, *tmp;
395
396 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
397 if (map__overlap(pos, map)) {
398 list_del_init(&pos->node);
399 /* XXX leaks dsos */
400 free(pos);
401 }
402 }
403
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300404 list_add_tail(&map->node, &self->maps);
405}
406
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200407static int thread__fork(struct thread *self, struct thread *parent)
408{
409 struct map *map;
410
411 if (self->comm)
412 free(self->comm);
413 self->comm = strdup(parent->comm);
414 if (!self->comm)
415 return -ENOMEM;
416
417 list_for_each_entry(map, &parent->maps, node) {
418 struct map *new = map__clone(map);
419 if (!new)
420 return -ENOMEM;
421 thread__insert_map(self, new);
422 }
423
424 return 0;
425}
426
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200427static struct map *thread__find_map(struct thread *self, __u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300428{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200429 struct map *pos;
430
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300431 if (self == NULL)
432 return NULL;
433
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300434 list_for_each_entry(pos, &self->maps, node)
435 if (ip >= pos->start && ip <= pos->end)
436 return pos;
437
438 return NULL;
439}
440
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300441static size_t threads__fprintf(FILE *fp)
442{
443 size_t ret = 0;
444 struct rb_node *nd;
445
446 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
447 struct thread *pos = rb_entry(nd, struct thread, rb_node);
448
449 ret += thread__fprintf(pos, fp);
450 }
451
452 return ret;
453}
454
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200455/*
456 * histogram, sorted on item, collects counts
457 */
458
459static struct rb_root hist;
460
461struct hist_entry {
462 struct rb_node rb_node;
463
464 struct thread *thread;
465 struct map *map;
466 struct dso *dso;
467 struct symbol *sym;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200468 struct symbol *parent;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200469 __u64 ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200470 char level;
471
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200472 __u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200473};
474
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200475/*
476 * configurable sorting bits
477 */
478
479struct sort_entry {
480 struct list_head list;
481
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200482 char *header;
483
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200484 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200485 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200486 size_t (*print)(FILE *fp, struct hist_entry *);
487};
488
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200489static int64_t cmp_null(void *l, void *r)
490{
491 if (!l && !r)
492 return 0;
493 else if (!l)
494 return -1;
495 else
496 return 1;
497}
498
Peter Zijlstra82292892009-06-03 12:37:36 +0200499/* --sort pid */
500
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200501static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200502sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
503{
504 return right->thread->pid - left->thread->pid;
505}
506
507static size_t
508sort__thread_print(FILE *fp, struct hist_entry *self)
509{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200510 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200511}
512
513static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200514 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200515 .cmp = sort__thread_cmp,
516 .print = sort__thread_print,
517};
518
Peter Zijlstra82292892009-06-03 12:37:36 +0200519/* --sort comm */
520
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200521static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200522sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
523{
Peter Zijlstra82292892009-06-03 12:37:36 +0200524 return right->thread->pid - left->thread->pid;
525}
526
527static int64_t
528sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
529{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200530 char *comm_l = left->thread->comm;
531 char *comm_r = right->thread->comm;
532
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200533 if (!comm_l || !comm_r)
534 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200535
536 return strcmp(comm_l, comm_r);
537}
538
539static size_t
540sort__comm_print(FILE *fp, struct hist_entry *self)
541{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200542 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200543}
544
545static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200546 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200547 .cmp = sort__comm_cmp,
548 .collapse = sort__comm_collapse,
549 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200550};
551
Peter Zijlstra82292892009-06-03 12:37:36 +0200552/* --sort dso */
553
Peter Zijlstra992444b2009-05-27 20:20:27 +0200554static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200555sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
556{
557 struct dso *dso_l = left->dso;
558 struct dso *dso_r = right->dso;
559
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200560 if (!dso_l || !dso_r)
561 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200562
563 return strcmp(dso_l->name, dso_r->name);
564}
565
566static size_t
567sort__dso_print(FILE *fp, struct hist_entry *self)
568{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200569 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200570 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200571
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200572 return fprintf(fp, "%016llx ", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200573}
574
575static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200576 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200577 .cmp = sort__dso_cmp,
578 .print = sort__dso_print,
579};
580
Peter Zijlstra82292892009-06-03 12:37:36 +0200581/* --sort symbol */
582
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200583static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200584sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300585{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200586 __u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200587
588 if (left->sym == right->sym)
589 return 0;
590
591 ip_l = left->sym ? left->sym->start : left->ip;
592 ip_r = right->sym ? right->sym->start : right->ip;
593
594 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300595}
596
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200597static size_t
598sort__sym_print(FILE *fp, struct hist_entry *self)
599{
600 size_t ret = 0;
601
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200602 if (verbose)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200603 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200604
Ingo Molnar8edd4282009-06-05 14:13:18 +0200605 if (self->sym) {
606 ret += fprintf(fp, "[%c] %s",
607 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
608 } else {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200609 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200610 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200611
612 return ret;
613}
614
615static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200616 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200617 .cmp = sort__sym_cmp,
618 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200619};
620
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200621/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200622
623static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200624sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200625{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200626 struct symbol *sym_l = left->parent;
627 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200628
629 if (!sym_l || !sym_r)
630 return cmp_null(sym_l, sym_r);
631
632 return strcmp(sym_l->name, sym_r->name);
633}
634
635static size_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200636sort__parent_print(FILE *fp, struct hist_entry *self)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200637{
638 size_t ret = 0;
639
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200640 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200641
642 return ret;
643}
644
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200645static struct sort_entry sort_parent = {
646 .header = "Parent symbol ",
647 .cmp = sort__parent_cmp,
648 .print = sort__parent_print,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200649};
650
Peter Zijlstra82292892009-06-03 12:37:36 +0200651static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200652static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200653
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200654struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200655 char *name;
656 struct sort_entry *entry;
657 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200658};
659
660static struct sort_dimension sort_dimensions[] = {
661 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200662 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200663 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200664 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200665 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200666};
667
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200668static LIST_HEAD(hist_entry__sort_list);
669
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200670static int sort_dimension__add(char *tok)
671{
672 int i;
673
674 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
675 struct sort_dimension *sd = &sort_dimensions[i];
676
677 if (sd->taken)
678 continue;
679
Ingo Molnar5352f352009-06-03 10:07:39 +0200680 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200681 continue;
682
Peter Zijlstra82292892009-06-03 12:37:36 +0200683 if (sd->entry->collapse)
684 sort__need_collapse = 1;
685
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200686 if (sd->entry == &sort_parent) {
687 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200688 if (ret) {
689 char err[BUFSIZ];
690
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200691 regerror(ret, &parent_regex, err, sizeof(err));
692 fprintf(stderr, "Invalid regex: %s\n%s",
693 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200694 exit(-1);
695 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200696 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200697 }
698
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200699 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
700 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200701
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200702 return 0;
703 }
704
705 return -ESRCH;
706}
707
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200708static int64_t
709hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
710{
711 struct sort_entry *se;
712 int64_t cmp = 0;
713
714 list_for_each_entry(se, &hist_entry__sort_list, list) {
715 cmp = se->cmp(left, right);
716 if (cmp)
717 break;
718 }
719
720 return cmp;
721}
722
Peter Zijlstra82292892009-06-03 12:37:36 +0200723static int64_t
724hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
725{
726 struct sort_entry *se;
727 int64_t cmp = 0;
728
729 list_for_each_entry(se, &hist_entry__sort_list, list) {
730 int64_t (*f)(struct hist_entry *, struct hist_entry *);
731
732 f = se->collapse ?: se->cmp;
733
734 cmp = f(left, right);
735 if (cmp)
736 break;
737 }
738
739 return cmp;
740}
741
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200742static size_t
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200743hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200744{
745 struct sort_entry *se;
746 size_t ret;
747
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200748 if (exclude_other && !self->parent)
749 return 0;
750
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200751 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200752 double percent = self->count * 100.0 / total_samples;
753 char *color = PERF_COLOR_NORMAL;
754
755 /*
Ingo Molnaraefcf372009-06-08 23:15:28 +0200756 * We color high-overhead entries in red, mid-overhead
757 * entries in green - and keep the low overhead places
758 * normal:
Ingo Molnar8fc03212009-06-04 15:19:47 +0200759 */
Ingo Molnaraefcf372009-06-08 23:15:28 +0200760 if (percent >= 5.0) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200761 color = PERF_COLOR_RED;
Ingo Molnaraefcf372009-06-08 23:15:28 +0200762 } else {
763 if (percent >= 0.5)
764 color = PERF_COLOR_GREEN;
765 }
Ingo Molnar8fc03212009-06-04 15:19:47 +0200766
767 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200768 (self->count * 100.0) / total_samples);
769 } else
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200770 ret = fprintf(fp, "%12Ld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200771
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200772 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200773 if (exclude_other && (se == &sort_parent))
774 continue;
775
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200776 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200777 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200778 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200779
780 ret += fprintf(fp, "\n");
781
782 return ret;
783}
784
785/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200786 *
787 */
788
789static struct symbol *
790resolve_symbol(struct thread *thread, struct map **mapp,
791 struct dso **dsop, __u64 *ipp)
792{
793 struct dso *dso = dsop ? *dsop : NULL;
794 struct map *map = mapp ? *mapp : NULL;
795 uint64_t ip = *ipp;
796
797 if (!thread)
798 return NULL;
799
800 if (dso)
801 goto got_dso;
802
803 if (map)
804 goto got_map;
805
806 map = thread__find_map(thread, ip);
807 if (map != NULL) {
808 if (mapp)
809 *mapp = map;
810got_map:
811 ip = map->map_ip(map, ip);
812 *ipp = ip;
813
814 dso = map->dso;
815 } else {
816 /*
817 * If this is outside of all known maps,
818 * and is a negative address, try to look it
819 * up in the kernel dso, as it might be a
820 * vsyscall (which executes in user-mode):
821 */
822 if ((long long)ip < 0)
823 dso = kernel_dso;
824 }
825 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
826
827 if (dsop)
828 *dsop = dso;
829
830 if (!dso)
831 return NULL;
832got_dso:
833 return dso->find_symbol(dso, ip);
834}
835
836static struct symbol *call__match(struct symbol *sym)
837{
838 if (!sym)
839 return NULL;
840
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200841 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200842 return sym;
843
844 return NULL;
845}
846
847/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200848 * collect histogram counts
849 */
850
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200851static int
852hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Ingo Molnar75220602009-06-18 08:00:17 +0200853 struct symbol *sym, __u64 ip, struct perf_callchain_entry *chain,
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200854 char level, __u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300855{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200856 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300857 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200858 struct hist_entry *he;
859 struct hist_entry entry = {
860 .thread = thread,
861 .map = map,
862 .dso = dso,
863 .sym = sym,
864 .ip = ip,
865 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200866 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200867 .parent = NULL,
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200868 };
869 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300870
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200871 if (sort__has_parent && chain) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200872 int i, nr = chain->hv;
873 struct symbol *sym;
874 struct dso *dso;
875 __u64 ip;
876
877 for (i = 0; i < chain->kernel; i++) {
Ingo Molnar75220602009-06-18 08:00:17 +0200878 ip = chain->ip[nr + i];
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200879 dso = kernel_dso;
880 sym = resolve_symbol(thread, NULL, &dso, &ip);
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200881 entry.parent = call__match(sym);
882 if (entry.parent)
883 goto got_parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200884 }
885 nr += i;
886
887 for (i = 0; i < chain->user; i++) {
Ingo Molnar75220602009-06-18 08:00:17 +0200888 ip = chain->ip[nr + i];
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200889 sym = resolve_symbol(thread, NULL, NULL, &ip);
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200890 entry.parent = call__match(sym);
891 if (entry.parent)
892 goto got_parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200893 }
894 nr += i;
895 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200896got_parent:
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200897
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300898 while (*p != NULL) {
899 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200900 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300901
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200902 cmp = hist_entry__cmp(&entry, he);
903
904 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200905 he->count += count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200906 return 0;
907 }
908
909 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300910 p = &(*p)->rb_left;
911 else
912 p = &(*p)->rb_right;
913 }
914
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200915 he = malloc(sizeof(*he));
916 if (!he)
917 return -ENOMEM;
918 *he = entry;
919 rb_link_node(&he->rb_node, parent, p);
920 rb_insert_color(&he->rb_node, &hist);
921
922 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300923}
924
Peter Zijlstra82292892009-06-03 12:37:36 +0200925static void hist_entry__free(struct hist_entry *he)
926{
927 free(he);
928}
929
930/*
931 * collapse the histogram
932 */
933
934static struct rb_root collapse_hists;
935
936static void collapse__insert_entry(struct hist_entry *he)
937{
938 struct rb_node **p = &collapse_hists.rb_node;
939 struct rb_node *parent = NULL;
940 struct hist_entry *iter;
941 int64_t cmp;
942
943 while (*p != NULL) {
944 parent = *p;
945 iter = rb_entry(parent, struct hist_entry, rb_node);
946
947 cmp = hist_entry__collapse(iter, he);
948
949 if (!cmp) {
950 iter->count += he->count;
951 hist_entry__free(he);
952 return;
953 }
954
955 if (cmp < 0)
956 p = &(*p)->rb_left;
957 else
958 p = &(*p)->rb_right;
959 }
960
961 rb_link_node(&he->rb_node, parent, p);
962 rb_insert_color(&he->rb_node, &collapse_hists);
963}
964
965static void collapse__resort(void)
966{
967 struct rb_node *next;
968 struct hist_entry *n;
969
970 if (!sort__need_collapse)
971 return;
972
973 next = rb_first(&hist);
974 while (next) {
975 n = rb_entry(next, struct hist_entry, rb_node);
976 next = rb_next(&n->rb_node);
977
978 rb_erase(&n->rb_node, &hist);
979 collapse__insert_entry(n);
980 }
981}
982
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200983/*
984 * reverse the map, sort on count.
985 */
986
987static struct rb_root output_hists;
988
989static void output__insert_entry(struct hist_entry *he)
990{
991 struct rb_node **p = &output_hists.rb_node;
992 struct rb_node *parent = NULL;
993 struct hist_entry *iter;
994
995 while (*p != NULL) {
996 parent = *p;
997 iter = rb_entry(parent, struct hist_entry, rb_node);
998
999 if (he->count > iter->count)
1000 p = &(*p)->rb_left;
1001 else
1002 p = &(*p)->rb_right;
1003 }
1004
1005 rb_link_node(&he->rb_node, parent, p);
1006 rb_insert_color(&he->rb_node, &output_hists);
1007}
1008
1009static void output__resort(void)
1010{
Peter Zijlstra82292892009-06-03 12:37:36 +02001011 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001012 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001013 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001014
Peter Zijlstra82292892009-06-03 12:37:36 +02001015 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001016 tree = &collapse_hists;
1017
1018 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001019
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001020 while (next) {
1021 n = rb_entry(next, struct hist_entry, rb_node);
1022 next = rb_next(&n->rb_node);
1023
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001024 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001025 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001026 }
1027}
1028
Ingo Molnar729ff5e22009-06-11 14:16:15 +02001029static size_t output__fprintf(FILE *fp, __u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001030{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001031 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001032 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001033 struct rb_node *nd;
1034 size_t ret = 0;
1035
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001036 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001037 fprintf(fp, "#\n");
Ingo Molnar2debbc82009-06-05 14:29:10 +02001038 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001039 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001040
1041 fprintf(fp, "# Overhead");
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001042 list_for_each_entry(se, &hist_entry__sort_list, list) {
1043 if (exclude_other && (se == &sort_parent))
1044 continue;
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001045 fprintf(fp, " %s", se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001046 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001047 fprintf(fp, "\n");
1048
1049 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001050 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001051 int i;
1052
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001053 if (exclude_other && (se == &sort_parent))
1054 continue;
1055
Ingo Molnar4593bba2009-06-02 15:34:25 +02001056 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001057 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001058 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001059 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001060 fprintf(fp, "\n");
1061
1062 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001063
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001064 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1065 pos = rb_entry(nd, struct hist_entry, rb_node);
1066 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001067 }
1068
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001069 if (sort_order == default_sort_order &&
1070 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001071 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001072 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001073 fprintf(fp, "#\n");
1074 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001075 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001076
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001077 return ret;
1078}
1079
Peter Zijlstra436224a2009-06-02 21:02:36 +02001080static void register_idle_thread(void)
1081{
1082 struct thread *thread = threads__findnew(0);
1083
1084 if (thread == NULL ||
1085 thread__set_comm(thread, "[idle]")) {
1086 fprintf(stderr, "problem inserting idle task.\n");
1087 exit(-1);
1088 }
1089}
1090
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001091static unsigned long total = 0,
1092 total_mmap = 0,
1093 total_comm = 0,
1094 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001095 total_unknown = 0,
1096 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001097
Ingo Molnar75220602009-06-18 08:00:17 +02001098static int validate_chain(struct perf_callchain_entry *chain, event_t *event)
1099{
1100 unsigned int chain_size;
1101
1102 if (chain->nr > MAX_STACK_DEPTH)
1103 return -1;
1104 if (chain->hv > MAX_STACK_DEPTH)
1105 return -1;
1106 if (chain->kernel > MAX_STACK_DEPTH)
1107 return -1;
1108 if (chain->user > MAX_STACK_DEPTH)
1109 return -1;
1110 if (chain->hv + chain->kernel + chain->user != chain->nr)
1111 return -1;
1112
1113 chain_size = event->header.size;
1114 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1115
1116 if (chain->nr*sizeof(__u64) > chain_size)
1117 return -1;
1118
1119 return 0;
1120}
1121
Ingo Molnard80d3382009-06-03 23:14:49 +02001122static int
Ingo Molnar75051722009-06-03 23:14:49 +02001123process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1124{
1125 char level;
1126 int show = 0;
1127 struct dso *dso = NULL;
1128 struct thread *thread = threads__findnew(event->ip.pid);
Ingo Molnar729ff5e22009-06-11 14:16:15 +02001129 __u64 ip = event->ip.ip;
1130 __u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001131 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001132 void *more_data = event->ip.__more_data;
Ingo Molnar75220602009-06-18 08:00:17 +02001133 struct perf_callchain_entry *chain = NULL;
Ingo Molnar75051722009-06-03 23:14:49 +02001134
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001135 if (event->header.type & PERF_SAMPLE_PERIOD) {
1136 period = *(__u64 *)more_data;
1137 more_data += sizeof(__u64);
1138 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001139
Peter Zijlstra4502d772009-06-10 15:03:06 +02001140 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001141 (void *)(offset + head),
1142 (void *)(long)(event->header.size),
1143 event->header.misc,
1144 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001145 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001146 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001147
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001148 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
1149 int i;
1150
1151 chain = (void *)more_data;
1152
Ingo Molnar75220602009-06-18 08:00:17 +02001153 dprintf("... chain: u:%d, k:%d, nr:%d\n",
1154 chain->user,
1155 chain->kernel,
1156 chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001157
Ingo Molnar75220602009-06-18 08:00:17 +02001158 if (validate_chain(chain, event) < 0) {
1159 eprintf("call-chain problem with event, skipping it.\n");
1160 return 0;
1161 }
1162
1163 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001164 for (i = 0; i < chain->nr; i++)
Ingo Molnar75220602009-06-18 08:00:17 +02001165 dprintf("..... %2d: %016Lx\n", i, chain->ip[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001166 }
1167 }
1168
Ingo Molnar75051722009-06-03 23:14:49 +02001169 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1170
1171 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001172 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001173 event->header.type);
1174 return -1;
1175 }
1176
1177 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1178 show = SHOW_KERNEL;
1179 level = 'k';
1180
1181 dso = kernel_dso;
1182
1183 dprintf(" ...... dso: %s\n", dso->name);
1184
1185 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1186
1187 show = SHOW_USER;
1188 level = '.';
1189
Ingo Molnar75051722009-06-03 23:14:49 +02001190 } else {
1191 show = SHOW_HV;
1192 level = 'H';
1193 dprintf(" ...... dso: [hypervisor]\n");
1194 }
1195
1196 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001197 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001198
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001199 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001200 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001201 return -1;
1202 }
1203 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001204 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001205
1206 return 0;
1207}
1208
1209static int
1210process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1211{
1212 struct thread *thread = threads__findnew(event->mmap.pid);
1213 struct map *map = map__new(&event->mmap);
1214
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001215 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001216 (void *)(offset + head),
1217 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001218 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001219 (void *)(long)event->mmap.start,
1220 (void *)(long)event->mmap.len,
1221 (void *)(long)event->mmap.pgoff,
1222 event->mmap.filename);
1223
1224 if (thread == NULL || map == NULL) {
1225 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001226 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001227 }
1228
1229 thread__insert_map(thread, map);
1230 total_mmap++;
1231
1232 return 0;
1233}
1234
1235static int
1236process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1237{
1238 struct thread *thread = threads__findnew(event->comm.pid);
1239
1240 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1241 (void *)(offset + head),
1242 (void *)(long)(event->header.size),
1243 event->comm.comm, event->comm.pid);
1244
1245 if (thread == NULL ||
1246 thread__set_comm(thread, event->comm.comm)) {
1247 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1248 return -1;
1249 }
1250 total_comm++;
1251
1252 return 0;
1253}
1254
1255static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001256process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1257{
1258 struct thread *thread = threads__findnew(event->fork.pid);
1259 struct thread *parent = threads__findnew(event->fork.ppid);
1260
1261 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1262 (void *)(offset + head),
1263 (void *)(long)(event->header.size),
1264 event->fork.pid, event->fork.ppid);
1265
1266 if (!thread || !parent || thread__fork(thread, parent)) {
1267 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1268 return -1;
1269 }
1270 total_fork++;
1271
1272 return 0;
1273}
1274
1275static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001276process_period_event(event_t *event, unsigned long offset, unsigned long head)
1277{
1278 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1279 (void *)(offset + head),
1280 (void *)(long)(event->header.size),
1281 event->period.time,
1282 event->period.id,
1283 event->period.sample_period);
1284
1285 return 0;
1286}
1287
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001288static int
1289process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1290{
1291 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1292 (void *)(offset + head),
1293 (void *)(long)(event->header.size),
1294 event->lost.id,
1295 event->lost.lost);
1296
1297 total_lost += event->lost.lost;
1298
1299 return 0;
1300}
1301
Ingo Molnar8465b052009-06-14 14:44:07 +02001302static void trace_event(event_t *event)
1303{
1304 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001305 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001306 int i, j;
1307
1308 if (!dump_trace)
1309 return;
1310
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001311 dprintf(".");
1312 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001313
1314 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001315 if ((i & 15) == 0) {
1316 dprintf(".");
1317 cdprintf(" %04x: ", i);
1318 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001319
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001320 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001321
1322 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001323 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001324 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001325 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001326 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001327 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001328 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001329 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001330 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001331 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001332 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001333 }
1334 }
1335 dprintf(".\n");
1336}
1337
Ingo Molnarb2fef072009-06-05 18:07:51 +02001338static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001339process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001340{
Ingo Molnar8465b052009-06-14 14:44:07 +02001341 trace_event(event);
1342
Ingo Molnar75051722009-06-03 23:14:49 +02001343 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1344 return process_overflow_event(event, offset, head);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001345
Ingo Molnar75051722009-06-03 23:14:49 +02001346 switch (event->header.type) {
1347 case PERF_EVENT_MMAP:
1348 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001349
Ingo Molnar75051722009-06-03 23:14:49 +02001350 case PERF_EVENT_COMM:
1351 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001352
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001353 case PERF_EVENT_FORK:
1354 return process_fork_event(event, offset, head);
1355
Ingo Molnarb2fef072009-06-05 18:07:51 +02001356 case PERF_EVENT_PERIOD:
1357 return process_period_event(event, offset, head);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001358
1359 case PERF_EVENT_LOST:
1360 return process_lost_event(event, offset, head);
1361
Ingo Molnard11444d2009-06-03 23:29:14 +02001362 /*
1363 * We dont process them right now but they are fine:
1364 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001365
Ingo Molnard11444d2009-06-03 23:29:14 +02001366 case PERF_EVENT_THROTTLE:
1367 case PERF_EVENT_UNTHROTTLE:
1368 return 0;
1369
Ingo Molnard80d3382009-06-03 23:14:49 +02001370 default:
1371 return -1;
1372 }
1373
1374 return 0;
1375}
1376
1377static int __cmd_report(void)
1378{
Ingo Molnar75051722009-06-03 23:14:49 +02001379 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001380 unsigned long offset = 0;
1381 unsigned long head = 0;
1382 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001383 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001384 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001385 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001386
1387 register_idle_thread();
1388
1389 input = open(input_name, O_RDONLY);
1390 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001391 fprintf(stderr, " failed to open file: %s", input_name);
1392 if (!strcmp(input_name, "perf.data"))
1393 fprintf(stderr, " (try 'perf record' first)");
1394 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001395 exit(-1);
1396 }
1397
1398 ret = fstat(input, &stat);
1399 if (ret < 0) {
1400 perror("failed to stat file");
1401 exit(-1);
1402 }
1403
1404 if (!stat.st_size) {
1405 fprintf(stderr, "zero-sized file, nothing to do!\n");
1406 exit(0);
1407 }
1408
1409 if (load_kernel() < 0) {
1410 perror("failed to load kernel symbols");
1411 return EXIT_FAILURE;
1412 }
1413
1414 if (!full_paths) {
1415 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1416 perror("failed to get the current directory");
1417 return EXIT_FAILURE;
1418 }
1419 cwdlen = strlen(cwd);
1420 } else {
1421 cwd = NULL;
1422 cwdlen = 0;
1423 }
1424remap:
1425 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1426 MAP_SHARED, input, offset);
1427 if (buf == MAP_FAILED) {
1428 perror("failed to mmap file");
1429 exit(-1);
1430 }
1431
1432more:
1433 event = (event_t *)(buf + head);
1434
1435 size = event->header.size;
1436 if (!size)
1437 size = 8;
1438
1439 if (head + event->header.size >= page_size * mmap_window) {
1440 unsigned long shift = page_size * (head / page_size);
1441 int ret;
1442
1443 ret = munmap(buf, page_size * mmap_window);
1444 assert(ret == 0);
1445
1446 offset += shift;
1447 head -= shift;
1448 goto remap;
1449 }
1450
1451 size = event->header.size;
1452
Ingo Molnar8465b052009-06-14 14:44:07 +02001453 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001454 (void *)(offset + head),
1455 (void *)(long)event->header.size,
1456 event->header.type);
1457
Ingo Molnard80d3382009-06-03 23:14:49 +02001458 if (!size || process_event(event, offset, head) < 0) {
1459
Ingo Molnar35029732009-06-03 09:38:58 +02001460 dprintf("%p [%p]: skipping unknown header type: %d\n",
1461 (void *)(offset + head),
1462 (void *)(long)(event->header.size),
1463 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001464
Ingo Molnar3e706112009-05-26 18:53:17 +02001465 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001466
1467 /*
1468 * assume we lost track of the stream, check alignment, and
1469 * increment a single u64 in the hope to catch on again 'soon'.
1470 */
1471
1472 if (unlikely(head & 7))
1473 head &= ~7ULL;
1474
1475 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001476 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001477
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001478 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001479
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001480 if (offset + head < stat.st_size)
1481 goto more;
1482
1483 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001484 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001485
Ingo Molnar35029732009-06-03 09:38:58 +02001486 dprintf(" IP events: %10ld\n", total);
1487 dprintf(" mmap events: %10ld\n", total_mmap);
1488 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001489 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001490 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001491 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001492
Ingo Molnar35029732009-06-03 09:38:58 +02001493 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001494 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001495
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001496 if (verbose >= 3)
1497 threads__fprintf(stdout);
1498
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001499 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001500 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001501
Peter Zijlstra82292892009-06-03 12:37:36 +02001502 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001503 output__resort();
1504 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001505
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001506 return rc;
1507}
1508
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001509static const char * const report_usage[] = {
1510 "perf report [<options>] <command>",
1511 NULL
1512};
1513
1514static const struct option options[] = {
1515 OPT_STRING('i', "input", &input_name, "file",
1516 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001517 OPT_BOOLEAN('v', "verbose", &verbose,
1518 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001519 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1520 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001521 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001522 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001523 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001524 OPT_BOOLEAN('P', "full-paths", &full_paths,
1525 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001526 OPT_STRING('p', "parent", &parent_pattern, "regex",
1527 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001528 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1529 "Only display entries with parent-match"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001530 OPT_END()
1531};
1532
Ingo Molnar5352f352009-06-03 10:07:39 +02001533static void setup_sorting(void)
1534{
1535 char *tmp, *tok, *str = strdup(sort_order);
1536
1537 for (tok = strtok_r(str, ", ", &tmp);
1538 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1539 if (sort_dimension__add(tok) < 0) {
1540 error("Unknown --sort key: `%s'", tok);
1541 usage_with_options(report_usage, options);
1542 }
1543 }
1544
1545 free(str);
1546}
1547
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001548int cmd_report(int argc, const char **argv, const char *prefix)
1549{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001550 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001551
1552 page_size = getpagesize();
1553
Ingo Molnaredc52de2009-06-04 16:24:37 +02001554 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001555
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001556 setup_sorting();
1557
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001558 if (parent_pattern != default_parent_pattern)
1559 sort_dimension__add("parent");
1560 else
1561 exclude_other = 0;
1562
Ingo Molnaredc52de2009-06-04 16:24:37 +02001563 /*
1564 * Any (unrecognized) arguments left?
1565 */
1566 if (argc)
1567 usage_with_options(report_usage, options);
1568
Ingo Molnara930d2c2009-05-27 09:50:13 +02001569 setup_pager();
1570
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001571 return __cmd_report();
1572}