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