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