]> Pileus Git - ~andy/linux/blob - tools/perf/ui/hist.c
perf diff: Switching the base hists to be pairs head
[~andy/linux] / tools / perf / ui / hist.c
1 #include <math.h>
2 #include <linux/compiler.h>
3
4 #include "../util/hist.h"
5 #include "../util/util.h"
6 #include "../util/sort.h"
7 #include "../util/evsel.h"
8
9 /* hist period print (hpp) functions */
10
11 typedef int (*hpp_snprint_fn)(char *buf, size_t size, const char *fmt, ...);
12
13 static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
14                       u64 (*get_field)(struct hist_entry *),
15                       const char *fmt, hpp_snprint_fn print_fn,
16                       bool fmt_percent)
17 {
18         int ret;
19         struct hists *hists = he->hists;
20         struct perf_evsel *evsel = hists_to_evsel(hists);
21
22         if (fmt_percent) {
23                 double percent = 0.0;
24
25                 if (hists->stats.total_period)
26                         percent = 100.0 * get_field(he) /
27                                   hists->stats.total_period;
28
29                 ret = print_fn(hpp->buf, hpp->size, fmt, percent);
30         } else
31                 ret = print_fn(hpp->buf, hpp->size, fmt, get_field(he));
32
33         if (perf_evsel__is_group_event(evsel)) {
34                 int prev_idx, idx_delta;
35                 struct hist_entry *pair;
36                 int nr_members = evsel->nr_members;
37
38                 prev_idx = perf_evsel__group_idx(evsel);
39
40                 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
41                         u64 period = get_field(pair);
42                         u64 total = pair->hists->stats.total_period;
43
44                         if (!total)
45                                 continue;
46
47                         evsel = hists_to_evsel(pair->hists);
48                         idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
49
50                         while (idx_delta--) {
51                                 /*
52                                  * zero-fill group members in the middle which
53                                  * have no sample
54                                  */
55                                 ret += print_fn(hpp->buf + ret, hpp->size - ret,
56                                                 fmt, 0);
57                         }
58
59                         if (fmt_percent)
60                                 ret += print_fn(hpp->buf + ret, hpp->size - ret,
61                                                 fmt, 100.0 * period / total);
62                         else
63                                 ret += print_fn(hpp->buf + ret, hpp->size - ret,
64                                                 fmt, period);
65
66                         prev_idx = perf_evsel__group_idx(evsel);
67                 }
68
69                 idx_delta = nr_members - prev_idx - 1;
70
71                 while (idx_delta--) {
72                         /*
73                          * zero-fill group members at last which have no sample
74                          */
75                         ret += print_fn(hpp->buf + ret, hpp->size - ret,
76                                         fmt, 0);
77                 }
78         }
79         return ret;
80 }
81
82 #define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width)           \
83 static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
84                                struct perf_hpp *hpp)                    \
85 {                                                                       \
86         int len = _min_width;                                           \
87                                                                         \
88         if (symbol_conf.event_group) {                                  \
89                 struct perf_evsel *evsel = hpp->ptr;                    \
90                                                                         \
91                 len = max(len, evsel->nr_members * _unit_width);        \
92         }                                                               \
93         return scnprintf(hpp->buf, hpp->size, "%*s", len, _str);        \
94 }
95
96 #define __HPP_WIDTH_FN(_type, _min_width, _unit_width)                  \
97 static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused,  \
98                               struct perf_hpp *hpp __maybe_unused)      \
99 {                                                                       \
100         int len = _min_width;                                           \
101                                                                         \
102         if (symbol_conf.event_group) {                                  \
103                 struct perf_evsel *evsel = hpp->ptr;                    \
104                                                                         \
105                 len = max(len, evsel->nr_members * _unit_width);        \
106         }                                                               \
107         return len;                                                     \
108 }
109
110 #define __HPP_COLOR_PERCENT_FN(_type, _field)                                   \
111 static u64 he_get_##_field(struct hist_entry *he)                               \
112 {                                                                               \
113         return he->stat._field;                                                 \
114 }                                                                               \
115                                                                                 \
116 static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused,          \
117                               struct perf_hpp *hpp, struct hist_entry *he)      \
118 {                                                                               \
119         return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%",                 \
120                           (hpp_snprint_fn)percent_color_snprintf, true);        \
121 }
122
123 #define __HPP_ENTRY_PERCENT_FN(_type, _field)                                   \
124 static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused,         \
125                               struct perf_hpp *hpp, struct hist_entry *he)      \
126 {                                                                               \
127         const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%";         \
128         return __hpp__fmt(hpp, he, he_get_##_field, fmt,                        \
129                           scnprintf, true);                                     \
130 }
131
132 #define __HPP_ENTRY_RAW_FN(_type, _field)                                       \
133 static u64 he_get_raw_##_field(struct hist_entry *he)                           \
134 {                                                                               \
135         return he->stat._field;                                                 \
136 }                                                                               \
137                                                                                 \
138 static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused,         \
139                               struct perf_hpp *hpp, struct hist_entry *he)      \
140 {                                                                               \
141         const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64;    \
142         return __hpp__fmt(hpp, he, he_get_raw_##_field, fmt, scnprintf, false); \
143 }
144
145 #define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width)   \
146 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width)                   \
147 __HPP_WIDTH_FN(_type, _min_width, _unit_width)                          \
148 __HPP_COLOR_PERCENT_FN(_type, _field)                                   \
149 __HPP_ENTRY_PERCENT_FN(_type, _field)
150
151 #define HPP_RAW_FNS(_type, _str, _field, _min_width, _unit_width)       \
152 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width)                   \
153 __HPP_WIDTH_FN(_type, _min_width, _unit_width)                          \
154 __HPP_ENTRY_RAW_FN(_type, _field)
155
156
157 HPP_PERCENT_FNS(overhead, "Overhead", period, 8, 8)
158 HPP_PERCENT_FNS(overhead_sys, "sys", period_sys, 8, 8)
159 HPP_PERCENT_FNS(overhead_us, "usr", period_us, 8, 8)
160 HPP_PERCENT_FNS(overhead_guest_sys, "guest sys", period_guest_sys, 9, 8)
161 HPP_PERCENT_FNS(overhead_guest_us, "guest usr", period_guest_us, 9, 8)
162
163 HPP_RAW_FNS(samples, "Samples", nr_events, 12, 12)
164 HPP_RAW_FNS(period, "Period", period, 12, 12)
165
166
167 static int hpp__header_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
168                                 struct perf_hpp *hpp)
169 {
170         return scnprintf(hpp->buf, hpp->size, "Baseline");
171 }
172
173 static int hpp__width_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
174                                struct perf_hpp *hpp __maybe_unused)
175 {
176         return 8;
177 }
178
179 static double baseline_percent(struct hist_entry *he)
180 {
181         struct hists *hists = he->hists;
182         return 100.0 * he->stat.period / hists->stats.total_period;
183 }
184
185 static int hpp__color_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
186                                struct perf_hpp *hpp, struct hist_entry *he)
187 {
188         double percent = baseline_percent(he);
189
190         return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%",
191                                       percent);
192 }
193
194 static int hpp__entry_baseline(struct perf_hpp_fmt *_fmt __maybe_unused,
195                                struct perf_hpp *hpp, struct hist_entry *he)
196 {
197         double percent = baseline_percent(he);
198         const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
199
200         return scnprintf(hpp->buf, hpp->size, fmt, percent);
201 }
202
203 static int hpp__header_period_baseline(struct perf_hpp_fmt *_fmt __maybe_unused,
204                                        struct perf_hpp *hpp)
205 {
206         const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
207
208         return scnprintf(hpp->buf, hpp->size, fmt, "Period Base");
209 }
210
211 static int hpp__width_period_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
212                                       struct perf_hpp *hpp __maybe_unused)
213 {
214         return 12;
215 }
216
217 static int hpp__entry_period_baseline(struct perf_hpp_fmt *_fmt __maybe_unused,
218                                       struct perf_hpp *hpp, struct hist_entry *he)
219 {
220         struct hist_entry *pair = hist_entry__next_pair(he);
221         u64 period = pair ? pair->stat.period : 0;
222         const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
223
224         return scnprintf(hpp->buf, hpp->size, fmt, period);
225 }
226
227 static int hpp__header_delta(struct perf_hpp_fmt *_fmt __maybe_unused,
228                              struct perf_hpp *hpp)
229 {
230         const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
231
232         return scnprintf(hpp->buf, hpp->size, fmt, "Delta");
233 }
234
235 static int hpp__width_delta(struct perf_hpp_fmt *fmt __maybe_unused,
236                             struct perf_hpp *hpp __maybe_unused)
237 {
238         return 7;
239 }
240
241 static int hpp__entry_delta(struct perf_hpp_fmt *_fmt __maybe_unused,
242                             struct perf_hpp *hpp, struct hist_entry *he)
243 {
244         struct hist_entry *pair = hist_entry__next_pair(he);
245         const char *fmt = symbol_conf.field_sep ? "%s" : "%7.7s";
246         char buf[32] = " ";
247         double diff = 0.0;
248
249         if (pair) {
250                 if (he->diff.computed)
251                         diff = he->diff.period_ratio_delta;
252                 else
253                         diff = perf_diff__compute_delta(he, pair);
254         } else
255                 diff = perf_diff__period_percent(he, he->stat.period);
256
257         if (fabs(diff) >= 0.01)
258                 scnprintf(buf, sizeof(buf), "%+4.2F%%", diff);
259
260         return scnprintf(hpp->buf, hpp->size, fmt, buf);
261 }
262
263 static int hpp__header_ratio(struct perf_hpp_fmt *_fmt __maybe_unused,
264                              struct perf_hpp *hpp)
265 {
266         const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
267
268         return scnprintf(hpp->buf, hpp->size, fmt, "Ratio");
269 }
270
271 static int hpp__width_ratio(struct perf_hpp_fmt *fmt __maybe_unused,
272                             struct perf_hpp *hpp __maybe_unused)
273 {
274         return 14;
275 }
276
277 static int hpp__entry_ratio(struct perf_hpp_fmt *_fmt __maybe_unused,
278                             struct perf_hpp *hpp, struct hist_entry *he)
279 {
280         struct hist_entry *pair = hist_entry__next_pair(he);
281         const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
282         char buf[32] = " ";
283         double ratio = 0.0;
284
285         if (pair) {
286                 if (he->diff.computed)
287                         ratio = he->diff.period_ratio;
288                 else
289                         ratio = perf_diff__compute_ratio(he, pair);
290         }
291
292         if (ratio > 0.0)
293                 scnprintf(buf, sizeof(buf), "%+14.6F", ratio);
294
295         return scnprintf(hpp->buf, hpp->size, fmt, buf);
296 }
297
298 static int hpp__header_wdiff(struct perf_hpp_fmt *_fmt __maybe_unused,
299                              struct perf_hpp *hpp)
300 {
301         const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
302
303         return scnprintf(hpp->buf, hpp->size, fmt, "Weighted diff");
304 }
305
306 static int hpp__width_wdiff(struct perf_hpp_fmt *fmt __maybe_unused,
307                             struct perf_hpp *hpp __maybe_unused)
308 {
309         return 14;
310 }
311
312 static int hpp__entry_wdiff(struct perf_hpp_fmt *_fmt __maybe_unused,
313                             struct perf_hpp *hpp, struct hist_entry *he)
314 {
315         struct hist_entry *pair = hist_entry__next_pair(he);
316         const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
317         char buf[32] = " ";
318         s64 wdiff = 0;
319
320         if (pair) {
321                 if (he->diff.computed)
322                         wdiff = he->diff.wdiff;
323                 else
324                         wdiff = perf_diff__compute_wdiff(he, pair);
325         }
326
327         if (wdiff != 0)
328                 scnprintf(buf, sizeof(buf), "%14ld", wdiff);
329
330         return scnprintf(hpp->buf, hpp->size, fmt, buf);
331 }
332
333 static int hpp__header_formula(struct perf_hpp_fmt *_fmt __maybe_unused,
334                                struct perf_hpp *hpp)
335 {
336         const char *fmt = symbol_conf.field_sep ? "%s" : "%70s";
337
338         return scnprintf(hpp->buf, hpp->size, fmt, "Formula");
339 }
340
341 static int hpp__width_formula(struct perf_hpp_fmt *fmt __maybe_unused,
342                               struct perf_hpp *hpp __maybe_unused)
343 {
344         return 70;
345 }
346
347 static int hpp__entry_formula(struct perf_hpp_fmt *_fmt __maybe_unused,
348                               struct perf_hpp *hpp, struct hist_entry *he)
349 {
350         struct hist_entry *pair = hist_entry__next_pair(he);
351         const char *fmt = symbol_conf.field_sep ? "%s" : "%-70s";
352         char buf[96] = " ";
353
354         if (pair)
355                 perf_diff__formula(he, pair, buf, sizeof(buf));
356
357         return scnprintf(hpp->buf, hpp->size, fmt, buf);
358 }
359
360 #define HPP__COLOR_PRINT_FNS(_name)                     \
361         {                                               \
362                 .header = hpp__header_ ## _name,        \
363                 .width  = hpp__width_ ## _name,         \
364                 .color  = hpp__color_ ## _name,         \
365                 .entry  = hpp__entry_ ## _name          \
366         }
367
368 #define HPP__PRINT_FNS(_name)                           \
369         {                                               \
370                 .header = hpp__header_ ## _name,        \
371                 .width  = hpp__width_ ## _name,         \
372                 .entry  = hpp__entry_ ## _name          \
373         }
374
375 struct perf_hpp_fmt perf_hpp__format[] = {
376         HPP__COLOR_PRINT_FNS(baseline),
377         HPP__COLOR_PRINT_FNS(overhead),
378         HPP__COLOR_PRINT_FNS(overhead_sys),
379         HPP__COLOR_PRINT_FNS(overhead_us),
380         HPP__COLOR_PRINT_FNS(overhead_guest_sys),
381         HPP__COLOR_PRINT_FNS(overhead_guest_us),
382         HPP__PRINT_FNS(samples),
383         HPP__PRINT_FNS(period),
384         HPP__PRINT_FNS(period_baseline),
385         HPP__PRINT_FNS(delta),
386         HPP__PRINT_FNS(ratio),
387         HPP__PRINT_FNS(wdiff),
388         HPP__PRINT_FNS(formula)
389 };
390
391 LIST_HEAD(perf_hpp__list);
392
393
394 #undef HPP__COLOR_PRINT_FNS
395 #undef HPP__PRINT_FNS
396
397 #undef HPP_PERCENT_FNS
398 #undef HPP_RAW_FNS
399
400 #undef __HPP_HEADER_FN
401 #undef __HPP_WIDTH_FN
402 #undef __HPP_COLOR_PERCENT_FN
403 #undef __HPP_ENTRY_PERCENT_FN
404 #undef __HPP_ENTRY_RAW_FN
405
406
407 void perf_hpp__init(void)
408 {
409         perf_hpp__column_enable(PERF_HPP__OVERHEAD);
410
411         if (symbol_conf.show_cpu_utilization) {
412                 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
413                 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
414
415                 if (perf_guest) {
416                         perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
417                         perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
418                 }
419         }
420
421         if (symbol_conf.show_nr_samples)
422                 perf_hpp__column_enable(PERF_HPP__SAMPLES);
423
424         if (symbol_conf.show_total_period)
425                 perf_hpp__column_enable(PERF_HPP__PERIOD);
426 }
427
428 void perf_hpp__column_register(struct perf_hpp_fmt *format)
429 {
430         list_add_tail(&format->list, &perf_hpp__list);
431 }
432
433 void perf_hpp__column_enable(unsigned col)
434 {
435         BUG_ON(col >= PERF_HPP__MAX_INDEX);
436         perf_hpp__column_register(&perf_hpp__format[col]);
437 }
438
439 static inline void advance_hpp(struct perf_hpp *hpp, int inc)
440 {
441         hpp->buf  += inc;
442         hpp->size -= inc;
443 }
444
445 int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
446                                 bool color)
447 {
448         const char *sep = symbol_conf.field_sep;
449         struct perf_hpp_fmt *fmt;
450         char *start = hpp->buf;
451         int ret;
452         bool first = true;
453
454         if (symbol_conf.exclude_other && !he->parent)
455                 return 0;
456
457         perf_hpp__for_each_format(fmt) {
458                 /*
459                  * If there's no field_sep, we still need
460                  * to display initial '  '.
461                  */
462                 if (!sep || !first) {
463                         ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: "  ");
464                         advance_hpp(hpp, ret);
465                 } else
466                         first = false;
467
468                 if (color && fmt->color)
469                         ret = fmt->color(fmt, hpp, he);
470                 else
471                         ret = fmt->entry(fmt, hpp, he);
472
473                 advance_hpp(hpp, ret);
474         }
475
476         return hpp->buf - start;
477 }
478
479 int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
480                               struct hists *hists)
481 {
482         const char *sep = symbol_conf.field_sep;
483         struct sort_entry *se;
484         int ret = 0;
485
486         list_for_each_entry(se, &hist_entry__sort_list, list) {
487                 if (se->elide)
488                         continue;
489
490                 ret += scnprintf(s + ret, size - ret, "%s", sep ?: "  ");
491                 ret += se->se_snprintf(he, s + ret, size - ret,
492                                        hists__col_len(hists, se->se_width_idx));
493         }
494
495         return ret;
496 }
497
498 /*
499  * See hists__fprintf to match the column widths
500  */
501 unsigned int hists__sort_list_width(struct hists *hists)
502 {
503         struct perf_hpp_fmt *fmt;
504         struct sort_entry *se;
505         int i = 0, ret = 0;
506         struct perf_hpp dummy_hpp = {
507                 .ptr    = hists_to_evsel(hists),
508         };
509
510         perf_hpp__for_each_format(fmt) {
511                 if (i)
512                         ret += 2;
513
514                 ret += fmt->width(fmt, &dummy_hpp);
515         }
516
517         list_for_each_entry(se, &hist_entry__sort_list, list)
518                 if (!se->elide)
519                         ret += 2 + hists__col_len(hists, se->se_width_idx);
520
521         if (verbose) /* Addr + origin */
522                 ret += 3 + BITS_PER_LONG / 4;
523
524         return ret;
525 }