blob: 0af48401f089f0f27720e4aae7911b42c1415094 [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 Weisbecker016e92f2009-10-07 12:47:31 +020029#include "util/data_map.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020030#include "util/thread.h"
John Kacurdd68ada2009-09-24 18:02:49 +020031#include "util/sort.h"
John Kacur3d1d07e2009-09-28 15:32:55 +020032#include "util/hist.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020033
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020034static char const *input_name = "perf.data";
Ingo Molnarbd741372009-06-04 14:13:04 +020035
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030036static char *dso_list_str, *comm_list_str, *sym_list_str,
37 *col_width_list_str;
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -030038static struct strlist *dso_list, *comm_list, *sym_list;
Ingo Molnarbd741372009-06-04 14:13:04 +020039
Peter Zijlstrafa6963b2009-08-19 11:18:26 +020040static int force;
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -020041static bool use_modules;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030042
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030043static int full_paths;
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -030044static int show_nr_samples;
Ingo Molnar97b07b62009-05-26 18:48:58 +020045
Brice Goglin8d513272009-08-07 13:55:24 +020046static int show_threads;
47static struct perf_read_values show_threads_values;
48
Brice Goglin9f866692009-08-10 15:26:32 +020049static char default_pretty_printing_style[] = "normal";
50static char *pretty_printing_style = default_pretty_printing_style;
51
Ingo Molnarb8e6d822009-06-18 14:32:19 +020052static int exclude_other = 1;
Frederic Weisbeckerbe903882009-07-05 07:39:18 +020053
Frederic Weisbecker805d1272009-07-05 07:39:21 +020054static char callchain_default_opt[] = "fractal,0.5";
55
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020056static char *cwd;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020057static int cwdlen;
58
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +020059static struct perf_header *header;
60
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020061static u64 sample_type;
62
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020063
64static size_t
65callchain__fprintf_left_margin(FILE *fp, int left_margin)
66{
67 int i;
68 int ret;
69
70 ret = fprintf(fp, " ");
71
72 for (i = 0; i < left_margin; i++)
73 ret += fprintf(fp, " ");
74
75 return ret;
76}
77
78static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
79 int left_margin)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020080{
81 int i;
82 size_t ret = 0;
83
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020084 ret += callchain__fprintf_left_margin(fp, left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020085
86 for (i = 0; i < depth; i++)
87 if (depth_mask & (1 << i))
88 ret += fprintf(fp, "| ");
89 else
90 ret += fprintf(fp, " ");
91
92 ret += fprintf(fp, "\n");
93
94 return ret;
95}
Peter Zijlstra1aa16732009-05-27 20:20:25 +020096static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020097ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
98 int depth_mask, int count, u64 total_samples,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020099 int hits, int left_margin)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200100{
101 int i;
102 size_t ret = 0;
103
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200104 ret += callchain__fprintf_left_margin(fp, left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200105 for (i = 0; i < depth; i++) {
106 if (depth_mask & (1 << i))
107 ret += fprintf(fp, "|");
108 else
109 ret += fprintf(fp, " ");
110 if (!count && i == depth - 1) {
111 double percent;
112
113 percent = hits * 100.0 / total_samples;
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200114 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200115 } else
116 ret += fprintf(fp, "%s", " ");
117 }
118 if (chain->sym)
119 ret += fprintf(fp, "%s\n", chain->sym->name);
120 else
121 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
122
123 return ret;
124}
125
Frederic Weisbecker25446032009-08-08 02:16:25 +0200126static struct symbol *rem_sq_bracket;
127static struct callchain_list rem_hits;
128
129static void init_rem_hits(void)
130{
131 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
132 if (!rem_sq_bracket) {
133 fprintf(stderr, "Not enough memory to display remaining hits\n");
134 return;
135 }
136
137 strcpy(rem_sq_bracket->name, "[...]");
138 rem_hits.sym = rem_sq_bracket;
139}
140
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200141static size_t
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200142__callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200143 u64 total_samples, int depth, int depth_mask,
144 int left_margin)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200145{
146 struct rb_node *node, *next;
147 struct callchain_node *child;
148 struct callchain_list *chain;
149 int new_depth_mask = depth_mask;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200150 u64 new_total;
Frederic Weisbecker25446032009-08-08 02:16:25 +0200151 u64 remaining;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200152 size_t ret = 0;
153 int i;
154
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200155 if (callchain_param.mode == CHAIN_GRAPH_REL)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200156 new_total = self->children_hit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200157 else
158 new_total = total_samples;
159
Frederic Weisbecker25446032009-08-08 02:16:25 +0200160 remaining = new_total;
161
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200162 node = rb_first(&self->rb_root);
163 while (node) {
Frederic Weisbecker25446032009-08-08 02:16:25 +0200164 u64 cumul;
165
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200166 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200167 cumul = cumul_hits(child);
168 remaining -= cumul;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200169
170 /*
171 * The depth mask manages the output of pipes that show
172 * the depth. We don't want to keep the pipes of the current
Frederic Weisbecker25446032009-08-08 02:16:25 +0200173 * level for the last child of this depth.
174 * Except if we have remaining filtered hits. They will
175 * supersede the last child
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200176 */
177 next = rb_next(node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200178 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200179 new_depth_mask &= ~(1 << (depth - 1));
180
181 /*
182 * But we keep the older depth mask for the line seperator
183 * to keep the level link until we reach the last child
184 */
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200185 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
186 left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200187 i = 0;
188 list_for_each_entry(chain, &child->val, list) {
189 if (chain->ip >= PERF_CONTEXT_MAX)
190 continue;
191 ret += ipchain__fprintf_graph(fp, chain, depth,
192 new_depth_mask, i++,
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200193 new_total,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200194 cumul,
195 left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200196 }
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200197 ret += __callchain__fprintf_graph(fp, child, new_total,
198 depth + 1,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200199 new_depth_mask | (1 << depth),
200 left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200201 node = next;
202 }
203
Frederic Weisbecker25446032009-08-08 02:16:25 +0200204 if (callchain_param.mode == CHAIN_GRAPH_REL &&
205 remaining && remaining != new_total) {
206
207 if (!rem_sq_bracket)
208 return ret;
209
210 new_depth_mask &= ~(1 << (depth - 1));
211
212 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
213 new_depth_mask, 0, new_total,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200214 remaining, left_margin);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200215 }
216
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200217 return ret;
218}
219
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200220
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200221static size_t
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200222callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200223 u64 total_samples, int left_margin)
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200224{
225 struct callchain_list *chain;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200226 bool printed = false;
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200227 int i = 0;
228 int ret = 0;
229
230 list_for_each_entry(chain, &self->val, list) {
231 if (chain->ip >= PERF_CONTEXT_MAX)
232 continue;
233
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200234 if (!i++ && sort__first_dimension == SORT_SYM)
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200235 continue;
236
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200237 if (!printed) {
238 ret += callchain__fprintf_left_margin(fp, left_margin);
239 ret += fprintf(fp, "|\n");
240 ret += callchain__fprintf_left_margin(fp, left_margin);
241 ret += fprintf(fp, "---");
242
243 left_margin += 3;
244 printed = true;
245 } else
246 ret += callchain__fprintf_left_margin(fp, left_margin);
247
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200248 if (chain->sym)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200249 ret += fprintf(fp, " %s\n", chain->sym->name);
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200250 else
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200251 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200252 }
253
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200254 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200255
256 return ret;
257}
258
259static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200260callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
261 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200262{
263 struct callchain_list *chain;
264 size_t ret = 0;
265
266 if (!self)
267 return 0;
268
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200269 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200270
271
Frederic Weisbecker44249612009-07-01 05:35:14 +0200272 list_for_each_entry(chain, &self->val, list) {
273 if (chain->ip >= PERF_CONTEXT_MAX)
274 continue;
275 if (chain->sym)
276 ret += fprintf(fp, " %s\n", chain->sym->name);
277 else
278 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200279 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200280 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200281
282 return ret;
283}
284
285static size_t
286hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200287 u64 total_samples, int left_margin)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200288{
289 struct rb_node *rb_node;
290 struct callchain_node *chain;
291 size_t ret = 0;
292
293 rb_node = rb_first(&self->sorted_chain);
294 while (rb_node) {
295 double percent;
296
297 chain = rb_entry(rb_node, struct callchain_node, rb_node);
298 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200299 switch (callchain_param.mode) {
300 case CHAIN_FLAT:
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200301 ret += percent_color_fprintf(fp, " %6.2f%%\n",
302 percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200303 ret += callchain__fprintf_flat(fp, chain, total_samples);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200304 break;
305 case CHAIN_GRAPH_ABS: /* Falldown */
306 case CHAIN_GRAPH_REL:
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200307 ret += callchain__fprintf_graph(fp, chain, total_samples,
308 left_margin);
Ingo Molnar83a09442009-08-15 12:26:57 +0200309 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200310 default:
311 break;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200312 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200313 ret += fprintf(fp, "\n");
314 rb_node = rb_next(rb_node);
315 }
316
317 return ret;
318}
319
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200320static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000321hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200322{
323 struct sort_entry *se;
324 size_t ret;
325
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200326 if (exclude_other && !self->parent)
327 return 0;
328
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200329 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300330 ret = percent_color_fprintf(fp,
331 field_sep ? "%.2f" : " %6.2f%%",
332 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200333 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300334 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200335
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300336 if (show_nr_samples) {
337 if (field_sep)
338 fprintf(fp, "%c%lld", *field_sep, self->count);
339 else
340 fprintf(fp, "%11lld", self->count);
341 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200342
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200343 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300344 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200345 continue;
346
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300347 fprintf(fp, "%s", field_sep ?: " ");
348 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200349 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200350
351 ret += fprintf(fp, "\n");
352
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200353 if (callchain) {
354 int left_margin = 0;
355
356 if (sort__first_dimension == SORT_COMM) {
357 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
358 list);
359 left_margin = se->width ? *se->width : 0;
360 left_margin -= thread__comm_len(self->thread);
361 }
362
363 hist_entry_callchain__fprintf(fp, self, total_samples,
364 left_margin);
365 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200366
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200367 return ret;
368}
369
370/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200371 *
372 */
373
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300374static void dso__calc_col_width(struct dso *self)
375{
376 if (!col_width_list_str && !field_sep &&
377 (!dso_list || strlist__has_entry(dso_list, self->name))) {
378 unsigned int slen = strlen(self->name);
379 if (slen > dsos__col_width)
380 dsos__col_width = slen;
381 }
382
383 self->slen_calculated = 1;
384}
385
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200386static void thread__comm_adjust(struct thread *self)
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200387{
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200388 char *comm = self->comm;
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200389
390 if (!col_width_list_str && !field_sep &&
391 (!comm_list || strlist__has_entry(comm_list, comm))) {
392 unsigned int slen = strlen(comm);
393
394 if (slen > comms__col_width) {
395 comms__col_width = slen;
396 threads__col_width = slen + 6;
397 }
398 }
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200399}
400
401static int thread__set_comm_adjust(struct thread *self, const char *comm)
402{
403 int ret = thread__set_comm(self, comm);
404
405 if (ret)
406 return ret;
407
408 thread__comm_adjust(self);
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200409
410 return 0;
411}
412
413
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200414static struct symbol *
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300415resolve_symbol(struct thread *thread, struct map **mapp, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200416{
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200417 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200418 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200419
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200420 if (map)
421 goto got_map;
422
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300423 if (!thread)
424 return NULL;
425
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200426 map = thread__find_map(thread, ip);
427 if (map != NULL) {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300428 /*
429 * We have to do this here as we may have a dso
430 * with no symbol hit that has a name longer than
431 * the ones with symbols sampled.
432 */
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300433 if (!sort_dso.elide && !map->dso->slen_calculated)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300434 dso__calc_col_width(map->dso);
435
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200436 if (mapp)
437 *mapp = map;
438got_map:
439 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200440 } else {
441 /*
442 * If this is outside of all known maps,
443 * and is a negative address, try to look it
444 * up in the kernel dso, as it might be a
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300445 * vsyscall or vdso (which executes in user-mode).
446 *
447 * XXX This is nasty, we should have a symbol list in
448 * the "[vdso]" dso, but for now lets use the old
449 * trick of looking in the whole kernel symbol list.
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200450 */
Arnaldo Carvalho de Meloc3b32fc2009-10-05 14:26:16 -0300451 if ((long long)ip < 0)
452 return kernel_maps__find_symbol(ip, mapp);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200453 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300454 dump_printf(" ...... dso: %s\n",
455 map ? map->dso->long_name : "<not found>");
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200456 dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200457 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200458
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200459 return map ? map__find_symbol(map, ip, NULL) : NULL;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200460}
461
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200462static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200463{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200464 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200465 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200466
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200467 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200468}
469
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300470static struct symbol **resolve_callchain(struct thread *thread, struct map *map,
471 struct ip_callchain *chain,
472 struct symbol **parent)
Frederic Weisbecker44249612009-07-01 05:35:14 +0200473{
Frederic Weisbecker44249612009-07-01 05:35:14 +0200474 u64 context = PERF_CONTEXT_MAX;
Ingo Molnar029e5b12009-07-03 13:17:28 +0200475 struct symbol **syms = NULL;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200476 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200477
478 if (callchain) {
479 syms = calloc(chain->nr, sizeof(*syms));
480 if (!syms) {
481 fprintf(stderr, "Can't allocate memory for symbols\n");
482 exit(-1);
483 }
484 }
485
486 for (i = 0; i < chain->nr; i++) {
487 u64 ip = chain->ips[i];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300488 struct symbol *sym = NULL;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200489
490 if (ip >= PERF_CONTEXT_MAX) {
491 context = ip;
492 continue;
493 }
494
495 switch (context) {
Ingo Molnar88a69df2009-07-01 11:17:20 +0200496 case PERF_CONTEXT_HV:
Ingo Molnar88a69df2009-07-01 11:17:20 +0200497 break;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200498 case PERF_CONTEXT_KERNEL:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300499 sym = kernel_maps__find_symbol(ip, &map);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200500 break;
501 default:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300502 sym = resolve_symbol(thread, &map, &ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200503 break;
504 }
505
Frederic Weisbecker44249612009-07-01 05:35:14 +0200506 if (sym) {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300507 if (sort__has_parent && !*parent && call__match(sym))
508 *parent = sym;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200509 if (!callchain)
510 break;
511 syms[i] = sym;
512 }
513 }
514
515 return syms;
516}
517
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200518/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200519 * collect histogram counts
520 */
521
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200522static int
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300523hist_entry__add(struct thread *thread, struct map *map,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000524 struct symbol *sym, u64 ip, struct ip_callchain *chain,
525 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300526{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300527 struct symbol **syms = NULL, *parent = NULL;
528 bool hit;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200529 struct hist_entry *he;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300530
Frederic Weisbecker44249612009-07-01 05:35:14 +0200531 if ((sort__has_parent || callchain) && chain)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300532 syms = resolve_callchain(thread, map, chain, &parent);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200533
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300534 he = __hist_entry__add(thread, map, sym, parent,
535 ip, count, level, &hit);
536 if (he == NULL)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200537 return -ENOMEM;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300538
539 if (hit)
540 he->count += count;
541
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200542 if (callchain) {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300543 if (!hit)
544 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200545 append_chain(&he->callchain, chain, syms);
546 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200547 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200548
549 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300550}
551
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000552static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300553{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200554 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200555 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300556 struct rb_node *nd;
557 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300558 unsigned int width;
559 char *col_width = col_width_list_str;
Brice Goglin9f866692009-08-10 15:26:32 +0200560 int raw_printing_style;
561
562 raw_printing_style = !strcmp(pretty_printing_style, "raw");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300563
Frederic Weisbecker25446032009-08-08 02:16:25 +0200564 init_rem_hits();
565
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300566 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +0200567 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200568
569 fprintf(fp, "# Overhead");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300570 if (show_nr_samples) {
571 if (field_sep)
572 fprintf(fp, "%cSamples", *field_sep);
573 else
574 fputs(" Samples ", fp);
575 }
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200576 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300577 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200578 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300579 if (field_sep) {
580 fprintf(fp, "%c%s", *field_sep, se->header);
581 continue;
582 }
583 width = strlen(se->header);
584 if (se->width) {
585 if (col_width_list_str) {
586 if (col_width) {
587 *se->width = atoi(col_width);
588 col_width = strchr(col_width, ',');
589 if (col_width)
590 ++col_width;
591 }
592 }
593 width = *se->width = max(*se->width, width);
594 }
595 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200596 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200597 fprintf(fp, "\n");
598
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300599 if (field_sep)
600 goto print_entries;
601
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200602 fprintf(fp, "# ........");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300603 if (show_nr_samples)
604 fprintf(fp, " ..........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200605 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200606 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200607
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300608 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200609 continue;
610
Ingo Molnar4593bba2009-06-02 15:34:25 +0200611 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300612 if (se->width)
613 width = *se->width;
614 else
615 width = strlen(se->header);
616 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200617 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200618 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200619 fprintf(fp, "\n");
620
621 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200622
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300623print_entries:
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200624 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
625 pos = rb_entry(nd, struct hist_entry, rb_node);
626 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300627 }
628
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200629 if (sort_order == default_sort_order &&
630 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200631 fprintf(fp, "#\n");
Pekka Enberg114cfab2009-08-05 13:25:21 +0300632 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200633 fprintf(fp, "#\n");
634 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200635 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200636
Frederic Weisbecker25446032009-08-08 02:16:25 +0200637 free(rem_sq_bracket);
638
Brice Goglin8d513272009-08-07 13:55:24 +0200639 if (show_threads)
Brice Goglin9f866692009-08-10 15:26:32 +0200640 perf_read_values_display(fp, &show_threads_values,
641 raw_printing_style);
Brice Goglin8d513272009-08-07 13:55:24 +0200642
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300643 return ret;
644}
645
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200646static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +0200647{
648 unsigned int chain_size;
649
Ingo Molnar75220602009-06-18 08:00:17 +0200650 chain_size = event->header.size;
651 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
652
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000653 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +0200654 return -1;
655
656 return 0;
657}
658
Ingo Molnard80d3382009-06-03 23:14:49 +0200659static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200660process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +0200661{
662 char level;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300663 struct symbol *sym = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000664 u64 ip = event->ip.ip;
665 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +0200666 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200667 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200668 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000669 int cpumode;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300670 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200671
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200672 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000673 period = *(u64 *)more_data;
674 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200675 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200676
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200677 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200678 (void *)(offset + head),
679 (void *)(long)(event->header.size),
680 event->header.misc,
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -0300681 event->ip.pid, event->ip.tid,
Peter Zijlstra4502d772009-06-10 15:03:06 +0200682 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200683 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +0200684
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200685 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200686 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200687
688 chain = (void *)more_data;
689
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200690 dump_printf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200691
Ingo Molnar75220602009-06-18 08:00:17 +0200692 if (validate_chain(chain, event) < 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200693 pr_debug("call-chain problem with event, "
694 "skipping it.\n");
Ingo Molnar75220602009-06-18 08:00:17 +0200695 return 0;
696 }
697
698 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200699 for (i = 0; i < chain->nr; i++)
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200700 dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200701 }
702 }
703
Ingo Molnar75051722009-06-03 23:14:49 +0200704 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200705 pr_debug("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200706 event->header.type);
707 return -1;
708 }
709
Julia Lawallf39cdf22009-10-17 08:43:17 +0200710 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
711
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300712 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
713 return 0;
714
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200715 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000716
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200717 if (cpumode == PERF_RECORD_MISC_KERNEL) {
Ingo Molnar75051722009-06-03 23:14:49 +0200718 level = 'k';
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300719 sym = kernel_maps__find_symbol(ip, &map);
720 dump_printf(" ...... dso: %s\n",
721 map ? map->dso->long_name : "<not found>");
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200722 } else if (cpumode == PERF_RECORD_MISC_USER) {
Ingo Molnar75051722009-06-03 23:14:49 +0200723 level = '.';
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300724 sym = resolve_symbol(thread, &map, &ip);
Ingo Molnar75051722009-06-03 23:14:49 +0200725
Ingo Molnar75051722009-06-03 23:14:49 +0200726 } else {
Ingo Molnar75051722009-06-03 23:14:49 +0200727 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200728 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar75051722009-06-03 23:14:49 +0200729 }
730
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300731 if (dso_list &&
732 (!map || !map->dso ||
733 !(strlist__has_entry(dso_list, map->dso->short_name) ||
734 (map->dso->short_name != map->dso->long_name &&
735 strlist__has_entry(dso_list, map->dso->long_name)))))
736 return 0;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300737
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300738 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
739 return 0;
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -0300740
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300741 if (hist_entry__add(thread, map, sym, ip,
742 chain, level, period)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200743 pr_debug("problem incrementing symbol count, skipping event\n");
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300744 return -1;
Ingo Molnar75051722009-06-03 23:14:49 +0200745 }
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300746
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200747 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +0200748
749 return 0;
750}
751
752static int
753process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
754{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200755 struct map *map = map__new(&event->mmap, cwd, cwdlen);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300756 struct thread *thread = threads__findnew(event->mmap.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200757
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200758 dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200759 (void *)(offset + head),
760 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200761 event->mmap.pid,
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -0300762 event->mmap.tid,
Ingo Molnar75051722009-06-03 23:14:49 +0200763 (void *)(long)event->mmap.start,
764 (void *)(long)event->mmap.len,
765 (void *)(long)event->mmap.pgoff,
766 event->mmap.filename);
767
768 if (thread == NULL || map == NULL) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200769 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +0200770 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +0200771 }
772
773 thread__insert_map(thread, map);
774 total_mmap++;
775
776 return 0;
777}
778
779static int
780process_comm_event(event_t *event, unsigned long offset, unsigned long head)
781{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300782 struct thread *thread = threads__findnew(event->comm.pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200783
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200784 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200785 (void *)(offset + head),
786 (void *)(long)(event->header.size),
787 event->comm.comm, event->comm.pid);
788
789 if (thread == NULL ||
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200790 thread__set_comm_adjust(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200791 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar75051722009-06-03 23:14:49 +0200792 return -1;
793 }
794 total_comm++;
795
796 return 0;
797}
798
799static int
Peter Zijlstra27d028d2009-07-23 16:52:41 +0200800process_task_event(event_t *event, unsigned long offset, unsigned long head)
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200801{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300802 struct thread *thread = threads__findnew(event->fork.pid);
803 struct thread *parent = threads__findnew(event->fork.ppid);
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200804
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200805 dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200806 (void *)(offset + head),
807 (void *)(long)(event->header.size),
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200808 event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
Peter Zijlstra27d028d2009-07-23 16:52:41 +0200809 event->fork.pid, event->fork.tid,
810 event->fork.ppid, event->fork.ptid);
811
812 /*
813 * A thread clone will have the same PID for both
814 * parent and child.
815 */
816 if (thread == parent)
817 return 0;
818
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200819 if (event->header.type == PERF_RECORD_EXIT)
Peter Zijlstra27d028d2009-07-23 16:52:41 +0200820 return 0;
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200821
822 if (!thread || !parent || thread__fork(thread, parent)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200823 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200824 return -1;
825 }
826 total_fork++;
827
828 return 0;
829}
830
831static int
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200832process_lost_event(event_t *event, unsigned long offset, unsigned long head)
833{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200834 dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200835 (void *)(offset + head),
836 (void *)(long)(event->header.size),
837 event->lost.id,
838 event->lost.lost);
839
840 total_lost += event->lost.lost;
841
842 return 0;
843}
844
Ingo Molnarb2fef072009-06-05 18:07:51 +0200845static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200846process_read_event(event_t *event, unsigned long offset, unsigned long head)
847{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200848 struct perf_event_attr *attr;
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200849
850 attr = perf_header__find_attr(event->read.id, header);
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200851
Brice Goglin8d513272009-08-07 13:55:24 +0200852 if (show_threads) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200853 const char *name = attr ? __event_name(attr->type, attr->config)
Brice Goglin8d513272009-08-07 13:55:24 +0200854 : "unknown";
855 perf_read_values_add_value(&show_threads_values,
856 event->read.pid, event->read.tid,
857 event->read.id,
858 name,
859 event->read.value);
860 }
861
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200862 dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200863 (void *)(offset + head),
864 (void *)(long)(event->header.size),
865 event->read.pid,
866 event->read.tid,
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200867 attr ? __event_name(attr->type, attr->config)
868 : "FAIL",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200869 event->read.value);
870
871 return 0;
872}
873
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200874static int sample_type_check(u64 type)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300875{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200876 sample_type = type;
Ingo Molnar8465b052009-06-14 14:44:07 +0200877
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200878 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
879 if (sort__has_parent) {
880 fprintf(stderr, "selected --sort parent, but no"
881 " callchain data. Did you call"
882 " perf record without -g?\n");
883 return -1;
884 }
885 if (callchain) {
886 fprintf(stderr, "selected -g but no callchain data."
887 " Did you call perf record without"
888 " -g?\n");
889 return -1;
890 }
891 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
892 callchain = 1;
893 if (register_callchain_param(&callchain_param) < 0) {
894 fprintf(stderr, "Can't register callchain"
895 " params\n");
896 return -1;
897 }
Ingo Molnard80d3382009-06-03 23:14:49 +0200898 }
899
900 return 0;
901}
902
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200903static struct perf_file_handler file_handler = {
904 .process_sample_event = process_sample_event,
905 .process_mmap_event = process_mmap_event,
906 .process_comm_event = process_comm_event,
907 .process_exit_event = process_task_event,
908 .process_fork_event = process_task_event,
909 .process_lost_event = process_lost_event,
910 .process_read_event = process_read_event,
911 .sample_type_check = sample_type_check,
912};
913
914
Ingo Molnard80d3382009-06-03 23:14:49 +0200915static int __cmd_report(void)
916{
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200917 struct thread *idle;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200918 int ret;
Ingo Molnard80d3382009-06-03 23:14:49 +0200919
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300920 idle = register_idle_thread();
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200921 thread__comm_adjust(idle);
Ingo Molnard80d3382009-06-03 23:14:49 +0200922
Brice Goglin8d513272009-08-07 13:55:24 +0200923 if (show_threads)
924 perf_read_values_init(&show_threads_values);
925
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200926 register_perf_file_handler(&file_handler);
Ingo Molnard80d3382009-06-03 23:14:49 +0200927
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200928 ret = mmap_dispatch_perf_file(&header, input_name, force, full_paths,
929 &cwdlen, &cwd);
930 if (ret)
931 return ret;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200932
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200933 dump_printf(" IP events: %10ld\n", total);
934 dump_printf(" mmap events: %10ld\n", total_mmap);
935 dump_printf(" comm events: %10ld\n", total_comm);
936 dump_printf(" fork events: %10ld\n", total_fork);
937 dump_printf(" lost events: %10ld\n", total_lost);
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200938 dump_printf(" unknown events: %10ld\n", file_handler.total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200939
Ingo Molnar35029732009-06-03 09:38:58 +0200940 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +0200941 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200942
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300943 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300944 threads__fprintf(stdout);
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300945
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300946 if (verbose > 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +0200947 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200948
Peter Zijlstra82292892009-06-03 12:37:36 +0200949 collapse__resort();
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200950 output__resort(total);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200951 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300952
Brice Goglin8d513272009-08-07 13:55:24 +0200953 if (show_threads)
954 perf_read_values_destroy(&show_threads_values);
955
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200956 return ret;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300957}
958
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200959static int
960parse_callchain_opt(const struct option *opt __used, const char *arg,
961 int unset __used)
962{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200963 char *tok;
964 char *endptr;
965
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200966 callchain = 1;
967
968 if (!arg)
969 return 0;
970
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200971 tok = strtok((char *)arg, ",");
972 if (!tok)
973 return -1;
974
975 /* get the output mode */
976 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200977 callchain_param.mode = CHAIN_GRAPH_ABS;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200978
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200979 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200980 callchain_param.mode = CHAIN_FLAT;
981
982 else if (!strncmp(tok, "fractal", strlen(arg)))
983 callchain_param.mode = CHAIN_GRAPH_REL;
984
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +0200985 else if (!strncmp(tok, "none", strlen(arg))) {
986 callchain_param.mode = CHAIN_NONE;
987 callchain = 0;
988
989 return 0;
990 }
991
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200992 else
993 return -1;
994
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200995 /* get the min percentage */
996 tok = strtok(NULL, ",");
997 if (!tok)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200998 goto setup;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200999
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001000 callchain_param.min_percent = strtod(tok, &endptr);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001001 if (tok == endptr)
1002 return -1;
1003
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001004setup:
1005 if (register_callchain_param(&callchain_param) < 0) {
1006 fprintf(stderr, "Can't register callchain params\n");
1007 return -1;
1008 }
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001009 return 0;
1010}
1011
John Kacurdd68ada2009-09-24 18:02:49 +02001012//static const char * const report_usage[] = {
1013const char * const report_usage[] = {
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001014 "perf report [<options>] <command>",
1015 NULL
1016};
1017
1018static const struct option options[] = {
1019 OPT_STRING('i', "input", &input_name, "file",
1020 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001021 OPT_BOOLEAN('v', "verbose", &verbose,
1022 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001023 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1024 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001025 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +02001026 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001027 OPT_BOOLEAN('m', "modules", &use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +02001028 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001029 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1030 "Show a column with the number of samples"),
Brice Goglin8d513272009-08-07 13:55:24 +02001031 OPT_BOOLEAN('T', "threads", &show_threads,
1032 "Show per-thread event counters"),
Brice Goglin9f866692009-08-10 15:26:32 +02001033 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
1034 "pretty printing style key: normal raw"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001035 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001036 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001037 OPT_BOOLEAN('P', "full-paths", &full_paths,
1038 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001039 OPT_STRING('p', "parent", &parent_pattern, "regex",
1040 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001041 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1042 "Only display entries with parent-match"),
Anton Blanchard1483b192009-07-16 15:44:29 +02001043 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001044 "Display callchains using output_type and min percent threshold. "
Anton Blanchard1483b192009-07-16 15:44:29 +02001045 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001046 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1047 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001048 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1049 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001050 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1051 "only consider these symbols"),
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001052 OPT_STRING('w', "column-widths", &col_width_list_str,
1053 "width[,width...]",
1054 "don't try to adjust column width, use these fixed values"),
1055 OPT_STRING('t', "field-separator", &field_sep, "separator",
1056 "separator for columns, no spaces will be added between "
1057 "columns '.' is reserved."),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001058 OPT_END()
1059};
1060
Ingo Molnar5352f352009-06-03 10:07:39 +02001061static void setup_sorting(void)
1062{
1063 char *tmp, *tok, *str = strdup(sort_order);
1064
1065 for (tok = strtok_r(str, ", ", &tmp);
1066 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1067 if (sort_dimension__add(tok) < 0) {
1068 error("Unknown --sort key: `%s'", tok);
1069 usage_with_options(report_usage, options);
1070 }
1071 }
1072
1073 free(str);
1074}
1075
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001076static void setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001077 struct sort_entry *se, const char *list_name,
1078 FILE *fp)
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001079{
1080 if (list_str) {
1081 *list = strlist__new(true, list_str);
1082 if (!*list) {
1083 fprintf(stderr, "problems parsing %s list\n",
1084 list_name);
1085 exit(129);
1086 }
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001087 if (strlist__nr_entries(*list) == 1) {
1088 fprintf(fp, "# %s: %s\n", list_name,
1089 strlist__entry(*list, 0)->s);
1090 se->elide = true;
1091 }
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001092 }
1093}
1094
Ingo Molnarf37a2912009-07-01 12:37:06 +02001095int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001096{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001097 symbol__init(0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001098
Ingo Molnaredc52de2009-06-04 16:24:37 +02001099 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001100
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001101 setup_sorting();
1102
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001103 if (parent_pattern != default_parent_pattern) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001104 sort_dimension__add("parent");
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001105 sort_parent.elide = 1;
1106 } else
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001107 exclude_other = 0;
1108
Ingo Molnaredc52de2009-06-04 16:24:37 +02001109 /*
1110 * Any (unrecognized) arguments left?
1111 */
1112 if (argc)
1113 usage_with_options(report_usage, options);
1114
Ingo Molnara930d2c2009-05-27 09:50:13 +02001115 setup_pager();
1116
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001117 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
1118 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
1119 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001120
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001121 if (field_sep && *field_sep == '.') {
1122 fputs("'.' is the only non valid --field-separator argument\n",
1123 stderr);
1124 exit(129);
1125 }
1126
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001127 return __cmd_report();
1128}