blob: e8c98179fe45726d444571c3748be590ba6f4711 [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 Melo7bec7a92009-06-30 19:01:22 -030036static char *dso_list_str, *comm_list_str, *sym_list_str;
37static struct strlist *dso_list, *comm_list, *sym_list;
Ingo Molnarbd741372009-06-04 14:13:04 +020038
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030039static int input;
40static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
41
Ingo Molnar97b07b62009-05-26 18:48:58 +020042static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020043#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020044#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
Ingo Molnar35029732009-06-03 09:38:58 +020045
Ingo Molnar16f762a2009-05-27 09:10:38 +020046static int verbose;
Ingo Molnar75220602009-06-18 08:00:17 +020047#define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
48
Mike Galbraith42976482009-07-02 08:09:46 +020049static int modules;
50
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030051static int full_paths;
Ingo Molnar97b07b62009-05-26 18:48:58 +020052
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030053static unsigned long page_size;
54static unsigned long mmap_window = 32;
55
Ingo Molnarb8e6d822009-06-18 14:32:19 +020056static char default_parent_pattern[] = "^sys_|^do_page_fault";
57static char *parent_pattern = default_parent_pattern;
Ingo Molnarb25bcf22009-06-18 07:01:03 +020058static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020059
Ingo Molnarb8e6d822009-06-18 14:32:19 +020060static int exclude_other = 1;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020061static int callchain;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020062static enum chain_mode callchain_mode;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +020063static double callchain_min_percent = 0.0;
Ingo Molnarb8e6d822009-06-18 14:32:19 +020064
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020065static u64 sample_type;
66
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030067struct ip_event {
68 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100069 u64 ip;
70 u32 pid, tid;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020071 unsigned char __more_data[];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030072};
Ingo Molnar75051722009-06-03 23:14:49 +020073
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030074struct mmap_event {
75 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100076 u32 pid, tid;
77 u64 start;
78 u64 len;
79 u64 pgoff;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030080 char filename[PATH_MAX];
81};
Ingo Molnar75051722009-06-03 23:14:49 +020082
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030083struct comm_event {
84 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100085 u32 pid, tid;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030086 char comm[16];
87};
88
Peter Zijlstra62fc4452009-06-04 16:53:49 +020089struct fork_event {
90 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100091 u32 pid, ppid;
Peter Zijlstra62fc4452009-06-04 16:53:49 +020092};
93
Ingo Molnarb2fef072009-06-05 18:07:51 +020094struct period_event {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030095 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100096 u64 time;
97 u64 id;
98 u64 sample_period;
Ingo Molnarb2fef072009-06-05 18:07:51 +020099};
100
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200101struct lost_event {
102 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000103 u64 id;
104 u64 lost;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200105};
106
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200107struct read_event {
108 struct perf_event_header header;
109 u32 pid,tid;
110 u64 value;
111 u64 format[3];
112};
113
Ingo Molnarb2fef072009-06-05 18:07:51 +0200114typedef union event_union {
115 struct perf_event_header header;
116 struct ip_event ip;
117 struct mmap_event mmap;
118 struct comm_event comm;
119 struct fork_event fork;
120 struct period_event period;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200121 struct lost_event lost;
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200122 struct read_event read;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300123} event_t;
124
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300125static LIST_HEAD(dsos);
126static struct dso *kernel_dso;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200127static struct dso *vdso;
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000128static struct dso *hypervisor_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300129
130static void dsos__add(struct dso *dso)
131{
132 list_add_tail(&dso->node, &dsos);
133}
134
135static struct dso *dsos__find(const char *name)
136{
137 struct dso *pos;
138
139 list_for_each_entry(pos, &dsos, node)
140 if (strcmp(pos->name, name) == 0)
141 return pos;
142 return NULL;
143}
144
145static struct dso *dsos__findnew(const char *name)
146{
147 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200148 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300149
Ingo Molnar4593bba2009-06-02 15:34:25 +0200150 if (dso)
151 return dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300152
Ingo Molnar4593bba2009-06-02 15:34:25 +0200153 dso = dso__new(name, 0);
154 if (!dso)
155 goto out_delete_dso;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200156
Ingo Molnarbd741372009-06-04 14:13:04 +0200157 nr = dso__load(dso, NULL, verbose);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200158 if (nr < 0) {
Ingo Molnar75220602009-06-18 08:00:17 +0200159 eprintf("Failed to open: %s\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200160 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300161 }
Ingo Molnar75220602009-06-18 08:00:17 +0200162 if (!nr)
163 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
Ingo Molnar4593bba2009-06-02 15:34:25 +0200164
165 dsos__add(dso);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300166
167 return dso;
168
169out_delete_dso:
170 dso__delete(dso);
171 return NULL;
172}
173
Ingo Molnar16f762a2009-05-27 09:10:38 +0200174static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300175{
176 struct dso *pos;
177
178 list_for_each_entry(pos, &dsos, node)
179 dso__fprintf(pos, fp);
180}
181
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000182static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200183{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200184 return dso__find_symbol(dso, ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200185}
186
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200187static int load_kernel(void)
188{
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300189 int err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200190
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300191 kernel_dso = dso__new("[kernel]", 0);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200192 if (!kernel_dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300193 return -1;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200194
Mike Galbraith42976482009-07-02 08:09:46 +0200195 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
Mike Galbraith9974f492009-07-02 08:05:58 +0200196 if (err <= 0) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300197 dso__delete(kernel_dso);
198 kernel_dso = NULL;
199 } else
200 dsos__add(kernel_dso);
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200201
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200202 vdso = dso__new("[vdso]", 0);
203 if (!vdso)
204 return -1;
205
206 vdso->find_symbol = vdso__find_symbol;
207
208 dsos__add(vdso);
209
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000210 hypervisor_dso = dso__new("[hypervisor]", 0);
211 if (!hypervisor_dso)
212 return -1;
213 dsos__add(hypervisor_dso);
214
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300215 return err;
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200216}
217
Ingo Molnard80d3382009-06-03 23:14:49 +0200218static char __cwd[PATH_MAX];
219static char *cwd = __cwd;
220static int cwdlen;
221
222static int strcommon(const char *pathname)
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300223{
224 int n = 0;
225
226 while (pathname[n] == cwd[n] && n < cwdlen)
227 ++n;
228
229 return n;
230}
231
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300232struct map {
233 struct list_head node;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000234 u64 start;
235 u64 end;
236 u64 pgoff;
237 u64 (*map_ip)(struct map *, u64);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300238 struct dso *dso;
239};
240
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000241static u64 map__map_ip(struct map *map, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200242{
243 return ip - map->start + map->pgoff;
244}
245
Ingo Molnarf37a2912009-07-01 12:37:06 +0200246static u64 vdso__map_ip(struct map *map __used, u64 ip)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200247{
248 return ip;
249}
250
Pekka Enberg80d496b2009-06-08 21:12:48 +0300251static inline int is_anon_memory(const char *filename)
252{
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300253 return strcmp(filename, "//anon") == 0;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300254}
255
Ingo Molnard80d3382009-06-03 23:14:49 +0200256static struct map *map__new(struct mmap_event *event)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300257{
258 struct map *self = malloc(sizeof(*self));
259
260 if (self != NULL) {
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300261 const char *filename = event->filename;
262 char newfilename[PATH_MAX];
Pekka Enberg80d496b2009-06-08 21:12:48 +0300263 int anon;
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300264
265 if (cwd) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200266 int n = strcommon(filename);
267
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300268 if (n == cwdlen) {
269 snprintf(newfilename, sizeof(newfilename),
270 ".%s", filename + n);
271 filename = newfilename;
272 }
273 }
274
Pekka Enberg80d496b2009-06-08 21:12:48 +0300275 anon = is_anon_memory(filename);
276
277 if (anon) {
278 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
279 filename = newfilename;
280 }
281
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300282 self->start = event->start;
283 self->end = event->start + event->len;
284 self->pgoff = event->pgoff;
285
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300286 self->dso = dsos__findnew(filename);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300287 if (self->dso == NULL)
288 goto out_delete;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200289
Pekka Enberg80d496b2009-06-08 21:12:48 +0300290 if (self->dso == vdso || anon)
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200291 self->map_ip = vdso__map_ip;
292 else
293 self->map_ip = map__map_ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300294 }
295 return self;
296out_delete:
297 free(self);
298 return NULL;
299}
300
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200301static struct map *map__clone(struct map *self)
302{
303 struct map *map = malloc(sizeof(*self));
304
305 if (!map)
306 return NULL;
307
308 memcpy(map, self, sizeof(*self));
309
310 return map;
311}
312
313static int map__overlap(struct map *l, struct map *r)
314{
315 if (l->start > r->start) {
316 struct map *t = l;
317 l = r;
318 r = t;
319 }
320
321 if (l->end > r->start)
322 return 1;
323
324 return 0;
325}
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300326
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300327static size_t map__fprintf(struct map *self, FILE *fp)
328{
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200329 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300330 self->start, self->end, self->pgoff, self->dso->name);
331}
332
333
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300334struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300335 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300336 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300337 pid_t pid;
338 char *comm;
339};
340
341static struct thread *thread__new(pid_t pid)
342{
343 struct thread *self = malloc(sizeof(*self));
344
345 if (self != NULL) {
346 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200347 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200348 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200349 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300350 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300351 }
352
353 return self;
354}
355
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300356static int thread__set_comm(struct thread *self, const char *comm)
357{
Peter Zijlstra82292892009-06-03 12:37:36 +0200358 if (self->comm)
359 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300360 self->comm = strdup(comm);
361 return self->comm ? 0 : -ENOMEM;
362}
363
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300364static size_t thread__fprintf(struct thread *self, FILE *fp)
365{
366 struct map *pos;
367 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
368
369 list_for_each_entry(pos, &self->maps, node)
370 ret += map__fprintf(pos, fp);
371
372 return ret;
373}
374
375
Ingo Molnar16f762a2009-05-27 09:10:38 +0200376static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200377static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300378
379static struct thread *threads__findnew(pid_t pid)
380{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300381 struct rb_node **p = &threads.rb_node;
382 struct rb_node *parent = NULL;
383 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300384
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200385 /*
386 * Font-end cache - PID lookups come in blocks,
387 * so most of the time we dont have to look up
388 * the full rbtree:
389 */
390 if (last_match && last_match->pid == pid)
391 return last_match;
392
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300393 while (*p != NULL) {
394 parent = *p;
395 th = rb_entry(parent, struct thread, rb_node);
396
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200397 if (th->pid == pid) {
398 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300399 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200400 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300401
402 if (pid < th->pid)
403 p = &(*p)->rb_left;
404 else
405 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300406 }
407
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300408 th = thread__new(pid);
409 if (th != NULL) {
410 rb_link_node(&th->rb_node, parent, p);
411 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200412 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300413 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200414
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300415 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300416}
417
418static void thread__insert_map(struct thread *self, struct map *map)
419{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200420 struct map *pos, *tmp;
421
422 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
423 if (map__overlap(pos, map)) {
Peter Zijlstra3d906ef2009-06-23 11:23:07 +0200424 if (verbose >= 2) {
425 printf("overlapping maps:\n");
426 map__fprintf(map, stdout);
427 map__fprintf(pos, stdout);
428 }
429
430 if (map->start <= pos->start && map->end > pos->start)
431 pos->start = map->end;
432
433 if (map->end >= pos->end && map->start < pos->end)
434 pos->end = map->start;
435
436 if (verbose >= 2) {
437 printf("after collision:\n");
438 map__fprintf(pos, stdout);
439 }
440
441 if (pos->start >= pos->end) {
442 list_del_init(&pos->node);
443 free(pos);
444 }
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200445 }
446 }
447
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300448 list_add_tail(&map->node, &self->maps);
449}
450
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200451static int thread__fork(struct thread *self, struct thread *parent)
452{
453 struct map *map;
454
455 if (self->comm)
456 free(self->comm);
457 self->comm = strdup(parent->comm);
458 if (!self->comm)
459 return -ENOMEM;
460
461 list_for_each_entry(map, &parent->maps, node) {
462 struct map *new = map__clone(map);
463 if (!new)
464 return -ENOMEM;
465 thread__insert_map(self, new);
466 }
467
468 return 0;
469}
470
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000471static struct map *thread__find_map(struct thread *self, u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300472{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200473 struct map *pos;
474
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300475 if (self == NULL)
476 return NULL;
477
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300478 list_for_each_entry(pos, &self->maps, node)
479 if (ip >= pos->start && ip <= pos->end)
480 return pos;
481
482 return NULL;
483}
484
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300485static size_t threads__fprintf(FILE *fp)
486{
487 size_t ret = 0;
488 struct rb_node *nd;
489
490 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
491 struct thread *pos = rb_entry(nd, struct thread, rb_node);
492
493 ret += thread__fprintf(pos, fp);
494 }
495
496 return ret;
497}
498
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200499/*
500 * histogram, sorted on item, collects counts
501 */
502
503static struct rb_root hist;
504
505struct hist_entry {
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200506 struct rb_node rb_node;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200507
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200508 struct thread *thread;
509 struct map *map;
510 struct dso *dso;
511 struct symbol *sym;
512 struct symbol *parent;
513 u64 ip;
514 char level;
515 struct callchain_node callchain;
516 struct rb_root sorted_chain;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200517
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200518 u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200519};
520
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200521/*
522 * configurable sorting bits
523 */
524
525struct sort_entry {
526 struct list_head list;
527
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200528 char *header;
529
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200530 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200531 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200532 size_t (*print)(FILE *fp, struct hist_entry *);
533};
534
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200535static int64_t cmp_null(void *l, void *r)
536{
537 if (!l && !r)
538 return 0;
539 else if (!l)
540 return -1;
541 else
542 return 1;
543}
544
Peter Zijlstra82292892009-06-03 12:37:36 +0200545/* --sort pid */
546
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200547static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200548sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
549{
550 return right->thread->pid - left->thread->pid;
551}
552
553static size_t
554sort__thread_print(FILE *fp, struct hist_entry *self)
555{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200556 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200557}
558
559static struct sort_entry sort_thread = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200560 .header = " Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200561 .cmp = sort__thread_cmp,
562 .print = sort__thread_print,
563};
564
Peter Zijlstra82292892009-06-03 12:37:36 +0200565/* --sort comm */
566
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200567static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200568sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
569{
Peter Zijlstra82292892009-06-03 12:37:36 +0200570 return right->thread->pid - left->thread->pid;
571}
572
573static int64_t
574sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
575{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200576 char *comm_l = left->thread->comm;
577 char *comm_r = right->thread->comm;
578
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200579 if (!comm_l || !comm_r)
580 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200581
582 return strcmp(comm_l, comm_r);
583}
584
585static size_t
586sort__comm_print(FILE *fp, struct hist_entry *self)
587{
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200588 return fprintf(fp, "%16s", self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200589}
590
591static struct sort_entry sort_comm = {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200592 .header = " Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200593 .cmp = sort__comm_cmp,
594 .collapse = sort__comm_collapse,
595 .print = sort__comm_print,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200596};
597
Peter Zijlstra82292892009-06-03 12:37:36 +0200598/* --sort dso */
599
Peter Zijlstra992444b2009-05-27 20:20:27 +0200600static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200601sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
602{
603 struct dso *dso_l = left->dso;
604 struct dso *dso_r = right->dso;
605
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200606 if (!dso_l || !dso_r)
607 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200608
609 return strcmp(dso_l->name, dso_r->name);
610}
611
612static size_t
613sort__dso_print(FILE *fp, struct hist_entry *self)
614{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200615 if (self->dso)
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200616 return fprintf(fp, "%-25s", self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200617
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000618 return fprintf(fp, "%016llx ", (u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200619}
620
621static struct sort_entry sort_dso = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200622 .header = "Shared Object ",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200623 .cmp = sort__dso_cmp,
624 .print = sort__dso_print,
625};
626
Peter Zijlstra82292892009-06-03 12:37:36 +0200627/* --sort symbol */
628
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200629static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200630sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300631{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000632 u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200633
634 if (left->sym == right->sym)
635 return 0;
636
637 ip_l = left->sym ? left->sym->start : left->ip;
638 ip_r = right->sym ? right->sym->start : right->ip;
639
640 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300641}
642
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200643static size_t
644sort__sym_print(FILE *fp, struct hist_entry *self)
645{
646 size_t ret = 0;
647
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200648 if (verbose)
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000649 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200650
Ingo Molnar8edd4282009-06-05 14:13:18 +0200651 if (self->sym) {
652 ret += fprintf(fp, "[%c] %s",
Anton Blanchardfb9c8182009-07-01 09:00:49 +1000653 self->dso == kernel_dso ? 'k' :
654 self->dso == hypervisor_dso ? 'h' : '.', self->sym->name);
Mike Galbraith42976482009-07-02 08:09:46 +0200655
656 if (self->sym->module)
657 ret += fprintf(fp, "\t[%s]", self->sym->module->name);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200658 } else {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000659 ret += fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200660 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200661
662 return ret;
663}
664
665static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200666 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200667 .cmp = sort__sym_cmp,
668 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200669};
670
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200671/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200672
673static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200674sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200675{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200676 struct symbol *sym_l = left->parent;
677 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200678
679 if (!sym_l || !sym_r)
680 return cmp_null(sym_l, sym_r);
681
682 return strcmp(sym_l->name, sym_r->name);
683}
684
685static size_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200686sort__parent_print(FILE *fp, struct hist_entry *self)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200687{
688 size_t ret = 0;
689
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200690 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200691
692 return ret;
693}
694
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200695static struct sort_entry sort_parent = {
696 .header = "Parent symbol ",
697 .cmp = sort__parent_cmp,
698 .print = sort__parent_print,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200699};
700
Peter Zijlstra82292892009-06-03 12:37:36 +0200701static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200702static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200703
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200704struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200705 char *name;
706 struct sort_entry *entry;
707 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200708};
709
710static struct sort_dimension sort_dimensions[] = {
711 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200712 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200713 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200714 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200715 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200716};
717
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200718static LIST_HEAD(hist_entry__sort_list);
719
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200720static int sort_dimension__add(char *tok)
721{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200722 unsigned int i;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200723
724 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
725 struct sort_dimension *sd = &sort_dimensions[i];
726
727 if (sd->taken)
728 continue;
729
Ingo Molnar5352f352009-06-03 10:07:39 +0200730 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200731 continue;
732
Peter Zijlstra82292892009-06-03 12:37:36 +0200733 if (sd->entry->collapse)
734 sort__need_collapse = 1;
735
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200736 if (sd->entry == &sort_parent) {
737 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200738 if (ret) {
739 char err[BUFSIZ];
740
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200741 regerror(ret, &parent_regex, err, sizeof(err));
742 fprintf(stderr, "Invalid regex: %s\n%s",
743 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200744 exit(-1);
745 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200746 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200747 }
748
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200749 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
750 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200751
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200752 return 0;
753 }
754
755 return -ESRCH;
756}
757
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200758static int64_t
759hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
760{
761 struct sort_entry *se;
762 int64_t cmp = 0;
763
764 list_for_each_entry(se, &hist_entry__sort_list, list) {
765 cmp = se->cmp(left, right);
766 if (cmp)
767 break;
768 }
769
770 return cmp;
771}
772
Peter Zijlstra82292892009-06-03 12:37:36 +0200773static int64_t
774hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
775{
776 struct sort_entry *se;
777 int64_t cmp = 0;
778
779 list_for_each_entry(se, &hist_entry__sort_list, list) {
780 int64_t (*f)(struct hist_entry *, struct hist_entry *);
781
782 f = se->collapse ?: se->cmp;
783
784 cmp = f(left, right);
785 if (cmp)
786 break;
787 }
788
789 return cmp;
790}
791
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200792static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
793{
794 int i;
795 size_t ret = 0;
796
797 ret += fprintf(fp, "%s", " ");
798
799 for (i = 0; i < depth; i++)
800 if (depth_mask & (1 << i))
801 ret += fprintf(fp, "| ");
802 else
803 ret += fprintf(fp, " ");
804
805 ret += fprintf(fp, "\n");
806
807 return ret;
808}
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200809static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200810ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
811 int depth_mask, int count, u64 total_samples,
812 int hits)
813{
814 int i;
815 size_t ret = 0;
816
817 ret += fprintf(fp, "%s", " ");
818 for (i = 0; i < depth; i++) {
819 if (depth_mask & (1 << i))
820 ret += fprintf(fp, "|");
821 else
822 ret += fprintf(fp, " ");
823 if (!count && i == depth - 1) {
824 double percent;
825
826 percent = hits * 100.0 / total_samples;
827 ret += fprintf(fp, "--%2.2f%%-- ", percent);
828 } else
829 ret += fprintf(fp, "%s", " ");
830 }
831 if (chain->sym)
832 ret += fprintf(fp, "%s\n", chain->sym->name);
833 else
834 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
835
836 return ret;
837}
838
839static size_t
840callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
841 u64 total_samples, int depth, int depth_mask)
842{
843 struct rb_node *node, *next;
844 struct callchain_node *child;
845 struct callchain_list *chain;
846 int new_depth_mask = depth_mask;
847 size_t ret = 0;
848 int i;
849
850 node = rb_first(&self->rb_root);
851 while (node) {
852 child = rb_entry(node, struct callchain_node, rb_node);
853
854 /*
855 * The depth mask manages the output of pipes that show
856 * the depth. We don't want to keep the pipes of the current
857 * level for the last child of this depth
858 */
859 next = rb_next(node);
860 if (!next)
861 new_depth_mask &= ~(1 << (depth - 1));
862
863 /*
864 * But we keep the older depth mask for the line seperator
865 * to keep the level link until we reach the last child
866 */
867 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
868 i = 0;
869 list_for_each_entry(chain, &child->val, list) {
870 if (chain->ip >= PERF_CONTEXT_MAX)
871 continue;
872 ret += ipchain__fprintf_graph(fp, chain, depth,
873 new_depth_mask, i++,
874 total_samples,
875 child->cumul_hit);
876 }
877 ret += callchain__fprintf_graph(fp, child, total_samples,
878 depth + 1,
879 new_depth_mask | (1 << depth));
880 node = next;
881 }
882
883 return ret;
884}
885
886static size_t
887callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
888 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200889{
890 struct callchain_list *chain;
891 size_t ret = 0;
892
893 if (!self)
894 return 0;
895
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200896 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200897
898
Frederic Weisbecker44249612009-07-01 05:35:14 +0200899 list_for_each_entry(chain, &self->val, list) {
900 if (chain->ip >= PERF_CONTEXT_MAX)
901 continue;
902 if (chain->sym)
903 ret += fprintf(fp, " %s\n", chain->sym->name);
904 else
905 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200906 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200907 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200908
909 return ret;
910}
911
912static size_t
913hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
914 u64 total_samples)
915{
916 struct rb_node *rb_node;
917 struct callchain_node *chain;
918 size_t ret = 0;
919
920 rb_node = rb_first(&self->sorted_chain);
921 while (rb_node) {
922 double percent;
923
924 chain = rb_entry(rb_node, struct callchain_node, rb_node);
925 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200926 if (callchain_mode == FLAT) {
927 ret += fprintf(fp, " %6.2f%%\n", percent);
928 ret += callchain__fprintf_flat(fp, chain, total_samples);
929 } else if (callchain_mode == GRAPH) {
930 ret += callchain__fprintf_graph(fp, chain,
931 total_samples, 1, 1);
932 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200933 ret += fprintf(fp, "\n");
934 rb_node = rb_next(rb_node);
935 }
936
937 return ret;
938}
939
940
941static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000942hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200943{
944 struct sort_entry *se;
945 size_t ret;
946
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200947 if (exclude_other && !self->parent)
948 return 0;
949
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200950 if (total_samples) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200951 double percent = self->count * 100.0 / total_samples;
952 char *color = PERF_COLOR_NORMAL;
953
954 /*
Ingo Molnaraefcf372009-06-08 23:15:28 +0200955 * We color high-overhead entries in red, mid-overhead
956 * entries in green - and keep the low overhead places
957 * normal:
Ingo Molnar8fc03212009-06-04 15:19:47 +0200958 */
Ingo Molnaraefcf372009-06-08 23:15:28 +0200959 if (percent >= 5.0) {
Ingo Molnar8fc03212009-06-04 15:19:47 +0200960 color = PERF_COLOR_RED;
Ingo Molnaraefcf372009-06-08 23:15:28 +0200961 } else {
962 if (percent >= 0.5)
963 color = PERF_COLOR_GREEN;
964 }
Ingo Molnar8fc03212009-06-04 15:19:47 +0200965
966 ret = color_fprintf(fp, color, " %6.2f%%",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200967 (self->count * 100.0) / total_samples);
968 } else
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200969 ret = fprintf(fp, "%12Ld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200970
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200971 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200972 if (exclude_other && (se == &sort_parent))
973 continue;
974
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200975 fprintf(fp, " ");
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200976 ret += se->print(fp, self);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200977 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200978
979 ret += fprintf(fp, "\n");
980
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200981 if (callchain)
982 hist_entry_callchain__fprintf(fp, self, total_samples);
983
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200984 return ret;
985}
986
987/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200988 *
989 */
990
991static struct symbol *
992resolve_symbol(struct thread *thread, struct map **mapp,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000993 struct dso **dsop, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200994{
995 struct dso *dso = dsop ? *dsop : NULL;
996 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200997 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200998
999 if (!thread)
1000 return NULL;
1001
1002 if (dso)
1003 goto got_dso;
1004
1005 if (map)
1006 goto got_map;
1007
1008 map = thread__find_map(thread, ip);
1009 if (map != NULL) {
1010 if (mapp)
1011 *mapp = map;
1012got_map:
1013 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001014
1015 dso = map->dso;
1016 } else {
1017 /*
1018 * If this is outside of all known maps,
1019 * and is a negative address, try to look it
1020 * up in the kernel dso, as it might be a
1021 * vsyscall (which executes in user-mode):
1022 */
1023 if ((long long)ip < 0)
1024 dso = kernel_dso;
1025 }
1026 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Peter Zijlstra520f2c32009-06-22 16:52:51 +02001027 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1028 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001029
1030 if (dsop)
1031 *dsop = dso;
1032
1033 if (!dso)
1034 return NULL;
1035got_dso:
1036 return dso->find_symbol(dso, ip);
1037}
1038
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001039static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001040{
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001041 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001042 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001043
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001044 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001045}
1046
Frederic Weisbecker44249612009-07-01 05:35:14 +02001047static struct symbol **
Ingo Molnarf37a2912009-07-01 12:37:06 +02001048resolve_callchain(struct thread *thread, struct map *map __used,
Frederic Weisbecker44249612009-07-01 05:35:14 +02001049 struct ip_callchain *chain, struct hist_entry *entry)
1050{
Frederic Weisbecker44249612009-07-01 05:35:14 +02001051 u64 context = PERF_CONTEXT_MAX;
Ingo Molnarf37a2912009-07-01 12:37:06 +02001052 struct symbol **syms;
1053 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001054
1055 if (callchain) {
1056 syms = calloc(chain->nr, sizeof(*syms));
1057 if (!syms) {
1058 fprintf(stderr, "Can't allocate memory for symbols\n");
1059 exit(-1);
1060 }
1061 }
1062
1063 for (i = 0; i < chain->nr; i++) {
1064 u64 ip = chain->ips[i];
1065 struct dso *dso = NULL;
1066 struct symbol *sym;
1067
1068 if (ip >= PERF_CONTEXT_MAX) {
1069 context = ip;
1070 continue;
1071 }
1072
1073 switch (context) {
Ingo Molnar88a69df2009-07-01 11:17:20 +02001074 case PERF_CONTEXT_HV:
1075 dso = hypervisor_dso;
1076 break;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001077 case PERF_CONTEXT_KERNEL:
1078 dso = kernel_dso;
1079 break;
1080 default:
1081 break;
1082 }
1083
1084 sym = resolve_symbol(thread, NULL, &dso, &ip);
1085
1086 if (sym) {
1087 if (sort__has_parent && call__match(sym) &&
1088 !entry->parent)
1089 entry->parent = sym;
1090 if (!callchain)
1091 break;
1092 syms[i] = sym;
1093 }
1094 }
1095
1096 return syms;
1097}
1098
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001099/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001100 * collect histogram counts
1101 */
1102
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001103static int
1104hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001105 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1106 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001107{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001108 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001109 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001110 struct hist_entry *he;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001111 struct symbol **syms = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001112 struct hist_entry entry = {
1113 .thread = thread,
1114 .map = map,
1115 .dso = dso,
1116 .sym = sym,
1117 .ip = ip,
1118 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001119 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001120 .parent = NULL,
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001121 .sorted_chain = RB_ROOT
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001122 };
1123 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001124
Frederic Weisbecker44249612009-07-01 05:35:14 +02001125 if ((sort__has_parent || callchain) && chain)
1126 syms = resolve_callchain(thread, map, chain, &entry);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001127
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001128 while (*p != NULL) {
1129 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001130 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001131
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001132 cmp = hist_entry__cmp(&entry, he);
1133
1134 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001135 he->count += count;
Frederic Weisbecker44249612009-07-01 05:35:14 +02001136 if (callchain) {
1137 append_chain(&he->callchain, chain, syms);
1138 free(syms);
1139 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001140 return 0;
1141 }
1142
1143 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001144 p = &(*p)->rb_left;
1145 else
1146 p = &(*p)->rb_right;
1147 }
1148
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001149 he = malloc(sizeof(*he));
1150 if (!he)
1151 return -ENOMEM;
1152 *he = entry;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001153 if (callchain) {
1154 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +02001155 append_chain(&he->callchain, chain, syms);
1156 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001157 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001158 rb_link_node(&he->rb_node, parent, p);
1159 rb_insert_color(&he->rb_node, &hist);
1160
1161 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001162}
1163
Peter Zijlstra82292892009-06-03 12:37:36 +02001164static void hist_entry__free(struct hist_entry *he)
1165{
1166 free(he);
1167}
1168
1169/*
1170 * collapse the histogram
1171 */
1172
1173static struct rb_root collapse_hists;
1174
1175static void collapse__insert_entry(struct hist_entry *he)
1176{
1177 struct rb_node **p = &collapse_hists.rb_node;
1178 struct rb_node *parent = NULL;
1179 struct hist_entry *iter;
1180 int64_t cmp;
1181
1182 while (*p != NULL) {
1183 parent = *p;
1184 iter = rb_entry(parent, struct hist_entry, rb_node);
1185
1186 cmp = hist_entry__collapse(iter, he);
1187
1188 if (!cmp) {
1189 iter->count += he->count;
1190 hist_entry__free(he);
1191 return;
1192 }
1193
1194 if (cmp < 0)
1195 p = &(*p)->rb_left;
1196 else
1197 p = &(*p)->rb_right;
1198 }
1199
1200 rb_link_node(&he->rb_node, parent, p);
1201 rb_insert_color(&he->rb_node, &collapse_hists);
1202}
1203
1204static void collapse__resort(void)
1205{
1206 struct rb_node *next;
1207 struct hist_entry *n;
1208
1209 if (!sort__need_collapse)
1210 return;
1211
1212 next = rb_first(&hist);
1213 while (next) {
1214 n = rb_entry(next, struct hist_entry, rb_node);
1215 next = rb_next(&n->rb_node);
1216
1217 rb_erase(&n->rb_node, &hist);
1218 collapse__insert_entry(n);
1219 }
1220}
1221
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001222/*
1223 * reverse the map, sort on count.
1224 */
1225
1226static struct rb_root output_hists;
1227
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001228static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001229{
1230 struct rb_node **p = &output_hists.rb_node;
1231 struct rb_node *parent = NULL;
1232 struct hist_entry *iter;
1233
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001234 if (callchain) {
1235 if (callchain_mode == FLAT)
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001236 sort_chain_flat(&he->sorted_chain, &he->callchain,
1237 min_callchain_hits);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001238 else if (callchain_mode == GRAPH)
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001239 sort_chain_graph(&he->sorted_chain, &he->callchain,
1240 min_callchain_hits);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001241 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001242
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001243 while (*p != NULL) {
1244 parent = *p;
1245 iter = rb_entry(parent, struct hist_entry, rb_node);
1246
1247 if (he->count > iter->count)
1248 p = &(*p)->rb_left;
1249 else
1250 p = &(*p)->rb_right;
1251 }
1252
1253 rb_link_node(&he->rb_node, parent, p);
1254 rb_insert_color(&he->rb_node, &output_hists);
1255}
1256
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001257static void output__resort(u64 total_samples)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001258{
Peter Zijlstra82292892009-06-03 12:37:36 +02001259 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001260 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001261 struct rb_root *tree = &hist;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001262 u64 min_callchain_hits;
1263
1264 min_callchain_hits = total_samples * (callchain_min_percent / 100);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001265
Peter Zijlstra82292892009-06-03 12:37:36 +02001266 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001267 tree = &collapse_hists;
1268
1269 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001270
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001271 while (next) {
1272 n = rb_entry(next, struct hist_entry, rb_node);
1273 next = rb_next(&n->rb_node);
1274
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001275 rb_erase(&n->rb_node, tree);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001276 output__insert_entry(n, min_callchain_hits);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001277 }
1278}
1279
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001280static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001281{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001282 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001283 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001284 struct rb_node *nd;
1285 size_t ret = 0;
1286
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001287 fprintf(fp, "\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001288 fprintf(fp, "#\n");
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001289 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001290 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001291
1292 fprintf(fp, "# Overhead");
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001293 list_for_each_entry(se, &hist_entry__sort_list, list) {
1294 if (exclude_other && (se == &sort_parent))
1295 continue;
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001296 fprintf(fp, " %s", se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001297 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001298 fprintf(fp, "\n");
1299
1300 fprintf(fp, "# ........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001301 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001302 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001303
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001304 if (exclude_other && (se == &sort_parent))
1305 continue;
1306
Ingo Molnar4593bba2009-06-02 15:34:25 +02001307 fprintf(fp, " ");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001308 for (i = 0; i < strlen(se->header); i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001309 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001310 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001311 fprintf(fp, "\n");
1312
1313 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001314
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001315 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1316 pos = rb_entry(nd, struct hist_entry, rb_node);
1317 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001318 }
1319
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001320 if (sort_order == default_sort_order &&
1321 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001322 fprintf(fp, "#\n");
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001323 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001324 fprintf(fp, "#\n");
1325 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001326 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001327
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001328 return ret;
1329}
1330
Peter Zijlstra436224a2009-06-02 21:02:36 +02001331static void register_idle_thread(void)
1332{
1333 struct thread *thread = threads__findnew(0);
1334
1335 if (thread == NULL ||
1336 thread__set_comm(thread, "[idle]")) {
1337 fprintf(stderr, "problem inserting idle task.\n");
1338 exit(-1);
1339 }
1340}
1341
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001342static unsigned long total = 0,
1343 total_mmap = 0,
1344 total_comm = 0,
1345 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001346 total_unknown = 0,
1347 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001348
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001349static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +02001350{
1351 unsigned int chain_size;
1352
Ingo Molnar75220602009-06-18 08:00:17 +02001353 chain_size = event->header.size;
1354 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1355
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001356 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +02001357 return -1;
1358
1359 return 0;
1360}
1361
Ingo Molnard80d3382009-06-03 23:14:49 +02001362static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001363process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +02001364{
1365 char level;
1366 int show = 0;
1367 struct dso *dso = NULL;
1368 struct thread *thread = threads__findnew(event->ip.pid);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001369 u64 ip = event->ip.ip;
1370 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001371 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001372 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001373 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001374 int cpumode;
Ingo Molnar75051722009-06-03 23:14:49 +02001375
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001376 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001377 period = *(u64 *)more_data;
1378 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001379 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001380
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001381 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001382 (void *)(offset + head),
1383 (void *)(long)(event->header.size),
1384 event->header.misc,
1385 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001386 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001387 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001388
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001389 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001390 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001391
1392 chain = (void *)more_data;
1393
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001394 dprintf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001395
Ingo Molnar75220602009-06-18 08:00:17 +02001396 if (validate_chain(chain, event) < 0) {
1397 eprintf("call-chain problem with event, skipping it.\n");
1398 return 0;
1399 }
1400
1401 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001402 for (i = 0; i < chain->nr; i++)
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001403 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001404 }
1405 }
1406
Ingo Molnar75051722009-06-03 23:14:49 +02001407 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1408
1409 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001410 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001411 event->header.type);
1412 return -1;
1413 }
1414
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001415 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1416 return 0;
1417
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001418 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1419
1420 if (cpumode == PERF_EVENT_MISC_KERNEL) {
Ingo Molnar75051722009-06-03 23:14:49 +02001421 show = SHOW_KERNEL;
1422 level = 'k';
1423
1424 dso = kernel_dso;
1425
1426 dprintf(" ...... dso: %s\n", dso->name);
1427
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001428 } else if (cpumode == PERF_EVENT_MISC_USER) {
Ingo Molnar75051722009-06-03 23:14:49 +02001429
1430 show = SHOW_USER;
1431 level = '.';
1432
Ingo Molnar75051722009-06-03 23:14:49 +02001433 } else {
1434 show = SHOW_HV;
1435 level = 'H';
Anton Blanchardfb9c8182009-07-01 09:00:49 +10001436
1437 dso = hypervisor_dso;
1438
Ingo Molnar75051722009-06-03 23:14:49 +02001439 dprintf(" ...... dso: [hypervisor]\n");
1440 }
1441
1442 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001443 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001444
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001445 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1446 return 0;
1447
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001448 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1449 return 0;
1450
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001451 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001452 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001453 return -1;
1454 }
1455 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001456 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001457
1458 return 0;
1459}
1460
1461static int
1462process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1463{
1464 struct thread *thread = threads__findnew(event->mmap.pid);
1465 struct map *map = map__new(&event->mmap);
1466
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001467 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001468 (void *)(offset + head),
1469 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001470 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001471 (void *)(long)event->mmap.start,
1472 (void *)(long)event->mmap.len,
1473 (void *)(long)event->mmap.pgoff,
1474 event->mmap.filename);
1475
1476 if (thread == NULL || map == NULL) {
1477 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001478 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001479 }
1480
1481 thread__insert_map(thread, map);
1482 total_mmap++;
1483
1484 return 0;
1485}
1486
1487static int
1488process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1489{
1490 struct thread *thread = threads__findnew(event->comm.pid);
1491
1492 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1493 (void *)(offset + head),
1494 (void *)(long)(event->header.size),
1495 event->comm.comm, event->comm.pid);
1496
1497 if (thread == NULL ||
1498 thread__set_comm(thread, event->comm.comm)) {
1499 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1500 return -1;
1501 }
1502 total_comm++;
1503
1504 return 0;
1505}
1506
1507static int
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001508process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1509{
1510 struct thread *thread = threads__findnew(event->fork.pid);
1511 struct thread *parent = threads__findnew(event->fork.ppid);
1512
1513 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1514 (void *)(offset + head),
1515 (void *)(long)(event->header.size),
1516 event->fork.pid, event->fork.ppid);
1517
1518 if (!thread || !parent || thread__fork(thread, parent)) {
1519 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1520 return -1;
1521 }
1522 total_fork++;
1523
1524 return 0;
1525}
1526
1527static int
Ingo Molnarb2fef072009-06-05 18:07:51 +02001528process_period_event(event_t *event, unsigned long offset, unsigned long head)
1529{
1530 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1531 (void *)(offset + head),
1532 (void *)(long)(event->header.size),
1533 event->period.time,
1534 event->period.id,
1535 event->period.sample_period);
1536
1537 return 0;
1538}
1539
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001540static int
1541process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1542{
1543 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1544 (void *)(offset + head),
1545 (void *)(long)(event->header.size),
1546 event->lost.id,
1547 event->lost.lost);
1548
1549 total_lost += event->lost.lost;
1550
1551 return 0;
1552}
1553
Ingo Molnar8465b052009-06-14 14:44:07 +02001554static void trace_event(event_t *event)
1555{
1556 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001557 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001558 int i, j;
1559
1560 if (!dump_trace)
1561 return;
1562
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001563 dprintf(".");
1564 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001565
1566 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001567 if ((i & 15) == 0) {
1568 dprintf(".");
1569 cdprintf(" %04x: ", i);
1570 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001571
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001572 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001573
1574 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001575 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001576 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001577 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001578 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001579 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001580 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001581 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001582 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001583 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001584 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001585 }
1586 }
1587 dprintf(".\n");
1588}
1589
Ingo Molnarb2fef072009-06-05 18:07:51 +02001590static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001591process_read_event(event_t *event, unsigned long offset, unsigned long head)
1592{
1593 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1594 (void *)(offset + head),
1595 (void *)(long)(event->header.size),
1596 event->read.pid,
1597 event->read.tid,
1598 event->read.value);
1599
1600 return 0;
1601}
1602
1603static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001604process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001605{
Ingo Molnar8465b052009-06-14 14:44:07 +02001606 trace_event(event);
1607
Ingo Molnar75051722009-06-03 23:14:49 +02001608 switch (event->header.type) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001609 case PERF_EVENT_SAMPLE:
1610 return process_sample_event(event, offset, head);
1611
Ingo Molnar75051722009-06-03 23:14:49 +02001612 case PERF_EVENT_MMAP:
1613 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001614
Ingo Molnar75051722009-06-03 23:14:49 +02001615 case PERF_EVENT_COMM:
1616 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001617
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001618 case PERF_EVENT_FORK:
1619 return process_fork_event(event, offset, head);
1620
Ingo Molnarb2fef072009-06-05 18:07:51 +02001621 case PERF_EVENT_PERIOD:
1622 return process_period_event(event, offset, head);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001623
1624 case PERF_EVENT_LOST:
1625 return process_lost_event(event, offset, head);
1626
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001627 case PERF_EVENT_READ:
1628 return process_read_event(event, offset, head);
1629
Ingo Molnard11444d2009-06-03 23:29:14 +02001630 /*
1631 * We dont process them right now but they are fine:
1632 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001633
Ingo Molnard11444d2009-06-03 23:29:14 +02001634 case PERF_EVENT_THROTTLE:
1635 case PERF_EVENT_UNTHROTTLE:
1636 return 0;
1637
Ingo Molnard80d3382009-06-03 23:14:49 +02001638 default:
1639 return -1;
1640 }
1641
1642 return 0;
1643}
1644
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001645static struct perf_header *header;
1646
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001647static u64 perf_header__sample_type(void)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001648{
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001649 u64 sample_type = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001650 int i;
1651
1652 for (i = 0; i < header->attrs; i++) {
1653 struct perf_header_attr *attr = header->attr[i];
1654
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001655 if (!sample_type)
1656 sample_type = attr->attr.sample_type;
1657 else if (sample_type != attr->attr.sample_type)
1658 die("non matching sample_type");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001659 }
1660
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001661 return sample_type;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001662}
Peter Zijlstraf5970552009-06-18 23:22:55 +02001663
Ingo Molnard80d3382009-06-03 23:14:49 +02001664static int __cmd_report(void)
1665{
Ingo Molnar75051722009-06-03 23:14:49 +02001666 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001667 unsigned long offset = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001668 unsigned long head, shift;
Ingo Molnard80d3382009-06-03 23:14:49 +02001669 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001670 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001671 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001672 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001673
1674 register_idle_thread();
1675
1676 input = open(input_name, O_RDONLY);
1677 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001678 fprintf(stderr, " failed to open file: %s", input_name);
1679 if (!strcmp(input_name, "perf.data"))
1680 fprintf(stderr, " (try 'perf record' first)");
1681 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001682 exit(-1);
1683 }
1684
1685 ret = fstat(input, &stat);
1686 if (ret < 0) {
1687 perror("failed to stat file");
1688 exit(-1);
1689 }
1690
1691 if (!stat.st_size) {
1692 fprintf(stderr, "zero-sized file, nothing to do!\n");
1693 exit(0);
1694 }
1695
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001696 header = perf_header__read(input);
1697 head = header->data_offset;
Peter Zijlstraf5970552009-06-18 23:22:55 +02001698
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001699 sample_type = perf_header__sample_type();
1700
1701 if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
Peter Zijlstraf5970552009-06-18 23:22:55 +02001702 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1703 exit(-1);
1704 }
1705
Ingo Molnard80d3382009-06-03 23:14:49 +02001706 if (load_kernel() < 0) {
1707 perror("failed to load kernel symbols");
1708 return EXIT_FAILURE;
1709 }
1710
1711 if (!full_paths) {
1712 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1713 perror("failed to get the current directory");
1714 return EXIT_FAILURE;
1715 }
1716 cwdlen = strlen(cwd);
1717 } else {
1718 cwd = NULL;
1719 cwdlen = 0;
1720 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001721
1722 shift = page_size * (head / page_size);
1723 offset += shift;
1724 head -= shift;
1725
Ingo Molnard80d3382009-06-03 23:14:49 +02001726remap:
1727 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1728 MAP_SHARED, input, offset);
1729 if (buf == MAP_FAILED) {
1730 perror("failed to mmap file");
1731 exit(-1);
1732 }
1733
1734more:
1735 event = (event_t *)(buf + head);
1736
1737 size = event->header.size;
1738 if (!size)
1739 size = 8;
1740
1741 if (head + event->header.size >= page_size * mmap_window) {
Ingo Molnard80d3382009-06-03 23:14:49 +02001742 int ret;
1743
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001744 shift = page_size * (head / page_size);
1745
Ingo Molnard80d3382009-06-03 23:14:49 +02001746 ret = munmap(buf, page_size * mmap_window);
1747 assert(ret == 0);
1748
1749 offset += shift;
1750 head -= shift;
1751 goto remap;
1752 }
1753
1754 size = event->header.size;
1755
Ingo Molnar8465b052009-06-14 14:44:07 +02001756 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001757 (void *)(offset + head),
1758 (void *)(long)event->header.size,
1759 event->header.type);
1760
Ingo Molnard80d3382009-06-03 23:14:49 +02001761 if (!size || process_event(event, offset, head) < 0) {
1762
Ingo Molnar35029732009-06-03 09:38:58 +02001763 dprintf("%p [%p]: skipping unknown header type: %d\n",
1764 (void *)(offset + head),
1765 (void *)(long)(event->header.size),
1766 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001767
Ingo Molnar3e706112009-05-26 18:53:17 +02001768 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001769
1770 /*
1771 * assume we lost track of the stream, check alignment, and
1772 * increment a single u64 in the hope to catch on again 'soon'.
1773 */
1774
1775 if (unlikely(head & 7))
1776 head &= ~7ULL;
1777
1778 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001779 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001780
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001781 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001782
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001783 if (offset + head >= header->data_offset + header->data_size)
Peter Zijlstraf5970552009-06-18 23:22:55 +02001784 goto done;
1785
Ingo Molnarf37a2912009-07-01 12:37:06 +02001786 if (offset + head < (unsigned long)stat.st_size)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001787 goto more;
1788
Peter Zijlstraf5970552009-06-18 23:22:55 +02001789done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001790 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001791 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001792
Ingo Molnar35029732009-06-03 09:38:58 +02001793 dprintf(" IP events: %10ld\n", total);
1794 dprintf(" mmap events: %10ld\n", total_mmap);
1795 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001796 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001797 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001798 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001799
Ingo Molnar35029732009-06-03 09:38:58 +02001800 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001801 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001802
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001803 if (verbose >= 3)
1804 threads__fprintf(stdout);
1805
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001806 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001807 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001808
Peter Zijlstra82292892009-06-03 12:37:36 +02001809 collapse__resort();
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001810 output__resort(total);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001811 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001812
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001813 return rc;
1814}
1815
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001816static int
1817parse_callchain_opt(const struct option *opt __used, const char *arg,
1818 int unset __used)
1819{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001820 char *tok;
1821 char *endptr;
1822
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001823 callchain = 1;
1824
1825 if (!arg)
1826 return 0;
1827
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001828 tok = strtok((char *)arg, ",");
1829 if (!tok)
1830 return -1;
1831
1832 /* get the output mode */
1833 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001834 callchain_mode = GRAPH;
1835
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001836 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001837 callchain_mode = FLAT;
1838 else
1839 return -1;
1840
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001841 /* get the min percentage */
1842 tok = strtok(NULL, ",");
1843 if (!tok)
1844 return 0;
1845
1846 callchain_min_percent = strtod(tok, &endptr);
1847 if (tok == endptr)
1848 return -1;
1849
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001850 return 0;
1851}
1852
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001853static const char * const report_usage[] = {
1854 "perf report [<options>] <command>",
1855 NULL
1856};
1857
1858static const struct option options[] = {
1859 OPT_STRING('i', "input", &input_name, "file",
1860 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001861 OPT_BOOLEAN('v', "verbose", &verbose,
1862 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001863 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1864 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001865 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +02001866 OPT_BOOLEAN('m', "modules", &modules,
1867 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001868 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001869 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001870 OPT_BOOLEAN('P', "full-paths", &full_paths,
1871 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001872 OPT_STRING('p', "parent", &parent_pattern, "regex",
1873 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001874 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1875 "Only display entries with parent-match"),
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001876 OPT_CALLBACK_DEFAULT('c', "callchain", NULL, "output_type,min_percent",
1877 "Display callchains using output_type and min percent threshold. "
1878 "Default: flat,0", &parse_callchain_opt, "flat,100"),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001879 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1880 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001881 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1882 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001883 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1884 "only consider these symbols"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001885 OPT_END()
1886};
1887
Ingo Molnar5352f352009-06-03 10:07:39 +02001888static void setup_sorting(void)
1889{
1890 char *tmp, *tok, *str = strdup(sort_order);
1891
1892 for (tok = strtok_r(str, ", ", &tmp);
1893 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1894 if (sort_dimension__add(tok) < 0) {
1895 error("Unknown --sort key: `%s'", tok);
1896 usage_with_options(report_usage, options);
1897 }
1898 }
1899
1900 free(str);
1901}
1902
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001903static void setup_list(struct strlist **list, const char *list_str,
1904 const char *list_name)
1905{
1906 if (list_str) {
1907 *list = strlist__new(true, list_str);
1908 if (!*list) {
1909 fprintf(stderr, "problems parsing %s list\n",
1910 list_name);
1911 exit(129);
1912 }
1913 }
1914}
1915
Ingo Molnarf37a2912009-07-01 12:37:06 +02001916int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001917{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001918 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001919
1920 page_size = getpagesize();
1921
Ingo Molnaredc52de2009-06-04 16:24:37 +02001922 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001923
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001924 setup_sorting();
1925
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001926 if (parent_pattern != default_parent_pattern)
1927 sort_dimension__add("parent");
1928 else
1929 exclude_other = 0;
1930
Ingo Molnaredc52de2009-06-04 16:24:37 +02001931 /*
1932 * Any (unrecognized) arguments left?
1933 */
1934 if (argc)
1935 usage_with_options(report_usage, options);
1936
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001937 setup_list(&dso_list, dso_list_str, "dso");
1938 setup_list(&comm_list, comm_list_str, "comm");
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001939 setup_list(&sym_list, sym_list_str, "symbol");
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001940
Ingo Molnara930d2c2009-05-27 09:50:13 +02001941 setup_pager();
1942
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001943 return __cmd_report();
1944}