blob: f86bb07c0e84e16881b118957764b36d8aa6a372 [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)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020039#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
Ingo Molnar35029732009-06-03 09:38:58 +020040
Ingo Molnar16f762a2009-05-27 09:10:38 +020041static int verbose;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030042static int full_paths;
Ingo Molnar3dfabc72009-06-15 11:24:38 +020043static int collapse_syscalls;
Ingo Molnar97b07b62009-05-26 18:48:58 +020044
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030045static unsigned long page_size;
46static unsigned long mmap_window = 32;
47
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020048struct ip_chain_event {
49 __u16 nr;
50 __u16 hv;
51 __u16 kernel;
52 __u16 user;
53 __u64 ips[];
54};
55
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030056struct ip_event {
57 struct perf_event_header header;
58 __u64 ip;
59 __u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020060 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030061};
Ingo Molnar75051722009-06-03 23:14:49 +020062
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030063struct mmap_event {
64 struct perf_event_header header;
65 __u32 pid, tid;
66 __u64 start;
67 __u64 len;
68 __u64 pgoff;
69 char filename[PATH_MAX];
70};
Ingo Molnar75051722009-06-03 23:14:49 +020071
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030072struct comm_event {
73 struct perf_event_header header;
Ingo Molnar75051722009-06-03 23:14:49 +020074 __u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030075 char comm[16];
76};
77
Peter Zijlstra62fc4452009-06-04 16:53:49 +020078struct fork_event {
79 struct perf_event_header header;
80 __u32 pid, ppid;
81};
82
Ingo Molnarb2fef072009-06-05 18:07:51 +020083struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030084 struct perf_event_header header;
Ingo Molnarb2fef072009-06-05 18:07:51 +020085 __u64 time;
86 __u64 id;
87 __u64 sample_period;
88};
89
90typedef union event_union {
91 struct perf_event_header header;
92 struct ip_event ip;
93 struct mmap_event mmap;
94 struct comm_event comm;
95 struct fork_event fork;
96 struct period_event period;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030097} event_t;
98
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030099static LIST_HEAD(dsos);
100static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200101static struct dso *vdso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300102
103static void dsos__add(struct dso *dso)
104{
105 list_add_tail(&dso->node, &dsos);
106}
107
108static struct dso *dsos__find(const char *name)
109{
110 struct dso *pos;
111
112 list_for_each_entry(pos, &dsos, node)
113 if (strcmp(pos->name, name) == 0)
114 return pos;
115 return NULL;
116}
117
118static struct dso *dsos__findnew(const char *name)
119{
120 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200121 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300122
Ingo Molnar4593bba2009-06-02 15:34:25 +0200123 if (dso)
124 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300125
Ingo Molnar4593bba2009-06-02 15:34:25 +0200126 dso = dso__new(name, 0);
127 if (!dso)
128 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200129
Ingo Molnarbd741372009-06-04 14:13:04 +0200130 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200131 if (nr < 0) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200132 if (verbose)
133 fprintf(stderr, "Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200134 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300135 }
Ingo Molnar4593bba2009-06-02 15:34:25 +0200136 if (!nr && verbose) {
137 fprintf(stderr,
138 "No symbols found in: %s, maybe install a debug package?\n",
139 name);
140 }
141
142 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300143
144 return dso;
145
146out_delete_dso:
147 dso__delete(dso);
148 return NULL;
149}
150
Ingo Molnar16f762a2009-05-27 09:10:38 +0200151static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300152{
153 struct dso *pos;
154
155 list_for_each_entry(pos, &dsos, node)
156 dso__fprintf(pos, fp);
157}
158
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200159static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200160{
161 return dso__find_symbol(kernel_dso, ip);
162}
163
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200164static int load_kernel(void)
165{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300166 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200167
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300168 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200169 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300170 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200171
Ingo Molnarbd741372009-06-04 14:13:04 +0200172 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300173 if (err) {
174 dso__delete(kernel_dso);
175 kernel_dso = NULL;
176 } else
177 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200178
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200179 vdso = dso__new("[vdso]", 0);
180 if (!vdso)
181 return -1;
182
183 vdso->find_symbol = vdso__find_symbol;
184
185 dsos__add(vdso);
186
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300187 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200188}
189
Ingo Molnard80d3382009-06-03 23:14:49 +0200190static char __cwd[PATH_MAX];
191static char *cwd = __cwd;
192static int cwdlen;
193
194static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300195{
196 int n = 0;
197
198 while (pathname[n] == cwd[n] && n < cwdlen)
199 ++n;
200
201 return n;
202}
203
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300204struct map {
205 struct list_head node;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200206 __u64 start;
207 __u64 end;
208 __u64 pgoff;
209 __u64 (*map_ip)(struct map *, __u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300210 struct dso *dso;
211};
212
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200213static __u64 map__map_ip(struct map *map, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200214{
215 return ip - map->start + map->pgoff;
216}
217
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200218static __u64 vdso__map_ip(struct map *map, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200219{
220 return ip;
221}
222
Pekka Enberg80d496b2009-06-08 21:12:48 +0300223static inline int is_anon_memory(const char *filename)
224{
225 return strcmp(filename, "//anon") == 0;
226}
227
Ingo Molnard80d3382009-06-03 23:14:49 +0200228static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300229{
230 struct map *self = malloc(sizeof(*self));
231
232 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300233 const char *filename = event->filename;
234 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300235 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300236
237 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200238 int n = strcommon(filename);
239
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300240 if (n == cwdlen) {
241 snprintf(newfilename, sizeof(newfilename),
242 ".%s", filename + n);
243 filename = newfilename;
244 }
245 }
246
Pekka Enberg80d496b2009-06-08 21:12:48 +0300247 anon = is_anon_memory(filename);
248
249 if (anon) {
250 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
251 filename = newfilename;
252 }
253
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300254 self->start = event->start;
255 self->end = event->start + event->len;
256 self->pgoff = event->pgoff;
257
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300258 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300259 if (self->dso == NULL)
260 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200261
Pekka Enberg80d496b2009-06-08 21:12:48 +0300262 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200263 self->map_ip = vdso__map_ip;
264 else
265 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300266 }
267 return self;
268out_delete:
269 free(self);
270 return NULL;
271}
272
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200273static struct map *map__clone(struct map *self)
274{
275 struct map *map = malloc(sizeof(*self));
276
277 if (!map)
278 return NULL;
279
280 memcpy(map, self, sizeof(*self));
281
282 return map;
283}
284
285static int map__overlap(struct map *l, struct map *r)
286{
287 if (l->start > r->start) {
288 struct map *t = l;
289 l = r;
290 r = t;
291 }
292
293 if (l->end > r->start)
294 return 1;
295
296 return 0;
297}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300298
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300299static size_t map__fprintf(struct map *self, FILE *fp)
300{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200301 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300302 self->start, self->end, self->pgoff, self->dso->name);
303}
304
305
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300306struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300307 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300308 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300309 pid_t pid;
310 char *comm;
311};
312
313static struct thread *thread__new(pid_t pid)
314{
315 struct thread *self = malloc(sizeof(*self));
316
317 if (self != NULL) {
318 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200319 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200320 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200321 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300322 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300323 }
324
325 return self;
326}
327
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300328static int thread__set_comm(struct thread *self, const char *comm)
329{
Peter Zijlstra82292892009-06-03 12:37:36 +0200330 if (self->comm)
331 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300332 self->comm = strdup(comm);
333 return self->comm ? 0 : -ENOMEM;
334}
335
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300336static size_t thread__fprintf(struct thread *self, FILE *fp)
337{
338 struct map *pos;
339 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
340
341 list_for_each_entry(pos, &self->maps, node)
342 ret += map__fprintf(pos, fp);
343
344 return ret;
345}
346
347
Ingo Molnar16f762a2009-05-27 09:10:38 +0200348static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200349static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300350
351static struct thread *threads__findnew(pid_t pid)
352{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300353 struct rb_node **p = &threads.rb_node;
354 struct rb_node *parent = NULL;
355 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300356
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200357 /*
358 * Font-end cache - PID lookups come in blocks,
359 * so most of the time we dont have to look up
360 * the full rbtree:
361 */
362 if (last_match && last_match->pid == pid)
363 return last_match;
364
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300365 while (*p != NULL) {
366 parent = *p;
367 th = rb_entry(parent, struct thread, rb_node);
368
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200369 if (th->pid == pid) {
370 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300371 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200372 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300373
374 if (pid < th->pid)
375 p = &(*p)->rb_left;
376 else
377 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300378 }
379
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300380 th = thread__new(pid);
381 if (th != NULL) {
382 rb_link_node(&th->rb_node, parent, p);
383 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200384 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300385 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200386
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300387 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300388}
389
390static void thread__insert_map(struct thread *self, struct map *map)
391{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200392 struct map *pos, *tmp;
393
394 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
395 if (map__overlap(pos, map)) {
396 list_del_init(&pos->node);
397 /* XXX leaks dsos */
398 free(pos);
399 }
400 }
401
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300402 list_add_tail(&map->node, &self->maps);
403}
404
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200405static int thread__fork(struct thread *self, struct thread *parent)
406{
407 struct map *map;
408
409 if (self->comm)
410 free(self->comm);
411 self->comm = strdup(parent->comm);
412 if (!self->comm)
413 return -ENOMEM;
414
415 list_for_each_entry(map, &parent->maps, node) {
416 struct map *new = map__clone(map);
417 if (!new)
418 return -ENOMEM;
419 thread__insert_map(self, new);
420 }
421
422 return 0;
423}
424
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200425static struct map *thread__find_map(struct thread *self, __u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300426{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200427 struct map *pos;
428
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300429 if (self == NULL)
430 return NULL;
431
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300432 list_for_each_entry(pos, &self->maps, node)
433 if (ip >= pos->start && ip <= pos->end)
434 return pos;
435
436 return NULL;
437}
438
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300439static size_t threads__fprintf(FILE *fp)
440{
441 size_t ret = 0;
442 struct rb_node *nd;
443
444 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
445 struct thread *pos = rb_entry(nd, struct thread, rb_node);
446
447 ret += thread__fprintf(pos, fp);
448 }
449
450 return ret;
451}
452
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200453/*
454 * histogram, sorted on item, collects counts
455 */
456
457static struct rb_root hist;
458
459struct hist_entry {
460 struct rb_node rb_node;
461
462 struct thread *thread;
463 struct map *map;
464 struct dso *dso;
465 struct symbol *sym;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200466 __u64 ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200467 char level;
468
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200469 __u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200470};
471
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200472/*
473 * configurable sorting bits
474 */
475
476struct sort_entry {
477 struct list_head list;
478
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200479 char *header;
480
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200481 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200482 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200483 size_t (*print)(FILE *fp, struct hist_entry *);
484};
485
Peter Zijlstra82292892009-06-03 12:37:36 +0200486/* --sort pid */
487
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200488static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200489sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
490{
491 return right->thread->pid - left->thread->pid;
492}
493
494static size_t
495sort__thread_print(FILE *fp, struct hist_entry *self)
496{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200497 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200498}
499
500static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200501 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200502 .cmp = sort__thread_cmp,
503 .print = sort__thread_print,
504};
505
Peter Zijlstra82292892009-06-03 12:37:36 +0200506/* --sort comm */
507
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200508static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200509sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
510{
Peter Zijlstra82292892009-06-03 12:37:36 +0200511 return right->thread->pid - left->thread->pid;
512}
513
514static int64_t
515sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
516{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200517 char *comm_l = left->thread->comm;
518 char *comm_r = right->thread->comm;
519
520 if (!comm_l || !comm_r) {
521 if (!comm_l && !comm_r)
522 return 0;
523 else if (!comm_l)
524 return -1;
525 else
526 return 1;
527 }
528
529 return strcmp(comm_l, comm_r);
530}
531
532static size_t
533sort__comm_print(FILE *fp, struct hist_entry *self)
534{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200535 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200536}
537
538static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200539 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200540 .cmp = sort__comm_cmp,
541 .collapse = sort__comm_collapse,
542 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200543};
544
Peter Zijlstra82292892009-06-03 12:37:36 +0200545/* --sort dso */
546
Peter Zijlstra992444b2009-05-27 20:20:27 +0200547static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200548sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
549{
550 struct dso *dso_l = left->dso;
551 struct dso *dso_r = right->dso;
552
553 if (!dso_l || !dso_r) {
554 if (!dso_l && !dso_r)
555 return 0;
556 else if (!dso_l)
557 return -1;
558 else
559 return 1;
560 }
561
562 return strcmp(dso_l->name, dso_r->name);
563}
564
565static size_t
566sort__dso_print(FILE *fp, struct hist_entry *self)
567{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200568 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200569 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200570
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200571 return fprintf(fp, "%016llx ", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200572}
573
574static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200575 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200576 .cmp = sort__dso_cmp,
577 .print = sort__dso_print,
578};
579
Peter Zijlstra82292892009-06-03 12:37:36 +0200580/* --sort symbol */
581
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200582static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200583sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300584{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200585 __u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200586
587 if (left->sym == right->sym)
588 return 0;
589
590 ip_l = left->sym ? left->sym->start : left->ip;
591 ip_r = right->sym ? right->sym->start : right->ip;
592
593 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300594}
595
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200596static size_t
597sort__sym_print(FILE *fp, struct hist_entry *self)
598{
599 size_t ret = 0;
600
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200601 if (verbose)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200602 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200603
Ingo Molnar8edd4282009-06-05 14:13:18 +0200604 if (self->sym) {
605 ret += fprintf(fp, "[%c] %s",
606 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
607 } else {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200608 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200609 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200610
611 return ret;
612}
613
614static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200615 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200616 .cmp = sort__sym_cmp,
617 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200618};
619
Peter Zijlstra82292892009-06-03 12:37:36 +0200620static int sort__need_collapse = 0;
621
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200622struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200623 char *name;
624 struct sort_entry *entry;
625 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200626};
627
628static struct sort_dimension sort_dimensions[] = {
629 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200630 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200631 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200632 { .name = "symbol", .entry = &sort_sym, },
633};
634
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200635static LIST_HEAD(hist_entry__sort_list);
636
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200637static int sort_dimension__add(char *tok)
638{
639 int i;
640
641 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
642 struct sort_dimension *sd = &sort_dimensions[i];
643
644 if (sd->taken)
645 continue;
646
Ingo Molnar5352f352009-06-03 10:07:39 +0200647 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200648 continue;
649
Peter Zijlstra82292892009-06-03 12:37:36 +0200650 if (sd->entry->collapse)
651 sort__need_collapse = 1;
652
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200653 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
654 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200655
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200656 return 0;
657 }
658
659 return -ESRCH;
660}
661
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200662static int64_t
663hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
664{
665 struct sort_entry *se;
666 int64_t cmp = 0;
667
668 list_for_each_entry(se, &hist_entry__sort_list, list) {
669 cmp = se->cmp(left, right);
670 if (cmp)
671 break;
672 }
673
674 return cmp;
675}
676
Peter Zijlstra82292892009-06-03 12:37:36 +0200677static int64_t
678hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
679{
680 struct sort_entry *se;
681 int64_t cmp = 0;
682
683 list_for_each_entry(se, &hist_entry__sort_list, list) {
684 int64_t (*f)(struct hist_entry *, struct hist_entry *);
685
686 f = se->collapse ?: se->cmp;
687
688 cmp = f(left, right);
689 if (cmp)
690 break;
691 }
692
693 return cmp;
694}
695
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200696static size_t
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200697hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200698{
699 struct sort_entry *se;
700 size_t ret;
701
702 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200703 double percent = self->count * 100.0 / total_samples;
704 char *color = PERF_COLOR_NORMAL;
705
706 /*
Ingo Molnaraefcf372009-06-08 23:15:28 +0200707 * We color high-overhead entries in red, mid-overhead
708 * entries in green - and keep the low overhead places
709 * normal:
Ingo Molnar8fc03212009-06-04 15:19:47 +0200710 */
Ingo Molnaraefcf372009-06-08 23:15:28 +0200711 if (percent >= 5.0) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200712 color = PERF_COLOR_RED;
Ingo Molnaraefcf372009-06-08 23:15:28 +0200713 } else {
714 if (percent >= 0.5)
715 color = PERF_COLOR_GREEN;
716 }
Ingo Molnar8fc03212009-06-04 15:19:47 +0200717
718 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200719 (self->count * 100.0) / total_samples);
720 } else
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200721 ret = fprintf(fp, "%12Ld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200722
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200723 list_for_each_entry(se, &hist_entry__sort_list, list) {
724 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200725 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200726 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200727
728 ret += fprintf(fp, "\n");
729
730 return ret;
731}
732
733/*
734 * collect histogram counts
735 */
736
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200737static int
738hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200739 struct symbol *sym, __u64 ip, char level, __u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300740{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200741 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300742 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200743 struct hist_entry *he;
744 struct hist_entry entry = {
745 .thread = thread,
746 .map = map,
747 .dso = dso,
748 .sym = sym,
749 .ip = ip,
750 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200751 .count = count,
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200752 };
753 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300754
755 while (*p != NULL) {
756 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200757 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300758
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200759 cmp = hist_entry__cmp(&entry, he);
760
761 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200762 he->count += count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200763 return 0;
764 }
765
766 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300767 p = &(*p)->rb_left;
768 else
769 p = &(*p)->rb_right;
770 }
771
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200772 he = malloc(sizeof(*he));
773 if (!he)
774 return -ENOMEM;
775 *he = entry;
776 rb_link_node(&he->rb_node, parent, p);
777 rb_insert_color(&he->rb_node, &hist);
778
779 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300780}
781
Peter Zijlstra82292892009-06-03 12:37:36 +0200782static void hist_entry__free(struct hist_entry *he)
783{
784 free(he);
785}
786
787/*
788 * collapse the histogram
789 */
790
791static struct rb_root collapse_hists;
792
793static void collapse__insert_entry(struct hist_entry *he)
794{
795 struct rb_node **p = &collapse_hists.rb_node;
796 struct rb_node *parent = NULL;
797 struct hist_entry *iter;
798 int64_t cmp;
799
800 while (*p != NULL) {
801 parent = *p;
802 iter = rb_entry(parent, struct hist_entry, rb_node);
803
804 cmp = hist_entry__collapse(iter, he);
805
806 if (!cmp) {
807 iter->count += he->count;
808 hist_entry__free(he);
809 return;
810 }
811
812 if (cmp < 0)
813 p = &(*p)->rb_left;
814 else
815 p = &(*p)->rb_right;
816 }
817
818 rb_link_node(&he->rb_node, parent, p);
819 rb_insert_color(&he->rb_node, &collapse_hists);
820}
821
822static void collapse__resort(void)
823{
824 struct rb_node *next;
825 struct hist_entry *n;
826
827 if (!sort__need_collapse)
828 return;
829
830 next = rb_first(&hist);
831 while (next) {
832 n = rb_entry(next, struct hist_entry, rb_node);
833 next = rb_next(&n->rb_node);
834
835 rb_erase(&n->rb_node, &hist);
836 collapse__insert_entry(n);
837 }
838}
839
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200840/*
841 * reverse the map, sort on count.
842 */
843
844static struct rb_root output_hists;
845
846static void output__insert_entry(struct hist_entry *he)
847{
848 struct rb_node **p = &output_hists.rb_node;
849 struct rb_node *parent = NULL;
850 struct hist_entry *iter;
851
852 while (*p != NULL) {
853 parent = *p;
854 iter = rb_entry(parent, struct hist_entry, rb_node);
855
856 if (he->count > iter->count)
857 p = &(*p)->rb_left;
858 else
859 p = &(*p)->rb_right;
860 }
861
862 rb_link_node(&he->rb_node, parent, p);
863 rb_insert_color(&he->rb_node, &output_hists);
864}
865
866static void output__resort(void)
867{
Peter Zijlstra82292892009-06-03 12:37:36 +0200868 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200869 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300870 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200871
Peter Zijlstra82292892009-06-03 12:37:36 +0200872 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300873 tree = &collapse_hists;
874
875 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +0200876
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200877 while (next) {
878 n = rb_entry(next, struct hist_entry, rb_node);
879 next = rb_next(&n->rb_node);
880
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300881 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200882 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300883 }
884}
885
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200886static size_t output__fprintf(FILE *fp, __u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300887{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200888 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200889 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300890 struct rb_node *nd;
891 size_t ret = 0;
892
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200893 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200894 fprintf(fp, "#\n");
Ingo Molnar2debbc82009-06-05 14:29:10 +0200895 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +0200896 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200897
898 fprintf(fp, "# Overhead");
899 list_for_each_entry(se, &hist_entry__sort_list, list)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200900 fprintf(fp, " %s", se->header);
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200901 fprintf(fp, "\n");
902
903 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200904 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200905 int i;
906
Ingo Molnar4593bba2009-06-02 15:34:25 +0200907 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200908 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200909 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200910 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200911 fprintf(fp, "\n");
912
913 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200914
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200915 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
916 pos = rb_entry(nd, struct hist_entry, rb_node);
917 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300918 }
919
Ingo Molnarbd741372009-06-04 14:13:04 +0200920 if (!strcmp(sort_order, default_sort_order)) {
921 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200922 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200923 fprintf(fp, "#\n");
924 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200925 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200926
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300927 return ret;
928}
929
Peter Zijlstra436224a2009-06-02 21:02:36 +0200930static void register_idle_thread(void)
931{
932 struct thread *thread = threads__findnew(0);
933
934 if (thread == NULL ||
935 thread__set_comm(thread, "[idle]")) {
936 fprintf(stderr, "problem inserting idle task.\n");
937 exit(-1);
938 }
939}
940
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200941static unsigned long total = 0,
942 total_mmap = 0,
943 total_comm = 0,
944 total_fork = 0,
945 total_unknown = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200946
Ingo Molnard80d3382009-06-03 23:14:49 +0200947static int
Ingo Molnar75051722009-06-03 23:14:49 +0200948process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
949{
950 char level;
951 int show = 0;
952 struct dso *dso = NULL;
953 struct thread *thread = threads__findnew(event->ip.pid);
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200954 __u64 ip = event->ip.ip;
955 __u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +0200956 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200957 void *more_data = event->ip.__more_data;
958 struct ip_chain_event *chain;
Ingo Molnar75051722009-06-03 23:14:49 +0200959
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200960 if (event->header.type & PERF_SAMPLE_PERIOD) {
961 period = *(__u64 *)more_data;
962 more_data += sizeof(__u64);
963 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200964
Peter Zijlstra4502d772009-06-10 15:03:06 +0200965 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200966 (void *)(offset + head),
967 (void *)(long)(event->header.size),
968 event->header.misc,
969 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +0200970 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200971 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +0200972
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200973 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
974 int i;
975
976 chain = (void *)more_data;
977
978 if (dump_trace) {
979 dprintf("... chain: u:%d, k:%d, nr:%d\n",
980 chain->user,
981 chain->kernel,
982 chain->nr);
983
984 for (i = 0; i < chain->nr; i++)
Ingo Molnare2eae0f2009-06-15 16:15:19 +0200985 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200986 }
Ingo Molnar3dfabc72009-06-15 11:24:38 +0200987 if (collapse_syscalls) {
988 /*
989 * Find the all-but-last kernel entry
990 * amongst the call-chains - to get
991 * to the level of system calls:
992 */
993 if (chain->kernel >= 2)
994 ip = chain->ips[chain->kernel-2];
995 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200996 }
997
Ingo Molnar75051722009-06-03 23:14:49 +0200998 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
999
1000 if (thread == NULL) {
1001 fprintf(stderr, "problem processing %d event, skipping it.\n",
1002 event->header.type);
1003 return -1;
1004 }
1005
1006 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1007 show = SHOW_KERNEL;
1008 level = 'k';
1009
1010 dso = kernel_dso;
1011
1012 dprintf(" ...... dso: %s\n", dso->name);
1013
1014 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1015
1016 show = SHOW_USER;
1017 level = '.';
1018
1019 map = thread__find_map(thread, ip);
1020 if (map != NULL) {
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001021 ip = map->map_ip(map, ip);
Ingo Molnar75051722009-06-03 23:14:49 +02001022 dso = map->dso;
Ingo Molnar75051722009-06-03 23:14:49 +02001023 } else {
1024 /*
1025 * If this is outside of all known maps,
1026 * and is a negative address, try to look it
1027 * up in the kernel dso, as it might be a
1028 * vsyscall (which executes in user-mode):
1029 */
1030 if ((long long)ip < 0)
1031 dso = kernel_dso;
1032 }
1033 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1034
1035 } else {
1036 show = SHOW_HV;
1037 level = 'H';
1038 dprintf(" ...... dso: [hypervisor]\n");
1039 }
1040
1041 if (show & show_mask) {
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001042 struct symbol *sym = NULL;
1043
1044 if (dso)
1045 sym = dso->find_symbol(dso, ip);
Ingo Molnar75051722009-06-03 23:14:49 +02001046
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001047 if (hist_entry__add(thread, map, dso, sym, ip, level, period)) {
Ingo Molnar75051722009-06-03 23:14:49 +02001048 fprintf(stderr,
1049 "problem incrementing symbol count, skipping event\n");
1050 return -1;
1051 }
1052 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001053 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001054
1055 return 0;
1056}
1057
1058static int
1059process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1060{
1061 struct thread *thread = threads__findnew(event->mmap.pid);
1062 struct map *map = map__new(&event->mmap);
1063
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001064 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001065 (void *)(offset + head),
1066 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001067 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001068 (void *)(long)event->mmap.start,
1069 (void *)(long)event->mmap.len,
1070 (void *)(long)event->mmap.pgoff,
1071 event->mmap.filename);
1072
1073 if (thread == NULL || map == NULL) {
1074 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001075 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001076 }
1077
1078 thread__insert_map(thread, map);
1079 total_mmap++;
1080
1081 return 0;
1082}
1083
1084static int
1085process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1086{
1087 struct thread *thread = threads__findnew(event->comm.pid);
1088
1089 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1090 (void *)(offset + head),
1091 (void *)(long)(event->header.size),
1092 event->comm.comm, event->comm.pid);
1093
1094 if (thread == NULL ||
1095 thread__set_comm(thread, event->comm.comm)) {
1096 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1097 return -1;
1098 }
1099 total_comm++;
1100
1101 return 0;
1102}
1103
1104static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001105process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1106{
1107 struct thread *thread = threads__findnew(event->fork.pid);
1108 struct thread *parent = threads__findnew(event->fork.ppid);
1109
1110 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1111 (void *)(offset + head),
1112 (void *)(long)(event->header.size),
1113 event->fork.pid, event->fork.ppid);
1114
1115 if (!thread || !parent || thread__fork(thread, parent)) {
1116 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1117 return -1;
1118 }
1119 total_fork++;
1120
1121 return 0;
1122}
1123
1124static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001125process_period_event(event_t *event, unsigned long offset, unsigned long head)
1126{
1127 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1128 (void *)(offset + head),
1129 (void *)(long)(event->header.size),
1130 event->period.time,
1131 event->period.id,
1132 event->period.sample_period);
1133
1134 return 0;
1135}
1136
Ingo Molnar8465b052009-06-14 14:44:07 +02001137static void trace_event(event_t *event)
1138{
1139 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001140 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001141 int i, j;
1142
1143 if (!dump_trace)
1144 return;
1145
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001146 dprintf(".");
1147 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001148
1149 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001150 if ((i & 15) == 0) {
1151 dprintf(".");
1152 cdprintf(" %04x: ", i);
1153 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001154
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001155 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001156
1157 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001158 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001159 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001160 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001161 for (j = 0; j < (i & 15); j++) {
1162 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001163 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001164 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001165 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001166 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001167 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001168 }
1169 }
1170 dprintf(".\n");
1171}
1172
Ingo Molnarb2fef072009-06-05 18:07:51 +02001173static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001174process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001175{
Ingo Molnar8465b052009-06-14 14:44:07 +02001176 trace_event(event);
1177
Ingo Molnar75051722009-06-03 23:14:49 +02001178 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1179 return process_overflow_event(event, offset, head);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001180
Ingo Molnar75051722009-06-03 23:14:49 +02001181 switch (event->header.type) {
1182 case PERF_EVENT_MMAP:
1183 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001184
Ingo Molnar75051722009-06-03 23:14:49 +02001185 case PERF_EVENT_COMM:
1186 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001187
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001188 case PERF_EVENT_FORK:
1189 return process_fork_event(event, offset, head);
1190
Ingo Molnarb2fef072009-06-05 18:07:51 +02001191 case PERF_EVENT_PERIOD:
1192 return process_period_event(event, offset, head);
Ingo Molnard11444d2009-06-03 23:29:14 +02001193 /*
1194 * We dont process them right now but they are fine:
1195 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001196
Ingo Molnard11444d2009-06-03 23:29:14 +02001197 case PERF_EVENT_THROTTLE:
1198 case PERF_EVENT_UNTHROTTLE:
1199 return 0;
1200
Ingo Molnard80d3382009-06-03 23:14:49 +02001201 default:
1202 return -1;
1203 }
1204
1205 return 0;
1206}
1207
1208static int __cmd_report(void)
1209{
Ingo Molnar75051722009-06-03 23:14:49 +02001210 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001211 unsigned long offset = 0;
1212 unsigned long head = 0;
1213 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001214 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001215 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001216 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001217
1218 register_idle_thread();
1219
1220 input = open(input_name, O_RDONLY);
1221 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001222 fprintf(stderr, " failed to open file: %s", input_name);
1223 if (!strcmp(input_name, "perf.data"))
1224 fprintf(stderr, " (try 'perf record' first)");
1225 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001226 exit(-1);
1227 }
1228
1229 ret = fstat(input, &stat);
1230 if (ret < 0) {
1231 perror("failed to stat file");
1232 exit(-1);
1233 }
1234
1235 if (!stat.st_size) {
1236 fprintf(stderr, "zero-sized file, nothing to do!\n");
1237 exit(0);
1238 }
1239
1240 if (load_kernel() < 0) {
1241 perror("failed to load kernel symbols");
1242 return EXIT_FAILURE;
1243 }
1244
1245 if (!full_paths) {
1246 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1247 perror("failed to get the current directory");
1248 return EXIT_FAILURE;
1249 }
1250 cwdlen = strlen(cwd);
1251 } else {
1252 cwd = NULL;
1253 cwdlen = 0;
1254 }
1255remap:
1256 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1257 MAP_SHARED, input, offset);
1258 if (buf == MAP_FAILED) {
1259 perror("failed to mmap file");
1260 exit(-1);
1261 }
1262
1263more:
1264 event = (event_t *)(buf + head);
1265
1266 size = event->header.size;
1267 if (!size)
1268 size = 8;
1269
1270 if (head + event->header.size >= page_size * mmap_window) {
1271 unsigned long shift = page_size * (head / page_size);
1272 int ret;
1273
1274 ret = munmap(buf, page_size * mmap_window);
1275 assert(ret == 0);
1276
1277 offset += shift;
1278 head -= shift;
1279 goto remap;
1280 }
1281
1282 size = event->header.size;
1283
Ingo Molnar8465b052009-06-14 14:44:07 +02001284 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001285 (void *)(offset + head),
1286 (void *)(long)event->header.size,
1287 event->header.type);
1288
Ingo Molnard80d3382009-06-03 23:14:49 +02001289 if (!size || process_event(event, offset, head) < 0) {
1290
Ingo Molnar35029732009-06-03 09:38:58 +02001291 dprintf("%p [%p]: skipping unknown header type: %d\n",
1292 (void *)(offset + head),
1293 (void *)(long)(event->header.size),
1294 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001295
Ingo Molnar3e706112009-05-26 18:53:17 +02001296 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001297
1298 /*
1299 * assume we lost track of the stream, check alignment, and
1300 * increment a single u64 in the hope to catch on again 'soon'.
1301 */
1302
1303 if (unlikely(head & 7))
1304 head &= ~7ULL;
1305
1306 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001307 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001308
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001309 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001310
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001311 if (offset + head < stat.st_size)
1312 goto more;
1313
1314 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001315 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001316
Ingo Molnar35029732009-06-03 09:38:58 +02001317 dprintf(" IP events: %10ld\n", total);
1318 dprintf(" mmap events: %10ld\n", total_mmap);
1319 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001320 dprintf(" fork events: %10ld\n", total_fork);
Ingo Molnar35029732009-06-03 09:38:58 +02001321 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001322
Ingo Molnar35029732009-06-03 09:38:58 +02001323 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001324 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001325
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001326 if (verbose >= 3)
1327 threads__fprintf(stdout);
1328
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001329 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001330 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001331
Peter Zijlstra82292892009-06-03 12:37:36 +02001332 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001333 output__resort();
1334 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001335
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001336 return rc;
1337}
1338
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001339static const char * const report_usage[] = {
1340 "perf report [<options>] <command>",
1341 NULL
1342};
1343
1344static const struct option options[] = {
1345 OPT_STRING('i', "input", &input_name, "file",
1346 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001347 OPT_BOOLEAN('v', "verbose", &verbose,
1348 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001349 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1350 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001351 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001352 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1353 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001354 OPT_BOOLEAN('P', "full-paths", &full_paths,
1355 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar3dfabc72009-06-15 11:24:38 +02001356 OPT_BOOLEAN('S', "syscalls", &collapse_syscalls,
1357 "show per syscall summary overhead, using call graph"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001358 OPT_END()
1359};
1360
Ingo Molnar5352f352009-06-03 10:07:39 +02001361static void setup_sorting(void)
1362{
1363 char *tmp, *tok, *str = strdup(sort_order);
1364
1365 for (tok = strtok_r(str, ", ", &tmp);
1366 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1367 if (sort_dimension__add(tok) < 0) {
1368 error("Unknown --sort key: `%s'", tok);
1369 usage_with_options(report_usage, options);
1370 }
1371 }
1372
1373 free(str);
1374}
1375
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001376int cmd_report(int argc, const char **argv, const char *prefix)
1377{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001378 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001379
1380 page_size = getpagesize();
1381
Ingo Molnaredc52de2009-06-04 16:24:37 +02001382 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001383
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001384 setup_sorting();
1385
Ingo Molnaredc52de2009-06-04 16:24:37 +02001386 /*
1387 * Any (unrecognized) arguments left?
1388 */
1389 if (argc)
1390 usage_with_options(report_usage, options);
1391
Ingo Molnara930d2c2009-05-27 09:50:13 +02001392 setup_pager();
1393
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001394 return __cmd_report();
1395}