blob: 4705679debae1ffb40be481361bff6b17d6e9bb4 [file] [log] [blame]
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001#include "util/util.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +02002#include "builtin.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02003
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -03004#include "util/list.h"
Ingo Molnara930d2c2009-05-27 09:50:13 +02005#include "util/cache.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -03006#include "util/rbtree.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03007#include "util/symbol.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03008
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02009#include "perf.h"
10
11#include "util/parse-options.h"
12#include "util/parse-events.h"
13
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030014#define SHOW_KERNEL 1
15#define SHOW_USER 2
16#define SHOW_HV 4
17
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020018static char const *input_name = "perf.data";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020019static char *vmlinux = NULL;
Peter Zijlstra37f440c2009-05-27 20:20:26 +020020static char *sort_order = "pid,symbol";
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030021static int input;
22static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
23
Ingo Molnar97b07b62009-05-26 18:48:58 +020024static int dump_trace = 0;
Ingo Molnar16f762a2009-05-27 09:10:38 +020025static int verbose;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030026static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020027
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030028static unsigned long page_size;
29static unsigned long mmap_window = 32;
30
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020031const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030032 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
33 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
34 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
35};
36
37struct ip_event {
38 struct perf_event_header header;
39 __u64 ip;
40 __u32 pid, tid;
41};
42struct mmap_event {
43 struct perf_event_header header;
44 __u32 pid, tid;
45 __u64 start;
46 __u64 len;
47 __u64 pgoff;
48 char filename[PATH_MAX];
49};
50struct comm_event {
51 struct perf_event_header header;
52 __u32 pid,tid;
53 char comm[16];
54};
55
56typedef union event_union {
57 struct perf_event_header header;
58 struct ip_event ip;
59 struct mmap_event mmap;
60 struct comm_event comm;
61} event_t;
62
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030063static LIST_HEAD(dsos);
64static struct dso *kernel_dso;
65
66static void dsos__add(struct dso *dso)
67{
68 list_add_tail(&dso->node, &dsos);
69}
70
71static struct dso *dsos__find(const char *name)
72{
73 struct dso *pos;
74
75 list_for_each_entry(pos, &dsos, node)
76 if (strcmp(pos->name, name) == 0)
77 return pos;
78 return NULL;
79}
80
81static struct dso *dsos__findnew(const char *name)
82{
83 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +020084 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030085
86 if (dso == NULL) {
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -030087 dso = dso__new(name, 0);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +020088 if (!dso)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030089 goto out_delete_dso;
90
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -030091 nr = dso__load(dso, NULL);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +020092 if (nr < 0) {
93 fprintf(stderr, "Failed to open: %s\n", name);
94 goto out_delete_dso;
95 }
96 if (!nr) {
97 fprintf(stderr,
98 "Failed to find debug symbols for: %s, maybe install a debug package?\n",
99 name);
100 }
101
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300102 dsos__add(dso);
103 }
104
105 return dso;
106
107out_delete_dso:
108 dso__delete(dso);
109 return NULL;
110}
111
Ingo Molnar16f762a2009-05-27 09:10:38 +0200112static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300113{
114 struct dso *pos;
115
116 list_for_each_entry(pos, &dsos, node)
117 dso__fprintf(pos, fp);
118}
119
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200120static int load_kernel(void)
121{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300122 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200123
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300124 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200125 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300126 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200127
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300128 err = dso__load_kernel(kernel_dso, vmlinux, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300129 if (err) {
130 dso__delete(kernel_dso);
131 kernel_dso = NULL;
132 } else
133 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200134
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300135 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200136}
137
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300138static int strcommon(const char *pathname, const char *cwd, int cwdlen)
139{
140 int n = 0;
141
142 while (pathname[n] == cwd[n] && n < cwdlen)
143 ++n;
144
145 return n;
146}
147
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300148struct map {
149 struct list_head node;
150 uint64_t start;
151 uint64_t end;
152 uint64_t pgoff;
153 struct dso *dso;
154};
155
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300156static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300157{
158 struct map *self = malloc(sizeof(*self));
159
160 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300161 const char *filename = event->filename;
162 char newfilename[PATH_MAX];
163
164 if (cwd) {
165 int n = strcommon(filename, cwd, cwdlen);
166 if (n == cwdlen) {
167 snprintf(newfilename, sizeof(newfilename),
168 ".%s", filename + n);
169 filename = newfilename;
170 }
171 }
172
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300173 self->start = event->start;
174 self->end = event->start + event->len;
175 self->pgoff = event->pgoff;
176
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300177 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300178 if (self->dso == NULL)
179 goto out_delete;
180 }
181 return self;
182out_delete:
183 free(self);
184 return NULL;
185}
186
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300187struct thread;
188
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300189struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300190 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300191 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300192 pid_t pid;
193 char *comm;
194};
195
196static struct thread *thread__new(pid_t pid)
197{
198 struct thread *self = malloc(sizeof(*self));
199
200 if (self != NULL) {
201 self->pid = pid;
202 self->comm = NULL;
203 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300204 }
205
206 return self;
207}
208
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300209static int thread__set_comm(struct thread *self, const char *comm)
210{
211 self->comm = strdup(comm);
212 return self->comm ? 0 : -ENOMEM;
213}
214
Ingo Molnar16f762a2009-05-27 09:10:38 +0200215static struct rb_root threads;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300216
217static struct thread *threads__findnew(pid_t pid)
218{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300219 struct rb_node **p = &threads.rb_node;
220 struct rb_node *parent = NULL;
221 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300222
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300223 while (*p != NULL) {
224 parent = *p;
225 th = rb_entry(parent, struct thread, rb_node);
226
227 if (th->pid == pid)
228 return th;
229
230 if (pid < th->pid)
231 p = &(*p)->rb_left;
232 else
233 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300234 }
235
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300236 th = thread__new(pid);
237 if (th != NULL) {
238 rb_link_node(&th->rb_node, parent, p);
239 rb_insert_color(&th->rb_node, &threads);
240 }
241 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300242}
243
244static void thread__insert_map(struct thread *self, struct map *map)
245{
246 list_add_tail(&map->node, &self->maps);
247}
248
249static struct map *thread__find_map(struct thread *self, uint64_t ip)
250{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200251 struct map *pos;
252
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300253 if (self == NULL)
254 return NULL;
255
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300256 list_for_each_entry(pos, &self->maps, node)
257 if (ip >= pos->start && ip <= pos->end)
258 return pos;
259
260 return NULL;
261}
262
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200263/*
264 * histogram, sorted on item, collects counts
265 */
266
267static struct rb_root hist;
268
269struct hist_entry {
270 struct rb_node rb_node;
271
272 struct thread *thread;
273 struct map *map;
274 struct dso *dso;
275 struct symbol *sym;
276 uint64_t ip;
277 char level;
278
279 uint32_t count;
280};
281
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200282/*
283 * configurable sorting bits
284 */
285
286struct sort_entry {
287 struct list_head list;
288
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200289 char *header;
290
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200291 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
292 size_t (*print)(FILE *fp, struct hist_entry *);
293};
294
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200295static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200296sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
297{
298 return right->thread->pid - left->thread->pid;
299}
300
301static size_t
302sort__thread_print(FILE *fp, struct hist_entry *self)
303{
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200304 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200305}
306
307static struct sort_entry sort_thread = {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200308 .header = " Command: Pid ",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200309 .cmp = sort__thread_cmp,
310 .print = sort__thread_print,
311};
312
313static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200314sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
315{
316 char *comm_l = left->thread->comm;
317 char *comm_r = right->thread->comm;
318
319 if (!comm_l || !comm_r) {
320 if (!comm_l && !comm_r)
321 return 0;
322 else if (!comm_l)
323 return -1;
324 else
325 return 1;
326 }
327
328 return strcmp(comm_l, comm_r);
329}
330
331static size_t
332sort__comm_print(FILE *fp, struct hist_entry *self)
333{
Ingo Molnar2d655372009-05-27 21:36:22 +0200334 return fprintf(fp, " %16s", self->thread->comm ?: "<unknown>");
Peter Zijlstra992444b2009-05-27 20:20:27 +0200335}
336
337static struct sort_entry sort_comm = {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200338 .header = " Command",
Peter Zijlstra992444b2009-05-27 20:20:27 +0200339 .cmp = sort__comm_cmp,
340 .print = sort__comm_print,
341};
342
343static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200344sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
345{
346 struct dso *dso_l = left->dso;
347 struct dso *dso_r = right->dso;
348
349 if (!dso_l || !dso_r) {
350 if (!dso_l && !dso_r)
351 return 0;
352 else if (!dso_l)
353 return -1;
354 else
355 return 1;
356 }
357
358 return strcmp(dso_l->name, dso_r->name);
359}
360
361static size_t
362sort__dso_print(FILE *fp, struct hist_entry *self)
363{
Ingo Molnar2d655372009-05-27 21:36:22 +0200364 return fprintf(fp, " %64s", self->dso ? self->dso->name : "<unknown>");
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200365}
366
367static struct sort_entry sort_dso = {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200368 .header = " Shared Object",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200369 .cmp = sort__dso_cmp,
370 .print = sort__dso_print,
371};
372
373static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200374sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300375{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200376 uint64_t ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200377
378 if (left->sym == right->sym)
379 return 0;
380
381 ip_l = left->sym ? left->sym->start : left->ip;
382 ip_r = right->sym ? right->sym->start : right->ip;
383
384 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300385}
386
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200387static size_t
388sort__sym_print(FILE *fp, struct hist_entry *self)
389{
390 size_t ret = 0;
391
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200392 if (verbose)
Ingo Molnar2d655372009-05-27 21:36:22 +0200393 ret += fprintf(fp, " %#018llx", (unsigned long long)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200394
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200395 ret += fprintf(fp, " %s: %s",
396 self->dso ? self->dso->name : "<unknown>",
397 self->sym ? self->sym->name : "<unknown>");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200398
399 return ret;
400}
401
402static struct sort_entry sort_sym = {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200403 .header = "Shared Object: Symbol",
404 .cmp = sort__sym_cmp,
405 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200406};
407
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200408struct sort_dimension {
409 char *name;
410 struct sort_entry *entry;
411 int taken;
412};
413
414static struct sort_dimension sort_dimensions[] = {
415 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200416 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200417 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200418 { .name = "symbol", .entry = &sort_sym, },
419};
420
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200421static LIST_HEAD(hist_entry__sort_list);
422
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200423static int sort_dimension__add(char *tok)
424{
425 int i;
426
427 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
428 struct sort_dimension *sd = &sort_dimensions[i];
429
430 if (sd->taken)
431 continue;
432
433 if (strcmp(tok, sd->name))
434 continue;
435
436 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
437 sd->taken = 1;
438 return 0;
439 }
440
441 return -ESRCH;
442}
443
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200444static void setup_sorting(void)
445{
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200446 char *tmp, *tok, *str = strdup(sort_order);
447
448 for (tok = strtok_r(str, ", ", &tmp);
449 tok; tok = strtok_r(NULL, ", ", &tmp))
450 sort_dimension__add(tok);
451
452 free(str);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200453}
454
455static int64_t
456hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
457{
458 struct sort_entry *se;
459 int64_t cmp = 0;
460
461 list_for_each_entry(se, &hist_entry__sort_list, list) {
462 cmp = se->cmp(left, right);
463 if (cmp)
464 break;
465 }
466
467 return cmp;
468}
469
470static size_t
471hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
472{
473 struct sort_entry *se;
474 size_t ret;
475
476 if (total_samples) {
Ingo Molnar2d655372009-05-27 21:36:22 +0200477 ret = fprintf(fp, " %5.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200478 (self->count * 100.0) / total_samples);
479 } else
480 ret = fprintf(fp, "%12d ", self->count);
481
482 list_for_each_entry(se, &hist_entry__sort_list, list)
483 ret += se->print(fp, self);
484
485 ret += fprintf(fp, "\n");
486
487 return ret;
488}
489
490/*
491 * collect histogram counts
492 */
493
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200494static int
495hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
496 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300497{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200498 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300499 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200500 struct hist_entry *he;
501 struct hist_entry entry = {
502 .thread = thread,
503 .map = map,
504 .dso = dso,
505 .sym = sym,
506 .ip = ip,
507 .level = level,
508 .count = 1,
509 };
510 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300511
512 while (*p != NULL) {
513 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200514 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300515
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200516 cmp = hist_entry__cmp(&entry, he);
517
518 if (!cmp) {
519 he->count++;
520 return 0;
521 }
522
523 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300524 p = &(*p)->rb_left;
525 else
526 p = &(*p)->rb_right;
527 }
528
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200529 he = malloc(sizeof(*he));
530 if (!he)
531 return -ENOMEM;
532 *he = entry;
533 rb_link_node(&he->rb_node, parent, p);
534 rb_insert_color(&he->rb_node, &hist);
535
536 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300537}
538
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200539/*
540 * reverse the map, sort on count.
541 */
542
543static struct rb_root output_hists;
544
545static void output__insert_entry(struct hist_entry *he)
546{
547 struct rb_node **p = &output_hists.rb_node;
548 struct rb_node *parent = NULL;
549 struct hist_entry *iter;
550
551 while (*p != NULL) {
552 parent = *p;
553 iter = rb_entry(parent, struct hist_entry, rb_node);
554
555 if (he->count > iter->count)
556 p = &(*p)->rb_left;
557 else
558 p = &(*p)->rb_right;
559 }
560
561 rb_link_node(&he->rb_node, parent, p);
562 rb_insert_color(&he->rb_node, &output_hists);
563}
564
565static void output__resort(void)
566{
567 struct rb_node *next = rb_first(&hist);
568 struct hist_entry *n;
569
570 while (next) {
571 n = rb_entry(next, struct hist_entry, rb_node);
572 next = rb_next(&n->rb_node);
573
574 rb_erase(&n->rb_node, &hist);
575 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300576 }
577}
578
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200579static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300580{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200581 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200582 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300583 struct rb_node *nd;
584 size_t ret = 0;
585
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200586 fprintf(fp, "#\n");
587
588 fprintf(fp, "# Overhead");
589 list_for_each_entry(se, &hist_entry__sort_list, list)
590 fprintf(fp, " %s", se->header);
591 fprintf(fp, "\n");
592
593 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200594 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200595 int i;
596
597 fprintf(fp, " ");
598 for (i = 0; i < strlen(se->header); i++)
599 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200600 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200601 fprintf(fp, "\n");
602
603 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200604
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200605 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
606 pos = rb_entry(nd, struct hist_entry, rb_node);
607 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300608 }
609
610 return ret;
611}
612
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200613
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200614static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300615{
616 unsigned long offset = 0;
617 unsigned long head = 0;
618 struct stat stat;
619 char *buf;
620 event_t *event;
621 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200622 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200623 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300624 char cwd[PATH_MAX], *cwdp = cwd;
625 int cwdlen;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300626
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300627 input = open(input_name, O_RDONLY);
628 if (input < 0) {
629 perror("failed to open file");
630 exit(-1);
631 }
632
633 ret = fstat(input, &stat);
634 if (ret < 0) {
635 perror("failed to stat file");
636 exit(-1);
637 }
638
639 if (!stat.st_size) {
640 fprintf(stderr, "zero-sized file, nothing to do!\n");
641 exit(0);
642 }
643
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200644 if (load_kernel() < 0) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300645 perror("failed to load kernel symbols");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300646 return EXIT_FAILURE;
647 }
648
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300649 if (!full_paths) {
650 if (getcwd(cwd, sizeof(cwd)) == NULL) {
651 perror("failed to get the current directory");
652 return EXIT_FAILURE;
653 }
654 cwdlen = strlen(cwd);
655 } else
656 cwdp = NULL;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300657remap:
658 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
659 MAP_SHARED, input, offset);
660 if (buf == MAP_FAILED) {
661 perror("failed to mmap file");
662 exit(-1);
663 }
664
665more:
666 event = (event_t *)(buf + head);
667
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200668 size = event->header.size;
669 if (!size)
670 size = 8;
671
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300672 if (head + event->header.size >= page_size * mmap_window) {
673 unsigned long shift = page_size * (head / page_size);
674 int ret;
675
676 ret = munmap(buf, page_size * mmap_window);
677 assert(ret == 0);
678
679 offset += shift;
680 head -= shift;
681 goto remap;
682 }
683
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200684 size = event->header.size;
685 if (!size)
686 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300687
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300688 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
689 char level;
690 int show = 0;
691 struct dso *dso = NULL;
692 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200693 uint64_t ip = event->ip.ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200694 struct map *map = NULL;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300695
Ingo Molnar97b07b62009-05-26 18:48:58 +0200696 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200697 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
698 (void *)(offset + head),
699 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200700 event->header.misc,
701 event->ip.pid,
Ingo Molnar16f762a2009-05-27 09:10:38 +0200702 (void *)(long)ip);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200703 }
704
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300705 if (thread == NULL) {
Ingo Molnar55717312009-05-27 22:13:17 +0200706 fprintf(stderr, "problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300707 event->header.type);
Ingo Molnar55717312009-05-27 22:13:17 +0200708 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300709 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300710
711 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
712 show = SHOW_KERNEL;
713 level = 'k';
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200714
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300715 dso = kernel_dso;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200716
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300717 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200718
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300719 show = SHOW_USER;
720 level = '.';
Ingo Molnar16f762a2009-05-27 09:10:38 +0200721
722 map = thread__find_map(thread, ip);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200723 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300724 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200725 ip -= map->start + map->pgoff;
726 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200727
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300728 } else {
729 show = SHOW_HV;
730 level = 'H';
731 }
732
733 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200734 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300735
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200736 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
737 fprintf(stderr,
Ingo Molnar55717312009-05-27 22:13:17 +0200738 "problem incrementing symbol count, skipping event\n");
739 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300740 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300741 }
742 total++;
743 } else switch (event->header.type) {
744 case PERF_EVENT_MMAP: {
745 struct thread *thread = threads__findnew(event->mmap.pid);
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300746 struct map *map = map__new(&event->mmap, cwdp, cwdlen);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300747
Ingo Molnar97b07b62009-05-26 18:48:58 +0200748 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200749 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
750 (void *)(offset + head),
751 (void *)(long)(event->header.size),
Ingo Molnar16f762a2009-05-27 09:10:38 +0200752 (void *)(long)event->mmap.start,
753 (void *)(long)event->mmap.len,
754 (void *)(long)event->mmap.pgoff,
Ingo Molnar97b07b62009-05-26 18:48:58 +0200755 event->mmap.filename);
756 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300757 if (thread == NULL || map == NULL) {
Ingo Molnar55717312009-05-27 22:13:17 +0200758 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
759 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300760 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300761 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200762 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300763 break;
764 }
765 case PERF_EVENT_COMM: {
766 struct thread *thread = threads__findnew(event->comm.pid);
767
Ingo Molnar97b07b62009-05-26 18:48:58 +0200768 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200769 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
770 (void *)(offset + head),
771 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200772 event->comm.comm, event->comm.pid);
773 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300774 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300775 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnar55717312009-05-27 22:13:17 +0200776 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
777 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300778 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200779 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300780 break;
781 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200782 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200783broken_event:
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200784 if (dump_trace)
785 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
786 (void *)(offset + head),
787 (void *)(long)(event->header.size),
788 event->header.type);
789
Ingo Molnar3e706112009-05-26 18:53:17 +0200790 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200791
792 /*
793 * assume we lost track of the stream, check alignment, and
794 * increment a single u64 in the hope to catch on again 'soon'.
795 */
796
797 if (unlikely(head & 7))
798 head &= ~7ULL;
799
800 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200801 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300802 }
803
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200804 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200805
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300806 if (offset + head < stat.st_size)
807 goto more;
808
809 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300810 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200811
812 if (dump_trace) {
Ingo Molnar3e706112009-05-26 18:53:17 +0200813 fprintf(stderr, " IP events: %10ld\n", total);
814 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
815 fprintf(stderr, " comm events: %10ld\n", total_comm);
816 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200817
818 return 0;
819 }
820
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200821 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +0200822 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200823
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200824 output__resort();
825 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300826
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300827 return rc;
828}
829
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200830static const char * const report_usage[] = {
831 "perf report [<options>] <command>",
832 NULL
833};
834
835static const struct option options[] = {
836 OPT_STRING('i', "input", &input_name, "file",
837 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300838 OPT_BOOLEAN('v', "verbose", &verbose,
839 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200840 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
841 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200842 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +0200843 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
844 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300845 OPT_BOOLEAN('P', "full-paths", &full_paths,
846 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200847 OPT_END()
848};
849
850int cmd_report(int argc, const char **argv, const char *prefix)
851{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300852 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200853
854 page_size = getpagesize();
855
856 parse_options(argc, argv, options, report_usage, 0);
857
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200858 setup_sorting();
859
Ingo Molnara930d2c2009-05-27 09:50:13 +0200860 setup_pager();
861
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200862 return __cmd_report();
863}