blob: 6321951fe1bfd6706817d5c0ea0243a855ed03eb [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"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020023#include "util/header.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020024
25#include "util/parse-options.h"
26#include "util/parse-events.h"
27
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030028#define SHOW_KERNEL 1
29#define SHOW_USER 2
30#define SHOW_HV 4
31
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020032static char const *input_name = "perf.data";
Ingo Molnarbd741372009-06-04 14:13:04 +020033
Pekka Enberg114cfab2009-08-05 13:25:21 +030034static char default_sort_order[] = "comm,dso,symbol";
Ingo Molnarbd741372009-06-04 14:13:04 +020035static char *sort_order = default_sort_order;
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;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030039static char *field_sep;
Ingo Molnarbd741372009-06-04 14:13:04 +020040
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030041static int input;
42static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
43
Ingo Molnar97b07b62009-05-26 18:48:58 +020044static int dump_trace = 0;
Ingo Molnar35029732009-06-03 09:38:58 +020045#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020046#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
Ingo Molnar35029732009-06-03 09:38:58 +020047
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -030048static int full_paths;
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -030049static int show_nr_samples;
Ingo Molnar97b07b62009-05-26 18:48:58 +020050
Brice Goglin8d513272009-08-07 13:55:24 +020051static int show_threads;
52static struct perf_read_values show_threads_values;
53
Brice Goglin9f866692009-08-10 15:26:32 +020054static char default_pretty_printing_style[] = "normal";
55static char *pretty_printing_style = default_pretty_printing_style;
56
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030057static unsigned long page_size;
58static unsigned long mmap_window = 32;
59
Ingo Molnarb8e6d822009-06-18 14:32:19 +020060static char default_parent_pattern[] = "^sys_|^do_page_fault";
61static char *parent_pattern = default_parent_pattern;
Ingo Molnarb25bcf22009-06-18 07:01:03 +020062static regex_t parent_regex;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +020063
Ingo Molnarb8e6d822009-06-18 14:32:19 +020064static int exclude_other = 1;
Frederic Weisbeckerbe903882009-07-05 07:39:18 +020065
Frederic Weisbecker805d1272009-07-05 07:39:21 +020066static char callchain_default_opt[] = "fractal,0.5";
67
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +020068static int callchain;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020069
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020070static char __cwd[PATH_MAX];
71static char *cwd = __cwd;
72static int cwdlen;
73
Frederic Weisbecker805d1272009-07-05 07:39:21 +020074static
75struct callchain_param callchain_param = {
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +020076 .mode = CHAIN_GRAPH_REL,
Frederic Weisbecker805d1272009-07-05 07:39:21 +020077 .min_percent = 0.5
78};
Ingo Molnarb8e6d822009-06-18 14:32:19 +020079
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020080static u64 sample_type;
81
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030082static int repsep_fprintf(FILE *fp, const char *fmt, ...)
83{
84 int n;
85 va_list ap;
86
87 va_start(ap, fmt);
88 if (!field_sep)
89 n = vfprintf(fp, fmt, ap);
90 else {
91 char *bf = NULL;
92 n = vasprintf(&bf, fmt, ap);
93 if (n > 0) {
94 char *sep = bf;
95 while (1) {
96 sep = strchr(sep, *field_sep);
97 if (sep == NULL)
98 break;
99 *sep = '.';
100 }
101 }
102 fputs(bf, fp);
103 free(bf);
104 }
105 va_end(ap);
106 return n;
107}
108
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300109struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300110 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300111 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300112 pid_t pid;
113 char *comm;
114};
115
116static struct thread *thread__new(pid_t pid)
117{
118 struct thread *self = malloc(sizeof(*self));
119
120 if (self != NULL) {
121 self->pid = pid;
Peter Zijlstra82292892009-06-03 12:37:36 +0200122 self->comm = malloc(32);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200123 if (self->comm)
Peter Zijlstra82292892009-06-03 12:37:36 +0200124 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300125 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300126 }
127
128 return self;
129}
130
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300131static unsigned int dsos__col_width,
132 comms__col_width,
133 threads__col_width;
134
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300135static int thread__set_comm(struct thread *self, const char *comm)
136{
Peter Zijlstra82292892009-06-03 12:37:36 +0200137 if (self->comm)
138 free(self->comm);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300139 self->comm = strdup(comm);
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300140 if (!self->comm)
141 return -ENOMEM;
142
143 if (!col_width_list_str && !field_sep &&
144 (!comm_list || strlist__has_entry(comm_list, comm))) {
145 unsigned int slen = strlen(comm);
146 if (slen > comms__col_width) {
147 comms__col_width = slen;
148 threads__col_width = slen + 6;
149 }
150 }
151
152 return 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300153}
154
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300155static size_t thread__fprintf(struct thread *self, FILE *fp)
156{
157 struct map *pos;
158 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
159
160 list_for_each_entry(pos, &self->maps, node)
161 ret += map__fprintf(pos, fp);
162
163 return ret;
164}
165
166
Ingo Molnar16f762a2009-05-27 09:10:38 +0200167static struct rb_root threads;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200168static struct thread *last_match;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300169
170static struct thread *threads__findnew(pid_t pid)
171{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300172 struct rb_node **p = &threads.rb_node;
173 struct rb_node *parent = NULL;
174 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300175
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200176 /*
177 * Font-end cache - PID lookups come in blocks,
178 * so most of the time we dont have to look up
179 * the full rbtree:
180 */
181 if (last_match && last_match->pid == pid)
182 return last_match;
183
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300184 while (*p != NULL) {
185 parent = *p;
186 th = rb_entry(parent, struct thread, rb_node);
187
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200188 if (th->pid == pid) {
189 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300190 return th;
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200191 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300192
193 if (pid < th->pid)
194 p = &(*p)->rb_left;
195 else
196 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300197 }
198
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300199 th = thread__new(pid);
200 if (th != NULL) {
201 rb_link_node(&th->rb_node, parent, p);
202 rb_insert_color(&th->rb_node, &threads);
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200203 last_match = th;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300204 }
Ingo Molnareed4dcd2009-06-03 19:59:24 +0200205
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300206 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300207}
208
209static void thread__insert_map(struct thread *self, struct map *map)
210{
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200211 struct map *pos, *tmp;
212
213 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
214 if (map__overlap(pos, map)) {
Peter Zijlstra3d906ef2009-06-23 11:23:07 +0200215 if (verbose >= 2) {
216 printf("overlapping maps:\n");
217 map__fprintf(map, stdout);
218 map__fprintf(pos, stdout);
219 }
220
221 if (map->start <= pos->start && map->end > pos->start)
222 pos->start = map->end;
223
224 if (map->end >= pos->end && map->start < pos->end)
225 pos->end = map->start;
226
227 if (verbose >= 2) {
228 printf("after collision:\n");
229 map__fprintf(pos, stdout);
230 }
231
232 if (pos->start >= pos->end) {
233 list_del_init(&pos->node);
234 free(pos);
235 }
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200236 }
237 }
238
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300239 list_add_tail(&map->node, &self->maps);
240}
241
Peter Zijlstra62fc4452009-06-04 16:53:49 +0200242static int thread__fork(struct thread *self, struct thread *parent)
243{
244 struct map *map;
245
246 if (self->comm)
247 free(self->comm);
248 self->comm = strdup(parent->comm);
249 if (!self->comm)
250 return -ENOMEM;
251
252 list_for_each_entry(map, &parent->maps, node) {
253 struct map *new = map__clone(map);
254 if (!new)
255 return -ENOMEM;
256 thread__insert_map(self, new);
257 }
258
259 return 0;
260}
261
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000262static struct map *thread__find_map(struct thread *self, u64 ip)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300263{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200264 struct map *pos;
265
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300266 if (self == NULL)
267 return NULL;
268
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300269 list_for_each_entry(pos, &self->maps, node)
270 if (ip >= pos->start && ip <= pos->end)
271 return pos;
272
273 return NULL;
274}
275
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -0300276static size_t threads__fprintf(FILE *fp)
277{
278 size_t ret = 0;
279 struct rb_node *nd;
280
281 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
282 struct thread *pos = rb_entry(nd, struct thread, rb_node);
283
284 ret += thread__fprintf(pos, fp);
285 }
286
287 return ret;
288}
289
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200290/*
291 * histogram, sorted on item, collects counts
292 */
293
294static struct rb_root hist;
295
296struct hist_entry {
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200297 struct rb_node rb_node;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200298
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200299 struct thread *thread;
300 struct map *map;
301 struct dso *dso;
302 struct symbol *sym;
303 struct symbol *parent;
304 u64 ip;
305 char level;
306 struct callchain_node callchain;
307 struct rb_root sorted_chain;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200308
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200309 u64 count;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200310};
311
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200312/*
313 * configurable sorting bits
314 */
315
316struct sort_entry {
317 struct list_head list;
318
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200319 char *header;
320
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200321 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
Peter Zijlstra82292892009-06-03 12:37:36 +0200322 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300323 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
324 unsigned int *width;
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300325 bool elide;
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200326};
327
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200328static int64_t cmp_null(void *l, void *r)
329{
330 if (!l && !r)
331 return 0;
332 else if (!l)
333 return -1;
334 else
335 return 1;
336}
337
Peter Zijlstra82292892009-06-03 12:37:36 +0200338/* --sort pid */
339
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200340static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200341sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
342{
343 return right->thread->pid - left->thread->pid;
344}
345
346static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300347sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200348{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300349 return repsep_fprintf(fp, "%*s:%5d", width - 6,
350 self->thread->comm ?: "", self->thread->pid);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200351}
352
353static struct sort_entry sort_thread = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300354 .header = "Command: Pid",
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200355 .cmp = sort__thread_cmp,
356 .print = sort__thread_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300357 .width = &threads__col_width,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200358};
359
Peter Zijlstra82292892009-06-03 12:37:36 +0200360/* --sort comm */
361
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200362static int64_t
Peter Zijlstra992444b2009-05-27 20:20:27 +0200363sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
364{
Peter Zijlstra82292892009-06-03 12:37:36 +0200365 return right->thread->pid - left->thread->pid;
366}
367
368static int64_t
369sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
370{
Peter Zijlstra992444b2009-05-27 20:20:27 +0200371 char *comm_l = left->thread->comm;
372 char *comm_r = right->thread->comm;
373
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200374 if (!comm_l || !comm_r)
375 return cmp_null(comm_l, comm_r);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200376
377 return strcmp(comm_l, comm_r);
378}
379
380static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300381sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra992444b2009-05-27 20:20:27 +0200382{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300383 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
Peter Zijlstra992444b2009-05-27 20:20:27 +0200384}
385
386static struct sort_entry sort_comm = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300387 .header = "Command",
Peter Zijlstra82292892009-06-03 12:37:36 +0200388 .cmp = sort__comm_cmp,
389 .collapse = sort__comm_collapse,
390 .print = sort__comm_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300391 .width = &comms__col_width,
Peter Zijlstra992444b2009-05-27 20:20:27 +0200392};
393
Peter Zijlstra82292892009-06-03 12:37:36 +0200394/* --sort dso */
395
Peter Zijlstra992444b2009-05-27 20:20:27 +0200396static int64_t
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200397sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
398{
399 struct dso *dso_l = left->dso;
400 struct dso *dso_r = right->dso;
401
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200402 if (!dso_l || !dso_r)
403 return cmp_null(dso_l, dso_r);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200404
405 return strcmp(dso_l->name, dso_r->name);
406}
407
408static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300409sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200410{
Ingo Molnar0a520c62009-06-02 23:24:45 +0200411 if (self->dso)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300412 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
Ingo Molnar0a520c62009-06-02 23:24:45 +0200413
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300414 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200415}
416
417static struct sort_entry sort_dso = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300418 .header = "Shared Object",
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200419 .cmp = sort__dso_cmp,
420 .print = sort__dso_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300421 .width = &dsos__col_width,
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200422};
423
Peter Zijlstra82292892009-06-03 12:37:36 +0200424/* --sort symbol */
425
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200426static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200427sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300428{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000429 u64 ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200430
431 if (left->sym == right->sym)
432 return 0;
433
434 ip_l = left->sym ? left->sym->start : left->ip;
435 ip_r = right->sym ? right->sym->start : right->ip;
436
437 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300438}
439
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200440static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300441sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200442{
443 size_t ret = 0;
444
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200445 if (verbose)
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300446 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
447 dso__symtab_origin(self->dso));
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200448
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300449 ret += repsep_fprintf(fp, "[%c] ", self->level);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200450 if (self->sym) {
Arnaldo Carvalho de Melo60c1baf2009-07-11 12:18:33 -0300451 ret += repsep_fprintf(fp, "%s", self->sym->name);
Mike Galbraith42976482009-07-02 08:09:46 +0200452
453 if (self->sym->module)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300454 ret += repsep_fprintf(fp, "\t[%s]",
455 self->sym->module->name);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200456 } else {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300457 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8edd4282009-06-05 14:13:18 +0200458 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200459
460 return ret;
461}
462
463static struct sort_entry sort_sym = {
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200464 .header = "Symbol",
Peter Zijlstraca8cdee2009-05-28 11:08:33 +0200465 .cmp = sort__sym_cmp,
466 .print = sort__sym_print,
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200467};
468
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200469/* --sort parent */
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200470
471static int64_t
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200472sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200473{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200474 struct symbol *sym_l = left->parent;
475 struct symbol *sym_r = right->parent;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200476
477 if (!sym_l || !sym_r)
478 return cmp_null(sym_l, sym_r);
479
480 return strcmp(sym_l->name, sym_r->name);
481}
482
483static size_t
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300484sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200485{
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300486 return repsep_fprintf(fp, "%-*s", width,
487 self->parent ? self->parent->name : "[other]");
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200488}
489
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300490static unsigned int parent_symbol__col_width;
491
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200492static struct sort_entry sort_parent = {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300493 .header = "Parent symbol",
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200494 .cmp = sort__parent_cmp,
495 .print = sort__parent_print,
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300496 .width = &parent_symbol__col_width,
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200497};
498
Peter Zijlstra82292892009-06-03 12:37:36 +0200499static int sort__need_collapse = 0;
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200500static int sort__has_parent = 0;
Peter Zijlstra82292892009-06-03 12:37:36 +0200501
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200502struct sort_dimension {
Ingo Molnar8edd4282009-06-05 14:13:18 +0200503 char *name;
504 struct sort_entry *entry;
505 int taken;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200506};
507
508static struct sort_dimension sort_dimensions[] = {
509 { .name = "pid", .entry = &sort_thread, },
Peter Zijlstra992444b2009-05-27 20:20:27 +0200510 { .name = "comm", .entry = &sort_comm, },
Peter Zijlstra55e5ec42009-05-27 20:20:28 +0200511 { .name = "dso", .entry = &sort_dso, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200512 { .name = "symbol", .entry = &sort_sym, },
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200513 { .name = "parent", .entry = &sort_parent, },
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200514};
515
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200516static LIST_HEAD(hist_entry__sort_list);
517
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200518static int sort_dimension__add(char *tok)
519{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200520 unsigned int i;
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200521
522 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
523 struct sort_dimension *sd = &sort_dimensions[i];
524
525 if (sd->taken)
526 continue;
527
Ingo Molnar5352f352009-06-03 10:07:39 +0200528 if (strncasecmp(tok, sd->name, strlen(tok)))
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200529 continue;
530
Peter Zijlstra82292892009-06-03 12:37:36 +0200531 if (sd->entry->collapse)
532 sort__need_collapse = 1;
533
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200534 if (sd->entry == &sort_parent) {
535 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200536 if (ret) {
537 char err[BUFSIZ];
538
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200539 regerror(ret, &parent_regex, err, sizeof(err));
540 fprintf(stderr, "Invalid regex: %s\n%s",
541 parent_pattern, err);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200542 exit(-1);
543 }
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200544 sort__has_parent = 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200545 }
546
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200547 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
548 sd->taken = 1;
Ingo Molnar5352f352009-06-03 10:07:39 +0200549
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200550 return 0;
551 }
552
553 return -ESRCH;
554}
555
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200556static int64_t
557hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
558{
559 struct sort_entry *se;
560 int64_t cmp = 0;
561
562 list_for_each_entry(se, &hist_entry__sort_list, list) {
563 cmp = se->cmp(left, right);
564 if (cmp)
565 break;
566 }
567
568 return cmp;
569}
570
Peter Zijlstra82292892009-06-03 12:37:36 +0200571static int64_t
572hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
573{
574 struct sort_entry *se;
575 int64_t cmp = 0;
576
577 list_for_each_entry(se, &hist_entry__sort_list, list) {
578 int64_t (*f)(struct hist_entry *, struct hist_entry *);
579
580 f = se->collapse ?: se->cmp;
581
582 cmp = f(left, right);
583 if (cmp)
584 break;
585 }
586
587 return cmp;
588}
589
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200590static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
591{
592 int i;
593 size_t ret = 0;
594
595 ret += fprintf(fp, "%s", " ");
596
597 for (i = 0; i < depth; i++)
598 if (depth_mask & (1 << i))
599 ret += fprintf(fp, "| ");
600 else
601 ret += fprintf(fp, " ");
602
603 ret += fprintf(fp, "\n");
604
605 return ret;
606}
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200607static size_t
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200608ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
609 int depth_mask, int count, u64 total_samples,
610 int hits)
611{
612 int i;
613 size_t ret = 0;
614
615 ret += fprintf(fp, "%s", " ");
616 for (i = 0; i < depth; i++) {
617 if (depth_mask & (1 << i))
618 ret += fprintf(fp, "|");
619 else
620 ret += fprintf(fp, " ");
621 if (!count && i == depth - 1) {
622 double percent;
623
624 percent = hits * 100.0 / total_samples;
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200625 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200626 } else
627 ret += fprintf(fp, "%s", " ");
628 }
629 if (chain->sym)
630 ret += fprintf(fp, "%s\n", chain->sym->name);
631 else
632 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
633
634 return ret;
635}
636
Frederic Weisbecker25446032009-08-08 02:16:25 +0200637static struct symbol *rem_sq_bracket;
638static struct callchain_list rem_hits;
639
640static void init_rem_hits(void)
641{
642 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
643 if (!rem_sq_bracket) {
644 fprintf(stderr, "Not enough memory to display remaining hits\n");
645 return;
646 }
647
648 strcpy(rem_sq_bracket->name, "[...]");
649 rem_hits.sym = rem_sq_bracket;
650}
651
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200652static size_t
653callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
654 u64 total_samples, int depth, int depth_mask)
655{
656 struct rb_node *node, *next;
657 struct callchain_node *child;
658 struct callchain_list *chain;
659 int new_depth_mask = depth_mask;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200660 u64 new_total;
Frederic Weisbecker25446032009-08-08 02:16:25 +0200661 u64 remaining;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200662 size_t ret = 0;
663 int i;
664
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200665 if (callchain_param.mode == CHAIN_GRAPH_REL)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200666 new_total = self->children_hit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200667 else
668 new_total = total_samples;
669
Frederic Weisbecker25446032009-08-08 02:16:25 +0200670 remaining = new_total;
671
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200672 node = rb_first(&self->rb_root);
673 while (node) {
Frederic Weisbecker25446032009-08-08 02:16:25 +0200674 u64 cumul;
675
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200676 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200677 cumul = cumul_hits(child);
678 remaining -= cumul;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200679
680 /*
681 * The depth mask manages the output of pipes that show
682 * the depth. We don't want to keep the pipes of the current
Frederic Weisbecker25446032009-08-08 02:16:25 +0200683 * level for the last child of this depth.
684 * Except if we have remaining filtered hits. They will
685 * supersede the last child
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200686 */
687 next = rb_next(node);
Frederic Weisbecker25446032009-08-08 02:16:25 +0200688 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200689 new_depth_mask &= ~(1 << (depth - 1));
690
691 /*
692 * But we keep the older depth mask for the line seperator
693 * to keep the level link until we reach the last child
694 */
695 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
696 i = 0;
697 list_for_each_entry(chain, &child->val, list) {
698 if (chain->ip >= PERF_CONTEXT_MAX)
699 continue;
700 ret += ipchain__fprintf_graph(fp, chain, depth,
701 new_depth_mask, i++,
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200702 new_total,
Frederic Weisbecker25446032009-08-08 02:16:25 +0200703 cumul);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200704 }
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200705 ret += callchain__fprintf_graph(fp, child, new_total,
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200706 depth + 1,
707 new_depth_mask | (1 << depth));
708 node = next;
709 }
710
Frederic Weisbecker25446032009-08-08 02:16:25 +0200711 if (callchain_param.mode == CHAIN_GRAPH_REL &&
712 remaining && remaining != new_total) {
713
714 if (!rem_sq_bracket)
715 return ret;
716
717 new_depth_mask &= ~(1 << (depth - 1));
718
719 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
720 new_depth_mask, 0, new_total,
721 remaining);
722 }
723
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200724 return ret;
725}
726
727static size_t
728callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
729 u64 total_samples)
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200730{
731 struct callchain_list *chain;
732 size_t ret = 0;
733
734 if (!self)
735 return 0;
736
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200737 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200738
739
Frederic Weisbecker44249612009-07-01 05:35:14 +0200740 list_for_each_entry(chain, &self->val, list) {
741 if (chain->ip >= PERF_CONTEXT_MAX)
742 continue;
743 if (chain->sym)
744 ret += fprintf(fp, " %s\n", chain->sym->name);
745 else
746 ret += fprintf(fp, " %p\n",
Ingo Molnarf37a2912009-07-01 12:37:06 +0200747 (void *)(long)chain->ip);
Frederic Weisbecker44249612009-07-01 05:35:14 +0200748 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200749
750 return ret;
751}
752
753static size_t
754hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
755 u64 total_samples)
756{
757 struct rb_node *rb_node;
758 struct callchain_node *chain;
759 size_t ret = 0;
760
761 rb_node = rb_first(&self->sorted_chain);
762 while (rb_node) {
763 double percent;
764
765 chain = rb_entry(rb_node, struct callchain_node, rb_node);
766 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200767 switch (callchain_param.mode) {
768 case CHAIN_FLAT:
Frederic Weisbecker24b57c62009-07-02 20:14:35 +0200769 ret += percent_color_fprintf(fp, " %6.2f%%\n",
770 percent);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200771 ret += callchain__fprintf_flat(fp, chain, total_samples);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200772 break;
773 case CHAIN_GRAPH_ABS: /* Falldown */
774 case CHAIN_GRAPH_REL:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200775 ret += callchain__fprintf_graph(fp, chain,
776 total_samples, 1, 1);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200777 default:
778 break;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200779 }
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200780 ret += fprintf(fp, "\n");
781 rb_node = rb_next(rb_node);
782 }
783
784 return ret;
785}
786
787
788static size_t
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000789hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200790{
791 struct sort_entry *se;
792 size_t ret;
793
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200794 if (exclude_other && !self->parent)
795 return 0;
796
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200797 if (total_samples)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300798 ret = percent_color_fprintf(fp,
799 field_sep ? "%.2f" : " %6.2f%%",
800 (self->count * 100.0) / total_samples);
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200801 else
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300802 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200803
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -0300804 if (show_nr_samples) {
805 if (field_sep)
806 fprintf(fp, "%c%lld", *field_sep, self->count);
807 else
808 fprintf(fp, "%11lld", self->count);
809 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200810
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200811 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300812 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200813 continue;
814
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300815 fprintf(fp, "%s", field_sep ?: " ");
816 ret += se->print(fp, self, se->width ? *se->width : 0);
Peter Zijlstra71dd8942009-06-04 15:16:56 +0200817 }
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200818
819 ret += fprintf(fp, "\n");
820
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200821 if (callchain)
822 hist_entry_callchain__fprintf(fp, self, total_samples);
823
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200824 return ret;
825}
826
827/*
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200828 *
829 */
830
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300831static void dso__calc_col_width(struct dso *self)
832{
833 if (!col_width_list_str && !field_sep &&
834 (!dso_list || strlist__has_entry(dso_list, self->name))) {
835 unsigned int slen = strlen(self->name);
836 if (slen > dsos__col_width)
837 dsos__col_width = slen;
838 }
839
840 self->slen_calculated = 1;
841}
842
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200843static struct symbol *
844resolve_symbol(struct thread *thread, struct map **mapp,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000845 struct dso **dsop, u64 *ipp)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200846{
847 struct dso *dso = dsop ? *dsop : NULL;
848 struct map *map = mapp ? *mapp : NULL;
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200849 u64 ip = *ipp;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200850
851 if (!thread)
852 return NULL;
853
854 if (dso)
855 goto got_dso;
856
857 if (map)
858 goto got_map;
859
860 map = thread__find_map(thread, ip);
861 if (map != NULL) {
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300862 /*
863 * We have to do this here as we may have a dso
864 * with no symbol hit that has a name longer than
865 * the ones with symbols sampled.
866 */
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -0300867 if (!sort_dso.elide && !map->dso->slen_calculated)
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300868 dso__calc_col_width(map->dso);
869
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200870 if (mapp)
871 *mapp = map;
872got_map:
873 ip = map->map_ip(map, ip);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200874
875 dso = map->dso;
876 } else {
877 /*
878 * If this is outside of all known maps,
879 * and is a negative address, try to look it
880 * up in the kernel dso, as it might be a
881 * vsyscall (which executes in user-mode):
882 */
883 if ((long long)ip < 0)
884 dso = kernel_dso;
885 }
886 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200887 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
888 *ipp = ip;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200889
890 if (dsop)
891 *dsop = dso;
892
893 if (!dso)
894 return NULL;
895got_dso:
896 return dso->find_symbol(dso, ip);
897}
898
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200899static int call__match(struct symbol *sym)
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200900{
Ingo Molnarb25bcf22009-06-18 07:01:03 +0200901 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200902 return 1;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200903
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +0200904 return 0;
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200905}
906
Frederic Weisbecker44249612009-07-01 05:35:14 +0200907static struct symbol **
Ingo Molnarf37a2912009-07-01 12:37:06 +0200908resolve_callchain(struct thread *thread, struct map *map __used,
Frederic Weisbecker44249612009-07-01 05:35:14 +0200909 struct ip_callchain *chain, struct hist_entry *entry)
910{
Frederic Weisbecker44249612009-07-01 05:35:14 +0200911 u64 context = PERF_CONTEXT_MAX;
Ingo Molnar029e5b12009-07-03 13:17:28 +0200912 struct symbol **syms = NULL;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200913 unsigned int i;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200914
915 if (callchain) {
916 syms = calloc(chain->nr, sizeof(*syms));
917 if (!syms) {
918 fprintf(stderr, "Can't allocate memory for symbols\n");
919 exit(-1);
920 }
921 }
922
923 for (i = 0; i < chain->nr; i++) {
924 u64 ip = chain->ips[i];
925 struct dso *dso = NULL;
926 struct symbol *sym;
927
928 if (ip >= PERF_CONTEXT_MAX) {
929 context = ip;
930 continue;
931 }
932
933 switch (context) {
Ingo Molnar88a69df2009-07-01 11:17:20 +0200934 case PERF_CONTEXT_HV:
935 dso = hypervisor_dso;
936 break;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200937 case PERF_CONTEXT_KERNEL:
938 dso = kernel_dso;
939 break;
940 default:
941 break;
942 }
943
944 sym = resolve_symbol(thread, NULL, &dso, &ip);
945
946 if (sym) {
947 if (sort__has_parent && call__match(sym) &&
948 !entry->parent)
949 entry->parent = sym;
950 if (!callchain)
951 break;
952 syms[i] = sym;
953 }
954 }
955
956 return syms;
957}
958
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200959/*
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200960 * collect histogram counts
961 */
962
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200963static int
964hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000965 struct symbol *sym, u64 ip, struct ip_callchain *chain,
966 char level, u64 count)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300967{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200968 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300969 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200970 struct hist_entry *he;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200971 struct symbol **syms = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200972 struct hist_entry entry = {
973 .thread = thread,
974 .map = map,
975 .dso = dso,
976 .sym = sym,
977 .ip = ip,
978 .level = level,
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200979 .count = count,
Ingo Molnarb8e6d822009-06-18 14:32:19 +0200980 .parent = NULL,
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +0200981 .sorted_chain = RB_ROOT
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200982 };
983 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300984
Frederic Weisbecker44249612009-07-01 05:35:14 +0200985 if ((sort__has_parent || callchain) && chain)
986 syms = resolve_callchain(thread, map, chain, &entry);
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +0200987
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300988 while (*p != NULL) {
989 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200990 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300991
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200992 cmp = hist_entry__cmp(&entry, he);
993
994 if (!cmp) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200995 he->count += count;
Frederic Weisbecker44249612009-07-01 05:35:14 +0200996 if (callchain) {
997 append_chain(&he->callchain, chain, syms);
998 free(syms);
999 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001000 return 0;
1001 }
1002
1003 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001004 p = &(*p)->rb_left;
1005 else
1006 p = &(*p)->rb_right;
1007 }
1008
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001009 he = malloc(sizeof(*he));
1010 if (!he)
1011 return -ENOMEM;
1012 *he = entry;
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001013 if (callchain) {
1014 callchain_init(&he->callchain);
Frederic Weisbecker44249612009-07-01 05:35:14 +02001015 append_chain(&he->callchain, chain, syms);
1016 free(syms);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001017 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001018 rb_link_node(&he->rb_node, parent, p);
1019 rb_insert_color(&he->rb_node, &hist);
1020
1021 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001022}
1023
Peter Zijlstra82292892009-06-03 12:37:36 +02001024static void hist_entry__free(struct hist_entry *he)
1025{
1026 free(he);
1027}
1028
1029/*
1030 * collapse the histogram
1031 */
1032
1033static struct rb_root collapse_hists;
1034
1035static void collapse__insert_entry(struct hist_entry *he)
1036{
1037 struct rb_node **p = &collapse_hists.rb_node;
1038 struct rb_node *parent = NULL;
1039 struct hist_entry *iter;
1040 int64_t cmp;
1041
1042 while (*p != NULL) {
1043 parent = *p;
1044 iter = rb_entry(parent, struct hist_entry, rb_node);
1045
1046 cmp = hist_entry__collapse(iter, he);
1047
1048 if (!cmp) {
1049 iter->count += he->count;
1050 hist_entry__free(he);
1051 return;
1052 }
1053
1054 if (cmp < 0)
1055 p = &(*p)->rb_left;
1056 else
1057 p = &(*p)->rb_right;
1058 }
1059
1060 rb_link_node(&he->rb_node, parent, p);
1061 rb_insert_color(&he->rb_node, &collapse_hists);
1062}
1063
1064static void collapse__resort(void)
1065{
1066 struct rb_node *next;
1067 struct hist_entry *n;
1068
1069 if (!sort__need_collapse)
1070 return;
1071
1072 next = rb_first(&hist);
1073 while (next) {
1074 n = rb_entry(next, struct hist_entry, rb_node);
1075 next = rb_next(&n->rb_node);
1076
1077 rb_erase(&n->rb_node, &hist);
1078 collapse__insert_entry(n);
1079 }
1080}
1081
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001082/*
1083 * reverse the map, sort on count.
1084 */
1085
1086static struct rb_root output_hists;
1087
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001088static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001089{
1090 struct rb_node **p = &output_hists.rb_node;
1091 struct rb_node *parent = NULL;
1092 struct hist_entry *iter;
1093
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001094 if (callchain)
1095 callchain_param.sort(&he->sorted_chain, &he->callchain,
1096 min_callchain_hits, &callchain_param);
Frederic Weisbeckerf55c5552009-06-26 16:28:01 +02001097
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001098 while (*p != NULL) {
1099 parent = *p;
1100 iter = rb_entry(parent, struct hist_entry, rb_node);
1101
1102 if (he->count > iter->count)
1103 p = &(*p)->rb_left;
1104 else
1105 p = &(*p)->rb_right;
1106 }
1107
1108 rb_link_node(&he->rb_node, parent, p);
1109 rb_insert_color(&he->rb_node, &output_hists);
1110}
1111
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001112static void output__resort(u64 total_samples)
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001113{
Peter Zijlstra82292892009-06-03 12:37:36 +02001114 struct rb_node *next;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001115 struct hist_entry *n;
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001116 struct rb_root *tree = &hist;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001117 u64 min_callchain_hits;
1118
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001119 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001120
Peter Zijlstra82292892009-06-03 12:37:36 +02001121 if (sort__need_collapse)
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001122 tree = &collapse_hists;
1123
1124 next = rb_first(tree);
Peter Zijlstra82292892009-06-03 12:37:36 +02001125
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001126 while (next) {
1127 n = rb_entry(next, struct hist_entry, rb_node);
1128 next = rb_next(&n->rb_node);
1129
Arnaldo Carvalho de Meloa4c43be2009-06-03 23:02:33 -03001130 rb_erase(&n->rb_node, tree);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001131 output__insert_entry(n, min_callchain_hits);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001132 }
1133}
1134
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001135static size_t output__fprintf(FILE *fp, u64 total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001136{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001137 struct hist_entry *pos;
Ingo Molnar2d655372009-05-27 21:36:22 +02001138 struct sort_entry *se;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001139 struct rb_node *nd;
1140 size_t ret = 0;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001141 unsigned int width;
1142 char *col_width = col_width_list_str;
Brice Goglin9f866692009-08-10 15:26:32 +02001143 int raw_printing_style;
1144
1145 raw_printing_style = !strcmp(pretty_printing_style, "raw");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001146
Frederic Weisbecker25446032009-08-08 02:16:25 +02001147 init_rem_hits();
1148
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001149 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
Ingo Molnar05ca0612009-06-04 14:21:16 +02001150 fprintf(fp, "#\n");
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001151
1152 fprintf(fp, "# Overhead");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001153 if (show_nr_samples) {
1154 if (field_sep)
1155 fprintf(fp, "%cSamples", *field_sep);
1156 else
1157 fputs(" Samples ", fp);
1158 }
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001159 list_for_each_entry(se, &hist_entry__sort_list, list) {
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001160 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001161 continue;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001162 if (field_sep) {
1163 fprintf(fp, "%c%s", *field_sep, se->header);
1164 continue;
1165 }
1166 width = strlen(se->header);
1167 if (se->width) {
1168 if (col_width_list_str) {
1169 if (col_width) {
1170 *se->width = atoi(col_width);
1171 col_width = strchr(col_width, ',');
1172 if (col_width)
1173 ++col_width;
1174 }
1175 }
1176 width = *se->width = max(*se->width, width);
1177 }
1178 fprintf(fp, " %*s", width, se->header);
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001179 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001180 fprintf(fp, "\n");
1181
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001182 if (field_sep)
1183 goto print_entries;
1184
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001185 fprintf(fp, "# ........");
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001186 if (show_nr_samples)
1187 fprintf(fp, " ..........");
Ingo Molnar2d655372009-05-27 21:36:22 +02001188 list_for_each_entry(se, &hist_entry__sort_list, list) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001189 unsigned int i;
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001190
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001191 if (se->elide)
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001192 continue;
1193
Ingo Molnar4593bba2009-06-02 15:34:25 +02001194 fprintf(fp, " ");
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001195 if (se->width)
1196 width = *se->width;
1197 else
1198 width = strlen(se->header);
1199 for (i = 0; i < width; i++)
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001200 fprintf(fp, ".");
Ingo Molnar2d655372009-05-27 21:36:22 +02001201 }
Peter Zijlstraca8cdee2009-05-28 11:08:33 +02001202 fprintf(fp, "\n");
1203
1204 fprintf(fp, "#\n");
Ingo Molnar2d655372009-05-27 21:36:22 +02001205
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001206print_entries:
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001207 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1208 pos = rb_entry(nd, struct hist_entry, rb_node);
1209 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001210 }
1211
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001212 if (sort_order == default_sort_order &&
1213 parent_pattern == default_parent_pattern) {
Ingo Molnarbd741372009-06-04 14:13:04 +02001214 fprintf(fp, "#\n");
Pekka Enberg114cfab2009-08-05 13:25:21 +03001215 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001216 fprintf(fp, "#\n");
1217 }
Peter Zijlstra71dd8942009-06-04 15:16:56 +02001218 fprintf(fp, "\n");
Ingo Molnarbd741372009-06-04 14:13:04 +02001219
Frederic Weisbecker25446032009-08-08 02:16:25 +02001220 free(rem_sq_bracket);
1221
Brice Goglin8d513272009-08-07 13:55:24 +02001222 if (show_threads)
Brice Goglin9f866692009-08-10 15:26:32 +02001223 perf_read_values_display(fp, &show_threads_values,
1224 raw_printing_style);
Brice Goglin8d513272009-08-07 13:55:24 +02001225
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001226 return ret;
1227}
1228
Peter Zijlstra436224a2009-06-02 21:02:36 +02001229static void register_idle_thread(void)
1230{
1231 struct thread *thread = threads__findnew(0);
1232
1233 if (thread == NULL ||
1234 thread__set_comm(thread, "[idle]")) {
1235 fprintf(stderr, "problem inserting idle task.\n");
1236 exit(-1);
1237 }
1238}
1239
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001240static unsigned long total = 0,
1241 total_mmap = 0,
1242 total_comm = 0,
1243 total_fork = 0,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001244 total_unknown = 0,
1245 total_lost = 0;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001246
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001247static int validate_chain(struct ip_callchain *chain, event_t *event)
Ingo Molnar75220602009-06-18 08:00:17 +02001248{
1249 unsigned int chain_size;
1250
Ingo Molnar75220602009-06-18 08:00:17 +02001251 chain_size = event->header.size;
1252 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1253
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001254 if (chain->nr*sizeof(u64) > chain_size)
Ingo Molnar75220602009-06-18 08:00:17 +02001255 return -1;
1256
1257 return 0;
1258}
1259
Ingo Molnard80d3382009-06-03 23:14:49 +02001260static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001261process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar75051722009-06-03 23:14:49 +02001262{
1263 char level;
1264 int show = 0;
1265 struct dso *dso = NULL;
1266 struct thread *thread = threads__findnew(event->ip.pid);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001267 u64 ip = event->ip.ip;
1268 u64 period = 1;
Ingo Molnar75051722009-06-03 23:14:49 +02001269 struct map *map = NULL;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001270 void *more_data = event->ip.__more_data;
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001271 struct ip_callchain *chain = NULL;
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001272 int cpumode;
Ingo Molnar75051722009-06-03 23:14:49 +02001273
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001274 if (sample_type & PERF_SAMPLE_PERIOD) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001275 period = *(u64 *)more_data;
1276 more_data += sizeof(u64);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001277 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001278
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -03001279 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001280 (void *)(offset + head),
1281 (void *)(long)(event->header.size),
1282 event->header.misc,
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -03001283 event->ip.pid, event->ip.tid,
Peter Zijlstra4502d772009-06-10 15:03:06 +02001284 (void *)(long)ip,
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001285 (long long)period);
Ingo Molnar75051722009-06-03 23:14:49 +02001286
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001287 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Ingo Molnarf37a2912009-07-01 12:37:06 +02001288 unsigned int i;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001289
1290 chain = (void *)more_data;
1291
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001292 dprintf("... chain: nr:%Lu\n", chain->nr);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001293
Ingo Molnar75220602009-06-18 08:00:17 +02001294 if (validate_chain(chain, event) < 0) {
1295 eprintf("call-chain problem with event, skipping it.\n");
1296 return 0;
1297 }
1298
1299 if (dump_trace) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001300 for (i = 0; i < chain->nr; i++)
Peter Zijlstra2a0a50f2009-06-18 22:20:45 +02001301 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001302 }
1303 }
1304
Ingo Molnar75051722009-06-03 23:14:49 +02001305 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1306
1307 if (thread == NULL) {
Ingo Molnar75220602009-06-18 08:00:17 +02001308 eprintf("problem processing %d event, skipping it.\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001309 event->header.type);
1310 return -1;
1311 }
1312
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001313 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1314 return 0;
1315
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001316 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1317
1318 if (cpumode == PERF_EVENT_MISC_KERNEL) {
Ingo Molnar75051722009-06-03 23:14:49 +02001319 show = SHOW_KERNEL;
1320 level = 'k';
1321
1322 dso = kernel_dso;
1323
1324 dprintf(" ...... dso: %s\n", dso->name);
1325
Anton Blanchardd8db1b52009-07-01 09:00:48 +10001326 } else if (cpumode == PERF_EVENT_MISC_USER) {
Ingo Molnar75051722009-06-03 23:14:49 +02001327
1328 show = SHOW_USER;
1329 level = '.';
1330
Ingo Molnar75051722009-06-03 23:14:49 +02001331 } else {
1332 show = SHOW_HV;
1333 level = 'H';
Anton Blanchardfb9c8182009-07-01 09:00:49 +10001334
1335 dso = hypervisor_dso;
1336
Ingo Molnar75051722009-06-03 23:14:49 +02001337 dprintf(" ...... dso: [hypervisor]\n");
1338 }
1339
1340 if (show & show_mask) {
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001341 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
Peter Zijlstrafc54db52009-06-05 14:04:59 +02001342
Arnaldo Carvalho de Melo8fd101f2009-08-12 18:19:57 -03001343 if (dso_list && (!dso || !dso->name ||
1344 !strlist__has_entry(dso_list, dso->name)))
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001345 return 0;
1346
Arnaldo Carvalho de Melo8fd101f2009-08-12 18:19:57 -03001347 if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name)))
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001348 return 0;
1349
Peter Zijlstra6e7d6fd2009-06-17 15:51:44 +02001350 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
Ingo Molnar75220602009-06-18 08:00:17 +02001351 eprintf("problem incrementing symbol count, skipping event\n");
Ingo Molnar75051722009-06-03 23:14:49 +02001352 return -1;
1353 }
1354 }
Peter Zijlstraea1900e2009-06-10 21:45:22 +02001355 total += period;
Ingo Molnar75051722009-06-03 23:14:49 +02001356
1357 return 0;
1358}
1359
1360static int
1361process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1362{
1363 struct thread *thread = threads__findnew(event->mmap.pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02001364 struct map *map = map__new(&event->mmap, cwd, cwdlen);
Ingo Molnar75051722009-06-03 23:14:49 +02001365
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -03001366 dprintf("%p [%p]: PERF_EVENT_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
Ingo Molnar75051722009-06-03 23:14:49 +02001367 (void *)(offset + head),
1368 (void *)(long)(event->header.size),
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001369 event->mmap.pid,
Arnaldo Carvalho de Melo94a24752009-08-11 16:21:38 -03001370 event->mmap.tid,
Ingo Molnar75051722009-06-03 23:14:49 +02001371 (void *)(long)event->mmap.start,
1372 (void *)(long)event->mmap.len,
1373 (void *)(long)event->mmap.pgoff,
1374 event->mmap.filename);
1375
1376 if (thread == NULL || map == NULL) {
1377 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnardf979922009-06-04 13:41:22 +02001378 return 0;
Ingo Molnar75051722009-06-03 23:14:49 +02001379 }
1380
1381 thread__insert_map(thread, map);
1382 total_mmap++;
1383
1384 return 0;
1385}
1386
1387static int
1388process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1389{
1390 struct thread *thread = threads__findnew(event->comm.pid);
1391
1392 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1393 (void *)(offset + head),
1394 (void *)(long)(event->header.size),
1395 event->comm.comm, event->comm.pid);
1396
1397 if (thread == NULL ||
1398 thread__set_comm(thread, event->comm.comm)) {
1399 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1400 return -1;
1401 }
1402 total_comm++;
1403
1404 return 0;
1405}
1406
1407static int
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001408process_task_event(event_t *event, unsigned long offset, unsigned long head)
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001409{
1410 struct thread *thread = threads__findnew(event->fork.pid);
1411 struct thread *parent = threads__findnew(event->fork.ppid);
1412
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001413 dprintf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001414 (void *)(offset + head),
1415 (void *)(long)(event->header.size),
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001416 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1417 event->fork.pid, event->fork.tid,
1418 event->fork.ppid, event->fork.ptid);
1419
1420 /*
1421 * A thread clone will have the same PID for both
1422 * parent and child.
1423 */
1424 if (thread == parent)
1425 return 0;
1426
1427 if (event->header.type == PERF_EVENT_EXIT)
1428 return 0;
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001429
1430 if (!thread || !parent || thread__fork(thread, parent)) {
1431 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1432 return -1;
1433 }
1434 total_fork++;
1435
1436 return 0;
1437}
1438
1439static int
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001440process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1441{
1442 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1443 (void *)(offset + head),
1444 (void *)(long)(event->header.size),
1445 event->lost.id,
1446 event->lost.lost);
1447
1448 total_lost += event->lost.lost;
1449
1450 return 0;
1451}
1452
Ingo Molnar8465b052009-06-14 14:44:07 +02001453static void trace_event(event_t *event)
1454{
1455 unsigned char *raw_event = (void *)event;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001456 char *color = PERF_COLOR_BLUE;
Ingo Molnar8465b052009-06-14 14:44:07 +02001457 int i, j;
1458
1459 if (!dump_trace)
1460 return;
1461
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001462 dprintf(".");
1463 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
Ingo Molnar8465b052009-06-14 14:44:07 +02001464
1465 for (i = 0; i < event->header.size; i++) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001466 if ((i & 15) == 0) {
1467 dprintf(".");
1468 cdprintf(" %04x: ", i);
1469 }
Ingo Molnar8465b052009-06-14 14:44:07 +02001470
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001471 cdprintf(" %02x", raw_event[i]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001472
1473 if (((i & 15) == 15) || i == event->header.size-1) {
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001474 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001475 for (j = 0; j < 15-(i & 15); j++)
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001476 cdprintf(" ");
Ingo Molnar8465b052009-06-14 14:44:07 +02001477 for (j = 0; j < (i & 15); j++) {
Peter Zijlstraa73c7d82009-06-18 09:44:20 +02001478 if (isprint(raw_event[i-15+j]))
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001479 cdprintf("%c", raw_event[i-15+j]);
Ingo Molnar8465b052009-06-14 14:44:07 +02001480 else
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001481 cdprintf(".");
Ingo Molnar8465b052009-06-14 14:44:07 +02001482 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +02001483 cdprintf("\n");
Ingo Molnar8465b052009-06-14 14:44:07 +02001484 }
1485 }
1486 dprintf(".\n");
1487}
1488
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001489static struct perf_header *header;
1490
1491static struct perf_counter_attr *perf_header__find_attr(u64 id)
1492{
1493 int i;
1494
1495 for (i = 0; i < header->attrs; i++) {
1496 struct perf_header_attr *attr = header->attr[i];
1497 int j;
1498
1499 for (j = 0; j < attr->ids; j++) {
1500 if (attr->id[j] == id)
1501 return &attr->attr;
1502 }
1503 }
1504
1505 return NULL;
1506}
1507
Ingo Molnarb2fef072009-06-05 18:07:51 +02001508static int
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001509process_read_event(event_t *event, unsigned long offset, unsigned long head)
1510{
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001511 struct perf_counter_attr *attr = perf_header__find_attr(event->read.id);
1512
Brice Goglin8d513272009-08-07 13:55:24 +02001513 if (show_threads) {
1514 char *name = attr ? __event_name(attr->type, attr->config)
1515 : "unknown";
1516 perf_read_values_add_value(&show_threads_values,
1517 event->read.pid, event->read.tid,
1518 event->read.id,
1519 name,
1520 event->read.value);
1521 }
1522
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001523 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001524 (void *)(offset + head),
1525 (void *)(long)(event->header.size),
1526 event->read.pid,
1527 event->read.tid,
Peter Zijlstra8f18aec2009-08-06 19:40:28 +02001528 attr ? __event_name(attr->type, attr->config)
1529 : "FAIL",
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001530 event->read.value);
1531
1532 return 0;
1533}
1534
1535static int
Ingo Molnard80d3382009-06-03 23:14:49 +02001536process_event(event_t *event, unsigned long offset, unsigned long head)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001537{
Ingo Molnar8465b052009-06-14 14:44:07 +02001538 trace_event(event);
1539
Ingo Molnar75051722009-06-03 23:14:49 +02001540 switch (event->header.type) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001541 case PERF_EVENT_SAMPLE:
1542 return process_sample_event(event, offset, head);
1543
Ingo Molnar75051722009-06-03 23:14:49 +02001544 case PERF_EVENT_MMAP:
1545 return process_mmap_event(event, offset, head);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001546
Ingo Molnar75051722009-06-03 23:14:49 +02001547 case PERF_EVENT_COMM:
1548 return process_comm_event(event, offset, head);
Ingo Molnared966aa2009-06-03 10:39:26 +02001549
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001550 case PERF_EVENT_FORK:
Peter Zijlstra27d028d2009-07-23 16:52:41 +02001551 case PERF_EVENT_EXIT:
1552 return process_task_event(event, offset, head);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001553
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001554 case PERF_EVENT_LOST:
1555 return process_lost_event(event, offset, head);
1556
Peter Zijlstrae9ea2fd2009-06-24 22:46:04 +02001557 case PERF_EVENT_READ:
1558 return process_read_event(event, offset, head);
1559
Ingo Molnard11444d2009-06-03 23:29:14 +02001560 /*
1561 * We dont process them right now but they are fine:
1562 */
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001563
Ingo Molnard11444d2009-06-03 23:29:14 +02001564 case PERF_EVENT_THROTTLE:
1565 case PERF_EVENT_UNTHROTTLE:
1566 return 0;
1567
Ingo Molnard80d3382009-06-03 23:14:49 +02001568 default:
1569 return -1;
1570 }
1571
1572 return 0;
1573}
1574
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001575static u64 perf_header__sample_type(void)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001576{
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001577 u64 sample_type = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001578 int i;
1579
1580 for (i = 0; i < header->attrs; i++) {
1581 struct perf_header_attr *attr = header->attr[i];
1582
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001583 if (!sample_type)
1584 sample_type = attr->attr.sample_type;
1585 else if (sample_type != attr->attr.sample_type)
1586 die("non matching sample_type");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001587 }
1588
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001589 return sample_type;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001590}
Peter Zijlstraf5970552009-06-18 23:22:55 +02001591
Ingo Molnard80d3382009-06-03 23:14:49 +02001592static int __cmd_report(void)
1593{
Ingo Molnar75051722009-06-03 23:14:49 +02001594 int ret, rc = EXIT_FAILURE;
Ingo Molnard80d3382009-06-03 23:14:49 +02001595 unsigned long offset = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001596 unsigned long head, shift;
Ingo Molnard80d3382009-06-03 23:14:49 +02001597 struct stat stat;
Ingo Molnard80d3382009-06-03 23:14:49 +02001598 event_t *event;
Ingo Molnard80d3382009-06-03 23:14:49 +02001599 uint32_t size;
Ingo Molnar75051722009-06-03 23:14:49 +02001600 char *buf;
Ingo Molnard80d3382009-06-03 23:14:49 +02001601
1602 register_idle_thread();
1603
Brice Goglin8d513272009-08-07 13:55:24 +02001604 if (show_threads)
1605 perf_read_values_init(&show_threads_values);
1606
Ingo Molnard80d3382009-06-03 23:14:49 +02001607 input = open(input_name, O_RDONLY);
1608 if (input < 0) {
Ingo Molnara14832f2009-06-07 17:58:23 +02001609 fprintf(stderr, " failed to open file: %s", input_name);
1610 if (!strcmp(input_name, "perf.data"))
1611 fprintf(stderr, " (try 'perf record' first)");
1612 fprintf(stderr, "\n");
Ingo Molnard80d3382009-06-03 23:14:49 +02001613 exit(-1);
1614 }
1615
1616 ret = fstat(input, &stat);
1617 if (ret < 0) {
1618 perror("failed to stat file");
1619 exit(-1);
1620 }
1621
1622 if (!stat.st_size) {
1623 fprintf(stderr, "zero-sized file, nothing to do!\n");
1624 exit(0);
1625 }
1626
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001627 header = perf_header__read(input);
1628 head = header->data_offset;
Peter Zijlstraf5970552009-06-18 23:22:55 +02001629
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02001630 sample_type = perf_header__sample_type();
1631
Frederic Weisbecker91b4eae2009-07-05 07:39:17 +02001632 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1633 if (sort__has_parent) {
1634 fprintf(stderr, "selected --sort parent, but no"
1635 " callchain data. Did you call"
1636 " perf record without -g?\n");
1637 exit(-1);
1638 }
1639 if (callchain) {
1640 fprintf(stderr, "selected -c but no callchain data."
1641 " Did you call perf record without"
1642 " -g?\n");
1643 exit(-1);
1644 }
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +02001645 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1646 callchain = 1;
1647 if (register_callchain_param(&callchain_param) < 0) {
1648 fprintf(stderr, "Can't register callchain"
1649 " params\n");
1650 exit(-1);
1651 }
Peter Zijlstraf5970552009-06-18 23:22:55 +02001652 }
1653
Ingo Molnard80d3382009-06-03 23:14:49 +02001654 if (load_kernel() < 0) {
1655 perror("failed to load kernel symbols");
1656 return EXIT_FAILURE;
1657 }
1658
1659 if (!full_paths) {
1660 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1661 perror("failed to get the current directory");
1662 return EXIT_FAILURE;
1663 }
1664 cwdlen = strlen(cwd);
1665 } else {
1666 cwd = NULL;
1667 cwdlen = 0;
1668 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001669
1670 shift = page_size * (head / page_size);
1671 offset += shift;
1672 head -= shift;
1673
Ingo Molnard80d3382009-06-03 23:14:49 +02001674remap:
1675 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1676 MAP_SHARED, input, offset);
1677 if (buf == MAP_FAILED) {
1678 perror("failed to mmap file");
1679 exit(-1);
1680 }
1681
1682more:
1683 event = (event_t *)(buf + head);
1684
1685 size = event->header.size;
1686 if (!size)
1687 size = 8;
1688
1689 if (head + event->header.size >= page_size * mmap_window) {
Ingo Molnard80d3382009-06-03 23:14:49 +02001690 int ret;
1691
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001692 shift = page_size * (head / page_size);
1693
Ingo Molnard80d3382009-06-03 23:14:49 +02001694 ret = munmap(buf, page_size * mmap_window);
1695 assert(ret == 0);
1696
1697 offset += shift;
1698 head -= shift;
1699 goto remap;
1700 }
1701
1702 size = event->header.size;
1703
Ingo Molnar8465b052009-06-14 14:44:07 +02001704 dprintf("\n%p [%p]: event: %d\n",
Ingo Molnarb2fef072009-06-05 18:07:51 +02001705 (void *)(offset + head),
1706 (void *)(long)event->header.size,
1707 event->header.type);
1708
Ingo Molnard80d3382009-06-03 23:14:49 +02001709 if (!size || process_event(event, offset, head) < 0) {
1710
Ingo Molnar35029732009-06-03 09:38:58 +02001711 dprintf("%p [%p]: skipping unknown header type: %d\n",
1712 (void *)(offset + head),
1713 (void *)(long)(event->header.size),
1714 event->header.type);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001715
Ingo Molnar3e706112009-05-26 18:53:17 +02001716 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001717
1718 /*
1719 * assume we lost track of the stream, check alignment, and
1720 * increment a single u64 in the hope to catch on again 'soon'.
1721 */
1722
1723 if (unlikely(head & 7))
1724 head &= ~7ULL;
1725
1726 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001727 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001728
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001729 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001730
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001731 if (offset + head >= header->data_offset + header->data_size)
Peter Zijlstraf5970552009-06-18 23:22:55 +02001732 goto done;
1733
Ingo Molnarf37a2912009-07-01 12:37:06 +02001734 if (offset + head < (unsigned long)stat.st_size)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001735 goto more;
1736
Peter Zijlstraf5970552009-06-18 23:22:55 +02001737done:
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001738 rc = EXIT_SUCCESS;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001739 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001740
Ingo Molnar35029732009-06-03 09:38:58 +02001741 dprintf(" IP events: %10ld\n", total);
1742 dprintf(" mmap events: %10ld\n", total_mmap);
1743 dprintf(" comm events: %10ld\n", total_comm);
Peter Zijlstra62fc4452009-06-04 16:53:49 +02001744 dprintf(" fork events: %10ld\n", total_fork);
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +02001745 dprintf(" lost events: %10ld\n", total_lost);
Ingo Molnar35029732009-06-03 09:38:58 +02001746 dprintf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001747
Ingo Molnar35029732009-06-03 09:38:58 +02001748 if (dump_trace)
Ingo Molnar97b07b62009-05-26 18:48:58 +02001749 return 0;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001750
Arnaldo Carvalho de Melo9ac99542009-06-04 13:54:00 -03001751 if (verbose >= 3)
1752 threads__fprintf(stdout);
1753
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001754 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001755 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001756
Peter Zijlstra82292892009-06-03 12:37:36 +02001757 collapse__resort();
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001758 output__resort(total);
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001759 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001760
Brice Goglin8d513272009-08-07 13:55:24 +02001761 if (show_threads)
1762 perf_read_values_destroy(&show_threads_values);
1763
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001764 return rc;
1765}
1766
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001767static int
1768parse_callchain_opt(const struct option *opt __used, const char *arg,
1769 int unset __used)
1770{
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001771 char *tok;
1772 char *endptr;
1773
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001774 callchain = 1;
1775
1776 if (!arg)
1777 return 0;
1778
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001779 tok = strtok((char *)arg, ",");
1780 if (!tok)
1781 return -1;
1782
1783 /* get the output mode */
1784 if (!strncmp(tok, "graph", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001785 callchain_param.mode = CHAIN_GRAPH_ABS;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001786
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001787 else if (!strncmp(tok, "flat", strlen(arg)))
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001788 callchain_param.mode = CHAIN_FLAT;
1789
1790 else if (!strncmp(tok, "fractal", strlen(arg)))
1791 callchain_param.mode = CHAIN_GRAPH_REL;
1792
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +02001793 else if (!strncmp(tok, "none", strlen(arg))) {
1794 callchain_param.mode = CHAIN_NONE;
1795 callchain = 0;
1796
1797 return 0;
1798 }
1799
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001800 else
1801 return -1;
1802
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001803 /* get the min percentage */
1804 tok = strtok(NULL, ",");
1805 if (!tok)
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001806 goto setup;
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001807
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001808 callchain_param.min_percent = strtod(tok, &endptr);
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001809 if (tok == endptr)
1810 return -1;
1811
Frederic Weisbecker805d1272009-07-05 07:39:21 +02001812setup:
1813 if (register_callchain_param(&callchain_param) < 0) {
1814 fprintf(stderr, "Can't register callchain params\n");
1815 return -1;
1816 }
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +02001817 return 0;
1818}
1819
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001820static const char * const report_usage[] = {
1821 "perf report [<options>] <command>",
1822 NULL
1823};
1824
1825static const struct option options[] = {
1826 OPT_STRING('i', "input", &input_name, "file",
1827 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001828 OPT_BOOLEAN('v', "verbose", &verbose,
1829 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001830 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1831 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001832 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +02001833 OPT_BOOLEAN('m', "modules", &modules,
1834 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloe3d7e182009-07-11 12:18:37 -03001835 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1836 "Show a column with the number of samples"),
Brice Goglin8d513272009-08-07 13:55:24 +02001837 OPT_BOOLEAN('T', "threads", &show_threads,
1838 "Show per-thread event counters"),
Brice Goglin9f866692009-08-10 15:26:32 +02001839 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
1840 "pretty printing style key: normal raw"),
Ingo Molnar63299f02009-05-28 10:52:00 +02001841 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001842 "sort by key(s): pid, comm, dso, symbol, parent"),
Arnaldo Carvalho de Melob78c07d2009-05-29 13:48:59 -03001843 OPT_BOOLEAN('P', "full-paths", &full_paths,
1844 "Don't shorten the pathnames taking into account the cwd"),
Ingo Molnarb25bcf22009-06-18 07:01:03 +02001845 OPT_STRING('p', "parent", &parent_pattern, "regex",
1846 "regex filter to identify parent, see: '--sort parent'"),
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001847 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1848 "Only display entries with parent-match"),
Anton Blanchard1483b19f2009-07-16 15:44:29 +02001849 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +02001850 "Display callchains using output_type and min percent threshold. "
Anton Blanchard1483b19f2009-07-16 15:44:29 +02001851 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
Arnaldo Carvalho de Melo25903402009-06-30 19:01:20 -03001852 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1853 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001854 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1855 "only consider symbols in these comms"),
Arnaldo Carvalho de Melo7bec7a92009-06-30 19:01:22 -03001856 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1857 "only consider these symbols"),
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001858 OPT_STRING('w', "column-widths", &col_width_list_str,
1859 "width[,width...]",
1860 "don't try to adjust column width, use these fixed values"),
1861 OPT_STRING('t', "field-separator", &field_sep, "separator",
1862 "separator for columns, no spaces will be added between "
1863 "columns '.' is reserved."),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001864 OPT_END()
1865};
1866
Ingo Molnar5352f352009-06-03 10:07:39 +02001867static void setup_sorting(void)
1868{
1869 char *tmp, *tok, *str = strdup(sort_order);
1870
1871 for (tok = strtok_r(str, ", ", &tmp);
1872 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1873 if (sort_dimension__add(tok) < 0) {
1874 error("Unknown --sort key: `%s'", tok);
1875 usage_with_options(report_usage, options);
1876 }
1877 }
1878
1879 free(str);
1880}
1881
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001882static void setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001883 struct sort_entry *se, const char *list_name,
1884 FILE *fp)
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001885{
1886 if (list_str) {
1887 *list = strlist__new(true, list_str);
1888 if (!*list) {
1889 fprintf(stderr, "problems parsing %s list\n",
1890 list_name);
1891 exit(129);
1892 }
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001893 if (strlist__nr_entries(*list) == 1) {
1894 fprintf(fp, "# %s: %s\n", list_name,
1895 strlist__entry(*list, 0)->s);
1896 se->elide = true;
1897 }
Arnaldo Carvalho de Melocc8b88b2009-06-30 19:01:21 -03001898 }
1899}
1900
Ingo Molnarf37a2912009-07-01 12:37:06 +02001901int cmd_report(int argc, const char **argv, const char *prefix __used)
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001902{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001903 symbol__init();
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001904
1905 page_size = getpagesize();
1906
Ingo Molnaredc52de2009-06-04 16:24:37 +02001907 argc = parse_options(argc, argv, options, report_usage, 0);
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001908
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001909 setup_sorting();
1910
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001911 if (parent_pattern != default_parent_pattern) {
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001912 sort_dimension__add("parent");
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001913 sort_parent.elide = 1;
1914 } else
Ingo Molnarb8e6d822009-06-18 14:32:19 +02001915 exclude_other = 0;
1916
Ingo Molnaredc52de2009-06-04 16:24:37 +02001917 /*
1918 * Any (unrecognized) arguments left?
1919 */
1920 if (argc)
1921 usage_with_options(report_usage, options);
1922
Ingo Molnara930d2c2009-05-27 09:50:13 +02001923 setup_pager();
1924
Arnaldo Carvalho de Melo021191b2009-07-11 12:18:35 -03001925 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
1926 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
1927 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001928
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -03001929 if (field_sep && *field_sep == '.') {
1930 fputs("'.' is the only non valid --field-separator argument\n",
1931 stderr);
1932 exit(129);
1933 }
1934
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001935 return __cmd_report();
1936}