blob: 0f403b83e9d1c1da19b7be850c7c49ffba0b074d [file] [log] [blame]
Namhyung Kimea251d52012-09-03 11:53:06 +09001#include <math.h>
Jiri Olsa2c5d4b42013-01-31 23:31:11 +01002#include <linux/compiler.h>
Namhyung Kimea251d52012-09-03 11:53:06 +09003
4#include "../util/hist.h"
5#include "../util/util.h"
6#include "../util/sort.h"
Namhyung Kim4fb71072013-01-22 18:09:34 +09007#include "../util/evsel.h"
Namhyung Kimea251d52012-09-03 11:53:06 +09008
9/* hist period print (hpp) functions */
Namhyung Kimea251d52012-09-03 11:53:06 +090010
Namhyung Kima0088ad2014-03-03 10:14:04 +090011#define hpp__call_print_fn(hpp, fn, fmt, ...) \
12({ \
13 int __ret = fn(hpp, fmt, ##__VA_ARGS__); \
14 advance_hpp(hpp, __ret); \
15 __ret; \
16})
17
Namhyung Kim4a621092014-03-03 10:14:03 +090018int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
Namhyung Kim2f6d9002014-03-03 10:14:05 +090019 hpp_field_fn get_field, hpp_callback_fn callback,
Namhyung Kim4a621092014-03-03 10:14:03 +090020 const char *fmt, hpp_snprint_fn print_fn, bool fmt_percent)
Namhyung Kimea251d52012-09-03 11:53:06 +090021{
Namhyung Kim2f6d9002014-03-03 10:14:05 +090022 int ret = 0;
Jiri Olsab5ff71c2012-10-04 21:49:40 +090023 struct hists *hists = he->hists;
Namhyung Kim759ff492013-03-05 14:53:26 +090024 struct perf_evsel *evsel = hists_to_evsel(hists);
Namhyung Kima0088ad2014-03-03 10:14:04 +090025 char *buf = hpp->buf;
26 size_t size = hpp->size;
Namhyung Kimea251d52012-09-03 11:53:06 +090027
Namhyung Kim2f6d9002014-03-03 10:14:05 +090028 if (callback) {
29 ret = callback(hpp, true);
30 advance_hpp(hpp, ret);
31 }
32
Jiri Olsa0c5268b2013-02-04 13:32:55 +010033 if (fmt_percent) {
34 double percent = 0.0;
Namhyung Kim4fb71072013-01-22 18:09:34 +090035
Jiri Olsa0c5268b2013-02-04 13:32:55 +010036 if (hists->stats.total_period)
37 percent = 100.0 * get_field(he) /
38 hists->stats.total_period;
39
Namhyung Kim2f6d9002014-03-03 10:14:05 +090040 ret += hpp__call_print_fn(hpp, print_fn, fmt, percent);
Jiri Olsa0c5268b2013-02-04 13:32:55 +010041 } else
Namhyung Kim2f6d9002014-03-03 10:14:05 +090042 ret += hpp__call_print_fn(hpp, print_fn, fmt, get_field(he));
Namhyung Kim5b9e2142013-01-22 18:09:37 +090043
Namhyung Kim759ff492013-03-05 14:53:26 +090044 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kim5b9e2142013-01-22 18:09:37 +090045 int prev_idx, idx_delta;
Namhyung Kim5b9e2142013-01-22 18:09:37 +090046 struct hist_entry *pair;
47 int nr_members = evsel->nr_members;
48
Namhyung Kim5b9e2142013-01-22 18:09:37 +090049 prev_idx = perf_evsel__group_idx(evsel);
50
51 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
52 u64 period = get_field(pair);
53 u64 total = pair->hists->stats.total_period;
54
55 if (!total)
56 continue;
57
58 evsel = hists_to_evsel(pair->hists);
59 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
60
61 while (idx_delta--) {
62 /*
63 * zero-fill group members in the middle which
64 * have no sample
65 */
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090066 if (fmt_percent) {
Namhyung Kima0088ad2014-03-03 10:14:04 +090067 ret += hpp__call_print_fn(hpp, print_fn,
68 fmt, 0.0);
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090069 } else {
Namhyung Kima0088ad2014-03-03 10:14:04 +090070 ret += hpp__call_print_fn(hpp, print_fn,
71 fmt, 0ULL);
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090072 }
Namhyung Kim5b9e2142013-01-22 18:09:37 +090073 }
74
Namhyung Kima0088ad2014-03-03 10:14:04 +090075 if (fmt_percent) {
76 ret += hpp__call_print_fn(hpp, print_fn, fmt,
77 100.0 * period / total);
78 } else {
79 ret += hpp__call_print_fn(hpp, print_fn, fmt,
80 period);
81 }
Namhyung Kim5b9e2142013-01-22 18:09:37 +090082
83 prev_idx = perf_evsel__group_idx(evsel);
84 }
85
86 idx_delta = nr_members - prev_idx - 1;
87
88 while (idx_delta--) {
89 /*
90 * zero-fill group members at last which have no sample
91 */
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090092 if (fmt_percent) {
Namhyung Kima0088ad2014-03-03 10:14:04 +090093 ret += hpp__call_print_fn(hpp, print_fn,
94 fmt, 0.0);
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090095 } else {
Namhyung Kima0088ad2014-03-03 10:14:04 +090096 ret += hpp__call_print_fn(hpp, print_fn,
97 fmt, 0ULL);
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090098 }
Namhyung Kim5b9e2142013-01-22 18:09:37 +090099 }
100 }
Namhyung Kima0088ad2014-03-03 10:14:04 +0900101
Namhyung Kim2f6d9002014-03-03 10:14:05 +0900102 if (callback) {
103 int __ret = callback(hpp, false);
104
105 advance_hpp(hpp, __ret);
106 ret += __ret;
107 }
108
Namhyung Kima0088ad2014-03-03 10:14:04 +0900109 /*
110 * Restore original buf and size as it's where caller expects
111 * the result will be saved.
112 */
113 hpp->buf = buf;
114 hpp->size = size;
115
Namhyung Kim4fb71072013-01-22 18:09:34 +0900116 return ret;
Namhyung Kimea251d52012-09-03 11:53:06 +0900117}
118
Namhyung Kim4fb71072013-01-22 18:09:34 +0900119#define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100120static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
Namhyung Kim94a07932014-03-10 16:43:52 +0900121 struct perf_hpp *hpp, \
122 struct perf_evsel *evsel) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900123{ \
124 int len = _min_width; \
125 \
Namhyung Kim94a07932014-03-10 16:43:52 +0900126 if (symbol_conf.event_group) \
Namhyung Kim5b9e2142013-01-22 18:09:37 +0900127 len = max(len, evsel->nr_members * _unit_width); \
Namhyung Kim94a07932014-03-10 16:43:52 +0900128 \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900129 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900130}
131
Namhyung Kim4fb71072013-01-22 18:09:34 +0900132#define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100133static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
Namhyung Kim94a07932014-03-10 16:43:52 +0900134 struct perf_hpp *hpp __maybe_unused, \
135 struct perf_evsel *evsel) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900136{ \
137 int len = _min_width; \
138 \
Namhyung Kim94a07932014-03-10 16:43:52 +0900139 if (symbol_conf.event_group) \
Namhyung Kim5b9e2142013-01-22 18:09:37 +0900140 len = max(len, evsel->nr_members * _unit_width); \
Namhyung Kim94a07932014-03-10 16:43:52 +0900141 \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900142 return len; \
Namhyung Kimea251d52012-09-03 11:53:06 +0900143}
144
Namhyung Kima0088ad2014-03-03 10:14:04 +0900145static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
146{
147 va_list args;
148 ssize_t ssize = hpp->size;
149 double percent;
150 int ret;
151
152 va_start(args, fmt);
153 percent = va_arg(args, double);
154 ret = value_color_snprintf(hpp->buf, hpp->size, fmt, percent);
155 va_end(args);
156
157 return (ret >= ssize) ? (ssize - 1) : ret;
158}
159
160static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
161{
162 va_list args;
163 ssize_t ssize = hpp->size;
164 int ret;
165
166 va_start(args, fmt);
167 ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
168 va_end(args);
169
170 return (ret >= ssize) ? (ssize - 1) : ret;
171}
172
Namhyung Kim4fb71072013-01-22 18:09:34 +0900173#define __HPP_COLOR_PERCENT_FN(_type, _field) \
174static u64 he_get_##_field(struct hist_entry *he) \
175{ \
176 return he->stat._field; \
177} \
178 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100179static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
180 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900181{ \
Namhyung Kim2f6d9002014-03-03 10:14:05 +0900182 return __hpp__fmt(hpp, he, he_get_##_field, NULL, " %6.2f%%", \
Namhyung Kima0088ad2014-03-03 10:14:04 +0900183 hpp_color_scnprintf, true); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900184}
185
Namhyung Kim4fb71072013-01-22 18:09:34 +0900186#define __HPP_ENTRY_PERCENT_FN(_type, _field) \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100187static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
188 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900189{ \
190 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
Namhyung Kim2f6d9002014-03-03 10:14:05 +0900191 return __hpp__fmt(hpp, he, he_get_##_field, NULL, fmt, \
Namhyung Kima0088ad2014-03-03 10:14:04 +0900192 hpp_entry_scnprintf, true); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900193}
194
Namhyung Kim4fb71072013-01-22 18:09:34 +0900195#define __HPP_ENTRY_RAW_FN(_type, _field) \
196static u64 he_get_raw_##_field(struct hist_entry *he) \
197{ \
198 return he->stat._field; \
199} \
200 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100201static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
202 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900203{ \
204 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
Namhyung Kim2f6d9002014-03-03 10:14:05 +0900205 return __hpp__fmt(hpp, he, he_get_raw_##_field, NULL, fmt, \
Namhyung Kima0088ad2014-03-03 10:14:04 +0900206 hpp_entry_scnprintf, false); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900207}
208
Namhyung Kim4fb71072013-01-22 18:09:34 +0900209#define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \
210__HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
211__HPP_WIDTH_FN(_type, _min_width, _unit_width) \
212__HPP_COLOR_PERCENT_FN(_type, _field) \
213__HPP_ENTRY_PERCENT_FN(_type, _field)
Namhyung Kimea251d52012-09-03 11:53:06 +0900214
Namhyung Kim4fb71072013-01-22 18:09:34 +0900215#define HPP_RAW_FNS(_type, _str, _field, _min_width, _unit_width) \
216__HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
217__HPP_WIDTH_FN(_type, _min_width, _unit_width) \
218__HPP_ENTRY_RAW_FN(_type, _field)
Jiri Olsab5ff71c2012-10-04 21:49:40 +0900219
Namhyung Kimea251d52012-09-03 11:53:06 +0900220
Namhyung Kim4fb71072013-01-22 18:09:34 +0900221HPP_PERCENT_FNS(overhead, "Overhead", period, 8, 8)
222HPP_PERCENT_FNS(overhead_sys, "sys", period_sys, 8, 8)
223HPP_PERCENT_FNS(overhead_us, "usr", period_us, 8, 8)
224HPP_PERCENT_FNS(overhead_guest_sys, "guest sys", period_guest_sys, 9, 8)
225HPP_PERCENT_FNS(overhead_guest_us, "guest usr", period_guest_us, 9, 8)
Namhyung Kim9ffad982012-09-03 11:53:07 +0900226
Namhyung Kim4fb71072013-01-22 18:09:34 +0900227HPP_RAW_FNS(samples, "Samples", nr_events, 12, 12)
228HPP_RAW_FNS(period, "Period", period, 12, 12)
Namhyung Kimea251d52012-09-03 11:53:06 +0900229
Jiri Olsa12400052012-10-13 00:06:16 +0200230#define HPP__COLOR_PRINT_FNS(_name) \
231 { \
232 .header = hpp__header_ ## _name, \
233 .width = hpp__width_ ## _name, \
234 .color = hpp__color_ ## _name, \
235 .entry = hpp__entry_ ## _name \
236 }
Namhyung Kimea251d52012-09-03 11:53:06 +0900237
Jiri Olsa12400052012-10-13 00:06:16 +0200238#define HPP__PRINT_FNS(_name) \
239 { \
240 .header = hpp__header_ ## _name, \
241 .width = hpp__width_ ## _name, \
242 .entry = hpp__entry_ ## _name \
243 }
Namhyung Kimea251d52012-09-03 11:53:06 +0900244
245struct perf_hpp_fmt perf_hpp__format[] = {
Jiri Olsa12400052012-10-13 00:06:16 +0200246 HPP__COLOR_PRINT_FNS(overhead),
247 HPP__COLOR_PRINT_FNS(overhead_sys),
248 HPP__COLOR_PRINT_FNS(overhead_us),
249 HPP__COLOR_PRINT_FNS(overhead_guest_sys),
250 HPP__COLOR_PRINT_FNS(overhead_guest_us),
251 HPP__PRINT_FNS(samples),
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100252 HPP__PRINT_FNS(period)
Namhyung Kimea251d52012-09-03 11:53:06 +0900253};
254
Jiri Olsa12400052012-10-13 00:06:16 +0200255LIST_HEAD(perf_hpp__list);
256
Namhyung Kim4fb71072013-01-22 18:09:34 +0900257
Namhyung Kimea251d52012-09-03 11:53:06 +0900258#undef HPP__COLOR_PRINT_FNS
259#undef HPP__PRINT_FNS
260
Namhyung Kim4fb71072013-01-22 18:09:34 +0900261#undef HPP_PERCENT_FNS
262#undef HPP_RAW_FNS
263
264#undef __HPP_HEADER_FN
265#undef __HPP_WIDTH_FN
266#undef __HPP_COLOR_PERCENT_FN
267#undef __HPP_ENTRY_PERCENT_FN
268#undef __HPP_ENTRY_RAW_FN
269
270
Jiri Olsa1d778222012-10-04 21:49:39 +0900271void perf_hpp__init(void)
Namhyung Kimea251d52012-09-03 11:53:06 +0900272{
Jiri Olsa2b8bfa62013-01-31 23:34:25 +0100273 perf_hpp__column_enable(PERF_HPP__OVERHEAD);
274
Namhyung Kimea251d52012-09-03 11:53:06 +0900275 if (symbol_conf.show_cpu_utilization) {
Jiri Olsa12400052012-10-13 00:06:16 +0200276 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
277 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
Namhyung Kimea251d52012-09-03 11:53:06 +0900278
279 if (perf_guest) {
Jiri Olsa12400052012-10-13 00:06:16 +0200280 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
281 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
Namhyung Kimea251d52012-09-03 11:53:06 +0900282 }
283 }
284
285 if (symbol_conf.show_nr_samples)
Jiri Olsa12400052012-10-13 00:06:16 +0200286 perf_hpp__column_enable(PERF_HPP__SAMPLES);
Namhyung Kimea251d52012-09-03 11:53:06 +0900287
288 if (symbol_conf.show_total_period)
Jiri Olsa12400052012-10-13 00:06:16 +0200289 perf_hpp__column_enable(PERF_HPP__PERIOD);
Jiri Olsa1d778222012-10-04 21:49:39 +0900290}
Namhyung Kimea251d52012-09-03 11:53:06 +0900291
Jiri Olsa12400052012-10-13 00:06:16 +0200292void perf_hpp__column_register(struct perf_hpp_fmt *format)
293{
294 list_add_tail(&format->list, &perf_hpp__list);
295}
296
297void perf_hpp__column_enable(unsigned col)
Jiri Olsa1d778222012-10-04 21:49:39 +0900298{
299 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa12400052012-10-13 00:06:16 +0200300 perf_hpp__column_register(&perf_hpp__format[col]);
Namhyung Kimea251d52012-09-03 11:53:06 +0900301}
302
Namhyung Kimea251d52012-09-03 11:53:06 +0900303int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
304 struct hists *hists)
305{
306 const char *sep = symbol_conf.field_sep;
307 struct sort_entry *se;
308 int ret = 0;
309
310 list_for_each_entry(se, &hist_entry__sort_list, list) {
311 if (se->elide)
312 continue;
313
314 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
315 ret += se->se_snprintf(he, s + ret, size - ret,
316 hists__col_len(hists, se->se_width_idx));
317 }
318
319 return ret;
320}
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900321
322/*
323 * See hists__fprintf to match the column widths
324 */
325unsigned int hists__sort_list_width(struct hists *hists)
326{
Jiri Olsa12400052012-10-13 00:06:16 +0200327 struct perf_hpp_fmt *fmt;
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900328 struct sort_entry *se;
Jiri Olsa12400052012-10-13 00:06:16 +0200329 int i = 0, ret = 0;
Namhyung Kim94a07932014-03-10 16:43:52 +0900330 struct perf_hpp dummy_hpp;
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900331
Jiri Olsa12400052012-10-13 00:06:16 +0200332 perf_hpp__for_each_format(fmt) {
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900333 if (i)
334 ret += 2;
335
Namhyung Kim94a07932014-03-10 16:43:52 +0900336 ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900337 }
338
339 list_for_each_entry(se, &hist_entry__sort_list, list)
340 if (!se->elide)
341 ret += 2 + hists__col_len(hists, se->se_width_idx);
342
343 if (verbose) /* Addr + origin */
344 ret += 3 + BITS_PER_LONG / 4;
345
346 return ret;
347}