fix a kmap leak in virtio_console
[linux/fpc-iii.git] / tools / perf / util / hist.c
blobe4e6249b87d4eef1cd89d3df1aa3da022b550f84
1 #include "util.h"
2 #include "build-id.h"
3 #include "hist.h"
4 #include "session.h"
5 #include "sort.h"
6 #include "evsel.h"
7 #include <math.h>
9 static bool hists__filter_entry_by_dso(struct hists *hists,
10 struct hist_entry *he);
11 static bool hists__filter_entry_by_thread(struct hists *hists,
12 struct hist_entry *he);
13 static bool hists__filter_entry_by_symbol(struct hists *hists,
14 struct hist_entry *he);
16 enum hist_filter {
17 HIST_FILTER__DSO,
18 HIST_FILTER__THREAD,
19 HIST_FILTER__PARENT,
20 HIST_FILTER__SYMBOL,
23 struct callchain_param callchain_param = {
24 .mode = CHAIN_GRAPH_REL,
25 .min_percent = 0.5,
26 .order = ORDER_CALLEE,
27 .key = CCKEY_FUNCTION
30 u16 hists__col_len(struct hists *hists, enum hist_column col)
32 return hists->col_len[col];
35 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
37 hists->col_len[col] = len;
40 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
42 if (len > hists__col_len(hists, col)) {
43 hists__set_col_len(hists, col, len);
44 return true;
46 return false;
49 void hists__reset_col_len(struct hists *hists)
51 enum hist_column col;
53 for (col = 0; col < HISTC_NR_COLS; ++col)
54 hists__set_col_len(hists, col, 0);
57 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
59 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
61 if (hists__col_len(hists, dso) < unresolved_col_width &&
62 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
63 !symbol_conf.dso_list)
64 hists__set_col_len(hists, dso, unresolved_col_width);
67 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
69 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
70 int symlen;
71 u16 len;
74 * +4 accounts for '[x] ' priv level info
75 * +2 accounts for 0x prefix on raw addresses
76 * +3 accounts for ' y ' symtab origin info
78 if (h->ms.sym) {
79 symlen = h->ms.sym->namelen + 4;
80 if (verbose)
81 symlen += BITS_PER_LONG / 4 + 2 + 3;
82 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
83 } else {
84 symlen = unresolved_col_width + 4 + 2;
85 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
86 hists__set_unres_dso_col_len(hists, HISTC_DSO);
89 len = thread__comm_len(h->thread);
90 if (hists__new_col_len(hists, HISTC_COMM, len))
91 hists__set_col_len(hists, HISTC_THREAD, len + 6);
93 if (h->ms.map) {
94 len = dso__name_len(h->ms.map->dso);
95 hists__new_col_len(hists, HISTC_DSO, len);
98 if (h->parent)
99 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
101 if (h->branch_info) {
102 if (h->branch_info->from.sym) {
103 symlen = (int)h->branch_info->from.sym->namelen + 4;
104 if (verbose)
105 symlen += BITS_PER_LONG / 4 + 2 + 3;
106 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
108 symlen = dso__name_len(h->branch_info->from.map->dso);
109 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
110 } else {
111 symlen = unresolved_col_width + 4 + 2;
112 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
113 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
116 if (h->branch_info->to.sym) {
117 symlen = (int)h->branch_info->to.sym->namelen + 4;
118 if (verbose)
119 symlen += BITS_PER_LONG / 4 + 2 + 3;
120 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
122 symlen = dso__name_len(h->branch_info->to.map->dso);
123 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
124 } else {
125 symlen = unresolved_col_width + 4 + 2;
126 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
127 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
131 if (h->mem_info) {
132 if (h->mem_info->daddr.sym) {
133 symlen = (int)h->mem_info->daddr.sym->namelen + 4
134 + unresolved_col_width + 2;
135 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
136 symlen);
137 } else {
138 symlen = unresolved_col_width + 4 + 2;
139 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
140 symlen);
142 if (h->mem_info->daddr.map) {
143 symlen = dso__name_len(h->mem_info->daddr.map->dso);
144 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
145 symlen);
146 } else {
147 symlen = unresolved_col_width + 4 + 2;
148 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
150 } else {
151 symlen = unresolved_col_width + 4 + 2;
152 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
153 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
156 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
157 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
158 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
159 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
160 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
161 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
163 if (h->transaction)
164 hists__new_col_len(hists, HISTC_TRANSACTION,
165 hist_entry__transaction_len());
168 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
170 struct rb_node *next = rb_first(&hists->entries);
171 struct hist_entry *n;
172 int row = 0;
174 hists__reset_col_len(hists);
176 while (next && row++ < max_rows) {
177 n = rb_entry(next, struct hist_entry, rb_node);
178 if (!n->filtered)
179 hists__calc_col_len(hists, n);
180 next = rb_next(&n->rb_node);
184 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
185 unsigned int cpumode, u64 period)
187 switch (cpumode) {
188 case PERF_RECORD_MISC_KERNEL:
189 he_stat->period_sys += period;
190 break;
191 case PERF_RECORD_MISC_USER:
192 he_stat->period_us += period;
193 break;
194 case PERF_RECORD_MISC_GUEST_KERNEL:
195 he_stat->period_guest_sys += period;
196 break;
197 case PERF_RECORD_MISC_GUEST_USER:
198 he_stat->period_guest_us += period;
199 break;
200 default:
201 break;
205 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
206 u64 weight)
209 he_stat->period += period;
210 he_stat->weight += weight;
211 he_stat->nr_events += 1;
214 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
216 dest->period += src->period;
217 dest->period_sys += src->period_sys;
218 dest->period_us += src->period_us;
219 dest->period_guest_sys += src->period_guest_sys;
220 dest->period_guest_us += src->period_guest_us;
221 dest->nr_events += src->nr_events;
222 dest->weight += src->weight;
225 static void he_stat__decay(struct he_stat *he_stat)
227 he_stat->period = (he_stat->period * 7) / 8;
228 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
229 /* XXX need decay for weight too? */
232 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
234 u64 prev_period = he->stat.period;
236 if (prev_period == 0)
237 return true;
239 he_stat__decay(&he->stat);
241 if (!he->filtered)
242 hists->stats.total_period -= prev_period - he->stat.period;
244 return he->stat.period == 0;
247 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
249 struct rb_node *next = rb_first(&hists->entries);
250 struct hist_entry *n;
252 while (next) {
253 n = rb_entry(next, struct hist_entry, rb_node);
254 next = rb_next(&n->rb_node);
256 * We may be annotating this, for instance, so keep it here in
257 * case some it gets new samples, we'll eventually free it when
258 * the user stops browsing and it agains gets fully decayed.
260 if (((zap_user && n->level == '.') ||
261 (zap_kernel && n->level != '.') ||
262 hists__decay_entry(hists, n)) &&
263 !n->used) {
264 rb_erase(&n->rb_node, &hists->entries);
266 if (sort__need_collapse)
267 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
269 hist_entry__free(n);
270 --hists->nr_entries;
276 * histogram, sorted on item, collects periods
279 static struct hist_entry *hist_entry__new(struct hist_entry *template)
281 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
282 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
284 if (he != NULL) {
285 *he = *template;
287 if (he->ms.map)
288 he->ms.map->referenced = true;
290 if (he->branch_info) {
292 * This branch info is (a part of) allocated from
293 * machine__resolve_bstack() and will be freed after
294 * adding new entries. So we need to save a copy.
296 he->branch_info = malloc(sizeof(*he->branch_info));
297 if (he->branch_info == NULL) {
298 free(he);
299 return NULL;
302 memcpy(he->branch_info, template->branch_info,
303 sizeof(*he->branch_info));
305 if (he->branch_info->from.map)
306 he->branch_info->from.map->referenced = true;
307 if (he->branch_info->to.map)
308 he->branch_info->to.map->referenced = true;
311 if (he->mem_info) {
312 if (he->mem_info->iaddr.map)
313 he->mem_info->iaddr.map->referenced = true;
314 if (he->mem_info->daddr.map)
315 he->mem_info->daddr.map->referenced = true;
318 if (symbol_conf.use_callchain)
319 callchain_init(he->callchain);
321 INIT_LIST_HEAD(&he->pairs.node);
324 return he;
327 void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
329 if (!h->filtered) {
330 hists__calc_col_len(hists, h);
331 ++hists->nr_entries;
332 hists->stats.total_period += h->stat.period;
336 static u8 symbol__parent_filter(const struct symbol *parent)
338 if (symbol_conf.exclude_other && parent == NULL)
339 return 1 << HIST_FILTER__PARENT;
340 return 0;
343 static struct hist_entry *add_hist_entry(struct hists *hists,
344 struct hist_entry *entry,
345 struct addr_location *al)
347 struct rb_node **p;
348 struct rb_node *parent = NULL;
349 struct hist_entry *he;
350 int64_t cmp;
351 u64 period = entry->stat.period;
352 u64 weight = entry->stat.weight;
354 p = &hists->entries_in->rb_node;
356 while (*p != NULL) {
357 parent = *p;
358 he = rb_entry(parent, struct hist_entry, rb_node_in);
361 * Make sure that it receives arguments in a same order as
362 * hist_entry__collapse() so that we can use an appropriate
363 * function when searching an entry regardless which sort
364 * keys were used.
366 cmp = hist_entry__cmp(he, entry);
368 if (!cmp) {
369 he_stat__add_period(&he->stat, period, weight);
372 * This mem info was allocated from machine__resolve_mem
373 * and will not be used anymore.
375 zfree(&entry->mem_info);
377 /* If the map of an existing hist_entry has
378 * become out-of-date due to an exec() or
379 * similar, update it. Otherwise we will
380 * mis-adjust symbol addresses when computing
381 * the history counter to increment.
383 if (he->ms.map != entry->ms.map) {
384 he->ms.map = entry->ms.map;
385 if (he->ms.map)
386 he->ms.map->referenced = true;
388 goto out;
391 if (cmp < 0)
392 p = &(*p)->rb_left;
393 else
394 p = &(*p)->rb_right;
397 he = hist_entry__new(entry);
398 if (!he)
399 return NULL;
401 hists->nr_entries++;
402 rb_link_node(&he->rb_node_in, parent, p);
403 rb_insert_color(&he->rb_node_in, hists->entries_in);
404 out:
405 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
406 return he;
409 struct hist_entry *__hists__add_entry(struct hists *hists,
410 struct addr_location *al,
411 struct symbol *sym_parent,
412 struct branch_info *bi,
413 struct mem_info *mi,
414 u64 period, u64 weight, u64 transaction)
416 struct hist_entry entry = {
417 .thread = al->thread,
418 .comm = thread__comm(al->thread),
419 .ms = {
420 .map = al->map,
421 .sym = al->sym,
423 .cpu = al->cpu,
424 .ip = al->addr,
425 .level = al->level,
426 .stat = {
427 .nr_events = 1,
428 .period = period,
429 .weight = weight,
431 .parent = sym_parent,
432 .filtered = symbol__parent_filter(sym_parent),
433 .hists = hists,
434 .branch_info = bi,
435 .mem_info = mi,
436 .transaction = transaction,
439 return add_hist_entry(hists, &entry, al);
442 int64_t
443 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
445 struct sort_entry *se;
446 int64_t cmp = 0;
448 list_for_each_entry(se, &hist_entry__sort_list, list) {
449 cmp = se->se_cmp(left, right);
450 if (cmp)
451 break;
454 return cmp;
457 int64_t
458 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
460 struct sort_entry *se;
461 int64_t cmp = 0;
463 list_for_each_entry(se, &hist_entry__sort_list, list) {
464 int64_t (*f)(struct hist_entry *, struct hist_entry *);
466 f = se->se_collapse ?: se->se_cmp;
468 cmp = f(left, right);
469 if (cmp)
470 break;
473 return cmp;
476 void hist_entry__free(struct hist_entry *he)
478 zfree(&he->branch_info);
479 zfree(&he->mem_info);
480 free_srcline(he->srcline);
481 free(he);
485 * collapse the histogram
488 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
489 struct rb_root *root,
490 struct hist_entry *he)
492 struct rb_node **p = &root->rb_node;
493 struct rb_node *parent = NULL;
494 struct hist_entry *iter;
495 int64_t cmp;
497 while (*p != NULL) {
498 parent = *p;
499 iter = rb_entry(parent, struct hist_entry, rb_node_in);
501 cmp = hist_entry__collapse(iter, he);
503 if (!cmp) {
504 he_stat__add_stat(&iter->stat, &he->stat);
506 if (symbol_conf.use_callchain) {
507 callchain_cursor_reset(&callchain_cursor);
508 callchain_merge(&callchain_cursor,
509 iter->callchain,
510 he->callchain);
512 hist_entry__free(he);
513 return false;
516 if (cmp < 0)
517 p = &(*p)->rb_left;
518 else
519 p = &(*p)->rb_right;
522 rb_link_node(&he->rb_node_in, parent, p);
523 rb_insert_color(&he->rb_node_in, root);
524 return true;
527 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
529 struct rb_root *root;
531 pthread_mutex_lock(&hists->lock);
533 root = hists->entries_in;
534 if (++hists->entries_in > &hists->entries_in_array[1])
535 hists->entries_in = &hists->entries_in_array[0];
537 pthread_mutex_unlock(&hists->lock);
539 return root;
542 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
544 hists__filter_entry_by_dso(hists, he);
545 hists__filter_entry_by_thread(hists, he);
546 hists__filter_entry_by_symbol(hists, he);
549 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
551 struct rb_root *root;
552 struct rb_node *next;
553 struct hist_entry *n;
555 if (!sort__need_collapse)
556 return;
558 root = hists__get_rotate_entries_in(hists);
559 next = rb_first(root);
561 while (next) {
562 if (session_done())
563 break;
564 n = rb_entry(next, struct hist_entry, rb_node_in);
565 next = rb_next(&n->rb_node_in);
567 rb_erase(&n->rb_node_in, root);
568 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
570 * If it wasn't combined with one of the entries already
571 * collapsed, we need to apply the filters that may have
572 * been set by, say, the hist_browser.
574 hists__apply_filters(hists, n);
576 if (prog)
577 ui_progress__update(prog, 1);
582 * reverse the map, sort on period.
585 static int period_cmp(u64 period_a, u64 period_b)
587 if (period_a > period_b)
588 return 1;
589 if (period_a < period_b)
590 return -1;
591 return 0;
594 static int hist_entry__sort_on_period(struct hist_entry *a,
595 struct hist_entry *b)
597 int ret;
598 int i, nr_members;
599 struct perf_evsel *evsel;
600 struct hist_entry *pair;
601 u64 *periods_a, *periods_b;
603 ret = period_cmp(a->stat.period, b->stat.period);
604 if (ret || !symbol_conf.event_group)
605 return ret;
607 evsel = hists_to_evsel(a->hists);
608 nr_members = evsel->nr_members;
609 if (nr_members <= 1)
610 return ret;
612 periods_a = zalloc(sizeof(periods_a) * nr_members);
613 periods_b = zalloc(sizeof(periods_b) * nr_members);
615 if (!periods_a || !periods_b)
616 goto out;
618 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
619 evsel = hists_to_evsel(pair->hists);
620 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
623 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
624 evsel = hists_to_evsel(pair->hists);
625 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
628 for (i = 1; i < nr_members; i++) {
629 ret = period_cmp(periods_a[i], periods_b[i]);
630 if (ret)
631 break;
634 out:
635 free(periods_a);
636 free(periods_b);
638 return ret;
641 static void __hists__insert_output_entry(struct rb_root *entries,
642 struct hist_entry *he,
643 u64 min_callchain_hits)
645 struct rb_node **p = &entries->rb_node;
646 struct rb_node *parent = NULL;
647 struct hist_entry *iter;
649 if (symbol_conf.use_callchain)
650 callchain_param.sort(&he->sorted_chain, he->callchain,
651 min_callchain_hits, &callchain_param);
653 while (*p != NULL) {
654 parent = *p;
655 iter = rb_entry(parent, struct hist_entry, rb_node);
657 if (hist_entry__sort_on_period(he, iter) > 0)
658 p = &(*p)->rb_left;
659 else
660 p = &(*p)->rb_right;
663 rb_link_node(&he->rb_node, parent, p);
664 rb_insert_color(&he->rb_node, entries);
667 void hists__output_resort(struct hists *hists)
669 struct rb_root *root;
670 struct rb_node *next;
671 struct hist_entry *n;
672 u64 min_callchain_hits;
674 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
676 if (sort__need_collapse)
677 root = &hists->entries_collapsed;
678 else
679 root = hists->entries_in;
681 next = rb_first(root);
682 hists->entries = RB_ROOT;
684 hists->nr_entries = 0;
685 hists->stats.total_period = 0;
686 hists__reset_col_len(hists);
688 while (next) {
689 n = rb_entry(next, struct hist_entry, rb_node_in);
690 next = rb_next(&n->rb_node_in);
692 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
693 hists__inc_nr_entries(hists, n);
697 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
698 enum hist_filter filter)
700 h->filtered &= ~(1 << filter);
701 if (h->filtered)
702 return;
704 ++hists->nr_entries;
705 if (h->ms.unfolded)
706 hists->nr_entries += h->nr_rows;
707 h->row_offset = 0;
708 hists->stats.total_period += h->stat.period;
709 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
711 hists__calc_col_len(hists, h);
715 static bool hists__filter_entry_by_dso(struct hists *hists,
716 struct hist_entry *he)
718 if (hists->dso_filter != NULL &&
719 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
720 he->filtered |= (1 << HIST_FILTER__DSO);
721 return true;
724 return false;
727 void hists__filter_by_dso(struct hists *hists)
729 struct rb_node *nd;
731 hists->nr_entries = hists->stats.total_period = 0;
732 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
733 hists__reset_col_len(hists);
735 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
736 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
738 if (symbol_conf.exclude_other && !h->parent)
739 continue;
741 if (hists__filter_entry_by_dso(hists, h))
742 continue;
744 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
748 static bool hists__filter_entry_by_thread(struct hists *hists,
749 struct hist_entry *he)
751 if (hists->thread_filter != NULL &&
752 he->thread != hists->thread_filter) {
753 he->filtered |= (1 << HIST_FILTER__THREAD);
754 return true;
757 return false;
760 void hists__filter_by_thread(struct hists *hists)
762 struct rb_node *nd;
764 hists->nr_entries = hists->stats.total_period = 0;
765 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
766 hists__reset_col_len(hists);
768 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
769 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
771 if (hists__filter_entry_by_thread(hists, h))
772 continue;
774 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
778 static bool hists__filter_entry_by_symbol(struct hists *hists,
779 struct hist_entry *he)
781 if (hists->symbol_filter_str != NULL &&
782 (!he->ms.sym || strstr(he->ms.sym->name,
783 hists->symbol_filter_str) == NULL)) {
784 he->filtered |= (1 << HIST_FILTER__SYMBOL);
785 return true;
788 return false;
791 void hists__filter_by_symbol(struct hists *hists)
793 struct rb_node *nd;
795 hists->nr_entries = hists->stats.total_period = 0;
796 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
797 hists__reset_col_len(hists);
799 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
800 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
802 if (hists__filter_entry_by_symbol(hists, h))
803 continue;
805 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
809 void events_stats__inc(struct events_stats *stats, u32 type)
811 ++stats->nr_events[0];
812 ++stats->nr_events[type];
815 void hists__inc_nr_events(struct hists *hists, u32 type)
817 events_stats__inc(&hists->stats, type);
820 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
821 struct hist_entry *pair)
823 struct rb_root *root;
824 struct rb_node **p;
825 struct rb_node *parent = NULL;
826 struct hist_entry *he;
827 int64_t cmp;
829 if (sort__need_collapse)
830 root = &hists->entries_collapsed;
831 else
832 root = hists->entries_in;
834 p = &root->rb_node;
836 while (*p != NULL) {
837 parent = *p;
838 he = rb_entry(parent, struct hist_entry, rb_node_in);
840 cmp = hist_entry__collapse(he, pair);
842 if (!cmp)
843 goto out;
845 if (cmp < 0)
846 p = &(*p)->rb_left;
847 else
848 p = &(*p)->rb_right;
851 he = hist_entry__new(pair);
852 if (he) {
853 memset(&he->stat, 0, sizeof(he->stat));
854 he->hists = hists;
855 rb_link_node(&he->rb_node_in, parent, p);
856 rb_insert_color(&he->rb_node_in, root);
857 hists__inc_nr_entries(hists, he);
858 he->dummy = true;
860 out:
861 return he;
864 static struct hist_entry *hists__find_entry(struct hists *hists,
865 struct hist_entry *he)
867 struct rb_node *n;
869 if (sort__need_collapse)
870 n = hists->entries_collapsed.rb_node;
871 else
872 n = hists->entries_in->rb_node;
874 while (n) {
875 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
876 int64_t cmp = hist_entry__collapse(iter, he);
878 if (cmp < 0)
879 n = n->rb_left;
880 else if (cmp > 0)
881 n = n->rb_right;
882 else
883 return iter;
886 return NULL;
890 * Look for pairs to link to the leader buckets (hist_entries):
892 void hists__match(struct hists *leader, struct hists *other)
894 struct rb_root *root;
895 struct rb_node *nd;
896 struct hist_entry *pos, *pair;
898 if (sort__need_collapse)
899 root = &leader->entries_collapsed;
900 else
901 root = leader->entries_in;
903 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
904 pos = rb_entry(nd, struct hist_entry, rb_node_in);
905 pair = hists__find_entry(other, pos);
907 if (pair)
908 hist_entry__add_pair(pair, pos);
913 * Look for entries in the other hists that are not present in the leader, if
914 * we find them, just add a dummy entry on the leader hists, with period=0,
915 * nr_events=0, to serve as the list header.
917 int hists__link(struct hists *leader, struct hists *other)
919 struct rb_root *root;
920 struct rb_node *nd;
921 struct hist_entry *pos, *pair;
923 if (sort__need_collapse)
924 root = &other->entries_collapsed;
925 else
926 root = other->entries_in;
928 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
929 pos = rb_entry(nd, struct hist_entry, rb_node_in);
931 if (!hist_entry__has_pairs(pos)) {
932 pair = hists__add_dummy_entry(leader, pos);
933 if (pair == NULL)
934 return -1;
935 hist_entry__add_pair(pos, pair);
939 return 0;