blob: df51560f16f7ea1e9212c2606ff7624a91357fc5 [file] [log] [blame]
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02001#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02002#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02003#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02004#include "session.h"
5#include "sort.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -02006#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +02007
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -03008enum hist_filter {
9 HIST_FILTER__DSO,
10 HIST_FILTER__THREAD,
11 HIST_FILTER__PARENT,
12};
13
John Kacur3d1d07e2009-09-28 15:32:55 +020014struct callchain_param callchain_param = {
15 .mode = CHAIN_GRAPH_REL,
16 .min_percent = 0.5
17};
18
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030019u16 hists__col_len(struct hists *self, enum hist_column col)
20{
21 return self->col_len[col];
22}
23
24void hists__set_col_len(struct hists *self, enum hist_column col, u16 len)
25{
26 self->col_len[col] = len;
27}
28
29bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len)
30{
31 if (len > hists__col_len(self, col)) {
32 hists__set_col_len(self, col, len);
33 return true;
34 }
35 return false;
36}
37
38static void hists__reset_col_len(struct hists *self)
39{
40 enum hist_column col;
41
42 for (col = 0; col < HISTC_NR_COLS; ++col)
43 hists__set_col_len(self, col, 0);
44}
45
46static void hists__calc_col_len(struct hists *self, struct hist_entry *h)
47{
48 u16 len;
49
50 if (h->ms.sym)
51 hists__new_col_len(self, HISTC_SYMBOL, h->ms.sym->namelen);
52
53 len = thread__comm_len(h->thread);
54 if (hists__new_col_len(self, HISTC_COMM, len))
55 hists__set_col_len(self, HISTC_THREAD, len + 6);
56
57 if (h->ms.map) {
58 len = dso__name_len(h->ms.map->dso);
59 hists__new_col_len(self, HISTC_DSO, len);
60 }
61}
62
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030063static void hist_entry__add_cpumode_period(struct hist_entry *self,
64 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080065{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030066 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080067 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030068 self->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080069 break;
70 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030071 self->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080072 break;
73 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030074 self->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080075 break;
76 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030077 self->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080078 break;
79 default:
80 break;
81 }
82}
83
John Kacur3d1d07e2009-09-28 15:32:55 +020084/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030085 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +020086 */
87
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030088static struct hist_entry *hist_entry__new(struct hist_entry *template)
89{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020090 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030091 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
92
93 if (self != NULL) {
94 *self = *template;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030095 self->nr_events = 1;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -030096 if (self->ms.map)
97 self->ms.map->referenced = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030098 if (symbol_conf.use_callchain)
99 callchain_init(self->callchain);
100 }
101
102 return self;
103}
104
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300105static void hists__inc_nr_entries(struct hists *self, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300106{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300107 if (!h->filtered) {
108 hists__calc_col_len(self, h);
109 ++self->nr_entries;
110 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300111}
112
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300113static u8 symbol__parent_filter(const struct symbol *parent)
114{
115 if (symbol_conf.exclude_other && parent == NULL)
116 return 1 << HIST_FILTER__PARENT;
117 return 0;
118}
119
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300120struct hist_entry *__hists__add_entry(struct hists *self,
121 struct addr_location *al,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300122 struct symbol *sym_parent, u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300123{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300124 struct rb_node **p = &self->entries.rb_node;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300125 struct rb_node *parent = NULL;
126 struct hist_entry *he;
127 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200128 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300129 .ms = {
130 .map = al->map,
131 .sym = al->sym,
132 },
Arun Sharmaf60f3592010-06-04 11:27:10 -0300133 .cpu = al->cpu,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200134 .ip = al->addr,
135 .level = al->level,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300136 .period = period,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300137 .parent = sym_parent,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300138 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300139 };
140 int cmp;
141
142 while (*p != NULL) {
143 parent = *p;
144 he = rb_entry(parent, struct hist_entry, rb_node);
145
146 cmp = hist_entry__cmp(&entry, he);
147
148 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300149 he->period += period;
150 ++he->nr_events;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300151 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300152 }
153
154 if (cmp < 0)
155 p = &(*p)->rb_left;
156 else
157 p = &(*p)->rb_right;
158 }
159
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300160 he = hist_entry__new(&entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300161 if (!he)
162 return NULL;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300163 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300164 rb_insert_color(&he->rb_node, &self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300165 hists__inc_nr_entries(self, he);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300166out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300167 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300168 return he;
169}
170
John Kacur3d1d07e2009-09-28 15:32:55 +0200171int64_t
172hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
173{
174 struct sort_entry *se;
175 int64_t cmp = 0;
176
177 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200178 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200179 if (cmp)
180 break;
181 }
182
183 return cmp;
184}
185
186int64_t
187hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
188{
189 struct sort_entry *se;
190 int64_t cmp = 0;
191
192 list_for_each_entry(se, &hist_entry__sort_list, list) {
193 int64_t (*f)(struct hist_entry *, struct hist_entry *);
194
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200195 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200196
197 cmp = f(left, right);
198 if (cmp)
199 break;
200 }
201
202 return cmp;
203}
204
205void hist_entry__free(struct hist_entry *he)
206{
207 free(he);
208}
209
210/*
211 * collapse the histogram
212 */
213
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300214static bool collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200215{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200216 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200217 struct rb_node *parent = NULL;
218 struct hist_entry *iter;
219 int64_t cmp;
220
221 while (*p != NULL) {
222 parent = *p;
223 iter = rb_entry(parent, struct hist_entry, rb_node);
224
225 cmp = hist_entry__collapse(iter, he);
226
227 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300228 iter->period += he->period;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200229 if (symbol_conf.use_callchain)
230 callchain_merge(iter->callchain, he->callchain);
John Kacur3d1d07e2009-09-28 15:32:55 +0200231 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300232 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200233 }
234
235 if (cmp < 0)
236 p = &(*p)->rb_left;
237 else
238 p = &(*p)->rb_right;
239 }
240
241 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200242 rb_insert_color(&he->rb_node, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300243 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200244}
245
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300246void hists__collapse_resort(struct hists *self)
John Kacur3d1d07e2009-09-28 15:32:55 +0200247{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200248 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200249 struct rb_node *next;
250 struct hist_entry *n;
251
252 if (!sort__need_collapse)
253 return;
254
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200255 tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300256 next = rb_first(&self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300257 self->nr_entries = 0;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300258 hists__reset_col_len(self);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200259
John Kacur3d1d07e2009-09-28 15:32:55 +0200260 while (next) {
261 n = rb_entry(next, struct hist_entry, rb_node);
262 next = rb_next(&n->rb_node);
263
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300264 rb_erase(&n->rb_node, &self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300265 if (collapse__insert_entry(&tmp, n))
266 hists__inc_nr_entries(self, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200267 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200268
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300269 self->entries = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200270}
271
272/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300273 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200274 */
275
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300276static void __hists__insert_output_entry(struct rb_root *entries,
277 struct hist_entry *he,
278 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200279{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300280 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200281 struct rb_node *parent = NULL;
282 struct hist_entry *iter;
283
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200284 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300285 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200286 min_callchain_hits, &callchain_param);
287
288 while (*p != NULL) {
289 parent = *p;
290 iter = rb_entry(parent, struct hist_entry, rb_node);
291
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300292 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200293 p = &(*p)->rb_left;
294 else
295 p = &(*p)->rb_right;
296 }
297
298 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300299 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200300}
301
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300302void hists__output_resort(struct hists *self)
John Kacur3d1d07e2009-09-28 15:32:55 +0200303{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200304 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200305 struct rb_node *next;
306 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200307 u64 min_callchain_hits;
308
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300309 min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200310
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200311 tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300312 next = rb_first(&self->entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200313
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300314 self->nr_entries = 0;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300315 hists__reset_col_len(self);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300316
John Kacur3d1d07e2009-09-28 15:32:55 +0200317 while (next) {
318 n = rb_entry(next, struct hist_entry, rb_node);
319 next = rb_next(&n->rb_node);
320
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300321 rb_erase(&n->rb_node, &self->entries);
322 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300323 hists__inc_nr_entries(self, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200324 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200325
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300326 self->entries = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200327}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200328
329static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
330{
331 int i;
332 int ret = fprintf(fp, " ");
333
334 for (i = 0; i < left_margin; i++)
335 ret += fprintf(fp, " ");
336
337 return ret;
338}
339
340static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
341 int left_margin)
342{
343 int i;
344 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
345
346 for (i = 0; i < depth; i++)
347 if (depth_mask & (1 << i))
348 ret += fprintf(fp, "| ");
349 else
350 ret += fprintf(fp, " ");
351
352 ret += fprintf(fp, "\n");
353
354 return ret;
355}
356
357static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300358 int depth, int depth_mask, int period,
Frederic Weisbeckerd425de52011-01-03 16:13:11 +0100359 u64 total_samples, u64 hits,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200360 int left_margin)
361{
362 int i;
363 size_t ret = 0;
364
365 ret += callchain__fprintf_left_margin(fp, left_margin);
366 for (i = 0; i < depth; i++) {
367 if (depth_mask & (1 << i))
368 ret += fprintf(fp, "|");
369 else
370 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300371 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200372 double percent;
373
374 percent = hits * 100.0 / total_samples;
375 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
376 } else
377 ret += fprintf(fp, "%s", " ");
378 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300379 if (chain->ms.sym)
380 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200381 else
382 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
383
384 return ret;
385}
386
387static struct symbol *rem_sq_bracket;
388static struct callchain_list rem_hits;
389
390static void init_rem_hits(void)
391{
392 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
393 if (!rem_sq_bracket) {
394 fprintf(stderr, "Not enough memory to display remaining hits\n");
395 return;
396 }
397
398 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300399 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200400}
401
402static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
403 u64 total_samples, int depth,
404 int depth_mask, int left_margin)
405{
406 struct rb_node *node, *next;
407 struct callchain_node *child;
408 struct callchain_list *chain;
409 int new_depth_mask = depth_mask;
410 u64 new_total;
411 u64 remaining;
412 size_t ret = 0;
413 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300414 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200415
416 if (callchain_param.mode == CHAIN_GRAPH_REL)
417 new_total = self->children_hit;
418 else
419 new_total = total_samples;
420
421 remaining = new_total;
422
423 node = rb_first(&self->rb_root);
424 while (node) {
425 u64 cumul;
426
427 child = rb_entry(node, struct callchain_node, rb_node);
428 cumul = cumul_hits(child);
429 remaining -= cumul;
430
431 /*
432 * The depth mask manages the output of pipes that show
433 * the depth. We don't want to keep the pipes of the current
434 * level for the last child of this depth.
435 * Except if we have remaining filtered hits. They will
436 * supersede the last child
437 */
438 next = rb_next(node);
439 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
440 new_depth_mask &= ~(1 << (depth - 1));
441
442 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800443 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200444 * to keep the level link until we reach the last child
445 */
446 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
447 left_margin);
448 i = 0;
449 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200450 ret += ipchain__fprintf_graph(fp, chain, depth,
451 new_depth_mask, i++,
452 new_total,
453 cumul,
454 left_margin);
455 }
456 ret += __callchain__fprintf_graph(fp, child, new_total,
457 depth + 1,
458 new_depth_mask | (1 << depth),
459 left_margin);
460 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300461 if (++entries_printed == callchain_param.print_limit)
462 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200463 }
464
465 if (callchain_param.mode == CHAIN_GRAPH_REL &&
466 remaining && remaining != new_total) {
467
468 if (!rem_sq_bracket)
469 return ret;
470
471 new_depth_mask &= ~(1 << (depth - 1));
472
473 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
474 new_depth_mask, 0, new_total,
475 remaining, left_margin);
476 }
477
478 return ret;
479}
480
481static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
482 u64 total_samples, int left_margin)
483{
484 struct callchain_list *chain;
485 bool printed = false;
486 int i = 0;
487 int ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300488 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200489
490 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200491 if (!i++ && sort__first_dimension == SORT_SYM)
492 continue;
493
494 if (!printed) {
495 ret += callchain__fprintf_left_margin(fp, left_margin);
496 ret += fprintf(fp, "|\n");
497 ret += callchain__fprintf_left_margin(fp, left_margin);
498 ret += fprintf(fp, "---");
499
500 left_margin += 3;
501 printed = true;
502 } else
503 ret += callchain__fprintf_left_margin(fp, left_margin);
504
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300505 if (chain->ms.sym)
506 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200507 else
508 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300509
510 if (++entries_printed == callchain_param.print_limit)
511 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200512 }
513
514 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
515
516 return ret;
517}
518
519static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
520 u64 total_samples)
521{
522 struct callchain_list *chain;
523 size_t ret = 0;
524
525 if (!self)
526 return 0;
527
528 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
529
530
531 list_for_each_entry(chain, &self->val, list) {
532 if (chain->ip >= PERF_CONTEXT_MAX)
533 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300534 if (chain->ms.sym)
535 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200536 else
537 ret += fprintf(fp, " %p\n",
538 (void *)(long)chain->ip);
539 }
540
541 return ret;
542}
543
544static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
545 u64 total_samples, int left_margin)
546{
547 struct rb_node *rb_node;
548 struct callchain_node *chain;
549 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300550 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200551
552 rb_node = rb_first(&self->sorted_chain);
553 while (rb_node) {
554 double percent;
555
556 chain = rb_entry(rb_node, struct callchain_node, rb_node);
557 percent = chain->hit * 100.0 / total_samples;
558 switch (callchain_param.mode) {
559 case CHAIN_FLAT:
560 ret += percent_color_fprintf(fp, " %6.2f%%\n",
561 percent);
562 ret += callchain__fprintf_flat(fp, chain, total_samples);
563 break;
564 case CHAIN_GRAPH_ABS: /* Falldown */
565 case CHAIN_GRAPH_REL:
566 ret += callchain__fprintf_graph(fp, chain, total_samples,
567 left_margin);
568 case CHAIN_NONE:
569 default:
570 break;
571 }
572 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300573 if (++entries_printed == callchain_param.print_limit)
574 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200575 rb_node = rb_next(rb_node);
576 }
577
578 return ret;
579}
580
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300581int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300582 struct hists *hists, struct hists *pair_hists,
583 bool show_displacement, long displacement,
584 bool color, u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200585{
586 struct sort_entry *se;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300587 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Melo69cf0212011-02-17 10:37:23 -0200588 u64 nr_events;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200589 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300590 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200591
592 if (symbol_conf.exclude_other && !self->parent)
593 return 0;
594
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300595 if (pair_hists) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300596 period = self->pair ? self->pair->period : 0;
Arnaldo Carvalho de Melo69cf0212011-02-17 10:37:23 -0200597 nr_events = self->pair ? self->pair->nr_events : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300598 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300599 period_sys = self->pair ? self->pair->period_sys : 0;
600 period_us = self->pair ? self->pair->period_us : 0;
601 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
602 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200603 } else {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300604 period = self->period;
Arnaldo Carvalho de Melo69cf0212011-02-17 10:37:23 -0200605 nr_events = self->nr_events;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300606 total = session_total;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300607 period_sys = self->period_sys;
608 period_us = self->period_us;
609 period_guest_sys = self->period_guest_sys;
610 period_guest_us = self->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200611 }
612
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300613 if (total) {
614 if (color)
615 ret = percent_color_snprintf(s, size,
616 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300617 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300618 else
619 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300620 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800621 if (symbol_conf.show_cpu_utilization) {
622 ret += percent_color_snprintf(s + ret, size - ret,
623 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300624 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800625 ret += percent_color_snprintf(s + ret, size - ret,
626 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300627 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800628 if (perf_guest) {
629 ret += percent_color_snprintf(s + ret,
630 size - ret,
631 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300632 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800633 total);
634 ret += percent_color_snprintf(s + ret,
635 size - ret,
636 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300637 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800638 total);
639 }
640 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300641 } else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200642 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200643
644 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200645 if (sep)
Arnaldo Carvalho de Melo69cf0212011-02-17 10:37:23 -0200646 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200647 else
Arnaldo Carvalho de Melo69cf0212011-02-17 10:37:23 -0200648 ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200649 }
650
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300651 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200652 char bf[32];
653 double old_percent = 0, new_percent = 0, diff;
654
655 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300656 old_percent = (period * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300657 if (session_total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300658 new_percent = (self->period * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200659
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200660 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200661
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200662 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200663 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
664 else
665 snprintf(bf, sizeof(bf), " ");
666
667 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300668 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200669 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300670 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200671
672 if (show_displacement) {
673 if (displacement)
674 snprintf(bf, sizeof(bf), "%+4ld", displacement);
675 else
676 snprintf(bf, sizeof(bf), " ");
677
678 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300679 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200680 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300681 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200682 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200683 }
684
685 list_for_each_entry(se, &hist_entry__sort_list, list) {
686 if (se->elide)
687 continue;
688
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300689 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200690 ret += se->se_snprintf(self, s + ret, size - ret,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300691 hists__col_len(hists, se->se_width_idx));
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200692 }
693
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300694 return ret;
695}
696
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300697int hist_entry__fprintf(struct hist_entry *self, struct hists *hists,
698 struct hists *pair_hists, bool show_displacement,
699 long displacement, FILE *fp, u64 session_total)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300700{
701 char bf[512];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300702 hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300703 show_displacement, displacement,
704 true, session_total);
705 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300706}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200707
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300708static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
709 struct hists *hists, FILE *fp,
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300710 u64 session_total)
711{
712 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200713
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300714 if (sort__first_dimension == SORT_COMM) {
715 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
716 typeof(*se), list);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300717 left_margin = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300718 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200719 }
720
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300721 return hist_entry_callchain__fprintf(fp, self, session_total,
722 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200723}
724
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300725size_t hists__fprintf(struct hists *self, struct hists *pair,
726 bool show_displacement, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200727{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200728 struct sort_entry *se;
729 struct rb_node *nd;
730 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200731 unsigned long position = 1;
732 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200733 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200734 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300735 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200736
737 init_rem_hits();
738
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200739 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
740
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200741 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200742 if (sep)
743 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200744 else
745 fputs(" Samples ", fp);
746 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200747
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800748 if (symbol_conf.show_cpu_utilization) {
749 if (sep) {
750 ret += fprintf(fp, "%csys", *sep);
751 ret += fprintf(fp, "%cus", *sep);
752 if (perf_guest) {
753 ret += fprintf(fp, "%cguest sys", *sep);
754 ret += fprintf(fp, "%cguest us", *sep);
755 }
756 } else {
757 ret += fprintf(fp, " sys ");
758 ret += fprintf(fp, " us ");
759 if (perf_guest) {
760 ret += fprintf(fp, " guest sys ");
761 ret += fprintf(fp, " guest us ");
762 }
763 }
764 }
765
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200766 if (pair) {
767 if (sep)
768 ret += fprintf(fp, "%cDelta", *sep);
769 else
770 ret += fprintf(fp, " Delta ");
771
772 if (show_displacement) {
773 if (sep)
774 ret += fprintf(fp, "%cDisplacement", *sep);
775 else
776 ret += fprintf(fp, " Displ");
777 }
778 }
779
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200780 list_for_each_entry(se, &hist_entry__sort_list, list) {
781 if (se->elide)
782 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200783 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200784 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200785 continue;
786 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200787 width = strlen(se->se_header);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300788 if (symbol_conf.col_width_list_str) {
789 if (col_width) {
790 hists__set_col_len(self, se->se_width_idx,
791 atoi(col_width));
792 col_width = strchr(col_width, ',');
793 if (col_width)
794 ++col_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200795 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200796 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300797 if (!hists__new_col_len(self, se->se_width_idx, width))
798 width = hists__col_len(self, se->se_width_idx);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200799 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200800 }
801 fprintf(fp, "\n");
802
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200803 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200804 goto print_entries;
805
806 fprintf(fp, "# ........");
807 if (symbol_conf.show_nr_samples)
808 fprintf(fp, " ..........");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200809 if (pair) {
810 fprintf(fp, " ..........");
811 if (show_displacement)
812 fprintf(fp, " .....");
813 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200814 list_for_each_entry(se, &hist_entry__sort_list, list) {
815 unsigned int i;
816
817 if (se->elide)
818 continue;
819
820 fprintf(fp, " ");
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300821 width = hists__col_len(self, se->se_width_idx);
822 if (width == 0)
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200823 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200824 for (i = 0; i < width; i++)
825 fprintf(fp, ".");
826 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200827
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200828 fprintf(fp, "\n#\n");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200829
830print_entries:
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300831 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200832 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
833
834 if (show_displacement) {
835 if (h->pair != NULL)
836 displacement = ((long)h->pair->position -
837 (long)position);
838 else
839 displacement = 0;
840 ++position;
841 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300842 ret += hist_entry__fprintf(h, self, pair, show_displacement,
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300843 displacement, fp, self->stats.total_period);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300844
845 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300846 ret += hist_entry__fprintf_callchain(h, self, fp,
847 self->stats.total_period);
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300848 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300849 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300850 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300851 fprintf(fp, "%.10s end\n", graph_dotted_line);
852 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200853 }
854
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200855 free(rem_sq_bracket);
856
857 return ret;
858}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300859
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -0300860/*
861 * See hists__fprintf to match the column widths
862 */
863unsigned int hists__sort_list_width(struct hists *self)
864{
865 struct sort_entry *se;
866 int ret = 9; /* total % */
867
868 if (symbol_conf.show_cpu_utilization) {
869 ret += 7; /* count_sys % */
870 ret += 6; /* count_us % */
871 if (perf_guest) {
872 ret += 13; /* count_guest_sys % */
873 ret += 12; /* count_guest_us % */
874 }
875 }
876
877 if (symbol_conf.show_nr_samples)
878 ret += 11;
879
880 list_for_each_entry(se, &hist_entry__sort_list, list)
881 if (!se->elide)
882 ret += 2 + hists__col_len(self, se->se_width_idx);
883
Arnaldo Carvalho de Melo903cce62010-08-05 19:15:48 -0300884 if (verbose) /* Addr + origin */
885 ret += 3 + BITS_PER_LONG / 4;
886
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -0300887 return ret;
888}
889
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300890static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
891 enum hist_filter filter)
892{
893 h->filtered &= ~(1 << filter);
894 if (h->filtered)
895 return;
896
897 ++self->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300898 if (h->ms.unfolded)
899 self->nr_entries += h->nr_rows;
900 h->row_offset = 0;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300901 self->stats.total_period += h->period;
902 self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
903
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300904 hists__calc_col_len(self, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300905}
906
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300907void hists__filter_by_dso(struct hists *self, const struct dso *dso)
908{
909 struct rb_node *nd;
910
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300911 self->nr_entries = self->stats.total_period = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300912 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300913 hists__reset_col_len(self);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300914
915 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
916 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
917
918 if (symbol_conf.exclude_other && !h->parent)
919 continue;
920
921 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
922 h->filtered |= (1 << HIST_FILTER__DSO);
923 continue;
924 }
925
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300926 hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300927 }
928}
929
930void hists__filter_by_thread(struct hists *self, const struct thread *thread)
931{
932 struct rb_node *nd;
933
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300934 self->nr_entries = self->stats.total_period = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300935 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300936 hists__reset_col_len(self);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300937
938 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
939 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
940
941 if (thread != NULL && h->thread != thread) {
942 h->filtered |= (1 << HIST_FILTER__THREAD);
943 continue;
944 }
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300945
946 hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300947 }
948}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300949
950static int symbol__alloc_hist(struct symbol *self)
951{
952 struct sym_priv *priv = symbol__priv(self);
953 const int size = (sizeof(*priv->hist) +
954 (self->end - self->start) * sizeof(u64));
955
956 priv->hist = zalloc(size);
957 return priv->hist == NULL ? -1 : 0;
958}
959
960int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip)
961{
962 unsigned int sym_size, offset;
963 struct symbol *sym = self->ms.sym;
964 struct sym_priv *priv;
965 struct sym_hist *h;
966
967 if (!sym || !self->ms.map)
968 return 0;
969
970 priv = symbol__priv(sym);
971 if (priv->hist == NULL && symbol__alloc_hist(sym) < 0)
972 return -ENOMEM;
973
974 sym_size = sym->end - sym->start;
975 offset = ip - sym->start;
976
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200977 pr_debug3("%s: ip=%#" PRIx64 "\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip));
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300978
979 if (offset >= sym_size)
980 return 0;
981
982 h = priv->hist;
983 h->sum++;
984 h->ip[offset]++;
985
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200986 pr_debug3("%#" PRIx64 " %s: period++ [ip: %#" PRIx64 ", %#" PRIx64
987 "] => %" PRIu64 "\n", self->ms.sym->start, self->ms.sym->name,
988 ip, ip - self->ms.sym->start, h->ip[offset]);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300989 return 0;
990}
991
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300992static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300993{
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300994 struct objdump_line *self = malloc(sizeof(*self) + privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300995
996 if (self != NULL) {
997 self->offset = offset;
998 self->line = line;
999 }
1000
1001 return self;
1002}
1003
1004void objdump_line__free(struct objdump_line *self)
1005{
1006 free(self->line);
1007 free(self);
1008}
1009
1010static void objdump__add_line(struct list_head *head, struct objdump_line *line)
1011{
1012 list_add_tail(&line->node, head);
1013}
1014
1015struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
1016 struct objdump_line *pos)
1017{
1018 list_for_each_entry_continue(pos, head, node)
1019 if (pos->offset >= 0)
1020 return pos;
1021
1022 return NULL;
1023}
1024
1025static int hist_entry__parse_objdump_line(struct hist_entry *self, FILE *file,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -03001026 struct list_head *head, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001027{
1028 struct symbol *sym = self->ms.sym;
1029 struct objdump_line *objdump_line;
1030 char *line = NULL, *tmp, *tmp2, *c;
1031 size_t line_len;
1032 s64 line_ip, offset = -1;
1033
1034 if (getline(&line, &line_len, file) < 0)
1035 return -1;
1036
1037 if (!line)
1038 return -1;
1039
1040 while (line_len != 0 && isspace(line[line_len - 1]))
1041 line[--line_len] = '\0';
1042
1043 c = strchr(line, '\n');
1044 if (c)
1045 *c = 0;
1046
1047 line_ip = -1;
1048
1049 /*
1050 * Strip leading spaces:
1051 */
1052 tmp = line;
1053 while (*tmp) {
1054 if (*tmp != ' ')
1055 break;
1056 tmp++;
1057 }
1058
1059 if (*tmp) {
1060 /*
1061 * Parse hexa addresses followed by ':'
1062 */
1063 line_ip = strtoull(tmp, &tmp2, 16);
Arnaldo Carvalho de Melo70a7cb32010-07-22 14:04:13 -03001064 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001065 line_ip = -1;
1066 }
1067
1068 if (line_ip != -1) {
Arnaldo Carvalho de Melo70a7cb32010-07-22 14:04:13 -03001069 u64 start = map__rip_2objdump(self->ms.map, sym->start),
1070 end = map__rip_2objdump(self->ms.map, sym->end);
1071
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001072 offset = line_ip - start;
Arnaldo Carvalho de Melo70a7cb32010-07-22 14:04:13 -03001073 if (offset < 0 || (u64)line_ip > end)
1074 offset = -1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001075 }
1076
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -03001077 objdump_line = objdump_line__new(offset, line, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001078 if (objdump_line == NULL) {
1079 free(line);
1080 return -1;
1081 }
1082 objdump__add_line(head, objdump_line);
1083
1084 return 0;
1085}
1086
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -03001087int hist_entry__annotate(struct hist_entry *self, struct list_head *head,
1088 size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001089{
1090 struct symbol *sym = self->ms.sym;
1091 struct map *map = self->ms.map;
1092 struct dso *dso = map->dso;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001093 char *filename = dso__build_id_filename(dso, NULL, 0);
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001094 bool free_filename = true;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001095 char command[PATH_MAX * 2];
1096 FILE *file;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001097 int err = 0;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001098 u64 len;
David Ahernec5761e2010-12-09 13:27:07 -07001099 char symfs_filename[PATH_MAX];
1100
1101 if (filename) {
1102 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
1103 symbol_conf.symfs, filename);
1104 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001105
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001106 if (filename == NULL) {
1107 if (dso->has_build_id) {
1108 pr_err("Can't annotate %s: not enough memory\n",
1109 sym->name);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001110 return -ENOMEM;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001111 }
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001112 goto fallback;
David Ahernec5761e2010-12-09 13:27:07 -07001113 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001114 strstr(command, "[kernel.kallsyms]") ||
David Ahernec5761e2010-12-09 13:27:07 -07001115 access(symfs_filename, R_OK)) {
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001116 free(filename);
1117fallback:
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001118 /*
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001119 * If we don't have build-ids or the build-id file isn't in the
1120 * cache, or is just a kallsyms file, well, lets hope that this
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001121 * DSO is the same as when 'perf record' ran.
1122 */
1123 filename = dso->long_name;
David Ahernec5761e2010-12-09 13:27:07 -07001124 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
1125 symbol_conf.symfs, filename);
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001126 free_filename = false;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001127 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001128
1129 if (dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001130 if (dso->annotate_warned)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001131 goto out_free_filename;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001132 err = -ENOENT;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001133 dso->annotate_warned = 1;
1134 pr_err("Can't annotate %s: No vmlinux file was found in the "
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001135 "path\n", sym->name);
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001136 goto out_free_filename;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001137 }
1138
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001139 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001140 filename, sym->name, map->unmap_ip(map, sym->start),
1141 map->unmap_ip(map, sym->end));
1142
1143 len = sym->end - sym->start;
1144
1145 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1146 dso, dso->long_name, sym, sym->name);
1147
1148 snprintf(command, sizeof(command),
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001149 "objdump --start-address=0x%016" PRIx64 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001150 map__rip_2objdump(map, sym->start),
1151 map__rip_2objdump(map, sym->end),
David Ahernec5761e2010-12-09 13:27:07 -07001152 symfs_filename, filename);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001153
1154 pr_debug("Executing: %s\n", command);
1155
1156 file = popen(command, "r");
1157 if (!file)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001158 goto out_free_filename;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001159
1160 while (!feof(file))
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -03001161 if (hist_entry__parse_objdump_line(self, file, head, privsize) < 0)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001162 break;
1163
1164 pclose(file);
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001165out_free_filename:
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001166 if (free_filename)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001167 free(filename);
1168 return err;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001169}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001170
1171void hists__inc_nr_events(struct hists *self, u32 type)
1172{
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001173 ++self->stats.nr_events[0];
1174 ++self->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001175}
1176
1177size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
1178{
1179 int i;
1180 size_t ret = 0;
1181
1182 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001183 const char *name = event__get_event_name(i);
1184
1185 if (!strcmp(name, "UNKNOWN"))
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001186 continue;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001187
1188 ret += fprintf(fp, "%16s events: %10d\n", name,
1189 self->stats.nr_events[i]);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001190 }
1191
1192 return ret;
1193}