blob: 19c1e056bb665b165947d91c8851d0983bec07c0 [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 Meloa0055ae2009-06-01 17:50:19 -03008#include "util/string.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03009
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020010#include "perf.h"
11
12#include "util/parse-options.h"
13#include "util/parse-events.h"
14
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030015#define SHOW_KERNEL 1
16#define SHOW_USER 2
17#define SHOW_HV 4
18
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020019static char const *input_name = "perf.data";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020020static char *vmlinux = NULL;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +020021static char *sort_order = "comm,dso";
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030022static int input;
23static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
24
Ingo Molnar97b07b62009-05-26 18:48:58 +020025static int dump_trace = 0;
Ingo Molnar16f762a2009-05-27 09:10:38 +020026static int verbose;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030027static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020028
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030029static unsigned long page_size;
30static unsigned long mmap_window = 32;
31
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020032const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030033 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
34 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
35 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
36};
37
38struct ip_event {
39 struct perf_event_header header;
40 __u64 ip;
41 __u32 pid, tid;
42};
43struct mmap_event {
44 struct perf_event_header header;
45 __u32 pid, tid;
46 __u64 start;
47 __u64 len;
48 __u64 pgoff;
49 char filename[PATH_MAX];
50};
51struct comm_event {
52 struct perf_event_header header;
53 __u32 pid,tid;
54 char comm[16];
55};
56
57typedef union event_union {
58 struct perf_event_header header;
59 struct ip_event ip;
60 struct mmap_event mmap;
61 struct comm_event comm;
62} event_t;
63
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030064static LIST_HEAD(dsos);
65static struct dso *kernel_dso;
66
67static void dsos__add(struct dso *dso)
68{
69 list_add_tail(&dso->node, &dsos);
70}
71
72static struct dso *dsos__find(const char *name)
73{
74 struct dso *pos;
75
76 list_for_each_entry(pos, &dsos, node)
77 if (strcmp(pos->name, name) == 0)
78 return pos;
79 return NULL;
80}
81
82static struct dso *dsos__findnew(const char *name)
83{
84 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +020085 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030086
Ingo Molnar4593bba2009-06-02 15:34:25 +020087 if (dso)
88 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030089
Ingo Molnar4593bba2009-06-02 15:34:25 +020090 dso = dso__new(name, 0);
91 if (!dso)
92 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +020093
Ingo Molnar4593bba2009-06-02 15:34:25 +020094 nr = dso__load(dso, NULL);
95 if (nr < 0) {
96 fprintf(stderr, "Failed to open: %s\n", name);
97 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030098 }
Ingo Molnar4593bba2009-06-02 15:34:25 +020099 if (!nr && verbose) {
100 fprintf(stderr,
101 "No symbols found in: %s, maybe install a debug package?\n",
102 name);
103 }
104
105 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300106
107 return dso;
108
109out_delete_dso:
110 dso__delete(dso);
111 return NULL;
112}
113
Ingo Molnar16f762a2009-05-27 09:10:38 +0200114static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300115{
116 struct dso *pos;
117
118 list_for_each_entry(pos, &dsos, node)
119 dso__fprintf(pos, fp);
120}
121
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200122static int load_kernel(void)
123{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300124 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200125
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300126 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200127 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300128 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200129
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300130 err = dso__load_kernel(kernel_dso, vmlinux, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300131 if (err) {
132 dso__delete(kernel_dso);
133 kernel_dso = NULL;
134 } else
135 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200136
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300137 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200138}
139
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300140static int strcommon(const char *pathname, const char *cwd, int cwdlen)
141{
142 int n = 0;
143
144 while (pathname[n] == cwd[n] && n < cwdlen)
145 ++n;
146
147 return n;
148}
149
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300150struct map {
151 struct list_head node;
152 uint64_t start;
153 uint64_t end;
154 uint64_t pgoff;
155 struct dso *dso;
156};
157
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300158static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300159{
160 struct map *self = malloc(sizeof(*self));
161
162 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300163 const char *filename = event->filename;
164 char newfilename[PATH_MAX];
165
166 if (cwd) {
167 int n = strcommon(filename, cwd, cwdlen);
168 if (n == cwdlen) {
169 snprintf(newfilename, sizeof(newfilename),
170 ".%s", filename + n);
171 filename = newfilename;
172 }
173 }
174
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300175 self->start = event->start;
176 self->end = event->start + event->len;
177 self->pgoff = event->pgoff;
178
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300179 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300180 if (self->dso == NULL)
181 goto out_delete;
182 }
183 return self;
184out_delete:
185 free(self);
186 return NULL;
187}
188
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300189struct thread;
190
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300191struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300192 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300193 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300194 pid_t pid;
195 char *comm;
196};
197
198static struct thread *thread__new(pid_t pid)
199{
200 struct thread *self = malloc(sizeof(*self));
201
202 if (self != NULL) {
203 self->pid = pid;
204 self->comm = NULL;
205 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300206 }
207
208 return self;
209}
210
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300211static int thread__set_comm(struct thread *self, const char *comm)
212{
213 self->comm = strdup(comm);
214 return self->comm ? 0 : -ENOMEM;
215}
216
Ingo Molnar16f762a2009-05-27 09:10:38 +0200217static struct rb_root threads;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300218
219static struct thread *threads__findnew(pid_t pid)
220{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300221 struct rb_node **p = &threads.rb_node;
222 struct rb_node *parent = NULL;
223 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300224
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300225 while (*p != NULL) {
226 parent = *p;
227 th = rb_entry(parent, struct thread, rb_node);
228
229 if (th->pid == pid)
230 return th;
231
232 if (pid < th->pid)
233 p = &(*p)->rb_left;
234 else
235 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300236 }
237
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300238 th = thread__new(pid);
239 if (th != NULL) {
240 rb_link_node(&th->rb_node, parent, p);
241 rb_insert_color(&th->rb_node, &threads);
242 }
243 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300244}
245
246static void thread__insert_map(struct thread *self, struct map *map)
247{
248 list_add_tail(&map->node, &self->maps);
249}
250
251static struct map *thread__find_map(struct thread *self, uint64_t ip)
252{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200253 struct map *pos;
254
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300255 if (self == NULL)
256 return NULL;
257
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300258 list_for_each_entry(pos, &self->maps, node)
259 if (ip >= pos->start && ip <= pos->end)
260 return pos;
261
262 return NULL;
263}
264
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200265/*
266 * histogram, sorted on item, collects counts
267 */
268
269static struct rb_root hist;
270
271struct hist_entry {
272 struct rb_node rb_node;
273
274 struct thread *thread;
275 struct map *map;
276 struct dso *dso;
277 struct symbol *sym;
278 uint64_t ip;
279 char level;
280
281 uint32_t count;
282};
283
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200284/*
285 * configurable sorting bits
286 */
287
288struct sort_entry {
289 struct list_head list;
290
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200291 char *header;
292
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200293 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
294 size_t (*print)(FILE *fp, struct hist_entry *);
295};
296
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200297static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200298sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
299{
300 return right->thread->pid - left->thread->pid;
301}
302
303static size_t
304sort__thread_print(FILE *fp, struct hist_entry *self)
305{
Ingo Molnar4593bba2009-06-02 15:34:25 +0200306 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200307}
308
309static struct sort_entry sort_thread = {
Ingo Molnar4593bba2009-06-02 15:34:25 +0200310 .header = " Command: Pid ",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200311 .cmp = sort__thread_cmp,
312 .print = sort__thread_print,
313};
314
315static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200316sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
317{
318 char *comm_l = left->thread->comm;
319 char *comm_r = right->thread->comm;
320
321 if (!comm_l || !comm_r) {
322 if (!comm_l && !comm_r)
323 return 0;
324 else if (!comm_l)
325 return -1;
326 else
327 return 1;
328 }
329
330 return strcmp(comm_l, comm_r);
331}
332
333static size_t
334sort__comm_print(FILE *fp, struct hist_entry *self)
335{
Ingo Molnar4593bba2009-06-02 15:34:25 +0200336 return fprintf(fp, " %16s", self->thread->comm ?: "<unknown>");
Peter Zijlstra992444b2009-05-27 20:20:27 +0200337}
338
339static struct sort_entry sort_comm = {
Ingo Molnar4593bba2009-06-02 15:34:25 +0200340 .header = " Command",
Peter Zijlstra992444b2009-05-27 20:20:27 +0200341 .cmp = sort__comm_cmp,
342 .print = sort__comm_print,
343};
344
345static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200346sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
347{
348 struct dso *dso_l = left->dso;
349 struct dso *dso_r = right->dso;
350
351 if (!dso_l || !dso_r) {
352 if (!dso_l && !dso_r)
353 return 0;
354 else if (!dso_l)
355 return -1;
356 else
357 return 1;
358 }
359
360 return strcmp(dso_l->name, dso_r->name);
361}
362
363static size_t
364sort__dso_print(FILE *fp, struct hist_entry *self)
365{
Ingo Molnar4593bba2009-06-02 15:34:25 +0200366 return fprintf(fp, " %s", self->dso ? self->dso->name : "<unknown>");
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200367}
368
369static struct sort_entry sort_dso = {
Ingo Molnar4593bba2009-06-02 15:34:25 +0200370 .header = " Shared Object",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200371 .cmp = sort__dso_cmp,
372 .print = sort__dso_print,
373};
374
375static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200376sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300377{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200378 uint64_t ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200379
380 if (left->sym == right->sym)
381 return 0;
382
383 ip_l = left->sym ? left->sym->start : left->ip;
384 ip_r = right->sym ? right->sym->start : right->ip;
385
386 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300387}
388
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200389static size_t
390sort__sym_print(FILE *fp, struct hist_entry *self)
391{
392 size_t ret = 0;
393
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200394 if (verbose)
Ingo Molnar4593bba2009-06-02 15:34:25 +0200395 ret += fprintf(fp, " %#018llx", (unsigned long long)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200396
Ingo Molnar4593bba2009-06-02 15:34:25 +0200397 ret += fprintf(fp, " %s: %s",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200398 self->dso ? self->dso->name : "<unknown>",
399 self->sym ? self->sym->name : "<unknown>");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200400
401 return ret;
402}
403
404static struct sort_entry sort_sym = {
Ingo Molnar4593bba2009-06-02 15:34:25 +0200405 .header = " Shared Object: Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200406 .cmp = sort__sym_cmp,
407 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200408};
409
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200410struct sort_dimension {
411 char *name;
412 struct sort_entry *entry;
413 int taken;
414};
415
416static struct sort_dimension sort_dimensions[] = {
417 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200418 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200419 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200420 { .name = "symbol", .entry = &sort_sym, },
421};
422
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200423static LIST_HEAD(hist_entry__sort_list);
424
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200425static int sort_dimension__add(char *tok)
426{
427 int i;
428
429 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
430 struct sort_dimension *sd = &sort_dimensions[i];
431
432 if (sd->taken)
433 continue;
434
435 if (strcmp(tok, sd->name))
436 continue;
437
438 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
439 sd->taken = 1;
440 return 0;
441 }
442
443 return -ESRCH;
444}
445
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200446static void setup_sorting(void)
447{
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200448 char *tmp, *tok, *str = strdup(sort_order);
449
450 for (tok = strtok_r(str, ", ", &tmp);
451 tok; tok = strtok_r(NULL, ", ", &tmp))
452 sort_dimension__add(tok);
453
454 free(str);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200455}
456
457static int64_t
458hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
459{
460 struct sort_entry *se;
461 int64_t cmp = 0;
462
463 list_for_each_entry(se, &hist_entry__sort_list, list) {
464 cmp = se->cmp(left, right);
465 if (cmp)
466 break;
467 }
468
469 return cmp;
470}
471
472static size_t
473hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
474{
475 struct sort_entry *se;
476 size_t ret;
477
478 if (total_samples) {
Ingo Molnar2d655372009-05-27 21:36:22 +0200479 ret = fprintf(fp, " %5.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200480 (self->count * 100.0) / total_samples);
481 } else
482 ret = fprintf(fp, "%12d ", self->count);
483
484 list_for_each_entry(se, &hist_entry__sort_list, list)
485 ret += se->print(fp, self);
486
487 ret += fprintf(fp, "\n");
488
489 return ret;
490}
491
492/*
493 * collect histogram counts
494 */
495
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200496static int
497hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
498 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300499{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200500 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300501 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200502 struct hist_entry *he;
503 struct hist_entry entry = {
504 .thread = thread,
505 .map = map,
506 .dso = dso,
507 .sym = sym,
508 .ip = ip,
509 .level = level,
510 .count = 1,
511 };
512 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300513
514 while (*p != NULL) {
515 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200516 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300517
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200518 cmp = hist_entry__cmp(&entry, he);
519
520 if (!cmp) {
521 he->count++;
522 return 0;
523 }
524
525 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300526 p = &(*p)->rb_left;
527 else
528 p = &(*p)->rb_right;
529 }
530
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200531 he = malloc(sizeof(*he));
532 if (!he)
533 return -ENOMEM;
534 *he = entry;
535 rb_link_node(&he->rb_node, parent, p);
536 rb_insert_color(&he->rb_node, &hist);
537
538 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300539}
540
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200541/*
542 * reverse the map, sort on count.
543 */
544
545static struct rb_root output_hists;
546
547static void output__insert_entry(struct hist_entry *he)
548{
549 struct rb_node **p = &output_hists.rb_node;
550 struct rb_node *parent = NULL;
551 struct hist_entry *iter;
552
553 while (*p != NULL) {
554 parent = *p;
555 iter = rb_entry(parent, struct hist_entry, rb_node);
556
557 if (he->count > iter->count)
558 p = &(*p)->rb_left;
559 else
560 p = &(*p)->rb_right;
561 }
562
563 rb_link_node(&he->rb_node, parent, p);
564 rb_insert_color(&he->rb_node, &output_hists);
565}
566
567static void output__resort(void)
568{
569 struct rb_node *next = rb_first(&hist);
570 struct hist_entry *n;
571
572 while (next) {
573 n = rb_entry(next, struct hist_entry, rb_node);
574 next = rb_next(&n->rb_node);
575
576 rb_erase(&n->rb_node, &hist);
577 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300578 }
579}
580
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200581static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300582{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200583 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200584 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300585 struct rb_node *nd;
586 size_t ret = 0;
587
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200588 fprintf(fp, "#\n");
589
590 fprintf(fp, "# Overhead");
591 list_for_each_entry(se, &hist_entry__sort_list, list)
592 fprintf(fp, " %s", se->header);
593 fprintf(fp, "\n");
594
595 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200596 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200597 int i;
598
Ingo Molnar4593bba2009-06-02 15:34:25 +0200599 fprintf(fp, " ");
600 for (i = 0; i < strlen(se->header)-1; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200601 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200602 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200603 fprintf(fp, "\n");
604
605 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200606
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200607 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
608 pos = rb_entry(nd, struct hist_entry, rb_node);
609 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300610 }
611
612 return ret;
613}
614
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200615
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200616static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300617{
618 unsigned long offset = 0;
619 unsigned long head = 0;
620 struct stat stat;
621 char *buf;
622 event_t *event;
623 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200624 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200625 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300626 char cwd[PATH_MAX], *cwdp = cwd;
627 int cwdlen;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300628
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300629 input = open(input_name, O_RDONLY);
630 if (input < 0) {
631 perror("failed to open file");
632 exit(-1);
633 }
634
635 ret = fstat(input, &stat);
636 if (ret < 0) {
637 perror("failed to stat file");
638 exit(-1);
639 }
640
641 if (!stat.st_size) {
642 fprintf(stderr, "zero-sized file, nothing to do!\n");
643 exit(0);
644 }
645
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200646 if (load_kernel() < 0) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300647 perror("failed to load kernel symbols");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300648 return EXIT_FAILURE;
649 }
650
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300651 if (!full_paths) {
652 if (getcwd(cwd, sizeof(cwd)) == NULL) {
653 perror("failed to get the current directory");
654 return EXIT_FAILURE;
655 }
656 cwdlen = strlen(cwd);
Mike Galbraith10a28252009-06-02 11:04:44 +0200657 } else {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300658 cwdp = NULL;
Mike Galbraith10a28252009-06-02 11:04:44 +0200659 cwdlen = 0;
660 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300661remap:
662 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
663 MAP_SHARED, input, offset);
664 if (buf == MAP_FAILED) {
665 perror("failed to mmap file");
666 exit(-1);
667 }
668
669more:
670 event = (event_t *)(buf + head);
671
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200672 size = event->header.size;
673 if (!size)
674 size = 8;
675
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300676 if (head + event->header.size >= page_size * mmap_window) {
677 unsigned long shift = page_size * (head / page_size);
678 int ret;
679
680 ret = munmap(buf, page_size * mmap_window);
681 assert(ret == 0);
682
683 offset += shift;
684 head -= shift;
685 goto remap;
686 }
687
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200688 size = event->header.size;
689 if (!size)
690 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300691
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300692 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
693 char level;
694 int show = 0;
695 struct dso *dso = NULL;
696 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200697 uint64_t ip = event->ip.ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200698 struct map *map = NULL;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300699
Ingo Molnar97b07b62009-05-26 18:48:58 +0200700 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200701 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
702 (void *)(offset + head),
703 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200704 event->header.misc,
705 event->ip.pid,
Ingo Molnar16f762a2009-05-27 09:10:38 +0200706 (void *)(long)ip);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200707 }
708
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300709 if (thread == NULL) {
Ingo Molnar55717312009-05-27 22:13:17 +0200710 fprintf(stderr, "problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300711 event->header.type);
Ingo Molnar55717312009-05-27 22:13:17 +0200712 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300713 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300714
715 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
716 show = SHOW_KERNEL;
717 level = 'k';
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200718
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300719 dso = kernel_dso;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200720
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300721 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200722
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300723 show = SHOW_USER;
724 level = '.';
Ingo Molnar16f762a2009-05-27 09:10:38 +0200725
726 map = thread__find_map(thread, ip);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200727 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300728 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200729 ip -= map->start + map->pgoff;
730 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200731
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300732 } else {
733 show = SHOW_HV;
734 level = 'H';
735 }
736
737 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200738 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300739
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200740 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
741 fprintf(stderr,
Ingo Molnar55717312009-05-27 22:13:17 +0200742 "problem incrementing symbol count, skipping event\n");
743 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300744 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300745 }
746 total++;
747 } else switch (event->header.type) {
748 case PERF_EVENT_MMAP: {
749 struct thread *thread = threads__findnew(event->mmap.pid);
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300750 struct map *map = map__new(&event->mmap, cwdp, cwdlen);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300751
Ingo Molnar97b07b62009-05-26 18:48:58 +0200752 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200753 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
754 (void *)(offset + head),
755 (void *)(long)(event->header.size),
Ingo Molnar16f762a2009-05-27 09:10:38 +0200756 (void *)(long)event->mmap.start,
757 (void *)(long)event->mmap.len,
758 (void *)(long)event->mmap.pgoff,
Ingo Molnar97b07b62009-05-26 18:48:58 +0200759 event->mmap.filename);
760 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300761 if (thread == NULL || map == NULL) {
Ingo Molnar55717312009-05-27 22:13:17 +0200762 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
763 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300764 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300765 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200766 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300767 break;
768 }
769 case PERF_EVENT_COMM: {
770 struct thread *thread = threads__findnew(event->comm.pid);
771
Ingo Molnar97b07b62009-05-26 18:48:58 +0200772 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200773 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
774 (void *)(offset + head),
775 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200776 event->comm.comm, event->comm.pid);
777 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300778 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300779 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnar55717312009-05-27 22:13:17 +0200780 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
781 goto broken_event;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300782 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200783 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300784 break;
785 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200786 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200787broken_event:
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200788 if (dump_trace)
789 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
790 (void *)(offset + head),
791 (void *)(long)(event->header.size),
792 event->header.type);
793
Ingo Molnar3e706112009-05-26 18:53:17 +0200794 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200795
796 /*
797 * assume we lost track of the stream, check alignment, and
798 * increment a single u64 in the hope to catch on again 'soon'.
799 */
800
801 if (unlikely(head & 7))
802 head &= ~7ULL;
803
804 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200805 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300806 }
807
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200808 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200809
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300810 if (offset + head < stat.st_size)
811 goto more;
812
813 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300814 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200815
816 if (dump_trace) {
Ingo Molnar3e706112009-05-26 18:53:17 +0200817 fprintf(stderr, " IP events: %10ld\n", total);
818 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
819 fprintf(stderr, " comm events: %10ld\n", total_comm);
820 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200821
822 return 0;
823 }
824
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200825 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +0200826 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200827
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200828 output__resort();
829 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300830
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300831 return rc;
832}
833
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200834static const char * const report_usage[] = {
835 "perf report [<options>] <command>",
836 NULL
837};
838
839static const struct option options[] = {
840 OPT_STRING('i', "input", &input_name, "file",
841 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300842 OPT_BOOLEAN('v', "verbose", &verbose,
843 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200844 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
845 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200846 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +0200847 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
848 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300849 OPT_BOOLEAN('P', "full-paths", &full_paths,
850 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200851 OPT_END()
852};
853
854int cmd_report(int argc, const char **argv, const char *prefix)
855{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300856 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200857
858 page_size = getpagesize();
859
860 parse_options(argc, argv, options, report_usage, 0);
861
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200862 setup_sorting();
863
Ingo Molnara930d2c2009-05-27 09:50:13 +0200864 setup_pager();
865
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200866 return __cmd_report();
867}