blob: c877c52ff4bc0560b1848ee43d723324fa996aae [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 Kim5b591662014-07-31 14:47:38 +090018static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
19 hpp_field_fn get_field, const char *fmt, int len,
20 hpp_snprint_fn print_fn, bool fmt_percent)
Namhyung Kimea251d52012-09-03 11:53:06 +090021{
Namhyung Kimfb821c9e2014-03-03 17:05:19 +090022 int ret;
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
Jiri Olsa0c5268b2013-02-04 13:32:55 +010028 if (fmt_percent) {
29 double percent = 0.0;
Namhyung Kimf2148332014-01-14 11:52:48 +090030 u64 total = hists__total_period(hists);
Namhyung Kim4fb71072013-01-22 18:09:34 +090031
Namhyung Kimf2148332014-01-14 11:52:48 +090032 if (total)
33 percent = 100.0 * get_field(he) / total;
Jiri Olsa0c5268b2013-02-04 13:32:55 +010034
Namhyung Kimd6751072014-07-31 14:47:36 +090035 ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
Jiri Olsa0c5268b2013-02-04 13:32:55 +010036 } else
Namhyung Kimd6751072014-07-31 14:47:36 +090037 ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he));
Namhyung Kim5b9e2142013-01-22 18:09:37 +090038
Namhyung Kim759ff492013-03-05 14:53:26 +090039 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kim5b9e2142013-01-22 18:09:37 +090040 int prev_idx, idx_delta;
Namhyung Kim5b9e2142013-01-22 18:09:37 +090041 struct hist_entry *pair;
42 int nr_members = evsel->nr_members;
43
Namhyung Kim5b9e2142013-01-22 18:09:37 +090044 prev_idx = perf_evsel__group_idx(evsel);
45
46 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
47 u64 period = get_field(pair);
Namhyung Kimf2148332014-01-14 11:52:48 +090048 u64 total = hists__total_period(pair->hists);
Namhyung Kim5b9e2142013-01-22 18:09:37 +090049
50 if (!total)
51 continue;
52
53 evsel = hists_to_evsel(pair->hists);
54 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
55
56 while (idx_delta--) {
57 /*
58 * zero-fill group members in the middle which
59 * have no sample
60 */
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090061 if (fmt_percent) {
Namhyung Kima0088ad2014-03-03 10:14:04 +090062 ret += hpp__call_print_fn(hpp, print_fn,
Namhyung Kimd6751072014-07-31 14:47:36 +090063 fmt, len, 0.0);
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090064 } else {
Namhyung Kima0088ad2014-03-03 10:14:04 +090065 ret += hpp__call_print_fn(hpp, print_fn,
Namhyung Kimd6751072014-07-31 14:47:36 +090066 fmt, len, 0ULL);
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090067 }
Namhyung Kim5b9e2142013-01-22 18:09:37 +090068 }
69
Namhyung Kima0088ad2014-03-03 10:14:04 +090070 if (fmt_percent) {
Namhyung Kimd6751072014-07-31 14:47:36 +090071 ret += hpp__call_print_fn(hpp, print_fn, fmt, len,
Namhyung Kima0088ad2014-03-03 10:14:04 +090072 100.0 * period / total);
73 } else {
74 ret += hpp__call_print_fn(hpp, print_fn, fmt,
Namhyung Kimd6751072014-07-31 14:47:36 +090075 len, period);
Namhyung Kima0088ad2014-03-03 10:14:04 +090076 }
Namhyung Kim5b9e2142013-01-22 18:09:37 +090077
78 prev_idx = perf_evsel__group_idx(evsel);
79 }
80
81 idx_delta = nr_members - prev_idx - 1;
82
83 while (idx_delta--) {
84 /*
85 * zero-fill group members at last which have no sample
86 */
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090087 if (fmt_percent) {
Namhyung Kima0088ad2014-03-03 10:14:04 +090088 ret += hpp__call_print_fn(hpp, print_fn,
Namhyung Kimd6751072014-07-31 14:47:36 +090089 fmt, len, 0.0);
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090090 } else {
Namhyung Kima0088ad2014-03-03 10:14:04 +090091 ret += hpp__call_print_fn(hpp, print_fn,
Namhyung Kimd6751072014-07-31 14:47:36 +090092 fmt, len, 0ULL);
Namhyung Kim9b0d2fb2014-03-03 10:14:02 +090093 }
Namhyung Kim5b9e2142013-01-22 18:09:37 +090094 }
95 }
Namhyung Kima0088ad2014-03-03 10:14:04 +090096
97 /*
98 * Restore original buf and size as it's where caller expects
99 * the result will be saved.
100 */
101 hpp->buf = buf;
102 hpp->size = size;
103
Namhyung Kim4fb71072013-01-22 18:09:34 +0900104 return ret;
Namhyung Kimea251d52012-09-03 11:53:06 +0900105}
106
Namhyung Kim5b591662014-07-31 14:47:38 +0900107int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
108 struct hist_entry *he, hpp_field_fn get_field,
109 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900110{
Namhyung Kim5b591662014-07-31 14:47:38 +0900111 int len = fmt->user_len ?: fmt->len;
112
113 if (symbol_conf.field_sep) {
114 return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
115 print_fn, fmt_percent);
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900116 }
117
Namhyung Kim5b591662014-07-31 14:47:38 +0900118 if (fmt_percent)
119 len -= 2; /* 2 for a space and a % sign */
120 else
121 len -= 1;
122
123 return __hpp__fmt(hpp, he, get_field, fmtstr, len, print_fn, fmt_percent);
124}
125
126int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
127 struct hist_entry *he, hpp_field_fn get_field,
128 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
129{
130 if (!symbol_conf.cumulate_callchain) {
131 int len = fmt->user_len ?: fmt->len;
132 return snprintf(hpp->buf, hpp->size, " %*s", len - 1, "N/A");
133 }
134
135 return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmt_percent);
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900136}
137
Namhyung Kimf156d842014-03-03 14:14:03 +0900138static int field_cmp(u64 field_a, u64 field_b)
139{
140 if (field_a > field_b)
141 return 1;
142 if (field_a < field_b)
143 return -1;
144 return 0;
145}
146
147static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
148 hpp_field_fn get_field)
149{
150 s64 ret;
151 int i, nr_members;
152 struct perf_evsel *evsel;
153 struct hist_entry *pair;
154 u64 *fields_a, *fields_b;
155
156 ret = field_cmp(get_field(a), get_field(b));
157 if (ret || !symbol_conf.event_group)
158 return ret;
159
160 evsel = hists_to_evsel(a->hists);
161 if (!perf_evsel__is_group_event(evsel))
162 return ret;
163
164 nr_members = evsel->nr_members;
Arjun Sreedharane4e458b2014-12-06 17:10:43 +0530165 fields_a = calloc(nr_members, sizeof(*fields_a));
166 fields_b = calloc(nr_members, sizeof(*fields_b));
Namhyung Kimf156d842014-03-03 14:14:03 +0900167
168 if (!fields_a || !fields_b)
169 goto out;
170
171 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
172 evsel = hists_to_evsel(pair->hists);
173 fields_a[perf_evsel__group_idx(evsel)] = get_field(pair);
174 }
175
176 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
177 evsel = hists_to_evsel(pair->hists);
178 fields_b[perf_evsel__group_idx(evsel)] = get_field(pair);
179 }
180
181 for (i = 1; i < nr_members; i++) {
182 ret = field_cmp(fields_a[i], fields_b[i]);
183 if (ret)
184 break;
185 }
186
187out:
188 free(fields_a);
189 free(fields_b);
190
191 return ret;
192}
193
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900194static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
195 hpp_field_fn get_field)
196{
197 s64 ret = 0;
198
199 if (symbol_conf.cumulate_callchain) {
200 /*
201 * Put caller above callee when they have equal period.
202 */
203 ret = field_cmp(get_field(a), get_field(b));
204 if (ret)
205 return ret;
206
Namhyung Kim5ca82712014-12-23 13:36:21 +0900207 if (a->thread != b->thread || !symbol_conf.use_callchain)
208 return 0;
209
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900210 ret = b->callchain->max_depth - a->callchain->max_depth;
211 }
212 return ret;
213}
214
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900215static int hpp__width_fn(struct perf_hpp_fmt *fmt,
216 struct perf_hpp *hpp __maybe_unused,
217 struct perf_evsel *evsel)
218{
219 int len = fmt->user_len ?: fmt->len;
220
221 if (symbol_conf.event_group)
222 len = max(len, evsel->nr_members * fmt->len);
223
224 if (len < (int)strlen(fmt->name))
225 len = strlen(fmt->name);
226
227 return len;
Namhyung Kimea251d52012-09-03 11:53:06 +0900228}
229
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900230static int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
231 struct perf_evsel *evsel)
232{
233 int len = hpp__width_fn(fmt, hpp, evsel);
234 return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
Namhyung Kime0d66c72014-07-31 14:47:37 +0900235}
236
Namhyung Kima0088ad2014-03-03 10:14:04 +0900237static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
238{
239 va_list args;
240 ssize_t ssize = hpp->size;
241 double percent;
Namhyung Kimd6751072014-07-31 14:47:36 +0900242 int ret, len;
Namhyung Kima0088ad2014-03-03 10:14:04 +0900243
244 va_start(args, fmt);
Namhyung Kimd6751072014-07-31 14:47:36 +0900245 len = va_arg(args, int);
Namhyung Kima0088ad2014-03-03 10:14:04 +0900246 percent = va_arg(args, double);
Namhyung Kimd6751072014-07-31 14:47:36 +0900247 ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
Namhyung Kima0088ad2014-03-03 10:14:04 +0900248 va_end(args);
249
250 return (ret >= ssize) ? (ssize - 1) : ret;
251}
252
253static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
254{
255 va_list args;
256 ssize_t ssize = hpp->size;
257 int ret;
258
259 va_start(args, fmt);
260 ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
261 va_end(args);
262
263 return (ret >= ssize) ? (ssize - 1) : ret;
264}
265
Namhyung Kim4fb71072013-01-22 18:09:34 +0900266#define __HPP_COLOR_PERCENT_FN(_type, _field) \
267static u64 he_get_##_field(struct hist_entry *he) \
268{ \
269 return he->stat._field; \
270} \
271 \
Namhyung Kime0d66c72014-07-31 14:47:37 +0900272static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100273 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900274{ \
Namhyung Kim5b591662014-07-31 14:47:38 +0900275 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
276 hpp_color_scnprintf, true); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900277}
278
Namhyung Kim4fb71072013-01-22 18:09:34 +0900279#define __HPP_ENTRY_PERCENT_FN(_type, _field) \
Namhyung Kime0d66c72014-07-31 14:47:37 +0900280static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100281 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900282{ \
Namhyung Kim5b591662014-07-31 14:47:38 +0900283 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
284 hpp_entry_scnprintf, true); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900285}
286
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900287#define __HPP_SORT_FN(_type, _field) \
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900288static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
289 struct hist_entry *a, struct hist_entry *b) \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900290{ \
Namhyung Kimf156d842014-03-03 14:14:03 +0900291 return __hpp__sort(a, b, he_get_##_field); \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900292}
293
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900294#define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
295static u64 he_get_acc_##_field(struct hist_entry *he) \
296{ \
297 return he->stat_acc->_field; \
298} \
299 \
Namhyung Kime0d66c72014-07-31 14:47:37 +0900300static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900301 struct perf_hpp *hpp, struct hist_entry *he) \
302{ \
Namhyung Kim5b591662014-07-31 14:47:38 +0900303 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
304 hpp_color_scnprintf, true); \
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900305}
306
307#define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
Namhyung Kime0d66c72014-07-31 14:47:37 +0900308static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900309 struct perf_hpp *hpp, struct hist_entry *he) \
310{ \
Namhyung Kim2bfa1522014-08-20 17:07:56 +0900311 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
Namhyung Kim5b591662014-07-31 14:47:38 +0900312 hpp_entry_scnprintf, true); \
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900313}
314
315#define __HPP_SORT_ACC_FN(_type, _field) \
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900316static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
317 struct hist_entry *a, struct hist_entry *b) \
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900318{ \
319 return __hpp__sort_acc(a, b, he_get_acc_##_field); \
320}
321
Namhyung Kim4fb71072013-01-22 18:09:34 +0900322#define __HPP_ENTRY_RAW_FN(_type, _field) \
323static u64 he_get_raw_##_field(struct hist_entry *he) \
324{ \
325 return he->stat._field; \
326} \
327 \
Namhyung Kime0d66c72014-07-31 14:47:37 +0900328static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100329 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900330{ \
Namhyung Kim5b591662014-07-31 14:47:38 +0900331 return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, \
332 hpp_entry_scnprintf, false); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900333}
334
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900335#define __HPP_SORT_RAW_FN(_type, _field) \
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900336static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
337 struct hist_entry *a, struct hist_entry *b) \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900338{ \
Namhyung Kimf156d842014-03-03 14:14:03 +0900339 return __hpp__sort(a, b, he_get_raw_##_field); \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900340}
341
342
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900343#define HPP_PERCENT_FNS(_type, _field) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900344__HPP_COLOR_PERCENT_FN(_type, _field) \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900345__HPP_ENTRY_PERCENT_FN(_type, _field) \
346__HPP_SORT_FN(_type, _field)
Namhyung Kimea251d52012-09-03 11:53:06 +0900347
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900348#define HPP_PERCENT_ACC_FNS(_type, _field) \
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900349__HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
350__HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
351__HPP_SORT_ACC_FN(_type, _field)
352
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900353#define HPP_RAW_FNS(_type, _field) \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900354__HPP_ENTRY_RAW_FN(_type, _field) \
355__HPP_SORT_RAW_FN(_type, _field)
Jiri Olsab5ff71c2012-10-04 21:49:40 +0900356
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900357HPP_PERCENT_FNS(overhead, period)
358HPP_PERCENT_FNS(overhead_sys, period_sys)
359HPP_PERCENT_FNS(overhead_us, period_us)
360HPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys)
361HPP_PERCENT_FNS(overhead_guest_us, period_guest_us)
362HPP_PERCENT_ACC_FNS(overhead_acc, period)
Namhyung Kimea251d52012-09-03 11:53:06 +0900363
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900364HPP_RAW_FNS(samples, nr_events)
365HPP_RAW_FNS(period, period)
Namhyung Kimea251d52012-09-03 11:53:06 +0900366
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900367static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
368 struct hist_entry *a __maybe_unused,
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900369 struct hist_entry *b __maybe_unused)
370{
371 return 0;
372}
373
Jiri Olsac0020ef2016-01-18 10:24:04 +0100374static bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt *a)
375{
376 return a->header == hpp__header_fn;
377}
378
379static bool hpp__equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
380{
381 if (!perf_hpp__is_hpp_entry(a) || !perf_hpp__is_hpp_entry(b))
382 return false;
383
384 return a->idx == b->idx;
385}
386
Jiri Olsab21a7632016-01-18 10:24:01 +0100387#define HPP__COLOR_PRINT_FNS(_name, _fn, _idx) \
Jiri Olsa12400052012-10-13 00:06:16 +0200388 { \
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900389 .name = _name, \
390 .header = hpp__header_fn, \
391 .width = hpp__width_fn, \
392 .color = hpp__color_ ## _fn, \
393 .entry = hpp__entry_ ## _fn, \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900394 .cmp = hpp__nop_cmp, \
395 .collapse = hpp__nop_cmp, \
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900396 .sort = hpp__sort_ ## _fn, \
Jiri Olsab21a7632016-01-18 10:24:01 +0100397 .idx = PERF_HPP__ ## _idx, \
Jiri Olsac0020ef2016-01-18 10:24:04 +0100398 .equal = hpp__equal, \
Jiri Olsa12400052012-10-13 00:06:16 +0200399 }
Namhyung Kimea251d52012-09-03 11:53:06 +0900400
Jiri Olsab21a7632016-01-18 10:24:01 +0100401#define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx) \
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900402 { \
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900403 .name = _name, \
404 .header = hpp__header_fn, \
405 .width = hpp__width_fn, \
406 .color = hpp__color_ ## _fn, \
407 .entry = hpp__entry_ ## _fn, \
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900408 .cmp = hpp__nop_cmp, \
409 .collapse = hpp__nop_cmp, \
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900410 .sort = hpp__sort_ ## _fn, \
Jiri Olsab21a7632016-01-18 10:24:01 +0100411 .idx = PERF_HPP__ ## _idx, \
Jiri Olsac0020ef2016-01-18 10:24:04 +0100412 .equal = hpp__equal, \
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900413 }
414
Jiri Olsab21a7632016-01-18 10:24:01 +0100415#define HPP__PRINT_FNS(_name, _fn, _idx) \
Jiri Olsa12400052012-10-13 00:06:16 +0200416 { \
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900417 .name = _name, \
418 .header = hpp__header_fn, \
419 .width = hpp__width_fn, \
420 .entry = hpp__entry_ ## _fn, \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900421 .cmp = hpp__nop_cmp, \
422 .collapse = hpp__nop_cmp, \
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900423 .sort = hpp__sort_ ## _fn, \
Jiri Olsab21a7632016-01-18 10:24:01 +0100424 .idx = PERF_HPP__ ## _idx, \
Jiri Olsac0020ef2016-01-18 10:24:04 +0100425 .equal = hpp__equal, \
Jiri Olsa12400052012-10-13 00:06:16 +0200426 }
Namhyung Kimea251d52012-09-03 11:53:06 +0900427
428struct perf_hpp_fmt perf_hpp__format[] = {
Jiri Olsab21a7632016-01-18 10:24:01 +0100429 HPP__COLOR_PRINT_FNS("Overhead", overhead, OVERHEAD),
430 HPP__COLOR_PRINT_FNS("sys", overhead_sys, OVERHEAD_SYS),
431 HPP__COLOR_PRINT_FNS("usr", overhead_us, OVERHEAD_US),
432 HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys, OVERHEAD_GUEST_SYS),
433 HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us, OVERHEAD_GUEST_US),
434 HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc, OVERHEAD_ACC),
435 HPP__PRINT_FNS("Samples", samples, SAMPLES),
436 HPP__PRINT_FNS("Period", period, PERIOD)
Namhyung Kimea251d52012-09-03 11:53:06 +0900437};
438
Jiri Olsa12400052012-10-13 00:06:16 +0200439LIST_HEAD(perf_hpp__list);
Namhyung Kim8b536992014-03-03 11:46:55 +0900440LIST_HEAD(perf_hpp__sort_list);
Jiri Olsa12400052012-10-13 00:06:16 +0200441
Namhyung Kim4fb71072013-01-22 18:09:34 +0900442
Namhyung Kimea251d52012-09-03 11:53:06 +0900443#undef HPP__COLOR_PRINT_FNS
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900444#undef HPP__COLOR_ACC_PRINT_FNS
Namhyung Kimea251d52012-09-03 11:53:06 +0900445#undef HPP__PRINT_FNS
446
Namhyung Kim4fb71072013-01-22 18:09:34 +0900447#undef HPP_PERCENT_FNS
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900448#undef HPP_PERCENT_ACC_FNS
Namhyung Kim4fb71072013-01-22 18:09:34 +0900449#undef HPP_RAW_FNS
450
451#undef __HPP_HEADER_FN
452#undef __HPP_WIDTH_FN
453#undef __HPP_COLOR_PERCENT_FN
454#undef __HPP_ENTRY_PERCENT_FN
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900455#undef __HPP_COLOR_ACC_PERCENT_FN
456#undef __HPP_ENTRY_ACC_PERCENT_FN
Namhyung Kim4fb71072013-01-22 18:09:34 +0900457#undef __HPP_ENTRY_RAW_FN
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900458#undef __HPP_SORT_FN
459#undef __HPP_SORT_ACC_FN
460#undef __HPP_SORT_RAW_FN
Namhyung Kim4fb71072013-01-22 18:09:34 +0900461
462
Jiri Olsa1d778222012-10-04 21:49:39 +0900463void perf_hpp__init(void)
Namhyung Kimea251d52012-09-03 11:53:06 +0900464{
Namhyung Kim26d8b332014-03-03 16:16:20 +0900465 int i;
466
467 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
Namhyung Kima2ce0672014-03-04 09:06:42 +0900468 struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
469
470 INIT_LIST_HEAD(&fmt->list);
471
472 /* sort_list may be linked by setup_sorting() */
473 if (fmt->sort_list.next == NULL)
474 INIT_LIST_HEAD(&fmt->sort_list);
Namhyung Kim26d8b332014-03-03 16:16:20 +0900475 }
476
Namhyung Kima7d945b2014-03-04 10:46:34 +0900477 /*
478 * If user specified field order, no need to setup default fields.
479 */
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +0200480 if (is_strict_order(field_order))
Namhyung Kima7d945b2014-03-04 10:46:34 +0900481 return;
482
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900483 if (symbol_conf.cumulate_callchain) {
Jiri Olsa1178bfd2015-10-06 14:25:12 +0200484 hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC);
Namhyung Kim1ecd4452014-07-31 14:47:40 +0900485 perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900486 }
487
Jiri Olsa1178bfd2015-10-06 14:25:12 +0200488 hpp_dimension__add_output(PERF_HPP__OVERHEAD);
Jiri Olsa2b8bfa62013-01-31 23:34:25 +0100489
Namhyung Kimea251d52012-09-03 11:53:06 +0900490 if (symbol_conf.show_cpu_utilization) {
Jiri Olsa1178bfd2015-10-06 14:25:12 +0200491 hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS);
492 hpp_dimension__add_output(PERF_HPP__OVERHEAD_US);
Namhyung Kimea251d52012-09-03 11:53:06 +0900493
494 if (perf_guest) {
Jiri Olsa1178bfd2015-10-06 14:25:12 +0200495 hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS);
496 hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US);
Namhyung Kimea251d52012-09-03 11:53:06 +0900497 }
498 }
499
500 if (symbol_conf.show_nr_samples)
Jiri Olsa1178bfd2015-10-06 14:25:12 +0200501 hpp_dimension__add_output(PERF_HPP__SAMPLES);
Namhyung Kimea251d52012-09-03 11:53:06 +0900502
503 if (symbol_conf.show_total_period)
Jiri Olsa1178bfd2015-10-06 14:25:12 +0200504 hpp_dimension__add_output(PERF_HPP__PERIOD);
Jiri Olsa1d778222012-10-04 21:49:39 +0900505}
Namhyung Kimea251d52012-09-03 11:53:06 +0900506
Jiri Olsa12400052012-10-13 00:06:16 +0200507void perf_hpp__column_register(struct perf_hpp_fmt *format)
508{
509 list_add_tail(&format->list, &perf_hpp__list);
510}
511
Namhyung Kim77284de2013-12-16 16:55:13 +0900512void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
513{
514 list_del(&format->list);
515}
516
Namhyung Kim8b536992014-03-03 11:46:55 +0900517void perf_hpp__register_sort_field(struct perf_hpp_fmt *format)
518{
519 list_add_tail(&format->sort_list, &perf_hpp__sort_list);
520}
521
Jiri Olsa12400052012-10-13 00:06:16 +0200522void perf_hpp__column_enable(unsigned col)
Jiri Olsa1d778222012-10-04 21:49:39 +0900523{
524 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa12400052012-10-13 00:06:16 +0200525 perf_hpp__column_register(&perf_hpp__format[col]);
Namhyung Kimea251d52012-09-03 11:53:06 +0900526}
527
Namhyung Kim77284de2013-12-16 16:55:13 +0900528void perf_hpp__column_disable(unsigned col)
529{
530 BUG_ON(col >= PERF_HPP__MAX_INDEX);
531 perf_hpp__column_unregister(&perf_hpp__format[col]);
532}
533
534void perf_hpp__cancel_cumulate(void)
535{
Jiri Olsa1945c3e2016-01-18 10:24:07 +0100536 struct perf_hpp_fmt *fmt, *acc, *ovh, *tmp;
537
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +0200538 if (is_strict_order(field_order))
Namhyung Kim2bf1a122014-03-20 09:10:29 +0900539 return;
540
Jiri Olsa1945c3e2016-01-18 10:24:07 +0100541 ovh = &perf_hpp__format[PERF_HPP__OVERHEAD];
542 acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC];
543
544 perf_hpp__for_each_format_safe(fmt, tmp) {
545 if (acc->equal(acc, fmt)) {
546 perf_hpp__column_unregister(fmt);
547 continue;
548 }
549
550 if (ovh->equal(ovh, fmt))
551 fmt->name = "Overhead";
552 }
Namhyung Kim77284de2013-12-16 16:55:13 +0900553}
554
Jiri Olsa97358082016-01-18 10:24:03 +0100555static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
556{
557 return a->equal && a->equal(a, b);
558}
559
Namhyung Kim26d8b332014-03-03 16:16:20 +0900560void perf_hpp__setup_output_field(void)
561{
562 struct perf_hpp_fmt *fmt;
563
564 /* append sort keys to output field */
565 perf_hpp__for_each_sort_list(fmt) {
Jiri Olsa3f931f22016-01-18 10:24:05 +0100566 struct perf_hpp_fmt *pos;
Namhyung Kima7d945b2014-03-04 10:46:34 +0900567
Jiri Olsa3f931f22016-01-18 10:24:05 +0100568 perf_hpp__for_each_format(pos) {
569 if (fmt_equal(fmt, pos))
570 goto next;
Namhyung Kima7d945b2014-03-04 10:46:34 +0900571 }
572
573 perf_hpp__column_register(fmt);
574next:
575 continue;
576 }
577}
578
579void perf_hpp__append_sort_keys(void)
580{
581 struct perf_hpp_fmt *fmt;
582
583 /* append output fields to sort keys */
584 perf_hpp__for_each_format(fmt) {
Jiri Olsa3f931f22016-01-18 10:24:05 +0100585 struct perf_hpp_fmt *pos;
Namhyung Kima7d945b2014-03-04 10:46:34 +0900586
Jiri Olsa3f931f22016-01-18 10:24:05 +0100587 perf_hpp__for_each_sort_list(pos) {
588 if (fmt_equal(fmt, pos))
589 goto next;
Namhyung Kima7d945b2014-03-04 10:46:34 +0900590 }
591
592 perf_hpp__register_sort_field(fmt);
593next:
594 continue;
Namhyung Kim26d8b332014-03-03 16:16:20 +0900595 }
596}
597
Namhyung Kim1c89fe92014-05-07 18:42:24 +0900598void perf_hpp__reset_output_field(void)
599{
600 struct perf_hpp_fmt *fmt, *tmp;
601
602 /* reset output fields */
603 perf_hpp__for_each_format_safe(fmt, tmp) {
604 list_del_init(&fmt->list);
605 list_del_init(&fmt->sort_list);
606 }
607
608 /* reset sort keys */
609 perf_hpp__for_each_sort_list_safe(fmt, tmp) {
610 list_del_init(&fmt->list);
611 list_del_init(&fmt->sort_list);
612 }
613}
614
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900615/*
616 * See hists__fprintf to match the column widths
617 */
618unsigned int hists__sort_list_width(struct hists *hists)
619{
Jiri Olsa12400052012-10-13 00:06:16 +0200620 struct perf_hpp_fmt *fmt;
Namhyung Kimcfaa1542014-05-19 14:19:30 +0900621 int ret = 0;
622 bool first = true;
Namhyung Kim94a07932014-03-10 16:43:52 +0900623 struct perf_hpp dummy_hpp;
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900624
Jiri Olsa12400052012-10-13 00:06:16 +0200625 perf_hpp__for_each_format(fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +0900626 if (perf_hpp__should_skip(fmt, hists))
Namhyung Kimcfaa1542014-05-19 14:19:30 +0900627 continue;
628
629 if (first)
630 first = false;
631 else
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900632 ret += 2;
633
Namhyung Kim94a07932014-03-10 16:43:52 +0900634 ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900635 }
636
Namhyung Kimcfaa1542014-05-19 14:19:30 +0900637 if (verbose && sort__has_sym) /* Addr + origin */
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900638 ret += 3 + BITS_PER_LONG / 4;
639
640 return ret;
641}
Namhyung Kime0d66c72014-07-31 14:47:37 +0900642
643void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
644{
Namhyung Kime0d66c72014-07-31 14:47:37 +0900645 if (perf_hpp__is_sort_entry(fmt))
646 return perf_hpp__reset_sort_width(fmt, hists);
647
Jiri Olsa2e8b79e2016-01-18 10:24:02 +0100648 BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX);
Namhyung Kime0d66c72014-07-31 14:47:37 +0900649
Jiri Olsa2e8b79e2016-01-18 10:24:02 +0100650 switch (fmt->idx) {
Namhyung Kime0d66c72014-07-31 14:47:37 +0900651 case PERF_HPP__OVERHEAD:
652 case PERF_HPP__OVERHEAD_SYS:
653 case PERF_HPP__OVERHEAD_US:
654 case PERF_HPP__OVERHEAD_ACC:
655 fmt->len = 8;
656 break;
657
658 case PERF_HPP__OVERHEAD_GUEST_SYS:
659 case PERF_HPP__OVERHEAD_GUEST_US:
660 fmt->len = 9;
661 break;
662
663 case PERF_HPP__SAMPLES:
664 case PERF_HPP__PERIOD:
665 fmt->len = 12;
666 break;
667
668 default:
669 break;
670 }
671}
Namhyung Kim5b591662014-07-31 14:47:38 +0900672
673void perf_hpp__set_user_width(const char *width_list_str)
674{
675 struct perf_hpp_fmt *fmt;
676 const char *ptr = width_list_str;
677
678 perf_hpp__for_each_format(fmt) {
679 char *p;
680
681 int len = strtol(ptr, &p, 10);
682 fmt->user_len = len;
683
684 if (*p == ',')
685 ptr = p + 1;
686 else
687 break;
688 }
689}