blob: a5e2f8df411c1c320793fdc1622a985bf36d72d5 [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
Pekka Enberg114cfab2009-08-05 13:25:21 +030034static char default_sort_order[] = "comm,dso,symbol";
Ingo Molnarbd741372009-06-04 14:13:04 +020035static 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;
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -030054static int show_nr_samples;
Ingo Molnar97b07b62009-05-26 18:48:58 +020055
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030056static unsigned long page_size;
57static unsigned long mmap_window = 32;
58
Ingo Molnarb8e6d822009-06-18 14:32:19 +020059static char default_parent_pattern[] = "^sys_|^do_page_fault";
60static char *parent_pattern = default_parent_pattern;
Ingo Molnarb25bcf22009-06-18 07:01:03 +020061static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020062
Ingo Molnarb8e6d822009-06-18 14:32:19 +020063static int exclude_other = 1;
Frederic Weisbeckerbe903882009-07-05 07:39:18 +020064
Frederic Weisbecker805d1272009-07-05 07:39:21 +020065static char callchain_default_opt[] = "fractal,0.5";
66
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020067static int callchain;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020068
69static
70struct callchain_param callchain_param = {
71 .mode = CHAIN_GRAPH_ABS,
72 .min_percent = 0.5
73};
Ingo Molnarb8e6d822009-06-18 14:32:19 +020074
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020075static u64 sample_type;
76
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030077struct ip_event {
78 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100079 u64 ip;
80 u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020081 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030082};
Ingo Molnar75051722009-06-03 23:14:49 +020083
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030084struct mmap_event {
85 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100086 u32 pid, tid;
87 u64 start;
88 u64 len;
89 u64 pgoff;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030090 char filename[PATH_MAX];
91};
Ingo Molnar75051722009-06-03 23:14:49 +020092
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030093struct comm_event {
94 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100095 u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030096 char comm[16];
97};
98
Peter Zijlstra62fc4452009-06-04 16:53:49 +020099struct fork_event {
100 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000101 u32 pid, ppid;
Peter Zijlstra27d028d2009-07-23 16:52:41 +0200102 u32 tid, ptid;
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200103};
104
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200105struct lost_event {
106 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000107 u64 id;
108 u64 lost;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200109};
110
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200111struct read_event {
112 struct perf_event_header header;
113 u32 pid,tid;
114 u64 value;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200115 u64 time_enabled;
116 u64 time_running;
117 u64 id;
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200118};
119
Ingo Molnarb2fef072009-06-05 18:07:51 +0200120typedef union event_union {
121 struct perf_event_header header;
122 struct ip_event ip;
123 struct mmap_event mmap;
124 struct comm_event comm;
125 struct fork_event fork;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200126 struct lost_event lost;
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200127 struct read_event read;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300128} event_t;
129
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300130static int repsep_fprintf(FILE *fp, const char *fmt, ...)
131{
132 int n;
133 va_list ap;
134
135 va_start(ap, fmt);
136 if (!field_sep)
137 n = vfprintf(fp, fmt, ap);
138 else {
139 char *bf = NULL;
140 n = vasprintf(&bf, fmt, ap);
141 if (n > 0) {
142 char *sep = bf;
143 while (1) {
144 sep = strchr(sep, *field_sep);
145 if (sep == NULL)
146 break;
147 *sep = '.';
148 }
149 }
150 fputs(bf, fp);
151 free(bf);
152 }
153 va_end(ap);
154 return n;
155}
156
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300157static LIST_HEAD(dsos);
158static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200159static struct dso *vdso;
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000160static struct dso *hypervisor_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300161
162static void dsos__add(struct dso *dso)
163{
164 list_add_tail(&dso->node, &dsos);
165}
166
167static struct dso *dsos__find(const char *name)
168{
169 struct dso *pos;
170
171 list_for_each_entry(pos, &dsos, node)
172 if (strcmp(pos->name, name) == 0)
173 return pos;
174 return NULL;
175}
176
177static struct dso *dsos__findnew(const char *name)
178{
179 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200180 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300181
Ingo Molnar4593bba2009-06-02 15:34:25 +0200182 if (dso)
183 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300184
Ingo Molnar4593bba2009-06-02 15:34:25 +0200185 dso = dso__new(name, 0);
186 if (!dso)
187 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200188
Ingo Molnarbd741372009-06-04 14:13:04 +0200189 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200190 if (nr < 0) {
Ingo Molnar75220602009-06-18 08:00:17 +0200191 eprintf("Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200192 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300193 }
Ingo Molnar75220602009-06-18 08:00:17 +0200194 if (!nr)
195 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200196
197 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300198
199 return dso;
200
201out_delete_dso:
202 dso__delete(dso);
203 return NULL;
204}
205
Ingo Molnar16f762a2009-05-27 09:10:38 +0200206static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300207{
208 struct dso *pos;
209
210 list_for_each_entry(pos, &dsos, node)
211 dso__fprintf(pos, fp);
212}
213
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000214static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200215{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200216 return dso__find_symbol(dso, ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200217}
218
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200219static int load_kernel(void)
220{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300221 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200222
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300223 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200224 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300225 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200226
Mike Galbraith42976482009-07-02 08:09:46 +0200227 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
Mike Galbraith9974f492009-07-02 08:05:58 +0200228 if (err <= 0) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300229 dso__delete(kernel_dso);
230 kernel_dso = NULL;
231 } else
232 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200233
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200234 vdso = dso__new("[vdso]", 0);
235 if (!vdso)
236 return -1;
237
238 vdso->find_symbol = vdso__find_symbol;
239
240 dsos__add(vdso);
241
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000242 hypervisor_dso = dso__new("[hypervisor]", 0);
243 if (!hypervisor_dso)
244 return -1;
245 dsos__add(hypervisor_dso);
246
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300247 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200248}
249
Ingo Molnard80d3382009-06-03 23:14:49 +0200250static char __cwd[PATH_MAX];
251static char *cwd = __cwd;
252static int cwdlen;
253
254static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300255{
256 int n = 0;
257
Roel Kluin7e030652009-08-02 13:43:11 +0200258 while (n < cwdlen && pathname[n] == cwd[n])
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300259 ++n;
260
261 return n;
262}
263
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300264struct map {
265 struct list_head node;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000266 u64 start;
267 u64 end;
268 u64 pgoff;
269 u64 (*map_ip)(struct map *, u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300270 struct dso *dso;
271};
272
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000273static u64 map__map_ip(struct map *map, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200274{
275 return ip - map->start + map->pgoff;
276}
277
Ingo Molnarf37a2912009-07-01 12:37:06 +0200278static u64 vdso__map_ip(struct map *map __used, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200279{
280 return ip;
281}
282
Pekka Enberg80d496b2009-06-08 21:12:48 +0300283static inline int is_anon_memory(const char *filename)
284{
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300285 return strcmp(filename, "//anon") == 0;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300286}
287
Ingo Molnard80d3382009-06-03 23:14:49 +0200288static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300289{
290 struct map *self = malloc(sizeof(*self));
291
292 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300293 const char *filename = event->filename;
294 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300295 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300296
297 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200298 int n = strcommon(filename);
299
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300300 if (n == cwdlen) {
301 snprintf(newfilename, sizeof(newfilename),
302 ".%s", filename + n);
303 filename = newfilename;
304 }
305 }
306
Pekka Enberg80d496b2009-06-08 21:12:48 +0300307 anon = is_anon_memory(filename);
308
309 if (anon) {
310 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
311 filename = newfilename;
312 }
313
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300314 self->start = event->start;
315 self->end = event->start + event->len;
316 self->pgoff = event->pgoff;
317
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300318 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300319 if (self->dso == NULL)
320 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200321
Pekka Enberg80d496b2009-06-08 21:12:48 +0300322 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200323 self->map_ip = vdso__map_ip;
324 else
325 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300326 }
327 return self;
328out_delete:
329 free(self);
330 return NULL;
331}
332
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200333static struct map *map__clone(struct map *self)
334{
335 struct map *map = malloc(sizeof(*self));
336
337 if (!map)
338 return NULL;
339
340 memcpy(map, self, sizeof(*self));
341
342 return map;
343}
344
345static int map__overlap(struct map *l, struct map *r)
346{
347 if (l->start > r->start) {
348 struct map *t = l;
349 l = r;
350 r = t;
351 }
352
353 if (l->end > r->start)
354 return 1;
355
356 return 0;
357}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300358
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300359static size_t map__fprintf(struct map *self, FILE *fp)
360{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200361 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300362 self->start, self->end, self->pgoff, self->dso->name);
363}
364
365
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300366struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300367 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300368 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300369 pid_t pid;
370 char *comm;
371};
372
373static struct thread *thread__new(pid_t pid)
374{
375 struct thread *self = malloc(sizeof(*self));
376
377 if (self != NULL) {
378 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200379 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200380 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200381 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300382 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300383 }
384
385 return self;
386}
387
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300388static unsigned int dsos__col_width,
389 comms__col_width,
390 threads__col_width;
391
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300392static int thread__set_comm(struct thread *self, const char *comm)
393{
Peter Zijlstra82292892009-06-03 12:37:36 +0200394 if (self->comm)
395 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300396 self->comm = strdup(comm);
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300397 if (!self->comm)
398 return -ENOMEM;
399
400 if (!col_width_list_str && !field_sep &&
401 (!comm_list || strlist__has_entry(comm_list, comm))) {
402 unsigned int slen = strlen(comm);
403 if (slen > comms__col_width) {
404 comms__col_width = slen;
405 threads__col_width = slen + 6;
406 }
407 }
408
409 return 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300410}
411
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300412static size_t thread__fprintf(struct thread *self, FILE *fp)
413{
414 struct map *pos;
415 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
416
417 list_for_each_entry(pos, &self->maps, node)
418 ret += map__fprintf(pos, fp);
419
420 return ret;
421}
422
423
Ingo Molnar16f762a2009-05-27 09:10:38 +0200424static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200425static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300426
427static struct thread *threads__findnew(pid_t pid)
428{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300429 struct rb_node **p = &threads.rb_node;
430 struct rb_node *parent = NULL;
431 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300432
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200433 /*
434 * Font-end cache - PID lookups come in blocks,
435 * so most of the time we dont have to look up
436 * the full rbtree:
437 */
438 if (last_match && last_match->pid == pid)
439 return last_match;
440
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300441 while (*p != NULL) {
442 parent = *p;
443 th = rb_entry(parent, struct thread, rb_node);
444
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200445 if (th->pid == pid) {
446 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300447 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200448 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300449
450 if (pid < th->pid)
451 p = &(*p)->rb_left;
452 else
453 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300454 }
455
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300456 th = thread__new(pid);
457 if (th != NULL) {
458 rb_link_node(&th->rb_node, parent, p);
459 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200460 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300461 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200462
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300463 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300464}
465
466static void thread__insert_map(struct thread *self, struct map *map)
467{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200468 struct map *pos, *tmp;
469
470 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
471 if (map__overlap(pos, map)) {
Peter Zijlstra3d906ef2009-06-23 11:23:07 +0200472 if (verbose >= 2) {
473 printf("overlapping maps:\n");
474 map__fprintf(map, stdout);
475 map__fprintf(pos, stdout);
476 }
477
478 if (map->start <= pos->start && map->end > pos->start)
479 pos->start = map->end;
480
481 if (map->end >= pos->end && map->start < pos->end)
482 pos->end = map->start;
483
484 if (verbose >= 2) {
485 printf("after collision:\n");
486 map__fprintf(pos, stdout);
487 }
488
489 if (pos->start >= pos->end) {
490 list_del_init(&pos->node);
491 free(pos);
492 }
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200493 }
494 }
495
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300496 list_add_tail(&map->node, &self->maps);
497}
498
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200499static int thread__fork(struct thread *self, struct thread *parent)
500{
501 struct map *map;
502
503 if (self->comm)
504 free(self->comm);
505 self->comm = strdup(parent->comm);
506 if (!self->comm)
507 return -ENOMEM;
508
509 list_for_each_entry(map, &parent->maps, node) {
510 struct map *new = map__clone(map);
511 if (!new)
512 return -ENOMEM;
513 thread__insert_map(self, new);
514 }
515
516 return 0;
517}
518
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000519static struct map *thread__find_map(struct thread *self, u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300520{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200521 struct map *pos;
522
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300523 if (self == NULL)
524 return NULL;
525
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300526 list_for_each_entry(pos, &self->maps, node)
527 if (ip >= pos->start && ip <= pos->end)
528 return pos;
529
530 return NULL;
531}
532
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300533static size_t threads__fprintf(FILE *fp)
534{
535 size_t ret = 0;
536 struct rb_node *nd;
537
538 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
539 struct thread *pos = rb_entry(nd, struct thread, rb_node);
540
541 ret += thread__fprintf(pos, fp);
542 }
543
544 return ret;
545}
546
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200547/*
548 * histogram, sorted on item, collects counts
549 */
550
551static struct rb_root hist;
552
553struct hist_entry {
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200554 struct rb_node rb_node;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200555
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200556 struct thread *thread;
557 struct map *map;
558 struct dso *dso;
559 struct symbol *sym;
560 struct symbol *parent;
561 u64 ip;
562 char level;
563 struct callchain_node callchain;
564 struct rb_root sorted_chain;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200565
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200566 u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200567};
568
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200569/*
570 * configurable sorting bits
571 */
572
573struct sort_entry {
574 struct list_head list;
575
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200576 char *header;
577
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200578 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200579 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300580 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
581 unsigned int *width;
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300582 bool elide;
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200583};
584
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200585static int64_t cmp_null(void *l, void *r)
586{
587 if (!l && !r)
588 return 0;
589 else if (!l)
590 return -1;
591 else
592 return 1;
593}
594
Peter Zijlstra82292892009-06-03 12:37:36 +0200595/* --sort pid */
596
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200597static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200598sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
599{
600 return right->thread->pid - left->thread->pid;
601}
602
603static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300604sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200605{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300606 return repsep_fprintf(fp, "%*s:%5d", width - 6,
607 self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200608}
609
610static struct sort_entry sort_thread = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300611 .header = "Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200612 .cmp = sort__thread_cmp,
613 .print = sort__thread_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300614 .width = &threads__col_width,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200615};
616
Peter Zijlstra82292892009-06-03 12:37:36 +0200617/* --sort comm */
618
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200619static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200620sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
621{
Peter Zijlstra82292892009-06-03 12:37:36 +0200622 return right->thread->pid - left->thread->pid;
623}
624
625static int64_t
626sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
627{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200628 char *comm_l = left->thread->comm;
629 char *comm_r = right->thread->comm;
630
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200631 if (!comm_l || !comm_r)
632 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200633
634 return strcmp(comm_l, comm_r);
635}
636
637static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300638sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra992444b2009-05-27 20:20:27 +0200639{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300640 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200641}
642
643static struct sort_entry sort_comm = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300644 .header = "Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200645 .cmp = sort__comm_cmp,
646 .collapse = sort__comm_collapse,
647 .print = sort__comm_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300648 .width = &comms__col_width,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200649};
650
Peter Zijlstra82292892009-06-03 12:37:36 +0200651/* --sort dso */
652
Peter Zijlstra992444b2009-05-27 20:20:27 +0200653static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200654sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
655{
656 struct dso *dso_l = left->dso;
657 struct dso *dso_r = right->dso;
658
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200659 if (!dso_l || !dso_r)
660 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200661
662 return strcmp(dso_l->name, dso_r->name);
663}
664
665static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300666sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200667{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200668 if (self->dso)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300669 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200670
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300671 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200672}
673
674static struct sort_entry sort_dso = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300675 .header = "Shared Object",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200676 .cmp = sort__dso_cmp,
677 .print = sort__dso_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300678 .width = &dsos__col_width,
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200679};
680
Peter Zijlstra82292892009-06-03 12:37:36 +0200681/* --sort symbol */
682
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200683static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200684sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300685{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000686 u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200687
688 if (left->sym == right->sym)
689 return 0;
690
691 ip_l = left->sym ? left->sym->start : left->ip;
692 ip_r = right->sym ? right->sym->start : right->ip;
693
694 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300695}
696
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200697static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300698sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200699{
700 size_t ret = 0;
701
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200702 if (verbose)
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300703 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
704 dso__symtab_origin(self->dso));
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200705
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300706 ret += repsep_fprintf(fp, "[%c] ", self->level);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200707 if (self->sym) {
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300708 ret += repsep_fprintf(fp, "%s", self->sym->name);
Mike Galbraith42976482009-07-02 08:09:46 +0200709
710 if (self->sym->module)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300711 ret += repsep_fprintf(fp, "\t[%s]",
712 self->sym->module->name);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200713 } else {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300714 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200715 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200716
717 return ret;
718}
719
720static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200721 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200722 .cmp = sort__sym_cmp,
723 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200724};
725
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200726/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200727
728static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200729sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200730{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200731 struct symbol *sym_l = left->parent;
732 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200733
734 if (!sym_l || !sym_r)
735 return cmp_null(sym_l, sym_r);
736
737 return strcmp(sym_l->name, sym_r->name);
738}
739
740static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300741sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200742{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300743 return repsep_fprintf(fp, "%-*s", width,
744 self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200745}
746
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300747static unsigned int parent_symbol__col_width;
748
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200749static struct sort_entry sort_parent = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300750 .header = "Parent symbol",
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200751 .cmp = sort__parent_cmp,
752 .print = sort__parent_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300753 .width = &parent_symbol__col_width,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200754};
755
Peter Zijlstra82292892009-06-03 12:37:36 +0200756static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200757static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200758
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200759struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200760 char *name;
761 struct sort_entry *entry;
762 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200763};
764
765static struct sort_dimension sort_dimensions[] = {
766 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200767 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200768 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200769 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200770 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200771};
772
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200773static LIST_HEAD(hist_entry__sort_list);
774
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200775static int sort_dimension__add(char *tok)
776{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200777 unsigned int i;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200778
779 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
780 struct sort_dimension *sd = &sort_dimensions[i];
781
782 if (sd->taken)
783 continue;
784
Ingo Molnar5352f352009-06-03 10:07:39 +0200785 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200786 continue;
787
Peter Zijlstra82292892009-06-03 12:37:36 +0200788 if (sd->entry->collapse)
789 sort__need_collapse = 1;
790
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200791 if (sd->entry == &sort_parent) {
792 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200793 if (ret) {
794 char err[BUFSIZ];
795
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200796 regerror(ret, &parent_regex, err, sizeof(err));
797 fprintf(stderr, "Invalid regex: %s\n%s",
798 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200799 exit(-1);
800 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200801 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200802 }
803
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200804 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
805 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200806
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200807 return 0;
808 }
809
810 return -ESRCH;
811}
812
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200813static int64_t
814hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
815{
816 struct sort_entry *se;
817 int64_t cmp = 0;
818
819 list_for_each_entry(se, &hist_entry__sort_list, list) {
820 cmp = se->cmp(left, right);
821 if (cmp)
822 break;
823 }
824
825 return cmp;
826}
827
Peter Zijlstra82292892009-06-03 12:37:36 +0200828static int64_t
829hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
830{
831 struct sort_entry *se;
832 int64_t cmp = 0;
833
834 list_for_each_entry(se, &hist_entry__sort_list, list) {
835 int64_t (*f)(struct hist_entry *, struct hist_entry *);
836
837 f = se->collapse ?: se->cmp;
838
839 cmp = f(left, right);
840 if (cmp)
841 break;
842 }
843
844 return cmp;
845}
846
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200847static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
848{
849 int i;
850 size_t ret = 0;
851
852 ret += fprintf(fp, "%s", " ");
853
854 for (i = 0; i < depth; i++)
855 if (depth_mask & (1 << i))
856 ret += fprintf(fp, "| ");
857 else
858 ret += fprintf(fp, " ");
859
860 ret += fprintf(fp, "\n");
861
862 return ret;
863}
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200864static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200865ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
866 int depth_mask, int count, u64 total_samples,
867 int hits)
868{
869 int i;
870 size_t ret = 0;
871
872 ret += fprintf(fp, "%s", " ");
873 for (i = 0; i < depth; i++) {
874 if (depth_mask & (1 << i))
875 ret += fprintf(fp, "|");
876 else
877 ret += fprintf(fp, " ");
878 if (!count && i == depth - 1) {
879 double percent;
880
881 percent = hits * 100.0 / total_samples;
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200882 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200883 } else
884 ret += fprintf(fp, "%s", " ");
885 }
886 if (chain->sym)
887 ret += fprintf(fp, "%s\n", chain->sym->name);
888 else
889 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
890
891 return ret;
892}
893
894static size_t
895callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
896 u64 total_samples, int depth, int depth_mask)
897{
898 struct rb_node *node, *next;
899 struct callchain_node *child;
900 struct callchain_list *chain;
901 int new_depth_mask = depth_mask;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200902 u64 new_total;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200903 size_t ret = 0;
904 int i;
905
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200906 if (callchain_param.mode == CHAIN_GRAPH_REL)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200907 new_total = self->children_hit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200908 else
909 new_total = total_samples;
910
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200911 node = rb_first(&self->rb_root);
912 while (node) {
913 child = rb_entry(node, struct callchain_node, rb_node);
914
915 /*
916 * The depth mask manages the output of pipes that show
917 * the depth. We don't want to keep the pipes of the current
918 * level for the last child of this depth
919 */
920 next = rb_next(node);
921 if (!next)
922 new_depth_mask &= ~(1 << (depth - 1));
923
924 /*
925 * But we keep the older depth mask for the line seperator
926 * to keep the level link until we reach the last child
927 */
928 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
929 i = 0;
930 list_for_each_entry(chain, &child->val, list) {
931 if (chain->ip >= PERF_CONTEXT_MAX)
932 continue;
933 ret += ipchain__fprintf_graph(fp, chain, depth,
934 new_depth_mask, i++,
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200935 new_total,
Frederic Weisbecker19532872009-08-07 07:11:05 +0200936 cumul_hits(child));
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200937 }
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200938 ret += callchain__fprintf_graph(fp, child, new_total,
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200939 depth + 1,
940 new_depth_mask | (1 << depth));
941 node = next;
942 }
943
944 return ret;
945}
946
947static size_t
948callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
949 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200950{
951 struct callchain_list *chain;
952 size_t ret = 0;
953
954 if (!self)
955 return 0;
956
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200957 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200958
959
Frederic Weisbecker44249612009-07-01 05:35:14 +0200960 list_for_each_entry(chain, &self->val, list) {
961 if (chain->ip >= PERF_CONTEXT_MAX)
962 continue;
963 if (chain->sym)
964 ret += fprintf(fp, " %s\n", chain->sym->name);
965 else
966 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200967 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200968 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200969
970 return ret;
971}
972
973static size_t
974hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
975 u64 total_samples)
976{
977 struct rb_node *rb_node;
978 struct callchain_node *chain;
979 size_t ret = 0;
980
981 rb_node = rb_first(&self->sorted_chain);
982 while (rb_node) {
983 double percent;
984
985 chain = rb_entry(rb_node, struct callchain_node, rb_node);
986 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200987 switch (callchain_param.mode) {
988 case CHAIN_FLAT:
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200989 ret += percent_color_fprintf(fp, " %6.2f%%\n",
990 percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200991 ret += callchain__fprintf_flat(fp, chain, total_samples);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200992 break;
993 case CHAIN_GRAPH_ABS: /* Falldown */
994 case CHAIN_GRAPH_REL:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200995 ret += callchain__fprintf_graph(fp, chain,
996 total_samples, 1, 1);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200997 default:
998 break;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200999 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001000 ret += fprintf(fp, "\n");
1001 rb_node = rb_next(rb_node);
1002 }
1003
1004 return ret;
1005}
1006
1007
1008static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001009hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001010{
1011 struct sort_entry *se;
1012 size_t ret;
1013
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001014 if (exclude_other && !self->parent)
1015 return 0;
1016
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +02001017 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001018 ret = percent_color_fprintf(fp,
1019 field_sep ? "%.2f" : " %6.2f%%",
1020 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +02001021 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001022 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001023
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001024 if (show_nr_samples) {
1025 if (field_sep)
1026 fprintf(fp, "%c%lld", *field_sep, self->count);
1027 else
1028 fprintf(fp, "%11lld", self->count);
1029 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001030
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001031 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001032 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001033 continue;
1034
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001035 fprintf(fp, "%s", field_sep ?: " ");
1036 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001037 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001038
1039 ret += fprintf(fp, "\n");
1040
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001041 if (callchain)
1042 hist_entry_callchain__fprintf(fp, self, total_samples);
1043
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001044 return ret;
1045}
1046
1047/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001048 *
1049 */
1050
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001051static void dso__calc_col_width(struct dso *self)
1052{
1053 if (!col_width_list_str && !field_sep &&
1054 (!dso_list || strlist__has_entry(dso_list, self->name))) {
1055 unsigned int slen = strlen(self->name);
1056 if (slen > dsos__col_width)
1057 dsos__col_width = slen;
1058 }
1059
1060 self->slen_calculated = 1;
1061}
1062
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001063static struct symbol *
1064resolve_symbol(struct thread *thread, struct map **mapp,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001065 struct dso **dsop, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001066{
1067 struct dso *dso = dsop ? *dsop : NULL;
1068 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +02001069 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001070
1071 if (!thread)
1072 return NULL;
1073
1074 if (dso)
1075 goto got_dso;
1076
1077 if (map)
1078 goto got_map;
1079
1080 map = thread__find_map(thread, ip);
1081 if (map != NULL) {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001082 /*
1083 * We have to do this here as we may have a dso
1084 * with no symbol hit that has a name longer than
1085 * the ones with symbols sampled.
1086 */
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001087 if (!sort_dso.elide && !map->dso->slen_calculated)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001088 dso__calc_col_width(map->dso);
1089
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001090 if (mapp)
1091 *mapp = map;
1092got_map:
1093 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001094
1095 dso = map->dso;
1096 } else {
1097 /*
1098 * If this is outside of all known maps,
1099 * and is a negative address, try to look it
1100 * up in the kernel dso, as it might be a
1101 * vsyscall (which executes in user-mode):
1102 */
1103 if ((long long)ip < 0)
1104 dso = kernel_dso;
1105 }
1106 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Peter Zijlstra520f2c32009-06-22 16:52:51 +02001107 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1108 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001109
1110 if (dsop)
1111 *dsop = dso;
1112
1113 if (!dso)
1114 return NULL;
1115got_dso:
1116 return dso->find_symbol(dso, ip);
1117}
1118
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001119static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001120{
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001121 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001122 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001123
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001124 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001125}
1126
Frederic Weisbecker44249612009-07-01 05:35:14 +02001127static struct symbol **
Ingo Molnarf37a2912009-07-01 12:37:06 +02001128resolve_callchain(struct thread *thread, struct map *map __used,
Frederic Weisbecker44249612009-07-01 05:35:14 +02001129 struct ip_callchain *chain, struct hist_entry *entry)
1130{
Frederic Weisbecker44249612009-07-01 05:35:14 +02001131 u64 context = PERF_CONTEXT_MAX;
Ingo Molnar029e5b12009-07-03 13:17:28 +02001132 struct symbol **syms = NULL;
Ingo Molnarf37a2912009-07-01 12:37:06 +02001133 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001134
1135 if (callchain) {
1136 syms = calloc(chain->nr, sizeof(*syms));
1137 if (!syms) {
1138 fprintf(stderr, "Can't allocate memory for symbols\n");
1139 exit(-1);
1140 }
1141 }
1142
1143 for (i = 0; i < chain->nr; i++) {
1144 u64 ip = chain->ips[i];
1145 struct dso *dso = NULL;
1146 struct symbol *sym;
1147
1148 if (ip >= PERF_CONTEXT_MAX) {
1149 context = ip;
1150 continue;
1151 }
1152
1153 switch (context) {
Ingo Molnar88a69df2009-07-01 11:17:20 +02001154 case PERF_CONTEXT_HV:
1155 dso = hypervisor_dso;
1156 break;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001157 case PERF_CONTEXT_KERNEL:
1158 dso = kernel_dso;
1159 break;
1160 default:
1161 break;
1162 }
1163
1164 sym = resolve_symbol(thread, NULL, &dso, &ip);
1165
1166 if (sym) {
1167 if (sort__has_parent && call__match(sym) &&
1168 !entry->parent)
1169 entry->parent = sym;
1170 if (!callchain)
1171 break;
1172 syms[i] = sym;
1173 }
1174 }
1175
1176 return syms;
1177}
1178
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001179/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001180 * collect histogram counts
1181 */
1182
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001183static int
1184hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001185 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1186 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001187{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001188 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001189 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001190 struct hist_entry *he;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001191 struct symbol **syms = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001192 struct hist_entry entry = {
1193 .thread = thread,
1194 .map = map,
1195 .dso = dso,
1196 .sym = sym,
1197 .ip = ip,
1198 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001199 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001200 .parent = NULL,
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001201 .sorted_chain = RB_ROOT
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001202 };
1203 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001204
Frederic Weisbecker44249612009-07-01 05:35:14 +02001205 if ((sort__has_parent || callchain) && chain)
1206 syms = resolve_callchain(thread, map, chain, &entry);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001207
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001208 while (*p != NULL) {
1209 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001210 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001211
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001212 cmp = hist_entry__cmp(&entry, he);
1213
1214 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001215 he->count += count;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001216 if (callchain) {
1217 append_chain(&he->callchain, chain, syms);
1218 free(syms);
1219 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001220 return 0;
1221 }
1222
1223 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001224 p = &(*p)->rb_left;
1225 else
1226 p = &(*p)->rb_right;
1227 }
1228
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001229 he = malloc(sizeof(*he));
1230 if (!he)
1231 return -ENOMEM;
1232 *he = entry;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001233 if (callchain) {
1234 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +02001235 append_chain(&he->callchain, chain, syms);
1236 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001237 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001238 rb_link_node(&he->rb_node, parent, p);
1239 rb_insert_color(&he->rb_node, &hist);
1240
1241 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001242}
1243
Peter Zijlstra82292892009-06-03 12:37:36 +02001244static void hist_entry__free(struct hist_entry *he)
1245{
1246 free(he);
1247}
1248
1249/*
1250 * collapse the histogram
1251 */
1252
1253static struct rb_root collapse_hists;
1254
1255static void collapse__insert_entry(struct hist_entry *he)
1256{
1257 struct rb_node **p = &collapse_hists.rb_node;
1258 struct rb_node *parent = NULL;
1259 struct hist_entry *iter;
1260 int64_t cmp;
1261
1262 while (*p != NULL) {
1263 parent = *p;
1264 iter = rb_entry(parent, struct hist_entry, rb_node);
1265
1266 cmp = hist_entry__collapse(iter, he);
1267
1268 if (!cmp) {
1269 iter->count += he->count;
1270 hist_entry__free(he);
1271 return;
1272 }
1273
1274 if (cmp < 0)
1275 p = &(*p)->rb_left;
1276 else
1277 p = &(*p)->rb_right;
1278 }
1279
1280 rb_link_node(&he->rb_node, parent, p);
1281 rb_insert_color(&he->rb_node, &collapse_hists);
1282}
1283
1284static void collapse__resort(void)
1285{
1286 struct rb_node *next;
1287 struct hist_entry *n;
1288
1289 if (!sort__need_collapse)
1290 return;
1291
1292 next = rb_first(&hist);
1293 while (next) {
1294 n = rb_entry(next, struct hist_entry, rb_node);
1295 next = rb_next(&n->rb_node);
1296
1297 rb_erase(&n->rb_node, &hist);
1298 collapse__insert_entry(n);
1299 }
1300}
1301
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001302/*
1303 * reverse the map, sort on count.
1304 */
1305
1306static struct rb_root output_hists;
1307
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001308static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001309{
1310 struct rb_node **p = &output_hists.rb_node;
1311 struct rb_node *parent = NULL;
1312 struct hist_entry *iter;
1313
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001314 if (callchain)
1315 callchain_param.sort(&he->sorted_chain, &he->callchain,
1316 min_callchain_hits, &callchain_param);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001317
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001318 while (*p != NULL) {
1319 parent = *p;
1320 iter = rb_entry(parent, struct hist_entry, rb_node);
1321
1322 if (he->count > iter->count)
1323 p = &(*p)->rb_left;
1324 else
1325 p = &(*p)->rb_right;
1326 }
1327
1328 rb_link_node(&he->rb_node, parent, p);
1329 rb_insert_color(&he->rb_node, &output_hists);
1330}
1331
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001332static void output__resort(u64 total_samples)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001333{
Peter Zijlstra82292892009-06-03 12:37:36 +02001334 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001335 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001336 struct rb_root *tree = &hist;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001337 u64 min_callchain_hits;
1338
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001339 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001340
Peter Zijlstra82292892009-06-03 12:37:36 +02001341 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001342 tree = &collapse_hists;
1343
1344 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001345
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001346 while (next) {
1347 n = rb_entry(next, struct hist_entry, rb_node);
1348 next = rb_next(&n->rb_node);
1349
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001350 rb_erase(&n->rb_node, tree);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001351 output__insert_entry(n, min_callchain_hits);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001352 }
1353}
1354
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001355static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001356{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001357 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001358 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001359 struct rb_node *nd;
1360 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001361 unsigned int width;
1362 char *col_width = col_width_list_str;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001363
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001364 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001365 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001366
1367 fprintf(fp, "# Overhead");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001368 if (show_nr_samples) {
1369 if (field_sep)
1370 fprintf(fp, "%cSamples", *field_sep);
1371 else
1372 fputs(" Samples ", fp);
1373 }
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001374 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001375 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001376 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001377 if (field_sep) {
1378 fprintf(fp, "%c%s", *field_sep, se->header);
1379 continue;
1380 }
1381 width = strlen(se->header);
1382 if (se->width) {
1383 if (col_width_list_str) {
1384 if (col_width) {
1385 *se->width = atoi(col_width);
1386 col_width = strchr(col_width, ',');
1387 if (col_width)
1388 ++col_width;
1389 }
1390 }
1391 width = *se->width = max(*se->width, width);
1392 }
1393 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001394 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001395 fprintf(fp, "\n");
1396
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001397 if (field_sep)
1398 goto print_entries;
1399
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001400 fprintf(fp, "# ........");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001401 if (show_nr_samples)
1402 fprintf(fp, " ..........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001403 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001404 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001405
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001406 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001407 continue;
1408
Ingo Molnar4593bba2009-06-02 15:34:25 +02001409 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001410 if (se->width)
1411 width = *se->width;
1412 else
1413 width = strlen(se->header);
1414 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001415 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001416 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001417 fprintf(fp, "\n");
1418
1419 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001420
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001421print_entries:
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001422 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1423 pos = rb_entry(nd, struct hist_entry, rb_node);
1424 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001425 }
1426
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001427 if (sort_order == default_sort_order &&
1428 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001429 fprintf(fp, "#\n");
Pekka Enberg114cfab2009-08-05 13:25:21 +03001430 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001431 fprintf(fp, "#\n");
1432 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001433 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001434
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001435 return ret;
1436}
1437
Peter Zijlstra436224a2009-06-02 21:02:36 +02001438static void register_idle_thread(void)
1439{
1440 struct thread *thread = threads__findnew(0);
1441
1442 if (thread == NULL ||
1443 thread__set_comm(thread, "[idle]")) {
1444 fprintf(stderr, "problem inserting idle task.\n");
1445 exit(-1);
1446 }
1447}
1448
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001449static unsigned long total = 0,
1450 total_mmap = 0,
1451 total_comm = 0,
1452 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001453 total_unknown = 0,
1454 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001455
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001456static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +02001457{
1458 unsigned int chain_size;
1459
Ingo Molnar75220602009-06-18 08:00:17 +02001460 chain_size = event->header.size;
1461 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1462
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001463 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +02001464 return -1;
1465
1466 return 0;
1467}
1468
Ingo Molnard80d3382009-06-03 23:14:49 +02001469static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001470process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +02001471{
1472 char level;
1473 int show = 0;
1474 struct dso *dso = NULL;
1475 struct thread *thread = threads__findnew(event->ip.pid);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001476 u64 ip = event->ip.ip;
1477 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001478 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001479 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001480 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001481 int cpumode;
Ingo Molnar75051722009-06-03 23:14:49 +02001482
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001483 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001484 period = *(u64 *)more_data;
1485 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001486 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001487
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001488 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001489 (void *)(offset + head),
1490 (void *)(long)(event->header.size),
1491 event->header.misc,
1492 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001493 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001494 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001495
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001496 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001497 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001498
1499 chain = (void *)more_data;
1500
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001501 dprintf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001502
Ingo Molnar75220602009-06-18 08:00:17 +02001503 if (validate_chain(chain, event) < 0) {
1504 eprintf("call-chain problem with event, skipping it.\n");
1505 return 0;
1506 }
1507
1508 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001509 for (i = 0; i < chain->nr; i++)
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001510 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001511 }
1512 }
1513
Ingo Molnar75051722009-06-03 23:14:49 +02001514 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1515
1516 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001517 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001518 event->header.type);
1519 return -1;
1520 }
1521
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001522 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1523 return 0;
1524
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001525 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1526
1527 if (cpumode == PERF_EVENT_MISC_KERNEL) {
Ingo Molnar75051722009-06-03 23:14:49 +02001528 show = SHOW_KERNEL;
1529 level = 'k';
1530
1531 dso = kernel_dso;
1532
1533 dprintf(" ...... dso: %s\n", dso->name);
1534
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001535 } else if (cpumode == PERF_EVENT_MISC_USER) {
Ingo Molnar75051722009-06-03 23:14:49 +02001536
1537 show = SHOW_USER;
1538 level = '.';
1539
Ingo Molnar75051722009-06-03 23:14:49 +02001540 } else {
1541 show = SHOW_HV;
1542 level = 'H';
Anton Blanchardfb9c8182009-07-01 09:00:49 +10001543
1544 dso = hypervisor_dso;
1545
Ingo Molnar75051722009-06-03 23:14:49 +02001546 dprintf(" ...... dso: [hypervisor]\n");
1547 }
1548
1549 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001550 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001551
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001552 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1553 return 0;
1554
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001555 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1556 return 0;
1557
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001558 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001559 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001560 return -1;
1561 }
1562 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001563 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001564
1565 return 0;
1566}
1567
1568static int
1569process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1570{
1571 struct thread *thread = threads__findnew(event->mmap.pid);
1572 struct map *map = map__new(&event->mmap);
1573
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001574 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001575 (void *)(offset + head),
1576 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001577 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001578 (void *)(long)event->mmap.start,
1579 (void *)(long)event->mmap.len,
1580 (void *)(long)event->mmap.pgoff,
1581 event->mmap.filename);
1582
1583 if (thread == NULL || map == NULL) {
1584 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001585 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001586 }
1587
1588 thread__insert_map(thread, map);
1589 total_mmap++;
1590
1591 return 0;
1592}
1593
1594static int
1595process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1596{
1597 struct thread *thread = threads__findnew(event->comm.pid);
1598
1599 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1600 (void *)(offset + head),
1601 (void *)(long)(event->header.size),
1602 event->comm.comm, event->comm.pid);
1603
1604 if (thread == NULL ||
1605 thread__set_comm(thread, event->comm.comm)) {
1606 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1607 return -1;
1608 }
1609 total_comm++;
1610
1611 return 0;
1612}
1613
1614static int
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001615process_task_event(event_t *event, unsigned long offset, unsigned long head)
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001616{
1617 struct thread *thread = threads__findnew(event->fork.pid);
1618 struct thread *parent = threads__findnew(event->fork.ppid);
1619
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001620 dprintf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001621 (void *)(offset + head),
1622 (void *)(long)(event->header.size),
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001623 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1624 event->fork.pid, event->fork.tid,
1625 event->fork.ppid, event->fork.ptid);
1626
1627 /*
1628 * A thread clone will have the same PID for both
1629 * parent and child.
1630 */
1631 if (thread == parent)
1632 return 0;
1633
1634 if (event->header.type == PERF_EVENT_EXIT)
1635 return 0;
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001636
1637 if (!thread || !parent || thread__fork(thread, parent)) {
1638 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1639 return -1;
1640 }
1641 total_fork++;
1642
1643 return 0;
1644}
1645
1646static int
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001647process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1648{
1649 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1650 (void *)(offset + head),
1651 (void *)(long)(event->header.size),
1652 event->lost.id,
1653 event->lost.lost);
1654
1655 total_lost += event->lost.lost;
1656
1657 return 0;
1658}
1659
Ingo Molnar8465b052009-06-14 14:44:07 +02001660static void trace_event(event_t *event)
1661{
1662 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001663 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001664 int i, j;
1665
1666 if (!dump_trace)
1667 return;
1668
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001669 dprintf(".");
1670 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001671
1672 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001673 if ((i & 15) == 0) {
1674 dprintf(".");
1675 cdprintf(" %04x: ", i);
1676 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001677
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001678 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001679
1680 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001681 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001682 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001683 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001684 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001685 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001686 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001687 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001688 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001689 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001690 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001691 }
1692 }
1693 dprintf(".\n");
1694}
1695
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001696static struct perf_header *header;
1697
1698static struct perf_counter_attr *perf_header__find_attr(u64 id)
1699{
1700 int i;
1701
1702 for (i = 0; i < header->attrs; i++) {
1703 struct perf_header_attr *attr = header->attr[i];
1704 int j;
1705
1706 for (j = 0; j < attr->ids; j++) {
1707 if (attr->id[j] == id)
1708 return &attr->attr;
1709 }
1710 }
1711
1712 return NULL;
1713}
1714
Ingo Molnarb2fef072009-06-05 18:07:51 +02001715static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001716process_read_event(event_t *event, unsigned long offset, unsigned long head)
1717{
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001718 struct perf_counter_attr *attr = perf_header__find_attr(event->read.id);
1719
1720 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001721 (void *)(offset + head),
1722 (void *)(long)(event->header.size),
1723 event->read.pid,
1724 event->read.tid,
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001725 attr ? __event_name(attr->type, attr->config)
1726 : "FAIL",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001727 event->read.value);
1728
1729 return 0;
1730}
1731
1732static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001733process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001734{
Ingo Molnar8465b052009-06-14 14:44:07 +02001735 trace_event(event);
1736
Ingo Molnar75051722009-06-03 23:14:49 +02001737 switch (event->header.type) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001738 case PERF_EVENT_SAMPLE:
1739 return process_sample_event(event, offset, head);
1740
Ingo Molnar75051722009-06-03 23:14:49 +02001741 case PERF_EVENT_MMAP:
1742 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001743
Ingo Molnar75051722009-06-03 23:14:49 +02001744 case PERF_EVENT_COMM:
1745 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001746
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001747 case PERF_EVENT_FORK:
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001748 case PERF_EVENT_EXIT:
1749 return process_task_event(event, offset, head);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001750
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001751 case PERF_EVENT_LOST:
1752 return process_lost_event(event, offset, head);
1753
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001754 case PERF_EVENT_READ:
1755 return process_read_event(event, offset, head);
1756
Ingo Molnard11444d2009-06-03 23:29:14 +02001757 /*
1758 * We dont process them right now but they are fine:
1759 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001760
Ingo Molnard11444d2009-06-03 23:29:14 +02001761 case PERF_EVENT_THROTTLE:
1762 case PERF_EVENT_UNTHROTTLE:
1763 return 0;
1764
Ingo Molnard80d3382009-06-03 23:14:49 +02001765 default:
1766 return -1;
1767 }
1768
1769 return 0;
1770}
1771
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001772static u64 perf_header__sample_type(void)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001773{
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001774 u64 sample_type = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001775 int i;
1776
1777 for (i = 0; i < header->attrs; i++) {
1778 struct perf_header_attr *attr = header->attr[i];
1779
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001780 if (!sample_type)
1781 sample_type = attr->attr.sample_type;
1782 else if (sample_type != attr->attr.sample_type)
1783 die("non matching sample_type");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001784 }
1785
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001786 return sample_type;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001787}
Peter Zijlstraf5970552009-06-18 23:22:55 +02001788
Ingo Molnard80d3382009-06-03 23:14:49 +02001789static int __cmd_report(void)
1790{
Ingo Molnar75051722009-06-03 23:14:49 +02001791 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001792 unsigned long offset = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001793 unsigned long head, shift;
Ingo Molnard80d3382009-06-03 23:14:49 +02001794 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001795 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001796 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001797 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001798
1799 register_idle_thread();
1800
1801 input = open(input_name, O_RDONLY);
1802 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001803 fprintf(stderr, " failed to open file: %s", input_name);
1804 if (!strcmp(input_name, "perf.data"))
1805 fprintf(stderr, " (try 'perf record' first)");
1806 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001807 exit(-1);
1808 }
1809
1810 ret = fstat(input, &stat);
1811 if (ret < 0) {
1812 perror("failed to stat file");
1813 exit(-1);
1814 }
1815
1816 if (!stat.st_size) {
1817 fprintf(stderr, "zero-sized file, nothing to do!\n");
1818 exit(0);
1819 }
1820
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001821 header = perf_header__read(input);
1822 head = header->data_offset;
Peter Zijlstraf5970552009-06-18 23:22:55 +02001823
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001824 sample_type = perf_header__sample_type();
1825
Frederic Weisbecker91b4eae2009-07-05 07:39:17 +02001826 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1827 if (sort__has_parent) {
1828 fprintf(stderr, "selected --sort parent, but no"
1829 " callchain data. Did you call"
1830 " perf record without -g?\n");
1831 exit(-1);
1832 }
1833 if (callchain) {
1834 fprintf(stderr, "selected -c but no callchain data."
1835 " Did you call perf record without"
1836 " -g?\n");
1837 exit(-1);
1838 }
Peter Zijlstraf5970552009-06-18 23:22:55 +02001839 }
1840
Ingo Molnard80d3382009-06-03 23:14:49 +02001841 if (load_kernel() < 0) {
1842 perror("failed to load kernel symbols");
1843 return EXIT_FAILURE;
1844 }
1845
1846 if (!full_paths) {
1847 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1848 perror("failed to get the current directory");
1849 return EXIT_FAILURE;
1850 }
1851 cwdlen = strlen(cwd);
1852 } else {
1853 cwd = NULL;
1854 cwdlen = 0;
1855 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001856
1857 shift = page_size * (head / page_size);
1858 offset += shift;
1859 head -= shift;
1860
Ingo Molnard80d3382009-06-03 23:14:49 +02001861remap:
1862 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1863 MAP_SHARED, input, offset);
1864 if (buf == MAP_FAILED) {
1865 perror("failed to mmap file");
1866 exit(-1);
1867 }
1868
1869more:
1870 event = (event_t *)(buf + head);
1871
1872 size = event->header.size;
1873 if (!size)
1874 size = 8;
1875
1876 if (head + event->header.size >= page_size * mmap_window) {
Ingo Molnard80d3382009-06-03 23:14:49 +02001877 int ret;
1878
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001879 shift = page_size * (head / page_size);
1880
Ingo Molnard80d3382009-06-03 23:14:49 +02001881 ret = munmap(buf, page_size * mmap_window);
1882 assert(ret == 0);
1883
1884 offset += shift;
1885 head -= shift;
1886 goto remap;
1887 }
1888
1889 size = event->header.size;
1890
Ingo Molnar8465b052009-06-14 14:44:07 +02001891 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001892 (void *)(offset + head),
1893 (void *)(long)event->header.size,
1894 event->header.type);
1895
Ingo Molnard80d3382009-06-03 23:14:49 +02001896 if (!size || process_event(event, offset, head) < 0) {
1897
Ingo Molnar35029732009-06-03 09:38:58 +02001898 dprintf("%p [%p]: skipping unknown header type: %d\n",
1899 (void *)(offset + head),
1900 (void *)(long)(event->header.size),
1901 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001902
Ingo Molnar3e706112009-05-26 18:53:17 +02001903 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001904
1905 /*
1906 * assume we lost track of the stream, check alignment, and
1907 * increment a single u64 in the hope to catch on again 'soon'.
1908 */
1909
1910 if (unlikely(head & 7))
1911 head &= ~7ULL;
1912
1913 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001914 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001915
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001916 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001917
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001918 if (offset + head >= header->data_offset + header->data_size)
Peter Zijlstraf5970552009-06-18 23:22:55 +02001919 goto done;
1920
Ingo Molnarf37a2912009-07-01 12:37:06 +02001921 if (offset + head < (unsigned long)stat.st_size)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001922 goto more;
1923
Peter Zijlstraf5970552009-06-18 23:22:55 +02001924done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001925 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001926 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001927
Ingo Molnar35029732009-06-03 09:38:58 +02001928 dprintf(" IP events: %10ld\n", total);
1929 dprintf(" mmap events: %10ld\n", total_mmap);
1930 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001931 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001932 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001933 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001934
Ingo Molnar35029732009-06-03 09:38:58 +02001935 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001936 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001937
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001938 if (verbose >= 3)
1939 threads__fprintf(stdout);
1940
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001941 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001942 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001943
Peter Zijlstra82292892009-06-03 12:37:36 +02001944 collapse__resort();
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001945 output__resort(total);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001946 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001947
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001948 return rc;
1949}
1950
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001951static int
1952parse_callchain_opt(const struct option *opt __used, const char *arg,
1953 int unset __used)
1954{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001955 char *tok;
1956 char *endptr;
1957
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001958 callchain = 1;
1959
1960 if (!arg)
1961 return 0;
1962
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001963 tok = strtok((char *)arg, ",");
1964 if (!tok)
1965 return -1;
1966
1967 /* get the output mode */
1968 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001969 callchain_param.mode = CHAIN_GRAPH_ABS;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001970
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001971 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001972 callchain_param.mode = CHAIN_FLAT;
1973
1974 else if (!strncmp(tok, "fractal", strlen(arg)))
1975 callchain_param.mode = CHAIN_GRAPH_REL;
1976
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001977 else
1978 return -1;
1979
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001980 /* get the min percentage */
1981 tok = strtok(NULL, ",");
1982 if (!tok)
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001983 goto setup;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001984
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001985 callchain_param.min_percent = strtod(tok, &endptr);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001986 if (tok == endptr)
1987 return -1;
1988
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001989setup:
1990 if (register_callchain_param(&callchain_param) < 0) {
1991 fprintf(stderr, "Can't register callchain params\n");
1992 return -1;
1993 }
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001994 return 0;
1995}
1996
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001997static const char * const report_usage[] = {
1998 "perf report [<options>] <command>",
1999 NULL
2000};
2001
2002static const struct option options[] = {
2003 OPT_STRING('i', "input", &input_name, "file",
2004 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03002005 OPT_BOOLEAN('v', "verbose", &verbose,
2006 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02002007 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2008 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02002009 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +02002010 OPT_BOOLEAN('m', "modules", &modules,
2011 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03002012 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
2013 "Show a column with the number of samples"),
Ingo Molnar63299f02009-05-28 10:52:00 +02002014 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02002015 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03002016 OPT_BOOLEAN('P', "full-paths", &full_paths,
2017 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02002018 OPT_STRING('p', "parent", &parent_pattern, "regex",
2019 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02002020 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
2021 "Only display entries with parent-match"),
Anton Blanchard1483b19f2009-07-16 15:44:29 +02002022 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02002023 "Display callchains using output_type and min percent threshold. "
Anton Blanchard1483b19f2009-07-16 15:44:29 +02002024 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03002025 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
2026 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03002027 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
2028 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03002029 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
2030 "only consider these symbols"),
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03002031 OPT_STRING('w', "column-widths", &col_width_list_str,
2032 "width[,width...]",
2033 "don't try to adjust column width, use these fixed values"),
2034 OPT_STRING('t', "field-separator", &field_sep, "separator",
2035 "separator for columns, no spaces will be added between "
2036 "columns '.' is reserved."),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02002037 OPT_END()
2038};
2039
Ingo Molnar5352f352009-06-03 10:07:39 +02002040static void setup_sorting(void)
2041{
2042 char *tmp, *tok, *str = strdup(sort_order);
2043
2044 for (tok = strtok_r(str, ", ", &tmp);
2045 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2046 if (sort_dimension__add(tok) < 0) {
2047 error("Unknown --sort key: `%s'", tok);
2048 usage_with_options(report_usage, options);
2049 }
2050 }
2051
2052 free(str);
2053}
2054
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03002055static void setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002056 struct sort_entry *se, const char *list_name,
2057 FILE *fp)
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03002058{
2059 if (list_str) {
2060 *list = strlist__new(true, list_str);
2061 if (!*list) {
2062 fprintf(stderr, "problems parsing %s list\n",
2063 list_name);
2064 exit(129);
2065 }
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002066 if (strlist__nr_entries(*list) == 1) {
2067 fprintf(fp, "# %s: %s\n", list_name,
2068 strlist__entry(*list, 0)->s);
2069 se->elide = true;
2070 }
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03002071 }
2072}
2073
Ingo Molnarf37a2912009-07-01 12:37:06 +02002074int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02002075{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03002076 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02002077
2078 page_size = getpagesize();
2079
Ingo Molnaredc52de2009-06-04 16:24:37 +02002080 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02002081
Peter Zijlstra1aa16732009-05-27 20:20:25 +02002082 setup_sorting();
2083
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002084 if (parent_pattern != default_parent_pattern) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +02002085 sort_dimension__add("parent");
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002086 sort_parent.elide = 1;
2087 } else
Ingo Molnarb8e6d822009-06-18 14:32:19 +02002088 exclude_other = 0;
2089
Ingo Molnaredc52de2009-06-04 16:24:37 +02002090 /*
2091 * Any (unrecognized) arguments left?
2092 */
2093 if (argc)
2094 usage_with_options(report_usage, options);
2095
Ingo Molnara930d2c2009-05-27 09:50:13 +02002096 setup_pager();
2097
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03002098 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
2099 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
2100 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03002101
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03002102 if (field_sep && *field_sep == '.') {
2103 fputs("'.' is the only non valid --field-separator argument\n",
2104 stderr);
2105 exit(129);
2106 }
2107
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03002108 return __cmd_report();
2109}