ARM: dts: kirkwood: gpio-leds fixes for linkstation ls-wvl/vl
[linux/fpc-iii.git] / tools / perf / ui / hist.c
blobbf2a66e254eac35c63a0dd44e83628f34e4dc2f9
1 #include <math.h>
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, ...) \
12 ({ \
13 int __ret = fn(hpp, fmt, ##__VA_ARGS__); \
14 advance_hpp(hpp, __ret); \
15 __ret; \
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)
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;
28 if (fmt_percent) {
29 double percent = 0.0;
30 u64 total = hists__total_period(hists);
32 if (total)
33 percent = 100.0 * get_field(he) / total;
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));
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);
50 if (!total)
51 continue;
53 evsel = hists_to_evsel(pair->hists);
54 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
56 while (idx_delta--) {
58 * zero-fill group members in the middle which
59 * have no sample
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);
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);
78 prev_idx = perf_evsel__group_idx(evsel);
81 idx_delta = nr_members - prev_idx - 1;
83 while (idx_delta--) {
85 * zero-fill group members at last which have no sample
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);
98 * Restore original buf and size as it's where caller expects
99 * the result will be saved.
101 hpp->buf = buf;
102 hpp->size = size;
104 return ret;
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)
111 int len = fmt->user_len ?: fmt->len;
113 if (symbol_conf.field_sep) {
114 return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
115 print_fn, fmt_percent);
118 if (fmt_percent)
119 len -= 2; /* 2 for a space and a % sign */
120 else
121 len -= 1;
123 return __hpp__fmt(hpp, he, get_field, fmtstr, len, print_fn, fmt_percent);
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)
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");
135 return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmt_percent);
138 static int field_cmp(u64 field_a, u64 field_b)
140 if (field_a > field_b)
141 return 1;
142 if (field_a < field_b)
143 return -1;
144 return 0;
147 static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
148 hpp_field_fn get_field)
150 s64 ret;
151 int i, nr_members;
152 struct perf_evsel *evsel;
153 struct hist_entry *pair;
154 u64 *fields_a, *fields_b;
156 ret = field_cmp(get_field(a), get_field(b));
157 if (ret || !symbol_conf.event_group)
158 return ret;
160 evsel = hists_to_evsel(a->hists);
161 if (!perf_evsel__is_group_event(evsel))
162 return ret;
164 nr_members = evsel->nr_members;
165 fields_a = calloc(nr_members, sizeof(*fields_a));
166 fields_b = calloc(nr_members, sizeof(*fields_b));
168 if (!fields_a || !fields_b)
169 goto out;
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);
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);
181 for (i = 1; i < nr_members; i++) {
182 ret = field_cmp(fields_a[i], fields_b[i]);
183 if (ret)
184 break;
187 out:
188 free(fields_a);
189 free(fields_b);
191 return ret;
194 static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
195 hpp_field_fn get_field)
197 s64 ret = 0;
199 if (symbol_conf.cumulate_callchain) {
201 * Put caller above callee when they have equal period.
203 ret = field_cmp(get_field(a), get_field(b));
204 if (ret)
205 return ret;
207 if (a->thread != b->thread || !symbol_conf.use_callchain)
208 return 0;
210 ret = b->callchain->max_depth - a->callchain->max_depth;
212 return ret;
215 static int hpp__width_fn(struct perf_hpp_fmt *fmt,
216 struct perf_hpp *hpp __maybe_unused,
217 struct perf_evsel *evsel)
219 int len = fmt->user_len ?: fmt->len;
221 if (symbol_conf.event_group)
222 len = max(len, evsel->nr_members * fmt->len);
224 if (len < (int)strlen(fmt->name))
225 len = strlen(fmt->name);
227 return len;
230 static int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
231 struct perf_evsel *evsel)
233 int len = hpp__width_fn(fmt, hpp, evsel);
234 return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
237 static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
239 va_list args;
240 ssize_t ssize = hpp->size;
241 double percent;
242 int ret, len;
244 va_start(args, fmt);
245 len = va_arg(args, int);
246 percent = va_arg(args, double);
247 ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
248 va_end(args);
250 return (ret >= ssize) ? (ssize - 1) : ret;
253 static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
255 va_list args;
256 ssize_t ssize = hpp->size;
257 int ret;
259 va_start(args, fmt);
260 ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
261 va_end(args);
263 return (ret >= ssize) ? (ssize - 1) : ret;
266 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
267 static u64 he_get_##_field(struct hist_entry *he) \
269 return he->stat._field; \
272 static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
273 struct perf_hpp *hpp, struct hist_entry *he) \
275 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
276 hpp_color_scnprintf, true); \
279 #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
280 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
281 struct perf_hpp *hpp, struct hist_entry *he) \
283 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
284 hpp_entry_scnprintf, true); \
287 #define __HPP_SORT_FN(_type, _field) \
288 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
289 struct hist_entry *a, struct hist_entry *b) \
291 return __hpp__sort(a, b, he_get_##_field); \
294 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
295 static u64 he_get_acc_##_field(struct hist_entry *he) \
297 return he->stat_acc->_field; \
300 static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
301 struct perf_hpp *hpp, struct hist_entry *he) \
303 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
304 hpp_color_scnprintf, true); \
307 #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
308 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
309 struct perf_hpp *hpp, struct hist_entry *he) \
311 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
312 hpp_entry_scnprintf, true); \
315 #define __HPP_SORT_ACC_FN(_type, _field) \
316 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
317 struct hist_entry *a, struct hist_entry *b) \
319 return __hpp__sort_acc(a, b, he_get_acc_##_field); \
322 #define __HPP_ENTRY_RAW_FN(_type, _field) \
323 static u64 he_get_raw_##_field(struct hist_entry *he) \
325 return he->stat._field; \
328 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
329 struct perf_hpp *hpp, struct hist_entry *he) \
331 return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, \
332 hpp_entry_scnprintf, false); \
335 #define __HPP_SORT_RAW_FN(_type, _field) \
336 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
337 struct hist_entry *a, struct hist_entry *b) \
339 return __hpp__sort(a, b, he_get_raw_##_field); \
343 #define HPP_PERCENT_FNS(_type, _field) \
344 __HPP_COLOR_PERCENT_FN(_type, _field) \
345 __HPP_ENTRY_PERCENT_FN(_type, _field) \
346 __HPP_SORT_FN(_type, _field)
348 #define HPP_PERCENT_ACC_FNS(_type, _field) \
349 __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
350 __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
351 __HPP_SORT_ACC_FN(_type, _field)
353 #define HPP_RAW_FNS(_type, _field) \
354 __HPP_ENTRY_RAW_FN(_type, _field) \
355 __HPP_SORT_RAW_FN(_type, _field)
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)
364 HPP_RAW_FNS(samples, nr_events)
365 HPP_RAW_FNS(period, period)
367 static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
368 struct hist_entry *a __maybe_unused,
369 struct hist_entry *b __maybe_unused)
371 return 0;
374 #define HPP__COLOR_PRINT_FNS(_name, _fn) \
376 .name = _name, \
377 .header = hpp__header_fn, \
378 .width = hpp__width_fn, \
379 .color = hpp__color_ ## _fn, \
380 .entry = hpp__entry_ ## _fn, \
381 .cmp = hpp__nop_cmp, \
382 .collapse = hpp__nop_cmp, \
383 .sort = hpp__sort_ ## _fn, \
386 #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn) \
388 .name = _name, \
389 .header = hpp__header_fn, \
390 .width = hpp__width_fn, \
391 .color = hpp__color_ ## _fn, \
392 .entry = hpp__entry_ ## _fn, \
393 .cmp = hpp__nop_cmp, \
394 .collapse = hpp__nop_cmp, \
395 .sort = hpp__sort_ ## _fn, \
398 #define HPP__PRINT_FNS(_name, _fn) \
400 .name = _name, \
401 .header = hpp__header_fn, \
402 .width = hpp__width_fn, \
403 .entry = hpp__entry_ ## _fn, \
404 .cmp = hpp__nop_cmp, \
405 .collapse = hpp__nop_cmp, \
406 .sort = hpp__sort_ ## _fn, \
409 struct perf_hpp_fmt perf_hpp__format[] = {
410 HPP__COLOR_PRINT_FNS("Overhead", overhead),
411 HPP__COLOR_PRINT_FNS("sys", overhead_sys),
412 HPP__COLOR_PRINT_FNS("usr", overhead_us),
413 HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys),
414 HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us),
415 HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc),
416 HPP__PRINT_FNS("Samples", samples),
417 HPP__PRINT_FNS("Period", period)
420 LIST_HEAD(perf_hpp__list);
421 LIST_HEAD(perf_hpp__sort_list);
424 #undef HPP__COLOR_PRINT_FNS
425 #undef HPP__COLOR_ACC_PRINT_FNS
426 #undef HPP__PRINT_FNS
428 #undef HPP_PERCENT_FNS
429 #undef HPP_PERCENT_ACC_FNS
430 #undef HPP_RAW_FNS
432 #undef __HPP_HEADER_FN
433 #undef __HPP_WIDTH_FN
434 #undef __HPP_COLOR_PERCENT_FN
435 #undef __HPP_ENTRY_PERCENT_FN
436 #undef __HPP_COLOR_ACC_PERCENT_FN
437 #undef __HPP_ENTRY_ACC_PERCENT_FN
438 #undef __HPP_ENTRY_RAW_FN
439 #undef __HPP_SORT_FN
440 #undef __HPP_SORT_ACC_FN
441 #undef __HPP_SORT_RAW_FN
444 void perf_hpp__init(void)
446 int i;
448 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
449 struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
451 INIT_LIST_HEAD(&fmt->list);
453 /* sort_list may be linked by setup_sorting() */
454 if (fmt->sort_list.next == NULL)
455 INIT_LIST_HEAD(&fmt->sort_list);
459 * If user specified field order, no need to setup default fields.
461 if (is_strict_order(field_order))
462 return;
464 if (symbol_conf.cumulate_callchain) {
465 hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC);
466 perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
469 hpp_dimension__add_output(PERF_HPP__OVERHEAD);
471 if (symbol_conf.show_cpu_utilization) {
472 hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS);
473 hpp_dimension__add_output(PERF_HPP__OVERHEAD_US);
475 if (perf_guest) {
476 hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS);
477 hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US);
481 if (symbol_conf.show_nr_samples)
482 hpp_dimension__add_output(PERF_HPP__SAMPLES);
484 if (symbol_conf.show_total_period)
485 hpp_dimension__add_output(PERF_HPP__PERIOD);
488 void perf_hpp__column_register(struct perf_hpp_fmt *format)
490 list_add_tail(&format->list, &perf_hpp__list);
493 void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
495 list_del(&format->list);
498 void perf_hpp__register_sort_field(struct perf_hpp_fmt *format)
500 list_add_tail(&format->sort_list, &perf_hpp__sort_list);
503 void perf_hpp__column_enable(unsigned col)
505 BUG_ON(col >= PERF_HPP__MAX_INDEX);
506 perf_hpp__column_register(&perf_hpp__format[col]);
509 void perf_hpp__column_disable(unsigned col)
511 BUG_ON(col >= PERF_HPP__MAX_INDEX);
512 perf_hpp__column_unregister(&perf_hpp__format[col]);
515 void perf_hpp__cancel_cumulate(void)
517 if (is_strict_order(field_order))
518 return;
520 perf_hpp__column_disable(PERF_HPP__OVERHEAD_ACC);
521 perf_hpp__format[PERF_HPP__OVERHEAD].name = "Overhead";
524 void perf_hpp__setup_output_field(void)
526 struct perf_hpp_fmt *fmt;
528 /* append sort keys to output field */
529 perf_hpp__for_each_sort_list(fmt) {
530 if (!list_empty(&fmt->list))
531 continue;
534 * sort entry fields are dynamically created,
535 * so they can share a same sort key even though
536 * the list is empty.
538 if (perf_hpp__is_sort_entry(fmt)) {
539 struct perf_hpp_fmt *pos;
541 perf_hpp__for_each_format(pos) {
542 if (perf_hpp__same_sort_entry(pos, fmt))
543 goto next;
547 perf_hpp__column_register(fmt);
548 next:
549 continue;
553 void perf_hpp__append_sort_keys(void)
555 struct perf_hpp_fmt *fmt;
557 /* append output fields to sort keys */
558 perf_hpp__for_each_format(fmt) {
559 if (!list_empty(&fmt->sort_list))
560 continue;
563 * sort entry fields are dynamically created,
564 * so they can share a same sort key even though
565 * the list is empty.
567 if (perf_hpp__is_sort_entry(fmt)) {
568 struct perf_hpp_fmt *pos;
570 perf_hpp__for_each_sort_list(pos) {
571 if (perf_hpp__same_sort_entry(pos, fmt))
572 goto next;
576 perf_hpp__register_sort_field(fmt);
577 next:
578 continue;
582 void perf_hpp__reset_output_field(void)
584 struct perf_hpp_fmt *fmt, *tmp;
586 /* reset output fields */
587 perf_hpp__for_each_format_safe(fmt, tmp) {
588 list_del_init(&fmt->list);
589 list_del_init(&fmt->sort_list);
592 /* reset sort keys */
593 perf_hpp__for_each_sort_list_safe(fmt, tmp) {
594 list_del_init(&fmt->list);
595 list_del_init(&fmt->sort_list);
600 * See hists__fprintf to match the column widths
602 unsigned int hists__sort_list_width(struct hists *hists)
604 struct perf_hpp_fmt *fmt;
605 int ret = 0;
606 bool first = true;
607 struct perf_hpp dummy_hpp;
609 perf_hpp__for_each_format(fmt) {
610 if (perf_hpp__should_skip(fmt, hists))
611 continue;
613 if (first)
614 first = false;
615 else
616 ret += 2;
618 ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
621 if (verbose && sort__has_sym) /* Addr + origin */
622 ret += 3 + BITS_PER_LONG / 4;
624 return ret;
627 void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
629 int idx;
631 if (perf_hpp__is_sort_entry(fmt))
632 return perf_hpp__reset_sort_width(fmt, hists);
634 for (idx = 0; idx < PERF_HPP__MAX_INDEX; idx++) {
635 if (fmt == &perf_hpp__format[idx])
636 break;
639 if (idx == PERF_HPP__MAX_INDEX)
640 return;
642 switch (idx) {
643 case PERF_HPP__OVERHEAD:
644 case PERF_HPP__OVERHEAD_SYS:
645 case PERF_HPP__OVERHEAD_US:
646 case PERF_HPP__OVERHEAD_ACC:
647 fmt->len = 8;
648 break;
650 case PERF_HPP__OVERHEAD_GUEST_SYS:
651 case PERF_HPP__OVERHEAD_GUEST_US:
652 fmt->len = 9;
653 break;
655 case PERF_HPP__SAMPLES:
656 case PERF_HPP__PERIOD:
657 fmt->len = 12;
658 break;
660 default:
661 break;
665 void perf_hpp__set_user_width(const char *width_list_str)
667 struct perf_hpp_fmt *fmt;
668 const char *ptr = width_list_str;
670 perf_hpp__for_each_format(fmt) {
671 char *p;
673 int len = strtol(ptr, &p, 10);
674 fmt->user_len = len;
676 if (*p == ',')
677 ptr = p + 1;
678 else
679 break;