blob: c203eaf73a2f82959f7b6eba4c1523c05ee4277b [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"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020025#include "util/session.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020026
27#include "util/parse-options.h"
28#include "util/parse-events.h"
29
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
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020055static u64 sample_type;
56
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020057struct symbol_conf symbol_conf;
58
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020059
60static size_t
61callchain__fprintf_left_margin(FILE *fp, int left_margin)
62{
63 int i;
64 int ret;
65
66 ret = fprintf(fp, " ");
67
68 for (i = 0; i < left_margin; i++)
69 ret += fprintf(fp, " ");
70
71 return ret;
72}
73
74static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
75 int left_margin)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020076{
77 int i;
78 size_t ret = 0;
79
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020080 ret += callchain__fprintf_left_margin(fp, left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020081
82 for (i = 0; i < depth; i++)
83 if (depth_mask & (1 << i))
84 ret += fprintf(fp, "| ");
85 else
86 ret += fprintf(fp, " ");
87
88 ret += fprintf(fp, "\n");
89
90 return ret;
91}
Peter Zijlstra1aa16732009-05-27 20:20:25 +020092static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020093ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
94 int depth_mask, int count, u64 total_samples,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020095 int hits, int left_margin)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020096{
97 int i;
98 size_t ret = 0;
99
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200100 ret += callchain__fprintf_left_margin(fp, left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200101 for (i = 0; i < depth; i++) {
102 if (depth_mask & (1 << i))
103 ret += fprintf(fp, "|");
104 else
105 ret += fprintf(fp, " ");
106 if (!count && i == depth - 1) {
107 double percent;
108
109 percent = hits * 100.0 / total_samples;
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200110 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200111 } else
112 ret += fprintf(fp, "%s", " ");
113 }
114 if (chain->sym)
115 ret += fprintf(fp, "%s\n", chain->sym->name);
116 else
117 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
118
119 return ret;
120}
121
Frederic Weisbecker25446032009-08-08 02:16:25 +0200122static struct symbol *rem_sq_bracket;
123static struct callchain_list rem_hits;
124
125static void init_rem_hits(void)
126{
127 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
128 if (!rem_sq_bracket) {
129 fprintf(stderr, "Not enough memory to display remaining hits\n");
130 return;
131 }
132
133 strcpy(rem_sq_bracket->name, "[...]");
134 rem_hits.sym = rem_sq_bracket;
135}
136
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200137static size_t
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200138__callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200139 u64 total_samples, int depth, int depth_mask,
140 int left_margin)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200141{
142 struct rb_node *node, *next;
143 struct callchain_node *child;
144 struct callchain_list *chain;
145 int new_depth_mask = depth_mask;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200146 u64 new_total;
Frederic Weisbecker25446032009-08-08 02:16:25 +0200147 u64 remaining;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200148 size_t ret = 0;
149 int i;
150
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200151 if (callchain_param.mode == CHAIN_GRAPH_REL)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200152 new_total = self->children_hit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200153 else
154 new_total = total_samples;
155
Frederic Weisbecker25446032009-08-08 02:16:25 +0200156 remaining = new_total;
157
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200158 node = rb_first(&self->rb_root);
159 while (node) {
Frederic Weisbecker25446032009-08-08 02:16:25 +0200160 u64 cumul;
161
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200162 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200163 cumul = cumul_hits(child);
164 remaining -= cumul;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200165
166 /*
167 * The depth mask manages the output of pipes that show
168 * the depth. We don't want to keep the pipes of the current
Frederic Weisbecker25446032009-08-08 02:16:25 +0200169 * level for the last child of this depth.
170 * Except if we have remaining filtered hits. They will
171 * supersede the last child
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200172 */
173 next = rb_next(node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200174 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200175 new_depth_mask &= ~(1 << (depth - 1));
176
177 /*
178 * But we keep the older depth mask for the line seperator
179 * to keep the level link until we reach the last child
180 */
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200181 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
182 left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200183 i = 0;
184 list_for_each_entry(chain, &child->val, list) {
185 if (chain->ip >= PERF_CONTEXT_MAX)
186 continue;
187 ret += ipchain__fprintf_graph(fp, chain, depth,
188 new_depth_mask, i++,
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200189 new_total,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200190 cumul,
191 left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200192 }
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200193 ret += __callchain__fprintf_graph(fp, child, new_total,
194 depth + 1,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200195 new_depth_mask | (1 << depth),
196 left_margin);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200197 node = next;
198 }
199
Frederic Weisbecker25446032009-08-08 02:16:25 +0200200 if (callchain_param.mode == CHAIN_GRAPH_REL &&
201 remaining && remaining != new_total) {
202
203 if (!rem_sq_bracket)
204 return ret;
205
206 new_depth_mask &= ~(1 << (depth - 1));
207
208 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
209 new_depth_mask, 0, new_total,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200210 remaining, left_margin);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200211 }
212
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200213 return ret;
214}
215
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200216
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200217static size_t
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200218callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200219 u64 total_samples, int left_margin)
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200220{
221 struct callchain_list *chain;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200222 bool printed = false;
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200223 int i = 0;
224 int ret = 0;
225
226 list_for_each_entry(chain, &self->val, list) {
227 if (chain->ip >= PERF_CONTEXT_MAX)
228 continue;
229
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200230 if (!i++ && sort__first_dimension == SORT_SYM)
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200231 continue;
232
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200233 if (!printed) {
234 ret += callchain__fprintf_left_margin(fp, left_margin);
235 ret += fprintf(fp, "|\n");
236 ret += callchain__fprintf_left_margin(fp, left_margin);
237 ret += fprintf(fp, "---");
238
239 left_margin += 3;
240 printed = true;
241 } else
242 ret += callchain__fprintf_left_margin(fp, left_margin);
243
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200244 if (chain->sym)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200245 ret += fprintf(fp, " %s\n", chain->sym->name);
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200246 else
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200247 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200248 }
249
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200250 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +0200251
252 return ret;
253}
254
255static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200256callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
257 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200258{
259 struct callchain_list *chain;
260 size_t ret = 0;
261
262 if (!self)
263 return 0;
264
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200265 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200266
267
Frederic Weisbecker44249612009-07-01 05:35:14 +0200268 list_for_each_entry(chain, &self->val, list) {
269 if (chain->ip >= PERF_CONTEXT_MAX)
270 continue;
271 if (chain->sym)
272 ret += fprintf(fp, " %s\n", chain->sym->name);
273 else
274 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200275 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200276 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200277
278 return ret;
279}
280
281static size_t
282hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200283 u64 total_samples, int left_margin)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200284{
285 struct rb_node *rb_node;
286 struct callchain_node *chain;
287 size_t ret = 0;
288
289 rb_node = rb_first(&self->sorted_chain);
290 while (rb_node) {
291 double percent;
292
293 chain = rb_entry(rb_node, struct callchain_node, rb_node);
294 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200295 switch (callchain_param.mode) {
296 case CHAIN_FLAT:
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200297 ret += percent_color_fprintf(fp, " %6.2f%%\n",
298 percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200299 ret += callchain__fprintf_flat(fp, chain, total_samples);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200300 break;
301 case CHAIN_GRAPH_ABS: /* Falldown */
302 case CHAIN_GRAPH_REL:
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200303 ret += callchain__fprintf_graph(fp, chain, total_samples,
304 left_margin);
Ingo Molnar83a09442009-08-15 12:26:57 +0200305 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200306 default:
307 break;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200308 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200309 ret += fprintf(fp, "\n");
310 rb_node = rb_next(rb_node);
311 }
312
313 return ret;
314}
315
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200316static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000317hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200318{
319 struct sort_entry *se;
320 size_t ret;
321
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200322 if (exclude_other && !self->parent)
323 return 0;
324
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200325 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300326 ret = percent_color_fprintf(fp,
327 field_sep ? "%.2f" : " %6.2f%%",
328 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200329 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300330 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200331
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300332 if (show_nr_samples) {
333 if (field_sep)
334 fprintf(fp, "%c%lld", *field_sep, self->count);
335 else
336 fprintf(fp, "%11lld", self->count);
337 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200338
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200339 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300340 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200341 continue;
342
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300343 fprintf(fp, "%s", field_sep ?: " ");
344 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200345 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200346
347 ret += fprintf(fp, "\n");
348
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200349 if (callchain) {
350 int left_margin = 0;
351
352 if (sort__first_dimension == SORT_COMM) {
353 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
354 list);
355 left_margin = se->width ? *se->width : 0;
356 left_margin -= thread__comm_len(self->thread);
357 }
358
359 hist_entry_callchain__fprintf(fp, self, total_samples,
360 left_margin);
361 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200362
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200363 return ret;
364}
365
366/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200367 *
368 */
369
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300370static void dso__calc_col_width(struct dso *self)
371{
372 if (!col_width_list_str && !field_sep &&
373 (!dso_list || strlist__has_entry(dso_list, self->name))) {
374 unsigned int slen = strlen(self->name);
375 if (slen > dsos__col_width)
376 dsos__col_width = slen;
377 }
378
379 self->slen_calculated = 1;
380}
381
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200382static void thread__comm_adjust(struct thread *self)
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200383{
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200384 char *comm = self->comm;
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200385
386 if (!col_width_list_str && !field_sep &&
387 (!comm_list || strlist__has_entry(comm_list, comm))) {
388 unsigned int slen = strlen(comm);
389
390 if (slen > comms__col_width) {
391 comms__col_width = slen;
392 threads__col_width = slen + 6;
393 }
394 }
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200395}
396
397static int thread__set_comm_adjust(struct thread *self, const char *comm)
398{
399 int ret = thread__set_comm(self, comm);
400
401 if (ret)
402 return ret;
403
404 thread__comm_adjust(self);
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200405
406 return 0;
407}
408
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200409static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200410{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200411 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200412 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200413
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200414 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200415}
416
Arnaldo Carvalho de Melo50e50952009-11-22 14:59:22 -0200417static struct symbol **resolve_callchain(struct thread *thread,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300418 struct ip_callchain *chain,
419 struct symbol **parent)
Frederic Weisbecker44249612009-07-01 05:35:14 +0200420{
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200421 u8 cpumode = PERF_RECORD_MISC_USER;
Ingo Molnar029e5b12009-07-03 13:17:28 +0200422 struct symbol **syms = NULL;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200423 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200424
425 if (callchain) {
426 syms = calloc(chain->nr, sizeof(*syms));
427 if (!syms) {
428 fprintf(stderr, "Can't allocate memory for symbols\n");
429 exit(-1);
430 }
431 }
432
433 for (i = 0; i < chain->nr; i++) {
434 u64 ip = chain->ips[i];
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200435 struct addr_location al;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200436
437 if (ip >= PERF_CONTEXT_MAX) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200438 switch (ip) {
439 case PERF_CONTEXT_HV:
440 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
441 case PERF_CONTEXT_KERNEL:
442 cpumode = PERF_RECORD_MISC_KERNEL; break;
443 case PERF_CONTEXT_USER:
444 cpumode = PERF_RECORD_MISC_USER; break;
445 default:
446 break;
447 }
Frederic Weisbecker44249612009-07-01 05:35:14 +0200448 continue;
449 }
450
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200451 thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
452 ip, &al, NULL);
453 if (al.sym != NULL) {
454 if (sort__has_parent && !*parent &&
455 call__match(al.sym))
456 *parent = al.sym;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200457 if (!callchain)
458 break;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200459 syms[i] = al.sym;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200460 }
461 }
462
463 return syms;
464}
465
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200466/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200467 * collect histogram counts
468 */
469
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200470static int hist_entry__add(struct addr_location *al,
471 struct ip_callchain *chain, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300472{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300473 struct symbol **syms = NULL, *parent = NULL;
474 bool hit;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200475 struct hist_entry *he;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300476
Frederic Weisbecker44249612009-07-01 05:35:14 +0200477 if ((sort__has_parent || callchain) && chain)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200478 syms = resolve_callchain(al->thread, chain, &parent);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200479
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200480 he = __hist_entry__add(al, parent, count, &hit);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300481 if (he == NULL)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200482 return -ENOMEM;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300483
484 if (hit)
485 he->count += count;
486
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200487 if (callchain) {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300488 if (!hit)
489 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200490 append_chain(&he->callchain, chain, syms);
491 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200492 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200493
494 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300495}
496
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000497static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300498{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200499 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200500 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300501 struct rb_node *nd;
502 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300503 unsigned int width;
504 char *col_width = col_width_list_str;
Brice Goglin9f866692009-08-10 15:26:32 +0200505 int raw_printing_style;
506
507 raw_printing_style = !strcmp(pretty_printing_style, "raw");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300508
Frederic Weisbecker25446032009-08-08 02:16:25 +0200509 init_rem_hits();
510
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300511 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +0200512 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200513
514 fprintf(fp, "# Overhead");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300515 if (show_nr_samples) {
516 if (field_sep)
517 fprintf(fp, "%cSamples", *field_sep);
518 else
519 fputs(" Samples ", fp);
520 }
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200521 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300522 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200523 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300524 if (field_sep) {
525 fprintf(fp, "%c%s", *field_sep, se->header);
526 continue;
527 }
528 width = strlen(se->header);
529 if (se->width) {
530 if (col_width_list_str) {
531 if (col_width) {
532 *se->width = atoi(col_width);
533 col_width = strchr(col_width, ',');
534 if (col_width)
535 ++col_width;
536 }
537 }
538 width = *se->width = max(*se->width, width);
539 }
540 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200541 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200542 fprintf(fp, "\n");
543
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300544 if (field_sep)
545 goto print_entries;
546
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200547 fprintf(fp, "# ........");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300548 if (show_nr_samples)
549 fprintf(fp, " ..........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200550 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200551 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200552
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300553 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200554 continue;
555
Ingo Molnar4593bba2009-06-02 15:34:25 +0200556 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300557 if (se->width)
558 width = *se->width;
559 else
560 width = strlen(se->header);
561 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200562 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200563 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200564 fprintf(fp, "\n");
565
566 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200567
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300568print_entries:
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200569 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
570 pos = rb_entry(nd, struct hist_entry, rb_node);
571 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300572 }
573
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200574 if (sort_order == default_sort_order &&
575 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200576 fprintf(fp, "#\n");
Pekka Enberg114cfab2009-08-05 13:25:21 +0300577 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200578 fprintf(fp, "#\n");
579 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200580 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200581
Frederic Weisbecker25446032009-08-08 02:16:25 +0200582 free(rem_sq_bracket);
583
Brice Goglin8d513272009-08-07 13:55:24 +0200584 if (show_threads)
Brice Goglin9f866692009-08-10 15:26:32 +0200585 perf_read_values_display(fp, &show_threads_values,
586 raw_printing_style);
Brice Goglin8d513272009-08-07 13:55:24 +0200587
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300588 return ret;
589}
590
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200591static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +0200592{
593 unsigned int chain_size;
594
Ingo Molnar75220602009-06-18 08:00:17 +0200595 chain_size = event->header.size;
596 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
597
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000598 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +0200599 return -1;
600
601 return 0;
602}
603
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200604static int process_sample_event(event_t *event, struct perf_session *session __used)
Ingo Molnar75051722009-06-03 23:14:49 +0200605{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900606 struct sample_data data;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000607 int cpumode;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200608 struct addr_location al;
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900609 struct thread *thread;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200610
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900611 memset(&data, 0, sizeof(data));
612 data.period = 1;
613
614 event__parse_sample(event, sample_type, &data);
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200615
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200616 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200617 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900618 data.pid, data.tid,
619 (void *)(long)data.ip,
620 (long long)data.period);
Ingo Molnar75051722009-06-03 23:14:49 +0200621
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200622 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200623 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200624
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900625 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200626
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900627 if (validate_chain(data.callchain, event) < 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200628 pr_debug("call-chain problem with event, "
629 "skipping it.\n");
Ingo Molnar75220602009-06-18 08:00:17 +0200630 return 0;
631 }
632
633 if (dump_trace) {
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900634 for (i = 0; i < data.callchain->nr; i++)
635 dump_printf("..... %2d: %016Lx\n",
636 i, data.callchain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200637 }
638 }
639
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900640 thread = threads__findnew(data.pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200641 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200642 pr_debug("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200643 event->header.type);
644 return -1;
645 }
646
Julia Lawallf39cdf22009-10-17 08:43:17 +0200647 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
648
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300649 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
650 return 0;
651
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200652 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000653
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200654 thread__find_addr_location(thread, cpumode,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900655 MAP__FUNCTION, data.ip, &al, NULL);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200656 /*
657 * We have to do this here as we may have a dso with no symbol hit that
658 * has a name longer than the ones with symbols sampled.
659 */
660 if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
661 dso__calc_col_width(al.map->dso);
Ingo Molnar75051722009-06-03 23:14:49 +0200662
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300663 if (dso_list &&
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200664 (!al.map || !al.map->dso ||
665 !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
666 (al.map->dso->short_name != al.map->dso->long_name &&
667 strlist__has_entry(dso_list, al.map->dso->long_name)))))
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300668 return 0;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300669
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200670 if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name))
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300671 return 0;
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -0300672
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900673 if (hist_entry__add(&al, data.callchain, data.period)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200674 pr_debug("problem incrementing symbol count, skipping event\n");
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300675 return -1;
Ingo Molnar75051722009-06-03 23:14:49 +0200676 }
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300677
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900678 event__stats.total += data.period;
Ingo Molnar75051722009-06-03 23:14:49 +0200679
680 return 0;
681}
682
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200683static int process_comm_event(event_t *event, struct perf_session *session __used)
Ingo Molnar75051722009-06-03 23:14:49 +0200684{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300685 struct thread *thread = threads__findnew(event->comm.pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200686
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200687 dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200688
689 if (thread == NULL ||
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200690 thread__set_comm_adjust(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200691 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar75051722009-06-03 23:14:49 +0200692 return -1;
693 }
Ingo Molnar75051722009-06-03 23:14:49 +0200694
695 return 0;
696}
697
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200698static int process_read_event(event_t *event, struct perf_session *session __used)
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200699{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200700 struct perf_event_attr *attr;
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200701
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200702 attr = perf_header__find_attr(event->read.id, &session->header);
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200703
Brice Goglin8d513272009-08-07 13:55:24 +0200704 if (show_threads) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200705 const char *name = attr ? __event_name(attr->type, attr->config)
Brice Goglin8d513272009-08-07 13:55:24 +0200706 : "unknown";
707 perf_read_values_add_value(&show_threads_values,
708 event->read.pid, event->read.tid,
709 event->read.id,
710 name,
711 event->read.value);
712 }
713
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200714 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
715 attr ? __event_name(attr->type, attr->config) : "FAIL",
716 event->read.value);
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200717
718 return 0;
719}
720
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200721static int sample_type_check(u64 type)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300722{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200723 sample_type = type;
Ingo Molnar8465b052009-06-14 14:44:07 +0200724
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200725 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
726 if (sort__has_parent) {
727 fprintf(stderr, "selected --sort parent, but no"
728 " callchain data. Did you call"
729 " perf record without -g?\n");
730 return -1;
731 }
732 if (callchain) {
733 fprintf(stderr, "selected -g but no callchain data."
734 " Did you call perf record without"
735 " -g?\n");
736 return -1;
737 }
738 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
739 callchain = 1;
740 if (register_callchain_param(&callchain_param) < 0) {
741 fprintf(stderr, "Can't register callchain"
742 " params\n");
743 return -1;
744 }
Ingo Molnard80d3382009-06-03 23:14:49 +0200745 }
746
747 return 0;
748}
749
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200750static struct perf_event_ops event_ops = {
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200751 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200752 .process_mmap_event = event__process_mmap,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200753 .process_comm_event = process_comm_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200754 .process_exit_event = event__process_task,
755 .process_fork_event = event__process_task,
756 .process_lost_event = event__process_lost,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200757 .process_read_event = process_read_event,
758 .sample_type_check = sample_type_check,
759};
760
761
Ingo Molnard80d3382009-06-03 23:14:49 +0200762static int __cmd_report(void)
763{
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200764 struct thread *idle;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200765 int ret;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200766 struct perf_session *session;
Ingo Molnard80d3382009-06-03 23:14:49 +0200767
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200768 session = perf_session__new(input_name, O_RDONLY, force);
769 if (session == NULL)
770 return -ENOMEM;
771
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
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200778 ret = perf_session__process_events(session, &event_ops, full_paths,
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200779 &event__cwdlen, &event__cwd);
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200780 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200781 goto out_delete;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200782
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200783 if (dump_trace) {
784 event__print_totals();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200785 goto out_delete;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200786 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200787
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300788 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300789 threads__fprintf(stdout);
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300790
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300791 if (verbose > 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +0200792 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200793
Peter Zijlstra82292892009-06-03 12:37:36 +0200794 collapse__resort();
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200795 output__resort(event__stats.total);
796 output__fprintf(stdout, event__stats.total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300797
Brice Goglin8d513272009-08-07 13:55:24 +0200798 if (show_threads)
799 perf_read_values_destroy(&show_threads_values);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200800out_delete:
801 perf_session__delete(session);
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200802 return ret;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300803}
804
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200805static int
806parse_callchain_opt(const struct option *opt __used, const char *arg,
807 int unset __used)
808{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200809 char *tok;
810 char *endptr;
811
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200812 callchain = 1;
813
814 if (!arg)
815 return 0;
816
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200817 tok = strtok((char *)arg, ",");
818 if (!tok)
819 return -1;
820
821 /* get the output mode */
822 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200823 callchain_param.mode = CHAIN_GRAPH_ABS;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200824
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200825 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200826 callchain_param.mode = CHAIN_FLAT;
827
828 else if (!strncmp(tok, "fractal", strlen(arg)))
829 callchain_param.mode = CHAIN_GRAPH_REL;
830
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +0200831 else if (!strncmp(tok, "none", strlen(arg))) {
832 callchain_param.mode = CHAIN_NONE;
833 callchain = 0;
834
835 return 0;
836 }
837
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200838 else
839 return -1;
840
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200841 /* get the min percentage */
842 tok = strtok(NULL, ",");
843 if (!tok)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200844 goto setup;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200845
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200846 callchain_param.min_percent = strtod(tok, &endptr);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200847 if (tok == endptr)
848 return -1;
849
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200850setup:
851 if (register_callchain_param(&callchain_param) < 0) {
852 fprintf(stderr, "Can't register callchain params\n");
853 return -1;
854 }
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200855 return 0;
856}
857
John Kacurdd68ada2009-09-24 18:02:49 +0200858//static const char * const report_usage[] = {
859const char * const report_usage[] = {
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200860 "perf report [<options>] <command>",
861 NULL
862};
863
864static const struct option options[] = {
865 OPT_STRING('i', "input", &input_name, "file",
866 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300867 OPT_BOOLEAN('v', "verbose", &verbose,
868 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200869 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
870 "dump raw trace in ASCII"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200871 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
872 "file", "vmlinux pathname"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200873 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200874 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200875 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300876 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
877 "Show a column with the number of samples"),
Brice Goglin8d513272009-08-07 13:55:24 +0200878 OPT_BOOLEAN('T', "threads", &show_threads,
879 "Show per-thread event counters"),
Brice Goglin9f866692009-08-10 15:26:32 +0200880 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
881 "pretty printing style key: normal raw"),
Ingo Molnar63299f02009-05-28 10:52:00 +0200882 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200883 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300884 OPT_BOOLEAN('P', "full-paths", &full_paths,
885 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200886 OPT_STRING('p', "parent", &parent_pattern, "regex",
887 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200888 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
889 "Only display entries with parent-match"),
Anton Blanchard1483b19f2009-07-16 15:44:29 +0200890 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200891 "Display callchains using output_type and min percent threshold. "
Anton Blanchard1483b19f2009-07-16 15:44:29 +0200892 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300893 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
894 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300895 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
896 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -0300897 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
898 "only consider these symbols"),
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300899 OPT_STRING('w', "column-widths", &col_width_list_str,
900 "width[,width...]",
901 "don't try to adjust column width, use these fixed values"),
902 OPT_STRING('t', "field-separator", &field_sep, "separator",
903 "separator for columns, no spaces will be added between "
904 "columns '.' is reserved."),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200905 OPT_END()
906};
907
Ingo Molnar5352f352009-06-03 10:07:39 +0200908static void setup_sorting(void)
909{
910 char *tmp, *tok, *str = strdup(sort_order);
911
912 for (tok = strtok_r(str, ", ", &tmp);
913 tok; tok = strtok_r(NULL, ", ", &tmp)) {
914 if (sort_dimension__add(tok) < 0) {
915 error("Unknown --sort key: `%s'", tok);
916 usage_with_options(report_usage, options);
917 }
918 }
919
920 free(str);
921}
922
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300923static void setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300924 struct sort_entry *se, const char *list_name,
925 FILE *fp)
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300926{
927 if (list_str) {
928 *list = strlist__new(true, list_str);
929 if (!*list) {
930 fprintf(stderr, "problems parsing %s list\n",
931 list_name);
932 exit(129);
933 }
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300934 if (strlist__nr_entries(*list) == 1) {
935 fprintf(fp, "# %s: %s\n", list_name,
936 strlist__entry(*list, 0)->s);
937 se->elide = true;
938 }
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300939 }
940}
941
Ingo Molnarf37a2912009-07-01 12:37:06 +0200942int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200943{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200944 if (symbol__init(&symbol_conf) < 0)
945 return -1;
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200946
Ingo Molnaredc52de2009-06-04 16:24:37 +0200947 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200948
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200949 setup_sorting();
950
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300951 if (parent_pattern != default_parent_pattern) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200952 sort_dimension__add("parent");
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300953 sort_parent.elide = 1;
954 } else
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200955 exclude_other = 0;
956
Ingo Molnaredc52de2009-06-04 16:24:37 +0200957 /*
958 * Any (unrecognized) arguments left?
959 */
960 if (argc)
961 usage_with_options(report_usage, options);
962
Ingo Molnara930d2c2009-05-27 09:50:13 +0200963 setup_pager();
964
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300965 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
966 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
967 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300968
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300969 if (field_sep && *field_sep == '.') {
970 fputs("'.' is the only non valid --field-separator argument\n",
971 stderr);
972 exit(129);
973 }
974
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300975 return __cmd_report();
976}