blob: 9da990fba4a5eb797f15befe860620c4b05fec5d [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
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030012#include "util/list.h"
Ingo Molnara930d2c2009-05-27 09:50:13 +020013#include "util/cache.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030014#include "util/rbtree.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030015#include "util/symbol.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030016#include "util/string.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030017
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020018#include "perf.h"
19
20#include "util/parse-options.h"
21#include "util/parse-events.h"
22
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030023#define SHOW_KERNEL 1
24#define SHOW_USER 2
25#define SHOW_HV 4
26
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020027static char const *input_name = "perf.data";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020028static char *vmlinux = NULL;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +020029static char *sort_order = "comm,dso";
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030030static int input;
31static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
32
Ingo Molnar97b07b62009-05-26 18:48:58 +020033static int dump_trace = 0;
Ingo Molnar16f762a2009-05-27 09:10:38 +020034static int verbose;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030035static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020036
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030037static unsigned long page_size;
38static unsigned long mmap_window = 32;
39
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020040const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030041 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
42 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
43 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
44};
45
46struct ip_event {
47 struct perf_event_header header;
48 __u64 ip;
49 __u32 pid, tid;
50};
51struct mmap_event {
52 struct perf_event_header header;
53 __u32 pid, tid;
54 __u64 start;
55 __u64 len;
56 __u64 pgoff;
57 char filename[PATH_MAX];
58};
59struct comm_event {
60 struct perf_event_header header;
61 __u32 pid,tid;
62 char comm[16];
63};
64
65typedef union event_union {
66 struct perf_event_header header;
67 struct ip_event ip;
68 struct mmap_event mmap;
69 struct comm_event comm;
70} event_t;
71
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030072static LIST_HEAD(dsos);
73static struct dso *kernel_dso;
74
75static void dsos__add(struct dso *dso)
76{
77 list_add_tail(&dso->node, &dsos);
78}
79
80static struct dso *dsos__find(const char *name)
81{
82 struct dso *pos;
83
84 list_for_each_entry(pos, &dsos, node)
85 if (strcmp(pos->name, name) == 0)
86 return pos;
87 return NULL;
88}
89
90static struct dso *dsos__findnew(const char *name)
91{
92 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +020093 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030094
Ingo Molnar4593bba2009-06-02 15:34:25 +020095 if (dso)
96 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030097
Ingo Molnar4593bba2009-06-02 15:34:25 +020098 dso = dso__new(name, 0);
99 if (!dso)
100 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200101
Ingo Molnar4593bba2009-06-02 15:34:25 +0200102 nr = dso__load(dso, NULL);
103 if (nr < 0) {
104 fprintf(stderr, "Failed to open: %s\n", name);
105 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300106 }
Ingo Molnar4593bba2009-06-02 15:34:25 +0200107 if (!nr && verbose) {
108 fprintf(stderr,
109 "No symbols found in: %s, maybe install a debug package?\n",
110 name);
111 }
112
113 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300114
115 return dso;
116
117out_delete_dso:
118 dso__delete(dso);
119 return NULL;
120}
121
Ingo Molnar16f762a2009-05-27 09:10:38 +0200122static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300123{
124 struct dso *pos;
125
126 list_for_each_entry(pos, &dsos, node)
127 dso__fprintf(pos, fp);
128}
129
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200130static int load_kernel(void)
131{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300132 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200133
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300134 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200135 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300136 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200137
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300138 err = dso__load_kernel(kernel_dso, vmlinux, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300139 if (err) {
140 dso__delete(kernel_dso);
141 kernel_dso = NULL;
142 } else
143 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200144
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300145 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200146}
147
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300148static int strcommon(const char *pathname, const char *cwd, int cwdlen)
149{
150 int n = 0;
151
152 while (pathname[n] == cwd[n] && n < cwdlen)
153 ++n;
154
155 return n;
156}
157
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300158struct map {
159 struct list_head node;
160 uint64_t start;
161 uint64_t end;
162 uint64_t pgoff;
163 struct dso *dso;
164};
165
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300166static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300167{
168 struct map *self = malloc(sizeof(*self));
169
170 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300171 const char *filename = event->filename;
172 char newfilename[PATH_MAX];
173
174 if (cwd) {
175 int n = strcommon(filename, cwd, cwdlen);
176 if (n == cwdlen) {
177 snprintf(newfilename, sizeof(newfilename),
178 ".%s", filename + n);
179 filename = newfilename;
180 }
181 }
182
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300183 self->start = event->start;
184 self->end = event->start + event->len;
185 self->pgoff = event->pgoff;
186
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300187 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300188 if (self->dso == NULL)
189 goto out_delete;
190 }
191 return self;
192out_delete:
193 free(self);
194 return NULL;
195}
196
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300197struct thread;
198
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300199struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300200 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300201 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300202 pid_t pid;
203 char *comm;
204};
205
206static struct thread *thread__new(pid_t pid)
207{
208 struct thread *self = malloc(sizeof(*self));
209
210 if (self != NULL) {
211 self->pid = pid;
Ingo Molnar0a520c62009-06-02 23:24:45 +0200212 self->comm = malloc(30);
213 if (self->comm)
214 sprintf(self->comm, ":%d", pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300215 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300216 }
217
218 return self;
219}
220
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300221static int thread__set_comm(struct thread *self, const char *comm)
222{
223 self->comm = strdup(comm);
224 return self->comm ? 0 : -ENOMEM;
225}
226
Ingo Molnar16f762a2009-05-27 09:10:38 +0200227static struct rb_root threads;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300228
229static struct thread *threads__findnew(pid_t pid)
230{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300231 struct rb_node **p = &threads.rb_node;
232 struct rb_node *parent = NULL;
233 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300234
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300235 while (*p != NULL) {
236 parent = *p;
237 th = rb_entry(parent, struct thread, rb_node);
238
239 if (th->pid == pid)
240 return th;
241
242 if (pid < th->pid)
243 p = &(*p)->rb_left;
244 else
245 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300246 }
247
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300248 th = thread__new(pid);
249 if (th != NULL) {
250 rb_link_node(&th->rb_node, parent, p);
251 rb_insert_color(&th->rb_node, &threads);
252 }
253 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300254}
255
256static void thread__insert_map(struct thread *self, struct map *map)
257{
258 list_add_tail(&map->node, &self->maps);
259}
260
261static struct map *thread__find_map(struct thread *self, uint64_t ip)
262{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200263 struct map *pos;
264
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300265 if (self == NULL)
266 return NULL;
267
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300268 list_for_each_entry(pos, &self->maps, node)
269 if (ip >= pos->start && ip <= pos->end)
270 return pos;
271
272 return NULL;
273}
274
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200275/*
276 * histogram, sorted on item, collects counts
277 */
278
279static struct rb_root hist;
280
281struct hist_entry {
282 struct rb_node rb_node;
283
284 struct thread *thread;
285 struct map *map;
286 struct dso *dso;
287 struct symbol *sym;
288 uint64_t ip;
289 char level;
290
291 uint32_t count;
292};
293
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200294/*
295 * configurable sorting bits
296 */
297
298struct sort_entry {
299 struct list_head list;
300
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200301 char *header;
302
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200303 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
304 size_t (*print)(FILE *fp, struct hist_entry *);
305};
306
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200307static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200308sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
309{
310 return right->thread->pid - left->thread->pid;
311}
312
313static size_t
314sort__thread_print(FILE *fp, struct hist_entry *self)
315{
Ingo Molnarcf25c632009-06-02 22:12:14 +0200316 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200317}
318
319static struct sort_entry sort_thread = {
Ingo Molnarcf25c632009-06-02 22:12:14 +0200320 .header = " Command: Pid ",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200321 .cmp = sort__thread_cmp,
322 .print = sort__thread_print,
323};
324
325static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200326sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
327{
328 char *comm_l = left->thread->comm;
329 char *comm_r = right->thread->comm;
330
331 if (!comm_l || !comm_r) {
332 if (!comm_l && !comm_r)
333 return 0;
334 else if (!comm_l)
335 return -1;
336 else
337 return 1;
338 }
339
340 return strcmp(comm_l, comm_r);
341}
342
343static size_t
344sort__comm_print(FILE *fp, struct hist_entry *self)
345{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200346 return fprintf(fp, " %16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200347}
348
349static struct sort_entry sort_comm = {
Ingo Molnar4593bba2009-06-02 15:34:25 +0200350 .header = " Command",
Peter Zijlstra992444b2009-05-27 20:20:27 +0200351 .cmp = sort__comm_cmp,
352 .print = sort__comm_print,
353};
354
355static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200356sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
357{
358 struct dso *dso_l = left->dso;
359 struct dso *dso_r = right->dso;
360
361 if (!dso_l || !dso_r) {
362 if (!dso_l && !dso_r)
363 return 0;
364 else if (!dso_l)
365 return -1;
366 else
367 return 1;
368 }
369
370 return strcmp(dso_l->name, dso_r->name);
371}
372
373static size_t
374sort__dso_print(FILE *fp, struct hist_entry *self)
375{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200376 if (self->dso)
377 return fprintf(fp, " %-25s", self->dso->name);
378
379 return fprintf(fp, " %016llx", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200380}
381
382static struct sort_entry sort_dso = {
Ingo Molnarcf25c632009-06-02 22:12:14 +0200383 .header = " Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200384 .cmp = sort__dso_cmp,
385 .print = sort__dso_print,
386};
387
388static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200389sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300390{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200391 uint64_t ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200392
393 if (left->sym == right->sym)
394 return 0;
395
396 ip_l = left->sym ? left->sym->start : left->ip;
397 ip_r = right->sym ? right->sym->start : right->ip;
398
399 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300400}
401
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200402static size_t
403sort__sym_print(FILE *fp, struct hist_entry *self)
404{
405 size_t ret = 0;
406
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200407 if (verbose)
Ingo Molnar0a520c62009-06-02 23:24:45 +0200408 ret += fprintf(fp, " %#018llx", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200409
Ingo Molnar0a520c62009-06-02 23:24:45 +0200410 if (self->dso)
411 ret += fprintf(fp, " %s: ", self->dso->name);
412 else
413 ret += fprintf(fp, " %#016llx: ", (__u64)self->ip);
414
415 if (self->sym)
416 ret += fprintf(fp, "%s", self->sym->name);
417 else
418 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200419
420 return ret;
421}
422
423static struct sort_entry sort_sym = {
Ingo Molnar4593bba2009-06-02 15:34:25 +0200424 .header = " Shared Object: Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200425 .cmp = sort__sym_cmp,
426 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200427};
428
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200429struct sort_dimension {
430 char *name;
431 struct sort_entry *entry;
432 int taken;
433};
434
435static struct sort_dimension sort_dimensions[] = {
436 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200437 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200438 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200439 { .name = "symbol", .entry = &sort_sym, },
440};
441
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200442static LIST_HEAD(hist_entry__sort_list);
443
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200444static int sort_dimension__add(char *tok)
445{
446 int i;
447
448 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
449 struct sort_dimension *sd = &sort_dimensions[i];
450
451 if (sd->taken)
452 continue;
453
454 if (strcmp(tok, sd->name))
455 continue;
456
457 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
458 sd->taken = 1;
459 return 0;
460 }
461
462 return -ESRCH;
463}
464
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200465static void setup_sorting(void)
466{
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200467 char *tmp, *tok, *str = strdup(sort_order);
468
469 for (tok = strtok_r(str, ", ", &tmp);
470 tok; tok = strtok_r(NULL, ", ", &tmp))
471 sort_dimension__add(tok);
472
473 free(str);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200474}
475
476static int64_t
477hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
478{
479 struct sort_entry *se;
480 int64_t cmp = 0;
481
482 list_for_each_entry(se, &hist_entry__sort_list, list) {
483 cmp = se->cmp(left, right);
484 if (cmp)
485 break;
486 }
487
488 return cmp;
489}
490
491static size_t
492hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
493{
494 struct sort_entry *se;
495 size_t ret;
496
497 if (total_samples) {
Ingo Molnar2d655372009-05-27 21:36:22 +0200498 ret = fprintf(fp, " %5.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200499 (self->count * 100.0) / total_samples);
500 } else
501 ret = fprintf(fp, "%12d ", self->count);
502
503 list_for_each_entry(se, &hist_entry__sort_list, list)
504 ret += se->print(fp, self);
505
506 ret += fprintf(fp, "\n");
507
508 return ret;
509}
510
511/*
512 * collect histogram counts
513 */
514
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200515static int
516hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
517 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300518{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200519 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300520 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200521 struct hist_entry *he;
522 struct hist_entry entry = {
523 .thread = thread,
524 .map = map,
525 .dso = dso,
526 .sym = sym,
527 .ip = ip,
528 .level = level,
529 .count = 1,
530 };
531 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300532
533 while (*p != NULL) {
534 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200535 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300536
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200537 cmp = hist_entry__cmp(&entry, he);
538
539 if (!cmp) {
540 he->count++;
541 return 0;
542 }
543
544 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300545 p = &(*p)->rb_left;
546 else
547 p = &(*p)->rb_right;
548 }
549
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200550 he = malloc(sizeof(*he));
551 if (!he)
552 return -ENOMEM;
553 *he = entry;
554 rb_link_node(&he->rb_node, parent, p);
555 rb_insert_color(&he->rb_node, &hist);
556
557 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300558}
559
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200560/*
561 * reverse the map, sort on count.
562 */
563
564static struct rb_root output_hists;
565
566static void output__insert_entry(struct hist_entry *he)
567{
568 struct rb_node **p = &output_hists.rb_node;
569 struct rb_node *parent = NULL;
570 struct hist_entry *iter;
571
572 while (*p != NULL) {
573 parent = *p;
574 iter = rb_entry(parent, struct hist_entry, rb_node);
575
576 if (he->count > iter->count)
577 p = &(*p)->rb_left;
578 else
579 p = &(*p)->rb_right;
580 }
581
582 rb_link_node(&he->rb_node, parent, p);
583 rb_insert_color(&he->rb_node, &output_hists);
584}
585
586static void output__resort(void)
587{
588 struct rb_node *next = rb_first(&hist);
589 struct hist_entry *n;
590
591 while (next) {
592 n = rb_entry(next, struct hist_entry, rb_node);
593 next = rb_next(&n->rb_node);
594
595 rb_erase(&n->rb_node, &hist);
596 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300597 }
598}
599
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200600static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300601{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200602 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200603 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300604 struct rb_node *nd;
605 size_t ret = 0;
606
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200607 fprintf(fp, "#\n");
608
609 fprintf(fp, "# Overhead");
610 list_for_each_entry(se, &hist_entry__sort_list, list)
611 fprintf(fp, " %s", se->header);
612 fprintf(fp, "\n");
613
614 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200615 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200616 int i;
617
Ingo Molnar4593bba2009-06-02 15:34:25 +0200618 fprintf(fp, " ");
619 for (i = 0; i < strlen(se->header)-1; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200620 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200621 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200622 fprintf(fp, "\n");
623
624 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200625
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200626 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
627 pos = rb_entry(nd, struct hist_entry, rb_node);
628 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300629 }
630
631 return ret;
632}
633
Peter Zijlstra436224a2009-06-02 21:02:36 +0200634static void register_idle_thread(void)
635{
636 struct thread *thread = threads__findnew(0);
637
638 if (thread == NULL ||
639 thread__set_comm(thread, "[idle]")) {
640 fprintf(stderr, "problem inserting idle task.\n");
641 exit(-1);
642 }
643}
644
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200645
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200646static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300647{
648 unsigned long offset = 0;
649 unsigned long head = 0;
650 struct stat stat;
651 char *buf;
652 event_t *event;
653 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200654 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200655 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300656 char cwd[PATH_MAX], *cwdp = cwd;
657 int cwdlen;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300658
Peter Zijlstra436224a2009-06-02 21:02:36 +0200659 register_idle_thread();
660
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300661 input = open(input_name, O_RDONLY);
662 if (input < 0) {
663 perror("failed to open file");
664 exit(-1);
665 }
666
667 ret = fstat(input, &stat);
668 if (ret < 0) {
669 perror("failed to stat file");
670 exit(-1);
671 }
672
673 if (!stat.st_size) {
674 fprintf(stderr, "zero-sized file, nothing to do!\n");
675 exit(0);
676 }
677
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200678 if (load_kernel() < 0) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300679 perror("failed to load kernel symbols");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300680 return EXIT_FAILURE;
681 }
682
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300683 if (!full_paths) {
684 if (getcwd(cwd, sizeof(cwd)) == NULL) {
685 perror("failed to get the current directory");
686 return EXIT_FAILURE;
687 }
688 cwdlen = strlen(cwd);
Mike Galbraith10a28252009-06-02 11:04:44 +0200689 } else {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300690 cwdp = NULL;
Mike Galbraith10a28252009-06-02 11:04:44 +0200691 cwdlen = 0;
692 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300693remap:
694 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
695 MAP_SHARED, input, offset);
696 if (buf == MAP_FAILED) {
697 perror("failed to mmap file");
698 exit(-1);
699 }
700
701more:
702 event = (event_t *)(buf + head);
703
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200704 size = event->header.size;
705 if (!size)
706 size = 8;
707
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300708 if (head + event->header.size >= page_size * mmap_window) {
709 unsigned long shift = page_size * (head / page_size);
710 int ret;
711
712 ret = munmap(buf, page_size * mmap_window);
713 assert(ret == 0);
714
715 offset += shift;
716 head -= shift;
717 goto remap;
718 }
719
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200720 size = event->header.size;
721 if (!size)
722 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300723
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300724 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
725 char level;
726 int show = 0;
727 struct dso *dso = NULL;
728 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200729 uint64_t ip = event->ip.ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200730 struct map *map = NULL;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300731
Ingo Molnar97b07b62009-05-26 18:48:58 +0200732 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200733 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
734 (void *)(offset + head),
735 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200736 event->header.misc,
737 event->ip.pid,
Ingo Molnar16f762a2009-05-27 09:10:38 +0200738 (void *)(long)ip);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200739 }
740
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300741 if (thread == NULL) {
Ingo Molnar55717312009-05-27 22:13:17 +0200742 fprintf(stderr, "problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300743 event->header.type);
Ingo Molnar55717312009-05-27 22:13:17 +0200744 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300745 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300746
747 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
748 show = SHOW_KERNEL;
749 level = 'k';
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200750
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300751 dso = kernel_dso;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200752
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300753 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200754
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300755 show = SHOW_USER;
756 level = '.';
Ingo Molnar16f762a2009-05-27 09:10:38 +0200757
758 map = thread__find_map(thread, ip);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200759 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300760 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200761 ip -= map->start + map->pgoff;
762 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200763
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300764 } else {
765 show = SHOW_HV;
766 level = 'H';
767 }
768
769 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200770 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300771
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200772 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
773 fprintf(stderr,
Ingo Molnar55717312009-05-27 22:13:17 +0200774 "problem incrementing symbol count, skipping event\n");
775 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300776 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300777 }
778 total++;
779 } else switch (event->header.type) {
780 case PERF_EVENT_MMAP: {
781 struct thread *thread = threads__findnew(event->mmap.pid);
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300782 struct map *map = map__new(&event->mmap, cwdp, cwdlen);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300783
Ingo Molnar97b07b62009-05-26 18:48:58 +0200784 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200785 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
786 (void *)(offset + head),
787 (void *)(long)(event->header.size),
Ingo Molnar16f762a2009-05-27 09:10:38 +0200788 (void *)(long)event->mmap.start,
789 (void *)(long)event->mmap.len,
790 (void *)(long)event->mmap.pgoff,
Ingo Molnar97b07b62009-05-26 18:48:58 +0200791 event->mmap.filename);
792 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300793 if (thread == NULL || map == NULL) {
Ingo Molnar0a520c62009-06-02 23:24:45 +0200794 if (verbose)
795 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnar55717312009-05-27 22:13:17 +0200796 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300797 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300798 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200799 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300800 break;
801 }
802 case PERF_EVENT_COMM: {
803 struct thread *thread = threads__findnew(event->comm.pid);
804
Ingo Molnar97b07b62009-05-26 18:48:58 +0200805 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200806 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
807 (void *)(offset + head),
808 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200809 event->comm.comm, event->comm.pid);
810 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300811 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300812 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnar55717312009-05-27 22:13:17 +0200813 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
814 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300815 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200816 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300817 break;
818 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200819 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200820broken_event:
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200821 if (dump_trace)
822 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
823 (void *)(offset + head),
824 (void *)(long)(event->header.size),
825 event->header.type);
826
Ingo Molnar3e706112009-05-26 18:53:17 +0200827 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200828
829 /*
830 * assume we lost track of the stream, check alignment, and
831 * increment a single u64 in the hope to catch on again 'soon'.
832 */
833
834 if (unlikely(head & 7))
835 head &= ~7ULL;
836
837 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200838 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300839 }
840
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200841 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200842
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300843 if (offset + head < stat.st_size)
844 goto more;
845
846 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300847 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200848
849 if (dump_trace) {
Ingo Molnar3e706112009-05-26 18:53:17 +0200850 fprintf(stderr, " IP events: %10ld\n", total);
851 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
852 fprintf(stderr, " comm events: %10ld\n", total_comm);
853 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200854
855 return 0;
856 }
857
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200858 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +0200859 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200860
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200861 output__resort();
862 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300863
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300864 return rc;
865}
866
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200867static const char * const report_usage[] = {
868 "perf report [<options>] <command>",
869 NULL
870};
871
872static const struct option options[] = {
873 OPT_STRING('i', "input", &input_name, "file",
874 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300875 OPT_BOOLEAN('v', "verbose", &verbose,
876 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200877 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
878 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200879 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +0200880 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
881 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300882 OPT_BOOLEAN('P', "full-paths", &full_paths,
883 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200884 OPT_END()
885};
886
887int cmd_report(int argc, const char **argv, const char *prefix)
888{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300889 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200890
891 page_size = getpagesize();
892
893 parse_options(argc, argv, options, report_usage, 0);
894
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200895 setup_sorting();
896
Ingo Molnara930d2c2009-05-27 09:50:13 +0200897 setup_pager();
898
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200899 return __cmd_report();
900}