blob: 383c4ab4f9af06e6cfaa0e6f6e358aa63e1d1d68 [file] [log] [blame]
Ingo Molnarbf9e1872009-06-02 23:37:05 +02001/*
2 * builtin-report.c
3 *
4 * Builtin report command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
Ingo Molnar16f762a2009-05-27 09:10:38 +02008#include "builtin.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02009
Ingo Molnarbf9e1872009-06-02 23:37:05 +020010#include "util/util.h"
11
Ingo Molnar8fc03212009-06-04 15:19:47 +020012#include "util/color.h"
Arnaldo Carvalho de Melo5da50252009-07-01 14:46:08 -030013#include <linux/list.h>
Ingo Molnara930d2c2009-05-27 09:50:13 +020014#include "util/cache.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030015#include <linux/rbtree.h>
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030016#include "util/symbol.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030017#include "util/string.h"
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020018#include "util/callchain.h"
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -030019#include "util/strlist.h"
Brice Goglin8d513272009-08-07 13:55:24 +020020#include "util/values.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030021
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020022#include "perf.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020023#include "util/debug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020024#include "util/header.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020025
26#include "util/parse-options.h"
27#include "util/parse-events.h"
28
Frederic 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 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
Ingo Molnarb8e6d822009-06-18 14:32:19 +020051static int exclude_other = 1;
Frederic Weisbeckerbe903882009-07-05 07:39:18 +020052
Frederic Weisbecker805d1272009-07-05 07:39:21 +020053static char callchain_default_opt[] = "fractal,0.5";
54
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +020055static struct perf_header *header;
56
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020057static u64 sample_type;
58
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020059struct symbol_conf symbol_conf;
60
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020061
62static size_t
63callchain__fprintf_left_margin(FILE *fp, int left_margin)
64{
65 int i;
66 int ret;
67
68 ret = fprintf(fp, " ");
69
70 for (i = 0; i < left_margin; i++)
71 ret += fprintf(fp, " ");
72
73 return ret;
74}
75
76static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
77 int left_margin)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020078{
79 int i;
80 size_t ret = 0;
81
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020082 ret += callchain__fprintf_left_margin(fp, left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020083
84 for (i = 0; i < depth; i++)
85 if (depth_mask & (1 << i))
86 ret += fprintf(fp, "| ");
87 else
88 ret += fprintf(fp, " ");
89
90 ret += fprintf(fp, "\n");
91
92 return ret;
93}
Peter Zijlstra1aa16732009-05-27 20:20:25 +020094static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020095ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
96 int depth_mask, int count, u64 total_samples,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020097 int hits, int left_margin)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020098{
99 int i;
100 size_t ret = 0;
101
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200102 ret += callchain__fprintf_left_margin(fp, left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200103 for (i = 0; i < depth; i++) {
104 if (depth_mask & (1 << i))
105 ret += fprintf(fp, "|");
106 else
107 ret += fprintf(fp, " ");
108 if (!count && i == depth - 1) {
109 double percent;
110
111 percent = hits * 100.0 / total_samples;
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200112 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200113 } else
114 ret += fprintf(fp, "%s", " ");
115 }
116 if (chain->sym)
117 ret += fprintf(fp, "%s\n", chain->sym->name);
118 else
119 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
120
121 return ret;
122}
123
Frederic Weisbecker25446032009-08-08 02:16:25 +0200124static struct symbol *rem_sq_bracket;
125static struct callchain_list rem_hits;
126
127static void init_rem_hits(void)
128{
129 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
130 if (!rem_sq_bracket) {
131 fprintf(stderr, "Not enough memory to display remaining hits\n");
132 return;
133 }
134
135 strcpy(rem_sq_bracket->name, "[...]");
136 rem_hits.sym = rem_sq_bracket;
137}
138
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200139static size_t
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200140__callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200141 u64 total_samples, int depth, int depth_mask,
142 int left_margin)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200143{
144 struct rb_node *node, *next;
145 struct callchain_node *child;
146 struct callchain_list *chain;
147 int new_depth_mask = depth_mask;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200148 u64 new_total;
Frederic Weisbecker25446032009-08-08 02:16:25 +0200149 u64 remaining;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200150 size_t ret = 0;
151 int i;
152
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200153 if (callchain_param.mode == CHAIN_GRAPH_REL)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200154 new_total = self->children_hit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200155 else
156 new_total = total_samples;
157
Frederic Weisbecker25446032009-08-08 02:16:25 +0200158 remaining = new_total;
159
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200160 node = rb_first(&self->rb_root);
161 while (node) {
Frederic Weisbecker25446032009-08-08 02:16:25 +0200162 u64 cumul;
163
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200164 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200165 cumul = cumul_hits(child);
166 remaining -= cumul;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200167
168 /*
169 * The depth mask manages the output of pipes that show
170 * the depth. We don't want to keep the pipes of the current
Frederic Weisbecker25446032009-08-08 02:16:25 +0200171 * level for the last child of this depth.
172 * Except if we have remaining filtered hits. They will
173 * supersede the last child
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200174 */
175 next = rb_next(node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200176 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200177 new_depth_mask &= ~(1 << (depth - 1));
178
179 /*
180 * But we keep the older depth mask for the line seperator
181 * to keep the level link until we reach the last child
182 */
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200183 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
184 left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200185 i = 0;
186 list_for_each_entry(chain, &child->val, list) {
187 if (chain->ip >= PERF_CONTEXT_MAX)
188 continue;
189 ret += ipchain__fprintf_graph(fp, chain, depth,
190 new_depth_mask, i++,
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200191 new_total,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200192 cumul,
193 left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200194 }
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200195 ret += __callchain__fprintf_graph(fp, child, new_total,
196 depth + 1,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200197 new_depth_mask | (1 << depth),
198 left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200199 node = next;
200 }
201
Frederic Weisbecker25446032009-08-08 02:16:25 +0200202 if (callchain_param.mode == CHAIN_GRAPH_REL &&
203 remaining && remaining != new_total) {
204
205 if (!rem_sq_bracket)
206 return ret;
207
208 new_depth_mask &= ~(1 << (depth - 1));
209
210 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
211 new_depth_mask, 0, new_total,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200212 remaining, left_margin);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200213 }
214
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200215 return ret;
216}
217
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200218
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200219static size_t
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200220callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200221 u64 total_samples, int left_margin)
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200222{
223 struct callchain_list *chain;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200224 bool printed = false;
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200225 int i = 0;
226 int ret = 0;
227
228 list_for_each_entry(chain, &self->val, list) {
229 if (chain->ip >= PERF_CONTEXT_MAX)
230 continue;
231
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200232 if (!i++ && sort__first_dimension == SORT_SYM)
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200233 continue;
234
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200235 if (!printed) {
236 ret += callchain__fprintf_left_margin(fp, left_margin);
237 ret += fprintf(fp, "|\n");
238 ret += callchain__fprintf_left_margin(fp, left_margin);
239 ret += fprintf(fp, "---");
240
241 left_margin += 3;
242 printed = true;
243 } else
244 ret += callchain__fprintf_left_margin(fp, left_margin);
245
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200246 if (chain->sym)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200247 ret += fprintf(fp, " %s\n", chain->sym->name);
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200248 else
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200249 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200250 }
251
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200252 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200253
254 return ret;
255}
256
257static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200258callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
259 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200260{
261 struct callchain_list *chain;
262 size_t ret = 0;
263
264 if (!self)
265 return 0;
266
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200267 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200268
269
Frederic Weisbecker44249612009-07-01 05:35:14 +0200270 list_for_each_entry(chain, &self->val, list) {
271 if (chain->ip >= PERF_CONTEXT_MAX)
272 continue;
273 if (chain->sym)
274 ret += fprintf(fp, " %s\n", chain->sym->name);
275 else
276 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200277 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200278 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200279
280 return ret;
281}
282
283static size_t
284hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200285 u64 total_samples, int left_margin)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200286{
287 struct rb_node *rb_node;
288 struct callchain_node *chain;
289 size_t ret = 0;
290
291 rb_node = rb_first(&self->sorted_chain);
292 while (rb_node) {
293 double percent;
294
295 chain = rb_entry(rb_node, struct callchain_node, rb_node);
296 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200297 switch (callchain_param.mode) {
298 case CHAIN_FLAT:
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200299 ret += percent_color_fprintf(fp, " %6.2f%%\n",
300 percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200301 ret += callchain__fprintf_flat(fp, chain, total_samples);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200302 break;
303 case CHAIN_GRAPH_ABS: /* Falldown */
304 case CHAIN_GRAPH_REL:
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200305 ret += callchain__fprintf_graph(fp, chain, total_samples,
306 left_margin);
Ingo Molnar83a09442009-08-15 12:26:57 +0200307 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200308 default:
309 break;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200310 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200311 ret += fprintf(fp, "\n");
312 rb_node = rb_next(rb_node);
313 }
314
315 return ret;
316}
317
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200318static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000319hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200320{
321 struct sort_entry *se;
322 size_t ret;
323
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200324 if (exclude_other && !self->parent)
325 return 0;
326
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200327 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300328 ret = percent_color_fprintf(fp,
329 field_sep ? "%.2f" : " %6.2f%%",
330 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200331 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300332 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200333
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300334 if (show_nr_samples) {
335 if (field_sep)
336 fprintf(fp, "%c%lld", *field_sep, self->count);
337 else
338 fprintf(fp, "%11lld", self->count);
339 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200340
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200341 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300342 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200343 continue;
344
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300345 fprintf(fp, "%s", field_sep ?: " ");
346 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200347 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200348
349 ret += fprintf(fp, "\n");
350
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200351 if (callchain) {
352 int left_margin = 0;
353
354 if (sort__first_dimension == SORT_COMM) {
355 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
356 list);
357 left_margin = se->width ? *se->width : 0;
358 left_margin -= thread__comm_len(self->thread);
359 }
360
361 hist_entry_callchain__fprintf(fp, self, total_samples,
362 left_margin);
363 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200364
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200365 return ret;
366}
367
368/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200369 *
370 */
371
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300372static void dso__calc_col_width(struct dso *self)
373{
374 if (!col_width_list_str && !field_sep &&
375 (!dso_list || strlist__has_entry(dso_list, self->name))) {
376 unsigned int slen = strlen(self->name);
377 if (slen > dsos__col_width)
378 dsos__col_width = slen;
379 }
380
381 self->slen_calculated = 1;
382}
383
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200384static void thread__comm_adjust(struct thread *self)
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200385{
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200386 char *comm = self->comm;
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200387
388 if (!col_width_list_str && !field_sep &&
389 (!comm_list || strlist__has_entry(comm_list, comm))) {
390 unsigned int slen = strlen(comm);
391
392 if (slen > comms__col_width) {
393 comms__col_width = slen;
394 threads__col_width = slen + 6;
395 }
396 }
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200397}
398
399static int thread__set_comm_adjust(struct thread *self, const char *comm)
400{
401 int ret = thread__set_comm(self, comm);
402
403 if (ret)
404 return ret;
405
406 thread__comm_adjust(self);
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200407
408 return 0;
409}
410
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200411static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200412{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200413 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200414 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200415
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200416 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200417}
418
Arnaldo Carvalho de Melo50e50952009-11-22 14:59:22 -0200419static struct symbol **resolve_callchain(struct thread *thread,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300420 struct ip_callchain *chain,
421 struct symbol **parent)
Frederic Weisbecker44249612009-07-01 05:35:14 +0200422{
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200423 u8 cpumode = PERF_RECORD_MISC_USER;
Ingo Molnar029e5b12009-07-03 13:17:28 +0200424 struct symbol **syms = NULL;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200425 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200426
427 if (callchain) {
428 syms = calloc(chain->nr, sizeof(*syms));
429 if (!syms) {
430 fprintf(stderr, "Can't allocate memory for symbols\n");
431 exit(-1);
432 }
433 }
434
435 for (i = 0; i < chain->nr; i++) {
436 u64 ip = chain->ips[i];
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200437 struct addr_location al;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200438
439 if (ip >= PERF_CONTEXT_MAX) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200440 switch (ip) {
441 case PERF_CONTEXT_HV:
442 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
443 case PERF_CONTEXT_KERNEL:
444 cpumode = PERF_RECORD_MISC_KERNEL; break;
445 case PERF_CONTEXT_USER:
446 cpumode = PERF_RECORD_MISC_USER; break;
447 default:
448 break;
449 }
Frederic Weisbecker44249612009-07-01 05:35:14 +0200450 continue;
451 }
452
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200453 thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
454 ip, &al, NULL);
455 if (al.sym != NULL) {
456 if (sort__has_parent && !*parent &&
457 call__match(al.sym))
458 *parent = al.sym;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200459 if (!callchain)
460 break;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200461 syms[i] = al.sym;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200462 }
463 }
464
465 return syms;
466}
467
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200468/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200469 * collect histogram counts
470 */
471
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200472static int hist_entry__add(struct addr_location *al,
473 struct ip_callchain *chain, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300474{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300475 struct symbol **syms = NULL, *parent = NULL;
476 bool hit;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200477 struct hist_entry *he;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300478
Frederic Weisbecker44249612009-07-01 05:35:14 +0200479 if ((sort__has_parent || callchain) && chain)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200480 syms = resolve_callchain(al->thread, chain, &parent);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200481
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200482 he = __hist_entry__add(al, parent, count, &hit);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300483 if (he == NULL)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200484 return -ENOMEM;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300485
486 if (hit)
487 he->count += count;
488
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200489 if (callchain) {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300490 if (!hit)
491 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200492 append_chain(&he->callchain, chain, syms);
493 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200494 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200495
496 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300497}
498
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000499static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300500{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200501 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200502 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300503 struct rb_node *nd;
504 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300505 unsigned int width;
506 char *col_width = col_width_list_str;
Brice Goglin9f866692009-08-10 15:26:32 +0200507 int raw_printing_style;
508
509 raw_printing_style = !strcmp(pretty_printing_style, "raw");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300510
Frederic Weisbecker25446032009-08-08 02:16:25 +0200511 init_rem_hits();
512
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300513 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +0200514 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200515
516 fprintf(fp, "# Overhead");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300517 if (show_nr_samples) {
518 if (field_sep)
519 fprintf(fp, "%cSamples", *field_sep);
520 else
521 fputs(" Samples ", fp);
522 }
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200523 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300524 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200525 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300526 if (field_sep) {
527 fprintf(fp, "%c%s", *field_sep, se->header);
528 continue;
529 }
530 width = strlen(se->header);
531 if (se->width) {
532 if (col_width_list_str) {
533 if (col_width) {
534 *se->width = atoi(col_width);
535 col_width = strchr(col_width, ',');
536 if (col_width)
537 ++col_width;
538 }
539 }
540 width = *se->width = max(*se->width, width);
541 }
542 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200543 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200544 fprintf(fp, "\n");
545
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300546 if (field_sep)
547 goto print_entries;
548
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200549 fprintf(fp, "# ........");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300550 if (show_nr_samples)
551 fprintf(fp, " ..........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200552 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200553 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200554
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300555 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200556 continue;
557
Ingo Molnar4593bba2009-06-02 15:34:25 +0200558 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300559 if (se->width)
560 width = *se->width;
561 else
562 width = strlen(se->header);
563 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200564 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200565 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200566 fprintf(fp, "\n");
567
568 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200569
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300570print_entries:
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200571 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
572 pos = rb_entry(nd, struct hist_entry, rb_node);
573 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300574 }
575
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200576 if (sort_order == default_sort_order &&
577 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200578 fprintf(fp, "#\n");
Pekka Enberg114cfab2009-08-05 13:25:21 +0300579 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200580 fprintf(fp, "#\n");
581 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200582 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200583
Frederic Weisbecker25446032009-08-08 02:16:25 +0200584 free(rem_sq_bracket);
585
Brice Goglin8d513272009-08-07 13:55:24 +0200586 if (show_threads)
Brice Goglin9f866692009-08-10 15:26:32 +0200587 perf_read_values_display(fp, &show_threads_values,
588 raw_printing_style);
Brice Goglin8d513272009-08-07 13:55:24 +0200589
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300590 return ret;
591}
592
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200593static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +0200594{
595 unsigned int chain_size;
596
Ingo Molnar75220602009-06-18 08:00:17 +0200597 chain_size = event->header.size;
598 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
599
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000600 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +0200601 return -1;
602
603 return 0;
604}
605
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200606static int process_sample_event(event_t *event)
Ingo Molnar75051722009-06-03 23:14:49 +0200607{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000608 u64 ip = event->ip.ip;
609 u64 period = 1;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200610 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200611 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000612 int cpumode;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200613 struct addr_location al;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300614 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200615
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200616 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000617 period = *(u64 *)more_data;
618 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200619 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200620
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200621 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200622 event->header.misc,
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -0300623 event->ip.pid, event->ip.tid,
Peter Zijlstra4502d772009-06-10 15:03:06 +0200624 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200625 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +0200626
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200627 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200628 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200629
630 chain = (void *)more_data;
631
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200632 dump_printf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200633
Ingo Molnar75220602009-06-18 08:00:17 +0200634 if (validate_chain(chain, event) < 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200635 pr_debug("call-chain problem with event, "
636 "skipping it.\n");
Ingo Molnar75220602009-06-18 08:00:17 +0200637 return 0;
638 }
639
640 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200641 for (i = 0; i < chain->nr; i++)
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200642 dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200643 }
644 }
645
Ingo Molnar75051722009-06-03 23:14:49 +0200646 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200647 pr_debug("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200648 event->header.type);
649 return -1;
650 }
651
Julia Lawallf39cdf22009-10-17 08:43:17 +0200652 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
653
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300654 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
655 return 0;
656
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200657 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000658
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200659 thread__find_addr_location(thread, cpumode,
660 MAP__FUNCTION, ip, &al, NULL);
661 /*
662 * We have to do this here as we may have a dso with no symbol hit that
663 * has a name longer than the ones with symbols sampled.
664 */
665 if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
666 dso__calc_col_width(al.map->dso);
Ingo Molnar75051722009-06-03 23:14:49 +0200667
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300668 if (dso_list &&
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200669 (!al.map || !al.map->dso ||
670 !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
671 (al.map->dso->short_name != al.map->dso->long_name &&
672 strlist__has_entry(dso_list, al.map->dso->long_name)))))
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300673 return 0;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300674
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200675 if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name))
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300676 return 0;
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -0300677
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200678 if (hist_entry__add(&al, chain, period)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200679 pr_debug("problem incrementing symbol count, skipping event\n");
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300680 return -1;
Ingo Molnar75051722009-06-03 23:14:49 +0200681 }
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300682
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200683 event__stats.total += period;
Ingo Molnar75051722009-06-03 23:14:49 +0200684
685 return 0;
686}
687
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200688static int process_comm_event(event_t *event)
Ingo Molnar75051722009-06-03 23:14:49 +0200689{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300690 struct thread *thread = threads__findnew(event->comm.pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200691
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200692 dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200693
694 if (thread == NULL ||
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200695 thread__set_comm_adjust(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200696 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar75051722009-06-03 23:14:49 +0200697 return -1;
698 }
Ingo Molnar75051722009-06-03 23:14:49 +0200699
700 return 0;
701}
702
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200703static int process_read_event(event_t *event)
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200704{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200705 struct perf_event_attr *attr;
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200706
707 attr = perf_header__find_attr(event->read.id, header);
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200708
Brice Goglin8d513272009-08-07 13:55:24 +0200709 if (show_threads) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200710 const char *name = attr ? __event_name(attr->type, attr->config)
Brice Goglin8d513272009-08-07 13:55:24 +0200711 : "unknown";
712 perf_read_values_add_value(&show_threads_values,
713 event->read.pid, event->read.tid,
714 event->read.id,
715 name,
716 event->read.value);
717 }
718
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200719 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
720 attr ? __event_name(attr->type, attr->config) : "FAIL",
721 event->read.value);
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200722
723 return 0;
724}
725
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200726static int sample_type_check(u64 type)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300727{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200728 sample_type = type;
Ingo Molnar8465b052009-06-14 14:44:07 +0200729
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200730 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
731 if (sort__has_parent) {
732 fprintf(stderr, "selected --sort parent, but no"
733 " callchain data. Did you call"
734 " perf record without -g?\n");
735 return -1;
736 }
737 if (callchain) {
738 fprintf(stderr, "selected -g but no callchain data."
739 " Did you call perf record without"
740 " -g?\n");
741 return -1;
742 }
743 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
744 callchain = 1;
745 if (register_callchain_param(&callchain_param) < 0) {
746 fprintf(stderr, "Can't register callchain"
747 " params\n");
748 return -1;
749 }
Ingo Molnard80d3382009-06-03 23:14:49 +0200750 }
751
752 return 0;
753}
754
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200755static struct perf_file_handler file_handler = {
756 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200757 .process_mmap_event = event__process_mmap,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200758 .process_comm_event = process_comm_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200759 .process_exit_event = event__process_task,
760 .process_fork_event = event__process_task,
761 .process_lost_event = event__process_lost,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200762 .process_read_event = process_read_event,
763 .sample_type_check = sample_type_check,
764};
765
766
Ingo Molnard80d3382009-06-03 23:14:49 +0200767static int __cmd_report(void)
768{
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200769 struct thread *idle;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200770 int ret;
Ingo Molnard80d3382009-06-03 23:14:49 +0200771
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300772 idle = register_idle_thread();
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200773 thread__comm_adjust(idle);
Ingo Molnard80d3382009-06-03 23:14:49 +0200774
Brice Goglin8d513272009-08-07 13:55:24 +0200775 if (show_threads)
776 perf_read_values_init(&show_threads_values);
777
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200778 register_perf_file_handler(&file_handler);
Ingo Molnard80d3382009-06-03 23:14:49 +0200779
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200780 ret = mmap_dispatch_perf_file(&header, input_name, force,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200781 full_paths, &event__cwdlen, &event__cwd);
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200782 if (ret)
783 return ret;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200784
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200785 if (dump_trace) {
786 event__print_totals();
Ingo Molnar97b07b62009-05-26 18:48:58 +0200787 return 0;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200788 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200789
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300790 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300791 threads__fprintf(stdout);
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300792
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300793 if (verbose > 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +0200794 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200795
Peter Zijlstra82292892009-06-03 12:37:36 +0200796 collapse__resort();
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200797 output__resort(event__stats.total);
798 output__fprintf(stdout, event__stats.total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300799
Brice Goglin8d513272009-08-07 13:55:24 +0200800 if (show_threads)
801 perf_read_values_destroy(&show_threads_values);
802
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200803 return ret;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300804}
805
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200806static int
807parse_callchain_opt(const struct option *opt __used, const char *arg,
808 int unset __used)
809{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200810 char *tok;
811 char *endptr;
812
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200813 callchain = 1;
814
815 if (!arg)
816 return 0;
817
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200818 tok = strtok((char *)arg, ",");
819 if (!tok)
820 return -1;
821
822 /* get the output mode */
823 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200824 callchain_param.mode = CHAIN_GRAPH_ABS;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200825
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200826 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200827 callchain_param.mode = CHAIN_FLAT;
828
829 else if (!strncmp(tok, "fractal", strlen(arg)))
830 callchain_param.mode = CHAIN_GRAPH_REL;
831
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +0200832 else if (!strncmp(tok, "none", strlen(arg))) {
833 callchain_param.mode = CHAIN_NONE;
834 callchain = 0;
835
836 return 0;
837 }
838
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200839 else
840 return -1;
841
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200842 /* get the min percentage */
843 tok = strtok(NULL, ",");
844 if (!tok)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200845 goto setup;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200846
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200847 callchain_param.min_percent = strtod(tok, &endptr);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200848 if (tok == endptr)
849 return -1;
850
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200851setup:
852 if (register_callchain_param(&callchain_param) < 0) {
853 fprintf(stderr, "Can't register callchain params\n");
854 return -1;
855 }
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200856 return 0;
857}
858
John Kacurdd68ada2009-09-24 18:02:49 +0200859//static const char * const report_usage[] = {
860const char * const report_usage[] = {
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200861 "perf report [<options>] <command>",
862 NULL
863};
864
865static const struct option options[] = {
866 OPT_STRING('i', "input", &input_name, "file",
867 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300868 OPT_BOOLEAN('v', "verbose", &verbose,
869 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200870 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
871 "dump raw trace in ASCII"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200872 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
873 "file", "vmlinux pathname"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200874 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200875 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200876 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300877 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
878 "Show a column with the number of samples"),
Brice Goglin8d513272009-08-07 13:55:24 +0200879 OPT_BOOLEAN('T', "threads", &show_threads,
880 "Show per-thread event counters"),
Brice Goglin9f866692009-08-10 15:26:32 +0200881 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
882 "pretty printing style key: normal raw"),
Ingo Molnar63299f02009-05-28 10:52:00 +0200883 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200884 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300885 OPT_BOOLEAN('P', "full-paths", &full_paths,
886 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200887 OPT_STRING('p', "parent", &parent_pattern, "regex",
888 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200889 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
890 "Only display entries with parent-match"),
Anton Blanchard1483b19f2009-07-16 15:44:29 +0200891 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200892 "Display callchains using output_type and min percent threshold. "
Anton Blanchard1483b19f2009-07-16 15:44:29 +0200893 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300894 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
895 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300896 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
897 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -0300898 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
899 "only consider these symbols"),
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300900 OPT_STRING('w', "column-widths", &col_width_list_str,
901 "width[,width...]",
902 "don't try to adjust column width, use these fixed values"),
903 OPT_STRING('t', "field-separator", &field_sep, "separator",
904 "separator for columns, no spaces will be added between "
905 "columns '.' is reserved."),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200906 OPT_END()
907};
908
Ingo Molnar5352f352009-06-03 10:07:39 +0200909static void setup_sorting(void)
910{
911 char *tmp, *tok, *str = strdup(sort_order);
912
913 for (tok = strtok_r(str, ", ", &tmp);
914 tok; tok = strtok_r(NULL, ", ", &tmp)) {
915 if (sort_dimension__add(tok) < 0) {
916 error("Unknown --sort key: `%s'", tok);
917 usage_with_options(report_usage, options);
918 }
919 }
920
921 free(str);
922}
923
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300924static void setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300925 struct sort_entry *se, const char *list_name,
926 FILE *fp)
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300927{
928 if (list_str) {
929 *list = strlist__new(true, list_str);
930 if (!*list) {
931 fprintf(stderr, "problems parsing %s list\n",
932 list_name);
933 exit(129);
934 }
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300935 if (strlist__nr_entries(*list) == 1) {
936 fprintf(fp, "# %s: %s\n", list_name,
937 strlist__entry(*list, 0)->s);
938 se->elide = true;
939 }
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300940 }
941}
942
Ingo Molnarf37a2912009-07-01 12:37:06 +0200943int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200944{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200945 if (symbol__init(&symbol_conf) < 0)
946 return -1;
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200947
Ingo Molnaredc52de2009-06-04 16:24:37 +0200948 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200949
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200950 setup_sorting();
951
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300952 if (parent_pattern != default_parent_pattern) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200953 sort_dimension__add("parent");
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300954 sort_parent.elide = 1;
955 } else
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200956 exclude_other = 0;
957
Ingo Molnaredc52de2009-06-04 16:24:37 +0200958 /*
959 * Any (unrecognized) arguments left?
960 */
961 if (argc)
962 usage_with_options(report_usage, options);
963
Ingo Molnara930d2c2009-05-27 09:50:13 +0200964 setup_pager();
965
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300966 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
967 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
968 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300969
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300970 if (field_sep && *field_sep == '.') {
971 fputs("'.' is the only non valid --field-separator argument\n",
972 stderr);
973 exit(129);
974 }
975
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300976 return __cmd_report();
977}