blob: 93945ecdac8673b3c148fe4da8908ac46cd222c6 [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"
Brice Goglin8d513272009-08-07 13:55:24 +020020#include "util/values.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030021
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020022#include "perf.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020023#include "util/header.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020024
25#include "util/parse-options.h"
26#include "util/parse-events.h"
27
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030028#define SHOW_KERNEL 1
29#define SHOW_USER 2
30#define SHOW_HV 4
31
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020032static char const *input_name = "perf.data";
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
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030048static int full_paths;
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -030049static int show_nr_samples;
Ingo Molnar97b07b62009-05-26 18:48:58 +020050
Brice Goglin8d513272009-08-07 13:55:24 +020051static int show_threads;
52static struct perf_read_values show_threads_values;
53
Brice Goglin9f866692009-08-10 15:26:32 +020054static char default_pretty_printing_style[] = "normal";
55static char *pretty_printing_style = default_pretty_printing_style;
56
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030057static unsigned long page_size;
58static unsigned long mmap_window = 32;
59
Ingo Molnarb8e6d822009-06-18 14:32:19 +020060static char default_parent_pattern[] = "^sys_|^do_page_fault";
61static char *parent_pattern = default_parent_pattern;
Ingo Molnarb25bcf22009-06-18 07:01:03 +020062static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020063
Ingo Molnarb8e6d822009-06-18 14:32:19 +020064static int exclude_other = 1;
Frederic Weisbeckerbe903882009-07-05 07:39:18 +020065
Frederic Weisbecker805d1272009-07-05 07:39:21 +020066static char callchain_default_opt[] = "fractal,0.5";
67
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020068static int callchain;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020069
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020070static char __cwd[PATH_MAX];
71static char *cwd = __cwd;
72static int cwdlen;
73
Frederic Weisbecker805d1272009-07-05 07:39:21 +020074static
75struct callchain_param callchain_param = {
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +020076 .mode = CHAIN_GRAPH_REL,
Frederic Weisbecker805d1272009-07-05 07:39:21 +020077 .min_percent = 0.5
78};
Ingo Molnarb8e6d822009-06-18 14:32:19 +020079
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020080static u64 sample_type;
81
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030082static int repsep_fprintf(FILE *fp, const char *fmt, ...)
83{
84 int n;
85 va_list ap;
86
87 va_start(ap, fmt);
88 if (!field_sep)
89 n = vfprintf(fp, fmt, ap);
90 else {
91 char *bf = NULL;
92 n = vasprintf(&bf, fmt, ap);
93 if (n > 0) {
94 char *sep = bf;
95 while (1) {
96 sep = strchr(sep, *field_sep);
97 if (sep == NULL)
98 break;
99 *sep = '.';
100 }
101 }
102 fputs(bf, fp);
103 free(bf);
104 }
105 va_end(ap);
106 return n;
107}
108
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300109struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300110 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300111 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300112 pid_t pid;
113 char *comm;
114};
115
116static struct thread *thread__new(pid_t pid)
117{
118 struct thread *self = malloc(sizeof(*self));
119
120 if (self != NULL) {
121 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200122 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200123 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200124 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300125 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300126 }
127
128 return self;
129}
130
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300131static unsigned int dsos__col_width,
132 comms__col_width,
133 threads__col_width;
134
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300135static int thread__set_comm(struct thread *self, const char *comm)
136{
Peter Zijlstra82292892009-06-03 12:37:36 +0200137 if (self->comm)
138 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300139 self->comm = strdup(comm);
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300140 if (!self->comm)
141 return -ENOMEM;
142
143 if (!col_width_list_str && !field_sep &&
144 (!comm_list || strlist__has_entry(comm_list, comm))) {
145 unsigned int slen = strlen(comm);
146 if (slen > comms__col_width) {
147 comms__col_width = slen;
148 threads__col_width = slen + 6;
149 }
150 }
151
152 return 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300153}
154
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300155static size_t thread__fprintf(struct thread *self, FILE *fp)
156{
157 struct map *pos;
158 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
159
160 list_for_each_entry(pos, &self->maps, node)
161 ret += map__fprintf(pos, fp);
162
163 return ret;
164}
165
166
Ingo Molnar16f762a2009-05-27 09:10:38 +0200167static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200168static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300169
170static struct thread *threads__findnew(pid_t pid)
171{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300172 struct rb_node **p = &threads.rb_node;
173 struct rb_node *parent = NULL;
174 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300175
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200176 /*
177 * Font-end cache - PID lookups come in blocks,
178 * so most of the time we dont have to look up
179 * the full rbtree:
180 */
181 if (last_match && last_match->pid == pid)
182 return last_match;
183
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300184 while (*p != NULL) {
185 parent = *p;
186 th = rb_entry(parent, struct thread, rb_node);
187
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200188 if (th->pid == pid) {
189 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300190 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200191 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300192
193 if (pid < th->pid)
194 p = &(*p)->rb_left;
195 else
196 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300197 }
198
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300199 th = thread__new(pid);
200 if (th != NULL) {
201 rb_link_node(&th->rb_node, parent, p);
202 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200203 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300204 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200205
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300206 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300207}
208
209static void thread__insert_map(struct thread *self, struct map *map)
210{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200211 struct map *pos, *tmp;
212
213 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
214 if (map__overlap(pos, map)) {
Peter Zijlstra3d906ef2009-06-23 11:23:07 +0200215 if (verbose >= 2) {
216 printf("overlapping maps:\n");
217 map__fprintf(map, stdout);
218 map__fprintf(pos, stdout);
219 }
220
221 if (map->start <= pos->start && map->end > pos->start)
222 pos->start = map->end;
223
224 if (map->end >= pos->end && map->start < pos->end)
225 pos->end = map->start;
226
227 if (verbose >= 2) {
228 printf("after collision:\n");
229 map__fprintf(pos, stdout);
230 }
231
232 if (pos->start >= pos->end) {
233 list_del_init(&pos->node);
234 free(pos);
235 }
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200236 }
237 }
238
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300239 list_add_tail(&map->node, &self->maps);
240}
241
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200242static int thread__fork(struct thread *self, struct thread *parent)
243{
244 struct map *map;
245
246 if (self->comm)
247 free(self->comm);
248 self->comm = strdup(parent->comm);
249 if (!self->comm)
250 return -ENOMEM;
251
252 list_for_each_entry(map, &parent->maps, node) {
253 struct map *new = map__clone(map);
254 if (!new)
255 return -ENOMEM;
256 thread__insert_map(self, new);
257 }
258
259 return 0;
260}
261
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000262static struct map *thread__find_map(struct thread *self, u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300263{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200264 struct map *pos;
265
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300266 if (self == NULL)
267 return NULL;
268
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300269 list_for_each_entry(pos, &self->maps, node)
270 if (ip >= pos->start && ip <= pos->end)
271 return pos;
272
273 return NULL;
274}
275
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300276static size_t threads__fprintf(FILE *fp)
277{
278 size_t ret = 0;
279 struct rb_node *nd;
280
281 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
282 struct thread *pos = rb_entry(nd, struct thread, rb_node);
283
284 ret += thread__fprintf(pos, fp);
285 }
286
287 return ret;
288}
289
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200290/*
291 * histogram, sorted on item, collects counts
292 */
293
294static struct rb_root hist;
295
296struct hist_entry {
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200297 struct rb_node rb_node;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200298
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200299 struct thread *thread;
300 struct map *map;
301 struct dso *dso;
302 struct symbol *sym;
303 struct symbol *parent;
304 u64 ip;
305 char level;
306 struct callchain_node callchain;
307 struct rb_root sorted_chain;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200308
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200309 u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200310};
311
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200312/*
313 * configurable sorting bits
314 */
315
316struct sort_entry {
317 struct list_head list;
318
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200319 char *header;
320
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200321 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200322 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300323 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
324 unsigned int *width;
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300325 bool elide;
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200326};
327
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200328static int64_t cmp_null(void *l, void *r)
329{
330 if (!l && !r)
331 return 0;
332 else if (!l)
333 return -1;
334 else
335 return 1;
336}
337
Peter Zijlstra82292892009-06-03 12:37:36 +0200338/* --sort pid */
339
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200340static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200341sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
342{
343 return right->thread->pid - left->thread->pid;
344}
345
346static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300347sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200348{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300349 return repsep_fprintf(fp, "%*s:%5d", width - 6,
350 self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200351}
352
353static struct sort_entry sort_thread = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300354 .header = "Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200355 .cmp = sort__thread_cmp,
356 .print = sort__thread_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300357 .width = &threads__col_width,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200358};
359
Peter Zijlstra82292892009-06-03 12:37:36 +0200360/* --sort comm */
361
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200362static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200363sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
364{
Peter Zijlstra82292892009-06-03 12:37:36 +0200365 return right->thread->pid - left->thread->pid;
366}
367
368static int64_t
369sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
370{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200371 char *comm_l = left->thread->comm;
372 char *comm_r = right->thread->comm;
373
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200374 if (!comm_l || !comm_r)
375 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200376
377 return strcmp(comm_l, comm_r);
378}
379
380static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300381sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra992444b2009-05-27 20:20:27 +0200382{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300383 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200384}
385
386static struct sort_entry sort_comm = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300387 .header = "Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200388 .cmp = sort__comm_cmp,
389 .collapse = sort__comm_collapse,
390 .print = sort__comm_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300391 .width = &comms__col_width,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200392};
393
Peter Zijlstra82292892009-06-03 12:37:36 +0200394/* --sort dso */
395
Peter Zijlstra992444b2009-05-27 20:20:27 +0200396static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200397sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
398{
399 struct dso *dso_l = left->dso;
400 struct dso *dso_r = right->dso;
401
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200402 if (!dso_l || !dso_r)
403 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200404
405 return strcmp(dso_l->name, dso_r->name);
406}
407
408static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300409sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200410{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200411 if (self->dso)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300412 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200413
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300414 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200415}
416
417static struct sort_entry sort_dso = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300418 .header = "Shared Object",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200419 .cmp = sort__dso_cmp,
420 .print = sort__dso_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300421 .width = &dsos__col_width,
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200422};
423
Peter Zijlstra82292892009-06-03 12:37:36 +0200424/* --sort symbol */
425
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200426static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200427sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300428{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000429 u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200430
431 if (left->sym == right->sym)
432 return 0;
433
434 ip_l = left->sym ? left->sym->start : left->ip;
435 ip_r = right->sym ? right->sym->start : right->ip;
436
437 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300438}
439
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200440static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300441sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200442{
443 size_t ret = 0;
444
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200445 if (verbose)
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300446 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
447 dso__symtab_origin(self->dso));
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200448
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300449 ret += repsep_fprintf(fp, "[%c] ", self->level);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200450 if (self->sym) {
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300451 ret += repsep_fprintf(fp, "%s", self->sym->name);
Mike Galbraith42976482009-07-02 08:09:46 +0200452
453 if (self->sym->module)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300454 ret += repsep_fprintf(fp, "\t[%s]",
455 self->sym->module->name);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200456 } else {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300457 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200458 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200459
460 return ret;
461}
462
463static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200464 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200465 .cmp = sort__sym_cmp,
466 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200467};
468
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200469/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200470
471static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200472sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200473{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200474 struct symbol *sym_l = left->parent;
475 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200476
477 if (!sym_l || !sym_r)
478 return cmp_null(sym_l, sym_r);
479
480 return strcmp(sym_l->name, sym_r->name);
481}
482
483static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300484sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200485{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300486 return repsep_fprintf(fp, "%-*s", width,
487 self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200488}
489
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300490static unsigned int parent_symbol__col_width;
491
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200492static struct sort_entry sort_parent = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300493 .header = "Parent symbol",
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200494 .cmp = sort__parent_cmp,
495 .print = sort__parent_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300496 .width = &parent_symbol__col_width,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200497};
498
Peter Zijlstra82292892009-06-03 12:37:36 +0200499static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200500static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200501
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200502struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200503 char *name;
504 struct sort_entry *entry;
505 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200506};
507
508static struct sort_dimension sort_dimensions[] = {
509 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200510 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200511 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200512 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200513 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200514};
515
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200516static LIST_HEAD(hist_entry__sort_list);
517
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200518static int sort_dimension__add(char *tok)
519{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200520 unsigned int i;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200521
522 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
523 struct sort_dimension *sd = &sort_dimensions[i];
524
525 if (sd->taken)
526 continue;
527
Ingo Molnar5352f352009-06-03 10:07:39 +0200528 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200529 continue;
530
Peter Zijlstra82292892009-06-03 12:37:36 +0200531 if (sd->entry->collapse)
532 sort__need_collapse = 1;
533
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200534 if (sd->entry == &sort_parent) {
535 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200536 if (ret) {
537 char err[BUFSIZ];
538
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200539 regerror(ret, &parent_regex, err, sizeof(err));
540 fprintf(stderr, "Invalid regex: %s\n%s",
541 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200542 exit(-1);
543 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200544 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200545 }
546
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200547 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
548 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200549
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200550 return 0;
551 }
552
553 return -ESRCH;
554}
555
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200556static int64_t
557hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
558{
559 struct sort_entry *se;
560 int64_t cmp = 0;
561
562 list_for_each_entry(se, &hist_entry__sort_list, list) {
563 cmp = se->cmp(left, right);
564 if (cmp)
565 break;
566 }
567
568 return cmp;
569}
570
Peter Zijlstra82292892009-06-03 12:37:36 +0200571static int64_t
572hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
573{
574 struct sort_entry *se;
575 int64_t cmp = 0;
576
577 list_for_each_entry(se, &hist_entry__sort_list, list) {
578 int64_t (*f)(struct hist_entry *, struct hist_entry *);
579
580 f = se->collapse ?: se->cmp;
581
582 cmp = f(left, right);
583 if (cmp)
584 break;
585 }
586
587 return cmp;
588}
589
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200590static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
591{
592 int i;
593 size_t ret = 0;
594
595 ret += fprintf(fp, "%s", " ");
596
597 for (i = 0; i < depth; i++)
598 if (depth_mask & (1 << i))
599 ret += fprintf(fp, "| ");
600 else
601 ret += fprintf(fp, " ");
602
603 ret += fprintf(fp, "\n");
604
605 return ret;
606}
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200607static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200608ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
609 int depth_mask, int count, u64 total_samples,
610 int hits)
611{
612 int i;
613 size_t ret = 0;
614
615 ret += fprintf(fp, "%s", " ");
616 for (i = 0; i < depth; i++) {
617 if (depth_mask & (1 << i))
618 ret += fprintf(fp, "|");
619 else
620 ret += fprintf(fp, " ");
621 if (!count && i == depth - 1) {
622 double percent;
623
624 percent = hits * 100.0 / total_samples;
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200625 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200626 } else
627 ret += fprintf(fp, "%s", " ");
628 }
629 if (chain->sym)
630 ret += fprintf(fp, "%s\n", chain->sym->name);
631 else
632 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
633
634 return ret;
635}
636
Frederic Weisbecker25446032009-08-08 02:16:25 +0200637static struct symbol *rem_sq_bracket;
638static struct callchain_list rem_hits;
639
640static void init_rem_hits(void)
641{
642 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
643 if (!rem_sq_bracket) {
644 fprintf(stderr, "Not enough memory to display remaining hits\n");
645 return;
646 }
647
648 strcpy(rem_sq_bracket->name, "[...]");
649 rem_hits.sym = rem_sq_bracket;
650}
651
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200652static size_t
653callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
654 u64 total_samples, int depth, int depth_mask)
655{
656 struct rb_node *node, *next;
657 struct callchain_node *child;
658 struct callchain_list *chain;
659 int new_depth_mask = depth_mask;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200660 u64 new_total;
Frederic Weisbecker25446032009-08-08 02:16:25 +0200661 u64 remaining;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200662 size_t ret = 0;
663 int i;
664
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200665 if (callchain_param.mode == CHAIN_GRAPH_REL)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200666 new_total = self->children_hit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200667 else
668 new_total = total_samples;
669
Frederic Weisbecker25446032009-08-08 02:16:25 +0200670 remaining = new_total;
671
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200672 node = rb_first(&self->rb_root);
673 while (node) {
Frederic Weisbecker25446032009-08-08 02:16:25 +0200674 u64 cumul;
675
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200676 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200677 cumul = cumul_hits(child);
678 remaining -= cumul;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200679
680 /*
681 * The depth mask manages the output of pipes that show
682 * the depth. We don't want to keep the pipes of the current
Frederic Weisbecker25446032009-08-08 02:16:25 +0200683 * level for the last child of this depth.
684 * Except if we have remaining filtered hits. They will
685 * supersede the last child
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200686 */
687 next = rb_next(node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200688 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200689 new_depth_mask &= ~(1 << (depth - 1));
690
691 /*
692 * But we keep the older depth mask for the line seperator
693 * to keep the level link until we reach the last child
694 */
695 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
696 i = 0;
697 list_for_each_entry(chain, &child->val, list) {
698 if (chain->ip >= PERF_CONTEXT_MAX)
699 continue;
700 ret += ipchain__fprintf_graph(fp, chain, depth,
701 new_depth_mask, i++,
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200702 new_total,
Frederic Weisbecker25446032009-08-08 02:16:25 +0200703 cumul);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200704 }
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200705 ret += callchain__fprintf_graph(fp, child, new_total,
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200706 depth + 1,
707 new_depth_mask | (1 << depth));
708 node = next;
709 }
710
Frederic Weisbecker25446032009-08-08 02:16:25 +0200711 if (callchain_param.mode == CHAIN_GRAPH_REL &&
712 remaining && remaining != new_total) {
713
714 if (!rem_sq_bracket)
715 return ret;
716
717 new_depth_mask &= ~(1 << (depth - 1));
718
719 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
720 new_depth_mask, 0, new_total,
721 remaining);
722 }
723
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200724 return ret;
725}
726
727static size_t
728callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
729 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200730{
731 struct callchain_list *chain;
732 size_t ret = 0;
733
734 if (!self)
735 return 0;
736
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200737 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200738
739
Frederic Weisbecker44249612009-07-01 05:35:14 +0200740 list_for_each_entry(chain, &self->val, list) {
741 if (chain->ip >= PERF_CONTEXT_MAX)
742 continue;
743 if (chain->sym)
744 ret += fprintf(fp, " %s\n", chain->sym->name);
745 else
746 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200747 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200748 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200749
750 return ret;
751}
752
753static size_t
754hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
755 u64 total_samples)
756{
757 struct rb_node *rb_node;
758 struct callchain_node *chain;
759 size_t ret = 0;
760
761 rb_node = rb_first(&self->sorted_chain);
762 while (rb_node) {
763 double percent;
764
765 chain = rb_entry(rb_node, struct callchain_node, rb_node);
766 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200767 switch (callchain_param.mode) {
768 case CHAIN_FLAT:
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200769 ret += percent_color_fprintf(fp, " %6.2f%%\n",
770 percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200771 ret += callchain__fprintf_flat(fp, chain, total_samples);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200772 break;
773 case CHAIN_GRAPH_ABS: /* Falldown */
774 case CHAIN_GRAPH_REL:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200775 ret += callchain__fprintf_graph(fp, chain,
776 total_samples, 1, 1);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200777 default:
778 break;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200779 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200780 ret += fprintf(fp, "\n");
781 rb_node = rb_next(rb_node);
782 }
783
784 return ret;
785}
786
787
788static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000789hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200790{
791 struct sort_entry *se;
792 size_t ret;
793
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200794 if (exclude_other && !self->parent)
795 return 0;
796
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200797 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300798 ret = percent_color_fprintf(fp,
799 field_sep ? "%.2f" : " %6.2f%%",
800 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200801 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300802 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200803
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300804 if (show_nr_samples) {
805 if (field_sep)
806 fprintf(fp, "%c%lld", *field_sep, self->count);
807 else
808 fprintf(fp, "%11lld", self->count);
809 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200810
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200811 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300812 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200813 continue;
814
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300815 fprintf(fp, "%s", field_sep ?: " ");
816 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200817 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200818
819 ret += fprintf(fp, "\n");
820
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200821 if (callchain)
822 hist_entry_callchain__fprintf(fp, self, total_samples);
823
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200824 return ret;
825}
826
827/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200828 *
829 */
830
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300831static void dso__calc_col_width(struct dso *self)
832{
833 if (!col_width_list_str && !field_sep &&
834 (!dso_list || strlist__has_entry(dso_list, self->name))) {
835 unsigned int slen = strlen(self->name);
836 if (slen > dsos__col_width)
837 dsos__col_width = slen;
838 }
839
840 self->slen_calculated = 1;
841}
842
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200843static struct symbol *
844resolve_symbol(struct thread *thread, struct map **mapp,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000845 struct dso **dsop, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200846{
847 struct dso *dso = dsop ? *dsop : NULL;
848 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200849 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200850
851 if (!thread)
852 return NULL;
853
854 if (dso)
855 goto got_dso;
856
857 if (map)
858 goto got_map;
859
860 map = thread__find_map(thread, ip);
861 if (map != NULL) {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300862 /*
863 * We have to do this here as we may have a dso
864 * with no symbol hit that has a name longer than
865 * the ones with symbols sampled.
866 */
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300867 if (!sort_dso.elide && !map->dso->slen_calculated)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300868 dso__calc_col_width(map->dso);
869
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200870 if (mapp)
871 *mapp = map;
872got_map:
873 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200874
875 dso = map->dso;
876 } else {
877 /*
878 * If this is outside of all known maps,
879 * and is a negative address, try to look it
880 * up in the kernel dso, as it might be a
881 * vsyscall (which executes in user-mode):
882 */
883 if ((long long)ip < 0)
884 dso = kernel_dso;
885 }
886 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200887 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
888 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200889
890 if (dsop)
891 *dsop = dso;
892
893 if (!dso)
894 return NULL;
895got_dso:
896 return dso->find_symbol(dso, ip);
897}
898
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200899static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200900{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200901 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200902 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200903
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200904 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200905}
906
Frederic Weisbecker44249612009-07-01 05:35:14 +0200907static struct symbol **
Ingo Molnarf37a2912009-07-01 12:37:06 +0200908resolve_callchain(struct thread *thread, struct map *map __used,
Frederic Weisbecker44249612009-07-01 05:35:14 +0200909 struct ip_callchain *chain, struct hist_entry *entry)
910{
Frederic Weisbecker44249612009-07-01 05:35:14 +0200911 u64 context = PERF_CONTEXT_MAX;
Ingo Molnar029e5b12009-07-03 13:17:28 +0200912 struct symbol **syms = NULL;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200913 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200914
915 if (callchain) {
916 syms = calloc(chain->nr, sizeof(*syms));
917 if (!syms) {
918 fprintf(stderr, "Can't allocate memory for symbols\n");
919 exit(-1);
920 }
921 }
922
923 for (i = 0; i < chain->nr; i++) {
924 u64 ip = chain->ips[i];
925 struct dso *dso = NULL;
926 struct symbol *sym;
927
928 if (ip >= PERF_CONTEXT_MAX) {
929 context = ip;
930 continue;
931 }
932
933 switch (context) {
Ingo Molnar88a69df2009-07-01 11:17:20 +0200934 case PERF_CONTEXT_HV:
935 dso = hypervisor_dso;
936 break;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200937 case PERF_CONTEXT_KERNEL:
938 dso = kernel_dso;
939 break;
940 default:
941 break;
942 }
943
944 sym = resolve_symbol(thread, NULL, &dso, &ip);
945
946 if (sym) {
947 if (sort__has_parent && call__match(sym) &&
948 !entry->parent)
949 entry->parent = sym;
950 if (!callchain)
951 break;
952 syms[i] = sym;
953 }
954 }
955
956 return syms;
957}
958
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200959/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200960 * collect histogram counts
961 */
962
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200963static int
964hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000965 struct symbol *sym, u64 ip, struct ip_callchain *chain,
966 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300967{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200968 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300969 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200970 struct hist_entry *he;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200971 struct symbol **syms = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200972 struct hist_entry entry = {
973 .thread = thread,
974 .map = map,
975 .dso = dso,
976 .sym = sym,
977 .ip = ip,
978 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200979 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200980 .parent = NULL,
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200981 .sorted_chain = RB_ROOT
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200982 };
983 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300984
Frederic Weisbecker44249612009-07-01 05:35:14 +0200985 if ((sort__has_parent || callchain) && chain)
986 syms = resolve_callchain(thread, map, chain, &entry);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200987
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300988 while (*p != NULL) {
989 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200990 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300991
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200992 cmp = hist_entry__cmp(&entry, he);
993
994 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200995 he->count += count;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200996 if (callchain) {
997 append_chain(&he->callchain, chain, syms);
998 free(syms);
999 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001000 return 0;
1001 }
1002
1003 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001004 p = &(*p)->rb_left;
1005 else
1006 p = &(*p)->rb_right;
1007 }
1008
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001009 he = malloc(sizeof(*he));
1010 if (!he)
1011 return -ENOMEM;
1012 *he = entry;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001013 if (callchain) {
1014 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +02001015 append_chain(&he->callchain, chain, syms);
1016 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001017 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001018 rb_link_node(&he->rb_node, parent, p);
1019 rb_insert_color(&he->rb_node, &hist);
1020
1021 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001022}
1023
Peter Zijlstra82292892009-06-03 12:37:36 +02001024static void hist_entry__free(struct hist_entry *he)
1025{
1026 free(he);
1027}
1028
1029/*
1030 * collapse the histogram
1031 */
1032
1033static struct rb_root collapse_hists;
1034
1035static void collapse__insert_entry(struct hist_entry *he)
1036{
1037 struct rb_node **p = &collapse_hists.rb_node;
1038 struct rb_node *parent = NULL;
1039 struct hist_entry *iter;
1040 int64_t cmp;
1041
1042 while (*p != NULL) {
1043 parent = *p;
1044 iter = rb_entry(parent, struct hist_entry, rb_node);
1045
1046 cmp = hist_entry__collapse(iter, he);
1047
1048 if (!cmp) {
1049 iter->count += he->count;
1050 hist_entry__free(he);
1051 return;
1052 }
1053
1054 if (cmp < 0)
1055 p = &(*p)->rb_left;
1056 else
1057 p = &(*p)->rb_right;
1058 }
1059
1060 rb_link_node(&he->rb_node, parent, p);
1061 rb_insert_color(&he->rb_node, &collapse_hists);
1062}
1063
1064static void collapse__resort(void)
1065{
1066 struct rb_node *next;
1067 struct hist_entry *n;
1068
1069 if (!sort__need_collapse)
1070 return;
1071
1072 next = rb_first(&hist);
1073 while (next) {
1074 n = rb_entry(next, struct hist_entry, rb_node);
1075 next = rb_next(&n->rb_node);
1076
1077 rb_erase(&n->rb_node, &hist);
1078 collapse__insert_entry(n);
1079 }
1080}
1081
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001082/*
1083 * reverse the map, sort on count.
1084 */
1085
1086static struct rb_root output_hists;
1087
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001088static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001089{
1090 struct rb_node **p = &output_hists.rb_node;
1091 struct rb_node *parent = NULL;
1092 struct hist_entry *iter;
1093
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001094 if (callchain)
1095 callchain_param.sort(&he->sorted_chain, &he->callchain,
1096 min_callchain_hits, &callchain_param);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001097
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001098 while (*p != NULL) {
1099 parent = *p;
1100 iter = rb_entry(parent, struct hist_entry, rb_node);
1101
1102 if (he->count > iter->count)
1103 p = &(*p)->rb_left;
1104 else
1105 p = &(*p)->rb_right;
1106 }
1107
1108 rb_link_node(&he->rb_node, parent, p);
1109 rb_insert_color(&he->rb_node, &output_hists);
1110}
1111
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001112static void output__resort(u64 total_samples)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001113{
Peter Zijlstra82292892009-06-03 12:37:36 +02001114 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001115 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001116 struct rb_root *tree = &hist;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001117 u64 min_callchain_hits;
1118
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001119 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001120
Peter Zijlstra82292892009-06-03 12:37:36 +02001121 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001122 tree = &collapse_hists;
1123
1124 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001125
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001126 while (next) {
1127 n = rb_entry(next, struct hist_entry, rb_node);
1128 next = rb_next(&n->rb_node);
1129
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001130 rb_erase(&n->rb_node, tree);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001131 output__insert_entry(n, min_callchain_hits);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001132 }
1133}
1134
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001135static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001136{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001137 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001138 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001139 struct rb_node *nd;
1140 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001141 unsigned int width;
1142 char *col_width = col_width_list_str;
Brice Goglin9f866692009-08-10 15:26:32 +02001143 int raw_printing_style;
1144
1145 raw_printing_style = !strcmp(pretty_printing_style, "raw");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001146
Frederic Weisbecker25446032009-08-08 02:16:25 +02001147 init_rem_hits();
1148
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001149 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001150 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001151
1152 fprintf(fp, "# Overhead");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001153 if (show_nr_samples) {
1154 if (field_sep)
1155 fprintf(fp, "%cSamples", *field_sep);
1156 else
1157 fputs(" Samples ", fp);
1158 }
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001159 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001160 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001161 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001162 if (field_sep) {
1163 fprintf(fp, "%c%s", *field_sep, se->header);
1164 continue;
1165 }
1166 width = strlen(se->header);
1167 if (se->width) {
1168 if (col_width_list_str) {
1169 if (col_width) {
1170 *se->width = atoi(col_width);
1171 col_width = strchr(col_width, ',');
1172 if (col_width)
1173 ++col_width;
1174 }
1175 }
1176 width = *se->width = max(*se->width, width);
1177 }
1178 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001179 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001180 fprintf(fp, "\n");
1181
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001182 if (field_sep)
1183 goto print_entries;
1184
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001185 fprintf(fp, "# ........");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001186 if (show_nr_samples)
1187 fprintf(fp, " ..........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001188 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001189 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001190
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001191 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001192 continue;
1193
Ingo Molnar4593bba2009-06-02 15:34:25 +02001194 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001195 if (se->width)
1196 width = *se->width;
1197 else
1198 width = strlen(se->header);
1199 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001200 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001201 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001202 fprintf(fp, "\n");
1203
1204 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001205
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001206print_entries:
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001207 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1208 pos = rb_entry(nd, struct hist_entry, rb_node);
1209 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001210 }
1211
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001212 if (sort_order == default_sort_order &&
1213 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001214 fprintf(fp, "#\n");
Pekka Enberg114cfab2009-08-05 13:25:21 +03001215 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001216 fprintf(fp, "#\n");
1217 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001218 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001219
Frederic Weisbecker25446032009-08-08 02:16:25 +02001220 free(rem_sq_bracket);
1221
Brice Goglin8d513272009-08-07 13:55:24 +02001222 if (show_threads)
Brice Goglin9f866692009-08-10 15:26:32 +02001223 perf_read_values_display(fp, &show_threads_values,
1224 raw_printing_style);
Brice Goglin8d513272009-08-07 13:55:24 +02001225
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001226 return ret;
1227}
1228
Peter Zijlstra436224a2009-06-02 21:02:36 +02001229static void register_idle_thread(void)
1230{
1231 struct thread *thread = threads__findnew(0);
1232
1233 if (thread == NULL ||
1234 thread__set_comm(thread, "[idle]")) {
1235 fprintf(stderr, "problem inserting idle task.\n");
1236 exit(-1);
1237 }
1238}
1239
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001240static unsigned long total = 0,
1241 total_mmap = 0,
1242 total_comm = 0,
1243 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001244 total_unknown = 0,
1245 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001246
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001247static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +02001248{
1249 unsigned int chain_size;
1250
Ingo Molnar75220602009-06-18 08:00:17 +02001251 chain_size = event->header.size;
1252 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1253
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001254 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +02001255 return -1;
1256
1257 return 0;
1258}
1259
Ingo Molnard80d3382009-06-03 23:14:49 +02001260static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001261process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +02001262{
1263 char level;
1264 int show = 0;
1265 struct dso *dso = NULL;
1266 struct thread *thread = threads__findnew(event->ip.pid);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001267 u64 ip = event->ip.ip;
1268 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001269 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001270 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001271 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001272 int cpumode;
Ingo Molnar75051722009-06-03 23:14:49 +02001273
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001274 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001275 period = *(u64 *)more_data;
1276 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001277 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001278
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001279 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001280 (void *)(offset + head),
1281 (void *)(long)(event->header.size),
1282 event->header.misc,
1283 event->ip.pid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001284 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001285 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001286
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001287 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001288 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001289
1290 chain = (void *)more_data;
1291
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001292 dprintf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001293
Ingo Molnar75220602009-06-18 08:00:17 +02001294 if (validate_chain(chain, event) < 0) {
1295 eprintf("call-chain problem with event, skipping it.\n");
1296 return 0;
1297 }
1298
1299 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001300 for (i = 0; i < chain->nr; i++)
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001301 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001302 }
1303 }
1304
Ingo Molnar75051722009-06-03 23:14:49 +02001305 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1306
1307 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001308 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001309 event->header.type);
1310 return -1;
1311 }
1312
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001313 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1314 return 0;
1315
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001316 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1317
1318 if (cpumode == PERF_EVENT_MISC_KERNEL) {
Ingo Molnar75051722009-06-03 23:14:49 +02001319 show = SHOW_KERNEL;
1320 level = 'k';
1321
1322 dso = kernel_dso;
1323
1324 dprintf(" ...... dso: %s\n", dso->name);
1325
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001326 } else if (cpumode == PERF_EVENT_MISC_USER) {
Ingo Molnar75051722009-06-03 23:14:49 +02001327
1328 show = SHOW_USER;
1329 level = '.';
1330
Ingo Molnar75051722009-06-03 23:14:49 +02001331 } else {
1332 show = SHOW_HV;
1333 level = 'H';
Anton Blanchardfb9c8182009-07-01 09:00:49 +10001334
1335 dso = hypervisor_dso;
1336
Ingo Molnar75051722009-06-03 23:14:49 +02001337 dprintf(" ...... dso: [hypervisor]\n");
1338 }
1339
1340 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001341 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001342
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001343 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1344 return 0;
1345
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001346 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1347 return 0;
1348
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001349 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001350 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001351 return -1;
1352 }
1353 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001354 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001355
1356 return 0;
1357}
1358
1359static int
1360process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1361{
1362 struct thread *thread = threads__findnew(event->mmap.pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02001363 struct map *map = map__new(&event->mmap, cwd, cwdlen);
Ingo Molnar75051722009-06-03 23:14:49 +02001364
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001365 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001366 (void *)(offset + head),
1367 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001368 event->mmap.pid,
Ingo Molnar75051722009-06-03 23:14:49 +02001369 (void *)(long)event->mmap.start,
1370 (void *)(long)event->mmap.len,
1371 (void *)(long)event->mmap.pgoff,
1372 event->mmap.filename);
1373
1374 if (thread == NULL || map == NULL) {
1375 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001376 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001377 }
1378
1379 thread__insert_map(thread, map);
1380 total_mmap++;
1381
1382 return 0;
1383}
1384
1385static int
1386process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1387{
1388 struct thread *thread = threads__findnew(event->comm.pid);
1389
1390 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1391 (void *)(offset + head),
1392 (void *)(long)(event->header.size),
1393 event->comm.comm, event->comm.pid);
1394
1395 if (thread == NULL ||
1396 thread__set_comm(thread, event->comm.comm)) {
1397 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1398 return -1;
1399 }
1400 total_comm++;
1401
1402 return 0;
1403}
1404
1405static int
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001406process_task_event(event_t *event, unsigned long offset, unsigned long head)
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001407{
1408 struct thread *thread = threads__findnew(event->fork.pid);
1409 struct thread *parent = threads__findnew(event->fork.ppid);
1410
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001411 dprintf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001412 (void *)(offset + head),
1413 (void *)(long)(event->header.size),
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001414 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1415 event->fork.pid, event->fork.tid,
1416 event->fork.ppid, event->fork.ptid);
1417
1418 /*
1419 * A thread clone will have the same PID for both
1420 * parent and child.
1421 */
1422 if (thread == parent)
1423 return 0;
1424
1425 if (event->header.type == PERF_EVENT_EXIT)
1426 return 0;
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001427
1428 if (!thread || !parent || thread__fork(thread, parent)) {
1429 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1430 return -1;
1431 }
1432 total_fork++;
1433
1434 return 0;
1435}
1436
1437static int
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001438process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1439{
1440 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1441 (void *)(offset + head),
1442 (void *)(long)(event->header.size),
1443 event->lost.id,
1444 event->lost.lost);
1445
1446 total_lost += event->lost.lost;
1447
1448 return 0;
1449}
1450
Ingo Molnar8465b052009-06-14 14:44:07 +02001451static void trace_event(event_t *event)
1452{
1453 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001454 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001455 int i, j;
1456
1457 if (!dump_trace)
1458 return;
1459
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001460 dprintf(".");
1461 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001462
1463 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001464 if ((i & 15) == 0) {
1465 dprintf(".");
1466 cdprintf(" %04x: ", i);
1467 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001468
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001469 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001470
1471 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001472 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001473 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001474 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001475 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001476 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001477 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001478 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001479 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001480 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001481 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001482 }
1483 }
1484 dprintf(".\n");
1485}
1486
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001487static struct perf_header *header;
1488
1489static struct perf_counter_attr *perf_header__find_attr(u64 id)
1490{
1491 int i;
1492
1493 for (i = 0; i < header->attrs; i++) {
1494 struct perf_header_attr *attr = header->attr[i];
1495 int j;
1496
1497 for (j = 0; j < attr->ids; j++) {
1498 if (attr->id[j] == id)
1499 return &attr->attr;
1500 }
1501 }
1502
1503 return NULL;
1504}
1505
Ingo Molnarb2fef072009-06-05 18:07:51 +02001506static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001507process_read_event(event_t *event, unsigned long offset, unsigned long head)
1508{
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001509 struct perf_counter_attr *attr = perf_header__find_attr(event->read.id);
1510
Brice Goglin8d513272009-08-07 13:55:24 +02001511 if (show_threads) {
1512 char *name = attr ? __event_name(attr->type, attr->config)
1513 : "unknown";
1514 perf_read_values_add_value(&show_threads_values,
1515 event->read.pid, event->read.tid,
1516 event->read.id,
1517 name,
1518 event->read.value);
1519 }
1520
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001521 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001522 (void *)(offset + head),
1523 (void *)(long)(event->header.size),
1524 event->read.pid,
1525 event->read.tid,
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001526 attr ? __event_name(attr->type, attr->config)
1527 : "FAIL",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001528 event->read.value);
1529
1530 return 0;
1531}
1532
1533static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001534process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001535{
Ingo Molnar8465b052009-06-14 14:44:07 +02001536 trace_event(event);
1537
Ingo Molnar75051722009-06-03 23:14:49 +02001538 switch (event->header.type) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001539 case PERF_EVENT_SAMPLE:
1540 return process_sample_event(event, offset, head);
1541
Ingo Molnar75051722009-06-03 23:14:49 +02001542 case PERF_EVENT_MMAP:
1543 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001544
Ingo Molnar75051722009-06-03 23:14:49 +02001545 case PERF_EVENT_COMM:
1546 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001547
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001548 case PERF_EVENT_FORK:
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001549 case PERF_EVENT_EXIT:
1550 return process_task_event(event, offset, head);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001551
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001552 case PERF_EVENT_LOST:
1553 return process_lost_event(event, offset, head);
1554
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001555 case PERF_EVENT_READ:
1556 return process_read_event(event, offset, head);
1557
Ingo Molnard11444d2009-06-03 23:29:14 +02001558 /*
1559 * We dont process them right now but they are fine:
1560 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001561
Ingo Molnard11444d2009-06-03 23:29:14 +02001562 case PERF_EVENT_THROTTLE:
1563 case PERF_EVENT_UNTHROTTLE:
1564 return 0;
1565
Ingo Molnard80d3382009-06-03 23:14:49 +02001566 default:
1567 return -1;
1568 }
1569
1570 return 0;
1571}
1572
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001573static u64 perf_header__sample_type(void)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001574{
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001575 u64 sample_type = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001576 int i;
1577
1578 for (i = 0; i < header->attrs; i++) {
1579 struct perf_header_attr *attr = header->attr[i];
1580
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001581 if (!sample_type)
1582 sample_type = attr->attr.sample_type;
1583 else if (sample_type != attr->attr.sample_type)
1584 die("non matching sample_type");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001585 }
1586
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001587 return sample_type;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001588}
Peter Zijlstraf5970552009-06-18 23:22:55 +02001589
Ingo Molnard80d3382009-06-03 23:14:49 +02001590static int __cmd_report(void)
1591{
Ingo Molnar75051722009-06-03 23:14:49 +02001592 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001593 unsigned long offset = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001594 unsigned long head, shift;
Ingo Molnard80d3382009-06-03 23:14:49 +02001595 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001596 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001597 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001598 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001599
1600 register_idle_thread();
1601
Brice Goglin8d513272009-08-07 13:55:24 +02001602 if (show_threads)
1603 perf_read_values_init(&show_threads_values);
1604
Ingo Molnard80d3382009-06-03 23:14:49 +02001605 input = open(input_name, O_RDONLY);
1606 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001607 fprintf(stderr, " failed to open file: %s", input_name);
1608 if (!strcmp(input_name, "perf.data"))
1609 fprintf(stderr, " (try 'perf record' first)");
1610 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001611 exit(-1);
1612 }
1613
1614 ret = fstat(input, &stat);
1615 if (ret < 0) {
1616 perror("failed to stat file");
1617 exit(-1);
1618 }
1619
1620 if (!stat.st_size) {
1621 fprintf(stderr, "zero-sized file, nothing to do!\n");
1622 exit(0);
1623 }
1624
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001625 header = perf_header__read(input);
1626 head = header->data_offset;
Peter Zijlstraf5970552009-06-18 23:22:55 +02001627
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001628 sample_type = perf_header__sample_type();
1629
Frederic Weisbecker91b4eae2009-07-05 07:39:17 +02001630 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1631 if (sort__has_parent) {
1632 fprintf(stderr, "selected --sort parent, but no"
1633 " callchain data. Did you call"
1634 " perf record without -g?\n");
1635 exit(-1);
1636 }
1637 if (callchain) {
1638 fprintf(stderr, "selected -c but no callchain data."
1639 " Did you call perf record without"
1640 " -g?\n");
1641 exit(-1);
1642 }
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +02001643 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1644 callchain = 1;
1645 if (register_callchain_param(&callchain_param) < 0) {
1646 fprintf(stderr, "Can't register callchain"
1647 " params\n");
1648 exit(-1);
1649 }
Peter Zijlstraf5970552009-06-18 23:22:55 +02001650 }
1651
Ingo Molnard80d3382009-06-03 23:14:49 +02001652 if (load_kernel() < 0) {
1653 perror("failed to load kernel symbols");
1654 return EXIT_FAILURE;
1655 }
1656
1657 if (!full_paths) {
1658 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1659 perror("failed to get the current directory");
1660 return EXIT_FAILURE;
1661 }
1662 cwdlen = strlen(cwd);
1663 } else {
1664 cwd = NULL;
1665 cwdlen = 0;
1666 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001667
1668 shift = page_size * (head / page_size);
1669 offset += shift;
1670 head -= shift;
1671
Ingo Molnard80d3382009-06-03 23:14:49 +02001672remap:
1673 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1674 MAP_SHARED, input, offset);
1675 if (buf == MAP_FAILED) {
1676 perror("failed to mmap file");
1677 exit(-1);
1678 }
1679
1680more:
1681 event = (event_t *)(buf + head);
1682
1683 size = event->header.size;
1684 if (!size)
1685 size = 8;
1686
1687 if (head + event->header.size >= page_size * mmap_window) {
Ingo Molnard80d3382009-06-03 23:14:49 +02001688 int ret;
1689
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001690 shift = page_size * (head / page_size);
1691
Ingo Molnard80d3382009-06-03 23:14:49 +02001692 ret = munmap(buf, page_size * mmap_window);
1693 assert(ret == 0);
1694
1695 offset += shift;
1696 head -= shift;
1697 goto remap;
1698 }
1699
1700 size = event->header.size;
1701
Ingo Molnar8465b052009-06-14 14:44:07 +02001702 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001703 (void *)(offset + head),
1704 (void *)(long)event->header.size,
1705 event->header.type);
1706
Ingo Molnard80d3382009-06-03 23:14:49 +02001707 if (!size || process_event(event, offset, head) < 0) {
1708
Ingo Molnar35029732009-06-03 09:38:58 +02001709 dprintf("%p [%p]: skipping unknown header type: %d\n",
1710 (void *)(offset + head),
1711 (void *)(long)(event->header.size),
1712 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001713
Ingo Molnar3e706112009-05-26 18:53:17 +02001714 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001715
1716 /*
1717 * assume we lost track of the stream, check alignment, and
1718 * increment a single u64 in the hope to catch on again 'soon'.
1719 */
1720
1721 if (unlikely(head & 7))
1722 head &= ~7ULL;
1723
1724 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001725 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001726
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001727 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001728
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001729 if (offset + head >= header->data_offset + header->data_size)
Peter Zijlstraf5970552009-06-18 23:22:55 +02001730 goto done;
1731
Ingo Molnarf37a2912009-07-01 12:37:06 +02001732 if (offset + head < (unsigned long)stat.st_size)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001733 goto more;
1734
Peter Zijlstraf5970552009-06-18 23:22:55 +02001735done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001736 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001737 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001738
Ingo Molnar35029732009-06-03 09:38:58 +02001739 dprintf(" IP events: %10ld\n", total);
1740 dprintf(" mmap events: %10ld\n", total_mmap);
1741 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001742 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001743 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001744 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001745
Ingo Molnar35029732009-06-03 09:38:58 +02001746 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001747 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001748
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001749 if (verbose >= 3)
1750 threads__fprintf(stdout);
1751
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001752 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001753 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001754
Peter Zijlstra82292892009-06-03 12:37:36 +02001755 collapse__resort();
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001756 output__resort(total);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001757 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001758
Brice Goglin8d513272009-08-07 13:55:24 +02001759 if (show_threads)
1760 perf_read_values_destroy(&show_threads_values);
1761
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001762 return rc;
1763}
1764
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001765static int
1766parse_callchain_opt(const struct option *opt __used, const char *arg,
1767 int unset __used)
1768{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001769 char *tok;
1770 char *endptr;
1771
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001772 callchain = 1;
1773
1774 if (!arg)
1775 return 0;
1776
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001777 tok = strtok((char *)arg, ",");
1778 if (!tok)
1779 return -1;
1780
1781 /* get the output mode */
1782 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001783 callchain_param.mode = CHAIN_GRAPH_ABS;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001784
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001785 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001786 callchain_param.mode = CHAIN_FLAT;
1787
1788 else if (!strncmp(tok, "fractal", strlen(arg)))
1789 callchain_param.mode = CHAIN_GRAPH_REL;
1790
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +02001791 else if (!strncmp(tok, "none", strlen(arg))) {
1792 callchain_param.mode = CHAIN_NONE;
1793 callchain = 0;
1794
1795 return 0;
1796 }
1797
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001798 else
1799 return -1;
1800
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001801 /* get the min percentage */
1802 tok = strtok(NULL, ",");
1803 if (!tok)
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001804 goto setup;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001805
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001806 callchain_param.min_percent = strtod(tok, &endptr);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001807 if (tok == endptr)
1808 return -1;
1809
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001810setup:
1811 if (register_callchain_param(&callchain_param) < 0) {
1812 fprintf(stderr, "Can't register callchain params\n");
1813 return -1;
1814 }
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001815 return 0;
1816}
1817
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001818static const char * const report_usage[] = {
1819 "perf report [<options>] <command>",
1820 NULL
1821};
1822
1823static const struct option options[] = {
1824 OPT_STRING('i', "input", &input_name, "file",
1825 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001826 OPT_BOOLEAN('v', "verbose", &verbose,
1827 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001828 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1829 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001830 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +02001831 OPT_BOOLEAN('m', "modules", &modules,
1832 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001833 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1834 "Show a column with the number of samples"),
Brice Goglin8d513272009-08-07 13:55:24 +02001835 OPT_BOOLEAN('T', "threads", &show_threads,
1836 "Show per-thread event counters"),
Brice Goglin9f866692009-08-10 15:26:32 +02001837 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
1838 "pretty printing style key: normal raw"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001839 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001840 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001841 OPT_BOOLEAN('P', "full-paths", &full_paths,
1842 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001843 OPT_STRING('p', "parent", &parent_pattern, "regex",
1844 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001845 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1846 "Only display entries with parent-match"),
Anton Blanchard1483b19f2009-07-16 15:44:29 +02001847 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001848 "Display callchains using output_type and min percent threshold. "
Anton Blanchard1483b19f2009-07-16 15:44:29 +02001849 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001850 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1851 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001852 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1853 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001854 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1855 "only consider these symbols"),
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001856 OPT_STRING('w', "column-widths", &col_width_list_str,
1857 "width[,width...]",
1858 "don't try to adjust column width, use these fixed values"),
1859 OPT_STRING('t', "field-separator", &field_sep, "separator",
1860 "separator for columns, no spaces will be added between "
1861 "columns '.' is reserved."),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001862 OPT_END()
1863};
1864
Ingo Molnar5352f352009-06-03 10:07:39 +02001865static void setup_sorting(void)
1866{
1867 char *tmp, *tok, *str = strdup(sort_order);
1868
1869 for (tok = strtok_r(str, ", ", &tmp);
1870 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1871 if (sort_dimension__add(tok) < 0) {
1872 error("Unknown --sort key: `%s'", tok);
1873 usage_with_options(report_usage, options);
1874 }
1875 }
1876
1877 free(str);
1878}
1879
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001880static void setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001881 struct sort_entry *se, const char *list_name,
1882 FILE *fp)
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001883{
1884 if (list_str) {
1885 *list = strlist__new(true, list_str);
1886 if (!*list) {
1887 fprintf(stderr, "problems parsing %s list\n",
1888 list_name);
1889 exit(129);
1890 }
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001891 if (strlist__nr_entries(*list) == 1) {
1892 fprintf(fp, "# %s: %s\n", list_name,
1893 strlist__entry(*list, 0)->s);
1894 se->elide = true;
1895 }
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001896 }
1897}
1898
Ingo Molnarf37a2912009-07-01 12:37:06 +02001899int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001900{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001901 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001902
1903 page_size = getpagesize();
1904
Ingo Molnaredc52de2009-06-04 16:24:37 +02001905 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001906
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001907 setup_sorting();
1908
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001909 if (parent_pattern != default_parent_pattern) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001910 sort_dimension__add("parent");
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001911 sort_parent.elide = 1;
1912 } else
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001913 exclude_other = 0;
1914
Ingo Molnaredc52de2009-06-04 16:24:37 +02001915 /*
1916 * Any (unrecognized) arguments left?
1917 */
1918 if (argc)
1919 usage_with_options(report_usage, options);
1920
Ingo Molnara930d2c2009-05-27 09:50:13 +02001921 setup_pager();
1922
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001923 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
1924 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
1925 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001926
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001927 if (field_sep && *field_sep == '.') {
1928 fputs("'.' is the only non valid --field-separator argument\n",
1929 stderr);
1930 exit(129);
1931 }
1932
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001933 return __cmd_report();
1934}