]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - tools/perf/util/hist.c
perf callchain: Rename cumul_hits into callchain_cumul_hits
[linux-3.10.git] / tools / perf / util / hist.c
1 #include "util.h"
2 #include "build-id.h"
3 #include "hist.h"
4 #include "session.h"
5 #include "sort.h"
6 #include <math.h>
7
8 enum hist_filter {
9         HIST_FILTER__DSO,
10         HIST_FILTER__THREAD,
11         HIST_FILTER__PARENT,
12 };
13
14 struct callchain_param  callchain_param = {
15         .mode   = CHAIN_GRAPH_REL,
16         .min_percent = 0.5
17 };
18
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
63 static void hist_entry__add_cpumode_period(struct hist_entry *self,
64                                            unsigned int cpumode, u64 period)
65 {
66         switch (cpumode) {
67         case PERF_RECORD_MISC_KERNEL:
68                 self->period_sys += period;
69                 break;
70         case PERF_RECORD_MISC_USER:
71                 self->period_us += period;
72                 break;
73         case PERF_RECORD_MISC_GUEST_KERNEL:
74                 self->period_guest_sys += period;
75                 break;
76         case PERF_RECORD_MISC_GUEST_USER:
77                 self->period_guest_us += period;
78                 break;
79         default:
80                 break;
81         }
82 }
83
84 /*
85  * histogram, sorted on item, collects periods
86  */
87
88 static struct hist_entry *hist_entry__new(struct hist_entry *template)
89 {
90         size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
91         struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
92
93         if (self != NULL) {
94                 *self = *template;
95                 self->nr_events = 1;
96                 if (self->ms.map)
97                         self->ms.map->referenced = true;
98                 if (symbol_conf.use_callchain)
99                         callchain_init(self->callchain);
100         }
101
102         return self;
103 }
104
105 static void hists__inc_nr_entries(struct hists *self, struct hist_entry *h)
106 {
107         if (!h->filtered) {
108                 hists__calc_col_len(self, h);
109                 ++self->nr_entries;
110         }
111 }
112
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
120 struct hist_entry *__hists__add_entry(struct hists *self,
121                                       struct addr_location *al,
122                                       struct symbol *sym_parent, u64 period)
123 {
124         struct rb_node **p = &self->entries.rb_node;
125         struct rb_node *parent = NULL;
126         struct hist_entry *he;
127         struct hist_entry entry = {
128                 .thread = al->thread,
129                 .ms = {
130                         .map    = al->map,
131                         .sym    = al->sym,
132                 },
133                 .cpu    = al->cpu,
134                 .ip     = al->addr,
135                 .level  = al->level,
136                 .period = period,
137                 .parent = sym_parent,
138                 .filtered = symbol__parent_filter(sym_parent),
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) {
149                         he->period += period;
150                         ++he->nr_events;
151                         goto out;
152                 }
153
154                 if (cmp < 0)
155                         p = &(*p)->rb_left;
156                 else
157                         p = &(*p)->rb_right;
158         }
159
160         he = hist_entry__new(&entry);
161         if (!he)
162                 return NULL;
163         rb_link_node(&he->rb_node, parent, p);
164         rb_insert_color(&he->rb_node, &self->entries);
165         hists__inc_nr_entries(self, he);
166 out:
167         hist_entry__add_cpumode_period(he, al->cpumode, period);
168         return he;
169 }
170
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) {
178                 cmp = se->se_cmp(left, right);
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
195                 f = se->se_collapse ?: se->se_cmp;
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
214 static bool hists__collapse_insert_entry(struct hists *self,
215                                          struct rb_root *root,
216                                          struct hist_entry *he)
217 {
218         struct rb_node **p = &root->rb_node;
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) {
230                         iter->period += he->period;
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                         }
236                         hist_entry__free(he);
237                         return false;
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);
247         rb_insert_color(&he->rb_node, root);
248         return true;
249 }
250
251 void hists__collapse_resort(struct hists *self)
252 {
253         struct rb_root tmp;
254         struct rb_node *next;
255         struct hist_entry *n;
256
257         if (!sort__need_collapse)
258                 return;
259
260         tmp = RB_ROOT;
261         next = rb_first(&self->entries);
262         self->nr_entries = 0;
263         hists__reset_col_len(self);
264
265         while (next) {
266                 n = rb_entry(next, struct hist_entry, rb_node);
267                 next = rb_next(&n->rb_node);
268
269                 rb_erase(&n->rb_node, &self->entries);
270                 if (hists__collapse_insert_entry(self, &tmp, n))
271                         hists__inc_nr_entries(self, n);
272         }
273
274         self->entries = tmp;
275 }
276
277 /*
278  * reverse the map, sort on period.
279  */
280
281 static void __hists__insert_output_entry(struct rb_root *entries,
282                                          struct hist_entry *he,
283                                          u64 min_callchain_hits)
284 {
285         struct rb_node **p = &entries->rb_node;
286         struct rb_node *parent = NULL;
287         struct hist_entry *iter;
288
289         if (symbol_conf.use_callchain)
290                 callchain_param.sort(&he->sorted_chain, he->callchain,
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
297                 if (he->period > iter->period)
298                         p = &(*p)->rb_left;
299                 else
300                         p = &(*p)->rb_right;
301         }
302
303         rb_link_node(&he->rb_node, parent, p);
304         rb_insert_color(&he->rb_node, entries);
305 }
306
307 void hists__output_resort(struct hists *self)
308 {
309         struct rb_root tmp;
310         struct rb_node *next;
311         struct hist_entry *n;
312         u64 min_callchain_hits;
313
314         min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
315
316         tmp = RB_ROOT;
317         next = rb_first(&self->entries);
318
319         self->nr_entries = 0;
320         hists__reset_col_len(self);
321
322         while (next) {
323                 n = rb_entry(next, struct hist_entry, rb_node);
324                 next = rb_next(&n->rb_node);
325
326                 rb_erase(&n->rb_node, &self->entries);
327                 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
328                 hists__inc_nr_entries(self, n);
329         }
330
331         self->entries = tmp;
332 }
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,
363                                      int depth, int depth_mask, int period,
364                                      u64 total_samples, u64 hits,
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, " ");
376                 if (!period && i == depth - 1) {
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         }
384         if (chain->ms.sym)
385                 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
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, "[...]");
404         rem_hits.ms.sym = rem_sq_bracket;
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;
419         uint entries_printed = 0;
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);
433                 cumul = callchain_cumul_hits(child);
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                 /*
448                  * But we keep the older depth mask for the line separator
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) {
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;
466                 if (++entries_printed == callchain_param.print_limit)
467                         break;
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;
493         u32 entries_printed = 0;
494
495         list_for_each_entry(chain, &self->val, list) {
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
510                 if (chain->ms.sym)
511                         ret += fprintf(fp, " %s\n", chain->ms.sym->name);
512                 else
513                         ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
514
515                 if (++entries_printed == callchain_param.print_limit)
516                         break;
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;
539                 if (chain->ms.sym)
540                         ret += fprintf(fp, "                %s\n", chain->ms.sym->name);
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;
555         u32 entries_printed = 0;
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");
578                 if (++entries_printed == callchain_param.print_limit)
579                         break;
580                 rb_node = rb_next(rb_node);
581         }
582
583         return ret;
584 }
585
586 int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
587                          struct hists *hists, struct hists *pair_hists,
588                          bool show_displacement, long displacement,
589                          bool color, u64 session_total)
590 {
591         struct sort_entry *se;
592         u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
593         const char *sep = symbol_conf.field_sep;
594         int ret;
595
596         if (symbol_conf.exclude_other && !self->parent)
597                 return 0;
598
599         if (pair_hists) {
600                 period = self->pair ? self->pair->period : 0;
601                 total = pair_hists->stats.total_period;
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;
606         } else {
607                 period = self->period;
608                 total = session_total;
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;
613         }
614
615         if (total) {
616                 if (color)
617                         ret = percent_color_snprintf(s, size,
618                                                      sep ? "%.2f" : "   %6.2f%%",
619                                                      (period * 100.0) / total);
620                 else
621                         ret = snprintf(s, size, sep ? "%.2f" : "   %6.2f%%",
622                                        (period * 100.0) / total);
623                 if (symbol_conf.show_cpu_utilization) {
624                         ret += percent_color_snprintf(s + ret, size - ret,
625                                         sep ? "%.2f" : "   %6.2f%%",
626                                         (period_sys * 100.0) / total);
627                         ret += percent_color_snprintf(s + ret, size - ret,
628                                         sep ? "%.2f" : "   %6.2f%%",
629                                         (period_us * 100.0) / total);
630                         if (perf_guest) {
631                                 ret += percent_color_snprintf(s + ret,
632                                                 size - ret,
633                                                 sep ? "%.2f" : "   %6.2f%%",
634                                                 (period_guest_sys * 100.0) /
635                                                                 total);
636                                 ret += percent_color_snprintf(s + ret,
637                                                 size - ret,
638                                                 sep ? "%.2f" : "   %6.2f%%",
639                                                 (period_guest_us * 100.0) /
640                                                                 total);
641                         }
642                 }
643         } else
644                 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
645
646         if (symbol_conf.show_nr_samples) {
647                 if (sep)
648                         ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
649                 else
650                         ret += snprintf(s + ret, size - ret, "%11" PRIu64, period);
651         }
652
653         if (pair_hists) {
654                 char bf[32];
655                 double old_percent = 0, new_percent = 0, diff;
656
657                 if (total > 0)
658                         old_percent = (period * 100.0) / total;
659                 if (session_total > 0)
660                         new_percent = (self->period * 100.0) / session_total;
661
662                 diff = new_percent - old_percent;
663
664                 if (fabs(diff) >= 0.01)
665                         snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
666                 else
667                         snprintf(bf, sizeof(bf), " ");
668
669                 if (sep)
670                         ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
671                 else
672                         ret += snprintf(s + ret, size - ret, "%11.11s", bf);
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)
681                                 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
682                         else
683                                 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
684                 }
685         }
686
687         list_for_each_entry(se, &hist_entry__sort_list, list) {
688                 if (se->elide)
689                         continue;
690
691                 ret += snprintf(s + ret, size - ret, "%s", sep ?: "  ");
692                 ret += se->se_snprintf(self, s + ret, size - ret,
693                                        hists__col_len(hists, se->se_width_idx));
694         }
695
696         return ret;
697 }
698
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)
702 {
703         char bf[512];
704         hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
705                              show_displacement, displacement,
706                              true, session_total);
707         return fprintf(fp, "%s\n", bf);
708 }
709
710 static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
711                                             struct hists *hists, FILE *fp,
712                                             u64 session_total)
713 {
714         int left_margin = 0;
715
716         if (sort__first_dimension == SORT_COMM) {
717                 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
718                                                          typeof(*se), list);
719                 left_margin = hists__col_len(hists, se->se_width_idx);
720                 left_margin -= thread__comm_len(self->thread);
721         }
722
723         return hist_entry_callchain__fprintf(fp, self, session_total,
724                                              left_margin);
725 }
726
727 size_t hists__fprintf(struct hists *self, struct hists *pair,
728                       bool show_displacement, FILE *fp)
729 {
730         struct sort_entry *se;
731         struct rb_node *nd;
732         size_t ret = 0;
733         unsigned long position = 1;
734         long displacement = 0;
735         unsigned int width;
736         const char *sep = symbol_conf.field_sep;
737         const char *col_width = symbol_conf.col_width_list_str;
738
739         init_rem_hits();
740
741         fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
742
743         if (symbol_conf.show_nr_samples) {
744                 if (sep)
745                         fprintf(fp, "%cSamples", *sep);
746                 else
747                         fputs("  Samples  ", fp);
748         }
749
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
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
782         list_for_each_entry(se, &hist_entry__sort_list, list) {
783                 if (se->elide)
784                         continue;
785                 if (sep) {
786                         fprintf(fp, "%c%s", *sep, se->se_header);
787                         continue;
788                 }
789                 width = strlen(se->se_header);
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;
797                         }
798                 }
799                 if (!hists__new_col_len(self, se->se_width_idx, width))
800                         width = hists__col_len(self, se->se_width_idx);
801                 fprintf(fp, "  %*s", width, se->se_header);
802         }
803         fprintf(fp, "\n");
804
805         if (sep)
806                 goto print_entries;
807
808         fprintf(fp, "# ........");
809         if (symbol_conf.show_nr_samples)
810                 fprintf(fp, " ..........");
811         if (pair) {
812                 fprintf(fp, " ..........");
813                 if (show_displacement)
814                         fprintf(fp, " .....");
815         }
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, "  ");
823                 width = hists__col_len(self, se->se_width_idx);
824                 if (width == 0)
825                         width = strlen(se->se_header);
826                 for (i = 0; i < width; i++)
827                         fprintf(fp, ".");
828         }
829
830         fprintf(fp, "\n#\n");
831
832 print_entries:
833         for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
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                 }
844                 ret += hist_entry__fprintf(h, self, pair, show_displacement,
845                                            displacement, fp, self->stats.total_period);
846
847                 if (symbol_conf.use_callchain)
848                         ret += hist_entry__fprintf_callchain(h, self, fp,
849                                                              self->stats.total_period);
850                 if (h->ms.map == NULL && verbose > 1) {
851                         __map_groups__fprintf_maps(&h->thread->mg,
852                                                    MAP__FUNCTION, verbose, fp);
853                         fprintf(fp, "%.10s end\n", graph_dotted_line);
854                 }
855         }
856
857         free(rem_sq_bracket);
858
859         return ret;
860 }
861
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
886         if (verbose) /* Addr + origin */
887                 ret += 3 + BITS_PER_LONG / 4;
888
889         return ret;
890 }
891
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;
900         if (h->ms.unfolded)
901                 self->nr_entries += h->nr_rows;
902         h->row_offset = 0;
903         self->stats.total_period += h->period;
904         self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
905
906         hists__calc_col_len(self, h);
907 }
908
909 void hists__filter_by_dso(struct hists *self, const struct dso *dso)
910 {
911         struct rb_node *nd;
912
913         self->nr_entries = self->stats.total_period = 0;
914         self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
915         hists__reset_col_len(self);
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
928                 hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
929         }
930 }
931
932 void hists__filter_by_thread(struct hists *self, const struct thread *thread)
933 {
934         struct rb_node *nd;
935
936         self->nr_entries = self->stats.total_period = 0;
937         self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
938         hists__reset_col_len(self);
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                 }
947
948                 hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
949         }
950 }
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
979         pr_debug3("%s: ip=%#" PRIx64 "\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip));
980
981         if (offset >= sym_size)
982                 return 0;
983
984         h = priv->hist;
985         h->sum++;
986         h->ip[offset]++;
987
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]);
991         return 0;
992 }
993
994 static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
995 {
996         struct objdump_line *self = malloc(sizeof(*self) + privsize);
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,
1028                                           struct list_head *head, size_t privsize)
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);
1066                 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1067                         line_ip = -1;
1068         }
1069
1070         if (line_ip != -1) {
1071                 u64 start = map__rip_2objdump(self->ms.map, sym->start),
1072                     end = map__rip_2objdump(self->ms.map, sym->end);
1073
1074                 offset = line_ip - start;
1075                 if (offset < 0 || (u64)line_ip > end)
1076                         offset = -1;
1077         }
1078
1079         objdump_line = objdump_line__new(offset, line, privsize);
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
1089 int hist_entry__annotate(struct hist_entry *self, struct list_head *head,
1090                          size_t privsize)
1091 {
1092         struct symbol *sym = self->ms.sym;
1093         struct map *map = self->ms.map;
1094         struct dso *dso = map->dso;
1095         char *filename = dso__build_id_filename(dso, NULL, 0);
1096         bool free_filename = true;
1097         char command[PATH_MAX * 2];
1098         FILE *file;
1099         int err = 0;
1100         u64 len;
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         }
1107
1108         if (filename == NULL) {
1109                 if (dso->has_build_id) {
1110                         pr_err("Can't annotate %s: not enough memory\n",
1111                                sym->name);
1112                         return -ENOMEM;
1113                 }
1114                 goto fallback;
1115         } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
1116                    strstr(command, "[kernel.kallsyms]") ||
1117                    access(symfs_filename, R_OK)) {
1118                 free(filename);
1119 fallback:
1120                 /*
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
1123                  * DSO is the same as when 'perf record' ran.
1124                  */
1125                 filename = dso->long_name;
1126                 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
1127                          symbol_conf.symfs, filename);
1128                 free_filename = false;
1129         }
1130
1131         if (dso->origin == DSO__ORIG_KERNEL) {
1132                 if (dso->annotate_warned)
1133                         goto out_free_filename;
1134                 err = -ENOENT;
1135                 dso->annotate_warned = 1;
1136                 pr_err("Can't annotate %s: No vmlinux file was found in the "
1137                        "path\n", sym->name);
1138                 goto out_free_filename;
1139         }
1140
1141         pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
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),
1151                  "objdump --start-address=0x%016" PRIx64 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
1152                  map__rip_2objdump(map, sym->start),
1153                  map__rip_2objdump(map, sym->end),
1154                  symfs_filename, filename);
1155
1156         pr_debug("Executing: %s\n", command);
1157
1158         file = popen(command, "r");
1159         if (!file)
1160                 goto out_free_filename;
1161
1162         while (!feof(file))
1163                 if (hist_entry__parse_objdump_line(self, file, head, privsize) < 0)
1164                         break;
1165
1166         pclose(file);
1167 out_free_filename:
1168         if (free_filename)
1169                 free(filename);
1170         return err;
1171 }
1172
1173 void hists__inc_nr_events(struct hists *self, u32 type)
1174 {
1175         ++self->stats.nr_events[0];
1176         ++self->stats.nr_events[type];
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) {
1185                 const char *name = event__get_event_name(i);
1186
1187                 if (!strcmp(name, "UNKNOWN"))
1188                         continue;
1189
1190                 ret += fprintf(fp, "%16s events: %10d\n", name,
1191                                self->stats.nr_events[i]);
1192         }
1193
1194         return ret;
1195 }