1#include <math.h>
2#include <linux/compiler.h>
3
4#include "../util/hist.h"
5#include "../util/util.h"
6#include "../util/sort.h"
7#include "../util/evsel.h"
8
9
10
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
18static 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)
21{
22 int ret;
23 struct hists *hists = he->hists;
24 struct perf_evsel *evsel = hists_to_evsel(hists);
25 char *buf = hpp->buf;
26 size_t size = hpp->size;
27
28 if (fmt_percent) {
29 double percent = 0.0;
30 u64 total = hists__total_period(hists);
31
32 if (total)
33 percent = 100.0 * get_field(he) / total;
34
35 ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
36 } else
37 ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he));
38
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;
43
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);
48 u64 total = hists__total_period(pair->hists);
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
59
60
61 if (fmt_percent) {
62 ret += hpp__call_print_fn(hpp, print_fn,
63 fmt, len, 0.0);
64 } else {
65 ret += hpp__call_print_fn(hpp, print_fn,
66 fmt, len, 0ULL);
67 }
68 }
69
70 if (fmt_percent) {
71 ret += hpp__call_print_fn(hpp, print_fn, fmt, len,
72 100.0 * period / total);
73 } else {
74 ret += hpp__call_print_fn(hpp, print_fn, fmt,
75 len, period);
76 }
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
86
87 if (fmt_percent) {
88 ret += hpp__call_print_fn(hpp, print_fn,
89 fmt, len, 0.0);
90 } else {
91 ret += hpp__call_print_fn(hpp, print_fn,
92 fmt, len, 0ULL);
93 }
94 }
95 }
96
97
98
99
100
101 hpp->buf = buf;
102 hpp->size = size;
103
104 return ret;
105}
106
107int 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;
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);
136}
137
138static 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;
165 fields_a = calloc(sizeof(*fields_a), nr_members);
166 fields_b = calloc(sizeof(*fields_b), nr_members);
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
187out:
188 free(fields_a);
189 free(fields_b);
190
191 return ret;
192}
193
194static 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
202
203 ret = field_cmp(get_field(a), get_field(b));
204 if (ret)
205 return ret;
206
207 ret = b->callchain->max_depth - a->callchain->max_depth;
208 }
209 return ret;
210}
211
212static int hpp__width_fn(struct perf_hpp_fmt *fmt,
213 struct perf_hpp *hpp __maybe_unused,
214 struct perf_evsel *evsel)
215{
216 int len = fmt->user_len ?: fmt->len;
217
218 if (symbol_conf.event_group)
219 len = max(len, evsel->nr_members * fmt->len);
220
221 if (len < (int)strlen(fmt->name))
222 len = strlen(fmt->name);
223
224 return len;
225}
226
227static int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
228 struct perf_evsel *evsel)
229{
230 int len = hpp__width_fn(fmt, hpp, evsel);
231 return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
232}
233
234static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
235{
236 va_list args;
237 ssize_t ssize = hpp->size;
238 double percent;
239 int ret, len;
240
241 va_start(args, fmt);
242 len = va_arg(args, int);
243 percent = va_arg(args, double);
244 ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
245 va_end(args);
246
247 return (ret >= ssize) ? (ssize - 1) : ret;
248}
249
250static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
251{
252 va_list args;
253 ssize_t ssize = hpp->size;
254 int ret;
255
256 va_start(args, fmt);
257 ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
258 va_end(args);
259
260 return (ret >= ssize) ? (ssize - 1) : ret;
261}
262
263#define __HPP_COLOR_PERCENT_FN(_type, _field) \
264static u64 he_get_##_field(struct hist_entry *he) \
265{ \
266 return he->stat._field; \
267} \
268 \
269static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
270 struct perf_hpp *hpp, struct hist_entry *he) \
271{ \
272 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
273 hpp_color_scnprintf, true); \
274}
275
276#define __HPP_ENTRY_PERCENT_FN(_type, _field) \
277static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
278 struct perf_hpp *hpp, struct hist_entry *he) \
279{ \
280 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
281 hpp_entry_scnprintf, true); \
282}
283
284#define __HPP_SORT_FN(_type, _field) \
285static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
286{ \
287 return __hpp__sort(a, b, he_get_##_field); \
288}
289
290#define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
291static u64 he_get_acc_##_field(struct hist_entry *he) \
292{ \
293 return he->stat_acc->_field; \
294} \
295 \
296static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
297 struct perf_hpp *hpp, struct hist_entry *he) \
298{ \
299 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
300 hpp_color_scnprintf, true); \
301}
302
303#define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
304static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
305 struct perf_hpp *hpp, struct hist_entry *he) \
306{ \
307 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
308 hpp_entry_scnprintf, true); \
309}
310
311#define __HPP_SORT_ACC_FN(_type, _field) \
312static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
313{ \
314 return __hpp__sort_acc(a, b, he_get_acc_##_field); \
315}
316
317#define __HPP_ENTRY_RAW_FN(_type, _field) \
318static u64 he_get_raw_##_field(struct hist_entry *he) \
319{ \
320 return he->stat._field; \
321} \
322 \
323static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
324 struct perf_hpp *hpp, struct hist_entry *he) \
325{ \
326 return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, \
327 hpp_entry_scnprintf, false); \
328}
329
330#define __HPP_SORT_RAW_FN(_type, _field) \
331static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
332{ \
333 return __hpp__sort(a, b, he_get_raw_##_field); \
334}
335
336
337#define HPP_PERCENT_FNS(_type, _field) \
338__HPP_COLOR_PERCENT_FN(_type, _field) \
339__HPP_ENTRY_PERCENT_FN(_type, _field) \
340__HPP_SORT_FN(_type, _field)
341
342#define HPP_PERCENT_ACC_FNS(_type, _field) \
343__HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
344__HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
345__HPP_SORT_ACC_FN(_type, _field)
346
347#define HPP_RAW_FNS(_type, _field) \
348__HPP_ENTRY_RAW_FN(_type, _field) \
349__HPP_SORT_RAW_FN(_type, _field)
350
351HPP_PERCENT_FNS(overhead, period)
352HPP_PERCENT_FNS(overhead_sys, period_sys)
353HPP_PERCENT_FNS(overhead_us, period_us)
354HPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys)
355HPP_PERCENT_FNS(overhead_guest_us, period_guest_us)
356HPP_PERCENT_ACC_FNS(overhead_acc, period)
357
358HPP_RAW_FNS(samples, nr_events)
359HPP_RAW_FNS(period, period)
360
361static int64_t hpp__nop_cmp(struct hist_entry *a __maybe_unused,
362 struct hist_entry *b __maybe_unused)
363{
364 return 0;
365}
366
367#define HPP__COLOR_PRINT_FNS(_name, _fn) \
368 { \
369 .name = _name, \
370 .header = hpp__header_fn, \
371 .width = hpp__width_fn, \
372 .color = hpp__color_ ## _fn, \
373 .entry = hpp__entry_ ## _fn, \
374 .cmp = hpp__nop_cmp, \
375 .collapse = hpp__nop_cmp, \
376 .sort = hpp__sort_ ## _fn, \
377 }
378
379#define HPP__COLOR_ACC_PRINT_FNS(_name, _fn) \
380 { \
381 .name = _name, \
382 .header = hpp__header_fn, \
383 .width = hpp__width_fn, \
384 .color = hpp__color_ ## _fn, \
385 .entry = hpp__entry_ ## _fn, \
386 .cmp = hpp__nop_cmp, \
387 .collapse = hpp__nop_cmp, \
388 .sort = hpp__sort_ ## _fn, \
389 }
390
391#define HPP__PRINT_FNS(_name, _fn) \
392 { \
393 .name = _name, \
394 .header = hpp__header_fn, \
395 .width = hpp__width_fn, \
396 .entry = hpp__entry_ ## _fn, \
397 .cmp = hpp__nop_cmp, \
398 .collapse = hpp__nop_cmp, \
399 .sort = hpp__sort_ ## _fn, \
400 }
401
402struct perf_hpp_fmt perf_hpp__format[] = {
403 HPP__COLOR_PRINT_FNS("Overhead", overhead),
404 HPP__COLOR_PRINT_FNS("sys", overhead_sys),
405 HPP__COLOR_PRINT_FNS("usr", overhead_us),
406 HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys),
407 HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us),
408 HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc),
409 HPP__PRINT_FNS("Samples", samples),
410 HPP__PRINT_FNS("Period", period)
411};
412
413LIST_HEAD(perf_hpp__list);
414LIST_HEAD(perf_hpp__sort_list);
415
416
417#undef HPP__COLOR_PRINT_FNS
418#undef HPP__COLOR_ACC_PRINT_FNS
419#undef HPP__PRINT_FNS
420
421#undef HPP_PERCENT_FNS
422#undef HPP_PERCENT_ACC_FNS
423#undef HPP_RAW_FNS
424
425#undef __HPP_HEADER_FN
426#undef __HPP_WIDTH_FN
427#undef __HPP_COLOR_PERCENT_FN
428#undef __HPP_ENTRY_PERCENT_FN
429#undef __HPP_COLOR_ACC_PERCENT_FN
430#undef __HPP_ENTRY_ACC_PERCENT_FN
431#undef __HPP_ENTRY_RAW_FN
432#undef __HPP_SORT_FN
433#undef __HPP_SORT_ACC_FN
434#undef __HPP_SORT_RAW_FN
435
436
437void perf_hpp__init(void)
438{
439 struct list_head *list;
440 int i;
441
442 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
443 struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
444
445 INIT_LIST_HEAD(&fmt->list);
446
447
448 if (fmt->sort_list.next == NULL)
449 INIT_LIST_HEAD(&fmt->sort_list);
450 }
451
452
453
454
455 if (is_strict_order(field_order))
456 return;
457
458 if (symbol_conf.cumulate_callchain) {
459 perf_hpp__column_enable(PERF_HPP__OVERHEAD_ACC);
460 perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
461 }
462
463 perf_hpp__column_enable(PERF_HPP__OVERHEAD);
464
465 if (symbol_conf.show_cpu_utilization) {
466 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
467 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
468
469 if (perf_guest) {
470 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
471 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
472 }
473 }
474
475 if (symbol_conf.show_nr_samples)
476 perf_hpp__column_enable(PERF_HPP__SAMPLES);
477
478 if (symbol_conf.show_total_period)
479 perf_hpp__column_enable(PERF_HPP__PERIOD);
480
481
482 list = &perf_hpp__format[PERF_HPP__OVERHEAD].sort_list;
483 if (list_empty(list))
484 list_add(list, &perf_hpp__sort_list);
485
486 if (symbol_conf.cumulate_callchain) {
487 list = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC].sort_list;
488 if (list_empty(list))
489 list_add(list, &perf_hpp__sort_list);
490 }
491}
492
493void perf_hpp__column_register(struct perf_hpp_fmt *format)
494{
495 list_add_tail(&format->list, &perf_hpp__list);
496}
497
498void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
499{
500 list_del(&format->list);
501}
502
503void perf_hpp__register_sort_field(struct perf_hpp_fmt *format)
504{
505 list_add_tail(&format->sort_list, &perf_hpp__sort_list);
506}
507
508void perf_hpp__column_enable(unsigned col)
509{
510 BUG_ON(col >= PERF_HPP__MAX_INDEX);
511 perf_hpp__column_register(&perf_hpp__format[col]);
512}
513
514void perf_hpp__column_disable(unsigned col)
515{
516 BUG_ON(col >= PERF_HPP__MAX_INDEX);
517 perf_hpp__column_unregister(&perf_hpp__format[col]);
518}
519
520void perf_hpp__cancel_cumulate(void)
521{
522 if (is_strict_order(field_order))
523 return;
524
525 perf_hpp__column_disable(PERF_HPP__OVERHEAD_ACC);
526 perf_hpp__format[PERF_HPP__OVERHEAD].name = "Overhead";
527}
528
529void perf_hpp__setup_output_field(void)
530{
531 struct perf_hpp_fmt *fmt;
532
533
534 perf_hpp__for_each_sort_list(fmt) {
535 if (!list_empty(&fmt->list))
536 continue;
537
538
539
540
541
542
543 if (perf_hpp__is_sort_entry(fmt)) {
544 struct perf_hpp_fmt *pos;
545
546 perf_hpp__for_each_format(pos) {
547 if (perf_hpp__same_sort_entry(pos, fmt))
548 goto next;
549 }
550 }
551
552 perf_hpp__column_register(fmt);
553next:
554 continue;
555 }
556}
557
558void perf_hpp__append_sort_keys(void)
559{
560 struct perf_hpp_fmt *fmt;
561
562
563 perf_hpp__for_each_format(fmt) {
564 if (!list_empty(&fmt->sort_list))
565 continue;
566
567
568
569
570
571
572 if (perf_hpp__is_sort_entry(fmt)) {
573 struct perf_hpp_fmt *pos;
574
575 perf_hpp__for_each_sort_list(pos) {
576 if (perf_hpp__same_sort_entry(pos, fmt))
577 goto next;
578 }
579 }
580
581 perf_hpp__register_sort_field(fmt);
582next:
583 continue;
584 }
585}
586
587void perf_hpp__reset_output_field(void)
588{
589 struct perf_hpp_fmt *fmt, *tmp;
590
591
592 perf_hpp__for_each_format_safe(fmt, tmp) {
593 list_del_init(&fmt->list);
594 list_del_init(&fmt->sort_list);
595 }
596
597
598 perf_hpp__for_each_sort_list_safe(fmt, tmp) {
599 list_del_init(&fmt->list);
600 list_del_init(&fmt->sort_list);
601 }
602}
603
604
605
606
607unsigned int hists__sort_list_width(struct hists *hists)
608{
609 struct perf_hpp_fmt *fmt;
610 int ret = 0;
611 bool first = true;
612 struct perf_hpp dummy_hpp;
613
614 perf_hpp__for_each_format(fmt) {
615 if (perf_hpp__should_skip(fmt))
616 continue;
617
618 if (first)
619 first = false;
620 else
621 ret += 2;
622
623 ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
624 }
625
626 if (verbose && sort__has_sym)
627 ret += 3 + BITS_PER_LONG / 4;
628
629 return ret;
630}
631
632void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
633{
634 int idx;
635
636 if (perf_hpp__is_sort_entry(fmt))
637 return perf_hpp__reset_sort_width(fmt, hists);
638
639 for (idx = 0; idx < PERF_HPP__MAX_INDEX; idx++) {
640 if (fmt == &perf_hpp__format[idx])
641 break;
642 }
643
644 if (idx == PERF_HPP__MAX_INDEX)
645 return;
646
647 switch (idx) {
648 case PERF_HPP__OVERHEAD:
649 case PERF_HPP__OVERHEAD_SYS:
650 case PERF_HPP__OVERHEAD_US:
651 case PERF_HPP__OVERHEAD_ACC:
652 fmt->len = 8;
653 break;
654
655 case PERF_HPP__OVERHEAD_GUEST_SYS:
656 case PERF_HPP__OVERHEAD_GUEST_US:
657 fmt->len = 9;
658 break;
659
660 case PERF_HPP__SAMPLES:
661 case PERF_HPP__PERIOD:
662 fmt->len = 12;
663 break;
664
665 default:
666 break;
667 }
668}
669
670void perf_hpp__set_user_width(const char *width_list_str)
671{
672 struct perf_hpp_fmt *fmt;
673 const char *ptr = width_list_str;
674
675 perf_hpp__for_each_format(fmt) {
676 char *p;
677
678 int len = strtol(ptr, &p, 10);
679 fmt->user_len = len;
680
681 if (*p == ',')
682 ptr = p + 1;
683 else
684 break;
685 }
686}
687