blob: b4e76f75ba87f41c06d5bc74b048d1b84d629b56 [file] [log] [blame]
Ingo Molnarbf9e1872009-06-02 23:37:05 +02001/*
2 * builtin-report.c
3 *
4 * Builtin report command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
Ingo Molnar16f762a2009-05-27 09:10:38 +02008#include "builtin.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02009
Ingo Molnarbf9e1872009-06-02 23:37:05 +020010#include "util/util.h"
11
Ingo Molnar8fc03212009-06-04 15:19:47 +020012#include "util/color.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030013#include "util/list.h"
Ingo Molnara930d2c2009-05-27 09:50:13 +020014#include "util/cache.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030015#include "util/rbtree.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030016#include "util/symbol.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030017#include "util/string.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030018
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020019#include "perf.h"
20
21#include "util/parse-options.h"
22#include "util/parse-events.h"
23
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030024#define SHOW_KERNEL 1
25#define SHOW_USER 2
26#define SHOW_HV 4
27
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020028static char const *input_name = "perf.data";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020029static char *vmlinux = NULL;
Ingo Molnarbd741372009-06-04 14:13:04 +020030
31static char default_sort_order[] = "comm,dso";
32static char *sort_order = default_sort_order;
33
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030034static int input;
35static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
36
Ingo Molnar97b07b62009-05-26 18:48:58 +020037static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020038#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020039#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
Ingo Molnar35029732009-06-03 09:38:58 +020040
Ingo Molnar16f762a2009-05-27 09:10:38 +020041static int verbose;
Ingo Molnar75220602009-06-18 08:00:17 +020042#define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
43
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030044static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020045
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030046static unsigned long page_size;
47static unsigned long mmap_window = 32;
48
Ingo Molnarb8e6d822009-06-18 14:32:19 +020049static char default_parent_pattern[] = "^sys_|^do_page_fault";
50static char *parent_pattern = default_parent_pattern;
Ingo Molnarb25bcf22009-06-18 07:01:03 +020051static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020052
Ingo Molnarb8e6d822009-06-18 14:32:19 +020053static int exclude_other = 1;
54
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030055struct ip_event {
56 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100057 u64 ip;
58 u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020059 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030060};
Ingo Molnar75051722009-06-03 23:14:49 +020061
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +020062struct ip_callchain {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100063 u64 nr;
64 u64 ips[0];
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +020065};
66
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030067struct mmap_event {
68 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100069 u32 pid, tid;
70 u64 start;
71 u64 len;
72 u64 pgoff;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030073 char filename[PATH_MAX];
74};
Ingo Molnar75051722009-06-03 23:14:49 +020075
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030076struct comm_event {
77 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100078 u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030079 char comm[16];
80};
81
Peter Zijlstra62fc4452009-06-04 16:53:49 +020082struct fork_event {
83 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100084 u32 pid, ppid;
Peter Zijlstra62fc4452009-06-04 16:53:49 +020085};
86
Ingo Molnarb2fef072009-06-05 18:07:51 +020087struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030088 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100089 u64 time;
90 u64 id;
91 u64 sample_period;
Ingo Molnarb2fef072009-06-05 18:07:51 +020092};
93
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020094struct lost_event {
95 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100096 u64 id;
97 u64 lost;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020098};
99
Ingo Molnarb2fef072009-06-05 18:07:51 +0200100typedef union event_union {
101 struct perf_event_header header;
102 struct ip_event ip;
103 struct mmap_event mmap;
104 struct comm_event comm;
105 struct fork_event fork;
106 struct period_event period;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200107 struct lost_event lost;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300108} event_t;
109
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300110static LIST_HEAD(dsos);
111static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200112static struct dso *vdso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300113
114static void dsos__add(struct dso *dso)
115{
116 list_add_tail(&dso->node, &dsos);
117}
118
119static struct dso *dsos__find(const char *name)
120{
121 struct dso *pos;
122
123 list_for_each_entry(pos, &dsos, node)
124 if (strcmp(pos->name, name) == 0)
125 return pos;
126 return NULL;
127}
128
129static struct dso *dsos__findnew(const char *name)
130{
131 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200132 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300133
Ingo Molnar4593bba2009-06-02 15:34:25 +0200134 if (dso)
135 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300136
Ingo Molnar4593bba2009-06-02 15:34:25 +0200137 dso = dso__new(name, 0);
138 if (!dso)
139 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200140
Ingo Molnarbd741372009-06-04 14:13:04 +0200141 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200142 if (nr < 0) {
Ingo Molnar75220602009-06-18 08:00:17 +0200143 eprintf("Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200144 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300145 }
Ingo Molnar75220602009-06-18 08:00:17 +0200146 if (!nr)
147 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200148
149 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300150
151 return dso;
152
153out_delete_dso:
154 dso__delete(dso);
155 return NULL;
156}
157
Ingo Molnar16f762a2009-05-27 09:10:38 +0200158static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300159{
160 struct dso *pos;
161
162 list_for_each_entry(pos, &dsos, node)
163 dso__fprintf(pos, fp);
164}
165
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000166static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200167{
168 return dso__find_symbol(kernel_dso, ip);
169}
170
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200171static int load_kernel(void)
172{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300173 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200174
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300175 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200176 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300177 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200178
Ingo Molnarbd741372009-06-04 14:13:04 +0200179 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300180 if (err) {
181 dso__delete(kernel_dso);
182 kernel_dso = NULL;
183 } else
184 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200185
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200186 vdso = dso__new("[vdso]", 0);
187 if (!vdso)
188 return -1;
189
190 vdso->find_symbol = vdso__find_symbol;
191
192 dsos__add(vdso);
193
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300194 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200195}
196
Ingo Molnard80d3382009-06-03 23:14:49 +0200197static char __cwd[PATH_MAX];
198static char *cwd = __cwd;
199static int cwdlen;
200
201static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300202{
203 int n = 0;
204
205 while (pathname[n] == cwd[n] && n < cwdlen)
206 ++n;
207
208 return n;
209}
210
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300211struct map {
212 struct list_head node;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000213 u64 start;
214 u64 end;
215 u64 pgoff;
216 u64 (*map_ip)(struct map *, u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300217 struct dso *dso;
218};
219
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000220static u64 map__map_ip(struct map *map, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200221{
222 return ip - map->start + map->pgoff;
223}
224
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000225static u64 vdso__map_ip(struct map *map, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200226{
227 return ip;
228}
229
Pekka Enberg80d496b2009-06-08 21:12:48 +0300230static inline int is_anon_memory(const char *filename)
231{
232 return strcmp(filename, "//anon") == 0;
233}
234
Ingo Molnard80d3382009-06-03 23:14:49 +0200235static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300236{
237 struct map *self = malloc(sizeof(*self));
238
239 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300240 const char *filename = event->filename;
241 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300242 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300243
244 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200245 int n = strcommon(filename);
246
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300247 if (n == cwdlen) {
248 snprintf(newfilename, sizeof(newfilename),
249 ".%s", filename + n);
250 filename = newfilename;
251 }
252 }
253
Pekka Enberg80d496b2009-06-08 21:12:48 +0300254 anon = is_anon_memory(filename);
255
256 if (anon) {
257 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
258 filename = newfilename;
259 }
260
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300261 self->start = event->start;
262 self->end = event->start + event->len;
263 self->pgoff = event->pgoff;
264
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300265 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300266 if (self->dso == NULL)
267 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200268
Pekka Enberg80d496b2009-06-08 21:12:48 +0300269 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200270 self->map_ip = vdso__map_ip;
271 else
272 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300273 }
274 return self;
275out_delete:
276 free(self);
277 return NULL;
278}
279
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200280static struct map *map__clone(struct map *self)
281{
282 struct map *map = malloc(sizeof(*self));
283
284 if (!map)
285 return NULL;
286
287 memcpy(map, self, sizeof(*self));
288
289 return map;
290}
291
292static int map__overlap(struct map *l, struct map *r)
293{
294 if (l->start > r->start) {
295 struct map *t = l;
296 l = r;
297 r = t;
298 }
299
300 if (l->end > r->start)
301 return 1;
302
303 return 0;
304}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300305
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300306static size_t map__fprintf(struct map *self, FILE *fp)
307{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200308 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300309 self->start, self->end, self->pgoff, self->dso->name);
310}
311
312
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300313struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300314 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300315 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300316 pid_t pid;
317 char *comm;
318};
319
320static struct thread *thread__new(pid_t pid)
321{
322 struct thread *self = malloc(sizeof(*self));
323
324 if (self != NULL) {
325 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200326 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200327 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200328 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300329 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300330 }
331
332 return self;
333}
334
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300335static int thread__set_comm(struct thread *self, const char *comm)
336{
Peter Zijlstra82292892009-06-03 12:37:36 +0200337 if (self->comm)
338 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300339 self->comm = strdup(comm);
340 return self->comm ? 0 : -ENOMEM;
341}
342
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300343static size_t thread__fprintf(struct thread *self, FILE *fp)
344{
345 struct map *pos;
346 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
347
348 list_for_each_entry(pos, &self->maps, node)
349 ret += map__fprintf(pos, fp);
350
351 return ret;
352}
353
354
Ingo Molnar16f762a2009-05-27 09:10:38 +0200355static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200356static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300357
358static struct thread *threads__findnew(pid_t pid)
359{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300360 struct rb_node **p = &threads.rb_node;
361 struct rb_node *parent = NULL;
362 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300363
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200364 /*
365 * Font-end cache - PID lookups come in blocks,
366 * so most of the time we dont have to look up
367 * the full rbtree:
368 */
369 if (last_match && last_match->pid == pid)
370 return last_match;
371
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300372 while (*p != NULL) {
373 parent = *p;
374 th = rb_entry(parent, struct thread, rb_node);
375
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200376 if (th->pid == pid) {
377 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300378 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200379 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300380
381 if (pid < th->pid)
382 p = &(*p)->rb_left;
383 else
384 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300385 }
386
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300387 th = thread__new(pid);
388 if (th != NULL) {
389 rb_link_node(&th->rb_node, parent, p);
390 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200391 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300392 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200393
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300394 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300395}
396
397static void thread__insert_map(struct thread *self, struct map *map)
398{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200399 struct map *pos, *tmp;
400
401 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
402 if (map__overlap(pos, map)) {
Peter Zijlstra3d906ef2009-06-23 11:23:07 +0200403 if (verbose >= 2) {
404 printf("overlapping maps:\n");
405 map__fprintf(map, stdout);
406 map__fprintf(pos, stdout);
407 }
408
409 if (map->start <= pos->start && map->end > pos->start)
410 pos->start = map->end;
411
412 if (map->end >= pos->end && map->start < pos->end)
413 pos->end = map->start;
414
415 if (verbose >= 2) {
416 printf("after collision:\n");
417 map__fprintf(pos, stdout);
418 }
419
420 if (pos->start >= pos->end) {
421 list_del_init(&pos->node);
422 free(pos);
423 }
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200424 }
425 }
426
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300427 list_add_tail(&map->node, &self->maps);
428}
429
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200430static int thread__fork(struct thread *self, struct thread *parent)
431{
432 struct map *map;
433
434 if (self->comm)
435 free(self->comm);
436 self->comm = strdup(parent->comm);
437 if (!self->comm)
438 return -ENOMEM;
439
440 list_for_each_entry(map, &parent->maps, node) {
441 struct map *new = map__clone(map);
442 if (!new)
443 return -ENOMEM;
444 thread__insert_map(self, new);
445 }
446
447 return 0;
448}
449
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000450static struct map *thread__find_map(struct thread *self, u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300451{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200452 struct map *pos;
453
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300454 if (self == NULL)
455 return NULL;
456
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300457 list_for_each_entry(pos, &self->maps, node)
458 if (ip >= pos->start && ip <= pos->end)
459 return pos;
460
461 return NULL;
462}
463
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300464static size_t threads__fprintf(FILE *fp)
465{
466 size_t ret = 0;
467 struct rb_node *nd;
468
469 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
470 struct thread *pos = rb_entry(nd, struct thread, rb_node);
471
472 ret += thread__fprintf(pos, fp);
473 }
474
475 return ret;
476}
477
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200478/*
479 * histogram, sorted on item, collects counts
480 */
481
482static struct rb_root hist;
483
484struct hist_entry {
485 struct rb_node rb_node;
486
487 struct thread *thread;
488 struct map *map;
489 struct dso *dso;
490 struct symbol *sym;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200491 struct symbol *parent;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000492 u64 ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200493 char level;
494
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000495 u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200496};
497
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200498/*
499 * configurable sorting bits
500 */
501
502struct sort_entry {
503 struct list_head list;
504
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200505 char *header;
506
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200507 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200508 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200509 size_t (*print)(FILE *fp, struct hist_entry *);
510};
511
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200512static int64_t cmp_null(void *l, void *r)
513{
514 if (!l && !r)
515 return 0;
516 else if (!l)
517 return -1;
518 else
519 return 1;
520}
521
Peter Zijlstra82292892009-06-03 12:37:36 +0200522/* --sort pid */
523
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200524static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200525sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
526{
527 return right->thread->pid - left->thread->pid;
528}
529
530static size_t
531sort__thread_print(FILE *fp, struct hist_entry *self)
532{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200533 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200534}
535
536static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200537 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200538 .cmp = sort__thread_cmp,
539 .print = sort__thread_print,
540};
541
Peter Zijlstra82292892009-06-03 12:37:36 +0200542/* --sort comm */
543
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200544static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200545sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
546{
Peter Zijlstra82292892009-06-03 12:37:36 +0200547 return right->thread->pid - left->thread->pid;
548}
549
550static int64_t
551sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
552{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200553 char *comm_l = left->thread->comm;
554 char *comm_r = right->thread->comm;
555
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200556 if (!comm_l || !comm_r)
557 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200558
559 return strcmp(comm_l, comm_r);
560}
561
562static size_t
563sort__comm_print(FILE *fp, struct hist_entry *self)
564{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200565 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200566}
567
568static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200569 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200570 .cmp = sort__comm_cmp,
571 .collapse = sort__comm_collapse,
572 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200573};
574
Peter Zijlstra82292892009-06-03 12:37:36 +0200575/* --sort dso */
576
Peter Zijlstra992444b2009-05-27 20:20:27 +0200577static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200578sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
579{
580 struct dso *dso_l = left->dso;
581 struct dso *dso_r = right->dso;
582
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200583 if (!dso_l || !dso_r)
584 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200585
586 return strcmp(dso_l->name, dso_r->name);
587}
588
589static size_t
590sort__dso_print(FILE *fp, struct hist_entry *self)
591{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200592 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200593 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200594
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000595 return fprintf(fp, "%016llx ", (u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200596}
597
598static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200599 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200600 .cmp = sort__dso_cmp,
601 .print = sort__dso_print,
602};
603
Peter Zijlstra82292892009-06-03 12:37:36 +0200604/* --sort symbol */
605
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200606static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200607sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300608{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000609 u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200610
611 if (left->sym == right->sym)
612 return 0;
613
614 ip_l = left->sym ? left->sym->start : left->ip;
615 ip_r = right->sym ? right->sym->start : right->ip;
616
617 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300618}
619
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200620static size_t
621sort__sym_print(FILE *fp, struct hist_entry *self)
622{
623 size_t ret = 0;
624
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200625 if (verbose)
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000626 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200627
Ingo Molnar8edd4282009-06-05 14:13:18 +0200628 if (self->sym) {
629 ret += fprintf(fp, "[%c] %s",
630 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
631 } else {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000632 ret += fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200633 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200634
635 return ret;
636}
637
638static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200639 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200640 .cmp = sort__sym_cmp,
641 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200642};
643
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200644/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200645
646static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200647sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200648{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200649 struct symbol *sym_l = left->parent;
650 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200651
652 if (!sym_l || !sym_r)
653 return cmp_null(sym_l, sym_r);
654
655 return strcmp(sym_l->name, sym_r->name);
656}
657
658static size_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200659sort__parent_print(FILE *fp, struct hist_entry *self)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200660{
661 size_t ret = 0;
662
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200663 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200664
665 return ret;
666}
667
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200668static struct sort_entry sort_parent = {
669 .header = "Parent symbol ",
670 .cmp = sort__parent_cmp,
671 .print = sort__parent_print,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200672};
673
Peter Zijlstra82292892009-06-03 12:37:36 +0200674static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200675static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200676
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200677struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200678 char *name;
679 struct sort_entry *entry;
680 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200681};
682
683static struct sort_dimension sort_dimensions[] = {
684 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200685 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200686 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200687 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200688 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200689};
690
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200691static LIST_HEAD(hist_entry__sort_list);
692
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200693static int sort_dimension__add(char *tok)
694{
695 int i;
696
697 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
698 struct sort_dimension *sd = &sort_dimensions[i];
699
700 if (sd->taken)
701 continue;
702
Ingo Molnar5352f352009-06-03 10:07:39 +0200703 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200704 continue;
705
Peter Zijlstra82292892009-06-03 12:37:36 +0200706 if (sd->entry->collapse)
707 sort__need_collapse = 1;
708
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200709 if (sd->entry == &sort_parent) {
710 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200711 if (ret) {
712 char err[BUFSIZ];
713
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200714 regerror(ret, &parent_regex, err, sizeof(err));
715 fprintf(stderr, "Invalid regex: %s\n%s",
716 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200717 exit(-1);
718 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200719 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200720 }
721
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200722 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
723 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200724
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200725 return 0;
726 }
727
728 return -ESRCH;
729}
730
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200731static int64_t
732hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
733{
734 struct sort_entry *se;
735 int64_t cmp = 0;
736
737 list_for_each_entry(se, &hist_entry__sort_list, list) {
738 cmp = se->cmp(left, right);
739 if (cmp)
740 break;
741 }
742
743 return cmp;
744}
745
Peter Zijlstra82292892009-06-03 12:37:36 +0200746static int64_t
747hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
748{
749 struct sort_entry *se;
750 int64_t cmp = 0;
751
752 list_for_each_entry(se, &hist_entry__sort_list, list) {
753 int64_t (*f)(struct hist_entry *, struct hist_entry *);
754
755 f = se->collapse ?: se->cmp;
756
757 cmp = f(left, right);
758 if (cmp)
759 break;
760 }
761
762 return cmp;
763}
764
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200765static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000766hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200767{
768 struct sort_entry *se;
769 size_t ret;
770
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200771 if (exclude_other && !self->parent)
772 return 0;
773
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200774 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200775 double percent = self->count * 100.0 / total_samples;
776 char *color = PERF_COLOR_NORMAL;
777
778 /*
Ingo Molnaraefcf372009-06-08 23:15:28 +0200779 * We color high-overhead entries in red, mid-overhead
780 * entries in green - and keep the low overhead places
781 * normal:
Ingo Molnar8fc03212009-06-04 15:19:47 +0200782 */
Ingo Molnaraefcf372009-06-08 23:15:28 +0200783 if (percent >= 5.0) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200784 color = PERF_COLOR_RED;
Ingo Molnaraefcf372009-06-08 23:15:28 +0200785 } else {
786 if (percent >= 0.5)
787 color = PERF_COLOR_GREEN;
788 }
Ingo Molnar8fc03212009-06-04 15:19:47 +0200789
790 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200791 (self->count * 100.0) / total_samples);
792 } else
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200793 ret = fprintf(fp, "%12Ld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200794
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200795 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200796 if (exclude_other && (se == &sort_parent))
797 continue;
798
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200799 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200800 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200801 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200802
803 ret += fprintf(fp, "\n");
804
805 return ret;
806}
807
808/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200809 *
810 */
811
812static struct symbol *
813resolve_symbol(struct thread *thread, struct map **mapp,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000814 struct dso **dsop, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200815{
816 struct dso *dso = dsop ? *dsop : NULL;
817 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200818 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200819
820 if (!thread)
821 return NULL;
822
823 if (dso)
824 goto got_dso;
825
826 if (map)
827 goto got_map;
828
829 map = thread__find_map(thread, ip);
830 if (map != NULL) {
831 if (mapp)
832 *mapp = map;
833got_map:
834 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200835
836 dso = map->dso;
837 } else {
838 /*
839 * If this is outside of all known maps,
840 * and is a negative address, try to look it
841 * up in the kernel dso, as it might be a
842 * vsyscall (which executes in user-mode):
843 */
844 if ((long long)ip < 0)
845 dso = kernel_dso;
846 }
847 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200848 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
849 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200850
851 if (dsop)
852 *dsop = dso;
853
854 if (!dso)
855 return NULL;
856got_dso:
857 return dso->find_symbol(dso, ip);
858}
859
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200860static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200861{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200862 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200863 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200864
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200865 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200866}
867
868/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200869 * collect histogram counts
870 */
871
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200872static int
873hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000874 struct symbol *sym, u64 ip, struct ip_callchain *chain,
875 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300876{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200877 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300878 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200879 struct hist_entry *he;
880 struct hist_entry entry = {
881 .thread = thread,
882 .map = map,
883 .dso = dso,
884 .sym = sym,
885 .ip = ip,
886 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200887 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200888 .parent = NULL,
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200889 };
890 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300891
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200892 if (sort__has_parent && chain) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000893 u64 context = PERF_CONTEXT_MAX;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200894 int i;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200895
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200896 for (i = 0; i < chain->nr; i++) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000897 u64 ip = chain->ips[i];
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200898 struct dso *dso = NULL;
899 struct symbol *sym;
900
901 if (ip >= PERF_CONTEXT_MAX) {
902 context = ip;
903 continue;
904 }
905
906 switch (context) {
907 case PERF_CONTEXT_KERNEL:
908 dso = kernel_dso;
909 break;
910 default:
911 break;
912 }
913
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200914 sym = resolve_symbol(thread, NULL, &dso, &ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200915
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200916 if (sym && call__match(sym)) {
917 entry.parent = sym;
918 break;
919 }
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200920 }
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200921 }
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200922
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300923 while (*p != NULL) {
924 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200925 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300926
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200927 cmp = hist_entry__cmp(&entry, he);
928
929 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200930 he->count += count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200931 return 0;
932 }
933
934 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300935 p = &(*p)->rb_left;
936 else
937 p = &(*p)->rb_right;
938 }
939
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200940 he = malloc(sizeof(*he));
941 if (!he)
942 return -ENOMEM;
943 *he = entry;
944 rb_link_node(&he->rb_node, parent, p);
945 rb_insert_color(&he->rb_node, &hist);
946
947 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300948}
949
Peter Zijlstra82292892009-06-03 12:37:36 +0200950static void hist_entry__free(struct hist_entry *he)
951{
952 free(he);
953}
954
955/*
956 * collapse the histogram
957 */
958
959static struct rb_root collapse_hists;
960
961static void collapse__insert_entry(struct hist_entry *he)
962{
963 struct rb_node **p = &collapse_hists.rb_node;
964 struct rb_node *parent = NULL;
965 struct hist_entry *iter;
966 int64_t cmp;
967
968 while (*p != NULL) {
969 parent = *p;
970 iter = rb_entry(parent, struct hist_entry, rb_node);
971
972 cmp = hist_entry__collapse(iter, he);
973
974 if (!cmp) {
975 iter->count += he->count;
976 hist_entry__free(he);
977 return;
978 }
979
980 if (cmp < 0)
981 p = &(*p)->rb_left;
982 else
983 p = &(*p)->rb_right;
984 }
985
986 rb_link_node(&he->rb_node, parent, p);
987 rb_insert_color(&he->rb_node, &collapse_hists);
988}
989
990static void collapse__resort(void)
991{
992 struct rb_node *next;
993 struct hist_entry *n;
994
995 if (!sort__need_collapse)
996 return;
997
998 next = rb_first(&hist);
999 while (next) {
1000 n = rb_entry(next, struct hist_entry, rb_node);
1001 next = rb_next(&n->rb_node);
1002
1003 rb_erase(&n->rb_node, &hist);
1004 collapse__insert_entry(n);
1005 }
1006}
1007
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001008/*
1009 * reverse the map, sort on count.
1010 */
1011
1012static struct rb_root output_hists;
1013
1014static void output__insert_entry(struct hist_entry *he)
1015{
1016 struct rb_node **p = &output_hists.rb_node;
1017 struct rb_node *parent = NULL;
1018 struct hist_entry *iter;
1019
1020 while (*p != NULL) {
1021 parent = *p;
1022 iter = rb_entry(parent, struct hist_entry, rb_node);
1023
1024 if (he->count > iter->count)
1025 p = &(*p)->rb_left;
1026 else
1027 p = &(*p)->rb_right;
1028 }
1029
1030 rb_link_node(&he->rb_node, parent, p);
1031 rb_insert_color(&he->rb_node, &output_hists);
1032}
1033
1034static void output__resort(void)
1035{
Peter Zijlstra82292892009-06-03 12:37:36 +02001036 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001037 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001038 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001039
Peter Zijlstra82292892009-06-03 12:37:36 +02001040 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001041 tree = &collapse_hists;
1042
1043 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001044
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001045 while (next) {
1046 n = rb_entry(next, struct hist_entry, rb_node);
1047 next = rb_next(&n->rb_node);
1048
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001049 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001050 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001051 }
1052}
1053
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001054static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001055{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001056 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001057 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001058 struct rb_node *nd;
1059 size_t ret = 0;
1060
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001061 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001062 fprintf(fp, "#\n");
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001063 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001064 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001065
1066 fprintf(fp, "# Overhead");
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001067 list_for_each_entry(se, &hist_entry__sort_list, list) {
1068 if (exclude_other && (se == &sort_parent))
1069 continue;
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001070 fprintf(fp, " %s", se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001071 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001072 fprintf(fp, "\n");
1073
1074 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001075 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001076 int i;
1077
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001078 if (exclude_other && (se == &sort_parent))
1079 continue;
1080
Ingo Molnar4593bba2009-06-02 15:34:25 +02001081 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001082 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001083 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001084 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001085 fprintf(fp, "\n");
1086
1087 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001088
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001089 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1090 pos = rb_entry(nd, struct hist_entry, rb_node);
1091 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001092 }
1093
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001094 if (sort_order == default_sort_order &&
1095 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001096 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001097 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001098 fprintf(fp, "#\n");
1099 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001100 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001101
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001102 return ret;
1103}
1104
Peter Zijlstra436224a2009-06-02 21:02:36 +02001105static void register_idle_thread(void)
1106{
1107 struct thread *thread = threads__findnew(0);
1108
1109 if (thread == NULL ||
1110 thread__set_comm(thread, "[idle]")) {
1111 fprintf(stderr, "problem inserting idle task.\n");
1112 exit(-1);
1113 }
1114}
1115
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001116static unsigned long total = 0,
1117 total_mmap = 0,
1118 total_comm = 0,
1119 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001120 total_unknown = 0,
1121 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001122
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001123static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +02001124{
1125 unsigned int chain_size;
1126
Ingo Molnar75220602009-06-18 08:00:17 +02001127 chain_size = event->header.size;
1128 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1129
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001130 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +02001131 return -1;
1132
1133 return 0;
1134}
1135
Ingo Molnard80d3382009-06-03 23:14:49 +02001136static int
Ingo Molnar75051722009-06-03 23:14:49 +02001137process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1138{
1139 char level;
1140 int show = 0;
1141 struct dso *dso = NULL;
1142 struct thread *thread = threads__findnew(event->ip.pid);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001143 u64 ip = event->ip.ip;
1144 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001145 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001146 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001147 struct ip_callchain *chain = NULL;
Ingo Molnar75051722009-06-03 23:14:49 +02001148
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001149 if (event->header.type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001150 period = *(u64 *)more_data;
1151 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001152 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001153
Peter Zijlstra4502d772009-06-10 15:03:06 +02001154 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001155 (void *)(offset + head),
1156 (void *)(long)(event->header.size),
1157 event->header.misc,
1158 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001159 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001160 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001161
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001162 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
1163 int i;
1164
1165 chain = (void *)more_data;
1166
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001167 dprintf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001168
Ingo Molnar75220602009-06-18 08:00:17 +02001169 if (validate_chain(chain, event) < 0) {
1170 eprintf("call-chain problem with event, skipping it.\n");
1171 return 0;
1172 }
1173
1174 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001175 for (i = 0; i < chain->nr; i++)
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001176 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001177 }
1178 }
1179
Ingo Molnar75051722009-06-03 23:14:49 +02001180 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1181
1182 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001183 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001184 event->header.type);
1185 return -1;
1186 }
1187
1188 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1189 show = SHOW_KERNEL;
1190 level = 'k';
1191
1192 dso = kernel_dso;
1193
1194 dprintf(" ...... dso: %s\n", dso->name);
1195
1196 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1197
1198 show = SHOW_USER;
1199 level = '.';
1200
Ingo Molnar75051722009-06-03 23:14:49 +02001201 } else {
1202 show = SHOW_HV;
1203 level = 'H';
1204 dprintf(" ...... dso: [hypervisor]\n");
1205 }
1206
1207 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001208 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001209
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001210 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001211 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001212 return -1;
1213 }
1214 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001215 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001216
1217 return 0;
1218}
1219
1220static int
1221process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1222{
1223 struct thread *thread = threads__findnew(event->mmap.pid);
1224 struct map *map = map__new(&event->mmap);
1225
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001226 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001227 (void *)(offset + head),
1228 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001229 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001230 (void *)(long)event->mmap.start,
1231 (void *)(long)event->mmap.len,
1232 (void *)(long)event->mmap.pgoff,
1233 event->mmap.filename);
1234
1235 if (thread == NULL || map == NULL) {
1236 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001237 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001238 }
1239
1240 thread__insert_map(thread, map);
1241 total_mmap++;
1242
1243 return 0;
1244}
1245
1246static int
1247process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1248{
1249 struct thread *thread = threads__findnew(event->comm.pid);
1250
1251 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1252 (void *)(offset + head),
1253 (void *)(long)(event->header.size),
1254 event->comm.comm, event->comm.pid);
1255
1256 if (thread == NULL ||
1257 thread__set_comm(thread, event->comm.comm)) {
1258 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1259 return -1;
1260 }
1261 total_comm++;
1262
1263 return 0;
1264}
1265
1266static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001267process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1268{
1269 struct thread *thread = threads__findnew(event->fork.pid);
1270 struct thread *parent = threads__findnew(event->fork.ppid);
1271
1272 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1273 (void *)(offset + head),
1274 (void *)(long)(event->header.size),
1275 event->fork.pid, event->fork.ppid);
1276
1277 if (!thread || !parent || thread__fork(thread, parent)) {
1278 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1279 return -1;
1280 }
1281 total_fork++;
1282
1283 return 0;
1284}
1285
1286static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001287process_period_event(event_t *event, unsigned long offset, unsigned long head)
1288{
1289 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1290 (void *)(offset + head),
1291 (void *)(long)(event->header.size),
1292 event->period.time,
1293 event->period.id,
1294 event->period.sample_period);
1295
1296 return 0;
1297}
1298
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001299static int
1300process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1301{
1302 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1303 (void *)(offset + head),
1304 (void *)(long)(event->header.size),
1305 event->lost.id,
1306 event->lost.lost);
1307
1308 total_lost += event->lost.lost;
1309
1310 return 0;
1311}
1312
Ingo Molnar8465b052009-06-14 14:44:07 +02001313static void trace_event(event_t *event)
1314{
1315 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001316 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001317 int i, j;
1318
1319 if (!dump_trace)
1320 return;
1321
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001322 dprintf(".");
1323 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001324
1325 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001326 if ((i & 15) == 0) {
1327 dprintf(".");
1328 cdprintf(" %04x: ", i);
1329 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001330
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001331 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001332
1333 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001334 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001335 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001336 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001337 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001338 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001339 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001340 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001341 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001342 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001343 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001344 }
1345 }
1346 dprintf(".\n");
1347}
1348
Ingo Molnarb2fef072009-06-05 18:07:51 +02001349static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001350process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001351{
Ingo Molnar8465b052009-06-14 14:44:07 +02001352 trace_event(event);
1353
Ingo Molnar75051722009-06-03 23:14:49 +02001354 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1355 return process_overflow_event(event, offset, head);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001356
Ingo Molnar75051722009-06-03 23:14:49 +02001357 switch (event->header.type) {
1358 case PERF_EVENT_MMAP:
1359 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001360
Ingo Molnar75051722009-06-03 23:14:49 +02001361 case PERF_EVENT_COMM:
1362 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001363
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001364 case PERF_EVENT_FORK:
1365 return process_fork_event(event, offset, head);
1366
Ingo Molnarb2fef072009-06-05 18:07:51 +02001367 case PERF_EVENT_PERIOD:
1368 return process_period_event(event, offset, head);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001369
1370 case PERF_EVENT_LOST:
1371 return process_lost_event(event, offset, head);
1372
Ingo Molnard11444d2009-06-03 23:29:14 +02001373 /*
1374 * We dont process them right now but they are fine:
1375 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001376
Ingo Molnard11444d2009-06-03 23:29:14 +02001377 case PERF_EVENT_THROTTLE:
1378 case PERF_EVENT_UNTHROTTLE:
1379 return 0;
1380
Ingo Molnard80d3382009-06-03 23:14:49 +02001381 default:
1382 return -1;
1383 }
1384
1385 return 0;
1386}
1387
Peter Zijlstraf5970552009-06-18 23:22:55 +02001388static struct perf_file_header file_header;
1389
Ingo Molnard80d3382009-06-03 23:14:49 +02001390static int __cmd_report(void)
1391{
Ingo Molnar75051722009-06-03 23:14:49 +02001392 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001393 unsigned long offset = 0;
Peter Zijlstraf5970552009-06-18 23:22:55 +02001394 unsigned long head = sizeof(file_header);
Ingo Molnard80d3382009-06-03 23:14:49 +02001395 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001396 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001397 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001398 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001399
1400 register_idle_thread();
1401
1402 input = open(input_name, O_RDONLY);
1403 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001404 fprintf(stderr, " failed to open file: %s", input_name);
1405 if (!strcmp(input_name, "perf.data"))
1406 fprintf(stderr, " (try 'perf record' first)");
1407 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001408 exit(-1);
1409 }
1410
1411 ret = fstat(input, &stat);
1412 if (ret < 0) {
1413 perror("failed to stat file");
1414 exit(-1);
1415 }
1416
1417 if (!stat.st_size) {
1418 fprintf(stderr, "zero-sized file, nothing to do!\n");
1419 exit(0);
1420 }
1421
Frederic Weisbeckereadc84c2009-06-20 02:01:40 +02001422 if (read(input, &file_header, sizeof(file_header)) == -1) {
1423 perror("failed to read file headers");
1424 exit(-1);
1425 }
Peter Zijlstraf5970552009-06-18 23:22:55 +02001426
1427 if (sort__has_parent &&
1428 !(file_header.sample_type & PERF_SAMPLE_CALLCHAIN)) {
1429 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1430 exit(-1);
1431 }
1432
Ingo Molnard80d3382009-06-03 23:14:49 +02001433 if (load_kernel() < 0) {
1434 perror("failed to load kernel symbols");
1435 return EXIT_FAILURE;
1436 }
1437
1438 if (!full_paths) {
1439 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1440 perror("failed to get the current directory");
1441 return EXIT_FAILURE;
1442 }
1443 cwdlen = strlen(cwd);
1444 } else {
1445 cwd = NULL;
1446 cwdlen = 0;
1447 }
1448remap:
1449 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1450 MAP_SHARED, input, offset);
1451 if (buf == MAP_FAILED) {
1452 perror("failed to mmap file");
1453 exit(-1);
1454 }
1455
1456more:
1457 event = (event_t *)(buf + head);
1458
1459 size = event->header.size;
1460 if (!size)
1461 size = 8;
1462
1463 if (head + event->header.size >= page_size * mmap_window) {
1464 unsigned long shift = page_size * (head / page_size);
1465 int ret;
1466
1467 ret = munmap(buf, page_size * mmap_window);
1468 assert(ret == 0);
1469
1470 offset += shift;
1471 head -= shift;
1472 goto remap;
1473 }
1474
1475 size = event->header.size;
1476
Ingo Molnar8465b052009-06-14 14:44:07 +02001477 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001478 (void *)(offset + head),
1479 (void *)(long)event->header.size,
1480 event->header.type);
1481
Ingo Molnard80d3382009-06-03 23:14:49 +02001482 if (!size || process_event(event, offset, head) < 0) {
1483
Ingo Molnar35029732009-06-03 09:38:58 +02001484 dprintf("%p [%p]: skipping unknown header type: %d\n",
1485 (void *)(offset + head),
1486 (void *)(long)(event->header.size),
1487 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001488
Ingo Molnar3e706112009-05-26 18:53:17 +02001489 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001490
1491 /*
1492 * assume we lost track of the stream, check alignment, and
1493 * increment a single u64 in the hope to catch on again 'soon'.
1494 */
1495
1496 if (unlikely(head & 7))
1497 head &= ~7ULL;
1498
1499 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001500 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001501
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001502 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001503
Peter Zijlstraf5970552009-06-18 23:22:55 +02001504 if (offset + head >= sizeof(file_header) + file_header.data_size)
1505 goto done;
1506
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001507 if (offset + head < stat.st_size)
1508 goto more;
1509
Peter Zijlstraf5970552009-06-18 23:22:55 +02001510done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001511 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001512 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001513
Ingo Molnar35029732009-06-03 09:38:58 +02001514 dprintf(" IP events: %10ld\n", total);
1515 dprintf(" mmap events: %10ld\n", total_mmap);
1516 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001517 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001518 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001519 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001520
Ingo Molnar35029732009-06-03 09:38:58 +02001521 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001522 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001523
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001524 if (verbose >= 3)
1525 threads__fprintf(stdout);
1526
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001527 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001528 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001529
Peter Zijlstra82292892009-06-03 12:37:36 +02001530 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001531 output__resort();
1532 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001533
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001534 return rc;
1535}
1536
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001537static const char * const report_usage[] = {
1538 "perf report [<options>] <command>",
1539 NULL
1540};
1541
1542static const struct option options[] = {
1543 OPT_STRING('i', "input", &input_name, "file",
1544 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001545 OPT_BOOLEAN('v', "verbose", &verbose,
1546 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001547 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1548 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001549 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001550 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001551 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001552 OPT_BOOLEAN('P', "full-paths", &full_paths,
1553 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001554 OPT_STRING('p', "parent", &parent_pattern, "regex",
1555 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001556 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1557 "Only display entries with parent-match"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001558 OPT_END()
1559};
1560
Ingo Molnar5352f352009-06-03 10:07:39 +02001561static void setup_sorting(void)
1562{
1563 char *tmp, *tok, *str = strdup(sort_order);
1564
1565 for (tok = strtok_r(str, ", ", &tmp);
1566 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1567 if (sort_dimension__add(tok) < 0) {
1568 error("Unknown --sort key: `%s'", tok);
1569 usage_with_options(report_usage, options);
1570 }
1571 }
1572
1573 free(str);
1574}
1575
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001576int cmd_report(int argc, const char **argv, const char *prefix)
1577{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001578 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001579
1580 page_size = getpagesize();
1581
Ingo Molnaredc52de2009-06-04 16:24:37 +02001582 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001583
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001584 setup_sorting();
1585
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001586 if (parent_pattern != default_parent_pattern)
1587 sort_dimension__add("parent");
1588 else
1589 exclude_other = 0;
1590
Ingo Molnaredc52de2009-06-04 16:24:37 +02001591 /*
1592 * Any (unrecognized) arguments left?
1593 */
1594 if (argc)
1595 usage_with_options(report_usage, options);
1596
Ingo Molnara930d2c2009-05-27 09:50:13 +02001597 setup_pager();
1598
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001599 return __cmd_report();
1600}