]> bbs.cooldavid.org Git - net-next-2.6.git/blame - tools/perf/util/hist.c
perf annotate: Fix handling of goto labels that are valid hex numbers
[net-next-2.6.git] / tools / perf / util / hist.c
CommitLineData
8a0ecfb8 1#include "util.h"
598357eb 2#include "build-id.h"
3d1d07ec 3#include "hist.h"
4e4f06e4
ACM
4#include "session.h"
5#include "sort.h"
9b33827d 6#include <math.h>
3d1d07ec
JK
7
8struct callchain_param callchain_param = {
9 .mode = CHAIN_GRAPH_REL,
10 .min_percent = 0.5
11};
12
c82ee828
ACM
13static void hist_entry__add_cpumode_period(struct hist_entry *self,
14 unsigned int cpumode, u64 period)
a1645ce1 15{
28e2a106 16 switch (cpumode) {
a1645ce1 17 case PERF_RECORD_MISC_KERNEL:
c82ee828 18 self->period_sys += period;
a1645ce1
ZY
19 break;
20 case PERF_RECORD_MISC_USER:
c82ee828 21 self->period_us += period;
a1645ce1
ZY
22 break;
23 case PERF_RECORD_MISC_GUEST_KERNEL:
c82ee828 24 self->period_guest_sys += period;
a1645ce1
ZY
25 break;
26 case PERF_RECORD_MISC_GUEST_USER:
c82ee828 27 self->period_guest_us += period;
a1645ce1
ZY
28 break;
29 default:
30 break;
31 }
32}
33
3d1d07ec 34/*
c82ee828 35 * histogram, sorted on item, collects periods
3d1d07ec
JK
36 */
37
28e2a106
ACM
38static struct hist_entry *hist_entry__new(struct hist_entry *template)
39{
40 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_node) : 0;
41 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
42
43 if (self != NULL) {
44 *self = *template;
c82ee828 45 self->nr_events = 1;
28e2a106
ACM
46 if (symbol_conf.use_callchain)
47 callchain_init(self->callchain);
48 }
49
50 return self;
51}
52
fefb0b94
ACM
53static void hists__inc_nr_entries(struct hists *self, struct hist_entry *entry)
54{
55 if (entry->ms.sym && self->max_sym_namelen < entry->ms.sym->namelen)
56 self->max_sym_namelen = entry->ms.sym->namelen;
57 ++self->nr_entries;
58}
59
1c02c4d2
ACM
60struct hist_entry *__hists__add_entry(struct hists *self,
61 struct addr_location *al,
c82ee828 62 struct symbol *sym_parent, u64 period)
9735abf1 63{
1c02c4d2 64 struct rb_node **p = &self->entries.rb_node;
9735abf1
ACM
65 struct rb_node *parent = NULL;
66 struct hist_entry *he;
67 struct hist_entry entry = {
1ed091c4 68 .thread = al->thread,
59fd5306
ACM
69 .ms = {
70 .map = al->map,
71 .sym = al->sym,
72 },
1ed091c4
ACM
73 .ip = al->addr,
74 .level = al->level,
c82ee828 75 .period = period,
9735abf1
ACM
76 .parent = sym_parent,
77 };
78 int cmp;
79
80 while (*p != NULL) {
81 parent = *p;
82 he = rb_entry(parent, struct hist_entry, rb_node);
83
84 cmp = hist_entry__cmp(&entry, he);
85
86 if (!cmp) {
c82ee828
ACM
87 he->period += period;
88 ++he->nr_events;
28e2a106 89 goto out;
9735abf1
ACM
90 }
91
92 if (cmp < 0)
93 p = &(*p)->rb_left;
94 else
95 p = &(*p)->rb_right;
96 }
97
28e2a106 98 he = hist_entry__new(&entry);
9735abf1
ACM
99 if (!he)
100 return NULL;
9735abf1 101 rb_link_node(&he->rb_node, parent, p);
1c02c4d2 102 rb_insert_color(&he->rb_node, &self->entries);
fefb0b94 103 hists__inc_nr_entries(self, he);
28e2a106 104out:
c82ee828 105 hist_entry__add_cpumode_period(he, al->cpumode, period);
9735abf1
ACM
106 return he;
107}
108
3d1d07ec
JK
109int64_t
110hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
111{
112 struct sort_entry *se;
113 int64_t cmp = 0;
114
115 list_for_each_entry(se, &hist_entry__sort_list, list) {
fcd14984 116 cmp = se->se_cmp(left, right);
3d1d07ec
JK
117 if (cmp)
118 break;
119 }
120
121 return cmp;
122}
123
124int64_t
125hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
126{
127 struct sort_entry *se;
128 int64_t cmp = 0;
129
130 list_for_each_entry(se, &hist_entry__sort_list, list) {
131 int64_t (*f)(struct hist_entry *, struct hist_entry *);
132
fcd14984 133 f = se->se_collapse ?: se->se_cmp;
3d1d07ec
JK
134
135 cmp = f(left, right);
136 if (cmp)
137 break;
138 }
139
140 return cmp;
141}
142
143void hist_entry__free(struct hist_entry *he)
144{
145 free(he);
146}
147
148/*
149 * collapse the histogram
150 */
151
fefb0b94 152static bool collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
3d1d07ec 153{
b9bf0892 154 struct rb_node **p = &root->rb_node;
3d1d07ec
JK
155 struct rb_node *parent = NULL;
156 struct hist_entry *iter;
157 int64_t cmp;
158
159 while (*p != NULL) {
160 parent = *p;
161 iter = rb_entry(parent, struct hist_entry, rb_node);
162
163 cmp = hist_entry__collapse(iter, he);
164
165 if (!cmp) {
c82ee828 166 iter->period += he->period;
3d1d07ec 167 hist_entry__free(he);
fefb0b94 168 return false;
3d1d07ec
JK
169 }
170
171 if (cmp < 0)
172 p = &(*p)->rb_left;
173 else
174 p = &(*p)->rb_right;
175 }
176
177 rb_link_node(&he->rb_node, parent, p);
b9bf0892 178 rb_insert_color(&he->rb_node, root);
fefb0b94 179 return true;
3d1d07ec
JK
180}
181
1c02c4d2 182void hists__collapse_resort(struct hists *self)
3d1d07ec 183{
b9bf0892 184 struct rb_root tmp;
3d1d07ec
JK
185 struct rb_node *next;
186 struct hist_entry *n;
187
188 if (!sort__need_collapse)
189 return;
190
b9bf0892 191 tmp = RB_ROOT;
1c02c4d2 192 next = rb_first(&self->entries);
fefb0b94
ACM
193 self->nr_entries = 0;
194 self->max_sym_namelen = 0;
b9bf0892 195
3d1d07ec
JK
196 while (next) {
197 n = rb_entry(next, struct hist_entry, rb_node);
198 next = rb_next(&n->rb_node);
199
1c02c4d2 200 rb_erase(&n->rb_node, &self->entries);
fefb0b94
ACM
201 if (collapse__insert_entry(&tmp, n))
202 hists__inc_nr_entries(self, n);
3d1d07ec 203 }
b9bf0892 204
1c02c4d2 205 self->entries = tmp;
3d1d07ec
JK
206}
207
208/*
c82ee828 209 * reverse the map, sort on period.
3d1d07ec
JK
210 */
211
1c02c4d2
ACM
212static void __hists__insert_output_entry(struct rb_root *entries,
213 struct hist_entry *he,
214 u64 min_callchain_hits)
3d1d07ec 215{
1c02c4d2 216 struct rb_node **p = &entries->rb_node;
3d1d07ec
JK
217 struct rb_node *parent = NULL;
218 struct hist_entry *iter;
219
d599db3f 220 if (symbol_conf.use_callchain)
b9fb9304 221 callchain_param.sort(&he->sorted_chain, he->callchain,
3d1d07ec
JK
222 min_callchain_hits, &callchain_param);
223
224 while (*p != NULL) {
225 parent = *p;
226 iter = rb_entry(parent, struct hist_entry, rb_node);
227
c82ee828 228 if (he->period > iter->period)
3d1d07ec
JK
229 p = &(*p)->rb_left;
230 else
231 p = &(*p)->rb_right;
232 }
233
234 rb_link_node(&he->rb_node, parent, p);
1c02c4d2 235 rb_insert_color(&he->rb_node, entries);
3d1d07ec
JK
236}
237
fefb0b94 238void hists__output_resort(struct hists *self)
3d1d07ec 239{
b9bf0892 240 struct rb_root tmp;
3d1d07ec
JK
241 struct rb_node *next;
242 struct hist_entry *n;
3d1d07ec
JK
243 u64 min_callchain_hits;
244
cee75ac7 245 min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
3d1d07ec 246
b9bf0892 247 tmp = RB_ROOT;
1c02c4d2 248 next = rb_first(&self->entries);
3d1d07ec 249
fefb0b94
ACM
250 self->nr_entries = 0;
251 self->max_sym_namelen = 0;
252
3d1d07ec
JK
253 while (next) {
254 n = rb_entry(next, struct hist_entry, rb_node);
255 next = rb_next(&n->rb_node);
256
1c02c4d2
ACM
257 rb_erase(&n->rb_node, &self->entries);
258 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
fefb0b94 259 hists__inc_nr_entries(self, n);
3d1d07ec 260 }
b9bf0892 261
1c02c4d2 262 self->entries = tmp;
3d1d07ec 263}
4ecf84d0
ACM
264
265static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
266{
267 int i;
268 int ret = fprintf(fp, " ");
269
270 for (i = 0; i < left_margin; i++)
271 ret += fprintf(fp, " ");
272
273 return ret;
274}
275
276static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
277 int left_margin)
278{
279 int i;
280 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
281
282 for (i = 0; i < depth; i++)
283 if (depth_mask & (1 << i))
284 ret += fprintf(fp, "| ");
285 else
286 ret += fprintf(fp, " ");
287
288 ret += fprintf(fp, "\n");
289
290 return ret;
291}
292
293static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
c82ee828 294 int depth, int depth_mask, int period,
4ecf84d0
ACM
295 u64 total_samples, int hits,
296 int left_margin)
297{
298 int i;
299 size_t ret = 0;
300
301 ret += callchain__fprintf_left_margin(fp, left_margin);
302 for (i = 0; i < depth; i++) {
303 if (depth_mask & (1 << i))
304 ret += fprintf(fp, "|");
305 else
306 ret += fprintf(fp, " ");
c82ee828 307 if (!period && i == depth - 1) {
4ecf84d0
ACM
308 double percent;
309
310 percent = hits * 100.0 / total_samples;
311 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
312 } else
313 ret += fprintf(fp, "%s", " ");
314 }
b3c9ac08
ACM
315 if (chain->ms.sym)
316 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
4ecf84d0
ACM
317 else
318 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
319
320 return ret;
321}
322
323static struct symbol *rem_sq_bracket;
324static struct callchain_list rem_hits;
325
326static void init_rem_hits(void)
327{
328 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
329 if (!rem_sq_bracket) {
330 fprintf(stderr, "Not enough memory to display remaining hits\n");
331 return;
332 }
333
334 strcpy(rem_sq_bracket->name, "[...]");
b3c9ac08 335 rem_hits.ms.sym = rem_sq_bracket;
4ecf84d0
ACM
336}
337
338static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
339 u64 total_samples, int depth,
340 int depth_mask, int left_margin)
341{
342 struct rb_node *node, *next;
343 struct callchain_node *child;
344 struct callchain_list *chain;
345 int new_depth_mask = depth_mask;
346 u64 new_total;
347 u64 remaining;
348 size_t ret = 0;
349 int i;
232a5c94 350 uint entries_printed = 0;
4ecf84d0
ACM
351
352 if (callchain_param.mode == CHAIN_GRAPH_REL)
353 new_total = self->children_hit;
354 else
355 new_total = total_samples;
356
357 remaining = new_total;
358
359 node = rb_first(&self->rb_root);
360 while (node) {
361 u64 cumul;
362
363 child = rb_entry(node, struct callchain_node, rb_node);
364 cumul = cumul_hits(child);
365 remaining -= cumul;
366
367 /*
368 * The depth mask manages the output of pipes that show
369 * the depth. We don't want to keep the pipes of the current
370 * level for the last child of this depth.
371 * Except if we have remaining filtered hits. They will
372 * supersede the last child
373 */
374 next = rb_next(node);
375 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
376 new_depth_mask &= ~(1 << (depth - 1));
377
378 /*
3ad2f3fb 379 * But we keep the older depth mask for the line separator
4ecf84d0
ACM
380 * to keep the level link until we reach the last child
381 */
382 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
383 left_margin);
384 i = 0;
385 list_for_each_entry(chain, &child->val, list) {
4ecf84d0
ACM
386 ret += ipchain__fprintf_graph(fp, chain, depth,
387 new_depth_mask, i++,
388 new_total,
389 cumul,
390 left_margin);
391 }
392 ret += __callchain__fprintf_graph(fp, child, new_total,
393 depth + 1,
394 new_depth_mask | (1 << depth),
395 left_margin);
396 node = next;
232a5c94
ACM
397 if (++entries_printed == callchain_param.print_limit)
398 break;
4ecf84d0
ACM
399 }
400
401 if (callchain_param.mode == CHAIN_GRAPH_REL &&
402 remaining && remaining != new_total) {
403
404 if (!rem_sq_bracket)
405 return ret;
406
407 new_depth_mask &= ~(1 << (depth - 1));
408
409 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
410 new_depth_mask, 0, new_total,
411 remaining, left_margin);
412 }
413
414 return ret;
415}
416
417static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
418 u64 total_samples, int left_margin)
419{
420 struct callchain_list *chain;
421 bool printed = false;
422 int i = 0;
423 int ret = 0;
232a5c94 424 u32 entries_printed = 0;
4ecf84d0
ACM
425
426 list_for_each_entry(chain, &self->val, list) {
4ecf84d0
ACM
427 if (!i++ && sort__first_dimension == SORT_SYM)
428 continue;
429
430 if (!printed) {
431 ret += callchain__fprintf_left_margin(fp, left_margin);
432 ret += fprintf(fp, "|\n");
433 ret += callchain__fprintf_left_margin(fp, left_margin);
434 ret += fprintf(fp, "---");
435
436 left_margin += 3;
437 printed = true;
438 } else
439 ret += callchain__fprintf_left_margin(fp, left_margin);
440
b3c9ac08
ACM
441 if (chain->ms.sym)
442 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
4ecf84d0
ACM
443 else
444 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
232a5c94
ACM
445
446 if (++entries_printed == callchain_param.print_limit)
447 break;
4ecf84d0
ACM
448 }
449
450 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
451
452 return ret;
453}
454
455static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
456 u64 total_samples)
457{
458 struct callchain_list *chain;
459 size_t ret = 0;
460
461 if (!self)
462 return 0;
463
464 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
465
466
467 list_for_each_entry(chain, &self->val, list) {
468 if (chain->ip >= PERF_CONTEXT_MAX)
469 continue;
b3c9ac08
ACM
470 if (chain->ms.sym)
471 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
4ecf84d0
ACM
472 else
473 ret += fprintf(fp, " %p\n",
474 (void *)(long)chain->ip);
475 }
476
477 return ret;
478}
479
480static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
481 u64 total_samples, int left_margin)
482{
483 struct rb_node *rb_node;
484 struct callchain_node *chain;
485 size_t ret = 0;
232a5c94 486 u32 entries_printed = 0;
4ecf84d0
ACM
487
488 rb_node = rb_first(&self->sorted_chain);
489 while (rb_node) {
490 double percent;
491
492 chain = rb_entry(rb_node, struct callchain_node, rb_node);
493 percent = chain->hit * 100.0 / total_samples;
494 switch (callchain_param.mode) {
495 case CHAIN_FLAT:
496 ret += percent_color_fprintf(fp, " %6.2f%%\n",
497 percent);
498 ret += callchain__fprintf_flat(fp, chain, total_samples);
499 break;
500 case CHAIN_GRAPH_ABS: /* Falldown */
501 case CHAIN_GRAPH_REL:
502 ret += callchain__fprintf_graph(fp, chain, total_samples,
503 left_margin);
504 case CHAIN_NONE:
505 default:
506 break;
507 }
508 ret += fprintf(fp, "\n");
232a5c94
ACM
509 if (++entries_printed == callchain_param.print_limit)
510 break;
4ecf84d0
ACM
511 rb_node = rb_next(rb_node);
512 }
513
514 return ret;
515}
516
1c02c4d2
ACM
517int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
518 struct hists *pair_hists, bool show_displacement,
519 long displacement, bool color, u64 session_total)
4ecf84d0
ACM
520{
521 struct sort_entry *se;
c82ee828 522 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
c351c281 523 const char *sep = symbol_conf.field_sep;
a4e3b956 524 int ret;
4ecf84d0
ACM
525
526 if (symbol_conf.exclude_other && !self->parent)
527 return 0;
528
1c02c4d2 529 if (pair_hists) {
c82ee828 530 period = self->pair ? self->pair->period : 0;
cee75ac7 531 total = pair_hists->stats.total_period;
c82ee828
ACM
532 period_sys = self->pair ? self->pair->period_sys : 0;
533 period_us = self->pair ? self->pair->period_us : 0;
534 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
535 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
c351c281 536 } else {
c82ee828 537 period = self->period;
eefc465c 538 total = session_total;
c82ee828
ACM
539 period_sys = self->period_sys;
540 period_us = self->period_us;
541 period_guest_sys = self->period_guest_sys;
542 period_guest_us = self->period_guest_us;
c351c281
ACM
543 }
544
a4e3b956
ACM
545 if (total) {
546 if (color)
547 ret = percent_color_snprintf(s, size,
548 sep ? "%.2f" : " %6.2f%%",
c82ee828 549 (period * 100.0) / total);
a4e3b956
ACM
550 else
551 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
c82ee828 552 (period * 100.0) / total);
a1645ce1
ZY
553 if (symbol_conf.show_cpu_utilization) {
554 ret += percent_color_snprintf(s + ret, size - ret,
555 sep ? "%.2f" : " %6.2f%%",
c82ee828 556 (period_sys * 100.0) / total);
a1645ce1
ZY
557 ret += percent_color_snprintf(s + ret, size - ret,
558 sep ? "%.2f" : " %6.2f%%",
c82ee828 559 (period_us * 100.0) / total);
a1645ce1
ZY
560 if (perf_guest) {
561 ret += percent_color_snprintf(s + ret,
562 size - ret,
563 sep ? "%.2f" : " %6.2f%%",
c82ee828 564 (period_guest_sys * 100.0) /
a1645ce1
ZY
565 total);
566 ret += percent_color_snprintf(s + ret,
567 size - ret,
568 sep ? "%.2f" : " %6.2f%%",
c82ee828 569 (period_guest_us * 100.0) /
a1645ce1
ZY
570 total);
571 }
572 }
a4e3b956 573 } else
c82ee828 574 ret = snprintf(s, size, sep ? "%lld" : "%12lld ", period);
4ecf84d0
ACM
575
576 if (symbol_conf.show_nr_samples) {
c351c281 577 if (sep)
c82ee828 578 ret += snprintf(s + ret, size - ret, "%c%lld", *sep, period);
4ecf84d0 579 else
c82ee828 580 ret += snprintf(s + ret, size - ret, "%11lld", period);
c351c281
ACM
581 }
582
1c02c4d2 583 if (pair_hists) {
c351c281
ACM
584 char bf[32];
585 double old_percent = 0, new_percent = 0, diff;
586
587 if (total > 0)
c82ee828 588 old_percent = (period * 100.0) / total;
eefc465c 589 if (session_total > 0)
c82ee828 590 new_percent = (self->period * 100.0) / session_total;
c351c281 591
9b33827d 592 diff = new_percent - old_percent;
c351c281 593
9b33827d 594 if (fabs(diff) >= 0.01)
c351c281
ACM
595 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
596 else
597 snprintf(bf, sizeof(bf), " ");
598
599 if (sep)
a4e3b956 600 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
c351c281 601 else
a4e3b956 602 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
c351c281
ACM
603
604 if (show_displacement) {
605 if (displacement)
606 snprintf(bf, sizeof(bf), "%+4ld", displacement);
607 else
608 snprintf(bf, sizeof(bf), " ");
609
610 if (sep)
a4e3b956 611 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
c351c281 612 else
a4e3b956 613 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
c351c281 614 }
4ecf84d0
ACM
615 }
616
617 list_for_each_entry(se, &hist_entry__sort_list, list) {
618 if (se->elide)
619 continue;
620
a4e3b956 621 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
fcd14984
FW
622 ret += se->se_snprintf(self, s + ret, size - ret,
623 se->se_width ? *se->se_width : 0);
4ecf84d0
ACM
624 }
625
a4e3b956
ACM
626 return ret;
627}
628
1c02c4d2
ACM
629int hist_entry__fprintf(struct hist_entry *self, struct hists *pair_hists,
630 bool show_displacement, long displacement, FILE *fp,
a4e3b956
ACM
631 u64 session_total)
632{
633 char bf[512];
58c34390
FW
634 int ret;
635
636 ret = hist_entry__snprintf(self, bf, sizeof(bf), pair_hists,
637 show_displacement, displacement,
638 true, session_total);
639 if (!ret)
640 return 0;
641
a4e3b956 642 return fprintf(fp, "%s\n", bf);
3997d377 643}
4ecf84d0 644
3997d377
ACM
645static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
646 u64 session_total)
647{
648 int left_margin = 0;
4ecf84d0 649
3997d377
ACM
650 if (sort__first_dimension == SORT_COMM) {
651 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
652 typeof(*se), list);
fcd14984 653 left_margin = se->se_width ? *se->se_width : 0;
3997d377 654 left_margin -= thread__comm_len(self->thread);
4ecf84d0
ACM
655 }
656
3997d377
ACM
657 return hist_entry_callchain__fprintf(fp, self, session_total,
658 left_margin);
4ecf84d0
ACM
659}
660
1c02c4d2
ACM
661size_t hists__fprintf(struct hists *self, struct hists *pair,
662 bool show_displacement, FILE *fp)
4ecf84d0 663{
4ecf84d0
ACM
664 struct sort_entry *se;
665 struct rb_node *nd;
666 size_t ret = 0;
c351c281
ACM
667 unsigned long position = 1;
668 long displacement = 0;
4ecf84d0 669 unsigned int width;
c351c281 670 const char *sep = symbol_conf.field_sep;
edb7c60e 671 const char *col_width = symbol_conf.col_width_list_str;
4ecf84d0
ACM
672
673 init_rem_hits();
674
c351c281
ACM
675 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
676
4ecf84d0 677 if (symbol_conf.show_nr_samples) {
c351c281
ACM
678 if (sep)
679 fprintf(fp, "%cSamples", *sep);
4ecf84d0
ACM
680 else
681 fputs(" Samples ", fp);
682 }
c351c281 683
a1645ce1
ZY
684 if (symbol_conf.show_cpu_utilization) {
685 if (sep) {
686 ret += fprintf(fp, "%csys", *sep);
687 ret += fprintf(fp, "%cus", *sep);
688 if (perf_guest) {
689 ret += fprintf(fp, "%cguest sys", *sep);
690 ret += fprintf(fp, "%cguest us", *sep);
691 }
692 } else {
693 ret += fprintf(fp, " sys ");
694 ret += fprintf(fp, " us ");
695 if (perf_guest) {
696 ret += fprintf(fp, " guest sys ");
697 ret += fprintf(fp, " guest us ");
698 }
699 }
700 }
701
c351c281
ACM
702 if (pair) {
703 if (sep)
704 ret += fprintf(fp, "%cDelta", *sep);
705 else
706 ret += fprintf(fp, " Delta ");
707
708 if (show_displacement) {
709 if (sep)
710 ret += fprintf(fp, "%cDisplacement", *sep);
711 else
712 ret += fprintf(fp, " Displ");
713 }
714 }
715
4ecf84d0
ACM
716 list_for_each_entry(se, &hist_entry__sort_list, list) {
717 if (se->elide)
718 continue;
c351c281 719 if (sep) {
fcd14984 720 fprintf(fp, "%c%s", *sep, se->se_header);
4ecf84d0
ACM
721 continue;
722 }
fcd14984
FW
723 width = strlen(se->se_header);
724 if (se->se_width) {
4ecf84d0
ACM
725 if (symbol_conf.col_width_list_str) {
726 if (col_width) {
fcd14984 727 *se->se_width = atoi(col_width);
4ecf84d0
ACM
728 col_width = strchr(col_width, ',');
729 if (col_width)
730 ++col_width;
731 }
732 }
fcd14984 733 width = *se->se_width = max(*se->se_width, width);
4ecf84d0 734 }
fcd14984 735 fprintf(fp, " %*s", width, se->se_header);
4ecf84d0
ACM
736 }
737 fprintf(fp, "\n");
738
c351c281 739 if (sep)
4ecf84d0
ACM
740 goto print_entries;
741
742 fprintf(fp, "# ........");
743 if (symbol_conf.show_nr_samples)
744 fprintf(fp, " ..........");
c351c281
ACM
745 if (pair) {
746 fprintf(fp, " ..........");
747 if (show_displacement)
748 fprintf(fp, " .....");
749 }
4ecf84d0
ACM
750 list_for_each_entry(se, &hist_entry__sort_list, list) {
751 unsigned int i;
752
753 if (se->elide)
754 continue;
755
756 fprintf(fp, " ");
fcd14984
FW
757 if (se->se_width)
758 width = *se->se_width;
4ecf84d0 759 else
fcd14984 760 width = strlen(se->se_header);
4ecf84d0
ACM
761 for (i = 0; i < width; i++)
762 fprintf(fp, ".");
763 }
4ecf84d0 764
c351c281 765 fprintf(fp, "\n#\n");
4ecf84d0
ACM
766
767print_entries:
1c02c4d2 768 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
c351c281 769 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
58c34390 770 int cnt;
c351c281
ACM
771
772 if (show_displacement) {
773 if (h->pair != NULL)
774 displacement = ((long)h->pair->position -
775 (long)position);
776 else
777 displacement = 0;
778 ++position;
779 }
58c34390
FW
780 cnt = hist_entry__fprintf(h, pair, show_displacement,
781 displacement, fp, self->stats.total_period);
782 /* Ignore those that didn't match the parent filter */
783 if (!cnt)
784 continue;
785
786 ret += cnt;
3997d377
ACM
787
788 if (symbol_conf.use_callchain)
cee75ac7 789 ret += hist_entry__fprintf_callchain(h, fp, self->stats.total_period);
3997d377 790
59fd5306 791 if (h->ms.map == NULL && verbose > 1) {
65f2ed2b 792 __map_groups__fprintf_maps(&h->thread->mg,
c6e718ff 793 MAP__FUNCTION, verbose, fp);
65f2ed2b
ACM
794 fprintf(fp, "%.10s end\n", graph_dotted_line);
795 }
4ecf84d0
ACM
796 }
797
4ecf84d0
ACM
798 free(rem_sq_bracket);
799
800 return ret;
801}
b09e0190
ACM
802
803enum hist_filter {
804 HIST_FILTER__DSO,
805 HIST_FILTER__THREAD,
806};
807
808void hists__filter_by_dso(struct hists *self, const struct dso *dso)
809{
810 struct rb_node *nd;
811
cee75ac7 812 self->nr_entries = self->stats.total_period = 0;
c82ee828 813 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
b09e0190
ACM
814 self->max_sym_namelen = 0;
815
816 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
817 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
818
819 if (symbol_conf.exclude_other && !h->parent)
820 continue;
821
822 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
823 h->filtered |= (1 << HIST_FILTER__DSO);
824 continue;
825 }
826
827 h->filtered &= ~(1 << HIST_FILTER__DSO);
828 if (!h->filtered) {
829 ++self->nr_entries;
c82ee828
ACM
830 self->stats.total_period += h->period;
831 self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
b09e0190
ACM
832 if (h->ms.sym &&
833 self->max_sym_namelen < h->ms.sym->namelen)
834 self->max_sym_namelen = h->ms.sym->namelen;
835 }
836 }
837}
838
839void hists__filter_by_thread(struct hists *self, const struct thread *thread)
840{
841 struct rb_node *nd;
842
cee75ac7 843 self->nr_entries = self->stats.total_period = 0;
c82ee828 844 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
b09e0190
ACM
845 self->max_sym_namelen = 0;
846
847 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
848 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
849
850 if (thread != NULL && h->thread != thread) {
851 h->filtered |= (1 << HIST_FILTER__THREAD);
852 continue;
853 }
854 h->filtered &= ~(1 << HIST_FILTER__THREAD);
855 if (!h->filtered) {
856 ++self->nr_entries;
c82ee828
ACM
857 self->stats.total_period += h->period;
858 self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
b09e0190
ACM
859 if (h->ms.sym &&
860 self->max_sym_namelen < h->ms.sym->namelen)
861 self->max_sym_namelen = h->ms.sym->namelen;
862 }
863 }
864}
ef7b93a1
ACM
865
866static int symbol__alloc_hist(struct symbol *self)
867{
868 struct sym_priv *priv = symbol__priv(self);
869 const int size = (sizeof(*priv->hist) +
870 (self->end - self->start) * sizeof(u64));
871
872 priv->hist = zalloc(size);
873 return priv->hist == NULL ? -1 : 0;
874}
875
876int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip)
877{
878 unsigned int sym_size, offset;
879 struct symbol *sym = self->ms.sym;
880 struct sym_priv *priv;
881 struct sym_hist *h;
882
883 if (!sym || !self->ms.map)
884 return 0;
885
886 priv = symbol__priv(sym);
887 if (priv->hist == NULL && symbol__alloc_hist(sym) < 0)
888 return -ENOMEM;
889
890 sym_size = sym->end - sym->start;
891 offset = ip - sym->start;
892
893 pr_debug3("%s: ip=%#Lx\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip));
894
895 if (offset >= sym_size)
896 return 0;
897
898 h = priv->hist;
899 h->sum++;
900 h->ip[offset]++;
901
c82ee828 902 pr_debug3("%#Lx %s: period++ [ip: %#Lx, %#Lx] => %Ld\n", self->ms.sym->start,
ef7b93a1
ACM
903 self->ms.sym->name, ip, ip - self->ms.sym->start, h->ip[offset]);
904 return 0;
905}
906
907static struct objdump_line *objdump_line__new(s64 offset, char *line)
908{
909 struct objdump_line *self = malloc(sizeof(*self));
910
911 if (self != NULL) {
912 self->offset = offset;
913 self->line = line;
914 }
915
916 return self;
917}
918
919void objdump_line__free(struct objdump_line *self)
920{
921 free(self->line);
922 free(self);
923}
924
925static void objdump__add_line(struct list_head *head, struct objdump_line *line)
926{
927 list_add_tail(&line->node, head);
928}
929
930struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
931 struct objdump_line *pos)
932{
933 list_for_each_entry_continue(pos, head, node)
934 if (pos->offset >= 0)
935 return pos;
936
937 return NULL;
938}
939
940static int hist_entry__parse_objdump_line(struct hist_entry *self, FILE *file,
941 struct list_head *head)
942{
943 struct symbol *sym = self->ms.sym;
944 struct objdump_line *objdump_line;
945 char *line = NULL, *tmp, *tmp2, *c;
946 size_t line_len;
947 s64 line_ip, offset = -1;
948
949 if (getline(&line, &line_len, file) < 0)
950 return -1;
951
952 if (!line)
953 return -1;
954
955 while (line_len != 0 && isspace(line[line_len - 1]))
956 line[--line_len] = '\0';
957
958 c = strchr(line, '\n');
959 if (c)
960 *c = 0;
961
962 line_ip = -1;
963
964 /*
965 * Strip leading spaces:
966 */
967 tmp = line;
968 while (*tmp) {
969 if (*tmp != ' ')
970 break;
971 tmp++;
972 }
973
974 if (*tmp) {
975 /*
976 * Parse hexa addresses followed by ':'
977 */
978 line_ip = strtoull(tmp, &tmp2, 16);
70a7cb3b 979 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
ef7b93a1
ACM
980 line_ip = -1;
981 }
982
983 if (line_ip != -1) {
70a7cb3b
ACM
984 u64 start = map__rip_2objdump(self->ms.map, sym->start),
985 end = map__rip_2objdump(self->ms.map, sym->end);
986
ef7b93a1 987 offset = line_ip - start;
70a7cb3b
ACM
988 if (offset < 0 || (u64)line_ip > end)
989 offset = -1;
ef7b93a1
ACM
990 }
991
992 objdump_line = objdump_line__new(offset, line);
993 if (objdump_line == NULL) {
994 free(line);
995 return -1;
996 }
997 objdump__add_line(head, objdump_line);
998
999 return 0;
1000}
1001
1002int hist_entry__annotate(struct hist_entry *self, struct list_head *head)
1003{
1004 struct symbol *sym = self->ms.sym;
1005 struct map *map = self->ms.map;
1006 struct dso *dso = map->dso;
b36f19d5 1007 char *filename = dso__build_id_filename(dso, NULL, 0);
44bf4606 1008 bool free_filename = true;
ef7b93a1
ACM
1009 char command[PATH_MAX * 2];
1010 FILE *file;
46e3e055 1011 int err = 0;
ef7b93a1
ACM
1012 u64 len;
1013
b36f19d5
ACM
1014 if (filename == NULL) {
1015 if (dso->has_build_id) {
1016 pr_err("Can't annotate %s: not enough memory\n",
1017 sym->name);
46e3e055 1018 return -ENOMEM;
b36f19d5 1019 }
44bf4606
ACM
1020 goto fallback;
1021 } else if (readlink(filename, command, sizeof(command)) < 0 ||
1022 strstr(command, "[kernel.kallsyms]") ||
1023 access(filename, R_OK)) {
1024 free(filename);
1025fallback:
b36f19d5 1026 /*
44bf4606
ACM
1027 * If we don't have build-ids or the build-id file isn't in the
1028 * cache, or is just a kallsyms file, well, lets hope that this
b36f19d5
ACM
1029 * DSO is the same as when 'perf record' ran.
1030 */
1031 filename = dso->long_name;
44bf4606 1032 free_filename = false;
b36f19d5 1033 }
ef7b93a1
ACM
1034
1035 if (dso->origin == DSO__ORIG_KERNEL) {
46e3e055 1036 if (dso->annotate_warned)
b36f19d5 1037 goto out_free_filename;
46e3e055 1038 err = -ENOENT;
ef7b93a1
ACM
1039 dso->annotate_warned = 1;
1040 pr_err("Can't annotate %s: No vmlinux file was found in the "
46e3e055 1041 "path\n", sym->name);
b36f19d5 1042 goto out_free_filename;
ef7b93a1
ACM
1043 }
1044
1045 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
1046 filename, sym->name, map->unmap_ip(map, sym->start),
1047 map->unmap_ip(map, sym->end));
1048
1049 len = sym->end - sym->start;
1050
1051 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1052 dso, dso->long_name, sym, sym->name);
1053
1054 snprintf(command, sizeof(command),
1055 "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s|expand",
1056 map__rip_2objdump(map, sym->start),
1057 map__rip_2objdump(map, sym->end),
1058 filename, filename);
1059
1060 pr_debug("Executing: %s\n", command);
1061
1062 file = popen(command, "r");
1063 if (!file)
b36f19d5 1064 goto out_free_filename;
ef7b93a1
ACM
1065
1066 while (!feof(file))
1067 if (hist_entry__parse_objdump_line(self, file, head) < 0)
1068 break;
1069
1070 pclose(file);
b36f19d5 1071out_free_filename:
44bf4606 1072 if (free_filename)
b36f19d5
ACM
1073 free(filename);
1074 return err;
ef7b93a1 1075}
c8446b9b
ACM
1076
1077void hists__inc_nr_events(struct hists *self, u32 type)
1078{
cee75ac7
ACM
1079 ++self->stats.nr_events[0];
1080 ++self->stats.nr_events[type];
c8446b9b
ACM
1081}
1082
1083size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
1084{
1085 int i;
1086 size_t ret = 0;
1087
1088 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
1089 if (!event__name[i])
1090 continue;
1091 ret += fprintf(fp, "%10s events: %10d\n",
1092 event__name[i], self->stats.nr_events[i]);
1093 }
1094
1095 return ret;
1096}