perf ui/stdio: Fix invalid output on event group report
[deliverable/linux.git] / tools / perf / ui / hist.c
CommitLineData
ea251d51 1#include <math.h>
2c5d4b4a 2#include <linux/compiler.h>
ea251d51
NK
3
4#include "../util/hist.h"
5#include "../util/util.h"
6#include "../util/sort.h"
4fb71074 7#include "../util/evsel.h"
ea251d51
NK
8
9/* hist period print (hpp) functions */
ea251d51 10
4fb71074 11typedef int (*hpp_snprint_fn)(char *buf, size_t size, const char *fmt, ...);
ea251d51 12
0c5268bf
JO
13static 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)
ea251d51 17{
4fb71074 18 int ret;
b5ff71c3 19 struct hists *hists = he->hists;
759ff497 20 struct perf_evsel *evsel = hists_to_evsel(hists);
ea251d51 21
0c5268bf
JO
22 if (fmt_percent) {
23 double percent = 0.0;
9ffad987 24
0c5268bf
JO
25 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));
5b9e2146 32
759ff497 33 if (perf_evsel__is_group_event(evsel)) {
5b9e2146 34 int prev_idx, idx_delta;
5b9e2146
NK
35 struct hist_entry *pair;
36 int nr_members = evsel->nr_members;
37
5b9e2146
NK
38 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 */
9b0d2fb8
NK
55 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 }
5b9e2146
NK
64 }
65
0c5268bf
JO
66 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);
5b9e2146
NK
72
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 */
9b0d2fb8
NK
82 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 }
5b9e2146
NK
89 }
90 }
4fb71074 91 return ret;
ea251d51
NK
92}
93
4fb71074 94#define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
2c5d4b4a
JO
95static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
96 struct perf_hpp *hpp) \
4fb71074
NK
97{ \
98 int len = _min_width; \
99 \
5b9e2146
NK
100 if (symbol_conf.event_group) { \
101 struct perf_evsel *evsel = hpp->ptr; \
102 \
103 len = max(len, evsel->nr_members * _unit_width); \
104 } \
4fb71074 105 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
ea251d51
NK
106}
107
4fb71074 108#define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
2c5d4b4a
JO
109static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
110 struct perf_hpp *hpp __maybe_unused) \
4fb71074
NK
111{ \
112 int len = _min_width; \
113 \
5b9e2146
NK
114 if (symbol_conf.event_group) { \
115 struct perf_evsel *evsel = hpp->ptr; \
116 \
117 len = max(len, evsel->nr_members * _unit_width); \
118 } \
4fb71074 119 return len; \
ea251d51
NK
120}
121
4fb71074
NK
122#define __HPP_COLOR_PERCENT_FN(_type, _field) \
123static u64 he_get_##_field(struct hist_entry *he) \
124{ \
125 return he->stat._field; \
126} \
127 \
2c5d4b4a
JO
128static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
129 struct perf_hpp *hpp, struct hist_entry *he) \
4fb71074 130{ \
0c5268bf 131 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \
53805eca 132 percent_color_snprintf, true); \
ea251d51
NK
133}
134
4fb71074 135#define __HPP_ENTRY_PERCENT_FN(_type, _field) \
2c5d4b4a
JO
136static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
137 struct perf_hpp *hpp, struct hist_entry *he) \
4fb71074
NK
138{ \
139 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
0c5268bf
JO
140 return __hpp__fmt(hpp, he, he_get_##_field, fmt, \
141 scnprintf, true); \
ea251d51
NK
142}
143
4fb71074
NK
144#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 \
2c5d4b4a
JO
150static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
151 struct perf_hpp *hpp, struct hist_entry *he) \
4fb71074
NK
152{ \
153 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
0c5268bf 154 return __hpp__fmt(hpp, he, he_get_raw_##_field, fmt, scnprintf, false); \
ea251d51
NK
155}
156
4fb71074
NK
157#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)
ea251d51 162
4fb71074
NK
163#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)
ea251d51 167
b5ff71c3 168
4fb71074
NK
169HPP_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)
ea251d51 174
4fb71074
NK
175HPP_RAW_FNS(samples, "Samples", nr_events, 12, 12)
176HPP_RAW_FNS(period, "Period", period, 12, 12)
9ffad987 177
1240005e
JO
178#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 }
ea251d51 185
1240005e
JO
186#define HPP__PRINT_FNS(_name) \
187 { \
188 .header = hpp__header_ ## _name, \
189 .width = hpp__width_ ## _name, \
190 .entry = hpp__entry_ ## _name \
191 }
ea251d51
NK
192
193struct perf_hpp_fmt perf_hpp__format[] = {
1240005e
JO
194 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),
345dc0b4 200 HPP__PRINT_FNS(period)
ea251d51
NK
201};
202
1240005e
JO
203LIST_HEAD(perf_hpp__list);
204
4fb71074 205
ea251d51
NK
206#undef HPP__COLOR_PRINT_FNS
207#undef HPP__PRINT_FNS
208
4fb71074
NK
209#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
1d77822e 219void perf_hpp__init(void)
ea251d51 220{
2b8bfa6b
JO
221 perf_hpp__column_enable(PERF_HPP__OVERHEAD);
222
ea251d51 223 if (symbol_conf.show_cpu_utilization) {
1240005e
JO
224 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
225 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
ea251d51
NK
226
227 if (perf_guest) {
1240005e
JO
228 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
229 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
ea251d51
NK
230 }
231 }
232
233 if (symbol_conf.show_nr_samples)
1240005e 234 perf_hpp__column_enable(PERF_HPP__SAMPLES);
ea251d51
NK
235
236 if (symbol_conf.show_total_period)
1240005e 237 perf_hpp__column_enable(PERF_HPP__PERIOD);
1d77822e 238}
ea251d51 239
1240005e
JO
240void 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)
1d77822e
JO
246{
247 BUG_ON(col >= PERF_HPP__MAX_INDEX);
1240005e 248 perf_hpp__column_register(&perf_hpp__format[col]);
ea251d51
NK
249}
250
ea251d51
NK
251int 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}
7e62ef44
NK
269
270/*
271 * See hists__fprintf to match the column widths
272 */
273unsigned int hists__sort_list_width(struct hists *hists)
274{
1240005e 275 struct perf_hpp_fmt *fmt;
7e62ef44 276 struct sort_entry *se;
1240005e 277 int i = 0, ret = 0;
5b9e2146
NK
278 struct perf_hpp dummy_hpp = {
279 .ptr = hists_to_evsel(hists),
280 };
7e62ef44 281
1240005e 282 perf_hpp__for_each_format(fmt) {
7e62ef44
NK
283 if (i)
284 ret += 2;
285
2c5d4b4a 286 ret += fmt->width(fmt, &dummy_hpp);
7e62ef44
NK
287 }
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}
This page took 0.08031 seconds and 5 git commands to generate.