blob: d998d1d706eb9358a653ab743d33a6f83be4790d [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 Meloc82ee822010-05-14 14:19:35 -030019static void hist_entry__add_cpumode_period(struct hist_entry *self,
20 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080021{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030022 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080023 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030024 self->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080025 break;
26 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030027 self->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080028 break;
29 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030030 self->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080031 break;
32 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030033 self->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080034 break;
35 default:
36 break;
37 }
38}
39
John Kacur3d1d07e2009-09-28 15:32:55 +020040/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030041 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +020042 */
43
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030044static struct hist_entry *hist_entry__new(struct hist_entry *template)
45{
46 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_node) : 0;
47 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
48
49 if (self != NULL) {
50 *self = *template;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030051 self->nr_events = 1;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030052 if (symbol_conf.use_callchain)
53 callchain_init(self->callchain);
54 }
55
56 return self;
57}
58
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -030059static void hists__inc_nr_entries(struct hists *self, struct hist_entry *entry)
60{
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030061 if (entry->filtered)
62 return;
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -030063 if (entry->ms.sym && self->max_sym_namelen < entry->ms.sym->namelen)
64 self->max_sym_namelen = entry->ms.sym->namelen;
65 ++self->nr_entries;
66}
67
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030068static u8 symbol__parent_filter(const struct symbol *parent)
69{
70 if (symbol_conf.exclude_other && parent == NULL)
71 return 1 << HIST_FILTER__PARENT;
72 return 0;
73}
74
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030075struct hist_entry *__hists__add_entry(struct hists *self,
76 struct addr_location *al,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030077 struct symbol *sym_parent, u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030078{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030079 struct rb_node **p = &self->entries.rb_node;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030080 struct rb_node *parent = NULL;
81 struct hist_entry *he;
82 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -020083 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -030084 .ms = {
85 .map = al->map,
86 .sym = al->sym,
87 },
Arun Sharmaf60f3592010-06-04 11:27:10 -030088 .cpu = al->cpu,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -020089 .ip = al->addr,
90 .level = al->level,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030091 .period = period,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030092 .parent = sym_parent,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030093 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030094 };
95 int cmp;
96
97 while (*p != NULL) {
98 parent = *p;
99 he = rb_entry(parent, struct hist_entry, rb_node);
100
101 cmp = hist_entry__cmp(&entry, he);
102
103 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300104 he->period += period;
105 ++he->nr_events;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300106 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300107 }
108
109 if (cmp < 0)
110 p = &(*p)->rb_left;
111 else
112 p = &(*p)->rb_right;
113 }
114
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300115 he = hist_entry__new(&entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300116 if (!he)
117 return NULL;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300118 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300119 rb_insert_color(&he->rb_node, &self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300120 hists__inc_nr_entries(self, he);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300121out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300122 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300123 return he;
124}
125
John Kacur3d1d07e2009-09-28 15:32:55 +0200126int64_t
127hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
128{
129 struct sort_entry *se;
130 int64_t cmp = 0;
131
132 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200133 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200134 if (cmp)
135 break;
136 }
137
138 return cmp;
139}
140
141int64_t
142hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
143{
144 struct sort_entry *se;
145 int64_t cmp = 0;
146
147 list_for_each_entry(se, &hist_entry__sort_list, list) {
148 int64_t (*f)(struct hist_entry *, struct hist_entry *);
149
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200150 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200151
152 cmp = f(left, right);
153 if (cmp)
154 break;
155 }
156
157 return cmp;
158}
159
160void hist_entry__free(struct hist_entry *he)
161{
162 free(he);
163}
164
165/*
166 * collapse the histogram
167 */
168
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300169static bool collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200170{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200171 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200172 struct rb_node *parent = NULL;
173 struct hist_entry *iter;
174 int64_t cmp;
175
176 while (*p != NULL) {
177 parent = *p;
178 iter = rb_entry(parent, struct hist_entry, rb_node);
179
180 cmp = hist_entry__collapse(iter, he);
181
182 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300183 iter->period += he->period;
John Kacur3d1d07e2009-09-28 15:32:55 +0200184 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300185 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200186 }
187
188 if (cmp < 0)
189 p = &(*p)->rb_left;
190 else
191 p = &(*p)->rb_right;
192 }
193
194 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200195 rb_insert_color(&he->rb_node, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300196 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200197}
198
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300199void hists__collapse_resort(struct hists *self)
John Kacur3d1d07e2009-09-28 15:32:55 +0200200{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200201 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200202 struct rb_node *next;
203 struct hist_entry *n;
204
205 if (!sort__need_collapse)
206 return;
207
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200208 tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300209 next = rb_first(&self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300210 self->nr_entries = 0;
211 self->max_sym_namelen = 0;
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200212
John Kacur3d1d07e2009-09-28 15:32:55 +0200213 while (next) {
214 n = rb_entry(next, struct hist_entry, rb_node);
215 next = rb_next(&n->rb_node);
216
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300217 rb_erase(&n->rb_node, &self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300218 if (collapse__insert_entry(&tmp, n))
219 hists__inc_nr_entries(self, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200220 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200221
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300222 self->entries = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200223}
224
225/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300226 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200227 */
228
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300229static void __hists__insert_output_entry(struct rb_root *entries,
230 struct hist_entry *he,
231 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200232{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300233 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200234 struct rb_node *parent = NULL;
235 struct hist_entry *iter;
236
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200237 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300238 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200239 min_callchain_hits, &callchain_param);
240
241 while (*p != NULL) {
242 parent = *p;
243 iter = rb_entry(parent, struct hist_entry, rb_node);
244
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300245 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200246 p = &(*p)->rb_left;
247 else
248 p = &(*p)->rb_right;
249 }
250
251 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300252 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200253}
254
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300255void hists__output_resort(struct hists *self)
John Kacur3d1d07e2009-09-28 15:32:55 +0200256{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200257 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200258 struct rb_node *next;
259 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200260 u64 min_callchain_hits;
261
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300262 min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200263
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200264 tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300265 next = rb_first(&self->entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200266
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300267 self->nr_entries = 0;
268 self->max_sym_namelen = 0;
269
John Kacur3d1d07e2009-09-28 15:32:55 +0200270 while (next) {
271 n = rb_entry(next, struct hist_entry, rb_node);
272 next = rb_next(&n->rb_node);
273
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300274 rb_erase(&n->rb_node, &self->entries);
275 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300276 hists__inc_nr_entries(self, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200277 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200278
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300279 self->entries = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200280}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200281
282static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
283{
284 int i;
285 int ret = fprintf(fp, " ");
286
287 for (i = 0; i < left_margin; i++)
288 ret += fprintf(fp, " ");
289
290 return ret;
291}
292
293static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
294 int left_margin)
295{
296 int i;
297 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
298
299 for (i = 0; i < depth; i++)
300 if (depth_mask & (1 << i))
301 ret += fprintf(fp, "| ");
302 else
303 ret += fprintf(fp, " ");
304
305 ret += fprintf(fp, "\n");
306
307 return ret;
308}
309
310static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300311 int depth, int depth_mask, int period,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200312 u64 total_samples, int hits,
313 int left_margin)
314{
315 int i;
316 size_t ret = 0;
317
318 ret += callchain__fprintf_left_margin(fp, left_margin);
319 for (i = 0; i < depth; i++) {
320 if (depth_mask & (1 << i))
321 ret += fprintf(fp, "|");
322 else
323 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300324 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200325 double percent;
326
327 percent = hits * 100.0 / total_samples;
328 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
329 } else
330 ret += fprintf(fp, "%s", " ");
331 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300332 if (chain->ms.sym)
333 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200334 else
335 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
336
337 return ret;
338}
339
340static struct symbol *rem_sq_bracket;
341static struct callchain_list rem_hits;
342
343static void init_rem_hits(void)
344{
345 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
346 if (!rem_sq_bracket) {
347 fprintf(stderr, "Not enough memory to display remaining hits\n");
348 return;
349 }
350
351 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300352 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200353}
354
355static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
356 u64 total_samples, int depth,
357 int depth_mask, int left_margin)
358{
359 struct rb_node *node, *next;
360 struct callchain_node *child;
361 struct callchain_list *chain;
362 int new_depth_mask = depth_mask;
363 u64 new_total;
364 u64 remaining;
365 size_t ret = 0;
366 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300367 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200368
369 if (callchain_param.mode == CHAIN_GRAPH_REL)
370 new_total = self->children_hit;
371 else
372 new_total = total_samples;
373
374 remaining = new_total;
375
376 node = rb_first(&self->rb_root);
377 while (node) {
378 u64 cumul;
379
380 child = rb_entry(node, struct callchain_node, rb_node);
381 cumul = cumul_hits(child);
382 remaining -= cumul;
383
384 /*
385 * The depth mask manages the output of pipes that show
386 * the depth. We don't want to keep the pipes of the current
387 * level for the last child of this depth.
388 * Except if we have remaining filtered hits. They will
389 * supersede the last child
390 */
391 next = rb_next(node);
392 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
393 new_depth_mask &= ~(1 << (depth - 1));
394
395 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800396 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200397 * to keep the level link until we reach the last child
398 */
399 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
400 left_margin);
401 i = 0;
402 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200403 ret += ipchain__fprintf_graph(fp, chain, depth,
404 new_depth_mask, i++,
405 new_total,
406 cumul,
407 left_margin);
408 }
409 ret += __callchain__fprintf_graph(fp, child, new_total,
410 depth + 1,
411 new_depth_mask | (1 << depth),
412 left_margin);
413 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300414 if (++entries_printed == callchain_param.print_limit)
415 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200416 }
417
418 if (callchain_param.mode == CHAIN_GRAPH_REL &&
419 remaining && remaining != new_total) {
420
421 if (!rem_sq_bracket)
422 return ret;
423
424 new_depth_mask &= ~(1 << (depth - 1));
425
426 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
427 new_depth_mask, 0, new_total,
428 remaining, left_margin);
429 }
430
431 return ret;
432}
433
434static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
435 u64 total_samples, int left_margin)
436{
437 struct callchain_list *chain;
438 bool printed = false;
439 int i = 0;
440 int ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300441 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200442
443 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200444 if (!i++ && sort__first_dimension == SORT_SYM)
445 continue;
446
447 if (!printed) {
448 ret += callchain__fprintf_left_margin(fp, left_margin);
449 ret += fprintf(fp, "|\n");
450 ret += callchain__fprintf_left_margin(fp, left_margin);
451 ret += fprintf(fp, "---");
452
453 left_margin += 3;
454 printed = true;
455 } else
456 ret += callchain__fprintf_left_margin(fp, left_margin);
457
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300458 if (chain->ms.sym)
459 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200460 else
461 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300462
463 if (++entries_printed == callchain_param.print_limit)
464 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200465 }
466
467 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
468
469 return ret;
470}
471
472static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
473 u64 total_samples)
474{
475 struct callchain_list *chain;
476 size_t ret = 0;
477
478 if (!self)
479 return 0;
480
481 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
482
483
484 list_for_each_entry(chain, &self->val, list) {
485 if (chain->ip >= PERF_CONTEXT_MAX)
486 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300487 if (chain->ms.sym)
488 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200489 else
490 ret += fprintf(fp, " %p\n",
491 (void *)(long)chain->ip);
492 }
493
494 return ret;
495}
496
497static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
498 u64 total_samples, int left_margin)
499{
500 struct rb_node *rb_node;
501 struct callchain_node *chain;
502 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300503 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200504
505 rb_node = rb_first(&self->sorted_chain);
506 while (rb_node) {
507 double percent;
508
509 chain = rb_entry(rb_node, struct callchain_node, rb_node);
510 percent = chain->hit * 100.0 / total_samples;
511 switch (callchain_param.mode) {
512 case CHAIN_FLAT:
513 ret += percent_color_fprintf(fp, " %6.2f%%\n",
514 percent);
515 ret += callchain__fprintf_flat(fp, chain, total_samples);
516 break;
517 case CHAIN_GRAPH_ABS: /* Falldown */
518 case CHAIN_GRAPH_REL:
519 ret += callchain__fprintf_graph(fp, chain, total_samples,
520 left_margin);
521 case CHAIN_NONE:
522 default:
523 break;
524 }
525 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300526 if (++entries_printed == callchain_param.print_limit)
527 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200528 rb_node = rb_next(rb_node);
529 }
530
531 return ret;
532}
533
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300534int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
535 struct hists *pair_hists, bool show_displacement,
536 long displacement, bool color, u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200537{
538 struct sort_entry *se;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300539 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200540 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300541 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200542
543 if (symbol_conf.exclude_other && !self->parent)
544 return 0;
545
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300546 if (pair_hists) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300547 period = self->pair ? self->pair->period : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300548 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300549 period_sys = self->pair ? self->pair->period_sys : 0;
550 period_us = self->pair ? self->pair->period_us : 0;
551 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
552 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200553 } else {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300554 period = self->period;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300555 total = session_total;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300556 period_sys = self->period_sys;
557 period_us = self->period_us;
558 period_guest_sys = self->period_guest_sys;
559 period_guest_us = self->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200560 }
561
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300562 if (total) {
563 if (color)
564 ret = percent_color_snprintf(s, size,
565 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300566 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300567 else
568 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300569 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800570 if (symbol_conf.show_cpu_utilization) {
571 ret += percent_color_snprintf(s + ret, size - ret,
572 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300573 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800574 ret += percent_color_snprintf(s + ret, size - ret,
575 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300576 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800577 if (perf_guest) {
578 ret += percent_color_snprintf(s + ret,
579 size - ret,
580 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300581 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800582 total);
583 ret += percent_color_snprintf(s + ret,
584 size - ret,
585 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300586 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800587 total);
588 }
589 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300590 } else
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300591 ret = snprintf(s, size, sep ? "%lld" : "%12lld ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200592
593 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200594 if (sep)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300595 ret += snprintf(s + ret, size - ret, "%c%lld", *sep, period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200596 else
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300597 ret += snprintf(s + ret, size - ret, "%11lld", period);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200598 }
599
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300600 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200601 char bf[32];
602 double old_percent = 0, new_percent = 0, diff;
603
604 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300605 old_percent = (period * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300606 if (session_total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300607 new_percent = (self->period * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200608
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200609 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200610
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200611 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200612 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
613 else
614 snprintf(bf, sizeof(bf), " ");
615
616 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300617 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200618 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300619 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200620
621 if (show_displacement) {
622 if (displacement)
623 snprintf(bf, sizeof(bf), "%+4ld", displacement);
624 else
625 snprintf(bf, sizeof(bf), " ");
626
627 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300628 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200629 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300630 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200631 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200632 }
633
634 list_for_each_entry(se, &hist_entry__sort_list, list) {
635 if (se->elide)
636 continue;
637
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300638 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200639 ret += se->se_snprintf(self, s + ret, size - ret,
640 se->se_width ? *se->se_width : 0);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200641 }
642
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300643 return ret;
644}
645
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300646int hist_entry__fprintf(struct hist_entry *self, struct hists *pair_hists,
647 bool show_displacement, long displacement, FILE *fp,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300648 u64 session_total)
649{
650 char bf[512];
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300651 hist_entry__snprintf(self, bf, sizeof(bf), pair_hists,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300652 show_displacement, displacement,
653 true, session_total);
654 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300655}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200656
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300657static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
658 u64 session_total)
659{
660 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200661
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300662 if (sort__first_dimension == SORT_COMM) {
663 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
664 typeof(*se), list);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200665 left_margin = se->se_width ? *se->se_width : 0;
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300666 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200667 }
668
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300669 return hist_entry_callchain__fprintf(fp, self, session_total,
670 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200671}
672
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300673size_t hists__fprintf(struct hists *self, struct hists *pair,
674 bool show_displacement, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200675{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200676 struct sort_entry *se;
677 struct rb_node *nd;
678 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200679 unsigned long position = 1;
680 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200681 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200682 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300683 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200684
685 init_rem_hits();
686
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200687 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
688
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200689 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200690 if (sep)
691 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200692 else
693 fputs(" Samples ", fp);
694 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200695
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800696 if (symbol_conf.show_cpu_utilization) {
697 if (sep) {
698 ret += fprintf(fp, "%csys", *sep);
699 ret += fprintf(fp, "%cus", *sep);
700 if (perf_guest) {
701 ret += fprintf(fp, "%cguest sys", *sep);
702 ret += fprintf(fp, "%cguest us", *sep);
703 }
704 } else {
705 ret += fprintf(fp, " sys ");
706 ret += fprintf(fp, " us ");
707 if (perf_guest) {
708 ret += fprintf(fp, " guest sys ");
709 ret += fprintf(fp, " guest us ");
710 }
711 }
712 }
713
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200714 if (pair) {
715 if (sep)
716 ret += fprintf(fp, "%cDelta", *sep);
717 else
718 ret += fprintf(fp, " Delta ");
719
720 if (show_displacement) {
721 if (sep)
722 ret += fprintf(fp, "%cDisplacement", *sep);
723 else
724 ret += fprintf(fp, " Displ");
725 }
726 }
727
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200728 list_for_each_entry(se, &hist_entry__sort_list, list) {
729 if (se->elide)
730 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200731 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200732 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200733 continue;
734 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200735 width = strlen(se->se_header);
736 if (se->se_width) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200737 if (symbol_conf.col_width_list_str) {
738 if (col_width) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200739 *se->se_width = atoi(col_width);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200740 col_width = strchr(col_width, ',');
741 if (col_width)
742 ++col_width;
743 }
744 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200745 width = *se->se_width = max(*se->se_width, width);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200746 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200747 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200748 }
749 fprintf(fp, "\n");
750
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200751 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200752 goto print_entries;
753
754 fprintf(fp, "# ........");
755 if (symbol_conf.show_nr_samples)
756 fprintf(fp, " ..........");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200757 if (pair) {
758 fprintf(fp, " ..........");
759 if (show_displacement)
760 fprintf(fp, " .....");
761 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200762 list_for_each_entry(se, &hist_entry__sort_list, list) {
763 unsigned int i;
764
765 if (se->elide)
766 continue;
767
768 fprintf(fp, " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200769 if (se->se_width)
770 width = *se->se_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200771 else
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200772 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200773 for (i = 0; i < width; i++)
774 fprintf(fp, ".");
775 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200776
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200777 fprintf(fp, "\n#\n");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200778
779print_entries:
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300780 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200781 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
782
783 if (show_displacement) {
784 if (h->pair != NULL)
785 displacement = ((long)h->pair->position -
786 (long)position);
787 else
788 displacement = 0;
789 ++position;
790 }
Eric B Munsoneefc4652010-03-05 12:51:08 -0300791 ret += hist_entry__fprintf(h, pair, show_displacement,
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300792 displacement, fp, self->stats.total_period);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300793
794 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300795 ret += hist_entry__fprintf_callchain(h, fp, self->stats.total_period);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300796
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300797 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300798 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300799 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300800 fprintf(fp, "%.10s end\n", graph_dotted_line);
801 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200802 }
803
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200804 free(rem_sq_bracket);
805
806 return ret;
807}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300808
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300809static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
810 enum hist_filter filter)
811{
812 h->filtered &= ~(1 << filter);
813 if (h->filtered)
814 return;
815
816 ++self->nr_entries;
817 self->stats.total_period += h->period;
818 self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
819
820 if (h->ms.sym && self->max_sym_namelen < h->ms.sym->namelen)
821 self->max_sym_namelen = h->ms.sym->namelen;
822}
823
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300824void hists__filter_by_dso(struct hists *self, const struct dso *dso)
825{
826 struct rb_node *nd;
827
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300828 self->nr_entries = self->stats.total_period = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300829 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300830 self->max_sym_namelen = 0;
831
832 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
833 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
834
835 if (symbol_conf.exclude_other && !h->parent)
836 continue;
837
838 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
839 h->filtered |= (1 << HIST_FILTER__DSO);
840 continue;
841 }
842
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300843 hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300844 }
845}
846
847void hists__filter_by_thread(struct hists *self, const struct thread *thread)
848{
849 struct rb_node *nd;
850
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300851 self->nr_entries = self->stats.total_period = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300852 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300853 self->max_sym_namelen = 0;
854
855 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
856 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
857
858 if (thread != NULL && h->thread != thread) {
859 h->filtered |= (1 << HIST_FILTER__THREAD);
860 continue;
861 }
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300862
863 hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300864 }
865}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300866
867static int symbol__alloc_hist(struct symbol *self)
868{
869 struct sym_priv *priv = symbol__priv(self);
870 const int size = (sizeof(*priv->hist) +
871 (self->end - self->start) * sizeof(u64));
872
873 priv->hist = zalloc(size);
874 return priv->hist == NULL ? -1 : 0;
875}
876
877int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip)
878{
879 unsigned int sym_size, offset;
880 struct symbol *sym = self->ms.sym;
881 struct sym_priv *priv;
882 struct sym_hist *h;
883
884 if (!sym || !self->ms.map)
885 return 0;
886
887 priv = symbol__priv(sym);
888 if (priv->hist == NULL && symbol__alloc_hist(sym) < 0)
889 return -ENOMEM;
890
891 sym_size = sym->end - sym->start;
892 offset = ip - sym->start;
893
894 pr_debug3("%s: ip=%#Lx\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip));
895
896 if (offset >= sym_size)
897 return 0;
898
899 h = priv->hist;
900 h->sum++;
901 h->ip[offset]++;
902
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300903 pr_debug3("%#Lx %s: period++ [ip: %#Lx, %#Lx] => %Ld\n", self->ms.sym->start,
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300904 self->ms.sym->name, ip, ip - self->ms.sym->start, h->ip[offset]);
905 return 0;
906}
907
908static struct objdump_line *objdump_line__new(s64 offset, char *line)
909{
910 struct objdump_line *self = malloc(sizeof(*self));
911
912 if (self != NULL) {
913 self->offset = offset;
914 self->line = line;
915 }
916
917 return self;
918}
919
920void objdump_line__free(struct objdump_line *self)
921{
922 free(self->line);
923 free(self);
924}
925
926static void objdump__add_line(struct list_head *head, struct objdump_line *line)
927{
928 list_add_tail(&line->node, head);
929}
930
931struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
932 struct objdump_line *pos)
933{
934 list_for_each_entry_continue(pos, head, node)
935 if (pos->offset >= 0)
936 return pos;
937
938 return NULL;
939}
940
941static int hist_entry__parse_objdump_line(struct hist_entry *self, FILE *file,
942 struct list_head *head)
943{
944 struct symbol *sym = self->ms.sym;
945 struct objdump_line *objdump_line;
946 char *line = NULL, *tmp, *tmp2, *c;
947 size_t line_len;
948 s64 line_ip, offset = -1;
949
950 if (getline(&line, &line_len, file) < 0)
951 return -1;
952
953 if (!line)
954 return -1;
955
956 while (line_len != 0 && isspace(line[line_len - 1]))
957 line[--line_len] = '\0';
958
959 c = strchr(line, '\n');
960 if (c)
961 *c = 0;
962
963 line_ip = -1;
964
965 /*
966 * Strip leading spaces:
967 */
968 tmp = line;
969 while (*tmp) {
970 if (*tmp != ' ')
971 break;
972 tmp++;
973 }
974
975 if (*tmp) {
976 /*
977 * Parse hexa addresses followed by ':'
978 */
979 line_ip = strtoull(tmp, &tmp2, 16);
Konstantin Stepanyuk75d9ef172010-05-27 02:10:11 +0400980 if (*tmp2 != ':' || tmp == tmp2)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300981 line_ip = -1;
982 }
983
984 if (line_ip != -1) {
985 u64 start = map__rip_2objdump(self->ms.map, sym->start);
986 offset = line_ip - start;
987 }
988
989 objdump_line = objdump_line__new(offset, line);
990 if (objdump_line == NULL) {
991 free(line);
992 return -1;
993 }
994 objdump__add_line(head, objdump_line);
995
996 return 0;
997}
998
999int hist_entry__annotate(struct hist_entry *self, struct list_head *head)
1000{
1001 struct symbol *sym = self->ms.sym;
1002 struct map *map = self->ms.map;
1003 struct dso *dso = map->dso;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001004 char *filename = dso__build_id_filename(dso, NULL, 0);
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001005 bool free_filename = true;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001006 char command[PATH_MAX * 2];
1007 FILE *file;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001008 int err = 0;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001009 u64 len;
1010
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001011 if (filename == NULL) {
1012 if (dso->has_build_id) {
1013 pr_err("Can't annotate %s: not enough memory\n",
1014 sym->name);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001015 return -ENOMEM;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001016 }
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001017 goto fallback;
1018 } else if (readlink(filename, command, sizeof(command)) < 0 ||
1019 strstr(command, "[kernel.kallsyms]") ||
1020 access(filename, R_OK)) {
1021 free(filename);
1022fallback:
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001023 /*
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001024 * If we don't have build-ids or the build-id file isn't in the
1025 * cache, or is just a kallsyms file, well, lets hope that this
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001026 * DSO is the same as when 'perf record' ran.
1027 */
1028 filename = dso->long_name;
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001029 free_filename = false;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001030 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001031
1032 if (dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001033 if (dso->annotate_warned)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001034 goto out_free_filename;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001035 err = -ENOENT;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001036 dso->annotate_warned = 1;
1037 pr_err("Can't annotate %s: No vmlinux file was found in the "
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001038 "path\n", sym->name);
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001039 goto out_free_filename;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001040 }
1041
1042 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
1043 filename, sym->name, map->unmap_ip(map, sym->start),
1044 map->unmap_ip(map, sym->end));
1045
1046 len = sym->end - sym->start;
1047
1048 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1049 dso, dso->long_name, sym, sym->name);
1050
1051 snprintf(command, sizeof(command),
Stephane Eranian45d8e802010-06-03 15:50:01 +02001052 "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS -C %s|grep -v %s|expand",
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001053 map__rip_2objdump(map, sym->start),
1054 map__rip_2objdump(map, sym->end),
1055 filename, filename);
1056
1057 pr_debug("Executing: %s\n", command);
1058
1059 file = popen(command, "r");
1060 if (!file)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001061 goto out_free_filename;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001062
1063 while (!feof(file))
1064 if (hist_entry__parse_objdump_line(self, file, head) < 0)
1065 break;
1066
1067 pclose(file);
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001068out_free_filename:
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001069 if (free_filename)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001070 free(filename);
1071 return err;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001072}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001073
1074void hists__inc_nr_events(struct hists *self, u32 type)
1075{
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001076 ++self->stats.nr_events[0];
1077 ++self->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001078}
1079
1080size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
1081{
1082 int i;
1083 size_t ret = 0;
1084
1085 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
1086 if (!event__name[i])
1087 continue;
1088 ret += fprintf(fp, "%10s events: %10d\n",
1089 event__name[i], self->stats.nr_events[i]);
1090 }
1091
1092 return ret;
1093}