d25fded72a11209957833aef9df71c780204e481
[firefly-linux-kernel-4.4.55.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 enum hist_filter {
10         HIST_FILTER__DSO,
11         HIST_FILTER__THREAD,
12         HIST_FILTER__PARENT,
13 };
14
15 struct callchain_param  callchain_param = {
16         .mode   = CHAIN_GRAPH_REL,
17         .min_percent = 0.5
18 };
19
20 u16 hists__col_len(struct hists *self, enum hist_column col)
21 {
22         return self->col_len[col];
23 }
24
25 void hists__set_col_len(struct hists *self, enum hist_column col, u16 len)
26 {
27         self->col_len[col] = len;
28 }
29
30 bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len)
31 {
32         if (len > hists__col_len(self, col)) {
33                 hists__set_col_len(self, col, len);
34                 return true;
35         }
36         return false;
37 }
38
39 static void hists__reset_col_len(struct hists *self)
40 {
41         enum hist_column col;
42
43         for (col = 0; col < HISTC_NR_COLS; ++col)
44                 hists__set_col_len(self, col, 0);
45 }
46
47 static void hists__calc_col_len(struct hists *self, struct hist_entry *h)
48 {
49         u16 len;
50
51         if (h->ms.sym)
52                 hists__new_col_len(self, HISTC_SYMBOL, h->ms.sym->namelen);
53         else {
54                 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
55
56                 if (hists__col_len(self, HISTC_DSO) < unresolved_col_width &&
57                     !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
58                     !symbol_conf.dso_list)
59                         hists__set_col_len(self, HISTC_DSO,
60                                            unresolved_col_width);
61         }
62
63         len = thread__comm_len(h->thread);
64         if (hists__new_col_len(self, HISTC_COMM, len))
65                 hists__set_col_len(self, HISTC_THREAD, len + 6);
66
67         if (h->ms.map) {
68                 len = dso__name_len(h->ms.map->dso);
69                 hists__new_col_len(self, HISTC_DSO, len);
70         }
71 }
72
73 static void hist_entry__add_cpumode_period(struct hist_entry *self,
74                                            unsigned int cpumode, u64 period)
75 {
76         switch (cpumode) {
77         case PERF_RECORD_MISC_KERNEL:
78                 self->period_sys += period;
79                 break;
80         case PERF_RECORD_MISC_USER:
81                 self->period_us += period;
82                 break;
83         case PERF_RECORD_MISC_GUEST_KERNEL:
84                 self->period_guest_sys += period;
85                 break;
86         case PERF_RECORD_MISC_GUEST_USER:
87                 self->period_guest_us += period;
88                 break;
89         default:
90                 break;
91         }
92 }
93
94 /*
95  * histogram, sorted on item, collects periods
96  */
97
98 static struct hist_entry *hist_entry__new(struct hist_entry *template)
99 {
100         size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
101         struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
102
103         if (self != NULL) {
104                 *self = *template;
105                 self->nr_events = 1;
106                 if (self->ms.map)
107                         self->ms.map->referenced = true;
108                 if (symbol_conf.use_callchain)
109                         callchain_init(self->callchain);
110         }
111
112         return self;
113 }
114
115 static void hists__inc_nr_entries(struct hists *self, struct hist_entry *h)
116 {
117         if (!h->filtered) {
118                 hists__calc_col_len(self, h);
119                 ++self->nr_entries;
120         }
121 }
122
123 static u8 symbol__parent_filter(const struct symbol *parent)
124 {
125         if (symbol_conf.exclude_other && parent == NULL)
126                 return 1 << HIST_FILTER__PARENT;
127         return 0;
128 }
129
130 struct hist_entry *__hists__add_entry(struct hists *self,
131                                       struct addr_location *al,
132                                       struct symbol *sym_parent, u64 period)
133 {
134         struct rb_node **p = &self->entries.rb_node;
135         struct rb_node *parent = NULL;
136         struct hist_entry *he;
137         struct hist_entry entry = {
138                 .thread = al->thread,
139                 .ms = {
140                         .map    = al->map,
141                         .sym    = al->sym,
142                 },
143                 .cpu    = al->cpu,
144                 .ip     = al->addr,
145                 .level  = al->level,
146                 .period = period,
147                 .parent = sym_parent,
148                 .filtered = symbol__parent_filter(sym_parent),
149         };
150         int cmp;
151
152         while (*p != NULL) {
153                 parent = *p;
154                 he = rb_entry(parent, struct hist_entry, rb_node);
155
156                 cmp = hist_entry__cmp(&entry, he);
157
158                 if (!cmp) {
159                         he->period += period;
160                         ++he->nr_events;
161
162                         /* If the map of an existing hist_entry has
163                          * become out-of-date due to an exec() or
164                          * similar, update it.  Otherwise we will
165                          * mis-adjust symbol addresses when computing
166                          * the history counter to increment.
167                          */
168                         if (he->ms.map != entry->ms.map) {
169                                 he->ms.map = entry->ms.map;
170                                 if (he->ms.map)
171                                         he->ms.map->referenced = true;
172                         }
173                         goto out;
174                 }
175
176                 if (cmp < 0)
177                         p = &(*p)->rb_left;
178                 else
179                         p = &(*p)->rb_right;
180         }
181
182         he = hist_entry__new(&entry);
183         if (!he)
184                 return NULL;
185         rb_link_node(&he->rb_node, parent, p);
186         rb_insert_color(&he->rb_node, &self->entries);
187         hists__inc_nr_entries(self, he);
188 out:
189         hist_entry__add_cpumode_period(he, al->cpumode, period);
190         return he;
191 }
192
193 int64_t
194 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
195 {
196         struct sort_entry *se;
197         int64_t cmp = 0;
198
199         list_for_each_entry(se, &hist_entry__sort_list, list) {
200                 cmp = se->se_cmp(left, right);
201                 if (cmp)
202                         break;
203         }
204
205         return cmp;
206 }
207
208 int64_t
209 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
210 {
211         struct sort_entry *se;
212         int64_t cmp = 0;
213
214         list_for_each_entry(se, &hist_entry__sort_list, list) {
215                 int64_t (*f)(struct hist_entry *, struct hist_entry *);
216
217                 f = se->se_collapse ?: se->se_cmp;
218
219                 cmp = f(left, right);
220                 if (cmp)
221                         break;
222         }
223
224         return cmp;
225 }
226
227 void hist_entry__free(struct hist_entry *he)
228 {
229         free(he);
230 }
231
232 /*
233  * collapse the histogram
234  */
235
236 static bool hists__collapse_insert_entry(struct hists *self,
237                                          struct rb_root *root,
238                                          struct hist_entry *he)
239 {
240         struct rb_node **p = &root->rb_node;
241         struct rb_node *parent = NULL;
242         struct hist_entry *iter;
243         int64_t cmp;
244
245         while (*p != NULL) {
246                 parent = *p;
247                 iter = rb_entry(parent, struct hist_entry, rb_node);
248
249                 cmp = hist_entry__collapse(iter, he);
250
251                 if (!cmp) {
252                         iter->period += he->period;
253                         if (symbol_conf.use_callchain) {
254                                 callchain_cursor_reset(&self->callchain_cursor);
255                                 callchain_merge(&self->callchain_cursor, iter->callchain,
256                                                 he->callchain);
257                         }
258                         hist_entry__free(he);
259                         return false;
260                 }
261
262                 if (cmp < 0)
263                         p = &(*p)->rb_left;
264                 else
265                         p = &(*p)->rb_right;
266         }
267
268         rb_link_node(&he->rb_node, parent, p);
269         rb_insert_color(&he->rb_node, root);
270         return true;
271 }
272
273 void hists__collapse_resort(struct hists *self)
274 {
275         struct rb_root tmp;
276         struct rb_node *next;
277         struct hist_entry *n;
278
279         if (!sort__need_collapse)
280                 return;
281
282         tmp = RB_ROOT;
283         next = rb_first(&self->entries);
284         self->nr_entries = 0;
285         hists__reset_col_len(self);
286
287         while (next) {
288                 n = rb_entry(next, struct hist_entry, rb_node);
289                 next = rb_next(&n->rb_node);
290
291                 rb_erase(&n->rb_node, &self->entries);
292                 if (hists__collapse_insert_entry(self, &tmp, n))
293                         hists__inc_nr_entries(self, n);
294         }
295
296         self->entries = tmp;
297 }
298
299 /*
300  * reverse the map, sort on period.
301  */
302
303 static void __hists__insert_output_entry(struct rb_root *entries,
304                                          struct hist_entry *he,
305                                          u64 min_callchain_hits)
306 {
307         struct rb_node **p = &entries->rb_node;
308         struct rb_node *parent = NULL;
309         struct hist_entry *iter;
310
311         if (symbol_conf.use_callchain)
312                 callchain_param.sort(&he->sorted_chain, he->callchain,
313                                       min_callchain_hits, &callchain_param);
314
315         while (*p != NULL) {
316                 parent = *p;
317                 iter = rb_entry(parent, struct hist_entry, rb_node);
318
319                 if (he->period > iter->period)
320                         p = &(*p)->rb_left;
321                 else
322                         p = &(*p)->rb_right;
323         }
324
325         rb_link_node(&he->rb_node, parent, p);
326         rb_insert_color(&he->rb_node, entries);
327 }
328
329 void hists__output_resort(struct hists *self)
330 {
331         struct rb_root tmp;
332         struct rb_node *next;
333         struct hist_entry *n;
334         u64 min_callchain_hits;
335
336         min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
337
338         tmp = RB_ROOT;
339         next = rb_first(&self->entries);
340
341         self->nr_entries = 0;
342         hists__reset_col_len(self);
343
344         while (next) {
345                 n = rb_entry(next, struct hist_entry, rb_node);
346                 next = rb_next(&n->rb_node);
347
348                 rb_erase(&n->rb_node, &self->entries);
349                 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
350                 hists__inc_nr_entries(self, n);
351         }
352
353         self->entries = tmp;
354 }
355
356 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
357 {
358         int i;
359         int ret = fprintf(fp, "            ");
360
361         for (i = 0; i < left_margin; i++)
362                 ret += fprintf(fp, " ");
363
364         return ret;
365 }
366
367 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
368                                           int left_margin)
369 {
370         int i;
371         size_t ret = callchain__fprintf_left_margin(fp, left_margin);
372
373         for (i = 0; i < depth; i++)
374                 if (depth_mask & (1 << i))
375                         ret += fprintf(fp, "|          ");
376                 else
377                         ret += fprintf(fp, "           ");
378
379         ret += fprintf(fp, "\n");
380
381         return ret;
382 }
383
384 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
385                                      int depth, int depth_mask, int period,
386                                      u64 total_samples, u64 hits,
387                                      int left_margin)
388 {
389         int i;
390         size_t ret = 0;
391
392         ret += callchain__fprintf_left_margin(fp, left_margin);
393         for (i = 0; i < depth; i++) {
394                 if (depth_mask & (1 << i))
395                         ret += fprintf(fp, "|");
396                 else
397                         ret += fprintf(fp, " ");
398                 if (!period && i == depth - 1) {
399                         double percent;
400
401                         percent = hits * 100.0 / total_samples;
402                         ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
403                 } else
404                         ret += fprintf(fp, "%s", "          ");
405         }
406         if (chain->ms.sym)
407                 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
408         else
409                 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
410
411         return ret;
412 }
413
414 static struct symbol *rem_sq_bracket;
415 static struct callchain_list rem_hits;
416
417 static void init_rem_hits(void)
418 {
419         rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
420         if (!rem_sq_bracket) {
421                 fprintf(stderr, "Not enough memory to display remaining hits\n");
422                 return;
423         }
424
425         strcpy(rem_sq_bracket->name, "[...]");
426         rem_hits.ms.sym = rem_sq_bracket;
427 }
428
429 static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
430                                          u64 total_samples, int depth,
431                                          int depth_mask, int left_margin)
432 {
433         struct rb_node *node, *next;
434         struct callchain_node *child;
435         struct callchain_list *chain;
436         int new_depth_mask = depth_mask;
437         u64 new_total;
438         u64 remaining;
439         size_t ret = 0;
440         int i;
441         uint entries_printed = 0;
442
443         if (callchain_param.mode == CHAIN_GRAPH_REL)
444                 new_total = self->children_hit;
445         else
446                 new_total = total_samples;
447
448         remaining = new_total;
449
450         node = rb_first(&self->rb_root);
451         while (node) {
452                 u64 cumul;
453
454                 child = rb_entry(node, struct callchain_node, rb_node);
455                 cumul = callchain_cumul_hits(child);
456                 remaining -= cumul;
457
458                 /*
459                  * The depth mask manages the output of pipes that show
460                  * the depth. We don't want to keep the pipes of the current
461                  * level for the last child of this depth.
462                  * Except if we have remaining filtered hits. They will
463                  * supersede the last child
464                  */
465                 next = rb_next(node);
466                 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
467                         new_depth_mask &= ~(1 << (depth - 1));
468
469                 /*
470                  * But we keep the older depth mask for the line separator
471                  * to keep the level link until we reach the last child
472                  */
473                 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
474                                                    left_margin);
475                 i = 0;
476                 list_for_each_entry(chain, &child->val, list) {
477                         ret += ipchain__fprintf_graph(fp, chain, depth,
478                                                       new_depth_mask, i++,
479                                                       new_total,
480                                                       cumul,
481                                                       left_margin);
482                 }
483                 ret += __callchain__fprintf_graph(fp, child, new_total,
484                                                   depth + 1,
485                                                   new_depth_mask | (1 << depth),
486                                                   left_margin);
487                 node = next;
488                 if (++entries_printed == callchain_param.print_limit)
489                         break;
490         }
491
492         if (callchain_param.mode == CHAIN_GRAPH_REL &&
493                 remaining && remaining != new_total) {
494
495                 if (!rem_sq_bracket)
496                         return ret;
497
498                 new_depth_mask &= ~(1 << (depth - 1));
499
500                 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
501                                               new_depth_mask, 0, new_total,
502                                               remaining, left_margin);
503         }
504
505         return ret;
506 }
507
508 static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
509                                        u64 total_samples, int left_margin)
510 {
511         struct callchain_list *chain;
512         bool printed = false;
513         int i = 0;
514         int ret = 0;
515         u32 entries_printed = 0;
516
517         list_for_each_entry(chain, &self->val, list) {
518                 if (!i++ && sort__first_dimension == SORT_SYM)
519                         continue;
520
521                 if (!printed) {
522                         ret += callchain__fprintf_left_margin(fp, left_margin);
523                         ret += fprintf(fp, "|\n");
524                         ret += callchain__fprintf_left_margin(fp, left_margin);
525                         ret += fprintf(fp, "---");
526
527                         left_margin += 3;
528                         printed = true;
529                 } else
530                         ret += callchain__fprintf_left_margin(fp, left_margin);
531
532                 if (chain->ms.sym)
533                         ret += fprintf(fp, " %s\n", chain->ms.sym->name);
534                 else
535                         ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
536
537                 if (++entries_printed == callchain_param.print_limit)
538                         break;
539         }
540
541         ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
542
543         return ret;
544 }
545
546 static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
547                                       u64 total_samples)
548 {
549         struct callchain_list *chain;
550         size_t ret = 0;
551
552         if (!self)
553                 return 0;
554
555         ret += callchain__fprintf_flat(fp, self->parent, total_samples);
556
557
558         list_for_each_entry(chain, &self->val, list) {
559                 if (chain->ip >= PERF_CONTEXT_MAX)
560                         continue;
561                 if (chain->ms.sym)
562                         ret += fprintf(fp, "                %s\n", chain->ms.sym->name);
563                 else
564                         ret += fprintf(fp, "                %p\n",
565                                         (void *)(long)chain->ip);
566         }
567
568         return ret;
569 }
570
571 static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
572                                             u64 total_samples, int left_margin)
573 {
574         struct rb_node *rb_node;
575         struct callchain_node *chain;
576         size_t ret = 0;
577         u32 entries_printed = 0;
578
579         rb_node = rb_first(&self->sorted_chain);
580         while (rb_node) {
581                 double percent;
582
583                 chain = rb_entry(rb_node, struct callchain_node, rb_node);
584                 percent = chain->hit * 100.0 / total_samples;
585                 switch (callchain_param.mode) {
586                 case CHAIN_FLAT:
587                         ret += percent_color_fprintf(fp, "           %6.2f%%\n",
588                                                      percent);
589                         ret += callchain__fprintf_flat(fp, chain, total_samples);
590                         break;
591                 case CHAIN_GRAPH_ABS: /* Falldown */
592                 case CHAIN_GRAPH_REL:
593                         ret += callchain__fprintf_graph(fp, chain, total_samples,
594                                                         left_margin);
595                 case CHAIN_NONE:
596                 default:
597                         break;
598                 }
599                 ret += fprintf(fp, "\n");
600                 if (++entries_printed == callchain_param.print_limit)
601                         break;
602                 rb_node = rb_next(rb_node);
603         }
604
605         return ret;
606 }
607
608 int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
609                          struct hists *hists, struct hists *pair_hists,
610                          bool show_displacement, long displacement,
611                          bool color, u64 session_total)
612 {
613         struct sort_entry *se;
614         u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
615         u64 nr_events;
616         const char *sep = symbol_conf.field_sep;
617         int ret;
618
619         if (symbol_conf.exclude_other && !self->parent)
620                 return 0;
621
622         if (pair_hists) {
623                 period = self->pair ? self->pair->period : 0;
624                 nr_events = self->pair ? self->pair->nr_events : 0;
625                 total = pair_hists->stats.total_period;
626                 period_sys = self->pair ? self->pair->period_sys : 0;
627                 period_us = self->pair ? self->pair->period_us : 0;
628                 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
629                 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
630         } else {
631                 period = self->period;
632                 nr_events = self->nr_events;
633                 total = session_total;
634                 period_sys = self->period_sys;
635                 period_us = self->period_us;
636                 period_guest_sys = self->period_guest_sys;
637                 period_guest_us = self->period_guest_us;
638         }
639
640         if (total) {
641                 if (color)
642                         ret = percent_color_snprintf(s, size,
643                                                      sep ? "%.2f" : "   %6.2f%%",
644                                                      (period * 100.0) / total);
645                 else
646                         ret = snprintf(s, size, sep ? "%.2f" : "   %6.2f%%",
647                                        (period * 100.0) / total);
648                 if (symbol_conf.show_cpu_utilization) {
649                         ret += percent_color_snprintf(s + ret, size - ret,
650                                         sep ? "%.2f" : "   %6.2f%%",
651                                         (period_sys * 100.0) / total);
652                         ret += percent_color_snprintf(s + ret, size - ret,
653                                         sep ? "%.2f" : "   %6.2f%%",
654                                         (period_us * 100.0) / total);
655                         if (perf_guest) {
656                                 ret += percent_color_snprintf(s + ret,
657                                                 size - ret,
658                                                 sep ? "%.2f" : "   %6.2f%%",
659                                                 (period_guest_sys * 100.0) /
660                                                                 total);
661                                 ret += percent_color_snprintf(s + ret,
662                                                 size - ret,
663                                                 sep ? "%.2f" : "   %6.2f%%",
664                                                 (period_guest_us * 100.0) /
665                                                                 total);
666                         }
667                 }
668         } else
669                 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
670
671         if (symbol_conf.show_nr_samples) {
672                 if (sep)
673                         ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
674                 else
675                         ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
676         }
677
678         if (pair_hists) {
679                 char bf[32];
680                 double old_percent = 0, new_percent = 0, diff;
681
682                 if (total > 0)
683                         old_percent = (period * 100.0) / total;
684                 if (session_total > 0)
685                         new_percent = (self->period * 100.0) / session_total;
686
687                 diff = new_percent - old_percent;
688
689                 if (fabs(diff) >= 0.01)
690                         snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
691                 else
692                         snprintf(bf, sizeof(bf), " ");
693
694                 if (sep)
695                         ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
696                 else
697                         ret += snprintf(s + ret, size - ret, "%11.11s", bf);
698
699                 if (show_displacement) {
700                         if (displacement)
701                                 snprintf(bf, sizeof(bf), "%+4ld", displacement);
702                         else
703                                 snprintf(bf, sizeof(bf), " ");
704
705                         if (sep)
706                                 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
707                         else
708                                 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
709                 }
710         }
711
712         list_for_each_entry(se, &hist_entry__sort_list, list) {
713                 if (se->elide)
714                         continue;
715
716                 ret += snprintf(s + ret, size - ret, "%s", sep ?: "  ");
717                 ret += se->se_snprintf(self, s + ret, size - ret,
718                                        hists__col_len(hists, se->se_width_idx));
719         }
720
721         return ret;
722 }
723
724 int hist_entry__fprintf(struct hist_entry *self, struct hists *hists,
725                         struct hists *pair_hists, bool show_displacement,
726                         long displacement, FILE *fp, u64 session_total)
727 {
728         char bf[512];
729         hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
730                              show_displacement, displacement,
731                              true, session_total);
732         return fprintf(fp, "%s\n", bf);
733 }
734
735 static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
736                                             struct hists *hists, FILE *fp,
737                                             u64 session_total)
738 {
739         int left_margin = 0;
740
741         if (sort__first_dimension == SORT_COMM) {
742                 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
743                                                          typeof(*se), list);
744                 left_margin = hists__col_len(hists, se->se_width_idx);
745                 left_margin -= thread__comm_len(self->thread);
746         }
747
748         return hist_entry_callchain__fprintf(fp, self, session_total,
749                                              left_margin);
750 }
751
752 size_t hists__fprintf(struct hists *self, struct hists *pair,
753                       bool show_displacement, FILE *fp)
754 {
755         struct sort_entry *se;
756         struct rb_node *nd;
757         size_t ret = 0;
758         unsigned long position = 1;
759         long displacement = 0;
760         unsigned int width;
761         const char *sep = symbol_conf.field_sep;
762         const char *col_width = symbol_conf.col_width_list_str;
763
764         init_rem_hits();
765
766         fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
767
768         if (symbol_conf.show_nr_samples) {
769                 if (sep)
770                         fprintf(fp, "%cSamples", *sep);
771                 else
772                         fputs("  Samples  ", fp);
773         }
774
775         if (symbol_conf.show_cpu_utilization) {
776                 if (sep) {
777                         ret += fprintf(fp, "%csys", *sep);
778                         ret += fprintf(fp, "%cus", *sep);
779                         if (perf_guest) {
780                                 ret += fprintf(fp, "%cguest sys", *sep);
781                                 ret += fprintf(fp, "%cguest us", *sep);
782                         }
783                 } else {
784                         ret += fprintf(fp, "  sys  ");
785                         ret += fprintf(fp, "  us  ");
786                         if (perf_guest) {
787                                 ret += fprintf(fp, "  guest sys  ");
788                                 ret += fprintf(fp, "  guest us  ");
789                         }
790                 }
791         }
792
793         if (pair) {
794                 if (sep)
795                         ret += fprintf(fp, "%cDelta", *sep);
796                 else
797                         ret += fprintf(fp, "  Delta    ");
798
799                 if (show_displacement) {
800                         if (sep)
801                                 ret += fprintf(fp, "%cDisplacement", *sep);
802                         else
803                                 ret += fprintf(fp, " Displ");
804                 }
805         }
806
807         list_for_each_entry(se, &hist_entry__sort_list, list) {
808                 if (se->elide)
809                         continue;
810                 if (sep) {
811                         fprintf(fp, "%c%s", *sep, se->se_header);
812                         continue;
813                 }
814                 width = strlen(se->se_header);
815                 if (symbol_conf.col_width_list_str) {
816                         if (col_width) {
817                                 hists__set_col_len(self, se->se_width_idx,
818                                                    atoi(col_width));
819                                 col_width = strchr(col_width, ',');
820                                 if (col_width)
821                                         ++col_width;
822                         }
823                 }
824                 if (!hists__new_col_len(self, se->se_width_idx, width))
825                         width = hists__col_len(self, se->se_width_idx);
826                 fprintf(fp, "  %*s", width, se->se_header);
827         }
828         fprintf(fp, "\n");
829
830         if (sep)
831                 goto print_entries;
832
833         fprintf(fp, "# ........");
834         if (symbol_conf.show_nr_samples)
835                 fprintf(fp, " ..........");
836         if (pair) {
837                 fprintf(fp, " ..........");
838                 if (show_displacement)
839                         fprintf(fp, " .....");
840         }
841         list_for_each_entry(se, &hist_entry__sort_list, list) {
842                 unsigned int i;
843
844                 if (se->elide)
845                         continue;
846
847                 fprintf(fp, "  ");
848                 width = hists__col_len(self, se->se_width_idx);
849                 if (width == 0)
850                         width = strlen(se->se_header);
851                 for (i = 0; i < width; i++)
852                         fprintf(fp, ".");
853         }
854
855         fprintf(fp, "\n#\n");
856
857 print_entries:
858         for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
859                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
860
861                 if (show_displacement) {
862                         if (h->pair != NULL)
863                                 displacement = ((long)h->pair->position -
864                                                 (long)position);
865                         else
866                                 displacement = 0;
867                         ++position;
868                 }
869                 ret += hist_entry__fprintf(h, self, pair, show_displacement,
870                                            displacement, fp, self->stats.total_period);
871
872                 if (symbol_conf.use_callchain)
873                         ret += hist_entry__fprintf_callchain(h, self, fp,
874                                                              self->stats.total_period);
875                 if (h->ms.map == NULL && verbose > 1) {
876                         __map_groups__fprintf_maps(&h->thread->mg,
877                                                    MAP__FUNCTION, verbose, fp);
878                         fprintf(fp, "%.10s end\n", graph_dotted_line);
879                 }
880         }
881
882         free(rem_sq_bracket);
883
884         return ret;
885 }
886
887 /*
888  * See hists__fprintf to match the column widths
889  */
890 unsigned int hists__sort_list_width(struct hists *self)
891 {
892         struct sort_entry *se;
893         int ret = 9; /* total % */
894
895         if (symbol_conf.show_cpu_utilization) {
896                 ret += 7; /* count_sys % */
897                 ret += 6; /* count_us % */
898                 if (perf_guest) {
899                         ret += 13; /* count_guest_sys % */
900                         ret += 12; /* count_guest_us % */
901                 }
902         }
903
904         if (symbol_conf.show_nr_samples)
905                 ret += 11;
906
907         list_for_each_entry(se, &hist_entry__sort_list, list)
908                 if (!se->elide)
909                         ret += 2 + hists__col_len(self, se->se_width_idx);
910
911         if (verbose) /* Addr + origin */
912                 ret += 3 + BITS_PER_LONG / 4;
913
914         return ret;
915 }
916
917 static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
918                                        enum hist_filter filter)
919 {
920         h->filtered &= ~(1 << filter);
921         if (h->filtered)
922                 return;
923
924         ++self->nr_entries;
925         if (h->ms.unfolded)
926                 self->nr_entries += h->nr_rows;
927         h->row_offset = 0;
928         self->stats.total_period += h->period;
929         self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
930
931         hists__calc_col_len(self, h);
932 }
933
934 void hists__filter_by_dso(struct hists *self, const struct dso *dso)
935 {
936         struct rb_node *nd;
937
938         self->nr_entries = self->stats.total_period = 0;
939         self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
940         hists__reset_col_len(self);
941
942         for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
943                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
944
945                 if (symbol_conf.exclude_other && !h->parent)
946                         continue;
947
948                 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
949                         h->filtered |= (1 << HIST_FILTER__DSO);
950                         continue;
951                 }
952
953                 hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
954         }
955 }
956
957 void hists__filter_by_thread(struct hists *self, const struct thread *thread)
958 {
959         struct rb_node *nd;
960
961         self->nr_entries = self->stats.total_period = 0;
962         self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
963         hists__reset_col_len(self);
964
965         for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
966                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
967
968                 if (thread != NULL && h->thread != thread) {
969                         h->filtered |= (1 << HIST_FILTER__THREAD);
970                         continue;
971                 }
972
973                 hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
974         }
975 }
976
977 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
978 {
979         return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
980 }
981
982 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
983 {
984         return symbol__annotate(he->ms.sym, he->ms.map, privsize);
985 }
986
987 void hists__inc_nr_events(struct hists *self, u32 type)
988 {
989         ++self->stats.nr_events[0];
990         ++self->stats.nr_events[type];
991 }
992
993 size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
994 {
995         int i;
996         size_t ret = 0;
997
998         for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
999                 const char *name;
1000
1001                 if (self->stats.nr_events[i] == 0)
1002                         continue;
1003
1004                 name = perf_event__name(i);
1005                 if (!strcmp(name, "UNKNOWN"))
1006                         continue;
1007
1008                 ret += fprintf(fp, "%16s events: %10d\n", name,
1009                                self->stats.nr_events[i]);
1010         }
1011
1012         return ret;
1013 }