blob: 197793051fa51d1b9a24d4836b15f2c02af532b4 [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"
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020018#include "util/callchain.h"
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030019#include "util/strlist.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030020
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020021#include "perf.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022#include "util/header.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020023
24#include "util/parse-options.h"
25#include "util/parse-events.h"
26
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030027#define SHOW_KERNEL 1
28#define SHOW_USER 2
29#define SHOW_HV 4
30
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020031static char const *input_name = "perf.data";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020032static char *vmlinux = NULL;
Ingo Molnarbd741372009-06-04 14:13:04 +020033
34static char default_sort_order[] = "comm,dso";
35static char *sort_order = default_sort_order;
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -030036static char *dso_list_str, *comm_list_str, *sym_list_str;
37static struct strlist *dso_list, *comm_list, *sym_list;
Ingo Molnarbd741372009-06-04 14:13:04 +020038
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030039static int input;
40static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
41
Ingo Molnar97b07b62009-05-26 18:48:58 +020042static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020043#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020044#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
Ingo Molnar35029732009-06-03 09:38:58 +020045
Ingo Molnar16f762a2009-05-27 09:10:38 +020046static int verbose;
Ingo Molnar75220602009-06-18 08:00:17 +020047#define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
48
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030049static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020050
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030051static unsigned long page_size;
52static unsigned long mmap_window = 32;
53
Ingo Molnarb8e6d822009-06-18 14:32:19 +020054static char default_parent_pattern[] = "^sys_|^do_page_fault";
55static char *parent_pattern = default_parent_pattern;
Ingo Molnarb25bcf22009-06-18 07:01:03 +020056static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020057
Ingo Molnarb8e6d822009-06-18 14:32:19 +020058static int exclude_other = 1;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020059static int callchain;
Ingo Molnarb8e6d822009-06-18 14:32:19 +020060
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020061static u64 sample_type;
62
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030063struct ip_event {
64 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100065 u64 ip;
66 u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020067 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030068};
Ingo Molnar75051722009-06-03 23:14:49 +020069
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030070struct mmap_event {
71 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100072 u32 pid, tid;
73 u64 start;
74 u64 len;
75 u64 pgoff;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030076 char filename[PATH_MAX];
77};
Ingo Molnar75051722009-06-03 23:14:49 +020078
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030079struct comm_event {
80 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100081 u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030082 char comm[16];
83};
84
Peter Zijlstra62fc4452009-06-04 16:53:49 +020085struct fork_event {
86 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100087 u32 pid, ppid;
Peter Zijlstra62fc4452009-06-04 16:53:49 +020088};
89
Ingo Molnarb2fef072009-06-05 18:07:51 +020090struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030091 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100092 u64 time;
93 u64 id;
94 u64 sample_period;
Ingo Molnarb2fef072009-06-05 18:07:51 +020095};
96
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020097struct lost_event {
98 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100099 u64 id;
100 u64 lost;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200101};
102
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200103struct read_event {
104 struct perf_event_header header;
105 u32 pid,tid;
106 u64 value;
107 u64 format[3];
108};
109
Ingo Molnarb2fef072009-06-05 18:07:51 +0200110typedef union event_union {
111 struct perf_event_header header;
112 struct ip_event ip;
113 struct mmap_event mmap;
114 struct comm_event comm;
115 struct fork_event fork;
116 struct period_event period;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200117 struct lost_event lost;
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200118 struct read_event read;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300119} event_t;
120
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300121static LIST_HEAD(dsos);
122static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200123static struct dso *vdso;
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000124static struct dso *hypervisor_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300125
126static void dsos__add(struct dso *dso)
127{
128 list_add_tail(&dso->node, &dsos);
129}
130
131static struct dso *dsos__find(const char *name)
132{
133 struct dso *pos;
134
135 list_for_each_entry(pos, &dsos, node)
136 if (strcmp(pos->name, name) == 0)
137 return pos;
138 return NULL;
139}
140
141static struct dso *dsos__findnew(const char *name)
142{
143 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200144 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300145
Ingo Molnar4593bba2009-06-02 15:34:25 +0200146 if (dso)
147 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300148
Ingo Molnar4593bba2009-06-02 15:34:25 +0200149 dso = dso__new(name, 0);
150 if (!dso)
151 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200152
Ingo Molnarbd741372009-06-04 14:13:04 +0200153 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200154 if (nr < 0) {
Ingo Molnar75220602009-06-18 08:00:17 +0200155 eprintf("Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200156 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300157 }
Ingo Molnar75220602009-06-18 08:00:17 +0200158 if (!nr)
159 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200160
161 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300162
163 return dso;
164
165out_delete_dso:
166 dso__delete(dso);
167 return NULL;
168}
169
Ingo Molnar16f762a2009-05-27 09:10:38 +0200170static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300171{
172 struct dso *pos;
173
174 list_for_each_entry(pos, &dsos, node)
175 dso__fprintf(pos, fp);
176}
177
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000178static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200179{
180 return dso__find_symbol(kernel_dso, ip);
181}
182
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200183static int load_kernel(void)
184{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300185 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200186
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300187 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200188 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300189 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200190
Ingo Molnarbd741372009-06-04 14:13:04 +0200191 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300192 if (err) {
193 dso__delete(kernel_dso);
194 kernel_dso = NULL;
195 } else
196 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200197
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200198 vdso = dso__new("[vdso]", 0);
199 if (!vdso)
200 return -1;
201
202 vdso->find_symbol = vdso__find_symbol;
203
204 dsos__add(vdso);
205
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000206 hypervisor_dso = dso__new("[hypervisor]", 0);
207 if (!hypervisor_dso)
208 return -1;
209 dsos__add(hypervisor_dso);
210
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300211 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200212}
213
Ingo Molnard80d3382009-06-03 23:14:49 +0200214static char __cwd[PATH_MAX];
215static char *cwd = __cwd;
216static int cwdlen;
217
218static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300219{
220 int n = 0;
221
222 while (pathname[n] == cwd[n] && n < cwdlen)
223 ++n;
224
225 return n;
226}
227
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300228struct map {
229 struct list_head node;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000230 u64 start;
231 u64 end;
232 u64 pgoff;
233 u64 (*map_ip)(struct map *, u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300234 struct dso *dso;
235};
236
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000237static u64 map__map_ip(struct map *map, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200238{
239 return ip - map->start + map->pgoff;
240}
241
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000242static u64 vdso__map_ip(struct map *map, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200243{
244 return ip;
245}
246
Pekka Enberg80d496b2009-06-08 21:12:48 +0300247static inline int is_anon_memory(const char *filename)
248{
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300249 return strcmp(filename, "//anon") == 0;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300250}
251
Ingo Molnard80d3382009-06-03 23:14:49 +0200252static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300253{
254 struct map *self = malloc(sizeof(*self));
255
256 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300257 const char *filename = event->filename;
258 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300259 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300260
261 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200262 int n = strcommon(filename);
263
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300264 if (n == cwdlen) {
265 snprintf(newfilename, sizeof(newfilename),
266 ".%s", filename + n);
267 filename = newfilename;
268 }
269 }
270
Pekka Enberg80d496b2009-06-08 21:12:48 +0300271 anon = is_anon_memory(filename);
272
273 if (anon) {
274 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
275 filename = newfilename;
276 }
277
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300278 self->start = event->start;
279 self->end = event->start + event->len;
280 self->pgoff = event->pgoff;
281
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300282 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300283 if (self->dso == NULL)
284 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200285
Pekka Enberg80d496b2009-06-08 21:12:48 +0300286 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200287 self->map_ip = vdso__map_ip;
288 else
289 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300290 }
291 return self;
292out_delete:
293 free(self);
294 return NULL;
295}
296
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200297static struct map *map__clone(struct map *self)
298{
299 struct map *map = malloc(sizeof(*self));
300
301 if (!map)
302 return NULL;
303
304 memcpy(map, self, sizeof(*self));
305
306 return map;
307}
308
309static int map__overlap(struct map *l, struct map *r)
310{
311 if (l->start > r->start) {
312 struct map *t = l;
313 l = r;
314 r = t;
315 }
316
317 if (l->end > r->start)
318 return 1;
319
320 return 0;
321}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300322
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300323static size_t map__fprintf(struct map *self, FILE *fp)
324{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200325 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300326 self->start, self->end, self->pgoff, self->dso->name);
327}
328
329
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300330struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300331 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300332 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300333 pid_t pid;
334 char *comm;
335};
336
337static struct thread *thread__new(pid_t pid)
338{
339 struct thread *self = malloc(sizeof(*self));
340
341 if (self != NULL) {
342 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200343 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200344 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200345 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300346 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300347 }
348
349 return self;
350}
351
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300352static int thread__set_comm(struct thread *self, const char *comm)
353{
Peter Zijlstra82292892009-06-03 12:37:36 +0200354 if (self->comm)
355 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300356 self->comm = strdup(comm);
357 return self->comm ? 0 : -ENOMEM;
358}
359
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300360static size_t thread__fprintf(struct thread *self, FILE *fp)
361{
362 struct map *pos;
363 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
364
365 list_for_each_entry(pos, &self->maps, node)
366 ret += map__fprintf(pos, fp);
367
368 return ret;
369}
370
371
Ingo Molnar16f762a2009-05-27 09:10:38 +0200372static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200373static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300374
375static struct thread *threads__findnew(pid_t pid)
376{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300377 struct rb_node **p = &threads.rb_node;
378 struct rb_node *parent = NULL;
379 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300380
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200381 /*
382 * Font-end cache - PID lookups come in blocks,
383 * so most of the time we dont have to look up
384 * the full rbtree:
385 */
386 if (last_match && last_match->pid == pid)
387 return last_match;
388
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300389 while (*p != NULL) {
390 parent = *p;
391 th = rb_entry(parent, struct thread, rb_node);
392
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200393 if (th->pid == pid) {
394 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300395 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200396 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300397
398 if (pid < th->pid)
399 p = &(*p)->rb_left;
400 else
401 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300402 }
403
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300404 th = thread__new(pid);
405 if (th != NULL) {
406 rb_link_node(&th->rb_node, parent, p);
407 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200408 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300409 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200410
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300411 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300412}
413
414static void thread__insert_map(struct thread *self, struct map *map)
415{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200416 struct map *pos, *tmp;
417
418 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
419 if (map__overlap(pos, map)) {
Peter Zijlstra3d906ef2009-06-23 11:23:07 +0200420 if (verbose >= 2) {
421 printf("overlapping maps:\n");
422 map__fprintf(map, stdout);
423 map__fprintf(pos, stdout);
424 }
425
426 if (map->start <= pos->start && map->end > pos->start)
427 pos->start = map->end;
428
429 if (map->end >= pos->end && map->start < pos->end)
430 pos->end = map->start;
431
432 if (verbose >= 2) {
433 printf("after collision:\n");
434 map__fprintf(pos, stdout);
435 }
436
437 if (pos->start >= pos->end) {
438 list_del_init(&pos->node);
439 free(pos);
440 }
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200441 }
442 }
443
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300444 list_add_tail(&map->node, &self->maps);
445}
446
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200447static int thread__fork(struct thread *self, struct thread *parent)
448{
449 struct map *map;
450
451 if (self->comm)
452 free(self->comm);
453 self->comm = strdup(parent->comm);
454 if (!self->comm)
455 return -ENOMEM;
456
457 list_for_each_entry(map, &parent->maps, node) {
458 struct map *new = map__clone(map);
459 if (!new)
460 return -ENOMEM;
461 thread__insert_map(self, new);
462 }
463
464 return 0;
465}
466
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000467static struct map *thread__find_map(struct thread *self, u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300468{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200469 struct map *pos;
470
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300471 if (self == NULL)
472 return NULL;
473
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300474 list_for_each_entry(pos, &self->maps, node)
475 if (ip >= pos->start && ip <= pos->end)
476 return pos;
477
478 return NULL;
479}
480
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300481static size_t threads__fprintf(FILE *fp)
482{
483 size_t ret = 0;
484 struct rb_node *nd;
485
486 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
487 struct thread *pos = rb_entry(nd, struct thread, rb_node);
488
489 ret += thread__fprintf(pos, fp);
490 }
491
492 return ret;
493}
494
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200495/*
496 * histogram, sorted on item, collects counts
497 */
498
499static struct rb_root hist;
500
501struct hist_entry {
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200502 struct rb_node rb_node;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200503
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200504 struct thread *thread;
505 struct map *map;
506 struct dso *dso;
507 struct symbol *sym;
508 struct symbol *parent;
509 u64 ip;
510 char level;
511 struct callchain_node callchain;
512 struct rb_root sorted_chain;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200513
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200514 u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200515};
516
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200517/*
518 * configurable sorting bits
519 */
520
521struct sort_entry {
522 struct list_head list;
523
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200524 char *header;
525
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200526 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200527 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200528 size_t (*print)(FILE *fp, struct hist_entry *);
529};
530
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200531static int64_t cmp_null(void *l, void *r)
532{
533 if (!l && !r)
534 return 0;
535 else if (!l)
536 return -1;
537 else
538 return 1;
539}
540
Peter Zijlstra82292892009-06-03 12:37:36 +0200541/* --sort pid */
542
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200543static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200544sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
545{
546 return right->thread->pid - left->thread->pid;
547}
548
549static size_t
550sort__thread_print(FILE *fp, struct hist_entry *self)
551{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200552 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200553}
554
555static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200556 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200557 .cmp = sort__thread_cmp,
558 .print = sort__thread_print,
559};
560
Peter Zijlstra82292892009-06-03 12:37:36 +0200561/* --sort comm */
562
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200563static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200564sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
565{
Peter Zijlstra82292892009-06-03 12:37:36 +0200566 return right->thread->pid - left->thread->pid;
567}
568
569static int64_t
570sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
571{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200572 char *comm_l = left->thread->comm;
573 char *comm_r = right->thread->comm;
574
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200575 if (!comm_l || !comm_r)
576 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200577
578 return strcmp(comm_l, comm_r);
579}
580
581static size_t
582sort__comm_print(FILE *fp, struct hist_entry *self)
583{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200584 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200585}
586
587static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200588 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200589 .cmp = sort__comm_cmp,
590 .collapse = sort__comm_collapse,
591 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200592};
593
Peter Zijlstra82292892009-06-03 12:37:36 +0200594/* --sort dso */
595
Peter Zijlstra992444b2009-05-27 20:20:27 +0200596static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200597sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
598{
599 struct dso *dso_l = left->dso;
600 struct dso *dso_r = right->dso;
601
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200602 if (!dso_l || !dso_r)
603 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200604
605 return strcmp(dso_l->name, dso_r->name);
606}
607
608static size_t
609sort__dso_print(FILE *fp, struct hist_entry *self)
610{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200611 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200612 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200613
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000614 return fprintf(fp, "%016llx ", (u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200615}
616
617static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200618 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200619 .cmp = sort__dso_cmp,
620 .print = sort__dso_print,
621};
622
Peter Zijlstra82292892009-06-03 12:37:36 +0200623/* --sort symbol */
624
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200625static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200626sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300627{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000628 u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200629
630 if (left->sym == right->sym)
631 return 0;
632
633 ip_l = left->sym ? left->sym->start : left->ip;
634 ip_r = right->sym ? right->sym->start : right->ip;
635
636 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300637}
638
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200639static size_t
640sort__sym_print(FILE *fp, struct hist_entry *self)
641{
642 size_t ret = 0;
643
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200644 if (verbose)
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000645 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200646
Ingo Molnar8edd4282009-06-05 14:13:18 +0200647 if (self->sym) {
648 ret += fprintf(fp, "[%c] %s",
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000649 self->dso == kernel_dso ? 'k' :
650 self->dso == hypervisor_dso ? 'h' : '.', self->sym->name);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200651 } else {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000652 ret += fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200653 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200654
655 return ret;
656}
657
658static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200659 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200660 .cmp = sort__sym_cmp,
661 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200662};
663
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200664/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200665
666static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200667sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200668{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200669 struct symbol *sym_l = left->parent;
670 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200671
672 if (!sym_l || !sym_r)
673 return cmp_null(sym_l, sym_r);
674
675 return strcmp(sym_l->name, sym_r->name);
676}
677
678static size_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200679sort__parent_print(FILE *fp, struct hist_entry *self)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200680{
681 size_t ret = 0;
682
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200683 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200684
685 return ret;
686}
687
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200688static struct sort_entry sort_parent = {
689 .header = "Parent symbol ",
690 .cmp = sort__parent_cmp,
691 .print = sort__parent_print,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200692};
693
Peter Zijlstra82292892009-06-03 12:37:36 +0200694static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200695static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200696
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200697struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200698 char *name;
699 struct sort_entry *entry;
700 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200701};
702
703static struct sort_dimension sort_dimensions[] = {
704 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200705 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200706 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200707 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200708 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200709};
710
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200711static LIST_HEAD(hist_entry__sort_list);
712
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200713static int sort_dimension__add(char *tok)
714{
715 int i;
716
717 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
718 struct sort_dimension *sd = &sort_dimensions[i];
719
720 if (sd->taken)
721 continue;
722
Ingo Molnar5352f352009-06-03 10:07:39 +0200723 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200724 continue;
725
Peter Zijlstra82292892009-06-03 12:37:36 +0200726 if (sd->entry->collapse)
727 sort__need_collapse = 1;
728
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200729 if (sd->entry == &sort_parent) {
730 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200731 if (ret) {
732 char err[BUFSIZ];
733
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200734 regerror(ret, &parent_regex, err, sizeof(err));
735 fprintf(stderr, "Invalid regex: %s\n%s",
736 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200737 exit(-1);
738 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200739 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200740 }
741
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200742 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
743 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200744
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200745 return 0;
746 }
747
748 return -ESRCH;
749}
750
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200751static int64_t
752hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
753{
754 struct sort_entry *se;
755 int64_t cmp = 0;
756
757 list_for_each_entry(se, &hist_entry__sort_list, list) {
758 cmp = se->cmp(left, right);
759 if (cmp)
760 break;
761 }
762
763 return cmp;
764}
765
Peter Zijlstra82292892009-06-03 12:37:36 +0200766static int64_t
767hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
768{
769 struct sort_entry *se;
770 int64_t cmp = 0;
771
772 list_for_each_entry(se, &hist_entry__sort_list, list) {
773 int64_t (*f)(struct hist_entry *, struct hist_entry *);
774
775 f = se->collapse ?: se->cmp;
776
777 cmp = f(left, right);
778 if (cmp)
779 break;
780 }
781
782 return cmp;
783}
784
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200785static size_t
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200786callchain__fprintf(FILE *fp, struct callchain_node *self, u64 total_samples)
787{
788 struct callchain_list *chain;
789 size_t ret = 0;
790
791 if (!self)
792 return 0;
793
794 ret += callchain__fprintf(fp, self->parent, total_samples);
795
796
Frederic Weisbecker44249612009-07-01 05:35:14 +0200797 list_for_each_entry(chain, &self->val, list) {
798 if (chain->ip >= PERF_CONTEXT_MAX)
799 continue;
800 if (chain->sym)
801 ret += fprintf(fp, " %s\n", chain->sym->name);
802 else
803 ret += fprintf(fp, " %p\n",
804 (void *)chain->ip);
805 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200806
807 return ret;
808}
809
810static size_t
811hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
812 u64 total_samples)
813{
814 struct rb_node *rb_node;
815 struct callchain_node *chain;
816 size_t ret = 0;
817
818 rb_node = rb_first(&self->sorted_chain);
819 while (rb_node) {
820 double percent;
821
822 chain = rb_entry(rb_node, struct callchain_node, rb_node);
823 percent = chain->hit * 100.0 / total_samples;
824 ret += fprintf(fp, " %6.2f%%\n", percent);
825 ret += callchain__fprintf(fp, chain, total_samples);
826 ret += fprintf(fp, "\n");
827 rb_node = rb_next(rb_node);
828 }
829
830 return ret;
831}
832
833
834static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000835hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200836{
837 struct sort_entry *se;
838 size_t ret;
839
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200840 if (exclude_other && !self->parent)
841 return 0;
842
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200843 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200844 double percent = self->count * 100.0 / total_samples;
845 char *color = PERF_COLOR_NORMAL;
846
847 /*
Ingo Molnaraefcf372009-06-08 23:15:28 +0200848 * We color high-overhead entries in red, mid-overhead
849 * entries in green - and keep the low overhead places
850 * normal:
Ingo Molnar8fc03212009-06-04 15:19:47 +0200851 */
Ingo Molnaraefcf372009-06-08 23:15:28 +0200852 if (percent >= 5.0) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200853 color = PERF_COLOR_RED;
Ingo Molnaraefcf372009-06-08 23:15:28 +0200854 } else {
855 if (percent >= 0.5)
856 color = PERF_COLOR_GREEN;
857 }
Ingo Molnar8fc03212009-06-04 15:19:47 +0200858
859 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200860 (self->count * 100.0) / total_samples);
861 } else
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200862 ret = fprintf(fp, "%12Ld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200863
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200864 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200865 if (exclude_other && (se == &sort_parent))
866 continue;
867
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200868 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200869 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200870 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200871
872 ret += fprintf(fp, "\n");
873
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200874 if (callchain)
875 hist_entry_callchain__fprintf(fp, self, total_samples);
876
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200877 return ret;
878}
879
880/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200881 *
882 */
883
884static struct symbol *
885resolve_symbol(struct thread *thread, struct map **mapp,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000886 struct dso **dsop, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200887{
888 struct dso *dso = dsop ? *dsop : NULL;
889 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200890 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200891
892 if (!thread)
893 return NULL;
894
895 if (dso)
896 goto got_dso;
897
898 if (map)
899 goto got_map;
900
901 map = thread__find_map(thread, ip);
902 if (map != NULL) {
903 if (mapp)
904 *mapp = map;
905got_map:
906 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200907
908 dso = map->dso;
909 } else {
910 /*
911 * If this is outside of all known maps,
912 * and is a negative address, try to look it
913 * up in the kernel dso, as it might be a
914 * vsyscall (which executes in user-mode):
915 */
916 if ((long long)ip < 0)
917 dso = kernel_dso;
918 }
919 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200920 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
921 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200922
923 if (dsop)
924 *dsop = dso;
925
926 if (!dso)
927 return NULL;
928got_dso:
929 return dso->find_symbol(dso, ip);
930}
931
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200932static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200933{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200934 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200935 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200936
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200937 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200938}
939
Frederic Weisbecker44249612009-07-01 05:35:14 +0200940static struct symbol **
941resolve_callchain(struct thread *thread, struct map *map,
942 struct ip_callchain *chain, struct hist_entry *entry)
943{
944 int i;
945 struct symbol **syms;
946 u64 context = PERF_CONTEXT_MAX;
947
948 if (callchain) {
949 syms = calloc(chain->nr, sizeof(*syms));
950 if (!syms) {
951 fprintf(stderr, "Can't allocate memory for symbols\n");
952 exit(-1);
953 }
954 }
955
956 for (i = 0; i < chain->nr; i++) {
957 u64 ip = chain->ips[i];
958 struct dso *dso = NULL;
959 struct symbol *sym;
960
961 if (ip >= PERF_CONTEXT_MAX) {
962 context = ip;
963 continue;
964 }
965
966 switch (context) {
967 case PERF_CONTEXT_KERNEL:
968 dso = kernel_dso;
969 break;
970 default:
971 break;
972 }
973
974 sym = resolve_symbol(thread, NULL, &dso, &ip);
975
976 if (sym) {
977 if (sort__has_parent && call__match(sym) &&
978 !entry->parent)
979 entry->parent = sym;
980 if (!callchain)
981 break;
982 syms[i] = sym;
983 }
984 }
985
986 return syms;
987}
988
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200989/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200990 * collect histogram counts
991 */
992
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200993static int
994hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000995 struct symbol *sym, u64 ip, struct ip_callchain *chain,
996 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300997{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200998 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300999 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001000 struct hist_entry *he;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001001 struct symbol **syms = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001002 struct hist_entry entry = {
1003 .thread = thread,
1004 .map = map,
1005 .dso = dso,
1006 .sym = sym,
1007 .ip = ip,
1008 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001009 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001010 .parent = NULL,
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001011 .sorted_chain = RB_ROOT
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001012 };
1013 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001014
Anton Blanchardfb9c8182009-07-01 09:00:49 +10001015 case PERF_CONTEXT_HV:
1016 dso = hypervisor_dso;
1017 break;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001018 if ((sort__has_parent || callchain) && chain)
1019 syms = resolve_callchain(thread, map, chain, &entry);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001020
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001021 while (*p != NULL) {
1022 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001023 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001024
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001025 cmp = hist_entry__cmp(&entry, he);
1026
1027 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001028 he->count += count;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001029 if (callchain) {
1030 append_chain(&he->callchain, chain, syms);
1031 free(syms);
1032 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001033 return 0;
1034 }
1035
1036 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001037 p = &(*p)->rb_left;
1038 else
1039 p = &(*p)->rb_right;
1040 }
1041
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001042 he = malloc(sizeof(*he));
1043 if (!he)
1044 return -ENOMEM;
1045 *he = entry;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001046 if (callchain) {
1047 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +02001048 append_chain(&he->callchain, chain, syms);
1049 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001050 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001051 rb_link_node(&he->rb_node, parent, p);
1052 rb_insert_color(&he->rb_node, &hist);
1053
1054 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001055}
1056
Peter Zijlstra82292892009-06-03 12:37:36 +02001057static void hist_entry__free(struct hist_entry *he)
1058{
1059 free(he);
1060}
1061
1062/*
1063 * collapse the histogram
1064 */
1065
1066static struct rb_root collapse_hists;
1067
1068static void collapse__insert_entry(struct hist_entry *he)
1069{
1070 struct rb_node **p = &collapse_hists.rb_node;
1071 struct rb_node *parent = NULL;
1072 struct hist_entry *iter;
1073 int64_t cmp;
1074
1075 while (*p != NULL) {
1076 parent = *p;
1077 iter = rb_entry(parent, struct hist_entry, rb_node);
1078
1079 cmp = hist_entry__collapse(iter, he);
1080
1081 if (!cmp) {
1082 iter->count += he->count;
1083 hist_entry__free(he);
1084 return;
1085 }
1086
1087 if (cmp < 0)
1088 p = &(*p)->rb_left;
1089 else
1090 p = &(*p)->rb_right;
1091 }
1092
1093 rb_link_node(&he->rb_node, parent, p);
1094 rb_insert_color(&he->rb_node, &collapse_hists);
1095}
1096
1097static void collapse__resort(void)
1098{
1099 struct rb_node *next;
1100 struct hist_entry *n;
1101
1102 if (!sort__need_collapse)
1103 return;
1104
1105 next = rb_first(&hist);
1106 while (next) {
1107 n = rb_entry(next, struct hist_entry, rb_node);
1108 next = rb_next(&n->rb_node);
1109
1110 rb_erase(&n->rb_node, &hist);
1111 collapse__insert_entry(n);
1112 }
1113}
1114
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001115/*
1116 * reverse the map, sort on count.
1117 */
1118
1119static struct rb_root output_hists;
1120
1121static void output__insert_entry(struct hist_entry *he)
1122{
1123 struct rb_node **p = &output_hists.rb_node;
1124 struct rb_node *parent = NULL;
1125 struct hist_entry *iter;
1126
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001127 if (callchain)
1128 sort_chain_to_rbtree(&he->sorted_chain, &he->callchain);
1129
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001130 while (*p != NULL) {
1131 parent = *p;
1132 iter = rb_entry(parent, struct hist_entry, rb_node);
1133
1134 if (he->count > iter->count)
1135 p = &(*p)->rb_left;
1136 else
1137 p = &(*p)->rb_right;
1138 }
1139
1140 rb_link_node(&he->rb_node, parent, p);
1141 rb_insert_color(&he->rb_node, &output_hists);
1142}
1143
1144static void output__resort(void)
1145{
Peter Zijlstra82292892009-06-03 12:37:36 +02001146 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001147 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001148 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001149
Peter Zijlstra82292892009-06-03 12:37:36 +02001150 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001151 tree = &collapse_hists;
1152
1153 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001154
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001155 while (next) {
1156 n = rb_entry(next, struct hist_entry, rb_node);
1157 next = rb_next(&n->rb_node);
1158
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001159 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001160 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001161 }
1162}
1163
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001164static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001165{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001166 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001167 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001168 struct rb_node *nd;
1169 size_t ret = 0;
1170
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001171 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001172 fprintf(fp, "#\n");
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001173 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001174 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001175
1176 fprintf(fp, "# Overhead");
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001177 list_for_each_entry(se, &hist_entry__sort_list, list) {
1178 if (exclude_other && (se == &sort_parent))
1179 continue;
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001180 fprintf(fp, " %s", se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001181 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001182 fprintf(fp, "\n");
1183
1184 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001185 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001186 int i;
1187
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001188 if (exclude_other && (se == &sort_parent))
1189 continue;
1190
Ingo Molnar4593bba2009-06-02 15:34:25 +02001191 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001192 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001193 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001194 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001195 fprintf(fp, "\n");
1196
1197 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001198
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001199 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1200 pos = rb_entry(nd, struct hist_entry, rb_node);
1201 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001202 }
1203
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001204 if (sort_order == default_sort_order &&
1205 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001206 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001207 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001208 fprintf(fp, "#\n");
1209 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001210 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001211
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001212 return ret;
1213}
1214
Peter Zijlstra436224a2009-06-02 21:02:36 +02001215static void register_idle_thread(void)
1216{
1217 struct thread *thread = threads__findnew(0);
1218
1219 if (thread == NULL ||
1220 thread__set_comm(thread, "[idle]")) {
1221 fprintf(stderr, "problem inserting idle task.\n");
1222 exit(-1);
1223 }
1224}
1225
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001226static unsigned long total = 0,
1227 total_mmap = 0,
1228 total_comm = 0,
1229 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001230 total_unknown = 0,
1231 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001232
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001233static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +02001234{
1235 unsigned int chain_size;
1236
Ingo Molnar75220602009-06-18 08:00:17 +02001237 chain_size = event->header.size;
1238 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1239
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001240 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +02001241 return -1;
1242
1243 return 0;
1244}
1245
Ingo Molnard80d3382009-06-03 23:14:49 +02001246static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001247process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +02001248{
1249 char level;
1250 int show = 0;
1251 struct dso *dso = NULL;
1252 struct thread *thread = threads__findnew(event->ip.pid);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001253 u64 ip = event->ip.ip;
1254 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001255 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001256 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001257 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001258 int cpumode;
Ingo Molnar75051722009-06-03 23:14:49 +02001259
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001260 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001261 period = *(u64 *)more_data;
1262 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001263 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001264
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001265 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001266 (void *)(offset + head),
1267 (void *)(long)(event->header.size),
1268 event->header.misc,
1269 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001270 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001271 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001272
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001273 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001274 int i;
1275
1276 chain = (void *)more_data;
1277
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001278 dprintf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001279
Ingo Molnar75220602009-06-18 08:00:17 +02001280 if (validate_chain(chain, event) < 0) {
1281 eprintf("call-chain problem with event, skipping it.\n");
1282 return 0;
1283 }
1284
1285 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001286 for (i = 0; i < chain->nr; i++)
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001287 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001288 }
1289 }
1290
Ingo Molnar75051722009-06-03 23:14:49 +02001291 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1292
1293 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001294 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001295 event->header.type);
1296 return -1;
1297 }
1298
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001299 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1300 return 0;
1301
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001302 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1303
1304 if (cpumode == PERF_EVENT_MISC_KERNEL) {
Ingo Molnar75051722009-06-03 23:14:49 +02001305 show = SHOW_KERNEL;
1306 level = 'k';
1307
1308 dso = kernel_dso;
1309
1310 dprintf(" ...... dso: %s\n", dso->name);
1311
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001312 } else if (cpumode == PERF_EVENT_MISC_USER) {
Ingo Molnar75051722009-06-03 23:14:49 +02001313
1314 show = SHOW_USER;
1315 level = '.';
1316
Ingo Molnar75051722009-06-03 23:14:49 +02001317 } else {
1318 show = SHOW_HV;
1319 level = 'H';
Anton Blanchardfb9c8182009-07-01 09:00:49 +10001320
1321 dso = hypervisor_dso;
1322
Ingo Molnar75051722009-06-03 23:14:49 +02001323 dprintf(" ...... dso: [hypervisor]\n");
1324 }
1325
1326 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001327 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001328
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001329 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1330 return 0;
1331
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001332 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1333 return 0;
1334
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001335 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001336 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001337 return -1;
1338 }
1339 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001340 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001341
1342 return 0;
1343}
1344
1345static int
1346process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1347{
1348 struct thread *thread = threads__findnew(event->mmap.pid);
1349 struct map *map = map__new(&event->mmap);
1350
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001351 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001352 (void *)(offset + head),
1353 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001354 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001355 (void *)(long)event->mmap.start,
1356 (void *)(long)event->mmap.len,
1357 (void *)(long)event->mmap.pgoff,
1358 event->mmap.filename);
1359
1360 if (thread == NULL || map == NULL) {
1361 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001362 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001363 }
1364
1365 thread__insert_map(thread, map);
1366 total_mmap++;
1367
1368 return 0;
1369}
1370
1371static int
1372process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1373{
1374 struct thread *thread = threads__findnew(event->comm.pid);
1375
1376 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1377 (void *)(offset + head),
1378 (void *)(long)(event->header.size),
1379 event->comm.comm, event->comm.pid);
1380
1381 if (thread == NULL ||
1382 thread__set_comm(thread, event->comm.comm)) {
1383 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1384 return -1;
1385 }
1386 total_comm++;
1387
1388 return 0;
1389}
1390
1391static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001392process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1393{
1394 struct thread *thread = threads__findnew(event->fork.pid);
1395 struct thread *parent = threads__findnew(event->fork.ppid);
1396
1397 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1398 (void *)(offset + head),
1399 (void *)(long)(event->header.size),
1400 event->fork.pid, event->fork.ppid);
1401
1402 if (!thread || !parent || thread__fork(thread, parent)) {
1403 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1404 return -1;
1405 }
1406 total_fork++;
1407
1408 return 0;
1409}
1410
1411static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001412process_period_event(event_t *event, unsigned long offset, unsigned long head)
1413{
1414 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1415 (void *)(offset + head),
1416 (void *)(long)(event->header.size),
1417 event->period.time,
1418 event->period.id,
1419 event->period.sample_period);
1420
1421 return 0;
1422}
1423
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001424static int
1425process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1426{
1427 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1428 (void *)(offset + head),
1429 (void *)(long)(event->header.size),
1430 event->lost.id,
1431 event->lost.lost);
1432
1433 total_lost += event->lost.lost;
1434
1435 return 0;
1436}
1437
Ingo Molnar8465b052009-06-14 14:44:07 +02001438static void trace_event(event_t *event)
1439{
1440 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001441 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001442 int i, j;
1443
1444 if (!dump_trace)
1445 return;
1446
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001447 dprintf(".");
1448 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001449
1450 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001451 if ((i & 15) == 0) {
1452 dprintf(".");
1453 cdprintf(" %04x: ", i);
1454 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001455
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001456 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001457
1458 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001459 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001460 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001461 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001462 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001463 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001464 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001465 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001466 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001467 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001468 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001469 }
1470 }
1471 dprintf(".\n");
1472}
1473
Ingo Molnarb2fef072009-06-05 18:07:51 +02001474static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001475process_read_event(event_t *event, unsigned long offset, unsigned long head)
1476{
1477 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1478 (void *)(offset + head),
1479 (void *)(long)(event->header.size),
1480 event->read.pid,
1481 event->read.tid,
1482 event->read.value);
1483
1484 return 0;
1485}
1486
1487static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001488process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001489{
Ingo Molnar8465b052009-06-14 14:44:07 +02001490 trace_event(event);
1491
Ingo Molnar75051722009-06-03 23:14:49 +02001492 switch (event->header.type) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001493 case PERF_EVENT_SAMPLE:
1494 return process_sample_event(event, offset, head);
1495
Ingo Molnar75051722009-06-03 23:14:49 +02001496 case PERF_EVENT_MMAP:
1497 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001498
Ingo Molnar75051722009-06-03 23:14:49 +02001499 case PERF_EVENT_COMM:
1500 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001501
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001502 case PERF_EVENT_FORK:
1503 return process_fork_event(event, offset, head);
1504
Ingo Molnarb2fef072009-06-05 18:07:51 +02001505 case PERF_EVENT_PERIOD:
1506 return process_period_event(event, offset, head);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001507
1508 case PERF_EVENT_LOST:
1509 return process_lost_event(event, offset, head);
1510
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001511 case PERF_EVENT_READ:
1512 return process_read_event(event, offset, head);
1513
Ingo Molnard11444d2009-06-03 23:29:14 +02001514 /*
1515 * We dont process them right now but they are fine:
1516 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001517
Ingo Molnard11444d2009-06-03 23:29:14 +02001518 case PERF_EVENT_THROTTLE:
1519 case PERF_EVENT_UNTHROTTLE:
1520 return 0;
1521
Ingo Molnard80d3382009-06-03 23:14:49 +02001522 default:
1523 return -1;
1524 }
1525
1526 return 0;
1527}
1528
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001529static struct perf_header *header;
1530
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001531static u64 perf_header__sample_type(void)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001532{
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001533 u64 sample_type = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001534 int i;
1535
1536 for (i = 0; i < header->attrs; i++) {
1537 struct perf_header_attr *attr = header->attr[i];
1538
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001539 if (!sample_type)
1540 sample_type = attr->attr.sample_type;
1541 else if (sample_type != attr->attr.sample_type)
1542 die("non matching sample_type");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001543 }
1544
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001545 return sample_type;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001546}
Peter Zijlstraf5970552009-06-18 23:22:55 +02001547
Ingo Molnard80d3382009-06-03 23:14:49 +02001548static int __cmd_report(void)
1549{
Ingo Molnar75051722009-06-03 23:14:49 +02001550 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001551 unsigned long offset = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001552 unsigned long head, shift;
Ingo Molnard80d3382009-06-03 23:14:49 +02001553 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001554 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001555 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001556 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001557
1558 register_idle_thread();
1559
1560 input = open(input_name, O_RDONLY);
1561 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001562 fprintf(stderr, " failed to open file: %s", input_name);
1563 if (!strcmp(input_name, "perf.data"))
1564 fprintf(stderr, " (try 'perf record' first)");
1565 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001566 exit(-1);
1567 }
1568
1569 ret = fstat(input, &stat);
1570 if (ret < 0) {
1571 perror("failed to stat file");
1572 exit(-1);
1573 }
1574
1575 if (!stat.st_size) {
1576 fprintf(stderr, "zero-sized file, nothing to do!\n");
1577 exit(0);
1578 }
1579
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001580 header = perf_header__read(input);
1581 head = header->data_offset;
Peter Zijlstraf5970552009-06-18 23:22:55 +02001582
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001583 sample_type = perf_header__sample_type();
1584
1585 if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
Peter Zijlstraf5970552009-06-18 23:22:55 +02001586 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1587 exit(-1);
1588 }
1589
Ingo Molnard80d3382009-06-03 23:14:49 +02001590 if (load_kernel() < 0) {
1591 perror("failed to load kernel symbols");
1592 return EXIT_FAILURE;
1593 }
1594
1595 if (!full_paths) {
1596 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1597 perror("failed to get the current directory");
1598 return EXIT_FAILURE;
1599 }
1600 cwdlen = strlen(cwd);
1601 } else {
1602 cwd = NULL;
1603 cwdlen = 0;
1604 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001605
1606 shift = page_size * (head / page_size);
1607 offset += shift;
1608 head -= shift;
1609
Ingo Molnard80d3382009-06-03 23:14:49 +02001610remap:
1611 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1612 MAP_SHARED, input, offset);
1613 if (buf == MAP_FAILED) {
1614 perror("failed to mmap file");
1615 exit(-1);
1616 }
1617
1618more:
1619 event = (event_t *)(buf + head);
1620
1621 size = event->header.size;
1622 if (!size)
1623 size = 8;
1624
1625 if (head + event->header.size >= page_size * mmap_window) {
Ingo Molnard80d3382009-06-03 23:14:49 +02001626 int ret;
1627
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001628 shift = page_size * (head / page_size);
1629
Ingo Molnard80d3382009-06-03 23:14:49 +02001630 ret = munmap(buf, page_size * mmap_window);
1631 assert(ret == 0);
1632
1633 offset += shift;
1634 head -= shift;
1635 goto remap;
1636 }
1637
1638 size = event->header.size;
1639
Ingo Molnar8465b052009-06-14 14:44:07 +02001640 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001641 (void *)(offset + head),
1642 (void *)(long)event->header.size,
1643 event->header.type);
1644
Ingo Molnard80d3382009-06-03 23:14:49 +02001645 if (!size || process_event(event, offset, head) < 0) {
1646
Ingo Molnar35029732009-06-03 09:38:58 +02001647 dprintf("%p [%p]: skipping unknown header type: %d\n",
1648 (void *)(offset + head),
1649 (void *)(long)(event->header.size),
1650 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001651
Ingo Molnar3e706112009-05-26 18:53:17 +02001652 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001653
1654 /*
1655 * assume we lost track of the stream, check alignment, and
1656 * increment a single u64 in the hope to catch on again 'soon'.
1657 */
1658
1659 if (unlikely(head & 7))
1660 head &= ~7ULL;
1661
1662 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001663 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001664
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001665 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001666
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001667 if (offset + head >= header->data_offset + header->data_size)
Peter Zijlstraf5970552009-06-18 23:22:55 +02001668 goto done;
1669
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001670 if (offset + head < stat.st_size)
1671 goto more;
1672
Peter Zijlstraf5970552009-06-18 23:22:55 +02001673done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001674 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001675 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001676
Ingo Molnar35029732009-06-03 09:38:58 +02001677 dprintf(" IP events: %10ld\n", total);
1678 dprintf(" mmap events: %10ld\n", total_mmap);
1679 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001680 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001681 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001682 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001683
Ingo Molnar35029732009-06-03 09:38:58 +02001684 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001685 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001686
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001687 if (verbose >= 3)
1688 threads__fprintf(stdout);
1689
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001690 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001691 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001692
Peter Zijlstra82292892009-06-03 12:37:36 +02001693 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001694 output__resort();
1695 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001696
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001697 return rc;
1698}
1699
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001700static const char * const report_usage[] = {
1701 "perf report [<options>] <command>",
1702 NULL
1703};
1704
1705static const struct option options[] = {
1706 OPT_STRING('i', "input", &input_name, "file",
1707 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001708 OPT_BOOLEAN('v', "verbose", &verbose,
1709 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001710 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1711 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001712 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001713 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001714 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001715 OPT_BOOLEAN('P', "full-paths", &full_paths,
1716 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001717 OPT_STRING('p', "parent", &parent_pattern, "regex",
1718 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001719 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1720 "Only display entries with parent-match"),
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001721 OPT_BOOLEAN('c', "callchain", &callchain, "Display callchains"),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001722 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1723 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001724 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1725 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001726 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1727 "only consider these symbols"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001728 OPT_END()
1729};
1730
Ingo Molnar5352f352009-06-03 10:07:39 +02001731static void setup_sorting(void)
1732{
1733 char *tmp, *tok, *str = strdup(sort_order);
1734
1735 for (tok = strtok_r(str, ", ", &tmp);
1736 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1737 if (sort_dimension__add(tok) < 0) {
1738 error("Unknown --sort key: `%s'", tok);
1739 usage_with_options(report_usage, options);
1740 }
1741 }
1742
1743 free(str);
1744}
1745
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001746static void setup_list(struct strlist **list, const char *list_str,
1747 const char *list_name)
1748{
1749 if (list_str) {
1750 *list = strlist__new(true, list_str);
1751 if (!*list) {
1752 fprintf(stderr, "problems parsing %s list\n",
1753 list_name);
1754 exit(129);
1755 }
1756 }
1757}
1758
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001759int cmd_report(int argc, const char **argv, const char *prefix)
1760{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001761 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001762
1763 page_size = getpagesize();
1764
Ingo Molnaredc52de2009-06-04 16:24:37 +02001765 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001766
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001767 setup_sorting();
1768
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001769 if (parent_pattern != default_parent_pattern)
1770 sort_dimension__add("parent");
1771 else
1772 exclude_other = 0;
1773
Ingo Molnaredc52de2009-06-04 16:24:37 +02001774 /*
1775 * Any (unrecognized) arguments left?
1776 */
1777 if (argc)
1778 usage_with_options(report_usage, options);
1779
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001780 setup_list(&dso_list, dso_list_str, "dso");
1781 setup_list(&comm_list, comm_list_str, "comm");
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001782 setup_list(&sym_list, sym_list_str, "symbol");
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001783
Ingo Molnara930d2c2009-05-27 09:50:13 +02001784 setup_pager();
1785
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001786 return __cmd_report();
1787}