Commit | Line | Data |
---|---|---|
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 | |
a0088adc NK |
11 | #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 | ||
5b591669 NK |
18 | static 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) | |
ea251d51 | 21 | { |
fb821c9e | 22 | int ret; |
b5ff71c3 | 23 | struct hists *hists = he->hists; |
759ff497 | 24 | struct perf_evsel *evsel = hists_to_evsel(hists); |
a0088adc NK |
25 | char *buf = hpp->buf; |
26 | size_t size = hpp->size; | |
ea251d51 | 27 | |
0c5268bf JO |
28 | if (fmt_percent) { |
29 | double percent = 0.0; | |
f2148330 | 30 | u64 total = hists__total_period(hists); |
9ffad987 | 31 | |
f2148330 NK |
32 | if (total) |
33 | percent = 100.0 * get_field(he) / total; | |
0c5268bf | 34 | |
d675107c | 35 | ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent); |
0c5268bf | 36 | } else |
d675107c | 37 | ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he)); |
5b9e2146 | 38 | |
759ff497 | 39 | if (perf_evsel__is_group_event(evsel)) { |
5b9e2146 | 40 | int prev_idx, idx_delta; |
5b9e2146 NK |
41 | struct hist_entry *pair; |
42 | int nr_members = evsel->nr_members; | |
43 | ||
5b9e2146 NK |
44 | 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); | |
f2148330 | 48 | u64 total = hists__total_period(pair->hists); |
5b9e2146 NK |
49 | |
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 | */ | |
9b0d2fb8 | 61 | if (fmt_percent) { |
a0088adc | 62 | ret += hpp__call_print_fn(hpp, print_fn, |
d675107c | 63 | fmt, len, 0.0); |
9b0d2fb8 | 64 | } else { |
a0088adc | 65 | ret += hpp__call_print_fn(hpp, print_fn, |
d675107c | 66 | fmt, len, 0ULL); |
9b0d2fb8 | 67 | } |
5b9e2146 NK |
68 | } |
69 | ||
a0088adc | 70 | if (fmt_percent) { |
d675107c | 71 | ret += hpp__call_print_fn(hpp, print_fn, fmt, len, |
a0088adc NK |
72 | 100.0 * period / total); |
73 | } else { | |
74 | ret += hpp__call_print_fn(hpp, print_fn, fmt, | |
d675107c | 75 | len, period); |
a0088adc | 76 | } |
5b9e2146 NK |
77 | |
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 | */ | |
9b0d2fb8 | 87 | if (fmt_percent) { |
a0088adc | 88 | ret += hpp__call_print_fn(hpp, print_fn, |
d675107c | 89 | fmt, len, 0.0); |
9b0d2fb8 | 90 | } else { |
a0088adc | 91 | ret += hpp__call_print_fn(hpp, print_fn, |
d675107c | 92 | fmt, len, 0ULL); |
9b0d2fb8 | 93 | } |
5b9e2146 NK |
94 | } |
95 | } | |
a0088adc NK |
96 | |
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 | ||
4fb71074 | 104 | return ret; |
ea251d51 NK |
105 | } |
106 | ||
5b591669 NK |
107 | int 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) | |
110 | { | |
111 | 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); | |
116 | } | |
117 | ||
118 | 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 | ||
126 | int 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) | |
594dcbf3 NK |
129 | { |
130 | if (!symbol_conf.cumulate_callchain) { | |
5b591669 NK |
131 | int len = fmt->user_len ?: fmt->len; |
132 | return snprintf(hpp->buf, hpp->size, " %*s", len - 1, "N/A"); | |
594dcbf3 NK |
133 | } |
134 | ||
5b591669 | 135 | return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmt_percent); |
594dcbf3 NK |
136 | } |
137 | ||
f156d84e NK |
138 | static 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 | ||
147 | static 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; | |
e4e458b4 AS |
165 | fields_a = calloc(nr_members, sizeof(*fields_a)); |
166 | fields_b = calloc(nr_members, sizeof(*fields_b)); | |
f156d84e NK |
167 | |
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 | ||
187 | out: | |
188 | free(fields_a); | |
189 | free(fields_b); | |
190 | ||
191 | return ret; | |
192 | } | |
193 | ||
594dcbf3 NK |
194 | static 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 | ||
5ca82710 NK |
207 | if (a->thread != b->thread || !symbol_conf.use_callchain) |
208 | return 0; | |
209 | ||
594dcbf3 NK |
210 | ret = b->callchain->max_depth - a->callchain->max_depth; |
211 | } | |
212 | return ret; | |
213 | } | |
214 | ||
1ecd4453 NK |
215 | static 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; | |
ea251d51 NK |
228 | } |
229 | ||
1ecd4453 NK |
230 | static 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); | |
e0d66c74 NK |
235 | } |
236 | ||
a0088adc NK |
237 | static 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; | |
d675107c | 242 | int ret, len; |
a0088adc NK |
243 | |
244 | va_start(args, fmt); | |
d675107c | 245 | len = va_arg(args, int); |
a0088adc | 246 | percent = va_arg(args, double); |
d675107c | 247 | ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent); |
a0088adc NK |
248 | va_end(args); |
249 | ||
250 | return (ret >= ssize) ? (ssize - 1) : ret; | |
251 | } | |
252 | ||
253 | static 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 | ||
4fb71074 NK |
266 | #define __HPP_COLOR_PERCENT_FN(_type, _field) \ |
267 | static u64 he_get_##_field(struct hist_entry *he) \ | |
268 | { \ | |
269 | return he->stat._field; \ | |
270 | } \ | |
271 | \ | |
e0d66c74 | 272 | static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \ |
2c5d4b4a | 273 | struct perf_hpp *hpp, struct hist_entry *he) \ |
4fb71074 | 274 | { \ |
5b591669 NK |
275 | return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \ |
276 | hpp_color_scnprintf, true); \ | |
ea251d51 NK |
277 | } |
278 | ||
4fb71074 | 279 | #define __HPP_ENTRY_PERCENT_FN(_type, _field) \ |
e0d66c74 | 280 | static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \ |
2c5d4b4a | 281 | struct perf_hpp *hpp, struct hist_entry *he) \ |
4fb71074 | 282 | { \ |
5b591669 NK |
283 | return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \ |
284 | hpp_entry_scnprintf, true); \ | |
ea251d51 NK |
285 | } |
286 | ||
bc18b7f2 | 287 | #define __HPP_SORT_FN(_type, _field) \ |
87bbdf76 NK |
288 | static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \ |
289 | struct hist_entry *a, struct hist_entry *b) \ | |
bc18b7f2 | 290 | { \ |
f156d84e | 291 | return __hpp__sort(a, b, he_get_##_field); \ |
bc18b7f2 NK |
292 | } |
293 | ||
594dcbf3 NK |
294 | #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \ |
295 | static u64 he_get_acc_##_field(struct hist_entry *he) \ | |
296 | { \ | |
297 | return he->stat_acc->_field; \ | |
298 | } \ | |
299 | \ | |
e0d66c74 | 300 | static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \ |
594dcbf3 NK |
301 | struct perf_hpp *hpp, struct hist_entry *he) \ |
302 | { \ | |
5b591669 NK |
303 | return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \ |
304 | hpp_color_scnprintf, true); \ | |
594dcbf3 NK |
305 | } |
306 | ||
307 | #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \ | |
e0d66c74 | 308 | static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \ |
594dcbf3 NK |
309 | struct perf_hpp *hpp, struct hist_entry *he) \ |
310 | { \ | |
2bfa1528 | 311 | return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \ |
5b591669 | 312 | hpp_entry_scnprintf, true); \ |
594dcbf3 NK |
313 | } |
314 | ||
315 | #define __HPP_SORT_ACC_FN(_type, _field) \ | |
87bbdf76 NK |
316 | static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \ |
317 | struct hist_entry *a, struct hist_entry *b) \ | |
594dcbf3 NK |
318 | { \ |
319 | return __hpp__sort_acc(a, b, he_get_acc_##_field); \ | |
320 | } | |
321 | ||
4fb71074 NK |
322 | #define __HPP_ENTRY_RAW_FN(_type, _field) \ |
323 | static u64 he_get_raw_##_field(struct hist_entry *he) \ | |
324 | { \ | |
325 | return he->stat._field; \ | |
326 | } \ | |
327 | \ | |
e0d66c74 | 328 | static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \ |
2c5d4b4a | 329 | struct perf_hpp *hpp, struct hist_entry *he) \ |
4fb71074 | 330 | { \ |
5b591669 NK |
331 | return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, \ |
332 | hpp_entry_scnprintf, false); \ | |
ea251d51 NK |
333 | } |
334 | ||
bc18b7f2 | 335 | #define __HPP_SORT_RAW_FN(_type, _field) \ |
87bbdf76 NK |
336 | static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \ |
337 | struct hist_entry *a, struct hist_entry *b) \ | |
bc18b7f2 | 338 | { \ |
f156d84e | 339 | return __hpp__sort(a, b, he_get_raw_##_field); \ |
bc18b7f2 NK |
340 | } |
341 | ||
342 | ||
1ecd4453 | 343 | #define HPP_PERCENT_FNS(_type, _field) \ |
4fb71074 | 344 | __HPP_COLOR_PERCENT_FN(_type, _field) \ |
bc18b7f2 NK |
345 | __HPP_ENTRY_PERCENT_FN(_type, _field) \ |
346 | __HPP_SORT_FN(_type, _field) | |
ea251d51 | 347 | |
1ecd4453 | 348 | #define HPP_PERCENT_ACC_FNS(_type, _field) \ |
594dcbf3 NK |
349 | __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \ |
350 | __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \ | |
351 | __HPP_SORT_ACC_FN(_type, _field) | |
352 | ||
1ecd4453 | 353 | #define HPP_RAW_FNS(_type, _field) \ |
bc18b7f2 NK |
354 | __HPP_ENTRY_RAW_FN(_type, _field) \ |
355 | __HPP_SORT_RAW_FN(_type, _field) | |
ea251d51 | 356 | |
1ecd4453 NK |
357 | HPP_PERCENT_FNS(overhead, period) |
358 | HPP_PERCENT_FNS(overhead_sys, period_sys) | |
359 | HPP_PERCENT_FNS(overhead_us, period_us) | |
360 | HPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys) | |
361 | HPP_PERCENT_FNS(overhead_guest_us, period_guest_us) | |
362 | HPP_PERCENT_ACC_FNS(overhead_acc, period) | |
b5ff71c3 | 363 | |
1ecd4453 NK |
364 | HPP_RAW_FNS(samples, nr_events) |
365 | HPP_RAW_FNS(period, period) | |
9ffad987 | 366 | |
87bbdf76 NK |
367 | static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused, |
368 | struct hist_entry *a __maybe_unused, | |
bc18b7f2 NK |
369 | struct hist_entry *b __maybe_unused) |
370 | { | |
371 | return 0; | |
372 | } | |
373 | ||
c0020efa JO |
374 | static bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt *a) |
375 | { | |
376 | return a->header == hpp__header_fn; | |
377 | } | |
378 | ||
379 | static 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 | ||
b21a763e | 387 | #define HPP__COLOR_PRINT_FNS(_name, _fn, _idx) \ |
1240005e | 388 | { \ |
1ecd4453 NK |
389 | .name = _name, \ |
390 | .header = hpp__header_fn, \ | |
391 | .width = hpp__width_fn, \ | |
392 | .color = hpp__color_ ## _fn, \ | |
393 | .entry = hpp__entry_ ## _fn, \ | |
bc18b7f2 NK |
394 | .cmp = hpp__nop_cmp, \ |
395 | .collapse = hpp__nop_cmp, \ | |
1ecd4453 | 396 | .sort = hpp__sort_ ## _fn, \ |
b21a763e | 397 | .idx = PERF_HPP__ ## _idx, \ |
c0020efa | 398 | .equal = hpp__equal, \ |
1240005e | 399 | } |
ea251d51 | 400 | |
b21a763e | 401 | #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx) \ |
594dcbf3 | 402 | { \ |
1ecd4453 NK |
403 | .name = _name, \ |
404 | .header = hpp__header_fn, \ | |
405 | .width = hpp__width_fn, \ | |
406 | .color = hpp__color_ ## _fn, \ | |
407 | .entry = hpp__entry_ ## _fn, \ | |
594dcbf3 NK |
408 | .cmp = hpp__nop_cmp, \ |
409 | .collapse = hpp__nop_cmp, \ | |
1ecd4453 | 410 | .sort = hpp__sort_ ## _fn, \ |
b21a763e | 411 | .idx = PERF_HPP__ ## _idx, \ |
c0020efa | 412 | .equal = hpp__equal, \ |
594dcbf3 NK |
413 | } |
414 | ||
b21a763e | 415 | #define HPP__PRINT_FNS(_name, _fn, _idx) \ |
1240005e | 416 | { \ |
1ecd4453 NK |
417 | .name = _name, \ |
418 | .header = hpp__header_fn, \ | |
419 | .width = hpp__width_fn, \ | |
420 | .entry = hpp__entry_ ## _fn, \ | |
bc18b7f2 NK |
421 | .cmp = hpp__nop_cmp, \ |
422 | .collapse = hpp__nop_cmp, \ | |
1ecd4453 | 423 | .sort = hpp__sort_ ## _fn, \ |
b21a763e | 424 | .idx = PERF_HPP__ ## _idx, \ |
c0020efa | 425 | .equal = hpp__equal, \ |
1240005e | 426 | } |
ea251d51 NK |
427 | |
428 | struct perf_hpp_fmt perf_hpp__format[] = { | |
b21a763e JO |
429 | 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) | |
ea251d51 NK |
437 | }; |
438 | ||
7c31e102 JO |
439 | struct perf_hpp_list perf_hpp_list = { |
440 | .fields = LIST_HEAD_INIT(perf_hpp_list.fields), | |
441 | .sorts = LIST_HEAD_INIT(perf_hpp_list.sorts), | |
442 | }; | |
4fb71074 | 443 | |
ea251d51 | 444 | #undef HPP__COLOR_PRINT_FNS |
594dcbf3 | 445 | #undef HPP__COLOR_ACC_PRINT_FNS |
ea251d51 NK |
446 | #undef HPP__PRINT_FNS |
447 | ||
4fb71074 | 448 | #undef HPP_PERCENT_FNS |
594dcbf3 | 449 | #undef HPP_PERCENT_ACC_FNS |
4fb71074 NK |
450 | #undef HPP_RAW_FNS |
451 | ||
452 | #undef __HPP_HEADER_FN | |
453 | #undef __HPP_WIDTH_FN | |
454 | #undef __HPP_COLOR_PERCENT_FN | |
455 | #undef __HPP_ENTRY_PERCENT_FN | |
594dcbf3 NK |
456 | #undef __HPP_COLOR_ACC_PERCENT_FN |
457 | #undef __HPP_ENTRY_ACC_PERCENT_FN | |
4fb71074 | 458 | #undef __HPP_ENTRY_RAW_FN |
594dcbf3 NK |
459 | #undef __HPP_SORT_FN |
460 | #undef __HPP_SORT_ACC_FN | |
461 | #undef __HPP_SORT_RAW_FN | |
4fb71074 NK |
462 | |
463 | ||
1d77822e | 464 | void perf_hpp__init(void) |
ea251d51 | 465 | { |
26d8b338 NK |
466 | int i; |
467 | ||
468 | for (i = 0; i < PERF_HPP__MAX_INDEX; i++) { | |
a2ce067e NK |
469 | struct perf_hpp_fmt *fmt = &perf_hpp__format[i]; |
470 | ||
471 | INIT_LIST_HEAD(&fmt->list); | |
472 | ||
473 | /* sort_list may be linked by setup_sorting() */ | |
474 | if (fmt->sort_list.next == NULL) | |
475 | INIT_LIST_HEAD(&fmt->sort_list); | |
26d8b338 NK |
476 | } |
477 | ||
a7d945bc NK |
478 | /* |
479 | * If user specified field order, no need to setup default fields. | |
480 | */ | |
2f3f9bcf | 481 | if (is_strict_order(field_order)) |
a7d945bc NK |
482 | return; |
483 | ||
594dcbf3 | 484 | if (symbol_conf.cumulate_callchain) { |
1178bfd4 | 485 | hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC); |
1ecd4453 | 486 | perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self"; |
594dcbf3 NK |
487 | } |
488 | ||
1178bfd4 | 489 | hpp_dimension__add_output(PERF_HPP__OVERHEAD); |
2b8bfa6b | 490 | |
ea251d51 | 491 | if (symbol_conf.show_cpu_utilization) { |
1178bfd4 JO |
492 | hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS); |
493 | hpp_dimension__add_output(PERF_HPP__OVERHEAD_US); | |
ea251d51 NK |
494 | |
495 | if (perf_guest) { | |
1178bfd4 JO |
496 | hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS); |
497 | hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US); | |
ea251d51 NK |
498 | } |
499 | } | |
500 | ||
501 | if (symbol_conf.show_nr_samples) | |
1178bfd4 | 502 | hpp_dimension__add_output(PERF_HPP__SAMPLES); |
ea251d51 NK |
503 | |
504 | if (symbol_conf.show_total_period) | |
1178bfd4 | 505 | hpp_dimension__add_output(PERF_HPP__PERIOD); |
1d77822e | 506 | } |
ea251d51 | 507 | |
ebdd98e0 JO |
508 | void perf_hpp_list__column_register(struct perf_hpp_list *list, |
509 | struct perf_hpp_fmt *format) | |
1240005e | 510 | { |
ebdd98e0 | 511 | list_add_tail(&format->list, &list->fields); |
1240005e JO |
512 | } |
513 | ||
ebdd98e0 JO |
514 | void perf_hpp_list__register_sort_field(struct perf_hpp_list *list, |
515 | struct perf_hpp_fmt *format) | |
77284de3 | 516 | { |
ebdd98e0 | 517 | list_add_tail(&format->sort_list, &list->sorts); |
77284de3 NK |
518 | } |
519 | ||
ebdd98e0 | 520 | void perf_hpp__column_unregister(struct perf_hpp_fmt *format) |
8b536999 | 521 | { |
ebdd98e0 | 522 | list_del(&format->list); |
8b536999 NK |
523 | } |
524 | ||
77284de3 NK |
525 | void perf_hpp__cancel_cumulate(void) |
526 | { | |
1945c3e7 JO |
527 | struct perf_hpp_fmt *fmt, *acc, *ovh, *tmp; |
528 | ||
2f3f9bcf | 529 | if (is_strict_order(field_order)) |
2bf1a123 NK |
530 | return; |
531 | ||
1945c3e7 JO |
532 | ovh = &perf_hpp__format[PERF_HPP__OVERHEAD]; |
533 | acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC]; | |
534 | ||
7a1799e0 | 535 | perf_hpp_list__for_each_format_safe(&perf_hpp_list, fmt, tmp) { |
1945c3e7 JO |
536 | if (acc->equal(acc, fmt)) { |
537 | perf_hpp__column_unregister(fmt); | |
538 | continue; | |
539 | } | |
540 | ||
541 | if (ovh->equal(ovh, fmt)) | |
542 | fmt->name = "Overhead"; | |
543 | } | |
77284de3 NK |
544 | } |
545 | ||
97358084 JO |
546 | static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b) |
547 | { | |
548 | return a->equal && a->equal(a, b); | |
549 | } | |
550 | ||
43e0a68f | 551 | void perf_hpp__setup_output_field(struct perf_hpp_list *list) |
26d8b338 NK |
552 | { |
553 | struct perf_hpp_fmt *fmt; | |
554 | ||
555 | /* append sort keys to output field */ | |
43e0a68f | 556 | perf_hpp_list__for_each_sort_list(list, fmt) { |
3f931f2c | 557 | struct perf_hpp_fmt *pos; |
a7d945bc | 558 | |
43e0a68f | 559 | perf_hpp_list__for_each_format(list, pos) { |
3f931f2c JO |
560 | if (fmt_equal(fmt, pos)) |
561 | goto next; | |
a7d945bc NK |
562 | } |
563 | ||
564 | perf_hpp__column_register(fmt); | |
565 | next: | |
566 | continue; | |
567 | } | |
568 | } | |
569 | ||
43e0a68f | 570 | void perf_hpp__append_sort_keys(struct perf_hpp_list *list) |
a7d945bc NK |
571 | { |
572 | struct perf_hpp_fmt *fmt; | |
573 | ||
574 | /* append output fields to sort keys */ | |
43e0a68f | 575 | perf_hpp_list__for_each_format(list, fmt) { |
3f931f2c | 576 | struct perf_hpp_fmt *pos; |
a7d945bc | 577 | |
43e0a68f | 578 | perf_hpp_list__for_each_sort_list(list, pos) { |
3f931f2c JO |
579 | if (fmt_equal(fmt, pos)) |
580 | goto next; | |
a7d945bc NK |
581 | } |
582 | ||
583 | perf_hpp__register_sort_field(fmt); | |
584 | next: | |
585 | continue; | |
26d8b338 NK |
586 | } |
587 | } | |
588 | ||
43e0a68f | 589 | |
564132f3 JO |
590 | static void fmt_free(struct perf_hpp_fmt *fmt) |
591 | { | |
592 | if (fmt->free) | |
593 | fmt->free(fmt); | |
594 | } | |
595 | ||
43e0a68f | 596 | void perf_hpp__reset_output_field(struct perf_hpp_list *list) |
1c89fe9b NK |
597 | { |
598 | struct perf_hpp_fmt *fmt, *tmp; | |
599 | ||
600 | /* reset output fields */ | |
43e0a68f | 601 | perf_hpp_list__for_each_format_safe(list, fmt, tmp) { |
1c89fe9b NK |
602 | list_del_init(&fmt->list); |
603 | list_del_init(&fmt->sort_list); | |
564132f3 | 604 | fmt_free(fmt); |
1c89fe9b NK |
605 | } |
606 | ||
607 | /* reset sort keys */ | |
43e0a68f | 608 | perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) { |
1c89fe9b NK |
609 | list_del_init(&fmt->list); |
610 | list_del_init(&fmt->sort_list); | |
564132f3 | 611 | fmt_free(fmt); |
1c89fe9b NK |
612 | } |
613 | } | |
614 | ||
7e62ef44 NK |
615 | /* |
616 | * See hists__fprintf to match the column widths | |
617 | */ | |
618 | unsigned int hists__sort_list_width(struct hists *hists) | |
619 | { | |
1240005e | 620 | struct perf_hpp_fmt *fmt; |
cfaa154b NK |
621 | int ret = 0; |
622 | bool first = true; | |
94a0793d | 623 | struct perf_hpp dummy_hpp; |
7e62ef44 | 624 | |
cf094045 | 625 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
361459f1 | 626 | if (perf_hpp__should_skip(fmt, hists)) |
cfaa154b NK |
627 | continue; |
628 | ||
629 | if (first) | |
630 | first = false; | |
631 | else | |
7e62ef44 NK |
632 | ret += 2; |
633 | ||
94a0793d | 634 | ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists)); |
7e62ef44 NK |
635 | } |
636 | ||
cfaa154b | 637 | if (verbose && sort__has_sym) /* Addr + origin */ |
7e62ef44 NK |
638 | ret += 3 + BITS_PER_LONG / 4; |
639 | ||
640 | return ret; | |
641 | } | |
e0d66c74 NK |
642 | |
643 | void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists) | |
644 | { | |
e0d66c74 NK |
645 | if (perf_hpp__is_sort_entry(fmt)) |
646 | return perf_hpp__reset_sort_width(fmt, hists); | |
647 | ||
2e8b79e7 | 648 | BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX); |
e0d66c74 | 649 | |
2e8b79e7 | 650 | switch (fmt->idx) { |
e0d66c74 NK |
651 | 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 | } | |
5b591669 NK |
672 | |
673 | void 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 | ||
cf094045 | 678 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
5b591669 NK |
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 | } |