]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - tools/perf/util/hist.c
perf tools: Mark branch_info maps as referenced
[karo-tx-linux.git] / tools / perf / util / hist.c
1 #include "annotate.h"
2 #include "util.h"
3 #include "build-id.h"
4 #include "hist.h"
5 #include "session.h"
6 #include "sort.h"
7 #include <math.h>
8
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);
15
16 enum hist_filter {
17         HIST_FILTER__DSO,
18         HIST_FILTER__THREAD,
19         HIST_FILTER__PARENT,
20         HIST_FILTER__SYMBOL,
21 };
22
23 struct callchain_param  callchain_param = {
24         .mode   = CHAIN_GRAPH_REL,
25         .min_percent = 0.5,
26         .order  = ORDER_CALLEE
27 };
28
29 u16 hists__col_len(struct hists *hists, enum hist_column col)
30 {
31         return hists->col_len[col];
32 }
33
34 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
35 {
36         hists->col_len[col] = len;
37 }
38
39 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
40 {
41         if (len > hists__col_len(hists, col)) {
42                 hists__set_col_len(hists, col, len);
43                 return true;
44         }
45         return false;
46 }
47
48 void hists__reset_col_len(struct hists *hists)
49 {
50         enum hist_column col;
51
52         for (col = 0; col < HISTC_NR_COLS; ++col)
53                 hists__set_col_len(hists, col, 0);
54 }
55
56 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
57 {
58         const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
59
60         if (hists__col_len(hists, dso) < unresolved_col_width &&
61             !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
62             !symbol_conf.dso_list)
63                 hists__set_col_len(hists, dso, unresolved_col_width);
64 }
65
66 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
67 {
68         const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
69         u16 len;
70
71         if (h->ms.sym)
72                 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
73         else
74                 hists__set_unres_dso_col_len(hists, HISTC_DSO);
75
76         len = thread__comm_len(h->thread);
77         if (hists__new_col_len(hists, HISTC_COMM, len))
78                 hists__set_col_len(hists, HISTC_THREAD, len + 6);
79
80         if (h->ms.map) {
81                 len = dso__name_len(h->ms.map->dso);
82                 hists__new_col_len(hists, HISTC_DSO, len);
83         }
84
85         if (h->parent)
86                 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
87
88         if (h->branch_info) {
89                 int symlen;
90                 /*
91                  * +4 accounts for '[x] ' priv level info
92                  * +2 account of 0x prefix on raw addresses
93                  */
94                 if (h->branch_info->from.sym) {
95                         symlen = (int)h->branch_info->from.sym->namelen + 4;
96                         hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
97
98                         symlen = dso__name_len(h->branch_info->from.map->dso);
99                         hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
100                 } else {
101                         symlen = unresolved_col_width + 4 + 2;
102                         hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
103                         hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
104                 }
105
106                 if (h->branch_info->to.sym) {
107                         symlen = (int)h->branch_info->to.sym->namelen + 4;
108                         hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
109
110                         symlen = dso__name_len(h->branch_info->to.map->dso);
111                         hists__new_col_len(hists, HISTC_DSO_TO, symlen);
112                 } else {
113                         symlen = unresolved_col_width + 4 + 2;
114                         hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
115                         hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
116                 }
117         }
118 }
119
120 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
121 {
122         struct rb_node *next = rb_first(&hists->entries);
123         struct hist_entry *n;
124         int row = 0;
125
126         hists__reset_col_len(hists);
127
128         while (next && row++ < max_rows) {
129                 n = rb_entry(next, struct hist_entry, rb_node);
130                 if (!n->filtered)
131                         hists__calc_col_len(hists, n);
132                 next = rb_next(&n->rb_node);
133         }
134 }
135
136 static void hist_entry__add_cpumode_period(struct hist_entry *he,
137                                            unsigned int cpumode, u64 period)
138 {
139         switch (cpumode) {
140         case PERF_RECORD_MISC_KERNEL:
141                 he->stat.period_sys += period;
142                 break;
143         case PERF_RECORD_MISC_USER:
144                 he->stat.period_us += period;
145                 break;
146         case PERF_RECORD_MISC_GUEST_KERNEL:
147                 he->stat.period_guest_sys += period;
148                 break;
149         case PERF_RECORD_MISC_GUEST_USER:
150                 he->stat.period_guest_us += period;
151                 break;
152         default:
153                 break;
154         }
155 }
156
157 static void he_stat__add_period(struct he_stat *he_stat, u64 period)
158 {
159         he_stat->period         += period;
160         he_stat->nr_events      += 1;
161 }
162
163 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
164 {
165         dest->period            += src->period;
166         dest->period_sys        += src->period_sys;
167         dest->period_us         += src->period_us;
168         dest->period_guest_sys  += src->period_guest_sys;
169         dest->period_guest_us   += src->period_guest_us;
170         dest->nr_events         += src->nr_events;
171 }
172
173 static void hist_entry__decay(struct hist_entry *he)
174 {
175         he->stat.period = (he->stat.period * 7) / 8;
176         he->stat.nr_events = (he->stat.nr_events * 7) / 8;
177 }
178
179 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
180 {
181         u64 prev_period = he->stat.period;
182
183         if (prev_period == 0)
184                 return true;
185
186         hist_entry__decay(he);
187
188         if (!he->filtered)
189                 hists->stats.total_period -= prev_period - he->stat.period;
190
191         return he->stat.period == 0;
192 }
193
194 static void __hists__decay_entries(struct hists *hists, bool zap_user,
195                                    bool zap_kernel, bool threaded)
196 {
197         struct rb_node *next = rb_first(&hists->entries);
198         struct hist_entry *n;
199
200         while (next) {
201                 n = rb_entry(next, struct hist_entry, rb_node);
202                 next = rb_next(&n->rb_node);
203                 /*
204                  * We may be annotating this, for instance, so keep it here in
205                  * case some it gets new samples, we'll eventually free it when
206                  * the user stops browsing and it agains gets fully decayed.
207                  */
208                 if (((zap_user && n->level == '.') ||
209                      (zap_kernel && n->level != '.') ||
210                      hists__decay_entry(hists, n)) &&
211                     !n->used) {
212                         rb_erase(&n->rb_node, &hists->entries);
213
214                         if (sort__need_collapse || threaded)
215                                 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
216
217                         hist_entry__free(n);
218                         --hists->nr_entries;
219                 }
220         }
221 }
222
223 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
224 {
225         return __hists__decay_entries(hists, zap_user, zap_kernel, false);
226 }
227
228 void hists__decay_entries_threaded(struct hists *hists,
229                                    bool zap_user, bool zap_kernel)
230 {
231         return __hists__decay_entries(hists, zap_user, zap_kernel, true);
232 }
233
234 /*
235  * histogram, sorted on item, collects periods
236  */
237
238 static struct hist_entry *hist_entry__new(struct hist_entry *template)
239 {
240         size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
241         struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
242
243         if (he != NULL) {
244                 *he = *template;
245
246                 if (he->ms.map)
247                         he->ms.map->referenced = true;
248
249                 if (he->branch_info) {
250                         if (he->branch_info->from.map)
251                                 he->branch_info->from.map->referenced = true;
252                         if (he->branch_info->to.map)
253                                 he->branch_info->to.map->referenced = true;
254                 }
255
256                 if (symbol_conf.use_callchain)
257                         callchain_init(he->callchain);
258
259                 INIT_LIST_HEAD(&he->pairs.node);
260         }
261
262         return he;
263 }
264
265 void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
266 {
267         if (!h->filtered) {
268                 hists__calc_col_len(hists, h);
269                 ++hists->nr_entries;
270                 hists->stats.total_period += h->stat.period;
271         }
272 }
273
274 static u8 symbol__parent_filter(const struct symbol *parent)
275 {
276         if (symbol_conf.exclude_other && parent == NULL)
277                 return 1 << HIST_FILTER__PARENT;
278         return 0;
279 }
280
281 static struct hist_entry *add_hist_entry(struct hists *hists,
282                                       struct hist_entry *entry,
283                                       struct addr_location *al,
284                                       u64 period)
285 {
286         struct rb_node **p;
287         struct rb_node *parent = NULL;
288         struct hist_entry *he;
289         int cmp;
290
291         pthread_mutex_lock(&hists->lock);
292
293         p = &hists->entries_in->rb_node;
294
295         while (*p != NULL) {
296                 parent = *p;
297                 he = rb_entry(parent, struct hist_entry, rb_node_in);
298
299                 /*
300                  * Make sure that it receives arguments in a same order as
301                  * hist_entry__collapse() so that we can use an appropriate
302                  * function when searching an entry regardless which sort
303                  * keys were used.
304                  */
305                 cmp = hist_entry__cmp(he, entry);
306
307                 if (!cmp) {
308                         he_stat__add_period(&he->stat, period);
309
310                         /* If the map of an existing hist_entry has
311                          * become out-of-date due to an exec() or
312                          * similar, update it.  Otherwise we will
313                          * mis-adjust symbol addresses when computing
314                          * the history counter to increment.
315                          */
316                         if (he->ms.map != entry->ms.map) {
317                                 he->ms.map = entry->ms.map;
318                                 if (he->ms.map)
319                                         he->ms.map->referenced = true;
320                         }
321                         goto out;
322                 }
323
324                 if (cmp < 0)
325                         p = &(*p)->rb_left;
326                 else
327                         p = &(*p)->rb_right;
328         }
329
330         he = hist_entry__new(entry);
331         if (!he)
332                 goto out_unlock;
333
334         rb_link_node(&he->rb_node_in, parent, p);
335         rb_insert_color(&he->rb_node_in, hists->entries_in);
336 out:
337         hist_entry__add_cpumode_period(he, al->cpumode, period);
338 out_unlock:
339         pthread_mutex_unlock(&hists->lock);
340         return he;
341 }
342
343 struct hist_entry *__hists__add_branch_entry(struct hists *self,
344                                              struct addr_location *al,
345                                              struct symbol *sym_parent,
346                                              struct branch_info *bi,
347                                              u64 period)
348 {
349         struct hist_entry entry = {
350                 .thread = al->thread,
351                 .ms = {
352                         .map    = bi->to.map,
353                         .sym    = bi->to.sym,
354                 },
355                 .cpu    = al->cpu,
356                 .ip     = bi->to.addr,
357                 .level  = al->level,
358                 .stat = {
359                         .period = period,
360                         .nr_events = 1,
361                 },
362                 .parent = sym_parent,
363                 .filtered = symbol__parent_filter(sym_parent),
364                 .branch_info = bi,
365                 .hists  = self,
366         };
367
368         return add_hist_entry(self, &entry, al, period);
369 }
370
371 struct hist_entry *__hists__add_entry(struct hists *self,
372                                       struct addr_location *al,
373                                       struct symbol *sym_parent, u64 period)
374 {
375         struct hist_entry entry = {
376                 .thread = al->thread,
377                 .ms = {
378                         .map    = al->map,
379                         .sym    = al->sym,
380                 },
381                 .cpu    = al->cpu,
382                 .ip     = al->addr,
383                 .level  = al->level,
384                 .stat = {
385                         .period = period,
386                         .nr_events = 1,
387                 },
388                 .parent = sym_parent,
389                 .filtered = symbol__parent_filter(sym_parent),
390                 .hists  = self,
391         };
392
393         return add_hist_entry(self, &entry, al, period);
394 }
395
396 int64_t
397 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
398 {
399         struct sort_entry *se;
400         int64_t cmp = 0;
401
402         list_for_each_entry(se, &hist_entry__sort_list, list) {
403                 cmp = se->se_cmp(left, right);
404                 if (cmp)
405                         break;
406         }
407
408         return cmp;
409 }
410
411 int64_t
412 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
413 {
414         struct sort_entry *se;
415         int64_t cmp = 0;
416
417         list_for_each_entry(se, &hist_entry__sort_list, list) {
418                 int64_t (*f)(struct hist_entry *, struct hist_entry *);
419
420                 f = se->se_collapse ?: se->se_cmp;
421
422                 cmp = f(left, right);
423                 if (cmp)
424                         break;
425         }
426
427         return cmp;
428 }
429
430 void hist_entry__free(struct hist_entry *he)
431 {
432         free(he->branch_info);
433         free(he);
434 }
435
436 /*
437  * collapse the histogram
438  */
439
440 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
441                                          struct rb_root *root,
442                                          struct hist_entry *he)
443 {
444         struct rb_node **p = &root->rb_node;
445         struct rb_node *parent = NULL;
446         struct hist_entry *iter;
447         int64_t cmp;
448
449         while (*p != NULL) {
450                 parent = *p;
451                 iter = rb_entry(parent, struct hist_entry, rb_node_in);
452
453                 cmp = hist_entry__collapse(iter, he);
454
455                 if (!cmp) {
456                         he_stat__add_stat(&iter->stat, &he->stat);
457
458                         if (symbol_conf.use_callchain) {
459                                 callchain_cursor_reset(&callchain_cursor);
460                                 callchain_merge(&callchain_cursor,
461                                                 iter->callchain,
462                                                 he->callchain);
463                         }
464                         hist_entry__free(he);
465                         return false;
466                 }
467
468                 if (cmp < 0)
469                         p = &(*p)->rb_left;
470                 else
471                         p = &(*p)->rb_right;
472         }
473
474         rb_link_node(&he->rb_node_in, parent, p);
475         rb_insert_color(&he->rb_node_in, root);
476         return true;
477 }
478
479 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
480 {
481         struct rb_root *root;
482
483         pthread_mutex_lock(&hists->lock);
484
485         root = hists->entries_in;
486         if (++hists->entries_in > &hists->entries_in_array[1])
487                 hists->entries_in = &hists->entries_in_array[0];
488
489         pthread_mutex_unlock(&hists->lock);
490
491         return root;
492 }
493
494 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
495 {
496         hists__filter_entry_by_dso(hists, he);
497         hists__filter_entry_by_thread(hists, he);
498         hists__filter_entry_by_symbol(hists, he);
499 }
500
501 static void __hists__collapse_resort(struct hists *hists, bool threaded)
502 {
503         struct rb_root *root;
504         struct rb_node *next;
505         struct hist_entry *n;
506
507         if (!sort__need_collapse && !threaded)
508                 return;
509
510         root = hists__get_rotate_entries_in(hists);
511         next = rb_first(root);
512
513         while (next) {
514                 n = rb_entry(next, struct hist_entry, rb_node_in);
515                 next = rb_next(&n->rb_node_in);
516
517                 rb_erase(&n->rb_node_in, root);
518                 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
519                         /*
520                          * If it wasn't combined with one of the entries already
521                          * collapsed, we need to apply the filters that may have
522                          * been set by, say, the hist_browser.
523                          */
524                         hists__apply_filters(hists, n);
525                 }
526         }
527 }
528
529 void hists__collapse_resort(struct hists *hists)
530 {
531         return __hists__collapse_resort(hists, false);
532 }
533
534 void hists__collapse_resort_threaded(struct hists *hists)
535 {
536         return __hists__collapse_resort(hists, true);
537 }
538
539 /*
540  * reverse the map, sort on period.
541  */
542
543 static void __hists__insert_output_entry(struct rb_root *entries,
544                                          struct hist_entry *he,
545                                          u64 min_callchain_hits)
546 {
547         struct rb_node **p = &entries->rb_node;
548         struct rb_node *parent = NULL;
549         struct hist_entry *iter;
550
551         if (symbol_conf.use_callchain)
552                 callchain_param.sort(&he->sorted_chain, he->callchain,
553                                       min_callchain_hits, &callchain_param);
554
555         while (*p != NULL) {
556                 parent = *p;
557                 iter = rb_entry(parent, struct hist_entry, rb_node);
558
559                 if (he->stat.period > iter->stat.period)
560                         p = &(*p)->rb_left;
561                 else
562                         p = &(*p)->rb_right;
563         }
564
565         rb_link_node(&he->rb_node, parent, p);
566         rb_insert_color(&he->rb_node, entries);
567 }
568
569 static void __hists__output_resort(struct hists *hists, bool threaded)
570 {
571         struct rb_root *root;
572         struct rb_node *next;
573         struct hist_entry *n;
574         u64 min_callchain_hits;
575
576         min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
577
578         if (sort__need_collapse || threaded)
579                 root = &hists->entries_collapsed;
580         else
581                 root = hists->entries_in;
582
583         next = rb_first(root);
584         hists->entries = RB_ROOT;
585
586         hists->nr_entries = 0;
587         hists->stats.total_period = 0;
588         hists__reset_col_len(hists);
589
590         while (next) {
591                 n = rb_entry(next, struct hist_entry, rb_node_in);
592                 next = rb_next(&n->rb_node_in);
593
594                 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
595                 hists__inc_nr_entries(hists, n);
596         }
597 }
598
599 void hists__output_resort(struct hists *hists)
600 {
601         return __hists__output_resort(hists, false);
602 }
603
604 void hists__output_resort_threaded(struct hists *hists)
605 {
606         return __hists__output_resort(hists, true);
607 }
608
609 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
610                                        enum hist_filter filter)
611 {
612         h->filtered &= ~(1 << filter);
613         if (h->filtered)
614                 return;
615
616         ++hists->nr_entries;
617         if (h->ms.unfolded)
618                 hists->nr_entries += h->nr_rows;
619         h->row_offset = 0;
620         hists->stats.total_period += h->stat.period;
621         hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
622
623         hists__calc_col_len(hists, h);
624 }
625
626
627 static bool hists__filter_entry_by_dso(struct hists *hists,
628                                        struct hist_entry *he)
629 {
630         if (hists->dso_filter != NULL &&
631             (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
632                 he->filtered |= (1 << HIST_FILTER__DSO);
633                 return true;
634         }
635
636         return false;
637 }
638
639 void hists__filter_by_dso(struct hists *hists)
640 {
641         struct rb_node *nd;
642
643         hists->nr_entries = hists->stats.total_period = 0;
644         hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
645         hists__reset_col_len(hists);
646
647         for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
648                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
649
650                 if (symbol_conf.exclude_other && !h->parent)
651                         continue;
652
653                 if (hists__filter_entry_by_dso(hists, h))
654                         continue;
655
656                 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
657         }
658 }
659
660 static bool hists__filter_entry_by_thread(struct hists *hists,
661                                           struct hist_entry *he)
662 {
663         if (hists->thread_filter != NULL &&
664             he->thread != hists->thread_filter) {
665                 he->filtered |= (1 << HIST_FILTER__THREAD);
666                 return true;
667         }
668
669         return false;
670 }
671
672 void hists__filter_by_thread(struct hists *hists)
673 {
674         struct rb_node *nd;
675
676         hists->nr_entries = hists->stats.total_period = 0;
677         hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
678         hists__reset_col_len(hists);
679
680         for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
681                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
682
683                 if (hists__filter_entry_by_thread(hists, h))
684                         continue;
685
686                 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
687         }
688 }
689
690 static bool hists__filter_entry_by_symbol(struct hists *hists,
691                                           struct hist_entry *he)
692 {
693         if (hists->symbol_filter_str != NULL &&
694             (!he->ms.sym || strstr(he->ms.sym->name,
695                                    hists->symbol_filter_str) == NULL)) {
696                 he->filtered |= (1 << HIST_FILTER__SYMBOL);
697                 return true;
698         }
699
700         return false;
701 }
702
703 void hists__filter_by_symbol(struct hists *hists)
704 {
705         struct rb_node *nd;
706
707         hists->nr_entries = hists->stats.total_period = 0;
708         hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
709         hists__reset_col_len(hists);
710
711         for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
712                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
713
714                 if (hists__filter_entry_by_symbol(hists, h))
715                         continue;
716
717                 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
718         }
719 }
720
721 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
722 {
723         return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
724 }
725
726 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
727 {
728         return symbol__annotate(he->ms.sym, he->ms.map, privsize);
729 }
730
731 void events_stats__inc(struct events_stats *stats, u32 type)
732 {
733         ++stats->nr_events[0];
734         ++stats->nr_events[type];
735 }
736
737 void hists__inc_nr_events(struct hists *hists, u32 type)
738 {
739         events_stats__inc(&hists->stats, type);
740 }
741
742 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
743                                                  struct hist_entry *pair)
744 {
745         struct rb_root *root;
746         struct rb_node **p;
747         struct rb_node *parent = NULL;
748         struct hist_entry *he;
749         int cmp;
750
751         if (sort__need_collapse)
752                 root = &hists->entries_collapsed;
753         else
754                 root = hists->entries_in;
755
756         p = &root->rb_node;
757
758         while (*p != NULL) {
759                 parent = *p;
760                 he = rb_entry(parent, struct hist_entry, rb_node_in);
761
762                 cmp = hist_entry__collapse(he, pair);
763
764                 if (!cmp)
765                         goto out;
766
767                 if (cmp < 0)
768                         p = &(*p)->rb_left;
769                 else
770                         p = &(*p)->rb_right;
771         }
772
773         he = hist_entry__new(pair);
774         if (he) {
775                 memset(&he->stat, 0, sizeof(he->stat));
776                 he->hists = hists;
777                 rb_link_node(&he->rb_node_in, parent, p);
778                 rb_insert_color(&he->rb_node_in, root);
779                 hists__inc_nr_entries(hists, he);
780         }
781 out:
782         return he;
783 }
784
785 static struct hist_entry *hists__find_entry(struct hists *hists,
786                                             struct hist_entry *he)
787 {
788         struct rb_node *n;
789
790         if (sort__need_collapse)
791                 n = hists->entries_collapsed.rb_node;
792         else
793                 n = hists->entries_in->rb_node;
794
795         while (n) {
796                 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
797                 int64_t cmp = hist_entry__collapse(iter, he);
798
799                 if (cmp < 0)
800                         n = n->rb_left;
801                 else if (cmp > 0)
802                         n = n->rb_right;
803                 else
804                         return iter;
805         }
806
807         return NULL;
808 }
809
810 /*
811  * Look for pairs to link to the leader buckets (hist_entries):
812  */
813 void hists__match(struct hists *leader, struct hists *other)
814 {
815         struct rb_root *root;
816         struct rb_node *nd;
817         struct hist_entry *pos, *pair;
818
819         if (sort__need_collapse)
820                 root = &leader->entries_collapsed;
821         else
822                 root = leader->entries_in;
823
824         for (nd = rb_first(root); nd; nd = rb_next(nd)) {
825                 pos  = rb_entry(nd, struct hist_entry, rb_node_in);
826                 pair = hists__find_entry(other, pos);
827
828                 if (pair)
829                         hist_entry__add_pair(pair, pos);
830         }
831 }
832
833 /*
834  * Look for entries in the other hists that are not present in the leader, if
835  * we find them, just add a dummy entry on the leader hists, with period=0,
836  * nr_events=0, to serve as the list header.
837  */
838 int hists__link(struct hists *leader, struct hists *other)
839 {
840         struct rb_root *root;
841         struct rb_node *nd;
842         struct hist_entry *pos, *pair;
843
844         if (sort__need_collapse)
845                 root = &other->entries_collapsed;
846         else
847                 root = other->entries_in;
848
849         for (nd = rb_first(root); nd; nd = rb_next(nd)) {
850                 pos = rb_entry(nd, struct hist_entry, rb_node_in);
851
852                 if (!hist_entry__has_pairs(pos)) {
853                         pair = hists__add_dummy_entry(leader, pos);
854                         if (pair == NULL)
855                                 return -1;
856                         hist_entry__add_pair(pos, pair);
857                 }
858         }
859
860         return 0;
861 }