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