]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/perf/util/hist.c
ASoC: wm0010.c: add static to local variable
[mirror_ubuntu-bionic-kernel.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,
521 struct addr_location *al __maybe_unused)
522 {
523 struct perf_evsel *evsel = iter->evsel;
524 struct hist_entry *he = iter->he;
525 int err = -EINVAL;
526
527 if (he == NULL)
528 goto out;
529
530 hists__inc_nr_samples(&evsel->hists, he->filtered);
531
532 err = hist_entry__append_callchain(he, iter->sample);
533
534 out:
535 /*
536 * We don't need to free iter->priv (mem_info) here since
537 * the mem info was either already freed in add_hist_entry() or
538 * passed to a new hist entry by hist_entry__new().
539 */
540 iter->priv = NULL;
541
542 iter->he = NULL;
543 return err;
544 }
545
546 static int
547 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
548 {
549 struct branch_info *bi;
550 struct perf_sample *sample = iter->sample;
551
552 bi = sample__resolve_bstack(sample, al);
553 if (!bi)
554 return -ENOMEM;
555
556 iter->curr = 0;
557 iter->total = sample->branch_stack->nr;
558
559 iter->priv = bi;
560 return 0;
561 }
562
563 static int
564 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
565 struct addr_location *al __maybe_unused)
566 {
567 /* to avoid calling callback function */
568 iter->he = NULL;
569
570 return 0;
571 }
572
573 static int
574 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
575 {
576 struct branch_info *bi = iter->priv;
577 int i = iter->curr;
578
579 if (bi == NULL)
580 return 0;
581
582 if (iter->curr >= iter->total)
583 return 0;
584
585 al->map = bi[i].to.map;
586 al->sym = bi[i].to.sym;
587 al->addr = bi[i].to.addr;
588 return 1;
589 }
590
591 static int
592 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
593 {
594 struct branch_info *bi;
595 struct perf_evsel *evsel = iter->evsel;
596 struct hist_entry *he = NULL;
597 int i = iter->curr;
598 int err = 0;
599
600 bi = iter->priv;
601
602 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
603 goto out;
604
605 /*
606 * The report shows the percentage of total branches captured
607 * and not events sampled. Thus we use a pseudo period of 1.
608 */
609 he = __hists__add_entry(&evsel->hists, al, iter->parent, &bi[i], NULL,
610 1, 1, 0, true);
611 if (he == NULL)
612 return -ENOMEM;
613
614 hists__inc_nr_samples(&evsel->hists, he->filtered);
615
616 out:
617 iter->he = he;
618 iter->curr++;
619 return err;
620 }
621
622 static int
623 iter_finish_branch_entry(struct hist_entry_iter *iter,
624 struct addr_location *al __maybe_unused)
625 {
626 zfree(&iter->priv);
627 iter->he = NULL;
628
629 return iter->curr >= iter->total ? 0 : -1;
630 }
631
632 static int
633 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
634 struct addr_location *al __maybe_unused)
635 {
636 return 0;
637 }
638
639 static int
640 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
641 {
642 struct perf_evsel *evsel = iter->evsel;
643 struct perf_sample *sample = iter->sample;
644 struct hist_entry *he;
645
646 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
647 sample->period, sample->weight,
648 sample->transaction, true);
649 if (he == NULL)
650 return -ENOMEM;
651
652 iter->he = he;
653 return 0;
654 }
655
656 static int
657 iter_finish_normal_entry(struct hist_entry_iter *iter,
658 struct addr_location *al __maybe_unused)
659 {
660 struct hist_entry *he = iter->he;
661 struct perf_evsel *evsel = iter->evsel;
662 struct perf_sample *sample = iter->sample;
663
664 if (he == NULL)
665 return 0;
666
667 iter->he = NULL;
668
669 hists__inc_nr_samples(&evsel->hists, he->filtered);
670
671 return hist_entry__append_callchain(he, sample);
672 }
673
674 static int
675 iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
676 struct addr_location *al __maybe_unused)
677 {
678 struct hist_entry **he_cache;
679
680 callchain_cursor_commit(&callchain_cursor);
681
682 /*
683 * This is for detecting cycles or recursions so that they're
684 * cumulated only one time to prevent entries more than 100%
685 * overhead.
686 */
687 he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1));
688 if (he_cache == NULL)
689 return -ENOMEM;
690
691 iter->priv = he_cache;
692 iter->curr = 0;
693
694 return 0;
695 }
696
697 static int
698 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
699 struct addr_location *al)
700 {
701 struct perf_evsel *evsel = iter->evsel;
702 struct perf_sample *sample = iter->sample;
703 struct hist_entry **he_cache = iter->priv;
704 struct hist_entry *he;
705 int err = 0;
706
707 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
708 sample->period, sample->weight,
709 sample->transaction, true);
710 if (he == NULL)
711 return -ENOMEM;
712
713 iter->he = he;
714 he_cache[iter->curr++] = he;
715
716 callchain_append(he->callchain, &callchain_cursor, sample->period);
717
718 /*
719 * We need to re-initialize the cursor since callchain_append()
720 * advanced the cursor to the end.
721 */
722 callchain_cursor_commit(&callchain_cursor);
723
724 hists__inc_nr_samples(&evsel->hists, he->filtered);
725
726 return err;
727 }
728
729 static int
730 iter_next_cumulative_entry(struct hist_entry_iter *iter,
731 struct addr_location *al)
732 {
733 struct callchain_cursor_node *node;
734
735 node = callchain_cursor_current(&callchain_cursor);
736 if (node == NULL)
737 return 0;
738
739 return fill_callchain_info(al, node, iter->hide_unresolved);
740 }
741
742 static int
743 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
744 struct addr_location *al)
745 {
746 struct perf_evsel *evsel = iter->evsel;
747 struct perf_sample *sample = iter->sample;
748 struct hist_entry **he_cache = iter->priv;
749 struct hist_entry *he;
750 struct hist_entry he_tmp = {
751 .cpu = al->cpu,
752 .thread = al->thread,
753 .comm = thread__comm(al->thread),
754 .ip = al->addr,
755 .ms = {
756 .map = al->map,
757 .sym = al->sym,
758 },
759 .parent = iter->parent,
760 };
761 int i;
762 struct callchain_cursor cursor;
763
764 callchain_cursor_snapshot(&cursor, &callchain_cursor);
765
766 callchain_cursor_advance(&callchain_cursor);
767
768 /*
769 * Check if there's duplicate entries in the callchain.
770 * It's possible that it has cycles or recursive calls.
771 */
772 for (i = 0; i < iter->curr; i++) {
773 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
774 /* to avoid calling callback function */
775 iter->he = NULL;
776 return 0;
777 }
778 }
779
780 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
781 sample->period, sample->weight,
782 sample->transaction, false);
783 if (he == NULL)
784 return -ENOMEM;
785
786 iter->he = he;
787 he_cache[iter->curr++] = he;
788
789 callchain_append(he->callchain, &cursor, sample->period);
790 return 0;
791 }
792
793 static int
794 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
795 struct addr_location *al __maybe_unused)
796 {
797 zfree(&iter->priv);
798 iter->he = NULL;
799
800 return 0;
801 }
802
803 const struct hist_iter_ops hist_iter_mem = {
804 .prepare_entry = iter_prepare_mem_entry,
805 .add_single_entry = iter_add_single_mem_entry,
806 .next_entry = iter_next_nop_entry,
807 .add_next_entry = iter_add_next_nop_entry,
808 .finish_entry = iter_finish_mem_entry,
809 };
810
811 const struct hist_iter_ops hist_iter_branch = {
812 .prepare_entry = iter_prepare_branch_entry,
813 .add_single_entry = iter_add_single_branch_entry,
814 .next_entry = iter_next_branch_entry,
815 .add_next_entry = iter_add_next_branch_entry,
816 .finish_entry = iter_finish_branch_entry,
817 };
818
819 const struct hist_iter_ops hist_iter_normal = {
820 .prepare_entry = iter_prepare_normal_entry,
821 .add_single_entry = iter_add_single_normal_entry,
822 .next_entry = iter_next_nop_entry,
823 .add_next_entry = iter_add_next_nop_entry,
824 .finish_entry = iter_finish_normal_entry,
825 };
826
827 const struct hist_iter_ops hist_iter_cumulative = {
828 .prepare_entry = iter_prepare_cumulative_entry,
829 .add_single_entry = iter_add_single_cumulative_entry,
830 .next_entry = iter_next_cumulative_entry,
831 .add_next_entry = iter_add_next_cumulative_entry,
832 .finish_entry = iter_finish_cumulative_entry,
833 };
834
835 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
836 struct perf_evsel *evsel, struct perf_sample *sample,
837 int max_stack_depth, void *arg)
838 {
839 int err, err2;
840
841 err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
842 max_stack_depth);
843 if (err)
844 return err;
845
846 iter->evsel = evsel;
847 iter->sample = sample;
848
849 err = iter->ops->prepare_entry(iter, al);
850 if (err)
851 goto out;
852
853 err = iter->ops->add_single_entry(iter, al);
854 if (err)
855 goto out;
856
857 if (iter->he && iter->add_entry_cb) {
858 err = iter->add_entry_cb(iter, al, true, arg);
859 if (err)
860 goto out;
861 }
862
863 while (iter->ops->next_entry(iter, al)) {
864 err = iter->ops->add_next_entry(iter, al);
865 if (err)
866 break;
867
868 if (iter->he && iter->add_entry_cb) {
869 err = iter->add_entry_cb(iter, al, false, arg);
870 if (err)
871 goto out;
872 }
873 }
874
875 out:
876 err2 = iter->ops->finish_entry(iter, al);
877 if (!err)
878 err = err2;
879
880 return err;
881 }
882
883 int64_t
884 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
885 {
886 struct perf_hpp_fmt *fmt;
887 int64_t cmp = 0;
888
889 perf_hpp__for_each_sort_list(fmt) {
890 if (perf_hpp__should_skip(fmt))
891 continue;
892
893 cmp = fmt->cmp(left, right);
894 if (cmp)
895 break;
896 }
897
898 return cmp;
899 }
900
901 int64_t
902 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
903 {
904 struct perf_hpp_fmt *fmt;
905 int64_t cmp = 0;
906
907 perf_hpp__for_each_sort_list(fmt) {
908 if (perf_hpp__should_skip(fmt))
909 continue;
910
911 cmp = fmt->collapse(left, right);
912 if (cmp)
913 break;
914 }
915
916 return cmp;
917 }
918
919 void hist_entry__free(struct hist_entry *he)
920 {
921 zfree(&he->branch_info);
922 zfree(&he->mem_info);
923 zfree(&he->stat_acc);
924 free_srcline(he->srcline);
925 free(he);
926 }
927
928 /*
929 * collapse the histogram
930 */
931
932 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
933 struct rb_root *root,
934 struct hist_entry *he)
935 {
936 struct rb_node **p = &root->rb_node;
937 struct rb_node *parent = NULL;
938 struct hist_entry *iter;
939 int64_t cmp;
940
941 while (*p != NULL) {
942 parent = *p;
943 iter = rb_entry(parent, struct hist_entry, rb_node_in);
944
945 cmp = hist_entry__collapse(iter, he);
946
947 if (!cmp) {
948 he_stat__add_stat(&iter->stat, &he->stat);
949 if (symbol_conf.cumulate_callchain)
950 he_stat__add_stat(iter->stat_acc, he->stat_acc);
951
952 if (symbol_conf.use_callchain) {
953 callchain_cursor_reset(&callchain_cursor);
954 callchain_merge(&callchain_cursor,
955 iter->callchain,
956 he->callchain);
957 }
958 hist_entry__free(he);
959 return false;
960 }
961
962 if (cmp < 0)
963 p = &(*p)->rb_left;
964 else
965 p = &(*p)->rb_right;
966 }
967
968 rb_link_node(&he->rb_node_in, parent, p);
969 rb_insert_color(&he->rb_node_in, root);
970 return true;
971 }
972
973 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
974 {
975 struct rb_root *root;
976
977 pthread_mutex_lock(&hists->lock);
978
979 root = hists->entries_in;
980 if (++hists->entries_in > &hists->entries_in_array[1])
981 hists->entries_in = &hists->entries_in_array[0];
982
983 pthread_mutex_unlock(&hists->lock);
984
985 return root;
986 }
987
988 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
989 {
990 hists__filter_entry_by_dso(hists, he);
991 hists__filter_entry_by_thread(hists, he);
992 hists__filter_entry_by_symbol(hists, he);
993 }
994
995 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
996 {
997 struct rb_root *root;
998 struct rb_node *next;
999 struct hist_entry *n;
1000
1001 if (!sort__need_collapse)
1002 return;
1003
1004 root = hists__get_rotate_entries_in(hists);
1005 next = rb_first(root);
1006
1007 while (next) {
1008 if (session_done())
1009 break;
1010 n = rb_entry(next, struct hist_entry, rb_node_in);
1011 next = rb_next(&n->rb_node_in);
1012
1013 rb_erase(&n->rb_node_in, root);
1014 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1015 /*
1016 * If it wasn't combined with one of the entries already
1017 * collapsed, we need to apply the filters that may have
1018 * been set by, say, the hist_browser.
1019 */
1020 hists__apply_filters(hists, n);
1021 }
1022 if (prog)
1023 ui_progress__update(prog, 1);
1024 }
1025 }
1026
1027 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1028 {
1029 struct perf_hpp_fmt *fmt;
1030 int64_t cmp = 0;
1031
1032 perf_hpp__for_each_sort_list(fmt) {
1033 if (perf_hpp__should_skip(fmt))
1034 continue;
1035
1036 cmp = fmt->sort(a, b);
1037 if (cmp)
1038 break;
1039 }
1040
1041 return cmp;
1042 }
1043
1044 static void hists__reset_filter_stats(struct hists *hists)
1045 {
1046 hists->nr_non_filtered_entries = 0;
1047 hists->stats.total_non_filtered_period = 0;
1048 }
1049
1050 void hists__reset_stats(struct hists *hists)
1051 {
1052 hists->nr_entries = 0;
1053 hists->stats.total_period = 0;
1054
1055 hists__reset_filter_stats(hists);
1056 }
1057
1058 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1059 {
1060 hists->nr_non_filtered_entries++;
1061 hists->stats.total_non_filtered_period += h->stat.period;
1062 }
1063
1064 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1065 {
1066 if (!h->filtered)
1067 hists__inc_filter_stats(hists, h);
1068
1069 hists->nr_entries++;
1070 hists->stats.total_period += h->stat.period;
1071 }
1072
1073 static void __hists__insert_output_entry(struct rb_root *entries,
1074 struct hist_entry *he,
1075 u64 min_callchain_hits)
1076 {
1077 struct rb_node **p = &entries->rb_node;
1078 struct rb_node *parent = NULL;
1079 struct hist_entry *iter;
1080
1081 if (symbol_conf.use_callchain)
1082 callchain_param.sort(&he->sorted_chain, he->callchain,
1083 min_callchain_hits, &callchain_param);
1084
1085 while (*p != NULL) {
1086 parent = *p;
1087 iter = rb_entry(parent, struct hist_entry, rb_node);
1088
1089 if (hist_entry__sort(he, iter) > 0)
1090 p = &(*p)->rb_left;
1091 else
1092 p = &(*p)->rb_right;
1093 }
1094
1095 rb_link_node(&he->rb_node, parent, p);
1096 rb_insert_color(&he->rb_node, entries);
1097 }
1098
1099 void hists__output_resort(struct hists *hists)
1100 {
1101 struct rb_root *root;
1102 struct rb_node *next;
1103 struct hist_entry *n;
1104 u64 min_callchain_hits;
1105
1106 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
1107
1108 if (sort__need_collapse)
1109 root = &hists->entries_collapsed;
1110 else
1111 root = hists->entries_in;
1112
1113 next = rb_first(root);
1114 hists->entries = RB_ROOT;
1115
1116 hists__reset_stats(hists);
1117 hists__reset_col_len(hists);
1118
1119 while (next) {
1120 n = rb_entry(next, struct hist_entry, rb_node_in);
1121 next = rb_next(&n->rb_node_in);
1122
1123 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
1124 hists__inc_stats(hists, n);
1125
1126 if (!n->filtered)
1127 hists__calc_col_len(hists, n);
1128 }
1129 }
1130
1131 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1132 enum hist_filter filter)
1133 {
1134 h->filtered &= ~(1 << filter);
1135 if (h->filtered)
1136 return;
1137
1138 /* force fold unfiltered entry for simplicity */
1139 h->ms.unfolded = false;
1140 h->row_offset = 0;
1141
1142 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1143
1144 hists__inc_filter_stats(hists, h);
1145 hists__calc_col_len(hists, h);
1146 }
1147
1148
1149 static bool hists__filter_entry_by_dso(struct hists *hists,
1150 struct hist_entry *he)
1151 {
1152 if (hists->dso_filter != NULL &&
1153 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1154 he->filtered |= (1 << HIST_FILTER__DSO);
1155 return true;
1156 }
1157
1158 return false;
1159 }
1160
1161 void hists__filter_by_dso(struct hists *hists)
1162 {
1163 struct rb_node *nd;
1164
1165 hists->stats.nr_non_filtered_samples = 0;
1166
1167 hists__reset_filter_stats(hists);
1168 hists__reset_col_len(hists);
1169
1170 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1171 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1172
1173 if (symbol_conf.exclude_other && !h->parent)
1174 continue;
1175
1176 if (hists__filter_entry_by_dso(hists, h))
1177 continue;
1178
1179 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1180 }
1181 }
1182
1183 static bool hists__filter_entry_by_thread(struct hists *hists,
1184 struct hist_entry *he)
1185 {
1186 if (hists->thread_filter != NULL &&
1187 he->thread != hists->thread_filter) {
1188 he->filtered |= (1 << HIST_FILTER__THREAD);
1189 return true;
1190 }
1191
1192 return false;
1193 }
1194
1195 void hists__filter_by_thread(struct hists *hists)
1196 {
1197 struct rb_node *nd;
1198
1199 hists->stats.nr_non_filtered_samples = 0;
1200
1201 hists__reset_filter_stats(hists);
1202 hists__reset_col_len(hists);
1203
1204 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1205 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1206
1207 if (hists__filter_entry_by_thread(hists, h))
1208 continue;
1209
1210 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1211 }
1212 }
1213
1214 static bool hists__filter_entry_by_symbol(struct hists *hists,
1215 struct hist_entry *he)
1216 {
1217 if (hists->symbol_filter_str != NULL &&
1218 (!he->ms.sym || strstr(he->ms.sym->name,
1219 hists->symbol_filter_str) == NULL)) {
1220 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1221 return true;
1222 }
1223
1224 return false;
1225 }
1226
1227 void hists__filter_by_symbol(struct hists *hists)
1228 {
1229 struct rb_node *nd;
1230
1231 hists->stats.nr_non_filtered_samples = 0;
1232
1233 hists__reset_filter_stats(hists);
1234 hists__reset_col_len(hists);
1235
1236 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1237 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1238
1239 if (hists__filter_entry_by_symbol(hists, h))
1240 continue;
1241
1242 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1243 }
1244 }
1245
1246 void events_stats__inc(struct events_stats *stats, u32 type)
1247 {
1248 ++stats->nr_events[0];
1249 ++stats->nr_events[type];
1250 }
1251
1252 void hists__inc_nr_events(struct hists *hists, u32 type)
1253 {
1254 events_stats__inc(&hists->stats, type);
1255 }
1256
1257 void hists__inc_nr_samples(struct hists *hists, bool filtered)
1258 {
1259 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1260 if (!filtered)
1261 hists->stats.nr_non_filtered_samples++;
1262 }
1263
1264 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1265 struct hist_entry *pair)
1266 {
1267 struct rb_root *root;
1268 struct rb_node **p;
1269 struct rb_node *parent = NULL;
1270 struct hist_entry *he;
1271 int64_t cmp;
1272
1273 if (sort__need_collapse)
1274 root = &hists->entries_collapsed;
1275 else
1276 root = hists->entries_in;
1277
1278 p = &root->rb_node;
1279
1280 while (*p != NULL) {
1281 parent = *p;
1282 he = rb_entry(parent, struct hist_entry, rb_node_in);
1283
1284 cmp = hist_entry__collapse(he, pair);
1285
1286 if (!cmp)
1287 goto out;
1288
1289 if (cmp < 0)
1290 p = &(*p)->rb_left;
1291 else
1292 p = &(*p)->rb_right;
1293 }
1294
1295 he = hist_entry__new(pair, true);
1296 if (he) {
1297 memset(&he->stat, 0, sizeof(he->stat));
1298 he->hists = hists;
1299 rb_link_node(&he->rb_node_in, parent, p);
1300 rb_insert_color(&he->rb_node_in, root);
1301 hists__inc_stats(hists, he);
1302 he->dummy = true;
1303 }
1304 out:
1305 return he;
1306 }
1307
1308 static struct hist_entry *hists__find_entry(struct hists *hists,
1309 struct hist_entry *he)
1310 {
1311 struct rb_node *n;
1312
1313 if (sort__need_collapse)
1314 n = hists->entries_collapsed.rb_node;
1315 else
1316 n = hists->entries_in->rb_node;
1317
1318 while (n) {
1319 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1320 int64_t cmp = hist_entry__collapse(iter, he);
1321
1322 if (cmp < 0)
1323 n = n->rb_left;
1324 else if (cmp > 0)
1325 n = n->rb_right;
1326 else
1327 return iter;
1328 }
1329
1330 return NULL;
1331 }
1332
1333 /*
1334 * Look for pairs to link to the leader buckets (hist_entries):
1335 */
1336 void hists__match(struct hists *leader, struct hists *other)
1337 {
1338 struct rb_root *root;
1339 struct rb_node *nd;
1340 struct hist_entry *pos, *pair;
1341
1342 if (sort__need_collapse)
1343 root = &leader->entries_collapsed;
1344 else
1345 root = leader->entries_in;
1346
1347 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1348 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1349 pair = hists__find_entry(other, pos);
1350
1351 if (pair)
1352 hist_entry__add_pair(pair, pos);
1353 }
1354 }
1355
1356 /*
1357 * Look for entries in the other hists that are not present in the leader, if
1358 * we find them, just add a dummy entry on the leader hists, with period=0,
1359 * nr_events=0, to serve as the list header.
1360 */
1361 int hists__link(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 = &other->entries_collapsed;
1369 else
1370 root = other->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
1375 if (!hist_entry__has_pairs(pos)) {
1376 pair = hists__add_dummy_entry(leader, pos);
1377 if (pair == NULL)
1378 return -1;
1379 hist_entry__add_pair(pos, pair);
1380 }
1381 }
1382
1383 return 0;
1384 }
1385
1386 u64 hists__total_period(struct hists *hists)
1387 {
1388 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1389 hists->stats.total_period;
1390 }
1391
1392 int parse_filter_percentage(const struct option *opt __maybe_unused,
1393 const char *arg, int unset __maybe_unused)
1394 {
1395 if (!strcmp(arg, "relative"))
1396 symbol_conf.filter_relative = true;
1397 else if (!strcmp(arg, "absolute"))
1398 symbol_conf.filter_relative = false;
1399 else
1400 return -1;
1401
1402 return 0;
1403 }
1404
1405 int perf_hist_config(const char *var, const char *value)
1406 {
1407 if (!strcmp(var, "hist.percentage"))
1408 return parse_filter_percentage(NULL, value, 0);
1409
1410 return 0;
1411 }