blob: 854427f0e57edce4523f1c750fbb3345ddf144b8 [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"
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 Melo4e4f06e2009-12-14 13:10:39 -020041static bool use_callchain;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030042
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
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200316static size_t hist_entry__fprintf(FILE *fp, struct hist_entry *self,
317 struct perf_session *session,
318 u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200319{
320 struct sort_entry *se;
321 size_t ret;
322
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200323 if (exclude_other && !self->parent)
324 return 0;
325
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200326 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300327 ret = percent_color_fprintf(fp,
328 field_sep ? "%.2f" : " %6.2f%%",
329 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200330 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300331 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200332
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300333 if (show_nr_samples) {
334 if (field_sep)
335 fprintf(fp, "%c%lld", *field_sep, self->count);
336 else
337 fprintf(fp, "%11lld", self->count);
338 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200339
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200340 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300341 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200342 continue;
343
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300344 fprintf(fp, "%s", field_sep ?: " ");
345 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200346 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200347
348 ret += fprintf(fp, "\n");
349
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200350 if (session->use_callchain) {
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200351 int left_margin = 0;
352
353 if (sort__first_dimension == SORT_COMM) {
354 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
355 list);
356 left_margin = se->width ? *se->width : 0;
357 left_margin -= thread__comm_len(self->thread);
358 }
359
360 hist_entry_callchain__fprintf(fp, self, total_samples,
361 left_margin);
362 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200363
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200364 return ret;
365}
366
367/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200368 *
369 */
370
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300371static void dso__calc_col_width(struct dso *self)
372{
373 if (!col_width_list_str && !field_sep &&
374 (!dso_list || strlist__has_entry(dso_list, self->name))) {
375 unsigned int slen = strlen(self->name);
376 if (slen > dsos__col_width)
377 dsos__col_width = slen;
378 }
379
380 self->slen_calculated = 1;
381}
382
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200383static void thread__comm_adjust(struct thread *self)
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200384{
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200385 char *comm = self->comm;
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200386
387 if (!col_width_list_str && !field_sep &&
388 (!comm_list || strlist__has_entry(comm_list, comm))) {
389 unsigned int slen = strlen(comm);
390
391 if (slen > comms__col_width) {
392 comms__col_width = slen;
393 threads__col_width = slen + 6;
394 }
395 }
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200396}
397
398static int thread__set_comm_adjust(struct thread *self, const char *comm)
399{
400 int ret = thread__set_comm(self, comm);
401
402 if (ret)
403 return ret;
404
405 thread__comm_adjust(self);
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200406
407 return 0;
408}
409
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200410static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200411{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200412 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200413 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200414
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200415 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200416}
417
Arnaldo Carvalho de Melo50e50952009-11-22 14:59:22 -0200418static struct symbol **resolve_callchain(struct thread *thread,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200419 struct perf_session *session,
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
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200427 if (session->use_callchain) {
Frederic Weisbecker44249612009-07-01 05:35:14 +0200428 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 Melo4aa65632009-12-13 19:50:29 -0200453 thread__find_addr_location(thread, session, cpumode,
454 MAP__FUNCTION, ip, &al, NULL);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200455 if (al.sym != NULL) {
456 if (sort__has_parent && !*parent &&
457 call__match(al.sym))
458 *parent = al.sym;
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200459 if (!session->use_callchain)
Frederic Weisbecker44249612009-07-01 05:35:14 +0200460 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 Melo4e4f06e2009-12-14 13:10:39 -0200472static int perf_session__add_hist_entry(struct perf_session *self,
473 struct addr_location *al,
474 struct ip_callchain *chain, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300475{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300476 struct symbol **syms = NULL, *parent = NULL;
477 bool hit;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200478 struct hist_entry *he;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300479
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200480 if ((sort__has_parent || self->use_callchain) && chain)
481 syms = resolve_callchain(al->thread, self, chain, &parent);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200482
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200483 he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300484 if (he == NULL)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200485 return -ENOMEM;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300486
487 if (hit)
488 he->count += count;
489
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200490 if (self->use_callchain) {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300491 if (!hit)
492 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200493 append_chain(&he->callchain, chain, syms);
494 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200495 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200496
497 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300498}
499
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200500static size_t perf_session__fprintf_hist_entries(struct perf_session *self,
501 u64 total_samples, FILE *fp)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300502{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200503 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +0200504 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300505 struct rb_node *nd;
506 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300507 unsigned int width;
508 char *col_width = col_width_list_str;
Brice Goglin9f866692009-08-10 15:26:32 +0200509 int raw_printing_style;
510
511 raw_printing_style = !strcmp(pretty_printing_style, "raw");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300512
Frederic Weisbecker25446032009-08-08 02:16:25 +0200513 init_rem_hits();
514
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300515 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +0200516 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200517
518 fprintf(fp, "# Overhead");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300519 if (show_nr_samples) {
520 if (field_sep)
521 fprintf(fp, "%cSamples", *field_sep);
522 else
523 fputs(" Samples ", fp);
524 }
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200525 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300526 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200527 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300528 if (field_sep) {
529 fprintf(fp, "%c%s", *field_sep, se->header);
530 continue;
531 }
532 width = strlen(se->header);
533 if (se->width) {
534 if (col_width_list_str) {
535 if (col_width) {
536 *se->width = atoi(col_width);
537 col_width = strchr(col_width, ',');
538 if (col_width)
539 ++col_width;
540 }
541 }
542 width = *se->width = max(*se->width, width);
543 }
544 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200545 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200546 fprintf(fp, "\n");
547
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300548 if (field_sep)
549 goto print_entries;
550
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200551 fprintf(fp, "# ........");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300552 if (show_nr_samples)
553 fprintf(fp, " ..........");
Ingo Molnar2d655372009-05-27 21:36:22 +0200554 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200555 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200556
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300557 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200558 continue;
559
Ingo Molnar4593bba2009-06-02 15:34:25 +0200560 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300561 if (se->width)
562 width = *se->width;
563 else
564 width = strlen(se->header);
565 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200566 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +0200567 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200568 fprintf(fp, "\n");
569
570 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +0200571
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300572print_entries:
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200573 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200574 pos = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200575 ret += hist_entry__fprintf(fp, pos, self, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300576 }
577
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200578 if (sort_order == default_sort_order &&
579 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200580 fprintf(fp, "#\n");
Pekka Enberg114cfab2009-08-05 13:25:21 +0300581 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200582 fprintf(fp, "#\n");
583 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200584 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +0200585
Frederic Weisbecker25446032009-08-08 02:16:25 +0200586 free(rem_sq_bracket);
587
Brice Goglin8d513272009-08-07 13:55:24 +0200588 if (show_threads)
Brice Goglin9f866692009-08-10 15:26:32 +0200589 perf_read_values_display(fp, &show_threads_values,
590 raw_printing_style);
Brice Goglin8d513272009-08-07 13:55:24 +0200591
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300592 return ret;
593}
594
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200595static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +0200596{
597 unsigned int chain_size;
598
Ingo Molnar75220602009-06-18 08:00:17 +0200599 chain_size = event->header.size;
600 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
601
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000602 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +0200603 return -1;
604
605 return 0;
606}
607
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200608static int process_sample_event(event_t *event, struct perf_session *session)
Ingo Molnar75051722009-06-03 23:14:49 +0200609{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900610 struct sample_data data;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000611 int cpumode;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200612 struct addr_location al;
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900613 struct thread *thread;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200614
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900615 memset(&data, 0, sizeof(data));
616 data.period = 1;
617
618 event__parse_sample(event, sample_type, &data);
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200619
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200620 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200621 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900622 data.pid, data.tid,
623 (void *)(long)data.ip,
624 (long long)data.period);
Ingo Molnar75051722009-06-03 23:14:49 +0200625
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200626 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200627 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200628
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900629 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200630
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900631 if (validate_chain(data.callchain, event) < 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200632 pr_debug("call-chain problem with event, "
633 "skipping it.\n");
Ingo Molnar75220602009-06-18 08:00:17 +0200634 return 0;
635 }
636
637 if (dump_trace) {
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900638 for (i = 0; i < data.callchain->nr; i++)
639 dump_printf("..... %2d: %016Lx\n",
640 i, data.callchain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200641 }
642 }
643
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200644 thread = perf_session__findnew(session, data.pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200645 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200646 pr_debug("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +0200647 event->header.type);
648 return -1;
649 }
650
Julia Lawallf39cdf22009-10-17 08:43:17 +0200651 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
652
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -0300653 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
654 return 0;
655
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200656 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Anton Blanchardd8db1b52009-07-01 09:00:48 +1000657
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200658 thread__find_addr_location(thread, session, cpumode,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900659 MAP__FUNCTION, data.ip, &al, NULL);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200660 /*
661 * We have to do this here as we may have a dso with no symbol hit that
662 * has a name longer than the ones with symbols sampled.
663 */
664 if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
665 dso__calc_col_width(al.map->dso);
Ingo Molnar75051722009-06-03 23:14:49 +0200666
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300667 if (dso_list &&
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200668 (!al.map || !al.map->dso ||
669 !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
670 (al.map->dso->short_name != al.map->dso->long_name &&
671 strlist__has_entry(dso_list, al.map->dso->long_name)))))
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300672 return 0;
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -0300673
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200674 if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name))
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300675 return 0;
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -0300676
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200677 if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200678 pr_debug("problem incrementing symbol count, skipping event\n");
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300679 return -1;
Ingo Molnar75051722009-06-03 23:14:49 +0200680 }
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300681
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900682 event__stats.total += data.period;
Ingo Molnar75051722009-06-03 23:14:49 +0200683
684 return 0;
685}
686
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200687static int process_comm_event(event_t *event, struct perf_session *session)
Ingo Molnar75051722009-06-03 23:14:49 +0200688{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200689 struct thread *thread = perf_session__findnew(session, event->comm.pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200690
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200691 dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
Ingo Molnar75051722009-06-03 23:14:49 +0200692
693 if (thread == NULL ||
Frederic Weisbecker4273b002009-08-18 16:03:46 +0200694 thread__set_comm_adjust(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200695 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar75051722009-06-03 23:14:49 +0200696 return -1;
697 }
Ingo Molnar75051722009-06-03 23:14:49 +0200698
699 return 0;
700}
701
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200702static int process_read_event(event_t *event, struct perf_session *session __used)
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200703{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200704 struct perf_event_attr *attr;
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200705
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200706 attr = perf_header__find_attr(event->read.id, &session->header);
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200707
Brice Goglin8d513272009-08-07 13:55:24 +0200708 if (show_threads) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200709 const char *name = attr ? __event_name(attr->type, attr->config)
Brice Goglin8d513272009-08-07 13:55:24 +0200710 : "unknown";
711 perf_read_values_add_value(&show_threads_values,
712 event->read.pid, event->read.tid,
713 event->read.id,
714 name,
715 event->read.value);
716 }
717
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200718 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
719 attr ? __event_name(attr->type, attr->config) : "FAIL",
720 event->read.value);
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +0200721
722 return 0;
723}
724
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200725static int sample_type_check(u64 type, struct perf_session *session)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300726{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200727 sample_type = type;
Ingo Molnar8465b052009-06-14 14:44:07 +0200728
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200729 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
730 if (sort__has_parent) {
731 fprintf(stderr, "selected --sort parent, but no"
732 " callchain data. Did you call"
733 " perf record without -g?\n");
734 return -1;
735 }
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200736 if (session->use_callchain) {
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200737 fprintf(stderr, "selected -g but no callchain data."
738 " Did you call perf record without"
739 " -g?\n");
740 return -1;
741 }
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200742 } else if (callchain_param.mode != CHAIN_NONE && !session->use_callchain) {
743 session->use_callchain = true;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200744 if (register_callchain_param(&callchain_param) < 0) {
745 fprintf(stderr, "Can't register callchain"
746 " params\n");
747 return -1;
748 }
Ingo Molnard80d3382009-06-03 23:14:49 +0200749 }
750
751 return 0;
752}
753
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200754static struct perf_event_ops event_ops = {
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200755 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200756 .process_mmap_event = event__process_mmap,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200757 .process_comm_event = process_comm_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200758 .process_exit_event = event__process_task,
759 .process_fork_event = event__process_task,
760 .process_lost_event = event__process_lost,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200761 .process_read_event = process_read_event,
762 .sample_type_check = sample_type_check,
763};
764
765
Ingo Molnard80d3382009-06-03 23:14:49 +0200766static int __cmd_report(void)
767{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200768 int ret;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200769 struct perf_session *session;
Ingo Molnard80d3382009-06-03 23:14:49 +0200770
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200771 session = perf_session__new(input_name, O_RDONLY, force, &symbol_conf);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200772 if (session == NULL)
773 return -ENOMEM;
774
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200775 session->use_callchain = use_callchain;
776
Brice Goglin8d513272009-08-07 13:55:24 +0200777 if (show_threads)
778 perf_read_values_init(&show_threads_values);
779
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200780 ret = perf_session__process_events(session, &event_ops);
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200781 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200782 goto out_delete;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200783
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200784 if (dump_trace) {
785 event__print_totals();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200786 goto out_delete;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200787 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200788
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300789 if (verbose > 3)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200790 perf_session__fprintf(session, stdout);
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300791
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300792 if (verbose > 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +0200793 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200794
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200795 perf_session__collapse_resort(session);
796 perf_session__output_resort(session, event__stats.total);
797 perf_session__fprintf_hist_entries(session, event__stats.total, stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300798
Brice Goglin8d513272009-08-07 13:55:24 +0200799 if (show_threads)
800 perf_read_values_destroy(&show_threads_values);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200801out_delete:
802 perf_session__delete(session);
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
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200813 use_callchain = true;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200814
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;
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200834 use_callchain = true;
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +0200835
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 Meloec913362009-12-13 19:50:27 -0200885 OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -0300886 "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}