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