]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame_incremental - tools/perf/util/hist.c
perf hists: Fix children sort key behavior
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / hist.c
... / ...
CommitLineData
1#include "util.h"
2#include "build-id.h"
3#include "hist.h"
4#include "session.h"
5#include "sort.h"
6#include "evlist.h"
7#include "evsel.h"
8#include "annotate.h"
9#include "ui/progress.h"
10#include <math.h>
11
12static bool hists__filter_entry_by_dso(struct hists *hists,
13 struct hist_entry *he);
14static bool hists__filter_entry_by_thread(struct hists *hists,
15 struct hist_entry *he);
16static bool hists__filter_entry_by_symbol(struct hists *hists,
17 struct hist_entry *he);
18
19u16 hists__col_len(struct hists *hists, enum hist_column col)
20{
21 return hists->col_len[col];
22}
23
24void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
25{
26 hists->col_len[col] = len;
27}
28
29bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
30{
31 if (len > hists__col_len(hists, col)) {
32 hists__set_col_len(hists, col, len);
33 return true;
34 }
35 return false;
36}
37
38void hists__reset_col_len(struct hists *hists)
39{
40 enum hist_column col;
41
42 for (col = 0; col < HISTC_NR_COLS; ++col)
43 hists__set_col_len(hists, col, 0);
44}
45
46static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
47{
48 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
49
50 if (hists__col_len(hists, dso) < unresolved_col_width &&
51 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
52 !symbol_conf.dso_list)
53 hists__set_col_len(hists, dso, unresolved_col_width);
54}
55
56void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
57{
58 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
59 int symlen;
60 u16 len;
61
62 /*
63 * +4 accounts for '[x] ' priv level info
64 * +2 accounts for 0x prefix on raw addresses
65 * +3 accounts for ' y ' symtab origin info
66 */
67 if (h->ms.sym) {
68 symlen = h->ms.sym->namelen + 4;
69 if (verbose)
70 symlen += BITS_PER_LONG / 4 + 2 + 3;
71 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
72 } else {
73 symlen = unresolved_col_width + 4 + 2;
74 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
75 hists__set_unres_dso_col_len(hists, HISTC_DSO);
76 }
77
78 len = thread__comm_len(h->thread);
79 if (hists__new_col_len(hists, HISTC_COMM, len))
80 hists__set_col_len(hists, HISTC_THREAD, len + 6);
81
82 if (h->ms.map) {
83 len = dso__name_len(h->ms.map->dso);
84 hists__new_col_len(hists, HISTC_DSO, len);
85 }
86
87 if (h->parent)
88 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
89
90 if (h->branch_info) {
91 if (h->branch_info->from.sym) {
92 symlen = (int)h->branch_info->from.sym->namelen + 4;
93 if (verbose)
94 symlen += BITS_PER_LONG / 4 + 2 + 3;
95 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
96
97 symlen = dso__name_len(h->branch_info->from.map->dso);
98 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
99 } else {
100 symlen = unresolved_col_width + 4 + 2;
101 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
102 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
103 }
104
105 if (h->branch_info->to.sym) {
106 symlen = (int)h->branch_info->to.sym->namelen + 4;
107 if (verbose)
108 symlen += BITS_PER_LONG / 4 + 2 + 3;
109 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
110
111 symlen = dso__name_len(h->branch_info->to.map->dso);
112 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
113 } else {
114 symlen = unresolved_col_width + 4 + 2;
115 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
116 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
117 }
118 }
119
120 if (h->mem_info) {
121 if (h->mem_info->daddr.sym) {
122 symlen = (int)h->mem_info->daddr.sym->namelen + 4
123 + unresolved_col_width + 2;
124 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
125 symlen);
126 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
127 symlen + 1);
128 } else {
129 symlen = unresolved_col_width + 4 + 2;
130 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
131 symlen);
132 }
133 if (h->mem_info->daddr.map) {
134 symlen = dso__name_len(h->mem_info->daddr.map->dso);
135 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
136 symlen);
137 } else {
138 symlen = unresolved_col_width + 4 + 2;
139 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
140 }
141 } else {
142 symlen = unresolved_col_width + 4 + 2;
143 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
144 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
145 }
146
147 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
148 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
149 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
150 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
151 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
152 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
153
154 if (h->transaction)
155 hists__new_col_len(hists, HISTC_TRANSACTION,
156 hist_entry__transaction_len());
157}
158
159void hists__output_recalc_col_len(struct hists *hists, int max_rows)
160{
161 struct rb_node *next = rb_first(&hists->entries);
162 struct hist_entry *n;
163 int row = 0;
164
165 hists__reset_col_len(hists);
166
167 while (next && row++ < max_rows) {
168 n = rb_entry(next, struct hist_entry, rb_node);
169 if (!n->filtered)
170 hists__calc_col_len(hists, n);
171 next = rb_next(&n->rb_node);
172 }
173}
174
175static void he_stat__add_cpumode_period(struct he_stat *he_stat,
176 unsigned int cpumode, u64 period)
177{
178 switch (cpumode) {
179 case PERF_RECORD_MISC_KERNEL:
180 he_stat->period_sys += period;
181 break;
182 case PERF_RECORD_MISC_USER:
183 he_stat->period_us += period;
184 break;
185 case PERF_RECORD_MISC_GUEST_KERNEL:
186 he_stat->period_guest_sys += period;
187 break;
188 case PERF_RECORD_MISC_GUEST_USER:
189 he_stat->period_guest_us += period;
190 break;
191 default:
192 break;
193 }
194}
195
196static void he_stat__add_period(struct he_stat *he_stat, u64 period,
197 u64 weight)
198{
199
200 he_stat->period += period;
201 he_stat->weight += weight;
202 he_stat->nr_events += 1;
203}
204
205static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
206{
207 dest->period += src->period;
208 dest->period_sys += src->period_sys;
209 dest->period_us += src->period_us;
210 dest->period_guest_sys += src->period_guest_sys;
211 dest->period_guest_us += src->period_guest_us;
212 dest->nr_events += src->nr_events;
213 dest->weight += src->weight;
214}
215
216static void he_stat__decay(struct he_stat *he_stat)
217{
218 he_stat->period = (he_stat->period * 7) / 8;
219 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
220 /* XXX need decay for weight too? */
221}
222
223static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
224{
225 u64 prev_period = he->stat.period;
226 u64 diff;
227
228 if (prev_period == 0)
229 return true;
230
231 he_stat__decay(&he->stat);
232 if (symbol_conf.cumulate_callchain)
233 he_stat__decay(he->stat_acc);
234
235 diff = prev_period - he->stat.period;
236
237 hists->stats.total_period -= diff;
238 if (!he->filtered)
239 hists->stats.total_non_filtered_period -= diff;
240
241 return he->stat.period == 0;
242}
243
244void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
245{
246 struct rb_node *next = rb_first(&hists->entries);
247 struct hist_entry *n;
248
249 while (next) {
250 n = rb_entry(next, struct hist_entry, rb_node);
251 next = rb_next(&n->rb_node);
252 /*
253 * We may be annotating this, for instance, so keep it here in
254 * case some it gets new samples, we'll eventually free it when
255 * the user stops browsing and it agains gets fully decayed.
256 */
257 if (((zap_user && n->level == '.') ||
258 (zap_kernel && n->level != '.') ||
259 hists__decay_entry(hists, n)) &&
260 !n->used) {
261 rb_erase(&n->rb_node, &hists->entries);
262
263 if (sort__need_collapse)
264 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
265
266 --hists->nr_entries;
267 if (!n->filtered)
268 --hists->nr_non_filtered_entries;
269
270 hist_entry__free(n);
271 }
272 }
273}
274
275void hists__delete_entries(struct hists *hists)
276{
277 struct rb_node *next = rb_first(&hists->entries);
278 struct hist_entry *n;
279
280 while (next) {
281 n = rb_entry(next, struct hist_entry, rb_node);
282 next = rb_next(&n->rb_node);
283
284 rb_erase(&n->rb_node, &hists->entries);
285
286 if (sort__need_collapse)
287 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
288
289 --hists->nr_entries;
290 if (!n->filtered)
291 --hists->nr_non_filtered_entries;
292
293 hist_entry__free(n);
294 }
295}
296
297/*
298 * histogram, sorted on item, collects periods
299 */
300
301static struct hist_entry *hist_entry__new(struct hist_entry *template,
302 bool sample_self)
303{
304 size_t callchain_size = 0;
305 struct hist_entry *he;
306
307 if (symbol_conf.use_callchain)
308 callchain_size = sizeof(struct callchain_root);
309
310 he = zalloc(sizeof(*he) + callchain_size);
311
312 if (he != NULL) {
313 *he = *template;
314
315 if (symbol_conf.cumulate_callchain) {
316 he->stat_acc = malloc(sizeof(he->stat));
317 if (he->stat_acc == NULL) {
318 free(he);
319 return NULL;
320 }
321 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
322 if (!sample_self)
323 memset(&he->stat, 0, sizeof(he->stat));
324 }
325
326 if (he->ms.map)
327 he->ms.map->referenced = true;
328
329 if (he->branch_info) {
330 /*
331 * This branch info is (a part of) allocated from
332 * sample__resolve_bstack() and will be freed after
333 * adding new entries. So we need to save a copy.
334 */
335 he->branch_info = malloc(sizeof(*he->branch_info));
336 if (he->branch_info == NULL) {
337 free(he->stat_acc);
338 free(he);
339 return NULL;
340 }
341
342 memcpy(he->branch_info, template->branch_info,
343 sizeof(*he->branch_info));
344
345 if (he->branch_info->from.map)
346 he->branch_info->from.map->referenced = true;
347 if (he->branch_info->to.map)
348 he->branch_info->to.map->referenced = true;
349 }
350
351 if (he->mem_info) {
352 if (he->mem_info->iaddr.map)
353 he->mem_info->iaddr.map->referenced = true;
354 if (he->mem_info->daddr.map)
355 he->mem_info->daddr.map->referenced = true;
356 }
357
358 if (symbol_conf.use_callchain)
359 callchain_init(he->callchain);
360
361 INIT_LIST_HEAD(&he->pairs.node);
362 }
363
364 return he;
365}
366
367static u8 symbol__parent_filter(const struct symbol *parent)
368{
369 if (symbol_conf.exclude_other && parent == NULL)
370 return 1 << HIST_FILTER__PARENT;
371 return 0;
372}
373
374static struct hist_entry *add_hist_entry(struct hists *hists,
375 struct hist_entry *entry,
376 struct addr_location *al,
377 bool sample_self)
378{
379 struct rb_node **p;
380 struct rb_node *parent = NULL;
381 struct hist_entry *he;
382 int64_t cmp;
383 u64 period = entry->stat.period;
384 u64 weight = entry->stat.weight;
385
386 p = &hists->entries_in->rb_node;
387
388 while (*p != NULL) {
389 parent = *p;
390 he = rb_entry(parent, struct hist_entry, rb_node_in);
391
392 /*
393 * Make sure that it receives arguments in a same order as
394 * hist_entry__collapse() so that we can use an appropriate
395 * function when searching an entry regardless which sort
396 * keys were used.
397 */
398 cmp = hist_entry__cmp(he, entry);
399
400 if (!cmp) {
401 if (sample_self)
402 he_stat__add_period(&he->stat, period, weight);
403 if (symbol_conf.cumulate_callchain)
404 he_stat__add_period(he->stat_acc, period, weight);
405
406 /*
407 * This mem info was allocated from sample__resolve_mem
408 * and will not be used anymore.
409 */
410 zfree(&entry->mem_info);
411
412 /* If the map of an existing hist_entry has
413 * become out-of-date due to an exec() or
414 * similar, update it. Otherwise we will
415 * mis-adjust symbol addresses when computing
416 * the history counter to increment.
417 */
418 if (he->ms.map != entry->ms.map) {
419 he->ms.map = entry->ms.map;
420 if (he->ms.map)
421 he->ms.map->referenced = true;
422 }
423 goto out;
424 }
425
426 if (cmp < 0)
427 p = &(*p)->rb_left;
428 else
429 p = &(*p)->rb_right;
430 }
431
432 he = hist_entry__new(entry, sample_self);
433 if (!he)
434 return NULL;
435
436 rb_link_node(&he->rb_node_in, parent, p);
437 rb_insert_color(&he->rb_node_in, hists->entries_in);
438out:
439 if (sample_self)
440 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
441 if (symbol_conf.cumulate_callchain)
442 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
443 return he;
444}
445
446struct hist_entry *__hists__add_entry(struct hists *hists,
447 struct addr_location *al,
448 struct symbol *sym_parent,
449 struct branch_info *bi,
450 struct mem_info *mi,
451 u64 period, u64 weight, u64 transaction,
452 bool sample_self)
453{
454 struct hist_entry entry = {
455 .thread = al->thread,
456 .comm = thread__comm(al->thread),
457 .ms = {
458 .map = al->map,
459 .sym = al->sym,
460 },
461 .cpu = al->cpu,
462 .cpumode = al->cpumode,
463 .ip = al->addr,
464 .level = al->level,
465 .stat = {
466 .nr_events = 1,
467 .period = period,
468 .weight = weight,
469 },
470 .parent = sym_parent,
471 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
472 .hists = hists,
473 .branch_info = bi,
474 .mem_info = mi,
475 .transaction = transaction,
476 };
477
478 return add_hist_entry(hists, &entry, al, sample_self);
479}
480
481static int
482iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
483 struct addr_location *al __maybe_unused)
484{
485 return 0;
486}
487
488static int
489iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
490 struct addr_location *al __maybe_unused)
491{
492 return 0;
493}
494
495static int
496iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
497{
498 struct perf_sample *sample = iter->sample;
499 struct mem_info *mi;
500
501 mi = sample__resolve_mem(sample, al);
502 if (mi == NULL)
503 return -ENOMEM;
504
505 iter->priv = mi;
506 return 0;
507}
508
509static int
510iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
511{
512 u64 cost;
513 struct mem_info *mi = iter->priv;
514 struct hists *hists = evsel__hists(iter->evsel);
515 struct hist_entry *he;
516
517 if (mi == NULL)
518 return -EINVAL;
519
520 cost = iter->sample->weight;
521 if (!cost)
522 cost = 1;
523
524 /*
525 * must pass period=weight in order to get the correct
526 * sorting from hists__collapse_resort() which is solely
527 * based on periods. We want sorting be done on nr_events * weight
528 * and this is indirectly achieved by passing period=weight here
529 * and the he_stat__add_period() function.
530 */
531 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
532 cost, cost, 0, true);
533 if (!he)
534 return -ENOMEM;
535
536 iter->he = he;
537 return 0;
538}
539
540static int
541iter_finish_mem_entry(struct hist_entry_iter *iter,
542 struct addr_location *al __maybe_unused)
543{
544 struct perf_evsel *evsel = iter->evsel;
545 struct hists *hists = evsel__hists(evsel);
546 struct hist_entry *he = iter->he;
547 int err = -EINVAL;
548
549 if (he == NULL)
550 goto out;
551
552 hists__inc_nr_samples(hists, he->filtered);
553
554 err = hist_entry__append_callchain(he, iter->sample);
555
556out:
557 /*
558 * We don't need to free iter->priv (mem_info) here since
559 * the mem info was either already freed in add_hist_entry() or
560 * passed to a new hist entry by hist_entry__new().
561 */
562 iter->priv = NULL;
563
564 iter->he = NULL;
565 return err;
566}
567
568static int
569iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
570{
571 struct branch_info *bi;
572 struct perf_sample *sample = iter->sample;
573
574 bi = sample__resolve_bstack(sample, al);
575 if (!bi)
576 return -ENOMEM;
577
578 iter->curr = 0;
579 iter->total = sample->branch_stack->nr;
580
581 iter->priv = bi;
582 return 0;
583}
584
585static int
586iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
587 struct addr_location *al __maybe_unused)
588{
589 /* to avoid calling callback function */
590 iter->he = NULL;
591
592 return 0;
593}
594
595static int
596iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
597{
598 struct branch_info *bi = iter->priv;
599 int i = iter->curr;
600
601 if (bi == NULL)
602 return 0;
603
604 if (iter->curr >= iter->total)
605 return 0;
606
607 al->map = bi[i].to.map;
608 al->sym = bi[i].to.sym;
609 al->addr = bi[i].to.addr;
610 return 1;
611}
612
613static int
614iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
615{
616 struct branch_info *bi;
617 struct perf_evsel *evsel = iter->evsel;
618 struct hists *hists = evsel__hists(evsel);
619 struct hist_entry *he = NULL;
620 int i = iter->curr;
621 int err = 0;
622
623 bi = iter->priv;
624
625 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
626 goto out;
627
628 /*
629 * The report shows the percentage of total branches captured
630 * and not events sampled. Thus we use a pseudo period of 1.
631 */
632 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
633 1, 1, 0, true);
634 if (he == NULL)
635 return -ENOMEM;
636
637 hists__inc_nr_samples(hists, he->filtered);
638
639out:
640 iter->he = he;
641 iter->curr++;
642 return err;
643}
644
645static int
646iter_finish_branch_entry(struct hist_entry_iter *iter,
647 struct addr_location *al __maybe_unused)
648{
649 zfree(&iter->priv);
650 iter->he = NULL;
651
652 return iter->curr >= iter->total ? 0 : -1;
653}
654
655static int
656iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
657 struct addr_location *al __maybe_unused)
658{
659 return 0;
660}
661
662static int
663iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
664{
665 struct perf_evsel *evsel = iter->evsel;
666 struct perf_sample *sample = iter->sample;
667 struct hist_entry *he;
668
669 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
670 sample->period, sample->weight,
671 sample->transaction, true);
672 if (he == NULL)
673 return -ENOMEM;
674
675 iter->he = he;
676 return 0;
677}
678
679static int
680iter_finish_normal_entry(struct hist_entry_iter *iter,
681 struct addr_location *al __maybe_unused)
682{
683 struct hist_entry *he = iter->he;
684 struct perf_evsel *evsel = iter->evsel;
685 struct perf_sample *sample = iter->sample;
686
687 if (he == NULL)
688 return 0;
689
690 iter->he = NULL;
691
692 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
693
694 return hist_entry__append_callchain(he, sample);
695}
696
697static int
698iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
699 struct addr_location *al __maybe_unused)
700{
701 struct hist_entry **he_cache;
702
703 callchain_cursor_commit(&callchain_cursor);
704
705 /*
706 * This is for detecting cycles or recursions so that they're
707 * cumulated only one time to prevent entries more than 100%
708 * overhead.
709 */
710 he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1));
711 if (he_cache == NULL)
712 return -ENOMEM;
713
714 iter->priv = he_cache;
715 iter->curr = 0;
716
717 return 0;
718}
719
720static int
721iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
722 struct addr_location *al)
723{
724 struct perf_evsel *evsel = iter->evsel;
725 struct hists *hists = evsel__hists(evsel);
726 struct perf_sample *sample = iter->sample;
727 struct hist_entry **he_cache = iter->priv;
728 struct hist_entry *he;
729 int err = 0;
730
731 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
732 sample->period, sample->weight,
733 sample->transaction, true);
734 if (he == NULL)
735 return -ENOMEM;
736
737 iter->he = he;
738 he_cache[iter->curr++] = he;
739
740 hist_entry__append_callchain(he, sample);
741
742 /*
743 * We need to re-initialize the cursor since callchain_append()
744 * advanced the cursor to the end.
745 */
746 callchain_cursor_commit(&callchain_cursor);
747
748 hists__inc_nr_samples(hists, he->filtered);
749
750 return err;
751}
752
753static int
754iter_next_cumulative_entry(struct hist_entry_iter *iter,
755 struct addr_location *al)
756{
757 struct callchain_cursor_node *node;
758
759 node = callchain_cursor_current(&callchain_cursor);
760 if (node == NULL)
761 return 0;
762
763 return fill_callchain_info(al, node, iter->hide_unresolved);
764}
765
766static int
767iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
768 struct addr_location *al)
769{
770 struct perf_evsel *evsel = iter->evsel;
771 struct perf_sample *sample = iter->sample;
772 struct hist_entry **he_cache = iter->priv;
773 struct hist_entry *he;
774 struct hist_entry he_tmp = {
775 .cpu = al->cpu,
776 .thread = al->thread,
777 .comm = thread__comm(al->thread),
778 .ip = al->addr,
779 .ms = {
780 .map = al->map,
781 .sym = al->sym,
782 },
783 .parent = iter->parent,
784 };
785 int i;
786 struct callchain_cursor cursor;
787
788 callchain_cursor_snapshot(&cursor, &callchain_cursor);
789
790 callchain_cursor_advance(&callchain_cursor);
791
792 /*
793 * Check if there's duplicate entries in the callchain.
794 * It's possible that it has cycles or recursive calls.
795 */
796 for (i = 0; i < iter->curr; i++) {
797 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
798 /* to avoid calling callback function */
799 iter->he = NULL;
800 return 0;
801 }
802 }
803
804 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
805 sample->period, sample->weight,
806 sample->transaction, false);
807 if (he == NULL)
808 return -ENOMEM;
809
810 iter->he = he;
811 he_cache[iter->curr++] = he;
812
813 if (symbol_conf.use_callchain)
814 callchain_append(he->callchain, &cursor, sample->period);
815 return 0;
816}
817
818static int
819iter_finish_cumulative_entry(struct hist_entry_iter *iter,
820 struct addr_location *al __maybe_unused)
821{
822 zfree(&iter->priv);
823 iter->he = NULL;
824
825 return 0;
826}
827
828const struct hist_iter_ops hist_iter_mem = {
829 .prepare_entry = iter_prepare_mem_entry,
830 .add_single_entry = iter_add_single_mem_entry,
831 .next_entry = iter_next_nop_entry,
832 .add_next_entry = iter_add_next_nop_entry,
833 .finish_entry = iter_finish_mem_entry,
834};
835
836const struct hist_iter_ops hist_iter_branch = {
837 .prepare_entry = iter_prepare_branch_entry,
838 .add_single_entry = iter_add_single_branch_entry,
839 .next_entry = iter_next_branch_entry,
840 .add_next_entry = iter_add_next_branch_entry,
841 .finish_entry = iter_finish_branch_entry,
842};
843
844const struct hist_iter_ops hist_iter_normal = {
845 .prepare_entry = iter_prepare_normal_entry,
846 .add_single_entry = iter_add_single_normal_entry,
847 .next_entry = iter_next_nop_entry,
848 .add_next_entry = iter_add_next_nop_entry,
849 .finish_entry = iter_finish_normal_entry,
850};
851
852const struct hist_iter_ops hist_iter_cumulative = {
853 .prepare_entry = iter_prepare_cumulative_entry,
854 .add_single_entry = iter_add_single_cumulative_entry,
855 .next_entry = iter_next_cumulative_entry,
856 .add_next_entry = iter_add_next_cumulative_entry,
857 .finish_entry = iter_finish_cumulative_entry,
858};
859
860int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
861 struct perf_evsel *evsel, struct perf_sample *sample,
862 int max_stack_depth, void *arg)
863{
864 int err, err2;
865
866 err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
867 max_stack_depth);
868 if (err)
869 return err;
870
871 iter->evsel = evsel;
872 iter->sample = sample;
873
874 err = iter->ops->prepare_entry(iter, al);
875 if (err)
876 goto out;
877
878 err = iter->ops->add_single_entry(iter, al);
879 if (err)
880 goto out;
881
882 if (iter->he && iter->add_entry_cb) {
883 err = iter->add_entry_cb(iter, al, true, arg);
884 if (err)
885 goto out;
886 }
887
888 while (iter->ops->next_entry(iter, al)) {
889 err = iter->ops->add_next_entry(iter, al);
890 if (err)
891 break;
892
893 if (iter->he && iter->add_entry_cb) {
894 err = iter->add_entry_cb(iter, al, false, arg);
895 if (err)
896 goto out;
897 }
898 }
899
900out:
901 err2 = iter->ops->finish_entry(iter, al);
902 if (!err)
903 err = err2;
904
905 return err;
906}
907
908int64_t
909hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
910{
911 struct perf_hpp_fmt *fmt;
912 int64_t cmp = 0;
913
914 perf_hpp__for_each_sort_list(fmt) {
915 if (perf_hpp__should_skip(fmt))
916 continue;
917
918 cmp = fmt->cmp(left, right);
919 if (cmp)
920 break;
921 }
922
923 return cmp;
924}
925
926int64_t
927hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
928{
929 struct perf_hpp_fmt *fmt;
930 int64_t cmp = 0;
931
932 perf_hpp__for_each_sort_list(fmt) {
933 if (perf_hpp__should_skip(fmt))
934 continue;
935
936 cmp = fmt->collapse(left, right);
937 if (cmp)
938 break;
939 }
940
941 return cmp;
942}
943
944void hist_entry__free(struct hist_entry *he)
945{
946 zfree(&he->branch_info);
947 zfree(&he->mem_info);
948 zfree(&he->stat_acc);
949 free_srcline(he->srcline);
950 free(he);
951}
952
953/*
954 * collapse the histogram
955 */
956
957static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
958 struct rb_root *root,
959 struct hist_entry *he)
960{
961 struct rb_node **p = &root->rb_node;
962 struct rb_node *parent = NULL;
963 struct hist_entry *iter;
964 int64_t cmp;
965
966 while (*p != NULL) {
967 parent = *p;
968 iter = rb_entry(parent, struct hist_entry, rb_node_in);
969
970 cmp = hist_entry__collapse(iter, he);
971
972 if (!cmp) {
973 he_stat__add_stat(&iter->stat, &he->stat);
974 if (symbol_conf.cumulate_callchain)
975 he_stat__add_stat(iter->stat_acc, he->stat_acc);
976
977 if (symbol_conf.use_callchain) {
978 callchain_cursor_reset(&callchain_cursor);
979 callchain_merge(&callchain_cursor,
980 iter->callchain,
981 he->callchain);
982 }
983 hist_entry__free(he);
984 return false;
985 }
986
987 if (cmp < 0)
988 p = &(*p)->rb_left;
989 else
990 p = &(*p)->rb_right;
991 }
992 hists->nr_entries++;
993
994 rb_link_node(&he->rb_node_in, parent, p);
995 rb_insert_color(&he->rb_node_in, root);
996 return true;
997}
998
999static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
1000{
1001 struct rb_root *root;
1002
1003 pthread_mutex_lock(&hists->lock);
1004
1005 root = hists->entries_in;
1006 if (++hists->entries_in > &hists->entries_in_array[1])
1007 hists->entries_in = &hists->entries_in_array[0];
1008
1009 pthread_mutex_unlock(&hists->lock);
1010
1011 return root;
1012}
1013
1014static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1015{
1016 hists__filter_entry_by_dso(hists, he);
1017 hists__filter_entry_by_thread(hists, he);
1018 hists__filter_entry_by_symbol(hists, he);
1019}
1020
1021void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1022{
1023 struct rb_root *root;
1024 struct rb_node *next;
1025 struct hist_entry *n;
1026
1027 if (!sort__need_collapse)
1028 return;
1029
1030 hists->nr_entries = 0;
1031
1032 root = hists__get_rotate_entries_in(hists);
1033
1034 next = rb_first(root);
1035
1036 while (next) {
1037 if (session_done())
1038 break;
1039 n = rb_entry(next, struct hist_entry, rb_node_in);
1040 next = rb_next(&n->rb_node_in);
1041
1042 rb_erase(&n->rb_node_in, root);
1043 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1044 /*
1045 * If it wasn't combined with one of the entries already
1046 * collapsed, we need to apply the filters that may have
1047 * been set by, say, the hist_browser.
1048 */
1049 hists__apply_filters(hists, n);
1050 }
1051 if (prog)
1052 ui_progress__update(prog, 1);
1053 }
1054}
1055
1056static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1057{
1058 struct perf_hpp_fmt *fmt;
1059 int64_t cmp = 0;
1060
1061 perf_hpp__for_each_sort_list(fmt) {
1062 if (perf_hpp__should_skip(fmt))
1063 continue;
1064
1065 cmp = fmt->sort(a, b);
1066 if (cmp)
1067 break;
1068 }
1069
1070 return cmp;
1071}
1072
1073static void hists__reset_filter_stats(struct hists *hists)
1074{
1075 hists->nr_non_filtered_entries = 0;
1076 hists->stats.total_non_filtered_period = 0;
1077}
1078
1079void hists__reset_stats(struct hists *hists)
1080{
1081 hists->nr_entries = 0;
1082 hists->stats.total_period = 0;
1083
1084 hists__reset_filter_stats(hists);
1085}
1086
1087static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1088{
1089 hists->nr_non_filtered_entries++;
1090 hists->stats.total_non_filtered_period += h->stat.period;
1091}
1092
1093void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1094{
1095 if (!h->filtered)
1096 hists__inc_filter_stats(hists, h);
1097
1098 hists->nr_entries++;
1099 hists->stats.total_period += h->stat.period;
1100}
1101
1102static void __hists__insert_output_entry(struct rb_root *entries,
1103 struct hist_entry *he,
1104 u64 min_callchain_hits)
1105{
1106 struct rb_node **p = &entries->rb_node;
1107 struct rb_node *parent = NULL;
1108 struct hist_entry *iter;
1109
1110 if (symbol_conf.use_callchain)
1111 callchain_param.sort(&he->sorted_chain, he->callchain,
1112 min_callchain_hits, &callchain_param);
1113
1114 while (*p != NULL) {
1115 parent = *p;
1116 iter = rb_entry(parent, struct hist_entry, rb_node);
1117
1118 if (hist_entry__sort(he, iter) > 0)
1119 p = &(*p)->rb_left;
1120 else
1121 p = &(*p)->rb_right;
1122 }
1123
1124 rb_link_node(&he->rb_node, parent, p);
1125 rb_insert_color(&he->rb_node, entries);
1126}
1127
1128void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1129{
1130 struct rb_root *root;
1131 struct rb_node *next;
1132 struct hist_entry *n;
1133 u64 min_callchain_hits;
1134
1135 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
1136
1137 if (sort__need_collapse)
1138 root = &hists->entries_collapsed;
1139 else
1140 root = hists->entries_in;
1141
1142 next = rb_first(root);
1143 hists->entries = RB_ROOT;
1144
1145 hists__reset_stats(hists);
1146 hists__reset_col_len(hists);
1147
1148 while (next) {
1149 n = rb_entry(next, struct hist_entry, rb_node_in);
1150 next = rb_next(&n->rb_node_in);
1151
1152 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
1153 hists__inc_stats(hists, n);
1154
1155 if (!n->filtered)
1156 hists__calc_col_len(hists, n);
1157
1158 if (prog)
1159 ui_progress__update(prog, 1);
1160 }
1161}
1162
1163static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1164 enum hist_filter filter)
1165{
1166 h->filtered &= ~(1 << filter);
1167 if (h->filtered)
1168 return;
1169
1170 /* force fold unfiltered entry for simplicity */
1171 h->ms.unfolded = false;
1172 h->row_offset = 0;
1173
1174 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1175
1176 hists__inc_filter_stats(hists, h);
1177 hists__calc_col_len(hists, h);
1178}
1179
1180
1181static bool hists__filter_entry_by_dso(struct hists *hists,
1182 struct hist_entry *he)
1183{
1184 if (hists->dso_filter != NULL &&
1185 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1186 he->filtered |= (1 << HIST_FILTER__DSO);
1187 return true;
1188 }
1189
1190 return false;
1191}
1192
1193void hists__filter_by_dso(struct hists *hists)
1194{
1195 struct rb_node *nd;
1196
1197 hists->stats.nr_non_filtered_samples = 0;
1198
1199 hists__reset_filter_stats(hists);
1200 hists__reset_col_len(hists);
1201
1202 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1203 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1204
1205 if (symbol_conf.exclude_other && !h->parent)
1206 continue;
1207
1208 if (hists__filter_entry_by_dso(hists, h))
1209 continue;
1210
1211 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1212 }
1213}
1214
1215static bool hists__filter_entry_by_thread(struct hists *hists,
1216 struct hist_entry *he)
1217{
1218 if (hists->thread_filter != NULL &&
1219 he->thread != hists->thread_filter) {
1220 he->filtered |= (1 << HIST_FILTER__THREAD);
1221 return true;
1222 }
1223
1224 return false;
1225}
1226
1227void hists__filter_by_thread(struct hists *hists)
1228{
1229 struct rb_node *nd;
1230
1231 hists->stats.nr_non_filtered_samples = 0;
1232
1233 hists__reset_filter_stats(hists);
1234 hists__reset_col_len(hists);
1235
1236 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1237 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1238
1239 if (hists__filter_entry_by_thread(hists, h))
1240 continue;
1241
1242 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1243 }
1244}
1245
1246static bool hists__filter_entry_by_symbol(struct hists *hists,
1247 struct hist_entry *he)
1248{
1249 if (hists->symbol_filter_str != NULL &&
1250 (!he->ms.sym || strstr(he->ms.sym->name,
1251 hists->symbol_filter_str) == NULL)) {
1252 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1253 return true;
1254 }
1255
1256 return false;
1257}
1258
1259void hists__filter_by_symbol(struct hists *hists)
1260{
1261 struct rb_node *nd;
1262
1263 hists->stats.nr_non_filtered_samples = 0;
1264
1265 hists__reset_filter_stats(hists);
1266 hists__reset_col_len(hists);
1267
1268 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1269 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1270
1271 if (hists__filter_entry_by_symbol(hists, h))
1272 continue;
1273
1274 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1275 }
1276}
1277
1278void events_stats__inc(struct events_stats *stats, u32 type)
1279{
1280 ++stats->nr_events[0];
1281 ++stats->nr_events[type];
1282}
1283
1284void hists__inc_nr_events(struct hists *hists, u32 type)
1285{
1286 events_stats__inc(&hists->stats, type);
1287}
1288
1289void hists__inc_nr_samples(struct hists *hists, bool filtered)
1290{
1291 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1292 if (!filtered)
1293 hists->stats.nr_non_filtered_samples++;
1294}
1295
1296static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1297 struct hist_entry *pair)
1298{
1299 struct rb_root *root;
1300 struct rb_node **p;
1301 struct rb_node *parent = NULL;
1302 struct hist_entry *he;
1303 int64_t cmp;
1304
1305 if (sort__need_collapse)
1306 root = &hists->entries_collapsed;
1307 else
1308 root = hists->entries_in;
1309
1310 p = &root->rb_node;
1311
1312 while (*p != NULL) {
1313 parent = *p;
1314 he = rb_entry(parent, struct hist_entry, rb_node_in);
1315
1316 cmp = hist_entry__collapse(he, pair);
1317
1318 if (!cmp)
1319 goto out;
1320
1321 if (cmp < 0)
1322 p = &(*p)->rb_left;
1323 else
1324 p = &(*p)->rb_right;
1325 }
1326
1327 he = hist_entry__new(pair, true);
1328 if (he) {
1329 memset(&he->stat, 0, sizeof(he->stat));
1330 he->hists = hists;
1331 rb_link_node(&he->rb_node_in, parent, p);
1332 rb_insert_color(&he->rb_node_in, root);
1333 hists__inc_stats(hists, he);
1334 he->dummy = true;
1335 }
1336out:
1337 return he;
1338}
1339
1340static struct hist_entry *hists__find_entry(struct hists *hists,
1341 struct hist_entry *he)
1342{
1343 struct rb_node *n;
1344
1345 if (sort__need_collapse)
1346 n = hists->entries_collapsed.rb_node;
1347 else
1348 n = hists->entries_in->rb_node;
1349
1350 while (n) {
1351 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1352 int64_t cmp = hist_entry__collapse(iter, he);
1353
1354 if (cmp < 0)
1355 n = n->rb_left;
1356 else if (cmp > 0)
1357 n = n->rb_right;
1358 else
1359 return iter;
1360 }
1361
1362 return NULL;
1363}
1364
1365/*
1366 * Look for pairs to link to the leader buckets (hist_entries):
1367 */
1368void hists__match(struct hists *leader, struct hists *other)
1369{
1370 struct rb_root *root;
1371 struct rb_node *nd;
1372 struct hist_entry *pos, *pair;
1373
1374 if (sort__need_collapse)
1375 root = &leader->entries_collapsed;
1376 else
1377 root = leader->entries_in;
1378
1379 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1380 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1381 pair = hists__find_entry(other, pos);
1382
1383 if (pair)
1384 hist_entry__add_pair(pair, pos);
1385 }
1386}
1387
1388/*
1389 * Look for entries in the other hists that are not present in the leader, if
1390 * we find them, just add a dummy entry on the leader hists, with period=0,
1391 * nr_events=0, to serve as the list header.
1392 */
1393int hists__link(struct hists *leader, struct hists *other)
1394{
1395 struct rb_root *root;
1396 struct rb_node *nd;
1397 struct hist_entry *pos, *pair;
1398
1399 if (sort__need_collapse)
1400 root = &other->entries_collapsed;
1401 else
1402 root = other->entries_in;
1403
1404 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1405 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1406
1407 if (!hist_entry__has_pairs(pos)) {
1408 pair = hists__add_dummy_entry(leader, pos);
1409 if (pair == NULL)
1410 return -1;
1411 hist_entry__add_pair(pos, pair);
1412 }
1413 }
1414
1415 return 0;
1416}
1417
1418
1419size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1420{
1421 struct perf_evsel *pos;
1422 size_t ret = 0;
1423
1424 evlist__for_each(evlist, pos) {
1425 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1426 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1427 }
1428
1429 return ret;
1430}
1431
1432
1433u64 hists__total_period(struct hists *hists)
1434{
1435 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1436 hists->stats.total_period;
1437}
1438
1439int parse_filter_percentage(const struct option *opt __maybe_unused,
1440 const char *arg, int unset __maybe_unused)
1441{
1442 if (!strcmp(arg, "relative"))
1443 symbol_conf.filter_relative = true;
1444 else if (!strcmp(arg, "absolute"))
1445 symbol_conf.filter_relative = false;
1446 else
1447 return -1;
1448
1449 return 0;
1450}
1451
1452int perf_hist_config(const char *var, const char *value)
1453{
1454 if (!strcmp(var, "hist.percentage"))
1455 return parse_filter_percentage(NULL, value, 0);
1456
1457 return 0;
1458}
1459
1460static int hists_evsel__init(struct perf_evsel *evsel)
1461{
1462 struct hists *hists = evsel__hists(evsel);
1463
1464 memset(hists, 0, sizeof(*hists));
1465 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1466 hists->entries_in = &hists->entries_in_array[0];
1467 hists->entries_collapsed = RB_ROOT;
1468 hists->entries = RB_ROOT;
1469 pthread_mutex_init(&hists->lock, NULL);
1470 return 0;
1471}
1472
1473/*
1474 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1475 * stored in the rbtree...
1476 */
1477
1478int hists__init(void)
1479{
1480 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1481 hists_evsel__init, NULL);
1482 if (err)
1483 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1484
1485 return err;
1486}