blob: 6207a3147fcb7d321965bc2c4660eff951539842 [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 Molnar35029732009-06-03 09:38:58 +020034#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
35
Ingo Molnar16f762a2009-05-27 09:10:38 +020036static int verbose;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030037static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020038
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030039static unsigned long page_size;
40static unsigned long mmap_window = 32;
41
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020042const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030043 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
44 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
45 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
46};
47
48struct ip_event {
49 struct perf_event_header header;
50 __u64 ip;
51 __u32 pid, tid;
52};
53struct mmap_event {
54 struct perf_event_header header;
55 __u32 pid, tid;
56 __u64 start;
57 __u64 len;
58 __u64 pgoff;
59 char filename[PATH_MAX];
60};
61struct comm_event {
62 struct perf_event_header header;
63 __u32 pid,tid;
64 char comm[16];
65};
66
67typedef union event_union {
68 struct perf_event_header header;
69 struct ip_event ip;
70 struct mmap_event mmap;
71 struct comm_event comm;
72} event_t;
73
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030074static LIST_HEAD(dsos);
75static struct dso *kernel_dso;
76
77static void dsos__add(struct dso *dso)
78{
79 list_add_tail(&dso->node, &dsos);
80}
81
82static struct dso *dsos__find(const char *name)
83{
84 struct dso *pos;
85
86 list_for_each_entry(pos, &dsos, node)
87 if (strcmp(pos->name, name) == 0)
88 return pos;
89 return NULL;
90}
91
92static struct dso *dsos__findnew(const char *name)
93{
94 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +020095 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030096
Ingo Molnar4593bba2009-06-02 15:34:25 +020097 if (dso)
98 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030099
Ingo Molnar4593bba2009-06-02 15:34:25 +0200100 dso = dso__new(name, 0);
101 if (!dso)
102 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200103
Ingo Molnar4593bba2009-06-02 15:34:25 +0200104 nr = dso__load(dso, NULL);
105 if (nr < 0) {
106 fprintf(stderr, "Failed to open: %s\n", name);
107 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300108 }
Ingo Molnar4593bba2009-06-02 15:34:25 +0200109 if (!nr && verbose) {
110 fprintf(stderr,
111 "No symbols found in: %s, maybe install a debug package?\n",
112 name);
113 }
114
115 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300116
117 return dso;
118
119out_delete_dso:
120 dso__delete(dso);
121 return NULL;
122}
123
Ingo Molnar16f762a2009-05-27 09:10:38 +0200124static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300125{
126 struct dso *pos;
127
128 list_for_each_entry(pos, &dsos, node)
129 dso__fprintf(pos, fp);
130}
131
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200132static int load_kernel(void)
133{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300134 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200135
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300136 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200137 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300138 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200139
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300140 err = dso__load_kernel(kernel_dso, vmlinux, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300141 if (err) {
142 dso__delete(kernel_dso);
143 kernel_dso = NULL;
144 } else
145 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200146
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300147 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200148}
149
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300150static int strcommon(const char *pathname, const char *cwd, int cwdlen)
151{
152 int n = 0;
153
154 while (pathname[n] == cwd[n] && n < cwdlen)
155 ++n;
156
157 return n;
158}
159
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300160struct map {
161 struct list_head node;
162 uint64_t start;
163 uint64_t end;
164 uint64_t pgoff;
165 struct dso *dso;
166};
167
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300168static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300169{
170 struct map *self = malloc(sizeof(*self));
171
172 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300173 const char *filename = event->filename;
174 char newfilename[PATH_MAX];
175
176 if (cwd) {
177 int n = strcommon(filename, cwd, cwdlen);
178 if (n == cwdlen) {
179 snprintf(newfilename, sizeof(newfilename),
180 ".%s", filename + n);
181 filename = newfilename;
182 }
183 }
184
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300185 self->start = event->start;
186 self->end = event->start + event->len;
187 self->pgoff = event->pgoff;
188
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300189 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300190 if (self->dso == NULL)
191 goto out_delete;
192 }
193 return self;
194out_delete:
195 free(self);
196 return NULL;
197}
198
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300199struct thread;
200
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300201struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300202 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300203 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300204 pid_t pid;
205 char *comm;
206};
207
208static struct thread *thread__new(pid_t pid)
209{
210 struct thread *self = malloc(sizeof(*self));
211
212 if (self != NULL) {
213 self->pid = pid;
Ingo Molnar0a520c62009-06-02 23:24:45 +0200214 self->comm = malloc(30);
215 if (self->comm)
216 sprintf(self->comm, ":%d", pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300217 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300218 }
219
220 return self;
221}
222
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300223static int thread__set_comm(struct thread *self, const char *comm)
224{
225 self->comm = strdup(comm);
226 return self->comm ? 0 : -ENOMEM;
227}
228
Ingo Molnar16f762a2009-05-27 09:10:38 +0200229static struct rb_root threads;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300230
231static struct thread *threads__findnew(pid_t pid)
232{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300233 struct rb_node **p = &threads.rb_node;
234 struct rb_node *parent = NULL;
235 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300236
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300237 while (*p != NULL) {
238 parent = *p;
239 th = rb_entry(parent, struct thread, rb_node);
240
241 if (th->pid == pid)
242 return th;
243
244 if (pid < th->pid)
245 p = &(*p)->rb_left;
246 else
247 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300248 }
249
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300250 th = thread__new(pid);
251 if (th != NULL) {
252 rb_link_node(&th->rb_node, parent, p);
253 rb_insert_color(&th->rb_node, &threads);
254 }
255 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300256}
257
258static void thread__insert_map(struct thread *self, struct map *map)
259{
260 list_add_tail(&map->node, &self->maps);
261}
262
263static struct map *thread__find_map(struct thread *self, uint64_t ip)
264{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200265 struct map *pos;
266
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300267 if (self == NULL)
268 return NULL;
269
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300270 list_for_each_entry(pos, &self->maps, node)
271 if (ip >= pos->start && ip <= pos->end)
272 return pos;
273
274 return NULL;
275}
276
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200277/*
278 * histogram, sorted on item, collects counts
279 */
280
281static struct rb_root hist;
282
283struct hist_entry {
284 struct rb_node rb_node;
285
286 struct thread *thread;
287 struct map *map;
288 struct dso *dso;
289 struct symbol *sym;
290 uint64_t ip;
291 char level;
292
293 uint32_t count;
294};
295
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200296/*
297 * configurable sorting bits
298 */
299
300struct sort_entry {
301 struct list_head list;
302
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200303 char *header;
304
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200305 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
306 size_t (*print)(FILE *fp, struct hist_entry *);
307};
308
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200309static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200310sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
311{
312 return right->thread->pid - left->thread->pid;
313}
314
315static size_t
316sort__thread_print(FILE *fp, struct hist_entry *self)
317{
Ingo Molnarcf25c632009-06-02 22:12:14 +0200318 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200319}
320
321static struct sort_entry sort_thread = {
Ingo Molnarcf25c632009-06-02 22:12:14 +0200322 .header = " Command: Pid ",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200323 .cmp = sort__thread_cmp,
324 .print = sort__thread_print,
325};
326
327static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200328sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
329{
330 char *comm_l = left->thread->comm;
331 char *comm_r = right->thread->comm;
332
333 if (!comm_l || !comm_r) {
334 if (!comm_l && !comm_r)
335 return 0;
336 else if (!comm_l)
337 return -1;
338 else
339 return 1;
340 }
341
342 return strcmp(comm_l, comm_r);
343}
344
345static size_t
346sort__comm_print(FILE *fp, struct hist_entry *self)
347{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200348 return fprintf(fp, " %16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200349}
350
351static struct sort_entry sort_comm = {
Ingo Molnar4593bba2009-06-02 15:34:25 +0200352 .header = " Command",
Peter Zijlstra992444b2009-05-27 20:20:27 +0200353 .cmp = sort__comm_cmp,
354 .print = sort__comm_print,
355};
356
357static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200358sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
359{
360 struct dso *dso_l = left->dso;
361 struct dso *dso_r = right->dso;
362
363 if (!dso_l || !dso_r) {
364 if (!dso_l && !dso_r)
365 return 0;
366 else if (!dso_l)
367 return -1;
368 else
369 return 1;
370 }
371
372 return strcmp(dso_l->name, dso_r->name);
373}
374
375static size_t
376sort__dso_print(FILE *fp, struct hist_entry *self)
377{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200378 if (self->dso)
379 return fprintf(fp, " %-25s", self->dso->name);
380
381 return fprintf(fp, " %016llx", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200382}
383
384static struct sort_entry sort_dso = {
Ingo Molnarcf25c632009-06-02 22:12:14 +0200385 .header = " Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200386 .cmp = sort__dso_cmp,
387 .print = sort__dso_print,
388};
389
390static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200391sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300392{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200393 uint64_t ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200394
395 if (left->sym == right->sym)
396 return 0;
397
398 ip_l = left->sym ? left->sym->start : left->ip;
399 ip_r = right->sym ? right->sym->start : right->ip;
400
401 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300402}
403
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200404static size_t
405sort__sym_print(FILE *fp, struct hist_entry *self)
406{
407 size_t ret = 0;
408
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200409 if (verbose)
Ingo Molnar0a520c62009-06-02 23:24:45 +0200410 ret += fprintf(fp, " %#018llx", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200411
Ingo Molnar0a520c62009-06-02 23:24:45 +0200412 if (self->dso)
413 ret += fprintf(fp, " %s: ", self->dso->name);
414 else
415 ret += fprintf(fp, " %#016llx: ", (__u64)self->ip);
416
417 if (self->sym)
418 ret += fprintf(fp, "%s", self->sym->name);
419 else
420 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200421
422 return ret;
423}
424
425static struct sort_entry sort_sym = {
Ingo Molnar4593bba2009-06-02 15:34:25 +0200426 .header = " Shared Object: Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200427 .cmp = sort__sym_cmp,
428 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200429};
430
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200431struct sort_dimension {
432 char *name;
433 struct sort_entry *entry;
434 int taken;
435};
436
437static struct sort_dimension sort_dimensions[] = {
438 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200439 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200440 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200441 { .name = "symbol", .entry = &sort_sym, },
442};
443
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200444static LIST_HEAD(hist_entry__sort_list);
445
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200446static int sort_dimension__add(char *tok)
447{
448 int i;
449
450 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
451 struct sort_dimension *sd = &sort_dimensions[i];
452
453 if (sd->taken)
454 continue;
455
456 if (strcmp(tok, sd->name))
457 continue;
458
459 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
460 sd->taken = 1;
461 return 0;
462 }
463
464 return -ESRCH;
465}
466
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200467static void setup_sorting(void)
468{
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200469 char *tmp, *tok, *str = strdup(sort_order);
470
471 for (tok = strtok_r(str, ", ", &tmp);
472 tok; tok = strtok_r(NULL, ", ", &tmp))
473 sort_dimension__add(tok);
474
475 free(str);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200476}
477
478static int64_t
479hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
480{
481 struct sort_entry *se;
482 int64_t cmp = 0;
483
484 list_for_each_entry(se, &hist_entry__sort_list, list) {
485 cmp = se->cmp(left, right);
486 if (cmp)
487 break;
488 }
489
490 return cmp;
491}
492
493static size_t
494hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
495{
496 struct sort_entry *se;
497 size_t ret;
498
499 if (total_samples) {
Ingo Molnar2d655372009-05-27 21:36:22 +0200500 ret = fprintf(fp, " %5.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200501 (self->count * 100.0) / total_samples);
502 } else
503 ret = fprintf(fp, "%12d ", self->count);
504
505 list_for_each_entry(se, &hist_entry__sort_list, list)
506 ret += se->print(fp, self);
507
508 ret += fprintf(fp, "\n");
509
510 return ret;
511}
512
513/*
514 * collect histogram counts
515 */
516
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200517static int
518hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
519 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300520{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200521 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300522 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200523 struct hist_entry *he;
524 struct hist_entry entry = {
525 .thread = thread,
526 .map = map,
527 .dso = dso,
528 .sym = sym,
529 .ip = ip,
530 .level = level,
531 .count = 1,
532 };
533 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300534
535 while (*p != NULL) {
536 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200537 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300538
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200539 cmp = hist_entry__cmp(&entry, he);
540
541 if (!cmp) {
542 he->count++;
543 return 0;
544 }
545
546 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300547 p = &(*p)->rb_left;
548 else
549 p = &(*p)->rb_right;
550 }
551
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200552 he = malloc(sizeof(*he));
553 if (!he)
554 return -ENOMEM;
555 *he = entry;
556 rb_link_node(&he->rb_node, parent, p);
557 rb_insert_color(&he->rb_node, &hist);
558
559 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300560}
561
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200562/*
563 * reverse the map, sort on count.
564 */
565
566static struct rb_root output_hists;
567
568static void output__insert_entry(struct hist_entry *he)
569{
570 struct rb_node **p = &output_hists.rb_node;
571 struct rb_node *parent = NULL;
572 struct hist_entry *iter;
573
574 while (*p != NULL) {
575 parent = *p;
576 iter = rb_entry(parent, struct hist_entry, rb_node);
577
578 if (he->count > iter->count)
579 p = &(*p)->rb_left;
580 else
581 p = &(*p)->rb_right;
582 }
583
584 rb_link_node(&he->rb_node, parent, p);
585 rb_insert_color(&he->rb_node, &output_hists);
586}
587
588static void output__resort(void)
589{
590 struct rb_node *next = rb_first(&hist);
591 struct hist_entry *n;
592
593 while (next) {
594 n = rb_entry(next, struct hist_entry, rb_node);
595 next = rb_next(&n->rb_node);
596
597 rb_erase(&n->rb_node, &hist);
598 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300599 }
600}
601
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200602static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300603{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200604 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200605 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300606 struct rb_node *nd;
607 size_t ret = 0;
608
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200609 fprintf(fp, "#\n");
610
611 fprintf(fp, "# Overhead");
612 list_for_each_entry(se, &hist_entry__sort_list, list)
613 fprintf(fp, " %s", se->header);
614 fprintf(fp, "\n");
615
616 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200617 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200618 int i;
619
Ingo Molnar4593bba2009-06-02 15:34:25 +0200620 fprintf(fp, " ");
621 for (i = 0; i < strlen(se->header)-1; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200622 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200623 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200624 fprintf(fp, "\n");
625
626 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200627
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200628 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
629 pos = rb_entry(nd, struct hist_entry, rb_node);
630 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300631 }
632
633 return ret;
634}
635
Peter Zijlstra436224a2009-06-02 21:02:36 +0200636static void register_idle_thread(void)
637{
638 struct thread *thread = threads__findnew(0);
639
640 if (thread == NULL ||
641 thread__set_comm(thread, "[idle]")) {
642 fprintf(stderr, "problem inserting idle task.\n");
643 exit(-1);
644 }
645}
646
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200647
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200648static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300649{
650 unsigned long offset = 0;
651 unsigned long head = 0;
652 struct stat stat;
653 char *buf;
654 event_t *event;
655 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200656 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200657 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300658 char cwd[PATH_MAX], *cwdp = cwd;
659 int cwdlen;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300660
Peter Zijlstra436224a2009-06-02 21:02:36 +0200661 register_idle_thread();
662
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300663 input = open(input_name, O_RDONLY);
664 if (input < 0) {
665 perror("failed to open file");
666 exit(-1);
667 }
668
669 ret = fstat(input, &stat);
670 if (ret < 0) {
671 perror("failed to stat file");
672 exit(-1);
673 }
674
675 if (!stat.st_size) {
676 fprintf(stderr, "zero-sized file, nothing to do!\n");
677 exit(0);
678 }
679
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200680 if (load_kernel() < 0) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300681 perror("failed to load kernel symbols");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300682 return EXIT_FAILURE;
683 }
684
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300685 if (!full_paths) {
686 if (getcwd(cwd, sizeof(cwd)) == NULL) {
687 perror("failed to get the current directory");
688 return EXIT_FAILURE;
689 }
690 cwdlen = strlen(cwd);
Mike Galbraith10a28252009-06-02 11:04:44 +0200691 } else {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300692 cwdp = NULL;
Mike Galbraith10a28252009-06-02 11:04:44 +0200693 cwdlen = 0;
694 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300695remap:
696 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
697 MAP_SHARED, input, offset);
698 if (buf == MAP_FAILED) {
699 perror("failed to mmap file");
700 exit(-1);
701 }
702
703more:
704 event = (event_t *)(buf + head);
705
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200706 size = event->header.size;
707 if (!size)
708 size = 8;
709
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300710 if (head + event->header.size >= page_size * mmap_window) {
711 unsigned long shift = page_size * (head / page_size);
712 int ret;
713
714 ret = munmap(buf, page_size * mmap_window);
715 assert(ret == 0);
716
717 offset += shift;
718 head -= shift;
719 goto remap;
720 }
721
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200722 size = event->header.size;
723 if (!size)
724 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300725
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300726 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
727 char level;
728 int show = 0;
729 struct dso *dso = NULL;
730 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200731 uint64_t ip = event->ip.ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200732 struct map *map = NULL;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300733
Ingo Molnar35029732009-06-03 09:38:58 +0200734 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
735 (void *)(offset + head),
736 (void *)(long)(event->header.size),
737 event->header.misc,
738 event->ip.pid,
739 (void *)(long)ip);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200740
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 Molnar35029732009-06-03 09:38:58 +0200784 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
785 (void *)(offset + head),
786 (void *)(long)(event->header.size),
787 (void *)(long)event->mmap.start,
788 (void *)(long)event->mmap.len,
789 (void *)(long)event->mmap.pgoff,
790 event->mmap.filename);
791
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300792 if (thread == NULL || map == NULL) {
Ingo Molnar0a520c62009-06-02 23:24:45 +0200793 if (verbose)
794 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnar55717312009-05-27 22:13:17 +0200795 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300796 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300797 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200798 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300799 break;
800 }
801 case PERF_EVENT_COMM: {
802 struct thread *thread = threads__findnew(event->comm.pid);
803
Ingo Molnar35029732009-06-03 09:38:58 +0200804 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
805 (void *)(offset + head),
806 (void *)(long)(event->header.size),
807 event->comm.comm, event->comm.pid);
808
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300809 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300810 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnar55717312009-05-27 22:13:17 +0200811 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
812 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300813 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200814 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300815 break;
816 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200817 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200818broken_event:
Ingo Molnar35029732009-06-03 09:38:58 +0200819 dprintf("%p [%p]: skipping unknown header type: %d\n",
820 (void *)(offset + head),
821 (void *)(long)(event->header.size),
822 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200823
Ingo Molnar3e706112009-05-26 18:53:17 +0200824 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200825
826 /*
827 * assume we lost track of the stream, check alignment, and
828 * increment a single u64 in the hope to catch on again 'soon'.
829 */
830
831 if (unlikely(head & 7))
832 head &= ~7ULL;
833
834 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200835 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300836 }
837
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200838 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200839
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300840 if (offset + head < stat.st_size)
841 goto more;
842
843 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300844 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200845
Ingo Molnar35029732009-06-03 09:38:58 +0200846 dprintf(" IP events: %10ld\n", total);
847 dprintf(" mmap events: %10ld\n", total_mmap);
848 dprintf(" comm events: %10ld\n", total_comm);
849 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200850
Ingo Molnar35029732009-06-03 09:38:58 +0200851 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +0200852 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200853
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200854 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +0200855 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200856
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200857 output__resort();
858 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300859
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300860 return rc;
861}
862
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200863static const char * const report_usage[] = {
864 "perf report [<options>] <command>",
865 NULL
866};
867
868static const struct option options[] = {
869 OPT_STRING('i', "input", &input_name, "file",
870 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300871 OPT_BOOLEAN('v', "verbose", &verbose,
872 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200873 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
874 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200875 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +0200876 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
877 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300878 OPT_BOOLEAN('P', "full-paths", &full_paths,
879 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200880 OPT_END()
881};
882
883int cmd_report(int argc, const char **argv, const char *prefix)
884{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300885 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200886
887 page_size = getpagesize();
888
889 parse_options(argc, argv, options, report_usage, 0);
890
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200891 setup_sorting();
892
Ingo Molnara930d2c2009-05-27 09:50:13 +0200893 setup_pager();
894
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200895 return __cmd_report();
896}