blob: fe66895111b1450c725d9789e6064836f59cf024 [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 Molnarb25bcf22009-06-18 07:01:03 +020049static char *parent_pattern = "^sys_|^do_page_fault";
50static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020051
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030052struct ip_event {
53 struct perf_event_header header;
54 __u64 ip;
55 __u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020056 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030057};
Ingo Molnar75051722009-06-03 23:14:49 +020058
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030059struct mmap_event {
60 struct perf_event_header header;
61 __u32 pid, tid;
62 __u64 start;
63 __u64 len;
64 __u64 pgoff;
65 char filename[PATH_MAX];
66};
Ingo Molnar75051722009-06-03 23:14:49 +020067
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030068struct comm_event {
69 struct perf_event_header header;
Ingo Molnar75051722009-06-03 23:14:49 +020070 __u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030071 char comm[16];
72};
73
Peter Zijlstra62fc4452009-06-04 16:53:49 +020074struct fork_event {
75 struct perf_event_header header;
76 __u32 pid, ppid;
77};
78
Ingo Molnarb2fef072009-06-05 18:07:51 +020079struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030080 struct perf_event_header header;
Ingo Molnarb2fef072009-06-05 18:07:51 +020081 __u64 time;
82 __u64 id;
83 __u64 sample_period;
84};
85
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020086struct lost_event {
87 struct perf_event_header header;
88 __u64 id;
89 __u64 lost;
90};
91
Ingo Molnarb2fef072009-06-05 18:07:51 +020092typedef union event_union {
93 struct perf_event_header header;
94 struct ip_event ip;
95 struct mmap_event mmap;
96 struct comm_event comm;
97 struct fork_event fork;
98 struct period_event period;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020099 struct lost_event lost;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300100} event_t;
101
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300102static LIST_HEAD(dsos);
103static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200104static struct dso *vdso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300105
106static void dsos__add(struct dso *dso)
107{
108 list_add_tail(&dso->node, &dsos);
109}
110
111static struct dso *dsos__find(const char *name)
112{
113 struct dso *pos;
114
115 list_for_each_entry(pos, &dsos, node)
116 if (strcmp(pos->name, name) == 0)
117 return pos;
118 return NULL;
119}
120
121static struct dso *dsos__findnew(const char *name)
122{
123 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200124 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300125
Ingo Molnar4593bba2009-06-02 15:34:25 +0200126 if (dso)
127 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300128
Ingo Molnar4593bba2009-06-02 15:34:25 +0200129 dso = dso__new(name, 0);
130 if (!dso)
131 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200132
Ingo Molnarbd741372009-06-04 14:13:04 +0200133 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200134 if (nr < 0) {
Ingo Molnar75220602009-06-18 08:00:17 +0200135 eprintf("Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200136 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300137 }
Ingo Molnar75220602009-06-18 08:00:17 +0200138 if (!nr)
139 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200140
141 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300142
143 return dso;
144
145out_delete_dso:
146 dso__delete(dso);
147 return NULL;
148}
149
Ingo Molnar16f762a2009-05-27 09:10:38 +0200150static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300151{
152 struct dso *pos;
153
154 list_for_each_entry(pos, &dsos, node)
155 dso__fprintf(pos, fp);
156}
157
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200158static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200159{
160 return dso__find_symbol(kernel_dso, ip);
161}
162
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200163static int load_kernel(void)
164{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300165 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200166
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300167 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200168 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300169 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200170
Ingo Molnarbd741372009-06-04 14:13:04 +0200171 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300172 if (err) {
173 dso__delete(kernel_dso);
174 kernel_dso = NULL;
175 } else
176 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200177
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200178 vdso = dso__new("[vdso]", 0);
179 if (!vdso)
180 return -1;
181
182 vdso->find_symbol = vdso__find_symbol;
183
184 dsos__add(vdso);
185
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300186 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200187}
188
Ingo Molnard80d3382009-06-03 23:14:49 +0200189static char __cwd[PATH_MAX];
190static char *cwd = __cwd;
191static int cwdlen;
192
193static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300194{
195 int n = 0;
196
197 while (pathname[n] == cwd[n] && n < cwdlen)
198 ++n;
199
200 return n;
201}
202
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300203struct map {
204 struct list_head node;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200205 __u64 start;
206 __u64 end;
207 __u64 pgoff;
208 __u64 (*map_ip)(struct map *, __u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300209 struct dso *dso;
210};
211
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200212static __u64 map__map_ip(struct map *map, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200213{
214 return ip - map->start + map->pgoff;
215}
216
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200217static __u64 vdso__map_ip(struct map *map, __u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200218{
219 return ip;
220}
221
Pekka Enberg80d496b2009-06-08 21:12:48 +0300222static inline int is_anon_memory(const char *filename)
223{
224 return strcmp(filename, "//anon") == 0;
225}
226
Ingo Molnard80d3382009-06-03 23:14:49 +0200227static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300228{
229 struct map *self = malloc(sizeof(*self));
230
231 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300232 const char *filename = event->filename;
233 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300234 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300235
236 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200237 int n = strcommon(filename);
238
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300239 if (n == cwdlen) {
240 snprintf(newfilename, sizeof(newfilename),
241 ".%s", filename + n);
242 filename = newfilename;
243 }
244 }
245
Pekka Enberg80d496b2009-06-08 21:12:48 +0300246 anon = is_anon_memory(filename);
247
248 if (anon) {
249 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
250 filename = newfilename;
251 }
252
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300253 self->start = event->start;
254 self->end = event->start + event->len;
255 self->pgoff = event->pgoff;
256
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300257 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300258 if (self->dso == NULL)
259 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200260
Pekka Enberg80d496b2009-06-08 21:12:48 +0300261 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200262 self->map_ip = vdso__map_ip;
263 else
264 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300265 }
266 return self;
267out_delete:
268 free(self);
269 return NULL;
270}
271
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200272static struct map *map__clone(struct map *self)
273{
274 struct map *map = malloc(sizeof(*self));
275
276 if (!map)
277 return NULL;
278
279 memcpy(map, self, sizeof(*self));
280
281 return map;
282}
283
284static int map__overlap(struct map *l, struct map *r)
285{
286 if (l->start > r->start) {
287 struct map *t = l;
288 l = r;
289 r = t;
290 }
291
292 if (l->end > r->start)
293 return 1;
294
295 return 0;
296}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300297
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300298static size_t map__fprintf(struct map *self, FILE *fp)
299{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200300 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300301 self->start, self->end, self->pgoff, self->dso->name);
302}
303
304
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300305struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300306 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300307 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300308 pid_t pid;
309 char *comm;
310};
311
312static struct thread *thread__new(pid_t pid)
313{
314 struct thread *self = malloc(sizeof(*self));
315
316 if (self != NULL) {
317 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200318 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200319 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200320 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300321 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300322 }
323
324 return self;
325}
326
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300327static int thread__set_comm(struct thread *self, const char *comm)
328{
Peter Zijlstra82292892009-06-03 12:37:36 +0200329 if (self->comm)
330 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300331 self->comm = strdup(comm);
332 return self->comm ? 0 : -ENOMEM;
333}
334
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300335static size_t thread__fprintf(struct thread *self, FILE *fp)
336{
337 struct map *pos;
338 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
339
340 list_for_each_entry(pos, &self->maps, node)
341 ret += map__fprintf(pos, fp);
342
343 return ret;
344}
345
346
Ingo Molnar16f762a2009-05-27 09:10:38 +0200347static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200348static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300349
350static struct thread *threads__findnew(pid_t pid)
351{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300352 struct rb_node **p = &threads.rb_node;
353 struct rb_node *parent = NULL;
354 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300355
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200356 /*
357 * Font-end cache - PID lookups come in blocks,
358 * so most of the time we dont have to look up
359 * the full rbtree:
360 */
361 if (last_match && last_match->pid == pid)
362 return last_match;
363
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300364 while (*p != NULL) {
365 parent = *p;
366 th = rb_entry(parent, struct thread, rb_node);
367
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200368 if (th->pid == pid) {
369 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300370 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200371 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300372
373 if (pid < th->pid)
374 p = &(*p)->rb_left;
375 else
376 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300377 }
378
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300379 th = thread__new(pid);
380 if (th != NULL) {
381 rb_link_node(&th->rb_node, parent, p);
382 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200383 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300384 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200385
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300386 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300387}
388
389static void thread__insert_map(struct thread *self, struct map *map)
390{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200391 struct map *pos, *tmp;
392
393 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
394 if (map__overlap(pos, map)) {
395 list_del_init(&pos->node);
396 /* XXX leaks dsos */
397 free(pos);
398 }
399 }
400
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300401 list_add_tail(&map->node, &self->maps);
402}
403
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200404static int thread__fork(struct thread *self, struct thread *parent)
405{
406 struct map *map;
407
408 if (self->comm)
409 free(self->comm);
410 self->comm = strdup(parent->comm);
411 if (!self->comm)
412 return -ENOMEM;
413
414 list_for_each_entry(map, &parent->maps, node) {
415 struct map *new = map__clone(map);
416 if (!new)
417 return -ENOMEM;
418 thread__insert_map(self, new);
419 }
420
421 return 0;
422}
423
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200424static struct map *thread__find_map(struct thread *self, __u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300425{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200426 struct map *pos;
427
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300428 if (self == NULL)
429 return NULL;
430
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300431 list_for_each_entry(pos, &self->maps, node)
432 if (ip >= pos->start && ip <= pos->end)
433 return pos;
434
435 return NULL;
436}
437
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300438static size_t threads__fprintf(FILE *fp)
439{
440 size_t ret = 0;
441 struct rb_node *nd;
442
443 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
444 struct thread *pos = rb_entry(nd, struct thread, rb_node);
445
446 ret += thread__fprintf(pos, fp);
447 }
448
449 return ret;
450}
451
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200452/*
453 * histogram, sorted on item, collects counts
454 */
455
456static struct rb_root hist;
457
458struct hist_entry {
459 struct rb_node rb_node;
460
461 struct thread *thread;
462 struct map *map;
463 struct dso *dso;
464 struct symbol *sym;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200465 struct symbol *parent;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200466 __u64 ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200467 char level;
468
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200469 __u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200470};
471
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200472/*
473 * configurable sorting bits
474 */
475
476struct sort_entry {
477 struct list_head list;
478
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200479 char *header;
480
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200481 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200482 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200483 size_t (*print)(FILE *fp, struct hist_entry *);
484};
485
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200486static int64_t cmp_null(void *l, void *r)
487{
488 if (!l && !r)
489 return 0;
490 else if (!l)
491 return -1;
492 else
493 return 1;
494}
495
Peter Zijlstra82292892009-06-03 12:37:36 +0200496/* --sort pid */
497
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200498static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200499sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
500{
501 return right->thread->pid - left->thread->pid;
502}
503
504static size_t
505sort__thread_print(FILE *fp, struct hist_entry *self)
506{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200507 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200508}
509
510static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200511 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200512 .cmp = sort__thread_cmp,
513 .print = sort__thread_print,
514};
515
Peter Zijlstra82292892009-06-03 12:37:36 +0200516/* --sort comm */
517
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200518static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200519sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
520{
Peter Zijlstra82292892009-06-03 12:37:36 +0200521 return right->thread->pid - left->thread->pid;
522}
523
524static int64_t
525sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
526{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200527 char *comm_l = left->thread->comm;
528 char *comm_r = right->thread->comm;
529
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200530 if (!comm_l || !comm_r)
531 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200532
533 return strcmp(comm_l, comm_r);
534}
535
536static size_t
537sort__comm_print(FILE *fp, struct hist_entry *self)
538{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200539 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200540}
541
542static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200543 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200544 .cmp = sort__comm_cmp,
545 .collapse = sort__comm_collapse,
546 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200547};
548
Peter Zijlstra82292892009-06-03 12:37:36 +0200549/* --sort dso */
550
Peter Zijlstra992444b2009-05-27 20:20:27 +0200551static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200552sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
553{
554 struct dso *dso_l = left->dso;
555 struct dso *dso_r = right->dso;
556
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200557 if (!dso_l || !dso_r)
558 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200559
560 return strcmp(dso_l->name, dso_r->name);
561}
562
563static size_t
564sort__dso_print(FILE *fp, struct hist_entry *self)
565{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200566 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200567 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200568
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200569 return fprintf(fp, "%016llx ", (__u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200570}
571
572static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200573 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200574 .cmp = sort__dso_cmp,
575 .print = sort__dso_print,
576};
577
Peter Zijlstra82292892009-06-03 12:37:36 +0200578/* --sort symbol */
579
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200580static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200581sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300582{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200583 __u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200584
585 if (left->sym == right->sym)
586 return 0;
587
588 ip_l = left->sym ? left->sym->start : left->ip;
589 ip_r = right->sym ? right->sym->start : right->ip;
590
591 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300592}
593
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200594static size_t
595sort__sym_print(FILE *fp, struct hist_entry *self)
596{
597 size_t ret = 0;
598
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200599 if (verbose)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200600 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200601
Ingo Molnar8edd4282009-06-05 14:13:18 +0200602 if (self->sym) {
603 ret += fprintf(fp, "[%c] %s",
604 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
605 } else {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200606 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200607 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200608
609 return ret;
610}
611
612static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200613 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200614 .cmp = sort__sym_cmp,
615 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200616};
617
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200618/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200619
620static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200621sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200622{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200623 struct symbol *sym_l = left->parent;
624 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200625
626 if (!sym_l || !sym_r)
627 return cmp_null(sym_l, sym_r);
628
629 return strcmp(sym_l->name, sym_r->name);
630}
631
632static size_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200633sort__parent_print(FILE *fp, struct hist_entry *self)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200634{
635 size_t ret = 0;
636
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200637 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200638
639 return ret;
640}
641
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200642static struct sort_entry sort_parent = {
643 .header = "Parent symbol ",
644 .cmp = sort__parent_cmp,
645 .print = sort__parent_print,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200646};
647
Peter Zijlstra82292892009-06-03 12:37:36 +0200648static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200649static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200650
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200651struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200652 char *name;
653 struct sort_entry *entry;
654 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200655};
656
657static struct sort_dimension sort_dimensions[] = {
658 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200659 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200660 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200661 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200662 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200663};
664
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200665static LIST_HEAD(hist_entry__sort_list);
666
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200667static int sort_dimension__add(char *tok)
668{
669 int i;
670
671 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
672 struct sort_dimension *sd = &sort_dimensions[i];
673
674 if (sd->taken)
675 continue;
676
Ingo Molnar5352f352009-06-03 10:07:39 +0200677 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200678 continue;
679
Peter Zijlstra82292892009-06-03 12:37:36 +0200680 if (sd->entry->collapse)
681 sort__need_collapse = 1;
682
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200683 if (sd->entry == &sort_parent) {
684 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200685 if (ret) {
686 char err[BUFSIZ];
687
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200688 regerror(ret, &parent_regex, err, sizeof(err));
689 fprintf(stderr, "Invalid regex: %s\n%s",
690 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200691 exit(-1);
692 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200693 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200694 }
695
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200696 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
697 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200698
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200699 return 0;
700 }
701
702 return -ESRCH;
703}
704
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200705static int64_t
706hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
707{
708 struct sort_entry *se;
709 int64_t cmp = 0;
710
711 list_for_each_entry(se, &hist_entry__sort_list, list) {
712 cmp = se->cmp(left, right);
713 if (cmp)
714 break;
715 }
716
717 return cmp;
718}
719
Peter Zijlstra82292892009-06-03 12:37:36 +0200720static int64_t
721hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
722{
723 struct sort_entry *se;
724 int64_t cmp = 0;
725
726 list_for_each_entry(se, &hist_entry__sort_list, list) {
727 int64_t (*f)(struct hist_entry *, struct hist_entry *);
728
729 f = se->collapse ?: se->cmp;
730
731 cmp = f(left, right);
732 if (cmp)
733 break;
734 }
735
736 return cmp;
737}
738
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200739static size_t
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200740hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200741{
742 struct sort_entry *se;
743 size_t ret;
744
745 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200746 double percent = self->count * 100.0 / total_samples;
747 char *color = PERF_COLOR_NORMAL;
748
749 /*
Ingo Molnaraefcf372009-06-08 23:15:28 +0200750 * We color high-overhead entries in red, mid-overhead
751 * entries in green - and keep the low overhead places
752 * normal:
Ingo Molnar8fc03212009-06-04 15:19:47 +0200753 */
Ingo Molnaraefcf372009-06-08 23:15:28 +0200754 if (percent >= 5.0) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200755 color = PERF_COLOR_RED;
Ingo Molnaraefcf372009-06-08 23:15:28 +0200756 } else {
757 if (percent >= 0.5)
758 color = PERF_COLOR_GREEN;
759 }
Ingo Molnar8fc03212009-06-04 15:19:47 +0200760
761 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200762 (self->count * 100.0) / total_samples);
763 } else
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200764 ret = fprintf(fp, "%12Ld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200765
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200766 list_for_each_entry(se, &hist_entry__sort_list, list) {
767 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200768 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200769 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200770
771 ret += fprintf(fp, "\n");
772
773 return ret;
774}
775
776/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200777 *
778 */
779
780static struct symbol *
781resolve_symbol(struct thread *thread, struct map **mapp,
782 struct dso **dsop, __u64 *ipp)
783{
784 struct dso *dso = dsop ? *dsop : NULL;
785 struct map *map = mapp ? *mapp : NULL;
786 uint64_t ip = *ipp;
787
788 if (!thread)
789 return NULL;
790
791 if (dso)
792 goto got_dso;
793
794 if (map)
795 goto got_map;
796
797 map = thread__find_map(thread, ip);
798 if (map != NULL) {
799 if (mapp)
800 *mapp = map;
801got_map:
802 ip = map->map_ip(map, ip);
803 *ipp = ip;
804
805 dso = map->dso;
806 } else {
807 /*
808 * If this is outside of all known maps,
809 * and is a negative address, try to look it
810 * up in the kernel dso, as it might be a
811 * vsyscall (which executes in user-mode):
812 */
813 if ((long long)ip < 0)
814 dso = kernel_dso;
815 }
816 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
817
818 if (dsop)
819 *dsop = dso;
820
821 if (!dso)
822 return NULL;
823got_dso:
824 return dso->find_symbol(dso, ip);
825}
826
827static struct symbol *call__match(struct symbol *sym)
828{
829 if (!sym)
830 return NULL;
831
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200832 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200833 return sym;
834
835 return NULL;
836}
837
838/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200839 * collect histogram counts
840 */
841
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200842static int
843hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Ingo Molnar75220602009-06-18 08:00:17 +0200844 struct symbol *sym, __u64 ip, struct perf_callchain_entry *chain,
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200845 char level, __u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300846{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200847 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300848 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200849 struct hist_entry *he;
850 struct hist_entry entry = {
851 .thread = thread,
852 .map = map,
853 .dso = dso,
854 .sym = sym,
855 .ip = ip,
856 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200857 .count = count,
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200858 };
859 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300860
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200861 if (sort__has_parent && chain) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200862 int i, nr = chain->hv;
863 struct symbol *sym;
864 struct dso *dso;
865 __u64 ip;
866
867 for (i = 0; i < chain->kernel; i++) {
Ingo Molnar75220602009-06-18 08:00:17 +0200868 ip = chain->ip[nr + i];
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200869 dso = kernel_dso;
870 sym = resolve_symbol(thread, NULL, &dso, &ip);
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200871 entry.parent = call__match(sym);
872 if (entry.parent)
873 goto got_parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200874 }
875 nr += i;
876
877 for (i = 0; i < chain->user; i++) {
Ingo Molnar75220602009-06-18 08:00:17 +0200878 ip = chain->ip[nr + i];
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200879 sym = resolve_symbol(thread, NULL, NULL, &ip);
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200880 entry.parent = call__match(sym);
881 if (entry.parent)
882 goto got_parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200883 }
884 nr += i;
885 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200886got_parent:
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200887
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300888 while (*p != NULL) {
889 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200890 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300891
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200892 cmp = hist_entry__cmp(&entry, he);
893
894 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200895 he->count += count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200896 return 0;
897 }
898
899 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300900 p = &(*p)->rb_left;
901 else
902 p = &(*p)->rb_right;
903 }
904
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200905 he = malloc(sizeof(*he));
906 if (!he)
907 return -ENOMEM;
908 *he = entry;
909 rb_link_node(&he->rb_node, parent, p);
910 rb_insert_color(&he->rb_node, &hist);
911
912 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300913}
914
Peter Zijlstra82292892009-06-03 12:37:36 +0200915static void hist_entry__free(struct hist_entry *he)
916{
917 free(he);
918}
919
920/*
921 * collapse the histogram
922 */
923
924static struct rb_root collapse_hists;
925
926static void collapse__insert_entry(struct hist_entry *he)
927{
928 struct rb_node **p = &collapse_hists.rb_node;
929 struct rb_node *parent = NULL;
930 struct hist_entry *iter;
931 int64_t cmp;
932
933 while (*p != NULL) {
934 parent = *p;
935 iter = rb_entry(parent, struct hist_entry, rb_node);
936
937 cmp = hist_entry__collapse(iter, he);
938
939 if (!cmp) {
940 iter->count += he->count;
941 hist_entry__free(he);
942 return;
943 }
944
945 if (cmp < 0)
946 p = &(*p)->rb_left;
947 else
948 p = &(*p)->rb_right;
949 }
950
951 rb_link_node(&he->rb_node, parent, p);
952 rb_insert_color(&he->rb_node, &collapse_hists);
953}
954
955static void collapse__resort(void)
956{
957 struct rb_node *next;
958 struct hist_entry *n;
959
960 if (!sort__need_collapse)
961 return;
962
963 next = rb_first(&hist);
964 while (next) {
965 n = rb_entry(next, struct hist_entry, rb_node);
966 next = rb_next(&n->rb_node);
967
968 rb_erase(&n->rb_node, &hist);
969 collapse__insert_entry(n);
970 }
971}
972
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200973/*
974 * reverse the map, sort on count.
975 */
976
977static struct rb_root output_hists;
978
979static void output__insert_entry(struct hist_entry *he)
980{
981 struct rb_node **p = &output_hists.rb_node;
982 struct rb_node *parent = NULL;
983 struct hist_entry *iter;
984
985 while (*p != NULL) {
986 parent = *p;
987 iter = rb_entry(parent, struct hist_entry, rb_node);
988
989 if (he->count > iter->count)
990 p = &(*p)->rb_left;
991 else
992 p = &(*p)->rb_right;
993 }
994
995 rb_link_node(&he->rb_node, parent, p);
996 rb_insert_color(&he->rb_node, &output_hists);
997}
998
999static void output__resort(void)
1000{
Peter Zijlstra82292892009-06-03 12:37:36 +02001001 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001002 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001003 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001004
Peter Zijlstra82292892009-06-03 12:37:36 +02001005 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001006 tree = &collapse_hists;
1007
1008 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001009
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001010 while (next) {
1011 n = rb_entry(next, struct hist_entry, rb_node);
1012 next = rb_next(&n->rb_node);
1013
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001014 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001015 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001016 }
1017}
1018
Ingo Molnar729ff5e22009-06-11 14:16:15 +02001019static size_t output__fprintf(FILE *fp, __u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001020{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001021 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001022 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001023 struct rb_node *nd;
1024 size_t ret = 0;
1025
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001026 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001027 fprintf(fp, "#\n");
Ingo Molnar2debbc82009-06-05 14:29:10 +02001028 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001029 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001030
1031 fprintf(fp, "# Overhead");
1032 list_for_each_entry(se, &hist_entry__sort_list, list)
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001033 fprintf(fp, " %s", se->header);
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001034 fprintf(fp, "\n");
1035
1036 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001037 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001038 int i;
1039
Ingo Molnar4593bba2009-06-02 15:34:25 +02001040 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001041 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001042 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001043 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001044 fprintf(fp, "\n");
1045
1046 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001047
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001048 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1049 pos = rb_entry(nd, struct hist_entry, rb_node);
1050 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001051 }
1052
Ingo Molnarbd741372009-06-04 14:13:04 +02001053 if (!strcmp(sort_order, default_sort_order)) {
1054 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001055 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001056 fprintf(fp, "#\n");
1057 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001058 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001059
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001060 return ret;
1061}
1062
Peter Zijlstra436224a2009-06-02 21:02:36 +02001063static void register_idle_thread(void)
1064{
1065 struct thread *thread = threads__findnew(0);
1066
1067 if (thread == NULL ||
1068 thread__set_comm(thread, "[idle]")) {
1069 fprintf(stderr, "problem inserting idle task.\n");
1070 exit(-1);
1071 }
1072}
1073
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001074static unsigned long total = 0,
1075 total_mmap = 0,
1076 total_comm = 0,
1077 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001078 total_unknown = 0,
1079 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001080
Ingo Molnar75220602009-06-18 08:00:17 +02001081static int validate_chain(struct perf_callchain_entry *chain, event_t *event)
1082{
1083 unsigned int chain_size;
1084
1085 if (chain->nr > MAX_STACK_DEPTH)
1086 return -1;
1087 if (chain->hv > MAX_STACK_DEPTH)
1088 return -1;
1089 if (chain->kernel > MAX_STACK_DEPTH)
1090 return -1;
1091 if (chain->user > MAX_STACK_DEPTH)
1092 return -1;
1093 if (chain->hv + chain->kernel + chain->user != chain->nr)
1094 return -1;
1095
1096 chain_size = event->header.size;
1097 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1098
1099 if (chain->nr*sizeof(__u64) > chain_size)
1100 return -1;
1101
1102 return 0;
1103}
1104
Ingo Molnard80d3382009-06-03 23:14:49 +02001105static int
Ingo Molnar75051722009-06-03 23:14:49 +02001106process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1107{
1108 char level;
1109 int show = 0;
1110 struct dso *dso = NULL;
1111 struct thread *thread = threads__findnew(event->ip.pid);
Ingo Molnar729ff5e22009-06-11 14:16:15 +02001112 __u64 ip = event->ip.ip;
1113 __u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001114 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001115 void *more_data = event->ip.__more_data;
Ingo Molnar75220602009-06-18 08:00:17 +02001116 struct perf_callchain_entry *chain = NULL;
Ingo Molnar75051722009-06-03 23:14:49 +02001117
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001118 if (event->header.type & PERF_SAMPLE_PERIOD) {
1119 period = *(__u64 *)more_data;
1120 more_data += sizeof(__u64);
1121 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001122
Peter Zijlstra4502d772009-06-10 15:03:06 +02001123 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001124 (void *)(offset + head),
1125 (void *)(long)(event->header.size),
1126 event->header.misc,
1127 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001128 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001129 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001130
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001131 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
1132 int i;
1133
1134 chain = (void *)more_data;
1135
Ingo Molnar75220602009-06-18 08:00:17 +02001136 dprintf("... chain: u:%d, k:%d, nr:%d\n",
1137 chain->user,
1138 chain->kernel,
1139 chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001140
Ingo Molnar75220602009-06-18 08:00:17 +02001141 if (validate_chain(chain, event) < 0) {
1142 eprintf("call-chain problem with event, skipping it.\n");
1143 return 0;
1144 }
1145
1146 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001147 for (i = 0; i < chain->nr; i++)
Ingo Molnar75220602009-06-18 08:00:17 +02001148 dprintf("..... %2d: %016Lx\n", i, chain->ip[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001149 }
1150 }
1151
Ingo Molnar75051722009-06-03 23:14:49 +02001152 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1153
1154 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001155 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001156 event->header.type);
1157 return -1;
1158 }
1159
1160 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1161 show = SHOW_KERNEL;
1162 level = 'k';
1163
1164 dso = kernel_dso;
1165
1166 dprintf(" ...... dso: %s\n", dso->name);
1167
1168 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1169
1170 show = SHOW_USER;
1171 level = '.';
1172
Ingo Molnar75051722009-06-03 23:14:49 +02001173 } else {
1174 show = SHOW_HV;
1175 level = 'H';
1176 dprintf(" ...... dso: [hypervisor]\n");
1177 }
1178
1179 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001180 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001181
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001182 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001183 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001184 return -1;
1185 }
1186 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001187 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001188
1189 return 0;
1190}
1191
1192static int
1193process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1194{
1195 struct thread *thread = threads__findnew(event->mmap.pid);
1196 struct map *map = map__new(&event->mmap);
1197
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001198 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001199 (void *)(offset + head),
1200 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001201 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001202 (void *)(long)event->mmap.start,
1203 (void *)(long)event->mmap.len,
1204 (void *)(long)event->mmap.pgoff,
1205 event->mmap.filename);
1206
1207 if (thread == NULL || map == NULL) {
1208 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001209 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001210 }
1211
1212 thread__insert_map(thread, map);
1213 total_mmap++;
1214
1215 return 0;
1216}
1217
1218static int
1219process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1220{
1221 struct thread *thread = threads__findnew(event->comm.pid);
1222
1223 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1224 (void *)(offset + head),
1225 (void *)(long)(event->header.size),
1226 event->comm.comm, event->comm.pid);
1227
1228 if (thread == NULL ||
1229 thread__set_comm(thread, event->comm.comm)) {
1230 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1231 return -1;
1232 }
1233 total_comm++;
1234
1235 return 0;
1236}
1237
1238static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001239process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1240{
1241 struct thread *thread = threads__findnew(event->fork.pid);
1242 struct thread *parent = threads__findnew(event->fork.ppid);
1243
1244 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1245 (void *)(offset + head),
1246 (void *)(long)(event->header.size),
1247 event->fork.pid, event->fork.ppid);
1248
1249 if (!thread || !parent || thread__fork(thread, parent)) {
1250 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1251 return -1;
1252 }
1253 total_fork++;
1254
1255 return 0;
1256}
1257
1258static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001259process_period_event(event_t *event, unsigned long offset, unsigned long head)
1260{
1261 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1262 (void *)(offset + head),
1263 (void *)(long)(event->header.size),
1264 event->period.time,
1265 event->period.id,
1266 event->period.sample_period);
1267
1268 return 0;
1269}
1270
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001271static int
1272process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1273{
1274 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1275 (void *)(offset + head),
1276 (void *)(long)(event->header.size),
1277 event->lost.id,
1278 event->lost.lost);
1279
1280 total_lost += event->lost.lost;
1281
1282 return 0;
1283}
1284
Ingo Molnar8465b052009-06-14 14:44:07 +02001285static void trace_event(event_t *event)
1286{
1287 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001288 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001289 int i, j;
1290
1291 if (!dump_trace)
1292 return;
1293
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001294 dprintf(".");
1295 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001296
1297 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001298 if ((i & 15) == 0) {
1299 dprintf(".");
1300 cdprintf(" %04x: ", i);
1301 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001302
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001303 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001304
1305 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001306 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001307 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001308 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001309 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001310 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001311 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001312 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001313 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001314 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001315 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001316 }
1317 }
1318 dprintf(".\n");
1319}
1320
Ingo Molnarb2fef072009-06-05 18:07:51 +02001321static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001322process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001323{
Ingo Molnar8465b052009-06-14 14:44:07 +02001324 trace_event(event);
1325
Ingo Molnar75051722009-06-03 23:14:49 +02001326 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1327 return process_overflow_event(event, offset, head);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001328
Ingo Molnar75051722009-06-03 23:14:49 +02001329 switch (event->header.type) {
1330 case PERF_EVENT_MMAP:
1331 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001332
Ingo Molnar75051722009-06-03 23:14:49 +02001333 case PERF_EVENT_COMM:
1334 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001335
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001336 case PERF_EVENT_FORK:
1337 return process_fork_event(event, offset, head);
1338
Ingo Molnarb2fef072009-06-05 18:07:51 +02001339 case PERF_EVENT_PERIOD:
1340 return process_period_event(event, offset, head);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001341
1342 case PERF_EVENT_LOST:
1343 return process_lost_event(event, offset, head);
1344
Ingo Molnard11444d2009-06-03 23:29:14 +02001345 /*
1346 * We dont process them right now but they are fine:
1347 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001348
Ingo Molnard11444d2009-06-03 23:29:14 +02001349 case PERF_EVENT_THROTTLE:
1350 case PERF_EVENT_UNTHROTTLE:
1351 return 0;
1352
Ingo Molnard80d3382009-06-03 23:14:49 +02001353 default:
1354 return -1;
1355 }
1356
1357 return 0;
1358}
1359
1360static int __cmd_report(void)
1361{
Ingo Molnar75051722009-06-03 23:14:49 +02001362 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001363 unsigned long offset = 0;
1364 unsigned long head = 0;
1365 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001366 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001367 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001368 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001369
1370 register_idle_thread();
1371
1372 input = open(input_name, O_RDONLY);
1373 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001374 fprintf(stderr, " failed to open file: %s", input_name);
1375 if (!strcmp(input_name, "perf.data"))
1376 fprintf(stderr, " (try 'perf record' first)");
1377 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001378 exit(-1);
1379 }
1380
1381 ret = fstat(input, &stat);
1382 if (ret < 0) {
1383 perror("failed to stat file");
1384 exit(-1);
1385 }
1386
1387 if (!stat.st_size) {
1388 fprintf(stderr, "zero-sized file, nothing to do!\n");
1389 exit(0);
1390 }
1391
1392 if (load_kernel() < 0) {
1393 perror("failed to load kernel symbols");
1394 return EXIT_FAILURE;
1395 }
1396
1397 if (!full_paths) {
1398 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1399 perror("failed to get the current directory");
1400 return EXIT_FAILURE;
1401 }
1402 cwdlen = strlen(cwd);
1403 } else {
1404 cwd = NULL;
1405 cwdlen = 0;
1406 }
1407remap:
1408 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1409 MAP_SHARED, input, offset);
1410 if (buf == MAP_FAILED) {
1411 perror("failed to mmap file");
1412 exit(-1);
1413 }
1414
1415more:
1416 event = (event_t *)(buf + head);
1417
1418 size = event->header.size;
1419 if (!size)
1420 size = 8;
1421
1422 if (head + event->header.size >= page_size * mmap_window) {
1423 unsigned long shift = page_size * (head / page_size);
1424 int ret;
1425
1426 ret = munmap(buf, page_size * mmap_window);
1427 assert(ret == 0);
1428
1429 offset += shift;
1430 head -= shift;
1431 goto remap;
1432 }
1433
1434 size = event->header.size;
1435
Ingo Molnar8465b052009-06-14 14:44:07 +02001436 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001437 (void *)(offset + head),
1438 (void *)(long)event->header.size,
1439 event->header.type);
1440
Ingo Molnard80d3382009-06-03 23:14:49 +02001441 if (!size || process_event(event, offset, head) < 0) {
1442
Ingo Molnar35029732009-06-03 09:38:58 +02001443 dprintf("%p [%p]: skipping unknown header type: %d\n",
1444 (void *)(offset + head),
1445 (void *)(long)(event->header.size),
1446 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001447
Ingo Molnar3e706112009-05-26 18:53:17 +02001448 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001449
1450 /*
1451 * assume we lost track of the stream, check alignment, and
1452 * increment a single u64 in the hope to catch on again 'soon'.
1453 */
1454
1455 if (unlikely(head & 7))
1456 head &= ~7ULL;
1457
1458 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001459 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001460
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001461 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001462
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001463 if (offset + head < stat.st_size)
1464 goto more;
1465
1466 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001467 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001468
Ingo Molnar35029732009-06-03 09:38:58 +02001469 dprintf(" IP events: %10ld\n", total);
1470 dprintf(" mmap events: %10ld\n", total_mmap);
1471 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001472 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001473 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001474 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001475
Ingo Molnar35029732009-06-03 09:38:58 +02001476 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001477 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001478
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001479 if (verbose >= 3)
1480 threads__fprintf(stdout);
1481
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001482 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001483 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001484
Peter Zijlstra82292892009-06-03 12:37:36 +02001485 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001486 output__resort();
1487 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001488
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001489 return rc;
1490}
1491
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001492static const char * const report_usage[] = {
1493 "perf report [<options>] <command>",
1494 NULL
1495};
1496
1497static const struct option options[] = {
1498 OPT_STRING('i', "input", &input_name, "file",
1499 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001500 OPT_BOOLEAN('v', "verbose", &verbose,
1501 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001502 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1503 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001504 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001505 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001506 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001507 OPT_BOOLEAN('P', "full-paths", &full_paths,
1508 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001509 OPT_STRING('p', "parent", &parent_pattern, "regex",
1510 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001511 OPT_END()
1512};
1513
Ingo Molnar5352f352009-06-03 10:07:39 +02001514static void setup_sorting(void)
1515{
1516 char *tmp, *tok, *str = strdup(sort_order);
1517
1518 for (tok = strtok_r(str, ", ", &tmp);
1519 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1520 if (sort_dimension__add(tok) < 0) {
1521 error("Unknown --sort key: `%s'", tok);
1522 usage_with_options(report_usage, options);
1523 }
1524 }
1525
1526 free(str);
1527}
1528
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001529int cmd_report(int argc, const char **argv, const char *prefix)
1530{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001531 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001532
1533 page_size = getpagesize();
1534
Ingo Molnaredc52de2009-06-04 16:24:37 +02001535 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001536
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001537 setup_sorting();
1538
Ingo Molnaredc52de2009-06-04 16:24:37 +02001539 /*
1540 * Any (unrecognized) arguments left?
1541 */
1542 if (argc)
1543 usage_with_options(report_usage, options);
1544
Ingo Molnara930d2c2009-05-27 09:50:13 +02001545 setup_pager();
1546
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001547 return __cmd_report();
1548}