]>
Commit | Line | Data |
---|---|---|
8a0ecfb8 | 1 | #include "util.h" |
598357eb | 2 | #include "build-id.h" |
3d1d07ec | 3 | #include "hist.h" |
9c68ae98 | 4 | #include "map.h" |
4e4f06e4 | 5 | #include "session.h" |
d890a98c | 6 | #include "namespaces.h" |
4e4f06e4 | 7 | #include "sort.h" |
2a1731fb | 8 | #include "evlist.h" |
29d720ed | 9 | #include "evsel.h" |
69bcb019 | 10 | #include "annotate.h" |
740b97f9 | 11 | #include "ui/progress.h" |
9b33827d | 12 | #include <math.h> |
3d1d07ec | 13 | |
90cf1fb5 ACM |
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); | |
e94d53eb NK |
18 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
19 | struct hist_entry *he); | |
21394d94 KL |
20 | static bool hists__filter_entry_by_socket(struct hists *hists, |
21 | struct hist_entry *he); | |
90cf1fb5 | 22 | |
42b28ac0 | 23 | u16 hists__col_len(struct hists *hists, enum hist_column col) |
8a6c5b26 | 24 | { |
42b28ac0 | 25 | return hists->col_len[col]; |
8a6c5b26 ACM |
26 | } |
27 | ||
42b28ac0 | 28 | void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 29 | { |
42b28ac0 | 30 | hists->col_len[col] = len; |
8a6c5b26 ACM |
31 | } |
32 | ||
42b28ac0 | 33 | bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 34 | { |
42b28ac0 ACM |
35 | if (len > hists__col_len(hists, col)) { |
36 | hists__set_col_len(hists, col, len); | |
8a6c5b26 ACM |
37 | return true; |
38 | } | |
39 | return false; | |
40 | } | |
41 | ||
7ccf4f90 | 42 | void hists__reset_col_len(struct hists *hists) |
8a6c5b26 ACM |
43 | { |
44 | enum hist_column col; | |
45 | ||
46 | for (col = 0; col < HISTC_NR_COLS; ++col) | |
42b28ac0 | 47 | hists__set_col_len(hists, col, 0); |
8a6c5b26 ACM |
48 | } |
49 | ||
b5387528 RAV |
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 | ||
7ccf4f90 | 60 | void hists__calc_col_len(struct hists *hists, struct hist_entry *h) |
8a6c5b26 | 61 | { |
b5387528 | 62 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
98a3b32c | 63 | int symlen; |
8a6c5b26 ACM |
64 | u16 len; |
65 | ||
ded19d57 NK |
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; | |
bb963e16 | 73 | if (verbose > 0) |
ded19d57 NK |
74 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
75 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); | |
76 | } else { | |
98a3b32c SE |
77 | symlen = unresolved_col_width + 4 + 2; |
78 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); | |
b5387528 | 79 | hists__set_unres_dso_col_len(hists, HISTC_DSO); |
98a3b32c | 80 | } |
8a6c5b26 ACM |
81 | |
82 | len = thread__comm_len(h->thread); | |
42b28ac0 | 83 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
89c7cb2c | 84 | hists__set_col_len(hists, HISTC_THREAD, len + 8); |
8a6c5b26 ACM |
85 | |
86 | if (h->ms.map) { | |
87 | len = dso__name_len(h->ms.map->dso); | |
42b28ac0 | 88 | hists__new_col_len(hists, HISTC_DSO, len); |
8a6c5b26 | 89 | } |
b5387528 | 90 | |
cb993744 NK |
91 | if (h->parent) |
92 | hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen); | |
93 | ||
b5387528 | 94 | if (h->branch_info) { |
b5387528 RAV |
95 | if (h->branch_info->from.sym) { |
96 | symlen = (int)h->branch_info->from.sym->namelen + 4; | |
bb963e16 | 97 | if (verbose > 0) |
ded19d57 | 98 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
b5387528 RAV |
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; | |
bb963e16 | 111 | if (verbose > 0) |
ded19d57 | 112 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
b5387528 RAV |
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 | } | |
508be0df AK |
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)); | |
b5387528 | 129 | } |
98a3b32c SE |
130 | |
131 | if (h->mem_info) { | |
98a3b32c SE |
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); | |
9b32ba71 DZ |
137 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, |
138 | symlen + 1); | |
98a3b32c SE |
139 | } else { |
140 | symlen = unresolved_col_width + 4 + 2; | |
141 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, | |
142 | symlen); | |
0805909f JO |
143 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, |
144 | symlen); | |
98a3b32c | 145 | } |
b34b3bf0 JO |
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 | ||
98a3b32c SE |
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); | |
b34b3bf0 | 169 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen); |
98a3b32c SE |
170 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); |
171 | } | |
172 | ||
d890a98c | 173 | hists__new_col_len(hists, HISTC_CGROUP_ID, 20); |
a4978eca | 174 | hists__new_col_len(hists, HISTC_CPU, 3); |
2e7ea3ab | 175 | hists__new_col_len(hists, HISTC_SOCKET, 6); |
98a3b32c SE |
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); | |
475eeab9 | 182 | |
f666ac0d JO |
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 | } | |
e8e6d37e | 187 | |
31191a85 AK |
188 | if (h->srcfile) |
189 | hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile)); | |
190 | ||
475eeab9 AK |
191 | if (h->transaction) |
192 | hists__new_col_len(hists, HISTC_TRANSACTION, | |
193 | hist_entry__transaction_len()); | |
0c0af78d NK |
194 | |
195 | if (h->trace_output) | |
196 | hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output)); | |
8a6c5b26 ACM |
197 | } |
198 | ||
7ccf4f90 NK |
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 | ||
f39056f9 NK |
215 | static void he_stat__add_cpumode_period(struct he_stat *he_stat, |
216 | unsigned int cpumode, u64 period) | |
a1645ce1 | 217 | { |
28e2a106 | 218 | switch (cpumode) { |
a1645ce1 | 219 | case PERF_RECORD_MISC_KERNEL: |
f39056f9 | 220 | he_stat->period_sys += period; |
a1645ce1 ZY |
221 | break; |
222 | case PERF_RECORD_MISC_USER: | |
f39056f9 | 223 | he_stat->period_us += period; |
a1645ce1 ZY |
224 | break; |
225 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
f39056f9 | 226 | he_stat->period_guest_sys += period; |
a1645ce1 ZY |
227 | break; |
228 | case PERF_RECORD_MISC_GUEST_USER: | |
f39056f9 | 229 | he_stat->period_guest_us += period; |
a1645ce1 ZY |
230 | break; |
231 | default: | |
232 | break; | |
233 | } | |
234 | } | |
235 | ||
05484298 AK |
236 | static void he_stat__add_period(struct he_stat *he_stat, u64 period, |
237 | u64 weight) | |
139c0815 | 238 | { |
98a3b32c | 239 | |
139c0815 | 240 | he_stat->period += period; |
05484298 | 241 | he_stat->weight += weight; |
139c0815 NK |
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; | |
05484298 | 253 | dest->weight += src->weight; |
139c0815 NK |
254 | } |
255 | ||
f39056f9 | 256 | static void he_stat__decay(struct he_stat *he_stat) |
ab81f3fd | 257 | { |
f39056f9 NK |
258 | he_stat->period = (he_stat->period * 7) / 8; |
259 | he_stat->nr_events = (he_stat->nr_events * 7) / 8; | |
05484298 | 260 | /* XXX need decay for weight too? */ |
ab81f3fd ACM |
261 | } |
262 | ||
5d8200ae NK |
263 | static void hists__delete_entry(struct hists *hists, struct hist_entry *he); |
264 | ||
ab81f3fd ACM |
265 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) |
266 | { | |
b24c28f7 | 267 | u64 prev_period = he->stat.period; |
3186b681 | 268 | u64 diff; |
c64550cf ACM |
269 | |
270 | if (prev_period == 0) | |
df71d95f | 271 | return true; |
c64550cf | 272 | |
f39056f9 | 273 | he_stat__decay(&he->stat); |
f8be1c8c NK |
274 | if (symbol_conf.cumulate_callchain) |
275 | he_stat__decay(he->stat_acc); | |
42b276a2 | 276 | decay_callchain(he->callchain); |
c64550cf | 277 | |
3186b681 NK |
278 | diff = prev_period - he->stat.period; |
279 | ||
5d8200ae NK |
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 | } | |
c64550cf | 297 | |
b24c28f7 | 298 | return he->stat.period == 0; |
ab81f3fd ACM |
299 | } |
300 | ||
956b65e1 ACM |
301 | static void hists__delete_entry(struct hists *hists, struct hist_entry *he) |
302 | { | |
5d8200ae NK |
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 { | |
52225036 | 310 | if (hists__has(hists, need_collapse)) |
5d8200ae NK |
311 | root_in = &hists->entries_collapsed; |
312 | else | |
313 | root_in = hists->entries_in; | |
314 | root_out = &hists->entries; | |
315 | } | |
956b65e1 | 316 | |
5d8200ae NK |
317 | rb_erase(&he->rb_node_in, root_in); |
318 | rb_erase(&he->rb_node, root_out); | |
956b65e1 ACM |
319 | |
320 | --hists->nr_entries; | |
321 | if (!he->filtered) | |
322 | --hists->nr_non_filtered_entries; | |
323 | ||
324 | hist_entry__delete(he); | |
325 | } | |
326 | ||
3a5714f8 | 327 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) |
ab81f3fd ACM |
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); | |
b079d4e9 ACM |
335 | if (((zap_user && n->level == '.') || |
336 | (zap_kernel && n->level != '.') || | |
4c47f4fc | 337 | hists__decay_entry(hists, n))) { |
956b65e1 | 338 | hists__delete_entry(hists, n); |
ab81f3fd ACM |
339 | } |
340 | } | |
341 | } | |
342 | ||
701937bd NK |
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 | ||
956b65e1 | 352 | hists__delete_entry(hists, n); |
701937bd NK |
353 | } |
354 | } | |
355 | ||
3d1d07ec | 356 | /* |
c82ee828 | 357 | * histogram, sorted on item, collects periods |
3d1d07ec JK |
358 | */ |
359 | ||
0a269a6b JO |
360 | static int hist_entry__init(struct hist_entry *he, |
361 | struct hist_entry *template, | |
362 | bool sample_self) | |
28e2a106 | 363 | { |
0a269a6b JO |
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 | } | |
f8be1c8c | 374 | |
0a269a6b JO |
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 | } | |
f8be1c8c | 389 | |
0a269a6b JO |
390 | memcpy(he->branch_info, template->branch_info, |
391 | sizeof(*he->branch_info)); | |
28e2a106 | 392 | |
0a269a6b JO |
393 | map__get(he->branch_info->from.map); |
394 | map__get(he->branch_info->to.map); | |
395 | } | |
c4b35351 | 396 | |
0a269a6b JO |
397 | if (he->mem_info) { |
398 | map__get(he->mem_info->iaddr.map); | |
399 | map__get(he->mem_info->daddr.map); | |
400 | } | |
f8be1c8c | 401 | |
0a269a6b JO |
402 | if (symbol_conf.use_callchain) |
403 | callchain_init(he->callchain); | |
26353a61 | 404 | |
0a269a6b JO |
405 | if (he->raw_data) { |
406 | he->raw_data = memdup(he->raw_data, he->raw_size); | |
26353a61 | 407 | |
0a269a6b JO |
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; | |
3cf0cb1f | 421 | } |
0a269a6b JO |
422 | } |
423 | INIT_LIST_HEAD(&he->pairs.node); | |
424 | thread__get(he->thread); | |
d2580c7a NK |
425 | he->hroot_in = RB_ROOT; |
426 | he->hroot_out = RB_ROOT; | |
3cf0cb1f | 427 | |
0a269a6b JO |
428 | if (!symbol_conf.report_hierarchy) |
429 | he->leaf = true; | |
98a3b32c | 430 | |
0a269a6b JO |
431 | return 0; |
432 | } | |
433 | ||
f542e767 JO |
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 | ||
0a269a6b JO |
449 | static struct hist_entry *hist_entry__new(struct hist_entry *template, |
450 | bool sample_self) | |
451 | { | |
f542e767 | 452 | struct hist_entry_ops *ops = template->ops; |
0a269a6b JO |
453 | size_t callchain_size = 0; |
454 | struct hist_entry *he; | |
455 | int err = 0; | |
aef810ec | 456 | |
f542e767 JO |
457 | if (!ops) |
458 | ops = template->ops = &default_ops; | |
459 | ||
0a269a6b JO |
460 | if (symbol_conf.use_callchain) |
461 | callchain_size = sizeof(struct callchain_root); | |
462 | ||
f542e767 | 463 | he = ops->new(callchain_size); |
0a269a6b JO |
464 | if (he) { |
465 | err = hist_entry__init(he, template, sample_self); | |
f542e767 JO |
466 | if (err) { |
467 | ops->free(he); | |
468 | he = NULL; | |
469 | } | |
28e2a106 ACM |
470 | } |
471 | ||
12c14278 | 472 | return he; |
28e2a106 ACM |
473 | } |
474 | ||
7a007ca9 ACM |
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 | ||
467ef10c NK |
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 | ||
e7e0efcd ACM |
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) | |
9735abf1 | 496 | { |
1980c2eb | 497 | struct rb_node **p; |
9735abf1 ACM |
498 | struct rb_node *parent = NULL; |
499 | struct hist_entry *he; | |
354cc40e | 500 | int64_t cmp; |
f1cbf78d NK |
501 | u64 period = entry->stat.period; |
502 | u64 weight = entry->stat.weight; | |
9735abf1 | 503 | |
1980c2eb ACM |
504 | p = &hists->entries_in->rb_node; |
505 | ||
9735abf1 ACM |
506 | while (*p != NULL) { |
507 | parent = *p; | |
1980c2eb | 508 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
9735abf1 | 509 | |
9afcf930 NK |
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); | |
9735abf1 ACM |
517 | |
518 | if (!cmp) { | |
0f58474e | 519 | if (sample_self) { |
a0b51af3 | 520 | he_stat__add_period(&he->stat, period, weight); |
467ef10c | 521 | hist_entry__add_callchain_period(he, period); |
0f58474e | 522 | } |
f8be1c8c NK |
523 | if (symbol_conf.cumulate_callchain) |
524 | he_stat__add_period(he->stat_acc, period, weight); | |
63fa471d | 525 | |
ceb2acbc | 526 | /* |
e80faac0 | 527 | * This mem info was allocated from sample__resolve_mem |
ceb2acbc NK |
528 | * and will not be used anymore. |
529 | */ | |
74cf249d | 530 | zfree(&entry->mem_info); |
ceb2acbc | 531 | |
63fa471d DM |
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) { | |
5c24b67a ACM |
539 | map__put(he->ms.map); |
540 | he->ms.map = map__get(entry->ms.map); | |
63fa471d | 541 | } |
28e2a106 | 542 | goto out; |
9735abf1 ACM |
543 | } |
544 | ||
545 | if (cmp < 0) | |
546 | p = &(*p)->rb_left; | |
547 | else | |
548 | p = &(*p)->rb_right; | |
549 | } | |
550 | ||
a0b51af3 | 551 | he = hist_entry__new(entry, sample_self); |
9735abf1 | 552 | if (!he) |
27a0dcb7 | 553 | return NULL; |
1980c2eb | 554 | |
0f58474e | 555 | if (sample_self) |
467ef10c NK |
556 | hist_entry__add_callchain_period(he, period); |
557 | hists->nr_entries++; | |
590cd344 | 558 | |
1980c2eb ACM |
559 | rb_link_node(&he->rb_node_in, parent, p); |
560 | rb_insert_color(&he->rb_node_in, hists->entries_in); | |
28e2a106 | 561 | out: |
a0b51af3 NK |
562 | if (sample_self) |
563 | he_stat__add_cpumode_period(&he->stat, al->cpumode, period); | |
f8be1c8c NK |
564 | if (symbol_conf.cumulate_callchain) |
565 | he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period); | |
9735abf1 ACM |
566 | return he; |
567 | } | |
568 | ||
a5051979 JO |
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) | |
b5387528 | 578 | { |
d890a98c | 579 | struct namespaces *ns = thread__namespaces(al->thread); |
b5387528 RAV |
580 | struct hist_entry entry = { |
581 | .thread = al->thread, | |
4dfced35 | 582 | .comm = thread__comm(al->thread), |
d890a98c HB |
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 | }, | |
b5387528 RAV |
587 | .ms = { |
588 | .map = al->map, | |
589 | .sym = al->sym, | |
590 | }, | |
0c4c4deb | 591 | .socket = al->socket, |
7365be55 DZ |
592 | .cpu = al->cpu, |
593 | .cpumode = al->cpumode, | |
594 | .ip = al->addr, | |
595 | .level = al->level, | |
b24c28f7 | 596 | .stat = { |
c4b35351 | 597 | .nr_events = 1, |
fd36f3dd NK |
598 | .period = sample->period, |
599 | .weight = sample->weight, | |
b24c28f7 | 600 | }, |
b5387528 | 601 | .parent = sym_parent, |
2c86c7ca | 602 | .filtered = symbol__parent_filter(sym_parent) | al->filtered, |
c824c433 | 603 | .hists = hists, |
41a4e6e2 NK |
604 | .branch_info = bi, |
605 | .mem_info = mi, | |
fd36f3dd | 606 | .transaction = sample->transaction, |
72392834 NK |
607 | .raw_data = sample->raw_data, |
608 | .raw_size = sample->raw_size, | |
a5051979 | 609 | .ops = ops, |
b5387528 RAV |
610 | }; |
611 | ||
e7e0efcd | 612 | return hists__findnew_entry(hists, &entry, al, sample_self); |
b5387528 RAV |
613 | } |
614 | ||
a5051979 JO |
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 | ||
69bcb019 NK |
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; | |
4ea062ed | 673 | struct hists *hists = evsel__hists(iter->evsel); |
fd36f3dd | 674 | struct perf_sample *sample = iter->sample; |
69bcb019 NK |
675 | struct hist_entry *he; |
676 | ||
677 | if (mi == NULL) | |
678 | return -EINVAL; | |
679 | ||
fd36f3dd | 680 | cost = sample->weight; |
69bcb019 NK |
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 | */ | |
fd36f3dd NK |
691 | sample->period = cost; |
692 | ||
0102ef3e JO |
693 | he = hists__add_entry(hists, al, iter->parent, NULL, mi, |
694 | sample, true); | |
69bcb019 NK |
695 | if (!he) |
696 | return -ENOMEM; | |
697 | ||
698 | iter->he = he; | |
699 | return 0; | |
700 | } | |
701 | ||
702 | static int | |
9d3c02d7 NK |
703 | iter_finish_mem_entry(struct hist_entry_iter *iter, |
704 | struct addr_location *al __maybe_unused) | |
69bcb019 NK |
705 | { |
706 | struct perf_evsel *evsel = iter->evsel; | |
4ea062ed | 707 | struct hists *hists = evsel__hists(evsel); |
69bcb019 | 708 | struct hist_entry *he = iter->he; |
69bcb019 NK |
709 | int err = -EINVAL; |
710 | ||
711 | if (he == NULL) | |
712 | goto out; | |
713 | ||
4ea062ed | 714 | hists__inc_nr_samples(hists, he->filtered); |
69bcb019 NK |
715 | |
716 | err = hist_entry__append_callchain(he, iter->sample); | |
717 | ||
718 | out: | |
719 | /* | |
e7e0efcd ACM |
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(). | |
69bcb019 NK |
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 | |
b8f8eb84 | 748 | iter_add_single_branch_entry(struct hist_entry_iter *iter, |
69bcb019 NK |
749 | struct addr_location *al __maybe_unused) |
750 | { | |
9d3c02d7 NK |
751 | /* to avoid calling callback function */ |
752 | iter->he = NULL; | |
753 | ||
69bcb019 NK |
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 | { | |
9d3c02d7 | 778 | struct branch_info *bi; |
69bcb019 | 779 | struct perf_evsel *evsel = iter->evsel; |
4ea062ed | 780 | struct hists *hists = evsel__hists(evsel); |
fd36f3dd | 781 | struct perf_sample *sample = iter->sample; |
69bcb019 NK |
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 | */ | |
fd36f3dd NK |
795 | sample->period = 1; |
796 | sample->weight = bi->flags.cycles ? bi->flags.cycles : 1; | |
797 | ||
0102ef3e JO |
798 | he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL, |
799 | sample, true); | |
69bcb019 NK |
800 | if (he == NULL) |
801 | return -ENOMEM; | |
802 | ||
4ea062ed | 803 | hists__inc_nr_samples(hists, he->filtered); |
69bcb019 NK |
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 | ||
0102ef3e JO |
835 | he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, |
836 | sample, true); | |
69bcb019 NK |
837 | if (he == NULL) |
838 | return -ENOMEM; | |
839 | ||
840 | iter->he = he; | |
841 | return 0; | |
842 | } | |
843 | ||
844 | static int | |
9d3c02d7 NK |
845 | iter_finish_normal_entry(struct hist_entry_iter *iter, |
846 | struct addr_location *al __maybe_unused) | |
69bcb019 | 847 | { |
69bcb019 NK |
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 | ||
4ea062ed | 857 | hists__inc_nr_samples(evsel__hists(evsel), he->filtered); |
69bcb019 NK |
858 | |
859 | return hist_entry__append_callchain(he, sample); | |
860 | } | |
861 | ||
7a13aa28 | 862 | static int |
96b40f3c | 863 | iter_prepare_cumulative_entry(struct hist_entry_iter *iter, |
7a13aa28 NK |
864 | struct addr_location *al __maybe_unused) |
865 | { | |
b4d3c8bd NK |
866 | struct hist_entry **he_cache; |
867 | ||
7a13aa28 | 868 | callchain_cursor_commit(&callchain_cursor); |
b4d3c8bd NK |
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 | */ | |
96b40f3c | 875 | he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1)); |
b4d3c8bd NK |
876 | if (he_cache == NULL) |
877 | return -ENOMEM; | |
878 | ||
879 | iter->priv = he_cache; | |
880 | iter->curr = 0; | |
881 | ||
7a13aa28 NK |
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; | |
4ea062ed | 890 | struct hists *hists = evsel__hists(evsel); |
7a13aa28 | 891 | struct perf_sample *sample = iter->sample; |
b4d3c8bd | 892 | struct hist_entry **he_cache = iter->priv; |
7a13aa28 NK |
893 | struct hist_entry *he; |
894 | int err = 0; | |
895 | ||
0102ef3e JO |
896 | he = hists__add_entry(hists, al, iter->parent, NULL, NULL, |
897 | sample, true); | |
7a13aa28 NK |
898 | if (he == NULL) |
899 | return -ENOMEM; | |
900 | ||
901 | iter->he = he; | |
b4d3c8bd | 902 | he_cache[iter->curr++] = he; |
7a13aa28 | 903 | |
82aa019e | 904 | hist_entry__append_callchain(he, sample); |
be7f855a NK |
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 | ||
4ea062ed | 912 | hists__inc_nr_samples(hists, he->filtered); |
7a13aa28 NK |
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 | ||
c7405d85 | 927 | return fill_callchain_info(al, node, iter->hide_unresolved); |
7a13aa28 NK |
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; | |
b4d3c8bd | 936 | struct hist_entry **he_cache = iter->priv; |
7a13aa28 | 937 | struct hist_entry *he; |
b4d3c8bd | 938 | struct hist_entry he_tmp = { |
5cef8976 | 939 | .hists = evsel__hists(evsel), |
b4d3c8bd NK |
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, | |
72392834 NK |
949 | .raw_data = sample->raw_data, |
950 | .raw_size = sample->raw_size, | |
b4d3c8bd NK |
951 | }; |
952 | int i; | |
be7f855a NK |
953 | struct callchain_cursor cursor; |
954 | ||
955 | callchain_cursor_snapshot(&cursor, &callchain_cursor); | |
956 | ||
957 | callchain_cursor_advance(&callchain_cursor); | |
b4d3c8bd NK |
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++) { | |
9d3c02d7 NK |
964 | if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) { |
965 | /* to avoid calling callback function */ | |
966 | iter->he = NULL; | |
b4d3c8bd | 967 | return 0; |
9d3c02d7 | 968 | } |
b4d3c8bd | 969 | } |
7a13aa28 | 970 | |
0102ef3e JO |
971 | he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, |
972 | sample, false); | |
7a13aa28 NK |
973 | if (he == NULL) |
974 | return -ENOMEM; | |
975 | ||
976 | iter->he = he; | |
b4d3c8bd | 977 | he_cache[iter->curr++] = he; |
7a13aa28 | 978 | |
82aa019e NK |
979 | if (symbol_conf.use_callchain) |
980 | callchain_append(he->callchain, &cursor, sample->period); | |
7a13aa28 NK |
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 | { | |
b4d3c8bd | 988 | zfree(&iter->priv); |
7a13aa28 | 989 | iter->he = NULL; |
b4d3c8bd | 990 | |
7a13aa28 NK |
991 | return 0; |
992 | } | |
993 | ||
69bcb019 NK |
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 | ||
7a13aa28 NK |
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 | ||
69bcb019 | 1026 | int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al, |
9d3c02d7 | 1027 | int max_stack_depth, void *arg) |
69bcb019 NK |
1028 | { |
1029 | int err, err2; | |
9c68ae98 KJ |
1030 | struct map *alm = NULL; |
1031 | ||
1032 | if (al && al->map) | |
1033 | alm = map__get(al->map); | |
69bcb019 | 1034 | |
91d7b2de | 1035 | err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent, |
063bd936 | 1036 | iter->evsel, al, max_stack_depth); |
69bcb019 NK |
1037 | if (err) |
1038 | return err; | |
1039 | ||
96b40f3c AH |
1040 | iter->max_stack = max_stack_depth; |
1041 | ||
69bcb019 NK |
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 | ||
9d3c02d7 NK |
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 | ||
69bcb019 NK |
1056 | while (iter->ops->next_entry(iter, al)) { |
1057 | err = iter->ops->add_next_entry(iter, al); | |
1058 | if (err) | |
1059 | break; | |
9d3c02d7 NK |
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 | } | |
69bcb019 NK |
1066 | } |
1067 | ||
1068 | out: | |
1069 | err2 = iter->ops->finish_entry(iter, al); | |
1070 | if (!err) | |
1071 | err = err2; | |
1072 | ||
9c68ae98 KJ |
1073 | map__put(alm); |
1074 | ||
69bcb019 NK |
1075 | return err; |
1076 | } | |
1077 | ||
3d1d07ec JK |
1078 | int64_t |
1079 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
1080 | { | |
aa6f50af | 1081 | struct hists *hists = left->hists; |
093f0ef3 | 1082 | struct perf_hpp_fmt *fmt; |
3d1d07ec JK |
1083 | int64_t cmp = 0; |
1084 | ||
aa6f50af | 1085 | hists__for_each_sort_list(hists, fmt) { |
84b6ee8e NK |
1086 | if (perf_hpp__is_dynamic_entry(fmt) && |
1087 | !perf_hpp__defined_dynamic_entry(fmt, hists)) | |
1088 | continue; | |
1089 | ||
87bbdf76 | 1090 | cmp = fmt->cmp(fmt, left, right); |
3d1d07ec JK |
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 | { | |
aa6f50af | 1101 | struct hists *hists = left->hists; |
093f0ef3 | 1102 | struct perf_hpp_fmt *fmt; |
3d1d07ec JK |
1103 | int64_t cmp = 0; |
1104 | ||
aa6f50af | 1105 | hists__for_each_sort_list(hists, fmt) { |
84b6ee8e NK |
1106 | if (perf_hpp__is_dynamic_entry(fmt) && |
1107 | !perf_hpp__defined_dynamic_entry(fmt, hists)) | |
1108 | continue; | |
1109 | ||
87bbdf76 | 1110 | cmp = fmt->collapse(fmt, left, right); |
3d1d07ec JK |
1111 | if (cmp) |
1112 | break; | |
1113 | } | |
1114 | ||
1115 | return cmp; | |
1116 | } | |
1117 | ||
6733d1bf | 1118 | void hist_entry__delete(struct hist_entry *he) |
3d1d07ec | 1119 | { |
f542e767 JO |
1120 | struct hist_entry_ops *ops = he->ops; |
1121 | ||
f3b623b8 | 1122 | thread__zput(he->thread); |
5c24b67a ACM |
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); | |
508be0df AK |
1128 | free_srcline(he->branch_info->srcline_from); |
1129 | free_srcline(he->branch_info->srcline_to); | |
5c24b67a ACM |
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 | ||
0d3eb0b7 JY |
1139 | if (he->inline_node) { |
1140 | inline_node__delete(he->inline_node); | |
1141 | he->inline_node = NULL; | |
1142 | } | |
1143 | ||
f8be1c8c | 1144 | zfree(&he->stat_acc); |
f048d548 | 1145 | free_srcline(he->srcline); |
31191a85 AK |
1146 | if (he->srcfile && he->srcfile[0]) |
1147 | free(he->srcfile); | |
d114960c | 1148 | free_callchain(he->callchain); |
60517d28 | 1149 | free(he->trace_output); |
72392834 | 1150 | free(he->raw_data); |
f542e767 | 1151 | ops->free(he); |
3d1d07ec JK |
1152 | } |
1153 | ||
89fee709 ACM |
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)) { | |
da1b0407 | 1165 | const int width = fmt->width(fmt, hpp, he->hists); |
89fee709 ACM |
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 | ||
3d1d07ec JK |
1175 | /* |
1176 | * collapse the histogram | |
1177 | */ | |
1178 | ||
aef810ec | 1179 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he); |
aec13a7e NK |
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: | |
9857b717 | 1217 | case HIST_FILTER__C2C: |
aec13a7e NK |
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 | } | |
aef810ec NK |
1273 | |
1274 | static struct hist_entry *hierarchy_insert_entry(struct hists *hists, | |
1275 | struct rb_root *root, | |
1276 | struct hist_entry *he, | |
aec13a7e | 1277 | struct hist_entry *parent_he, |
1b2dbbf4 | 1278 | struct perf_hpp_list *hpp_list) |
aef810ec NK |
1279 | { |
1280 | struct rb_node **p = &root->rb_node; | |
1281 | struct rb_node *parent = NULL; | |
1282 | struct hist_entry *iter, *new; | |
1b2dbbf4 | 1283 | struct perf_hpp_fmt *fmt; |
aef810ec NK |
1284 | int64_t cmp; |
1285 | ||
1286 | while (*p != NULL) { | |
1287 | parent = *p; | |
1288 | iter = rb_entry(parent, struct hist_entry, rb_node_in); | |
1289 | ||
1b2dbbf4 NK |
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 | ||
aef810ec NK |
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 | ||
aef810ec NK |
1312 | hists->nr_entries++; |
1313 | ||
1b2dbbf4 NK |
1314 | /* save related format list for output */ |
1315 | new->hpp_list = hpp_list; | |
aec13a7e NK |
1316 | new->parent_he = parent_he; |
1317 | ||
1318 | hist_entry__apply_hierarchy_filters(new); | |
aef810ec NK |
1319 | |
1320 | /* some fields are now passed to 'new' */ | |
1b2dbbf4 NK |
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; | |
aef810ec | 1326 | |
1b2dbbf4 NK |
1327 | if (perf_hpp__is_srcline_entry(fmt)) |
1328 | he->srcline = NULL; | |
1329 | else | |
1330 | new->srcline = NULL; | |
aef810ec | 1331 | |
1b2dbbf4 NK |
1332 | if (perf_hpp__is_srcfile_entry(fmt)) |
1333 | he->srcfile = NULL; | |
1334 | else | |
1335 | new->srcfile = NULL; | |
1336 | } | |
aef810ec NK |
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 | { | |
1b2dbbf4 | 1347 | struct perf_hpp_list_node *node; |
aef810ec NK |
1348 | struct hist_entry *new_he = NULL; |
1349 | struct hist_entry *parent = NULL; | |
1350 | int depth = 0; | |
1351 | int ret = 0; | |
1352 | ||
1b2dbbf4 NK |
1353 | list_for_each_entry(node, &hists->hpp_formats, list) { |
1354 | /* skip period (overhead) and elided columns */ | |
1355 | if (node->level == 0 || node->skip) | |
aef810ec NK |
1356 | continue; |
1357 | ||
1358 | /* insert copy of 'he' for each fmt into the hierarchy */ | |
aec13a7e | 1359 | new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp); |
aef810ec NK |
1360 | if (new_he == NULL) { |
1361 | ret = -1; | |
1362 | break; | |
1363 | } | |
1364 | ||
1365 | root = &new_he->hroot_in; | |
aef810ec NK |
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 | ||
592dac6f JO |
1389 | static int hists__collapse_insert_entry(struct hists *hists, |
1390 | struct rb_root *root, | |
1391 | struct hist_entry *he) | |
3d1d07ec | 1392 | { |
b9bf0892 | 1393 | struct rb_node **p = &root->rb_node; |
3d1d07ec JK |
1394 | struct rb_node *parent = NULL; |
1395 | struct hist_entry *iter; | |
1396 | int64_t cmp; | |
1397 | ||
aef810ec NK |
1398 | if (symbol_conf.report_hierarchy) |
1399 | return hists__hierarchy_insert_entry(hists, root, he); | |
1400 | ||
3d1d07ec JK |
1401 | while (*p != NULL) { |
1402 | parent = *p; | |
1980c2eb | 1403 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
3d1d07ec JK |
1404 | |
1405 | cmp = hist_entry__collapse(iter, he); | |
1406 | ||
1407 | if (!cmp) { | |
bba58cdf NK |
1408 | int ret = 0; |
1409 | ||
139c0815 | 1410 | he_stat__add_stat(&iter->stat, &he->stat); |
f8be1c8c NK |
1411 | if (symbol_conf.cumulate_callchain) |
1412 | he_stat__add_stat(iter->stat_acc, he->stat_acc); | |
9ec60972 | 1413 | |
1b3a0e95 | 1414 | if (symbol_conf.use_callchain) { |
47260645 | 1415 | callchain_cursor_reset(&callchain_cursor); |
bba58cdf NK |
1416 | if (callchain_merge(&callchain_cursor, |
1417 | iter->callchain, | |
1418 | he->callchain) < 0) | |
1419 | ret = -1; | |
1b3a0e95 | 1420 | } |
6733d1bf | 1421 | hist_entry__delete(he); |
bba58cdf | 1422 | return ret; |
3d1d07ec JK |
1423 | } |
1424 | ||
1425 | if (cmp < 0) | |
1426 | p = &(*p)->rb_left; | |
1427 | else | |
1428 | p = &(*p)->rb_right; | |
1429 | } | |
740b97f9 | 1430 | hists->nr_entries++; |
3d1d07ec | 1431 | |
1980c2eb ACM |
1432 | rb_link_node(&he->rb_node_in, parent, p); |
1433 | rb_insert_color(&he->rb_node_in, root); | |
bba58cdf | 1434 | return 1; |
3d1d07ec JK |
1435 | } |
1436 | ||
fc284be9 | 1437 | struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
3d1d07ec | 1438 | { |
1980c2eb ACM |
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 | ||
90cf1fb5 ACM |
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); | |
e94d53eb | 1456 | hists__filter_entry_by_symbol(hists, he); |
21394d94 | 1457 | hists__filter_entry_by_socket(hists, he); |
90cf1fb5 ACM |
1458 | } |
1459 | ||
bba58cdf | 1460 | int hists__collapse_resort(struct hists *hists, struct ui_progress *prog) |
1980c2eb ACM |
1461 | { |
1462 | struct rb_root *root; | |
3d1d07ec JK |
1463 | struct rb_node *next; |
1464 | struct hist_entry *n; | |
bba58cdf | 1465 | int ret; |
3d1d07ec | 1466 | |
52225036 | 1467 | if (!hists__has(hists, need_collapse)) |
bba58cdf | 1468 | return 0; |
3d1d07ec | 1469 | |
740b97f9 NK |
1470 | hists->nr_entries = 0; |
1471 | ||
1980c2eb | 1472 | root = hists__get_rotate_entries_in(hists); |
740b97f9 | 1473 | |
1980c2eb | 1474 | next = rb_first(root); |
b9bf0892 | 1475 | |
3d1d07ec | 1476 | while (next) { |
33e940a2 ACM |
1477 | if (session_done()) |
1478 | break; | |
1980c2eb ACM |
1479 | n = rb_entry(next, struct hist_entry, rb_node_in); |
1480 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 1481 | |
1980c2eb | 1482 | rb_erase(&n->rb_node_in, root); |
bba58cdf NK |
1483 | ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n); |
1484 | if (ret < 0) | |
1485 | return -1; | |
1486 | ||
1487 | if (ret) { | |
90cf1fb5 ACM |
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); | |
90cf1fb5 | 1494 | } |
c1fb5651 NK |
1495 | if (prog) |
1496 | ui_progress__update(prog, 1); | |
3d1d07ec | 1497 | } |
bba58cdf | 1498 | return 0; |
1980c2eb | 1499 | } |
b9bf0892 | 1500 | |
043ca389 | 1501 | static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b) |
29d720ed | 1502 | { |
aa6f50af | 1503 | struct hists *hists = a->hists; |
043ca389 NK |
1504 | struct perf_hpp_fmt *fmt; |
1505 | int64_t cmp = 0; | |
29d720ed | 1506 | |
aa6f50af | 1507 | hists__for_each_sort_list(hists, fmt) { |
361459f1 | 1508 | if (perf_hpp__should_skip(fmt, a->hists)) |
e67d49a7 NK |
1509 | continue; |
1510 | ||
87bbdf76 | 1511 | cmp = fmt->sort(fmt, a, b); |
043ca389 | 1512 | if (cmp) |
29d720ed NK |
1513 | break; |
1514 | } | |
1515 | ||
043ca389 | 1516 | return cmp; |
29d720ed NK |
1517 | } |
1518 | ||
9283ba9b NK |
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 | ||
f7fb538a NK |
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 | ||
1a3906a7 NK |
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; | |
1b2dbbf4 | 1579 | struct perf_hpp_fmt *fmt; |
1a3906a7 NK |
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); | |
abab5e7f NK |
1593 | |
1594 | /* update column width of dynamic entry */ | |
1b2dbbf4 NK |
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 | } | |
1a3906a7 NK |
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 | ||
c72ab446 NK |
1623 | hists->nr_entries++; |
1624 | if (!he->filtered) { | |
1625 | hists->nr_non_filtered_entries++; | |
1626 | hists__calc_col_len(hists, he); | |
1627 | } | |
1628 | ||
1a3906a7 NK |
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); | |
1a3906a7 NK |
1635 | continue; |
1636 | } | |
1637 | ||
1a3906a7 NK |
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 | ||
1c02c4d2 ACM |
1655 | static void __hists__insert_output_entry(struct rb_root *entries, |
1656 | struct hist_entry *he, | |
f9db0d0f KL |
1657 | u64 min_callchain_hits, |
1658 | bool use_callchain) | |
3d1d07ec | 1659 | { |
1c02c4d2 | 1660 | struct rb_node **p = &entries->rb_node; |
3d1d07ec JK |
1661 | struct rb_node *parent = NULL; |
1662 | struct hist_entry *iter; | |
abab5e7f | 1663 | struct perf_hpp_fmt *fmt; |
3d1d07ec | 1664 | |
744070e0 NK |
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 | } | |
b9fb9304 | 1674 | callchain_param.sort(&he->sorted_chain, he->callchain, |
3d1d07ec | 1675 | min_callchain_hits, &callchain_param); |
744070e0 | 1676 | } |
3d1d07ec JK |
1677 | |
1678 | while (*p != NULL) { | |
1679 | parent = *p; | |
1680 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
1681 | ||
043ca389 | 1682 | if (hist_entry__sort(he, iter) > 0) |
3d1d07ec JK |
1683 | p = &(*p)->rb_left; |
1684 | else | |
1685 | p = &(*p)->rb_right; | |
1686 | } | |
1687 | ||
1688 | rb_link_node(&he->rb_node, parent, p); | |
1c02c4d2 | 1689 | rb_insert_color(&he->rb_node, entries); |
abab5e7f NK |
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 | } | |
3d1d07ec JK |
1696 | } |
1697 | ||
01441af5 | 1698 | static void output_resort(struct hists *hists, struct ui_progress *prog, |
52c5cc36 | 1699 | bool use_callchain, hists__resort_cb_t cb) |
3d1d07ec | 1700 | { |
1980c2eb | 1701 | struct rb_root *root; |
3d1d07ec JK |
1702 | struct rb_node *next; |
1703 | struct hist_entry *n; | |
467ef10c | 1704 | u64 callchain_total; |
3d1d07ec JK |
1705 | u64 min_callchain_hits; |
1706 | ||
467ef10c NK |
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); | |
3d1d07ec | 1712 | |
1a3906a7 NK |
1713 | hists__reset_stats(hists); |
1714 | hists__reset_col_len(hists); | |
1715 | ||
1716 | if (symbol_conf.report_hierarchy) { | |
f7fb538a NK |
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; | |
1a3906a7 NK |
1724 | } |
1725 | ||
52225036 | 1726 | if (hists__has(hists, need_collapse)) |
1980c2eb ACM |
1727 | root = &hists->entries_collapsed; |
1728 | else | |
1729 | root = hists->entries_in; | |
1730 | ||
1731 | next = rb_first(root); | |
1732 | hists->entries = RB_ROOT; | |
3d1d07ec JK |
1733 | |
1734 | while (next) { | |
1980c2eb ACM |
1735 | n = rb_entry(next, struct hist_entry, rb_node_in); |
1736 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 1737 | |
52c5cc36 JO |
1738 | if (cb && cb(n)) |
1739 | continue; | |
1740 | ||
f9db0d0f | 1741 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain); |
6263835a | 1742 | hists__inc_stats(hists, n); |
ae993efc NK |
1743 | |
1744 | if (!n->filtered) | |
1745 | hists__calc_col_len(hists, n); | |
740b97f9 NK |
1746 | |
1747 | if (prog) | |
1748 | ui_progress__update(prog, 1); | |
3d1d07ec | 1749 | } |
1980c2eb | 1750 | } |
b9bf0892 | 1751 | |
452ce03b | 1752 | void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog) |
01441af5 | 1753 | { |
01441af5 JO |
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 | ||
52c5cc36 | 1761 | output_resort(evsel__hists(evsel), prog, use_callchain, NULL); |
452ce03b JO |
1762 | } |
1763 | ||
1764 | void hists__output_resort(struct hists *hists, struct ui_progress *prog) | |
1765 | { | |
52c5cc36 JO |
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); | |
01441af5 JO |
1773 | } |
1774 | ||
8c01872f NK |
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 | ||
a7b5895b NK |
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 | ||
42b28ac0 | 1856 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, |
cc5edb0e ACM |
1857 | enum hist_filter filter) |
1858 | { | |
1859 | h->filtered &= ~(1 << filter); | |
155e9aff NK |
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; | |
79dded87 | 1874 | parent->has_no_entry = false; |
155e9aff NK |
1875 | parent->row_offset = 0; |
1876 | parent->nr_rows = 0; | |
1877 | next: | |
1878 | parent = parent->parent_he; | |
1879 | } | |
1880 | } | |
1881 | ||
cc5edb0e ACM |
1882 | if (h->filtered) |
1883 | return; | |
1884 | ||
87e90f43 | 1885 | /* force fold unfiltered entry for simplicity */ |
3698dab1 | 1886 | h->unfolded = false; |
79dded87 | 1887 | h->has_no_entry = false; |
0f0cbf7a | 1888 | h->row_offset = 0; |
a8cd1f43 | 1889 | h->nr_rows = 0; |
9283ba9b | 1890 | |
1ab1fa5d | 1891 | hists->stats.nr_non_filtered_samples += h->stat.nr_events; |
cc5edb0e | 1892 | |
9283ba9b | 1893 | hists__inc_filter_stats(hists, h); |
42b28ac0 | 1894 | hists__calc_col_len(hists, h); |
cc5edb0e ACM |
1895 | } |
1896 | ||
90cf1fb5 ACM |
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 | ||
90cf1fb5 ACM |
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 | ||
e94d53eb NK |
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 | ||
21394d94 KL |
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 | ||
1f7c2541 NK |
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) | |
84734b06 KL |
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 | ||
1f7c2541 | 1961 | if (filter(hists, h)) |
84734b06 KL |
1962 | continue; |
1963 | ||
1f7c2541 | 1964 | hists__remove_entry_filter(hists, h, type); |
84734b06 KL |
1965 | } |
1966 | } | |
1967 | ||
70642850 NK |
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 | ||
155e9aff NK |
2005 | static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg) |
2006 | { | |
2007 | struct rb_node *nd; | |
70642850 | 2008 | struct rb_root new_root = RB_ROOT; |
155e9aff NK |
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 | } | |
70642850 | 2052 | |
f7fb538a NK |
2053 | hierarchy_recalc_total_periods(hists); |
2054 | ||
70642850 NK |
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; | |
155e9aff NK |
2070 | } |
2071 | ||
1f7c2541 NK |
2072 | void hists__filter_by_thread(struct hists *hists) |
2073 | { | |
155e9aff NK |
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); | |
1f7c2541 NK |
2080 | } |
2081 | ||
2082 | void hists__filter_by_dso(struct hists *hists) | |
2083 | { | |
155e9aff NK |
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); | |
1f7c2541 NK |
2090 | } |
2091 | ||
2092 | void hists__filter_by_symbol(struct hists *hists) | |
2093 | { | |
155e9aff NK |
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); | |
1f7c2541 NK |
2100 | } |
2101 | ||
2102 | void hists__filter_by_socket(struct hists *hists) | |
2103 | { | |
155e9aff NK |
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); | |
1f7c2541 NK |
2110 | } |
2111 | ||
28a6b6aa ACM |
2112 | void events_stats__inc(struct events_stats *stats, u32 type) |
2113 | { | |
2114 | ++stats->nr_events[0]; | |
2115 | ++stats->nr_events[type]; | |
2116 | } | |
2117 | ||
42b28ac0 | 2118 | void hists__inc_nr_events(struct hists *hists, u32 type) |
c8446b9b | 2119 | { |
28a6b6aa | 2120 | events_stats__inc(&hists->stats, type); |
c8446b9b | 2121 | } |
95529be4 | 2122 | |
1844dbcb NK |
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 | ||
494d70a1 ACM |
2130 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, |
2131 | struct hist_entry *pair) | |
2132 | { | |
ce74f60e NK |
2133 | struct rb_root *root; |
2134 | struct rb_node **p; | |
494d70a1 ACM |
2135 | struct rb_node *parent = NULL; |
2136 | struct hist_entry *he; | |
354cc40e | 2137 | int64_t cmp; |
494d70a1 | 2138 | |
52225036 | 2139 | if (hists__has(hists, need_collapse)) |
ce74f60e NK |
2140 | root = &hists->entries_collapsed; |
2141 | else | |
2142 | root = hists->entries_in; | |
2143 | ||
2144 | p = &root->rb_node; | |
2145 | ||
494d70a1 ACM |
2146 | while (*p != NULL) { |
2147 | parent = *p; | |
ce74f60e | 2148 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
494d70a1 | 2149 | |
ce74f60e | 2150 | cmp = hist_entry__collapse(he, pair); |
494d70a1 ACM |
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 | ||
a0b51af3 | 2161 | he = hist_entry__new(pair, true); |
494d70a1 | 2162 | if (he) { |
30193d78 ACM |
2163 | memset(&he->stat, 0, sizeof(he->stat)); |
2164 | he->hists = hists; | |
09623d79 KL |
2165 | if (symbol_conf.cumulate_callchain) |
2166 | memset(he->stat_acc, 0, sizeof(he->stat)); | |
ce74f60e NK |
2167 | rb_link_node(&he->rb_node_in, parent, p); |
2168 | rb_insert_color(&he->rb_node_in, root); | |
6263835a | 2169 | hists__inc_stats(hists, he); |
e0af43d2 | 2170 | he->dummy = true; |
494d70a1 ACM |
2171 | } |
2172 | out: | |
2173 | return he; | |
2174 | } | |
2175 | ||
9d97b8f5 NK |
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 | ||
95529be4 ACM |
2220 | static struct hist_entry *hists__find_entry(struct hists *hists, |
2221 | struct hist_entry *he) | |
2222 | { | |
ce74f60e NK |
2223 | struct rb_node *n; |
2224 | ||
52225036 | 2225 | if (hists__has(hists, need_collapse)) |
ce74f60e NK |
2226 | n = hists->entries_collapsed.rb_node; |
2227 | else | |
2228 | n = hists->entries_in->rb_node; | |
95529be4 ACM |
2229 | |
2230 | while (n) { | |
ce74f60e NK |
2231 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); |
2232 | int64_t cmp = hist_entry__collapse(iter, he); | |
95529be4 ACM |
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 | ||
09034de6 NK |
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 | ||
95529be4 ACM |
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 | { | |
ce74f60e | 2295 | struct rb_root *root; |
95529be4 ACM |
2296 | struct rb_node *nd; |
2297 | struct hist_entry *pos, *pair; | |
2298 | ||
09034de6 NK |
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 | ||
52225036 | 2305 | if (hists__has(leader, need_collapse)) |
ce74f60e NK |
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); | |
95529be4 ACM |
2312 | pair = hists__find_entry(other, pos); |
2313 | ||
2314 | if (pair) | |
5fa9041b | 2315 | hist_entry__add_pair(pair, pos); |
95529be4 ACM |
2316 | } |
2317 | } | |
494d70a1 | 2318 | |
9d97b8f5 NK |
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 | ||
494d70a1 ACM |
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 | { | |
ce74f60e | 2370 | struct rb_root *root; |
494d70a1 ACM |
2371 | struct rb_node *nd; |
2372 | struct hist_entry *pos, *pair; | |
2373 | ||
9d97b8f5 NK |
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 | ||
52225036 | 2381 | if (hists__has(other, need_collapse)) |
ce74f60e NK |
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); | |
494d70a1 ACM |
2388 | |
2389 | if (!hist_entry__has_pairs(pos)) { | |
2390 | pair = hists__add_dummy_entry(leader, pos); | |
2391 | if (pair == NULL) | |
2392 | return -1; | |
5fa9041b | 2393 | hist_entry__add_pair(pos, pair); |
494d70a1 ACM |
2394 | } |
2395 | } | |
2396 | ||
2397 | return 0; | |
2398 | } | |
f2148330 | 2399 | |
57849998 AK |
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 | } | |
2a1731fb ACM |
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 | ||
e5cadb93 | 2439 | evlist__for_each_entry(evlist, pos) { |
2a1731fb ACM |
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 | ||
f2148330 NK |
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 | } | |
33db4568 NK |
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; | |
ecc4c561 ACM |
2461 | else { |
2462 | pr_debug("Invalud percentage: %s\n", arg); | |
33db4568 | 2463 | return -1; |
ecc4c561 | 2464 | } |
33db4568 NK |
2465 | |
2466 | return 0; | |
2467 | } | |
0b93da17 NK |
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 | } | |
a635fc51 | 2476 | |
5b65855e | 2477 | int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list) |
a635fc51 | 2478 | { |
a635fc51 ACM |
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); | |
21394d94 | 2485 | hists->socket_filter = -1; |
5b65855e | 2486 | hists->hpp_list = hpp_list; |
c3bc0c43 | 2487 | INIT_LIST_HEAD(&hists->hpp_formats); |
a635fc51 ACM |
2488 | return 0; |
2489 | } | |
2490 | ||
61fa0e94 NK |
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 | ||
17577dec MH |
2513 | static void hists_evsel__exit(struct perf_evsel *evsel) |
2514 | { | |
2515 | struct hists *hists = evsel__hists(evsel); | |
c3bc0c43 NK |
2516 | struct perf_hpp_fmt *fmt, *pos; |
2517 | struct perf_hpp_list_node *node, *tmp; | |
17577dec | 2518 | |
61fa0e94 | 2519 | hists__delete_all_entries(hists); |
c3bc0c43 NK |
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 | } | |
17577dec MH |
2529 | } |
2530 | ||
fc284be9 NK |
2531 | static int hists_evsel__init(struct perf_evsel *evsel) |
2532 | { | |
2533 | struct hists *hists = evsel__hists(evsel); | |
2534 | ||
5b65855e | 2535 | __hists__init(hists, &perf_hpp_list); |
fc284be9 NK |
2536 | return 0; |
2537 | } | |
2538 | ||
a635fc51 ACM |
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), | |
17577dec MH |
2547 | hists_evsel__init, |
2548 | hists_evsel__exit); | |
a635fc51 ACM |
2549 | if (err) |
2550 | fputs("FATAL ERROR: Couldn't setup hists class\n", stderr); | |
2551 | ||
2552 | return err; | |
2553 | } | |
94b3dc38 JO |
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 | } |