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