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