5fb19013ca0c21b3970c93b49a79565dc029a200
[linux-flexiantxendom0-3.2.10.git] / tools / perf / util / hist.c
1 #include "annotate.h"
2 #include "util.h"
3 #include "build-id.h"
4 #include "hist.h"
5 #include "session.h"
6 #include "sort.h"
7 #include <math.h>
8
9 static bool hists__filter_entry_by_dso(struct hists *hists,
10                                        struct hist_entry *he);
11 static bool hists__filter_entry_by_thread(struct hists *hists,
12                                           struct hist_entry *he);
13 static bool hists__filter_entry_by_symbol(struct hists *hists,
14                                           struct hist_entry *he);
15
16 enum hist_filter {
17         HIST_FILTER__DSO,
18         HIST_FILTER__THREAD,
19         HIST_FILTER__PARENT,
20         HIST_FILTER__SYMBOL,
21 };
22
23 struct callchain_param  callchain_param = {
24         .mode   = CHAIN_GRAPH_REL,
25         .min_percent = 0.5,
26         .order  = ORDER_CALLEE
27 };
28
29 u16 hists__col_len(struct hists *hists, enum hist_column col)
30 {
31         return hists->col_len[col];
32 }
33
34 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
35 {
36         hists->col_len[col] = len;
37 }
38
39 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
40 {
41         if (len > hists__col_len(hists, col)) {
42                 hists__set_col_len(hists, col, len);
43                 return true;
44         }
45         return false;
46 }
47
48 static void hists__reset_col_len(struct hists *hists)
49 {
50         enum hist_column col;
51
52         for (col = 0; col < HISTC_NR_COLS; ++col)
53                 hists__set_col_len(hists, col, 0);
54 }
55
56 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
57 {
58         const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
59
60         if (hists__col_len(hists, dso) < unresolved_col_width &&
61             !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
62             !symbol_conf.dso_list)
63                 hists__set_col_len(hists, dso, unresolved_col_width);
64 }
65
66 static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
67 {
68         const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
69         u16 len;
70
71         if (h->ms.sym)
72                 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
73         else
74                 hists__set_unres_dso_col_len(hists, HISTC_DSO);
75
76         len = thread__comm_len(h->thread);
77         if (hists__new_col_len(hists, HISTC_COMM, len))
78                 hists__set_col_len(hists, HISTC_THREAD, len + 6);
79
80         if (h->ms.map) {
81                 len = dso__name_len(h->ms.map->dso);
82                 hists__new_col_len(hists, HISTC_DSO, len);
83         }
84
85         if (h->branch_info) {
86                 int symlen;
87                 /*
88                  * +4 accounts for '[x] ' priv level info
89                  * +2 account of 0x prefix on raw addresses
90                  */
91                 if (h->branch_info->from.sym) {
92                         symlen = (int)h->branch_info->from.sym->namelen + 4;
93                         hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
94
95                         symlen = dso__name_len(h->branch_info->from.map->dso);
96                         hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
97                 } else {
98                         symlen = unresolved_col_width + 4 + 2;
99                         hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
100                         hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
101                 }
102
103                 if (h->branch_info->to.sym) {
104                         symlen = (int)h->branch_info->to.sym->namelen + 4;
105                         hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
106
107                         symlen = dso__name_len(h->branch_info->to.map->dso);
108                         hists__new_col_len(hists, HISTC_DSO_TO, symlen);
109                 } else {
110                         symlen = unresolved_col_width + 4 + 2;
111                         hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112                         hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
113                 }
114         }
115 }
116
117 static void hist_entry__add_cpumode_period(struct hist_entry *he,
118                                            unsigned int cpumode, u64 period)
119 {
120         switch (cpumode) {
121         case PERF_RECORD_MISC_KERNEL:
122                 he->period_sys += period;
123                 break;
124         case PERF_RECORD_MISC_USER:
125                 he->period_us += period;
126                 break;
127         case PERF_RECORD_MISC_GUEST_KERNEL:
128                 he->period_guest_sys += period;
129                 break;
130         case PERF_RECORD_MISC_GUEST_USER:
131                 he->period_guest_us += period;
132                 break;
133         default:
134                 break;
135         }
136 }
137
138 static void hist_entry__decay(struct hist_entry *he)
139 {
140         he->period = (he->period * 7) / 8;
141         he->nr_events = (he->nr_events * 7) / 8;
142 }
143
144 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
145 {
146         u64 prev_period = he->period;
147
148         if (prev_period == 0)
149                 return true;
150
151         hist_entry__decay(he);
152
153         if (!he->filtered)
154                 hists->stats.total_period -= prev_period - he->period;
155
156         return he->period == 0;
157 }
158
159 static void __hists__decay_entries(struct hists *hists, bool zap_user,
160                                    bool zap_kernel, bool threaded)
161 {
162         struct rb_node *next = rb_first(&hists->entries);
163         struct hist_entry *n;
164
165         while (next) {
166                 n = rb_entry(next, struct hist_entry, rb_node);
167                 next = rb_next(&n->rb_node);
168                 /*
169                  * We may be annotating this, for instance, so keep it here in
170                  * case some it gets new samples, we'll eventually free it when
171                  * the user stops browsing and it agains gets fully decayed.
172                  */
173                 if (((zap_user && n->level == '.') ||
174                      (zap_kernel && n->level != '.') ||
175                      hists__decay_entry(hists, n)) &&
176                     !n->used) {
177                         rb_erase(&n->rb_node, &hists->entries);
178
179                         if (sort__need_collapse || threaded)
180                                 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
181
182                         hist_entry__free(n);
183                         --hists->nr_entries;
184                 }
185         }
186 }
187
188 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
189 {
190         return __hists__decay_entries(hists, zap_user, zap_kernel, false);
191 }
192
193 void hists__decay_entries_threaded(struct hists *hists,
194                                    bool zap_user, bool zap_kernel)
195 {
196         return __hists__decay_entries(hists, zap_user, zap_kernel, true);
197 }
198
199 /*
200  * histogram, sorted on item, collects periods
201  */
202
203 static struct hist_entry *hist_entry__new(struct hist_entry *template)
204 {
205         size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
206         struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
207
208         if (he != NULL) {
209                 *he = *template;
210                 he->nr_events = 1;
211                 if (he->ms.map)
212                         he->ms.map->referenced = true;
213                 if (symbol_conf.use_callchain)
214                         callchain_init(he->callchain);
215         }
216
217         return he;
218 }
219
220 static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
221 {
222         if (!h->filtered) {
223                 hists__calc_col_len(hists, h);
224                 ++hists->nr_entries;
225                 hists->stats.total_period += h->period;
226         }
227 }
228
229 static u8 symbol__parent_filter(const struct symbol *parent)
230 {
231         if (symbol_conf.exclude_other && parent == NULL)
232                 return 1 << HIST_FILTER__PARENT;
233         return 0;
234 }
235
236 static struct hist_entry *add_hist_entry(struct hists *hists,
237                                       struct hist_entry *entry,
238                                       struct addr_location *al,
239                                       u64 period)
240 {
241         struct rb_node **p;
242         struct rb_node *parent = NULL;
243         struct hist_entry *he;
244         int cmp;
245
246         pthread_mutex_lock(&hists->lock);
247
248         p = &hists->entries_in->rb_node;
249
250         while (*p != NULL) {
251                 parent = *p;
252                 he = rb_entry(parent, struct hist_entry, rb_node_in);
253
254                 cmp = hist_entry__cmp(entry, he);
255
256                 if (!cmp) {
257                         he->period += period;
258                         ++he->nr_events;
259                         goto out;
260                 }
261
262                 if (cmp < 0)
263                         p = &(*p)->rb_left;
264                 else
265                         p = &(*p)->rb_right;
266         }
267
268         he = hist_entry__new(entry);
269         if (!he)
270                 goto out_unlock;
271
272         rb_link_node(&he->rb_node_in, parent, p);
273         rb_insert_color(&he->rb_node_in, hists->entries_in);
274 out:
275         hist_entry__add_cpumode_period(he, al->cpumode, period);
276 out_unlock:
277         pthread_mutex_unlock(&hists->lock);
278         return he;
279 }
280
281 struct hist_entry *__hists__add_branch_entry(struct hists *self,
282                                              struct addr_location *al,
283                                              struct symbol *sym_parent,
284                                              struct branch_info *bi,
285                                              u64 period)
286 {
287         struct hist_entry entry = {
288                 .thread = al->thread,
289                 .ms = {
290                         .map    = bi->to.map,
291                         .sym    = bi->to.sym,
292                 },
293                 .cpu    = al->cpu,
294                 .ip     = bi->to.addr,
295                 .level  = al->level,
296                 .period = period,
297                 .parent = sym_parent,
298                 .filtered = symbol__parent_filter(sym_parent),
299                 .branch_info = bi,
300         };
301
302         return add_hist_entry(self, &entry, al, period);
303 }
304
305 struct hist_entry *__hists__add_entry(struct hists *self,
306                                       struct addr_location *al,
307                                       struct symbol *sym_parent, u64 period)
308 {
309         struct hist_entry entry = {
310                 .thread = al->thread,
311                 .ms = {
312                         .map    = al->map,
313                         .sym    = al->sym,
314                 },
315                 .cpu    = al->cpu,
316                 .ip     = al->addr,
317                 .level  = al->level,
318                 .period = period,
319                 .parent = sym_parent,
320                 .filtered = symbol__parent_filter(sym_parent),
321         };
322
323         return add_hist_entry(self, &entry, al, period);
324 }
325
326 int64_t
327 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
328 {
329         struct sort_entry *se;
330         int64_t cmp = 0;
331
332         list_for_each_entry(se, &hist_entry__sort_list, list) {
333                 cmp = se->se_cmp(left, right);
334                 if (cmp)
335                         break;
336         }
337
338         return cmp;
339 }
340
341 int64_t
342 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
343 {
344         struct sort_entry *se;
345         int64_t cmp = 0;
346
347         list_for_each_entry(se, &hist_entry__sort_list, list) {
348                 int64_t (*f)(struct hist_entry *, struct hist_entry *);
349
350                 f = se->se_collapse ?: se->se_cmp;
351
352                 cmp = f(left, right);
353                 if (cmp)
354                         break;
355         }
356
357         return cmp;
358 }
359
360 void hist_entry__free(struct hist_entry *he)
361 {
362         free(he);
363 }
364
365 /*
366  * collapse the histogram
367  */
368
369 static bool hists__collapse_insert_entry(struct hists *hists,
370                                          struct rb_root *root,
371                                          struct hist_entry *he)
372 {
373         struct rb_node **p = &root->rb_node;
374         struct rb_node *parent = NULL;
375         struct hist_entry *iter;
376         int64_t cmp;
377
378         while (*p != NULL) {
379                 parent = *p;
380                 iter = rb_entry(parent, struct hist_entry, rb_node_in);
381
382                 cmp = hist_entry__collapse(iter, he);
383
384                 if (!cmp) {
385                         iter->period += he->period;
386                         iter->nr_events += he->nr_events;
387                         if (symbol_conf.use_callchain) {
388                                 callchain_cursor_reset(&hists->callchain_cursor);
389                                 callchain_merge(&hists->callchain_cursor, iter->callchain,
390                                                 he->callchain);
391                         }
392                         hist_entry__free(he);
393                         return false;
394                 }
395
396                 if (cmp < 0)
397                         p = &(*p)->rb_left;
398                 else
399                         p = &(*p)->rb_right;
400         }
401
402         rb_link_node(&he->rb_node_in, parent, p);
403         rb_insert_color(&he->rb_node_in, root);
404         return true;
405 }
406
407 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
408 {
409         struct rb_root *root;
410
411         pthread_mutex_lock(&hists->lock);
412
413         root = hists->entries_in;
414         if (++hists->entries_in > &hists->entries_in_array[1])
415                 hists->entries_in = &hists->entries_in_array[0];
416
417         pthread_mutex_unlock(&hists->lock);
418
419         return root;
420 }
421
422 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
423 {
424         hists__filter_entry_by_dso(hists, he);
425         hists__filter_entry_by_thread(hists, he);
426         hists__filter_entry_by_symbol(hists, he);
427 }
428
429 static void __hists__collapse_resort(struct hists *hists, bool threaded)
430 {
431         struct rb_root *root;
432         struct rb_node *next;
433         struct hist_entry *n;
434
435         if (!sort__need_collapse && !threaded)
436                 return;
437
438         root = hists__get_rotate_entries_in(hists);
439         next = rb_first(root);
440
441         while (next) {
442                 n = rb_entry(next, struct hist_entry, rb_node_in);
443                 next = rb_next(&n->rb_node_in);
444
445                 rb_erase(&n->rb_node_in, root);
446                 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
447                         /*
448                          * If it wasn't combined with one of the entries already
449                          * collapsed, we need to apply the filters that may have
450                          * been set by, say, the hist_browser.
451                          */
452                         hists__apply_filters(hists, n);
453                 }
454         }
455 }
456
457 void hists__collapse_resort(struct hists *hists)
458 {
459         return __hists__collapse_resort(hists, false);
460 }
461
462 void hists__collapse_resort_threaded(struct hists *hists)
463 {
464         return __hists__collapse_resort(hists, true);
465 }
466
467 /*
468  * reverse the map, sort on period.
469  */
470
471 static void __hists__insert_output_entry(struct rb_root *entries,
472                                          struct hist_entry *he,
473                                          u64 min_callchain_hits)
474 {
475         struct rb_node **p = &entries->rb_node;
476         struct rb_node *parent = NULL;
477         struct hist_entry *iter;
478
479         if (symbol_conf.use_callchain)
480                 callchain_param.sort(&he->sorted_chain, he->callchain,
481                                       min_callchain_hits, &callchain_param);
482
483         while (*p != NULL) {
484                 parent = *p;
485                 iter = rb_entry(parent, struct hist_entry, rb_node);
486
487                 if (he->period > iter->period)
488                         p = &(*p)->rb_left;
489                 else
490                         p = &(*p)->rb_right;
491         }
492
493         rb_link_node(&he->rb_node, parent, p);
494         rb_insert_color(&he->rb_node, entries);
495 }
496
497 static void __hists__output_resort(struct hists *hists, bool threaded)
498 {
499         struct rb_root *root;
500         struct rb_node *next;
501         struct hist_entry *n;
502         u64 min_callchain_hits;
503
504         min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
505
506         if (sort__need_collapse || threaded)
507                 root = &hists->entries_collapsed;
508         else
509                 root = hists->entries_in;
510
511         next = rb_first(root);
512         hists->entries = RB_ROOT;
513
514         hists->nr_entries = 0;
515         hists->stats.total_period = 0;
516         hists__reset_col_len(hists);
517
518         while (next) {
519                 n = rb_entry(next, struct hist_entry, rb_node_in);
520                 next = rb_next(&n->rb_node_in);
521
522                 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
523                 hists__inc_nr_entries(hists, n);
524         }
525 }
526
527 void hists__output_resort(struct hists *hists)
528 {
529         return __hists__output_resort(hists, false);
530 }
531
532 void hists__output_resort_threaded(struct hists *hists)
533 {
534         return __hists__output_resort(hists, true);
535 }
536
537 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
538 {
539         int i;
540         int ret = fprintf(fp, "            ");
541
542         for (i = 0; i < left_margin; i++)
543                 ret += fprintf(fp, " ");
544
545         return ret;
546 }
547
548 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
549                                           int left_margin)
550 {
551         int i;
552         size_t ret = callchain__fprintf_left_margin(fp, left_margin);
553
554         for (i = 0; i < depth; i++)
555                 if (depth_mask & (1 << i))
556                         ret += fprintf(fp, "|          ");
557                 else
558                         ret += fprintf(fp, "           ");
559
560         ret += fprintf(fp, "\n");
561
562         return ret;
563 }
564
565 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
566                                      int depth, int depth_mask, int period,
567                                      u64 total_samples, u64 hits,
568                                      int left_margin)
569 {
570         int i;
571         size_t ret = 0;
572
573         ret += callchain__fprintf_left_margin(fp, left_margin);
574         for (i = 0; i < depth; i++) {
575                 if (depth_mask & (1 << i))
576                         ret += fprintf(fp, "|");
577                 else
578                         ret += fprintf(fp, " ");
579                 if (!period && i == depth - 1) {
580                         double percent;
581
582                         percent = hits * 100.0 / total_samples;
583                         ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
584                 } else
585                         ret += fprintf(fp, "%s", "          ");
586         }
587         if (chain->ms.sym)
588                 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
589         else
590                 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
591
592         return ret;
593 }
594
595 static struct symbol *rem_sq_bracket;
596 static struct callchain_list rem_hits;
597
598 static void init_rem_hits(void)
599 {
600         rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
601         if (!rem_sq_bracket) {
602                 fprintf(stderr, "Not enough memory to display remaining hits\n");
603                 return;
604         }
605
606         strcpy(rem_sq_bracket->name, "[...]");
607         rem_hits.ms.sym = rem_sq_bracket;
608 }
609
610 static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
611                                          u64 total_samples, int depth,
612                                          int depth_mask, int left_margin)
613 {
614         struct rb_node *node, *next;
615         struct callchain_node *child;
616         struct callchain_list *chain;
617         int new_depth_mask = depth_mask;
618         u64 new_total;
619         u64 remaining;
620         size_t ret = 0;
621         int i;
622         uint entries_printed = 0;
623
624         if (callchain_param.mode == CHAIN_GRAPH_REL)
625                 new_total = self->children_hit;
626         else
627                 new_total = total_samples;
628
629         remaining = new_total;
630
631         node = rb_first(&self->rb_root);
632         while (node) {
633                 u64 cumul;
634
635                 child = rb_entry(node, struct callchain_node, rb_node);
636                 cumul = callchain_cumul_hits(child);
637                 remaining -= cumul;
638
639                 /*
640                  * The depth mask manages the output of pipes that show
641                  * the depth. We don't want to keep the pipes of the current
642                  * level for the last child of this depth.
643                  * Except if we have remaining filtered hits. They will
644                  * supersede the last child
645                  */
646                 next = rb_next(node);
647                 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
648                         new_depth_mask &= ~(1 << (depth - 1));
649
650                 /*
651                  * But we keep the older depth mask for the line separator
652                  * to keep the level link until we reach the last child
653                  */
654                 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
655                                                    left_margin);
656                 i = 0;
657                 list_for_each_entry(chain, &child->val, list) {
658                         ret += ipchain__fprintf_graph(fp, chain, depth,
659                                                       new_depth_mask, i++,
660                                                       new_total,
661                                                       cumul,
662                                                       left_margin);
663                 }
664                 ret += __callchain__fprintf_graph(fp, child, new_total,
665                                                   depth + 1,
666                                                   new_depth_mask | (1 << depth),
667                                                   left_margin);
668                 node = next;
669                 if (++entries_printed == callchain_param.print_limit)
670                         break;
671         }
672
673         if (callchain_param.mode == CHAIN_GRAPH_REL &&
674                 remaining && remaining != new_total) {
675
676                 if (!rem_sq_bracket)
677                         return ret;
678
679                 new_depth_mask &= ~(1 << (depth - 1));
680
681                 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
682                                               new_depth_mask, 0, new_total,
683                                               remaining, left_margin);
684         }
685
686         return ret;
687 }
688
689 static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
690                                        u64 total_samples, int left_margin)
691 {
692         struct callchain_list *chain;
693         bool printed = false;
694         int i = 0;
695         int ret = 0;
696         u32 entries_printed = 0;
697
698         list_for_each_entry(chain, &self->val, list) {
699                 if (!i++ && sort__first_dimension == SORT_SYM)
700                         continue;
701
702                 if (!printed) {
703                         ret += callchain__fprintf_left_margin(fp, left_margin);
704                         ret += fprintf(fp, "|\n");
705                         ret += callchain__fprintf_left_margin(fp, left_margin);
706                         ret += fprintf(fp, "---");
707
708                         left_margin += 3;
709                         printed = true;
710                 } else
711                         ret += callchain__fprintf_left_margin(fp, left_margin);
712
713                 if (chain->ms.sym)
714                         ret += fprintf(fp, " %s\n", chain->ms.sym->name);
715                 else
716                         ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
717
718                 if (++entries_printed == callchain_param.print_limit)
719                         break;
720         }
721
722         ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
723
724         return ret;
725 }
726
727 static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
728                                       u64 total_samples)
729 {
730         struct callchain_list *chain;
731         size_t ret = 0;
732
733         if (!self)
734                 return 0;
735
736         ret += callchain__fprintf_flat(fp, self->parent, total_samples);
737
738
739         list_for_each_entry(chain, &self->val, list) {
740                 if (chain->ip >= PERF_CONTEXT_MAX)
741                         continue;
742                 if (chain->ms.sym)
743                         ret += fprintf(fp, "                %s\n", chain->ms.sym->name);
744                 else
745                         ret += fprintf(fp, "                %p\n",
746                                         (void *)(long)chain->ip);
747         }
748
749         return ret;
750 }
751
752 static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
753                                             u64 total_samples, int left_margin,
754                                             FILE *fp)
755 {
756         struct rb_node *rb_node;
757         struct callchain_node *chain;
758         size_t ret = 0;
759         u32 entries_printed = 0;
760
761         rb_node = rb_first(&he->sorted_chain);
762         while (rb_node) {
763                 double percent;
764
765                 chain = rb_entry(rb_node, struct callchain_node, rb_node);
766                 percent = chain->hit * 100.0 / total_samples;
767                 switch (callchain_param.mode) {
768                 case CHAIN_FLAT:
769                         ret += percent_color_fprintf(fp, "           %6.2f%%\n",
770                                                      percent);
771                         ret += callchain__fprintf_flat(fp, chain, total_samples);
772                         break;
773                 case CHAIN_GRAPH_ABS: /* Falldown */
774                 case CHAIN_GRAPH_REL:
775                         ret += callchain__fprintf_graph(fp, chain, total_samples,
776                                                         left_margin);
777                 case CHAIN_NONE:
778                 default:
779                         break;
780                 }
781                 ret += fprintf(fp, "\n");
782                 if (++entries_printed == callchain_param.print_limit)
783                         break;
784                 rb_node = rb_next(rb_node);
785         }
786
787         return ret;
788 }
789
790 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
791 {
792         struct rb_node *next = rb_first(&hists->entries);
793         struct hist_entry *n;
794         int row = 0;
795
796         hists__reset_col_len(hists);
797
798         while (next && row++ < max_rows) {
799                 n = rb_entry(next, struct hist_entry, rb_node);
800                 if (!n->filtered)
801                         hists__calc_col_len(hists, n);
802                 next = rb_next(&n->rb_node);
803         }
804 }
805
806 static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
807                                      size_t size, struct hists *pair_hists,
808                                      bool show_displacement, long displacement,
809                                      bool color, u64 total_period)
810 {
811         u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
812         u64 nr_events;
813         const char *sep = symbol_conf.field_sep;
814         int ret;
815
816         if (symbol_conf.exclude_other && !he->parent)
817                 return 0;
818
819         if (pair_hists) {
820                 period = he->pair ? he->pair->period : 0;
821                 nr_events = he->pair ? he->pair->nr_events : 0;
822                 total = pair_hists->stats.total_period;
823                 period_sys = he->pair ? he->pair->period_sys : 0;
824                 period_us = he->pair ? he->pair->period_us : 0;
825                 period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
826                 period_guest_us = he->pair ? he->pair->period_guest_us : 0;
827         } else {
828                 period = he->period;
829                 nr_events = he->nr_events;
830                 total = total_period;
831                 period_sys = he->period_sys;
832                 period_us = he->period_us;
833                 period_guest_sys = he->period_guest_sys;
834                 period_guest_us = he->period_guest_us;
835         }
836
837         if (total) {
838                 if (color)
839                         ret = percent_color_snprintf(s, size,
840                                                      sep ? "%.2f" : "   %6.2f%%",
841                                                      (period * 100.0) / total);
842                 else
843                         ret = scnprintf(s, size, sep ? "%.2f" : "   %6.2f%%",
844                                        (period * 100.0) / total);
845                 if (symbol_conf.show_cpu_utilization) {
846                         ret += percent_color_snprintf(s + ret, size - ret,
847                                         sep ? "%.2f" : "   %6.2f%%",
848                                         (period_sys * 100.0) / total);
849                         ret += percent_color_snprintf(s + ret, size - ret,
850                                         sep ? "%.2f" : "   %6.2f%%",
851                                         (period_us * 100.0) / total);
852                         if (perf_guest) {
853                                 ret += percent_color_snprintf(s + ret,
854                                                 size - ret,
855                                                 sep ? "%.2f" : "   %6.2f%%",
856                                                 (period_guest_sys * 100.0) /
857                                                                 total);
858                                 ret += percent_color_snprintf(s + ret,
859                                                 size - ret,
860                                                 sep ? "%.2f" : "   %6.2f%%",
861                                                 (period_guest_us * 100.0) /
862                                                                 total);
863                         }
864                 }
865         } else
866                 ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
867
868         if (symbol_conf.show_nr_samples) {
869                 if (sep)
870                         ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
871                 else
872                         ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
873         }
874
875         if (symbol_conf.show_total_period) {
876                 if (sep)
877                         ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
878                 else
879                         ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
880         }
881
882         if (pair_hists) {
883                 char bf[32];
884                 double old_percent = 0, new_percent = 0, diff;
885
886                 if (total > 0)
887                         old_percent = (period * 100.0) / total;
888                 if (total_period > 0)
889                         new_percent = (he->period * 100.0) / total_period;
890
891                 diff = new_percent - old_percent;
892
893                 if (fabs(diff) >= 0.01)
894                         ret += scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
895                 else
896                         ret += scnprintf(bf, sizeof(bf), " ");
897
898                 if (sep)
899                         ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
900                 else
901                         ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
902
903                 if (show_displacement) {
904                         if (displacement)
905                                 ret += scnprintf(bf, sizeof(bf), "%+4ld", displacement);
906                         else
907                                 ret += scnprintf(bf, sizeof(bf), " ");
908
909                         if (sep)
910                                 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
911                         else
912                                 ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
913                 }
914         }
915
916         return ret;
917 }
918
919 int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
920                          struct hists *hists)
921 {
922         const char *sep = symbol_conf.field_sep;
923         struct sort_entry *se;
924         int ret = 0;
925
926         list_for_each_entry(se, &hist_entry__sort_list, list) {
927                 if (se->elide)
928                         continue;
929
930                 ret += scnprintf(s + ret, size - ret, "%s", sep ?: "  ");
931                 ret += se->se_snprintf(he, s + ret, size - ret,
932                                        hists__col_len(hists, se->se_width_idx));
933         }
934
935         return ret;
936 }
937
938 static int hist_entry__fprintf(struct hist_entry *he, size_t size,
939                                struct hists *hists, struct hists *pair_hists,
940                                bool show_displacement, long displacement,
941                                u64 total_period, FILE *fp)
942 {
943         char bf[512];
944         int ret;
945
946         if (size == 0 || size > sizeof(bf))
947                 size = sizeof(bf);
948
949         ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
950                                         show_displacement, displacement,
951                                         true, total_period);
952         hist_entry__snprintf(he, bf + ret, size - ret, hists);
953         return fprintf(fp, "%s\n", bf);
954 }
955
956 static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
957                                             struct hists *hists,
958                                             u64 total_period, FILE *fp)
959 {
960         int left_margin = 0;
961
962         if (sort__first_dimension == SORT_COMM) {
963                 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
964                                                          typeof(*se), list);
965                 left_margin = hists__col_len(hists, se->se_width_idx);
966                 left_margin -= thread__comm_len(he->thread);
967         }
968
969         return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
970 }
971
972 size_t hists__fprintf(struct hists *hists, struct hists *pair,
973                       bool show_displacement, bool show_header, int max_rows,
974                       int max_cols, FILE *fp)
975 {
976         struct sort_entry *se;
977         struct rb_node *nd;
978         size_t ret = 0;
979         u64 total_period;
980         unsigned long position = 1;
981         long displacement = 0;
982         unsigned int width;
983         const char *sep = symbol_conf.field_sep;
984         const char *col_width = symbol_conf.col_width_list_str;
985         int nr_rows = 0;
986
987         init_rem_hits();
988
989         if (!show_header)
990                 goto print_entries;
991
992         fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
993
994         if (symbol_conf.show_cpu_utilization) {
995                 if (sep) {
996                         ret += fprintf(fp, "%csys", *sep);
997                         ret += fprintf(fp, "%cus", *sep);
998                         if (perf_guest) {
999                                 ret += fprintf(fp, "%cguest sys", *sep);
1000                                 ret += fprintf(fp, "%cguest us", *sep);
1001                         }
1002                 } else {
1003                         ret += fprintf(fp, "     sys  ");
1004                         ret += fprintf(fp, "      us  ");
1005                         if (perf_guest) {
1006                                 ret += fprintf(fp, "  guest sys  ");
1007                                 ret += fprintf(fp, "  guest us  ");
1008                         }
1009                 }
1010         }
1011
1012         if (symbol_conf.show_nr_samples) {
1013                 if (sep)
1014                         fprintf(fp, "%cSamples", *sep);
1015                 else
1016                         fputs("  Samples  ", fp);
1017         }
1018
1019         if (symbol_conf.show_total_period) {
1020                 if (sep)
1021                         ret += fprintf(fp, "%cPeriod", *sep);
1022                 else
1023                         ret += fprintf(fp, "   Period    ");
1024         }
1025
1026         if (pair) {
1027                 if (sep)
1028                         ret += fprintf(fp, "%cDelta", *sep);
1029                 else
1030                         ret += fprintf(fp, "  Delta    ");
1031
1032                 if (show_displacement) {
1033                         if (sep)
1034                                 ret += fprintf(fp, "%cDisplacement", *sep);
1035                         else
1036                                 ret += fprintf(fp, " Displ");
1037                 }
1038         }
1039
1040         list_for_each_entry(se, &hist_entry__sort_list, list) {
1041                 if (se->elide)
1042                         continue;
1043                 if (sep) {
1044                         fprintf(fp, "%c%s", *sep, se->se_header);
1045                         continue;
1046                 }
1047                 width = strlen(se->se_header);
1048                 if (symbol_conf.col_width_list_str) {
1049                         if (col_width) {
1050                                 hists__set_col_len(hists, se->se_width_idx,
1051                                                    atoi(col_width));
1052                                 col_width = strchr(col_width, ',');
1053                                 if (col_width)
1054                                         ++col_width;
1055                         }
1056                 }
1057                 if (!hists__new_col_len(hists, se->se_width_idx, width))
1058                         width = hists__col_len(hists, se->se_width_idx);
1059                 fprintf(fp, "  %*s", width, se->se_header);
1060         }
1061
1062         fprintf(fp, "\n");
1063         if (max_rows && ++nr_rows >= max_rows)
1064                 goto out;
1065
1066         if (sep)
1067                 goto print_entries;
1068
1069         fprintf(fp, "# ........");
1070         if (symbol_conf.show_cpu_utilization)
1071                 fprintf(fp, "   .......   .......");
1072         if (symbol_conf.show_nr_samples)
1073                 fprintf(fp, " ..........");
1074         if (symbol_conf.show_total_period)
1075                 fprintf(fp, " ............");
1076         if (pair) {
1077                 fprintf(fp, " ..........");
1078                 if (show_displacement)
1079                         fprintf(fp, " .....");
1080         }
1081         list_for_each_entry(se, &hist_entry__sort_list, list) {
1082                 unsigned int i;
1083
1084                 if (se->elide)
1085                         continue;
1086
1087                 fprintf(fp, "  ");
1088                 width = hists__col_len(hists, se->se_width_idx);
1089                 if (width == 0)
1090                         width = strlen(se->se_header);
1091                 for (i = 0; i < width; i++)
1092                         fprintf(fp, ".");
1093         }
1094
1095         fprintf(fp, "\n");
1096         if (max_rows && ++nr_rows >= max_rows)
1097                 goto out;
1098
1099         fprintf(fp, "#\n");
1100         if (max_rows && ++nr_rows >= max_rows)
1101                 goto out;
1102
1103 print_entries:
1104         total_period = hists->stats.total_period;
1105
1106         for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1107                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1108
1109                 if (h->filtered)
1110                         continue;
1111
1112                 if (show_displacement) {
1113                         if (h->pair != NULL)
1114                                 displacement = ((long)h->pair->position -
1115                                                 (long)position);
1116                         else
1117                                 displacement = 0;
1118                         ++position;
1119                 }
1120                 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
1121                                            displacement, total_period, fp);
1122
1123                 if (symbol_conf.use_callchain)
1124                         ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
1125                 if (max_rows && ++nr_rows >= max_rows)
1126                         goto out;
1127
1128                 if (h->ms.map == NULL && verbose > 1) {
1129                         __map_groups__fprintf_maps(&h->thread->mg,
1130                                                    MAP__FUNCTION, verbose, fp);
1131                         fprintf(fp, "%.10s end\n", graph_dotted_line);
1132                 }
1133         }
1134 out:
1135         free(rem_sq_bracket);
1136
1137         return ret;
1138 }
1139
1140 /*
1141  * See hists__fprintf to match the column widths
1142  */
1143 unsigned int hists__sort_list_width(struct hists *hists)
1144 {
1145         struct sort_entry *se;
1146         int ret = 9; /* total % */
1147
1148         if (symbol_conf.show_cpu_utilization) {
1149                 ret += 7; /* count_sys % */
1150                 ret += 6; /* count_us % */
1151                 if (perf_guest) {
1152                         ret += 13; /* count_guest_sys % */
1153                         ret += 12; /* count_guest_us % */
1154                 }
1155         }
1156
1157         if (symbol_conf.show_nr_samples)
1158                 ret += 11;
1159
1160         if (symbol_conf.show_total_period)
1161                 ret += 13;
1162
1163         list_for_each_entry(se, &hist_entry__sort_list, list)
1164                 if (!se->elide)
1165                         ret += 2 + hists__col_len(hists, se->se_width_idx);
1166
1167         if (verbose) /* Addr + origin */
1168                 ret += 3 + BITS_PER_LONG / 4;
1169
1170         return ret;
1171 }
1172
1173 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1174                                        enum hist_filter filter)
1175 {
1176         h->filtered &= ~(1 << filter);
1177         if (h->filtered)
1178                 return;
1179
1180         ++hists->nr_entries;
1181         if (h->ms.unfolded)
1182                 hists->nr_entries += h->nr_rows;
1183         h->row_offset = 0;
1184         hists->stats.total_period += h->period;
1185         hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
1186
1187         hists__calc_col_len(hists, h);
1188 }
1189
1190
1191 static bool hists__filter_entry_by_dso(struct hists *hists,
1192                                        struct hist_entry *he)
1193 {
1194         if (hists->dso_filter != NULL &&
1195             (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1196                 he->filtered |= (1 << HIST_FILTER__DSO);
1197                 return true;
1198         }
1199
1200         return false;
1201 }
1202
1203 void hists__filter_by_dso(struct hists *hists)
1204 {
1205         struct rb_node *nd;
1206
1207         hists->nr_entries = hists->stats.total_period = 0;
1208         hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1209         hists__reset_col_len(hists);
1210
1211         for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1212                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1213
1214                 if (symbol_conf.exclude_other && !h->parent)
1215                         continue;
1216
1217                 if (hists__filter_entry_by_dso(hists, h))
1218                         continue;
1219
1220                 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1221         }
1222 }
1223
1224 static bool hists__filter_entry_by_thread(struct hists *hists,
1225                                           struct hist_entry *he)
1226 {
1227         if (hists->thread_filter != NULL &&
1228             he->thread != hists->thread_filter) {
1229                 he->filtered |= (1 << HIST_FILTER__THREAD);
1230                 return true;
1231         }
1232
1233         return false;
1234 }
1235
1236 void hists__filter_by_thread(struct hists *hists)
1237 {
1238         struct rb_node *nd;
1239
1240         hists->nr_entries = hists->stats.total_period = 0;
1241         hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1242         hists__reset_col_len(hists);
1243
1244         for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1245                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1246
1247                 if (hists__filter_entry_by_thread(hists, h))
1248                         continue;
1249
1250                 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1251         }
1252 }
1253
1254 static bool hists__filter_entry_by_symbol(struct hists *hists,
1255                                           struct hist_entry *he)
1256 {
1257         if (hists->symbol_filter_str != NULL &&
1258             (!he->ms.sym || strstr(he->ms.sym->name,
1259                                    hists->symbol_filter_str) == NULL)) {
1260                 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1261                 return true;
1262         }
1263
1264         return false;
1265 }
1266
1267 void hists__filter_by_symbol(struct hists *hists)
1268 {
1269         struct rb_node *nd;
1270
1271         hists->nr_entries = hists->stats.total_period = 0;
1272         hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1273         hists__reset_col_len(hists);
1274
1275         for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1276                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1277
1278                 if (hists__filter_entry_by_symbol(hists, h))
1279                         continue;
1280
1281                 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1282         }
1283 }
1284
1285 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
1286 {
1287         return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
1288 }
1289
1290 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
1291 {
1292         return symbol__annotate(he->ms.sym, he->ms.map, privsize);
1293 }
1294
1295 void hists__inc_nr_events(struct hists *hists, u32 type)
1296 {
1297         ++hists->stats.nr_events[0];
1298         ++hists->stats.nr_events[type];
1299 }
1300
1301 size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
1302 {
1303         int i;
1304         size_t ret = 0;
1305
1306         for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
1307                 const char *name;
1308
1309                 if (hists->stats.nr_events[i] == 0)
1310                         continue;
1311
1312                 name = perf_event__name(i);
1313                 if (!strcmp(name, "UNKNOWN"))
1314                         continue;
1315
1316                 ret += fprintf(fp, "%16s events: %10d\n", name,
1317                                hists->stats.nr_events[i]);
1318         }
1319
1320         return ret;
1321 }