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