blob: 0f88d9ebb34a3c304252d9d2c52c51ea4e896946 [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
Ingo Molnar5352f352009-06-03 10:07:39 +0200456 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200457 continue;
458
459 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
460 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200461
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200462 return 0;
463 }
464
465 return -ESRCH;
466}
467
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200468static int64_t
469hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
470{
471 struct sort_entry *se;
472 int64_t cmp = 0;
473
474 list_for_each_entry(se, &hist_entry__sort_list, list) {
475 cmp = se->cmp(left, right);
476 if (cmp)
477 break;
478 }
479
480 return cmp;
481}
482
483static size_t
484hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
485{
486 struct sort_entry *se;
487 size_t ret;
488
489 if (total_samples) {
Ingo Molnar2d655372009-05-27 21:36:22 +0200490 ret = fprintf(fp, " %5.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200491 (self->count * 100.0) / total_samples);
492 } else
493 ret = fprintf(fp, "%12d ", self->count);
494
495 list_for_each_entry(se, &hist_entry__sort_list, list)
496 ret += se->print(fp, self);
497
498 ret += fprintf(fp, "\n");
499
500 return ret;
501}
502
503/*
504 * collect histogram counts
505 */
506
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200507static int
508hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
509 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300510{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200511 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300512 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200513 struct hist_entry *he;
514 struct hist_entry entry = {
515 .thread = thread,
516 .map = map,
517 .dso = dso,
518 .sym = sym,
519 .ip = ip,
520 .level = level,
521 .count = 1,
522 };
523 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300524
525 while (*p != NULL) {
526 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200527 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300528
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200529 cmp = hist_entry__cmp(&entry, he);
530
531 if (!cmp) {
532 he->count++;
533 return 0;
534 }
535
536 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300537 p = &(*p)->rb_left;
538 else
539 p = &(*p)->rb_right;
540 }
541
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200542 he = malloc(sizeof(*he));
543 if (!he)
544 return -ENOMEM;
545 *he = entry;
546 rb_link_node(&he->rb_node, parent, p);
547 rb_insert_color(&he->rb_node, &hist);
548
549 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300550}
551
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200552/*
553 * reverse the map, sort on count.
554 */
555
556static struct rb_root output_hists;
557
558static void output__insert_entry(struct hist_entry *he)
559{
560 struct rb_node **p = &output_hists.rb_node;
561 struct rb_node *parent = NULL;
562 struct hist_entry *iter;
563
564 while (*p != NULL) {
565 parent = *p;
566 iter = rb_entry(parent, struct hist_entry, rb_node);
567
568 if (he->count > iter->count)
569 p = &(*p)->rb_left;
570 else
571 p = &(*p)->rb_right;
572 }
573
574 rb_link_node(&he->rb_node, parent, p);
575 rb_insert_color(&he->rb_node, &output_hists);
576}
577
578static void output__resort(void)
579{
580 struct rb_node *next = rb_first(&hist);
581 struct hist_entry *n;
582
583 while (next) {
584 n = rb_entry(next, struct hist_entry, rb_node);
585 next = rb_next(&n->rb_node);
586
587 rb_erase(&n->rb_node, &hist);
588 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300589 }
590}
591
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200592static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300593{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200594 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200595 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300596 struct rb_node *nd;
597 size_t ret = 0;
598
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200599 fprintf(fp, "#\n");
600
601 fprintf(fp, "# Overhead");
602 list_for_each_entry(se, &hist_entry__sort_list, list)
603 fprintf(fp, " %s", se->header);
604 fprintf(fp, "\n");
605
606 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200607 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200608 int i;
609
Ingo Molnar4593bba2009-06-02 15:34:25 +0200610 fprintf(fp, " ");
611 for (i = 0; i < strlen(se->header)-1; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200612 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200613 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200614 fprintf(fp, "\n");
615
616 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200617
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200618 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
619 pos = rb_entry(nd, struct hist_entry, rb_node);
620 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300621 }
622
623 return ret;
624}
625
Peter Zijlstra436224a2009-06-02 21:02:36 +0200626static void register_idle_thread(void)
627{
628 struct thread *thread = threads__findnew(0);
629
630 if (thread == NULL ||
631 thread__set_comm(thread, "[idle]")) {
632 fprintf(stderr, "problem inserting idle task.\n");
633 exit(-1);
634 }
635}
636
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200637
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200638static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300639{
640 unsigned long offset = 0;
641 unsigned long head = 0;
642 struct stat stat;
643 char *buf;
644 event_t *event;
645 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200646 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200647 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300648 char cwd[PATH_MAX], *cwdp = cwd;
649 int cwdlen;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300650
Peter Zijlstra436224a2009-06-02 21:02:36 +0200651 register_idle_thread();
652
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300653 input = open(input_name, O_RDONLY);
654 if (input < 0) {
655 perror("failed to open file");
656 exit(-1);
657 }
658
659 ret = fstat(input, &stat);
660 if (ret < 0) {
661 perror("failed to stat file");
662 exit(-1);
663 }
664
665 if (!stat.st_size) {
666 fprintf(stderr, "zero-sized file, nothing to do!\n");
667 exit(0);
668 }
669
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200670 if (load_kernel() < 0) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300671 perror("failed to load kernel symbols");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300672 return EXIT_FAILURE;
673 }
674
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300675 if (!full_paths) {
676 if (getcwd(cwd, sizeof(cwd)) == NULL) {
677 perror("failed to get the current directory");
678 return EXIT_FAILURE;
679 }
680 cwdlen = strlen(cwd);
Mike Galbraith10a28252009-06-02 11:04:44 +0200681 } else {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300682 cwdp = NULL;
Mike Galbraith10a28252009-06-02 11:04:44 +0200683 cwdlen = 0;
684 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300685remap:
686 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
687 MAP_SHARED, input, offset);
688 if (buf == MAP_FAILED) {
689 perror("failed to mmap file");
690 exit(-1);
691 }
692
693more:
694 event = (event_t *)(buf + head);
695
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200696 size = event->header.size;
697 if (!size)
698 size = 8;
699
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300700 if (head + event->header.size >= page_size * mmap_window) {
701 unsigned long shift = page_size * (head / page_size);
702 int ret;
703
704 ret = munmap(buf, page_size * mmap_window);
705 assert(ret == 0);
706
707 offset += shift;
708 head -= shift;
709 goto remap;
710 }
711
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200712 size = event->header.size;
713 if (!size)
714 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300715
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300716 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
717 char level;
718 int show = 0;
719 struct dso *dso = NULL;
720 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200721 uint64_t ip = event->ip.ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200722 struct map *map = NULL;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300723
Ingo Molnar35029732009-06-03 09:38:58 +0200724 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
725 (void *)(offset + head),
726 (void *)(long)(event->header.size),
727 event->header.misc,
728 event->ip.pid,
729 (void *)(long)ip);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200730
Ingo Molnared966aa2009-06-03 10:39:26 +0200731 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
732
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300733 if (thread == NULL) {
Ingo Molnar55717312009-05-27 22:13:17 +0200734 fprintf(stderr, "problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300735 event->header.type);
Ingo Molnar55717312009-05-27 22:13:17 +0200736 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300737 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300738
739 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
740 show = SHOW_KERNEL;
741 level = 'k';
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200742
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300743 dso = kernel_dso;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200744
Ingo Molnared966aa2009-06-03 10:39:26 +0200745 dprintf(" ...... dso: %s\n", dso->name);
746
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300747 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200748
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300749 show = SHOW_USER;
750 level = '.';
Ingo Molnar16f762a2009-05-27 09:10:38 +0200751
752 map = thread__find_map(thread, ip);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200753 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300754 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200755 ip -= map->start + map->pgoff;
Ingo Molnared966aa2009-06-03 10:39:26 +0200756 } else {
757 /*
758 * If this is outside of all known maps,
759 * and is a negative address, try to look it
760 * up in the kernel dso, as it might be a
761 * vsyscall (which executes in user-mode):
762 */
763 if ((long long)ip < 0)
764 dso = kernel_dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200765 }
Ingo Molnared966aa2009-06-03 10:39:26 +0200766 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200767
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300768 } else {
769 show = SHOW_HV;
770 level = 'H';
Ingo Molnared966aa2009-06-03 10:39:26 +0200771 dprintf(" ...... dso: [hypervisor]\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300772 }
773
774 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200775 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300776
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200777 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
778 fprintf(stderr,
Ingo Molnar55717312009-05-27 22:13:17 +0200779 "problem incrementing symbol count, skipping event\n");
780 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300781 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300782 }
783 total++;
784 } else switch (event->header.type) {
785 case PERF_EVENT_MMAP: {
786 struct thread *thread = threads__findnew(event->mmap.pid);
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300787 struct map *map = map__new(&event->mmap, cwdp, cwdlen);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300788
Ingo Molnar35029732009-06-03 09:38:58 +0200789 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
790 (void *)(offset + head),
791 (void *)(long)(event->header.size),
792 (void *)(long)event->mmap.start,
793 (void *)(long)event->mmap.len,
794 (void *)(long)event->mmap.pgoff,
795 event->mmap.filename);
796
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300797 if (thread == NULL || map == NULL) {
Ingo Molnar0a520c62009-06-02 23:24:45 +0200798 if (verbose)
799 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnar55717312009-05-27 22:13:17 +0200800 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300801 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300802 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200803 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300804 break;
805 }
806 case PERF_EVENT_COMM: {
807 struct thread *thread = threads__findnew(event->comm.pid);
808
Ingo Molnar35029732009-06-03 09:38:58 +0200809 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
810 (void *)(offset + head),
811 (void *)(long)(event->header.size),
812 event->comm.comm, event->comm.pid);
813
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300814 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300815 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnar55717312009-05-27 22:13:17 +0200816 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
817 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300818 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200819 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300820 break;
821 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200822 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200823broken_event:
Ingo Molnar35029732009-06-03 09:38:58 +0200824 dprintf("%p [%p]: skipping unknown header type: %d\n",
825 (void *)(offset + head),
826 (void *)(long)(event->header.size),
827 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200828
Ingo Molnar3e706112009-05-26 18:53:17 +0200829 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200830
831 /*
832 * assume we lost track of the stream, check alignment, and
833 * increment a single u64 in the hope to catch on again 'soon'.
834 */
835
836 if (unlikely(head & 7))
837 head &= ~7ULL;
838
839 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200840 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300841 }
842
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200843 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200844
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300845 if (offset + head < stat.st_size)
846 goto more;
847
848 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300849 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200850
Ingo Molnar35029732009-06-03 09:38:58 +0200851 dprintf(" IP events: %10ld\n", total);
852 dprintf(" mmap events: %10ld\n", total_mmap);
853 dprintf(" comm events: %10ld\n", total_comm);
854 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200855
Ingo Molnar35029732009-06-03 09:38:58 +0200856 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +0200857 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200858
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200859 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +0200860 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200861
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200862 output__resort();
863 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300864
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300865 return rc;
866}
867
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200868static const char * const report_usage[] = {
869 "perf report [<options>] <command>",
870 NULL
871};
872
873static const struct option options[] = {
874 OPT_STRING('i', "input", &input_name, "file",
875 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300876 OPT_BOOLEAN('v', "verbose", &verbose,
877 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200878 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
879 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200880 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +0200881 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
882 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300883 OPT_BOOLEAN('P', "full-paths", &full_paths,
884 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200885 OPT_END()
886};
887
Ingo Molnar5352f352009-06-03 10:07:39 +0200888static void setup_sorting(void)
889{
890 char *tmp, *tok, *str = strdup(sort_order);
891
892 for (tok = strtok_r(str, ", ", &tmp);
893 tok; tok = strtok_r(NULL, ", ", &tmp)) {
894 if (sort_dimension__add(tok) < 0) {
895 error("Unknown --sort key: `%s'", tok);
896 usage_with_options(report_usage, options);
897 }
898 }
899
900 free(str);
901}
902
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200903int cmd_report(int argc, const char **argv, const char *prefix)
904{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300905 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200906
907 page_size = getpagesize();
908
909 parse_options(argc, argv, options, report_usage, 0);
910
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200911 setup_sorting();
912
Ingo Molnara930d2c2009-05-27 09:50:13 +0200913 setup_pager();
914
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200915 return __cmd_report();
916}