blob: 8ca638754acc8e37b4844609fbbc473d85dfc1e4 [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 Kimfb821c9e2014-03-03 17:05:19 +090019 hpp_field_fn get_field, const char *fmt,
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 Kimfb821c9e2014-03-03 17:05:19 +090035 ret = hpp__call_print_fn(hpp, print_fn, fmt, percent);
Jiri Olsa0c5268b2013-02-04 13:32:55 +010036 } else
Namhyung Kimfb821c9e2014-03-03 17:05:19 +090037 ret = hpp__call_print_fn(hpp, print_fn, fmt, 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,
63 fmt, 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,
66 fmt, 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) {
71 ret += hpp__call_print_fn(hpp, print_fn, fmt,
72 100.0 * period / total);
73 } else {
74 ret += hpp__call_print_fn(hpp, print_fn, fmt,
75 period);
76 }
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,
89 fmt, 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,
92 fmt, 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 Kim594dcbf2013-10-30 16:06:59 +0900107int __hpp__fmt_acc(struct perf_hpp *hpp, struct hist_entry *he,
108 hpp_field_fn get_field, const char *fmt,
109 hpp_snprint_fn print_fn, bool fmt_percent)
110{
111 if (!symbol_conf.cumulate_callchain) {
112 return snprintf(hpp->buf, hpp->size, "%*s",
113 fmt_percent ? 8 : 12, "N/A");
114 }
115
116 return __hpp__fmt(hpp, he, get_field, fmt, print_fn, fmt_percent);
117}
118
Namhyung Kimf156d842014-03-03 14:14:03 +0900119static int field_cmp(u64 field_a, u64 field_b)
120{
121 if (field_a > field_b)
122 return 1;
123 if (field_a < field_b)
124 return -1;
125 return 0;
126}
127
128static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
129 hpp_field_fn get_field)
130{
131 s64 ret;
132 int i, nr_members;
133 struct perf_evsel *evsel;
134 struct hist_entry *pair;
135 u64 *fields_a, *fields_b;
136
137 ret = field_cmp(get_field(a), get_field(b));
138 if (ret || !symbol_conf.event_group)
139 return ret;
140
141 evsel = hists_to_evsel(a->hists);
142 if (!perf_evsel__is_group_event(evsel))
143 return ret;
144
145 nr_members = evsel->nr_members;
146 fields_a = calloc(sizeof(*fields_a), nr_members);
147 fields_b = calloc(sizeof(*fields_b), nr_members);
148
149 if (!fields_a || !fields_b)
150 goto out;
151
152 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
153 evsel = hists_to_evsel(pair->hists);
154 fields_a[perf_evsel__group_idx(evsel)] = get_field(pair);
155 }
156
157 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
158 evsel = hists_to_evsel(pair->hists);
159 fields_b[perf_evsel__group_idx(evsel)] = get_field(pair);
160 }
161
162 for (i = 1; i < nr_members; i++) {
163 ret = field_cmp(fields_a[i], fields_b[i]);
164 if (ret)
165 break;
166 }
167
168out:
169 free(fields_a);
170 free(fields_b);
171
172 return ret;
173}
174
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900175static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
176 hpp_field_fn get_field)
177{
178 s64 ret = 0;
179
180 if (symbol_conf.cumulate_callchain) {
181 /*
182 * Put caller above callee when they have equal period.
183 */
184 ret = field_cmp(get_field(a), get_field(b));
185 if (ret)
186 return ret;
187
188 ret = b->callchain->max_depth - a->callchain->max_depth;
189 }
190 return ret;
191}
192
Namhyung Kim4fb71072013-01-22 18:09:34 +0900193#define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100194static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
Namhyung Kim94a07932014-03-10 16:43:52 +0900195 struct perf_hpp *hpp, \
196 struct perf_evsel *evsel) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900197{ \
198 int len = _min_width; \
199 \
Namhyung Kim94a07932014-03-10 16:43:52 +0900200 if (symbol_conf.event_group) \
Namhyung Kim5b9e2142013-01-22 18:09:37 +0900201 len = max(len, evsel->nr_members * _unit_width); \
Namhyung Kim94a07932014-03-10 16:43:52 +0900202 \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900203 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900204}
205
Namhyung Kim4fb71072013-01-22 18:09:34 +0900206#define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100207static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
Namhyung Kim94a07932014-03-10 16:43:52 +0900208 struct perf_hpp *hpp __maybe_unused, \
209 struct perf_evsel *evsel) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900210{ \
211 int len = _min_width; \
212 \
Namhyung Kim94a07932014-03-10 16:43:52 +0900213 if (symbol_conf.event_group) \
Namhyung Kim5b9e2142013-01-22 18:09:37 +0900214 len = max(len, evsel->nr_members * _unit_width); \
Namhyung Kim94a07932014-03-10 16:43:52 +0900215 \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900216 return len; \
Namhyung Kimea251d52012-09-03 11:53:06 +0900217}
218
Namhyung Kima0088ad2014-03-03 10:14:04 +0900219static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
220{
221 va_list args;
222 ssize_t ssize = hpp->size;
223 double percent;
224 int ret;
225
226 va_start(args, fmt);
227 percent = va_arg(args, double);
228 ret = value_color_snprintf(hpp->buf, hpp->size, fmt, percent);
229 va_end(args);
230
231 return (ret >= ssize) ? (ssize - 1) : ret;
232}
233
234static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
235{
236 va_list args;
237 ssize_t ssize = hpp->size;
238 int ret;
239
240 va_start(args, fmt);
241 ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
242 va_end(args);
243
244 return (ret >= ssize) ? (ssize - 1) : ret;
245}
246
Namhyung Kim4fb71072013-01-22 18:09:34 +0900247#define __HPP_COLOR_PERCENT_FN(_type, _field) \
248static u64 he_get_##_field(struct hist_entry *he) \
249{ \
250 return he->stat._field; \
251} \
252 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100253static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
254 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900255{ \
Namhyung Kimfb821c9e2014-03-03 17:05:19 +0900256 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \
Namhyung Kima0088ad2014-03-03 10:14:04 +0900257 hpp_color_scnprintf, true); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900258}
259
Namhyung Kim4fb71072013-01-22 18:09:34 +0900260#define __HPP_ENTRY_PERCENT_FN(_type, _field) \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100261static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
262 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900263{ \
264 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
Namhyung Kimfb821c9e2014-03-03 17:05:19 +0900265 return __hpp__fmt(hpp, he, he_get_##_field, fmt, \
Namhyung Kima0088ad2014-03-03 10:14:04 +0900266 hpp_entry_scnprintf, true); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900267}
268
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900269#define __HPP_SORT_FN(_type, _field) \
270static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
271{ \
Namhyung Kimf156d842014-03-03 14:14:03 +0900272 return __hpp__sort(a, b, he_get_##_field); \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900273}
274
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900275#define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
276static u64 he_get_acc_##_field(struct hist_entry *he) \
277{ \
278 return he->stat_acc->_field; \
279} \
280 \
281static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
282 struct perf_hpp *hpp, struct hist_entry *he) \
283{ \
284 return __hpp__fmt_acc(hpp, he, he_get_acc_##_field, " %6.2f%%", \
285 hpp_color_scnprintf, true); \
286}
287
288#define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
289static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
290 struct perf_hpp *hpp, struct hist_entry *he) \
291{ \
292 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
293 return __hpp__fmt_acc(hpp, he, he_get_acc_##_field, fmt, \
294 hpp_entry_scnprintf, true); \
295}
296
297#define __HPP_SORT_ACC_FN(_type, _field) \
298static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
299{ \
300 return __hpp__sort_acc(a, b, he_get_acc_##_field); \
301}
302
Namhyung Kim4fb71072013-01-22 18:09:34 +0900303#define __HPP_ENTRY_RAW_FN(_type, _field) \
304static u64 he_get_raw_##_field(struct hist_entry *he) \
305{ \
306 return he->stat._field; \
307} \
308 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100309static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
310 struct perf_hpp *hpp, struct hist_entry *he) \
Namhyung Kim4fb71072013-01-22 18:09:34 +0900311{ \
312 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
Namhyung Kimfb821c9e2014-03-03 17:05:19 +0900313 return __hpp__fmt(hpp, he, he_get_raw_##_field, fmt, \
Namhyung Kima0088ad2014-03-03 10:14:04 +0900314 hpp_entry_scnprintf, false); \
Namhyung Kimea251d52012-09-03 11:53:06 +0900315}
316
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900317#define __HPP_SORT_RAW_FN(_type, _field) \
318static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
319{ \
Namhyung Kimf156d842014-03-03 14:14:03 +0900320 return __hpp__sort(a, b, he_get_raw_##_field); \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900321}
322
323
Namhyung Kim4fb71072013-01-22 18:09:34 +0900324#define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \
325__HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
326__HPP_WIDTH_FN(_type, _min_width, _unit_width) \
327__HPP_COLOR_PERCENT_FN(_type, _field) \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900328__HPP_ENTRY_PERCENT_FN(_type, _field) \
329__HPP_SORT_FN(_type, _field)
Namhyung Kimea251d52012-09-03 11:53:06 +0900330
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900331#define HPP_PERCENT_ACC_FNS(_type, _str, _field, _min_width, _unit_width)\
332__HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
333__HPP_WIDTH_FN(_type, _min_width, _unit_width) \
334__HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
335__HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
336__HPP_SORT_ACC_FN(_type, _field)
337
Namhyung Kim4fb71072013-01-22 18:09:34 +0900338#define HPP_RAW_FNS(_type, _str, _field, _min_width, _unit_width) \
339__HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
340__HPP_WIDTH_FN(_type, _min_width, _unit_width) \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900341__HPP_ENTRY_RAW_FN(_type, _field) \
342__HPP_SORT_RAW_FN(_type, _field)
Jiri Olsab5ff71c2012-10-04 21:49:40 +0900343
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900344__HPP_HEADER_FN(overhead_self, "Self", 8, 8)
Namhyung Kimea251d52012-09-03 11:53:06 +0900345
Namhyung Kim4fb71072013-01-22 18:09:34 +0900346HPP_PERCENT_FNS(overhead, "Overhead", period, 8, 8)
347HPP_PERCENT_FNS(overhead_sys, "sys", period_sys, 8, 8)
348HPP_PERCENT_FNS(overhead_us, "usr", period_us, 8, 8)
349HPP_PERCENT_FNS(overhead_guest_sys, "guest sys", period_guest_sys, 9, 8)
350HPP_PERCENT_FNS(overhead_guest_us, "guest usr", period_guest_us, 9, 8)
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900351HPP_PERCENT_ACC_FNS(overhead_acc, "Children", period, 8, 8)
Namhyung Kim9ffad982012-09-03 11:53:07 +0900352
Namhyung Kim4fb71072013-01-22 18:09:34 +0900353HPP_RAW_FNS(samples, "Samples", nr_events, 12, 12)
354HPP_RAW_FNS(period, "Period", period, 12, 12)
Namhyung Kimea251d52012-09-03 11:53:06 +0900355
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900356static int64_t hpp__nop_cmp(struct hist_entry *a __maybe_unused,
357 struct hist_entry *b __maybe_unused)
358{
359 return 0;
360}
361
Jiri Olsa12400052012-10-13 00:06:16 +0200362#define HPP__COLOR_PRINT_FNS(_name) \
363 { \
364 .header = hpp__header_ ## _name, \
365 .width = hpp__width_ ## _name, \
366 .color = hpp__color_ ## _name, \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900367 .entry = hpp__entry_ ## _name, \
368 .cmp = hpp__nop_cmp, \
369 .collapse = hpp__nop_cmp, \
370 .sort = hpp__sort_ ## _name, \
Jiri Olsa12400052012-10-13 00:06:16 +0200371 }
Namhyung Kimea251d52012-09-03 11:53:06 +0900372
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900373#define HPP__COLOR_ACC_PRINT_FNS(_name) \
374 { \
375 .header = hpp__header_ ## _name, \
376 .width = hpp__width_ ## _name, \
377 .color = hpp__color_ ## _name, \
378 .entry = hpp__entry_ ## _name, \
379 .cmp = hpp__nop_cmp, \
380 .collapse = hpp__nop_cmp, \
381 .sort = hpp__sort_ ## _name, \
382 }
383
Jiri Olsa12400052012-10-13 00:06:16 +0200384#define HPP__PRINT_FNS(_name) \
385 { \
386 .header = hpp__header_ ## _name, \
387 .width = hpp__width_ ## _name, \
Namhyung Kimbc18b7f2014-03-03 10:59:57 +0900388 .entry = hpp__entry_ ## _name, \
389 .cmp = hpp__nop_cmp, \
390 .collapse = hpp__nop_cmp, \
391 .sort = hpp__sort_ ## _name, \
Jiri Olsa12400052012-10-13 00:06:16 +0200392 }
Namhyung Kimea251d52012-09-03 11:53:06 +0900393
394struct perf_hpp_fmt perf_hpp__format[] = {
Jiri Olsa12400052012-10-13 00:06:16 +0200395 HPP__COLOR_PRINT_FNS(overhead),
396 HPP__COLOR_PRINT_FNS(overhead_sys),
397 HPP__COLOR_PRINT_FNS(overhead_us),
398 HPP__COLOR_PRINT_FNS(overhead_guest_sys),
399 HPP__COLOR_PRINT_FNS(overhead_guest_us),
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900400 HPP__COLOR_ACC_PRINT_FNS(overhead_acc),
Jiri Olsa12400052012-10-13 00:06:16 +0200401 HPP__PRINT_FNS(samples),
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100402 HPP__PRINT_FNS(period)
Namhyung Kimea251d52012-09-03 11:53:06 +0900403};
404
Jiri Olsa12400052012-10-13 00:06:16 +0200405LIST_HEAD(perf_hpp__list);
Namhyung Kim8b536992014-03-03 11:46:55 +0900406LIST_HEAD(perf_hpp__sort_list);
Jiri Olsa12400052012-10-13 00:06:16 +0200407
Namhyung Kim4fb71072013-01-22 18:09:34 +0900408
Namhyung Kimea251d52012-09-03 11:53:06 +0900409#undef HPP__COLOR_PRINT_FNS
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900410#undef HPP__COLOR_ACC_PRINT_FNS
Namhyung Kimea251d52012-09-03 11:53:06 +0900411#undef HPP__PRINT_FNS
412
Namhyung Kim4fb71072013-01-22 18:09:34 +0900413#undef HPP_PERCENT_FNS
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900414#undef HPP_PERCENT_ACC_FNS
Namhyung Kim4fb71072013-01-22 18:09:34 +0900415#undef HPP_RAW_FNS
416
417#undef __HPP_HEADER_FN
418#undef __HPP_WIDTH_FN
419#undef __HPP_COLOR_PERCENT_FN
420#undef __HPP_ENTRY_PERCENT_FN
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900421#undef __HPP_COLOR_ACC_PERCENT_FN
422#undef __HPP_ENTRY_ACC_PERCENT_FN
Namhyung Kim4fb71072013-01-22 18:09:34 +0900423#undef __HPP_ENTRY_RAW_FN
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900424#undef __HPP_SORT_FN
425#undef __HPP_SORT_ACC_FN
426#undef __HPP_SORT_RAW_FN
Namhyung Kim4fb71072013-01-22 18:09:34 +0900427
428
Jiri Olsa1d778222012-10-04 21:49:39 +0900429void perf_hpp__init(void)
Namhyung Kimea251d52012-09-03 11:53:06 +0900430{
Namhyung Kim26d8b332014-03-03 16:16:20 +0900431 struct list_head *list;
432 int i;
433
434 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
Namhyung Kima2ce0672014-03-04 09:06:42 +0900435 struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
436
437 INIT_LIST_HEAD(&fmt->list);
438
439 /* sort_list may be linked by setup_sorting() */
440 if (fmt->sort_list.next == NULL)
441 INIT_LIST_HEAD(&fmt->sort_list);
Namhyung Kim26d8b332014-03-03 16:16:20 +0900442 }
443
Namhyung Kima7d945b2014-03-04 10:46:34 +0900444 /*
445 * If user specified field order, no need to setup default fields.
446 */
447 if (field_order)
448 return;
449
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900450 if (symbol_conf.cumulate_callchain) {
451 perf_hpp__column_enable(PERF_HPP__OVERHEAD_ACC);
452
453 perf_hpp__format[PERF_HPP__OVERHEAD].header =
454 hpp__header_overhead_self;
455 }
456
Jiri Olsa2b8bfa62013-01-31 23:34:25 +0100457 perf_hpp__column_enable(PERF_HPP__OVERHEAD);
458
Namhyung Kimea251d52012-09-03 11:53:06 +0900459 if (symbol_conf.show_cpu_utilization) {
Jiri Olsa12400052012-10-13 00:06:16 +0200460 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
461 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
Namhyung Kimea251d52012-09-03 11:53:06 +0900462
463 if (perf_guest) {
Jiri Olsa12400052012-10-13 00:06:16 +0200464 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
465 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
Namhyung Kimea251d52012-09-03 11:53:06 +0900466 }
467 }
468
469 if (symbol_conf.show_nr_samples)
Jiri Olsa12400052012-10-13 00:06:16 +0200470 perf_hpp__column_enable(PERF_HPP__SAMPLES);
Namhyung Kimea251d52012-09-03 11:53:06 +0900471
472 if (symbol_conf.show_total_period)
Jiri Olsa12400052012-10-13 00:06:16 +0200473 perf_hpp__column_enable(PERF_HPP__PERIOD);
Namhyung Kim26d8b332014-03-03 16:16:20 +0900474
475 /* prepend overhead field for backward compatiblity. */
476 list = &perf_hpp__format[PERF_HPP__OVERHEAD].sort_list;
477 if (list_empty(list))
478 list_add(list, &perf_hpp__sort_list);
Namhyung Kim594dcbf2013-10-30 16:06:59 +0900479
480 if (symbol_conf.cumulate_callchain) {
481 list = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC].sort_list;
482 if (list_empty(list))
483 list_add(list, &perf_hpp__sort_list);
484 }
Jiri Olsa1d778222012-10-04 21:49:39 +0900485}
Namhyung Kimea251d52012-09-03 11:53:06 +0900486
Jiri Olsa12400052012-10-13 00:06:16 +0200487void perf_hpp__column_register(struct perf_hpp_fmt *format)
488{
489 list_add_tail(&format->list, &perf_hpp__list);
490}
491
Namhyung Kim77284de2013-12-16 16:55:13 +0900492void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
493{
494 list_del(&format->list);
495}
496
Namhyung Kim8b536992014-03-03 11:46:55 +0900497void perf_hpp__register_sort_field(struct perf_hpp_fmt *format)
498{
499 list_add_tail(&format->sort_list, &perf_hpp__sort_list);
500}
501
Jiri Olsa12400052012-10-13 00:06:16 +0200502void perf_hpp__column_enable(unsigned col)
Jiri Olsa1d778222012-10-04 21:49:39 +0900503{
504 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa12400052012-10-13 00:06:16 +0200505 perf_hpp__column_register(&perf_hpp__format[col]);
Namhyung Kimea251d52012-09-03 11:53:06 +0900506}
507
Namhyung Kim77284de2013-12-16 16:55:13 +0900508void perf_hpp__column_disable(unsigned col)
509{
510 BUG_ON(col >= PERF_HPP__MAX_INDEX);
511 perf_hpp__column_unregister(&perf_hpp__format[col]);
512}
513
514void perf_hpp__cancel_cumulate(void)
515{
516 perf_hpp__column_disable(PERF_HPP__OVERHEAD_ACC);
517 perf_hpp__format[PERF_HPP__OVERHEAD].header = hpp__header_overhead;
518}
519
Namhyung Kim26d8b332014-03-03 16:16:20 +0900520void perf_hpp__setup_output_field(void)
521{
522 struct perf_hpp_fmt *fmt;
523
524 /* append sort keys to output field */
525 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kima7d945b2014-03-04 10:46:34 +0900526 if (!list_empty(&fmt->list))
527 continue;
528
529 /*
530 * sort entry fields are dynamically created,
531 * so they can share a same sort key even though
532 * the list is empty.
533 */
534 if (perf_hpp__is_sort_entry(fmt)) {
535 struct perf_hpp_fmt *pos;
536
537 perf_hpp__for_each_format(pos) {
538 if (perf_hpp__same_sort_entry(pos, fmt))
539 goto next;
540 }
541 }
542
543 perf_hpp__column_register(fmt);
544next:
545 continue;
546 }
547}
548
549void perf_hpp__append_sort_keys(void)
550{
551 struct perf_hpp_fmt *fmt;
552
553 /* append output fields to sort keys */
554 perf_hpp__for_each_format(fmt) {
555 if (!list_empty(&fmt->sort_list))
556 continue;
557
558 /*
559 * sort entry fields are dynamically created,
560 * so they can share a same sort key even though
561 * the list is empty.
562 */
563 if (perf_hpp__is_sort_entry(fmt)) {
564 struct perf_hpp_fmt *pos;
565
566 perf_hpp__for_each_sort_list(pos) {
567 if (perf_hpp__same_sort_entry(pos, fmt))
568 goto next;
569 }
570 }
571
572 perf_hpp__register_sort_field(fmt);
573next:
574 continue;
Namhyung Kim26d8b332014-03-03 16:16:20 +0900575 }
576}
577
Namhyung Kim1c89fe92014-05-07 18:42:24 +0900578void perf_hpp__reset_output_field(void)
579{
580 struct perf_hpp_fmt *fmt, *tmp;
581
582 /* reset output fields */
583 perf_hpp__for_each_format_safe(fmt, tmp) {
584 list_del_init(&fmt->list);
585 list_del_init(&fmt->sort_list);
586 }
587
588 /* reset sort keys */
589 perf_hpp__for_each_sort_list_safe(fmt, tmp) {
590 list_del_init(&fmt->list);
591 list_del_init(&fmt->sort_list);
592 }
593}
594
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900595/*
596 * See hists__fprintf to match the column widths
597 */
598unsigned int hists__sort_list_width(struct hists *hists)
599{
Jiri Olsa12400052012-10-13 00:06:16 +0200600 struct perf_hpp_fmt *fmt;
Namhyung Kimcfaa1542014-05-19 14:19:30 +0900601 int ret = 0;
602 bool first = true;
Namhyung Kim94a07932014-03-10 16:43:52 +0900603 struct perf_hpp dummy_hpp;
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900604
Jiri Olsa12400052012-10-13 00:06:16 +0200605 perf_hpp__for_each_format(fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +0900606 if (perf_hpp__should_skip(fmt))
607 continue;
608
609 if (first)
610 first = false;
611 else
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900612 ret += 2;
613
Namhyung Kim94a07932014-03-10 16:43:52 +0900614 ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900615 }
616
Namhyung Kimcfaa1542014-05-19 14:19:30 +0900617 if (verbose && sort__has_sym) /* Addr + origin */
Namhyung Kim7e62ef42012-09-03 11:53:08 +0900618 ret += 3 + BITS_PER_LONG / 4;
619
620 return ret;
621}