blob: 56c664d1b628196e539138d24e24b6579a95f4b4 [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;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +020029static char *sort_order = "comm,dso";
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030030static int input;
31static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
32
Ingo Molnar97b07b62009-05-26 18:48:58 +020033static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020034#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
35
Ingo Molnar16f762a2009-05-27 09:10:38 +020036static int verbose;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030037static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020038
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030039static unsigned long page_size;
40static unsigned long mmap_window = 32;
41
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020042const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030043 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
44 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
45 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
46};
47
48struct ip_event {
49 struct perf_event_header header;
50 __u64 ip;
51 __u32 pid, tid;
52};
Ingo Molnar75051722009-06-03 23:14:49 +020053
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030054struct mmap_event {
55 struct perf_event_header header;
56 __u32 pid, tid;
57 __u64 start;
58 __u64 len;
59 __u64 pgoff;
60 char filename[PATH_MAX];
61};
Ingo Molnar75051722009-06-03 23:14:49 +020062
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030063struct comm_event {
64 struct perf_event_header header;
Ingo Molnar75051722009-06-03 23:14:49 +020065 __u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030066 char comm[16];
67};
68
69typedef union event_union {
70 struct perf_event_header header;
71 struct ip_event ip;
72 struct mmap_event mmap;
73 struct comm_event comm;
74} event_t;
75
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030076static LIST_HEAD(dsos);
77static struct dso *kernel_dso;
78
79static void dsos__add(struct dso *dso)
80{
81 list_add_tail(&dso->node, &dsos);
82}
83
84static struct dso *dsos__find(const char *name)
85{
86 struct dso *pos;
87
88 list_for_each_entry(pos, &dsos, node)
89 if (strcmp(pos->name, name) == 0)
90 return pos;
91 return NULL;
92}
93
94static struct dso *dsos__findnew(const char *name)
95{
96 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +020097 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030098
Ingo Molnar4593bba2009-06-02 15:34:25 +020099 if (dso)
100 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300101
Ingo Molnar4593bba2009-06-02 15:34:25 +0200102 dso = dso__new(name, 0);
103 if (!dso)
104 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200105
Ingo Molnar4593bba2009-06-02 15:34:25 +0200106 nr = dso__load(dso, NULL);
107 if (nr < 0) {
108 fprintf(stderr, "Failed to open: %s\n", name);
109 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300110 }
Ingo Molnar4593bba2009-06-02 15:34:25 +0200111 if (!nr && verbose) {
112 fprintf(stderr,
113 "No symbols found in: %s, maybe install a debug package?\n",
114 name);
115 }
116
117 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300118
119 return dso;
120
121out_delete_dso:
122 dso__delete(dso);
123 return NULL;
124}
125
Ingo Molnar16f762a2009-05-27 09:10:38 +0200126static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300127{
128 struct dso *pos;
129
130 list_for_each_entry(pos, &dsos, node)
131 dso__fprintf(pos, fp);
132}
133
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200134static int load_kernel(void)
135{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300136 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200137
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300138 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200139 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300140 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200141
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300142 err = dso__load_kernel(kernel_dso, vmlinux, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300143 if (err) {
144 dso__delete(kernel_dso);
145 kernel_dso = NULL;
146 } else
147 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200148
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300149 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200150}
151
Ingo Molnard80d3382009-06-03 23:14:49 +0200152static char __cwd[PATH_MAX];
153static char *cwd = __cwd;
154static int cwdlen;
155
156static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300157{
158 int n = 0;
159
160 while (pathname[n] == cwd[n] && n < cwdlen)
161 ++n;
162
163 return n;
164}
165
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300166struct map {
167 struct list_head node;
168 uint64_t start;
169 uint64_t end;
170 uint64_t pgoff;
171 struct dso *dso;
172};
173
Ingo Molnard80d3382009-06-03 23:14:49 +0200174static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300175{
176 struct map *self = malloc(sizeof(*self));
177
178 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300179 const char *filename = event->filename;
180 char newfilename[PATH_MAX];
181
182 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200183 int n = strcommon(filename);
184
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300185 if (n == cwdlen) {
186 snprintf(newfilename, sizeof(newfilename),
187 ".%s", filename + n);
188 filename = newfilename;
189 }
190 }
191
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300192 self->start = event->start;
193 self->end = event->start + event->len;
194 self->pgoff = event->pgoff;
195
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300196 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300197 if (self->dso == NULL)
198 goto out_delete;
199 }
200 return self;
201out_delete:
202 free(self);
203 return NULL;
204}
205
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300206struct thread;
207
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300208struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300209 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300210 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300211 pid_t pid;
212 char *comm;
213};
214
215static struct thread *thread__new(pid_t pid)
216{
217 struct thread *self = malloc(sizeof(*self));
218
219 if (self != NULL) {
220 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200221 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200222 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200223 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300224 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300225 }
226
227 return self;
228}
229
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300230static int thread__set_comm(struct thread *self, const char *comm)
231{
Peter Zijlstra82292892009-06-03 12:37:36 +0200232 if (self->comm)
233 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300234 self->comm = strdup(comm);
235 return self->comm ? 0 : -ENOMEM;
236}
237
Ingo Molnar16f762a2009-05-27 09:10:38 +0200238static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200239static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300240
241static struct thread *threads__findnew(pid_t pid)
242{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300243 struct rb_node **p = &threads.rb_node;
244 struct rb_node *parent = NULL;
245 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300246
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200247 /*
248 * Font-end cache - PID lookups come in blocks,
249 * so most of the time we dont have to look up
250 * the full rbtree:
251 */
252 if (last_match && last_match->pid == pid)
253 return last_match;
254
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300255 while (*p != NULL) {
256 parent = *p;
257 th = rb_entry(parent, struct thread, rb_node);
258
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200259 if (th->pid == pid) {
260 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300261 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200262 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300263
264 if (pid < th->pid)
265 p = &(*p)->rb_left;
266 else
267 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300268 }
269
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300270 th = thread__new(pid);
271 if (th != NULL) {
272 rb_link_node(&th->rb_node, parent, p);
273 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200274 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300275 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200276
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300277 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300278}
279
280static void thread__insert_map(struct thread *self, struct map *map)
281{
282 list_add_tail(&map->node, &self->maps);
283}
284
285static struct map *thread__find_map(struct thread *self, uint64_t ip)
286{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200287 struct map *pos;
288
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300289 if (self == NULL)
290 return NULL;
291
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300292 list_for_each_entry(pos, &self->maps, node)
293 if (ip >= pos->start && ip <= pos->end)
294 return pos;
295
296 return NULL;
297}
298
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200299/*
300 * histogram, sorted on item, collects counts
301 */
302
303static struct rb_root hist;
304
305struct hist_entry {
306 struct rb_node rb_node;
307
308 struct thread *thread;
309 struct map *map;
310 struct dso *dso;
311 struct symbol *sym;
312 uint64_t ip;
313 char level;
314
315 uint32_t count;
316};
317
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200318/*
319 * configurable sorting bits
320 */
321
322struct sort_entry {
323 struct list_head list;
324
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200325 char *header;
326
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200327 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200328 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200329 size_t (*print)(FILE *fp, struct hist_entry *);
330};
331
Peter Zijlstra82292892009-06-03 12:37:36 +0200332/* --sort pid */
333
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200334static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200335sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
336{
337 return right->thread->pid - left->thread->pid;
338}
339
340static size_t
341sort__thread_print(FILE *fp, struct hist_entry *self)
342{
Ingo Molnarcf25c632009-06-02 22:12:14 +0200343 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200344}
345
346static struct sort_entry sort_thread = {
Ingo Molnarcf25c632009-06-02 22:12:14 +0200347 .header = " Command: Pid ",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200348 .cmp = sort__thread_cmp,
349 .print = sort__thread_print,
350};
351
Peter Zijlstra82292892009-06-03 12:37:36 +0200352/* --sort comm */
353
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200354static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200355sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
356{
Peter Zijlstra82292892009-06-03 12:37:36 +0200357 return right->thread->pid - left->thread->pid;
358}
359
360static int64_t
361sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
362{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200363 char *comm_l = left->thread->comm;
364 char *comm_r = right->thread->comm;
365
366 if (!comm_l || !comm_r) {
367 if (!comm_l && !comm_r)
368 return 0;
369 else if (!comm_l)
370 return -1;
371 else
372 return 1;
373 }
374
375 return strcmp(comm_l, comm_r);
376}
377
378static size_t
379sort__comm_print(FILE *fp, struct hist_entry *self)
380{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200381 return fprintf(fp, " %16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200382}
383
384static struct sort_entry sort_comm = {
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200385 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200386 .cmp = sort__comm_cmp,
387 .collapse = sort__comm_collapse,
388 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200389};
390
Peter Zijlstra82292892009-06-03 12:37:36 +0200391/* --sort dso */
392
Peter Zijlstra992444b2009-05-27 20:20:27 +0200393static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200394sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
395{
396 struct dso *dso_l = left->dso;
397 struct dso *dso_r = right->dso;
398
399 if (!dso_l || !dso_r) {
400 if (!dso_l && !dso_r)
401 return 0;
402 else if (!dso_l)
403 return -1;
404 else
405 return 1;
406 }
407
408 return strcmp(dso_l->name, dso_r->name);
409}
410
411static size_t
412sort__dso_print(FILE *fp, struct hist_entry *self)
413{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200414 if (self->dso)
415 return fprintf(fp, " %-25s", self->dso->name);
416
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200417 return fprintf(fp, " %016llx ", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200418}
419
420static struct sort_entry sort_dso = {
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200421 .header = " Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200422 .cmp = sort__dso_cmp,
423 .print = sort__dso_print,
424};
425
Peter Zijlstra82292892009-06-03 12:37:36 +0200426/* --sort symbol */
427
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200428static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200429sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300430{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200431 uint64_t ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200432
433 if (left->sym == right->sym)
434 return 0;
435
436 ip_l = left->sym ? left->sym->start : left->ip;
437 ip_r = right->sym ? right->sym->start : right->ip;
438
439 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300440}
441
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200442static size_t
443sort__sym_print(FILE *fp, struct hist_entry *self)
444{
445 size_t ret = 0;
446
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200447 if (verbose)
Ingo Molnar0a520c62009-06-02 23:24:45 +0200448 ret += fprintf(fp, " %#018llx", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200449
Ingo Molnar0a520c62009-06-02 23:24:45 +0200450 if (self->sym)
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200451 ret += fprintf(fp, " %s", self->sym->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200452 else
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200453 ret += fprintf(fp, " %#016llx", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200454
455 return ret;
456}
457
458static struct sort_entry sort_sym = {
Peter Zijlstra95ed6fd2009-06-04 15:00:45 +0200459 .header = " Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200460 .cmp = sort__sym_cmp,
461 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200462};
463
Peter Zijlstra82292892009-06-03 12:37:36 +0200464static int sort__need_collapse = 0;
465
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200466struct sort_dimension {
467 char *name;
468 struct sort_entry *entry;
469 int taken;
470};
471
472static struct sort_dimension sort_dimensions[] = {
473 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200474 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200475 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200476 { .name = "symbol", .entry = &sort_sym, },
477};
478
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200479static LIST_HEAD(hist_entry__sort_list);
480
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200481static int sort_dimension__add(char *tok)
482{
483 int i;
484
485 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
486 struct sort_dimension *sd = &sort_dimensions[i];
487
488 if (sd->taken)
489 continue;
490
Ingo Molnar5352f352009-06-03 10:07:39 +0200491 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200492 continue;
493
Peter Zijlstra82292892009-06-03 12:37:36 +0200494 if (sd->entry->collapse)
495 sort__need_collapse = 1;
496
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200497 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
498 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200499
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200500 return 0;
501 }
502
503 return -ESRCH;
504}
505
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200506static int64_t
507hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
508{
509 struct sort_entry *se;
510 int64_t cmp = 0;
511
512 list_for_each_entry(se, &hist_entry__sort_list, list) {
513 cmp = se->cmp(left, right);
514 if (cmp)
515 break;
516 }
517
518 return cmp;
519}
520
Peter Zijlstra82292892009-06-03 12:37:36 +0200521static int64_t
522hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
523{
524 struct sort_entry *se;
525 int64_t cmp = 0;
526
527 list_for_each_entry(se, &hist_entry__sort_list, list) {
528 int64_t (*f)(struct hist_entry *, struct hist_entry *);
529
530 f = se->collapse ?: se->cmp;
531
532 cmp = f(left, right);
533 if (cmp)
534 break;
535 }
536
537 return cmp;
538}
539
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200540static size_t
541hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
542{
543 struct sort_entry *se;
544 size_t ret;
545
546 if (total_samples) {
Ingo Molnare98e96f2009-06-03 19:30:38 +0200547 ret = fprintf(fp, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200548 (self->count * 100.0) / total_samples);
549 } else
550 ret = fprintf(fp, "%12d ", self->count);
551
552 list_for_each_entry(se, &hist_entry__sort_list, list)
553 ret += se->print(fp, self);
554
555 ret += fprintf(fp, "\n");
556
557 return ret;
558}
559
560/*
561 * collect histogram counts
562 */
563
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200564static int
565hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
566 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300567{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200568 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300569 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200570 struct hist_entry *he;
571 struct hist_entry entry = {
572 .thread = thread,
573 .map = map,
574 .dso = dso,
575 .sym = sym,
576 .ip = ip,
577 .level = level,
578 .count = 1,
579 };
580 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300581
582 while (*p != NULL) {
583 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200584 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300585
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200586 cmp = hist_entry__cmp(&entry, he);
587
588 if (!cmp) {
589 he->count++;
590 return 0;
591 }
592
593 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300594 p = &(*p)->rb_left;
595 else
596 p = &(*p)->rb_right;
597 }
598
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200599 he = malloc(sizeof(*he));
600 if (!he)
601 return -ENOMEM;
602 *he = entry;
603 rb_link_node(&he->rb_node, parent, p);
604 rb_insert_color(&he->rb_node, &hist);
605
606 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300607}
608
Peter Zijlstra82292892009-06-03 12:37:36 +0200609static void hist_entry__free(struct hist_entry *he)
610{
611 free(he);
612}
613
614/*
615 * collapse the histogram
616 */
617
618static struct rb_root collapse_hists;
619
620static void collapse__insert_entry(struct hist_entry *he)
621{
622 struct rb_node **p = &collapse_hists.rb_node;
623 struct rb_node *parent = NULL;
624 struct hist_entry *iter;
625 int64_t cmp;
626
627 while (*p != NULL) {
628 parent = *p;
629 iter = rb_entry(parent, struct hist_entry, rb_node);
630
631 cmp = hist_entry__collapse(iter, he);
632
633 if (!cmp) {
634 iter->count += he->count;
635 hist_entry__free(he);
636 return;
637 }
638
639 if (cmp < 0)
640 p = &(*p)->rb_left;
641 else
642 p = &(*p)->rb_right;
643 }
644
645 rb_link_node(&he->rb_node, parent, p);
646 rb_insert_color(&he->rb_node, &collapse_hists);
647}
648
649static void collapse__resort(void)
650{
651 struct rb_node *next;
652 struct hist_entry *n;
653
654 if (!sort__need_collapse)
655 return;
656
657 next = rb_first(&hist);
658 while (next) {
659 n = rb_entry(next, struct hist_entry, rb_node);
660 next = rb_next(&n->rb_node);
661
662 rb_erase(&n->rb_node, &hist);
663 collapse__insert_entry(n);
664 }
665}
666
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200667/*
668 * reverse the map, sort on count.
669 */
670
671static struct rb_root output_hists;
672
673static void output__insert_entry(struct hist_entry *he)
674{
675 struct rb_node **p = &output_hists.rb_node;
676 struct rb_node *parent = NULL;
677 struct hist_entry *iter;
678
679 while (*p != NULL) {
680 parent = *p;
681 iter = rb_entry(parent, struct hist_entry, rb_node);
682
683 if (he->count > iter->count)
684 p = &(*p)->rb_left;
685 else
686 p = &(*p)->rb_right;
687 }
688
689 rb_link_node(&he->rb_node, parent, p);
690 rb_insert_color(&he->rb_node, &output_hists);
691}
692
693static void output__resort(void)
694{
Peter Zijlstra82292892009-06-03 12:37:36 +0200695 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200696 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300697 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200698
Peter Zijlstra82292892009-06-03 12:37:36 +0200699 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300700 tree = &collapse_hists;
701
702 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +0200703
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200704 while (next) {
705 n = rb_entry(next, struct hist_entry, rb_node);
706 next = rb_next(&n->rb_node);
707
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300708 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200709 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300710 }
711}
712
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200713static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300714{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200715 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200716 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300717 struct rb_node *nd;
718 size_t ret = 0;
719
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200720 fprintf(fp, "#\n");
721
722 fprintf(fp, "# Overhead");
723 list_for_each_entry(se, &hist_entry__sort_list, list)
724 fprintf(fp, " %s", se->header);
725 fprintf(fp, "\n");
726
727 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200728 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200729 int i;
730
Ingo Molnar4593bba2009-06-02 15:34:25 +0200731 fprintf(fp, " ");
732 for (i = 0; i < strlen(se->header)-1; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200733 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200734 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200735 fprintf(fp, "\n");
736
737 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200738
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200739 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
740 pos = rb_entry(nd, struct hist_entry, rb_node);
741 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300742 }
743
744 return ret;
745}
746
Peter Zijlstra436224a2009-06-02 21:02:36 +0200747static void register_idle_thread(void)
748{
749 struct thread *thread = threads__findnew(0);
750
751 if (thread == NULL ||
752 thread__set_comm(thread, "[idle]")) {
753 fprintf(stderr, "problem inserting idle task.\n");
754 exit(-1);
755 }
756}
757
Ingo Molnard80d3382009-06-03 23:14:49 +0200758static unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200759
Ingo Molnard80d3382009-06-03 23:14:49 +0200760static int
Ingo Molnar75051722009-06-03 23:14:49 +0200761process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
762{
763 char level;
764 int show = 0;
765 struct dso *dso = NULL;
766 struct thread *thread = threads__findnew(event->ip.pid);
767 uint64_t ip = event->ip.ip;
768 struct map *map = NULL;
769
770 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
771 (void *)(offset + head),
772 (void *)(long)(event->header.size),
773 event->header.misc,
774 event->ip.pid,
775 (void *)(long)ip);
776
777 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
778
779 if (thread == NULL) {
780 fprintf(stderr, "problem processing %d event, skipping it.\n",
781 event->header.type);
782 return -1;
783 }
784
785 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
786 show = SHOW_KERNEL;
787 level = 'k';
788
789 dso = kernel_dso;
790
791 dprintf(" ...... dso: %s\n", dso->name);
792
793 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
794
795 show = SHOW_USER;
796 level = '.';
797
798 map = thread__find_map(thread, ip);
799 if (map != NULL) {
800 dso = map->dso;
801 ip -= map->start + map->pgoff;
802 } else {
803 /*
804 * If this is outside of all known maps,
805 * and is a negative address, try to look it
806 * up in the kernel dso, as it might be a
807 * vsyscall (which executes in user-mode):
808 */
809 if ((long long)ip < 0)
810 dso = kernel_dso;
811 }
812 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
813
814 } else {
815 show = SHOW_HV;
816 level = 'H';
817 dprintf(" ...... dso: [hypervisor]\n");
818 }
819
820 if (show & show_mask) {
821 struct symbol *sym = dso__find_symbol(dso, ip);
822
823 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
824 fprintf(stderr,
825 "problem incrementing symbol count, skipping event\n");
826 return -1;
827 }
828 }
829 total++;
830
831 return 0;
832}
833
834static int
835process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
836{
837 struct thread *thread = threads__findnew(event->mmap.pid);
838 struct map *map = map__new(&event->mmap);
839
840 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
841 (void *)(offset + head),
842 (void *)(long)(event->header.size),
843 (void *)(long)event->mmap.start,
844 (void *)(long)event->mmap.len,
845 (void *)(long)event->mmap.pgoff,
846 event->mmap.filename);
847
848 if (thread == NULL || map == NULL) {
849 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +0200850 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +0200851 }
852
853 thread__insert_map(thread, map);
854 total_mmap++;
855
856 return 0;
857}
858
859static int
860process_comm_event(event_t *event, unsigned long offset, unsigned long head)
861{
862 struct thread *thread = threads__findnew(event->comm.pid);
863
864 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
865 (void *)(offset + head),
866 (void *)(long)(event->header.size),
867 event->comm.comm, event->comm.pid);
868
869 if (thread == NULL ||
870 thread__set_comm(thread, event->comm.comm)) {
871 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
872 return -1;
873 }
874 total_comm++;
875
876 return 0;
877}
878
879static int
Ingo Molnard80d3382009-06-03 23:14:49 +0200880process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300881{
Ingo Molnar75051722009-06-03 23:14:49 +0200882 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
883 return process_overflow_event(event, offset, head);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300884
Ingo Molnar75051722009-06-03 23:14:49 +0200885 switch (event->header.type) {
886 case PERF_EVENT_MMAP:
887 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200888
Ingo Molnar75051722009-06-03 23:14:49 +0200889 case PERF_EVENT_COMM:
890 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +0200891
Ingo Molnard11444d2009-06-03 23:29:14 +0200892 /*
893 * We dont process them right now but they are fine:
894 */
895 case PERF_EVENT_MUNMAP:
896 case PERF_EVENT_PERIOD:
897 case PERF_EVENT_THROTTLE:
898 case PERF_EVENT_UNTHROTTLE:
899 return 0;
900
Ingo Molnard80d3382009-06-03 23:14:49 +0200901 default:
902 return -1;
903 }
904
905 return 0;
906}
907
908static int __cmd_report(void)
909{
Ingo Molnar75051722009-06-03 23:14:49 +0200910 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +0200911 unsigned long offset = 0;
912 unsigned long head = 0;
913 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +0200914 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +0200915 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +0200916 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +0200917
918 register_idle_thread();
919
920 input = open(input_name, O_RDONLY);
921 if (input < 0) {
922 perror("failed to open file");
923 exit(-1);
924 }
925
926 ret = fstat(input, &stat);
927 if (ret < 0) {
928 perror("failed to stat file");
929 exit(-1);
930 }
931
932 if (!stat.st_size) {
933 fprintf(stderr, "zero-sized file, nothing to do!\n");
934 exit(0);
935 }
936
937 if (load_kernel() < 0) {
938 perror("failed to load kernel symbols");
939 return EXIT_FAILURE;
940 }
941
942 if (!full_paths) {
943 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
944 perror("failed to get the current directory");
945 return EXIT_FAILURE;
946 }
947 cwdlen = strlen(cwd);
948 } else {
949 cwd = NULL;
950 cwdlen = 0;
951 }
952remap:
953 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
954 MAP_SHARED, input, offset);
955 if (buf == MAP_FAILED) {
956 perror("failed to mmap file");
957 exit(-1);
958 }
959
960more:
961 event = (event_t *)(buf + head);
962
963 size = event->header.size;
964 if (!size)
965 size = 8;
966
967 if (head + event->header.size >= page_size * mmap_window) {
968 unsigned long shift = page_size * (head / page_size);
969 int ret;
970
971 ret = munmap(buf, page_size * mmap_window);
972 assert(ret == 0);
973
974 offset += shift;
975 head -= shift;
976 goto remap;
977 }
978
979 size = event->header.size;
980
981 if (!size || process_event(event, offset, head) < 0) {
982
Ingo Molnar35029732009-06-03 09:38:58 +0200983 dprintf("%p [%p]: skipping unknown header type: %d\n",
984 (void *)(offset + head),
985 (void *)(long)(event->header.size),
986 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200987
Ingo Molnar3e706112009-05-26 18:53:17 +0200988 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200989
990 /*
991 * assume we lost track of the stream, check alignment, and
992 * increment a single u64 in the hope to catch on again 'soon'.
993 */
994
995 if (unlikely(head & 7))
996 head &= ~7ULL;
997
998 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200999 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001000
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001001 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001002
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001003 if (offset + head < stat.st_size)
1004 goto more;
1005
1006 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001007 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001008
Ingo Molnar35029732009-06-03 09:38:58 +02001009 dprintf(" IP events: %10ld\n", total);
1010 dprintf(" mmap events: %10ld\n", total_mmap);
1011 dprintf(" comm events: %10ld\n", total_comm);
1012 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001013
Ingo Molnar35029732009-06-03 09:38:58 +02001014 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001015 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001016
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001017 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001018 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001019
Peter Zijlstra82292892009-06-03 12:37:36 +02001020 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001021 output__resort();
1022 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001023
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001024 return rc;
1025}
1026
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001027static const char * const report_usage[] = {
1028 "perf report [<options>] <command>",
1029 NULL
1030};
1031
1032static const struct option options[] = {
1033 OPT_STRING('i', "input", &input_name, "file",
1034 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001035 OPT_BOOLEAN('v', "verbose", &verbose,
1036 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001037 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1038 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001039 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001040 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1041 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001042 OPT_BOOLEAN('P', "full-paths", &full_paths,
1043 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001044 OPT_END()
1045};
1046
Ingo Molnar5352f352009-06-03 10:07:39 +02001047static void setup_sorting(void)
1048{
1049 char *tmp, *tok, *str = strdup(sort_order);
1050
1051 for (tok = strtok_r(str, ", ", &tmp);
1052 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1053 if (sort_dimension__add(tok) < 0) {
1054 error("Unknown --sort key: `%s'", tok);
1055 usage_with_options(report_usage, options);
1056 }
1057 }
1058
1059 free(str);
1060}
1061
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001062int cmd_report(int argc, const char **argv, const char *prefix)
1063{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001064 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001065
1066 page_size = getpagesize();
1067
1068 parse_options(argc, argv, options, report_usage, 0);
1069
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001070 setup_sorting();
1071
Ingo Molnara930d2c2009-05-27 09:50:13 +02001072 setup_pager();
1073
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001074 return __cmd_report();
1075}