]>
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" | |
9b33827d | 6 | #include <math.h> |
3d1d07ec | 7 | |
7a007ca9 ACM |
8 | enum hist_filter { |
9 | HIST_FILTER__DSO, | |
10 | HIST_FILTER__THREAD, | |
11 | HIST_FILTER__PARENT, | |
12 | }; | |
13 | ||
3d1d07ec JK |
14 | struct callchain_param callchain_param = { |
15 | .mode = CHAIN_GRAPH_REL, | |
16 | .min_percent = 0.5 | |
17 | }; | |
18 | ||
8a6c5b26 ACM |
19 | u16 hists__col_len(struct hists *self, enum hist_column col) |
20 | { | |
21 | return self->col_len[col]; | |
22 | } | |
23 | ||
24 | void hists__set_col_len(struct hists *self, enum hist_column col, u16 len) | |
25 | { | |
26 | self->col_len[col] = len; | |
27 | } | |
28 | ||
29 | bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len) | |
30 | { | |
31 | if (len > hists__col_len(self, col)) { | |
32 | hists__set_col_len(self, col, len); | |
33 | return true; | |
34 | } | |
35 | return false; | |
36 | } | |
37 | ||
38 | static void hists__reset_col_len(struct hists *self) | |
39 | { | |
40 | enum hist_column col; | |
41 | ||
42 | for (col = 0; col < HISTC_NR_COLS; ++col) | |
43 | hists__set_col_len(self, col, 0); | |
44 | } | |
45 | ||
46 | static void hists__calc_col_len(struct hists *self, struct hist_entry *h) | |
47 | { | |
48 | u16 len; | |
49 | ||
50 | if (h->ms.sym) | |
51 | hists__new_col_len(self, HISTC_SYMBOL, h->ms.sym->namelen); | |
52 | ||
53 | len = thread__comm_len(h->thread); | |
54 | if (hists__new_col_len(self, HISTC_COMM, len)) | |
55 | hists__set_col_len(self, HISTC_THREAD, len + 6); | |
56 | ||
57 | if (h->ms.map) { | |
58 | len = dso__name_len(h->ms.map->dso); | |
59 | hists__new_col_len(self, HISTC_DSO, len); | |
60 | } | |
61 | } | |
62 | ||
c82ee828 ACM |
63 | static void hist_entry__add_cpumode_period(struct hist_entry *self, |
64 | unsigned int cpumode, u64 period) | |
a1645ce1 | 65 | { |
28e2a106 | 66 | switch (cpumode) { |
a1645ce1 | 67 | case PERF_RECORD_MISC_KERNEL: |
c82ee828 | 68 | self->period_sys += period; |
a1645ce1 ZY |
69 | break; |
70 | case PERF_RECORD_MISC_USER: | |
c82ee828 | 71 | self->period_us += period; |
a1645ce1 ZY |
72 | break; |
73 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
c82ee828 | 74 | self->period_guest_sys += period; |
a1645ce1 ZY |
75 | break; |
76 | case PERF_RECORD_MISC_GUEST_USER: | |
c82ee828 | 77 | self->period_guest_us += period; |
a1645ce1 ZY |
78 | break; |
79 | default: | |
80 | break; | |
81 | } | |
82 | } | |
83 | ||
3d1d07ec | 84 | /* |
c82ee828 | 85 | * histogram, sorted on item, collects periods |
3d1d07ec JK |
86 | */ |
87 | ||
28e2a106 ACM |
88 | static struct hist_entry *hist_entry__new(struct hist_entry *template) |
89 | { | |
d2009c51 | 90 | size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0; |
28e2a106 ACM |
91 | struct hist_entry *self = malloc(sizeof(*self) + callchain_size); |
92 | ||
93 | if (self != NULL) { | |
94 | *self = *template; | |
c82ee828 | 95 | self->nr_events = 1; |
0a1eae39 ACM |
96 | if (self->ms.map) |
97 | self->ms.map->referenced = true; | |
28e2a106 ACM |
98 | if (symbol_conf.use_callchain) |
99 | callchain_init(self->callchain); | |
100 | } | |
101 | ||
102 | return self; | |
103 | } | |
104 | ||
8a6c5b26 | 105 | static void hists__inc_nr_entries(struct hists *self, struct hist_entry *h) |
fefb0b94 | 106 | { |
8a6c5b26 ACM |
107 | if (!h->filtered) { |
108 | hists__calc_col_len(self, h); | |
109 | ++self->nr_entries; | |
110 | } | |
fefb0b94 ACM |
111 | } |
112 | ||
7a007ca9 ACM |
113 | static u8 symbol__parent_filter(const struct symbol *parent) |
114 | { | |
115 | if (symbol_conf.exclude_other && parent == NULL) | |
116 | return 1 << HIST_FILTER__PARENT; | |
117 | return 0; | |
118 | } | |
119 | ||
1c02c4d2 ACM |
120 | struct hist_entry *__hists__add_entry(struct hists *self, |
121 | struct addr_location *al, | |
c82ee828 | 122 | struct symbol *sym_parent, u64 period) |
9735abf1 | 123 | { |
1c02c4d2 | 124 | struct rb_node **p = &self->entries.rb_node; |
9735abf1 ACM |
125 | struct rb_node *parent = NULL; |
126 | struct hist_entry *he; | |
127 | struct hist_entry entry = { | |
1ed091c4 | 128 | .thread = al->thread, |
59fd5306 ACM |
129 | .ms = { |
130 | .map = al->map, | |
131 | .sym = al->sym, | |
132 | }, | |
f60f3593 | 133 | .cpu = al->cpu, |
1ed091c4 ACM |
134 | .ip = al->addr, |
135 | .level = al->level, | |
c82ee828 | 136 | .period = period, |
9735abf1 | 137 | .parent = sym_parent, |
7a007ca9 | 138 | .filtered = symbol__parent_filter(sym_parent), |
9735abf1 ACM |
139 | }; |
140 | int cmp; | |
141 | ||
142 | while (*p != NULL) { | |
143 | parent = *p; | |
144 | he = rb_entry(parent, struct hist_entry, rb_node); | |
145 | ||
146 | cmp = hist_entry__cmp(&entry, he); | |
147 | ||
148 | if (!cmp) { | |
c82ee828 ACM |
149 | he->period += period; |
150 | ++he->nr_events; | |
28e2a106 | 151 | goto out; |
9735abf1 ACM |
152 | } |
153 | ||
154 | if (cmp < 0) | |
155 | p = &(*p)->rb_left; | |
156 | else | |
157 | p = &(*p)->rb_right; | |
158 | } | |
159 | ||
28e2a106 | 160 | he = hist_entry__new(&entry); |
9735abf1 ACM |
161 | if (!he) |
162 | return NULL; | |
9735abf1 | 163 | rb_link_node(&he->rb_node, parent, p); |
1c02c4d2 | 164 | rb_insert_color(&he->rb_node, &self->entries); |
fefb0b94 | 165 | hists__inc_nr_entries(self, he); |
28e2a106 | 166 | out: |
c82ee828 | 167 | hist_entry__add_cpumode_period(he, al->cpumode, period); |
9735abf1 ACM |
168 | return he; |
169 | } | |
170 | ||
3d1d07ec JK |
171 | int64_t |
172 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
173 | { | |
174 | struct sort_entry *se; | |
175 | int64_t cmp = 0; | |
176 | ||
177 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
fcd14984 | 178 | cmp = se->se_cmp(left, right); |
3d1d07ec JK |
179 | if (cmp) |
180 | break; | |
181 | } | |
182 | ||
183 | return cmp; | |
184 | } | |
185 | ||
186 | int64_t | |
187 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | |
188 | { | |
189 | struct sort_entry *se; | |
190 | int64_t cmp = 0; | |
191 | ||
192 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
193 | int64_t (*f)(struct hist_entry *, struct hist_entry *); | |
194 | ||
fcd14984 | 195 | f = se->se_collapse ?: se->se_cmp; |
3d1d07ec JK |
196 | |
197 | cmp = f(left, right); | |
198 | if (cmp) | |
199 | break; | |
200 | } | |
201 | ||
202 | return cmp; | |
203 | } | |
204 | ||
205 | void hist_entry__free(struct hist_entry *he) | |
206 | { | |
207 | free(he); | |
208 | } | |
209 | ||
210 | /* | |
211 | * collapse the histogram | |
212 | */ | |
213 | ||
1b3a0e95 FW |
214 | static bool hists__collapse_insert_entry(struct hists *self, |
215 | struct rb_root *root, | |
216 | struct hist_entry *he) | |
3d1d07ec | 217 | { |
b9bf0892 | 218 | struct rb_node **p = &root->rb_node; |
3d1d07ec JK |
219 | struct rb_node *parent = NULL; |
220 | struct hist_entry *iter; | |
221 | int64_t cmp; | |
222 | ||
223 | while (*p != NULL) { | |
224 | parent = *p; | |
225 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
226 | ||
227 | cmp = hist_entry__collapse(iter, he); | |
228 | ||
229 | if (!cmp) { | |
c82ee828 | 230 | iter->period += he->period; |
1b3a0e95 FW |
231 | if (symbol_conf.use_callchain) { |
232 | callchain_cursor_reset(&self->callchain_cursor); | |
233 | callchain_merge(&self->callchain_cursor, iter->callchain, | |
234 | he->callchain); | |
235 | } | |
3d1d07ec | 236 | hist_entry__free(he); |
fefb0b94 | 237 | return false; |
3d1d07ec JK |
238 | } |
239 | ||
240 | if (cmp < 0) | |
241 | p = &(*p)->rb_left; | |
242 | else | |
243 | p = &(*p)->rb_right; | |
244 | } | |
245 | ||
246 | rb_link_node(&he->rb_node, parent, p); | |
b9bf0892 | 247 | rb_insert_color(&he->rb_node, root); |
fefb0b94 | 248 | return true; |
3d1d07ec JK |
249 | } |
250 | ||
1c02c4d2 | 251 | void hists__collapse_resort(struct hists *self) |
3d1d07ec | 252 | { |
b9bf0892 | 253 | struct rb_root tmp; |
3d1d07ec JK |
254 | struct rb_node *next; |
255 | struct hist_entry *n; | |
256 | ||
257 | if (!sort__need_collapse) | |
258 | return; | |
259 | ||
b9bf0892 | 260 | tmp = RB_ROOT; |
1c02c4d2 | 261 | next = rb_first(&self->entries); |
fefb0b94 | 262 | self->nr_entries = 0; |
8a6c5b26 | 263 | hists__reset_col_len(self); |
b9bf0892 | 264 | |
3d1d07ec JK |
265 | while (next) { |
266 | n = rb_entry(next, struct hist_entry, rb_node); | |
267 | next = rb_next(&n->rb_node); | |
268 | ||
1c02c4d2 | 269 | rb_erase(&n->rb_node, &self->entries); |
1b3a0e95 | 270 | if (hists__collapse_insert_entry(self, &tmp, n)) |
fefb0b94 | 271 | hists__inc_nr_entries(self, n); |
3d1d07ec | 272 | } |
b9bf0892 | 273 | |
1c02c4d2 | 274 | self->entries = tmp; |
3d1d07ec JK |
275 | } |
276 | ||
277 | /* | |
c82ee828 | 278 | * reverse the map, sort on period. |
3d1d07ec JK |
279 | */ |
280 | ||
1c02c4d2 ACM |
281 | static void __hists__insert_output_entry(struct rb_root *entries, |
282 | struct hist_entry *he, | |
283 | u64 min_callchain_hits) | |
3d1d07ec | 284 | { |
1c02c4d2 | 285 | struct rb_node **p = &entries->rb_node; |
3d1d07ec JK |
286 | struct rb_node *parent = NULL; |
287 | struct hist_entry *iter; | |
288 | ||
d599db3f | 289 | if (symbol_conf.use_callchain) |
b9fb9304 | 290 | callchain_param.sort(&he->sorted_chain, he->callchain, |
3d1d07ec JK |
291 | min_callchain_hits, &callchain_param); |
292 | ||
293 | while (*p != NULL) { | |
294 | parent = *p; | |
295 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
296 | ||
c82ee828 | 297 | if (he->period > iter->period) |
3d1d07ec JK |
298 | p = &(*p)->rb_left; |
299 | else | |
300 | p = &(*p)->rb_right; | |
301 | } | |
302 | ||
303 | rb_link_node(&he->rb_node, parent, p); | |
1c02c4d2 | 304 | rb_insert_color(&he->rb_node, entries); |
3d1d07ec JK |
305 | } |
306 | ||
fefb0b94 | 307 | void hists__output_resort(struct hists *self) |
3d1d07ec | 308 | { |
b9bf0892 | 309 | struct rb_root tmp; |
3d1d07ec JK |
310 | struct rb_node *next; |
311 | struct hist_entry *n; | |
3d1d07ec JK |
312 | u64 min_callchain_hits; |
313 | ||
cee75ac7 | 314 | min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100); |
3d1d07ec | 315 | |
b9bf0892 | 316 | tmp = RB_ROOT; |
1c02c4d2 | 317 | next = rb_first(&self->entries); |
3d1d07ec | 318 | |
fefb0b94 | 319 | self->nr_entries = 0; |
8a6c5b26 | 320 | hists__reset_col_len(self); |
fefb0b94 | 321 | |
3d1d07ec JK |
322 | while (next) { |
323 | n = rb_entry(next, struct hist_entry, rb_node); | |
324 | next = rb_next(&n->rb_node); | |
325 | ||
1c02c4d2 ACM |
326 | rb_erase(&n->rb_node, &self->entries); |
327 | __hists__insert_output_entry(&tmp, n, min_callchain_hits); | |
fefb0b94 | 328 | hists__inc_nr_entries(self, n); |
3d1d07ec | 329 | } |
b9bf0892 | 330 | |
1c02c4d2 | 331 | self->entries = tmp; |
3d1d07ec | 332 | } |
4ecf84d0 ACM |
333 | |
334 | static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin) | |
335 | { | |
336 | int i; | |
337 | int ret = fprintf(fp, " "); | |
338 | ||
339 | for (i = 0; i < left_margin; i++) | |
340 | ret += fprintf(fp, " "); | |
341 | ||
342 | return ret; | |
343 | } | |
344 | ||
345 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, | |
346 | int left_margin) | |
347 | { | |
348 | int i; | |
349 | size_t ret = callchain__fprintf_left_margin(fp, left_margin); | |
350 | ||
351 | for (i = 0; i < depth; i++) | |
352 | if (depth_mask & (1 << i)) | |
353 | ret += fprintf(fp, "| "); | |
354 | else | |
355 | ret += fprintf(fp, " "); | |
356 | ||
357 | ret += fprintf(fp, "\n"); | |
358 | ||
359 | return ret; | |
360 | } | |
361 | ||
362 | static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, | |
c82ee828 | 363 | int depth, int depth_mask, int period, |
d425de54 | 364 | u64 total_samples, u64 hits, |
4ecf84d0 ACM |
365 | int left_margin) |
366 | { | |
367 | int i; | |
368 | size_t ret = 0; | |
369 | ||
370 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
371 | for (i = 0; i < depth; i++) { | |
372 | if (depth_mask & (1 << i)) | |
373 | ret += fprintf(fp, "|"); | |
374 | else | |
375 | ret += fprintf(fp, " "); | |
c82ee828 | 376 | if (!period && i == depth - 1) { |
4ecf84d0 ACM |
377 | double percent; |
378 | ||
379 | percent = hits * 100.0 / total_samples; | |
380 | ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent); | |
381 | } else | |
382 | ret += fprintf(fp, "%s", " "); | |
383 | } | |
b3c9ac08 ACM |
384 | if (chain->ms.sym) |
385 | ret += fprintf(fp, "%s\n", chain->ms.sym->name); | |
4ecf84d0 ACM |
386 | else |
387 | ret += fprintf(fp, "%p\n", (void *)(long)chain->ip); | |
388 | ||
389 | return ret; | |
390 | } | |
391 | ||
392 | static struct symbol *rem_sq_bracket; | |
393 | static struct callchain_list rem_hits; | |
394 | ||
395 | static void init_rem_hits(void) | |
396 | { | |
397 | rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6); | |
398 | if (!rem_sq_bracket) { | |
399 | fprintf(stderr, "Not enough memory to display remaining hits\n"); | |
400 | return; | |
401 | } | |
402 | ||
403 | strcpy(rem_sq_bracket->name, "[...]"); | |
b3c9ac08 | 404 | rem_hits.ms.sym = rem_sq_bracket; |
4ecf84d0 ACM |
405 | } |
406 | ||
407 | static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self, | |
408 | u64 total_samples, int depth, | |
409 | int depth_mask, int left_margin) | |
410 | { | |
411 | struct rb_node *node, *next; | |
412 | struct callchain_node *child; | |
413 | struct callchain_list *chain; | |
414 | int new_depth_mask = depth_mask; | |
415 | u64 new_total; | |
416 | u64 remaining; | |
417 | size_t ret = 0; | |
418 | int i; | |
232a5c94 | 419 | uint entries_printed = 0; |
4ecf84d0 ACM |
420 | |
421 | if (callchain_param.mode == CHAIN_GRAPH_REL) | |
422 | new_total = self->children_hit; | |
423 | else | |
424 | new_total = total_samples; | |
425 | ||
426 | remaining = new_total; | |
427 | ||
428 | node = rb_first(&self->rb_root); | |
429 | while (node) { | |
430 | u64 cumul; | |
431 | ||
432 | child = rb_entry(node, struct callchain_node, rb_node); | |
f08c3154 | 433 | cumul = callchain_cumul_hits(child); |
4ecf84d0 ACM |
434 | remaining -= cumul; |
435 | ||
436 | /* | |
437 | * The depth mask manages the output of pipes that show | |
438 | * the depth. We don't want to keep the pipes of the current | |
439 | * level for the last child of this depth. | |
440 | * Except if we have remaining filtered hits. They will | |
441 | * supersede the last child | |
442 | */ | |
443 | next = rb_next(node); | |
444 | if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining)) | |
445 | new_depth_mask &= ~(1 << (depth - 1)); | |
446 | ||
447 | /* | |
3ad2f3fb | 448 | * But we keep the older depth mask for the line separator |
4ecf84d0 ACM |
449 | * to keep the level link until we reach the last child |
450 | */ | |
451 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, | |
452 | left_margin); | |
453 | i = 0; | |
454 | list_for_each_entry(chain, &child->val, list) { | |
4ecf84d0 ACM |
455 | ret += ipchain__fprintf_graph(fp, chain, depth, |
456 | new_depth_mask, i++, | |
457 | new_total, | |
458 | cumul, | |
459 | left_margin); | |
460 | } | |
461 | ret += __callchain__fprintf_graph(fp, child, new_total, | |
462 | depth + 1, | |
463 | new_depth_mask | (1 << depth), | |
464 | left_margin); | |
465 | node = next; | |
232a5c94 ACM |
466 | if (++entries_printed == callchain_param.print_limit) |
467 | break; | |
4ecf84d0 ACM |
468 | } |
469 | ||
470 | if (callchain_param.mode == CHAIN_GRAPH_REL && | |
471 | remaining && remaining != new_total) { | |
472 | ||
473 | if (!rem_sq_bracket) | |
474 | return ret; | |
475 | ||
476 | new_depth_mask &= ~(1 << (depth - 1)); | |
477 | ||
478 | ret += ipchain__fprintf_graph(fp, &rem_hits, depth, | |
479 | new_depth_mask, 0, new_total, | |
480 | remaining, left_margin); | |
481 | } | |
482 | ||
483 | return ret; | |
484 | } | |
485 | ||
486 | static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self, | |
487 | u64 total_samples, int left_margin) | |
488 | { | |
489 | struct callchain_list *chain; | |
490 | bool printed = false; | |
491 | int i = 0; | |
492 | int ret = 0; | |
232a5c94 | 493 | u32 entries_printed = 0; |
4ecf84d0 ACM |
494 | |
495 | list_for_each_entry(chain, &self->val, list) { | |
4ecf84d0 ACM |
496 | if (!i++ && sort__first_dimension == SORT_SYM) |
497 | continue; | |
498 | ||
499 | if (!printed) { | |
500 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
501 | ret += fprintf(fp, "|\n"); | |
502 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
503 | ret += fprintf(fp, "---"); | |
504 | ||
505 | left_margin += 3; | |
506 | printed = true; | |
507 | } else | |
508 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
509 | ||
b3c9ac08 ACM |
510 | if (chain->ms.sym) |
511 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); | |
4ecf84d0 ACM |
512 | else |
513 | ret += fprintf(fp, " %p\n", (void *)(long)chain->ip); | |
232a5c94 ACM |
514 | |
515 | if (++entries_printed == callchain_param.print_limit) | |
516 | break; | |
4ecf84d0 ACM |
517 | } |
518 | ||
519 | ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin); | |
520 | ||
521 | return ret; | |
522 | } | |
523 | ||
524 | static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self, | |
525 | u64 total_samples) | |
526 | { | |
527 | struct callchain_list *chain; | |
528 | size_t ret = 0; | |
529 | ||
530 | if (!self) | |
531 | return 0; | |
532 | ||
533 | ret += callchain__fprintf_flat(fp, self->parent, total_samples); | |
534 | ||
535 | ||
536 | list_for_each_entry(chain, &self->val, list) { | |
537 | if (chain->ip >= PERF_CONTEXT_MAX) | |
538 | continue; | |
b3c9ac08 ACM |
539 | if (chain->ms.sym) |
540 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); | |
4ecf84d0 ACM |
541 | else |
542 | ret += fprintf(fp, " %p\n", | |
543 | (void *)(long)chain->ip); | |
544 | } | |
545 | ||
546 | return ret; | |
547 | } | |
548 | ||
549 | static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, | |
550 | u64 total_samples, int left_margin) | |
551 | { | |
552 | struct rb_node *rb_node; | |
553 | struct callchain_node *chain; | |
554 | size_t ret = 0; | |
232a5c94 | 555 | u32 entries_printed = 0; |
4ecf84d0 ACM |
556 | |
557 | rb_node = rb_first(&self->sorted_chain); | |
558 | while (rb_node) { | |
559 | double percent; | |
560 | ||
561 | chain = rb_entry(rb_node, struct callchain_node, rb_node); | |
562 | percent = chain->hit * 100.0 / total_samples; | |
563 | switch (callchain_param.mode) { | |
564 | case CHAIN_FLAT: | |
565 | ret += percent_color_fprintf(fp, " %6.2f%%\n", | |
566 | percent); | |
567 | ret += callchain__fprintf_flat(fp, chain, total_samples); | |
568 | break; | |
569 | case CHAIN_GRAPH_ABS: /* Falldown */ | |
570 | case CHAIN_GRAPH_REL: | |
571 | ret += callchain__fprintf_graph(fp, chain, total_samples, | |
572 | left_margin); | |
573 | case CHAIN_NONE: | |
574 | default: | |
575 | break; | |
576 | } | |
577 | ret += fprintf(fp, "\n"); | |
232a5c94 ACM |
578 | if (++entries_printed == callchain_param.print_limit) |
579 | break; | |
4ecf84d0 ACM |
580 | rb_node = rb_next(rb_node); |
581 | } | |
582 | ||
583 | return ret; | |
584 | } | |
585 | ||
1c02c4d2 | 586 | int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size, |
8a6c5b26 ACM |
587 | struct hists *hists, struct hists *pair_hists, |
588 | bool show_displacement, long displacement, | |
589 | bool color, u64 session_total) | |
4ecf84d0 ACM |
590 | { |
591 | struct sort_entry *se; | |
c82ee828 | 592 | u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us; |
c351c281 | 593 | const char *sep = symbol_conf.field_sep; |
a4e3b956 | 594 | int ret; |
4ecf84d0 ACM |
595 | |
596 | if (symbol_conf.exclude_other && !self->parent) | |
597 | return 0; | |
598 | ||
1c02c4d2 | 599 | if (pair_hists) { |
c82ee828 | 600 | period = self->pair ? self->pair->period : 0; |
cee75ac7 | 601 | total = pair_hists->stats.total_period; |
c82ee828 ACM |
602 | period_sys = self->pair ? self->pair->period_sys : 0; |
603 | period_us = self->pair ? self->pair->period_us : 0; | |
604 | period_guest_sys = self->pair ? self->pair->period_guest_sys : 0; | |
605 | period_guest_us = self->pair ? self->pair->period_guest_us : 0; | |
c351c281 | 606 | } else { |
c82ee828 | 607 | period = self->period; |
eefc465c | 608 | total = session_total; |
c82ee828 ACM |
609 | period_sys = self->period_sys; |
610 | period_us = self->period_us; | |
611 | period_guest_sys = self->period_guest_sys; | |
612 | period_guest_us = self->period_guest_us; | |
c351c281 ACM |
613 | } |
614 | ||
a4e3b956 ACM |
615 | if (total) { |
616 | if (color) | |
617 | ret = percent_color_snprintf(s, size, | |
618 | sep ? "%.2f" : " %6.2f%%", | |
c82ee828 | 619 | (period * 100.0) / total); |
a4e3b956 ACM |
620 | else |
621 | ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%", | |
c82ee828 | 622 | (period * 100.0) / total); |
a1645ce1 ZY |
623 | if (symbol_conf.show_cpu_utilization) { |
624 | ret += percent_color_snprintf(s + ret, size - ret, | |
625 | sep ? "%.2f" : " %6.2f%%", | |
c82ee828 | 626 | (period_sys * 100.0) / total); |
a1645ce1 ZY |
627 | ret += percent_color_snprintf(s + ret, size - ret, |
628 | sep ? "%.2f" : " %6.2f%%", | |
c82ee828 | 629 | (period_us * 100.0) / total); |
a1645ce1 ZY |
630 | if (perf_guest) { |
631 | ret += percent_color_snprintf(s + ret, | |
632 | size - ret, | |
633 | sep ? "%.2f" : " %6.2f%%", | |
c82ee828 | 634 | (period_guest_sys * 100.0) / |
a1645ce1 ZY |
635 | total); |
636 | ret += percent_color_snprintf(s + ret, | |
637 | size - ret, | |
638 | sep ? "%.2f" : " %6.2f%%", | |
c82ee828 | 639 | (period_guest_us * 100.0) / |
a1645ce1 ZY |
640 | total); |
641 | } | |
642 | } | |
a4e3b956 | 643 | } else |
9486aa38 | 644 | ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period); |
4ecf84d0 ACM |
645 | |
646 | if (symbol_conf.show_nr_samples) { | |
c351c281 | 647 | if (sep) |
9486aa38 | 648 | ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period); |
4ecf84d0 | 649 | else |
9486aa38 | 650 | ret += snprintf(s + ret, size - ret, "%11" PRIu64, period); |
c351c281 ACM |
651 | } |
652 | ||
1c02c4d2 | 653 | if (pair_hists) { |
c351c281 ACM |
654 | char bf[32]; |
655 | double old_percent = 0, new_percent = 0, diff; | |
656 | ||
657 | if (total > 0) | |
c82ee828 | 658 | old_percent = (period * 100.0) / total; |
eefc465c | 659 | if (session_total > 0) |
c82ee828 | 660 | new_percent = (self->period * 100.0) / session_total; |
c351c281 | 661 | |
9b33827d | 662 | diff = new_percent - old_percent; |
c351c281 | 663 | |
9b33827d | 664 | if (fabs(diff) >= 0.01) |
c351c281 ACM |
665 | snprintf(bf, sizeof(bf), "%+4.2F%%", diff); |
666 | else | |
667 | snprintf(bf, sizeof(bf), " "); | |
668 | ||
669 | if (sep) | |
a4e3b956 | 670 | ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf); |
c351c281 | 671 | else |
a4e3b956 | 672 | ret += snprintf(s + ret, size - ret, "%11.11s", bf); |
c351c281 ACM |
673 | |
674 | if (show_displacement) { | |
675 | if (displacement) | |
676 | snprintf(bf, sizeof(bf), "%+4ld", displacement); | |
677 | else | |
678 | snprintf(bf, sizeof(bf), " "); | |
679 | ||
680 | if (sep) | |
a4e3b956 | 681 | ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf); |
c351c281 | 682 | else |
a4e3b956 | 683 | ret += snprintf(s + ret, size - ret, "%6.6s", bf); |
c351c281 | 684 | } |
4ecf84d0 ACM |
685 | } |
686 | ||
687 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
688 | if (se->elide) | |
689 | continue; | |
690 | ||
a4e3b956 | 691 | ret += snprintf(s + ret, size - ret, "%s", sep ?: " "); |
fcd14984 | 692 | ret += se->se_snprintf(self, s + ret, size - ret, |
8a6c5b26 | 693 | hists__col_len(hists, se->se_width_idx)); |
4ecf84d0 ACM |
694 | } |
695 | ||
a4e3b956 ACM |
696 | return ret; |
697 | } | |
698 | ||
8a6c5b26 ACM |
699 | int hist_entry__fprintf(struct hist_entry *self, struct hists *hists, |
700 | struct hists *pair_hists, bool show_displacement, | |
701 | long displacement, FILE *fp, u64 session_total) | |
a4e3b956 ACM |
702 | { |
703 | char bf[512]; | |
8a6c5b26 | 704 | hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists, |
a4e3b956 ACM |
705 | show_displacement, displacement, |
706 | true, session_total); | |
707 | return fprintf(fp, "%s\n", bf); | |
3997d377 | 708 | } |
4ecf84d0 | 709 | |
8a6c5b26 ACM |
710 | static size_t hist_entry__fprintf_callchain(struct hist_entry *self, |
711 | struct hists *hists, FILE *fp, | |
3997d377 ACM |
712 | u64 session_total) |
713 | { | |
714 | int left_margin = 0; | |
4ecf84d0 | 715 | |
3997d377 ACM |
716 | if (sort__first_dimension == SORT_COMM) { |
717 | struct sort_entry *se = list_first_entry(&hist_entry__sort_list, | |
718 | typeof(*se), list); | |
8a6c5b26 | 719 | left_margin = hists__col_len(hists, se->se_width_idx); |
3997d377 | 720 | left_margin -= thread__comm_len(self->thread); |
4ecf84d0 ACM |
721 | } |
722 | ||
3997d377 ACM |
723 | return hist_entry_callchain__fprintf(fp, self, session_total, |
724 | left_margin); | |
4ecf84d0 ACM |
725 | } |
726 | ||
1c02c4d2 ACM |
727 | size_t hists__fprintf(struct hists *self, struct hists *pair, |
728 | bool show_displacement, FILE *fp) | |
4ecf84d0 | 729 | { |
4ecf84d0 ACM |
730 | struct sort_entry *se; |
731 | struct rb_node *nd; | |
732 | size_t ret = 0; | |
c351c281 ACM |
733 | unsigned long position = 1; |
734 | long displacement = 0; | |
4ecf84d0 | 735 | unsigned int width; |
c351c281 | 736 | const char *sep = symbol_conf.field_sep; |
edb7c60e | 737 | const char *col_width = symbol_conf.col_width_list_str; |
4ecf84d0 ACM |
738 | |
739 | init_rem_hits(); | |
740 | ||
c351c281 ACM |
741 | fprintf(fp, "# %s", pair ? "Baseline" : "Overhead"); |
742 | ||
4ecf84d0 | 743 | if (symbol_conf.show_nr_samples) { |
c351c281 ACM |
744 | if (sep) |
745 | fprintf(fp, "%cSamples", *sep); | |
4ecf84d0 ACM |
746 | else |
747 | fputs(" Samples ", fp); | |
748 | } | |
c351c281 | 749 | |
a1645ce1 ZY |
750 | if (symbol_conf.show_cpu_utilization) { |
751 | if (sep) { | |
752 | ret += fprintf(fp, "%csys", *sep); | |
753 | ret += fprintf(fp, "%cus", *sep); | |
754 | if (perf_guest) { | |
755 | ret += fprintf(fp, "%cguest sys", *sep); | |
756 | ret += fprintf(fp, "%cguest us", *sep); | |
757 | } | |
758 | } else { | |
759 | ret += fprintf(fp, " sys "); | |
760 | ret += fprintf(fp, " us "); | |
761 | if (perf_guest) { | |
762 | ret += fprintf(fp, " guest sys "); | |
763 | ret += fprintf(fp, " guest us "); | |
764 | } | |
765 | } | |
766 | } | |
767 | ||
c351c281 ACM |
768 | if (pair) { |
769 | if (sep) | |
770 | ret += fprintf(fp, "%cDelta", *sep); | |
771 | else | |
772 | ret += fprintf(fp, " Delta "); | |
773 | ||
774 | if (show_displacement) { | |
775 | if (sep) | |
776 | ret += fprintf(fp, "%cDisplacement", *sep); | |
777 | else | |
778 | ret += fprintf(fp, " Displ"); | |
779 | } | |
780 | } | |
781 | ||
4ecf84d0 ACM |
782 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
783 | if (se->elide) | |
784 | continue; | |
c351c281 | 785 | if (sep) { |
fcd14984 | 786 | fprintf(fp, "%c%s", *sep, se->se_header); |
4ecf84d0 ACM |
787 | continue; |
788 | } | |
fcd14984 | 789 | width = strlen(se->se_header); |
8a6c5b26 ACM |
790 | if (symbol_conf.col_width_list_str) { |
791 | if (col_width) { | |
792 | hists__set_col_len(self, se->se_width_idx, | |
793 | atoi(col_width)); | |
794 | col_width = strchr(col_width, ','); | |
795 | if (col_width) | |
796 | ++col_width; | |
4ecf84d0 | 797 | } |
4ecf84d0 | 798 | } |
8a6c5b26 ACM |
799 | if (!hists__new_col_len(self, se->se_width_idx, width)) |
800 | width = hists__col_len(self, se->se_width_idx); | |
fcd14984 | 801 | fprintf(fp, " %*s", width, se->se_header); |
4ecf84d0 ACM |
802 | } |
803 | fprintf(fp, "\n"); | |
804 | ||
c351c281 | 805 | if (sep) |
4ecf84d0 ACM |
806 | goto print_entries; |
807 | ||
808 | fprintf(fp, "# ........"); | |
809 | if (symbol_conf.show_nr_samples) | |
810 | fprintf(fp, " .........."); | |
c351c281 ACM |
811 | if (pair) { |
812 | fprintf(fp, " .........."); | |
813 | if (show_displacement) | |
814 | fprintf(fp, " ....."); | |
815 | } | |
4ecf84d0 ACM |
816 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
817 | unsigned int i; | |
818 | ||
819 | if (se->elide) | |
820 | continue; | |
821 | ||
822 | fprintf(fp, " "); | |
8a6c5b26 ACM |
823 | width = hists__col_len(self, se->se_width_idx); |
824 | if (width == 0) | |
fcd14984 | 825 | width = strlen(se->se_header); |
4ecf84d0 ACM |
826 | for (i = 0; i < width; i++) |
827 | fprintf(fp, "."); | |
828 | } | |
4ecf84d0 | 829 | |
c351c281 | 830 | fprintf(fp, "\n#\n"); |
4ecf84d0 ACM |
831 | |
832 | print_entries: | |
1c02c4d2 | 833 | for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) { |
c351c281 ACM |
834 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
835 | ||
836 | if (show_displacement) { | |
837 | if (h->pair != NULL) | |
838 | displacement = ((long)h->pair->position - | |
839 | (long)position); | |
840 | else | |
841 | displacement = 0; | |
842 | ++position; | |
843 | } | |
8a6c5b26 | 844 | ret += hist_entry__fprintf(h, self, pair, show_displacement, |
cee75ac7 | 845 | displacement, fp, self->stats.total_period); |
3997d377 ACM |
846 | |
847 | if (symbol_conf.use_callchain) | |
8a6c5b26 ACM |
848 | ret += hist_entry__fprintf_callchain(h, self, fp, |
849 | self->stats.total_period); | |
59fd5306 | 850 | if (h->ms.map == NULL && verbose > 1) { |
65f2ed2b | 851 | __map_groups__fprintf_maps(&h->thread->mg, |
c6e718ff | 852 | MAP__FUNCTION, verbose, fp); |
65f2ed2b ACM |
853 | fprintf(fp, "%.10s end\n", graph_dotted_line); |
854 | } | |
4ecf84d0 ACM |
855 | } |
856 | ||
4ecf84d0 ACM |
857 | free(rem_sq_bracket); |
858 | ||
859 | return ret; | |
860 | } | |
b09e0190 | 861 | |
06daaaba ACM |
862 | /* |
863 | * See hists__fprintf to match the column widths | |
864 | */ | |
865 | unsigned int hists__sort_list_width(struct hists *self) | |
866 | { | |
867 | struct sort_entry *se; | |
868 | int ret = 9; /* total % */ | |
869 | ||
870 | if (symbol_conf.show_cpu_utilization) { | |
871 | ret += 7; /* count_sys % */ | |
872 | ret += 6; /* count_us % */ | |
873 | if (perf_guest) { | |
874 | ret += 13; /* count_guest_sys % */ | |
875 | ret += 12; /* count_guest_us % */ | |
876 | } | |
877 | } | |
878 | ||
879 | if (symbol_conf.show_nr_samples) | |
880 | ret += 11; | |
881 | ||
882 | list_for_each_entry(se, &hist_entry__sort_list, list) | |
883 | if (!se->elide) | |
884 | ret += 2 + hists__col_len(self, se->se_width_idx); | |
885 | ||
903cce6e ACM |
886 | if (verbose) /* Addr + origin */ |
887 | ret += 3 + BITS_PER_LONG / 4; | |
888 | ||
06daaaba ACM |
889 | return ret; |
890 | } | |
891 | ||
cc5edb0e ACM |
892 | static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h, |
893 | enum hist_filter filter) | |
894 | { | |
895 | h->filtered &= ~(1 << filter); | |
896 | if (h->filtered) | |
897 | return; | |
898 | ||
899 | ++self->nr_entries; | |
0f0cbf7a ACM |
900 | if (h->ms.unfolded) |
901 | self->nr_entries += h->nr_rows; | |
902 | h->row_offset = 0; | |
cc5edb0e ACM |
903 | self->stats.total_period += h->period; |
904 | self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events; | |
905 | ||
8a6c5b26 | 906 | hists__calc_col_len(self, h); |
cc5edb0e ACM |
907 | } |
908 | ||
b09e0190 ACM |
909 | void hists__filter_by_dso(struct hists *self, const struct dso *dso) |
910 | { | |
911 | struct rb_node *nd; | |
912 | ||
cee75ac7 | 913 | self->nr_entries = self->stats.total_period = 0; |
c82ee828 | 914 | self->stats.nr_events[PERF_RECORD_SAMPLE] = 0; |
8a6c5b26 | 915 | hists__reset_col_len(self); |
b09e0190 ACM |
916 | |
917 | for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) { | |
918 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
919 | ||
920 | if (symbol_conf.exclude_other && !h->parent) | |
921 | continue; | |
922 | ||
923 | if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) { | |
924 | h->filtered |= (1 << HIST_FILTER__DSO); | |
925 | continue; | |
926 | } | |
927 | ||
cc5edb0e | 928 | hists__remove_entry_filter(self, h, HIST_FILTER__DSO); |
b09e0190 ACM |
929 | } |
930 | } | |
931 | ||
932 | void hists__filter_by_thread(struct hists *self, const struct thread *thread) | |
933 | { | |
934 | struct rb_node *nd; | |
935 | ||
cee75ac7 | 936 | self->nr_entries = self->stats.total_period = 0; |
c82ee828 | 937 | self->stats.nr_events[PERF_RECORD_SAMPLE] = 0; |
8a6c5b26 | 938 | hists__reset_col_len(self); |
b09e0190 ACM |
939 | |
940 | for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) { | |
941 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
942 | ||
943 | if (thread != NULL && h->thread != thread) { | |
944 | h->filtered |= (1 << HIST_FILTER__THREAD); | |
945 | continue; | |
946 | } | |
cc5edb0e ACM |
947 | |
948 | hists__remove_entry_filter(self, h, HIST_FILTER__THREAD); | |
b09e0190 ACM |
949 | } |
950 | } | |
ef7b93a1 ACM |
951 | |
952 | static int symbol__alloc_hist(struct symbol *self) | |
953 | { | |
954 | struct sym_priv *priv = symbol__priv(self); | |
955 | const int size = (sizeof(*priv->hist) + | |
956 | (self->end - self->start) * sizeof(u64)); | |
957 | ||
958 | priv->hist = zalloc(size); | |
959 | return priv->hist == NULL ? -1 : 0; | |
960 | } | |
961 | ||
962 | int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip) | |
963 | { | |
964 | unsigned int sym_size, offset; | |
965 | struct symbol *sym = self->ms.sym; | |
966 | struct sym_priv *priv; | |
967 | struct sym_hist *h; | |
968 | ||
969 | if (!sym || !self->ms.map) | |
970 | return 0; | |
971 | ||
972 | priv = symbol__priv(sym); | |
973 | if (priv->hist == NULL && symbol__alloc_hist(sym) < 0) | |
974 | return -ENOMEM; | |
975 | ||
976 | sym_size = sym->end - sym->start; | |
977 | offset = ip - sym->start; | |
978 | ||
9486aa38 | 979 | pr_debug3("%s: ip=%#" PRIx64 "\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip)); |
ef7b93a1 ACM |
980 | |
981 | if (offset >= sym_size) | |
982 | return 0; | |
983 | ||
984 | h = priv->hist; | |
985 | h->sum++; | |
986 | h->ip[offset]++; | |
987 | ||
9486aa38 ACM |
988 | pr_debug3("%#" PRIx64 " %s: period++ [ip: %#" PRIx64 ", %#" PRIx64 |
989 | "] => %" PRIu64 "\n", self->ms.sym->start, self->ms.sym->name, | |
990 | ip, ip - self->ms.sym->start, h->ip[offset]); | |
ef7b93a1 ACM |
991 | return 0; |
992 | } | |
993 | ||
92221162 | 994 | static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize) |
ef7b93a1 | 995 | { |
92221162 | 996 | struct objdump_line *self = malloc(sizeof(*self) + privsize); |
ef7b93a1 ACM |
997 | |
998 | if (self != NULL) { | |
999 | self->offset = offset; | |
1000 | self->line = line; | |
1001 | } | |
1002 | ||
1003 | return self; | |
1004 | } | |
1005 | ||
1006 | void objdump_line__free(struct objdump_line *self) | |
1007 | { | |
1008 | free(self->line); | |
1009 | free(self); | |
1010 | } | |
1011 | ||
1012 | static void objdump__add_line(struct list_head *head, struct objdump_line *line) | |
1013 | { | |
1014 | list_add_tail(&line->node, head); | |
1015 | } | |
1016 | ||
1017 | struct objdump_line *objdump__get_next_ip_line(struct list_head *head, | |
1018 | struct objdump_line *pos) | |
1019 | { | |
1020 | list_for_each_entry_continue(pos, head, node) | |
1021 | if (pos->offset >= 0) | |
1022 | return pos; | |
1023 | ||
1024 | return NULL; | |
1025 | } | |
1026 | ||
1027 | static int hist_entry__parse_objdump_line(struct hist_entry *self, FILE *file, | |
92221162 | 1028 | struct list_head *head, size_t privsize) |
ef7b93a1 ACM |
1029 | { |
1030 | struct symbol *sym = self->ms.sym; | |
1031 | struct objdump_line *objdump_line; | |
1032 | char *line = NULL, *tmp, *tmp2, *c; | |
1033 | size_t line_len; | |
1034 | s64 line_ip, offset = -1; | |
1035 | ||
1036 | if (getline(&line, &line_len, file) < 0) | |
1037 | return -1; | |
1038 | ||
1039 | if (!line) | |
1040 | return -1; | |
1041 | ||
1042 | while (line_len != 0 && isspace(line[line_len - 1])) | |
1043 | line[--line_len] = '\0'; | |
1044 | ||
1045 | c = strchr(line, '\n'); | |
1046 | if (c) | |
1047 | *c = 0; | |
1048 | ||
1049 | line_ip = -1; | |
1050 | ||
1051 | /* | |
1052 | * Strip leading spaces: | |
1053 | */ | |
1054 | tmp = line; | |
1055 | while (*tmp) { | |
1056 | if (*tmp != ' ') | |
1057 | break; | |
1058 | tmp++; | |
1059 | } | |
1060 | ||
1061 | if (*tmp) { | |
1062 | /* | |
1063 | * Parse hexa addresses followed by ':' | |
1064 | */ | |
1065 | line_ip = strtoull(tmp, &tmp2, 16); | |
70a7cb3b | 1066 | if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') |
ef7b93a1 ACM |
1067 | line_ip = -1; |
1068 | } | |
1069 | ||
1070 | if (line_ip != -1) { | |
70a7cb3b ACM |
1071 | u64 start = map__rip_2objdump(self->ms.map, sym->start), |
1072 | end = map__rip_2objdump(self->ms.map, sym->end); | |
1073 | ||
ef7b93a1 | 1074 | offset = line_ip - start; |
70a7cb3b ACM |
1075 | if (offset < 0 || (u64)line_ip > end) |
1076 | offset = -1; | |
ef7b93a1 ACM |
1077 | } |
1078 | ||
92221162 | 1079 | objdump_line = objdump_line__new(offset, line, privsize); |
ef7b93a1 ACM |
1080 | if (objdump_line == NULL) { |
1081 | free(line); | |
1082 | return -1; | |
1083 | } | |
1084 | objdump__add_line(head, objdump_line); | |
1085 | ||
1086 | return 0; | |
1087 | } | |
1088 | ||
92221162 ACM |
1089 | int hist_entry__annotate(struct hist_entry *self, struct list_head *head, |
1090 | size_t privsize) | |
ef7b93a1 ACM |
1091 | { |
1092 | struct symbol *sym = self->ms.sym; | |
1093 | struct map *map = self->ms.map; | |
1094 | struct dso *dso = map->dso; | |
b36f19d5 | 1095 | char *filename = dso__build_id_filename(dso, NULL, 0); |
44bf4606 | 1096 | bool free_filename = true; |
ef7b93a1 ACM |
1097 | char command[PATH_MAX * 2]; |
1098 | FILE *file; | |
46e3e055 | 1099 | int err = 0; |
ef7b93a1 | 1100 | u64 len; |
ec5761ea DA |
1101 | char symfs_filename[PATH_MAX]; |
1102 | ||
1103 | if (filename) { | |
1104 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", | |
1105 | symbol_conf.symfs, filename); | |
1106 | } | |
ef7b93a1 | 1107 | |
b36f19d5 ACM |
1108 | if (filename == NULL) { |
1109 | if (dso->has_build_id) { | |
1110 | pr_err("Can't annotate %s: not enough memory\n", | |
1111 | sym->name); | |
46e3e055 | 1112 | return -ENOMEM; |
b36f19d5 | 1113 | } |
44bf4606 | 1114 | goto fallback; |
ec5761ea | 1115 | } else if (readlink(symfs_filename, command, sizeof(command)) < 0 || |
44bf4606 | 1116 | strstr(command, "[kernel.kallsyms]") || |
ec5761ea | 1117 | access(symfs_filename, R_OK)) { |
44bf4606 ACM |
1118 | free(filename); |
1119 | fallback: | |
b36f19d5 | 1120 | /* |
44bf4606 ACM |
1121 | * If we don't have build-ids or the build-id file isn't in the |
1122 | * cache, or is just a kallsyms file, well, lets hope that this | |
b36f19d5 ACM |
1123 | * DSO is the same as when 'perf record' ran. |
1124 | */ | |
1125 | filename = dso->long_name; | |
ec5761ea DA |
1126 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", |
1127 | symbol_conf.symfs, filename); | |
44bf4606 | 1128 | free_filename = false; |
b36f19d5 | 1129 | } |
ef7b93a1 ACM |
1130 | |
1131 | if (dso->origin == DSO__ORIG_KERNEL) { | |
46e3e055 | 1132 | if (dso->annotate_warned) |
b36f19d5 | 1133 | goto out_free_filename; |
46e3e055 | 1134 | err = -ENOENT; |
ef7b93a1 ACM |
1135 | dso->annotate_warned = 1; |
1136 | pr_err("Can't annotate %s: No vmlinux file was found in the " | |
46e3e055 | 1137 | "path\n", sym->name); |
b36f19d5 | 1138 | goto out_free_filename; |
ef7b93a1 ACM |
1139 | } |
1140 | ||
9486aa38 | 1141 | pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, |
ef7b93a1 ACM |
1142 | filename, sym->name, map->unmap_ip(map, sym->start), |
1143 | map->unmap_ip(map, sym->end)); | |
1144 | ||
1145 | len = sym->end - sym->start; | |
1146 | ||
1147 | pr_debug("annotating [%p] %30s : [%p] %30s\n", | |
1148 | dso, dso->long_name, sym, sym->name); | |
1149 | ||
1150 | snprintf(command, sizeof(command), | |
9486aa38 | 1151 | "objdump --start-address=0x%016" PRIx64 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand", |
ef7b93a1 ACM |
1152 | map__rip_2objdump(map, sym->start), |
1153 | map__rip_2objdump(map, sym->end), | |
ec5761ea | 1154 | symfs_filename, filename); |
ef7b93a1 ACM |
1155 | |
1156 | pr_debug("Executing: %s\n", command); | |
1157 | ||
1158 | file = popen(command, "r"); | |
1159 | if (!file) | |
b36f19d5 | 1160 | goto out_free_filename; |
ef7b93a1 ACM |
1161 | |
1162 | while (!feof(file)) | |
92221162 | 1163 | if (hist_entry__parse_objdump_line(self, file, head, privsize) < 0) |
ef7b93a1 ACM |
1164 | break; |
1165 | ||
1166 | pclose(file); | |
b36f19d5 | 1167 | out_free_filename: |
44bf4606 | 1168 | if (free_filename) |
b36f19d5 ACM |
1169 | free(filename); |
1170 | return err; | |
ef7b93a1 | 1171 | } |
c8446b9b ACM |
1172 | |
1173 | void hists__inc_nr_events(struct hists *self, u32 type) | |
1174 | { | |
cee75ac7 ACM |
1175 | ++self->stats.nr_events[0]; |
1176 | ++self->stats.nr_events[type]; | |
c8446b9b ACM |
1177 | } |
1178 | ||
1179 | size_t hists__fprintf_nr_events(struct hists *self, FILE *fp) | |
1180 | { | |
1181 | int i; | |
1182 | size_t ret = 0; | |
1183 | ||
1184 | for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) { | |
8115d60c | 1185 | const char *name = perf_event__name(i); |
3835bc00 TG |
1186 | |
1187 | if (!strcmp(name, "UNKNOWN")) | |
c8446b9b | 1188 | continue; |
3835bc00 TG |
1189 | |
1190 | ret += fprintf(fp, "%16s events: %10d\n", name, | |
1191 | self->stats.nr_events[i]); | |
c8446b9b ACM |
1192 | } |
1193 | ||
1194 | return ret; | |
1195 | } |