blob: 15fe9dae792b006a7154740aa37d9e0e178850df [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
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030012#include "util/list.h"
Ingo Molnara930d2c2009-05-27 09:50:13 +020013#include "util/cache.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030014#include "util/rbtree.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030015#include "util/symbol.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030016#include "util/string.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030017
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020018#include "perf.h"
19
20#include "util/parse-options.h"
21#include "util/parse-events.h"
22
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030023#define SHOW_KERNEL 1
24#define SHOW_USER 2
25#define SHOW_HV 4
26
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020027static char const *input_name = "perf.data";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020028static char *vmlinux = NULL;
Ingo Molnarbd741372009-06-04 14:13:04 +020029
30static char default_sort_order[] = "comm,dso";
31static char *sort_order = default_sort_order;
32
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030033static int input;
34static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
35
Ingo Molnar97b07b62009-05-26 18:48:58 +020036static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020037#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
38
Ingo Molnar16f762a2009-05-27 09:10:38 +020039static int verbose;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030040static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020041
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030042static unsigned long page_size;
43static unsigned long mmap_window = 32;
44
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020045const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030046 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
47 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
48 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
49};
50
51struct ip_event {
52 struct perf_event_header header;
53 __u64 ip;
54 __u32 pid, tid;
55};
Ingo Molnar75051722009-06-03 23:14:49 +020056
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030057struct mmap_event {
58 struct perf_event_header header;
59 __u32 pid, tid;
60 __u64 start;
61 __u64 len;
62 __u64 pgoff;
63 char filename[PATH_MAX];
64};
Ingo Molnar75051722009-06-03 23:14:49 +020065
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030066struct comm_event {
67 struct perf_event_header header;
Ingo Molnar75051722009-06-03 23:14:49 +020068 __u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030069 char comm[16];
70};
71
72typedef union event_union {
73 struct perf_event_header header;
74 struct ip_event ip;
75 struct mmap_event mmap;
76 struct comm_event comm;
77} event_t;
78
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030079static LIST_HEAD(dsos);
80static struct dso *kernel_dso;
81
82static void dsos__add(struct dso *dso)
83{
84 list_add_tail(&dso->node, &dsos);
85}
86
87static struct dso *dsos__find(const char *name)
88{
89 struct dso *pos;
90
91 list_for_each_entry(pos, &dsos, node)
92 if (strcmp(pos->name, name) == 0)
93 return pos;
94 return NULL;
95}
96
97static struct dso *dsos__findnew(const char *name)
98{
99 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200100 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300101
Ingo Molnar4593bba2009-06-02 15:34:25 +0200102 if (dso)
103 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300104
Ingo Molnar4593bba2009-06-02 15:34:25 +0200105 dso = dso__new(name, 0);
106 if (!dso)
107 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200108
Ingo Molnarbd741372009-06-04 14:13:04 +0200109 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200110 if (nr < 0) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200111 if (verbose)
112 fprintf(stderr, "Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200113 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300114 }
Ingo Molnar4593bba2009-06-02 15:34:25 +0200115 if (!nr && verbose) {
116 fprintf(stderr,
117 "No symbols found in: %s, maybe install a debug package?\n",
118 name);
119 }
120
121 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300122
123 return dso;
124
125out_delete_dso:
126 dso__delete(dso);
127 return NULL;
128}
129
Ingo Molnar16f762a2009-05-27 09:10:38 +0200130static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300131{
132 struct dso *pos;
133
134 list_for_each_entry(pos, &dsos, node)
135 dso__fprintf(pos, fp);
136}
137
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200138static int load_kernel(void)
139{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300140 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200141
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300142 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200143 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300144 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200145
Ingo Molnarbd741372009-06-04 14:13:04 +0200146 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300147 if (err) {
148 dso__delete(kernel_dso);
149 kernel_dso = NULL;
150 } else
151 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200152
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300153 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200154}
155
Ingo Molnard80d3382009-06-03 23:14:49 +0200156static char __cwd[PATH_MAX];
157static char *cwd = __cwd;
158static int cwdlen;
159
160static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300161{
162 int n = 0;
163
164 while (pathname[n] == cwd[n] && n < cwdlen)
165 ++n;
166
167 return n;
168}
169
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300170struct map {
171 struct list_head node;
172 uint64_t start;
173 uint64_t end;
174 uint64_t pgoff;
175 struct dso *dso;
176};
177
Ingo Molnard80d3382009-06-03 23:14:49 +0200178static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300179{
180 struct map *self = malloc(sizeof(*self));
181
182 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300183 const char *filename = event->filename;
184 char newfilename[PATH_MAX];
185
186 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200187 int n = strcommon(filename);
188
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300189 if (n == cwdlen) {
190 snprintf(newfilename, sizeof(newfilename),
191 ".%s", filename + n);
192 filename = newfilename;
193 }
194 }
195
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300196 self->start = event->start;
197 self->end = event->start + event->len;
198 self->pgoff = event->pgoff;
199
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300200 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300201 if (self->dso == NULL)
202 goto out_delete;
203 }
204 return self;
205out_delete:
206 free(self);
207 return NULL;
208}
209
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300210struct thread;
211
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300212struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300213 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300214 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300215 pid_t pid;
216 char *comm;
217};
218
219static struct thread *thread__new(pid_t pid)
220{
221 struct thread *self = malloc(sizeof(*self));
222
223 if (self != NULL) {
224 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200225 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200226 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200227 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300228 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300229 }
230
231 return self;
232}
233
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300234static int thread__set_comm(struct thread *self, const char *comm)
235{
Peter Zijlstra82292892009-06-03 12:37:36 +0200236 if (self->comm)
237 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300238 self->comm = strdup(comm);
239 return self->comm ? 0 : -ENOMEM;
240}
241
Ingo Molnar16f762a2009-05-27 09:10:38 +0200242static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200243static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300244
245static struct thread *threads__findnew(pid_t pid)
246{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300247 struct rb_node **p = &threads.rb_node;
248 struct rb_node *parent = NULL;
249 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300250
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200251 /*
252 * Font-end cache - PID lookups come in blocks,
253 * so most of the time we dont have to look up
254 * the full rbtree:
255 */
256 if (last_match && last_match->pid == pid)
257 return last_match;
258
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300259 while (*p != NULL) {
260 parent = *p;
261 th = rb_entry(parent, struct thread, rb_node);
262
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200263 if (th->pid == pid) {
264 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300265 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200266 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300267
268 if (pid < th->pid)
269 p = &(*p)->rb_left;
270 else
271 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300272 }
273
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300274 th = thread__new(pid);
275 if (th != NULL) {
276 rb_link_node(&th->rb_node, parent, p);
277 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200278 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300279 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200280
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300281 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300282}
283
284static void thread__insert_map(struct thread *self, struct map *map)
285{
286 list_add_tail(&map->node, &self->maps);
287}
288
289static struct map *thread__find_map(struct thread *self, uint64_t ip)
290{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200291 struct map *pos;
292
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300293 if (self == NULL)
294 return NULL;
295
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300296 list_for_each_entry(pos, &self->maps, node)
297 if (ip >= pos->start && ip <= pos->end)
298 return pos;
299
300 return NULL;
301}
302
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200303/*
304 * histogram, sorted on item, collects counts
305 */
306
307static struct rb_root hist;
308
309struct hist_entry {
310 struct rb_node rb_node;
311
312 struct thread *thread;
313 struct map *map;
314 struct dso *dso;
315 struct symbol *sym;
316 uint64_t ip;
317 char level;
318
319 uint32_t count;
320};
321
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200322/*
323 * configurable sorting bits
324 */
325
326struct sort_entry {
327 struct list_head list;
328
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200329 char *header;
330
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200331 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200332 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200333 size_t (*print)(FILE *fp, struct hist_entry *);
334};
335
Peter Zijlstra82292892009-06-03 12:37:36 +0200336/* --sort pid */
337
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200338static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200339sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
340{
341 return right->thread->pid - left->thread->pid;
342}
343
344static size_t
345sort__thread_print(FILE *fp, struct hist_entry *self)
346{
Ingo Molnarcf25c632009-06-02 22:12:14 +0200347 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200348}
349
350static struct sort_entry sort_thread = {
Ingo Molnarcf25c632009-06-02 22:12:14 +0200351 .header = " Command: Pid ",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200352 .cmp = sort__thread_cmp,
353 .print = sort__thread_print,
354};
355
Peter Zijlstra82292892009-06-03 12:37:36 +0200356/* --sort comm */
357
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200358static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200359sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
360{
Peter Zijlstra82292892009-06-03 12:37:36 +0200361 return right->thread->pid - left->thread->pid;
362}
363
364static int64_t
365sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
366{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200367 char *comm_l = left->thread->comm;
368 char *comm_r = right->thread->comm;
369
370 if (!comm_l || !comm_r) {
371 if (!comm_l && !comm_r)
372 return 0;
373 else if (!comm_l)
374 return -1;
375 else
376 return 1;
377 }
378
379 return strcmp(comm_l, comm_r);
380}
381
382static size_t
383sort__comm_print(FILE *fp, struct hist_entry *self)
384{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200385 return fprintf(fp, " %16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200386}
387
388static struct sort_entry sort_comm = {
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200389 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200390 .cmp = sort__comm_cmp,
391 .collapse = sort__comm_collapse,
392 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200393};
394
Peter Zijlstra82292892009-06-03 12:37:36 +0200395/* --sort dso */
396
Peter Zijlstra992444b2009-05-27 20:20:27 +0200397static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200398sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
399{
400 struct dso *dso_l = left->dso;
401 struct dso *dso_r = right->dso;
402
403 if (!dso_l || !dso_r) {
404 if (!dso_l && !dso_r)
405 return 0;
406 else if (!dso_l)
407 return -1;
408 else
409 return 1;
410 }
411
412 return strcmp(dso_l->name, dso_r->name);
413}
414
415static size_t
416sort__dso_print(FILE *fp, struct hist_entry *self)
417{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200418 if (self->dso)
419 return fprintf(fp, " %-25s", self->dso->name);
420
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200421 return fprintf(fp, " %016llx ", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200422}
423
424static struct sort_entry sort_dso = {
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200425 .header = " Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200426 .cmp = sort__dso_cmp,
427 .print = sort__dso_print,
428};
429
Peter Zijlstra82292892009-06-03 12:37:36 +0200430/* --sort symbol */
431
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200432static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200433sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300434{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200435 uint64_t ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200436
437 if (left->sym == right->sym)
438 return 0;
439
440 ip_l = left->sym ? left->sym->start : left->ip;
441 ip_r = right->sym ? right->sym->start : right->ip;
442
443 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300444}
445
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200446static size_t
447sort__sym_print(FILE *fp, struct hist_entry *self)
448{
449 size_t ret = 0;
450
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200451 if (verbose)
Ingo Molnar0a520c62009-06-02 23:24:45 +0200452 ret += fprintf(fp, " %#018llx", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200453
Ingo Molnar0a520c62009-06-02 23:24:45 +0200454 if (self->sym)
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200455 ret += fprintf(fp, " %s", self->sym->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200456 else
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200457 ret += fprintf(fp, " %#016llx", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200458
459 return ret;
460}
461
462static struct sort_entry sort_sym = {
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200463 .header = " Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200464 .cmp = sort__sym_cmp,
465 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200466};
467
Peter Zijlstra82292892009-06-03 12:37:36 +0200468static int sort__need_collapse = 0;
469
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200470struct sort_dimension {
471 char *name;
472 struct sort_entry *entry;
473 int taken;
474};
475
476static struct sort_dimension sort_dimensions[] = {
477 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200478 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200479 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200480 { .name = "symbol", .entry = &sort_sym, },
481};
482
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200483static LIST_HEAD(hist_entry__sort_list);
484
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200485static int sort_dimension__add(char *tok)
486{
487 int i;
488
489 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
490 struct sort_dimension *sd = &sort_dimensions[i];
491
492 if (sd->taken)
493 continue;
494
Ingo Molnar5352f352009-06-03 10:07:39 +0200495 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200496 continue;
497
Peter Zijlstra82292892009-06-03 12:37:36 +0200498 if (sd->entry->collapse)
499 sort__need_collapse = 1;
500
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200501 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
502 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200503
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200504 return 0;
505 }
506
507 return -ESRCH;
508}
509
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200510static int64_t
511hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
512{
513 struct sort_entry *se;
514 int64_t cmp = 0;
515
516 list_for_each_entry(se, &hist_entry__sort_list, list) {
517 cmp = se->cmp(left, right);
518 if (cmp)
519 break;
520 }
521
522 return cmp;
523}
524
Peter Zijlstra82292892009-06-03 12:37:36 +0200525static int64_t
526hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
527{
528 struct sort_entry *se;
529 int64_t cmp = 0;
530
531 list_for_each_entry(se, &hist_entry__sort_list, list) {
532 int64_t (*f)(struct hist_entry *, struct hist_entry *);
533
534 f = se->collapse ?: se->cmp;
535
536 cmp = f(left, right);
537 if (cmp)
538 break;
539 }
540
541 return cmp;
542}
543
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200544static size_t
545hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
546{
547 struct sort_entry *se;
548 size_t ret;
549
550 if (total_samples) {
Ingo Molnare98e96f2009-06-03 19:30:38 +0200551 ret = fprintf(fp, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200552 (self->count * 100.0) / total_samples);
553 } else
554 ret = fprintf(fp, "%12d ", self->count);
555
556 list_for_each_entry(se, &hist_entry__sort_list, list)
557 ret += se->print(fp, self);
558
559 ret += fprintf(fp, "\n");
560
561 return ret;
562}
563
564/*
565 * collect histogram counts
566 */
567
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200568static int
569hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
570 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300571{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200572 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300573 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200574 struct hist_entry *he;
575 struct hist_entry entry = {
576 .thread = thread,
577 .map = map,
578 .dso = dso,
579 .sym = sym,
580 .ip = ip,
581 .level = level,
582 .count = 1,
583 };
584 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300585
586 while (*p != NULL) {
587 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200588 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300589
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200590 cmp = hist_entry__cmp(&entry, he);
591
592 if (!cmp) {
593 he->count++;
594 return 0;
595 }
596
597 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300598 p = &(*p)->rb_left;
599 else
600 p = &(*p)->rb_right;
601 }
602
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200603 he = malloc(sizeof(*he));
604 if (!he)
605 return -ENOMEM;
606 *he = entry;
607 rb_link_node(&he->rb_node, parent, p);
608 rb_insert_color(&he->rb_node, &hist);
609
610 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300611}
612
Peter Zijlstra82292892009-06-03 12:37:36 +0200613static void hist_entry__free(struct hist_entry *he)
614{
615 free(he);
616}
617
618/*
619 * collapse the histogram
620 */
621
622static struct rb_root collapse_hists;
623
624static void collapse__insert_entry(struct hist_entry *he)
625{
626 struct rb_node **p = &collapse_hists.rb_node;
627 struct rb_node *parent = NULL;
628 struct hist_entry *iter;
629 int64_t cmp;
630
631 while (*p != NULL) {
632 parent = *p;
633 iter = rb_entry(parent, struct hist_entry, rb_node);
634
635 cmp = hist_entry__collapse(iter, he);
636
637 if (!cmp) {
638 iter->count += he->count;
639 hist_entry__free(he);
640 return;
641 }
642
643 if (cmp < 0)
644 p = &(*p)->rb_left;
645 else
646 p = &(*p)->rb_right;
647 }
648
649 rb_link_node(&he->rb_node, parent, p);
650 rb_insert_color(&he->rb_node, &collapse_hists);
651}
652
653static void collapse__resort(void)
654{
655 struct rb_node *next;
656 struct hist_entry *n;
657
658 if (!sort__need_collapse)
659 return;
660
661 next = rb_first(&hist);
662 while (next) {
663 n = rb_entry(next, struct hist_entry, rb_node);
664 next = rb_next(&n->rb_node);
665
666 rb_erase(&n->rb_node, &hist);
667 collapse__insert_entry(n);
668 }
669}
670
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200671/*
672 * reverse the map, sort on count.
673 */
674
675static struct rb_root output_hists;
676
677static void output__insert_entry(struct hist_entry *he)
678{
679 struct rb_node **p = &output_hists.rb_node;
680 struct rb_node *parent = NULL;
681 struct hist_entry *iter;
682
683 while (*p != NULL) {
684 parent = *p;
685 iter = rb_entry(parent, struct hist_entry, rb_node);
686
687 if (he->count > iter->count)
688 p = &(*p)->rb_left;
689 else
690 p = &(*p)->rb_right;
691 }
692
693 rb_link_node(&he->rb_node, parent, p);
694 rb_insert_color(&he->rb_node, &output_hists);
695}
696
697static void output__resort(void)
698{
Peter Zijlstra82292892009-06-03 12:37:36 +0200699 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200700 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300701 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200702
Peter Zijlstra82292892009-06-03 12:37:36 +0200703 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300704 tree = &collapse_hists;
705
706 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +0200707
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200708 while (next) {
709 n = rb_entry(next, struct hist_entry, rb_node);
710 next = rb_next(&n->rb_node);
711
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300712 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200713 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300714 }
715}
716
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200717static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300718{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200719 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200720 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300721 struct rb_node *nd;
722 size_t ret = 0;
723
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200724 fprintf(fp, "#\n");
725
726 fprintf(fp, "# Overhead");
727 list_for_each_entry(se, &hist_entry__sort_list, list)
728 fprintf(fp, " %s", se->header);
729 fprintf(fp, "\n");
730
731 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200732 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200733 int i;
734
Ingo Molnar4593bba2009-06-02 15:34:25 +0200735 fprintf(fp, " ");
736 for (i = 0; i < strlen(se->header)-1; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200737 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200738 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200739 fprintf(fp, "\n");
740
741 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200742
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200743 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
744 pos = rb_entry(nd, struct hist_entry, rb_node);
745 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300746 }
747
Ingo Molnarbd741372009-06-04 14:13:04 +0200748 if (!strcmp(sort_order, default_sort_order)) {
749 fprintf(fp, "#\n");
750 fprintf(fp, "# ( For more details, try: perf report --sort comm,dso,symbol )\n");
751 fprintf(fp, "#\n");
752 }
753
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300754 return ret;
755}
756
Peter Zijlstra436224a2009-06-02 21:02:36 +0200757static void register_idle_thread(void)
758{
759 struct thread *thread = threads__findnew(0);
760
761 if (thread == NULL ||
762 thread__set_comm(thread, "[idle]")) {
763 fprintf(stderr, "problem inserting idle task.\n");
764 exit(-1);
765 }
766}
767
Ingo Molnard80d3382009-06-03 23:14:49 +0200768static unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200769
Ingo Molnard80d3382009-06-03 23:14:49 +0200770static int
Ingo Molnar75051722009-06-03 23:14:49 +0200771process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
772{
773 char level;
774 int show = 0;
775 struct dso *dso = NULL;
776 struct thread *thread = threads__findnew(event->ip.pid);
777 uint64_t ip = event->ip.ip;
778 struct map *map = NULL;
779
780 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
781 (void *)(offset + head),
782 (void *)(long)(event->header.size),
783 event->header.misc,
784 event->ip.pid,
785 (void *)(long)ip);
786
787 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
788
789 if (thread == NULL) {
790 fprintf(stderr, "problem processing %d event, skipping it.\n",
791 event->header.type);
792 return -1;
793 }
794
795 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
796 show = SHOW_KERNEL;
797 level = 'k';
798
799 dso = kernel_dso;
800
801 dprintf(" ...... dso: %s\n", dso->name);
802
803 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
804
805 show = SHOW_USER;
806 level = '.';
807
808 map = thread__find_map(thread, ip);
809 if (map != NULL) {
810 dso = map->dso;
811 ip -= map->start + map->pgoff;
812 } else {
813 /*
814 * If this is outside of all known maps,
815 * and is a negative address, try to look it
816 * up in the kernel dso, as it might be a
817 * vsyscall (which executes in user-mode):
818 */
819 if ((long long)ip < 0)
820 dso = kernel_dso;
821 }
822 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
823
824 } else {
825 show = SHOW_HV;
826 level = 'H';
827 dprintf(" ...... dso: [hypervisor]\n");
828 }
829
830 if (show & show_mask) {
831 struct symbol *sym = dso__find_symbol(dso, ip);
832
833 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
834 fprintf(stderr,
835 "problem incrementing symbol count, skipping event\n");
836 return -1;
837 }
838 }
839 total++;
840
841 return 0;
842}
843
844static int
845process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
846{
847 struct thread *thread = threads__findnew(event->mmap.pid);
848 struct map *map = map__new(&event->mmap);
849
850 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
851 (void *)(offset + head),
852 (void *)(long)(event->header.size),
853 (void *)(long)event->mmap.start,
854 (void *)(long)event->mmap.len,
855 (void *)(long)event->mmap.pgoff,
856 event->mmap.filename);
857
858 if (thread == NULL || map == NULL) {
859 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +0200860 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +0200861 }
862
863 thread__insert_map(thread, map);
864 total_mmap++;
865
866 return 0;
867}
868
869static int
870process_comm_event(event_t *event, unsigned long offset, unsigned long head)
871{
872 struct thread *thread = threads__findnew(event->comm.pid);
873
874 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
875 (void *)(offset + head),
876 (void *)(long)(event->header.size),
877 event->comm.comm, event->comm.pid);
878
879 if (thread == NULL ||
880 thread__set_comm(thread, event->comm.comm)) {
881 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
882 return -1;
883 }
884 total_comm++;
885
886 return 0;
887}
888
889static int
Ingo Molnard80d3382009-06-03 23:14:49 +0200890process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300891{
Ingo Molnar75051722009-06-03 23:14:49 +0200892 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
893 return process_overflow_event(event, offset, head);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300894
Ingo Molnar75051722009-06-03 23:14:49 +0200895 switch (event->header.type) {
896 case PERF_EVENT_MMAP:
897 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200898
Ingo Molnar75051722009-06-03 23:14:49 +0200899 case PERF_EVENT_COMM:
900 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +0200901
Ingo Molnard11444d2009-06-03 23:29:14 +0200902 /*
903 * We dont process them right now but they are fine:
904 */
905 case PERF_EVENT_MUNMAP:
906 case PERF_EVENT_PERIOD:
907 case PERF_EVENT_THROTTLE:
908 case PERF_EVENT_UNTHROTTLE:
909 return 0;
910
Ingo Molnard80d3382009-06-03 23:14:49 +0200911 default:
912 return -1;
913 }
914
915 return 0;
916}
917
918static int __cmd_report(void)
919{
Ingo Molnar75051722009-06-03 23:14:49 +0200920 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +0200921 unsigned long offset = 0;
922 unsigned long head = 0;
923 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +0200924 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +0200925 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +0200926 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +0200927
928 register_idle_thread();
929
930 input = open(input_name, O_RDONLY);
931 if (input < 0) {
932 perror("failed to open file");
933 exit(-1);
934 }
935
936 ret = fstat(input, &stat);
937 if (ret < 0) {
938 perror("failed to stat file");
939 exit(-1);
940 }
941
942 if (!stat.st_size) {
943 fprintf(stderr, "zero-sized file, nothing to do!\n");
944 exit(0);
945 }
946
947 if (load_kernel() < 0) {
948 perror("failed to load kernel symbols");
949 return EXIT_FAILURE;
950 }
951
952 if (!full_paths) {
953 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
954 perror("failed to get the current directory");
955 return EXIT_FAILURE;
956 }
957 cwdlen = strlen(cwd);
958 } else {
959 cwd = NULL;
960 cwdlen = 0;
961 }
962remap:
963 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
964 MAP_SHARED, input, offset);
965 if (buf == MAP_FAILED) {
966 perror("failed to mmap file");
967 exit(-1);
968 }
969
970more:
971 event = (event_t *)(buf + head);
972
973 size = event->header.size;
974 if (!size)
975 size = 8;
976
977 if (head + event->header.size >= page_size * mmap_window) {
978 unsigned long shift = page_size * (head / page_size);
979 int ret;
980
981 ret = munmap(buf, page_size * mmap_window);
982 assert(ret == 0);
983
984 offset += shift;
985 head -= shift;
986 goto remap;
987 }
988
989 size = event->header.size;
990
991 if (!size || process_event(event, offset, head) < 0) {
992
Ingo Molnar35029732009-06-03 09:38:58 +0200993 dprintf("%p [%p]: skipping unknown header type: %d\n",
994 (void *)(offset + head),
995 (void *)(long)(event->header.size),
996 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200997
Ingo Molnar3e706112009-05-26 18:53:17 +0200998 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200999
1000 /*
1001 * assume we lost track of the stream, check alignment, and
1002 * increment a single u64 in the hope to catch on again 'soon'.
1003 */
1004
1005 if (unlikely(head & 7))
1006 head &= ~7ULL;
1007
1008 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001009 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001010
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001011 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001012
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001013 if (offset + head < stat.st_size)
1014 goto more;
1015
1016 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001017 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001018
Ingo Molnar35029732009-06-03 09:38:58 +02001019 dprintf(" IP events: %10ld\n", total);
1020 dprintf(" mmap events: %10ld\n", total_mmap);
1021 dprintf(" comm events: %10ld\n", total_comm);
1022 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001023
Ingo Molnar35029732009-06-03 09:38:58 +02001024 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001025 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001026
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001027 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001028 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001029
Peter Zijlstra82292892009-06-03 12:37:36 +02001030 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001031 output__resort();
1032 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001033
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001034 return rc;
1035}
1036
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001037static const char * const report_usage[] = {
1038 "perf report [<options>] <command>",
1039 NULL
1040};
1041
1042static const struct option options[] = {
1043 OPT_STRING('i', "input", &input_name, "file",
1044 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001045 OPT_BOOLEAN('v', "verbose", &verbose,
1046 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001047 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1048 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001049 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001050 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1051 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001052 OPT_BOOLEAN('P', "full-paths", &full_paths,
1053 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001054 OPT_END()
1055};
1056
Ingo Molnar5352f352009-06-03 10:07:39 +02001057static void setup_sorting(void)
1058{
1059 char *tmp, *tok, *str = strdup(sort_order);
1060
1061 for (tok = strtok_r(str, ", ", &tmp);
1062 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1063 if (sort_dimension__add(tok) < 0) {
1064 error("Unknown --sort key: `%s'", tok);
1065 usage_with_options(report_usage, options);
1066 }
1067 }
1068
1069 free(str);
1070}
1071
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001072int cmd_report(int argc, const char **argv, const char *prefix)
1073{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001074 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001075
1076 page_size = getpagesize();
1077
1078 parse_options(argc, argv, options, report_usage, 0);
1079
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001080 setup_sorting();
1081
Ingo Molnara930d2c2009-05-27 09:50:13 +02001082 setup_pager();
1083
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001084 return __cmd_report();
1085}