perf tools: Consolidate output field handling to hpp format routines
[deliverable/linux.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 <math.h>
8
9 static bool hists__filter_entry_by_dso(struct hists *hists,
10 struct hist_entry *he);
11 static bool hists__filter_entry_by_thread(struct hists *hists,
12 struct hist_entry *he);
13 static bool hists__filter_entry_by_symbol(struct hists *hists,
14 struct hist_entry *he);
15
16 struct callchain_param callchain_param = {
17 .mode = CHAIN_GRAPH_REL,
18 .min_percent = 0.5,
19 .order = ORDER_CALLEE,
20 .key = CCKEY_FUNCTION
21 };
22
23 u16 hists__col_len(struct hists *hists, enum hist_column col)
24 {
25 return hists->col_len[col];
26 }
27
28 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
29 {
30 hists->col_len[col] = len;
31 }
32
33 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
34 {
35 if (len > hists__col_len(hists, col)) {
36 hists__set_col_len(hists, col, len);
37 return true;
38 }
39 return false;
40 }
41
42 void hists__reset_col_len(struct hists *hists)
43 {
44 enum hist_column col;
45
46 for (col = 0; col < HISTC_NR_COLS; ++col)
47 hists__set_col_len(hists, col, 0);
48 }
49
50 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
51 {
52 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
53
54 if (hists__col_len(hists, dso) < unresolved_col_width &&
55 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
56 !symbol_conf.dso_list)
57 hists__set_col_len(hists, dso, unresolved_col_width);
58 }
59
60 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
61 {
62 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
63 int symlen;
64 u16 len;
65
66 /*
67 * +4 accounts for '[x] ' priv level info
68 * +2 accounts for 0x prefix on raw addresses
69 * +3 accounts for ' y ' symtab origin info
70 */
71 if (h->ms.sym) {
72 symlen = h->ms.sym->namelen + 4;
73 if (verbose)
74 symlen += BITS_PER_LONG / 4 + 2 + 3;
75 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
76 } else {
77 symlen = unresolved_col_width + 4 + 2;
78 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
79 hists__set_unres_dso_col_len(hists, HISTC_DSO);
80 }
81
82 len = thread__comm_len(h->thread);
83 if (hists__new_col_len(hists, HISTC_COMM, len))
84 hists__set_col_len(hists, HISTC_THREAD, len + 6);
85
86 if (h->ms.map) {
87 len = dso__name_len(h->ms.map->dso);
88 hists__new_col_len(hists, HISTC_DSO, len);
89 }
90
91 if (h->parent)
92 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
93
94 if (h->branch_info) {
95 if (h->branch_info->from.sym) {
96 symlen = (int)h->branch_info->from.sym->namelen + 4;
97 if (verbose)
98 symlen += BITS_PER_LONG / 4 + 2 + 3;
99 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
100
101 symlen = dso__name_len(h->branch_info->from.map->dso);
102 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
103 } else {
104 symlen = unresolved_col_width + 4 + 2;
105 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
106 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
107 }
108
109 if (h->branch_info->to.sym) {
110 symlen = (int)h->branch_info->to.sym->namelen + 4;
111 if (verbose)
112 symlen += BITS_PER_LONG / 4 + 2 + 3;
113 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
114
115 symlen = dso__name_len(h->branch_info->to.map->dso);
116 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
117 } else {
118 symlen = unresolved_col_width + 4 + 2;
119 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
120 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
121 }
122 }
123
124 if (h->mem_info) {
125 if (h->mem_info->daddr.sym) {
126 symlen = (int)h->mem_info->daddr.sym->namelen + 4
127 + unresolved_col_width + 2;
128 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
129 symlen);
130 } else {
131 symlen = unresolved_col_width + 4 + 2;
132 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
133 symlen);
134 }
135 if (h->mem_info->daddr.map) {
136 symlen = dso__name_len(h->mem_info->daddr.map->dso);
137 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
138 symlen);
139 } else {
140 symlen = unresolved_col_width + 4 + 2;
141 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
142 }
143 } else {
144 symlen = unresolved_col_width + 4 + 2;
145 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
146 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
147 }
148
149 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
150 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
151 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
152 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
153 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
154 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
155
156 if (h->transaction)
157 hists__new_col_len(hists, HISTC_TRANSACTION,
158 hist_entry__transaction_len());
159 }
160
161 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
162 {
163 struct rb_node *next = rb_first(&hists->entries);
164 struct hist_entry *n;
165 int row = 0;
166
167 hists__reset_col_len(hists);
168
169 while (next && row++ < max_rows) {
170 n = rb_entry(next, struct hist_entry, rb_node);
171 if (!n->filtered)
172 hists__calc_col_len(hists, n);
173 next = rb_next(&n->rb_node);
174 }
175 }
176
177 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
178 unsigned int cpumode, u64 period)
179 {
180 switch (cpumode) {
181 case PERF_RECORD_MISC_KERNEL:
182 he_stat->period_sys += period;
183 break;
184 case PERF_RECORD_MISC_USER:
185 he_stat->period_us += period;
186 break;
187 case PERF_RECORD_MISC_GUEST_KERNEL:
188 he_stat->period_guest_sys += period;
189 break;
190 case PERF_RECORD_MISC_GUEST_USER:
191 he_stat->period_guest_us += period;
192 break;
193 default:
194 break;
195 }
196 }
197
198 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
199 u64 weight)
200 {
201
202 he_stat->period += period;
203 he_stat->weight += weight;
204 he_stat->nr_events += 1;
205 }
206
207 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
208 {
209 dest->period += src->period;
210 dest->period_sys += src->period_sys;
211 dest->period_us += src->period_us;
212 dest->period_guest_sys += src->period_guest_sys;
213 dest->period_guest_us += src->period_guest_us;
214 dest->nr_events += src->nr_events;
215 dest->weight += src->weight;
216 }
217
218 static void he_stat__decay(struct he_stat *he_stat)
219 {
220 he_stat->period = (he_stat->period * 7) / 8;
221 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
222 /* XXX need decay for weight too? */
223 }
224
225 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
226 {
227 u64 prev_period = he->stat.period;
228 u64 diff;
229
230 if (prev_period == 0)
231 return true;
232
233 he_stat__decay(&he->stat);
234
235 diff = prev_period - he->stat.period;
236
237 hists->stats.total_period -= diff;
238 if (!he->filtered)
239 hists->stats.total_non_filtered_period -= diff;
240
241 return he->stat.period == 0;
242 }
243
244 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
245 {
246 struct rb_node *next = rb_first(&hists->entries);
247 struct hist_entry *n;
248
249 while (next) {
250 n = rb_entry(next, struct hist_entry, rb_node);
251 next = rb_next(&n->rb_node);
252 /*
253 * We may be annotating this, for instance, so keep it here in
254 * case some it gets new samples, we'll eventually free it when
255 * the user stops browsing and it agains gets fully decayed.
256 */
257 if (((zap_user && n->level == '.') ||
258 (zap_kernel && n->level != '.') ||
259 hists__decay_entry(hists, n)) &&
260 !n->used) {
261 rb_erase(&n->rb_node, &hists->entries);
262
263 if (sort__need_collapse)
264 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
265
266 --hists->nr_entries;
267 if (!n->filtered)
268 --hists->nr_non_filtered_entries;
269
270 hist_entry__free(n);
271 }
272 }
273 }
274
275 /*
276 * histogram, sorted on item, collects periods
277 */
278
279 static struct hist_entry *hist_entry__new(struct hist_entry *template)
280 {
281 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
282 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
283
284 if (he != NULL) {
285 *he = *template;
286
287 if (he->ms.map)
288 he->ms.map->referenced = true;
289
290 if (he->branch_info) {
291 /*
292 * This branch info is (a part of) allocated from
293 * sample__resolve_bstack() and will be freed after
294 * adding new entries. So we need to save a copy.
295 */
296 he->branch_info = malloc(sizeof(*he->branch_info));
297 if (he->branch_info == NULL) {
298 free(he);
299 return NULL;
300 }
301
302 memcpy(he->branch_info, template->branch_info,
303 sizeof(*he->branch_info));
304
305 if (he->branch_info->from.map)
306 he->branch_info->from.map->referenced = true;
307 if (he->branch_info->to.map)
308 he->branch_info->to.map->referenced = true;
309 }
310
311 if (he->mem_info) {
312 if (he->mem_info->iaddr.map)
313 he->mem_info->iaddr.map->referenced = true;
314 if (he->mem_info->daddr.map)
315 he->mem_info->daddr.map->referenced = true;
316 }
317
318 if (symbol_conf.use_callchain)
319 callchain_init(he->callchain);
320
321 INIT_LIST_HEAD(&he->pairs.node);
322 }
323
324 return he;
325 }
326
327 static u8 symbol__parent_filter(const struct symbol *parent)
328 {
329 if (symbol_conf.exclude_other && parent == NULL)
330 return 1 << HIST_FILTER__PARENT;
331 return 0;
332 }
333
334 static struct hist_entry *add_hist_entry(struct hists *hists,
335 struct hist_entry *entry,
336 struct addr_location *al)
337 {
338 struct rb_node **p;
339 struct rb_node *parent = NULL;
340 struct hist_entry *he;
341 int64_t cmp;
342 u64 period = entry->stat.period;
343 u64 weight = entry->stat.weight;
344
345 p = &hists->entries_in->rb_node;
346
347 while (*p != NULL) {
348 parent = *p;
349 he = rb_entry(parent, struct hist_entry, rb_node_in);
350
351 /*
352 * Make sure that it receives arguments in a same order as
353 * hist_entry__collapse() so that we can use an appropriate
354 * function when searching an entry regardless which sort
355 * keys were used.
356 */
357 cmp = hist_entry__cmp(he, entry);
358
359 if (!cmp) {
360 he_stat__add_period(&he->stat, period, weight);
361
362 /*
363 * This mem info was allocated from sample__resolve_mem
364 * and will not be used anymore.
365 */
366 zfree(&entry->mem_info);
367
368 /* If the map of an existing hist_entry has
369 * become out-of-date due to an exec() or
370 * similar, update it. Otherwise we will
371 * mis-adjust symbol addresses when computing
372 * the history counter to increment.
373 */
374 if (he->ms.map != entry->ms.map) {
375 he->ms.map = entry->ms.map;
376 if (he->ms.map)
377 he->ms.map->referenced = true;
378 }
379 goto out;
380 }
381
382 if (cmp < 0)
383 p = &(*p)->rb_left;
384 else
385 p = &(*p)->rb_right;
386 }
387
388 he = hist_entry__new(entry);
389 if (!he)
390 return NULL;
391
392 rb_link_node(&he->rb_node_in, parent, p);
393 rb_insert_color(&he->rb_node_in, hists->entries_in);
394 out:
395 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
396 return he;
397 }
398
399 struct hist_entry *__hists__add_entry(struct hists *hists,
400 struct addr_location *al,
401 struct symbol *sym_parent,
402 struct branch_info *bi,
403 struct mem_info *mi,
404 u64 period, u64 weight, u64 transaction)
405 {
406 struct hist_entry entry = {
407 .thread = al->thread,
408 .comm = thread__comm(al->thread),
409 .ms = {
410 .map = al->map,
411 .sym = al->sym,
412 },
413 .cpu = al->cpu,
414 .ip = al->addr,
415 .level = al->level,
416 .stat = {
417 .nr_events = 1,
418 .period = period,
419 .weight = weight,
420 },
421 .parent = sym_parent,
422 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
423 .hists = hists,
424 .branch_info = bi,
425 .mem_info = mi,
426 .transaction = transaction,
427 };
428
429 return add_hist_entry(hists, &entry, al);
430 }
431
432 int64_t
433 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
434 {
435 struct perf_hpp_fmt *fmt;
436 int64_t cmp = 0;
437
438 perf_hpp__for_each_sort_list(fmt) {
439 cmp = fmt->cmp(left, right);
440 if (cmp)
441 break;
442 }
443
444 return cmp;
445 }
446
447 int64_t
448 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
449 {
450 struct perf_hpp_fmt *fmt;
451 int64_t cmp = 0;
452
453 perf_hpp__for_each_sort_list(fmt) {
454 cmp = fmt->collapse(left, right);
455 if (cmp)
456 break;
457 }
458
459 return cmp;
460 }
461
462 void hist_entry__free(struct hist_entry *he)
463 {
464 zfree(&he->branch_info);
465 zfree(&he->mem_info);
466 free_srcline(he->srcline);
467 free(he);
468 }
469
470 /*
471 * collapse the histogram
472 */
473
474 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
475 struct rb_root *root,
476 struct hist_entry *he)
477 {
478 struct rb_node **p = &root->rb_node;
479 struct rb_node *parent = NULL;
480 struct hist_entry *iter;
481 int64_t cmp;
482
483 while (*p != NULL) {
484 parent = *p;
485 iter = rb_entry(parent, struct hist_entry, rb_node_in);
486
487 cmp = hist_entry__collapse(iter, he);
488
489 if (!cmp) {
490 he_stat__add_stat(&iter->stat, &he->stat);
491
492 if (symbol_conf.use_callchain) {
493 callchain_cursor_reset(&callchain_cursor);
494 callchain_merge(&callchain_cursor,
495 iter->callchain,
496 he->callchain);
497 }
498 hist_entry__free(he);
499 return false;
500 }
501
502 if (cmp < 0)
503 p = &(*p)->rb_left;
504 else
505 p = &(*p)->rb_right;
506 }
507
508 rb_link_node(&he->rb_node_in, parent, p);
509 rb_insert_color(&he->rb_node_in, root);
510 return true;
511 }
512
513 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
514 {
515 struct rb_root *root;
516
517 pthread_mutex_lock(&hists->lock);
518
519 root = hists->entries_in;
520 if (++hists->entries_in > &hists->entries_in_array[1])
521 hists->entries_in = &hists->entries_in_array[0];
522
523 pthread_mutex_unlock(&hists->lock);
524
525 return root;
526 }
527
528 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
529 {
530 hists__filter_entry_by_dso(hists, he);
531 hists__filter_entry_by_thread(hists, he);
532 hists__filter_entry_by_symbol(hists, he);
533 }
534
535 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
536 {
537 struct rb_root *root;
538 struct rb_node *next;
539 struct hist_entry *n;
540
541 if (!sort__need_collapse)
542 return;
543
544 root = hists__get_rotate_entries_in(hists);
545 next = rb_first(root);
546
547 while (next) {
548 if (session_done())
549 break;
550 n = rb_entry(next, struct hist_entry, rb_node_in);
551 next = rb_next(&n->rb_node_in);
552
553 rb_erase(&n->rb_node_in, root);
554 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
555 /*
556 * If it wasn't combined with one of the entries already
557 * collapsed, we need to apply the filters that may have
558 * been set by, say, the hist_browser.
559 */
560 hists__apply_filters(hists, n);
561 }
562 if (prog)
563 ui_progress__update(prog, 1);
564 }
565 }
566
567 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
568 {
569 struct perf_hpp_fmt *fmt;
570 int64_t cmp = 0;
571
572 perf_hpp__for_each_sort_list(fmt) {
573 cmp = fmt->sort(a, b);
574 if (cmp)
575 break;
576 }
577
578 return cmp;
579 }
580
581 static void hists__reset_filter_stats(struct hists *hists)
582 {
583 hists->nr_non_filtered_entries = 0;
584 hists->stats.total_non_filtered_period = 0;
585 }
586
587 void hists__reset_stats(struct hists *hists)
588 {
589 hists->nr_entries = 0;
590 hists->stats.total_period = 0;
591
592 hists__reset_filter_stats(hists);
593 }
594
595 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
596 {
597 hists->nr_non_filtered_entries++;
598 hists->stats.total_non_filtered_period += h->stat.period;
599 }
600
601 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
602 {
603 if (!h->filtered)
604 hists__inc_filter_stats(hists, h);
605
606 hists->nr_entries++;
607 hists->stats.total_period += h->stat.period;
608 }
609
610 static void __hists__insert_output_entry(struct rb_root *entries,
611 struct hist_entry *he,
612 u64 min_callchain_hits)
613 {
614 struct rb_node **p = &entries->rb_node;
615 struct rb_node *parent = NULL;
616 struct hist_entry *iter;
617
618 if (symbol_conf.use_callchain)
619 callchain_param.sort(&he->sorted_chain, he->callchain,
620 min_callchain_hits, &callchain_param);
621
622 while (*p != NULL) {
623 parent = *p;
624 iter = rb_entry(parent, struct hist_entry, rb_node);
625
626 if (hist_entry__sort(he, iter) > 0)
627 p = &(*p)->rb_left;
628 else
629 p = &(*p)->rb_right;
630 }
631
632 rb_link_node(&he->rb_node, parent, p);
633 rb_insert_color(&he->rb_node, entries);
634 }
635
636 void hists__output_resort(struct hists *hists)
637 {
638 struct rb_root *root;
639 struct rb_node *next;
640 struct hist_entry *n;
641 u64 min_callchain_hits;
642
643 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
644
645 if (sort__need_collapse)
646 root = &hists->entries_collapsed;
647 else
648 root = hists->entries_in;
649
650 next = rb_first(root);
651 hists->entries = RB_ROOT;
652
653 hists__reset_stats(hists);
654 hists__reset_col_len(hists);
655
656 while (next) {
657 n = rb_entry(next, struct hist_entry, rb_node_in);
658 next = rb_next(&n->rb_node_in);
659
660 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
661 hists__inc_stats(hists, n);
662
663 if (!n->filtered)
664 hists__calc_col_len(hists, n);
665 }
666 }
667
668 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
669 enum hist_filter filter)
670 {
671 h->filtered &= ~(1 << filter);
672 if (h->filtered)
673 return;
674
675 /* force fold unfiltered entry for simplicity */
676 h->ms.unfolded = false;
677 h->row_offset = 0;
678
679 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
680
681 hists__inc_filter_stats(hists, h);
682 hists__calc_col_len(hists, h);
683 }
684
685
686 static bool hists__filter_entry_by_dso(struct hists *hists,
687 struct hist_entry *he)
688 {
689 if (hists->dso_filter != NULL &&
690 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
691 he->filtered |= (1 << HIST_FILTER__DSO);
692 return true;
693 }
694
695 return false;
696 }
697
698 void hists__filter_by_dso(struct hists *hists)
699 {
700 struct rb_node *nd;
701
702 hists->stats.nr_non_filtered_samples = 0;
703
704 hists__reset_filter_stats(hists);
705 hists__reset_col_len(hists);
706
707 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
708 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
709
710 if (symbol_conf.exclude_other && !h->parent)
711 continue;
712
713 if (hists__filter_entry_by_dso(hists, h))
714 continue;
715
716 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
717 }
718 }
719
720 static bool hists__filter_entry_by_thread(struct hists *hists,
721 struct hist_entry *he)
722 {
723 if (hists->thread_filter != NULL &&
724 he->thread != hists->thread_filter) {
725 he->filtered |= (1 << HIST_FILTER__THREAD);
726 return true;
727 }
728
729 return false;
730 }
731
732 void hists__filter_by_thread(struct hists *hists)
733 {
734 struct rb_node *nd;
735
736 hists->stats.nr_non_filtered_samples = 0;
737
738 hists__reset_filter_stats(hists);
739 hists__reset_col_len(hists);
740
741 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
742 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
743
744 if (hists__filter_entry_by_thread(hists, h))
745 continue;
746
747 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
748 }
749 }
750
751 static bool hists__filter_entry_by_symbol(struct hists *hists,
752 struct hist_entry *he)
753 {
754 if (hists->symbol_filter_str != NULL &&
755 (!he->ms.sym || strstr(he->ms.sym->name,
756 hists->symbol_filter_str) == NULL)) {
757 he->filtered |= (1 << HIST_FILTER__SYMBOL);
758 return true;
759 }
760
761 return false;
762 }
763
764 void hists__filter_by_symbol(struct hists *hists)
765 {
766 struct rb_node *nd;
767
768 hists->stats.nr_non_filtered_samples = 0;
769
770 hists__reset_filter_stats(hists);
771 hists__reset_col_len(hists);
772
773 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
774 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
775
776 if (hists__filter_entry_by_symbol(hists, h))
777 continue;
778
779 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
780 }
781 }
782
783 void events_stats__inc(struct events_stats *stats, u32 type)
784 {
785 ++stats->nr_events[0];
786 ++stats->nr_events[type];
787 }
788
789 void hists__inc_nr_events(struct hists *hists, u32 type)
790 {
791 events_stats__inc(&hists->stats, type);
792 }
793
794 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
795 struct hist_entry *pair)
796 {
797 struct rb_root *root;
798 struct rb_node **p;
799 struct rb_node *parent = NULL;
800 struct hist_entry *he;
801 int64_t cmp;
802
803 if (sort__need_collapse)
804 root = &hists->entries_collapsed;
805 else
806 root = hists->entries_in;
807
808 p = &root->rb_node;
809
810 while (*p != NULL) {
811 parent = *p;
812 he = rb_entry(parent, struct hist_entry, rb_node_in);
813
814 cmp = hist_entry__collapse(he, pair);
815
816 if (!cmp)
817 goto out;
818
819 if (cmp < 0)
820 p = &(*p)->rb_left;
821 else
822 p = &(*p)->rb_right;
823 }
824
825 he = hist_entry__new(pair);
826 if (he) {
827 memset(&he->stat, 0, sizeof(he->stat));
828 he->hists = hists;
829 rb_link_node(&he->rb_node_in, parent, p);
830 rb_insert_color(&he->rb_node_in, root);
831 hists__inc_stats(hists, he);
832 he->dummy = true;
833 }
834 out:
835 return he;
836 }
837
838 static struct hist_entry *hists__find_entry(struct hists *hists,
839 struct hist_entry *he)
840 {
841 struct rb_node *n;
842
843 if (sort__need_collapse)
844 n = hists->entries_collapsed.rb_node;
845 else
846 n = hists->entries_in->rb_node;
847
848 while (n) {
849 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
850 int64_t cmp = hist_entry__collapse(iter, he);
851
852 if (cmp < 0)
853 n = n->rb_left;
854 else if (cmp > 0)
855 n = n->rb_right;
856 else
857 return iter;
858 }
859
860 return NULL;
861 }
862
863 /*
864 * Look for pairs to link to the leader buckets (hist_entries):
865 */
866 void hists__match(struct hists *leader, struct hists *other)
867 {
868 struct rb_root *root;
869 struct rb_node *nd;
870 struct hist_entry *pos, *pair;
871
872 if (sort__need_collapse)
873 root = &leader->entries_collapsed;
874 else
875 root = leader->entries_in;
876
877 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
878 pos = rb_entry(nd, struct hist_entry, rb_node_in);
879 pair = hists__find_entry(other, pos);
880
881 if (pair)
882 hist_entry__add_pair(pair, pos);
883 }
884 }
885
886 /*
887 * Look for entries in the other hists that are not present in the leader, if
888 * we find them, just add a dummy entry on the leader hists, with period=0,
889 * nr_events=0, to serve as the list header.
890 */
891 int hists__link(struct hists *leader, struct hists *other)
892 {
893 struct rb_root *root;
894 struct rb_node *nd;
895 struct hist_entry *pos, *pair;
896
897 if (sort__need_collapse)
898 root = &other->entries_collapsed;
899 else
900 root = other->entries_in;
901
902 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
903 pos = rb_entry(nd, struct hist_entry, rb_node_in);
904
905 if (!hist_entry__has_pairs(pos)) {
906 pair = hists__add_dummy_entry(leader, pos);
907 if (pair == NULL)
908 return -1;
909 hist_entry__add_pair(pos, pair);
910 }
911 }
912
913 return 0;
914 }
915
916 u64 hists__total_period(struct hists *hists)
917 {
918 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
919 hists->stats.total_period;
920 }
921
922 int parse_filter_percentage(const struct option *opt __maybe_unused,
923 const char *arg, int unset __maybe_unused)
924 {
925 if (!strcmp(arg, "relative"))
926 symbol_conf.filter_relative = true;
927 else if (!strcmp(arg, "absolute"))
928 symbol_conf.filter_relative = false;
929 else
930 return -1;
931
932 return 0;
933 }
934
935 int perf_hist_config(const char *var, const char *value)
936 {
937 if (!strcmp(var, "hist.percentage"))
938 return parse_filter_percentage(NULL, value, 0);
939
940 return 0;
941 }
This page took 0.117212 seconds and 6 git commands to generate.