Commit | Line | Data |
---|---|---|
8a0ecfb8 | 1 | #include "util.h" |
598357eb | 2 | #include "build-id.h" |
3d1d07ec | 3 | #include "hist.h" |
4e4f06e4 ACM |
4 | #include "session.h" |
5 | #include "sort.h" | |
2a1731fb | 6 | #include "evlist.h" |
29d720ed | 7 | #include "evsel.h" |
69bcb019 | 8 | #include "annotate.h" |
740b97f9 | 9 | #include "ui/progress.h" |
9b33827d | 10 | #include <math.h> |
3d1d07ec | 11 | |
90cf1fb5 ACM |
12 | static bool hists__filter_entry_by_dso(struct hists *hists, |
13 | struct hist_entry *he); | |
14 | static bool hists__filter_entry_by_thread(struct hists *hists, | |
15 | struct hist_entry *he); | |
e94d53eb NK |
16 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
17 | struct hist_entry *he); | |
21394d94 KL |
18 | static bool hists__filter_entry_by_socket(struct hists *hists, |
19 | struct hist_entry *he); | |
90cf1fb5 | 20 | |
42b28ac0 | 21 | u16 hists__col_len(struct hists *hists, enum hist_column col) |
8a6c5b26 | 22 | { |
42b28ac0 | 23 | return hists->col_len[col]; |
8a6c5b26 ACM |
24 | } |
25 | ||
42b28ac0 | 26 | void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 27 | { |
42b28ac0 | 28 | hists->col_len[col] = len; |
8a6c5b26 ACM |
29 | } |
30 | ||
42b28ac0 | 31 | bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 32 | { |
42b28ac0 ACM |
33 | if (len > hists__col_len(hists, col)) { |
34 | hists__set_col_len(hists, col, len); | |
8a6c5b26 ACM |
35 | return true; |
36 | } | |
37 | return false; | |
38 | } | |
39 | ||
7ccf4f90 | 40 | void hists__reset_col_len(struct hists *hists) |
8a6c5b26 ACM |
41 | { |
42 | enum hist_column col; | |
43 | ||
44 | for (col = 0; col < HISTC_NR_COLS; ++col) | |
42b28ac0 | 45 | hists__set_col_len(hists, col, 0); |
8a6c5b26 ACM |
46 | } |
47 | ||
b5387528 RAV |
48 | static void hists__set_unres_dso_col_len(struct hists *hists, int dso) |
49 | { | |
50 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; | |
51 | ||
52 | if (hists__col_len(hists, dso) < unresolved_col_width && | |
53 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && | |
54 | !symbol_conf.dso_list) | |
55 | hists__set_col_len(hists, dso, unresolved_col_width); | |
56 | } | |
57 | ||
7ccf4f90 | 58 | void hists__calc_col_len(struct hists *hists, struct hist_entry *h) |
8a6c5b26 | 59 | { |
b5387528 | 60 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
98a3b32c | 61 | int symlen; |
8a6c5b26 ACM |
62 | u16 len; |
63 | ||
ded19d57 NK |
64 | /* |
65 | * +4 accounts for '[x] ' priv level info | |
66 | * +2 accounts for 0x prefix on raw addresses | |
67 | * +3 accounts for ' y ' symtab origin info | |
68 | */ | |
69 | if (h->ms.sym) { | |
70 | symlen = h->ms.sym->namelen + 4; | |
71 | if (verbose) | |
72 | symlen += BITS_PER_LONG / 4 + 2 + 3; | |
73 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); | |
74 | } else { | |
98a3b32c SE |
75 | symlen = unresolved_col_width + 4 + 2; |
76 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); | |
b5387528 | 77 | hists__set_unres_dso_col_len(hists, HISTC_DSO); |
98a3b32c | 78 | } |
8a6c5b26 ACM |
79 | |
80 | len = thread__comm_len(h->thread); | |
42b28ac0 ACM |
81 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
82 | hists__set_col_len(hists, HISTC_THREAD, len + 6); | |
8a6c5b26 ACM |
83 | |
84 | if (h->ms.map) { | |
85 | len = dso__name_len(h->ms.map->dso); | |
42b28ac0 | 86 | hists__new_col_len(hists, HISTC_DSO, len); |
8a6c5b26 | 87 | } |
b5387528 | 88 | |
cb993744 NK |
89 | if (h->parent) |
90 | hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen); | |
91 | ||
b5387528 | 92 | if (h->branch_info) { |
b5387528 RAV |
93 | if (h->branch_info->from.sym) { |
94 | symlen = (int)h->branch_info->from.sym->namelen + 4; | |
ded19d57 NK |
95 | if (verbose) |
96 | symlen += BITS_PER_LONG / 4 + 2 + 3; | |
b5387528 RAV |
97 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); |
98 | ||
99 | symlen = dso__name_len(h->branch_info->from.map->dso); | |
100 | hists__new_col_len(hists, HISTC_DSO_FROM, symlen); | |
101 | } else { | |
102 | symlen = unresolved_col_width + 4 + 2; | |
103 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); | |
104 | hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM); | |
105 | } | |
106 | ||
107 | if (h->branch_info->to.sym) { | |
108 | symlen = (int)h->branch_info->to.sym->namelen + 4; | |
ded19d57 NK |
109 | if (verbose) |
110 | symlen += BITS_PER_LONG / 4 + 2 + 3; | |
b5387528 RAV |
111 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
112 | ||
113 | symlen = dso__name_len(h->branch_info->to.map->dso); | |
114 | hists__new_col_len(hists, HISTC_DSO_TO, symlen); | |
115 | } else { | |
116 | symlen = unresolved_col_width + 4 + 2; | |
117 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); | |
118 | hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); | |
119 | } | |
120 | } | |
98a3b32c SE |
121 | |
122 | if (h->mem_info) { | |
98a3b32c SE |
123 | if (h->mem_info->daddr.sym) { |
124 | symlen = (int)h->mem_info->daddr.sym->namelen + 4 | |
125 | + unresolved_col_width + 2; | |
126 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, | |
127 | symlen); | |
9b32ba71 DZ |
128 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, |
129 | symlen + 1); | |
98a3b32c SE |
130 | } else { |
131 | symlen = unresolved_col_width + 4 + 2; | |
132 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, | |
133 | symlen); | |
0805909f JO |
134 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, |
135 | symlen); | |
98a3b32c | 136 | } |
b34b3bf0 JO |
137 | |
138 | if (h->mem_info->iaddr.sym) { | |
139 | symlen = (int)h->mem_info->iaddr.sym->namelen + 4 | |
140 | + unresolved_col_width + 2; | |
141 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, | |
142 | symlen); | |
143 | } else { | |
144 | symlen = unresolved_col_width + 4 + 2; | |
145 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, | |
146 | symlen); | |
147 | } | |
148 | ||
98a3b32c SE |
149 | if (h->mem_info->daddr.map) { |
150 | symlen = dso__name_len(h->mem_info->daddr.map->dso); | |
151 | hists__new_col_len(hists, HISTC_MEM_DADDR_DSO, | |
152 | symlen); | |
153 | } else { | |
154 | symlen = unresolved_col_width + 4 + 2; | |
155 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); | |
156 | } | |
157 | } else { | |
158 | symlen = unresolved_col_width + 4 + 2; | |
159 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen); | |
b34b3bf0 | 160 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen); |
98a3b32c SE |
161 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); |
162 | } | |
163 | ||
a4978eca | 164 | hists__new_col_len(hists, HISTC_CPU, 3); |
2e7ea3ab | 165 | hists__new_col_len(hists, HISTC_SOCKET, 6); |
98a3b32c SE |
166 | hists__new_col_len(hists, HISTC_MEM_LOCKED, 6); |
167 | hists__new_col_len(hists, HISTC_MEM_TLB, 22); | |
168 | hists__new_col_len(hists, HISTC_MEM_SNOOP, 12); | |
169 | hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3); | |
170 | hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12); | |
171 | hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12); | |
475eeab9 | 172 | |
e8e6d37e ACM |
173 | if (h->srcline) |
174 | hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline)); | |
175 | ||
31191a85 AK |
176 | if (h->srcfile) |
177 | hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile)); | |
178 | ||
475eeab9 AK |
179 | if (h->transaction) |
180 | hists__new_col_len(hists, HISTC_TRANSACTION, | |
181 | hist_entry__transaction_len()); | |
0c0af78d NK |
182 | |
183 | if (h->trace_output) | |
184 | hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output)); | |
8a6c5b26 ACM |
185 | } |
186 | ||
7ccf4f90 NK |
187 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
188 | { | |
189 | struct rb_node *next = rb_first(&hists->entries); | |
190 | struct hist_entry *n; | |
191 | int row = 0; | |
192 | ||
193 | hists__reset_col_len(hists); | |
194 | ||
195 | while (next && row++ < max_rows) { | |
196 | n = rb_entry(next, struct hist_entry, rb_node); | |
197 | if (!n->filtered) | |
198 | hists__calc_col_len(hists, n); | |
199 | next = rb_next(&n->rb_node); | |
200 | } | |
201 | } | |
202 | ||
f39056f9 NK |
203 | static void he_stat__add_cpumode_period(struct he_stat *he_stat, |
204 | unsigned int cpumode, u64 period) | |
a1645ce1 | 205 | { |
28e2a106 | 206 | switch (cpumode) { |
a1645ce1 | 207 | case PERF_RECORD_MISC_KERNEL: |
f39056f9 | 208 | he_stat->period_sys += period; |
a1645ce1 ZY |
209 | break; |
210 | case PERF_RECORD_MISC_USER: | |
f39056f9 | 211 | he_stat->period_us += period; |
a1645ce1 ZY |
212 | break; |
213 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
f39056f9 | 214 | he_stat->period_guest_sys += period; |
a1645ce1 ZY |
215 | break; |
216 | case PERF_RECORD_MISC_GUEST_USER: | |
f39056f9 | 217 | he_stat->period_guest_us += period; |
a1645ce1 ZY |
218 | break; |
219 | default: | |
220 | break; | |
221 | } | |
222 | } | |
223 | ||
05484298 AK |
224 | static void he_stat__add_period(struct he_stat *he_stat, u64 period, |
225 | u64 weight) | |
139c0815 | 226 | { |
98a3b32c | 227 | |
139c0815 | 228 | he_stat->period += period; |
05484298 | 229 | he_stat->weight += weight; |
139c0815 NK |
230 | he_stat->nr_events += 1; |
231 | } | |
232 | ||
233 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) | |
234 | { | |
235 | dest->period += src->period; | |
236 | dest->period_sys += src->period_sys; | |
237 | dest->period_us += src->period_us; | |
238 | dest->period_guest_sys += src->period_guest_sys; | |
239 | dest->period_guest_us += src->period_guest_us; | |
240 | dest->nr_events += src->nr_events; | |
05484298 | 241 | dest->weight += src->weight; |
139c0815 NK |
242 | } |
243 | ||
f39056f9 | 244 | static void he_stat__decay(struct he_stat *he_stat) |
ab81f3fd | 245 | { |
f39056f9 NK |
246 | he_stat->period = (he_stat->period * 7) / 8; |
247 | he_stat->nr_events = (he_stat->nr_events * 7) / 8; | |
05484298 | 248 | /* XXX need decay for weight too? */ |
ab81f3fd ACM |
249 | } |
250 | ||
251 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) | |
252 | { | |
b24c28f7 | 253 | u64 prev_period = he->stat.period; |
3186b681 | 254 | u64 diff; |
c64550cf ACM |
255 | |
256 | if (prev_period == 0) | |
df71d95f | 257 | return true; |
c64550cf | 258 | |
f39056f9 | 259 | he_stat__decay(&he->stat); |
f8be1c8c NK |
260 | if (symbol_conf.cumulate_callchain) |
261 | he_stat__decay(he->stat_acc); | |
42b276a2 | 262 | decay_callchain(he->callchain); |
c64550cf | 263 | |
3186b681 NK |
264 | diff = prev_period - he->stat.period; |
265 | ||
266 | hists->stats.total_period -= diff; | |
c64550cf | 267 | if (!he->filtered) |
3186b681 | 268 | hists->stats.total_non_filtered_period -= diff; |
c64550cf | 269 | |
b24c28f7 | 270 | return he->stat.period == 0; |
ab81f3fd ACM |
271 | } |
272 | ||
956b65e1 ACM |
273 | static void hists__delete_entry(struct hists *hists, struct hist_entry *he) |
274 | { | |
275 | rb_erase(&he->rb_node, &hists->entries); | |
276 | ||
277 | if (sort__need_collapse) | |
278 | rb_erase(&he->rb_node_in, &hists->entries_collapsed); | |
61fa0e94 NK |
279 | else |
280 | rb_erase(&he->rb_node_in, hists->entries_in); | |
956b65e1 ACM |
281 | |
282 | --hists->nr_entries; | |
283 | if (!he->filtered) | |
284 | --hists->nr_non_filtered_entries; | |
285 | ||
286 | hist_entry__delete(he); | |
287 | } | |
288 | ||
3a5714f8 | 289 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) |
ab81f3fd ACM |
290 | { |
291 | struct rb_node *next = rb_first(&hists->entries); | |
292 | struct hist_entry *n; | |
293 | ||
294 | while (next) { | |
295 | n = rb_entry(next, struct hist_entry, rb_node); | |
296 | next = rb_next(&n->rb_node); | |
b079d4e9 ACM |
297 | if (((zap_user && n->level == '.') || |
298 | (zap_kernel && n->level != '.') || | |
4c47f4fc | 299 | hists__decay_entry(hists, n))) { |
956b65e1 | 300 | hists__delete_entry(hists, n); |
ab81f3fd ACM |
301 | } |
302 | } | |
303 | } | |
304 | ||
701937bd NK |
305 | void hists__delete_entries(struct hists *hists) |
306 | { | |
307 | struct rb_node *next = rb_first(&hists->entries); | |
308 | struct hist_entry *n; | |
309 | ||
310 | while (next) { | |
311 | n = rb_entry(next, struct hist_entry, rb_node); | |
312 | next = rb_next(&n->rb_node); | |
313 | ||
956b65e1 | 314 | hists__delete_entry(hists, n); |
701937bd NK |
315 | } |
316 | } | |
317 | ||
3d1d07ec | 318 | /* |
c82ee828 | 319 | * histogram, sorted on item, collects periods |
3d1d07ec JK |
320 | */ |
321 | ||
a0b51af3 NK |
322 | static struct hist_entry *hist_entry__new(struct hist_entry *template, |
323 | bool sample_self) | |
28e2a106 | 324 | { |
f8be1c8c NK |
325 | size_t callchain_size = 0; |
326 | struct hist_entry *he; | |
327 | ||
82aa019e | 328 | if (symbol_conf.use_callchain) |
f8be1c8c NK |
329 | callchain_size = sizeof(struct callchain_root); |
330 | ||
331 | he = zalloc(sizeof(*he) + callchain_size); | |
28e2a106 | 332 | |
12c14278 ACM |
333 | if (he != NULL) { |
334 | *he = *template; | |
c4b35351 | 335 | |
f8be1c8c NK |
336 | if (symbol_conf.cumulate_callchain) { |
337 | he->stat_acc = malloc(sizeof(he->stat)); | |
338 | if (he->stat_acc == NULL) { | |
339 | free(he); | |
340 | return NULL; | |
341 | } | |
342 | memcpy(he->stat_acc, &he->stat, sizeof(he->stat)); | |
a0b51af3 NK |
343 | if (!sample_self) |
344 | memset(&he->stat, 0, sizeof(he->stat)); | |
f8be1c8c NK |
345 | } |
346 | ||
5c24b67a | 347 | map__get(he->ms.map); |
3cf0cb1f SE |
348 | |
349 | if (he->branch_info) { | |
26353a61 NK |
350 | /* |
351 | * This branch info is (a part of) allocated from | |
644f2df2 | 352 | * sample__resolve_bstack() and will be freed after |
26353a61 NK |
353 | * adding new entries. So we need to save a copy. |
354 | */ | |
355 | he->branch_info = malloc(sizeof(*he->branch_info)); | |
356 | if (he->branch_info == NULL) { | |
5c24b67a | 357 | map__zput(he->ms.map); |
f8be1c8c | 358 | free(he->stat_acc); |
26353a61 NK |
359 | free(he); |
360 | return NULL; | |
361 | } | |
362 | ||
363 | memcpy(he->branch_info, template->branch_info, | |
364 | sizeof(*he->branch_info)); | |
365 | ||
5c24b67a ACM |
366 | map__get(he->branch_info->from.map); |
367 | map__get(he->branch_info->to.map); | |
3cf0cb1f SE |
368 | } |
369 | ||
98a3b32c | 370 | if (he->mem_info) { |
5c24b67a ACM |
371 | map__get(he->mem_info->iaddr.map); |
372 | map__get(he->mem_info->daddr.map); | |
98a3b32c SE |
373 | } |
374 | ||
28e2a106 | 375 | if (symbol_conf.use_callchain) |
12c14278 | 376 | callchain_init(he->callchain); |
b821c732 | 377 | |
72392834 NK |
378 | if (he->raw_data) { |
379 | he->raw_data = memdup(he->raw_data, he->raw_size); | |
380 | ||
381 | if (he->raw_data == NULL) { | |
382 | map__put(he->ms.map); | |
383 | if (he->branch_info) { | |
384 | map__put(he->branch_info->from.map); | |
385 | map__put(he->branch_info->to.map); | |
386 | free(he->branch_info); | |
387 | } | |
388 | if (he->mem_info) { | |
389 | map__put(he->mem_info->iaddr.map); | |
390 | map__put(he->mem_info->daddr.map); | |
391 | } | |
392 | free(he->stat_acc); | |
393 | free(he); | |
394 | return NULL; | |
395 | } | |
396 | } | |
b821c732 | 397 | INIT_LIST_HEAD(&he->pairs.node); |
f3b623b8 | 398 | thread__get(he->thread); |
aef810ec NK |
399 | |
400 | if (!symbol_conf.report_hierarchy) | |
401 | he->leaf = true; | |
28e2a106 ACM |
402 | } |
403 | ||
12c14278 | 404 | return he; |
28e2a106 ACM |
405 | } |
406 | ||
7a007ca9 ACM |
407 | static u8 symbol__parent_filter(const struct symbol *parent) |
408 | { | |
409 | if (symbol_conf.exclude_other && parent == NULL) | |
410 | return 1 << HIST_FILTER__PARENT; | |
411 | return 0; | |
412 | } | |
413 | ||
467ef10c NK |
414 | static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period) |
415 | { | |
416 | if (!symbol_conf.use_callchain) | |
417 | return; | |
418 | ||
419 | he->hists->callchain_period += period; | |
420 | if (!he->filtered) | |
421 | he->hists->callchain_non_filtered_period += period; | |
422 | } | |
423 | ||
e7e0efcd ACM |
424 | static struct hist_entry *hists__findnew_entry(struct hists *hists, |
425 | struct hist_entry *entry, | |
426 | struct addr_location *al, | |
427 | bool sample_self) | |
9735abf1 | 428 | { |
1980c2eb | 429 | struct rb_node **p; |
9735abf1 ACM |
430 | struct rb_node *parent = NULL; |
431 | struct hist_entry *he; | |
354cc40e | 432 | int64_t cmp; |
f1cbf78d NK |
433 | u64 period = entry->stat.period; |
434 | u64 weight = entry->stat.weight; | |
9735abf1 | 435 | |
1980c2eb ACM |
436 | p = &hists->entries_in->rb_node; |
437 | ||
9735abf1 ACM |
438 | while (*p != NULL) { |
439 | parent = *p; | |
1980c2eb | 440 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
9735abf1 | 441 | |
9afcf930 NK |
442 | /* |
443 | * Make sure that it receives arguments in a same order as | |
444 | * hist_entry__collapse() so that we can use an appropriate | |
445 | * function when searching an entry regardless which sort | |
446 | * keys were used. | |
447 | */ | |
448 | cmp = hist_entry__cmp(he, entry); | |
9735abf1 ACM |
449 | |
450 | if (!cmp) { | |
0f58474e | 451 | if (sample_self) { |
a0b51af3 | 452 | he_stat__add_period(&he->stat, period, weight); |
467ef10c | 453 | hist_entry__add_callchain_period(he, period); |
0f58474e | 454 | } |
f8be1c8c NK |
455 | if (symbol_conf.cumulate_callchain) |
456 | he_stat__add_period(he->stat_acc, period, weight); | |
63fa471d | 457 | |
ceb2acbc | 458 | /* |
e80faac0 | 459 | * This mem info was allocated from sample__resolve_mem |
ceb2acbc NK |
460 | * and will not be used anymore. |
461 | */ | |
74cf249d | 462 | zfree(&entry->mem_info); |
ceb2acbc | 463 | |
63fa471d DM |
464 | /* If the map of an existing hist_entry has |
465 | * become out-of-date due to an exec() or | |
466 | * similar, update it. Otherwise we will | |
467 | * mis-adjust symbol addresses when computing | |
468 | * the history counter to increment. | |
469 | */ | |
470 | if (he->ms.map != entry->ms.map) { | |
5c24b67a ACM |
471 | map__put(he->ms.map); |
472 | he->ms.map = map__get(entry->ms.map); | |
63fa471d | 473 | } |
28e2a106 | 474 | goto out; |
9735abf1 ACM |
475 | } |
476 | ||
477 | if (cmp < 0) | |
478 | p = &(*p)->rb_left; | |
479 | else | |
480 | p = &(*p)->rb_right; | |
481 | } | |
482 | ||
a0b51af3 | 483 | he = hist_entry__new(entry, sample_self); |
9735abf1 | 484 | if (!he) |
27a0dcb7 | 485 | return NULL; |
1980c2eb | 486 | |
0f58474e | 487 | if (sample_self) |
467ef10c NK |
488 | hist_entry__add_callchain_period(he, period); |
489 | hists->nr_entries++; | |
590cd344 | 490 | |
1980c2eb ACM |
491 | rb_link_node(&he->rb_node_in, parent, p); |
492 | rb_insert_color(&he->rb_node_in, hists->entries_in); | |
28e2a106 | 493 | out: |
a0b51af3 NK |
494 | if (sample_self) |
495 | he_stat__add_cpumode_period(&he->stat, al->cpumode, period); | |
f8be1c8c NK |
496 | if (symbol_conf.cumulate_callchain) |
497 | he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period); | |
9735abf1 ACM |
498 | return he; |
499 | } | |
500 | ||
c824c433 | 501 | struct hist_entry *__hists__add_entry(struct hists *hists, |
b5387528 | 502 | struct addr_location *al, |
41a4e6e2 NK |
503 | struct symbol *sym_parent, |
504 | struct branch_info *bi, | |
505 | struct mem_info *mi, | |
fd36f3dd | 506 | struct perf_sample *sample, |
a0b51af3 | 507 | bool sample_self) |
b5387528 RAV |
508 | { |
509 | struct hist_entry entry = { | |
510 | .thread = al->thread, | |
4dfced35 | 511 | .comm = thread__comm(al->thread), |
b5387528 RAV |
512 | .ms = { |
513 | .map = al->map, | |
514 | .sym = al->sym, | |
515 | }, | |
0c4c4deb | 516 | .socket = al->socket, |
7365be55 DZ |
517 | .cpu = al->cpu, |
518 | .cpumode = al->cpumode, | |
519 | .ip = al->addr, | |
520 | .level = al->level, | |
b24c28f7 | 521 | .stat = { |
c4b35351 | 522 | .nr_events = 1, |
fd36f3dd NK |
523 | .period = sample->period, |
524 | .weight = sample->weight, | |
b24c28f7 | 525 | }, |
b5387528 | 526 | .parent = sym_parent, |
2c86c7ca | 527 | .filtered = symbol__parent_filter(sym_parent) | al->filtered, |
c824c433 | 528 | .hists = hists, |
41a4e6e2 NK |
529 | .branch_info = bi, |
530 | .mem_info = mi, | |
fd36f3dd | 531 | .transaction = sample->transaction, |
72392834 NK |
532 | .raw_data = sample->raw_data, |
533 | .raw_size = sample->raw_size, | |
b5387528 RAV |
534 | }; |
535 | ||
e7e0efcd | 536 | return hists__findnew_entry(hists, &entry, al, sample_self); |
b5387528 RAV |
537 | } |
538 | ||
69bcb019 NK |
539 | static int |
540 | iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, | |
541 | struct addr_location *al __maybe_unused) | |
542 | { | |
543 | return 0; | |
544 | } | |
545 | ||
546 | static int | |
547 | iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, | |
548 | struct addr_location *al __maybe_unused) | |
549 | { | |
550 | return 0; | |
551 | } | |
552 | ||
553 | static int | |
554 | iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
555 | { | |
556 | struct perf_sample *sample = iter->sample; | |
557 | struct mem_info *mi; | |
558 | ||
559 | mi = sample__resolve_mem(sample, al); | |
560 | if (mi == NULL) | |
561 | return -ENOMEM; | |
562 | ||
563 | iter->priv = mi; | |
564 | return 0; | |
565 | } | |
566 | ||
567 | static int | |
568 | iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
569 | { | |
570 | u64 cost; | |
571 | struct mem_info *mi = iter->priv; | |
4ea062ed | 572 | struct hists *hists = evsel__hists(iter->evsel); |
fd36f3dd | 573 | struct perf_sample *sample = iter->sample; |
69bcb019 NK |
574 | struct hist_entry *he; |
575 | ||
576 | if (mi == NULL) | |
577 | return -EINVAL; | |
578 | ||
fd36f3dd | 579 | cost = sample->weight; |
69bcb019 NK |
580 | if (!cost) |
581 | cost = 1; | |
582 | ||
583 | /* | |
584 | * must pass period=weight in order to get the correct | |
585 | * sorting from hists__collapse_resort() which is solely | |
586 | * based on periods. We want sorting be done on nr_events * weight | |
587 | * and this is indirectly achieved by passing period=weight here | |
588 | * and the he_stat__add_period() function. | |
589 | */ | |
fd36f3dd NK |
590 | sample->period = cost; |
591 | ||
4ea062ed | 592 | he = __hists__add_entry(hists, al, iter->parent, NULL, mi, |
fd36f3dd | 593 | sample, true); |
69bcb019 NK |
594 | if (!he) |
595 | return -ENOMEM; | |
596 | ||
597 | iter->he = he; | |
598 | return 0; | |
599 | } | |
600 | ||
601 | static int | |
9d3c02d7 NK |
602 | iter_finish_mem_entry(struct hist_entry_iter *iter, |
603 | struct addr_location *al __maybe_unused) | |
69bcb019 NK |
604 | { |
605 | struct perf_evsel *evsel = iter->evsel; | |
4ea062ed | 606 | struct hists *hists = evsel__hists(evsel); |
69bcb019 | 607 | struct hist_entry *he = iter->he; |
69bcb019 NK |
608 | int err = -EINVAL; |
609 | ||
610 | if (he == NULL) | |
611 | goto out; | |
612 | ||
4ea062ed | 613 | hists__inc_nr_samples(hists, he->filtered); |
69bcb019 NK |
614 | |
615 | err = hist_entry__append_callchain(he, iter->sample); | |
616 | ||
617 | out: | |
618 | /* | |
e7e0efcd ACM |
619 | * We don't need to free iter->priv (mem_info) here since the mem info |
620 | * was either already freed in hists__findnew_entry() or passed to a | |
621 | * new hist entry by hist_entry__new(). | |
69bcb019 NK |
622 | */ |
623 | iter->priv = NULL; | |
624 | ||
625 | iter->he = NULL; | |
626 | return err; | |
627 | } | |
628 | ||
629 | static int | |
630 | iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
631 | { | |
632 | struct branch_info *bi; | |
633 | struct perf_sample *sample = iter->sample; | |
634 | ||
635 | bi = sample__resolve_bstack(sample, al); | |
636 | if (!bi) | |
637 | return -ENOMEM; | |
638 | ||
639 | iter->curr = 0; | |
640 | iter->total = sample->branch_stack->nr; | |
641 | ||
642 | iter->priv = bi; | |
643 | return 0; | |
644 | } | |
645 | ||
646 | static int | |
647 | iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused, | |
648 | struct addr_location *al __maybe_unused) | |
649 | { | |
9d3c02d7 NK |
650 | /* to avoid calling callback function */ |
651 | iter->he = NULL; | |
652 | ||
69bcb019 NK |
653 | return 0; |
654 | } | |
655 | ||
656 | static int | |
657 | iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
658 | { | |
659 | struct branch_info *bi = iter->priv; | |
660 | int i = iter->curr; | |
661 | ||
662 | if (bi == NULL) | |
663 | return 0; | |
664 | ||
665 | if (iter->curr >= iter->total) | |
666 | return 0; | |
667 | ||
668 | al->map = bi[i].to.map; | |
669 | al->sym = bi[i].to.sym; | |
670 | al->addr = bi[i].to.addr; | |
671 | return 1; | |
672 | } | |
673 | ||
674 | static int | |
675 | iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
676 | { | |
9d3c02d7 | 677 | struct branch_info *bi; |
69bcb019 | 678 | struct perf_evsel *evsel = iter->evsel; |
4ea062ed | 679 | struct hists *hists = evsel__hists(evsel); |
fd36f3dd | 680 | struct perf_sample *sample = iter->sample; |
69bcb019 NK |
681 | struct hist_entry *he = NULL; |
682 | int i = iter->curr; | |
683 | int err = 0; | |
684 | ||
685 | bi = iter->priv; | |
686 | ||
687 | if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym)) | |
688 | goto out; | |
689 | ||
690 | /* | |
691 | * The report shows the percentage of total branches captured | |
692 | * and not events sampled. Thus we use a pseudo period of 1. | |
693 | */ | |
fd36f3dd NK |
694 | sample->period = 1; |
695 | sample->weight = bi->flags.cycles ? bi->flags.cycles : 1; | |
696 | ||
4ea062ed | 697 | he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL, |
fd36f3dd | 698 | sample, true); |
69bcb019 NK |
699 | if (he == NULL) |
700 | return -ENOMEM; | |
701 | ||
4ea062ed | 702 | hists__inc_nr_samples(hists, he->filtered); |
69bcb019 NK |
703 | |
704 | out: | |
705 | iter->he = he; | |
706 | iter->curr++; | |
707 | return err; | |
708 | } | |
709 | ||
710 | static int | |
711 | iter_finish_branch_entry(struct hist_entry_iter *iter, | |
712 | struct addr_location *al __maybe_unused) | |
713 | { | |
714 | zfree(&iter->priv); | |
715 | iter->he = NULL; | |
716 | ||
717 | return iter->curr >= iter->total ? 0 : -1; | |
718 | } | |
719 | ||
720 | static int | |
721 | iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused, | |
722 | struct addr_location *al __maybe_unused) | |
723 | { | |
724 | return 0; | |
725 | } | |
726 | ||
727 | static int | |
728 | iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
729 | { | |
730 | struct perf_evsel *evsel = iter->evsel; | |
731 | struct perf_sample *sample = iter->sample; | |
732 | struct hist_entry *he; | |
733 | ||
4ea062ed | 734 | he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, |
fd36f3dd | 735 | sample, true); |
69bcb019 NK |
736 | if (he == NULL) |
737 | return -ENOMEM; | |
738 | ||
739 | iter->he = he; | |
740 | return 0; | |
741 | } | |
742 | ||
743 | static int | |
9d3c02d7 NK |
744 | iter_finish_normal_entry(struct hist_entry_iter *iter, |
745 | struct addr_location *al __maybe_unused) | |
69bcb019 | 746 | { |
69bcb019 NK |
747 | struct hist_entry *he = iter->he; |
748 | struct perf_evsel *evsel = iter->evsel; | |
749 | struct perf_sample *sample = iter->sample; | |
750 | ||
751 | if (he == NULL) | |
752 | return 0; | |
753 | ||
754 | iter->he = NULL; | |
755 | ||
4ea062ed | 756 | hists__inc_nr_samples(evsel__hists(evsel), he->filtered); |
69bcb019 NK |
757 | |
758 | return hist_entry__append_callchain(he, sample); | |
759 | } | |
760 | ||
7a13aa28 | 761 | static int |
96b40f3c | 762 | iter_prepare_cumulative_entry(struct hist_entry_iter *iter, |
7a13aa28 NK |
763 | struct addr_location *al __maybe_unused) |
764 | { | |
b4d3c8bd NK |
765 | struct hist_entry **he_cache; |
766 | ||
7a13aa28 | 767 | callchain_cursor_commit(&callchain_cursor); |
b4d3c8bd NK |
768 | |
769 | /* | |
770 | * This is for detecting cycles or recursions so that they're | |
771 | * cumulated only one time to prevent entries more than 100% | |
772 | * overhead. | |
773 | */ | |
96b40f3c | 774 | he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1)); |
b4d3c8bd NK |
775 | if (he_cache == NULL) |
776 | return -ENOMEM; | |
777 | ||
778 | iter->priv = he_cache; | |
779 | iter->curr = 0; | |
780 | ||
7a13aa28 NK |
781 | return 0; |
782 | } | |
783 | ||
784 | static int | |
785 | iter_add_single_cumulative_entry(struct hist_entry_iter *iter, | |
786 | struct addr_location *al) | |
787 | { | |
788 | struct perf_evsel *evsel = iter->evsel; | |
4ea062ed | 789 | struct hists *hists = evsel__hists(evsel); |
7a13aa28 | 790 | struct perf_sample *sample = iter->sample; |
b4d3c8bd | 791 | struct hist_entry **he_cache = iter->priv; |
7a13aa28 NK |
792 | struct hist_entry *he; |
793 | int err = 0; | |
794 | ||
4ea062ed | 795 | he = __hists__add_entry(hists, al, iter->parent, NULL, NULL, |
fd36f3dd | 796 | sample, true); |
7a13aa28 NK |
797 | if (he == NULL) |
798 | return -ENOMEM; | |
799 | ||
800 | iter->he = he; | |
b4d3c8bd | 801 | he_cache[iter->curr++] = he; |
7a13aa28 | 802 | |
82aa019e | 803 | hist_entry__append_callchain(he, sample); |
be7f855a NK |
804 | |
805 | /* | |
806 | * We need to re-initialize the cursor since callchain_append() | |
807 | * advanced the cursor to the end. | |
808 | */ | |
809 | callchain_cursor_commit(&callchain_cursor); | |
810 | ||
4ea062ed | 811 | hists__inc_nr_samples(hists, he->filtered); |
7a13aa28 NK |
812 | |
813 | return err; | |
814 | } | |
815 | ||
816 | static int | |
817 | iter_next_cumulative_entry(struct hist_entry_iter *iter, | |
818 | struct addr_location *al) | |
819 | { | |
820 | struct callchain_cursor_node *node; | |
821 | ||
822 | node = callchain_cursor_current(&callchain_cursor); | |
823 | if (node == NULL) | |
824 | return 0; | |
825 | ||
c7405d85 | 826 | return fill_callchain_info(al, node, iter->hide_unresolved); |
7a13aa28 NK |
827 | } |
828 | ||
829 | static int | |
830 | iter_add_next_cumulative_entry(struct hist_entry_iter *iter, | |
831 | struct addr_location *al) | |
832 | { | |
833 | struct perf_evsel *evsel = iter->evsel; | |
834 | struct perf_sample *sample = iter->sample; | |
b4d3c8bd | 835 | struct hist_entry **he_cache = iter->priv; |
7a13aa28 | 836 | struct hist_entry *he; |
b4d3c8bd | 837 | struct hist_entry he_tmp = { |
5cef8976 | 838 | .hists = evsel__hists(evsel), |
b4d3c8bd NK |
839 | .cpu = al->cpu, |
840 | .thread = al->thread, | |
841 | .comm = thread__comm(al->thread), | |
842 | .ip = al->addr, | |
843 | .ms = { | |
844 | .map = al->map, | |
845 | .sym = al->sym, | |
846 | }, | |
847 | .parent = iter->parent, | |
72392834 NK |
848 | .raw_data = sample->raw_data, |
849 | .raw_size = sample->raw_size, | |
b4d3c8bd NK |
850 | }; |
851 | int i; | |
be7f855a NK |
852 | struct callchain_cursor cursor; |
853 | ||
854 | callchain_cursor_snapshot(&cursor, &callchain_cursor); | |
855 | ||
856 | callchain_cursor_advance(&callchain_cursor); | |
b4d3c8bd NK |
857 | |
858 | /* | |
859 | * Check if there's duplicate entries in the callchain. | |
860 | * It's possible that it has cycles or recursive calls. | |
861 | */ | |
862 | for (i = 0; i < iter->curr; i++) { | |
9d3c02d7 NK |
863 | if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) { |
864 | /* to avoid calling callback function */ | |
865 | iter->he = NULL; | |
b4d3c8bd | 866 | return 0; |
9d3c02d7 | 867 | } |
b4d3c8bd | 868 | } |
7a13aa28 | 869 | |
4ea062ed | 870 | he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, |
fd36f3dd | 871 | sample, false); |
7a13aa28 NK |
872 | if (he == NULL) |
873 | return -ENOMEM; | |
874 | ||
875 | iter->he = he; | |
b4d3c8bd | 876 | he_cache[iter->curr++] = he; |
7a13aa28 | 877 | |
82aa019e NK |
878 | if (symbol_conf.use_callchain) |
879 | callchain_append(he->callchain, &cursor, sample->period); | |
7a13aa28 NK |
880 | return 0; |
881 | } | |
882 | ||
883 | static int | |
884 | iter_finish_cumulative_entry(struct hist_entry_iter *iter, | |
885 | struct addr_location *al __maybe_unused) | |
886 | { | |
b4d3c8bd | 887 | zfree(&iter->priv); |
7a13aa28 | 888 | iter->he = NULL; |
b4d3c8bd | 889 | |
7a13aa28 NK |
890 | return 0; |
891 | } | |
892 | ||
69bcb019 NK |
893 | const struct hist_iter_ops hist_iter_mem = { |
894 | .prepare_entry = iter_prepare_mem_entry, | |
895 | .add_single_entry = iter_add_single_mem_entry, | |
896 | .next_entry = iter_next_nop_entry, | |
897 | .add_next_entry = iter_add_next_nop_entry, | |
898 | .finish_entry = iter_finish_mem_entry, | |
899 | }; | |
900 | ||
901 | const struct hist_iter_ops hist_iter_branch = { | |
902 | .prepare_entry = iter_prepare_branch_entry, | |
903 | .add_single_entry = iter_add_single_branch_entry, | |
904 | .next_entry = iter_next_branch_entry, | |
905 | .add_next_entry = iter_add_next_branch_entry, | |
906 | .finish_entry = iter_finish_branch_entry, | |
907 | }; | |
908 | ||
909 | const struct hist_iter_ops hist_iter_normal = { | |
910 | .prepare_entry = iter_prepare_normal_entry, | |
911 | .add_single_entry = iter_add_single_normal_entry, | |
912 | .next_entry = iter_next_nop_entry, | |
913 | .add_next_entry = iter_add_next_nop_entry, | |
914 | .finish_entry = iter_finish_normal_entry, | |
915 | }; | |
916 | ||
7a13aa28 NK |
917 | const struct hist_iter_ops hist_iter_cumulative = { |
918 | .prepare_entry = iter_prepare_cumulative_entry, | |
919 | .add_single_entry = iter_add_single_cumulative_entry, | |
920 | .next_entry = iter_next_cumulative_entry, | |
921 | .add_next_entry = iter_add_next_cumulative_entry, | |
922 | .finish_entry = iter_finish_cumulative_entry, | |
923 | }; | |
924 | ||
69bcb019 | 925 | int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al, |
9d3c02d7 | 926 | int max_stack_depth, void *arg) |
69bcb019 NK |
927 | { |
928 | int err, err2; | |
929 | ||
063bd936 NK |
930 | err = sample__resolve_callchain(iter->sample, &iter->parent, |
931 | iter->evsel, al, max_stack_depth); | |
69bcb019 NK |
932 | if (err) |
933 | return err; | |
934 | ||
96b40f3c AH |
935 | iter->max_stack = max_stack_depth; |
936 | ||
69bcb019 NK |
937 | err = iter->ops->prepare_entry(iter, al); |
938 | if (err) | |
939 | goto out; | |
940 | ||
941 | err = iter->ops->add_single_entry(iter, al); | |
942 | if (err) | |
943 | goto out; | |
944 | ||
9d3c02d7 NK |
945 | if (iter->he && iter->add_entry_cb) { |
946 | err = iter->add_entry_cb(iter, al, true, arg); | |
947 | if (err) | |
948 | goto out; | |
949 | } | |
950 | ||
69bcb019 NK |
951 | while (iter->ops->next_entry(iter, al)) { |
952 | err = iter->ops->add_next_entry(iter, al); | |
953 | if (err) | |
954 | break; | |
9d3c02d7 NK |
955 | |
956 | if (iter->he && iter->add_entry_cb) { | |
957 | err = iter->add_entry_cb(iter, al, false, arg); | |
958 | if (err) | |
959 | goto out; | |
960 | } | |
69bcb019 NK |
961 | } |
962 | ||
963 | out: | |
964 | err2 = iter->ops->finish_entry(iter, al); | |
965 | if (!err) | |
966 | err = err2; | |
967 | ||
968 | return err; | |
969 | } | |
970 | ||
3d1d07ec JK |
971 | int64_t |
972 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
973 | { | |
aa6f50af | 974 | struct hists *hists = left->hists; |
093f0ef3 | 975 | struct perf_hpp_fmt *fmt; |
3d1d07ec JK |
976 | int64_t cmp = 0; |
977 | ||
aa6f50af | 978 | hists__for_each_sort_list(hists, fmt) { |
87bbdf76 | 979 | cmp = fmt->cmp(fmt, left, right); |
3d1d07ec JK |
980 | if (cmp) |
981 | break; | |
982 | } | |
983 | ||
984 | return cmp; | |
985 | } | |
986 | ||
987 | int64_t | |
988 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | |
989 | { | |
aa6f50af | 990 | struct hists *hists = left->hists; |
093f0ef3 | 991 | struct perf_hpp_fmt *fmt; |
3d1d07ec JK |
992 | int64_t cmp = 0; |
993 | ||
aa6f50af | 994 | hists__for_each_sort_list(hists, fmt) { |
87bbdf76 | 995 | cmp = fmt->collapse(fmt, left, right); |
3d1d07ec JK |
996 | if (cmp) |
997 | break; | |
998 | } | |
999 | ||
1000 | return cmp; | |
1001 | } | |
1002 | ||
6733d1bf | 1003 | void hist_entry__delete(struct hist_entry *he) |
3d1d07ec | 1004 | { |
f3b623b8 | 1005 | thread__zput(he->thread); |
5c24b67a ACM |
1006 | map__zput(he->ms.map); |
1007 | ||
1008 | if (he->branch_info) { | |
1009 | map__zput(he->branch_info->from.map); | |
1010 | map__zput(he->branch_info->to.map); | |
1011 | zfree(&he->branch_info); | |
1012 | } | |
1013 | ||
1014 | if (he->mem_info) { | |
1015 | map__zput(he->mem_info->iaddr.map); | |
1016 | map__zput(he->mem_info->daddr.map); | |
1017 | zfree(&he->mem_info); | |
1018 | } | |
1019 | ||
f8be1c8c | 1020 | zfree(&he->stat_acc); |
f048d548 | 1021 | free_srcline(he->srcline); |
31191a85 AK |
1022 | if (he->srcfile && he->srcfile[0]) |
1023 | free(he->srcfile); | |
d114960c | 1024 | free_callchain(he->callchain); |
60517d28 | 1025 | free(he->trace_output); |
72392834 | 1026 | free(he->raw_data); |
3d1d07ec JK |
1027 | free(he); |
1028 | } | |
1029 | ||
89fee709 ACM |
1030 | /* |
1031 | * If this is not the last column, then we need to pad it according to the | |
1032 | * pre-calculated max lenght for this column, otherwise don't bother adding | |
1033 | * spaces because that would break viewing this with, for instance, 'less', | |
1034 | * that would show tons of trailing spaces when a long C++ demangled method | |
1035 | * names is sampled. | |
1036 | */ | |
1037 | int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp, | |
1038 | struct perf_hpp_fmt *fmt, int printed) | |
1039 | { | |
1040 | if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) { | |
1041 | const int width = fmt->width(fmt, hpp, hists_to_evsel(he->hists)); | |
1042 | if (printed < width) { | |
1043 | advance_hpp(hpp, printed); | |
1044 | printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " "); | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | return printed; | |
1049 | } | |
1050 | ||
3d1d07ec JK |
1051 | /* |
1052 | * collapse the histogram | |
1053 | */ | |
1054 | ||
aef810ec NK |
1055 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he); |
1056 | ||
1057 | static struct hist_entry *hierarchy_insert_entry(struct hists *hists, | |
1058 | struct rb_root *root, | |
1059 | struct hist_entry *he, | |
1060 | struct perf_hpp_fmt *fmt) | |
1061 | { | |
1062 | struct rb_node **p = &root->rb_node; | |
1063 | struct rb_node *parent = NULL; | |
1064 | struct hist_entry *iter, *new; | |
1065 | int64_t cmp; | |
1066 | ||
1067 | while (*p != NULL) { | |
1068 | parent = *p; | |
1069 | iter = rb_entry(parent, struct hist_entry, rb_node_in); | |
1070 | ||
1071 | cmp = fmt->collapse(fmt, iter, he); | |
1072 | if (!cmp) { | |
1073 | he_stat__add_stat(&iter->stat, &he->stat); | |
1074 | return iter; | |
1075 | } | |
1076 | ||
1077 | if (cmp < 0) | |
1078 | p = &parent->rb_left; | |
1079 | else | |
1080 | p = &parent->rb_right; | |
1081 | } | |
1082 | ||
1083 | new = hist_entry__new(he, true); | |
1084 | if (new == NULL) | |
1085 | return NULL; | |
1086 | ||
1087 | hists__apply_filters(hists, new); | |
1088 | hists->nr_entries++; | |
1089 | ||
1090 | /* save related format for output */ | |
1091 | new->fmt = fmt; | |
1092 | ||
1093 | /* some fields are now passed to 'new' */ | |
1094 | if (perf_hpp__is_trace_entry(fmt)) | |
1095 | he->trace_output = NULL; | |
1096 | else | |
1097 | new->trace_output = NULL; | |
1098 | ||
1099 | if (perf_hpp__is_srcline_entry(fmt)) | |
1100 | he->srcline = NULL; | |
1101 | else | |
1102 | new->srcline = NULL; | |
1103 | ||
1104 | if (perf_hpp__is_srcfile_entry(fmt)) | |
1105 | he->srcfile = NULL; | |
1106 | else | |
1107 | new->srcfile = NULL; | |
1108 | ||
1109 | rb_link_node(&new->rb_node_in, parent, p); | |
1110 | rb_insert_color(&new->rb_node_in, root); | |
1111 | return new; | |
1112 | } | |
1113 | ||
1114 | static int hists__hierarchy_insert_entry(struct hists *hists, | |
1115 | struct rb_root *root, | |
1116 | struct hist_entry *he) | |
1117 | { | |
1118 | struct perf_hpp_fmt *fmt; | |
1119 | struct hist_entry *new_he = NULL; | |
1120 | struct hist_entry *parent = NULL; | |
1121 | int depth = 0; | |
1122 | int ret = 0; | |
1123 | ||
1124 | hists__for_each_sort_list(hists, fmt) { | |
1125 | if (!perf_hpp__is_sort_entry(fmt) && | |
1126 | !perf_hpp__is_dynamic_entry(fmt)) | |
1127 | continue; | |
1128 | if (perf_hpp__should_skip(fmt, hists)) | |
1129 | continue; | |
1130 | ||
1131 | /* insert copy of 'he' for each fmt into the hierarchy */ | |
1132 | new_he = hierarchy_insert_entry(hists, root, he, fmt); | |
1133 | if (new_he == NULL) { | |
1134 | ret = -1; | |
1135 | break; | |
1136 | } | |
1137 | ||
1138 | root = &new_he->hroot_in; | |
1139 | new_he->parent_he = parent; | |
1140 | new_he->depth = depth++; | |
1141 | parent = new_he; | |
1142 | } | |
1143 | ||
1144 | if (new_he) { | |
1145 | new_he->leaf = true; | |
1146 | ||
1147 | if (symbol_conf.use_callchain) { | |
1148 | callchain_cursor_reset(&callchain_cursor); | |
1149 | if (callchain_merge(&callchain_cursor, | |
1150 | new_he->callchain, | |
1151 | he->callchain) < 0) | |
1152 | ret = -1; | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | /* 'he' is no longer used */ | |
1157 | hist_entry__delete(he); | |
1158 | ||
1159 | /* return 0 (or -1) since it already applied filters */ | |
1160 | return ret; | |
1161 | } | |
1162 | ||
bba58cdf NK |
1163 | int hists__collapse_insert_entry(struct hists *hists, struct rb_root *root, |
1164 | struct hist_entry *he) | |
3d1d07ec | 1165 | { |
b9bf0892 | 1166 | struct rb_node **p = &root->rb_node; |
3d1d07ec JK |
1167 | struct rb_node *parent = NULL; |
1168 | struct hist_entry *iter; | |
1169 | int64_t cmp; | |
1170 | ||
aef810ec NK |
1171 | if (symbol_conf.report_hierarchy) |
1172 | return hists__hierarchy_insert_entry(hists, root, he); | |
1173 | ||
3d1d07ec JK |
1174 | while (*p != NULL) { |
1175 | parent = *p; | |
1980c2eb | 1176 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
3d1d07ec JK |
1177 | |
1178 | cmp = hist_entry__collapse(iter, he); | |
1179 | ||
1180 | if (!cmp) { | |
bba58cdf NK |
1181 | int ret = 0; |
1182 | ||
139c0815 | 1183 | he_stat__add_stat(&iter->stat, &he->stat); |
f8be1c8c NK |
1184 | if (symbol_conf.cumulate_callchain) |
1185 | he_stat__add_stat(iter->stat_acc, he->stat_acc); | |
9ec60972 | 1186 | |
1b3a0e95 | 1187 | if (symbol_conf.use_callchain) { |
47260645 | 1188 | callchain_cursor_reset(&callchain_cursor); |
bba58cdf NK |
1189 | if (callchain_merge(&callchain_cursor, |
1190 | iter->callchain, | |
1191 | he->callchain) < 0) | |
1192 | ret = -1; | |
1b3a0e95 | 1193 | } |
6733d1bf | 1194 | hist_entry__delete(he); |
bba58cdf | 1195 | return ret; |
3d1d07ec JK |
1196 | } |
1197 | ||
1198 | if (cmp < 0) | |
1199 | p = &(*p)->rb_left; | |
1200 | else | |
1201 | p = &(*p)->rb_right; | |
1202 | } | |
740b97f9 | 1203 | hists->nr_entries++; |
3d1d07ec | 1204 | |
1980c2eb ACM |
1205 | rb_link_node(&he->rb_node_in, parent, p); |
1206 | rb_insert_color(&he->rb_node_in, root); | |
bba58cdf | 1207 | return 1; |
3d1d07ec JK |
1208 | } |
1209 | ||
fc284be9 | 1210 | struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
3d1d07ec | 1211 | { |
1980c2eb ACM |
1212 | struct rb_root *root; |
1213 | ||
1214 | pthread_mutex_lock(&hists->lock); | |
1215 | ||
1216 | root = hists->entries_in; | |
1217 | if (++hists->entries_in > &hists->entries_in_array[1]) | |
1218 | hists->entries_in = &hists->entries_in_array[0]; | |
1219 | ||
1220 | pthread_mutex_unlock(&hists->lock); | |
1221 | ||
1222 | return root; | |
1223 | } | |
1224 | ||
90cf1fb5 ACM |
1225 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) |
1226 | { | |
1227 | hists__filter_entry_by_dso(hists, he); | |
1228 | hists__filter_entry_by_thread(hists, he); | |
e94d53eb | 1229 | hists__filter_entry_by_symbol(hists, he); |
21394d94 | 1230 | hists__filter_entry_by_socket(hists, he); |
90cf1fb5 ACM |
1231 | } |
1232 | ||
bba58cdf | 1233 | int hists__collapse_resort(struct hists *hists, struct ui_progress *prog) |
1980c2eb ACM |
1234 | { |
1235 | struct rb_root *root; | |
3d1d07ec JK |
1236 | struct rb_node *next; |
1237 | struct hist_entry *n; | |
bba58cdf | 1238 | int ret; |
3d1d07ec | 1239 | |
3a5714f8 | 1240 | if (!sort__need_collapse) |
bba58cdf | 1241 | return 0; |
3d1d07ec | 1242 | |
740b97f9 NK |
1243 | hists->nr_entries = 0; |
1244 | ||
1980c2eb | 1245 | root = hists__get_rotate_entries_in(hists); |
740b97f9 | 1246 | |
1980c2eb | 1247 | next = rb_first(root); |
b9bf0892 | 1248 | |
3d1d07ec | 1249 | while (next) { |
33e940a2 ACM |
1250 | if (session_done()) |
1251 | break; | |
1980c2eb ACM |
1252 | n = rb_entry(next, struct hist_entry, rb_node_in); |
1253 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 1254 | |
1980c2eb | 1255 | rb_erase(&n->rb_node_in, root); |
bba58cdf NK |
1256 | ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n); |
1257 | if (ret < 0) | |
1258 | return -1; | |
1259 | ||
1260 | if (ret) { | |
90cf1fb5 ACM |
1261 | /* |
1262 | * If it wasn't combined with one of the entries already | |
1263 | * collapsed, we need to apply the filters that may have | |
1264 | * been set by, say, the hist_browser. | |
1265 | */ | |
1266 | hists__apply_filters(hists, n); | |
90cf1fb5 | 1267 | } |
c1fb5651 NK |
1268 | if (prog) |
1269 | ui_progress__update(prog, 1); | |
3d1d07ec | 1270 | } |
bba58cdf | 1271 | return 0; |
1980c2eb | 1272 | } |
b9bf0892 | 1273 | |
043ca389 | 1274 | static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b) |
29d720ed | 1275 | { |
aa6f50af | 1276 | struct hists *hists = a->hists; |
043ca389 NK |
1277 | struct perf_hpp_fmt *fmt; |
1278 | int64_t cmp = 0; | |
29d720ed | 1279 | |
aa6f50af | 1280 | hists__for_each_sort_list(hists, fmt) { |
361459f1 | 1281 | if (perf_hpp__should_skip(fmt, a->hists)) |
e67d49a7 NK |
1282 | continue; |
1283 | ||
87bbdf76 | 1284 | cmp = fmt->sort(fmt, a, b); |
043ca389 | 1285 | if (cmp) |
29d720ed NK |
1286 | break; |
1287 | } | |
1288 | ||
043ca389 | 1289 | return cmp; |
29d720ed NK |
1290 | } |
1291 | ||
9283ba9b NK |
1292 | static void hists__reset_filter_stats(struct hists *hists) |
1293 | { | |
1294 | hists->nr_non_filtered_entries = 0; | |
1295 | hists->stats.total_non_filtered_period = 0; | |
1296 | } | |
1297 | ||
1298 | void hists__reset_stats(struct hists *hists) | |
1299 | { | |
1300 | hists->nr_entries = 0; | |
1301 | hists->stats.total_period = 0; | |
1302 | ||
1303 | hists__reset_filter_stats(hists); | |
1304 | } | |
1305 | ||
1306 | static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h) | |
1307 | { | |
1308 | hists->nr_non_filtered_entries++; | |
1309 | hists->stats.total_non_filtered_period += h->stat.period; | |
1310 | } | |
1311 | ||
1312 | void hists__inc_stats(struct hists *hists, struct hist_entry *h) | |
1313 | { | |
1314 | if (!h->filtered) | |
1315 | hists__inc_filter_stats(hists, h); | |
1316 | ||
1317 | hists->nr_entries++; | |
1318 | hists->stats.total_period += h->stat.period; | |
1319 | } | |
1320 | ||
1a3906a7 NK |
1321 | static void hierarchy_insert_output_entry(struct rb_root *root, |
1322 | struct hist_entry *he) | |
1323 | { | |
1324 | struct rb_node **p = &root->rb_node; | |
1325 | struct rb_node *parent = NULL; | |
1326 | struct hist_entry *iter; | |
1327 | ||
1328 | while (*p != NULL) { | |
1329 | parent = *p; | |
1330 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
1331 | ||
1332 | if (hist_entry__sort(he, iter) > 0) | |
1333 | p = &parent->rb_left; | |
1334 | else | |
1335 | p = &parent->rb_right; | |
1336 | } | |
1337 | ||
1338 | rb_link_node(&he->rb_node, parent, p); | |
1339 | rb_insert_color(&he->rb_node, root); | |
1340 | } | |
1341 | ||
1342 | static void hists__hierarchy_output_resort(struct hists *hists, | |
1343 | struct ui_progress *prog, | |
1344 | struct rb_root *root_in, | |
1345 | struct rb_root *root_out, | |
1346 | u64 min_callchain_hits, | |
1347 | bool use_callchain) | |
1348 | { | |
1349 | struct rb_node *node; | |
1350 | struct hist_entry *he; | |
1351 | ||
1352 | *root_out = RB_ROOT; | |
1353 | node = rb_first(root_in); | |
1354 | ||
1355 | while (node) { | |
1356 | he = rb_entry(node, struct hist_entry, rb_node_in); | |
1357 | node = rb_next(node); | |
1358 | ||
1359 | hierarchy_insert_output_entry(root_out, he); | |
1360 | ||
1361 | if (prog) | |
1362 | ui_progress__update(prog, 1); | |
1363 | ||
1364 | if (!he->leaf) { | |
1365 | hists__hierarchy_output_resort(hists, prog, | |
1366 | &he->hroot_in, | |
1367 | &he->hroot_out, | |
1368 | min_callchain_hits, | |
1369 | use_callchain); | |
1370 | hists->nr_entries++; | |
1371 | if (!he->filtered) { | |
1372 | hists->nr_non_filtered_entries++; | |
1373 | hists__calc_col_len(hists, he); | |
1374 | } | |
1375 | ||
1376 | continue; | |
1377 | } | |
1378 | ||
1379 | /* only update stat for leaf entries to avoid duplication */ | |
1380 | hists__inc_stats(hists, he); | |
1381 | if (!he->filtered) | |
1382 | hists__calc_col_len(hists, he); | |
1383 | ||
1384 | if (!use_callchain) | |
1385 | continue; | |
1386 | ||
1387 | if (callchain_param.mode == CHAIN_GRAPH_REL) { | |
1388 | u64 total = he->stat.period; | |
1389 | ||
1390 | if (symbol_conf.cumulate_callchain) | |
1391 | total = he->stat_acc->period; | |
1392 | ||
1393 | min_callchain_hits = total * (callchain_param.min_percent / 100); | |
1394 | } | |
1395 | ||
1396 | callchain_param.sort(&he->sorted_chain, he->callchain, | |
1397 | min_callchain_hits, &callchain_param); | |
1398 | } | |
1399 | } | |
1400 | ||
1c02c4d2 ACM |
1401 | static void __hists__insert_output_entry(struct rb_root *entries, |
1402 | struct hist_entry *he, | |
f9db0d0f KL |
1403 | u64 min_callchain_hits, |
1404 | bool use_callchain) | |
3d1d07ec | 1405 | { |
1c02c4d2 | 1406 | struct rb_node **p = &entries->rb_node; |
3d1d07ec JK |
1407 | struct rb_node *parent = NULL; |
1408 | struct hist_entry *iter; | |
1409 | ||
744070e0 NK |
1410 | if (use_callchain) { |
1411 | if (callchain_param.mode == CHAIN_GRAPH_REL) { | |
1412 | u64 total = he->stat.period; | |
1413 | ||
1414 | if (symbol_conf.cumulate_callchain) | |
1415 | total = he->stat_acc->period; | |
1416 | ||
1417 | min_callchain_hits = total * (callchain_param.min_percent / 100); | |
1418 | } | |
b9fb9304 | 1419 | callchain_param.sort(&he->sorted_chain, he->callchain, |
3d1d07ec | 1420 | min_callchain_hits, &callchain_param); |
744070e0 | 1421 | } |
3d1d07ec JK |
1422 | |
1423 | while (*p != NULL) { | |
1424 | parent = *p; | |
1425 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
1426 | ||
043ca389 | 1427 | if (hist_entry__sort(he, iter) > 0) |
3d1d07ec JK |
1428 | p = &(*p)->rb_left; |
1429 | else | |
1430 | p = &(*p)->rb_right; | |
1431 | } | |
1432 | ||
1433 | rb_link_node(&he->rb_node, parent, p); | |
1c02c4d2 | 1434 | rb_insert_color(&he->rb_node, entries); |
3d1d07ec JK |
1435 | } |
1436 | ||
01441af5 JO |
1437 | static void output_resort(struct hists *hists, struct ui_progress *prog, |
1438 | bool use_callchain) | |
3d1d07ec | 1439 | { |
1980c2eb | 1440 | struct rb_root *root; |
3d1d07ec JK |
1441 | struct rb_node *next; |
1442 | struct hist_entry *n; | |
467ef10c | 1443 | u64 callchain_total; |
3d1d07ec JK |
1444 | u64 min_callchain_hits; |
1445 | ||
467ef10c NK |
1446 | callchain_total = hists->callchain_period; |
1447 | if (symbol_conf.filter_relative) | |
1448 | callchain_total = hists->callchain_non_filtered_period; | |
1449 | ||
1450 | min_callchain_hits = callchain_total * (callchain_param.min_percent / 100); | |
3d1d07ec | 1451 | |
1a3906a7 NK |
1452 | hists__reset_stats(hists); |
1453 | hists__reset_col_len(hists); | |
1454 | ||
1455 | if (symbol_conf.report_hierarchy) { | |
1456 | return hists__hierarchy_output_resort(hists, prog, | |
1457 | &hists->entries_collapsed, | |
1458 | &hists->entries, | |
1459 | min_callchain_hits, | |
1460 | use_callchain); | |
1461 | } | |
1462 | ||
3a5714f8 | 1463 | if (sort__need_collapse) |
1980c2eb ACM |
1464 | root = &hists->entries_collapsed; |
1465 | else | |
1466 | root = hists->entries_in; | |
1467 | ||
1468 | next = rb_first(root); | |
1469 | hists->entries = RB_ROOT; | |
3d1d07ec JK |
1470 | |
1471 | while (next) { | |
1980c2eb ACM |
1472 | n = rb_entry(next, struct hist_entry, rb_node_in); |
1473 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 1474 | |
f9db0d0f | 1475 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain); |
6263835a | 1476 | hists__inc_stats(hists, n); |
ae993efc NK |
1477 | |
1478 | if (!n->filtered) | |
1479 | hists__calc_col_len(hists, n); | |
740b97f9 NK |
1480 | |
1481 | if (prog) | |
1482 | ui_progress__update(prog, 1); | |
3d1d07ec | 1483 | } |
1980c2eb | 1484 | } |
b9bf0892 | 1485 | |
452ce03b | 1486 | void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog) |
01441af5 | 1487 | { |
01441af5 JO |
1488 | bool use_callchain; |
1489 | ||
1490 | if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph) | |
1491 | use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN; | |
1492 | else | |
1493 | use_callchain = symbol_conf.use_callchain; | |
1494 | ||
452ce03b JO |
1495 | output_resort(evsel__hists(evsel), prog, use_callchain); |
1496 | } | |
1497 | ||
1498 | void hists__output_resort(struct hists *hists, struct ui_progress *prog) | |
1499 | { | |
1500 | output_resort(hists, prog, symbol_conf.use_callchain); | |
01441af5 JO |
1501 | } |
1502 | ||
42b28ac0 | 1503 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, |
cc5edb0e ACM |
1504 | enum hist_filter filter) |
1505 | { | |
1506 | h->filtered &= ~(1 << filter); | |
1507 | if (h->filtered) | |
1508 | return; | |
1509 | ||
87e90f43 | 1510 | /* force fold unfiltered entry for simplicity */ |
3698dab1 | 1511 | h->unfolded = false; |
0f0cbf7a | 1512 | h->row_offset = 0; |
a8cd1f43 | 1513 | h->nr_rows = 0; |
9283ba9b | 1514 | |
1ab1fa5d | 1515 | hists->stats.nr_non_filtered_samples += h->stat.nr_events; |
cc5edb0e | 1516 | |
9283ba9b | 1517 | hists__inc_filter_stats(hists, h); |
42b28ac0 | 1518 | hists__calc_col_len(hists, h); |
cc5edb0e ACM |
1519 | } |
1520 | ||
90cf1fb5 ACM |
1521 | |
1522 | static bool hists__filter_entry_by_dso(struct hists *hists, | |
1523 | struct hist_entry *he) | |
1524 | { | |
1525 | if (hists->dso_filter != NULL && | |
1526 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { | |
1527 | he->filtered |= (1 << HIST_FILTER__DSO); | |
1528 | return true; | |
1529 | } | |
1530 | ||
1531 | return false; | |
1532 | } | |
1533 | ||
90cf1fb5 ACM |
1534 | static bool hists__filter_entry_by_thread(struct hists *hists, |
1535 | struct hist_entry *he) | |
1536 | { | |
1537 | if (hists->thread_filter != NULL && | |
1538 | he->thread != hists->thread_filter) { | |
1539 | he->filtered |= (1 << HIST_FILTER__THREAD); | |
1540 | return true; | |
1541 | } | |
1542 | ||
1543 | return false; | |
1544 | } | |
1545 | ||
e94d53eb NK |
1546 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
1547 | struct hist_entry *he) | |
1548 | { | |
1549 | if (hists->symbol_filter_str != NULL && | |
1550 | (!he->ms.sym || strstr(he->ms.sym->name, | |
1551 | hists->symbol_filter_str) == NULL)) { | |
1552 | he->filtered |= (1 << HIST_FILTER__SYMBOL); | |
1553 | return true; | |
1554 | } | |
1555 | ||
1556 | return false; | |
1557 | } | |
1558 | ||
21394d94 KL |
1559 | static bool hists__filter_entry_by_socket(struct hists *hists, |
1560 | struct hist_entry *he) | |
1561 | { | |
1562 | if ((hists->socket_filter > -1) && | |
1563 | (he->socket != hists->socket_filter)) { | |
1564 | he->filtered |= (1 << HIST_FILTER__SOCKET); | |
1565 | return true; | |
1566 | } | |
1567 | ||
1568 | return false; | |
1569 | } | |
1570 | ||
1f7c2541 NK |
1571 | typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he); |
1572 | ||
1573 | static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter) | |
84734b06 KL |
1574 | { |
1575 | struct rb_node *nd; | |
1576 | ||
1577 | hists->stats.nr_non_filtered_samples = 0; | |
1578 | ||
1579 | hists__reset_filter_stats(hists); | |
1580 | hists__reset_col_len(hists); | |
1581 | ||
1582 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { | |
1583 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
1584 | ||
1f7c2541 | 1585 | if (filter(hists, h)) |
84734b06 KL |
1586 | continue; |
1587 | ||
1f7c2541 | 1588 | hists__remove_entry_filter(hists, h, type); |
84734b06 KL |
1589 | } |
1590 | } | |
1591 | ||
1f7c2541 NK |
1592 | void hists__filter_by_thread(struct hists *hists) |
1593 | { | |
1594 | hists__filter_by_type(hists, HIST_FILTER__THREAD, | |
1595 | hists__filter_entry_by_thread); | |
1596 | } | |
1597 | ||
1598 | void hists__filter_by_dso(struct hists *hists) | |
1599 | { | |
1600 | hists__filter_by_type(hists, HIST_FILTER__DSO, | |
1601 | hists__filter_entry_by_dso); | |
1602 | } | |
1603 | ||
1604 | void hists__filter_by_symbol(struct hists *hists) | |
1605 | { | |
1606 | hists__filter_by_type(hists, HIST_FILTER__SYMBOL, | |
1607 | hists__filter_entry_by_symbol); | |
1608 | } | |
1609 | ||
1610 | void hists__filter_by_socket(struct hists *hists) | |
1611 | { | |
1612 | hists__filter_by_type(hists, HIST_FILTER__SOCKET, | |
1613 | hists__filter_entry_by_socket); | |
1614 | } | |
1615 | ||
28a6b6aa ACM |
1616 | void events_stats__inc(struct events_stats *stats, u32 type) |
1617 | { | |
1618 | ++stats->nr_events[0]; | |
1619 | ++stats->nr_events[type]; | |
1620 | } | |
1621 | ||
42b28ac0 | 1622 | void hists__inc_nr_events(struct hists *hists, u32 type) |
c8446b9b | 1623 | { |
28a6b6aa | 1624 | events_stats__inc(&hists->stats, type); |
c8446b9b | 1625 | } |
95529be4 | 1626 | |
1844dbcb NK |
1627 | void hists__inc_nr_samples(struct hists *hists, bool filtered) |
1628 | { | |
1629 | events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE); | |
1630 | if (!filtered) | |
1631 | hists->stats.nr_non_filtered_samples++; | |
1632 | } | |
1633 | ||
494d70a1 ACM |
1634 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, |
1635 | struct hist_entry *pair) | |
1636 | { | |
ce74f60e NK |
1637 | struct rb_root *root; |
1638 | struct rb_node **p; | |
494d70a1 ACM |
1639 | struct rb_node *parent = NULL; |
1640 | struct hist_entry *he; | |
354cc40e | 1641 | int64_t cmp; |
494d70a1 | 1642 | |
ce74f60e NK |
1643 | if (sort__need_collapse) |
1644 | root = &hists->entries_collapsed; | |
1645 | else | |
1646 | root = hists->entries_in; | |
1647 | ||
1648 | p = &root->rb_node; | |
1649 | ||
494d70a1 ACM |
1650 | while (*p != NULL) { |
1651 | parent = *p; | |
ce74f60e | 1652 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
494d70a1 | 1653 | |
ce74f60e | 1654 | cmp = hist_entry__collapse(he, pair); |
494d70a1 ACM |
1655 | |
1656 | if (!cmp) | |
1657 | goto out; | |
1658 | ||
1659 | if (cmp < 0) | |
1660 | p = &(*p)->rb_left; | |
1661 | else | |
1662 | p = &(*p)->rb_right; | |
1663 | } | |
1664 | ||
a0b51af3 | 1665 | he = hist_entry__new(pair, true); |
494d70a1 | 1666 | if (he) { |
30193d78 ACM |
1667 | memset(&he->stat, 0, sizeof(he->stat)); |
1668 | he->hists = hists; | |
ce74f60e NK |
1669 | rb_link_node(&he->rb_node_in, parent, p); |
1670 | rb_insert_color(&he->rb_node_in, root); | |
6263835a | 1671 | hists__inc_stats(hists, he); |
e0af43d2 | 1672 | he->dummy = true; |
494d70a1 ACM |
1673 | } |
1674 | out: | |
1675 | return he; | |
1676 | } | |
1677 | ||
95529be4 ACM |
1678 | static struct hist_entry *hists__find_entry(struct hists *hists, |
1679 | struct hist_entry *he) | |
1680 | { | |
ce74f60e NK |
1681 | struct rb_node *n; |
1682 | ||
1683 | if (sort__need_collapse) | |
1684 | n = hists->entries_collapsed.rb_node; | |
1685 | else | |
1686 | n = hists->entries_in->rb_node; | |
95529be4 ACM |
1687 | |
1688 | while (n) { | |
ce74f60e NK |
1689 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); |
1690 | int64_t cmp = hist_entry__collapse(iter, he); | |
95529be4 ACM |
1691 | |
1692 | if (cmp < 0) | |
1693 | n = n->rb_left; | |
1694 | else if (cmp > 0) | |
1695 | n = n->rb_right; | |
1696 | else | |
1697 | return iter; | |
1698 | } | |
1699 | ||
1700 | return NULL; | |
1701 | } | |
1702 | ||
1703 | /* | |
1704 | * Look for pairs to link to the leader buckets (hist_entries): | |
1705 | */ | |
1706 | void hists__match(struct hists *leader, struct hists *other) | |
1707 | { | |
ce74f60e | 1708 | struct rb_root *root; |
95529be4 ACM |
1709 | struct rb_node *nd; |
1710 | struct hist_entry *pos, *pair; | |
1711 | ||
ce74f60e NK |
1712 | if (sort__need_collapse) |
1713 | root = &leader->entries_collapsed; | |
1714 | else | |
1715 | root = leader->entries_in; | |
1716 | ||
1717 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
1718 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
95529be4 ACM |
1719 | pair = hists__find_entry(other, pos); |
1720 | ||
1721 | if (pair) | |
5fa9041b | 1722 | hist_entry__add_pair(pair, pos); |
95529be4 ACM |
1723 | } |
1724 | } | |
494d70a1 ACM |
1725 | |
1726 | /* | |
1727 | * Look for entries in the other hists that are not present in the leader, if | |
1728 | * we find them, just add a dummy entry on the leader hists, with period=0, | |
1729 | * nr_events=0, to serve as the list header. | |
1730 | */ | |
1731 | int hists__link(struct hists *leader, struct hists *other) | |
1732 | { | |
ce74f60e | 1733 | struct rb_root *root; |
494d70a1 ACM |
1734 | struct rb_node *nd; |
1735 | struct hist_entry *pos, *pair; | |
1736 | ||
ce74f60e NK |
1737 | if (sort__need_collapse) |
1738 | root = &other->entries_collapsed; | |
1739 | else | |
1740 | root = other->entries_in; | |
1741 | ||
1742 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
1743 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
494d70a1 ACM |
1744 | |
1745 | if (!hist_entry__has_pairs(pos)) { | |
1746 | pair = hists__add_dummy_entry(leader, pos); | |
1747 | if (pair == NULL) | |
1748 | return -1; | |
5fa9041b | 1749 | hist_entry__add_pair(pos, pair); |
494d70a1 ACM |
1750 | } |
1751 | } | |
1752 | ||
1753 | return 0; | |
1754 | } | |
f2148330 | 1755 | |
57849998 AK |
1756 | void hist__account_cycles(struct branch_stack *bs, struct addr_location *al, |
1757 | struct perf_sample *sample, bool nonany_branch_mode) | |
1758 | { | |
1759 | struct branch_info *bi; | |
1760 | ||
1761 | /* If we have branch cycles always annotate them. */ | |
1762 | if (bs && bs->nr && bs->entries[0].flags.cycles) { | |
1763 | int i; | |
1764 | ||
1765 | bi = sample__resolve_bstack(sample, al); | |
1766 | if (bi) { | |
1767 | struct addr_map_symbol *prev = NULL; | |
1768 | ||
1769 | /* | |
1770 | * Ignore errors, still want to process the | |
1771 | * other entries. | |
1772 | * | |
1773 | * For non standard branch modes always | |
1774 | * force no IPC (prev == NULL) | |
1775 | * | |
1776 | * Note that perf stores branches reversed from | |
1777 | * program order! | |
1778 | */ | |
1779 | for (i = bs->nr - 1; i >= 0; i--) { | |
1780 | addr_map_symbol__account_cycles(&bi[i].from, | |
1781 | nonany_branch_mode ? NULL : prev, | |
1782 | bi[i].flags.cycles); | |
1783 | prev = &bi[i].to; | |
1784 | } | |
1785 | free(bi); | |
1786 | } | |
1787 | } | |
1788 | } | |
2a1731fb ACM |
1789 | |
1790 | size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp) | |
1791 | { | |
1792 | struct perf_evsel *pos; | |
1793 | size_t ret = 0; | |
1794 | ||
1795 | evlist__for_each(evlist, pos) { | |
1796 | ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos)); | |
1797 | ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp); | |
1798 | } | |
1799 | ||
1800 | return ret; | |
1801 | } | |
1802 | ||
1803 | ||
f2148330 NK |
1804 | u64 hists__total_period(struct hists *hists) |
1805 | { | |
1806 | return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period : | |
1807 | hists->stats.total_period; | |
1808 | } | |
33db4568 NK |
1809 | |
1810 | int parse_filter_percentage(const struct option *opt __maybe_unused, | |
1811 | const char *arg, int unset __maybe_unused) | |
1812 | { | |
1813 | if (!strcmp(arg, "relative")) | |
1814 | symbol_conf.filter_relative = true; | |
1815 | else if (!strcmp(arg, "absolute")) | |
1816 | symbol_conf.filter_relative = false; | |
1817 | else | |
1818 | return -1; | |
1819 | ||
1820 | return 0; | |
1821 | } | |
0b93da17 NK |
1822 | |
1823 | int perf_hist_config(const char *var, const char *value) | |
1824 | { | |
1825 | if (!strcmp(var, "hist.percentage")) | |
1826 | return parse_filter_percentage(NULL, value, 0); | |
1827 | ||
1828 | return 0; | |
1829 | } | |
a635fc51 | 1830 | |
5b65855e | 1831 | int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list) |
a635fc51 | 1832 | { |
a635fc51 ACM |
1833 | memset(hists, 0, sizeof(*hists)); |
1834 | hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT; | |
1835 | hists->entries_in = &hists->entries_in_array[0]; | |
1836 | hists->entries_collapsed = RB_ROOT; | |
1837 | hists->entries = RB_ROOT; | |
1838 | pthread_mutex_init(&hists->lock, NULL); | |
21394d94 | 1839 | hists->socket_filter = -1; |
5b65855e | 1840 | hists->hpp_list = hpp_list; |
a635fc51 ACM |
1841 | return 0; |
1842 | } | |
1843 | ||
61fa0e94 NK |
1844 | static void hists__delete_remaining_entries(struct rb_root *root) |
1845 | { | |
1846 | struct rb_node *node; | |
1847 | struct hist_entry *he; | |
1848 | ||
1849 | while (!RB_EMPTY_ROOT(root)) { | |
1850 | node = rb_first(root); | |
1851 | rb_erase(node, root); | |
1852 | ||
1853 | he = rb_entry(node, struct hist_entry, rb_node_in); | |
1854 | hist_entry__delete(he); | |
1855 | } | |
1856 | } | |
1857 | ||
1858 | static void hists__delete_all_entries(struct hists *hists) | |
1859 | { | |
1860 | hists__delete_entries(hists); | |
1861 | hists__delete_remaining_entries(&hists->entries_in_array[0]); | |
1862 | hists__delete_remaining_entries(&hists->entries_in_array[1]); | |
1863 | hists__delete_remaining_entries(&hists->entries_collapsed); | |
1864 | } | |
1865 | ||
17577dec MH |
1866 | static void hists_evsel__exit(struct perf_evsel *evsel) |
1867 | { | |
1868 | struct hists *hists = evsel__hists(evsel); | |
1869 | ||
61fa0e94 | 1870 | hists__delete_all_entries(hists); |
17577dec MH |
1871 | } |
1872 | ||
fc284be9 NK |
1873 | static int hists_evsel__init(struct perf_evsel *evsel) |
1874 | { | |
1875 | struct hists *hists = evsel__hists(evsel); | |
1876 | ||
5b65855e | 1877 | __hists__init(hists, &perf_hpp_list); |
fc284be9 NK |
1878 | return 0; |
1879 | } | |
1880 | ||
a635fc51 ACM |
1881 | /* |
1882 | * XXX We probably need a hists_evsel__exit() to free the hist_entries | |
1883 | * stored in the rbtree... | |
1884 | */ | |
1885 | ||
1886 | int hists__init(void) | |
1887 | { | |
1888 | int err = perf_evsel__object_config(sizeof(struct hists_evsel), | |
17577dec MH |
1889 | hists_evsel__init, |
1890 | hists_evsel__exit); | |
a635fc51 ACM |
1891 | if (err) |
1892 | fputs("FATAL ERROR: Couldn't setup hists class\n", stderr); | |
1893 | ||
1894 | return err; | |
1895 | } | |
94b3dc38 JO |
1896 | |
1897 | void perf_hpp_list__init(struct perf_hpp_list *list) | |
1898 | { | |
1899 | INIT_LIST_HEAD(&list->fields); | |
1900 | INIT_LIST_HEAD(&list->sorts); | |
1901 | } |