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