blob: fe4aadc9630f5a9db93dc38ae907273c70d4a378 [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 Weisbecker8f288272009-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"
John Kacurdd68ada2009-09-24 18:02:49 +020030#include "util/sort.h"
John Kacur3d1d07e2009-09-28 15:32:55 +020031#include "util/hist.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020032
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020033static char const *input_name = "perf.data";
Ingo Molnarbd741372009-06-04 14:13:04 +020034
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;
Ingo Molnarbd741372009-06-04 14:13:04 +020038
Peter Zijlstrafa6963b2009-08-19 11:18:26 +020039static int force;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030040static int input;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030041
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030042static int full_paths;
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -030043static int show_nr_samples;
Ingo Molnar97b07b62009-05-26 18:48:58 +020044
Brice Goglin8d513272009-08-07 13:55:24 +020045static int show_threads;
46static struct perf_read_values show_threads_values;
47
Brice Goglin9f866692009-08-10 15:26:32 +020048static char default_pretty_printing_style[] = "normal";
49static char *pretty_printing_style = default_pretty_printing_style;
50
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030051static unsigned long page_size;
52static unsigned long mmap_window = 32;
53
Ingo Molnarb8e6d822009-06-18 14:32:19 +020054static int exclude_other = 1;
Frederic Weisbeckerbe903882009-07-05 07:39:18 +020055
Frederic Weisbecker805d1272009-07-05 07:39:21 +020056static char callchain_default_opt[] = "fractal,0.5";
57
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020058static char __cwd[PATH_MAX];
59static char *cwd = __cwd;
60static int cwdlen;
61
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020062static struct rb_root threads;
63static struct thread *last_match;
64
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +020065static struct perf_header *header;
66
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020067static u64 sample_type;
68
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020069static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
70{
71 int i;
72 size_t ret = 0;
73
74 ret += fprintf(fp, "%s", " ");
75
76 for (i = 0; i < depth; i++)
77 if (depth_mask & (1 << i))
78 ret += fprintf(fp, "| ");
79 else
80 ret += fprintf(fp, " ");
81
82 ret += fprintf(fp, "\n");
83
84 return ret;
85}
Peter Zijlstra1aa16732009-05-27 20:20:25 +020086static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020087ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
88 int depth_mask, int count, u64 total_samples,
89 int hits)
90{
91 int i;
92 size_t ret = 0;
93
94 ret += fprintf(fp, "%s", " ");
95 for (i = 0; i < depth; i++) {
96 if (depth_mask & (1 << i))
97 ret += fprintf(fp, "|");
98 else
99 ret += fprintf(fp, " ");
100 if (!count && i == depth - 1) {
101 double percent;
102
103 percent = hits * 100.0 / total_samples;
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200104 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200105 } else
106 ret += fprintf(fp, "%s", " ");
107 }
108 if (chain->sym)
109 ret += fprintf(fp, "%s\n", chain->sym->name);
110 else
111 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
112
113 return ret;
114}
115
Frederic Weisbecker25446032009-08-08 02:16:25 +0200116static struct symbol *rem_sq_bracket;
117static struct callchain_list rem_hits;
118
119static void init_rem_hits(void)
120{
121 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
122 if (!rem_sq_bracket) {
123 fprintf(stderr, "Not enough memory to display remaining hits\n");
124 return;
125 }
126
127 strcpy(rem_sq_bracket->name, "[...]");
128 rem_hits.sym = rem_sq_bracket;
129}
130
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200131static size_t
132callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
133 u64 total_samples, int depth, int depth_mask)
134{
135 struct rb_node *node, *next;
136 struct callchain_node *child;
137 struct callchain_list *chain;
138 int new_depth_mask = depth_mask;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200139 u64 new_total;
Frederic Weisbecker25446032009-08-08 02:16:25 +0200140 u64 remaining;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200141 size_t ret = 0;
142 int i;
143
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200144 if (callchain_param.mode == CHAIN_GRAPH_REL)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200145 new_total = self->children_hit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200146 else
147 new_total = total_samples;
148
Frederic Weisbecker25446032009-08-08 02:16:25 +0200149 remaining = new_total;
150
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200151 node = rb_first(&self->rb_root);
152 while (node) {
Frederic Weisbecker25446032009-08-08 02:16:25 +0200153 u64 cumul;
154
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200155 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200156 cumul = cumul_hits(child);
157 remaining -= cumul;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200158
159 /*
160 * The depth mask manages the output of pipes that show
161 * the depth. We don't want to keep the pipes of the current
Frederic Weisbecker25446032009-08-08 02:16:25 +0200162 * level for the last child of this depth.
163 * Except if we have remaining filtered hits. They will
164 * supersede the last child
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200165 */
166 next = rb_next(node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200167 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200168 new_depth_mask &= ~(1 << (depth - 1));
169
170 /*
171 * But we keep the older depth mask for the line seperator
172 * to keep the level link until we reach the last child
173 */
174 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
175 i = 0;
176 list_for_each_entry(chain, &child->val, list) {
177 if (chain->ip >= PERF_CONTEXT_MAX)
178 continue;
179 ret += ipchain__fprintf_graph(fp, chain, depth,
180 new_depth_mask, i++,
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200181 new_total,
Frederic Weisbecker25446032009-08-08 02:16:25 +0200182 cumul);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200183 }
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200184 ret += callchain__fprintf_graph(fp, child, new_total,
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200185 depth + 1,
186 new_depth_mask | (1 << depth));
187 node = next;
188 }
189
Frederic Weisbecker25446032009-08-08 02:16:25 +0200190 if (callchain_param.mode == CHAIN_GRAPH_REL &&
191 remaining && remaining != new_total) {
192
193 if (!rem_sq_bracket)
194 return ret;
195
196 new_depth_mask &= ~(1 << (depth - 1));
197
198 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
199 new_depth_mask, 0, new_total,
200 remaining);
201 }
202
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200203 return ret;
204}
205
206static size_t
207callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
208 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200209{
210 struct callchain_list *chain;
211 size_t ret = 0;
212
213 if (!self)
214 return 0;
215
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200216 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200217
218
Frederic Weisbecker44249612009-07-01 05:35:14 +0200219 list_for_each_entry(chain, &self->val, list) {
220 if (chain->ip >= PERF_CONTEXT_MAX)
221 continue;
222 if (chain->sym)
223 ret += fprintf(fp, " %s\n", chain->sym->name);
224 else
225 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200226 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200227 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200228
229 return ret;
230}
231
232static size_t
233hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
234 u64 total_samples)
235{
236 struct rb_node *rb_node;
237 struct callchain_node *chain;
238 size_t ret = 0;
239
240 rb_node = rb_first(&self->sorted_chain);
241 while (rb_node) {
242 double percent;
243
244 chain = rb_entry(rb_node, struct callchain_node, rb_node);
245 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200246 switch (callchain_param.mode) {
247 case CHAIN_FLAT:
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200248 ret += percent_color_fprintf(fp, " %6.2f%%\n",
249 percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200250 ret += callchain__fprintf_flat(fp, chain, total_samples);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200251 break;
252 case CHAIN_GRAPH_ABS: /* Falldown */
253 case CHAIN_GRAPH_REL:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200254 ret += callchain__fprintf_graph(fp, chain,
255 total_samples, 1, 1);
Ingo Molnar83a09442009-08-15 12:26:57 +0200256 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200257 default:
258 break;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200259 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200260 ret += fprintf(fp, "\n");
261 rb_node = rb_next(rb_node);
262 }
263
264 return ret;
265}
266
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200267static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000268hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200269{
270 struct sort_entry *se;
271 size_t ret;
272
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200273 if (exclude_other && !self->parent)
274 return 0;
275
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200276 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300277 ret = percent_color_fprintf(fp,
278 field_sep ? "%.2f" : " %6.2f%%",
279 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200280 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300281 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200282
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300283 if (show_nr_samples) {
284 if (field_sep)
285 fprintf(fp, "%c%lld", *field_sep, self->count);
286 else
287 fprintf(fp, "%11lld", self->count);
288 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200289
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200290 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300291 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200292 continue;
293
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300294 fprintf(fp, "%s", field_sep ?: " ");
295 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200296 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200297
298 ret += fprintf(fp, "\n");
299
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200300 if (callchain)
301 hist_entry_callchain__fprintf(fp, self, total_samples);
302
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200303 return ret;
304}
305
306/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200307 *
308 */
309
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300310static void dso__calc_col_width(struct dso *self)
311{
312 if (!col_width_list_str && !field_sep &&
313 (!dso_list || strlist__has_entry(dso_list, self->name))) {
314 unsigned int slen = strlen(self->name);
315 if (slen > dsos__col_width)
316 dsos__col_width = slen;
317 }
318
319 self->slen_calculated = 1;
320}
321
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200322static void thread__comm_adjust(struct thread *self)
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200323{
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200324 char *comm = self->comm;
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200325
326 if (!col_width_list_str && !field_sep &&
327 (!comm_list || strlist__has_entry(comm_list, comm))) {
328 unsigned int slen = strlen(comm);
329
330 if (slen > comms__col_width) {
331 comms__col_width = slen;
332 threads__col_width = slen + 6;
333 }
334 }
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200335}
336
337static int thread__set_comm_adjust(struct thread *self, const char *comm)
338{
339 int ret = thread__set_comm(self, comm);
340
341 if (ret)
342 return ret;
343
344 thread__comm_adjust(self);
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200345
346 return 0;
347}
348
349
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200350static struct symbol *
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300351resolve_symbol(struct thread *thread, struct map **mapp, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200352{
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200353 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200354 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200355
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200356 if (map)
357 goto got_map;
358
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300359 if (!thread)
360 return NULL;
361
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200362 map = thread__find_map(thread, ip);
363 if (map != NULL) {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300364 /*
365 * We have to do this here as we may have a dso
366 * with no symbol hit that has a name longer than
367 * the ones with symbols sampled.
368 */
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300369 if (!sort_dso.elide && !map->dso->slen_calculated)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300370 dso__calc_col_width(map->dso);
371
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200372 if (mapp)
373 *mapp = map;
374got_map:
375 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200376 } else {
377 /*
378 * If this is outside of all known maps,
379 * and is a negative address, try to look it
380 * up in the kernel dso, as it might be a
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300381 * vsyscall or vdso (which executes in user-mode).
382 *
383 * XXX This is nasty, we should have a symbol list in
384 * the "[vdso]" dso, but for now lets use the old
385 * trick of looking in the whole kernel symbol list.
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200386 */
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300387 if ((long long)ip < 0) {
388 map = kernel_map;
389 if (mapp)
390 *mapp = map;
391 }
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200392 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300393 dump_printf(" ...... dso: %s\n",
394 map ? map->dso->long_name : "<not found>");
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200395 dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200396 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200397
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300398 return map ? map->dso->find_symbol(map->dso, ip) : NULL;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200399}
400
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200401static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200402{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200403 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200404 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200405
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200406 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200407}
408
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300409static struct symbol **resolve_callchain(struct thread *thread, struct map *map,
410 struct ip_callchain *chain,
411 struct symbol **parent)
Frederic Weisbecker44249612009-07-01 05:35:14 +0200412{
Frederic Weisbecker44249612009-07-01 05:35:14 +0200413 u64 context = PERF_CONTEXT_MAX;
Ingo Molnar029e5b12009-07-03 13:17:28 +0200414 struct symbol **syms = NULL;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200415 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200416
417 if (callchain) {
418 syms = calloc(chain->nr, sizeof(*syms));
419 if (!syms) {
420 fprintf(stderr, "Can't allocate memory for symbols\n");
421 exit(-1);
422 }
423 }
424
425 for (i = 0; i < chain->nr; i++) {
426 u64 ip = chain->ips[i];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300427 struct symbol *sym = NULL;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200428
429 if (ip >= PERF_CONTEXT_MAX) {
430 context = ip;
431 continue;
432 }
433
434 switch (context) {
Ingo Molnar88a69df2009-07-01 11:17:20 +0200435 case PERF_CONTEXT_HV:
Ingo Molnar88a69df2009-07-01 11:17:20 +0200436 break;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200437 case PERF_CONTEXT_KERNEL:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300438 sym = kernel_maps__find_symbol(ip, &map);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200439 break;
440 default:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300441 sym = resolve_symbol(thread, &map, &ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200442 break;
443 }
444
Frederic Weisbecker44249612009-07-01 05:35:14 +0200445 if (sym) {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300446 if (sort__has_parent && !*parent && call__match(sym))
447 *parent = sym;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200448 if (!callchain)
449 break;
450 syms[i] = sym;
451 }
452 }
453
454 return syms;
455}
456
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200457/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200458 * collect histogram counts
459 */
460
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200461static int
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300462hist_entry__add(struct thread *thread, struct map *map,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000463 struct symbol *sym, u64 ip, struct ip_callchain *chain,
464 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300465{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300466 struct symbol **syms = NULL, *parent = NULL;
467 bool hit;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200468 struct hist_entry *he;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300469
Frederic Weisbecker44249612009-07-01 05:35:14 +0200470 if ((sort__has_parent || callchain) && chain)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300471 syms = resolve_callchain(thread, map, chain, &parent);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200472
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300473 he = __hist_entry__add(thread, map, sym, parent,
474 ip, count, level, &hit);
475 if (he == NULL)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200476 return -ENOMEM;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300477
478 if (hit)
479 he->count += count;
480
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200481 if (callchain) {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300482 if (!hit)
483 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200484 append_chain(&he->callchain, chain, syms);
485 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200486 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200487
488 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300489}
490
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000491static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300492{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200493 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200494 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300495 struct rb_node *nd;
496 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300497 unsigned int width;
498 char *col_width = col_width_list_str;
Brice Goglin9f866692009-08-10 15:26:32 +0200499 int raw_printing_style;
500
501 raw_printing_style = !strcmp(pretty_printing_style, "raw");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300502
Frederic Weisbecker25446032009-08-08 02:16:25 +0200503 init_rem_hits();
504
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300505 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +0200506 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200507
508 fprintf(fp, "# Overhead");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300509 if (show_nr_samples) {
510 if (field_sep)
511 fprintf(fp, "%cSamples", *field_sep);
512 else
513 fputs(" Samples ", fp);
514 }
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200515 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300516 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200517 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300518 if (field_sep) {
519 fprintf(fp, "%c%s", *field_sep, se->header);
520 continue;
521 }
522 width = strlen(se->header);
523 if (se->width) {
524 if (col_width_list_str) {
525 if (col_width) {
526 *se->width = atoi(col_width);
527 col_width = strchr(col_width, ',');
528 if (col_width)
529 ++col_width;
530 }
531 }
532 width = *se->width = max(*se->width, width);
533 }
534 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200535 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200536 fprintf(fp, "\n");
537
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300538 if (field_sep)
539 goto print_entries;
540
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200541 fprintf(fp, "# ........");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300542 if (show_nr_samples)
543 fprintf(fp, " ..........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200544 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200545 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200546
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300547 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200548 continue;
549
Ingo Molnar4593bba2009-06-02 15:34:25 +0200550 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300551 if (se->width)
552 width = *se->width;
553 else
554 width = strlen(se->header);
555 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200556 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200557 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200558 fprintf(fp, "\n");
559
560 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200561
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300562print_entries:
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200563 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
564 pos = rb_entry(nd, struct hist_entry, rb_node);
565 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300566 }
567
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200568 if (sort_order == default_sort_order &&
569 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200570 fprintf(fp, "#\n");
Pekka Enberg114cfab2009-08-05 13:25:21 +0300571 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200572 fprintf(fp, "#\n");
573 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200574 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200575
Frederic Weisbecker25446032009-08-08 02:16:25 +0200576 free(rem_sq_bracket);
577
Brice Goglin8d513272009-08-07 13:55:24 +0200578 if (show_threads)
Brice Goglin9f866692009-08-10 15:26:32 +0200579 perf_read_values_display(fp, &show_threads_values,
580 raw_printing_style);
Brice Goglin8d513272009-08-07 13:55:24 +0200581
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300582 return ret;
583}
584
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200585static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +0200586{
587 unsigned int chain_size;
588
Ingo Molnar75220602009-06-18 08:00:17 +0200589 chain_size = event->header.size;
590 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
591
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000592 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +0200593 return -1;
594
595 return 0;
596}
597
Ingo Molnard80d3382009-06-03 23:14:49 +0200598static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200599process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +0200600{
601 char level;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300602 struct symbol *sym = NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200603 struct thread *thread;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000604 u64 ip = event->ip.ip;
605 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +0200606 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200607 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200608 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000609 int cpumode;
Ingo Molnar75051722009-06-03 23:14:49 +0200610
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200611 thread = threads__findnew(event->ip.pid, &threads, &last_match);
612
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200613 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000614 period = *(u64 *)more_data;
615 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200616 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200617
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200618 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200619 (void *)(offset + head),
620 (void *)(long)(event->header.size),
621 event->header.misc,
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -0300622 event->ip.pid, event->ip.tid,
Peter Zijlstra4502d772009-06-10 15:03:06 +0200623 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200624 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +0200625
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200626 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200627 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200628
629 chain = (void *)more_data;
630
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200631 dump_printf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200632
Ingo Molnar75220602009-06-18 08:00:17 +0200633 if (validate_chain(chain, event) < 0) {
634 eprintf("call-chain problem with event, skipping it.\n");
635 return 0;
636 }
637
638 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200639 for (i = 0; i < chain->nr; i++)
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200640 dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200641 }
642 }
643
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200644 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200645
646 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +0200647 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200648 event->header.type);
649 return -1;
650 }
651
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300652 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
653 return 0;
654
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200655 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000656
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200657 if (cpumode == PERF_RECORD_MISC_KERNEL) {
Ingo Molnar75051722009-06-03 23:14:49 +0200658 level = 'k';
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300659 sym = kernel_maps__find_symbol(ip, &map);
660 dump_printf(" ...... dso: %s\n",
661 map ? map->dso->long_name : "<not found>");
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200662 } else if (cpumode == PERF_RECORD_MISC_USER) {
Ingo Molnar75051722009-06-03 23:14:49 +0200663 level = '.';
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300664 sym = resolve_symbol(thread, &map, &ip);
Ingo Molnar75051722009-06-03 23:14:49 +0200665
Ingo Molnar75051722009-06-03 23:14:49 +0200666 } else {
Ingo Molnar75051722009-06-03 23:14:49 +0200667 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200668 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar75051722009-06-03 23:14:49 +0200669 }
670
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300671 if (dso_list &&
672 (!map || !map->dso ||
673 !(strlist__has_entry(dso_list, map->dso->short_name) ||
674 (map->dso->short_name != map->dso->long_name &&
675 strlist__has_entry(dso_list, map->dso->long_name)))))
676 return 0;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300677
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300678 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
679 return 0;
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -0300680
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300681 if (hist_entry__add(thread, map, sym, ip,
682 chain, level, period)) {
683 eprintf("problem incrementing symbol count, skipping event\n");
684 return -1;
Ingo Molnar75051722009-06-03 23:14:49 +0200685 }
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300686
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200687 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +0200688
689 return 0;
690}
691
692static int
693process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
694{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200695 struct thread *thread;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200696 struct map *map = map__new(&event->mmap, cwd, cwdlen);
Ingo Molnar75051722009-06-03 23:14:49 +0200697
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200698 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
699
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200700 dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200701 (void *)(offset + head),
702 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200703 event->mmap.pid,
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -0300704 event->mmap.tid,
Ingo Molnar75051722009-06-03 23:14:49 +0200705 (void *)(long)event->mmap.start,
706 (void *)(long)event->mmap.len,
707 (void *)(long)event->mmap.pgoff,
708 event->mmap.filename);
709
710 if (thread == NULL || map == NULL) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200711 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +0200712 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +0200713 }
714
715 thread__insert_map(thread, map);
716 total_mmap++;
717
718 return 0;
719}
720
721static int
722process_comm_event(event_t *event, unsigned long offset, unsigned long head)
723{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200724 struct thread *thread;
725
726 thread = threads__findnew(event->comm.pid, &threads, &last_match);
Ingo Molnar75051722009-06-03 23:14:49 +0200727
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200728 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200729 (void *)(offset + head),
730 (void *)(long)(event->header.size),
731 event->comm.comm, event->comm.pid);
732
733 if (thread == NULL ||
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200734 thread__set_comm_adjust(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200735 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar75051722009-06-03 23:14:49 +0200736 return -1;
737 }
738 total_comm++;
739
740 return 0;
741}
742
743static int
Peter Zijlstra27d028d2009-07-23 16:52:41 +0200744process_task_event(event_t *event, unsigned long offset, unsigned long head)
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200745{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200746 struct thread *thread;
747 struct thread *parent;
748
749 thread = threads__findnew(event->fork.pid, &threads, &last_match);
750 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200751
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200752 dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200753 (void *)(offset + head),
754 (void *)(long)(event->header.size),
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200755 event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
Peter Zijlstra27d028d2009-07-23 16:52:41 +0200756 event->fork.pid, event->fork.tid,
757 event->fork.ppid, event->fork.ptid);
758
759 /*
760 * A thread clone will have the same PID for both
761 * parent and child.
762 */
763 if (thread == parent)
764 return 0;
765
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200766 if (event->header.type == PERF_RECORD_EXIT)
Peter Zijlstra27d028d2009-07-23 16:52:41 +0200767 return 0;
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200768
769 if (!thread || !parent || thread__fork(thread, parent)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200770 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200771 return -1;
772 }
773 total_fork++;
774
775 return 0;
776}
777
778static int
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200779process_lost_event(event_t *event, unsigned long offset, unsigned long head)
780{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200781 dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200782 (void *)(offset + head),
783 (void *)(long)(event->header.size),
784 event->lost.id,
785 event->lost.lost);
786
787 total_lost += event->lost.lost;
788
789 return 0;
790}
791
Ingo Molnarb2fef072009-06-05 18:07:51 +0200792static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200793process_read_event(event_t *event, unsigned long offset, unsigned long head)
794{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200795 struct perf_event_attr *attr;
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200796
797 attr = perf_header__find_attr(event->read.id, header);
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200798
Brice Goglin8d513272009-08-07 13:55:24 +0200799 if (show_threads) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200800 const char *name = attr ? __event_name(attr->type, attr->config)
Brice Goglin8d513272009-08-07 13:55:24 +0200801 : "unknown";
802 perf_read_values_add_value(&show_threads_values,
803 event->read.pid, event->read.tid,
804 event->read.id,
805 name,
806 event->read.value);
807 }
808
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200809 dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200810 (void *)(offset + head),
811 (void *)(long)(event->header.size),
812 event->read.pid,
813 event->read.tid,
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200814 attr ? __event_name(attr->type, attr->config)
815 : "FAIL",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200816 event->read.value);
817
818 return 0;
819}
820
821static int
Ingo Molnard80d3382009-06-03 23:14:49 +0200822process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300823{
Ingo Molnar8465b052009-06-14 14:44:07 +0200824 trace_event(event);
825
Ingo Molnar75051722009-06-03 23:14:49 +0200826 switch (event->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200827 case PERF_RECORD_SAMPLE:
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200828 return process_sample_event(event, offset, head);
829
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200830 case PERF_RECORD_MMAP:
Ingo Molnar75051722009-06-03 23:14:49 +0200831 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200832
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200833 case PERF_RECORD_COMM:
Ingo Molnar75051722009-06-03 23:14:49 +0200834 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +0200835
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200836 case PERF_RECORD_FORK:
837 case PERF_RECORD_EXIT:
Peter Zijlstra27d028d2009-07-23 16:52:41 +0200838 return process_task_event(event, offset, head);
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200839
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200840 case PERF_RECORD_LOST:
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200841 return process_lost_event(event, offset, head);
842
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200843 case PERF_RECORD_READ:
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200844 return process_read_event(event, offset, head);
845
Ingo Molnard11444d2009-06-03 23:29:14 +0200846 /*
847 * We dont process them right now but they are fine:
848 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200849
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200850 case PERF_RECORD_THROTTLE:
851 case PERF_RECORD_UNTHROTTLE:
Ingo Molnard11444d2009-06-03 23:29:14 +0200852 return 0;
853
Ingo Molnard80d3382009-06-03 23:14:49 +0200854 default:
855 return -1;
856 }
857
858 return 0;
859}
860
861static int __cmd_report(void)
862{
Ingo Molnar75051722009-06-03 23:14:49 +0200863 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +0200864 unsigned long offset = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200865 unsigned long head, shift;
Ingo Molnar83a09442009-08-15 12:26:57 +0200866 struct stat input_stat;
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200867 struct thread *idle;
Ingo Molnard80d3382009-06-03 23:14:49 +0200868 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +0200869 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +0200870 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +0200871
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200872 idle = register_idle_thread(&threads, &last_match);
873 thread__comm_adjust(idle);
Ingo Molnard80d3382009-06-03 23:14:49 +0200874
Brice Goglin8d513272009-08-07 13:55:24 +0200875 if (show_threads)
876 perf_read_values_init(&show_threads_values);
877
Ingo Molnard80d3382009-06-03 23:14:49 +0200878 input = open(input_name, O_RDONLY);
879 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +0200880 fprintf(stderr, " failed to open file: %s", input_name);
881 if (!strcmp(input_name, "perf.data"))
882 fprintf(stderr, " (try 'perf record' first)");
883 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +0200884 exit(-1);
885 }
886
Ingo Molnar83a09442009-08-15 12:26:57 +0200887 ret = fstat(input, &input_stat);
Ingo Molnard80d3382009-06-03 23:14:49 +0200888 if (ret < 0) {
889 perror("failed to stat file");
890 exit(-1);
891 }
892
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200893 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
894 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200895 exit(-1);
896 }
897
Ingo Molnar83a09442009-08-15 12:26:57 +0200898 if (!input_stat.st_size) {
Ingo Molnard80d3382009-06-03 23:14:49 +0200899 fprintf(stderr, "zero-sized file, nothing to do!\n");
900 exit(0);
901 }
902
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200903 header = perf_header__read(input);
904 head = header->data_offset;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200905
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200906 sample_type = perf_header__sample_type(header);
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200907
Frederic Weisbecker91b4eae2009-07-05 07:39:17 +0200908 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
909 if (sort__has_parent) {
910 fprintf(stderr, "selected --sort parent, but no"
911 " callchain data. Did you call"
912 " perf record without -g?\n");
913 exit(-1);
914 }
915 if (callchain) {
Frederic Weisbecker6ede59c2009-08-17 23:07:48 +0200916 fprintf(stderr, "selected -g but no callchain data."
Frederic Weisbecker91b4eae2009-07-05 07:39:17 +0200917 " Did you call perf record without"
918 " -g?\n");
919 exit(-1);
920 }
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +0200921 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
922 callchain = 1;
923 if (register_callchain_param(&callchain_param) < 0) {
924 fprintf(stderr, "Can't register callchain"
925 " params\n");
926 exit(-1);
927 }
Peter Zijlstraf5970552009-06-18 23:22:55 +0200928 }
929
Ingo Molnard80d3382009-06-03 23:14:49 +0200930 if (load_kernel() < 0) {
931 perror("failed to load kernel symbols");
932 return EXIT_FAILURE;
933 }
934
935 if (!full_paths) {
936 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
937 perror("failed to get the current directory");
938 return EXIT_FAILURE;
939 }
940 cwdlen = strlen(cwd);
941 } else {
942 cwd = NULL;
943 cwdlen = 0;
944 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200945
946 shift = page_size * (head / page_size);
947 offset += shift;
948 head -= shift;
949
Ingo Molnard80d3382009-06-03 23:14:49 +0200950remap:
951 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
952 MAP_SHARED, input, offset);
953 if (buf == MAP_FAILED) {
954 perror("failed to mmap file");
955 exit(-1);
956 }
957
958more:
959 event = (event_t *)(buf + head);
960
961 size = event->header.size;
962 if (!size)
963 size = 8;
964
965 if (head + event->header.size >= page_size * mmap_window) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200966 int munmap_ret;
Ingo Molnard80d3382009-06-03 23:14:49 +0200967
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200968 shift = page_size * (head / page_size);
969
Ingo Molnar83a09442009-08-15 12:26:57 +0200970 munmap_ret = munmap(buf, page_size * mmap_window);
971 assert(munmap_ret == 0);
Ingo Molnard80d3382009-06-03 23:14:49 +0200972
973 offset += shift;
974 head -= shift;
975 goto remap;
976 }
977
978 size = event->header.size;
979
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200980 dump_printf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +0200981 (void *)(offset + head),
982 (void *)(long)event->header.size,
983 event->header.type);
984
Ingo Molnard80d3382009-06-03 23:14:49 +0200985 if (!size || process_event(event, offset, head) < 0) {
986
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200987 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar35029732009-06-03 09:38:58 +0200988 (void *)(offset + head),
989 (void *)(long)(event->header.size),
990 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200991
Ingo Molnar3e706112009-05-26 18:53:17 +0200992 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200993
994 /*
995 * assume we lost track of the stream, check alignment, and
996 * increment a single u64 in the hope to catch on again 'soon'.
997 */
998
999 if (unlikely(head & 7))
1000 head &= ~7ULL;
1001
1002 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001003 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001004
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001005 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001006
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001007 if (offset + head >= header->data_offset + header->data_size)
Peter Zijlstraf5970552009-06-18 23:22:55 +02001008 goto done;
1009
Ingo Molnar83a09442009-08-15 12:26:57 +02001010 if (offset + head < (unsigned long)input_stat.st_size)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001011 goto more;
1012
Peter Zijlstraf5970552009-06-18 23:22:55 +02001013done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001014 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001015 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001016
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001017 dump_printf(" IP events: %10ld\n", total);
1018 dump_printf(" mmap events: %10ld\n", total_mmap);
1019 dump_printf(" comm events: %10ld\n", total_comm);
1020 dump_printf(" fork events: %10ld\n", total_fork);
1021 dump_printf(" lost events: %10ld\n", total_lost);
1022 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001023
Ingo Molnar35029732009-06-03 09:38:58 +02001024 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001025 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001026
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001027 if (verbose >= 3)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001028 threads__fprintf(stdout, &threads);
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001029
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001030 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001031 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001032
Peter Zijlstra82292892009-06-03 12:37:36 +02001033 collapse__resort();
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001034 output__resort(total);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001035 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001036
Brice Goglin8d513272009-08-07 13:55:24 +02001037 if (show_threads)
1038 perf_read_values_destroy(&show_threads_values);
1039
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001040 return rc;
1041}
1042
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001043static int
1044parse_callchain_opt(const struct option *opt __used, const char *arg,
1045 int unset __used)
1046{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001047 char *tok;
1048 char *endptr;
1049
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001050 callchain = 1;
1051
1052 if (!arg)
1053 return 0;
1054
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001055 tok = strtok((char *)arg, ",");
1056 if (!tok)
1057 return -1;
1058
1059 /* get the output mode */
1060 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001061 callchain_param.mode = CHAIN_GRAPH_ABS;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001062
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001063 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001064 callchain_param.mode = CHAIN_FLAT;
1065
1066 else if (!strncmp(tok, "fractal", strlen(arg)))
1067 callchain_param.mode = CHAIN_GRAPH_REL;
1068
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +02001069 else if (!strncmp(tok, "none", strlen(arg))) {
1070 callchain_param.mode = CHAIN_NONE;
1071 callchain = 0;
1072
1073 return 0;
1074 }
1075
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001076 else
1077 return -1;
1078
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001079 /* get the min percentage */
1080 tok = strtok(NULL, ",");
1081 if (!tok)
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001082 goto setup;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001083
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001084 callchain_param.min_percent = strtod(tok, &endptr);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001085 if (tok == endptr)
1086 return -1;
1087
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001088setup:
1089 if (register_callchain_param(&callchain_param) < 0) {
1090 fprintf(stderr, "Can't register callchain params\n");
1091 return -1;
1092 }
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001093 return 0;
1094}
1095
John Kacurdd68ada2009-09-24 18:02:49 +02001096//static const char * const report_usage[] = {
1097const char * const report_usage[] = {
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001098 "perf report [<options>] <command>",
1099 NULL
1100};
1101
1102static const struct option options[] = {
1103 OPT_STRING('i', "input", &input_name, "file",
1104 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001105 OPT_BOOLEAN('v', "verbose", &verbose,
1106 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001107 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1108 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001109 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +02001110 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Mike Galbraith42976482009-07-02 08:09:46 +02001111 OPT_BOOLEAN('m', "modules", &modules,
1112 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001113 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1114 "Show a column with the number of samples"),
Brice Goglin8d513272009-08-07 13:55:24 +02001115 OPT_BOOLEAN('T', "threads", &show_threads,
1116 "Show per-thread event counters"),
Brice Goglin9f866692009-08-10 15:26:32 +02001117 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
1118 "pretty printing style key: normal raw"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001119 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001120 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001121 OPT_BOOLEAN('P', "full-paths", &full_paths,
1122 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001123 OPT_STRING('p', "parent", &parent_pattern, "regex",
1124 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001125 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1126 "Only display entries with parent-match"),
Anton Blanchard1483b192009-07-16 15:44:29 +02001127 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001128 "Display callchains using output_type and min percent threshold. "
Anton Blanchard1483b192009-07-16 15:44:29 +02001129 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001130 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1131 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001132 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1133 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001134 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1135 "only consider these symbols"),
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001136 OPT_STRING('w', "column-widths", &col_width_list_str,
1137 "width[,width...]",
1138 "don't try to adjust column width, use these fixed values"),
1139 OPT_STRING('t', "field-separator", &field_sep, "separator",
1140 "separator for columns, no spaces will be added between "
1141 "columns '.' is reserved."),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001142 OPT_END()
1143};
1144
Ingo Molnar5352f352009-06-03 10:07:39 +02001145static void setup_sorting(void)
1146{
1147 char *tmp, *tok, *str = strdup(sort_order);
1148
1149 for (tok = strtok_r(str, ", ", &tmp);
1150 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1151 if (sort_dimension__add(tok) < 0) {
1152 error("Unknown --sort key: `%s'", tok);
1153 usage_with_options(report_usage, options);
1154 }
1155 }
1156
1157 free(str);
1158}
1159
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001160static void setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001161 struct sort_entry *se, const char *list_name,
1162 FILE *fp)
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001163{
1164 if (list_str) {
1165 *list = strlist__new(true, list_str);
1166 if (!*list) {
1167 fprintf(stderr, "problems parsing %s list\n",
1168 list_name);
1169 exit(129);
1170 }
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001171 if (strlist__nr_entries(*list) == 1) {
1172 fprintf(fp, "# %s: %s\n", list_name,
1173 strlist__entry(*list, 0)->s);
1174 se->elide = true;
1175 }
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001176 }
1177}
1178
Ingo Molnarf37a2912009-07-01 12:37:06 +02001179int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001180{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001181 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001182
1183 page_size = getpagesize();
1184
Ingo Molnaredc52de2009-06-04 16:24:37 +02001185 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001186
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001187 setup_sorting();
1188
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001189 if (parent_pattern != default_parent_pattern) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001190 sort_dimension__add("parent");
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001191 sort_parent.elide = 1;
1192 } else
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001193 exclude_other = 0;
1194
Ingo Molnaredc52de2009-06-04 16:24:37 +02001195 /*
1196 * Any (unrecognized) arguments left?
1197 */
1198 if (argc)
1199 usage_with_options(report_usage, options);
1200
Ingo Molnara930d2c2009-05-27 09:50:13 +02001201 setup_pager();
1202
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001203 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
1204 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
1205 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001206
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001207 if (field_sep && *field_sep == '.') {
1208 fputs("'.' is the only non valid --field-separator argument\n",
1209 stderr);
1210 exit(129);
1211 }
1212
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001213 return __cmd_report();
1214}