]>
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); | |
90cf1fb5 | 18 | |
42b28ac0 | 19 | u16 hists__col_len(struct hists *hists, enum hist_column col) |
8a6c5b26 | 20 | { |
42b28ac0 | 21 | return hists->col_len[col]; |
8a6c5b26 ACM |
22 | } |
23 | ||
42b28ac0 | 24 | void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 25 | { |
42b28ac0 | 26 | hists->col_len[col] = len; |
8a6c5b26 ACM |
27 | } |
28 | ||
42b28ac0 | 29 | bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 30 | { |
42b28ac0 ACM |
31 | if (len > hists__col_len(hists, col)) { |
32 | hists__set_col_len(hists, col, len); | |
8a6c5b26 ACM |
33 | return true; |
34 | } | |
35 | return false; | |
36 | } | |
37 | ||
7ccf4f90 | 38 | void hists__reset_col_len(struct hists *hists) |
8a6c5b26 ACM |
39 | { |
40 | enum hist_column col; | |
41 | ||
42 | for (col = 0; col < HISTC_NR_COLS; ++col) | |
42b28ac0 | 43 | hists__set_col_len(hists, col, 0); |
8a6c5b26 ACM |
44 | } |
45 | ||
b5387528 RAV |
46 | static void hists__set_unres_dso_col_len(struct hists *hists, int dso) |
47 | { | |
48 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; | |
49 | ||
50 | if (hists__col_len(hists, dso) < unresolved_col_width && | |
51 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && | |
52 | !symbol_conf.dso_list) | |
53 | hists__set_col_len(hists, dso, unresolved_col_width); | |
54 | } | |
55 | ||
7ccf4f90 | 56 | void hists__calc_col_len(struct hists *hists, struct hist_entry *h) |
8a6c5b26 | 57 | { |
b5387528 | 58 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
98a3b32c | 59 | int symlen; |
8a6c5b26 ACM |
60 | u16 len; |
61 | ||
ded19d57 NK |
62 | /* |
63 | * +4 accounts for '[x] ' priv level info | |
64 | * +2 accounts for 0x prefix on raw addresses | |
65 | * +3 accounts for ' y ' symtab origin info | |
66 | */ | |
67 | if (h->ms.sym) { | |
68 | symlen = h->ms.sym->namelen + 4; | |
69 | if (verbose) | |
70 | symlen += BITS_PER_LONG / 4 + 2 + 3; | |
71 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); | |
72 | } else { | |
98a3b32c SE |
73 | symlen = unresolved_col_width + 4 + 2; |
74 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); | |
b5387528 | 75 | hists__set_unres_dso_col_len(hists, HISTC_DSO); |
98a3b32c | 76 | } |
8a6c5b26 ACM |
77 | |
78 | len = thread__comm_len(h->thread); | |
42b28ac0 ACM |
79 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
80 | hists__set_col_len(hists, HISTC_THREAD, len + 6); | |
8a6c5b26 ACM |
81 | |
82 | if (h->ms.map) { | |
83 | len = dso__name_len(h->ms.map->dso); | |
42b28ac0 | 84 | hists__new_col_len(hists, HISTC_DSO, len); |
8a6c5b26 | 85 | } |
b5387528 | 86 | |
cb993744 NK |
87 | if (h->parent) |
88 | hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen); | |
89 | ||
b5387528 | 90 | if (h->branch_info) { |
b5387528 RAV |
91 | if (h->branch_info->from.sym) { |
92 | symlen = (int)h->branch_info->from.sym->namelen + 4; | |
ded19d57 NK |
93 | if (verbose) |
94 | symlen += BITS_PER_LONG / 4 + 2 + 3; | |
b5387528 RAV |
95 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); |
96 | ||
97 | symlen = dso__name_len(h->branch_info->from.map->dso); | |
98 | hists__new_col_len(hists, HISTC_DSO_FROM, symlen); | |
99 | } else { | |
100 | symlen = unresolved_col_width + 4 + 2; | |
101 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); | |
102 | hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM); | |
103 | } | |
104 | ||
105 | if (h->branch_info->to.sym) { | |
106 | symlen = (int)h->branch_info->to.sym->namelen + 4; | |
ded19d57 NK |
107 | if (verbose) |
108 | symlen += BITS_PER_LONG / 4 + 2 + 3; | |
b5387528 RAV |
109 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
110 | ||
111 | symlen = dso__name_len(h->branch_info->to.map->dso); | |
112 | hists__new_col_len(hists, HISTC_DSO_TO, symlen); | |
113 | } else { | |
114 | symlen = unresolved_col_width + 4 + 2; | |
115 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); | |
116 | hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); | |
117 | } | |
118 | } | |
98a3b32c SE |
119 | |
120 | if (h->mem_info) { | |
98a3b32c SE |
121 | if (h->mem_info->daddr.sym) { |
122 | symlen = (int)h->mem_info->daddr.sym->namelen + 4 | |
123 | + unresolved_col_width + 2; | |
124 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, | |
125 | symlen); | |
9b32ba71 DZ |
126 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, |
127 | symlen + 1); | |
98a3b32c SE |
128 | } else { |
129 | symlen = unresolved_col_width + 4 + 2; | |
130 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, | |
131 | symlen); | |
132 | } | |
133 | if (h->mem_info->daddr.map) { | |
134 | symlen = dso__name_len(h->mem_info->daddr.map->dso); | |
135 | hists__new_col_len(hists, HISTC_MEM_DADDR_DSO, | |
136 | symlen); | |
137 | } else { | |
138 | symlen = unresolved_col_width + 4 + 2; | |
139 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); | |
140 | } | |
141 | } else { | |
142 | symlen = unresolved_col_width + 4 + 2; | |
143 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen); | |
144 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); | |
145 | } | |
146 | ||
147 | hists__new_col_len(hists, HISTC_MEM_LOCKED, 6); | |
148 | hists__new_col_len(hists, HISTC_MEM_TLB, 22); | |
149 | hists__new_col_len(hists, HISTC_MEM_SNOOP, 12); | |
150 | hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3); | |
151 | hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12); | |
152 | hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12); | |
475eeab9 AK |
153 | |
154 | if (h->transaction) | |
155 | hists__new_col_len(hists, HISTC_TRANSACTION, | |
156 | hist_entry__transaction_len()); | |
8a6c5b26 ACM |
157 | } |
158 | ||
7ccf4f90 NK |
159 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
160 | { | |
161 | struct rb_node *next = rb_first(&hists->entries); | |
162 | struct hist_entry *n; | |
163 | int row = 0; | |
164 | ||
165 | hists__reset_col_len(hists); | |
166 | ||
167 | while (next && row++ < max_rows) { | |
168 | n = rb_entry(next, struct hist_entry, rb_node); | |
169 | if (!n->filtered) | |
170 | hists__calc_col_len(hists, n); | |
171 | next = rb_next(&n->rb_node); | |
172 | } | |
173 | } | |
174 | ||
f39056f9 NK |
175 | static void he_stat__add_cpumode_period(struct he_stat *he_stat, |
176 | unsigned int cpumode, u64 period) | |
a1645ce1 | 177 | { |
28e2a106 | 178 | switch (cpumode) { |
a1645ce1 | 179 | case PERF_RECORD_MISC_KERNEL: |
f39056f9 | 180 | he_stat->period_sys += period; |
a1645ce1 ZY |
181 | break; |
182 | case PERF_RECORD_MISC_USER: | |
f39056f9 | 183 | he_stat->period_us += period; |
a1645ce1 ZY |
184 | break; |
185 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
f39056f9 | 186 | he_stat->period_guest_sys += period; |
a1645ce1 ZY |
187 | break; |
188 | case PERF_RECORD_MISC_GUEST_USER: | |
f39056f9 | 189 | he_stat->period_guest_us += period; |
a1645ce1 ZY |
190 | break; |
191 | default: | |
192 | break; | |
193 | } | |
194 | } | |
195 | ||
05484298 AK |
196 | static void he_stat__add_period(struct he_stat *he_stat, u64 period, |
197 | u64 weight) | |
139c0815 | 198 | { |
98a3b32c | 199 | |
139c0815 | 200 | he_stat->period += period; |
05484298 | 201 | he_stat->weight += weight; |
139c0815 NK |
202 | he_stat->nr_events += 1; |
203 | } | |
204 | ||
205 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) | |
206 | { | |
207 | dest->period += src->period; | |
208 | dest->period_sys += src->period_sys; | |
209 | dest->period_us += src->period_us; | |
210 | dest->period_guest_sys += src->period_guest_sys; | |
211 | dest->period_guest_us += src->period_guest_us; | |
212 | dest->nr_events += src->nr_events; | |
05484298 | 213 | dest->weight += src->weight; |
139c0815 NK |
214 | } |
215 | ||
f39056f9 | 216 | static void he_stat__decay(struct he_stat *he_stat) |
ab81f3fd | 217 | { |
f39056f9 NK |
218 | he_stat->period = (he_stat->period * 7) / 8; |
219 | he_stat->nr_events = (he_stat->nr_events * 7) / 8; | |
05484298 | 220 | /* XXX need decay for weight too? */ |
ab81f3fd ACM |
221 | } |
222 | ||
223 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) | |
224 | { | |
b24c28f7 | 225 | u64 prev_period = he->stat.period; |
3186b681 | 226 | u64 diff; |
c64550cf ACM |
227 | |
228 | if (prev_period == 0) | |
df71d95f | 229 | return true; |
c64550cf | 230 | |
f39056f9 | 231 | he_stat__decay(&he->stat); |
f8be1c8c NK |
232 | if (symbol_conf.cumulate_callchain) |
233 | he_stat__decay(he->stat_acc); | |
c64550cf | 234 | |
3186b681 NK |
235 | diff = prev_period - he->stat.period; |
236 | ||
237 | hists->stats.total_period -= diff; | |
c64550cf | 238 | if (!he->filtered) |
3186b681 | 239 | hists->stats.total_non_filtered_period -= diff; |
c64550cf | 240 | |
b24c28f7 | 241 | return he->stat.period == 0; |
ab81f3fd ACM |
242 | } |
243 | ||
3a5714f8 | 244 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) |
ab81f3fd ACM |
245 | { |
246 | struct rb_node *next = rb_first(&hists->entries); | |
247 | struct hist_entry *n; | |
248 | ||
249 | while (next) { | |
250 | n = rb_entry(next, struct hist_entry, rb_node); | |
251 | next = rb_next(&n->rb_node); | |
df71d95f ACM |
252 | /* |
253 | * We may be annotating this, for instance, so keep it here in | |
254 | * case some it gets new samples, we'll eventually free it when | |
255 | * the user stops browsing and it agains gets fully decayed. | |
256 | */ | |
b079d4e9 ACM |
257 | if (((zap_user && n->level == '.') || |
258 | (zap_kernel && n->level != '.') || | |
259 | hists__decay_entry(hists, n)) && | |
260 | !n->used) { | |
ab81f3fd ACM |
261 | rb_erase(&n->rb_node, &hists->entries); |
262 | ||
3a5714f8 | 263 | if (sort__need_collapse) |
ab81f3fd ACM |
264 | rb_erase(&n->rb_node_in, &hists->entries_collapsed); |
265 | ||
ab81f3fd | 266 | --hists->nr_entries; |
3186b681 NK |
267 | if (!n->filtered) |
268 | --hists->nr_non_filtered_entries; | |
269 | ||
270 | hist_entry__free(n); | |
ab81f3fd ACM |
271 | } |
272 | } | |
273 | } | |
274 | ||
701937bd NK |
275 | void hists__delete_entries(struct hists *hists) |
276 | { | |
277 | struct rb_node *next = rb_first(&hists->entries); | |
278 | struct hist_entry *n; | |
279 | ||
280 | while (next) { | |
281 | n = rb_entry(next, struct hist_entry, rb_node); | |
282 | next = rb_next(&n->rb_node); | |
283 | ||
284 | rb_erase(&n->rb_node, &hists->entries); | |
285 | ||
286 | if (sort__need_collapse) | |
287 | rb_erase(&n->rb_node_in, &hists->entries_collapsed); | |
288 | ||
289 | --hists->nr_entries; | |
290 | if (!n->filtered) | |
291 | --hists->nr_non_filtered_entries; | |
292 | ||
293 | hist_entry__free(n); | |
294 | } | |
295 | } | |
296 | ||
3d1d07ec | 297 | /* |
c82ee828 | 298 | * histogram, sorted on item, collects periods |
3d1d07ec JK |
299 | */ |
300 | ||
a0b51af3 NK |
301 | static struct hist_entry *hist_entry__new(struct hist_entry *template, |
302 | bool sample_self) | |
28e2a106 | 303 | { |
f8be1c8c NK |
304 | size_t callchain_size = 0; |
305 | struct hist_entry *he; | |
306 | ||
82aa019e | 307 | if (symbol_conf.use_callchain) |
f8be1c8c NK |
308 | callchain_size = sizeof(struct callchain_root); |
309 | ||
310 | he = zalloc(sizeof(*he) + callchain_size); | |
28e2a106 | 311 | |
12c14278 ACM |
312 | if (he != NULL) { |
313 | *he = *template; | |
c4b35351 | 314 | |
f8be1c8c NK |
315 | if (symbol_conf.cumulate_callchain) { |
316 | he->stat_acc = malloc(sizeof(he->stat)); | |
317 | if (he->stat_acc == NULL) { | |
318 | free(he); | |
319 | return NULL; | |
320 | } | |
321 | memcpy(he->stat_acc, &he->stat, sizeof(he->stat)); | |
a0b51af3 NK |
322 | if (!sample_self) |
323 | memset(&he->stat, 0, sizeof(he->stat)); | |
f8be1c8c NK |
324 | } |
325 | ||
12c14278 ACM |
326 | if (he->ms.map) |
327 | he->ms.map->referenced = true; | |
3cf0cb1f SE |
328 | |
329 | if (he->branch_info) { | |
26353a61 NK |
330 | /* |
331 | * This branch info is (a part of) allocated from | |
644f2df2 | 332 | * sample__resolve_bstack() and will be freed after |
26353a61 NK |
333 | * adding new entries. So we need to save a copy. |
334 | */ | |
335 | he->branch_info = malloc(sizeof(*he->branch_info)); | |
336 | if (he->branch_info == NULL) { | |
f8be1c8c | 337 | free(he->stat_acc); |
26353a61 NK |
338 | free(he); |
339 | return NULL; | |
340 | } | |
341 | ||
342 | memcpy(he->branch_info, template->branch_info, | |
343 | sizeof(*he->branch_info)); | |
344 | ||
3cf0cb1f SE |
345 | if (he->branch_info->from.map) |
346 | he->branch_info->from.map->referenced = true; | |
347 | if (he->branch_info->to.map) | |
348 | he->branch_info->to.map->referenced = true; | |
349 | } | |
350 | ||
98a3b32c SE |
351 | if (he->mem_info) { |
352 | if (he->mem_info->iaddr.map) | |
353 | he->mem_info->iaddr.map->referenced = true; | |
354 | if (he->mem_info->daddr.map) | |
355 | he->mem_info->daddr.map->referenced = true; | |
356 | } | |
357 | ||
28e2a106 | 358 | if (symbol_conf.use_callchain) |
12c14278 | 359 | callchain_init(he->callchain); |
b821c732 ACM |
360 | |
361 | INIT_LIST_HEAD(&he->pairs.node); | |
28e2a106 ACM |
362 | } |
363 | ||
12c14278 | 364 | return he; |
28e2a106 ACM |
365 | } |
366 | ||
7a007ca9 ACM |
367 | static u8 symbol__parent_filter(const struct symbol *parent) |
368 | { | |
369 | if (symbol_conf.exclude_other && parent == NULL) | |
370 | return 1 << HIST_FILTER__PARENT; | |
371 | return 0; | |
372 | } | |
373 | ||
b5387528 | 374 | static struct hist_entry *add_hist_entry(struct hists *hists, |
f1cbf78d | 375 | struct hist_entry *entry, |
a0b51af3 NK |
376 | struct addr_location *al, |
377 | bool sample_self) | |
9735abf1 | 378 | { |
1980c2eb | 379 | struct rb_node **p; |
9735abf1 ACM |
380 | struct rb_node *parent = NULL; |
381 | struct hist_entry *he; | |
354cc40e | 382 | int64_t cmp; |
f1cbf78d NK |
383 | u64 period = entry->stat.period; |
384 | u64 weight = entry->stat.weight; | |
9735abf1 | 385 | |
1980c2eb ACM |
386 | p = &hists->entries_in->rb_node; |
387 | ||
9735abf1 ACM |
388 | while (*p != NULL) { |
389 | parent = *p; | |
1980c2eb | 390 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
9735abf1 | 391 | |
9afcf930 NK |
392 | /* |
393 | * Make sure that it receives arguments in a same order as | |
394 | * hist_entry__collapse() so that we can use an appropriate | |
395 | * function when searching an entry regardless which sort | |
396 | * keys were used. | |
397 | */ | |
398 | cmp = hist_entry__cmp(he, entry); | |
9735abf1 ACM |
399 | |
400 | if (!cmp) { | |
a0b51af3 NK |
401 | if (sample_self) |
402 | he_stat__add_period(&he->stat, period, weight); | |
f8be1c8c NK |
403 | if (symbol_conf.cumulate_callchain) |
404 | he_stat__add_period(he->stat_acc, period, weight); | |
63fa471d | 405 | |
ceb2acbc | 406 | /* |
e80faac0 | 407 | * This mem info was allocated from sample__resolve_mem |
ceb2acbc NK |
408 | * and will not be used anymore. |
409 | */ | |
74cf249d | 410 | zfree(&entry->mem_info); |
ceb2acbc | 411 | |
63fa471d DM |
412 | /* If the map of an existing hist_entry has |
413 | * become out-of-date due to an exec() or | |
414 | * similar, update it. Otherwise we will | |
415 | * mis-adjust symbol addresses when computing | |
416 | * the history counter to increment. | |
417 | */ | |
418 | if (he->ms.map != entry->ms.map) { | |
419 | he->ms.map = entry->ms.map; | |
420 | if (he->ms.map) | |
421 | he->ms.map->referenced = true; | |
422 | } | |
28e2a106 | 423 | goto out; |
9735abf1 ACM |
424 | } |
425 | ||
426 | if (cmp < 0) | |
427 | p = &(*p)->rb_left; | |
428 | else | |
429 | p = &(*p)->rb_right; | |
430 | } | |
431 | ||
a0b51af3 | 432 | he = hist_entry__new(entry, sample_self); |
9735abf1 | 433 | if (!he) |
27a0dcb7 | 434 | return NULL; |
1980c2eb ACM |
435 | |
436 | rb_link_node(&he->rb_node_in, parent, p); | |
437 | rb_insert_color(&he->rb_node_in, hists->entries_in); | |
28e2a106 | 438 | out: |
a0b51af3 NK |
439 | if (sample_self) |
440 | he_stat__add_cpumode_period(&he->stat, al->cpumode, period); | |
f8be1c8c NK |
441 | if (symbol_conf.cumulate_callchain) |
442 | he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period); | |
9735abf1 ACM |
443 | return he; |
444 | } | |
445 | ||
c824c433 | 446 | struct hist_entry *__hists__add_entry(struct hists *hists, |
b5387528 | 447 | struct addr_location *al, |
41a4e6e2 NK |
448 | struct symbol *sym_parent, |
449 | struct branch_info *bi, | |
450 | struct mem_info *mi, | |
a0b51af3 NK |
451 | u64 period, u64 weight, u64 transaction, |
452 | bool sample_self) | |
b5387528 RAV |
453 | { |
454 | struct hist_entry entry = { | |
455 | .thread = al->thread, | |
4dfced35 | 456 | .comm = thread__comm(al->thread), |
b5387528 RAV |
457 | .ms = { |
458 | .map = al->map, | |
459 | .sym = al->sym, | |
460 | }, | |
7365be55 DZ |
461 | .cpu = al->cpu, |
462 | .cpumode = al->cpumode, | |
463 | .ip = al->addr, | |
464 | .level = al->level, | |
b24c28f7 | 465 | .stat = { |
c4b35351 | 466 | .nr_events = 1, |
41a4e6e2 | 467 | .period = period, |
05484298 | 468 | .weight = weight, |
b24c28f7 | 469 | }, |
b5387528 | 470 | .parent = sym_parent, |
2c86c7ca | 471 | .filtered = symbol__parent_filter(sym_parent) | al->filtered, |
c824c433 | 472 | .hists = hists, |
41a4e6e2 NK |
473 | .branch_info = bi, |
474 | .mem_info = mi, | |
475eeab9 | 475 | .transaction = transaction, |
b5387528 RAV |
476 | }; |
477 | ||
a0b51af3 | 478 | return add_hist_entry(hists, &entry, al, sample_self); |
b5387528 RAV |
479 | } |
480 | ||
69bcb019 NK |
481 | static int |
482 | iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, | |
483 | struct addr_location *al __maybe_unused) | |
484 | { | |
485 | return 0; | |
486 | } | |
487 | ||
488 | static int | |
489 | iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, | |
490 | struct addr_location *al __maybe_unused) | |
491 | { | |
492 | return 0; | |
493 | } | |
494 | ||
495 | static int | |
496 | iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
497 | { | |
498 | struct perf_sample *sample = iter->sample; | |
499 | struct mem_info *mi; | |
500 | ||
501 | mi = sample__resolve_mem(sample, al); | |
502 | if (mi == NULL) | |
503 | return -ENOMEM; | |
504 | ||
505 | iter->priv = mi; | |
506 | return 0; | |
507 | } | |
508 | ||
509 | static int | |
510 | iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
511 | { | |
512 | u64 cost; | |
513 | struct mem_info *mi = iter->priv; | |
4ea062ed | 514 | struct hists *hists = evsel__hists(iter->evsel); |
69bcb019 NK |
515 | struct hist_entry *he; |
516 | ||
517 | if (mi == NULL) | |
518 | return -EINVAL; | |
519 | ||
520 | cost = iter->sample->weight; | |
521 | if (!cost) | |
522 | cost = 1; | |
523 | ||
524 | /* | |
525 | * must pass period=weight in order to get the correct | |
526 | * sorting from hists__collapse_resort() which is solely | |
527 | * based on periods. We want sorting be done on nr_events * weight | |
528 | * and this is indirectly achieved by passing period=weight here | |
529 | * and the he_stat__add_period() function. | |
530 | */ | |
4ea062ed | 531 | he = __hists__add_entry(hists, al, iter->parent, NULL, mi, |
a0b51af3 | 532 | cost, cost, 0, true); |
69bcb019 NK |
533 | if (!he) |
534 | return -ENOMEM; | |
535 | ||
536 | iter->he = he; | |
537 | return 0; | |
538 | } | |
539 | ||
540 | static int | |
9d3c02d7 NK |
541 | iter_finish_mem_entry(struct hist_entry_iter *iter, |
542 | struct addr_location *al __maybe_unused) | |
69bcb019 NK |
543 | { |
544 | struct perf_evsel *evsel = iter->evsel; | |
4ea062ed | 545 | struct hists *hists = evsel__hists(evsel); |
69bcb019 | 546 | struct hist_entry *he = iter->he; |
69bcb019 NK |
547 | int err = -EINVAL; |
548 | ||
549 | if (he == NULL) | |
550 | goto out; | |
551 | ||
4ea062ed | 552 | hists__inc_nr_samples(hists, he->filtered); |
69bcb019 NK |
553 | |
554 | err = hist_entry__append_callchain(he, iter->sample); | |
555 | ||
556 | out: | |
557 | /* | |
558 | * We don't need to free iter->priv (mem_info) here since | |
559 | * the mem info was either already freed in add_hist_entry() or | |
560 | * passed to a new hist entry by hist_entry__new(). | |
561 | */ | |
562 | iter->priv = NULL; | |
563 | ||
564 | iter->he = NULL; | |
565 | return err; | |
566 | } | |
567 | ||
568 | static int | |
569 | iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
570 | { | |
571 | struct branch_info *bi; | |
572 | struct perf_sample *sample = iter->sample; | |
573 | ||
574 | bi = sample__resolve_bstack(sample, al); | |
575 | if (!bi) | |
576 | return -ENOMEM; | |
577 | ||
578 | iter->curr = 0; | |
579 | iter->total = sample->branch_stack->nr; | |
580 | ||
581 | iter->priv = bi; | |
582 | return 0; | |
583 | } | |
584 | ||
585 | static int | |
586 | iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused, | |
587 | struct addr_location *al __maybe_unused) | |
588 | { | |
9d3c02d7 NK |
589 | /* to avoid calling callback function */ |
590 | iter->he = NULL; | |
591 | ||
69bcb019 NK |
592 | return 0; |
593 | } | |
594 | ||
595 | static int | |
596 | iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
597 | { | |
598 | struct branch_info *bi = iter->priv; | |
599 | int i = iter->curr; | |
600 | ||
601 | if (bi == NULL) | |
602 | return 0; | |
603 | ||
604 | if (iter->curr >= iter->total) | |
605 | return 0; | |
606 | ||
607 | al->map = bi[i].to.map; | |
608 | al->sym = bi[i].to.sym; | |
609 | al->addr = bi[i].to.addr; | |
610 | return 1; | |
611 | } | |
612 | ||
613 | static int | |
614 | iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
615 | { | |
9d3c02d7 | 616 | struct branch_info *bi; |
69bcb019 | 617 | struct perf_evsel *evsel = iter->evsel; |
4ea062ed | 618 | struct hists *hists = evsel__hists(evsel); |
69bcb019 NK |
619 | struct hist_entry *he = NULL; |
620 | int i = iter->curr; | |
621 | int err = 0; | |
622 | ||
623 | bi = iter->priv; | |
624 | ||
625 | if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym)) | |
626 | goto out; | |
627 | ||
628 | /* | |
629 | * The report shows the percentage of total branches captured | |
630 | * and not events sampled. Thus we use a pseudo period of 1. | |
631 | */ | |
4ea062ed | 632 | he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL, |
a0b51af3 | 633 | 1, 1, 0, true); |
69bcb019 NK |
634 | if (he == NULL) |
635 | return -ENOMEM; | |
636 | ||
4ea062ed | 637 | hists__inc_nr_samples(hists, he->filtered); |
69bcb019 NK |
638 | |
639 | out: | |
640 | iter->he = he; | |
641 | iter->curr++; | |
642 | return err; | |
643 | } | |
644 | ||
645 | static int | |
646 | iter_finish_branch_entry(struct hist_entry_iter *iter, | |
647 | struct addr_location *al __maybe_unused) | |
648 | { | |
649 | zfree(&iter->priv); | |
650 | iter->he = NULL; | |
651 | ||
652 | return iter->curr >= iter->total ? 0 : -1; | |
653 | } | |
654 | ||
655 | static int | |
656 | iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused, | |
657 | struct addr_location *al __maybe_unused) | |
658 | { | |
659 | return 0; | |
660 | } | |
661 | ||
662 | static int | |
663 | iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al) | |
664 | { | |
665 | struct perf_evsel *evsel = iter->evsel; | |
666 | struct perf_sample *sample = iter->sample; | |
667 | struct hist_entry *he; | |
668 | ||
4ea062ed | 669 | he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, |
69bcb019 | 670 | sample->period, sample->weight, |
a0b51af3 | 671 | sample->transaction, true); |
69bcb019 NK |
672 | if (he == NULL) |
673 | return -ENOMEM; | |
674 | ||
675 | iter->he = he; | |
676 | return 0; | |
677 | } | |
678 | ||
679 | static int | |
9d3c02d7 NK |
680 | iter_finish_normal_entry(struct hist_entry_iter *iter, |
681 | struct addr_location *al __maybe_unused) | |
69bcb019 | 682 | { |
69bcb019 NK |
683 | struct hist_entry *he = iter->he; |
684 | struct perf_evsel *evsel = iter->evsel; | |
685 | struct perf_sample *sample = iter->sample; | |
686 | ||
687 | if (he == NULL) | |
688 | return 0; | |
689 | ||
690 | iter->he = NULL; | |
691 | ||
4ea062ed | 692 | hists__inc_nr_samples(evsel__hists(evsel), he->filtered); |
69bcb019 NK |
693 | |
694 | return hist_entry__append_callchain(he, sample); | |
695 | } | |
696 | ||
7a13aa28 NK |
697 | static int |
698 | iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused, | |
699 | struct addr_location *al __maybe_unused) | |
700 | { | |
b4d3c8bd NK |
701 | struct hist_entry **he_cache; |
702 | ||
7a13aa28 | 703 | callchain_cursor_commit(&callchain_cursor); |
b4d3c8bd NK |
704 | |
705 | /* | |
706 | * This is for detecting cycles or recursions so that they're | |
707 | * cumulated only one time to prevent entries more than 100% | |
708 | * overhead. | |
709 | */ | |
710 | he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1)); | |
711 | if (he_cache == NULL) | |
712 | return -ENOMEM; | |
713 | ||
714 | iter->priv = he_cache; | |
715 | iter->curr = 0; | |
716 | ||
7a13aa28 NK |
717 | return 0; |
718 | } | |
719 | ||
720 | static int | |
721 | iter_add_single_cumulative_entry(struct hist_entry_iter *iter, | |
722 | struct addr_location *al) | |
723 | { | |
724 | struct perf_evsel *evsel = iter->evsel; | |
4ea062ed | 725 | struct hists *hists = evsel__hists(evsel); |
7a13aa28 | 726 | struct perf_sample *sample = iter->sample; |
b4d3c8bd | 727 | struct hist_entry **he_cache = iter->priv; |
7a13aa28 NK |
728 | struct hist_entry *he; |
729 | int err = 0; | |
730 | ||
4ea062ed | 731 | he = __hists__add_entry(hists, al, iter->parent, NULL, NULL, |
7a13aa28 NK |
732 | sample->period, sample->weight, |
733 | sample->transaction, true); | |
734 | if (he == NULL) | |
735 | return -ENOMEM; | |
736 | ||
737 | iter->he = he; | |
b4d3c8bd | 738 | he_cache[iter->curr++] = he; |
7a13aa28 | 739 | |
82aa019e | 740 | hist_entry__append_callchain(he, sample); |
be7f855a NK |
741 | |
742 | /* | |
743 | * We need to re-initialize the cursor since callchain_append() | |
744 | * advanced the cursor to the end. | |
745 | */ | |
746 | callchain_cursor_commit(&callchain_cursor); | |
747 | ||
4ea062ed | 748 | hists__inc_nr_samples(hists, he->filtered); |
7a13aa28 NK |
749 | |
750 | return err; | |
751 | } | |
752 | ||
753 | static int | |
754 | iter_next_cumulative_entry(struct hist_entry_iter *iter, | |
755 | struct addr_location *al) | |
756 | { | |
757 | struct callchain_cursor_node *node; | |
758 | ||
759 | node = callchain_cursor_current(&callchain_cursor); | |
760 | if (node == NULL) | |
761 | return 0; | |
762 | ||
c7405d85 | 763 | return fill_callchain_info(al, node, iter->hide_unresolved); |
7a13aa28 NK |
764 | } |
765 | ||
766 | static int | |
767 | iter_add_next_cumulative_entry(struct hist_entry_iter *iter, | |
768 | struct addr_location *al) | |
769 | { | |
770 | struct perf_evsel *evsel = iter->evsel; | |
771 | struct perf_sample *sample = iter->sample; | |
b4d3c8bd | 772 | struct hist_entry **he_cache = iter->priv; |
7a13aa28 | 773 | struct hist_entry *he; |
b4d3c8bd NK |
774 | struct hist_entry he_tmp = { |
775 | .cpu = al->cpu, | |
776 | .thread = al->thread, | |
777 | .comm = thread__comm(al->thread), | |
778 | .ip = al->addr, | |
779 | .ms = { | |
780 | .map = al->map, | |
781 | .sym = al->sym, | |
782 | }, | |
783 | .parent = iter->parent, | |
784 | }; | |
785 | int i; | |
be7f855a NK |
786 | struct callchain_cursor cursor; |
787 | ||
788 | callchain_cursor_snapshot(&cursor, &callchain_cursor); | |
789 | ||
790 | callchain_cursor_advance(&callchain_cursor); | |
b4d3c8bd NK |
791 | |
792 | /* | |
793 | * Check if there's duplicate entries in the callchain. | |
794 | * It's possible that it has cycles or recursive calls. | |
795 | */ | |
796 | for (i = 0; i < iter->curr; i++) { | |
9d3c02d7 NK |
797 | if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) { |
798 | /* to avoid calling callback function */ | |
799 | iter->he = NULL; | |
b4d3c8bd | 800 | return 0; |
9d3c02d7 | 801 | } |
b4d3c8bd | 802 | } |
7a13aa28 | 803 | |
4ea062ed | 804 | he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, |
7a13aa28 NK |
805 | sample->period, sample->weight, |
806 | sample->transaction, false); | |
807 | if (he == NULL) | |
808 | return -ENOMEM; | |
809 | ||
810 | iter->he = he; | |
b4d3c8bd | 811 | he_cache[iter->curr++] = he; |
7a13aa28 | 812 | |
82aa019e NK |
813 | if (symbol_conf.use_callchain) |
814 | callchain_append(he->callchain, &cursor, sample->period); | |
7a13aa28 NK |
815 | return 0; |
816 | } | |
817 | ||
818 | static int | |
819 | iter_finish_cumulative_entry(struct hist_entry_iter *iter, | |
820 | struct addr_location *al __maybe_unused) | |
821 | { | |
b4d3c8bd | 822 | zfree(&iter->priv); |
7a13aa28 | 823 | iter->he = NULL; |
b4d3c8bd | 824 | |
7a13aa28 NK |
825 | return 0; |
826 | } | |
827 | ||
69bcb019 NK |
828 | const struct hist_iter_ops hist_iter_mem = { |
829 | .prepare_entry = iter_prepare_mem_entry, | |
830 | .add_single_entry = iter_add_single_mem_entry, | |
831 | .next_entry = iter_next_nop_entry, | |
832 | .add_next_entry = iter_add_next_nop_entry, | |
833 | .finish_entry = iter_finish_mem_entry, | |
834 | }; | |
835 | ||
836 | const struct hist_iter_ops hist_iter_branch = { | |
837 | .prepare_entry = iter_prepare_branch_entry, | |
838 | .add_single_entry = iter_add_single_branch_entry, | |
839 | .next_entry = iter_next_branch_entry, | |
840 | .add_next_entry = iter_add_next_branch_entry, | |
841 | .finish_entry = iter_finish_branch_entry, | |
842 | }; | |
843 | ||
844 | const struct hist_iter_ops hist_iter_normal = { | |
845 | .prepare_entry = iter_prepare_normal_entry, | |
846 | .add_single_entry = iter_add_single_normal_entry, | |
847 | .next_entry = iter_next_nop_entry, | |
848 | .add_next_entry = iter_add_next_nop_entry, | |
849 | .finish_entry = iter_finish_normal_entry, | |
850 | }; | |
851 | ||
7a13aa28 NK |
852 | const struct hist_iter_ops hist_iter_cumulative = { |
853 | .prepare_entry = iter_prepare_cumulative_entry, | |
854 | .add_single_entry = iter_add_single_cumulative_entry, | |
855 | .next_entry = iter_next_cumulative_entry, | |
856 | .add_next_entry = iter_add_next_cumulative_entry, | |
857 | .finish_entry = iter_finish_cumulative_entry, | |
858 | }; | |
859 | ||
69bcb019 NK |
860 | int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al, |
861 | struct perf_evsel *evsel, struct perf_sample *sample, | |
9d3c02d7 | 862 | int max_stack_depth, void *arg) |
69bcb019 NK |
863 | { |
864 | int err, err2; | |
865 | ||
866 | err = sample__resolve_callchain(sample, &iter->parent, evsel, al, | |
867 | max_stack_depth); | |
868 | if (err) | |
869 | return err; | |
870 | ||
871 | iter->evsel = evsel; | |
872 | iter->sample = sample; | |
873 | ||
874 | err = iter->ops->prepare_entry(iter, al); | |
875 | if (err) | |
876 | goto out; | |
877 | ||
878 | err = iter->ops->add_single_entry(iter, al); | |
879 | if (err) | |
880 | goto out; | |
881 | ||
9d3c02d7 NK |
882 | if (iter->he && iter->add_entry_cb) { |
883 | err = iter->add_entry_cb(iter, al, true, arg); | |
884 | if (err) | |
885 | goto out; | |
886 | } | |
887 | ||
69bcb019 NK |
888 | while (iter->ops->next_entry(iter, al)) { |
889 | err = iter->ops->add_next_entry(iter, al); | |
890 | if (err) | |
891 | break; | |
9d3c02d7 NK |
892 | |
893 | if (iter->he && iter->add_entry_cb) { | |
894 | err = iter->add_entry_cb(iter, al, false, arg); | |
895 | if (err) | |
896 | goto out; | |
897 | } | |
69bcb019 NK |
898 | } |
899 | ||
900 | out: | |
901 | err2 = iter->ops->finish_entry(iter, al); | |
902 | if (!err) | |
903 | err = err2; | |
904 | ||
905 | return err; | |
906 | } | |
907 | ||
3d1d07ec JK |
908 | int64_t |
909 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
910 | { | |
093f0ef3 | 911 | struct perf_hpp_fmt *fmt; |
3d1d07ec JK |
912 | int64_t cmp = 0; |
913 | ||
093f0ef3 | 914 | perf_hpp__for_each_sort_list(fmt) { |
e67d49a7 NK |
915 | if (perf_hpp__should_skip(fmt)) |
916 | continue; | |
917 | ||
093f0ef3 | 918 | cmp = fmt->cmp(left, right); |
3d1d07ec JK |
919 | if (cmp) |
920 | break; | |
921 | } | |
922 | ||
923 | return cmp; | |
924 | } | |
925 | ||
926 | int64_t | |
927 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | |
928 | { | |
093f0ef3 | 929 | struct perf_hpp_fmt *fmt; |
3d1d07ec JK |
930 | int64_t cmp = 0; |
931 | ||
093f0ef3 | 932 | perf_hpp__for_each_sort_list(fmt) { |
e67d49a7 NK |
933 | if (perf_hpp__should_skip(fmt)) |
934 | continue; | |
935 | ||
093f0ef3 | 936 | cmp = fmt->collapse(left, right); |
3d1d07ec JK |
937 | if (cmp) |
938 | break; | |
939 | } | |
940 | ||
941 | return cmp; | |
942 | } | |
943 | ||
944 | void hist_entry__free(struct hist_entry *he) | |
945 | { | |
74cf249d ACM |
946 | zfree(&he->branch_info); |
947 | zfree(&he->mem_info); | |
f8be1c8c | 948 | zfree(&he->stat_acc); |
f048d548 | 949 | free_srcline(he->srcline); |
3d1d07ec JK |
950 | free(he); |
951 | } | |
952 | ||
953 | /* | |
954 | * collapse the histogram | |
955 | */ | |
956 | ||
1d037ca1 | 957 | static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused, |
1b3a0e95 FW |
958 | struct rb_root *root, |
959 | struct hist_entry *he) | |
3d1d07ec | 960 | { |
b9bf0892 | 961 | struct rb_node **p = &root->rb_node; |
3d1d07ec JK |
962 | struct rb_node *parent = NULL; |
963 | struct hist_entry *iter; | |
964 | int64_t cmp; | |
965 | ||
966 | while (*p != NULL) { | |
967 | parent = *p; | |
1980c2eb | 968 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
3d1d07ec JK |
969 | |
970 | cmp = hist_entry__collapse(iter, he); | |
971 | ||
972 | if (!cmp) { | |
139c0815 | 973 | he_stat__add_stat(&iter->stat, &he->stat); |
f8be1c8c NK |
974 | if (symbol_conf.cumulate_callchain) |
975 | he_stat__add_stat(iter->stat_acc, he->stat_acc); | |
9ec60972 | 976 | |
1b3a0e95 | 977 | if (symbol_conf.use_callchain) { |
47260645 NK |
978 | callchain_cursor_reset(&callchain_cursor); |
979 | callchain_merge(&callchain_cursor, | |
980 | iter->callchain, | |
1b3a0e95 FW |
981 | he->callchain); |
982 | } | |
3d1d07ec | 983 | hist_entry__free(he); |
fefb0b94 | 984 | return false; |
3d1d07ec JK |
985 | } |
986 | ||
987 | if (cmp < 0) | |
988 | p = &(*p)->rb_left; | |
989 | else | |
990 | p = &(*p)->rb_right; | |
991 | } | |
740b97f9 | 992 | hists->nr_entries++; |
3d1d07ec | 993 | |
1980c2eb ACM |
994 | rb_link_node(&he->rb_node_in, parent, p); |
995 | rb_insert_color(&he->rb_node_in, root); | |
fefb0b94 | 996 | return true; |
3d1d07ec JK |
997 | } |
998 | ||
1980c2eb | 999 | static struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
3d1d07ec | 1000 | { |
1980c2eb ACM |
1001 | struct rb_root *root; |
1002 | ||
1003 | pthread_mutex_lock(&hists->lock); | |
1004 | ||
1005 | root = hists->entries_in; | |
1006 | if (++hists->entries_in > &hists->entries_in_array[1]) | |
1007 | hists->entries_in = &hists->entries_in_array[0]; | |
1008 | ||
1009 | pthread_mutex_unlock(&hists->lock); | |
1010 | ||
1011 | return root; | |
1012 | } | |
1013 | ||
90cf1fb5 ACM |
1014 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) |
1015 | { | |
1016 | hists__filter_entry_by_dso(hists, he); | |
1017 | hists__filter_entry_by_thread(hists, he); | |
e94d53eb | 1018 | hists__filter_entry_by_symbol(hists, he); |
90cf1fb5 ACM |
1019 | } |
1020 | ||
c1fb5651 | 1021 | void hists__collapse_resort(struct hists *hists, struct ui_progress *prog) |
1980c2eb ACM |
1022 | { |
1023 | struct rb_root *root; | |
3d1d07ec JK |
1024 | struct rb_node *next; |
1025 | struct hist_entry *n; | |
1026 | ||
3a5714f8 | 1027 | if (!sort__need_collapse) |
3d1d07ec JK |
1028 | return; |
1029 | ||
740b97f9 NK |
1030 | hists->nr_entries = 0; |
1031 | ||
1980c2eb | 1032 | root = hists__get_rotate_entries_in(hists); |
740b97f9 | 1033 | |
1980c2eb | 1034 | next = rb_first(root); |
b9bf0892 | 1035 | |
3d1d07ec | 1036 | while (next) { |
33e940a2 ACM |
1037 | if (session_done()) |
1038 | break; | |
1980c2eb ACM |
1039 | n = rb_entry(next, struct hist_entry, rb_node_in); |
1040 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 1041 | |
1980c2eb | 1042 | rb_erase(&n->rb_node_in, root); |
90cf1fb5 ACM |
1043 | if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) { |
1044 | /* | |
1045 | * If it wasn't combined with one of the entries already | |
1046 | * collapsed, we need to apply the filters that may have | |
1047 | * been set by, say, the hist_browser. | |
1048 | */ | |
1049 | hists__apply_filters(hists, n); | |
90cf1fb5 | 1050 | } |
c1fb5651 NK |
1051 | if (prog) |
1052 | ui_progress__update(prog, 1); | |
3d1d07ec | 1053 | } |
1980c2eb | 1054 | } |
b9bf0892 | 1055 | |
043ca389 | 1056 | static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b) |
29d720ed | 1057 | { |
043ca389 NK |
1058 | struct perf_hpp_fmt *fmt; |
1059 | int64_t cmp = 0; | |
29d720ed | 1060 | |
26d8b338 | 1061 | perf_hpp__for_each_sort_list(fmt) { |
e67d49a7 NK |
1062 | if (perf_hpp__should_skip(fmt)) |
1063 | continue; | |
1064 | ||
043ca389 NK |
1065 | cmp = fmt->sort(a, b); |
1066 | if (cmp) | |
29d720ed NK |
1067 | break; |
1068 | } | |
1069 | ||
043ca389 | 1070 | return cmp; |
29d720ed NK |
1071 | } |
1072 | ||
9283ba9b NK |
1073 | static void hists__reset_filter_stats(struct hists *hists) |
1074 | { | |
1075 | hists->nr_non_filtered_entries = 0; | |
1076 | hists->stats.total_non_filtered_period = 0; | |
1077 | } | |
1078 | ||
1079 | void hists__reset_stats(struct hists *hists) | |
1080 | { | |
1081 | hists->nr_entries = 0; | |
1082 | hists->stats.total_period = 0; | |
1083 | ||
1084 | hists__reset_filter_stats(hists); | |
1085 | } | |
1086 | ||
1087 | static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h) | |
1088 | { | |
1089 | hists->nr_non_filtered_entries++; | |
1090 | hists->stats.total_non_filtered_period += h->stat.period; | |
1091 | } | |
1092 | ||
1093 | void hists__inc_stats(struct hists *hists, struct hist_entry *h) | |
1094 | { | |
1095 | if (!h->filtered) | |
1096 | hists__inc_filter_stats(hists, h); | |
1097 | ||
1098 | hists->nr_entries++; | |
1099 | hists->stats.total_period += h->stat.period; | |
1100 | } | |
1101 | ||
1c02c4d2 ACM |
1102 | static void __hists__insert_output_entry(struct rb_root *entries, |
1103 | struct hist_entry *he, | |
1104 | u64 min_callchain_hits) | |
3d1d07ec | 1105 | { |
1c02c4d2 | 1106 | struct rb_node **p = &entries->rb_node; |
3d1d07ec JK |
1107 | struct rb_node *parent = NULL; |
1108 | struct hist_entry *iter; | |
1109 | ||
d599db3f | 1110 | if (symbol_conf.use_callchain) |
b9fb9304 | 1111 | callchain_param.sort(&he->sorted_chain, he->callchain, |
3d1d07ec JK |
1112 | min_callchain_hits, &callchain_param); |
1113 | ||
1114 | while (*p != NULL) { | |
1115 | parent = *p; | |
1116 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
1117 | ||
043ca389 | 1118 | if (hist_entry__sort(he, iter) > 0) |
3d1d07ec JK |
1119 | p = &(*p)->rb_left; |
1120 | else | |
1121 | p = &(*p)->rb_right; | |
1122 | } | |
1123 | ||
1124 | rb_link_node(&he->rb_node, parent, p); | |
1c02c4d2 | 1125 | rb_insert_color(&he->rb_node, entries); |
3d1d07ec JK |
1126 | } |
1127 | ||
740b97f9 | 1128 | void hists__output_resort(struct hists *hists, struct ui_progress *prog) |
3d1d07ec | 1129 | { |
1980c2eb | 1130 | struct rb_root *root; |
3d1d07ec JK |
1131 | struct rb_node *next; |
1132 | struct hist_entry *n; | |
3d1d07ec JK |
1133 | u64 min_callchain_hits; |
1134 | ||
42b28ac0 | 1135 | min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100); |
3d1d07ec | 1136 | |
3a5714f8 | 1137 | if (sort__need_collapse) |
1980c2eb ACM |
1138 | root = &hists->entries_collapsed; |
1139 | else | |
1140 | root = hists->entries_in; | |
1141 | ||
1142 | next = rb_first(root); | |
1143 | hists->entries = RB_ROOT; | |
3d1d07ec | 1144 | |
9283ba9b | 1145 | hists__reset_stats(hists); |
42b28ac0 | 1146 | hists__reset_col_len(hists); |
fefb0b94 | 1147 | |
3d1d07ec | 1148 | while (next) { |
1980c2eb ACM |
1149 | n = rb_entry(next, struct hist_entry, rb_node_in); |
1150 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 1151 | |
1980c2eb | 1152 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits); |
6263835a | 1153 | hists__inc_stats(hists, n); |
ae993efc NK |
1154 | |
1155 | if (!n->filtered) | |
1156 | hists__calc_col_len(hists, n); | |
740b97f9 NK |
1157 | |
1158 | if (prog) | |
1159 | ui_progress__update(prog, 1); | |
3d1d07ec | 1160 | } |
1980c2eb | 1161 | } |
b9bf0892 | 1162 | |
42b28ac0 | 1163 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, |
cc5edb0e ACM |
1164 | enum hist_filter filter) |
1165 | { | |
1166 | h->filtered &= ~(1 << filter); | |
1167 | if (h->filtered) | |
1168 | return; | |
1169 | ||
87e90f43 NK |
1170 | /* force fold unfiltered entry for simplicity */ |
1171 | h->ms.unfolded = false; | |
0f0cbf7a | 1172 | h->row_offset = 0; |
9283ba9b | 1173 | |
1ab1fa5d | 1174 | hists->stats.nr_non_filtered_samples += h->stat.nr_events; |
cc5edb0e | 1175 | |
9283ba9b | 1176 | hists__inc_filter_stats(hists, h); |
42b28ac0 | 1177 | hists__calc_col_len(hists, h); |
cc5edb0e ACM |
1178 | } |
1179 | ||
90cf1fb5 ACM |
1180 | |
1181 | static bool hists__filter_entry_by_dso(struct hists *hists, | |
1182 | struct hist_entry *he) | |
1183 | { | |
1184 | if (hists->dso_filter != NULL && | |
1185 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { | |
1186 | he->filtered |= (1 << HIST_FILTER__DSO); | |
1187 | return true; | |
1188 | } | |
1189 | ||
1190 | return false; | |
1191 | } | |
1192 | ||
d7b76f09 | 1193 | void hists__filter_by_dso(struct hists *hists) |
b09e0190 ACM |
1194 | { |
1195 | struct rb_node *nd; | |
1196 | ||
1ab1fa5d | 1197 | hists->stats.nr_non_filtered_samples = 0; |
9283ba9b NK |
1198 | |
1199 | hists__reset_filter_stats(hists); | |
42b28ac0 | 1200 | hists__reset_col_len(hists); |
b09e0190 | 1201 | |
42b28ac0 | 1202 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
b09e0190 ACM |
1203 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
1204 | ||
1205 | if (symbol_conf.exclude_other && !h->parent) | |
1206 | continue; | |
1207 | ||
90cf1fb5 | 1208 | if (hists__filter_entry_by_dso(hists, h)) |
b09e0190 | 1209 | continue; |
b09e0190 | 1210 | |
42b28ac0 | 1211 | hists__remove_entry_filter(hists, h, HIST_FILTER__DSO); |
b09e0190 ACM |
1212 | } |
1213 | } | |
1214 | ||
90cf1fb5 ACM |
1215 | static bool hists__filter_entry_by_thread(struct hists *hists, |
1216 | struct hist_entry *he) | |
1217 | { | |
1218 | if (hists->thread_filter != NULL && | |
1219 | he->thread != hists->thread_filter) { | |
1220 | he->filtered |= (1 << HIST_FILTER__THREAD); | |
1221 | return true; | |
1222 | } | |
1223 | ||
1224 | return false; | |
1225 | } | |
1226 | ||
d7b76f09 | 1227 | void hists__filter_by_thread(struct hists *hists) |
b09e0190 ACM |
1228 | { |
1229 | struct rb_node *nd; | |
1230 | ||
1ab1fa5d | 1231 | hists->stats.nr_non_filtered_samples = 0; |
9283ba9b NK |
1232 | |
1233 | hists__reset_filter_stats(hists); | |
42b28ac0 | 1234 | hists__reset_col_len(hists); |
b09e0190 | 1235 | |
42b28ac0 | 1236 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
b09e0190 ACM |
1237 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
1238 | ||
90cf1fb5 | 1239 | if (hists__filter_entry_by_thread(hists, h)) |
b09e0190 | 1240 | continue; |
cc5edb0e | 1241 | |
42b28ac0 | 1242 | hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD); |
b09e0190 ACM |
1243 | } |
1244 | } | |
ef7b93a1 | 1245 | |
e94d53eb NK |
1246 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
1247 | struct hist_entry *he) | |
1248 | { | |
1249 | if (hists->symbol_filter_str != NULL && | |
1250 | (!he->ms.sym || strstr(he->ms.sym->name, | |
1251 | hists->symbol_filter_str) == NULL)) { | |
1252 | he->filtered |= (1 << HIST_FILTER__SYMBOL); | |
1253 | return true; | |
1254 | } | |
1255 | ||
1256 | return false; | |
1257 | } | |
1258 | ||
1259 | void hists__filter_by_symbol(struct hists *hists) | |
1260 | { | |
1261 | struct rb_node *nd; | |
1262 | ||
1ab1fa5d | 1263 | hists->stats.nr_non_filtered_samples = 0; |
9283ba9b NK |
1264 | |
1265 | hists__reset_filter_stats(hists); | |
e94d53eb NK |
1266 | hists__reset_col_len(hists); |
1267 | ||
1268 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { | |
1269 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
1270 | ||
1271 | if (hists__filter_entry_by_symbol(hists, h)) | |
1272 | continue; | |
1273 | ||
1274 | hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL); | |
1275 | } | |
1276 | } | |
1277 | ||
28a6b6aa ACM |
1278 | void events_stats__inc(struct events_stats *stats, u32 type) |
1279 | { | |
1280 | ++stats->nr_events[0]; | |
1281 | ++stats->nr_events[type]; | |
1282 | } | |
1283 | ||
42b28ac0 | 1284 | void hists__inc_nr_events(struct hists *hists, u32 type) |
c8446b9b | 1285 | { |
28a6b6aa | 1286 | events_stats__inc(&hists->stats, type); |
c8446b9b | 1287 | } |
95529be4 | 1288 | |
1844dbcb NK |
1289 | void hists__inc_nr_samples(struct hists *hists, bool filtered) |
1290 | { | |
1291 | events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE); | |
1292 | if (!filtered) | |
1293 | hists->stats.nr_non_filtered_samples++; | |
1294 | } | |
1295 | ||
494d70a1 ACM |
1296 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, |
1297 | struct hist_entry *pair) | |
1298 | { | |
ce74f60e NK |
1299 | struct rb_root *root; |
1300 | struct rb_node **p; | |
494d70a1 ACM |
1301 | struct rb_node *parent = NULL; |
1302 | struct hist_entry *he; | |
354cc40e | 1303 | int64_t cmp; |
494d70a1 | 1304 | |
ce74f60e NK |
1305 | if (sort__need_collapse) |
1306 | root = &hists->entries_collapsed; | |
1307 | else | |
1308 | root = hists->entries_in; | |
1309 | ||
1310 | p = &root->rb_node; | |
1311 | ||
494d70a1 ACM |
1312 | while (*p != NULL) { |
1313 | parent = *p; | |
ce74f60e | 1314 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
494d70a1 | 1315 | |
ce74f60e | 1316 | cmp = hist_entry__collapse(he, pair); |
494d70a1 ACM |
1317 | |
1318 | if (!cmp) | |
1319 | goto out; | |
1320 | ||
1321 | if (cmp < 0) | |
1322 | p = &(*p)->rb_left; | |
1323 | else | |
1324 | p = &(*p)->rb_right; | |
1325 | } | |
1326 | ||
a0b51af3 | 1327 | he = hist_entry__new(pair, true); |
494d70a1 | 1328 | if (he) { |
30193d78 ACM |
1329 | memset(&he->stat, 0, sizeof(he->stat)); |
1330 | he->hists = hists; | |
ce74f60e NK |
1331 | rb_link_node(&he->rb_node_in, parent, p); |
1332 | rb_insert_color(&he->rb_node_in, root); | |
6263835a | 1333 | hists__inc_stats(hists, he); |
e0af43d2 | 1334 | he->dummy = true; |
494d70a1 ACM |
1335 | } |
1336 | out: | |
1337 | return he; | |
1338 | } | |
1339 | ||
95529be4 ACM |
1340 | static struct hist_entry *hists__find_entry(struct hists *hists, |
1341 | struct hist_entry *he) | |
1342 | { | |
ce74f60e NK |
1343 | struct rb_node *n; |
1344 | ||
1345 | if (sort__need_collapse) | |
1346 | n = hists->entries_collapsed.rb_node; | |
1347 | else | |
1348 | n = hists->entries_in->rb_node; | |
95529be4 ACM |
1349 | |
1350 | while (n) { | |
ce74f60e NK |
1351 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); |
1352 | int64_t cmp = hist_entry__collapse(iter, he); | |
95529be4 ACM |
1353 | |
1354 | if (cmp < 0) | |
1355 | n = n->rb_left; | |
1356 | else if (cmp > 0) | |
1357 | n = n->rb_right; | |
1358 | else | |
1359 | return iter; | |
1360 | } | |
1361 | ||
1362 | return NULL; | |
1363 | } | |
1364 | ||
1365 | /* | |
1366 | * Look for pairs to link to the leader buckets (hist_entries): | |
1367 | */ | |
1368 | void hists__match(struct hists *leader, struct hists *other) | |
1369 | { | |
ce74f60e | 1370 | struct rb_root *root; |
95529be4 ACM |
1371 | struct rb_node *nd; |
1372 | struct hist_entry *pos, *pair; | |
1373 | ||
ce74f60e NK |
1374 | if (sort__need_collapse) |
1375 | root = &leader->entries_collapsed; | |
1376 | else | |
1377 | root = leader->entries_in; | |
1378 | ||
1379 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
1380 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
95529be4 ACM |
1381 | pair = hists__find_entry(other, pos); |
1382 | ||
1383 | if (pair) | |
5fa9041b | 1384 | hist_entry__add_pair(pair, pos); |
95529be4 ACM |
1385 | } |
1386 | } | |
494d70a1 ACM |
1387 | |
1388 | /* | |
1389 | * Look for entries in the other hists that are not present in the leader, if | |
1390 | * we find them, just add a dummy entry on the leader hists, with period=0, | |
1391 | * nr_events=0, to serve as the list header. | |
1392 | */ | |
1393 | int hists__link(struct hists *leader, struct hists *other) | |
1394 | { | |
ce74f60e | 1395 | struct rb_root *root; |
494d70a1 ACM |
1396 | struct rb_node *nd; |
1397 | struct hist_entry *pos, *pair; | |
1398 | ||
ce74f60e NK |
1399 | if (sort__need_collapse) |
1400 | root = &other->entries_collapsed; | |
1401 | else | |
1402 | root = other->entries_in; | |
1403 | ||
1404 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
1405 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
494d70a1 ACM |
1406 | |
1407 | if (!hist_entry__has_pairs(pos)) { | |
1408 | pair = hists__add_dummy_entry(leader, pos); | |
1409 | if (pair == NULL) | |
1410 | return -1; | |
5fa9041b | 1411 | hist_entry__add_pair(pos, pair); |
494d70a1 ACM |
1412 | } |
1413 | } | |
1414 | ||
1415 | return 0; | |
1416 | } | |
f2148330 | 1417 | |
2a1731fb ACM |
1418 | |
1419 | size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp) | |
1420 | { | |
1421 | struct perf_evsel *pos; | |
1422 | size_t ret = 0; | |
1423 | ||
1424 | evlist__for_each(evlist, pos) { | |
1425 | ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos)); | |
1426 | ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp); | |
1427 | } | |
1428 | ||
1429 | return ret; | |
1430 | } | |
1431 | ||
1432 | ||
f2148330 NK |
1433 | u64 hists__total_period(struct hists *hists) |
1434 | { | |
1435 | return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period : | |
1436 | hists->stats.total_period; | |
1437 | } | |
33db4568 NK |
1438 | |
1439 | int parse_filter_percentage(const struct option *opt __maybe_unused, | |
1440 | const char *arg, int unset __maybe_unused) | |
1441 | { | |
1442 | if (!strcmp(arg, "relative")) | |
1443 | symbol_conf.filter_relative = true; | |
1444 | else if (!strcmp(arg, "absolute")) | |
1445 | symbol_conf.filter_relative = false; | |
1446 | else | |
1447 | return -1; | |
1448 | ||
1449 | return 0; | |
1450 | } | |
0b93da17 NK |
1451 | |
1452 | int perf_hist_config(const char *var, const char *value) | |
1453 | { | |
1454 | if (!strcmp(var, "hist.percentage")) | |
1455 | return parse_filter_percentage(NULL, value, 0); | |
1456 | ||
1457 | return 0; | |
1458 | } | |
a635fc51 ACM |
1459 | |
1460 | static int hists_evsel__init(struct perf_evsel *evsel) | |
1461 | { | |
1462 | struct hists *hists = evsel__hists(evsel); | |
1463 | ||
1464 | memset(hists, 0, sizeof(*hists)); | |
1465 | hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT; | |
1466 | hists->entries_in = &hists->entries_in_array[0]; | |
1467 | hists->entries_collapsed = RB_ROOT; | |
1468 | hists->entries = RB_ROOT; | |
1469 | pthread_mutex_init(&hists->lock, NULL); | |
1470 | return 0; | |
1471 | } | |
1472 | ||
1473 | /* | |
1474 | * XXX We probably need a hists_evsel__exit() to free the hist_entries | |
1475 | * stored in the rbtree... | |
1476 | */ | |
1477 | ||
1478 | int hists__init(void) | |
1479 | { | |
1480 | int err = perf_evsel__object_config(sizeof(struct hists_evsel), | |
1481 | hists_evsel__init, NULL); | |
1482 | if (err) | |
1483 | fputs("FATAL ERROR: Couldn't setup hists class\n", stderr); | |
1484 | ||
1485 | return err; | |
1486 | } |