blob: 6094562c95232f60bb5493ed23839b83d65d655d [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 Kim4fb71072013-01-22 18:09:34 +090011typedef int (*hpp_snprint_fn)(char *buf, size_t size, const char *fmt, ...);
Namhyung Kimea251d52012-09-03 11:53:06 +090012
Jiri Olsa0c5268b2013-02-04 13:32:55 +010013static 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)
Namhyung Kimea251d52012-09-03 11:53:06 +090017{
Namhyung Kim4fb71072013-01-22 18:09:34 +090018 int ret;
Jiri Olsab5ff71c2012-10-04 21:49:40 +090019 struct hists *hists = he->hists;
Namhyung Kim759ff492013-03-05 14:53:26 +090020 struct perf_evsel *evsel = hists_to_evsel(hists);
Namhyung Kimea251d52012-09-03 11:53:06 +090021
Jiri Olsa0c5268b2013-02-04 13:32:55 +010022 if (fmt_percent) {
23 double percent = 0.0;
Namhyung Kim4fb71072013-01-22 18:09:34 +090024
Jiri Olsa0c5268b2013-02-04 13:32:55 +010025 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));
Namhyung Kim5b9e2142013-01-22 18:09:37 +090032
Namhyung Kim759ff492013-03-05 14:53:26 +090033 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kim5b9e2142013-01-22 18:09:37 +090034 int prev_idx, idx_delta;
Namhyung Kim5b9e2142013-01-22 18:09:37 +090035 struct hist_entry *pair;
36 int nr_members = evsel->nr_members;
37
Namhyung Kim5b9e2142013-01-22 18:09:37 +090038 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 */
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090055 if (fmt_percent) {
56 ret += print_fn(hpp->buf + ret,
57 hpp->size - ret,
58 fmt, 0.0);
59 } else {
60 ret += print_fn(hpp->buf + ret,
61 hpp->size - ret,
62 fmt, 0ULL);
63 }
Namhyung Kim5b9e2142013-01-22 18:09:37 +090064 }
65
Jiri Olsa0c5268b2013-02-04 13:32:55 +010066 if (fmt_percent)
67 ret += print_fn(hpp->buf + ret, hpp->size - ret,
68 fmt, 100.0 * period / total);
69 else
70 ret += print_fn(hpp->buf + ret, hpp->size - ret,
71 fmt, period);
Namhyung Kim5b9e2142013-01-22 18:09:37 +090072
73 prev_idx = perf_evsel__group_idx(evsel);
74 }
75
76 idx_delta = nr_members - prev_idx - 1;
77
78 while (idx_delta--) {
79 /*
80 * zero-fill group members at last which have no sample
81 */
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090082 if (fmt_percent) {
83 ret += print_fn(hpp->buf + ret, hpp->size - ret,
84 fmt, 0.0);
85 } else {
86 ret += print_fn(hpp->buf + ret, hpp->size - ret,
87 fmt, 0ULL);
88 }
Namhyung Kim5b9e2142013-01-22 18:09:37 +090089 }
90 }
Namhyung Kim4fb71072013-01-22 18:09:34 +090091 return ret;
Namhyung Kimea251d52012-09-03 11:53:06 +090092}
93
Namhyung Kim4fb71072013-01-22 18:09:34 +090094#define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +010095static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
96 struct perf_hpp *hpp) \
Namhyung Kim4fb71072013-01-22 18:09:34 +090097{ \
98 int len = _min_width; \
99 \
Namhyung Kim5b9e2142013-01-22 18:09:37 +0900100 if (symbol_conf.event_group) { \
101 struct perf_evsel *evsel = hpp->ptr; \
102 \
103 len = max(len, evsel->nr_members * _unit_width); \
104 } \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900105 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900106}
107
Namhyung Kim4fb71072013-01-22 18:09:34 +0900108#define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100109static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
110 struct perf_hpp *hpp __maybe_unused) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900111{ \
112 int len = _min_width; \
113 \
Namhyung Kim5b9e2142013-01-22 18:09:37 +0900114 if (symbol_conf.event_group) { \
115 struct perf_evsel *evsel = hpp->ptr; \
116 \
117 len = max(len, evsel->nr_members * _unit_width); \
118 } \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900119 return len; \
Namhyung Kimea251d52012-09-03 11:53:06 +0900120}
121
Namhyung Kim4fb71072013-01-22 18:09:34 +0900122#define __HPP_COLOR_PERCENT_FN(_type, _field) \
123static u64 he_get_##_field(struct hist_entry *he) \
124{ \
125 return he->stat._field; \
126} \
127 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100128static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
129 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900130{ \
Jiri Olsa0c5268b2013-02-04 13:32:55 +0100131 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \
Michael Hudson-Doyle53805ec2013-10-31 16:47:45 -0700132 percent_color_snprintf, true); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900133}
134
Namhyung Kim4fb71072013-01-22 18:09:34 +0900135#define __HPP_ENTRY_PERCENT_FN(_type, _field) \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100136static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
137 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900138{ \
139 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
Jiri Olsa0c5268b2013-02-04 13:32:55 +0100140 return __hpp__fmt(hpp, he, he_get_##_field, fmt, \
141 scnprintf, true); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900142}
143
Namhyung Kim4fb71072013-01-22 18:09:34 +0900144#define __HPP_ENTRY_RAW_FN(_type, _field) \
145static u64 he_get_raw_##_field(struct hist_entry *he) \
146{ \
147 return he->stat._field; \
148} \
149 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100150static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
151 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900152{ \
153 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
Jiri Olsa0c5268b2013-02-04 13:32:55 +0100154 return __hpp__fmt(hpp, he, he_get_raw_##_field, fmt, scnprintf, false); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900155}
156
Namhyung Kim4fb71072013-01-22 18:09:34 +0900157#define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \
158__HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
159__HPP_WIDTH_FN(_type, _min_width, _unit_width) \
160__HPP_COLOR_PERCENT_FN(_type, _field) \
161__HPP_ENTRY_PERCENT_FN(_type, _field)
Namhyung Kimea251d52012-09-03 11:53:06 +0900162
Namhyung Kim4fb71072013-01-22 18:09:34 +0900163#define HPP_RAW_FNS(_type, _str, _field, _min_width, _unit_width) \
164__HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
165__HPP_WIDTH_FN(_type, _min_width, _unit_width) \
166__HPP_ENTRY_RAW_FN(_type, _field)
Jiri Olsab5ff71c2012-10-04 21:49:40 +0900167
Namhyung Kimea251d52012-09-03 11:53:06 +0900168
Namhyung Kim4fb71072013-01-22 18:09:34 +0900169HPP_PERCENT_FNS(overhead, "Overhead", period, 8, 8)
170HPP_PERCENT_FNS(overhead_sys, "sys", period_sys, 8, 8)
171HPP_PERCENT_FNS(overhead_us, "usr", period_us, 8, 8)
172HPP_PERCENT_FNS(overhead_guest_sys, "guest sys", period_guest_sys, 9, 8)
173HPP_PERCENT_FNS(overhead_guest_us, "guest usr", period_guest_us, 9, 8)
Namhyung Kim9ffad982012-09-03 11:53:07 +0900174
Namhyung Kim4fb71072013-01-22 18:09:34 +0900175HPP_RAW_FNS(samples, "Samples", nr_events, 12, 12)
176HPP_RAW_FNS(period, "Period", period, 12, 12)
Namhyung Kimea251d52012-09-03 11:53:06 +0900177
Jiri Olsa12400052012-10-13 00:06:16 +0200178#define HPP__COLOR_PRINT_FNS(_name) \
179 { \
180 .header = hpp__header_ ## _name, \
181 .width = hpp__width_ ## _name, \
182 .color = hpp__color_ ## _name, \
183 .entry = hpp__entry_ ## _name \
184 }
Namhyung Kimea251d52012-09-03 11:53:06 +0900185
Jiri Olsa12400052012-10-13 00:06:16 +0200186#define HPP__PRINT_FNS(_name) \
187 { \
188 .header = hpp__header_ ## _name, \
189 .width = hpp__width_ ## _name, \
190 .entry = hpp__entry_ ## _name \
191 }
Namhyung Kimea251d52012-09-03 11:53:06 +0900192
193struct perf_hpp_fmt perf_hpp__format[] = {
Jiri Olsa12400052012-10-13 00:06:16 +0200194 HPP__COLOR_PRINT_FNS(overhead),
195 HPP__COLOR_PRINT_FNS(overhead_sys),
196 HPP__COLOR_PRINT_FNS(overhead_us),
197 HPP__COLOR_PRINT_FNS(overhead_guest_sys),
198 HPP__COLOR_PRINT_FNS(overhead_guest_us),
199 HPP__PRINT_FNS(samples),
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100200 HPP__PRINT_FNS(period)
Namhyung Kimea251d52012-09-03 11:53:06 +0900201};
202
Jiri Olsa12400052012-10-13 00:06:16 +0200203LIST_HEAD(perf_hpp__list);
204
Namhyung Kim4fb71072013-01-22 18:09:34 +0900205
Namhyung Kimea251d52012-09-03 11:53:06 +0900206#undef HPP__COLOR_PRINT_FNS
207#undef HPP__PRINT_FNS
208
Namhyung Kim4fb71072013-01-22 18:09:34 +0900209#undef HPP_PERCENT_FNS
210#undef HPP_RAW_FNS
211
212#undef __HPP_HEADER_FN
213#undef __HPP_WIDTH_FN
214#undef __HPP_COLOR_PERCENT_FN
215#undef __HPP_ENTRY_PERCENT_FN
216#undef __HPP_ENTRY_RAW_FN
217
218
Jiri Olsa1d778222012-10-04 21:49:39 +0900219void perf_hpp__init(void)
Namhyung Kimea251d52012-09-03 11:53:06 +0900220{
Jiri Olsa2b8bfa62013-01-31 23:34:25 +0100221 perf_hpp__column_enable(PERF_HPP__OVERHEAD);
222
Namhyung Kimea251d52012-09-03 11:53:06 +0900223 if (symbol_conf.show_cpu_utilization) {
Jiri Olsa12400052012-10-13 00:06:16 +0200224 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
225 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
Namhyung Kimea251d52012-09-03 11:53:06 +0900226
227 if (perf_guest) {
Jiri Olsa12400052012-10-13 00:06:16 +0200228 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
229 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
Namhyung Kimea251d52012-09-03 11:53:06 +0900230 }
231 }
232
233 if (symbol_conf.show_nr_samples)
Jiri Olsa12400052012-10-13 00:06:16 +0200234 perf_hpp__column_enable(PERF_HPP__SAMPLES);
Namhyung Kimea251d52012-09-03 11:53:06 +0900235
236 if (symbol_conf.show_total_period)
Jiri Olsa12400052012-10-13 00:06:16 +0200237 perf_hpp__column_enable(PERF_HPP__PERIOD);
Jiri Olsa1d778222012-10-04 21:49:39 +0900238}
Namhyung Kimea251d52012-09-03 11:53:06 +0900239
Jiri Olsa12400052012-10-13 00:06:16 +0200240void perf_hpp__column_register(struct perf_hpp_fmt *format)
241{
242 list_add_tail(&format->list, &perf_hpp__list);
243}
244
245void perf_hpp__column_enable(unsigned col)
Jiri Olsa1d778222012-10-04 21:49:39 +0900246{
247 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa12400052012-10-13 00:06:16 +0200248 perf_hpp__column_register(&perf_hpp__format[col]);
Namhyung Kimea251d52012-09-03 11:53:06 +0900249}
250
Namhyung Kimea251d52012-09-03 11:53:06 +0900251int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
252 struct hists *hists)
253{
254 const char *sep = symbol_conf.field_sep;
255 struct sort_entry *se;
256 int ret = 0;
257
258 list_for_each_entry(se, &hist_entry__sort_list, list) {
259 if (se->elide)
260 continue;
261
262 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
263 ret += se->se_snprintf(he, s + ret, size - ret,
264 hists__col_len(hists, se->se_width_idx));
265 }
266
267 return ret;
268}
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900269
270/*
271 * See hists__fprintf to match the column widths
272 */
273unsigned int hists__sort_list_width(struct hists *hists)
274{
Jiri Olsa12400052012-10-13 00:06:16 +0200275 struct perf_hpp_fmt *fmt;
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900276 struct sort_entry *se;
Jiri Olsa12400052012-10-13 00:06:16 +0200277 int i = 0, ret = 0;
Namhyung Kim5b9e2142013-01-22 18:09:37 +0900278 struct perf_hpp dummy_hpp = {
279 .ptr = hists_to_evsel(hists),
280 };
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900281
Jiri Olsa12400052012-10-13 00:06:16 +0200282 perf_hpp__for_each_format(fmt) {
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900283 if (i)
284 ret += 2;
285
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100286 ret += fmt->width(fmt, &dummy_hpp);
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900287 }
288
289 list_for_each_entry(se, &hist_entry__sort_list, list)
290 if (!se->elide)
291 ret += 2 + hists__col_len(hists, se->se_width_idx);
292
293 if (verbose) /* Addr + origin */
294 ret += 3 + BITS_PER_LONG / 4;
295
296 return ret;
297}