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