OSDN Git Service

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