blob: ed391db9e0f8d78034c1ada0713f42f91e89a785 [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 Melo8fa66bd2009-05-18 12:45:42 -030019
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020020#include "perf.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020021#include "util/header.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020022
23#include "util/parse-options.h"
24#include "util/parse-events.h"
25
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030026#define SHOW_KERNEL 1
27#define SHOW_USER 2
28#define SHOW_HV 4
29
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020030static char const *input_name = "perf.data";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020031static char *vmlinux = NULL;
Ingo Molnarbd741372009-06-04 14:13:04 +020032
33static char default_sort_order[] = "comm,dso";
34static char *sort_order = default_sort_order;
35
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030036static int input;
37static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
38
Ingo Molnar97b07b62009-05-26 18:48:58 +020039static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020040#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020041#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
Ingo Molnar35029732009-06-03 09:38:58 +020042
Ingo Molnar16f762a2009-05-27 09:10:38 +020043static int verbose;
Ingo Molnar75220602009-06-18 08:00:17 +020044#define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
45
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030046static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020047
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030048static unsigned long page_size;
49static unsigned long mmap_window = 32;
50
Ingo Molnarb8e6d822009-06-18 14:32:19 +020051static char default_parent_pattern[] = "^sys_|^do_page_fault";
52static char *parent_pattern = default_parent_pattern;
Ingo Molnarb25bcf22009-06-18 07:01:03 +020053static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020054
Ingo Molnarb8e6d822009-06-18 14:32:19 +020055static int exclude_other = 1;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020056static int callchain;
Ingo Molnarb8e6d822009-06-18 14:32:19 +020057
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020058static u64 sample_type;
59
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030060struct ip_event {
61 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100062 u64 ip;
63 u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020064 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030065};
Ingo Molnar75051722009-06-03 23:14:49 +020066
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030067struct mmap_event {
68 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100069 u32 pid, tid;
70 u64 start;
71 u64 len;
72 u64 pgoff;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030073 char filename[PATH_MAX];
74};
Ingo Molnar75051722009-06-03 23:14:49 +020075
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030076struct comm_event {
77 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100078 u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030079 char comm[16];
80};
81
Peter Zijlstra62fc4452009-06-04 16:53:49 +020082struct fork_event {
83 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100084 u32 pid, ppid;
Peter Zijlstra62fc4452009-06-04 16:53:49 +020085};
86
Ingo Molnarb2fef072009-06-05 18:07:51 +020087struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030088 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100089 u64 time;
90 u64 id;
91 u64 sample_period;
Ingo Molnarb2fef072009-06-05 18:07:51 +020092};
93
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020094struct lost_event {
95 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100096 u64 id;
97 u64 lost;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020098};
99
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200100struct read_event {
101 struct perf_event_header header;
102 u32 pid,tid;
103 u64 value;
104 u64 format[3];
105};
106
Ingo Molnarb2fef072009-06-05 18:07:51 +0200107typedef union event_union {
108 struct perf_event_header header;
109 struct ip_event ip;
110 struct mmap_event mmap;
111 struct comm_event comm;
112 struct fork_event fork;
113 struct period_event period;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200114 struct lost_event lost;
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200115 struct read_event read;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300116} event_t;
117
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300118static LIST_HEAD(dsos);
119static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200120static struct dso *vdso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300121
122static void dsos__add(struct dso *dso)
123{
124 list_add_tail(&dso->node, &dsos);
125}
126
127static struct dso *dsos__find(const char *name)
128{
129 struct dso *pos;
130
131 list_for_each_entry(pos, &dsos, node)
132 if (strcmp(pos->name, name) == 0)
133 return pos;
134 return NULL;
135}
136
137static struct dso *dsos__findnew(const char *name)
138{
139 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200140 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300141
Ingo Molnar4593bba2009-06-02 15:34:25 +0200142 if (dso)
143 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300144
Ingo Molnar4593bba2009-06-02 15:34:25 +0200145 dso = dso__new(name, 0);
146 if (!dso)
147 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200148
Ingo Molnarbd741372009-06-04 14:13:04 +0200149 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200150 if (nr < 0) {
Ingo Molnar75220602009-06-18 08:00:17 +0200151 eprintf("Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200152 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300153 }
Ingo Molnar75220602009-06-18 08:00:17 +0200154 if (!nr)
155 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200156
157 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300158
159 return dso;
160
161out_delete_dso:
162 dso__delete(dso);
163 return NULL;
164}
165
Ingo Molnar16f762a2009-05-27 09:10:38 +0200166static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300167{
168 struct dso *pos;
169
170 list_for_each_entry(pos, &dsos, node)
171 dso__fprintf(pos, fp);
172}
173
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000174static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200175{
176 return dso__find_symbol(kernel_dso, ip);
177}
178
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200179static int load_kernel(void)
180{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300181 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200182
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300183 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200184 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300185 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200186
Ingo Molnarbd741372009-06-04 14:13:04 +0200187 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300188 if (err) {
189 dso__delete(kernel_dso);
190 kernel_dso = NULL;
191 } else
192 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200193
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200194 vdso = dso__new("[vdso]", 0);
195 if (!vdso)
196 return -1;
197
198 vdso->find_symbol = vdso__find_symbol;
199
200 dsos__add(vdso);
201
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300202 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200203}
204
Ingo Molnard80d3382009-06-03 23:14:49 +0200205static char __cwd[PATH_MAX];
206static char *cwd = __cwd;
207static int cwdlen;
208
209static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300210{
211 int n = 0;
212
213 while (pathname[n] == cwd[n] && n < cwdlen)
214 ++n;
215
216 return n;
217}
218
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300219struct map {
220 struct list_head node;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000221 u64 start;
222 u64 end;
223 u64 pgoff;
224 u64 (*map_ip)(struct map *, u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300225 struct dso *dso;
226};
227
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000228static u64 map__map_ip(struct map *map, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200229{
230 return ip - map->start + map->pgoff;
231}
232
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000233static u64 vdso__map_ip(struct map *map, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200234{
235 return ip;
236}
237
Pekka Enberg80d496b2009-06-08 21:12:48 +0300238static inline int is_anon_memory(const char *filename)
239{
240 return strcmp(filename, "//anon") == 0;
241}
242
Ingo Molnard80d3382009-06-03 23:14:49 +0200243static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300244{
245 struct map *self = malloc(sizeof(*self));
246
247 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300248 const char *filename = event->filename;
249 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300250 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300251
252 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200253 int n = strcommon(filename);
254
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300255 if (n == cwdlen) {
256 snprintf(newfilename, sizeof(newfilename),
257 ".%s", filename + n);
258 filename = newfilename;
259 }
260 }
261
Pekka Enberg80d496b2009-06-08 21:12:48 +0300262 anon = is_anon_memory(filename);
263
264 if (anon) {
265 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
266 filename = newfilename;
267 }
268
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300269 self->start = event->start;
270 self->end = event->start + event->len;
271 self->pgoff = event->pgoff;
272
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300273 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300274 if (self->dso == NULL)
275 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200276
Pekka Enberg80d496b2009-06-08 21:12:48 +0300277 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200278 self->map_ip = vdso__map_ip;
279 else
280 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300281 }
282 return self;
283out_delete:
284 free(self);
285 return NULL;
286}
287
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200288static struct map *map__clone(struct map *self)
289{
290 struct map *map = malloc(sizeof(*self));
291
292 if (!map)
293 return NULL;
294
295 memcpy(map, self, sizeof(*self));
296
297 return map;
298}
299
300static int map__overlap(struct map *l, struct map *r)
301{
302 if (l->start > r->start) {
303 struct map *t = l;
304 l = r;
305 r = t;
306 }
307
308 if (l->end > r->start)
309 return 1;
310
311 return 0;
312}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300313
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300314static size_t map__fprintf(struct map *self, FILE *fp)
315{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200316 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300317 self->start, self->end, self->pgoff, self->dso->name);
318}
319
320
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300321struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300322 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300323 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300324 pid_t pid;
325 char *comm;
326};
327
328static struct thread *thread__new(pid_t pid)
329{
330 struct thread *self = malloc(sizeof(*self));
331
332 if (self != NULL) {
333 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200334 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200335 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200336 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300337 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300338 }
339
340 return self;
341}
342
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300343static int thread__set_comm(struct thread *self, const char *comm)
344{
Peter Zijlstra82292892009-06-03 12:37:36 +0200345 if (self->comm)
346 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300347 self->comm = strdup(comm);
348 return self->comm ? 0 : -ENOMEM;
349}
350
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300351static size_t thread__fprintf(struct thread *self, FILE *fp)
352{
353 struct map *pos;
354 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
355
356 list_for_each_entry(pos, &self->maps, node)
357 ret += map__fprintf(pos, fp);
358
359 return ret;
360}
361
362
Ingo Molnar16f762a2009-05-27 09:10:38 +0200363static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200364static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300365
366static struct thread *threads__findnew(pid_t pid)
367{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300368 struct rb_node **p = &threads.rb_node;
369 struct rb_node *parent = NULL;
370 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300371
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200372 /*
373 * Font-end cache - PID lookups come in blocks,
374 * so most of the time we dont have to look up
375 * the full rbtree:
376 */
377 if (last_match && last_match->pid == pid)
378 return last_match;
379
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300380 while (*p != NULL) {
381 parent = *p;
382 th = rb_entry(parent, struct thread, rb_node);
383
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200384 if (th->pid == pid) {
385 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300386 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200387 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300388
389 if (pid < th->pid)
390 p = &(*p)->rb_left;
391 else
392 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300393 }
394
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300395 th = thread__new(pid);
396 if (th != NULL) {
397 rb_link_node(&th->rb_node, parent, p);
398 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200399 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300400 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200401
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300402 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300403}
404
405static void thread__insert_map(struct thread *self, struct map *map)
406{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200407 struct map *pos, *tmp;
408
409 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
410 if (map__overlap(pos, map)) {
Peter Zijlstra3d906ef2009-06-23 11:23:07 +0200411 if (verbose >= 2) {
412 printf("overlapping maps:\n");
413 map__fprintf(map, stdout);
414 map__fprintf(pos, stdout);
415 }
416
417 if (map->start <= pos->start && map->end > pos->start)
418 pos->start = map->end;
419
420 if (map->end >= pos->end && map->start < pos->end)
421 pos->end = map->start;
422
423 if (verbose >= 2) {
424 printf("after collision:\n");
425 map__fprintf(pos, stdout);
426 }
427
428 if (pos->start >= pos->end) {
429 list_del_init(&pos->node);
430 free(pos);
431 }
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200432 }
433 }
434
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300435 list_add_tail(&map->node, &self->maps);
436}
437
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200438static int thread__fork(struct thread *self, struct thread *parent)
439{
440 struct map *map;
441
442 if (self->comm)
443 free(self->comm);
444 self->comm = strdup(parent->comm);
445 if (!self->comm)
446 return -ENOMEM;
447
448 list_for_each_entry(map, &parent->maps, node) {
449 struct map *new = map__clone(map);
450 if (!new)
451 return -ENOMEM;
452 thread__insert_map(self, new);
453 }
454
455 return 0;
456}
457
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000458static struct map *thread__find_map(struct thread *self, u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300459{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200460 struct map *pos;
461
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300462 if (self == NULL)
463 return NULL;
464
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300465 list_for_each_entry(pos, &self->maps, node)
466 if (ip >= pos->start && ip <= pos->end)
467 return pos;
468
469 return NULL;
470}
471
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300472static size_t threads__fprintf(FILE *fp)
473{
474 size_t ret = 0;
475 struct rb_node *nd;
476
477 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
478 struct thread *pos = rb_entry(nd, struct thread, rb_node);
479
480 ret += thread__fprintf(pos, fp);
481 }
482
483 return ret;
484}
485
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200486/*
487 * histogram, sorted on item, collects counts
488 */
489
490static struct rb_root hist;
491
492struct hist_entry {
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200493 struct rb_node rb_node;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200494
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200495 struct thread *thread;
496 struct map *map;
497 struct dso *dso;
498 struct symbol *sym;
499 struct symbol *parent;
500 u64 ip;
501 char level;
502 struct callchain_node callchain;
503 struct rb_root sorted_chain;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200504
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200505 u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200506};
507
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200508/*
509 * configurable sorting bits
510 */
511
512struct sort_entry {
513 struct list_head list;
514
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200515 char *header;
516
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200517 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200518 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200519 size_t (*print)(FILE *fp, struct hist_entry *);
520};
521
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200522static int64_t cmp_null(void *l, void *r)
523{
524 if (!l && !r)
525 return 0;
526 else if (!l)
527 return -1;
528 else
529 return 1;
530}
531
Peter Zijlstra82292892009-06-03 12:37:36 +0200532/* --sort pid */
533
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200534static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200535sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
536{
537 return right->thread->pid - left->thread->pid;
538}
539
540static size_t
541sort__thread_print(FILE *fp, struct hist_entry *self)
542{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200543 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200544}
545
546static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200547 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200548 .cmp = sort__thread_cmp,
549 .print = sort__thread_print,
550};
551
Peter Zijlstra82292892009-06-03 12:37:36 +0200552/* --sort comm */
553
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200554static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200555sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
556{
Peter Zijlstra82292892009-06-03 12:37:36 +0200557 return right->thread->pid - left->thread->pid;
558}
559
560static int64_t
561sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
562{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200563 char *comm_l = left->thread->comm;
564 char *comm_r = right->thread->comm;
565
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200566 if (!comm_l || !comm_r)
567 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200568
569 return strcmp(comm_l, comm_r);
570}
571
572static size_t
573sort__comm_print(FILE *fp, struct hist_entry *self)
574{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200575 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200576}
577
578static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200579 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200580 .cmp = sort__comm_cmp,
581 .collapse = sort__comm_collapse,
582 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200583};
584
Peter Zijlstra82292892009-06-03 12:37:36 +0200585/* --sort dso */
586
Peter Zijlstra992444b2009-05-27 20:20:27 +0200587static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200588sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
589{
590 struct dso *dso_l = left->dso;
591 struct dso *dso_r = right->dso;
592
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200593 if (!dso_l || !dso_r)
594 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200595
596 return strcmp(dso_l->name, dso_r->name);
597}
598
599static size_t
600sort__dso_print(FILE *fp, struct hist_entry *self)
601{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200602 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200603 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200604
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000605 return fprintf(fp, "%016llx ", (u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200606}
607
608static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200609 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200610 .cmp = sort__dso_cmp,
611 .print = sort__dso_print,
612};
613
Peter Zijlstra82292892009-06-03 12:37:36 +0200614/* --sort symbol */
615
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200616static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200617sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300618{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000619 u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200620
621 if (left->sym == right->sym)
622 return 0;
623
624 ip_l = left->sym ? left->sym->start : left->ip;
625 ip_r = right->sym ? right->sym->start : right->ip;
626
627 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300628}
629
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200630static size_t
631sort__sym_print(FILE *fp, struct hist_entry *self)
632{
633 size_t ret = 0;
634
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200635 if (verbose)
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000636 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200637
Ingo Molnar8edd4282009-06-05 14:13:18 +0200638 if (self->sym) {
639 ret += fprintf(fp, "[%c] %s",
640 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
641 } else {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000642 ret += fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200643 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200644
645 return ret;
646}
647
648static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200649 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200650 .cmp = sort__sym_cmp,
651 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200652};
653
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200654/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200655
656static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200657sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200658{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200659 struct symbol *sym_l = left->parent;
660 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200661
662 if (!sym_l || !sym_r)
663 return cmp_null(sym_l, sym_r);
664
665 return strcmp(sym_l->name, sym_r->name);
666}
667
668static size_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200669sort__parent_print(FILE *fp, struct hist_entry *self)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200670{
671 size_t ret = 0;
672
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200673 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200674
675 return ret;
676}
677
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200678static struct sort_entry sort_parent = {
679 .header = "Parent symbol ",
680 .cmp = sort__parent_cmp,
681 .print = sort__parent_print,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200682};
683
Peter Zijlstra82292892009-06-03 12:37:36 +0200684static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200685static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200686
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200687struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200688 char *name;
689 struct sort_entry *entry;
690 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200691};
692
693static struct sort_dimension sort_dimensions[] = {
694 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200695 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200696 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200697 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200698 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200699};
700
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200701static LIST_HEAD(hist_entry__sort_list);
702
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200703static int sort_dimension__add(char *tok)
704{
705 int i;
706
707 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
708 struct sort_dimension *sd = &sort_dimensions[i];
709
710 if (sd->taken)
711 continue;
712
Ingo Molnar5352f352009-06-03 10:07:39 +0200713 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200714 continue;
715
Peter Zijlstra82292892009-06-03 12:37:36 +0200716 if (sd->entry->collapse)
717 sort__need_collapse = 1;
718
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200719 if (sd->entry == &sort_parent) {
720 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200721 if (ret) {
722 char err[BUFSIZ];
723
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200724 regerror(ret, &parent_regex, err, sizeof(err));
725 fprintf(stderr, "Invalid regex: %s\n%s",
726 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200727 exit(-1);
728 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200729 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200730 }
731
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200732 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
733 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200734
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200735 return 0;
736 }
737
738 return -ESRCH;
739}
740
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200741static int64_t
742hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
743{
744 struct sort_entry *se;
745 int64_t cmp = 0;
746
747 list_for_each_entry(se, &hist_entry__sort_list, list) {
748 cmp = se->cmp(left, right);
749 if (cmp)
750 break;
751 }
752
753 return cmp;
754}
755
Peter Zijlstra82292892009-06-03 12:37:36 +0200756static int64_t
757hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
758{
759 struct sort_entry *se;
760 int64_t cmp = 0;
761
762 list_for_each_entry(se, &hist_entry__sort_list, list) {
763 int64_t (*f)(struct hist_entry *, struct hist_entry *);
764
765 f = se->collapse ?: se->cmp;
766
767 cmp = f(left, right);
768 if (cmp)
769 break;
770 }
771
772 return cmp;
773}
774
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200775static size_t
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200776callchain__fprintf(FILE *fp, struct callchain_node *self, u64 total_samples)
777{
778 struct callchain_list *chain;
779 size_t ret = 0;
780
781 if (!self)
782 return 0;
783
784 ret += callchain__fprintf(fp, self->parent, total_samples);
785
786
787 list_for_each_entry(chain, &self->val, list)
788 ret += fprintf(fp, " %p\n", (void *)chain->ip);
789
790 return ret;
791}
792
793static size_t
794hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
795 u64 total_samples)
796{
797 struct rb_node *rb_node;
798 struct callchain_node *chain;
799 size_t ret = 0;
800
801 rb_node = rb_first(&self->sorted_chain);
802 while (rb_node) {
803 double percent;
804
805 chain = rb_entry(rb_node, struct callchain_node, rb_node);
806 percent = chain->hit * 100.0 / total_samples;
807 ret += fprintf(fp, " %6.2f%%\n", percent);
808 ret += callchain__fprintf(fp, chain, total_samples);
809 ret += fprintf(fp, "\n");
810 rb_node = rb_next(rb_node);
811 }
812
813 return ret;
814}
815
816
817static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000818hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200819{
820 struct sort_entry *se;
821 size_t ret;
822
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200823 if (exclude_other && !self->parent)
824 return 0;
825
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200826 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200827 double percent = self->count * 100.0 / total_samples;
828 char *color = PERF_COLOR_NORMAL;
829
830 /*
Ingo Molnaraefcf372009-06-08 23:15:28 +0200831 * We color high-overhead entries in red, mid-overhead
832 * entries in green - and keep the low overhead places
833 * normal:
Ingo Molnar8fc03212009-06-04 15:19:47 +0200834 */
Ingo Molnaraefcf372009-06-08 23:15:28 +0200835 if (percent >= 5.0) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200836 color = PERF_COLOR_RED;
Ingo Molnaraefcf372009-06-08 23:15:28 +0200837 } else {
838 if (percent >= 0.5)
839 color = PERF_COLOR_GREEN;
840 }
Ingo Molnar8fc03212009-06-04 15:19:47 +0200841
842 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200843 (self->count * 100.0) / total_samples);
844 } else
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200845 ret = fprintf(fp, "%12Ld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200846
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200847 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200848 if (exclude_other && (se == &sort_parent))
849 continue;
850
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200851 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200852 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200853 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200854
855 ret += fprintf(fp, "\n");
856
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200857 if (callchain)
858 hist_entry_callchain__fprintf(fp, self, total_samples);
859
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200860 return ret;
861}
862
863/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200864 *
865 */
866
867static struct symbol *
868resolve_symbol(struct thread *thread, struct map **mapp,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000869 struct dso **dsop, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200870{
871 struct dso *dso = dsop ? *dsop : NULL;
872 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c342009-06-22 16:52:51 +0200873 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200874
875 if (!thread)
876 return NULL;
877
878 if (dso)
879 goto got_dso;
880
881 if (map)
882 goto got_map;
883
884 map = thread__find_map(thread, ip);
885 if (map != NULL) {
886 if (mapp)
887 *mapp = map;
888got_map:
889 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200890
891 dso = map->dso;
892 } else {
893 /*
894 * If this is outside of all known maps,
895 * and is a negative address, try to look it
896 * up in the kernel dso, as it might be a
897 * vsyscall (which executes in user-mode):
898 */
899 if ((long long)ip < 0)
900 dso = kernel_dso;
901 }
902 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Peter Zijlstra520f2c342009-06-22 16:52:51 +0200903 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
904 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200905
906 if (dsop)
907 *dsop = dso;
908
909 if (!dso)
910 return NULL;
911got_dso:
912 return dso->find_symbol(dso, ip);
913}
914
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200915static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200916{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200917 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200918 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200919
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200920 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200921}
922
923/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200924 * collect histogram counts
925 */
926
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200927static int
928hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000929 struct symbol *sym, u64 ip, struct ip_callchain *chain,
930 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300931{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200932 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300933 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200934 struct hist_entry *he;
935 struct hist_entry entry = {
936 .thread = thread,
937 .map = map,
938 .dso = dso,
939 .sym = sym,
940 .ip = ip,
941 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200942 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200943 .parent = NULL,
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200944 .sorted_chain = RB_ROOT
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200945 };
946 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300947
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200948 if (sort__has_parent && chain) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000949 u64 context = PERF_CONTEXT_MAX;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200950 int i;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200951
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200952 for (i = 0; i < chain->nr; i++) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000953 u64 ip = chain->ips[i];
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200954 struct dso *dso = NULL;
955 struct symbol *sym;
956
957 if (ip >= PERF_CONTEXT_MAX) {
958 context = ip;
959 continue;
960 }
961
962 switch (context) {
963 case PERF_CONTEXT_KERNEL:
964 dso = kernel_dso;
965 break;
966 default:
967 break;
968 }
969
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200970 sym = resolve_symbol(thread, NULL, &dso, &ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200971
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200972 if (sym && call__match(sym)) {
973 entry.parent = sym;
974 break;
975 }
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200976 }
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200977 }
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200978
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300979 while (*p != NULL) {
980 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200981 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300982
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200983 cmp = hist_entry__cmp(&entry, he);
984
985 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200986 he->count += count;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200987 if (callchain)
988 append_chain(&he->callchain, chain);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200989 return 0;
990 }
991
992 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300993 p = &(*p)->rb_left;
994 else
995 p = &(*p)->rb_right;
996 }
997
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200998 he = malloc(sizeof(*he));
999 if (!he)
1000 return -ENOMEM;
1001 *he = entry;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001002 if (callchain) {
1003 callchain_init(&he->callchain);
1004 append_chain(&he->callchain, chain);
1005 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001006 rb_link_node(&he->rb_node, parent, p);
1007 rb_insert_color(&he->rb_node, &hist);
1008
1009 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001010}
1011
Peter Zijlstra82292892009-06-03 12:37:36 +02001012static void hist_entry__free(struct hist_entry *he)
1013{
1014 free(he);
1015}
1016
1017/*
1018 * collapse the histogram
1019 */
1020
1021static struct rb_root collapse_hists;
1022
1023static void collapse__insert_entry(struct hist_entry *he)
1024{
1025 struct rb_node **p = &collapse_hists.rb_node;
1026 struct rb_node *parent = NULL;
1027 struct hist_entry *iter;
1028 int64_t cmp;
1029
1030 while (*p != NULL) {
1031 parent = *p;
1032 iter = rb_entry(parent, struct hist_entry, rb_node);
1033
1034 cmp = hist_entry__collapse(iter, he);
1035
1036 if (!cmp) {
1037 iter->count += he->count;
1038 hist_entry__free(he);
1039 return;
1040 }
1041
1042 if (cmp < 0)
1043 p = &(*p)->rb_left;
1044 else
1045 p = &(*p)->rb_right;
1046 }
1047
1048 rb_link_node(&he->rb_node, parent, p);
1049 rb_insert_color(&he->rb_node, &collapse_hists);
1050}
1051
1052static void collapse__resort(void)
1053{
1054 struct rb_node *next;
1055 struct hist_entry *n;
1056
1057 if (!sort__need_collapse)
1058 return;
1059
1060 next = rb_first(&hist);
1061 while (next) {
1062 n = rb_entry(next, struct hist_entry, rb_node);
1063 next = rb_next(&n->rb_node);
1064
1065 rb_erase(&n->rb_node, &hist);
1066 collapse__insert_entry(n);
1067 }
1068}
1069
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001070/*
1071 * reverse the map, sort on count.
1072 */
1073
1074static struct rb_root output_hists;
1075
1076static void output__insert_entry(struct hist_entry *he)
1077{
1078 struct rb_node **p = &output_hists.rb_node;
1079 struct rb_node *parent = NULL;
1080 struct hist_entry *iter;
1081
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001082 if (callchain)
1083 sort_chain_to_rbtree(&he->sorted_chain, &he->callchain);
1084
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001085 while (*p != NULL) {
1086 parent = *p;
1087 iter = rb_entry(parent, struct hist_entry, rb_node);
1088
1089 if (he->count > iter->count)
1090 p = &(*p)->rb_left;
1091 else
1092 p = &(*p)->rb_right;
1093 }
1094
1095 rb_link_node(&he->rb_node, parent, p);
1096 rb_insert_color(&he->rb_node, &output_hists);
1097}
1098
1099static void output__resort(void)
1100{
Peter Zijlstra82292892009-06-03 12:37:36 +02001101 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001102 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43bea2009-06-03 23:02:33 -03001103 struct rb_root *tree = &hist;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001104
Peter Zijlstra82292892009-06-03 12:37:36 +02001105 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43bea2009-06-03 23:02:33 -03001106 tree = &collapse_hists;
1107
1108 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001109
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001110 while (next) {
1111 n = rb_entry(next, struct hist_entry, rb_node);
1112 next = rb_next(&n->rb_node);
1113
Arnaldo Carvalho de Meloa4c43bea2009-06-03 23:02:33 -03001114 rb_erase(&n->rb_node, tree);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001115 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001116 }
1117}
1118
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001119static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001120{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001121 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001122 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001123 struct rb_node *nd;
1124 size_t ret = 0;
1125
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001126 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001127 fprintf(fp, "#\n");
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001128 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001129 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001130
1131 fprintf(fp, "# Overhead");
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001132 list_for_each_entry(se, &hist_entry__sort_list, list) {
1133 if (exclude_other && (se == &sort_parent))
1134 continue;
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001135 fprintf(fp, " %s", se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001136 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001137 fprintf(fp, "\n");
1138
1139 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001140 list_for_each_entry(se, &hist_entry__sort_list, list) {
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001141 int i;
1142
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001143 if (exclude_other && (se == &sort_parent))
1144 continue;
1145
Ingo Molnar4593bba2009-06-02 15:34:25 +02001146 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001147 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001148 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001149 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001150 fprintf(fp, "\n");
1151
1152 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001153
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001154 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1155 pos = rb_entry(nd, struct hist_entry, rb_node);
1156 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001157 }
1158
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001159 if (sort_order == default_sort_order &&
1160 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001161 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001162 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001163 fprintf(fp, "#\n");
1164 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001165 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001166
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001167 return ret;
1168}
1169
Peter Zijlstra436224a2009-06-02 21:02:36 +02001170static void register_idle_thread(void)
1171{
1172 struct thread *thread = threads__findnew(0);
1173
1174 if (thread == NULL ||
1175 thread__set_comm(thread, "[idle]")) {
1176 fprintf(stderr, "problem inserting idle task.\n");
1177 exit(-1);
1178 }
1179}
1180
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001181static unsigned long total = 0,
1182 total_mmap = 0,
1183 total_comm = 0,
1184 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001185 total_unknown = 0,
1186 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001187
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001188static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +02001189{
1190 unsigned int chain_size;
1191
Ingo Molnar75220602009-06-18 08:00:17 +02001192 chain_size = event->header.size;
1193 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1194
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001195 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +02001196 return -1;
1197
1198 return 0;
1199}
1200
Ingo Molnard80d3382009-06-03 23:14:49 +02001201static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001202process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +02001203{
1204 char level;
1205 int show = 0;
1206 struct dso *dso = NULL;
1207 struct thread *thread = threads__findnew(event->ip.pid);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001208 u64 ip = event->ip.ip;
1209 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001210 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001211 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001212 struct ip_callchain *chain = NULL;
Ingo Molnar75051722009-06-03 23:14:49 +02001213
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001214 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001215 period = *(u64 *)more_data;
1216 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001217 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001218
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001219 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001220 (void *)(offset + head),
1221 (void *)(long)(event->header.size),
1222 event->header.misc,
1223 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001224 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001225 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001226
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001227 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001228 int i;
1229
1230 chain = (void *)more_data;
1231
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001232 dprintf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001233
Ingo Molnar75220602009-06-18 08:00:17 +02001234 if (validate_chain(chain, event) < 0) {
1235 eprintf("call-chain problem with event, skipping it.\n");
1236 return 0;
1237 }
1238
1239 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001240 for (i = 0; i < chain->nr; i++)
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001241 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001242 }
1243 }
1244
Ingo Molnar75051722009-06-03 23:14:49 +02001245 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1246
1247 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001248 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001249 event->header.type);
1250 return -1;
1251 }
1252
1253 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1254 show = SHOW_KERNEL;
1255 level = 'k';
1256
1257 dso = kernel_dso;
1258
1259 dprintf(" ...... dso: %s\n", dso->name);
1260
1261 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1262
1263 show = SHOW_USER;
1264 level = '.';
1265
Ingo Molnar75051722009-06-03 23:14:49 +02001266 } else {
1267 show = SHOW_HV;
1268 level = 'H';
1269 dprintf(" ...... dso: [hypervisor]\n");
1270 }
1271
1272 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001273 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001274
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001275 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001276 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001277 return -1;
1278 }
1279 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001280 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001281
1282 return 0;
1283}
1284
1285static int
1286process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1287{
1288 struct thread *thread = threads__findnew(event->mmap.pid);
1289 struct map *map = map__new(&event->mmap);
1290
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001291 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001292 (void *)(offset + head),
1293 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001294 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001295 (void *)(long)event->mmap.start,
1296 (void *)(long)event->mmap.len,
1297 (void *)(long)event->mmap.pgoff,
1298 event->mmap.filename);
1299
1300 if (thread == NULL || map == NULL) {
1301 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001302 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001303 }
1304
1305 thread__insert_map(thread, map);
1306 total_mmap++;
1307
1308 return 0;
1309}
1310
1311static int
1312process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1313{
1314 struct thread *thread = threads__findnew(event->comm.pid);
1315
1316 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1317 (void *)(offset + head),
1318 (void *)(long)(event->header.size),
1319 event->comm.comm, event->comm.pid);
1320
1321 if (thread == NULL ||
1322 thread__set_comm(thread, event->comm.comm)) {
1323 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1324 return -1;
1325 }
1326 total_comm++;
1327
1328 return 0;
1329}
1330
1331static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001332process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1333{
1334 struct thread *thread = threads__findnew(event->fork.pid);
1335 struct thread *parent = threads__findnew(event->fork.ppid);
1336
1337 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1338 (void *)(offset + head),
1339 (void *)(long)(event->header.size),
1340 event->fork.pid, event->fork.ppid);
1341
1342 if (!thread || !parent || thread__fork(thread, parent)) {
1343 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1344 return -1;
1345 }
1346 total_fork++;
1347
1348 return 0;
1349}
1350
1351static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001352process_period_event(event_t *event, unsigned long offset, unsigned long head)
1353{
1354 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1355 (void *)(offset + head),
1356 (void *)(long)(event->header.size),
1357 event->period.time,
1358 event->period.id,
1359 event->period.sample_period);
1360
1361 return 0;
1362}
1363
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001364static int
1365process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1366{
1367 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1368 (void *)(offset + head),
1369 (void *)(long)(event->header.size),
1370 event->lost.id,
1371 event->lost.lost);
1372
1373 total_lost += event->lost.lost;
1374
1375 return 0;
1376}
1377
Ingo Molnar8465b052009-06-14 14:44:07 +02001378static void trace_event(event_t *event)
1379{
1380 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001381 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001382 int i, j;
1383
1384 if (!dump_trace)
1385 return;
1386
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001387 dprintf(".");
1388 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001389
1390 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001391 if ((i & 15) == 0) {
1392 dprintf(".");
1393 cdprintf(" %04x: ", i);
1394 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001395
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001396 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001397
1398 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001399 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001400 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001401 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001402 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001403 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001404 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001405 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001406 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001407 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001408 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001409 }
1410 }
1411 dprintf(".\n");
1412}
1413
Ingo Molnarb2fef072009-06-05 18:07:51 +02001414static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001415process_read_event(event_t *event, unsigned long offset, unsigned long head)
1416{
1417 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1418 (void *)(offset + head),
1419 (void *)(long)(event->header.size),
1420 event->read.pid,
1421 event->read.tid,
1422 event->read.value);
1423
1424 return 0;
1425}
1426
1427static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001428process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001429{
Ingo Molnar8465b052009-06-14 14:44:07 +02001430 trace_event(event);
1431
Ingo Molnar75051722009-06-03 23:14:49 +02001432 switch (event->header.type) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001433 case PERF_EVENT_SAMPLE:
1434 return process_sample_event(event, offset, head);
1435
Ingo Molnar75051722009-06-03 23:14:49 +02001436 case PERF_EVENT_MMAP:
1437 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001438
Ingo Molnar75051722009-06-03 23:14:49 +02001439 case PERF_EVENT_COMM:
1440 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001441
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001442 case PERF_EVENT_FORK:
1443 return process_fork_event(event, offset, head);
1444
Ingo Molnarb2fef072009-06-05 18:07:51 +02001445 case PERF_EVENT_PERIOD:
1446 return process_period_event(event, offset, head);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001447
1448 case PERF_EVENT_LOST:
1449 return process_lost_event(event, offset, head);
1450
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001451 case PERF_EVENT_READ:
1452 return process_read_event(event, offset, head);
1453
Ingo Molnard11444d2009-06-03 23:29:14 +02001454 /*
1455 * We dont process them right now but they are fine:
1456 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001457
Ingo Molnard11444d2009-06-03 23:29:14 +02001458 case PERF_EVENT_THROTTLE:
1459 case PERF_EVENT_UNTHROTTLE:
1460 return 0;
1461
Ingo Molnard80d3382009-06-03 23:14:49 +02001462 default:
1463 return -1;
1464 }
1465
1466 return 0;
1467}
1468
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001469static struct perf_header *header;
1470
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001471static u64 perf_header__sample_type(void)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001472{
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001473 u64 sample_type = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001474 int i;
1475
1476 for (i = 0; i < header->attrs; i++) {
1477 struct perf_header_attr *attr = header->attr[i];
1478
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001479 if (!sample_type)
1480 sample_type = attr->attr.sample_type;
1481 else if (sample_type != attr->attr.sample_type)
1482 die("non matching sample_type");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001483 }
1484
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001485 return sample_type;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001486}
Peter Zijlstraf5970552009-06-18 23:22:55 +02001487
Ingo Molnard80d3382009-06-03 23:14:49 +02001488static int __cmd_report(void)
1489{
Ingo Molnar75051722009-06-03 23:14:49 +02001490 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001491 unsigned long offset = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001492 unsigned long head, shift;
Ingo Molnard80d3382009-06-03 23:14:49 +02001493 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001494 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001495 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001496 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001497
1498 register_idle_thread();
1499
1500 input = open(input_name, O_RDONLY);
1501 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001502 fprintf(stderr, " failed to open file: %s", input_name);
1503 if (!strcmp(input_name, "perf.data"))
1504 fprintf(stderr, " (try 'perf record' first)");
1505 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001506 exit(-1);
1507 }
1508
1509 ret = fstat(input, &stat);
1510 if (ret < 0) {
1511 perror("failed to stat file");
1512 exit(-1);
1513 }
1514
1515 if (!stat.st_size) {
1516 fprintf(stderr, "zero-sized file, nothing to do!\n");
1517 exit(0);
1518 }
1519
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001520 header = perf_header__read(input);
1521 head = header->data_offset;
Peter Zijlstraf5970552009-06-18 23:22:55 +02001522
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001523 sample_type = perf_header__sample_type();
1524
1525 if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
Peter Zijlstraf5970552009-06-18 23:22:55 +02001526 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1527 exit(-1);
1528 }
1529
Ingo Molnard80d3382009-06-03 23:14:49 +02001530 if (load_kernel() < 0) {
1531 perror("failed to load kernel symbols");
1532 return EXIT_FAILURE;
1533 }
1534
1535 if (!full_paths) {
1536 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1537 perror("failed to get the current directory");
1538 return EXIT_FAILURE;
1539 }
1540 cwdlen = strlen(cwd);
1541 } else {
1542 cwd = NULL;
1543 cwdlen = 0;
1544 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001545
1546 shift = page_size * (head / page_size);
1547 offset += shift;
1548 head -= shift;
1549
Ingo Molnard80d3382009-06-03 23:14:49 +02001550remap:
1551 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1552 MAP_SHARED, input, offset);
1553 if (buf == MAP_FAILED) {
1554 perror("failed to mmap file");
1555 exit(-1);
1556 }
1557
1558more:
1559 event = (event_t *)(buf + head);
1560
1561 size = event->header.size;
1562 if (!size)
1563 size = 8;
1564
1565 if (head + event->header.size >= page_size * mmap_window) {
Ingo Molnard80d3382009-06-03 23:14:49 +02001566 int ret;
1567
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001568 shift = page_size * (head / page_size);
1569
Ingo Molnard80d3382009-06-03 23:14:49 +02001570 ret = munmap(buf, page_size * mmap_window);
1571 assert(ret == 0);
1572
1573 offset += shift;
1574 head -= shift;
1575 goto remap;
1576 }
1577
1578 size = event->header.size;
1579
Ingo Molnar8465b052009-06-14 14:44:07 +02001580 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001581 (void *)(offset + head),
1582 (void *)(long)event->header.size,
1583 event->header.type);
1584
Ingo Molnard80d3382009-06-03 23:14:49 +02001585 if (!size || process_event(event, offset, head) < 0) {
1586
Ingo Molnar35029732009-06-03 09:38:58 +02001587 dprintf("%p [%p]: skipping unknown header type: %d\n",
1588 (void *)(offset + head),
1589 (void *)(long)(event->header.size),
1590 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001591
Ingo Molnar3e706112009-05-26 18:53:17 +02001592 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001593
1594 /*
1595 * assume we lost track of the stream, check alignment, and
1596 * increment a single u64 in the hope to catch on again 'soon'.
1597 */
1598
1599 if (unlikely(head & 7))
1600 head &= ~7ULL;
1601
1602 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001603 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001604
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001605 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001606
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001607 if (offset + head >= header->data_offset + header->data_size)
Peter Zijlstraf5970552009-06-18 23:22:55 +02001608 goto done;
1609
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001610 if (offset + head < stat.st_size)
1611 goto more;
1612
Peter Zijlstraf5970552009-06-18 23:22:55 +02001613done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001614 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001615 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001616
Ingo Molnar35029732009-06-03 09:38:58 +02001617 dprintf(" IP events: %10ld\n", total);
1618 dprintf(" mmap events: %10ld\n", total_mmap);
1619 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001620 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001621 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001622 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001623
Ingo Molnar35029732009-06-03 09:38:58 +02001624 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001625 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001626
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001627 if (verbose >= 3)
1628 threads__fprintf(stdout);
1629
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001630 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001631 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001632
Peter Zijlstra82292892009-06-03 12:37:36 +02001633 collapse__resort();
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001634 output__resort();
1635 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001636
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001637 return rc;
1638}
1639
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001640static const char * const report_usage[] = {
1641 "perf report [<options>] <command>",
1642 NULL
1643};
1644
1645static const struct option options[] = {
1646 OPT_STRING('i', "input", &input_name, "file",
1647 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001648 OPT_BOOLEAN('v', "verbose", &verbose,
1649 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001650 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1651 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001652 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001653 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001654 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001655 OPT_BOOLEAN('P', "full-paths", &full_paths,
1656 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001657 OPT_STRING('p', "parent", &parent_pattern, "regex",
1658 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001659 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1660 "Only display entries with parent-match"),
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001661 OPT_BOOLEAN('c', "callchain", &callchain, "Display callchains"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001662 OPT_END()
1663};
1664
Ingo Molnar5352f352009-06-03 10:07:39 +02001665static void setup_sorting(void)
1666{
1667 char *tmp, *tok, *str = strdup(sort_order);
1668
1669 for (tok = strtok_r(str, ", ", &tmp);
1670 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1671 if (sort_dimension__add(tok) < 0) {
1672 error("Unknown --sort key: `%s'", tok);
1673 usage_with_options(report_usage, options);
1674 }
1675 }
1676
1677 free(str);
1678}
1679
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001680int cmd_report(int argc, const char **argv, const char *prefix)
1681{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001682 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001683
1684 page_size = getpagesize();
1685
Ingo Molnaredc52de2009-06-04 16:24:37 +02001686 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001687
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001688 setup_sorting();
1689
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001690 if (parent_pattern != default_parent_pattern)
1691 sort_dimension__add("parent");
1692 else
1693 exclude_other = 0;
1694
Ingo Molnaredc52de2009-06-04 16:24:37 +02001695 /*
1696 * Any (unrecognized) arguments left?
1697 */
1698 if (argc)
1699 usage_with_options(report_usage, options);
1700
Ingo Molnara930d2c2009-05-27 09:50:13 +02001701 setup_pager();
1702
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001703 return __cmd_report();
1704}