2 #include <linux/compiler.h>
4 #include "../util/hist.h"
5 #include "../util/util.h"
6 #include "../util/sort.h"
7 #include "../util/evsel.h"
9 /* hist period print (hpp) functions */
11 #define hpp__call_print_fn(hpp, fn, fmt, ...) \
13 int __ret = fn(hpp, fmt, ##__VA_ARGS__); \
14 advance_hpp(hpp, __ret); \
18 int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
19 hpp_field_fn get_field, const char *fmt,
20 hpp_snprint_fn print_fn, bool fmt_percent)
23 struct hists *hists = he->hists;
24 struct perf_evsel *evsel = hists_to_evsel(hists);
26 size_t size = hpp->size;
30 u64 total = hists__total_period(hists);
33 percent = 100.0 * get_field(he) / total;
35 ret = hpp__call_print_fn(hpp, print_fn, fmt, percent);
37 ret = hpp__call_print_fn(hpp, print_fn, fmt, get_field(he));
39 if (perf_evsel__is_group_event(evsel)) {
40 int prev_idx, idx_delta;
41 struct hist_entry *pair;
42 int nr_members = evsel->nr_members;
44 prev_idx = perf_evsel__group_idx(evsel);
46 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
47 u64 period = get_field(pair);
48 u64 total = hists__total_period(pair->hists);
53 evsel = hists_to_evsel(pair->hists);
54 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
58 * zero-fill group members in the middle which
62 ret += hpp__call_print_fn(hpp, print_fn,
65 ret += hpp__call_print_fn(hpp, print_fn,
71 ret += hpp__call_print_fn(hpp, print_fn, fmt,
72 100.0 * period / total);
74 ret += hpp__call_print_fn(hpp, print_fn, fmt,
78 prev_idx = perf_evsel__group_idx(evsel);
81 idx_delta = nr_members - prev_idx - 1;
85 * zero-fill group members at last which have no sample
88 ret += hpp__call_print_fn(hpp, print_fn,
91 ret += hpp__call_print_fn(hpp, print_fn,
98 * Restore original buf and size as it's where caller expects
99 * the result will be saved.
107 int __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)
111 if (!symbol_conf.cumulate_callchain) {
112 return snprintf(hpp->buf, hpp->size, "%*s",
113 fmt_percent ? 8 : 12, "N/A");
116 return __hpp__fmt(hpp, he, get_field, fmt, print_fn, fmt_percent);
119 static int field_cmp(u64 field_a, u64 field_b)
121 if (field_a > field_b)
123 if (field_a < field_b)
128 static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
129 hpp_field_fn get_field)
133 struct perf_evsel *evsel;
134 struct hist_entry *pair;
135 u64 *fields_a, *fields_b;
137 ret = field_cmp(get_field(a), get_field(b));
138 if (ret || !symbol_conf.event_group)
141 evsel = hists_to_evsel(a->hists);
142 if (!perf_evsel__is_group_event(evsel))
145 nr_members = evsel->nr_members;
146 fields_a = calloc(sizeof(*fields_a), nr_members);
147 fields_b = calloc(sizeof(*fields_b), nr_members);
149 if (!fields_a || !fields_b)
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);
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);
162 for (i = 1; i < nr_members; i++) {
163 ret = field_cmp(fields_a[i], fields_b[i]);
175 static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
176 hpp_field_fn get_field)
180 if (symbol_conf.cumulate_callchain) {
182 * Put caller above callee when they have equal period.
184 ret = field_cmp(get_field(a), get_field(b));
188 ret = b->callchain->max_depth - a->callchain->max_depth;
193 #define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
194 static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
195 struct perf_hpp *hpp, \
196 struct perf_evsel *evsel) \
198 int len = _min_width; \
200 if (symbol_conf.event_group) \
201 len = max(len, evsel->nr_members * _unit_width); \
203 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
206 #define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
207 static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
208 struct perf_hpp *hpp __maybe_unused, \
209 struct perf_evsel *evsel) \
211 int len = _min_width; \
213 if (symbol_conf.event_group) \
214 len = max(len, evsel->nr_members * _unit_width); \
219 static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
222 ssize_t ssize = hpp->size;
227 percent = va_arg(args, double);
228 ret = value_color_snprintf(hpp->buf, hpp->size, fmt, percent);
231 return (ret >= ssize) ? (ssize - 1) : ret;
234 static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
237 ssize_t ssize = hpp->size;
241 ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
244 return (ret >= ssize) ? (ssize - 1) : ret;
247 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
248 static u64 he_get_##_field(struct hist_entry *he) \
250 return he->stat._field; \
253 static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
254 struct perf_hpp *hpp, struct hist_entry *he) \
256 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \
257 hpp_color_scnprintf, true); \
260 #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
261 static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
262 struct perf_hpp *hpp, struct hist_entry *he) \
264 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
265 return __hpp__fmt(hpp, he, he_get_##_field, fmt, \
266 hpp_entry_scnprintf, true); \
269 #define __HPP_SORT_FN(_type, _field) \
270 static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
272 return __hpp__sort(a, b, he_get_##_field); \
275 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
276 static u64 he_get_acc_##_field(struct hist_entry *he) \
278 return he->stat_acc->_field; \
281 static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
282 struct perf_hpp *hpp, struct hist_entry *he) \
284 return __hpp__fmt_acc(hpp, he, he_get_acc_##_field, " %6.2f%%", \
285 hpp_color_scnprintf, true); \
288 #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
289 static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
290 struct perf_hpp *hpp, struct hist_entry *he) \
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); \
297 #define __HPP_SORT_ACC_FN(_type, _field) \
298 static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
300 return __hpp__sort_acc(a, b, he_get_acc_##_field); \
303 #define __HPP_ENTRY_RAW_FN(_type, _field) \
304 static u64 he_get_raw_##_field(struct hist_entry *he) \
306 return he->stat._field; \
309 static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
310 struct perf_hpp *hpp, struct hist_entry *he) \
312 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
313 return __hpp__fmt(hpp, he, he_get_raw_##_field, fmt, \
314 hpp_entry_scnprintf, false); \
317 #define __HPP_SORT_RAW_FN(_type, _field) \
318 static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
320 return __hpp__sort(a, b, he_get_raw_##_field); \
324 #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) \
328 __HPP_ENTRY_PERCENT_FN(_type, _field) \
329 __HPP_SORT_FN(_type, _field)
331 #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)
338 #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) \
341 __HPP_ENTRY_RAW_FN(_type, _field) \
342 __HPP_SORT_RAW_FN(_type, _field)
344 __HPP_HEADER_FN(overhead_self, "Self", 8, 8)
346 HPP_PERCENT_FNS(overhead, "Overhead", period, 8, 8)
347 HPP_PERCENT_FNS(overhead_sys, "sys", period_sys, 8, 8)
348 HPP_PERCENT_FNS(overhead_us, "usr", period_us, 8, 8)
349 HPP_PERCENT_FNS(overhead_guest_sys, "guest sys", period_guest_sys, 9, 8)
350 HPP_PERCENT_FNS(overhead_guest_us, "guest usr", period_guest_us, 9, 8)
351 HPP_PERCENT_ACC_FNS(overhead_acc, "Children", period, 8, 8)
353 HPP_RAW_FNS(samples, "Samples", nr_events, 12, 12)
354 HPP_RAW_FNS(period, "Period", period, 12, 12)
356 static int64_t hpp__nop_cmp(struct hist_entry *a __maybe_unused,
357 struct hist_entry *b __maybe_unused)
362 #define HPP__COLOR_PRINT_FNS(_name) \
364 .header = hpp__header_ ## _name, \
365 .width = hpp__width_ ## _name, \
366 .color = hpp__color_ ## _name, \
367 .entry = hpp__entry_ ## _name, \
368 .cmp = hpp__nop_cmp, \
369 .collapse = hpp__nop_cmp, \
370 .sort = hpp__sort_ ## _name, \
373 #define HPP__COLOR_ACC_PRINT_FNS(_name) \
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, \
384 #define HPP__PRINT_FNS(_name) \
386 .header = hpp__header_ ## _name, \
387 .width = hpp__width_ ## _name, \
388 .entry = hpp__entry_ ## _name, \
389 .cmp = hpp__nop_cmp, \
390 .collapse = hpp__nop_cmp, \
391 .sort = hpp__sort_ ## _name, \
394 struct perf_hpp_fmt perf_hpp__format[] = {
395 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),
400 HPP__COLOR_ACC_PRINT_FNS(overhead_acc),
401 HPP__PRINT_FNS(samples),
402 HPP__PRINT_FNS(period)
405 LIST_HEAD(perf_hpp__list);
406 LIST_HEAD(perf_hpp__sort_list);
409 #undef HPP__COLOR_PRINT_FNS
410 #undef HPP__COLOR_ACC_PRINT_FNS
411 #undef HPP__PRINT_FNS
413 #undef HPP_PERCENT_FNS
414 #undef HPP_PERCENT_ACC_FNS
417 #undef __HPP_HEADER_FN
418 #undef __HPP_WIDTH_FN
419 #undef __HPP_COLOR_PERCENT_FN
420 #undef __HPP_ENTRY_PERCENT_FN
421 #undef __HPP_COLOR_ACC_PERCENT_FN
422 #undef __HPP_ENTRY_ACC_PERCENT_FN
423 #undef __HPP_ENTRY_RAW_FN
425 #undef __HPP_SORT_ACC_FN
426 #undef __HPP_SORT_RAW_FN
429 void perf_hpp__init(void)
431 struct list_head *list;
434 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
435 struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
437 INIT_LIST_HEAD(&fmt->list);
439 /* sort_list may be linked by setup_sorting() */
440 if (fmt->sort_list.next == NULL)
441 INIT_LIST_HEAD(&fmt->sort_list);
445 * If user specified field order, no need to setup default fields.
450 if (symbol_conf.cumulate_callchain) {
451 perf_hpp__column_enable(PERF_HPP__OVERHEAD_ACC);
453 perf_hpp__format[PERF_HPP__OVERHEAD].header =
454 hpp__header_overhead_self;
457 perf_hpp__column_enable(PERF_HPP__OVERHEAD);
459 if (symbol_conf.show_cpu_utilization) {
460 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
461 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
464 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
465 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
469 if (symbol_conf.show_nr_samples)
470 perf_hpp__column_enable(PERF_HPP__SAMPLES);
472 if (symbol_conf.show_total_period)
473 perf_hpp__column_enable(PERF_HPP__PERIOD);
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);
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);
487 void perf_hpp__column_register(struct perf_hpp_fmt *format)
489 list_add_tail(&format->list, &perf_hpp__list);
492 void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
494 list_del(&format->list);
497 void perf_hpp__register_sort_field(struct perf_hpp_fmt *format)
499 list_add_tail(&format->sort_list, &perf_hpp__sort_list);
502 void perf_hpp__column_enable(unsigned col)
504 BUG_ON(col >= PERF_HPP__MAX_INDEX);
505 perf_hpp__column_register(&perf_hpp__format[col]);
508 void perf_hpp__column_disable(unsigned col)
510 BUG_ON(col >= PERF_HPP__MAX_INDEX);
511 perf_hpp__column_unregister(&perf_hpp__format[col]);
514 void perf_hpp__cancel_cumulate(void)
519 perf_hpp__column_disable(PERF_HPP__OVERHEAD_ACC);
520 perf_hpp__format[PERF_HPP__OVERHEAD].header = hpp__header_overhead;
523 void perf_hpp__setup_output_field(void)
525 struct perf_hpp_fmt *fmt;
527 /* append sort keys to output field */
528 perf_hpp__for_each_sort_list(fmt) {
529 if (!list_empty(&fmt->list))
533 * sort entry fields are dynamically created,
534 * so they can share a same sort key even though
537 if (perf_hpp__is_sort_entry(fmt)) {
538 struct perf_hpp_fmt *pos;
540 perf_hpp__for_each_format(pos) {
541 if (perf_hpp__same_sort_entry(pos, fmt))
546 perf_hpp__column_register(fmt);
552 void perf_hpp__append_sort_keys(void)
554 struct perf_hpp_fmt *fmt;
556 /* append output fields to sort keys */
557 perf_hpp__for_each_format(fmt) {
558 if (!list_empty(&fmt->sort_list))
562 * sort entry fields are dynamically created,
563 * so they can share a same sort key even though
566 if (perf_hpp__is_sort_entry(fmt)) {
567 struct perf_hpp_fmt *pos;
569 perf_hpp__for_each_sort_list(pos) {
570 if (perf_hpp__same_sort_entry(pos, fmt))
575 perf_hpp__register_sort_field(fmt);
581 void perf_hpp__reset_output_field(void)
583 struct perf_hpp_fmt *fmt, *tmp;
585 /* reset output fields */
586 perf_hpp__for_each_format_safe(fmt, tmp) {
587 list_del_init(&fmt->list);
588 list_del_init(&fmt->sort_list);
591 /* reset sort keys */
592 perf_hpp__for_each_sort_list_safe(fmt, tmp) {
593 list_del_init(&fmt->list);
594 list_del_init(&fmt->sort_list);
599 * See hists__fprintf to match the column widths
601 unsigned int hists__sort_list_width(struct hists *hists)
603 struct perf_hpp_fmt *fmt;
606 struct perf_hpp dummy_hpp;
608 perf_hpp__for_each_format(fmt) {
609 if (perf_hpp__should_skip(fmt))
617 ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
620 if (verbose && sort__has_sym) /* Addr + origin */
621 ret += 3 + BITS_PER_LONG / 4;