blob: f053a7463dcf0cf6d99081bb98a07c9acd77217d [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
Ingo Molnar8fc03212009-06-04 15:19:47 +020012#include "util/color.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030013#include "util/list.h"
Ingo Molnara930d2c2009-05-27 09:50:13 +020014#include "util/cache.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030015#include "util/rbtree.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030016#include "util/symbol.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030017#include "util/string.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030018
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020019#include "perf.h"
20
21#include "util/parse-options.h"
22#include "util/parse-events.h"
23
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030024#define SHOW_KERNEL 1
25#define SHOW_USER 2
26#define SHOW_HV 4
27
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020028static char const *input_name = "perf.data";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020029static char *vmlinux = NULL;
Ingo Molnarbd741372009-06-04 14:13:04 +020030
31static char default_sort_order[] = "comm,dso";
32static char *sort_order = default_sort_order;
33
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030034static int input;
35static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
36
Ingo Molnar97b07b62009-05-26 18:48:58 +020037static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020038#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
39
Ingo Molnar16f762a2009-05-27 09:10:38 +020040static int verbose;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030041static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020042
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030043static unsigned long page_size;
44static unsigned long mmap_window = 32;
45
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030046struct ip_event {
47 struct perf_event_header header;
48 __u64 ip;
49 __u32 pid, tid;
50};
Ingo Molnar75051722009-06-03 23:14:49 +020051
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030052struct mmap_event {
53 struct perf_event_header header;
54 __u32 pid, tid;
55 __u64 start;
56 __u64 len;
57 __u64 pgoff;
58 char filename[PATH_MAX];
59};
Ingo Molnar75051722009-06-03 23:14:49 +020060
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030061struct comm_event {
62 struct perf_event_header header;
Ingo Molnar75051722009-06-03 23:14:49 +020063 __u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030064 char comm[16];
65};
66
Peter Zijlstra62fc4452009-06-04 16:53:49 +020067struct fork_event {
68 struct perf_event_header header;
69 __u32 pid, ppid;
70};
71
Ingo Molnarb2fef072009-06-05 18:07:51 +020072struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030073 struct perf_event_header header;
Ingo Molnarb2fef072009-06-05 18:07:51 +020074 __u64 time;
75 __u64 id;
76 __u64 sample_period;
77};
78
79typedef union event_union {
80 struct perf_event_header header;
81 struct ip_event ip;
82 struct mmap_event mmap;
83 struct comm_event comm;
84 struct fork_event fork;
85 struct period_event period;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030086} event_t;
87
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030088static LIST_HEAD(dsos);
89static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +020090static struct dso *vdso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030091
92static void dsos__add(struct dso *dso)
93{
94 list_add_tail(&dso->node, &dsos);
95}
96
97static struct dso *dsos__find(const char *name)
98{
99 struct dso *pos;
100
101 list_for_each_entry(pos, &dsos, node)
102 if (strcmp(pos->name, name) == 0)
103 return pos;
104 return NULL;
105}
106
107static struct dso *dsos__findnew(const char *name)
108{
109 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200110 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300111
Ingo Molnar4593bba2009-06-02 15:34:25 +0200112 if (dso)
113 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300114
Ingo Molnar4593bba2009-06-02 15:34:25 +0200115 dso = dso__new(name, 0);
116 if (!dso)
117 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200118
Ingo Molnarbd741372009-06-04 14:13:04 +0200119 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200120 if (nr < 0) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200121 if (verbose)
122 fprintf(stderr, "Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200123 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300124 }
Ingo Molnar4593bba2009-06-02 15:34:25 +0200125 if (!nr && verbose) {
126 fprintf(stderr,
127 "No symbols found in: %s, maybe install a debug package?\n",
128 name);
129 }
130
131 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300132
133 return dso;
134
135out_delete_dso:
136 dso__delete(dso);
137 return NULL;
138}
139
Ingo Molnar16f762a2009-05-27 09:10:38 +0200140static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300141{
142 struct dso *pos;
143
144 list_for_each_entry(pos, &dsos, node)
145 dso__fprintf(pos, fp);
146}
147
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200148static struct symbol *vdso__find_symbol(struct dso *dso, uint64_t ip)
149{
150 return dso__find_symbol(kernel_dso, ip);
151}
152
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200153static int load_kernel(void)
154{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300155 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200156
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300157 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200158 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300159 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200160
Ingo Molnarbd741372009-06-04 14:13:04 +0200161 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300162 if (err) {
163 dso__delete(kernel_dso);
164 kernel_dso = NULL;
165 } else
166 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200167
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200168 vdso = dso__new("[vdso]", 0);
169 if (!vdso)
170 return -1;
171
172 vdso->find_symbol = vdso__find_symbol;
173
174 dsos__add(vdso);
175
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300176 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200177}
178
Ingo Molnard80d3382009-06-03 23:14:49 +0200179static char __cwd[PATH_MAX];
180static char *cwd = __cwd;
181static int cwdlen;
182
183static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300184{
185 int n = 0;
186
187 while (pathname[n] == cwd[n] && n < cwdlen)
188 ++n;
189
190 return n;
191}
192
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300193struct map {
194 struct list_head node;
195 uint64_t start;
196 uint64_t end;
197 uint64_t pgoff;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200198 uint64_t (*map_ip)(struct map *, uint64_t);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300199 struct dso *dso;
200};
201
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200202static uint64_t map__map_ip(struct map *map, uint64_t ip)
203{
204 return ip - map->start + map->pgoff;
205}
206
207static uint64_t vdso__map_ip(struct map *map, uint64_t ip)
208{
209 return ip;
210}
211
Ingo Molnard80d3382009-06-03 23:14:49 +0200212static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300213{
214 struct map *self = malloc(sizeof(*self));
215
216 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300217 const char *filename = event->filename;
218 char newfilename[PATH_MAX];
219
220 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200221 int n = strcommon(filename);
222
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300223 if (n == cwdlen) {
224 snprintf(newfilename, sizeof(newfilename),
225 ".%s", filename + n);
226 filename = newfilename;
227 }
228 }
229
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300230 self->start = event->start;
231 self->end = event->start + event->len;
232 self->pgoff = event->pgoff;
233
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300234 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300235 if (self->dso == NULL)
236 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200237
238 if (self->dso == vdso)
239 self->map_ip = vdso__map_ip;
240 else
241 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300242 }
243 return self;
244out_delete:
245 free(self);
246 return NULL;
247}
248
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200249static struct map *map__clone(struct map *self)
250{
251 struct map *map = malloc(sizeof(*self));
252
253 if (!map)
254 return NULL;
255
256 memcpy(map, self, sizeof(*self));
257
258 return map;
259}
260
261static int map__overlap(struct map *l, struct map *r)
262{
263 if (l->start > r->start) {
264 struct map *t = l;
265 l = r;
266 r = t;
267 }
268
269 if (l->end > r->start)
270 return 1;
271
272 return 0;
273}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300274
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300275static size_t map__fprintf(struct map *self, FILE *fp)
276{
Yong Wangee7b31f2009-06-05 11:37:35 +0800277 return fprintf(fp, " %"PRIx64"-%"PRIx64" %"PRIx64" %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300278 self->start, self->end, self->pgoff, self->dso->name);
279}
280
281
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300282struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300283 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300284 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300285 pid_t pid;
286 char *comm;
287};
288
289static struct thread *thread__new(pid_t pid)
290{
291 struct thread *self = malloc(sizeof(*self));
292
293 if (self != NULL) {
294 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200295 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200296 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200297 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300298 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300299 }
300
301 return self;
302}
303
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300304static int thread__set_comm(struct thread *self, const char *comm)
305{
Peter Zijlstra82292892009-06-03 12:37:36 +0200306 if (self->comm)
307 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300308 self->comm = strdup(comm);
309 return self->comm ? 0 : -ENOMEM;
310}
311
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300312static size_t thread__fprintf(struct thread *self, FILE *fp)
313{
314 struct map *pos;
315 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
316
317 list_for_each_entry(pos, &self->maps, node)
318 ret += map__fprintf(pos, fp);
319
320 return ret;
321}
322
323
Ingo Molnar16f762a2009-05-27 09:10:38 +0200324static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200325static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300326
327static struct thread *threads__findnew(pid_t pid)
328{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300329 struct rb_node **p = &threads.rb_node;
330 struct rb_node *parent = NULL;
331 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300332
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200333 /*
334 * Font-end cache - PID lookups come in blocks,
335 * so most of the time we dont have to look up
336 * the full rbtree:
337 */
338 if (last_match && last_match->pid == pid)
339 return last_match;
340
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300341 while (*p != NULL) {
342 parent = *p;
343 th = rb_entry(parent, struct thread, rb_node);
344
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200345 if (th->pid == pid) {
346 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300347 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200348 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300349
350 if (pid < th->pid)
351 p = &(*p)->rb_left;
352 else
353 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300354 }
355
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300356 th = thread__new(pid);
357 if (th != NULL) {
358 rb_link_node(&th->rb_node, parent, p);
359 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200360 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300361 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200362
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300363 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300364}
365
366static void thread__insert_map(struct thread *self, struct map *map)
367{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200368 struct map *pos, *tmp;
369
370 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
371 if (map__overlap(pos, map)) {
372 list_del_init(&pos->node);
373 /* XXX leaks dsos */
374 free(pos);
375 }
376 }
377
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300378 list_add_tail(&map->node, &self->maps);
379}
380
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200381static int thread__fork(struct thread *self, struct thread *parent)
382{
383 struct map *map;
384
385 if (self->comm)
386 free(self->comm);
387 self->comm = strdup(parent->comm);
388 if (!self->comm)
389 return -ENOMEM;
390
391 list_for_each_entry(map, &parent->maps, node) {
392 struct map *new = map__clone(map);
393 if (!new)
394 return -ENOMEM;
395 thread__insert_map(self, new);
396 }
397
398 return 0;
399}
400
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300401static struct map *thread__find_map(struct thread *self, uint64_t ip)
402{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200403 struct map *pos;
404
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300405 if (self == NULL)
406 return NULL;
407
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300408 list_for_each_entry(pos, &self->maps, node)
409 if (ip >= pos->start && ip <= pos->end)
410 return pos;
411
412 return NULL;
413}
414
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300415static size_t threads__fprintf(FILE *fp)
416{
417 size_t ret = 0;
418 struct rb_node *nd;
419
420 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
421 struct thread *pos = rb_entry(nd, struct thread, rb_node);
422
423 ret += thread__fprintf(pos, fp);
424 }
425
426 return ret;
427}
428
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200429/*
430 * histogram, sorted on item, collects counts
431 */
432
433static struct rb_root hist;
434
435struct hist_entry {
436 struct rb_node rb_node;
437
438 struct thread *thread;
439 struct map *map;
440 struct dso *dso;
441 struct symbol *sym;
442 uint64_t ip;
443 char level;
444
445 uint32_t count;
446};
447
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200448/*
449 * configurable sorting bits
450 */
451
452struct sort_entry {
453 struct list_head list;
454
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200455 char *header;
456
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200457 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200458 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200459 size_t (*print)(FILE *fp, struct hist_entry *);
460};
461
Peter Zijlstra82292892009-06-03 12:37:36 +0200462/* --sort pid */
463
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200464static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200465sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
466{
467 return right->thread->pid - left->thread->pid;
468}
469
470static size_t
471sort__thread_print(FILE *fp, struct hist_entry *self)
472{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200473 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200474}
475
476static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200477 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200478 .cmp = sort__thread_cmp,
479 .print = sort__thread_print,
480};
481
Peter Zijlstra82292892009-06-03 12:37:36 +0200482/* --sort comm */
483
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200484static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200485sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
486{
Peter Zijlstra82292892009-06-03 12:37:36 +0200487 return right->thread->pid - left->thread->pid;
488}
489
490static int64_t
491sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
492{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200493 char *comm_l = left->thread->comm;
494 char *comm_r = right->thread->comm;
495
496 if (!comm_l || !comm_r) {
497 if (!comm_l && !comm_r)
498 return 0;
499 else if (!comm_l)
500 return -1;
501 else
502 return 1;
503 }
504
505 return strcmp(comm_l, comm_r);
506}
507
508static size_t
509sort__comm_print(FILE *fp, struct hist_entry *self)
510{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200511 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200512}
513
514static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200515 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200516 .cmp = sort__comm_cmp,
517 .collapse = sort__comm_collapse,
518 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200519};
520
Peter Zijlstra82292892009-06-03 12:37:36 +0200521/* --sort dso */
522
Peter Zijlstra992444b2009-05-27 20:20:27 +0200523static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200524sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
525{
526 struct dso *dso_l = left->dso;
527 struct dso *dso_r = right->dso;
528
529 if (!dso_l || !dso_r) {
530 if (!dso_l && !dso_r)
531 return 0;
532 else if (!dso_l)
533 return -1;
534 else
535 return 1;
536 }
537
538 return strcmp(dso_l->name, dso_r->name);
539}
540
541static size_t
542sort__dso_print(FILE *fp, struct hist_entry *self)
543{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200544 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200545 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200546
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200547 return fprintf(fp, "%016llx ", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200548}
549
550static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200551 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200552 .cmp = sort__dso_cmp,
553 .print = sort__dso_print,
554};
555
Peter Zijlstra82292892009-06-03 12:37:36 +0200556/* --sort symbol */
557
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200558static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200559sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300560{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200561 uint64_t ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200562
563 if (left->sym == right->sym)
564 return 0;
565
566 ip_l = left->sym ? left->sym->start : left->ip;
567 ip_r = right->sym ? right->sym->start : right->ip;
568
569 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300570}
571
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200572static size_t
573sort__sym_print(FILE *fp, struct hist_entry *self)
574{
575 size_t ret = 0;
576
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200577 if (verbose)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200578 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200579
Ingo Molnar8edd4282009-06-05 14:13:18 +0200580 if (self->sym) {
581 ret += fprintf(fp, "[%c] %s",
582 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
583 } else {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200584 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200585 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200586
587 return ret;
588}
589
590static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200591 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200592 .cmp = sort__sym_cmp,
593 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200594};
595
Peter Zijlstra82292892009-06-03 12:37:36 +0200596static int sort__need_collapse = 0;
597
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200598struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200599 char *name;
600 struct sort_entry *entry;
601 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200602};
603
604static struct sort_dimension sort_dimensions[] = {
605 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200606 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200607 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200608 { .name = "symbol", .entry = &sort_sym, },
609};
610
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200611static LIST_HEAD(hist_entry__sort_list);
612
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200613static int sort_dimension__add(char *tok)
614{
615 int i;
616
617 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
618 struct sort_dimension *sd = &sort_dimensions[i];
619
620 if (sd->taken)
621 continue;
622
Ingo Molnar5352f352009-06-03 10:07:39 +0200623 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200624 continue;
625
Peter Zijlstra82292892009-06-03 12:37:36 +0200626 if (sd->entry->collapse)
627 sort__need_collapse = 1;
628
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200629 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
630 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200631
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200632 return 0;
633 }
634
635 return -ESRCH;
636}
637
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200638static int64_t
639hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
640{
641 struct sort_entry *se;
642 int64_t cmp = 0;
643
644 list_for_each_entry(se, &hist_entry__sort_list, list) {
645 cmp = se->cmp(left, right);
646 if (cmp)
647 break;
648 }
649
650 return cmp;
651}
652
Peter Zijlstra82292892009-06-03 12:37:36 +0200653static int64_t
654hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
655{
656 struct sort_entry *se;
657 int64_t cmp = 0;
658
659 list_for_each_entry(se, &hist_entry__sort_list, list) {
660 int64_t (*f)(struct hist_entry *, struct hist_entry *);
661
662 f = se->collapse ?: se->cmp;
663
664 cmp = f(left, right);
665 if (cmp)
666 break;
667 }
668
669 return cmp;
670}
671
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200672static size_t
673hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
674{
675 struct sort_entry *se;
676 size_t ret;
677
678 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200679 double percent = self->count * 100.0 / total_samples;
680 char *color = PERF_COLOR_NORMAL;
681
682 /*
683 * We color high-overhead entries in red, low-overhead
684 * entries in green - and keep the middle ground normal:
685 */
686 if (percent >= 5.0)
687 color = PERF_COLOR_RED;
688 if (percent < 0.5)
689 color = PERF_COLOR_GREEN;
690
691 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200692 (self->count * 100.0) / total_samples);
693 } else
694 ret = fprintf(fp, "%12d ", self->count);
695
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200696 list_for_each_entry(se, &hist_entry__sort_list, list) {
697 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200698 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200699 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200700
701 ret += fprintf(fp, "\n");
702
703 return ret;
704}
705
706/*
707 * collect histogram counts
708 */
709
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200710static int
711hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
712 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300713{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200714 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300715 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200716 struct hist_entry *he;
717 struct hist_entry entry = {
718 .thread = thread,
719 .map = map,
720 .dso = dso,
721 .sym = sym,
722 .ip = ip,
723 .level = level,
724 .count = 1,
725 };
726 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300727
728 while (*p != NULL) {
729 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200730 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300731
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200732 cmp = hist_entry__cmp(&entry, he);
733
734 if (!cmp) {
735 he->count++;
736 return 0;
737 }
738
739 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300740 p = &(*p)->rb_left;
741 else
742 p = &(*p)->rb_right;
743 }
744
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200745 he = malloc(sizeof(*he));
746 if (!he)
747 return -ENOMEM;
748 *he = entry;
749 rb_link_node(&he->rb_node, parent, p);
750 rb_insert_color(&he->rb_node, &hist);
751
752 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300753}
754
Peter Zijlstra82292892009-06-03 12:37:36 +0200755static void hist_entry__free(struct hist_entry *he)
756{
757 free(he);
758}
759
760/*
761 * collapse the histogram
762 */
763
764static struct rb_root collapse_hists;
765
766static void collapse__insert_entry(struct hist_entry *he)
767{
768 struct rb_node **p = &collapse_hists.rb_node;
769 struct rb_node *parent = NULL;
770 struct hist_entry *iter;
771 int64_t cmp;
772
773 while (*p != NULL) {
774 parent = *p;
775 iter = rb_entry(parent, struct hist_entry, rb_node);
776
777 cmp = hist_entry__collapse(iter, he);
778
779 if (!cmp) {
780 iter->count += he->count;
781 hist_entry__free(he);
782 return;
783 }
784
785 if (cmp < 0)
786 p = &(*p)->rb_left;
787 else
788 p = &(*p)->rb_right;
789 }
790
791 rb_link_node(&he->rb_node, parent, p);
792 rb_insert_color(&he->rb_node, &collapse_hists);
793}
794
795static void collapse__resort(void)
796{
797 struct rb_node *next;
798 struct hist_entry *n;
799
800 if (!sort__need_collapse)
801 return;
802
803 next = rb_first(&hist);
804 while (next) {
805 n = rb_entry(next, struct hist_entry, rb_node);
806 next = rb_next(&n->rb_node);
807
808 rb_erase(&n->rb_node, &hist);
809 collapse__insert_entry(n);
810 }
811}
812
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200813/*
814 * reverse the map, sort on count.
815 */
816
817static struct rb_root output_hists;
818
819static void output__insert_entry(struct hist_entry *he)
820{
821 struct rb_node **p = &output_hists.rb_node;
822 struct rb_node *parent = NULL;
823 struct hist_entry *iter;
824
825 while (*p != NULL) {
826 parent = *p;
827 iter = rb_entry(parent, struct hist_entry, rb_node);
828
829 if (he->count > iter->count)
830 p = &(*p)->rb_left;
831 else
832 p = &(*p)->rb_right;
833 }
834
835 rb_link_node(&he->rb_node, parent, p);
836 rb_insert_color(&he->rb_node, &output_hists);
837}
838
839static void output__resort(void)
840{
Peter Zijlstra82292892009-06-03 12:37:36 +0200841 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200842 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300843 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200844
Peter Zijlstra82292892009-06-03 12:37:36 +0200845 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300846 tree = &collapse_hists;
847
848 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +0200849
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200850 while (next) {
851 n = rb_entry(next, struct hist_entry, rb_node);
852 next = rb_next(&n->rb_node);
853
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300854 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200855 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300856 }
857}
858
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200859static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300860{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200861 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200862 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300863 struct rb_node *nd;
864 size_t ret = 0;
865
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200866 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200867 fprintf(fp, "#\n");
Ingo Molnar2debbc82009-06-05 14:29:10 +0200868 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +0200869 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200870
871 fprintf(fp, "# Overhead");
872 list_for_each_entry(se, &hist_entry__sort_list, list)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200873 fprintf(fp, " %s", se->header);
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200874 fprintf(fp, "\n");
875
876 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200877 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200878 int i;
879
Ingo Molnar4593bba2009-06-02 15:34:25 +0200880 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200881 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200882 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200883 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200884 fprintf(fp, "\n");
885
886 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200887
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200888 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
889 pos = rb_entry(nd, struct hist_entry, rb_node);
890 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300891 }
892
Ingo Molnarbd741372009-06-04 14:13:04 +0200893 if (!strcmp(sort_order, default_sort_order)) {
894 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200895 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200896 fprintf(fp, "#\n");
897 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200898 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200899
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300900 return ret;
901}
902
Peter Zijlstra436224a2009-06-02 21:02:36 +0200903static void register_idle_thread(void)
904{
905 struct thread *thread = threads__findnew(0);
906
907 if (thread == NULL ||
908 thread__set_comm(thread, "[idle]")) {
909 fprintf(stderr, "problem inserting idle task.\n");
910 exit(-1);
911 }
912}
913
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200914static unsigned long total = 0,
915 total_mmap = 0,
916 total_comm = 0,
917 total_fork = 0,
918 total_unknown = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200919
Ingo Molnard80d3382009-06-03 23:14:49 +0200920static int
Ingo Molnar75051722009-06-03 23:14:49 +0200921process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
922{
923 char level;
924 int show = 0;
925 struct dso *dso = NULL;
926 struct thread *thread = threads__findnew(event->ip.pid);
927 uint64_t ip = event->ip.ip;
928 struct map *map = NULL;
929
930 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
931 (void *)(offset + head),
932 (void *)(long)(event->header.size),
933 event->header.misc,
934 event->ip.pid,
935 (void *)(long)ip);
936
937 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
938
939 if (thread == NULL) {
940 fprintf(stderr, "problem processing %d event, skipping it.\n",
941 event->header.type);
942 return -1;
943 }
944
945 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
946 show = SHOW_KERNEL;
947 level = 'k';
948
949 dso = kernel_dso;
950
951 dprintf(" ...... dso: %s\n", dso->name);
952
953 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
954
955 show = SHOW_USER;
956 level = '.';
957
958 map = thread__find_map(thread, ip);
959 if (map != NULL) {
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200960 ip = map->map_ip(map, ip);
Ingo Molnar75051722009-06-03 23:14:49 +0200961 dso = map->dso;
Ingo Molnar75051722009-06-03 23:14:49 +0200962 } else {
963 /*
964 * If this is outside of all known maps,
965 * and is a negative address, try to look it
966 * up in the kernel dso, as it might be a
967 * vsyscall (which executes in user-mode):
968 */
969 if ((long long)ip < 0)
970 dso = kernel_dso;
971 }
972 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
973
974 } else {
975 show = SHOW_HV;
976 level = 'H';
977 dprintf(" ...... dso: [hypervisor]\n");
978 }
979
980 if (show & show_mask) {
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200981 struct symbol *sym = NULL;
982
983 if (dso)
984 sym = dso->find_symbol(dso, ip);
Ingo Molnar75051722009-06-03 23:14:49 +0200985
986 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
987 fprintf(stderr,
988 "problem incrementing symbol count, skipping event\n");
989 return -1;
990 }
991 }
992 total++;
993
994 return 0;
995}
996
997static int
998process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
999{
1000 struct thread *thread = threads__findnew(event->mmap.pid);
1001 struct map *map = map__new(&event->mmap);
1002
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001003 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001004 (void *)(offset + head),
1005 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001006 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001007 (void *)(long)event->mmap.start,
1008 (void *)(long)event->mmap.len,
1009 (void *)(long)event->mmap.pgoff,
1010 event->mmap.filename);
1011
1012 if (thread == NULL || map == NULL) {
1013 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001014 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001015 }
1016
1017 thread__insert_map(thread, map);
1018 total_mmap++;
1019
1020 return 0;
1021}
1022
1023static int
1024process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1025{
1026 struct thread *thread = threads__findnew(event->comm.pid);
1027
1028 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1029 (void *)(offset + head),
1030 (void *)(long)(event->header.size),
1031 event->comm.comm, event->comm.pid);
1032
1033 if (thread == NULL ||
1034 thread__set_comm(thread, event->comm.comm)) {
1035 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1036 return -1;
1037 }
1038 total_comm++;
1039
1040 return 0;
1041}
1042
1043static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001044process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1045{
1046 struct thread *thread = threads__findnew(event->fork.pid);
1047 struct thread *parent = threads__findnew(event->fork.ppid);
1048
1049 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1050 (void *)(offset + head),
1051 (void *)(long)(event->header.size),
1052 event->fork.pid, event->fork.ppid);
1053
1054 if (!thread || !parent || thread__fork(thread, parent)) {
1055 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1056 return -1;
1057 }
1058 total_fork++;
1059
1060 return 0;
1061}
1062
1063static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001064process_period_event(event_t *event, unsigned long offset, unsigned long head)
1065{
1066 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1067 (void *)(offset + head),
1068 (void *)(long)(event->header.size),
1069 event->period.time,
1070 event->period.id,
1071 event->period.sample_period);
1072
1073 return 0;
1074}
1075
1076static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001077process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001078{
Ingo Molnar75051722009-06-03 23:14:49 +02001079 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1080 return process_overflow_event(event, offset, head);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001081
Ingo Molnar75051722009-06-03 23:14:49 +02001082 switch (event->header.type) {
1083 case PERF_EVENT_MMAP:
1084 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001085
Ingo Molnar75051722009-06-03 23:14:49 +02001086 case PERF_EVENT_COMM:
1087 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001088
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001089 case PERF_EVENT_FORK:
1090 return process_fork_event(event, offset, head);
1091
Ingo Molnarb2fef072009-06-05 18:07:51 +02001092 case PERF_EVENT_PERIOD:
1093 return process_period_event(event, offset, head);
Ingo Molnard11444d2009-06-03 23:29:14 +02001094 /*
1095 * We dont process them right now but they are fine:
1096 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001097
Ingo Molnard11444d2009-06-03 23:29:14 +02001098 case PERF_EVENT_THROTTLE:
1099 case PERF_EVENT_UNTHROTTLE:
1100 return 0;
1101
Ingo Molnard80d3382009-06-03 23:14:49 +02001102 default:
1103 return -1;
1104 }
1105
1106 return 0;
1107}
1108
1109static int __cmd_report(void)
1110{
Ingo Molnar75051722009-06-03 23:14:49 +02001111 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001112 unsigned long offset = 0;
1113 unsigned long head = 0;
1114 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001115 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001116 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001117 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001118
1119 register_idle_thread();
1120
1121 input = open(input_name, O_RDONLY);
1122 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001123 fprintf(stderr, " failed to open file: %s", input_name);
1124 if (!strcmp(input_name, "perf.data"))
1125 fprintf(stderr, " (try 'perf record' first)");
1126 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001127 exit(-1);
1128 }
1129
1130 ret = fstat(input, &stat);
1131 if (ret < 0) {
1132 perror("failed to stat file");
1133 exit(-1);
1134 }
1135
1136 if (!stat.st_size) {
1137 fprintf(stderr, "zero-sized file, nothing to do!\n");
1138 exit(0);
1139 }
1140
1141 if (load_kernel() < 0) {
1142 perror("failed to load kernel symbols");
1143 return EXIT_FAILURE;
1144 }
1145
1146 if (!full_paths) {
1147 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1148 perror("failed to get the current directory");
1149 return EXIT_FAILURE;
1150 }
1151 cwdlen = strlen(cwd);
1152 } else {
1153 cwd = NULL;
1154 cwdlen = 0;
1155 }
1156remap:
1157 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1158 MAP_SHARED, input, offset);
1159 if (buf == MAP_FAILED) {
1160 perror("failed to mmap file");
1161 exit(-1);
1162 }
1163
1164more:
1165 event = (event_t *)(buf + head);
1166
1167 size = event->header.size;
1168 if (!size)
1169 size = 8;
1170
1171 if (head + event->header.size >= page_size * mmap_window) {
1172 unsigned long shift = page_size * (head / page_size);
1173 int ret;
1174
1175 ret = munmap(buf, page_size * mmap_window);
1176 assert(ret == 0);
1177
1178 offset += shift;
1179 head -= shift;
1180 goto remap;
1181 }
1182
1183 size = event->header.size;
1184
Ingo Molnarb2fef072009-06-05 18:07:51 +02001185 dprintf("%p [%p]: event: %d\n",
1186 (void *)(offset + head),
1187 (void *)(long)event->header.size,
1188 event->header.type);
1189
Ingo Molnard80d3382009-06-03 23:14:49 +02001190 if (!size || process_event(event, offset, head) < 0) {
1191
Ingo Molnar35029732009-06-03 09:38:58 +02001192 dprintf("%p [%p]: skipping unknown header type: %d\n",
1193 (void *)(offset + head),
1194 (void *)(long)(event->header.size),
1195 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001196
Ingo Molnar3e706112009-05-26 18:53:17 +02001197 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001198
1199 /*
1200 * assume we lost track of the stream, check alignment, and
1201 * increment a single u64 in the hope to catch on again 'soon'.
1202 */
1203
1204 if (unlikely(head & 7))
1205 head &= ~7ULL;
1206
1207 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001208 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001209
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001210 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001211
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001212 if (offset + head < stat.st_size)
1213 goto more;
1214
1215 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001216 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001217
Ingo Molnar35029732009-06-03 09:38:58 +02001218 dprintf(" IP events: %10ld\n", total);
1219 dprintf(" mmap events: %10ld\n", total_mmap);
1220 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001221 dprintf(" fork events: %10ld\n", total_fork);
Ingo Molnar35029732009-06-03 09:38:58 +02001222 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001223
Ingo Molnar35029732009-06-03 09:38:58 +02001224 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001225 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001226
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001227 if (verbose >= 3)
1228 threads__fprintf(stdout);
1229
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001230 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001231 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001232
Peter Zijlstra82292892009-06-03 12:37:36 +02001233 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001234 output__resort();
1235 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001236
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001237 return rc;
1238}
1239
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001240static const char * const report_usage[] = {
1241 "perf report [<options>] <command>",
1242 NULL
1243};
1244
1245static const struct option options[] = {
1246 OPT_STRING('i', "input", &input_name, "file",
1247 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001248 OPT_BOOLEAN('v', "verbose", &verbose,
1249 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001250 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1251 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001252 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001253 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1254 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001255 OPT_BOOLEAN('P', "full-paths", &full_paths,
1256 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001257 OPT_END()
1258};
1259
Ingo Molnar5352f352009-06-03 10:07:39 +02001260static void setup_sorting(void)
1261{
1262 char *tmp, *tok, *str = strdup(sort_order);
1263
1264 for (tok = strtok_r(str, ", ", &tmp);
1265 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1266 if (sort_dimension__add(tok) < 0) {
1267 error("Unknown --sort key: `%s'", tok);
1268 usage_with_options(report_usage, options);
1269 }
1270 }
1271
1272 free(str);
1273}
1274
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001275int cmd_report(int argc, const char **argv, const char *prefix)
1276{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001277 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001278
1279 page_size = getpagesize();
1280
Ingo Molnaredc52de2009-06-04 16:24:37 +02001281 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001282
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001283 setup_sorting();
1284
Ingo Molnaredc52de2009-06-04 16:24:37 +02001285 /*
1286 * Any (unrecognized) arguments left?
1287 */
1288 if (argc)
1289 usage_with_options(report_usage, options);
1290
Ingo Molnara930d2c2009-05-27 09:50:13 +02001291 setup_pager();
1292
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001293 return __cmd_report();
1294}