]>
Commit | Line | Data |
---|---|---|
78f7defe | 1 | #include "annotate.h" |
8a0ecfb8 | 2 | #include "util.h" |
598357eb | 3 | #include "build-id.h" |
3d1d07ec | 4 | #include "hist.h" |
4e4f06e4 ACM |
5 | #include "session.h" |
6 | #include "sort.h" | |
29d720ed | 7 | #include "evsel.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 | |
7a007ca9 ACM |
17 | enum hist_filter { |
18 | HIST_FILTER__DSO, | |
19 | HIST_FILTER__THREAD, | |
20 | HIST_FILTER__PARENT, | |
e94d53eb | 21 | HIST_FILTER__SYMBOL, |
7a007ca9 ACM |
22 | }; |
23 | ||
3d1d07ec JK |
24 | struct callchain_param callchain_param = { |
25 | .mode = CHAIN_GRAPH_REL, | |
d797fdc5 SL |
26 | .min_percent = 0.5, |
27 | .order = ORDER_CALLEE | |
3d1d07ec JK |
28 | }; |
29 | ||
42b28ac0 | 30 | u16 hists__col_len(struct hists *hists, enum hist_column col) |
8a6c5b26 | 31 | { |
42b28ac0 | 32 | return hists->col_len[col]; |
8a6c5b26 ACM |
33 | } |
34 | ||
42b28ac0 | 35 | void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 36 | { |
42b28ac0 | 37 | hists->col_len[col] = len; |
8a6c5b26 ACM |
38 | } |
39 | ||
42b28ac0 | 40 | bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 41 | { |
42b28ac0 ACM |
42 | if (len > hists__col_len(hists, col)) { |
43 | hists__set_col_len(hists, col, len); | |
8a6c5b26 ACM |
44 | return true; |
45 | } | |
46 | return false; | |
47 | } | |
48 | ||
7ccf4f90 | 49 | void hists__reset_col_len(struct hists *hists) |
8a6c5b26 ACM |
50 | { |
51 | enum hist_column col; | |
52 | ||
53 | for (col = 0; col < HISTC_NR_COLS; ++col) | |
42b28ac0 | 54 | hists__set_col_len(hists, col, 0); |
8a6c5b26 ACM |
55 | } |
56 | ||
b5387528 RAV |
57 | static void hists__set_unres_dso_col_len(struct hists *hists, int dso) |
58 | { | |
59 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; | |
60 | ||
61 | if (hists__col_len(hists, dso) < unresolved_col_width && | |
62 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && | |
63 | !symbol_conf.dso_list) | |
64 | hists__set_col_len(hists, dso, unresolved_col_width); | |
65 | } | |
66 | ||
7ccf4f90 | 67 | void hists__calc_col_len(struct hists *hists, struct hist_entry *h) |
8a6c5b26 | 68 | { |
b5387528 | 69 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
98a3b32c | 70 | int symlen; |
8a6c5b26 ACM |
71 | u16 len; |
72 | ||
73 | if (h->ms.sym) | |
b5387528 | 74 | hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4); |
98a3b32c SE |
75 | else { |
76 | symlen = unresolved_col_width + 4 + 2; | |
77 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); | |
b5387528 | 78 | hists__set_unres_dso_col_len(hists, HISTC_DSO); |
98a3b32c | 79 | } |
8a6c5b26 ACM |
80 | |
81 | len = thread__comm_len(h->thread); | |
42b28ac0 ACM |
82 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
83 | hists__set_col_len(hists, HISTC_THREAD, len + 6); | |
8a6c5b26 ACM |
84 | |
85 | if (h->ms.map) { | |
86 | len = dso__name_len(h->ms.map->dso); | |
42b28ac0 | 87 | hists__new_col_len(hists, HISTC_DSO, len); |
8a6c5b26 | 88 | } |
b5387528 | 89 | |
cb993744 NK |
90 | if (h->parent) |
91 | hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen); | |
92 | ||
b5387528 | 93 | if (h->branch_info) { |
b5387528 RAV |
94 | /* |
95 | * +4 accounts for '[x] ' priv level info | |
96 | * +2 account of 0x prefix on raw addresses | |
97 | */ | |
98 | if (h->branch_info->from.sym) { | |
99 | symlen = (int)h->branch_info->from.sym->namelen + 4; | |
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; | |
112 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); | |
113 | ||
114 | symlen = dso__name_len(h->branch_info->to.map->dso); | |
115 | hists__new_col_len(hists, HISTC_DSO_TO, symlen); | |
116 | } else { | |
117 | symlen = unresolved_col_width + 4 + 2; | |
118 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); | |
119 | hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); | |
120 | } | |
121 | } | |
98a3b32c SE |
122 | |
123 | if (h->mem_info) { | |
124 | /* | |
125 | * +4 accounts for '[x] ' priv level info | |
126 | * +2 account of 0x prefix on raw addresses | |
127 | */ | |
128 | if (h->mem_info->daddr.sym) { | |
129 | symlen = (int)h->mem_info->daddr.sym->namelen + 4 | |
130 | + unresolved_col_width + 2; | |
131 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, | |
132 | symlen); | |
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); | |
8a6c5b26 ACM |
158 | } |
159 | ||
7ccf4f90 NK |
160 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
161 | { | |
162 | struct rb_node *next = rb_first(&hists->entries); | |
163 | struct hist_entry *n; | |
164 | int row = 0; | |
165 | ||
166 | hists__reset_col_len(hists); | |
167 | ||
168 | while (next && row++ < max_rows) { | |
169 | n = rb_entry(next, struct hist_entry, rb_node); | |
170 | if (!n->filtered) | |
171 | hists__calc_col_len(hists, n); | |
172 | next = rb_next(&n->rb_node); | |
173 | } | |
174 | } | |
175 | ||
12c14278 | 176 | static void hist_entry__add_cpumode_period(struct hist_entry *he, |
c82ee828 | 177 | unsigned int cpumode, u64 period) |
a1645ce1 | 178 | { |
28e2a106 | 179 | switch (cpumode) { |
a1645ce1 | 180 | case PERF_RECORD_MISC_KERNEL: |
b24c28f7 | 181 | he->stat.period_sys += period; |
a1645ce1 ZY |
182 | break; |
183 | case PERF_RECORD_MISC_USER: | |
b24c28f7 | 184 | he->stat.period_us += period; |
a1645ce1 ZY |
185 | break; |
186 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
b24c28f7 | 187 | he->stat.period_guest_sys += period; |
a1645ce1 ZY |
188 | break; |
189 | case PERF_RECORD_MISC_GUEST_USER: | |
b24c28f7 | 190 | he->stat.period_guest_us += period; |
a1645ce1 ZY |
191 | break; |
192 | default: | |
193 | break; | |
194 | } | |
195 | } | |
196 | ||
05484298 AK |
197 | static void he_stat__add_period(struct he_stat *he_stat, u64 period, |
198 | u64 weight) | |
139c0815 | 199 | { |
98a3b32c | 200 | |
139c0815 | 201 | he_stat->period += period; |
05484298 | 202 | he_stat->weight += weight; |
139c0815 NK |
203 | he_stat->nr_events += 1; |
204 | } | |
205 | ||
206 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) | |
207 | { | |
208 | dest->period += src->period; | |
209 | dest->period_sys += src->period_sys; | |
210 | dest->period_us += src->period_us; | |
211 | dest->period_guest_sys += src->period_guest_sys; | |
212 | dest->period_guest_us += src->period_guest_us; | |
213 | dest->nr_events += src->nr_events; | |
05484298 | 214 | dest->weight += src->weight; |
139c0815 NK |
215 | } |
216 | ||
ab81f3fd ACM |
217 | static void hist_entry__decay(struct hist_entry *he) |
218 | { | |
b24c28f7 NK |
219 | he->stat.period = (he->stat.period * 7) / 8; |
220 | he->stat.nr_events = (he->stat.nr_events * 7) / 8; | |
05484298 | 221 | /* XXX need decay for weight too? */ |
ab81f3fd ACM |
222 | } |
223 | ||
224 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) | |
225 | { | |
b24c28f7 | 226 | u64 prev_period = he->stat.period; |
c64550cf ACM |
227 | |
228 | if (prev_period == 0) | |
df71d95f | 229 | return true; |
c64550cf | 230 | |
ab81f3fd | 231 | hist_entry__decay(he); |
c64550cf ACM |
232 | |
233 | if (!he->filtered) | |
b24c28f7 | 234 | hists->stats.total_period -= prev_period - he->stat.period; |
c64550cf | 235 | |
b24c28f7 | 236 | return he->stat.period == 0; |
ab81f3fd ACM |
237 | } |
238 | ||
b079d4e9 ACM |
239 | static void __hists__decay_entries(struct hists *hists, bool zap_user, |
240 | bool zap_kernel, bool threaded) | |
ab81f3fd ACM |
241 | { |
242 | struct rb_node *next = rb_first(&hists->entries); | |
243 | struct hist_entry *n; | |
244 | ||
245 | while (next) { | |
246 | n = rb_entry(next, struct hist_entry, rb_node); | |
247 | next = rb_next(&n->rb_node); | |
df71d95f ACM |
248 | /* |
249 | * We may be annotating this, for instance, so keep it here in | |
250 | * case some it gets new samples, we'll eventually free it when | |
251 | * the user stops browsing and it agains gets fully decayed. | |
252 | */ | |
b079d4e9 ACM |
253 | if (((zap_user && n->level == '.') || |
254 | (zap_kernel && n->level != '.') || | |
255 | hists__decay_entry(hists, n)) && | |
256 | !n->used) { | |
ab81f3fd ACM |
257 | rb_erase(&n->rb_node, &hists->entries); |
258 | ||
e345fa18 | 259 | if (sort__need_collapse || threaded) |
ab81f3fd ACM |
260 | rb_erase(&n->rb_node_in, &hists->entries_collapsed); |
261 | ||
262 | hist_entry__free(n); | |
263 | --hists->nr_entries; | |
264 | } | |
265 | } | |
266 | } | |
267 | ||
b079d4e9 | 268 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) |
e345fa18 | 269 | { |
b079d4e9 | 270 | return __hists__decay_entries(hists, zap_user, zap_kernel, false); |
e345fa18 ACM |
271 | } |
272 | ||
b079d4e9 ACM |
273 | void hists__decay_entries_threaded(struct hists *hists, |
274 | bool zap_user, bool zap_kernel) | |
e345fa18 | 275 | { |
b079d4e9 | 276 | return __hists__decay_entries(hists, zap_user, zap_kernel, true); |
e345fa18 ACM |
277 | } |
278 | ||
3d1d07ec | 279 | /* |
c82ee828 | 280 | * histogram, sorted on item, collects periods |
3d1d07ec JK |
281 | */ |
282 | ||
28e2a106 ACM |
283 | static struct hist_entry *hist_entry__new(struct hist_entry *template) |
284 | { | |
d2009c51 | 285 | size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0; |
98a3b32c | 286 | struct hist_entry *he = zalloc(sizeof(*he) + callchain_size); |
28e2a106 | 287 | |
12c14278 ACM |
288 | if (he != NULL) { |
289 | *he = *template; | |
c4b35351 | 290 | |
12c14278 ACM |
291 | if (he->ms.map) |
292 | he->ms.map->referenced = true; | |
3cf0cb1f SE |
293 | |
294 | if (he->branch_info) { | |
295 | if (he->branch_info->from.map) | |
296 | he->branch_info->from.map->referenced = true; | |
297 | if (he->branch_info->to.map) | |
298 | he->branch_info->to.map->referenced = true; | |
299 | } | |
300 | ||
98a3b32c SE |
301 | if (he->mem_info) { |
302 | if (he->mem_info->iaddr.map) | |
303 | he->mem_info->iaddr.map->referenced = true; | |
304 | if (he->mem_info->daddr.map) | |
305 | he->mem_info->daddr.map->referenced = true; | |
306 | } | |
307 | ||
28e2a106 | 308 | if (symbol_conf.use_callchain) |
12c14278 | 309 | callchain_init(he->callchain); |
b821c732 ACM |
310 | |
311 | INIT_LIST_HEAD(&he->pairs.node); | |
28e2a106 ACM |
312 | } |
313 | ||
12c14278 | 314 | return he; |
28e2a106 ACM |
315 | } |
316 | ||
66f97ed3 | 317 | void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h) |
fefb0b94 | 318 | { |
8a6c5b26 | 319 | if (!h->filtered) { |
42b28ac0 ACM |
320 | hists__calc_col_len(hists, h); |
321 | ++hists->nr_entries; | |
b24c28f7 | 322 | hists->stats.total_period += h->stat.period; |
8a6c5b26 | 323 | } |
fefb0b94 ACM |
324 | } |
325 | ||
7a007ca9 ACM |
326 | static u8 symbol__parent_filter(const struct symbol *parent) |
327 | { | |
328 | if (symbol_conf.exclude_other && parent == NULL) | |
329 | return 1 << HIST_FILTER__PARENT; | |
330 | return 0; | |
331 | } | |
332 | ||
b5387528 RAV |
333 | static struct hist_entry *add_hist_entry(struct hists *hists, |
334 | struct hist_entry *entry, | |
1c02c4d2 | 335 | struct addr_location *al, |
05484298 AK |
336 | u64 period, |
337 | u64 weight) | |
9735abf1 | 338 | { |
1980c2eb | 339 | struct rb_node **p; |
9735abf1 ACM |
340 | struct rb_node *parent = NULL; |
341 | struct hist_entry *he; | |
9735abf1 ACM |
342 | int cmp; |
343 | ||
1980c2eb ACM |
344 | pthread_mutex_lock(&hists->lock); |
345 | ||
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 DM |
362 | |
363 | /* If the map of an existing hist_entry has | |
364 | * become out-of-date due to an exec() or | |
365 | * similar, update it. Otherwise we will | |
366 | * mis-adjust symbol addresses when computing | |
367 | * the history counter to increment. | |
368 | */ | |
369 | if (he->ms.map != entry->ms.map) { | |
370 | he->ms.map = entry->ms.map; | |
371 | if (he->ms.map) | |
372 | he->ms.map->referenced = true; | |
373 | } | |
28e2a106 | 374 | goto out; |
9735abf1 ACM |
375 | } |
376 | ||
377 | if (cmp < 0) | |
378 | p = &(*p)->rb_left; | |
379 | else | |
380 | p = &(*p)->rb_right; | |
381 | } | |
382 | ||
b5387528 | 383 | he = hist_entry__new(entry); |
9735abf1 | 384 | if (!he) |
1980c2eb ACM |
385 | goto out_unlock; |
386 | ||
387 | rb_link_node(&he->rb_node_in, parent, p); | |
388 | rb_insert_color(&he->rb_node_in, hists->entries_in); | |
28e2a106 | 389 | out: |
c82ee828 | 390 | hist_entry__add_cpumode_period(he, al->cpumode, period); |
1980c2eb ACM |
391 | out_unlock: |
392 | pthread_mutex_unlock(&hists->lock); | |
9735abf1 ACM |
393 | return he; |
394 | } | |
395 | ||
98a3b32c SE |
396 | struct hist_entry *__hists__add_mem_entry(struct hists *self, |
397 | struct addr_location *al, | |
398 | struct symbol *sym_parent, | |
399 | struct mem_info *mi, | |
400 | u64 period, | |
401 | u64 weight) | |
402 | { | |
403 | struct hist_entry entry = { | |
404 | .thread = al->thread, | |
405 | .ms = { | |
406 | .map = al->map, | |
407 | .sym = al->sym, | |
408 | }, | |
409 | .stat = { | |
410 | .period = period, | |
411 | .weight = weight, | |
412 | .nr_events = 1, | |
413 | }, | |
414 | .cpu = al->cpu, | |
415 | .ip = al->addr, | |
416 | .level = al->level, | |
417 | .parent = sym_parent, | |
418 | .filtered = symbol__parent_filter(sym_parent), | |
419 | .hists = self, | |
420 | .mem_info = mi, | |
421 | .branch_info = NULL, | |
422 | }; | |
423 | return add_hist_entry(self, &entry, al, period, weight); | |
424 | } | |
425 | ||
b5387528 RAV |
426 | struct hist_entry *__hists__add_branch_entry(struct hists *self, |
427 | struct addr_location *al, | |
428 | struct symbol *sym_parent, | |
429 | struct branch_info *bi, | |
05484298 AK |
430 | u64 period, |
431 | u64 weight) | |
b5387528 RAV |
432 | { |
433 | struct hist_entry entry = { | |
434 | .thread = al->thread, | |
435 | .ms = { | |
436 | .map = bi->to.map, | |
437 | .sym = bi->to.sym, | |
438 | }, | |
439 | .cpu = al->cpu, | |
440 | .ip = bi->to.addr, | |
441 | .level = al->level, | |
b24c28f7 NK |
442 | .stat = { |
443 | .period = period, | |
c4b35351 | 444 | .nr_events = 1, |
05484298 | 445 | .weight = weight, |
b24c28f7 | 446 | }, |
b5387528 RAV |
447 | .parent = sym_parent, |
448 | .filtered = symbol__parent_filter(sym_parent), | |
449 | .branch_info = bi, | |
ae359f19 | 450 | .hists = self, |
98a3b32c | 451 | .mem_info = NULL, |
b5387528 RAV |
452 | }; |
453 | ||
05484298 | 454 | return add_hist_entry(self, &entry, al, period, weight); |
b5387528 RAV |
455 | } |
456 | ||
457 | struct hist_entry *__hists__add_entry(struct hists *self, | |
458 | struct addr_location *al, | |
05484298 AK |
459 | struct symbol *sym_parent, u64 period, |
460 | u64 weight) | |
b5387528 RAV |
461 | { |
462 | struct hist_entry entry = { | |
463 | .thread = al->thread, | |
464 | .ms = { | |
465 | .map = al->map, | |
466 | .sym = al->sym, | |
467 | }, | |
468 | .cpu = al->cpu, | |
469 | .ip = al->addr, | |
470 | .level = al->level, | |
b24c28f7 NK |
471 | .stat = { |
472 | .period = period, | |
c4b35351 | 473 | .nr_events = 1, |
05484298 | 474 | .weight = weight, |
b24c28f7 | 475 | }, |
b5387528 RAV |
476 | .parent = sym_parent, |
477 | .filtered = symbol__parent_filter(sym_parent), | |
ae359f19 | 478 | .hists = self, |
98a3b32c SE |
479 | .branch_info = NULL, |
480 | .mem_info = NULL, | |
b5387528 RAV |
481 | }; |
482 | ||
05484298 | 483 | return add_hist_entry(self, &entry, al, period, weight); |
b5387528 RAV |
484 | } |
485 | ||
3d1d07ec JK |
486 | int64_t |
487 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
488 | { | |
489 | struct sort_entry *se; | |
490 | int64_t cmp = 0; | |
491 | ||
492 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
fcd14984 | 493 | cmp = se->se_cmp(left, right); |
3d1d07ec JK |
494 | if (cmp) |
495 | break; | |
496 | } | |
497 | ||
498 | return cmp; | |
499 | } | |
500 | ||
501 | int64_t | |
502 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | |
503 | { | |
504 | struct sort_entry *se; | |
505 | int64_t cmp = 0; | |
506 | ||
507 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
508 | int64_t (*f)(struct hist_entry *, struct hist_entry *); | |
509 | ||
fcd14984 | 510 | f = se->se_collapse ?: se->se_cmp; |
3d1d07ec JK |
511 | |
512 | cmp = f(left, right); | |
513 | if (cmp) | |
514 | break; | |
515 | } | |
516 | ||
517 | return cmp; | |
518 | } | |
519 | ||
520 | void hist_entry__free(struct hist_entry *he) | |
521 | { | |
580e338d | 522 | free(he->branch_info); |
3d1d07ec JK |
523 | free(he); |
524 | } | |
525 | ||
526 | /* | |
527 | * collapse the histogram | |
528 | */ | |
529 | ||
1d037ca1 | 530 | static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused, |
1b3a0e95 FW |
531 | struct rb_root *root, |
532 | struct hist_entry *he) | |
3d1d07ec | 533 | { |
b9bf0892 | 534 | struct rb_node **p = &root->rb_node; |
3d1d07ec JK |
535 | struct rb_node *parent = NULL; |
536 | struct hist_entry *iter; | |
537 | int64_t cmp; | |
538 | ||
539 | while (*p != NULL) { | |
540 | parent = *p; | |
1980c2eb | 541 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
3d1d07ec JK |
542 | |
543 | cmp = hist_entry__collapse(iter, he); | |
544 | ||
545 | if (!cmp) { | |
139c0815 | 546 | he_stat__add_stat(&iter->stat, &he->stat); |
9ec60972 | 547 | |
1b3a0e95 | 548 | if (symbol_conf.use_callchain) { |
47260645 NK |
549 | callchain_cursor_reset(&callchain_cursor); |
550 | callchain_merge(&callchain_cursor, | |
551 | iter->callchain, | |
1b3a0e95 FW |
552 | he->callchain); |
553 | } | |
3d1d07ec | 554 | hist_entry__free(he); |
fefb0b94 | 555 | return false; |
3d1d07ec JK |
556 | } |
557 | ||
558 | if (cmp < 0) | |
559 | p = &(*p)->rb_left; | |
560 | else | |
561 | p = &(*p)->rb_right; | |
562 | } | |
563 | ||
1980c2eb ACM |
564 | rb_link_node(&he->rb_node_in, parent, p); |
565 | rb_insert_color(&he->rb_node_in, root); | |
fefb0b94 | 566 | return true; |
3d1d07ec JK |
567 | } |
568 | ||
1980c2eb | 569 | static struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
3d1d07ec | 570 | { |
1980c2eb ACM |
571 | struct rb_root *root; |
572 | ||
573 | pthread_mutex_lock(&hists->lock); | |
574 | ||
575 | root = hists->entries_in; | |
576 | if (++hists->entries_in > &hists->entries_in_array[1]) | |
577 | hists->entries_in = &hists->entries_in_array[0]; | |
578 | ||
579 | pthread_mutex_unlock(&hists->lock); | |
580 | ||
581 | return root; | |
582 | } | |
583 | ||
90cf1fb5 ACM |
584 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) |
585 | { | |
586 | hists__filter_entry_by_dso(hists, he); | |
587 | hists__filter_entry_by_thread(hists, he); | |
e94d53eb | 588 | hists__filter_entry_by_symbol(hists, he); |
90cf1fb5 ACM |
589 | } |
590 | ||
1980c2eb ACM |
591 | static void __hists__collapse_resort(struct hists *hists, bool threaded) |
592 | { | |
593 | struct rb_root *root; | |
3d1d07ec JK |
594 | struct rb_node *next; |
595 | struct hist_entry *n; | |
596 | ||
1980c2eb | 597 | if (!sort__need_collapse && !threaded) |
3d1d07ec JK |
598 | return; |
599 | ||
1980c2eb ACM |
600 | root = hists__get_rotate_entries_in(hists); |
601 | next = rb_first(root); | |
b9bf0892 | 602 | |
3d1d07ec | 603 | while (next) { |
1980c2eb ACM |
604 | n = rb_entry(next, struct hist_entry, rb_node_in); |
605 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 606 | |
1980c2eb | 607 | rb_erase(&n->rb_node_in, root); |
90cf1fb5 ACM |
608 | if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) { |
609 | /* | |
610 | * If it wasn't combined with one of the entries already | |
611 | * collapsed, we need to apply the filters that may have | |
612 | * been set by, say, the hist_browser. | |
613 | */ | |
614 | hists__apply_filters(hists, n); | |
90cf1fb5 | 615 | } |
3d1d07ec | 616 | } |
1980c2eb | 617 | } |
b9bf0892 | 618 | |
1980c2eb ACM |
619 | void hists__collapse_resort(struct hists *hists) |
620 | { | |
621 | return __hists__collapse_resort(hists, false); | |
622 | } | |
623 | ||
624 | void hists__collapse_resort_threaded(struct hists *hists) | |
625 | { | |
626 | return __hists__collapse_resort(hists, true); | |
3d1d07ec JK |
627 | } |
628 | ||
629 | /* | |
c82ee828 | 630 | * reverse the map, sort on period. |
3d1d07ec JK |
631 | */ |
632 | ||
29d720ed NK |
633 | static int period_cmp(u64 period_a, u64 period_b) |
634 | { | |
635 | if (period_a > period_b) | |
636 | return 1; | |
637 | if (period_a < period_b) | |
638 | return -1; | |
639 | return 0; | |
640 | } | |
641 | ||
642 | static int hist_entry__sort_on_period(struct hist_entry *a, | |
643 | struct hist_entry *b) | |
644 | { | |
645 | int ret; | |
646 | int i, nr_members; | |
647 | struct perf_evsel *evsel; | |
648 | struct hist_entry *pair; | |
649 | u64 *periods_a, *periods_b; | |
650 | ||
651 | ret = period_cmp(a->stat.period, b->stat.period); | |
652 | if (ret || !symbol_conf.event_group) | |
653 | return ret; | |
654 | ||
655 | evsel = hists_to_evsel(a->hists); | |
656 | nr_members = evsel->nr_members; | |
657 | if (nr_members <= 1) | |
658 | return ret; | |
659 | ||
660 | periods_a = zalloc(sizeof(periods_a) * nr_members); | |
661 | periods_b = zalloc(sizeof(periods_b) * nr_members); | |
662 | ||
663 | if (!periods_a || !periods_b) | |
664 | goto out; | |
665 | ||
666 | list_for_each_entry(pair, &a->pairs.head, pairs.node) { | |
667 | evsel = hists_to_evsel(pair->hists); | |
668 | periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period; | |
669 | } | |
670 | ||
671 | list_for_each_entry(pair, &b->pairs.head, pairs.node) { | |
672 | evsel = hists_to_evsel(pair->hists); | |
673 | periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period; | |
674 | } | |
675 | ||
676 | for (i = 1; i < nr_members; i++) { | |
677 | ret = period_cmp(periods_a[i], periods_b[i]); | |
678 | if (ret) | |
679 | break; | |
680 | } | |
681 | ||
682 | out: | |
683 | free(periods_a); | |
684 | free(periods_b); | |
685 | ||
686 | return ret; | |
687 | } | |
688 | ||
1c02c4d2 ACM |
689 | static void __hists__insert_output_entry(struct rb_root *entries, |
690 | struct hist_entry *he, | |
691 | u64 min_callchain_hits) | |
3d1d07ec | 692 | { |
1c02c4d2 | 693 | struct rb_node **p = &entries->rb_node; |
3d1d07ec JK |
694 | struct rb_node *parent = NULL; |
695 | struct hist_entry *iter; | |
696 | ||
d599db3f | 697 | if (symbol_conf.use_callchain) |
b9fb9304 | 698 | callchain_param.sort(&he->sorted_chain, he->callchain, |
3d1d07ec JK |
699 | min_callchain_hits, &callchain_param); |
700 | ||
701 | while (*p != NULL) { | |
702 | parent = *p; | |
703 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
704 | ||
29d720ed | 705 | if (hist_entry__sort_on_period(he, iter) > 0) |
3d1d07ec JK |
706 | p = &(*p)->rb_left; |
707 | else | |
708 | p = &(*p)->rb_right; | |
709 | } | |
710 | ||
711 | rb_link_node(&he->rb_node, parent, p); | |
1c02c4d2 | 712 | rb_insert_color(&he->rb_node, entries); |
3d1d07ec JK |
713 | } |
714 | ||
1980c2eb | 715 | static void __hists__output_resort(struct hists *hists, bool threaded) |
3d1d07ec | 716 | { |
1980c2eb | 717 | struct rb_root *root; |
3d1d07ec JK |
718 | struct rb_node *next; |
719 | struct hist_entry *n; | |
3d1d07ec JK |
720 | u64 min_callchain_hits; |
721 | ||
42b28ac0 | 722 | min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100); |
3d1d07ec | 723 | |
1980c2eb ACM |
724 | if (sort__need_collapse || threaded) |
725 | root = &hists->entries_collapsed; | |
726 | else | |
727 | root = hists->entries_in; | |
728 | ||
729 | next = rb_first(root); | |
730 | hists->entries = RB_ROOT; | |
3d1d07ec | 731 | |
42b28ac0 | 732 | hists->nr_entries = 0; |
7928631a | 733 | hists->stats.total_period = 0; |
42b28ac0 | 734 | hists__reset_col_len(hists); |
fefb0b94 | 735 | |
3d1d07ec | 736 | while (next) { |
1980c2eb ACM |
737 | n = rb_entry(next, struct hist_entry, rb_node_in); |
738 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 739 | |
1980c2eb | 740 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits); |
42b28ac0 | 741 | hists__inc_nr_entries(hists, n); |
3d1d07ec | 742 | } |
1980c2eb | 743 | } |
b9bf0892 | 744 | |
1980c2eb ACM |
745 | void hists__output_resort(struct hists *hists) |
746 | { | |
747 | return __hists__output_resort(hists, false); | |
748 | } | |
749 | ||
750 | void hists__output_resort_threaded(struct hists *hists) | |
751 | { | |
752 | return __hists__output_resort(hists, true); | |
3d1d07ec | 753 | } |
4ecf84d0 | 754 | |
42b28ac0 | 755 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, |
cc5edb0e ACM |
756 | enum hist_filter filter) |
757 | { | |
758 | h->filtered &= ~(1 << filter); | |
759 | if (h->filtered) | |
760 | return; | |
761 | ||
42b28ac0 | 762 | ++hists->nr_entries; |
0f0cbf7a | 763 | if (h->ms.unfolded) |
42b28ac0 | 764 | hists->nr_entries += h->nr_rows; |
0f0cbf7a | 765 | h->row_offset = 0; |
b24c28f7 NK |
766 | hists->stats.total_period += h->stat.period; |
767 | hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events; | |
cc5edb0e | 768 | |
42b28ac0 | 769 | hists__calc_col_len(hists, h); |
cc5edb0e ACM |
770 | } |
771 | ||
90cf1fb5 ACM |
772 | |
773 | static bool hists__filter_entry_by_dso(struct hists *hists, | |
774 | struct hist_entry *he) | |
775 | { | |
776 | if (hists->dso_filter != NULL && | |
777 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { | |
778 | he->filtered |= (1 << HIST_FILTER__DSO); | |
779 | return true; | |
780 | } | |
781 | ||
782 | return false; | |
783 | } | |
784 | ||
d7b76f09 | 785 | void hists__filter_by_dso(struct hists *hists) |
b09e0190 ACM |
786 | { |
787 | struct rb_node *nd; | |
788 | ||
42b28ac0 ACM |
789 | hists->nr_entries = hists->stats.total_period = 0; |
790 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; | |
791 | hists__reset_col_len(hists); | |
b09e0190 | 792 | |
42b28ac0 | 793 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
b09e0190 ACM |
794 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
795 | ||
796 | if (symbol_conf.exclude_other && !h->parent) | |
797 | continue; | |
798 | ||
90cf1fb5 | 799 | if (hists__filter_entry_by_dso(hists, h)) |
b09e0190 | 800 | continue; |
b09e0190 | 801 | |
42b28ac0 | 802 | hists__remove_entry_filter(hists, h, HIST_FILTER__DSO); |
b09e0190 ACM |
803 | } |
804 | } | |
805 | ||
90cf1fb5 ACM |
806 | static bool hists__filter_entry_by_thread(struct hists *hists, |
807 | struct hist_entry *he) | |
808 | { | |
809 | if (hists->thread_filter != NULL && | |
810 | he->thread != hists->thread_filter) { | |
811 | he->filtered |= (1 << HIST_FILTER__THREAD); | |
812 | return true; | |
813 | } | |
814 | ||
815 | return false; | |
816 | } | |
817 | ||
d7b76f09 | 818 | void hists__filter_by_thread(struct hists *hists) |
b09e0190 ACM |
819 | { |
820 | struct rb_node *nd; | |
821 | ||
42b28ac0 ACM |
822 | hists->nr_entries = hists->stats.total_period = 0; |
823 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; | |
824 | hists__reset_col_len(hists); | |
b09e0190 | 825 | |
42b28ac0 | 826 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
b09e0190 ACM |
827 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
828 | ||
90cf1fb5 | 829 | if (hists__filter_entry_by_thread(hists, h)) |
b09e0190 | 830 | continue; |
cc5edb0e | 831 | |
42b28ac0 | 832 | hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD); |
b09e0190 ACM |
833 | } |
834 | } | |
ef7b93a1 | 835 | |
e94d53eb NK |
836 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
837 | struct hist_entry *he) | |
838 | { | |
839 | if (hists->symbol_filter_str != NULL && | |
840 | (!he->ms.sym || strstr(he->ms.sym->name, | |
841 | hists->symbol_filter_str) == NULL)) { | |
842 | he->filtered |= (1 << HIST_FILTER__SYMBOL); | |
843 | return true; | |
844 | } | |
845 | ||
846 | return false; | |
847 | } | |
848 | ||
849 | void hists__filter_by_symbol(struct hists *hists) | |
850 | { | |
851 | struct rb_node *nd; | |
852 | ||
853 | hists->nr_entries = hists->stats.total_period = 0; | |
854 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; | |
855 | hists__reset_col_len(hists); | |
856 | ||
857 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { | |
858 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
859 | ||
860 | if (hists__filter_entry_by_symbol(hists, h)) | |
861 | continue; | |
862 | ||
863 | hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL); | |
864 | } | |
865 | } | |
866 | ||
2f525d01 | 867 | int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip) |
ef7b93a1 | 868 | { |
2f525d01 | 869 | return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip); |
ef7b93a1 ACM |
870 | } |
871 | ||
ce6f4fab | 872 | int hist_entry__annotate(struct hist_entry *he, size_t privsize) |
ef7b93a1 | 873 | { |
ce6f4fab | 874 | return symbol__annotate(he->ms.sym, he->ms.map, privsize); |
ef7b93a1 | 875 | } |
c8446b9b | 876 | |
28a6b6aa ACM |
877 | void events_stats__inc(struct events_stats *stats, u32 type) |
878 | { | |
879 | ++stats->nr_events[0]; | |
880 | ++stats->nr_events[type]; | |
881 | } | |
882 | ||
42b28ac0 | 883 | void hists__inc_nr_events(struct hists *hists, u32 type) |
c8446b9b | 884 | { |
28a6b6aa | 885 | events_stats__inc(&hists->stats, type); |
c8446b9b | 886 | } |
95529be4 | 887 | |
494d70a1 ACM |
888 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, |
889 | struct hist_entry *pair) | |
890 | { | |
ce74f60e NK |
891 | struct rb_root *root; |
892 | struct rb_node **p; | |
494d70a1 ACM |
893 | struct rb_node *parent = NULL; |
894 | struct hist_entry *he; | |
895 | int cmp; | |
896 | ||
ce74f60e NK |
897 | if (sort__need_collapse) |
898 | root = &hists->entries_collapsed; | |
899 | else | |
900 | root = hists->entries_in; | |
901 | ||
902 | p = &root->rb_node; | |
903 | ||
494d70a1 ACM |
904 | while (*p != NULL) { |
905 | parent = *p; | |
ce74f60e | 906 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
494d70a1 | 907 | |
ce74f60e | 908 | cmp = hist_entry__collapse(he, pair); |
494d70a1 ACM |
909 | |
910 | if (!cmp) | |
911 | goto out; | |
912 | ||
913 | if (cmp < 0) | |
914 | p = &(*p)->rb_left; | |
915 | else | |
916 | p = &(*p)->rb_right; | |
917 | } | |
918 | ||
919 | he = hist_entry__new(pair); | |
920 | if (he) { | |
30193d78 ACM |
921 | memset(&he->stat, 0, sizeof(he->stat)); |
922 | he->hists = hists; | |
ce74f60e NK |
923 | rb_link_node(&he->rb_node_in, parent, p); |
924 | rb_insert_color(&he->rb_node_in, root); | |
494d70a1 ACM |
925 | hists__inc_nr_entries(hists, he); |
926 | } | |
927 | out: | |
928 | return he; | |
929 | } | |
930 | ||
95529be4 ACM |
931 | static struct hist_entry *hists__find_entry(struct hists *hists, |
932 | struct hist_entry *he) | |
933 | { | |
ce74f60e NK |
934 | struct rb_node *n; |
935 | ||
936 | if (sort__need_collapse) | |
937 | n = hists->entries_collapsed.rb_node; | |
938 | else | |
939 | n = hists->entries_in->rb_node; | |
95529be4 ACM |
940 | |
941 | while (n) { | |
ce74f60e NK |
942 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); |
943 | int64_t cmp = hist_entry__collapse(iter, he); | |
95529be4 ACM |
944 | |
945 | if (cmp < 0) | |
946 | n = n->rb_left; | |
947 | else if (cmp > 0) | |
948 | n = n->rb_right; | |
949 | else | |
950 | return iter; | |
951 | } | |
952 | ||
953 | return NULL; | |
954 | } | |
955 | ||
956 | /* | |
957 | * Look for pairs to link to the leader buckets (hist_entries): | |
958 | */ | |
959 | void hists__match(struct hists *leader, struct hists *other) | |
960 | { | |
ce74f60e | 961 | struct rb_root *root; |
95529be4 ACM |
962 | struct rb_node *nd; |
963 | struct hist_entry *pos, *pair; | |
964 | ||
ce74f60e NK |
965 | if (sort__need_collapse) |
966 | root = &leader->entries_collapsed; | |
967 | else | |
968 | root = leader->entries_in; | |
969 | ||
970 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
971 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
95529be4 ACM |
972 | pair = hists__find_entry(other, pos); |
973 | ||
974 | if (pair) | |
5fa9041b | 975 | hist_entry__add_pair(pair, pos); |
95529be4 ACM |
976 | } |
977 | } | |
494d70a1 ACM |
978 | |
979 | /* | |
980 | * Look for entries in the other hists that are not present in the leader, if | |
981 | * we find them, just add a dummy entry on the leader hists, with period=0, | |
982 | * nr_events=0, to serve as the list header. | |
983 | */ | |
984 | int hists__link(struct hists *leader, struct hists *other) | |
985 | { | |
ce74f60e | 986 | struct rb_root *root; |
494d70a1 ACM |
987 | struct rb_node *nd; |
988 | struct hist_entry *pos, *pair; | |
989 | ||
ce74f60e NK |
990 | if (sort__need_collapse) |
991 | root = &other->entries_collapsed; | |
992 | else | |
993 | root = other->entries_in; | |
994 | ||
995 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
996 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
494d70a1 ACM |
997 | |
998 | if (!hist_entry__has_pairs(pos)) { | |
999 | pair = hists__add_dummy_entry(leader, pos); | |
1000 | if (pair == NULL) | |
1001 | return -1; | |
5fa9041b | 1002 | hist_entry__add_pair(pos, pair); |
494d70a1 ACM |
1003 | } |
1004 | } | |
1005 | ||
1006 | return 0; | |
1007 | } |