blob: 61d871849b4460ac67bb5885b5979c77c26fe294 [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 Melo0085c952009-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
Pekka Enberg80d496b2009-06-08 21:12:48 +0300212static inline int is_anon_memory(const char *filename)
213{
214 return strcmp(filename, "//anon") == 0;
215}
216
Ingo Molnard80d3382009-06-03 23:14:49 +0200217static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300218{
219 struct map *self = malloc(sizeof(*self));
220
221 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300222 const char *filename = event->filename;
223 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300224 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300225
226 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200227 int n = strcommon(filename);
228
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300229 if (n == cwdlen) {
230 snprintf(newfilename, sizeof(newfilename),
231 ".%s", filename + n);
232 filename = newfilename;
233 }
234 }
235
Pekka Enberg80d496b2009-06-08 21:12:48 +0300236 anon = is_anon_memory(filename);
237
238 if (anon) {
239 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
240 filename = newfilename;
241 }
242
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300243 self->start = event->start;
244 self->end = event->start + event->len;
245 self->pgoff = event->pgoff;
246
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300247 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300248 if (self->dso == NULL)
249 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200250
Pekka Enberg80d496b2009-06-08 21:12:48 +0300251 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200252 self->map_ip = vdso__map_ip;
253 else
254 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300255 }
256 return self;
257out_delete:
258 free(self);
259 return NULL;
260}
261
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200262static struct map *map__clone(struct map *self)
263{
264 struct map *map = malloc(sizeof(*self));
265
266 if (!map)
267 return NULL;
268
269 memcpy(map, self, sizeof(*self));
270
271 return map;
272}
273
274static int map__overlap(struct map *l, struct map *r)
275{
276 if (l->start > r->start) {
277 struct map *t = l;
278 l = r;
279 r = t;
280 }
281
282 if (l->end > r->start)
283 return 1;
284
285 return 0;
286}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300287
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300288static size_t map__fprintf(struct map *self, FILE *fp)
289{
Yong Wangee7b31f2009-06-05 11:37:35 +0800290 return fprintf(fp, " %"PRIx64"-%"PRIx64" %"PRIx64" %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300291 self->start, self->end, self->pgoff, self->dso->name);
292}
293
294
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300295struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300296 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300297 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300298 pid_t pid;
299 char *comm;
300};
301
302static struct thread *thread__new(pid_t pid)
303{
304 struct thread *self = malloc(sizeof(*self));
305
306 if (self != NULL) {
307 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200308 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200309 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200310 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300311 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300312 }
313
314 return self;
315}
316
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300317static int thread__set_comm(struct thread *self, const char *comm)
318{
Peter Zijlstra82292892009-06-03 12:37:36 +0200319 if (self->comm)
320 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300321 self->comm = strdup(comm);
322 return self->comm ? 0 : -ENOMEM;
323}
324
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300325static size_t thread__fprintf(struct thread *self, FILE *fp)
326{
327 struct map *pos;
328 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
329
330 list_for_each_entry(pos, &self->maps, node)
331 ret += map__fprintf(pos, fp);
332
333 return ret;
334}
335
336
Ingo Molnar16f762a2009-05-27 09:10:38 +0200337static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200338static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300339
340static struct thread *threads__findnew(pid_t pid)
341{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300342 struct rb_node **p = &threads.rb_node;
343 struct rb_node *parent = NULL;
344 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300345
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200346 /*
347 * Font-end cache - PID lookups come in blocks,
348 * so most of the time we dont have to look up
349 * the full rbtree:
350 */
351 if (last_match && last_match->pid == pid)
352 return last_match;
353
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300354 while (*p != NULL) {
355 parent = *p;
356 th = rb_entry(parent, struct thread, rb_node);
357
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200358 if (th->pid == pid) {
359 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300360 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200361 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300362
363 if (pid < th->pid)
364 p = &(*p)->rb_left;
365 else
366 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300367 }
368
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300369 th = thread__new(pid);
370 if (th != NULL) {
371 rb_link_node(&th->rb_node, parent, p);
372 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200373 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300374 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200375
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300376 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300377}
378
379static void thread__insert_map(struct thread *self, struct map *map)
380{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200381 struct map *pos, *tmp;
382
383 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
384 if (map__overlap(pos, map)) {
385 list_del_init(&pos->node);
386 /* XXX leaks dsos */
387 free(pos);
388 }
389 }
390
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300391 list_add_tail(&map->node, &self->maps);
392}
393
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200394static int thread__fork(struct thread *self, struct thread *parent)
395{
396 struct map *map;
397
398 if (self->comm)
399 free(self->comm);
400 self->comm = strdup(parent->comm);
401 if (!self->comm)
402 return -ENOMEM;
403
404 list_for_each_entry(map, &parent->maps, node) {
405 struct map *new = map__clone(map);
406 if (!new)
407 return -ENOMEM;
408 thread__insert_map(self, new);
409 }
410
411 return 0;
412}
413
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300414static struct map *thread__find_map(struct thread *self, uint64_t ip)
415{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200416 struct map *pos;
417
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300418 if (self == NULL)
419 return NULL;
420
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300421 list_for_each_entry(pos, &self->maps, node)
422 if (ip >= pos->start && ip <= pos->end)
423 return pos;
424
425 return NULL;
426}
427
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300428static size_t threads__fprintf(FILE *fp)
429{
430 size_t ret = 0;
431 struct rb_node *nd;
432
433 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
434 struct thread *pos = rb_entry(nd, struct thread, rb_node);
435
436 ret += thread__fprintf(pos, fp);
437 }
438
439 return ret;
440}
441
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200442/*
443 * histogram, sorted on item, collects counts
444 */
445
446static struct rb_root hist;
447
448struct hist_entry {
449 struct rb_node rb_node;
450
451 struct thread *thread;
452 struct map *map;
453 struct dso *dso;
454 struct symbol *sym;
455 uint64_t ip;
456 char level;
457
458 uint32_t count;
459};
460
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200461/*
462 * configurable sorting bits
463 */
464
465struct sort_entry {
466 struct list_head list;
467
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200468 char *header;
469
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200470 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200471 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200472 size_t (*print)(FILE *fp, struct hist_entry *);
473};
474
Peter Zijlstra82292892009-06-03 12:37:36 +0200475/* --sort pid */
476
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200477static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200478sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
479{
480 return right->thread->pid - left->thread->pid;
481}
482
483static size_t
484sort__thread_print(FILE *fp, struct hist_entry *self)
485{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200486 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200487}
488
489static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200490 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200491 .cmp = sort__thread_cmp,
492 .print = sort__thread_print,
493};
494
Peter Zijlstra82292892009-06-03 12:37:36 +0200495/* --sort comm */
496
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200497static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200498sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
499{
Peter Zijlstra82292892009-06-03 12:37:36 +0200500 return right->thread->pid - left->thread->pid;
501}
502
503static int64_t
504sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
505{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200506 char *comm_l = left->thread->comm;
507 char *comm_r = right->thread->comm;
508
509 if (!comm_l || !comm_r) {
510 if (!comm_l && !comm_r)
511 return 0;
512 else if (!comm_l)
513 return -1;
514 else
515 return 1;
516 }
517
518 return strcmp(comm_l, comm_r);
519}
520
521static size_t
522sort__comm_print(FILE *fp, struct hist_entry *self)
523{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200524 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200525}
526
527static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200528 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200529 .cmp = sort__comm_cmp,
530 .collapse = sort__comm_collapse,
531 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200532};
533
Peter Zijlstra82292892009-06-03 12:37:36 +0200534/* --sort dso */
535
Peter Zijlstra992444b2009-05-27 20:20:27 +0200536static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200537sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
538{
539 struct dso *dso_l = left->dso;
540 struct dso *dso_r = right->dso;
541
542 if (!dso_l || !dso_r) {
543 if (!dso_l && !dso_r)
544 return 0;
545 else if (!dso_l)
546 return -1;
547 else
548 return 1;
549 }
550
551 return strcmp(dso_l->name, dso_r->name);
552}
553
554static size_t
555sort__dso_print(FILE *fp, struct hist_entry *self)
556{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200557 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200558 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200559
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200560 return fprintf(fp, "%016llx ", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200561}
562
563static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200564 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200565 .cmp = sort__dso_cmp,
566 .print = sort__dso_print,
567};
568
Peter Zijlstra82292892009-06-03 12:37:36 +0200569/* --sort symbol */
570
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200571static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200572sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300573{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200574 uint64_t ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200575
576 if (left->sym == right->sym)
577 return 0;
578
579 ip_l = left->sym ? left->sym->start : left->ip;
580 ip_r = right->sym ? right->sym->start : right->ip;
581
582 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300583}
584
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200585static size_t
586sort__sym_print(FILE *fp, struct hist_entry *self)
587{
588 size_t ret = 0;
589
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200590 if (verbose)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200591 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200592
Ingo Molnar8edd4282009-06-05 14:13:18 +0200593 if (self->sym) {
594 ret += fprintf(fp, "[%c] %s",
595 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
596 } else {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200597 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200598 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200599
600 return ret;
601}
602
603static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200604 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200605 .cmp = sort__sym_cmp,
606 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200607};
608
Peter Zijlstra82292892009-06-03 12:37:36 +0200609static int sort__need_collapse = 0;
610
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200611struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200612 char *name;
613 struct sort_entry *entry;
614 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200615};
616
617static struct sort_dimension sort_dimensions[] = {
618 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200619 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200620 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200621 { .name = "symbol", .entry = &sort_sym, },
622};
623
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200624static LIST_HEAD(hist_entry__sort_list);
625
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200626static int sort_dimension__add(char *tok)
627{
628 int i;
629
630 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
631 struct sort_dimension *sd = &sort_dimensions[i];
632
633 if (sd->taken)
634 continue;
635
Ingo Molnar5352f352009-06-03 10:07:39 +0200636 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200637 continue;
638
Peter Zijlstra82292892009-06-03 12:37:36 +0200639 if (sd->entry->collapse)
640 sort__need_collapse = 1;
641
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200642 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
643 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200644
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200645 return 0;
646 }
647
648 return -ESRCH;
649}
650
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200651static int64_t
652hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
653{
654 struct sort_entry *se;
655 int64_t cmp = 0;
656
657 list_for_each_entry(se, &hist_entry__sort_list, list) {
658 cmp = se->cmp(left, right);
659 if (cmp)
660 break;
661 }
662
663 return cmp;
664}
665
Peter Zijlstra82292892009-06-03 12:37:36 +0200666static int64_t
667hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
668{
669 struct sort_entry *se;
670 int64_t cmp = 0;
671
672 list_for_each_entry(se, &hist_entry__sort_list, list) {
673 int64_t (*f)(struct hist_entry *, struct hist_entry *);
674
675 f = se->collapse ?: se->cmp;
676
677 cmp = f(left, right);
678 if (cmp)
679 break;
680 }
681
682 return cmp;
683}
684
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200685static size_t
686hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
687{
688 struct sort_entry *se;
689 size_t ret;
690
691 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200692 double percent = self->count * 100.0 / total_samples;
693 char *color = PERF_COLOR_NORMAL;
694
695 /*
696 * We color high-overhead entries in red, low-overhead
697 * entries in green - and keep the middle ground normal:
698 */
699 if (percent >= 5.0)
700 color = PERF_COLOR_RED;
701 if (percent < 0.5)
702 color = PERF_COLOR_GREEN;
703
704 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200705 (self->count * 100.0) / total_samples);
706 } else
707 ret = fprintf(fp, "%12d ", self->count);
708
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200709 list_for_each_entry(se, &hist_entry__sort_list, list) {
710 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200711 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200712 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200713
714 ret += fprintf(fp, "\n");
715
716 return ret;
717}
718
719/*
720 * collect histogram counts
721 */
722
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200723static int
724hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
725 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300726{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200727 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300728 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200729 struct hist_entry *he;
730 struct hist_entry entry = {
731 .thread = thread,
732 .map = map,
733 .dso = dso,
734 .sym = sym,
735 .ip = ip,
736 .level = level,
737 .count = 1,
738 };
739 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300740
741 while (*p != NULL) {
742 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200743 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300744
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200745 cmp = hist_entry__cmp(&entry, he);
746
747 if (!cmp) {
748 he->count++;
749 return 0;
750 }
751
752 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300753 p = &(*p)->rb_left;
754 else
755 p = &(*p)->rb_right;
756 }
757
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200758 he = malloc(sizeof(*he));
759 if (!he)
760 return -ENOMEM;
761 *he = entry;
762 rb_link_node(&he->rb_node, parent, p);
763 rb_insert_color(&he->rb_node, &hist);
764
765 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300766}
767
Peter Zijlstra82292892009-06-03 12:37:36 +0200768static void hist_entry__free(struct hist_entry *he)
769{
770 free(he);
771}
772
773/*
774 * collapse the histogram
775 */
776
777static struct rb_root collapse_hists;
778
779static void collapse__insert_entry(struct hist_entry *he)
780{
781 struct rb_node **p = &collapse_hists.rb_node;
782 struct rb_node *parent = NULL;
783 struct hist_entry *iter;
784 int64_t cmp;
785
786 while (*p != NULL) {
787 parent = *p;
788 iter = rb_entry(parent, struct hist_entry, rb_node);
789
790 cmp = hist_entry__collapse(iter, he);
791
792 if (!cmp) {
793 iter->count += he->count;
794 hist_entry__free(he);
795 return;
796 }
797
798 if (cmp < 0)
799 p = &(*p)->rb_left;
800 else
801 p = &(*p)->rb_right;
802 }
803
804 rb_link_node(&he->rb_node, parent, p);
805 rb_insert_color(&he->rb_node, &collapse_hists);
806}
807
808static void collapse__resort(void)
809{
810 struct rb_node *next;
811 struct hist_entry *n;
812
813 if (!sort__need_collapse)
814 return;
815
816 next = rb_first(&hist);
817 while (next) {
818 n = rb_entry(next, struct hist_entry, rb_node);
819 next = rb_next(&n->rb_node);
820
821 rb_erase(&n->rb_node, &hist);
822 collapse__insert_entry(n);
823 }
824}
825
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200826/*
827 * reverse the map, sort on count.
828 */
829
830static struct rb_root output_hists;
831
832static void output__insert_entry(struct hist_entry *he)
833{
834 struct rb_node **p = &output_hists.rb_node;
835 struct rb_node *parent = NULL;
836 struct hist_entry *iter;
837
838 while (*p != NULL) {
839 parent = *p;
840 iter = rb_entry(parent, struct hist_entry, rb_node);
841
842 if (he->count > iter->count)
843 p = &(*p)->rb_left;
844 else
845 p = &(*p)->rb_right;
846 }
847
848 rb_link_node(&he->rb_node, parent, p);
849 rb_insert_color(&he->rb_node, &output_hists);
850}
851
852static void output__resort(void)
853{
Peter Zijlstra82292892009-06-03 12:37:36 +0200854 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200855 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300856 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200857
Peter Zijlstra82292892009-06-03 12:37:36 +0200858 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300859 tree = &collapse_hists;
860
861 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +0200862
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200863 while (next) {
864 n = rb_entry(next, struct hist_entry, rb_node);
865 next = rb_next(&n->rb_node);
866
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300867 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200868 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300869 }
870}
871
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200872static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300873{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200874 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200875 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300876 struct rb_node *nd;
877 size_t ret = 0;
878
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200879 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200880 fprintf(fp, "#\n");
Ingo Molnar2debbc82009-06-05 14:29:10 +0200881 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +0200882 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200883
884 fprintf(fp, "# Overhead");
885 list_for_each_entry(se, &hist_entry__sort_list, list)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200886 fprintf(fp, " %s", se->header);
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200887 fprintf(fp, "\n");
888
889 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200890 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200891 int i;
892
Ingo Molnar4593bba2009-06-02 15:34:25 +0200893 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200894 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200895 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200896 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200897 fprintf(fp, "\n");
898
899 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200900
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200901 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
902 pos = rb_entry(nd, struct hist_entry, rb_node);
903 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300904 }
905
Ingo Molnarbd741372009-06-04 14:13:04 +0200906 if (!strcmp(sort_order, default_sort_order)) {
907 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200908 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200909 fprintf(fp, "#\n");
910 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200911 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200912
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300913 return ret;
914}
915
Peter Zijlstra436224a2009-06-02 21:02:36 +0200916static void register_idle_thread(void)
917{
918 struct thread *thread = threads__findnew(0);
919
920 if (thread == NULL ||
921 thread__set_comm(thread, "[idle]")) {
922 fprintf(stderr, "problem inserting idle task.\n");
923 exit(-1);
924 }
925}
926
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200927static unsigned long total = 0,
928 total_mmap = 0,
929 total_comm = 0,
930 total_fork = 0,
931 total_unknown = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200932
Ingo Molnard80d3382009-06-03 23:14:49 +0200933static int
Ingo Molnar75051722009-06-03 23:14:49 +0200934process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
935{
936 char level;
937 int show = 0;
938 struct dso *dso = NULL;
939 struct thread *thread = threads__findnew(event->ip.pid);
940 uint64_t ip = event->ip.ip;
941 struct map *map = NULL;
942
943 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
944 (void *)(offset + head),
945 (void *)(long)(event->header.size),
946 event->header.misc,
947 event->ip.pid,
948 (void *)(long)ip);
949
950 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
951
952 if (thread == NULL) {
953 fprintf(stderr, "problem processing %d event, skipping it.\n",
954 event->header.type);
955 return -1;
956 }
957
958 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
959 show = SHOW_KERNEL;
960 level = 'k';
961
962 dso = kernel_dso;
963
964 dprintf(" ...... dso: %s\n", dso->name);
965
966 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
967
968 show = SHOW_USER;
969 level = '.';
970
971 map = thread__find_map(thread, ip);
972 if (map != NULL) {
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200973 ip = map->map_ip(map, ip);
Ingo Molnar75051722009-06-03 23:14:49 +0200974 dso = map->dso;
Ingo Molnar75051722009-06-03 23:14:49 +0200975 } else {
976 /*
977 * If this is outside of all known maps,
978 * and is a negative address, try to look it
979 * up in the kernel dso, as it might be a
980 * vsyscall (which executes in user-mode):
981 */
982 if ((long long)ip < 0)
983 dso = kernel_dso;
984 }
985 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
986
987 } else {
988 show = SHOW_HV;
989 level = 'H';
990 dprintf(" ...... dso: [hypervisor]\n");
991 }
992
993 if (show & show_mask) {
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200994 struct symbol *sym = NULL;
995
996 if (dso)
997 sym = dso->find_symbol(dso, ip);
Ingo Molnar75051722009-06-03 23:14:49 +0200998
999 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
1000 fprintf(stderr,
1001 "problem incrementing symbol count, skipping event\n");
1002 return -1;
1003 }
1004 }
1005 total++;
1006
1007 return 0;
1008}
1009
1010static int
1011process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1012{
1013 struct thread *thread = threads__findnew(event->mmap.pid);
1014 struct map *map = map__new(&event->mmap);
1015
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001016 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001017 (void *)(offset + head),
1018 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001019 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001020 (void *)(long)event->mmap.start,
1021 (void *)(long)event->mmap.len,
1022 (void *)(long)event->mmap.pgoff,
1023 event->mmap.filename);
1024
1025 if (thread == NULL || map == NULL) {
1026 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001027 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001028 }
1029
1030 thread__insert_map(thread, map);
1031 total_mmap++;
1032
1033 return 0;
1034}
1035
1036static int
1037process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1038{
1039 struct thread *thread = threads__findnew(event->comm.pid);
1040
1041 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1042 (void *)(offset + head),
1043 (void *)(long)(event->header.size),
1044 event->comm.comm, event->comm.pid);
1045
1046 if (thread == NULL ||
1047 thread__set_comm(thread, event->comm.comm)) {
1048 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1049 return -1;
1050 }
1051 total_comm++;
1052
1053 return 0;
1054}
1055
1056static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001057process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1058{
1059 struct thread *thread = threads__findnew(event->fork.pid);
1060 struct thread *parent = threads__findnew(event->fork.ppid);
1061
1062 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1063 (void *)(offset + head),
1064 (void *)(long)(event->header.size),
1065 event->fork.pid, event->fork.ppid);
1066
1067 if (!thread || !parent || thread__fork(thread, parent)) {
1068 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1069 return -1;
1070 }
1071 total_fork++;
1072
1073 return 0;
1074}
1075
1076static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001077process_period_event(event_t *event, unsigned long offset, unsigned long head)
1078{
1079 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1080 (void *)(offset + head),
1081 (void *)(long)(event->header.size),
1082 event->period.time,
1083 event->period.id,
1084 event->period.sample_period);
1085
1086 return 0;
1087}
1088
1089static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001090process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001091{
Ingo Molnar75051722009-06-03 23:14:49 +02001092 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1093 return process_overflow_event(event, offset, head);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001094
Ingo Molnar75051722009-06-03 23:14:49 +02001095 switch (event->header.type) {
1096 case PERF_EVENT_MMAP:
1097 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001098
Ingo Molnar75051722009-06-03 23:14:49 +02001099 case PERF_EVENT_COMM:
1100 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001101
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001102 case PERF_EVENT_FORK:
1103 return process_fork_event(event, offset, head);
1104
Ingo Molnarb2fef072009-06-05 18:07:51 +02001105 case PERF_EVENT_PERIOD:
1106 return process_period_event(event, offset, head);
Ingo Molnard11444d2009-06-03 23:29:14 +02001107 /*
1108 * We dont process them right now but they are fine:
1109 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001110
Ingo Molnard11444d2009-06-03 23:29:14 +02001111 case PERF_EVENT_THROTTLE:
1112 case PERF_EVENT_UNTHROTTLE:
1113 return 0;
1114
Ingo Molnard80d3382009-06-03 23:14:49 +02001115 default:
1116 return -1;
1117 }
1118
1119 return 0;
1120}
1121
1122static int __cmd_report(void)
1123{
Ingo Molnar75051722009-06-03 23:14:49 +02001124 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001125 unsigned long offset = 0;
1126 unsigned long head = 0;
1127 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001128 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001129 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001130 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001131
1132 register_idle_thread();
1133
1134 input = open(input_name, O_RDONLY);
1135 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001136 fprintf(stderr, " failed to open file: %s", input_name);
1137 if (!strcmp(input_name, "perf.data"))
1138 fprintf(stderr, " (try 'perf record' first)");
1139 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001140 exit(-1);
1141 }
1142
1143 ret = fstat(input, &stat);
1144 if (ret < 0) {
1145 perror("failed to stat file");
1146 exit(-1);
1147 }
1148
1149 if (!stat.st_size) {
1150 fprintf(stderr, "zero-sized file, nothing to do!\n");
1151 exit(0);
1152 }
1153
1154 if (load_kernel() < 0) {
1155 perror("failed to load kernel symbols");
1156 return EXIT_FAILURE;
1157 }
1158
1159 if (!full_paths) {
1160 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1161 perror("failed to get the current directory");
1162 return EXIT_FAILURE;
1163 }
1164 cwdlen = strlen(cwd);
1165 } else {
1166 cwd = NULL;
1167 cwdlen = 0;
1168 }
1169remap:
1170 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1171 MAP_SHARED, input, offset);
1172 if (buf == MAP_FAILED) {
1173 perror("failed to mmap file");
1174 exit(-1);
1175 }
1176
1177more:
1178 event = (event_t *)(buf + head);
1179
1180 size = event->header.size;
1181 if (!size)
1182 size = 8;
1183
1184 if (head + event->header.size >= page_size * mmap_window) {
1185 unsigned long shift = page_size * (head / page_size);
1186 int ret;
1187
1188 ret = munmap(buf, page_size * mmap_window);
1189 assert(ret == 0);
1190
1191 offset += shift;
1192 head -= shift;
1193 goto remap;
1194 }
1195
1196 size = event->header.size;
1197
Ingo Molnarb2fef072009-06-05 18:07:51 +02001198 dprintf("%p [%p]: event: %d\n",
1199 (void *)(offset + head),
1200 (void *)(long)event->header.size,
1201 event->header.type);
1202
Ingo Molnard80d3382009-06-03 23:14:49 +02001203 if (!size || process_event(event, offset, head) < 0) {
1204
Ingo Molnar35029732009-06-03 09:38:58 +02001205 dprintf("%p [%p]: skipping unknown header type: %d\n",
1206 (void *)(offset + head),
1207 (void *)(long)(event->header.size),
1208 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001209
Ingo Molnar3e706112009-05-26 18:53:17 +02001210 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001211
1212 /*
1213 * assume we lost track of the stream, check alignment, and
1214 * increment a single u64 in the hope to catch on again 'soon'.
1215 */
1216
1217 if (unlikely(head & 7))
1218 head &= ~7ULL;
1219
1220 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001221 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001222
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001223 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001224
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001225 if (offset + head < stat.st_size)
1226 goto more;
1227
1228 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001229 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001230
Ingo Molnar35029732009-06-03 09:38:58 +02001231 dprintf(" IP events: %10ld\n", total);
1232 dprintf(" mmap events: %10ld\n", total_mmap);
1233 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001234 dprintf(" fork events: %10ld\n", total_fork);
Ingo Molnar35029732009-06-03 09:38:58 +02001235 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001236
Ingo Molnar35029732009-06-03 09:38:58 +02001237 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001238 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001239
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001240 if (verbose >= 3)
1241 threads__fprintf(stdout);
1242
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001243 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001244 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001245
Peter Zijlstra82292892009-06-03 12:37:36 +02001246 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001247 output__resort();
1248 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001249
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001250 return rc;
1251}
1252
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001253static const char * const report_usage[] = {
1254 "perf report [<options>] <command>",
1255 NULL
1256};
1257
1258static const struct option options[] = {
1259 OPT_STRING('i', "input", &input_name, "file",
1260 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001261 OPT_BOOLEAN('v', "verbose", &verbose,
1262 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001263 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1264 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001265 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001266 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1267 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001268 OPT_BOOLEAN('P', "full-paths", &full_paths,
1269 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001270 OPT_END()
1271};
1272
Ingo Molnar5352f352009-06-03 10:07:39 +02001273static void setup_sorting(void)
1274{
1275 char *tmp, *tok, *str = strdup(sort_order);
1276
1277 for (tok = strtok_r(str, ", ", &tmp);
1278 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1279 if (sort_dimension__add(tok) < 0) {
1280 error("Unknown --sort key: `%s'", tok);
1281 usage_with_options(report_usage, options);
1282 }
1283 }
1284
1285 free(str);
1286}
1287
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001288int cmd_report(int argc, const char **argv, const char *prefix)
1289{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001290 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001291
1292 page_size = getpagesize();
1293
Ingo Molnaredc52de2009-06-04 16:24:37 +02001294 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001295
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001296 setup_sorting();
1297
Ingo Molnaredc52de2009-06-04 16:24:37 +02001298 /*
1299 * Any (unrecognized) arguments left?
1300 */
1301 if (argc)
1302 usage_with_options(report_usage, options);
1303
Ingo Molnara930d2c2009-05-27 09:50:13 +02001304 setup_pager();
1305
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001306 return __cmd_report();
1307}