]>
Commit | Line | Data |
---|---|---|
1 | #include "util.h" | |
2 | #include "build-id.h" | |
3 | #include "hist.h" | |
4 | #include "map.h" | |
5 | #include "session.h" | |
6 | #include "namespaces.h" | |
7 | #include "sort.h" | |
8 | #include "evlist.h" | |
9 | #include "evsel.h" | |
10 | #include "annotate.h" | |
11 | #include "ui/progress.h" | |
12 | #include <math.h> | |
13 | ||
14 | static bool hists__filter_entry_by_dso(struct hists *hists, | |
15 | struct hist_entry *he); | |
16 | static bool hists__filter_entry_by_thread(struct hists *hists, | |
17 | struct hist_entry *he); | |
18 | static bool hists__filter_entry_by_symbol(struct hists *hists, | |
19 | struct hist_entry *he); | |
20 | static bool hists__filter_entry_by_socket(struct hists *hists, | |
21 | struct hist_entry *he); | |
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 > 0) | |
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 + 8); | |
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 > 0) | |
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 > 0) | |
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 | if (h->branch_info->srcline_from) | |
124 | hists__new_col_len(hists, HISTC_SRCLINE_FROM, | |
125 | strlen(h->branch_info->srcline_from)); | |
126 | if (h->branch_info->srcline_to) | |
127 | hists__new_col_len(hists, HISTC_SRCLINE_TO, | |
128 | strlen(h->branch_info->srcline_to)); | |
129 | } | |
130 | ||
131 | if (h->mem_info) { | |
132 | if (h->mem_info->daddr.sym) { | |
133 | symlen = (int)h->mem_info->daddr.sym->namelen + 4 | |
134 | + unresolved_col_width + 2; | |
135 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, | |
136 | symlen); | |
137 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, | |
138 | symlen + 1); | |
139 | } else { | |
140 | symlen = unresolved_col_width + 4 + 2; | |
141 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, | |
142 | symlen); | |
143 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, | |
144 | symlen); | |
145 | } | |
146 | ||
147 | if (h->mem_info->iaddr.sym) { | |
148 | symlen = (int)h->mem_info->iaddr.sym->namelen + 4 | |
149 | + unresolved_col_width + 2; | |
150 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, | |
151 | symlen); | |
152 | } else { | |
153 | symlen = unresolved_col_width + 4 + 2; | |
154 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, | |
155 | symlen); | |
156 | } | |
157 | ||
158 | if (h->mem_info->daddr.map) { | |
159 | symlen = dso__name_len(h->mem_info->daddr.map->dso); | |
160 | hists__new_col_len(hists, HISTC_MEM_DADDR_DSO, | |
161 | symlen); | |
162 | } else { | |
163 | symlen = unresolved_col_width + 4 + 2; | |
164 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); | |
165 | } | |
166 | } else { | |
167 | symlen = unresolved_col_width + 4 + 2; | |
168 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen); | |
169 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen); | |
170 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); | |
171 | } | |
172 | ||
173 | hists__new_col_len(hists, HISTC_CGROUP_ID, 20); | |
174 | hists__new_col_len(hists, HISTC_CPU, 3); | |
175 | hists__new_col_len(hists, HISTC_SOCKET, 6); | |
176 | hists__new_col_len(hists, HISTC_MEM_LOCKED, 6); | |
177 | hists__new_col_len(hists, HISTC_MEM_TLB, 22); | |
178 | hists__new_col_len(hists, HISTC_MEM_SNOOP, 12); | |
179 | hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3); | |
180 | hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12); | |
181 | hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12); | |
182 | ||
183 | if (h->srcline) { | |
184 | len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header)); | |
185 | hists__new_col_len(hists, HISTC_SRCLINE, len); | |
186 | } | |
187 | ||
188 | if (h->srcfile) | |
189 | hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile)); | |
190 | ||
191 | if (h->transaction) | |
192 | hists__new_col_len(hists, HISTC_TRANSACTION, | |
193 | hist_entry__transaction_len()); | |
194 | ||
195 | if (h->trace_output) | |
196 | hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output)); | |
197 | } | |
198 | ||
199 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) | |
200 | { | |
201 | struct rb_node *next = rb_first(&hists->entries); | |
202 | struct hist_entry *n; | |
203 | int row = 0; | |
204 | ||
205 | hists__reset_col_len(hists); | |
206 | ||
207 | while (next && row++ < max_rows) { | |
208 | n = rb_entry(next, struct hist_entry, rb_node); | |
209 | if (!n->filtered) | |
210 | hists__calc_col_len(hists, n); | |
211 | next = rb_next(&n->rb_node); | |
212 | } | |
213 | } | |
214 | ||
215 | static void he_stat__add_cpumode_period(struct he_stat *he_stat, | |
216 | unsigned int cpumode, u64 period) | |
217 | { | |
218 | switch (cpumode) { | |
219 | case PERF_RECORD_MISC_KERNEL: | |
220 | he_stat->period_sys += period; | |
221 | break; | |
222 | case PERF_RECORD_MISC_USER: | |
223 | he_stat->period_us += period; | |
224 | break; | |
225 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
226 | he_stat->period_guest_sys += period; | |
227 | break; | |
228 | case PERF_RECORD_MISC_GUEST_USER: | |
229 | he_stat->period_guest_us += period; | |
230 | break; | |
231 | default: | |
232 | break; | |
233 | } | |
234 | } | |
235 | ||
236 | static void he_stat__add_period(struct he_stat *he_stat, u64 period, | |
237 | u64 weight) | |
238 | { | |
239 | ||
240 | he_stat->period += period; | |
241 | he_stat->weight += weight; | |
242 | he_stat->nr_events += 1; | |
243 | } | |
244 | ||
245 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) | |
246 | { | |
247 | dest->period += src->period; | |
248 | dest->period_sys += src->period_sys; | |
249 | dest->period_us += src->period_us; | |
250 | dest->period_guest_sys += src->period_guest_sys; | |
251 | dest->period_guest_us += src->period_guest_us; | |
252 | dest->nr_events += src->nr_events; | |
253 | dest->weight += src->weight; | |
254 | } | |
255 | ||
256 | static void he_stat__decay(struct he_stat *he_stat) | |
257 | { | |
258 | he_stat->period = (he_stat->period * 7) / 8; | |
259 | he_stat->nr_events = (he_stat->nr_events * 7) / 8; | |
260 | /* XXX need decay for weight too? */ | |
261 | } | |
262 | ||
263 | static void hists__delete_entry(struct hists *hists, struct hist_entry *he); | |
264 | ||
265 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) | |
266 | { | |
267 | u64 prev_period = he->stat.period; | |
268 | u64 diff; | |
269 | ||
270 | if (prev_period == 0) | |
271 | return true; | |
272 | ||
273 | he_stat__decay(&he->stat); | |
274 | if (symbol_conf.cumulate_callchain) | |
275 | he_stat__decay(he->stat_acc); | |
276 | decay_callchain(he->callchain); | |
277 | ||
278 | diff = prev_period - he->stat.period; | |
279 | ||
280 | if (!he->depth) { | |
281 | hists->stats.total_period -= diff; | |
282 | if (!he->filtered) | |
283 | hists->stats.total_non_filtered_period -= diff; | |
284 | } | |
285 | ||
286 | if (!he->leaf) { | |
287 | struct hist_entry *child; | |
288 | struct rb_node *node = rb_first(&he->hroot_out); | |
289 | while (node) { | |
290 | child = rb_entry(node, struct hist_entry, rb_node); | |
291 | node = rb_next(node); | |
292 | ||
293 | if (hists__decay_entry(hists, child)) | |
294 | hists__delete_entry(hists, child); | |
295 | } | |
296 | } | |
297 | ||
298 | return he->stat.period == 0; | |
299 | } | |
300 | ||
301 | static void hists__delete_entry(struct hists *hists, struct hist_entry *he) | |
302 | { | |
303 | struct rb_root *root_in; | |
304 | struct rb_root *root_out; | |
305 | ||
306 | if (he->parent_he) { | |
307 | root_in = &he->parent_he->hroot_in; | |
308 | root_out = &he->parent_he->hroot_out; | |
309 | } else { | |
310 | if (hists__has(hists, need_collapse)) | |
311 | root_in = &hists->entries_collapsed; | |
312 | else | |
313 | root_in = hists->entries_in; | |
314 | root_out = &hists->entries; | |
315 | } | |
316 | ||
317 | rb_erase(&he->rb_node_in, root_in); | |
318 | rb_erase(&he->rb_node, root_out); | |
319 | ||
320 | --hists->nr_entries; | |
321 | if (!he->filtered) | |
322 | --hists->nr_non_filtered_entries; | |
323 | ||
324 | hist_entry__delete(he); | |
325 | } | |
326 | ||
327 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) | |
328 | { | |
329 | struct rb_node *next = rb_first(&hists->entries); | |
330 | struct hist_entry *n; | |
331 | ||
332 | while (next) { | |
333 | n = rb_entry(next, struct hist_entry, rb_node); | |
334 | next = rb_next(&n->rb_node); | |
335 | if (((zap_user && n->level == '.') || | |
336 | (zap_kernel && n->level != '.') || | |
337 | hists__decay_entry(hists, n))) { | |
338 | hists__delete_entry(hists, n); | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | void hists__delete_entries(struct hists *hists) | |
344 | { | |
345 | struct rb_node *next = rb_first(&hists->entries); | |
346 | struct hist_entry *n; | |
347 | ||
348 | while (next) { | |
349 | n = rb_entry(next, struct hist_entry, rb_node); | |
350 | next = rb_next(&n->rb_node); | |
351 | ||
352 | hists__delete_entry(hists, n); | |
353 | } | |
354 | } | |
355 | ||
356 | /* | |
357 | * histogram, sorted on item, collects periods | |
358 | */ | |
359 | ||
360 | static int hist_entry__init(struct hist_entry *he, | |
361 | struct hist_entry *template, | |
362 | bool sample_self) | |
363 | { | |
364 | *he = *template; | |
365 | ||
366 | if (symbol_conf.cumulate_callchain) { | |
367 | he->stat_acc = malloc(sizeof(he->stat)); | |
368 | if (he->stat_acc == NULL) | |
369 | return -ENOMEM; | |
370 | memcpy(he->stat_acc, &he->stat, sizeof(he->stat)); | |
371 | if (!sample_self) | |
372 | memset(&he->stat, 0, sizeof(he->stat)); | |
373 | } | |
374 | ||
375 | map__get(he->ms.map); | |
376 | ||
377 | if (he->branch_info) { | |
378 | /* | |
379 | * This branch info is (a part of) allocated from | |
380 | * sample__resolve_bstack() and will be freed after | |
381 | * adding new entries. So we need to save a copy. | |
382 | */ | |
383 | he->branch_info = malloc(sizeof(*he->branch_info)); | |
384 | if (he->branch_info == NULL) { | |
385 | map__zput(he->ms.map); | |
386 | free(he->stat_acc); | |
387 | return -ENOMEM; | |
388 | } | |
389 | ||
390 | memcpy(he->branch_info, template->branch_info, | |
391 | sizeof(*he->branch_info)); | |
392 | ||
393 | map__get(he->branch_info->from.map); | |
394 | map__get(he->branch_info->to.map); | |
395 | } | |
396 | ||
397 | if (he->mem_info) { | |
398 | map__get(he->mem_info->iaddr.map); | |
399 | map__get(he->mem_info->daddr.map); | |
400 | } | |
401 | ||
402 | if (symbol_conf.use_callchain) | |
403 | callchain_init(he->callchain); | |
404 | ||
405 | if (he->raw_data) { | |
406 | he->raw_data = memdup(he->raw_data, he->raw_size); | |
407 | ||
408 | if (he->raw_data == NULL) { | |
409 | map__put(he->ms.map); | |
410 | if (he->branch_info) { | |
411 | map__put(he->branch_info->from.map); | |
412 | map__put(he->branch_info->to.map); | |
413 | free(he->branch_info); | |
414 | } | |
415 | if (he->mem_info) { | |
416 | map__put(he->mem_info->iaddr.map); | |
417 | map__put(he->mem_info->daddr.map); | |
418 | } | |
419 | free(he->stat_acc); | |
420 | return -ENOMEM; | |
421 | } | |
422 | } | |
423 | INIT_LIST_HEAD(&he->pairs.node); | |
424 | thread__get(he->thread); | |
425 | he->hroot_in = RB_ROOT; | |
426 | he->hroot_out = RB_ROOT; | |
427 | ||
428 | if (!symbol_conf.report_hierarchy) | |
429 | he->leaf = true; | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | static void *hist_entry__zalloc(size_t size) | |
435 | { | |
436 | return zalloc(size + sizeof(struct hist_entry)); | |
437 | } | |
438 | ||
439 | static void hist_entry__free(void *ptr) | |
440 | { | |
441 | free(ptr); | |
442 | } | |
443 | ||
444 | static struct hist_entry_ops default_ops = { | |
445 | .new = hist_entry__zalloc, | |
446 | .free = hist_entry__free, | |
447 | }; | |
448 | ||
449 | static struct hist_entry *hist_entry__new(struct hist_entry *template, | |
450 | bool sample_self) | |
451 | { | |
452 | struct hist_entry_ops *ops = template->ops; | |
453 | size_t callchain_size = 0; | |
454 | struct hist_entry *he; | |
455 | int err = 0; | |
456 | ||
457 | if (!ops) | |
458 | ops = template->ops = &default_ops; | |
459 | ||
460 | if (symbol_conf.use_callchain) | |
461 | callchain_size = sizeof(struct callchain_root); | |
462 | ||
463 | he = ops->new(callchain_size); | |
464 | if (he) { | |
465 | err = hist_entry__init(he, template, sample_self); | |
466 | if (err) { | |
467 | ops->free(he); | |
468 | he = NULL; | |
469 | } | |
470 | } | |
471 | ||
472 | return he; | |
473 | } | |
474 | ||
475 | static u8 symbol__parent_filter(const struct symbol *parent) | |
476 | { | |
477 | if (symbol_conf.exclude_other && parent == NULL) | |
478 | return 1 << HIST_FILTER__PARENT; | |
479 | return 0; | |
480 | } | |
481 | ||
482 | static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period) | |
483 | { | |
484 | if (!symbol_conf.use_callchain) | |
485 | return; | |
486 | ||
487 | he->hists->callchain_period += period; | |
488 | if (!he->filtered) | |
489 | he->hists->callchain_non_filtered_period += period; | |
490 | } | |
491 | ||
492 | static struct hist_entry *hists__findnew_entry(struct hists *hists, | |
493 | struct hist_entry *entry, | |
494 | struct addr_location *al, | |
495 | bool sample_self) | |
496 | { | |
497 | struct rb_node **p; | |
498 | struct rb_node *parent = NULL; | |
499 | struct hist_entry *he; | |
500 | int64_t cmp; | |
501 | u64 period = entry->stat.period; | |
502 | u64 weight = entry->stat.weight; | |
503 | ||
504 | p = &hists->entries_in->rb_node; | |
505 | ||
506 | while (*p != NULL) { | |
507 | parent = *p; | |
508 | he = rb_entry(parent, struct hist_entry, rb_node_in); | |
509 | ||
510 | /* | |
511 | * Make sure that it receives arguments in a same order as | |
512 | * hist_entry__collapse() so that we can use an appropriate | |
513 | * function when searching an entry regardless which sort | |
514 | * keys were used. | |
515 | */ | |
516 | cmp = hist_entry__cmp(he, entry); | |
517 | ||
518 | if (!cmp) { | |
519 | if (sample_self) { | |
520 | he_stat__add_period(&he->stat, period, weight); | |
521 | hist_entry__add_callchain_period(he, period); | |
522 | } | |
523 | if (symbol_conf.cumulate_callchain) | |
524 | he_stat__add_period(he->stat_acc, period, weight); | |
525 | ||
526 | /* | |
527 | * This mem info was allocated from sample__resolve_mem | |
528 | * and will not be used anymore. | |
529 | */ | |
530 | zfree(&entry->mem_info); | |
531 | ||
532 | /* If the map of an existing hist_entry has | |
533 | * become out-of-date due to an exec() or | |
534 | * similar, update it. Otherwise we will | |
535 | * mis-adjust symbol addresses when computing | |
536 | * the history counter to increment. | |
537 | */ | |
538 | if (he->ms.map != entry->ms.map) { | |
539 | map__put(he->ms.map); | |
540 | he->ms.map = map__get(entry->ms.map); | |
541 | } | |
542 | goto out; | |
543 | } | |
544 | ||
545 | if (cmp < 0) | |
546 | p = &(*p)->rb_left; | |
547 | else | |
548 | p = &(*p)->rb_right; | |
549 | } | |
550 | ||
551 | he = hist_entry__new(entry, sample_self); | |
552 | if (!he) | |
553 | return NULL; | |
554 | ||
555 | if (sample_self) | |
556 | hist_entry__add_callchain_period(he, period); | |
557 | hists->nr_entries++; | |
558 | ||
559 | rb_link_node(&he->rb_node_in, parent, p); | |
560 | rb_insert_color(&he->rb_node_in, hists->entries_in); | |
561 | out: | |
562 | if (sample_self) | |
563 | he_stat__add_cpumode_period(&he->stat, al->cpumode, period); | |
564 | if (symbol_conf.cumulate_callchain) | |
565 | he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period); | |
566 | return he; | |
567 | } | |
568 | ||
569 | static struct hist_entry* | |
570 | __hists__add_entry(struct hists *hists, | |
571 | struct addr_location *al, | |
572 | struct symbol *sym_parent, | |
573 | struct branch_info *bi, | |
574 | struct mem_info *mi, | |
575 | struct perf_sample *sample, | |
576 | bool sample_self, | |
577 | struct hist_entry_ops *ops) | |
578 | { | |
579 | struct namespaces *ns = thread__namespaces(al->thread); | |
580 | struct hist_entry entry = { | |
581 | .thread = al->thread, | |
582 | .comm = thread__comm(al->thread), | |
583 | .cgroup_id = { | |
584 | .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0, | |
585 | .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0, | |
586 | }, | |
587 | .ms = { | |
588 | .map = al->map, | |
589 | .sym = al->sym, | |
590 | }, | |
591 | .socket = al->socket, | |
592 | .cpu = al->cpu, | |
593 | .cpumode = al->cpumode, | |
594 | .ip = al->addr, | |
595 | .level = al->level, | |
596 | .stat = { | |
597 | .nr_events = 1, | |
598 | .period = sample->period, | |
599 | .weight = sample->weight, | |
600 | }, | |
601 | .parent = sym_parent, | |
602 | .filtered = symbol__parent_filter(sym_parent) | al->filtered, | |
603 | .hists = hists, | |
604 | .branch_info = bi, | |
605 | .mem_info = mi, | |
606 | .transaction = sample->transaction, | |
607 | .raw_data = sample->raw_data, | |
608 | .raw_size = sample->raw_size, | |
609 | .ops = ops, | |
610 | }; | |
611 | ||
612 | return hists__findnew_entry(hists, &entry, al, sample_self); | |
613 | } | |
614 | ||
615 | struct hist_entry *hists__add_entry(struct hists *hists, | |
616 | struct addr_location *al, | |
617 | struct symbol *sym_parent, | |
618 | struct branch_info *bi, | |
619 | struct mem_info *mi, | |
620 | struct perf_sample *sample, | |
621 | bool sample_self) | |
622 | { | |
623 | return __hists__add_entry(hists, al, sym_parent, bi, mi, | |
624 | sample, sample_self, NULL); | |
625 | } | |
626 | ||
627 | struct hist_entry *hists__add_entry_ops(struct hists *hists, | |
628 | struct hist_entry_ops *ops, | |
629 | struct addr_location *al, | |
630 | struct symbol *sym_parent, | |
631 | struct branch_info *bi, | |
632 | struct mem_info *mi, | |
633 | struct perf_sample *sample, | |
634 | bool sample_self) | |
635 | { | |
636 | return __hists__add_entry(hists, al, sym_parent, bi, mi, | |
637 | sample, sample_self, ops); | |
638 | } | |
639 | ||
640 | static int | |
641 | iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, | |
642 | struct addr_location *al __maybe_unused) | |
643 | { | |
644 | return 0; | |
645 | } | |
646 | ||
647 | static int | |
648 | iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, | |
649 | struct addr_location *al __maybe_unused) | |
650 | { | |
651 | return 0; | |
652 | } | |
653 | ||
654 | static int | |
655 | iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
656 | { | |
657 | struct perf_sample *sample = iter->sample; | |
658 | struct mem_info *mi; | |
659 | ||
660 | mi = sample__resolve_mem(sample, al); | |
661 | if (mi == NULL) | |
662 | return -ENOMEM; | |
663 | ||
664 | iter->priv = mi; | |
665 | return 0; | |
666 | } | |
667 | ||
668 | static int | |
669 | iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
670 | { | |
671 | u64 cost; | |
672 | struct mem_info *mi = iter->priv; | |
673 | struct hists *hists = evsel__hists(iter->evsel); | |
674 | struct perf_sample *sample = iter->sample; | |
675 | struct hist_entry *he; | |
676 | ||
677 | if (mi == NULL) | |
678 | return -EINVAL; | |
679 | ||
680 | cost = sample->weight; | |
681 | if (!cost) | |
682 | cost = 1; | |
683 | ||
684 | /* | |
685 | * must pass period=weight in order to get the correct | |
686 | * sorting from hists__collapse_resort() which is solely | |
687 | * based on periods. We want sorting be done on nr_events * weight | |
688 | * and this is indirectly achieved by passing period=weight here | |
689 | * and the he_stat__add_period() function. | |
690 | */ | |
691 | sample->period = cost; | |
692 | ||
693 | he = hists__add_entry(hists, al, iter->parent, NULL, mi, | |
694 | sample, true); | |
695 | if (!he) | |
696 | return -ENOMEM; | |
697 | ||
698 | iter->he = he; | |
699 | return 0; | |
700 | } | |
701 | ||
702 | static int | |
703 | iter_finish_mem_entry(struct hist_entry_iter *iter, | |
704 | struct addr_location *al __maybe_unused) | |
705 | { | |
706 | struct perf_evsel *evsel = iter->evsel; | |
707 | struct hists *hists = evsel__hists(evsel); | |
708 | struct hist_entry *he = iter->he; | |
709 | int err = -EINVAL; | |
710 | ||
711 | if (he == NULL) | |
712 | goto out; | |
713 | ||
714 | hists__inc_nr_samples(hists, he->filtered); | |
715 | ||
716 | err = hist_entry__append_callchain(he, iter->sample); | |
717 | ||
718 | out: | |
719 | /* | |
720 | * We don't need to free iter->priv (mem_info) here since the mem info | |
721 | * was either already freed in hists__findnew_entry() or passed to a | |
722 | * new hist entry by hist_entry__new(). | |
723 | */ | |
724 | iter->priv = NULL; | |
725 | ||
726 | iter->he = NULL; | |
727 | return err; | |
728 | } | |
729 | ||
730 | static int | |
731 | iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
732 | { | |
733 | struct branch_info *bi; | |
734 | struct perf_sample *sample = iter->sample; | |
735 | ||
736 | bi = sample__resolve_bstack(sample, al); | |
737 | if (!bi) | |
738 | return -ENOMEM; | |
739 | ||
740 | iter->curr = 0; | |
741 | iter->total = sample->branch_stack->nr; | |
742 | ||
743 | iter->priv = bi; | |
744 | return 0; | |
745 | } | |
746 | ||
747 | static int | |
748 | iter_add_single_branch_entry(struct hist_entry_iter *iter, | |
749 | struct addr_location *al __maybe_unused) | |
750 | { | |
751 | /* to avoid calling callback function */ | |
752 | iter->he = NULL; | |
753 | ||
754 | return 0; | |
755 | } | |
756 | ||
757 | static int | |
758 | iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
759 | { | |
760 | struct branch_info *bi = iter->priv; | |
761 | int i = iter->curr; | |
762 | ||
763 | if (bi == NULL) | |
764 | return 0; | |
765 | ||
766 | if (iter->curr >= iter->total) | |
767 | return 0; | |
768 | ||
769 | al->map = bi[i].to.map; | |
770 | al->sym = bi[i].to.sym; | |
771 | al->addr = bi[i].to.addr; | |
772 | return 1; | |
773 | } | |
774 | ||
775 | static int | |
776 | iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
777 | { | |
778 | struct branch_info *bi; | |
779 | struct perf_evsel *evsel = iter->evsel; | |
780 | struct hists *hists = evsel__hists(evsel); | |
781 | struct perf_sample *sample = iter->sample; | |
782 | struct hist_entry *he = NULL; | |
783 | int i = iter->curr; | |
784 | int err = 0; | |
785 | ||
786 | bi = iter->priv; | |
787 | ||
788 | if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym)) | |
789 | goto out; | |
790 | ||
791 | /* | |
792 | * The report shows the percentage of total branches captured | |
793 | * and not events sampled. Thus we use a pseudo period of 1. | |
794 | */ | |
795 | sample->period = 1; | |
796 | sample->weight = bi->flags.cycles ? bi->flags.cycles : 1; | |
797 | ||
798 | he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL, | |
799 | sample, true); | |
800 | if (he == NULL) | |
801 | return -ENOMEM; | |
802 | ||
803 | hists__inc_nr_samples(hists, he->filtered); | |
804 | ||
805 | out: | |
806 | iter->he = he; | |
807 | iter->curr++; | |
808 | return err; | |
809 | } | |
810 | ||
811 | static int | |
812 | iter_finish_branch_entry(struct hist_entry_iter *iter, | |
813 | struct addr_location *al __maybe_unused) | |
814 | { | |
815 | zfree(&iter->priv); | |
816 | iter->he = NULL; | |
817 | ||
818 | return iter->curr >= iter->total ? 0 : -1; | |
819 | } | |
820 | ||
821 | static int | |
822 | iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused, | |
823 | struct addr_location *al __maybe_unused) | |
824 | { | |
825 | return 0; | |
826 | } | |
827 | ||
828 | static int | |
829 | iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
830 | { | |
831 | struct perf_evsel *evsel = iter->evsel; | |
832 | struct perf_sample *sample = iter->sample; | |
833 | struct hist_entry *he; | |
834 | ||
835 | he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, | |
836 | sample, true); | |
837 | if (he == NULL) | |
838 | return -ENOMEM; | |
839 | ||
840 | iter->he = he; | |
841 | return 0; | |
842 | } | |
843 | ||
844 | static int | |
845 | iter_finish_normal_entry(struct hist_entry_iter *iter, | |
846 | struct addr_location *al __maybe_unused) | |
847 | { | |
848 | struct hist_entry *he = iter->he; | |
849 | struct perf_evsel *evsel = iter->evsel; | |
850 | struct perf_sample *sample = iter->sample; | |
851 | ||
852 | if (he == NULL) | |
853 | return 0; | |
854 | ||
855 | iter->he = NULL; | |
856 | ||
857 | hists__inc_nr_samples(evsel__hists(evsel), he->filtered); | |
858 | ||
859 | return hist_entry__append_callchain(he, sample); | |
860 | } | |
861 | ||
862 | static int | |
863 | iter_prepare_cumulative_entry(struct hist_entry_iter *iter, | |
864 | struct addr_location *al __maybe_unused) | |
865 | { | |
866 | struct hist_entry **he_cache; | |
867 | ||
868 | callchain_cursor_commit(&callchain_cursor); | |
869 | ||
870 | /* | |
871 | * This is for detecting cycles or recursions so that they're | |
872 | * cumulated only one time to prevent entries more than 100% | |
873 | * overhead. | |
874 | */ | |
875 | he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1)); | |
876 | if (he_cache == NULL) | |
877 | return -ENOMEM; | |
878 | ||
879 | iter->priv = he_cache; | |
880 | iter->curr = 0; | |
881 | ||
882 | return 0; | |
883 | } | |
884 | ||
885 | static int | |
886 | iter_add_single_cumulative_entry(struct hist_entry_iter *iter, | |
887 | struct addr_location *al) | |
888 | { | |
889 | struct perf_evsel *evsel = iter->evsel; | |
890 | struct hists *hists = evsel__hists(evsel); | |
891 | struct perf_sample *sample = iter->sample; | |
892 | struct hist_entry **he_cache = iter->priv; | |
893 | struct hist_entry *he; | |
894 | int err = 0; | |
895 | ||
896 | he = hists__add_entry(hists, al, iter->parent, NULL, NULL, | |
897 | sample, true); | |
898 | if (he == NULL) | |
899 | return -ENOMEM; | |
900 | ||
901 | iter->he = he; | |
902 | he_cache[iter->curr++] = he; | |
903 | ||
904 | hist_entry__append_callchain(he, sample); | |
905 | ||
906 | /* | |
907 | * We need to re-initialize the cursor since callchain_append() | |
908 | * advanced the cursor to the end. | |
909 | */ | |
910 | callchain_cursor_commit(&callchain_cursor); | |
911 | ||
912 | hists__inc_nr_samples(hists, he->filtered); | |
913 | ||
914 | return err; | |
915 | } | |
916 | ||
917 | static int | |
918 | iter_next_cumulative_entry(struct hist_entry_iter *iter, | |
919 | struct addr_location *al) | |
920 | { | |
921 | struct callchain_cursor_node *node; | |
922 | ||
923 | node = callchain_cursor_current(&callchain_cursor); | |
924 | if (node == NULL) | |
925 | return 0; | |
926 | ||
927 | return fill_callchain_info(al, node, iter->hide_unresolved); | |
928 | } | |
929 | ||
930 | static int | |
931 | iter_add_next_cumulative_entry(struct hist_entry_iter *iter, | |
932 | struct addr_location *al) | |
933 | { | |
934 | struct perf_evsel *evsel = iter->evsel; | |
935 | struct perf_sample *sample = iter->sample; | |
936 | struct hist_entry **he_cache = iter->priv; | |
937 | struct hist_entry *he; | |
938 | struct hist_entry he_tmp = { | |
939 | .hists = evsel__hists(evsel), | |
940 | .cpu = al->cpu, | |
941 | .thread = al->thread, | |
942 | .comm = thread__comm(al->thread), | |
943 | .ip = al->addr, | |
944 | .ms = { | |
945 | .map = al->map, | |
946 | .sym = al->sym, | |
947 | }, | |
948 | .parent = iter->parent, | |
949 | .raw_data = sample->raw_data, | |
950 | .raw_size = sample->raw_size, | |
951 | }; | |
952 | int i; | |
953 | struct callchain_cursor cursor; | |
954 | ||
955 | callchain_cursor_snapshot(&cursor, &callchain_cursor); | |
956 | ||
957 | callchain_cursor_advance(&callchain_cursor); | |
958 | ||
959 | /* | |
960 | * Check if there's duplicate entries in the callchain. | |
961 | * It's possible that it has cycles or recursive calls. | |
962 | */ | |
963 | for (i = 0; i < iter->curr; i++) { | |
964 | if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) { | |
965 | /* to avoid calling callback function */ | |
966 | iter->he = NULL; | |
967 | return 0; | |
968 | } | |
969 | } | |
970 | ||
971 | he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, | |
972 | sample, false); | |
973 | if (he == NULL) | |
974 | return -ENOMEM; | |
975 | ||
976 | iter->he = he; | |
977 | he_cache[iter->curr++] = he; | |
978 | ||
979 | if (symbol_conf.use_callchain) | |
980 | callchain_append(he->callchain, &cursor, sample->period); | |
981 | return 0; | |
982 | } | |
983 | ||
984 | static int | |
985 | iter_finish_cumulative_entry(struct hist_entry_iter *iter, | |
986 | struct addr_location *al __maybe_unused) | |
987 | { | |
988 | zfree(&iter->priv); | |
989 | iter->he = NULL; | |
990 | ||
991 | return 0; | |
992 | } | |
993 | ||
994 | const struct hist_iter_ops hist_iter_mem = { | |
995 | .prepare_entry = iter_prepare_mem_entry, | |
996 | .add_single_entry = iter_add_single_mem_entry, | |
997 | .next_entry = iter_next_nop_entry, | |
998 | .add_next_entry = iter_add_next_nop_entry, | |
999 | .finish_entry = iter_finish_mem_entry, | |
1000 | }; | |
1001 | ||
1002 | const struct hist_iter_ops hist_iter_branch = { | |
1003 | .prepare_entry = iter_prepare_branch_entry, | |
1004 | .add_single_entry = iter_add_single_branch_entry, | |
1005 | .next_entry = iter_next_branch_entry, | |
1006 | .add_next_entry = iter_add_next_branch_entry, | |
1007 | .finish_entry = iter_finish_branch_entry, | |
1008 | }; | |
1009 | ||
1010 | const struct hist_iter_ops hist_iter_normal = { | |
1011 | .prepare_entry = iter_prepare_normal_entry, | |
1012 | .add_single_entry = iter_add_single_normal_entry, | |
1013 | .next_entry = iter_next_nop_entry, | |
1014 | .add_next_entry = iter_add_next_nop_entry, | |
1015 | .finish_entry = iter_finish_normal_entry, | |
1016 | }; | |
1017 | ||
1018 | const struct hist_iter_ops hist_iter_cumulative = { | |
1019 | .prepare_entry = iter_prepare_cumulative_entry, | |
1020 | .add_single_entry = iter_add_single_cumulative_entry, | |
1021 | .next_entry = iter_next_cumulative_entry, | |
1022 | .add_next_entry = iter_add_next_cumulative_entry, | |
1023 | .finish_entry = iter_finish_cumulative_entry, | |
1024 | }; | |
1025 | ||
1026 | int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al, | |
1027 | int max_stack_depth, void *arg) | |
1028 | { | |
1029 | int err, err2; | |
1030 | struct map *alm = NULL; | |
1031 | ||
1032 | if (al && al->map) | |
1033 | alm = map__get(al->map); | |
1034 | ||
1035 | err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent, | |
1036 | iter->evsel, al, max_stack_depth); | |
1037 | if (err) | |
1038 | return err; | |
1039 | ||
1040 | iter->max_stack = max_stack_depth; | |
1041 | ||
1042 | err = iter->ops->prepare_entry(iter, al); | |
1043 | if (err) | |
1044 | goto out; | |
1045 | ||
1046 | err = iter->ops->add_single_entry(iter, al); | |
1047 | if (err) | |
1048 | goto out; | |
1049 | ||
1050 | if (iter->he && iter->add_entry_cb) { | |
1051 | err = iter->add_entry_cb(iter, al, true, arg); | |
1052 | if (err) | |
1053 | goto out; | |
1054 | } | |
1055 | ||
1056 | while (iter->ops->next_entry(iter, al)) { | |
1057 | err = iter->ops->add_next_entry(iter, al); | |
1058 | if (err) | |
1059 | break; | |
1060 | ||
1061 | if (iter->he && iter->add_entry_cb) { | |
1062 | err = iter->add_entry_cb(iter, al, false, arg); | |
1063 | if (err) | |
1064 | goto out; | |
1065 | } | |
1066 | } | |
1067 | ||
1068 | out: | |
1069 | err2 = iter->ops->finish_entry(iter, al); | |
1070 | if (!err) | |
1071 | err = err2; | |
1072 | ||
1073 | map__put(alm); | |
1074 | ||
1075 | return err; | |
1076 | } | |
1077 | ||
1078 | int64_t | |
1079 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
1080 | { | |
1081 | struct hists *hists = left->hists; | |
1082 | struct perf_hpp_fmt *fmt; | |
1083 | int64_t cmp = 0; | |
1084 | ||
1085 | hists__for_each_sort_list(hists, fmt) { | |
1086 | if (perf_hpp__is_dynamic_entry(fmt) && | |
1087 | !perf_hpp__defined_dynamic_entry(fmt, hists)) | |
1088 | continue; | |
1089 | ||
1090 | cmp = fmt->cmp(fmt, left, right); | |
1091 | if (cmp) | |
1092 | break; | |
1093 | } | |
1094 | ||
1095 | return cmp; | |
1096 | } | |
1097 | ||
1098 | int64_t | |
1099 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | |
1100 | { | |
1101 | struct hists *hists = left->hists; | |
1102 | struct perf_hpp_fmt *fmt; | |
1103 | int64_t cmp = 0; | |
1104 | ||
1105 | hists__for_each_sort_list(hists, fmt) { | |
1106 | if (perf_hpp__is_dynamic_entry(fmt) && | |
1107 | !perf_hpp__defined_dynamic_entry(fmt, hists)) | |
1108 | continue; | |
1109 | ||
1110 | cmp = fmt->collapse(fmt, left, right); | |
1111 | if (cmp) | |
1112 | break; | |
1113 | } | |
1114 | ||
1115 | return cmp; | |
1116 | } | |
1117 | ||
1118 | void hist_entry__delete(struct hist_entry *he) | |
1119 | { | |
1120 | struct hist_entry_ops *ops = he->ops; | |
1121 | ||
1122 | thread__zput(he->thread); | |
1123 | map__zput(he->ms.map); | |
1124 | ||
1125 | if (he->branch_info) { | |
1126 | map__zput(he->branch_info->from.map); | |
1127 | map__zput(he->branch_info->to.map); | |
1128 | free_srcline(he->branch_info->srcline_from); | |
1129 | free_srcline(he->branch_info->srcline_to); | |
1130 | zfree(&he->branch_info); | |
1131 | } | |
1132 | ||
1133 | if (he->mem_info) { | |
1134 | map__zput(he->mem_info->iaddr.map); | |
1135 | map__zput(he->mem_info->daddr.map); | |
1136 | zfree(&he->mem_info); | |
1137 | } | |
1138 | ||
1139 | if (he->inline_node) { | |
1140 | inline_node__delete(he->inline_node); | |
1141 | he->inline_node = NULL; | |
1142 | } | |
1143 | ||
1144 | zfree(&he->stat_acc); | |
1145 | free_srcline(he->srcline); | |
1146 | if (he->srcfile && he->srcfile[0]) | |
1147 | free(he->srcfile); | |
1148 | free_callchain(he->callchain); | |
1149 | free(he->trace_output); | |
1150 | free(he->raw_data); | |
1151 | ops->free(he); | |
1152 | } | |
1153 | ||
1154 | /* | |
1155 | * If this is not the last column, then we need to pad it according to the | |
1156 | * pre-calculated max lenght for this column, otherwise don't bother adding | |
1157 | * spaces because that would break viewing this with, for instance, 'less', | |
1158 | * that would show tons of trailing spaces when a long C++ demangled method | |
1159 | * names is sampled. | |
1160 | */ | |
1161 | int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp, | |
1162 | struct perf_hpp_fmt *fmt, int printed) | |
1163 | { | |
1164 | if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) { | |
1165 | const int width = fmt->width(fmt, hpp, he->hists); | |
1166 | if (printed < width) { | |
1167 | advance_hpp(hpp, printed); | |
1168 | printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " "); | |
1169 | } | |
1170 | } | |
1171 | ||
1172 | return printed; | |
1173 | } | |
1174 | ||
1175 | /* | |
1176 | * collapse the histogram | |
1177 | */ | |
1178 | ||
1179 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he); | |
1180 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he, | |
1181 | enum hist_filter type); | |
1182 | ||
1183 | typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt); | |
1184 | ||
1185 | static bool check_thread_entry(struct perf_hpp_fmt *fmt) | |
1186 | { | |
1187 | return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt); | |
1188 | } | |
1189 | ||
1190 | static void hist_entry__check_and_remove_filter(struct hist_entry *he, | |
1191 | enum hist_filter type, | |
1192 | fmt_chk_fn check) | |
1193 | { | |
1194 | struct perf_hpp_fmt *fmt; | |
1195 | bool type_match = false; | |
1196 | struct hist_entry *parent = he->parent_he; | |
1197 | ||
1198 | switch (type) { | |
1199 | case HIST_FILTER__THREAD: | |
1200 | if (symbol_conf.comm_list == NULL && | |
1201 | symbol_conf.pid_list == NULL && | |
1202 | symbol_conf.tid_list == NULL) | |
1203 | return; | |
1204 | break; | |
1205 | case HIST_FILTER__DSO: | |
1206 | if (symbol_conf.dso_list == NULL) | |
1207 | return; | |
1208 | break; | |
1209 | case HIST_FILTER__SYMBOL: | |
1210 | if (symbol_conf.sym_list == NULL) | |
1211 | return; | |
1212 | break; | |
1213 | case HIST_FILTER__PARENT: | |
1214 | case HIST_FILTER__GUEST: | |
1215 | case HIST_FILTER__HOST: | |
1216 | case HIST_FILTER__SOCKET: | |
1217 | case HIST_FILTER__C2C: | |
1218 | default: | |
1219 | return; | |
1220 | } | |
1221 | ||
1222 | /* if it's filtered by own fmt, it has to have filter bits */ | |
1223 | perf_hpp_list__for_each_format(he->hpp_list, fmt) { | |
1224 | if (check(fmt)) { | |
1225 | type_match = true; | |
1226 | break; | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | if (type_match) { | |
1231 | /* | |
1232 | * If the filter is for current level entry, propagate | |
1233 | * filter marker to parents. The marker bit was | |
1234 | * already set by default so it only needs to clear | |
1235 | * non-filtered entries. | |
1236 | */ | |
1237 | if (!(he->filtered & (1 << type))) { | |
1238 | while (parent) { | |
1239 | parent->filtered &= ~(1 << type); | |
1240 | parent = parent->parent_he; | |
1241 | } | |
1242 | } | |
1243 | } else { | |
1244 | /* | |
1245 | * If current entry doesn't have matching formats, set | |
1246 | * filter marker for upper level entries. it will be | |
1247 | * cleared if its lower level entries is not filtered. | |
1248 | * | |
1249 | * For lower-level entries, it inherits parent's | |
1250 | * filter bit so that lower level entries of a | |
1251 | * non-filtered entry won't set the filter marker. | |
1252 | */ | |
1253 | if (parent == NULL) | |
1254 | he->filtered |= (1 << type); | |
1255 | else | |
1256 | he->filtered |= (parent->filtered & (1 << type)); | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | static void hist_entry__apply_hierarchy_filters(struct hist_entry *he) | |
1261 | { | |
1262 | hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD, | |
1263 | check_thread_entry); | |
1264 | ||
1265 | hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO, | |
1266 | perf_hpp__is_dso_entry); | |
1267 | ||
1268 | hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL, | |
1269 | perf_hpp__is_sym_entry); | |
1270 | ||
1271 | hists__apply_filters(he->hists, he); | |
1272 | } | |
1273 | ||
1274 | static struct hist_entry *hierarchy_insert_entry(struct hists *hists, | |
1275 | struct rb_root *root, | |
1276 | struct hist_entry *he, | |
1277 | struct hist_entry *parent_he, | |
1278 | struct perf_hpp_list *hpp_list) | |
1279 | { | |
1280 | struct rb_node **p = &root->rb_node; | |
1281 | struct rb_node *parent = NULL; | |
1282 | struct hist_entry *iter, *new; | |
1283 | struct perf_hpp_fmt *fmt; | |
1284 | int64_t cmp; | |
1285 | ||
1286 | while (*p != NULL) { | |
1287 | parent = *p; | |
1288 | iter = rb_entry(parent, struct hist_entry, rb_node_in); | |
1289 | ||
1290 | cmp = 0; | |
1291 | perf_hpp_list__for_each_sort_list(hpp_list, fmt) { | |
1292 | cmp = fmt->collapse(fmt, iter, he); | |
1293 | if (cmp) | |
1294 | break; | |
1295 | } | |
1296 | ||
1297 | if (!cmp) { | |
1298 | he_stat__add_stat(&iter->stat, &he->stat); | |
1299 | return iter; | |
1300 | } | |
1301 | ||
1302 | if (cmp < 0) | |
1303 | p = &parent->rb_left; | |
1304 | else | |
1305 | p = &parent->rb_right; | |
1306 | } | |
1307 | ||
1308 | new = hist_entry__new(he, true); | |
1309 | if (new == NULL) | |
1310 | return NULL; | |
1311 | ||
1312 | hists->nr_entries++; | |
1313 | ||
1314 | /* save related format list for output */ | |
1315 | new->hpp_list = hpp_list; | |
1316 | new->parent_he = parent_he; | |
1317 | ||
1318 | hist_entry__apply_hierarchy_filters(new); | |
1319 | ||
1320 | /* some fields are now passed to 'new' */ | |
1321 | perf_hpp_list__for_each_sort_list(hpp_list, fmt) { | |
1322 | if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt)) | |
1323 | he->trace_output = NULL; | |
1324 | else | |
1325 | new->trace_output = NULL; | |
1326 | ||
1327 | if (perf_hpp__is_srcline_entry(fmt)) | |
1328 | he->srcline = NULL; | |
1329 | else | |
1330 | new->srcline = NULL; | |
1331 | ||
1332 | if (perf_hpp__is_srcfile_entry(fmt)) | |
1333 | he->srcfile = NULL; | |
1334 | else | |
1335 | new->srcfile = NULL; | |
1336 | } | |
1337 | ||
1338 | rb_link_node(&new->rb_node_in, parent, p); | |
1339 | rb_insert_color(&new->rb_node_in, root); | |
1340 | return new; | |
1341 | } | |
1342 | ||
1343 | static int hists__hierarchy_insert_entry(struct hists *hists, | |
1344 | struct rb_root *root, | |
1345 | struct hist_entry *he) | |
1346 | { | |
1347 | struct perf_hpp_list_node *node; | |
1348 | struct hist_entry *new_he = NULL; | |
1349 | struct hist_entry *parent = NULL; | |
1350 | int depth = 0; | |
1351 | int ret = 0; | |
1352 | ||
1353 | list_for_each_entry(node, &hists->hpp_formats, list) { | |
1354 | /* skip period (overhead) and elided columns */ | |
1355 | if (node->level == 0 || node->skip) | |
1356 | continue; | |
1357 | ||
1358 | /* insert copy of 'he' for each fmt into the hierarchy */ | |
1359 | new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp); | |
1360 | if (new_he == NULL) { | |
1361 | ret = -1; | |
1362 | break; | |
1363 | } | |
1364 | ||
1365 | root = &new_he->hroot_in; | |
1366 | new_he->depth = depth++; | |
1367 | parent = new_he; | |
1368 | } | |
1369 | ||
1370 | if (new_he) { | |
1371 | new_he->leaf = true; | |
1372 | ||
1373 | if (symbol_conf.use_callchain) { | |
1374 | callchain_cursor_reset(&callchain_cursor); | |
1375 | if (callchain_merge(&callchain_cursor, | |
1376 | new_he->callchain, | |
1377 | he->callchain) < 0) | |
1378 | ret = -1; | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | /* 'he' is no longer used */ | |
1383 | hist_entry__delete(he); | |
1384 | ||
1385 | /* return 0 (or -1) since it already applied filters */ | |
1386 | return ret; | |
1387 | } | |
1388 | ||
1389 | static int hists__collapse_insert_entry(struct hists *hists, | |
1390 | struct rb_root *root, | |
1391 | struct hist_entry *he) | |
1392 | { | |
1393 | struct rb_node **p = &root->rb_node; | |
1394 | struct rb_node *parent = NULL; | |
1395 | struct hist_entry *iter; | |
1396 | int64_t cmp; | |
1397 | ||
1398 | if (symbol_conf.report_hierarchy) | |
1399 | return hists__hierarchy_insert_entry(hists, root, he); | |
1400 | ||
1401 | while (*p != NULL) { | |
1402 | parent = *p; | |
1403 | iter = rb_entry(parent, struct hist_entry, rb_node_in); | |
1404 | ||
1405 | cmp = hist_entry__collapse(iter, he); | |
1406 | ||
1407 | if (!cmp) { | |
1408 | int ret = 0; | |
1409 | ||
1410 | he_stat__add_stat(&iter->stat, &he->stat); | |
1411 | if (symbol_conf.cumulate_callchain) | |
1412 | he_stat__add_stat(iter->stat_acc, he->stat_acc); | |
1413 | ||
1414 | if (symbol_conf.use_callchain) { | |
1415 | callchain_cursor_reset(&callchain_cursor); | |
1416 | if (callchain_merge(&callchain_cursor, | |
1417 | iter->callchain, | |
1418 | he->callchain) < 0) | |
1419 | ret = -1; | |
1420 | } | |
1421 | hist_entry__delete(he); | |
1422 | return ret; | |
1423 | } | |
1424 | ||
1425 | if (cmp < 0) | |
1426 | p = &(*p)->rb_left; | |
1427 | else | |
1428 | p = &(*p)->rb_right; | |
1429 | } | |
1430 | hists->nr_entries++; | |
1431 | ||
1432 | rb_link_node(&he->rb_node_in, parent, p); | |
1433 | rb_insert_color(&he->rb_node_in, root); | |
1434 | return 1; | |
1435 | } | |
1436 | ||
1437 | struct rb_root *hists__get_rotate_entries_in(struct hists *hists) | |
1438 | { | |
1439 | struct rb_root *root; | |
1440 | ||
1441 | pthread_mutex_lock(&hists->lock); | |
1442 | ||
1443 | root = hists->entries_in; | |
1444 | if (++hists->entries_in > &hists->entries_in_array[1]) | |
1445 | hists->entries_in = &hists->entries_in_array[0]; | |
1446 | ||
1447 | pthread_mutex_unlock(&hists->lock); | |
1448 | ||
1449 | return root; | |
1450 | } | |
1451 | ||
1452 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) | |
1453 | { | |
1454 | hists__filter_entry_by_dso(hists, he); | |
1455 | hists__filter_entry_by_thread(hists, he); | |
1456 | hists__filter_entry_by_symbol(hists, he); | |
1457 | hists__filter_entry_by_socket(hists, he); | |
1458 | } | |
1459 | ||
1460 | int hists__collapse_resort(struct hists *hists, struct ui_progress *prog) | |
1461 | { | |
1462 | struct rb_root *root; | |
1463 | struct rb_node *next; | |
1464 | struct hist_entry *n; | |
1465 | int ret; | |
1466 | ||
1467 | if (!hists__has(hists, need_collapse)) | |
1468 | return 0; | |
1469 | ||
1470 | hists->nr_entries = 0; | |
1471 | ||
1472 | root = hists__get_rotate_entries_in(hists); | |
1473 | ||
1474 | next = rb_first(root); | |
1475 | ||
1476 | while (next) { | |
1477 | if (session_done()) | |
1478 | break; | |
1479 | n = rb_entry(next, struct hist_entry, rb_node_in); | |
1480 | next = rb_next(&n->rb_node_in); | |
1481 | ||
1482 | rb_erase(&n->rb_node_in, root); | |
1483 | ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n); | |
1484 | if (ret < 0) | |
1485 | return -1; | |
1486 | ||
1487 | if (ret) { | |
1488 | /* | |
1489 | * If it wasn't combined with one of the entries already | |
1490 | * collapsed, we need to apply the filters that may have | |
1491 | * been set by, say, the hist_browser. | |
1492 | */ | |
1493 | hists__apply_filters(hists, n); | |
1494 | } | |
1495 | if (prog) | |
1496 | ui_progress__update(prog, 1); | |
1497 | } | |
1498 | return 0; | |
1499 | } | |
1500 | ||
1501 | static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b) | |
1502 | { | |
1503 | struct hists *hists = a->hists; | |
1504 | struct perf_hpp_fmt *fmt; | |
1505 | int64_t cmp = 0; | |
1506 | ||
1507 | hists__for_each_sort_list(hists, fmt) { | |
1508 | if (perf_hpp__should_skip(fmt, a->hists)) | |
1509 | continue; | |
1510 | ||
1511 | cmp = fmt->sort(fmt, a, b); | |
1512 | if (cmp) | |
1513 | break; | |
1514 | } | |
1515 | ||
1516 | return cmp; | |
1517 | } | |
1518 | ||
1519 | static void hists__reset_filter_stats(struct hists *hists) | |
1520 | { | |
1521 | hists->nr_non_filtered_entries = 0; | |
1522 | hists->stats.total_non_filtered_period = 0; | |
1523 | } | |
1524 | ||
1525 | void hists__reset_stats(struct hists *hists) | |
1526 | { | |
1527 | hists->nr_entries = 0; | |
1528 | hists->stats.total_period = 0; | |
1529 | ||
1530 | hists__reset_filter_stats(hists); | |
1531 | } | |
1532 | ||
1533 | static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h) | |
1534 | { | |
1535 | hists->nr_non_filtered_entries++; | |
1536 | hists->stats.total_non_filtered_period += h->stat.period; | |
1537 | } | |
1538 | ||
1539 | void hists__inc_stats(struct hists *hists, struct hist_entry *h) | |
1540 | { | |
1541 | if (!h->filtered) | |
1542 | hists__inc_filter_stats(hists, h); | |
1543 | ||
1544 | hists->nr_entries++; | |
1545 | hists->stats.total_period += h->stat.period; | |
1546 | } | |
1547 | ||
1548 | static void hierarchy_recalc_total_periods(struct hists *hists) | |
1549 | { | |
1550 | struct rb_node *node; | |
1551 | struct hist_entry *he; | |
1552 | ||
1553 | node = rb_first(&hists->entries); | |
1554 | ||
1555 | hists->stats.total_period = 0; | |
1556 | hists->stats.total_non_filtered_period = 0; | |
1557 | ||
1558 | /* | |
1559 | * recalculate total period using top-level entries only | |
1560 | * since lower level entries only see non-filtered entries | |
1561 | * but upper level entries have sum of both entries. | |
1562 | */ | |
1563 | while (node) { | |
1564 | he = rb_entry(node, struct hist_entry, rb_node); | |
1565 | node = rb_next(node); | |
1566 | ||
1567 | hists->stats.total_period += he->stat.period; | |
1568 | if (!he->filtered) | |
1569 | hists->stats.total_non_filtered_period += he->stat.period; | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | static void hierarchy_insert_output_entry(struct rb_root *root, | |
1574 | struct hist_entry *he) | |
1575 | { | |
1576 | struct rb_node **p = &root->rb_node; | |
1577 | struct rb_node *parent = NULL; | |
1578 | struct hist_entry *iter; | |
1579 | struct perf_hpp_fmt *fmt; | |
1580 | ||
1581 | while (*p != NULL) { | |
1582 | parent = *p; | |
1583 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
1584 | ||
1585 | if (hist_entry__sort(he, iter) > 0) | |
1586 | p = &parent->rb_left; | |
1587 | else | |
1588 | p = &parent->rb_right; | |
1589 | } | |
1590 | ||
1591 | rb_link_node(&he->rb_node, parent, p); | |
1592 | rb_insert_color(&he->rb_node, root); | |
1593 | ||
1594 | /* update column width of dynamic entry */ | |
1595 | perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) { | |
1596 | if (perf_hpp__is_dynamic_entry(fmt)) | |
1597 | fmt->sort(fmt, he, NULL); | |
1598 | } | |
1599 | } | |
1600 | ||
1601 | static void hists__hierarchy_output_resort(struct hists *hists, | |
1602 | struct ui_progress *prog, | |
1603 | struct rb_root *root_in, | |
1604 | struct rb_root *root_out, | |
1605 | u64 min_callchain_hits, | |
1606 | bool use_callchain) | |
1607 | { | |
1608 | struct rb_node *node; | |
1609 | struct hist_entry *he; | |
1610 | ||
1611 | *root_out = RB_ROOT; | |
1612 | node = rb_first(root_in); | |
1613 | ||
1614 | while (node) { | |
1615 | he = rb_entry(node, struct hist_entry, rb_node_in); | |
1616 | node = rb_next(node); | |
1617 | ||
1618 | hierarchy_insert_output_entry(root_out, he); | |
1619 | ||
1620 | if (prog) | |
1621 | ui_progress__update(prog, 1); | |
1622 | ||
1623 | hists->nr_entries++; | |
1624 | if (!he->filtered) { | |
1625 | hists->nr_non_filtered_entries++; | |
1626 | hists__calc_col_len(hists, he); | |
1627 | } | |
1628 | ||
1629 | if (!he->leaf) { | |
1630 | hists__hierarchy_output_resort(hists, prog, | |
1631 | &he->hroot_in, | |
1632 | &he->hroot_out, | |
1633 | min_callchain_hits, | |
1634 | use_callchain); | |
1635 | continue; | |
1636 | } | |
1637 | ||
1638 | if (!use_callchain) | |
1639 | continue; | |
1640 | ||
1641 | if (callchain_param.mode == CHAIN_GRAPH_REL) { | |
1642 | u64 total = he->stat.period; | |
1643 | ||
1644 | if (symbol_conf.cumulate_callchain) | |
1645 | total = he->stat_acc->period; | |
1646 | ||
1647 | min_callchain_hits = total * (callchain_param.min_percent / 100); | |
1648 | } | |
1649 | ||
1650 | callchain_param.sort(&he->sorted_chain, he->callchain, | |
1651 | min_callchain_hits, &callchain_param); | |
1652 | } | |
1653 | } | |
1654 | ||
1655 | static void __hists__insert_output_entry(struct rb_root *entries, | |
1656 | struct hist_entry *he, | |
1657 | u64 min_callchain_hits, | |
1658 | bool use_callchain) | |
1659 | { | |
1660 | struct rb_node **p = &entries->rb_node; | |
1661 | struct rb_node *parent = NULL; | |
1662 | struct hist_entry *iter; | |
1663 | struct perf_hpp_fmt *fmt; | |
1664 | ||
1665 | if (use_callchain) { | |
1666 | if (callchain_param.mode == CHAIN_GRAPH_REL) { | |
1667 | u64 total = he->stat.period; | |
1668 | ||
1669 | if (symbol_conf.cumulate_callchain) | |
1670 | total = he->stat_acc->period; | |
1671 | ||
1672 | min_callchain_hits = total * (callchain_param.min_percent / 100); | |
1673 | } | |
1674 | callchain_param.sort(&he->sorted_chain, he->callchain, | |
1675 | min_callchain_hits, &callchain_param); | |
1676 | } | |
1677 | ||
1678 | while (*p != NULL) { | |
1679 | parent = *p; | |
1680 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
1681 | ||
1682 | if (hist_entry__sort(he, iter) > 0) | |
1683 | p = &(*p)->rb_left; | |
1684 | else | |
1685 | p = &(*p)->rb_right; | |
1686 | } | |
1687 | ||
1688 | rb_link_node(&he->rb_node, parent, p); | |
1689 | rb_insert_color(&he->rb_node, entries); | |
1690 | ||
1691 | perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) { | |
1692 | if (perf_hpp__is_dynamic_entry(fmt) && | |
1693 | perf_hpp__defined_dynamic_entry(fmt, he->hists)) | |
1694 | fmt->sort(fmt, he, NULL); /* update column width */ | |
1695 | } | |
1696 | } | |
1697 | ||
1698 | static void output_resort(struct hists *hists, struct ui_progress *prog, | |
1699 | bool use_callchain, hists__resort_cb_t cb) | |
1700 | { | |
1701 | struct rb_root *root; | |
1702 | struct rb_node *next; | |
1703 | struct hist_entry *n; | |
1704 | u64 callchain_total; | |
1705 | u64 min_callchain_hits; | |
1706 | ||
1707 | callchain_total = hists->callchain_period; | |
1708 | if (symbol_conf.filter_relative) | |
1709 | callchain_total = hists->callchain_non_filtered_period; | |
1710 | ||
1711 | min_callchain_hits = callchain_total * (callchain_param.min_percent / 100); | |
1712 | ||
1713 | hists__reset_stats(hists); | |
1714 | hists__reset_col_len(hists); | |
1715 | ||
1716 | if (symbol_conf.report_hierarchy) { | |
1717 | hists__hierarchy_output_resort(hists, prog, | |
1718 | &hists->entries_collapsed, | |
1719 | &hists->entries, | |
1720 | min_callchain_hits, | |
1721 | use_callchain); | |
1722 | hierarchy_recalc_total_periods(hists); | |
1723 | return; | |
1724 | } | |
1725 | ||
1726 | if (hists__has(hists, need_collapse)) | |
1727 | root = &hists->entries_collapsed; | |
1728 | else | |
1729 | root = hists->entries_in; | |
1730 | ||
1731 | next = rb_first(root); | |
1732 | hists->entries = RB_ROOT; | |
1733 | ||
1734 | while (next) { | |
1735 | n = rb_entry(next, struct hist_entry, rb_node_in); | |
1736 | next = rb_next(&n->rb_node_in); | |
1737 | ||
1738 | if (cb && cb(n)) | |
1739 | continue; | |
1740 | ||
1741 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain); | |
1742 | hists__inc_stats(hists, n); | |
1743 | ||
1744 | if (!n->filtered) | |
1745 | hists__calc_col_len(hists, n); | |
1746 | ||
1747 | if (prog) | |
1748 | ui_progress__update(prog, 1); | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog) | |
1753 | { | |
1754 | bool use_callchain; | |
1755 | ||
1756 | if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph) | |
1757 | use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN; | |
1758 | else | |
1759 | use_callchain = symbol_conf.use_callchain; | |
1760 | ||
1761 | output_resort(evsel__hists(evsel), prog, use_callchain, NULL); | |
1762 | } | |
1763 | ||
1764 | void hists__output_resort(struct hists *hists, struct ui_progress *prog) | |
1765 | { | |
1766 | output_resort(hists, prog, symbol_conf.use_callchain, NULL); | |
1767 | } | |
1768 | ||
1769 | void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog, | |
1770 | hists__resort_cb_t cb) | |
1771 | { | |
1772 | output_resort(hists, prog, symbol_conf.use_callchain, cb); | |
1773 | } | |
1774 | ||
1775 | static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd) | |
1776 | { | |
1777 | if (he->leaf || hmd == HMD_FORCE_SIBLING) | |
1778 | return false; | |
1779 | ||
1780 | if (he->unfolded || hmd == HMD_FORCE_CHILD) | |
1781 | return true; | |
1782 | ||
1783 | return false; | |
1784 | } | |
1785 | ||
1786 | struct rb_node *rb_hierarchy_last(struct rb_node *node) | |
1787 | { | |
1788 | struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node); | |
1789 | ||
1790 | while (can_goto_child(he, HMD_NORMAL)) { | |
1791 | node = rb_last(&he->hroot_out); | |
1792 | he = rb_entry(node, struct hist_entry, rb_node); | |
1793 | } | |
1794 | return node; | |
1795 | } | |
1796 | ||
1797 | struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd) | |
1798 | { | |
1799 | struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node); | |
1800 | ||
1801 | if (can_goto_child(he, hmd)) | |
1802 | node = rb_first(&he->hroot_out); | |
1803 | else | |
1804 | node = rb_next(node); | |
1805 | ||
1806 | while (node == NULL) { | |
1807 | he = he->parent_he; | |
1808 | if (he == NULL) | |
1809 | break; | |
1810 | ||
1811 | node = rb_next(&he->rb_node); | |
1812 | } | |
1813 | return node; | |
1814 | } | |
1815 | ||
1816 | struct rb_node *rb_hierarchy_prev(struct rb_node *node) | |
1817 | { | |
1818 | struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node); | |
1819 | ||
1820 | node = rb_prev(node); | |
1821 | if (node) | |
1822 | return rb_hierarchy_last(node); | |
1823 | ||
1824 | he = he->parent_he; | |
1825 | if (he == NULL) | |
1826 | return NULL; | |
1827 | ||
1828 | return &he->rb_node; | |
1829 | } | |
1830 | ||
1831 | bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit) | |
1832 | { | |
1833 | struct rb_node *node; | |
1834 | struct hist_entry *child; | |
1835 | float percent; | |
1836 | ||
1837 | if (he->leaf) | |
1838 | return false; | |
1839 | ||
1840 | node = rb_first(&he->hroot_out); | |
1841 | child = rb_entry(node, struct hist_entry, rb_node); | |
1842 | ||
1843 | while (node && child->filtered) { | |
1844 | node = rb_next(node); | |
1845 | child = rb_entry(node, struct hist_entry, rb_node); | |
1846 | } | |
1847 | ||
1848 | if (node) | |
1849 | percent = hist_entry__get_percent_limit(child); | |
1850 | else | |
1851 | percent = 0; | |
1852 | ||
1853 | return node && percent >= limit; | |
1854 | } | |
1855 | ||
1856 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, | |
1857 | enum hist_filter filter) | |
1858 | { | |
1859 | h->filtered &= ~(1 << filter); | |
1860 | ||
1861 | if (symbol_conf.report_hierarchy) { | |
1862 | struct hist_entry *parent = h->parent_he; | |
1863 | ||
1864 | while (parent) { | |
1865 | he_stat__add_stat(&parent->stat, &h->stat); | |
1866 | ||
1867 | parent->filtered &= ~(1 << filter); | |
1868 | ||
1869 | if (parent->filtered) | |
1870 | goto next; | |
1871 | ||
1872 | /* force fold unfiltered entry for simplicity */ | |
1873 | parent->unfolded = false; | |
1874 | parent->has_no_entry = false; | |
1875 | parent->row_offset = 0; | |
1876 | parent->nr_rows = 0; | |
1877 | next: | |
1878 | parent = parent->parent_he; | |
1879 | } | |
1880 | } | |
1881 | ||
1882 | if (h->filtered) | |
1883 | return; | |
1884 | ||
1885 | /* force fold unfiltered entry for simplicity */ | |
1886 | h->unfolded = false; | |
1887 | h->has_no_entry = false; | |
1888 | h->row_offset = 0; | |
1889 | h->nr_rows = 0; | |
1890 | ||
1891 | hists->stats.nr_non_filtered_samples += h->stat.nr_events; | |
1892 | ||
1893 | hists__inc_filter_stats(hists, h); | |
1894 | hists__calc_col_len(hists, h); | |
1895 | } | |
1896 | ||
1897 | ||
1898 | static bool hists__filter_entry_by_dso(struct hists *hists, | |
1899 | struct hist_entry *he) | |
1900 | { | |
1901 | if (hists->dso_filter != NULL && | |
1902 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { | |
1903 | he->filtered |= (1 << HIST_FILTER__DSO); | |
1904 | return true; | |
1905 | } | |
1906 | ||
1907 | return false; | |
1908 | } | |
1909 | ||
1910 | static bool hists__filter_entry_by_thread(struct hists *hists, | |
1911 | struct hist_entry *he) | |
1912 | { | |
1913 | if (hists->thread_filter != NULL && | |
1914 | he->thread != hists->thread_filter) { | |
1915 | he->filtered |= (1 << HIST_FILTER__THREAD); | |
1916 | return true; | |
1917 | } | |
1918 | ||
1919 | return false; | |
1920 | } | |
1921 | ||
1922 | static bool hists__filter_entry_by_symbol(struct hists *hists, | |
1923 | struct hist_entry *he) | |
1924 | { | |
1925 | if (hists->symbol_filter_str != NULL && | |
1926 | (!he->ms.sym || strstr(he->ms.sym->name, | |
1927 | hists->symbol_filter_str) == NULL)) { | |
1928 | he->filtered |= (1 << HIST_FILTER__SYMBOL); | |
1929 | return true; | |
1930 | } | |
1931 | ||
1932 | return false; | |
1933 | } | |
1934 | ||
1935 | static bool hists__filter_entry_by_socket(struct hists *hists, | |
1936 | struct hist_entry *he) | |
1937 | { | |
1938 | if ((hists->socket_filter > -1) && | |
1939 | (he->socket != hists->socket_filter)) { | |
1940 | he->filtered |= (1 << HIST_FILTER__SOCKET); | |
1941 | return true; | |
1942 | } | |
1943 | ||
1944 | return false; | |
1945 | } | |
1946 | ||
1947 | typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he); | |
1948 | ||
1949 | static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter) | |
1950 | { | |
1951 | struct rb_node *nd; | |
1952 | ||
1953 | hists->stats.nr_non_filtered_samples = 0; | |
1954 | ||
1955 | hists__reset_filter_stats(hists); | |
1956 | hists__reset_col_len(hists); | |
1957 | ||
1958 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { | |
1959 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
1960 | ||
1961 | if (filter(hists, h)) | |
1962 | continue; | |
1963 | ||
1964 | hists__remove_entry_filter(hists, h, type); | |
1965 | } | |
1966 | } | |
1967 | ||
1968 | static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he) | |
1969 | { | |
1970 | struct rb_node **p = &root->rb_node; | |
1971 | struct rb_node *parent = NULL; | |
1972 | struct hist_entry *iter; | |
1973 | struct rb_root new_root = RB_ROOT; | |
1974 | struct rb_node *nd; | |
1975 | ||
1976 | while (*p != NULL) { | |
1977 | parent = *p; | |
1978 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
1979 | ||
1980 | if (hist_entry__sort(he, iter) > 0) | |
1981 | p = &(*p)->rb_left; | |
1982 | else | |
1983 | p = &(*p)->rb_right; | |
1984 | } | |
1985 | ||
1986 | rb_link_node(&he->rb_node, parent, p); | |
1987 | rb_insert_color(&he->rb_node, root); | |
1988 | ||
1989 | if (he->leaf || he->filtered) | |
1990 | return; | |
1991 | ||
1992 | nd = rb_first(&he->hroot_out); | |
1993 | while (nd) { | |
1994 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
1995 | ||
1996 | nd = rb_next(nd); | |
1997 | rb_erase(&h->rb_node, &he->hroot_out); | |
1998 | ||
1999 | resort_filtered_entry(&new_root, h); | |
2000 | } | |
2001 | ||
2002 | he->hroot_out = new_root; | |
2003 | } | |
2004 | ||
2005 | static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg) | |
2006 | { | |
2007 | struct rb_node *nd; | |
2008 | struct rb_root new_root = RB_ROOT; | |
2009 | ||
2010 | hists->stats.nr_non_filtered_samples = 0; | |
2011 | ||
2012 | hists__reset_filter_stats(hists); | |
2013 | hists__reset_col_len(hists); | |
2014 | ||
2015 | nd = rb_first(&hists->entries); | |
2016 | while (nd) { | |
2017 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
2018 | int ret; | |
2019 | ||
2020 | ret = hist_entry__filter(h, type, arg); | |
2021 | ||
2022 | /* | |
2023 | * case 1. non-matching type | |
2024 | * zero out the period, set filter marker and move to child | |
2025 | */ | |
2026 | if (ret < 0) { | |
2027 | memset(&h->stat, 0, sizeof(h->stat)); | |
2028 | h->filtered |= (1 << type); | |
2029 | ||
2030 | nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD); | |
2031 | } | |
2032 | /* | |
2033 | * case 2. matched type (filter out) | |
2034 | * set filter marker and move to next | |
2035 | */ | |
2036 | else if (ret == 1) { | |
2037 | h->filtered |= (1 << type); | |
2038 | ||
2039 | nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING); | |
2040 | } | |
2041 | /* | |
2042 | * case 3. ok (not filtered) | |
2043 | * add period to hists and parents, erase the filter marker | |
2044 | * and move to next sibling | |
2045 | */ | |
2046 | else { | |
2047 | hists__remove_entry_filter(hists, h, type); | |
2048 | ||
2049 | nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING); | |
2050 | } | |
2051 | } | |
2052 | ||
2053 | hierarchy_recalc_total_periods(hists); | |
2054 | ||
2055 | /* | |
2056 | * resort output after applying a new filter since filter in a lower | |
2057 | * hierarchy can change periods in a upper hierarchy. | |
2058 | */ | |
2059 | nd = rb_first(&hists->entries); | |
2060 | while (nd) { | |
2061 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
2062 | ||
2063 | nd = rb_next(nd); | |
2064 | rb_erase(&h->rb_node, &hists->entries); | |
2065 | ||
2066 | resort_filtered_entry(&new_root, h); | |
2067 | } | |
2068 | ||
2069 | hists->entries = new_root; | |
2070 | } | |
2071 | ||
2072 | void hists__filter_by_thread(struct hists *hists) | |
2073 | { | |
2074 | if (symbol_conf.report_hierarchy) | |
2075 | hists__filter_hierarchy(hists, HIST_FILTER__THREAD, | |
2076 | hists->thread_filter); | |
2077 | else | |
2078 | hists__filter_by_type(hists, HIST_FILTER__THREAD, | |
2079 | hists__filter_entry_by_thread); | |
2080 | } | |
2081 | ||
2082 | void hists__filter_by_dso(struct hists *hists) | |
2083 | { | |
2084 | if (symbol_conf.report_hierarchy) | |
2085 | hists__filter_hierarchy(hists, HIST_FILTER__DSO, | |
2086 | hists->dso_filter); | |
2087 | else | |
2088 | hists__filter_by_type(hists, HIST_FILTER__DSO, | |
2089 | hists__filter_entry_by_dso); | |
2090 | } | |
2091 | ||
2092 | void hists__filter_by_symbol(struct hists *hists) | |
2093 | { | |
2094 | if (symbol_conf.report_hierarchy) | |
2095 | hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL, | |
2096 | hists->symbol_filter_str); | |
2097 | else | |
2098 | hists__filter_by_type(hists, HIST_FILTER__SYMBOL, | |
2099 | hists__filter_entry_by_symbol); | |
2100 | } | |
2101 | ||
2102 | void hists__filter_by_socket(struct hists *hists) | |
2103 | { | |
2104 | if (symbol_conf.report_hierarchy) | |
2105 | hists__filter_hierarchy(hists, HIST_FILTER__SOCKET, | |
2106 | &hists->socket_filter); | |
2107 | else | |
2108 | hists__filter_by_type(hists, HIST_FILTER__SOCKET, | |
2109 | hists__filter_entry_by_socket); | |
2110 | } | |
2111 | ||
2112 | void events_stats__inc(struct events_stats *stats, u32 type) | |
2113 | { | |
2114 | ++stats->nr_events[0]; | |
2115 | ++stats->nr_events[type]; | |
2116 | } | |
2117 | ||
2118 | void hists__inc_nr_events(struct hists *hists, u32 type) | |
2119 | { | |
2120 | events_stats__inc(&hists->stats, type); | |
2121 | } | |
2122 | ||
2123 | void hists__inc_nr_samples(struct hists *hists, bool filtered) | |
2124 | { | |
2125 | events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE); | |
2126 | if (!filtered) | |
2127 | hists->stats.nr_non_filtered_samples++; | |
2128 | } | |
2129 | ||
2130 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, | |
2131 | struct hist_entry *pair) | |
2132 | { | |
2133 | struct rb_root *root; | |
2134 | struct rb_node **p; | |
2135 | struct rb_node *parent = NULL; | |
2136 | struct hist_entry *he; | |
2137 | int64_t cmp; | |
2138 | ||
2139 | if (hists__has(hists, need_collapse)) | |
2140 | root = &hists->entries_collapsed; | |
2141 | else | |
2142 | root = hists->entries_in; | |
2143 | ||
2144 | p = &root->rb_node; | |
2145 | ||
2146 | while (*p != NULL) { | |
2147 | parent = *p; | |
2148 | he = rb_entry(parent, struct hist_entry, rb_node_in); | |
2149 | ||
2150 | cmp = hist_entry__collapse(he, pair); | |
2151 | ||
2152 | if (!cmp) | |
2153 | goto out; | |
2154 | ||
2155 | if (cmp < 0) | |
2156 | p = &(*p)->rb_left; | |
2157 | else | |
2158 | p = &(*p)->rb_right; | |
2159 | } | |
2160 | ||
2161 | he = hist_entry__new(pair, true); | |
2162 | if (he) { | |
2163 | memset(&he->stat, 0, sizeof(he->stat)); | |
2164 | he->hists = hists; | |
2165 | if (symbol_conf.cumulate_callchain) | |
2166 | memset(he->stat_acc, 0, sizeof(he->stat)); | |
2167 | rb_link_node(&he->rb_node_in, parent, p); | |
2168 | rb_insert_color(&he->rb_node_in, root); | |
2169 | hists__inc_stats(hists, he); | |
2170 | he->dummy = true; | |
2171 | } | |
2172 | out: | |
2173 | return he; | |
2174 | } | |
2175 | ||
2176 | static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists, | |
2177 | struct rb_root *root, | |
2178 | struct hist_entry *pair) | |
2179 | { | |
2180 | struct rb_node **p; | |
2181 | struct rb_node *parent = NULL; | |
2182 | struct hist_entry *he; | |
2183 | struct perf_hpp_fmt *fmt; | |
2184 | ||
2185 | p = &root->rb_node; | |
2186 | while (*p != NULL) { | |
2187 | int64_t cmp = 0; | |
2188 | ||
2189 | parent = *p; | |
2190 | he = rb_entry(parent, struct hist_entry, rb_node_in); | |
2191 | ||
2192 | perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) { | |
2193 | cmp = fmt->collapse(fmt, he, pair); | |
2194 | if (cmp) | |
2195 | break; | |
2196 | } | |
2197 | if (!cmp) | |
2198 | goto out; | |
2199 | ||
2200 | if (cmp < 0) | |
2201 | p = &parent->rb_left; | |
2202 | else | |
2203 | p = &parent->rb_right; | |
2204 | } | |
2205 | ||
2206 | he = hist_entry__new(pair, true); | |
2207 | if (he) { | |
2208 | rb_link_node(&he->rb_node_in, parent, p); | |
2209 | rb_insert_color(&he->rb_node_in, root); | |
2210 | ||
2211 | he->dummy = true; | |
2212 | he->hists = hists; | |
2213 | memset(&he->stat, 0, sizeof(he->stat)); | |
2214 | hists__inc_stats(hists, he); | |
2215 | } | |
2216 | out: | |
2217 | return he; | |
2218 | } | |
2219 | ||
2220 | static struct hist_entry *hists__find_entry(struct hists *hists, | |
2221 | struct hist_entry *he) | |
2222 | { | |
2223 | struct rb_node *n; | |
2224 | ||
2225 | if (hists__has(hists, need_collapse)) | |
2226 | n = hists->entries_collapsed.rb_node; | |
2227 | else | |
2228 | n = hists->entries_in->rb_node; | |
2229 | ||
2230 | while (n) { | |
2231 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); | |
2232 | int64_t cmp = hist_entry__collapse(iter, he); | |
2233 | ||
2234 | if (cmp < 0) | |
2235 | n = n->rb_left; | |
2236 | else if (cmp > 0) | |
2237 | n = n->rb_right; | |
2238 | else | |
2239 | return iter; | |
2240 | } | |
2241 | ||
2242 | return NULL; | |
2243 | } | |
2244 | ||
2245 | static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root, | |
2246 | struct hist_entry *he) | |
2247 | { | |
2248 | struct rb_node *n = root->rb_node; | |
2249 | ||
2250 | while (n) { | |
2251 | struct hist_entry *iter; | |
2252 | struct perf_hpp_fmt *fmt; | |
2253 | int64_t cmp = 0; | |
2254 | ||
2255 | iter = rb_entry(n, struct hist_entry, rb_node_in); | |
2256 | perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) { | |
2257 | cmp = fmt->collapse(fmt, iter, he); | |
2258 | if (cmp) | |
2259 | break; | |
2260 | } | |
2261 | ||
2262 | if (cmp < 0) | |
2263 | n = n->rb_left; | |
2264 | else if (cmp > 0) | |
2265 | n = n->rb_right; | |
2266 | else | |
2267 | return iter; | |
2268 | } | |
2269 | ||
2270 | return NULL; | |
2271 | } | |
2272 | ||
2273 | static void hists__match_hierarchy(struct rb_root *leader_root, | |
2274 | struct rb_root *other_root) | |
2275 | { | |
2276 | struct rb_node *nd; | |
2277 | struct hist_entry *pos, *pair; | |
2278 | ||
2279 | for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) { | |
2280 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
2281 | pair = hists__find_hierarchy_entry(other_root, pos); | |
2282 | ||
2283 | if (pair) { | |
2284 | hist_entry__add_pair(pair, pos); | |
2285 | hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in); | |
2286 | } | |
2287 | } | |
2288 | } | |
2289 | ||
2290 | /* | |
2291 | * Look for pairs to link to the leader buckets (hist_entries): | |
2292 | */ | |
2293 | void hists__match(struct hists *leader, struct hists *other) | |
2294 | { | |
2295 | struct rb_root *root; | |
2296 | struct rb_node *nd; | |
2297 | struct hist_entry *pos, *pair; | |
2298 | ||
2299 | if (symbol_conf.report_hierarchy) { | |
2300 | /* hierarchy report always collapses entries */ | |
2301 | return hists__match_hierarchy(&leader->entries_collapsed, | |
2302 | &other->entries_collapsed); | |
2303 | } | |
2304 | ||
2305 | if (hists__has(leader, need_collapse)) | |
2306 | root = &leader->entries_collapsed; | |
2307 | else | |
2308 | root = leader->entries_in; | |
2309 | ||
2310 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
2311 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
2312 | pair = hists__find_entry(other, pos); | |
2313 | ||
2314 | if (pair) | |
2315 | hist_entry__add_pair(pair, pos); | |
2316 | } | |
2317 | } | |
2318 | ||
2319 | static int hists__link_hierarchy(struct hists *leader_hists, | |
2320 | struct hist_entry *parent, | |
2321 | struct rb_root *leader_root, | |
2322 | struct rb_root *other_root) | |
2323 | { | |
2324 | struct rb_node *nd; | |
2325 | struct hist_entry *pos, *leader; | |
2326 | ||
2327 | for (nd = rb_first(other_root); nd; nd = rb_next(nd)) { | |
2328 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
2329 | ||
2330 | if (hist_entry__has_pairs(pos)) { | |
2331 | bool found = false; | |
2332 | ||
2333 | list_for_each_entry(leader, &pos->pairs.head, pairs.node) { | |
2334 | if (leader->hists == leader_hists) { | |
2335 | found = true; | |
2336 | break; | |
2337 | } | |
2338 | } | |
2339 | if (!found) | |
2340 | return -1; | |
2341 | } else { | |
2342 | leader = add_dummy_hierarchy_entry(leader_hists, | |
2343 | leader_root, pos); | |
2344 | if (leader == NULL) | |
2345 | return -1; | |
2346 | ||
2347 | /* do not point parent in the pos */ | |
2348 | leader->parent_he = parent; | |
2349 | ||
2350 | hist_entry__add_pair(pos, leader); | |
2351 | } | |
2352 | ||
2353 | if (!pos->leaf) { | |
2354 | if (hists__link_hierarchy(leader_hists, leader, | |
2355 | &leader->hroot_in, | |
2356 | &pos->hroot_in) < 0) | |
2357 | return -1; | |
2358 | } | |
2359 | } | |
2360 | return 0; | |
2361 | } | |
2362 | ||
2363 | /* | |
2364 | * Look for entries in the other hists that are not present in the leader, if | |
2365 | * we find them, just add a dummy entry on the leader hists, with period=0, | |
2366 | * nr_events=0, to serve as the list header. | |
2367 | */ | |
2368 | int hists__link(struct hists *leader, struct hists *other) | |
2369 | { | |
2370 | struct rb_root *root; | |
2371 | struct rb_node *nd; | |
2372 | struct hist_entry *pos, *pair; | |
2373 | ||
2374 | if (symbol_conf.report_hierarchy) { | |
2375 | /* hierarchy report always collapses entries */ | |
2376 | return hists__link_hierarchy(leader, NULL, | |
2377 | &leader->entries_collapsed, | |
2378 | &other->entries_collapsed); | |
2379 | } | |
2380 | ||
2381 | if (hists__has(other, need_collapse)) | |
2382 | root = &other->entries_collapsed; | |
2383 | else | |
2384 | root = other->entries_in; | |
2385 | ||
2386 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
2387 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
2388 | ||
2389 | if (!hist_entry__has_pairs(pos)) { | |
2390 | pair = hists__add_dummy_entry(leader, pos); | |
2391 | if (pair == NULL) | |
2392 | return -1; | |
2393 | hist_entry__add_pair(pos, pair); | |
2394 | } | |
2395 | } | |
2396 | ||
2397 | return 0; | |
2398 | } | |
2399 | ||
2400 | void hist__account_cycles(struct branch_stack *bs, struct addr_location *al, | |
2401 | struct perf_sample *sample, bool nonany_branch_mode) | |
2402 | { | |
2403 | struct branch_info *bi; | |
2404 | ||
2405 | /* If we have branch cycles always annotate them. */ | |
2406 | if (bs && bs->nr && bs->entries[0].flags.cycles) { | |
2407 | int i; | |
2408 | ||
2409 | bi = sample__resolve_bstack(sample, al); | |
2410 | if (bi) { | |
2411 | struct addr_map_symbol *prev = NULL; | |
2412 | ||
2413 | /* | |
2414 | * Ignore errors, still want to process the | |
2415 | * other entries. | |
2416 | * | |
2417 | * For non standard branch modes always | |
2418 | * force no IPC (prev == NULL) | |
2419 | * | |
2420 | * Note that perf stores branches reversed from | |
2421 | * program order! | |
2422 | */ | |
2423 | for (i = bs->nr - 1; i >= 0; i--) { | |
2424 | addr_map_symbol__account_cycles(&bi[i].from, | |
2425 | nonany_branch_mode ? NULL : prev, | |
2426 | bi[i].flags.cycles); | |
2427 | prev = &bi[i].to; | |
2428 | } | |
2429 | free(bi); | |
2430 | } | |
2431 | } | |
2432 | } | |
2433 | ||
2434 | size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp) | |
2435 | { | |
2436 | struct perf_evsel *pos; | |
2437 | size_t ret = 0; | |
2438 | ||
2439 | evlist__for_each_entry(evlist, pos) { | |
2440 | ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos)); | |
2441 | ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp); | |
2442 | } | |
2443 | ||
2444 | return ret; | |
2445 | } | |
2446 | ||
2447 | ||
2448 | u64 hists__total_period(struct hists *hists) | |
2449 | { | |
2450 | return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period : | |
2451 | hists->stats.total_period; | |
2452 | } | |
2453 | ||
2454 | int parse_filter_percentage(const struct option *opt __maybe_unused, | |
2455 | const char *arg, int unset __maybe_unused) | |
2456 | { | |
2457 | if (!strcmp(arg, "relative")) | |
2458 | symbol_conf.filter_relative = true; | |
2459 | else if (!strcmp(arg, "absolute")) | |
2460 | symbol_conf.filter_relative = false; | |
2461 | else { | |
2462 | pr_debug("Invalud percentage: %s\n", arg); | |
2463 | return -1; | |
2464 | } | |
2465 | ||
2466 | return 0; | |
2467 | } | |
2468 | ||
2469 | int perf_hist_config(const char *var, const char *value) | |
2470 | { | |
2471 | if (!strcmp(var, "hist.percentage")) | |
2472 | return parse_filter_percentage(NULL, value, 0); | |
2473 | ||
2474 | return 0; | |
2475 | } | |
2476 | ||
2477 | int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list) | |
2478 | { | |
2479 | memset(hists, 0, sizeof(*hists)); | |
2480 | hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT; | |
2481 | hists->entries_in = &hists->entries_in_array[0]; | |
2482 | hists->entries_collapsed = RB_ROOT; | |
2483 | hists->entries = RB_ROOT; | |
2484 | pthread_mutex_init(&hists->lock, NULL); | |
2485 | hists->socket_filter = -1; | |
2486 | hists->hpp_list = hpp_list; | |
2487 | INIT_LIST_HEAD(&hists->hpp_formats); | |
2488 | return 0; | |
2489 | } | |
2490 | ||
2491 | static void hists__delete_remaining_entries(struct rb_root *root) | |
2492 | { | |
2493 | struct rb_node *node; | |
2494 | struct hist_entry *he; | |
2495 | ||
2496 | while (!RB_EMPTY_ROOT(root)) { | |
2497 | node = rb_first(root); | |
2498 | rb_erase(node, root); | |
2499 | ||
2500 | he = rb_entry(node, struct hist_entry, rb_node_in); | |
2501 | hist_entry__delete(he); | |
2502 | } | |
2503 | } | |
2504 | ||
2505 | static void hists__delete_all_entries(struct hists *hists) | |
2506 | { | |
2507 | hists__delete_entries(hists); | |
2508 | hists__delete_remaining_entries(&hists->entries_in_array[0]); | |
2509 | hists__delete_remaining_entries(&hists->entries_in_array[1]); | |
2510 | hists__delete_remaining_entries(&hists->entries_collapsed); | |
2511 | } | |
2512 | ||
2513 | static void hists_evsel__exit(struct perf_evsel *evsel) | |
2514 | { | |
2515 | struct hists *hists = evsel__hists(evsel); | |
2516 | struct perf_hpp_fmt *fmt, *pos; | |
2517 | struct perf_hpp_list_node *node, *tmp; | |
2518 | ||
2519 | hists__delete_all_entries(hists); | |
2520 | ||
2521 | list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) { | |
2522 | perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) { | |
2523 | list_del(&fmt->list); | |
2524 | free(fmt); | |
2525 | } | |
2526 | list_del(&node->list); | |
2527 | free(node); | |
2528 | } | |
2529 | } | |
2530 | ||
2531 | static int hists_evsel__init(struct perf_evsel *evsel) | |
2532 | { | |
2533 | struct hists *hists = evsel__hists(evsel); | |
2534 | ||
2535 | __hists__init(hists, &perf_hpp_list); | |
2536 | return 0; | |
2537 | } | |
2538 | ||
2539 | /* | |
2540 | * XXX We probably need a hists_evsel__exit() to free the hist_entries | |
2541 | * stored in the rbtree... | |
2542 | */ | |
2543 | ||
2544 | int hists__init(void) | |
2545 | { | |
2546 | int err = perf_evsel__object_config(sizeof(struct hists_evsel), | |
2547 | hists_evsel__init, | |
2548 | hists_evsel__exit); | |
2549 | if (err) | |
2550 | fputs("FATAL ERROR: Couldn't setup hists class\n", stderr); | |
2551 | ||
2552 | return err; | |
2553 | } | |
2554 | ||
2555 | void perf_hpp_list__init(struct perf_hpp_list *list) | |
2556 | { | |
2557 | INIT_LIST_HEAD(&list->fields); | |
2558 | INIT_LIST_HEAD(&list->sorts); | |
2559 | } |