blob: f3422121d858d125b79baf51a762df0214eb8653 [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 Melo5da50252009-07-01 14:46:08 -030013#include <linux/list.h>
Ingo Molnara930d2c2009-05-27 09:50:13 +020014#include "util/cache.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030015#include <linux/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 Melo52d422d2009-07-10 22:47:28 -030036static char *dso_list_str, *comm_list_str, *sym_list_str,
37 *col_width_list_str;
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -030038static struct strlist *dso_list, *comm_list, *sym_list;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030039static char *field_sep;
Ingo Molnarbd741372009-06-04 14:13:04 +020040
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030041static int input;
42static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
43
Ingo Molnar97b07b62009-05-26 18:48:58 +020044static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020045#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020046#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
Ingo Molnar35029732009-06-03 09:38:58 +020047
Ingo Molnar16f762a2009-05-27 09:10:38 +020048static int verbose;
Ingo Molnar75220602009-06-18 08:00:17 +020049#define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
50
Mike Galbraith42976482009-07-02 08:09:46 +020051static int modules;
52
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030053static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020054
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030055static unsigned long page_size;
56static unsigned long mmap_window = 32;
57
Ingo Molnarb8e6d822009-06-18 14:32:19 +020058static char default_parent_pattern[] = "^sys_|^do_page_fault";
59static char *parent_pattern = default_parent_pattern;
Ingo Molnarb25bcf22009-06-18 07:01:03 +020060static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020061
Ingo Molnarb8e6d822009-06-18 14:32:19 +020062static int exclude_other = 1;
Frederic Weisbeckerbe903882009-07-05 07:39:18 +020063
Frederic Weisbecker805d1272009-07-05 07:39:21 +020064static char callchain_default_opt[] = "fractal,0.5";
65
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020066static int callchain;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020067
68static
69struct callchain_param callchain_param = {
70 .mode = CHAIN_GRAPH_ABS,
71 .min_percent = 0.5
72};
Ingo Molnarb8e6d822009-06-18 14:32:19 +020073
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020074static u64 sample_type;
75
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030076struct ip_event {
77 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100078 u64 ip;
79 u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020080 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030081};
Ingo Molnar75051722009-06-03 23:14:49 +020082
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030083struct mmap_event {
84 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100085 u32 pid, tid;
86 u64 start;
87 u64 len;
88 u64 pgoff;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030089 char filename[PATH_MAX];
90};
Ingo Molnar75051722009-06-03 23:14:49 +020091
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030092struct comm_event {
93 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100094 u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030095 char comm[16];
96};
97
Peter Zijlstra62fc4452009-06-04 16:53:49 +020098struct fork_event {
99 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000100 u32 pid, ppid;
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200101};
102
Ingo Molnarb2fef072009-06-05 18:07:51 +0200103struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300104 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000105 u64 time;
106 u64 id;
107 u64 sample_period;
Ingo Molnarb2fef072009-06-05 18:07:51 +0200108};
109
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200110struct lost_event {
111 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000112 u64 id;
113 u64 lost;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200114};
115
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200116struct read_event {
117 struct perf_event_header header;
118 u32 pid,tid;
119 u64 value;
120 u64 format[3];
121};
122
Ingo Molnarb2fef072009-06-05 18:07:51 +0200123typedef union event_union {
124 struct perf_event_header header;
125 struct ip_event ip;
126 struct mmap_event mmap;
127 struct comm_event comm;
128 struct fork_event fork;
129 struct period_event period;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200130 struct lost_event lost;
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200131 struct read_event read;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300132} event_t;
133
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300134static int repsep_fprintf(FILE *fp, const char *fmt, ...)
135{
136 int n;
137 va_list ap;
138
139 va_start(ap, fmt);
140 if (!field_sep)
141 n = vfprintf(fp, fmt, ap);
142 else {
143 char *bf = NULL;
144 n = vasprintf(&bf, fmt, ap);
145 if (n > 0) {
146 char *sep = bf;
147 while (1) {
148 sep = strchr(sep, *field_sep);
149 if (sep == NULL)
150 break;
151 *sep = '.';
152 }
153 }
154 fputs(bf, fp);
155 free(bf);
156 }
157 va_end(ap);
158 return n;
159}
160
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300161static LIST_HEAD(dsos);
162static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200163static struct dso *vdso;
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000164static struct dso *hypervisor_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300165
166static void dsos__add(struct dso *dso)
167{
168 list_add_tail(&dso->node, &dsos);
169}
170
171static struct dso *dsos__find(const char *name)
172{
173 struct dso *pos;
174
175 list_for_each_entry(pos, &dsos, node)
176 if (strcmp(pos->name, name) == 0)
177 return pos;
178 return NULL;
179}
180
181static struct dso *dsos__findnew(const char *name)
182{
183 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200184 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300185
Ingo Molnar4593bba2009-06-02 15:34:25 +0200186 if (dso)
187 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300188
Ingo Molnar4593bba2009-06-02 15:34:25 +0200189 dso = dso__new(name, 0);
190 if (!dso)
191 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200192
Ingo Molnarbd741372009-06-04 14:13:04 +0200193 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200194 if (nr < 0) {
Ingo Molnar75220602009-06-18 08:00:17 +0200195 eprintf("Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200196 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300197 }
Ingo Molnar75220602009-06-18 08:00:17 +0200198 if (!nr)
199 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200200
201 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300202
203 return dso;
204
205out_delete_dso:
206 dso__delete(dso);
207 return NULL;
208}
209
Ingo Molnar16f762a2009-05-27 09:10:38 +0200210static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300211{
212 struct dso *pos;
213
214 list_for_each_entry(pos, &dsos, node)
215 dso__fprintf(pos, fp);
216}
217
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000218static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200219{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200220 return dso__find_symbol(dso, ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200221}
222
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200223static int load_kernel(void)
224{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300225 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200226
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300227 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200228 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300229 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200230
Mike Galbraith42976482009-07-02 08:09:46 +0200231 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
Mike Galbraith9974f492009-07-02 08:05:58 +0200232 if (err <= 0) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300233 dso__delete(kernel_dso);
234 kernel_dso = NULL;
235 } else
236 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200237
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200238 vdso = dso__new("[vdso]", 0);
239 if (!vdso)
240 return -1;
241
242 vdso->find_symbol = vdso__find_symbol;
243
244 dsos__add(vdso);
245
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000246 hypervisor_dso = dso__new("[hypervisor]", 0);
247 if (!hypervisor_dso)
248 return -1;
249 dsos__add(hypervisor_dso);
250
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300251 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200252}
253
Ingo Molnard80d3382009-06-03 23:14:49 +0200254static char __cwd[PATH_MAX];
255static char *cwd = __cwd;
256static int cwdlen;
257
258static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300259{
260 int n = 0;
261
262 while (pathname[n] == cwd[n] && n < cwdlen)
263 ++n;
264
265 return n;
266}
267
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300268struct map {
269 struct list_head node;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000270 u64 start;
271 u64 end;
272 u64 pgoff;
273 u64 (*map_ip)(struct map *, u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300274 struct dso *dso;
275};
276
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000277static u64 map__map_ip(struct map *map, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200278{
279 return ip - map->start + map->pgoff;
280}
281
Ingo Molnarf37a2912009-07-01 12:37:06 +0200282static u64 vdso__map_ip(struct map *map __used, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200283{
284 return ip;
285}
286
Pekka Enberg80d496b2009-06-08 21:12:48 +0300287static inline int is_anon_memory(const char *filename)
288{
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300289 return strcmp(filename, "//anon") == 0;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300290}
291
Ingo Molnard80d3382009-06-03 23:14:49 +0200292static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300293{
294 struct map *self = malloc(sizeof(*self));
295
296 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300297 const char *filename = event->filename;
298 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300299 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300300
301 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200302 int n = strcommon(filename);
303
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300304 if (n == cwdlen) {
305 snprintf(newfilename, sizeof(newfilename),
306 ".%s", filename + n);
307 filename = newfilename;
308 }
309 }
310
Pekka Enberg80d496b2009-06-08 21:12:48 +0300311 anon = is_anon_memory(filename);
312
313 if (anon) {
314 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
315 filename = newfilename;
316 }
317
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300318 self->start = event->start;
319 self->end = event->start + event->len;
320 self->pgoff = event->pgoff;
321
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300322 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300323 if (self->dso == NULL)
324 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200325
Pekka Enberg80d496b2009-06-08 21:12:48 +0300326 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200327 self->map_ip = vdso__map_ip;
328 else
329 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300330 }
331 return self;
332out_delete:
333 free(self);
334 return NULL;
335}
336
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200337static struct map *map__clone(struct map *self)
338{
339 struct map *map = malloc(sizeof(*self));
340
341 if (!map)
342 return NULL;
343
344 memcpy(map, self, sizeof(*self));
345
346 return map;
347}
348
349static int map__overlap(struct map *l, struct map *r)
350{
351 if (l->start > r->start) {
352 struct map *t = l;
353 l = r;
354 r = t;
355 }
356
357 if (l->end > r->start)
358 return 1;
359
360 return 0;
361}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300362
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300363static size_t map__fprintf(struct map *self, FILE *fp)
364{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200365 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300366 self->start, self->end, self->pgoff, self->dso->name);
367}
368
369
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300370struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300371 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300372 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300373 pid_t pid;
374 char *comm;
375};
376
377static struct thread *thread__new(pid_t pid)
378{
379 struct thread *self = malloc(sizeof(*self));
380
381 if (self != NULL) {
382 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200383 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200384 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200385 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300386 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300387 }
388
389 return self;
390}
391
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300392static unsigned int dsos__col_width,
393 comms__col_width,
394 threads__col_width;
395
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300396static int thread__set_comm(struct thread *self, const char *comm)
397{
Peter Zijlstra82292892009-06-03 12:37:36 +0200398 if (self->comm)
399 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300400 self->comm = strdup(comm);
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300401 if (!self->comm)
402 return -ENOMEM;
403
404 if (!col_width_list_str && !field_sep &&
405 (!comm_list || strlist__has_entry(comm_list, comm))) {
406 unsigned int slen = strlen(comm);
407 if (slen > comms__col_width) {
408 comms__col_width = slen;
409 threads__col_width = slen + 6;
410 }
411 }
412
413 return 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300414}
415
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300416static size_t thread__fprintf(struct thread *self, FILE *fp)
417{
418 struct map *pos;
419 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
420
421 list_for_each_entry(pos, &self->maps, node)
422 ret += map__fprintf(pos, fp);
423
424 return ret;
425}
426
427
Ingo Molnar16f762a2009-05-27 09:10:38 +0200428static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200429static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300430
431static struct thread *threads__findnew(pid_t pid)
432{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300433 struct rb_node **p = &threads.rb_node;
434 struct rb_node *parent = NULL;
435 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300436
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200437 /*
438 * Font-end cache - PID lookups come in blocks,
439 * so most of the time we dont have to look up
440 * the full rbtree:
441 */
442 if (last_match && last_match->pid == pid)
443 return last_match;
444
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300445 while (*p != NULL) {
446 parent = *p;
447 th = rb_entry(parent, struct thread, rb_node);
448
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200449 if (th->pid == pid) {
450 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300451 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200452 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300453
454 if (pid < th->pid)
455 p = &(*p)->rb_left;
456 else
457 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300458 }
459
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300460 th = thread__new(pid);
461 if (th != NULL) {
462 rb_link_node(&th->rb_node, parent, p);
463 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200464 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300465 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200466
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300467 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300468}
469
470static void thread__insert_map(struct thread *self, struct map *map)
471{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200472 struct map *pos, *tmp;
473
474 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
475 if (map__overlap(pos, map)) {
Peter Zijlstra3d906ef2009-06-23 11:23:07 +0200476 if (verbose >= 2) {
477 printf("overlapping maps:\n");
478 map__fprintf(map, stdout);
479 map__fprintf(pos, stdout);
480 }
481
482 if (map->start <= pos->start && map->end > pos->start)
483 pos->start = map->end;
484
485 if (map->end >= pos->end && map->start < pos->end)
486 pos->end = map->start;
487
488 if (verbose >= 2) {
489 printf("after collision:\n");
490 map__fprintf(pos, stdout);
491 }
492
493 if (pos->start >= pos->end) {
494 list_del_init(&pos->node);
495 free(pos);
496 }
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200497 }
498 }
499
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300500 list_add_tail(&map->node, &self->maps);
501}
502
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200503static int thread__fork(struct thread *self, struct thread *parent)
504{
505 struct map *map;
506
507 if (self->comm)
508 free(self->comm);
509 self->comm = strdup(parent->comm);
510 if (!self->comm)
511 return -ENOMEM;
512
513 list_for_each_entry(map, &parent->maps, node) {
514 struct map *new = map__clone(map);
515 if (!new)
516 return -ENOMEM;
517 thread__insert_map(self, new);
518 }
519
520 return 0;
521}
522
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000523static struct map *thread__find_map(struct thread *self, u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300524{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200525 struct map *pos;
526
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300527 if (self == NULL)
528 return NULL;
529
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300530 list_for_each_entry(pos, &self->maps, node)
531 if (ip >= pos->start && ip <= pos->end)
532 return pos;
533
534 return NULL;
535}
536
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300537static size_t threads__fprintf(FILE *fp)
538{
539 size_t ret = 0;
540 struct rb_node *nd;
541
542 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
543 struct thread *pos = rb_entry(nd, struct thread, rb_node);
544
545 ret += thread__fprintf(pos, fp);
546 }
547
548 return ret;
549}
550
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200551/*
552 * histogram, sorted on item, collects counts
553 */
554
555static struct rb_root hist;
556
557struct hist_entry {
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200558 struct rb_node rb_node;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200559
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200560 struct thread *thread;
561 struct map *map;
562 struct dso *dso;
563 struct symbol *sym;
564 struct symbol *parent;
565 u64 ip;
566 char level;
567 struct callchain_node callchain;
568 struct rb_root sorted_chain;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200569
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200570 u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200571};
572
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200573/*
574 * configurable sorting bits
575 */
576
577struct sort_entry {
578 struct list_head list;
579
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200580 char *header;
581
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200582 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200583 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300584 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
585 unsigned int *width;
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300586 bool elide;
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200587};
588
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200589static int64_t cmp_null(void *l, void *r)
590{
591 if (!l && !r)
592 return 0;
593 else if (!l)
594 return -1;
595 else
596 return 1;
597}
598
Peter Zijlstra82292892009-06-03 12:37:36 +0200599/* --sort pid */
600
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200601static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200602sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
603{
604 return right->thread->pid - left->thread->pid;
605}
606
607static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300608sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200609{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300610 return repsep_fprintf(fp, "%*s:%5d", width - 6,
611 self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200612}
613
614static struct sort_entry sort_thread = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300615 .header = "Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200616 .cmp = sort__thread_cmp,
617 .print = sort__thread_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300618 .width = &threads__col_width,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200619};
620
Peter Zijlstra82292892009-06-03 12:37:36 +0200621/* --sort comm */
622
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200623static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200624sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
625{
Peter Zijlstra82292892009-06-03 12:37:36 +0200626 return right->thread->pid - left->thread->pid;
627}
628
629static int64_t
630sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
631{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200632 char *comm_l = left->thread->comm;
633 char *comm_r = right->thread->comm;
634
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200635 if (!comm_l || !comm_r)
636 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200637
638 return strcmp(comm_l, comm_r);
639}
640
641static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300642sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra992444b2009-05-27 20:20:27 +0200643{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300644 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200645}
646
647static struct sort_entry sort_comm = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300648 .header = "Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200649 .cmp = sort__comm_cmp,
650 .collapse = sort__comm_collapse,
651 .print = sort__comm_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300652 .width = &comms__col_width,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200653};
654
Peter Zijlstra82292892009-06-03 12:37:36 +0200655/* --sort dso */
656
Peter Zijlstra992444b2009-05-27 20:20:27 +0200657static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200658sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
659{
660 struct dso *dso_l = left->dso;
661 struct dso *dso_r = right->dso;
662
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200663 if (!dso_l || !dso_r)
664 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200665
666 return strcmp(dso_l->name, dso_r->name);
667}
668
669static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300670sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200671{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200672 if (self->dso)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300673 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200674
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300675 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200676}
677
678static struct sort_entry sort_dso = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300679 .header = "Shared Object",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200680 .cmp = sort__dso_cmp,
681 .print = sort__dso_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300682 .width = &dsos__col_width,
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200683};
684
Peter Zijlstra82292892009-06-03 12:37:36 +0200685/* --sort symbol */
686
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200687static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200688sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300689{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000690 u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200691
692 if (left->sym == right->sym)
693 return 0;
694
695 ip_l = left->sym ? left->sym->start : left->ip;
696 ip_r = right->sym ? right->sym->start : right->ip;
697
698 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300699}
700
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200701static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300702sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200703{
704 size_t ret = 0;
705
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200706 if (verbose)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300707 ret += repsep_fprintf(fp, "%#018llx ", (u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200708
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300709 ret += repsep_fprintf(fp, "[%c] ", self->level);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200710 if (self->sym) {
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300711 ret += repsep_fprintf(fp, "%s", self->sym->name);
Mike Galbraith42976482009-07-02 08:09:46 +0200712
713 if (self->sym->module)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300714 ret += repsep_fprintf(fp, "\t[%s]",
715 self->sym->module->name);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200716 } else {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300717 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200718 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200719
720 return ret;
721}
722
723static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200724 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200725 .cmp = sort__sym_cmp,
726 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200727};
728
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200729/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200730
731static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200732sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200733{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200734 struct symbol *sym_l = left->parent;
735 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200736
737 if (!sym_l || !sym_r)
738 return cmp_null(sym_l, sym_r);
739
740 return strcmp(sym_l->name, sym_r->name);
741}
742
743static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300744sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200745{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300746 return repsep_fprintf(fp, "%-*s", width,
747 self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200748}
749
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300750static unsigned int parent_symbol__col_width;
751
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200752static struct sort_entry sort_parent = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300753 .header = "Parent symbol",
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200754 .cmp = sort__parent_cmp,
755 .print = sort__parent_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300756 .width = &parent_symbol__col_width,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200757};
758
Peter Zijlstra82292892009-06-03 12:37:36 +0200759static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200760static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200761
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200762struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200763 char *name;
764 struct sort_entry *entry;
765 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200766};
767
768static struct sort_dimension sort_dimensions[] = {
769 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200770 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200771 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200772 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200773 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200774};
775
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200776static LIST_HEAD(hist_entry__sort_list);
777
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200778static int sort_dimension__add(char *tok)
779{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200780 unsigned int i;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200781
782 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
783 struct sort_dimension *sd = &sort_dimensions[i];
784
785 if (sd->taken)
786 continue;
787
Ingo Molnar5352f352009-06-03 10:07:39 +0200788 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200789 continue;
790
Peter Zijlstra82292892009-06-03 12:37:36 +0200791 if (sd->entry->collapse)
792 sort__need_collapse = 1;
793
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200794 if (sd->entry == &sort_parent) {
795 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200796 if (ret) {
797 char err[BUFSIZ];
798
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200799 regerror(ret, &parent_regex, err, sizeof(err));
800 fprintf(stderr, "Invalid regex: %s\n%s",
801 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200802 exit(-1);
803 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200804 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200805 }
806
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200807 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
808 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200809
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200810 return 0;
811 }
812
813 return -ESRCH;
814}
815
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200816static int64_t
817hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
818{
819 struct sort_entry *se;
820 int64_t cmp = 0;
821
822 list_for_each_entry(se, &hist_entry__sort_list, list) {
823 cmp = se->cmp(left, right);
824 if (cmp)
825 break;
826 }
827
828 return cmp;
829}
830
Peter Zijlstra82292892009-06-03 12:37:36 +0200831static int64_t
832hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
833{
834 struct sort_entry *se;
835 int64_t cmp = 0;
836
837 list_for_each_entry(se, &hist_entry__sort_list, list) {
838 int64_t (*f)(struct hist_entry *, struct hist_entry *);
839
840 f = se->collapse ?: se->cmp;
841
842 cmp = f(left, right);
843 if (cmp)
844 break;
845 }
846
847 return cmp;
848}
849
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200850static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
851{
852 int i;
853 size_t ret = 0;
854
855 ret += fprintf(fp, "%s", " ");
856
857 for (i = 0; i < depth; i++)
858 if (depth_mask & (1 << i))
859 ret += fprintf(fp, "| ");
860 else
861 ret += fprintf(fp, " ");
862
863 ret += fprintf(fp, "\n");
864
865 return ret;
866}
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200867static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200868ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
869 int depth_mask, int count, u64 total_samples,
870 int hits)
871{
872 int i;
873 size_t ret = 0;
874
875 ret += fprintf(fp, "%s", " ");
876 for (i = 0; i < depth; i++) {
877 if (depth_mask & (1 << i))
878 ret += fprintf(fp, "|");
879 else
880 ret += fprintf(fp, " ");
881 if (!count && i == depth - 1) {
882 double percent;
883
884 percent = hits * 100.0 / total_samples;
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200885 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200886 } else
887 ret += fprintf(fp, "%s", " ");
888 }
889 if (chain->sym)
890 ret += fprintf(fp, "%s\n", chain->sym->name);
891 else
892 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
893
894 return ret;
895}
896
897static size_t
898callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
899 u64 total_samples, int depth, int depth_mask)
900{
901 struct rb_node *node, *next;
902 struct callchain_node *child;
903 struct callchain_list *chain;
904 int new_depth_mask = depth_mask;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200905 u64 new_total;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200906 size_t ret = 0;
907 int i;
908
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200909 if (callchain_param.mode == CHAIN_GRAPH_REL)
910 new_total = self->cumul_hit;
911 else
912 new_total = total_samples;
913
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200914 node = rb_first(&self->rb_root);
915 while (node) {
916 child = rb_entry(node, struct callchain_node, rb_node);
917
918 /*
919 * The depth mask manages the output of pipes that show
920 * the depth. We don't want to keep the pipes of the current
921 * level for the last child of this depth
922 */
923 next = rb_next(node);
924 if (!next)
925 new_depth_mask &= ~(1 << (depth - 1));
926
927 /*
928 * But we keep the older depth mask for the line seperator
929 * to keep the level link until we reach the last child
930 */
931 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
932 i = 0;
933 list_for_each_entry(chain, &child->val, list) {
934 if (chain->ip >= PERF_CONTEXT_MAX)
935 continue;
936 ret += ipchain__fprintf_graph(fp, chain, depth,
937 new_depth_mask, i++,
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200938 new_total,
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200939 child->cumul_hit);
940 }
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200941 ret += callchain__fprintf_graph(fp, child, new_total,
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200942 depth + 1,
943 new_depth_mask | (1 << depth));
944 node = next;
945 }
946
947 return ret;
948}
949
950static size_t
951callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
952 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200953{
954 struct callchain_list *chain;
955 size_t ret = 0;
956
957 if (!self)
958 return 0;
959
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200960 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200961
962
Frederic Weisbecker44249612009-07-01 05:35:14 +0200963 list_for_each_entry(chain, &self->val, list) {
964 if (chain->ip >= PERF_CONTEXT_MAX)
965 continue;
966 if (chain->sym)
967 ret += fprintf(fp, " %s\n", chain->sym->name);
968 else
969 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200970 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200971 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200972
973 return ret;
974}
975
976static size_t
977hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
978 u64 total_samples)
979{
980 struct rb_node *rb_node;
981 struct callchain_node *chain;
982 size_t ret = 0;
983
984 rb_node = rb_first(&self->sorted_chain);
985 while (rb_node) {
986 double percent;
987
988 chain = rb_entry(rb_node, struct callchain_node, rb_node);
989 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200990 switch (callchain_param.mode) {
991 case CHAIN_FLAT:
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200992 ret += percent_color_fprintf(fp, " %6.2f%%\n",
993 percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200994 ret += callchain__fprintf_flat(fp, chain, total_samples);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200995 break;
996 case CHAIN_GRAPH_ABS: /* Falldown */
997 case CHAIN_GRAPH_REL:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200998 ret += callchain__fprintf_graph(fp, chain,
999 total_samples, 1, 1);
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001000 default:
1001 break;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001002 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001003 ret += fprintf(fp, "\n");
1004 rb_node = rb_next(rb_node);
1005 }
1006
1007 return ret;
1008}
1009
1010
1011static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001012hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001013{
1014 struct sort_entry *se;
1015 size_t ret;
1016
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001017 if (exclude_other && !self->parent)
1018 return 0;
1019
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +02001020 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001021 ret = percent_color_fprintf(fp,
1022 field_sep ? "%.2f" : " %6.2f%%",
1023 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +02001024 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001025 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001026
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001027 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001028 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001029 continue;
1030
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001031 fprintf(fp, "%s", field_sep ?: " ");
1032 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001033 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001034
1035 ret += fprintf(fp, "\n");
1036
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001037 if (callchain)
1038 hist_entry_callchain__fprintf(fp, self, total_samples);
1039
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001040 return ret;
1041}
1042
1043/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001044 *
1045 */
1046
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001047static void dso__calc_col_width(struct dso *self)
1048{
1049 if (!col_width_list_str && !field_sep &&
1050 (!dso_list || strlist__has_entry(dso_list, self->name))) {
1051 unsigned int slen = strlen(self->name);
1052 if (slen > dsos__col_width)
1053 dsos__col_width = slen;
1054 }
1055
1056 self->slen_calculated = 1;
1057}
1058
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001059static struct symbol *
1060resolve_symbol(struct thread *thread, struct map **mapp,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001061 struct dso **dsop, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001062{
1063 struct dso *dso = dsop ? *dsop : NULL;
1064 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +02001065 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001066
1067 if (!thread)
1068 return NULL;
1069
1070 if (dso)
1071 goto got_dso;
1072
1073 if (map)
1074 goto got_map;
1075
1076 map = thread__find_map(thread, ip);
1077 if (map != NULL) {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001078 /*
1079 * We have to do this here as we may have a dso
1080 * with no symbol hit that has a name longer than
1081 * the ones with symbols sampled.
1082 */
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001083 if (!sort_dso.elide && !map->dso->slen_calculated)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001084 dso__calc_col_width(map->dso);
1085
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001086 if (mapp)
1087 *mapp = map;
1088got_map:
1089 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001090
1091 dso = map->dso;
1092 } else {
1093 /*
1094 * If this is outside of all known maps,
1095 * and is a negative address, try to look it
1096 * up in the kernel dso, as it might be a
1097 * vsyscall (which executes in user-mode):
1098 */
1099 if ((long long)ip < 0)
1100 dso = kernel_dso;
1101 }
1102 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Peter Zijlstra520f2c32009-06-22 16:52:51 +02001103 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1104 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001105
1106 if (dsop)
1107 *dsop = dso;
1108
1109 if (!dso)
1110 return NULL;
1111got_dso:
1112 return dso->find_symbol(dso, ip);
1113}
1114
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001115static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001116{
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001117 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001118 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001119
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001120 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001121}
1122
Frederic Weisbecker44249612009-07-01 05:35:14 +02001123static struct symbol **
Ingo Molnarf37a2912009-07-01 12:37:06 +02001124resolve_callchain(struct thread *thread, struct map *map __used,
Frederic Weisbecker44249612009-07-01 05:35:14 +02001125 struct ip_callchain *chain, struct hist_entry *entry)
1126{
Frederic Weisbecker44249612009-07-01 05:35:14 +02001127 u64 context = PERF_CONTEXT_MAX;
Ingo Molnar029e5b12009-07-03 13:17:28 +02001128 struct symbol **syms = NULL;
Ingo Molnarf37a2912009-07-01 12:37:06 +02001129 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001130
1131 if (callchain) {
1132 syms = calloc(chain->nr, sizeof(*syms));
1133 if (!syms) {
1134 fprintf(stderr, "Can't allocate memory for symbols\n");
1135 exit(-1);
1136 }
1137 }
1138
1139 for (i = 0; i < chain->nr; i++) {
1140 u64 ip = chain->ips[i];
1141 struct dso *dso = NULL;
1142 struct symbol *sym;
1143
1144 if (ip >= PERF_CONTEXT_MAX) {
1145 context = ip;
1146 continue;
1147 }
1148
1149 switch (context) {
Ingo Molnar88a69df2009-07-01 11:17:20 +02001150 case PERF_CONTEXT_HV:
1151 dso = hypervisor_dso;
1152 break;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001153 case PERF_CONTEXT_KERNEL:
1154 dso = kernel_dso;
1155 break;
1156 default:
1157 break;
1158 }
1159
1160 sym = resolve_symbol(thread, NULL, &dso, &ip);
1161
1162 if (sym) {
1163 if (sort__has_parent && call__match(sym) &&
1164 !entry->parent)
1165 entry->parent = sym;
1166 if (!callchain)
1167 break;
1168 syms[i] = sym;
1169 }
1170 }
1171
1172 return syms;
1173}
1174
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001175/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001176 * collect histogram counts
1177 */
1178
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001179static int
1180hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001181 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1182 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001183{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001184 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001185 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001186 struct hist_entry *he;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001187 struct symbol **syms = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001188 struct hist_entry entry = {
1189 .thread = thread,
1190 .map = map,
1191 .dso = dso,
1192 .sym = sym,
1193 .ip = ip,
1194 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001195 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001196 .parent = NULL,
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001197 .sorted_chain = RB_ROOT
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001198 };
1199 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001200
Frederic Weisbecker44249612009-07-01 05:35:14 +02001201 if ((sort__has_parent || callchain) && chain)
1202 syms = resolve_callchain(thread, map, chain, &entry);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001203
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001204 while (*p != NULL) {
1205 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001206 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001207
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001208 cmp = hist_entry__cmp(&entry, he);
1209
1210 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001211 he->count += count;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001212 if (callchain) {
1213 append_chain(&he->callchain, chain, syms);
1214 free(syms);
1215 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001216 return 0;
1217 }
1218
1219 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001220 p = &(*p)->rb_left;
1221 else
1222 p = &(*p)->rb_right;
1223 }
1224
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001225 he = malloc(sizeof(*he));
1226 if (!he)
1227 return -ENOMEM;
1228 *he = entry;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001229 if (callchain) {
1230 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +02001231 append_chain(&he->callchain, chain, syms);
1232 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001233 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001234 rb_link_node(&he->rb_node, parent, p);
1235 rb_insert_color(&he->rb_node, &hist);
1236
1237 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001238}
1239
Peter Zijlstra82292892009-06-03 12:37:36 +02001240static void hist_entry__free(struct hist_entry *he)
1241{
1242 free(he);
1243}
1244
1245/*
1246 * collapse the histogram
1247 */
1248
1249static struct rb_root collapse_hists;
1250
1251static void collapse__insert_entry(struct hist_entry *he)
1252{
1253 struct rb_node **p = &collapse_hists.rb_node;
1254 struct rb_node *parent = NULL;
1255 struct hist_entry *iter;
1256 int64_t cmp;
1257
1258 while (*p != NULL) {
1259 parent = *p;
1260 iter = rb_entry(parent, struct hist_entry, rb_node);
1261
1262 cmp = hist_entry__collapse(iter, he);
1263
1264 if (!cmp) {
1265 iter->count += he->count;
1266 hist_entry__free(he);
1267 return;
1268 }
1269
1270 if (cmp < 0)
1271 p = &(*p)->rb_left;
1272 else
1273 p = &(*p)->rb_right;
1274 }
1275
1276 rb_link_node(&he->rb_node, parent, p);
1277 rb_insert_color(&he->rb_node, &collapse_hists);
1278}
1279
1280static void collapse__resort(void)
1281{
1282 struct rb_node *next;
1283 struct hist_entry *n;
1284
1285 if (!sort__need_collapse)
1286 return;
1287
1288 next = rb_first(&hist);
1289 while (next) {
1290 n = rb_entry(next, struct hist_entry, rb_node);
1291 next = rb_next(&n->rb_node);
1292
1293 rb_erase(&n->rb_node, &hist);
1294 collapse__insert_entry(n);
1295 }
1296}
1297
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001298/*
1299 * reverse the map, sort on count.
1300 */
1301
1302static struct rb_root output_hists;
1303
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001304static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001305{
1306 struct rb_node **p = &output_hists.rb_node;
1307 struct rb_node *parent = NULL;
1308 struct hist_entry *iter;
1309
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001310 if (callchain)
1311 callchain_param.sort(&he->sorted_chain, &he->callchain,
1312 min_callchain_hits, &callchain_param);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001313
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001314 while (*p != NULL) {
1315 parent = *p;
1316 iter = rb_entry(parent, struct hist_entry, rb_node);
1317
1318 if (he->count > iter->count)
1319 p = &(*p)->rb_left;
1320 else
1321 p = &(*p)->rb_right;
1322 }
1323
1324 rb_link_node(&he->rb_node, parent, p);
1325 rb_insert_color(&he->rb_node, &output_hists);
1326}
1327
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001328static void output__resort(u64 total_samples)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001329{
Peter Zijlstra82292892009-06-03 12:37:36 +02001330 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001331 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001332 struct rb_root *tree = &hist;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001333 u64 min_callchain_hits;
1334
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001335 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001336
Peter Zijlstra82292892009-06-03 12:37:36 +02001337 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001338 tree = &collapse_hists;
1339
1340 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001341
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001342 while (next) {
1343 n = rb_entry(next, struct hist_entry, rb_node);
1344 next = rb_next(&n->rb_node);
1345
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001346 rb_erase(&n->rb_node, tree);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001347 output__insert_entry(n, min_callchain_hits);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001348 }
1349}
1350
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001351static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001352{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001353 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001354 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001355 struct rb_node *nd;
1356 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001357 unsigned int width;
1358 char *col_width = col_width_list_str;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001359
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001360 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001361 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001362
1363 fprintf(fp, "# Overhead");
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001364 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001365 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001366 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001367 if (field_sep) {
1368 fprintf(fp, "%c%s", *field_sep, se->header);
1369 continue;
1370 }
1371 width = strlen(se->header);
1372 if (se->width) {
1373 if (col_width_list_str) {
1374 if (col_width) {
1375 *se->width = atoi(col_width);
1376 col_width = strchr(col_width, ',');
1377 if (col_width)
1378 ++col_width;
1379 }
1380 }
1381 width = *se->width = max(*se->width, width);
1382 }
1383 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001384 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001385 fprintf(fp, "\n");
1386
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001387 if (field_sep)
1388 goto print_entries;
1389
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001390 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001391 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001392 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001393
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001394 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001395 continue;
1396
Ingo Molnar4593bba2009-06-02 15:34:25 +02001397 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001398 if (se->width)
1399 width = *se->width;
1400 else
1401 width = strlen(se->header);
1402 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001403 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001404 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001405 fprintf(fp, "\n");
1406
1407 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001408
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001409print_entries:
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001410 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1411 pos = rb_entry(nd, struct hist_entry, rb_node);
1412 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001413 }
1414
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001415 if (sort_order == default_sort_order &&
1416 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001417 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001418 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001419 fprintf(fp, "#\n");
1420 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001421 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001422
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001423 return ret;
1424}
1425
Peter Zijlstra436224a2009-06-02 21:02:36 +02001426static void register_idle_thread(void)
1427{
1428 struct thread *thread = threads__findnew(0);
1429
1430 if (thread == NULL ||
1431 thread__set_comm(thread, "[idle]")) {
1432 fprintf(stderr, "problem inserting idle task.\n");
1433 exit(-1);
1434 }
1435}
1436
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001437static unsigned long total = 0,
1438 total_mmap = 0,
1439 total_comm = 0,
1440 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001441 total_unknown = 0,
1442 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001443
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001444static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +02001445{
1446 unsigned int chain_size;
1447
Ingo Molnar75220602009-06-18 08:00:17 +02001448 chain_size = event->header.size;
1449 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1450
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001451 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +02001452 return -1;
1453
1454 return 0;
1455}
1456
Ingo Molnard80d3382009-06-03 23:14:49 +02001457static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001458process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +02001459{
1460 char level;
1461 int show = 0;
1462 struct dso *dso = NULL;
1463 struct thread *thread = threads__findnew(event->ip.pid);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001464 u64 ip = event->ip.ip;
1465 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001466 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001467 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001468 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001469 int cpumode;
Ingo Molnar75051722009-06-03 23:14:49 +02001470
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001471 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001472 period = *(u64 *)more_data;
1473 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001474 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001475
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001476 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001477 (void *)(offset + head),
1478 (void *)(long)(event->header.size),
1479 event->header.misc,
1480 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001481 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001482 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001483
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001484 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001485 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001486
1487 chain = (void *)more_data;
1488
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001489 dprintf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001490
Ingo Molnar75220602009-06-18 08:00:17 +02001491 if (validate_chain(chain, event) < 0) {
1492 eprintf("call-chain problem with event, skipping it.\n");
1493 return 0;
1494 }
1495
1496 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001497 for (i = 0; i < chain->nr; i++)
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001498 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001499 }
1500 }
1501
Ingo Molnar75051722009-06-03 23:14:49 +02001502 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1503
1504 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001505 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001506 event->header.type);
1507 return -1;
1508 }
1509
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001510 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1511 return 0;
1512
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001513 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1514
1515 if (cpumode == PERF_EVENT_MISC_KERNEL) {
Ingo Molnar75051722009-06-03 23:14:49 +02001516 show = SHOW_KERNEL;
1517 level = 'k';
1518
1519 dso = kernel_dso;
1520
1521 dprintf(" ...... dso: %s\n", dso->name);
1522
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001523 } else if (cpumode == PERF_EVENT_MISC_USER) {
Ingo Molnar75051722009-06-03 23:14:49 +02001524
1525 show = SHOW_USER;
1526 level = '.';
1527
Ingo Molnar75051722009-06-03 23:14:49 +02001528 } else {
1529 show = SHOW_HV;
1530 level = 'H';
Anton Blanchardfb9c8182009-07-01 09:00:49 +10001531
1532 dso = hypervisor_dso;
1533
Ingo Molnar75051722009-06-03 23:14:49 +02001534 dprintf(" ...... dso: [hypervisor]\n");
1535 }
1536
1537 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001538 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001539
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001540 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1541 return 0;
1542
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001543 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1544 return 0;
1545
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001546 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001547 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001548 return -1;
1549 }
1550 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001551 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001552
1553 return 0;
1554}
1555
1556static int
1557process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1558{
1559 struct thread *thread = threads__findnew(event->mmap.pid);
1560 struct map *map = map__new(&event->mmap);
1561
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001562 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001563 (void *)(offset + head),
1564 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001565 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001566 (void *)(long)event->mmap.start,
1567 (void *)(long)event->mmap.len,
1568 (void *)(long)event->mmap.pgoff,
1569 event->mmap.filename);
1570
1571 if (thread == NULL || map == NULL) {
1572 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001573 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001574 }
1575
1576 thread__insert_map(thread, map);
1577 total_mmap++;
1578
1579 return 0;
1580}
1581
1582static int
1583process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1584{
1585 struct thread *thread = threads__findnew(event->comm.pid);
1586
1587 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1588 (void *)(offset + head),
1589 (void *)(long)(event->header.size),
1590 event->comm.comm, event->comm.pid);
1591
1592 if (thread == NULL ||
1593 thread__set_comm(thread, event->comm.comm)) {
1594 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1595 return -1;
1596 }
1597 total_comm++;
1598
1599 return 0;
1600}
1601
1602static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001603process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1604{
1605 struct thread *thread = threads__findnew(event->fork.pid);
1606 struct thread *parent = threads__findnew(event->fork.ppid);
1607
1608 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1609 (void *)(offset + head),
1610 (void *)(long)(event->header.size),
1611 event->fork.pid, event->fork.ppid);
1612
1613 if (!thread || !parent || thread__fork(thread, parent)) {
1614 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1615 return -1;
1616 }
1617 total_fork++;
1618
1619 return 0;
1620}
1621
1622static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001623process_period_event(event_t *event, unsigned long offset, unsigned long head)
1624{
1625 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1626 (void *)(offset + head),
1627 (void *)(long)(event->header.size),
1628 event->period.time,
1629 event->period.id,
1630 event->period.sample_period);
1631
1632 return 0;
1633}
1634
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001635static int
1636process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1637{
1638 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1639 (void *)(offset + head),
1640 (void *)(long)(event->header.size),
1641 event->lost.id,
1642 event->lost.lost);
1643
1644 total_lost += event->lost.lost;
1645
1646 return 0;
1647}
1648
Ingo Molnar8465b052009-06-14 14:44:07 +02001649static void trace_event(event_t *event)
1650{
1651 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001652 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001653 int i, j;
1654
1655 if (!dump_trace)
1656 return;
1657
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001658 dprintf(".");
1659 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001660
1661 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001662 if ((i & 15) == 0) {
1663 dprintf(".");
1664 cdprintf(" %04x: ", i);
1665 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001666
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001667 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001668
1669 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001670 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001671 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001672 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001673 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001674 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001675 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001676 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001677 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001678 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001679 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001680 }
1681 }
1682 dprintf(".\n");
1683}
1684
Ingo Molnarb2fef072009-06-05 18:07:51 +02001685static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001686process_read_event(event_t *event, unsigned long offset, unsigned long head)
1687{
1688 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1689 (void *)(offset + head),
1690 (void *)(long)(event->header.size),
1691 event->read.pid,
1692 event->read.tid,
1693 event->read.value);
1694
1695 return 0;
1696}
1697
1698static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001699process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001700{
Ingo Molnar8465b052009-06-14 14:44:07 +02001701 trace_event(event);
1702
Ingo Molnar75051722009-06-03 23:14:49 +02001703 switch (event->header.type) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001704 case PERF_EVENT_SAMPLE:
1705 return process_sample_event(event, offset, head);
1706
Ingo Molnar75051722009-06-03 23:14:49 +02001707 case PERF_EVENT_MMAP:
1708 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001709
Ingo Molnar75051722009-06-03 23:14:49 +02001710 case PERF_EVENT_COMM:
1711 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001712
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001713 case PERF_EVENT_FORK:
1714 return process_fork_event(event, offset, head);
1715
Ingo Molnarb2fef072009-06-05 18:07:51 +02001716 case PERF_EVENT_PERIOD:
1717 return process_period_event(event, offset, head);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001718
1719 case PERF_EVENT_LOST:
1720 return process_lost_event(event, offset, head);
1721
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001722 case PERF_EVENT_READ:
1723 return process_read_event(event, offset, head);
1724
Ingo Molnard11444d2009-06-03 23:29:14 +02001725 /*
1726 * We dont process them right now but they are fine:
1727 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001728
Ingo Molnard11444d2009-06-03 23:29:14 +02001729 case PERF_EVENT_THROTTLE:
1730 case PERF_EVENT_UNTHROTTLE:
1731 return 0;
1732
Ingo Molnard80d3382009-06-03 23:14:49 +02001733 default:
1734 return -1;
1735 }
1736
1737 return 0;
1738}
1739
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001740static struct perf_header *header;
1741
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001742static u64 perf_header__sample_type(void)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001743{
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001744 u64 sample_type = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001745 int i;
1746
1747 for (i = 0; i < header->attrs; i++) {
1748 struct perf_header_attr *attr = header->attr[i];
1749
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001750 if (!sample_type)
1751 sample_type = attr->attr.sample_type;
1752 else if (sample_type != attr->attr.sample_type)
1753 die("non matching sample_type");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001754 }
1755
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001756 return sample_type;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001757}
Peter Zijlstraf5970552009-06-18 23:22:55 +02001758
Ingo Molnard80d3382009-06-03 23:14:49 +02001759static int __cmd_report(void)
1760{
Ingo Molnar75051722009-06-03 23:14:49 +02001761 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001762 unsigned long offset = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001763 unsigned long head, shift;
Ingo Molnard80d3382009-06-03 23:14:49 +02001764 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001765 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001766 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001767 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001768
1769 register_idle_thread();
1770
1771 input = open(input_name, O_RDONLY);
1772 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001773 fprintf(stderr, " failed to open file: %s", input_name);
1774 if (!strcmp(input_name, "perf.data"))
1775 fprintf(stderr, " (try 'perf record' first)");
1776 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001777 exit(-1);
1778 }
1779
1780 ret = fstat(input, &stat);
1781 if (ret < 0) {
1782 perror("failed to stat file");
1783 exit(-1);
1784 }
1785
1786 if (!stat.st_size) {
1787 fprintf(stderr, "zero-sized file, nothing to do!\n");
1788 exit(0);
1789 }
1790
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001791 header = perf_header__read(input);
1792 head = header->data_offset;
Peter Zijlstraf5970552009-06-18 23:22:55 +02001793
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001794 sample_type = perf_header__sample_type();
1795
Frederic Weisbecker91b4eae2009-07-05 07:39:17 +02001796 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1797 if (sort__has_parent) {
1798 fprintf(stderr, "selected --sort parent, but no"
1799 " callchain data. Did you call"
1800 " perf record without -g?\n");
1801 exit(-1);
1802 }
1803 if (callchain) {
1804 fprintf(stderr, "selected -c but no callchain data."
1805 " Did you call perf record without"
1806 " -g?\n");
1807 exit(-1);
1808 }
Peter Zijlstraf5970552009-06-18 23:22:55 +02001809 }
1810
Ingo Molnard80d3382009-06-03 23:14:49 +02001811 if (load_kernel() < 0) {
1812 perror("failed to load kernel symbols");
1813 return EXIT_FAILURE;
1814 }
1815
1816 if (!full_paths) {
1817 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1818 perror("failed to get the current directory");
1819 return EXIT_FAILURE;
1820 }
1821 cwdlen = strlen(cwd);
1822 } else {
1823 cwd = NULL;
1824 cwdlen = 0;
1825 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001826
1827 shift = page_size * (head / page_size);
1828 offset += shift;
1829 head -= shift;
1830
Ingo Molnard80d3382009-06-03 23:14:49 +02001831remap:
1832 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1833 MAP_SHARED, input, offset);
1834 if (buf == MAP_FAILED) {
1835 perror("failed to mmap file");
1836 exit(-1);
1837 }
1838
1839more:
1840 event = (event_t *)(buf + head);
1841
1842 size = event->header.size;
1843 if (!size)
1844 size = 8;
1845
1846 if (head + event->header.size >= page_size * mmap_window) {
Ingo Molnard80d3382009-06-03 23:14:49 +02001847 int ret;
1848
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001849 shift = page_size * (head / page_size);
1850
Ingo Molnard80d3382009-06-03 23:14:49 +02001851 ret = munmap(buf, page_size * mmap_window);
1852 assert(ret == 0);
1853
1854 offset += shift;
1855 head -= shift;
1856 goto remap;
1857 }
1858
1859 size = event->header.size;
1860
Ingo Molnar8465b052009-06-14 14:44:07 +02001861 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001862 (void *)(offset + head),
1863 (void *)(long)event->header.size,
1864 event->header.type);
1865
Ingo Molnard80d3382009-06-03 23:14:49 +02001866 if (!size || process_event(event, offset, head) < 0) {
1867
Ingo Molnar35029732009-06-03 09:38:58 +02001868 dprintf("%p [%p]: skipping unknown header type: %d\n",
1869 (void *)(offset + head),
1870 (void *)(long)(event->header.size),
1871 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001872
Ingo Molnar3e706112009-05-26 18:53:17 +02001873 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001874
1875 /*
1876 * assume we lost track of the stream, check alignment, and
1877 * increment a single u64 in the hope to catch on again 'soon'.
1878 */
1879
1880 if (unlikely(head & 7))
1881 head &= ~7ULL;
1882
1883 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001884 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001885
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001886 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001887
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001888 if (offset + head >= header->data_offset + header->data_size)
Peter Zijlstraf5970552009-06-18 23:22:55 +02001889 goto done;
1890
Ingo Molnarf37a2912009-07-01 12:37:06 +02001891 if (offset + head < (unsigned long)stat.st_size)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001892 goto more;
1893
Peter Zijlstraf5970552009-06-18 23:22:55 +02001894done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001895 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001896 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001897
Ingo Molnar35029732009-06-03 09:38:58 +02001898 dprintf(" IP events: %10ld\n", total);
1899 dprintf(" mmap events: %10ld\n", total_mmap);
1900 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001901 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001902 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001903 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001904
Ingo Molnar35029732009-06-03 09:38:58 +02001905 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001906 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001907
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001908 if (verbose >= 3)
1909 threads__fprintf(stdout);
1910
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001911 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001912 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001913
Peter Zijlstra82292892009-06-03 12:37:36 +02001914 collapse__resort();
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001915 output__resort(total);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001916 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001917
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001918 return rc;
1919}
1920
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001921static int
1922parse_callchain_opt(const struct option *opt __used, const char *arg,
1923 int unset __used)
1924{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001925 char *tok;
1926 char *endptr;
1927
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001928 callchain = 1;
1929
1930 if (!arg)
1931 return 0;
1932
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001933 tok = strtok((char *)arg, ",");
1934 if (!tok)
1935 return -1;
1936
1937 /* get the output mode */
1938 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001939 callchain_param.mode = CHAIN_GRAPH_ABS;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001940
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001941 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001942 callchain_param.mode = CHAIN_FLAT;
1943
1944 else if (!strncmp(tok, "fractal", strlen(arg)))
1945 callchain_param.mode = CHAIN_GRAPH_REL;
1946
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001947 else
1948 return -1;
1949
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001950 /* get the min percentage */
1951 tok = strtok(NULL, ",");
1952 if (!tok)
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001953 goto setup;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001954
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001955 callchain_param.min_percent = strtod(tok, &endptr);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001956 if (tok == endptr)
1957 return -1;
1958
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001959setup:
1960 if (register_callchain_param(&callchain_param) < 0) {
1961 fprintf(stderr, "Can't register callchain params\n");
1962 return -1;
1963 }
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001964 return 0;
1965}
1966
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001967static const char * const report_usage[] = {
1968 "perf report [<options>] <command>",
1969 NULL
1970};
1971
1972static const struct option options[] = {
1973 OPT_STRING('i', "input", &input_name, "file",
1974 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001975 OPT_BOOLEAN('v', "verbose", &verbose,
1976 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001977 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1978 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001979 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +02001980 OPT_BOOLEAN('m', "modules", &modules,
1981 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001982 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001983 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001984 OPT_BOOLEAN('P', "full-paths", &full_paths,
1985 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001986 OPT_STRING('p', "parent", &parent_pattern, "regex",
1987 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001988 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1989 "Only display entries with parent-match"),
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001990 OPT_CALLBACK_DEFAULT('c', "callchain", NULL, "output_type,min_percent",
1991 "Display callchains using output_type and min percent threshold. "
Frederic Weisbeckerbe903882009-07-05 07:39:18 +02001992 "Default: flat,0", &parse_callchain_opt, callchain_default_opt),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001993 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1994 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001995 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1996 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001997 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1998 "only consider these symbols"),
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001999 OPT_STRING('w', "column-widths", &col_width_list_str,
2000 "width[,width...]",
2001 "don't try to adjust column width, use these fixed values"),
2002 OPT_STRING('t', "field-separator", &field_sep, "separator",
2003 "separator for columns, no spaces will be added between "
2004 "columns '.' is reserved."),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02002005 OPT_END()
2006};
2007
Ingo Molnar5352f352009-06-03 10:07:39 +02002008static void setup_sorting(void)
2009{
2010 char *tmp, *tok, *str = strdup(sort_order);
2011
2012 for (tok = strtok_r(str, ", ", &tmp);
2013 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2014 if (sort_dimension__add(tok) < 0) {
2015 error("Unknown --sort key: `%s'", tok);
2016 usage_with_options(report_usage, options);
2017 }
2018 }
2019
2020 free(str);
2021}
2022
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03002023static void setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002024 struct sort_entry *se, const char *list_name,
2025 FILE *fp)
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03002026{
2027 if (list_str) {
2028 *list = strlist__new(true, list_str);
2029 if (!*list) {
2030 fprintf(stderr, "problems parsing %s list\n",
2031 list_name);
2032 exit(129);
2033 }
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002034 if (strlist__nr_entries(*list) == 1) {
2035 fprintf(fp, "# %s: %s\n", list_name,
2036 strlist__entry(*list, 0)->s);
2037 se->elide = true;
2038 }
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03002039 }
2040}
2041
Ingo Molnarf37a2912009-07-01 12:37:06 +02002042int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02002043{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03002044 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02002045
2046 page_size = getpagesize();
2047
Ingo Molnaredc52de2009-06-04 16:24:37 +02002048 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02002049
Peter Zijlstra1aa16732009-05-27 20:20:25 +02002050 setup_sorting();
2051
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002052 if (parent_pattern != default_parent_pattern) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +02002053 sort_dimension__add("parent");
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002054 sort_parent.elide = 1;
2055 } else
Ingo Molnarb8e6d822009-06-18 14:32:19 +02002056 exclude_other = 0;
2057
Ingo Molnaredc52de2009-06-04 16:24:37 +02002058 /*
2059 * Any (unrecognized) arguments left?
2060 */
2061 if (argc)
2062 usage_with_options(report_usage, options);
2063
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002064 setup_pager();
2065
2066 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
2067 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
2068 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03002069
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03002070 if (field_sep && *field_sep == '.') {
2071 fputs("'.' is the only non valid --field-separator argument\n",
2072 stderr);
2073 exit(129);
2074 }
2075
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02002076 return __cmd_report();
2077}