perf hists: Move he->stat.nr_events initialization to a template
[deliverable/linux.git] / tools / perf / util / hist.c
1 #include "annotate.h"
2 #include "util.h"
3 #include "build-id.h"
4 #include "hist.h"
5 #include "session.h"
6 #include "sort.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 enum hist_filter {
17 HIST_FILTER__DSO,
18 HIST_FILTER__THREAD,
19 HIST_FILTER__PARENT,
20 HIST_FILTER__SYMBOL,
21 };
22
23 struct callchain_param callchain_param = {
24 .mode = CHAIN_GRAPH_REL,
25 .min_percent = 0.5,
26 .order = ORDER_CALLEE
27 };
28
29 u16 hists__col_len(struct hists *hists, enum hist_column col)
30 {
31 return hists->col_len[col];
32 }
33
34 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
35 {
36 hists->col_len[col] = len;
37 }
38
39 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
40 {
41 if (len > hists__col_len(hists, col)) {
42 hists__set_col_len(hists, col, len);
43 return true;
44 }
45 return false;
46 }
47
48 void hists__reset_col_len(struct hists *hists)
49 {
50 enum hist_column col;
51
52 for (col = 0; col < HISTC_NR_COLS; ++col)
53 hists__set_col_len(hists, col, 0);
54 }
55
56 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
57 {
58 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
59
60 if (hists__col_len(hists, dso) < unresolved_col_width &&
61 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
62 !symbol_conf.dso_list)
63 hists__set_col_len(hists, dso, unresolved_col_width);
64 }
65
66 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
67 {
68 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
69 u16 len;
70
71 if (h->ms.sym)
72 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
73 else
74 hists__set_unres_dso_col_len(hists, HISTC_DSO);
75
76 len = thread__comm_len(h->thread);
77 if (hists__new_col_len(hists, HISTC_COMM, len))
78 hists__set_col_len(hists, HISTC_THREAD, len + 6);
79
80 if (h->ms.map) {
81 len = dso__name_len(h->ms.map->dso);
82 hists__new_col_len(hists, HISTC_DSO, len);
83 }
84
85 if (h->branch_info) {
86 int symlen;
87 /*
88 * +4 accounts for '[x] ' priv level info
89 * +2 account of 0x prefix on raw addresses
90 */
91 if (h->branch_info->from.sym) {
92 symlen = (int)h->branch_info->from.sym->namelen + 4;
93 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
94
95 symlen = dso__name_len(h->branch_info->from.map->dso);
96 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
97 } else {
98 symlen = unresolved_col_width + 4 + 2;
99 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
100 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
101 }
102
103 if (h->branch_info->to.sym) {
104 symlen = (int)h->branch_info->to.sym->namelen + 4;
105 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
106
107 symlen = dso__name_len(h->branch_info->to.map->dso);
108 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
109 } else {
110 symlen = unresolved_col_width + 4 + 2;
111 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
113 }
114 }
115 }
116
117 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
118 {
119 struct rb_node *next = rb_first(&hists->entries);
120 struct hist_entry *n;
121 int row = 0;
122
123 hists__reset_col_len(hists);
124
125 while (next && row++ < max_rows) {
126 n = rb_entry(next, struct hist_entry, rb_node);
127 if (!n->filtered)
128 hists__calc_col_len(hists, n);
129 next = rb_next(&n->rb_node);
130 }
131 }
132
133 static void hist_entry__add_cpumode_period(struct hist_entry *he,
134 unsigned int cpumode, u64 period)
135 {
136 switch (cpumode) {
137 case PERF_RECORD_MISC_KERNEL:
138 he->stat.period_sys += period;
139 break;
140 case PERF_RECORD_MISC_USER:
141 he->stat.period_us += period;
142 break;
143 case PERF_RECORD_MISC_GUEST_KERNEL:
144 he->stat.period_guest_sys += period;
145 break;
146 case PERF_RECORD_MISC_GUEST_USER:
147 he->stat.period_guest_us += period;
148 break;
149 default:
150 break;
151 }
152 }
153
154 static void hist_entry__decay(struct hist_entry *he)
155 {
156 he->stat.period = (he->stat.period * 7) / 8;
157 he->stat.nr_events = (he->stat.nr_events * 7) / 8;
158 }
159
160 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
161 {
162 u64 prev_period = he->stat.period;
163
164 if (prev_period == 0)
165 return true;
166
167 hist_entry__decay(he);
168
169 if (!he->filtered)
170 hists->stats.total_period -= prev_period - he->stat.period;
171
172 return he->stat.period == 0;
173 }
174
175 static void __hists__decay_entries(struct hists *hists, bool zap_user,
176 bool zap_kernel, bool threaded)
177 {
178 struct rb_node *next = rb_first(&hists->entries);
179 struct hist_entry *n;
180
181 while (next) {
182 n = rb_entry(next, struct hist_entry, rb_node);
183 next = rb_next(&n->rb_node);
184 /*
185 * We may be annotating this, for instance, so keep it here in
186 * case some it gets new samples, we'll eventually free it when
187 * the user stops browsing and it agains gets fully decayed.
188 */
189 if (((zap_user && n->level == '.') ||
190 (zap_kernel && n->level != '.') ||
191 hists__decay_entry(hists, n)) &&
192 !n->used) {
193 rb_erase(&n->rb_node, &hists->entries);
194
195 if (sort__need_collapse || threaded)
196 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
197
198 hist_entry__free(n);
199 --hists->nr_entries;
200 }
201 }
202 }
203
204 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
205 {
206 return __hists__decay_entries(hists, zap_user, zap_kernel, false);
207 }
208
209 void hists__decay_entries_threaded(struct hists *hists,
210 bool zap_user, bool zap_kernel)
211 {
212 return __hists__decay_entries(hists, zap_user, zap_kernel, true);
213 }
214
215 /*
216 * histogram, sorted on item, collects periods
217 */
218
219 static struct hist_entry *hist_entry__new(struct hist_entry *template)
220 {
221 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
222 struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
223
224 if (he != NULL) {
225 *he = *template;
226
227 if (he->ms.map)
228 he->ms.map->referenced = true;
229 if (symbol_conf.use_callchain)
230 callchain_init(he->callchain);
231 }
232
233 return he;
234 }
235
236 static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
237 {
238 if (!h->filtered) {
239 hists__calc_col_len(hists, h);
240 ++hists->nr_entries;
241 hists->stats.total_period += h->stat.period;
242 }
243 }
244
245 static u8 symbol__parent_filter(const struct symbol *parent)
246 {
247 if (symbol_conf.exclude_other && parent == NULL)
248 return 1 << HIST_FILTER__PARENT;
249 return 0;
250 }
251
252 static struct hist_entry *add_hist_entry(struct hists *hists,
253 struct hist_entry *entry,
254 struct addr_location *al,
255 u64 period)
256 {
257 struct rb_node **p;
258 struct rb_node *parent = NULL;
259 struct hist_entry *he;
260 int cmp;
261
262 pthread_mutex_lock(&hists->lock);
263
264 p = &hists->entries_in->rb_node;
265
266 while (*p != NULL) {
267 parent = *p;
268 he = rb_entry(parent, struct hist_entry, rb_node_in);
269
270 cmp = hist_entry__cmp(entry, he);
271
272 if (!cmp) {
273 he->stat.period += period;
274 ++he->stat.nr_events;
275
276 /* If the map of an existing hist_entry has
277 * become out-of-date due to an exec() or
278 * similar, update it. Otherwise we will
279 * mis-adjust symbol addresses when computing
280 * the history counter to increment.
281 */
282 if (he->ms.map != entry->ms.map) {
283 he->ms.map = entry->ms.map;
284 if (he->ms.map)
285 he->ms.map->referenced = true;
286 }
287 goto out;
288 }
289
290 if (cmp < 0)
291 p = &(*p)->rb_left;
292 else
293 p = &(*p)->rb_right;
294 }
295
296 he = hist_entry__new(entry);
297 if (!he)
298 goto out_unlock;
299
300 rb_link_node(&he->rb_node_in, parent, p);
301 rb_insert_color(&he->rb_node_in, hists->entries_in);
302 out:
303 hist_entry__add_cpumode_period(he, al->cpumode, period);
304 out_unlock:
305 pthread_mutex_unlock(&hists->lock);
306 return he;
307 }
308
309 struct hist_entry *__hists__add_branch_entry(struct hists *self,
310 struct addr_location *al,
311 struct symbol *sym_parent,
312 struct branch_info *bi,
313 u64 period)
314 {
315 struct hist_entry entry = {
316 .thread = al->thread,
317 .ms = {
318 .map = bi->to.map,
319 .sym = bi->to.sym,
320 },
321 .cpu = al->cpu,
322 .ip = bi->to.addr,
323 .level = al->level,
324 .stat = {
325 .period = period,
326 .nr_events = 1,
327 },
328 .parent = sym_parent,
329 .filtered = symbol__parent_filter(sym_parent),
330 .branch_info = bi,
331 .hists = self,
332 };
333
334 return add_hist_entry(self, &entry, al, period);
335 }
336
337 struct hist_entry *__hists__add_entry(struct hists *self,
338 struct addr_location *al,
339 struct symbol *sym_parent, u64 period)
340 {
341 struct hist_entry entry = {
342 .thread = al->thread,
343 .ms = {
344 .map = al->map,
345 .sym = al->sym,
346 },
347 .cpu = al->cpu,
348 .ip = al->addr,
349 .level = al->level,
350 .stat = {
351 .period = period,
352 .nr_events = 1,
353 },
354 .parent = sym_parent,
355 .filtered = symbol__parent_filter(sym_parent),
356 .hists = self,
357 };
358
359 return add_hist_entry(self, &entry, al, period);
360 }
361
362 int64_t
363 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
364 {
365 struct sort_entry *se;
366 int64_t cmp = 0;
367
368 list_for_each_entry(se, &hist_entry__sort_list, list) {
369 cmp = se->se_cmp(left, right);
370 if (cmp)
371 break;
372 }
373
374 return cmp;
375 }
376
377 int64_t
378 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
379 {
380 struct sort_entry *se;
381 int64_t cmp = 0;
382
383 list_for_each_entry(se, &hist_entry__sort_list, list) {
384 int64_t (*f)(struct hist_entry *, struct hist_entry *);
385
386 f = se->se_collapse ?: se->se_cmp;
387
388 cmp = f(left, right);
389 if (cmp)
390 break;
391 }
392
393 return cmp;
394 }
395
396 void hist_entry__free(struct hist_entry *he)
397 {
398 free(he);
399 }
400
401 /*
402 * collapse the histogram
403 */
404
405 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
406 struct rb_root *root,
407 struct hist_entry *he)
408 {
409 struct rb_node **p = &root->rb_node;
410 struct rb_node *parent = NULL;
411 struct hist_entry *iter;
412 int64_t cmp;
413
414 while (*p != NULL) {
415 parent = *p;
416 iter = rb_entry(parent, struct hist_entry, rb_node_in);
417
418 cmp = hist_entry__collapse(iter, he);
419
420 if (!cmp) {
421 iter->stat.period += he->stat.period;
422 iter->stat.period_sys += he->stat.period_sys;
423 iter->stat.period_us += he->stat.period_us;
424 iter->stat.period_guest_sys += he->stat.period_guest_sys;
425 iter->stat.period_guest_us += he->stat.period_guest_us;
426 iter->stat.nr_events += he->stat.nr_events;
427
428 if (symbol_conf.use_callchain) {
429 callchain_cursor_reset(&callchain_cursor);
430 callchain_merge(&callchain_cursor,
431 iter->callchain,
432 he->callchain);
433 }
434 hist_entry__free(he);
435 return false;
436 }
437
438 if (cmp < 0)
439 p = &(*p)->rb_left;
440 else
441 p = &(*p)->rb_right;
442 }
443
444 rb_link_node(&he->rb_node_in, parent, p);
445 rb_insert_color(&he->rb_node_in, root);
446 return true;
447 }
448
449 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
450 {
451 struct rb_root *root;
452
453 pthread_mutex_lock(&hists->lock);
454
455 root = hists->entries_in;
456 if (++hists->entries_in > &hists->entries_in_array[1])
457 hists->entries_in = &hists->entries_in_array[0];
458
459 pthread_mutex_unlock(&hists->lock);
460
461 return root;
462 }
463
464 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
465 {
466 hists__filter_entry_by_dso(hists, he);
467 hists__filter_entry_by_thread(hists, he);
468 hists__filter_entry_by_symbol(hists, he);
469 }
470
471 static void __hists__collapse_resort(struct hists *hists, bool threaded)
472 {
473 struct rb_root *root;
474 struct rb_node *next;
475 struct hist_entry *n;
476
477 if (!sort__need_collapse && !threaded)
478 return;
479
480 root = hists__get_rotate_entries_in(hists);
481 next = rb_first(root);
482
483 while (next) {
484 n = rb_entry(next, struct hist_entry, rb_node_in);
485 next = rb_next(&n->rb_node_in);
486
487 rb_erase(&n->rb_node_in, root);
488 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
489 /*
490 * If it wasn't combined with one of the entries already
491 * collapsed, we need to apply the filters that may have
492 * been set by, say, the hist_browser.
493 */
494 hists__apply_filters(hists, n);
495 }
496 }
497 }
498
499 void hists__collapse_resort(struct hists *hists)
500 {
501 return __hists__collapse_resort(hists, false);
502 }
503
504 void hists__collapse_resort_threaded(struct hists *hists)
505 {
506 return __hists__collapse_resort(hists, true);
507 }
508
509 /*
510 * reverse the map, sort on period.
511 */
512
513 static void __hists__insert_output_entry(struct rb_root *entries,
514 struct hist_entry *he,
515 u64 min_callchain_hits)
516 {
517 struct rb_node **p = &entries->rb_node;
518 struct rb_node *parent = NULL;
519 struct hist_entry *iter;
520
521 if (symbol_conf.use_callchain)
522 callchain_param.sort(&he->sorted_chain, he->callchain,
523 min_callchain_hits, &callchain_param);
524
525 while (*p != NULL) {
526 parent = *p;
527 iter = rb_entry(parent, struct hist_entry, rb_node);
528
529 if (he->stat.period > iter->stat.period)
530 p = &(*p)->rb_left;
531 else
532 p = &(*p)->rb_right;
533 }
534
535 rb_link_node(&he->rb_node, parent, p);
536 rb_insert_color(&he->rb_node, entries);
537 }
538
539 static void __hists__output_resort(struct hists *hists, bool threaded)
540 {
541 struct rb_root *root;
542 struct rb_node *next;
543 struct hist_entry *n;
544 u64 min_callchain_hits;
545
546 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
547
548 if (sort__need_collapse || threaded)
549 root = &hists->entries_collapsed;
550 else
551 root = hists->entries_in;
552
553 next = rb_first(root);
554 hists->entries = RB_ROOT;
555
556 hists->nr_entries = 0;
557 hists->stats.total_period = 0;
558 hists__reset_col_len(hists);
559
560 while (next) {
561 n = rb_entry(next, struct hist_entry, rb_node_in);
562 next = rb_next(&n->rb_node_in);
563
564 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
565 hists__inc_nr_entries(hists, n);
566 }
567 }
568
569 void hists__output_resort(struct hists *hists)
570 {
571 return __hists__output_resort(hists, false);
572 }
573
574 void hists__output_resort_threaded(struct hists *hists)
575 {
576 return __hists__output_resort(hists, true);
577 }
578
579 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
580 enum hist_filter filter)
581 {
582 h->filtered &= ~(1 << filter);
583 if (h->filtered)
584 return;
585
586 ++hists->nr_entries;
587 if (h->ms.unfolded)
588 hists->nr_entries += h->nr_rows;
589 h->row_offset = 0;
590 hists->stats.total_period += h->stat.period;
591 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
592
593 hists__calc_col_len(hists, h);
594 }
595
596
597 static bool hists__filter_entry_by_dso(struct hists *hists,
598 struct hist_entry *he)
599 {
600 if (hists->dso_filter != NULL &&
601 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
602 he->filtered |= (1 << HIST_FILTER__DSO);
603 return true;
604 }
605
606 return false;
607 }
608
609 void hists__filter_by_dso(struct hists *hists)
610 {
611 struct rb_node *nd;
612
613 hists->nr_entries = hists->stats.total_period = 0;
614 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
615 hists__reset_col_len(hists);
616
617 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
618 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
619
620 if (symbol_conf.exclude_other && !h->parent)
621 continue;
622
623 if (hists__filter_entry_by_dso(hists, h))
624 continue;
625
626 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
627 }
628 }
629
630 static bool hists__filter_entry_by_thread(struct hists *hists,
631 struct hist_entry *he)
632 {
633 if (hists->thread_filter != NULL &&
634 he->thread != hists->thread_filter) {
635 he->filtered |= (1 << HIST_FILTER__THREAD);
636 return true;
637 }
638
639 return false;
640 }
641
642 void hists__filter_by_thread(struct hists *hists)
643 {
644 struct rb_node *nd;
645
646 hists->nr_entries = hists->stats.total_period = 0;
647 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
648 hists__reset_col_len(hists);
649
650 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
651 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
652
653 if (hists__filter_entry_by_thread(hists, h))
654 continue;
655
656 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
657 }
658 }
659
660 static bool hists__filter_entry_by_symbol(struct hists *hists,
661 struct hist_entry *he)
662 {
663 if (hists->symbol_filter_str != NULL &&
664 (!he->ms.sym || strstr(he->ms.sym->name,
665 hists->symbol_filter_str) == NULL)) {
666 he->filtered |= (1 << HIST_FILTER__SYMBOL);
667 return true;
668 }
669
670 return false;
671 }
672
673 void hists__filter_by_symbol(struct hists *hists)
674 {
675 struct rb_node *nd;
676
677 hists->nr_entries = hists->stats.total_period = 0;
678 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
679 hists__reset_col_len(hists);
680
681 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
682 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
683
684 if (hists__filter_entry_by_symbol(hists, h))
685 continue;
686
687 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
688 }
689 }
690
691 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
692 {
693 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
694 }
695
696 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
697 {
698 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
699 }
700
701 void hists__inc_nr_events(struct hists *hists, u32 type)
702 {
703 ++hists->stats.nr_events[0];
704 ++hists->stats.nr_events[type];
705 }
This page took 0.104169 seconds and 5 git commands to generate.