blob: cdf9a8d27bb9929c1b546d9545b332945034c583 [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"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020023#include "util/debug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020024#include "util/header.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020025
26#include "util/parse-options.h"
27#include "util/parse-events.h"
28
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020029#include "util/thread.h"
30
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020031static char const *input_name = "perf.data";
Ingo Molnarbd741372009-06-04 14:13:04 +020032
Pekka Enberg114cfab2009-08-05 13:25:21 +030033static char default_sort_order[] = "comm,dso,symbol";
Ingo Molnarbd741372009-06-04 14:13:04 +020034static char *sort_order = default_sort_order;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030035static char *dso_list_str, *comm_list_str, *sym_list_str,
36 *col_width_list_str;
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -030037static struct strlist *dso_list, *comm_list, *sym_list;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030038static char *field_sep;
Ingo Molnarbd741372009-06-04 14:13:04 +020039
Peter Zijlstrafa6963b2009-08-19 11:18:26 +020040static int force;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030041static int input;
42static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
43
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030044static int full_paths;
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -030045static int show_nr_samples;
Ingo Molnar97b07b62009-05-26 18:48:58 +020046
Brice Goglin8d513272009-08-07 13:55:24 +020047static int show_threads;
48static struct perf_read_values show_threads_values;
49
Brice Goglin9f866692009-08-10 15:26:32 +020050static char default_pretty_printing_style[] = "normal";
51static char *pretty_printing_style = default_pretty_printing_style;
52
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 Weisbeckerbe903882009-07-05 07:39:18 +020061
Frederic Weisbecker805d1272009-07-05 07:39:21 +020062static char callchain_default_opt[] = "fractal,0.5";
63
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020064static int callchain;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020065
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020066static char __cwd[PATH_MAX];
67static char *cwd = __cwd;
68static int cwdlen;
69
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020070static struct rb_root threads;
71static struct thread *last_match;
72
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +020073static struct perf_header *header;
74
Frederic Weisbecker805d1272009-07-05 07:39:21 +020075static
76struct callchain_param callchain_param = {
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +020077 .mode = CHAIN_GRAPH_REL,
Frederic Weisbecker805d1272009-07-05 07:39:21 +020078 .min_percent = 0.5
79};
Ingo Molnarb8e6d822009-06-18 14:32:19 +020080
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020081static u64 sample_type;
82
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030083static int repsep_fprintf(FILE *fp, const char *fmt, ...)
84{
85 int n;
86 va_list ap;
87
88 va_start(ap, fmt);
89 if (!field_sep)
90 n = vfprintf(fp, fmt, ap);
91 else {
92 char *bf = NULL;
93 n = vasprintf(&bf, fmt, ap);
94 if (n > 0) {
95 char *sep = bf;
Ingo Molnar83a09442009-08-15 12:26:57 +020096
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030097 while (1) {
98 sep = strchr(sep, *field_sep);
99 if (sep == NULL)
100 break;
101 *sep = '.';
102 }
103 }
104 fputs(bf, fp);
105 free(bf);
106 }
107 va_end(ap);
108 return n;
109}
110
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300111static unsigned int dsos__col_width,
112 comms__col_width,
113 threads__col_width;
114
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200115/*
116 * histogram, sorted on item, collects counts
117 */
118
119static struct rb_root hist;
120
121struct hist_entry {
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200122 struct rb_node rb_node;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200123
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200124 struct thread *thread;
125 struct map *map;
126 struct dso *dso;
127 struct symbol *sym;
128 struct symbol *parent;
129 u64 ip;
130 char level;
131 struct callchain_node callchain;
132 struct rb_root sorted_chain;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200133
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200134 u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200135};
136
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200137/*
138 * configurable sorting bits
139 */
140
141struct sort_entry {
142 struct list_head list;
143
Ingo Molnar83a09442009-08-15 12:26:57 +0200144 const char *header;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200145
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200146 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200147 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300148 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
149 unsigned int *width;
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300150 bool elide;
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200151};
152
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200153static int64_t cmp_null(void *l, void *r)
154{
155 if (!l && !r)
156 return 0;
157 else if (!l)
158 return -1;
159 else
160 return 1;
161}
162
Peter Zijlstra82292892009-06-03 12:37:36 +0200163/* --sort pid */
164
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200165static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200166sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
167{
168 return right->thread->pid - left->thread->pid;
169}
170
171static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300172sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200173{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300174 return repsep_fprintf(fp, "%*s:%5d", width - 6,
175 self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200176}
177
178static struct sort_entry sort_thread = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300179 .header = "Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200180 .cmp = sort__thread_cmp,
181 .print = sort__thread_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300182 .width = &threads__col_width,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200183};
184
Peter Zijlstra82292892009-06-03 12:37:36 +0200185/* --sort comm */
186
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200187static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200188sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
189{
Peter Zijlstra82292892009-06-03 12:37:36 +0200190 return right->thread->pid - left->thread->pid;
191}
192
193static int64_t
194sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
195{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200196 char *comm_l = left->thread->comm;
197 char *comm_r = right->thread->comm;
198
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200199 if (!comm_l || !comm_r)
200 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200201
202 return strcmp(comm_l, comm_r);
203}
204
205static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300206sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra992444b2009-05-27 20:20:27 +0200207{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300208 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200209}
210
211static struct sort_entry sort_comm = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300212 .header = "Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200213 .cmp = sort__comm_cmp,
214 .collapse = sort__comm_collapse,
215 .print = sort__comm_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300216 .width = &comms__col_width,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200217};
218
Peter Zijlstra82292892009-06-03 12:37:36 +0200219/* --sort dso */
220
Peter Zijlstra992444b2009-05-27 20:20:27 +0200221static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200222sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
223{
224 struct dso *dso_l = left->dso;
225 struct dso *dso_r = right->dso;
226
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200227 if (!dso_l || !dso_r)
228 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200229
230 return strcmp(dso_l->name, dso_r->name);
231}
232
233static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300234sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200235{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200236 if (self->dso)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300237 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200238
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300239 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200240}
241
242static struct sort_entry sort_dso = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300243 .header = "Shared Object",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200244 .cmp = sort__dso_cmp,
245 .print = sort__dso_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300246 .width = &dsos__col_width,
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200247};
248
Peter Zijlstra82292892009-06-03 12:37:36 +0200249/* --sort symbol */
250
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200251static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200252sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300253{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000254 u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200255
256 if (left->sym == right->sym)
257 return 0;
258
259 ip_l = left->sym ? left->sym->start : left->ip;
260 ip_r = right->sym ? right->sym->start : right->ip;
261
262 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300263}
264
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200265static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300266sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200267{
268 size_t ret = 0;
269
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200270 if (verbose)
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300271 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
272 dso__symtab_origin(self->dso));
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200273
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300274 ret += repsep_fprintf(fp, "[%c] ", self->level);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200275 if (self->sym) {
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300276 ret += repsep_fprintf(fp, "%s", self->sym->name);
Mike Galbraith42976482009-07-02 08:09:46 +0200277
278 if (self->sym->module)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300279 ret += repsep_fprintf(fp, "\t[%s]",
280 self->sym->module->name);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200281 } else {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300282 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200283 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200284
285 return ret;
286}
287
288static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200289 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200290 .cmp = sort__sym_cmp,
291 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200292};
293
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200294/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200295
296static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200297sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200298{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200299 struct symbol *sym_l = left->parent;
300 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200301
302 if (!sym_l || !sym_r)
303 return cmp_null(sym_l, sym_r);
304
305 return strcmp(sym_l->name, sym_r->name);
306}
307
308static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300309sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200310{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300311 return repsep_fprintf(fp, "%-*s", width,
312 self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200313}
314
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300315static unsigned int parent_symbol__col_width;
316
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200317static struct sort_entry sort_parent = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300318 .header = "Parent symbol",
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200319 .cmp = sort__parent_cmp,
320 .print = sort__parent_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300321 .width = &parent_symbol__col_width,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200322};
323
Peter Zijlstra82292892009-06-03 12:37:36 +0200324static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200325static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200326
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200327struct sort_dimension {
Ingo Molnar83a09442009-08-15 12:26:57 +0200328 const char *name;
Ingo Molnar8edd4282009-06-05 14:13:18 +0200329 struct sort_entry *entry;
330 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200331};
332
333static struct sort_dimension sort_dimensions[] = {
334 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200335 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200336 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200337 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200338 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200339};
340
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200341static LIST_HEAD(hist_entry__sort_list);
342
Ingo Molnar83a09442009-08-15 12:26:57 +0200343static int sort_dimension__add(const char *tok)
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200344{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200345 unsigned int i;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200346
347 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
348 struct sort_dimension *sd = &sort_dimensions[i];
349
350 if (sd->taken)
351 continue;
352
Ingo Molnar5352f352009-06-03 10:07:39 +0200353 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200354 continue;
355
Peter Zijlstra82292892009-06-03 12:37:36 +0200356 if (sd->entry->collapse)
357 sort__need_collapse = 1;
358
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200359 if (sd->entry == &sort_parent) {
360 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200361 if (ret) {
362 char err[BUFSIZ];
363
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200364 regerror(ret, &parent_regex, err, sizeof(err));
365 fprintf(stderr, "Invalid regex: %s\n%s",
366 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200367 exit(-1);
368 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200369 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200370 }
371
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200372 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
373 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200374
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200375 return 0;
376 }
377
378 return -ESRCH;
379}
380
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200381static int64_t
382hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
383{
384 struct sort_entry *se;
385 int64_t cmp = 0;
386
387 list_for_each_entry(se, &hist_entry__sort_list, list) {
388 cmp = se->cmp(left, right);
389 if (cmp)
390 break;
391 }
392
393 return cmp;
394}
395
Peter Zijlstra82292892009-06-03 12:37:36 +0200396static int64_t
397hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
398{
399 struct sort_entry *se;
400 int64_t cmp = 0;
401
402 list_for_each_entry(se, &hist_entry__sort_list, list) {
403 int64_t (*f)(struct hist_entry *, struct hist_entry *);
404
405 f = se->collapse ?: se->cmp;
406
407 cmp = f(left, right);
408 if (cmp)
409 break;
410 }
411
412 return cmp;
413}
414
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200415static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
416{
417 int i;
418 size_t ret = 0;
419
420 ret += fprintf(fp, "%s", " ");
421
422 for (i = 0; i < depth; i++)
423 if (depth_mask & (1 << i))
424 ret += fprintf(fp, "| ");
425 else
426 ret += fprintf(fp, " ");
427
428 ret += fprintf(fp, "\n");
429
430 return ret;
431}
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200432static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200433ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
434 int depth_mask, int count, u64 total_samples,
435 int hits)
436{
437 int i;
438 size_t ret = 0;
439
440 ret += fprintf(fp, "%s", " ");
441 for (i = 0; i < depth; i++) {
442 if (depth_mask & (1 << i))
443 ret += fprintf(fp, "|");
444 else
445 ret += fprintf(fp, " ");
446 if (!count && i == depth - 1) {
447 double percent;
448
449 percent = hits * 100.0 / total_samples;
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200450 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200451 } else
452 ret += fprintf(fp, "%s", " ");
453 }
454 if (chain->sym)
455 ret += fprintf(fp, "%s\n", chain->sym->name);
456 else
457 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
458
459 return ret;
460}
461
Frederic Weisbecker25446032009-08-08 02:16:25 +0200462static struct symbol *rem_sq_bracket;
463static struct callchain_list rem_hits;
464
465static void init_rem_hits(void)
466{
467 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
468 if (!rem_sq_bracket) {
469 fprintf(stderr, "Not enough memory to display remaining hits\n");
470 return;
471 }
472
473 strcpy(rem_sq_bracket->name, "[...]");
474 rem_hits.sym = rem_sq_bracket;
475}
476
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200477static size_t
478callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
479 u64 total_samples, int depth, int depth_mask)
480{
481 struct rb_node *node, *next;
482 struct callchain_node *child;
483 struct callchain_list *chain;
484 int new_depth_mask = depth_mask;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200485 u64 new_total;
Frederic Weisbecker25446032009-08-08 02:16:25 +0200486 u64 remaining;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200487 size_t ret = 0;
488 int i;
489
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200490 if (callchain_param.mode == CHAIN_GRAPH_REL)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200491 new_total = self->children_hit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200492 else
493 new_total = total_samples;
494
Frederic Weisbecker25446032009-08-08 02:16:25 +0200495 remaining = new_total;
496
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200497 node = rb_first(&self->rb_root);
498 while (node) {
Frederic Weisbecker25446032009-08-08 02:16:25 +0200499 u64 cumul;
500
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200501 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200502 cumul = cumul_hits(child);
503 remaining -= cumul;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200504
505 /*
506 * The depth mask manages the output of pipes that show
507 * the depth. We don't want to keep the pipes of the current
Frederic Weisbecker25446032009-08-08 02:16:25 +0200508 * level for the last child of this depth.
509 * Except if we have remaining filtered hits. They will
510 * supersede the last child
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200511 */
512 next = rb_next(node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200513 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200514 new_depth_mask &= ~(1 << (depth - 1));
515
516 /*
517 * But we keep the older depth mask for the line seperator
518 * to keep the level link until we reach the last child
519 */
520 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
521 i = 0;
522 list_for_each_entry(chain, &child->val, list) {
523 if (chain->ip >= PERF_CONTEXT_MAX)
524 continue;
525 ret += ipchain__fprintf_graph(fp, chain, depth,
526 new_depth_mask, i++,
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200527 new_total,
Frederic Weisbecker25446032009-08-08 02:16:25 +0200528 cumul);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200529 }
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200530 ret += callchain__fprintf_graph(fp, child, new_total,
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200531 depth + 1,
532 new_depth_mask | (1 << depth));
533 node = next;
534 }
535
Frederic Weisbecker25446032009-08-08 02:16:25 +0200536 if (callchain_param.mode == CHAIN_GRAPH_REL &&
537 remaining && remaining != new_total) {
538
539 if (!rem_sq_bracket)
540 return ret;
541
542 new_depth_mask &= ~(1 << (depth - 1));
543
544 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
545 new_depth_mask, 0, new_total,
546 remaining);
547 }
548
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200549 return ret;
550}
551
552static size_t
553callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
554 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200555{
556 struct callchain_list *chain;
557 size_t ret = 0;
558
559 if (!self)
560 return 0;
561
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200562 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200563
564
Frederic Weisbecker44249612009-07-01 05:35:14 +0200565 list_for_each_entry(chain, &self->val, list) {
566 if (chain->ip >= PERF_CONTEXT_MAX)
567 continue;
568 if (chain->sym)
569 ret += fprintf(fp, " %s\n", chain->sym->name);
570 else
571 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200572 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200573 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200574
575 return ret;
576}
577
578static size_t
579hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
580 u64 total_samples)
581{
582 struct rb_node *rb_node;
583 struct callchain_node *chain;
584 size_t ret = 0;
585
586 rb_node = rb_first(&self->sorted_chain);
587 while (rb_node) {
588 double percent;
589
590 chain = rb_entry(rb_node, struct callchain_node, rb_node);
591 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200592 switch (callchain_param.mode) {
593 case CHAIN_FLAT:
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200594 ret += percent_color_fprintf(fp, " %6.2f%%\n",
595 percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200596 ret += callchain__fprintf_flat(fp, chain, total_samples);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200597 break;
598 case CHAIN_GRAPH_ABS: /* Falldown */
599 case CHAIN_GRAPH_REL:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200600 ret += callchain__fprintf_graph(fp, chain,
601 total_samples, 1, 1);
Ingo Molnar83a09442009-08-15 12:26:57 +0200602 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200603 default:
604 break;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200605 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200606 ret += fprintf(fp, "\n");
607 rb_node = rb_next(rb_node);
608 }
609
610 return ret;
611}
612
613
614static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000615hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200616{
617 struct sort_entry *se;
618 size_t ret;
619
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200620 if (exclude_other && !self->parent)
621 return 0;
622
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200623 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300624 ret = percent_color_fprintf(fp,
625 field_sep ? "%.2f" : " %6.2f%%",
626 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200627 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300628 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200629
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300630 if (show_nr_samples) {
631 if (field_sep)
632 fprintf(fp, "%c%lld", *field_sep, self->count);
633 else
634 fprintf(fp, "%11lld", self->count);
635 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200636
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200637 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300638 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200639 continue;
640
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300641 fprintf(fp, "%s", field_sep ?: " ");
642 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200643 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200644
645 ret += fprintf(fp, "\n");
646
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200647 if (callchain)
648 hist_entry_callchain__fprintf(fp, self, total_samples);
649
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200650 return ret;
651}
652
653/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200654 *
655 */
656
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300657static void dso__calc_col_width(struct dso *self)
658{
659 if (!col_width_list_str && !field_sep &&
660 (!dso_list || strlist__has_entry(dso_list, self->name))) {
661 unsigned int slen = strlen(self->name);
662 if (slen > dsos__col_width)
663 dsos__col_width = slen;
664 }
665
666 self->slen_calculated = 1;
667}
668
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200669static void thread__comm_adjust(struct thread *self)
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200670{
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200671 char *comm = self->comm;
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200672
673 if (!col_width_list_str && !field_sep &&
674 (!comm_list || strlist__has_entry(comm_list, comm))) {
675 unsigned int slen = strlen(comm);
676
677 if (slen > comms__col_width) {
678 comms__col_width = slen;
679 threads__col_width = slen + 6;
680 }
681 }
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200682}
683
684static int thread__set_comm_adjust(struct thread *self, const char *comm)
685{
686 int ret = thread__set_comm(self, comm);
687
688 if (ret)
689 return ret;
690
691 thread__comm_adjust(self);
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200692
693 return 0;
694}
695
696
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200697static struct symbol *
698resolve_symbol(struct thread *thread, struct map **mapp,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000699 struct dso **dsop, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200700{
701 struct dso *dso = dsop ? *dsop : NULL;
702 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200703 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200704
705 if (!thread)
706 return NULL;
707
708 if (dso)
709 goto got_dso;
710
711 if (map)
712 goto got_map;
713
714 map = thread__find_map(thread, ip);
715 if (map != NULL) {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300716 /*
717 * We have to do this here as we may have a dso
718 * with no symbol hit that has a name longer than
719 * the ones with symbols sampled.
720 */
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300721 if (!sort_dso.elide && !map->dso->slen_calculated)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300722 dso__calc_col_width(map->dso);
723
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200724 if (mapp)
725 *mapp = map;
726got_map:
727 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200728
729 dso = map->dso;
730 } else {
731 /*
732 * If this is outside of all known maps,
733 * and is a negative address, try to look it
734 * up in the kernel dso, as it might be a
735 * vsyscall (which executes in user-mode):
736 */
737 if ((long long)ip < 0)
738 dso = kernel_dso;
739 }
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200740 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
741 dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200742 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200743
744 if (dsop)
745 *dsop = dso;
746
747 if (!dso)
748 return NULL;
749got_dso:
750 return dso->find_symbol(dso, ip);
751}
752
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200753static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200754{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200755 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200756 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200757
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200758 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200759}
760
Frederic Weisbecker44249612009-07-01 05:35:14 +0200761static struct symbol **
Ingo Molnarf37a2912009-07-01 12:37:06 +0200762resolve_callchain(struct thread *thread, struct map *map __used,
Frederic Weisbecker44249612009-07-01 05:35:14 +0200763 struct ip_callchain *chain, struct hist_entry *entry)
764{
Frederic Weisbecker44249612009-07-01 05:35:14 +0200765 u64 context = PERF_CONTEXT_MAX;
Ingo Molnar029e5b12009-07-03 13:17:28 +0200766 struct symbol **syms = NULL;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200767 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200768
769 if (callchain) {
770 syms = calloc(chain->nr, sizeof(*syms));
771 if (!syms) {
772 fprintf(stderr, "Can't allocate memory for symbols\n");
773 exit(-1);
774 }
775 }
776
777 for (i = 0; i < chain->nr; i++) {
778 u64 ip = chain->ips[i];
779 struct dso *dso = NULL;
780 struct symbol *sym;
781
782 if (ip >= PERF_CONTEXT_MAX) {
783 context = ip;
784 continue;
785 }
786
787 switch (context) {
Ingo Molnar88a69df2009-07-01 11:17:20 +0200788 case PERF_CONTEXT_HV:
789 dso = hypervisor_dso;
790 break;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200791 case PERF_CONTEXT_KERNEL:
792 dso = kernel_dso;
793 break;
794 default:
795 break;
796 }
797
798 sym = resolve_symbol(thread, NULL, &dso, &ip);
799
800 if (sym) {
801 if (sort__has_parent && call__match(sym) &&
802 !entry->parent)
803 entry->parent = sym;
804 if (!callchain)
805 break;
806 syms[i] = sym;
807 }
808 }
809
810 return syms;
811}
812
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200813/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200814 * collect histogram counts
815 */
816
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200817static int
818hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000819 struct symbol *sym, u64 ip, struct ip_callchain *chain,
820 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300821{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200822 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300823 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200824 struct hist_entry *he;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200825 struct symbol **syms = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200826 struct hist_entry entry = {
827 .thread = thread,
828 .map = map,
829 .dso = dso,
830 .sym = sym,
831 .ip = ip,
832 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200833 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200834 .parent = NULL,
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200835 .sorted_chain = RB_ROOT
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200836 };
837 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300838
Frederic Weisbecker44249612009-07-01 05:35:14 +0200839 if ((sort__has_parent || callchain) && chain)
840 syms = resolve_callchain(thread, map, chain, &entry);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200841
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300842 while (*p != NULL) {
843 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200844 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300845
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200846 cmp = hist_entry__cmp(&entry, he);
847
848 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200849 he->count += count;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200850 if (callchain) {
851 append_chain(&he->callchain, chain, syms);
852 free(syms);
853 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200854 return 0;
855 }
856
857 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300858 p = &(*p)->rb_left;
859 else
860 p = &(*p)->rb_right;
861 }
862
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200863 he = malloc(sizeof(*he));
864 if (!he)
865 return -ENOMEM;
866 *he = entry;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200867 if (callchain) {
868 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200869 append_chain(&he->callchain, chain, syms);
870 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200871 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200872 rb_link_node(&he->rb_node, parent, p);
873 rb_insert_color(&he->rb_node, &hist);
874
875 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300876}
877
Peter Zijlstra82292892009-06-03 12:37:36 +0200878static void hist_entry__free(struct hist_entry *he)
879{
880 free(he);
881}
882
883/*
884 * collapse the histogram
885 */
886
887static struct rb_root collapse_hists;
888
889static void collapse__insert_entry(struct hist_entry *he)
890{
891 struct rb_node **p = &collapse_hists.rb_node;
892 struct rb_node *parent = NULL;
893 struct hist_entry *iter;
894 int64_t cmp;
895
896 while (*p != NULL) {
897 parent = *p;
898 iter = rb_entry(parent, struct hist_entry, rb_node);
899
900 cmp = hist_entry__collapse(iter, he);
901
902 if (!cmp) {
903 iter->count += he->count;
904 hist_entry__free(he);
905 return;
906 }
907
908 if (cmp < 0)
909 p = &(*p)->rb_left;
910 else
911 p = &(*p)->rb_right;
912 }
913
914 rb_link_node(&he->rb_node, parent, p);
915 rb_insert_color(&he->rb_node, &collapse_hists);
916}
917
918static void collapse__resort(void)
919{
920 struct rb_node *next;
921 struct hist_entry *n;
922
923 if (!sort__need_collapse)
924 return;
925
926 next = rb_first(&hist);
927 while (next) {
928 n = rb_entry(next, struct hist_entry, rb_node);
929 next = rb_next(&n->rb_node);
930
931 rb_erase(&n->rb_node, &hist);
932 collapse__insert_entry(n);
933 }
934}
935
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200936/*
937 * reverse the map, sort on count.
938 */
939
940static struct rb_root output_hists;
941
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200942static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200943{
944 struct rb_node **p = &output_hists.rb_node;
945 struct rb_node *parent = NULL;
946 struct hist_entry *iter;
947
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200948 if (callchain)
949 callchain_param.sort(&he->sorted_chain, &he->callchain,
950 min_callchain_hits, &callchain_param);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200951
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200952 while (*p != NULL) {
953 parent = *p;
954 iter = rb_entry(parent, struct hist_entry, rb_node);
955
956 if (he->count > iter->count)
957 p = &(*p)->rb_left;
958 else
959 p = &(*p)->rb_right;
960 }
961
962 rb_link_node(&he->rb_node, parent, p);
963 rb_insert_color(&he->rb_node, &output_hists);
964}
965
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200966static void output__resort(u64 total_samples)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200967{
Peter Zijlstra82292892009-06-03 12:37:36 +0200968 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200969 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300970 struct rb_root *tree = &hist;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200971 u64 min_callchain_hits;
972
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200973 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200974
Peter Zijlstra82292892009-06-03 12:37:36 +0200975 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300976 tree = &collapse_hists;
977
978 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +0200979
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200980 while (next) {
981 n = rb_entry(next, struct hist_entry, rb_node);
982 next = rb_next(&n->rb_node);
983
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -0300984 rb_erase(&n->rb_node, tree);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200985 output__insert_entry(n, min_callchain_hits);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300986 }
987}
988
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000989static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300990{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200991 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200992 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300993 struct rb_node *nd;
994 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300995 unsigned int width;
996 char *col_width = col_width_list_str;
Brice Goglin9f866692009-08-10 15:26:32 +0200997 int raw_printing_style;
998
999 raw_printing_style = !strcmp(pretty_printing_style, "raw");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001000
Frederic Weisbecker25446032009-08-08 02:16:25 +02001001 init_rem_hits();
1002
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001003 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001004 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001005
1006 fprintf(fp, "# Overhead");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001007 if (show_nr_samples) {
1008 if (field_sep)
1009 fprintf(fp, "%cSamples", *field_sep);
1010 else
1011 fputs(" Samples ", fp);
1012 }
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001013 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001014 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001015 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001016 if (field_sep) {
1017 fprintf(fp, "%c%s", *field_sep, se->header);
1018 continue;
1019 }
1020 width = strlen(se->header);
1021 if (se->width) {
1022 if (col_width_list_str) {
1023 if (col_width) {
1024 *se->width = atoi(col_width);
1025 col_width = strchr(col_width, ',');
1026 if (col_width)
1027 ++col_width;
1028 }
1029 }
1030 width = *se->width = max(*se->width, width);
1031 }
1032 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001033 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001034 fprintf(fp, "\n");
1035
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001036 if (field_sep)
1037 goto print_entries;
1038
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001039 fprintf(fp, "# ........");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001040 if (show_nr_samples)
1041 fprintf(fp, " ..........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001042 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001043 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001044
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001045 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001046 continue;
1047
Ingo Molnar4593bba2009-06-02 15:34:25 +02001048 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001049 if (se->width)
1050 width = *se->width;
1051 else
1052 width = strlen(se->header);
1053 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001054 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001055 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001056 fprintf(fp, "\n");
1057
1058 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001059
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001060print_entries:
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001061 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1062 pos = rb_entry(nd, struct hist_entry, rb_node);
1063 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001064 }
1065
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001066 if (sort_order == default_sort_order &&
1067 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001068 fprintf(fp, "#\n");
Pekka Enberg114cfab2009-08-05 13:25:21 +03001069 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001070 fprintf(fp, "#\n");
1071 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001072 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001073
Frederic Weisbecker25446032009-08-08 02:16:25 +02001074 free(rem_sq_bracket);
1075
Brice Goglin8d513272009-08-07 13:55:24 +02001076 if (show_threads)
Brice Goglin9f866692009-08-10 15:26:32 +02001077 perf_read_values_display(fp, &show_threads_values,
1078 raw_printing_style);
Brice Goglin8d513272009-08-07 13:55:24 +02001079
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001080 return ret;
1081}
1082
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001083static unsigned long total = 0,
1084 total_mmap = 0,
1085 total_comm = 0,
1086 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001087 total_unknown = 0,
1088 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001089
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001090static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +02001091{
1092 unsigned int chain_size;
1093
Ingo Molnar75220602009-06-18 08:00:17 +02001094 chain_size = event->header.size;
1095 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1096
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001097 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +02001098 return -1;
1099
1100 return 0;
1101}
1102
Ingo Molnard80d3382009-06-03 23:14:49 +02001103static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001104process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +02001105{
1106 char level;
1107 int show = 0;
1108 struct dso *dso = NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001109 struct thread *thread;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001110 u64 ip = event->ip.ip;
1111 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001112 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001113 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001114 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001115 int cpumode;
Ingo Molnar75051722009-06-03 23:14:49 +02001116
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001117 thread = threads__findnew(event->ip.pid, &threads, &last_match);
1118
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001119 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001120 period = *(u64 *)more_data;
1121 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001122 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001123
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001124 dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001125 (void *)(offset + head),
1126 (void *)(long)(event->header.size),
1127 event->header.misc,
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -03001128 event->ip.pid, event->ip.tid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001129 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001130 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001131
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001132 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001133 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001134
1135 chain = (void *)more_data;
1136
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001137 dump_printf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001138
Ingo Molnar75220602009-06-18 08:00:17 +02001139 if (validate_chain(chain, event) < 0) {
1140 eprintf("call-chain problem with event, skipping it.\n");
1141 return 0;
1142 }
1143
1144 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001145 for (i = 0; i < chain->nr; i++)
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001146 dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001147 }
1148 }
1149
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001150 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Ingo Molnar75051722009-06-03 23:14:49 +02001151
1152 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001153 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001154 event->header.type);
1155 return -1;
1156 }
1157
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001158 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1159 return 0;
1160
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001161 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1162
1163 if (cpumode == PERF_EVENT_MISC_KERNEL) {
Ingo Molnar75051722009-06-03 23:14:49 +02001164 show = SHOW_KERNEL;
1165 level = 'k';
1166
1167 dso = kernel_dso;
1168
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001169 dump_printf(" ...... dso: %s\n", dso->name);
Ingo Molnar75051722009-06-03 23:14:49 +02001170
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001171 } else if (cpumode == PERF_EVENT_MISC_USER) {
Ingo Molnar75051722009-06-03 23:14:49 +02001172
1173 show = SHOW_USER;
1174 level = '.';
1175
Ingo Molnar75051722009-06-03 23:14:49 +02001176 } else {
1177 show = SHOW_HV;
1178 level = 'H';
Anton Blanchardfb9c8182009-07-01 09:00:49 +10001179
1180 dso = hypervisor_dso;
1181
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001182 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001183 }
1184
1185 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001186 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001187
Arnaldo Carvalho de Melo8fd101f2009-08-12 18:19:57 -03001188 if (dso_list && (!dso || !dso->name ||
1189 !strlist__has_entry(dso_list, dso->name)))
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001190 return 0;
1191
Arnaldo Carvalho de Melo8fd101f2009-08-12 18:19:57 -03001192 if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name)))
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001193 return 0;
1194
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001195 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001196 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001197 return -1;
1198 }
1199 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001200 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001201
1202 return 0;
1203}
1204
1205static int
1206process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1207{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001208 struct thread *thread;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02001209 struct map *map = map__new(&event->mmap, cwd, cwdlen);
Ingo Molnar75051722009-06-03 23:14:49 +02001210
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001211 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
1212
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001213 dump_printf("%p [%p]: PERF_EVENT_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001214 (void *)(offset + head),
1215 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001216 event->mmap.pid,
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -03001217 event->mmap.tid,
Ingo Molnar75051722009-06-03 23:14:49 +02001218 (void *)(long)event->mmap.start,
1219 (void *)(long)event->mmap.len,
1220 (void *)(long)event->mmap.pgoff,
1221 event->mmap.filename);
1222
1223 if (thread == NULL || map == NULL) {
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001224 dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001225 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001226 }
1227
1228 thread__insert_map(thread, map);
1229 total_mmap++;
1230
1231 return 0;
1232}
1233
1234static int
1235process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1236{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001237 struct thread *thread;
1238
1239 thread = threads__findnew(event->comm.pid, &threads, &last_match);
Ingo Molnar75051722009-06-03 23:14:49 +02001240
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001241 dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001242 (void *)(offset + head),
1243 (void *)(long)(event->header.size),
1244 event->comm.comm, event->comm.pid);
1245
1246 if (thread == NULL ||
Frederic Weisbecker4273b002009-08-18 16:03:46 +02001247 thread__set_comm_adjust(thread, event->comm.comm)) {
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001248 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001249 return -1;
1250 }
1251 total_comm++;
1252
1253 return 0;
1254}
1255
1256static int
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001257process_task_event(event_t *event, unsigned long offset, unsigned long head)
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001258{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001259 struct thread *thread;
1260 struct thread *parent;
1261
1262 thread = threads__findnew(event->fork.pid, &threads, &last_match);
1263 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001264
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001265 dump_printf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001266 (void *)(offset + head),
1267 (void *)(long)(event->header.size),
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001268 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1269 event->fork.pid, event->fork.tid,
1270 event->fork.ppid, event->fork.ptid);
1271
1272 /*
1273 * A thread clone will have the same PID for both
1274 * parent and child.
1275 */
1276 if (thread == parent)
1277 return 0;
1278
1279 if (event->header.type == PERF_EVENT_EXIT)
1280 return 0;
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001281
1282 if (!thread || !parent || thread__fork(thread, parent)) {
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001283 dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n");
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001284 return -1;
1285 }
1286 total_fork++;
1287
1288 return 0;
1289}
1290
1291static int
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001292process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1293{
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001294 dump_printf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001295 (void *)(offset + head),
1296 (void *)(long)(event->header.size),
1297 event->lost.id,
1298 event->lost.lost);
1299
1300 total_lost += event->lost.lost;
1301
1302 return 0;
1303}
1304
Ingo Molnarb2fef072009-06-05 18:07:51 +02001305static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001306process_read_event(event_t *event, unsigned long offset, unsigned long head)
1307{
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02001308 struct perf_counter_attr *attr;
1309
1310 attr = perf_header__find_attr(event->read.id, header);
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001311
Brice Goglin8d513272009-08-07 13:55:24 +02001312 if (show_threads) {
Ingo Molnar83a09442009-08-15 12:26:57 +02001313 const char *name = attr ? __event_name(attr->type, attr->config)
Brice Goglin8d513272009-08-07 13:55:24 +02001314 : "unknown";
1315 perf_read_values_add_value(&show_threads_values,
1316 event->read.pid, event->read.tid,
1317 event->read.id,
1318 name,
1319 event->read.value);
1320 }
1321
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001322 dump_printf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001323 (void *)(offset + head),
1324 (void *)(long)(event->header.size),
1325 event->read.pid,
1326 event->read.tid,
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001327 attr ? __event_name(attr->type, attr->config)
1328 : "FAIL",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001329 event->read.value);
1330
1331 return 0;
1332}
1333
1334static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001335process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001336{
Ingo Molnar8465b052009-06-14 14:44:07 +02001337 trace_event(event);
1338
Ingo Molnar75051722009-06-03 23:14:49 +02001339 switch (event->header.type) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001340 case PERF_EVENT_SAMPLE:
1341 return process_sample_event(event, offset, head);
1342
Ingo Molnar75051722009-06-03 23:14:49 +02001343 case PERF_EVENT_MMAP:
1344 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001345
Ingo Molnar75051722009-06-03 23:14:49 +02001346 case PERF_EVENT_COMM:
1347 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001348
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001349 case PERF_EVENT_FORK:
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001350 case PERF_EVENT_EXIT:
1351 return process_task_event(event, offset, head);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001352
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001353 case PERF_EVENT_LOST:
1354 return process_lost_event(event, offset, head);
1355
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001356 case PERF_EVENT_READ:
1357 return process_read_event(event, offset, head);
1358
Ingo Molnard11444d2009-06-03 23:29:14 +02001359 /*
1360 * We dont process them right now but they are fine:
1361 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001362
Ingo Molnard11444d2009-06-03 23:29:14 +02001363 case PERF_EVENT_THROTTLE:
1364 case PERF_EVENT_UNTHROTTLE:
1365 return 0;
1366
Ingo Molnard80d3382009-06-03 23:14:49 +02001367 default:
1368 return -1;
1369 }
1370
1371 return 0;
1372}
1373
1374static int __cmd_report(void)
1375{
Ingo Molnar75051722009-06-03 23:14:49 +02001376 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001377 unsigned long offset = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001378 unsigned long head, shift;
Ingo Molnar83a09442009-08-15 12:26:57 +02001379 struct stat input_stat;
Frederic Weisbecker5b447a62009-08-31 06:45:18 +02001380 struct thread *idle;
Ingo Molnard80d3382009-06-03 23:14:49 +02001381 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001382 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001383 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001384
Frederic Weisbecker5b447a62009-08-31 06:45:18 +02001385 idle = register_idle_thread(&threads, &last_match);
1386 thread__comm_adjust(idle);
Ingo Molnard80d3382009-06-03 23:14:49 +02001387
Brice Goglin8d513272009-08-07 13:55:24 +02001388 if (show_threads)
1389 perf_read_values_init(&show_threads_values);
1390
Ingo Molnard80d3382009-06-03 23:14:49 +02001391 input = open(input_name, O_RDONLY);
1392 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001393 fprintf(stderr, " failed to open file: %s", input_name);
1394 if (!strcmp(input_name, "perf.data"))
1395 fprintf(stderr, " (try 'perf record' first)");
1396 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001397 exit(-1);
1398 }
1399
Ingo Molnar83a09442009-08-15 12:26:57 +02001400 ret = fstat(input, &input_stat);
Ingo Molnard80d3382009-06-03 23:14:49 +02001401 if (ret < 0) {
1402 perror("failed to stat file");
1403 exit(-1);
1404 }
1405
Pierre Habouzit119e7a22009-08-27 09:59:02 +02001406 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
1407 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +02001408 exit(-1);
1409 }
1410
Ingo Molnar83a09442009-08-15 12:26:57 +02001411 if (!input_stat.st_size) {
Ingo Molnard80d3382009-06-03 23:14:49 +02001412 fprintf(stderr, "zero-sized file, nothing to do!\n");
1413 exit(0);
1414 }
1415
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001416 header = perf_header__read(input);
1417 head = header->data_offset;
Peter Zijlstraf5970552009-06-18 23:22:55 +02001418
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02001419 sample_type = perf_header__sample_type(header);
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001420
Frederic Weisbecker91b4eae2009-07-05 07:39:17 +02001421 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1422 if (sort__has_parent) {
1423 fprintf(stderr, "selected --sort parent, but no"
1424 " callchain data. Did you call"
1425 " perf record without -g?\n");
1426 exit(-1);
1427 }
1428 if (callchain) {
Frederic Weisbecker6ede59c2009-08-17 23:07:48 +02001429 fprintf(stderr, "selected -g but no callchain data."
Frederic Weisbecker91b4eae2009-07-05 07:39:17 +02001430 " Did you call perf record without"
1431 " -g?\n");
1432 exit(-1);
1433 }
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +02001434 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1435 callchain = 1;
1436 if (register_callchain_param(&callchain_param) < 0) {
1437 fprintf(stderr, "Can't register callchain"
1438 " params\n");
1439 exit(-1);
1440 }
Peter Zijlstraf5970552009-06-18 23:22:55 +02001441 }
1442
Ingo Molnard80d3382009-06-03 23:14:49 +02001443 if (load_kernel() < 0) {
1444 perror("failed to load kernel symbols");
1445 return EXIT_FAILURE;
1446 }
1447
1448 if (!full_paths) {
1449 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1450 perror("failed to get the current directory");
1451 return EXIT_FAILURE;
1452 }
1453 cwdlen = strlen(cwd);
1454 } else {
1455 cwd = NULL;
1456 cwdlen = 0;
1457 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001458
1459 shift = page_size * (head / page_size);
1460 offset += shift;
1461 head -= shift;
1462
Ingo Molnard80d3382009-06-03 23:14:49 +02001463remap:
1464 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1465 MAP_SHARED, input, offset);
1466 if (buf == MAP_FAILED) {
1467 perror("failed to mmap file");
1468 exit(-1);
1469 }
1470
1471more:
1472 event = (event_t *)(buf + head);
1473
1474 size = event->header.size;
1475 if (!size)
1476 size = 8;
1477
1478 if (head + event->header.size >= page_size * mmap_window) {
Ingo Molnar83a09442009-08-15 12:26:57 +02001479 int munmap_ret;
Ingo Molnard80d3382009-06-03 23:14:49 +02001480
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001481 shift = page_size * (head / page_size);
1482
Ingo Molnar83a09442009-08-15 12:26:57 +02001483 munmap_ret = munmap(buf, page_size * mmap_window);
1484 assert(munmap_ret == 0);
Ingo Molnard80d3382009-06-03 23:14:49 +02001485
1486 offset += shift;
1487 head -= shift;
1488 goto remap;
1489 }
1490
1491 size = event->header.size;
1492
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001493 dump_printf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001494 (void *)(offset + head),
1495 (void *)(long)event->header.size,
1496 event->header.type);
1497
Ingo Molnard80d3382009-06-03 23:14:49 +02001498 if (!size || process_event(event, offset, head) < 0) {
1499
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001500 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar35029732009-06-03 09:38:58 +02001501 (void *)(offset + head),
1502 (void *)(long)(event->header.size),
1503 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001504
Ingo Molnar3e706112009-05-26 18:53:17 +02001505 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001506
1507 /*
1508 * assume we lost track of the stream, check alignment, and
1509 * increment a single u64 in the hope to catch on again 'soon'.
1510 */
1511
1512 if (unlikely(head & 7))
1513 head &= ~7ULL;
1514
1515 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001516 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001517
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001518 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001519
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001520 if (offset + head >= header->data_offset + header->data_size)
Peter Zijlstraf5970552009-06-18 23:22:55 +02001521 goto done;
1522
Ingo Molnar83a09442009-08-15 12:26:57 +02001523 if (offset + head < (unsigned long)input_stat.st_size)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001524 goto more;
1525
Peter Zijlstraf5970552009-06-18 23:22:55 +02001526done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001527 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001528 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001529
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001530 dump_printf(" IP events: %10ld\n", total);
1531 dump_printf(" mmap events: %10ld\n", total_mmap);
1532 dump_printf(" comm events: %10ld\n", total_comm);
1533 dump_printf(" fork events: %10ld\n", total_fork);
1534 dump_printf(" lost events: %10ld\n", total_lost);
1535 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001536
Ingo Molnar35029732009-06-03 09:38:58 +02001537 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001538 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001539
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001540 if (verbose >= 3)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001541 threads__fprintf(stdout, &threads);
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001542
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001543 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001544 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001545
Peter Zijlstra82292892009-06-03 12:37:36 +02001546 collapse__resort();
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001547 output__resort(total);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001548 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001549
Brice Goglin8d513272009-08-07 13:55:24 +02001550 if (show_threads)
1551 perf_read_values_destroy(&show_threads_values);
1552
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001553 return rc;
1554}
1555
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001556static int
1557parse_callchain_opt(const struct option *opt __used, const char *arg,
1558 int unset __used)
1559{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001560 char *tok;
1561 char *endptr;
1562
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001563 callchain = 1;
1564
1565 if (!arg)
1566 return 0;
1567
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001568 tok = strtok((char *)arg, ",");
1569 if (!tok)
1570 return -1;
1571
1572 /* get the output mode */
1573 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001574 callchain_param.mode = CHAIN_GRAPH_ABS;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001575
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001576 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001577 callchain_param.mode = CHAIN_FLAT;
1578
1579 else if (!strncmp(tok, "fractal", strlen(arg)))
1580 callchain_param.mode = CHAIN_GRAPH_REL;
1581
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +02001582 else if (!strncmp(tok, "none", strlen(arg))) {
1583 callchain_param.mode = CHAIN_NONE;
1584 callchain = 0;
1585
1586 return 0;
1587 }
1588
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001589 else
1590 return -1;
1591
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001592 /* get the min percentage */
1593 tok = strtok(NULL, ",");
1594 if (!tok)
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001595 goto setup;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001596
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001597 callchain_param.min_percent = strtod(tok, &endptr);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001598 if (tok == endptr)
1599 return -1;
1600
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001601setup:
1602 if (register_callchain_param(&callchain_param) < 0) {
1603 fprintf(stderr, "Can't register callchain params\n");
1604 return -1;
1605 }
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001606 return 0;
1607}
1608
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001609static const char * const report_usage[] = {
1610 "perf report [<options>] <command>",
1611 NULL
1612};
1613
1614static const struct option options[] = {
1615 OPT_STRING('i', "input", &input_name, "file",
1616 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001617 OPT_BOOLEAN('v', "verbose", &verbose,
1618 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001619 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1620 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001621 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +02001622 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Mike Galbraith42976482009-07-02 08:09:46 +02001623 OPT_BOOLEAN('m', "modules", &modules,
1624 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001625 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1626 "Show a column with the number of samples"),
Brice Goglin8d513272009-08-07 13:55:24 +02001627 OPT_BOOLEAN('T', "threads", &show_threads,
1628 "Show per-thread event counters"),
Brice Goglin9f866692009-08-10 15:26:32 +02001629 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
1630 "pretty printing style key: normal raw"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001631 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001632 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001633 OPT_BOOLEAN('P', "full-paths", &full_paths,
1634 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001635 OPT_STRING('p', "parent", &parent_pattern, "regex",
1636 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001637 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1638 "Only display entries with parent-match"),
Anton Blanchard1483b19f2009-07-16 15:44:29 +02001639 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001640 "Display callchains using output_type and min percent threshold. "
Anton Blanchard1483b19f2009-07-16 15:44:29 +02001641 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001642 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1643 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001644 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1645 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001646 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1647 "only consider these symbols"),
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001648 OPT_STRING('w', "column-widths", &col_width_list_str,
1649 "width[,width...]",
1650 "don't try to adjust column width, use these fixed values"),
1651 OPT_STRING('t', "field-separator", &field_sep, "separator",
1652 "separator for columns, no spaces will be added between "
1653 "columns '.' is reserved."),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001654 OPT_END()
1655};
1656
Ingo Molnar5352f352009-06-03 10:07:39 +02001657static void setup_sorting(void)
1658{
1659 char *tmp, *tok, *str = strdup(sort_order);
1660
1661 for (tok = strtok_r(str, ", ", &tmp);
1662 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1663 if (sort_dimension__add(tok) < 0) {
1664 error("Unknown --sort key: `%s'", tok);
1665 usage_with_options(report_usage, options);
1666 }
1667 }
1668
1669 free(str);
1670}
1671
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001672static void setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001673 struct sort_entry *se, const char *list_name,
1674 FILE *fp)
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001675{
1676 if (list_str) {
1677 *list = strlist__new(true, list_str);
1678 if (!*list) {
1679 fprintf(stderr, "problems parsing %s list\n",
1680 list_name);
1681 exit(129);
1682 }
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001683 if (strlist__nr_entries(*list) == 1) {
1684 fprintf(fp, "# %s: %s\n", list_name,
1685 strlist__entry(*list, 0)->s);
1686 se->elide = true;
1687 }
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001688 }
1689}
1690
Ingo Molnarf37a2912009-07-01 12:37:06 +02001691int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001692{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001693 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001694
1695 page_size = getpagesize();
1696
Ingo Molnaredc52de2009-06-04 16:24:37 +02001697 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001698
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001699 setup_sorting();
1700
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001701 if (parent_pattern != default_parent_pattern) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001702 sort_dimension__add("parent");
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001703 sort_parent.elide = 1;
1704 } else
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001705 exclude_other = 0;
1706
Ingo Molnaredc52de2009-06-04 16:24:37 +02001707 /*
1708 * Any (unrecognized) arguments left?
1709 */
1710 if (argc)
1711 usage_with_options(report_usage, options);
1712
Ingo Molnara930d2c2009-05-27 09:50:13 +02001713 setup_pager();
1714
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001715 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
1716 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
1717 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001718
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001719 if (field_sep && *field_sep == '.') {
1720 fputs("'.' is the only non valid --field-separator argument\n",
1721 stderr);
1722 exit(129);
1723 }
1724
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001725 return __cmd_report();
1726}