|
18 | 18 | #include "util/util.h" |
19 | 19 |
|
20 | 20 | #include <stdlib.h> |
| 21 | +#include <math.h> |
| 22 | + |
| 23 | +/* Diff command specific HPP columns. */ |
| 24 | +enum { |
| 25 | + PERF_HPP_DIFF__BASELINE, |
| 26 | + PERF_HPP_DIFF__PERIOD, |
| 27 | + PERF_HPP_DIFF__PERIOD_BASELINE, |
| 28 | + PERF_HPP_DIFF__DELTA, |
| 29 | + PERF_HPP_DIFF__RATIO, |
| 30 | + PERF_HPP_DIFF__WEIGHTED_DIFF, |
| 31 | + PERF_HPP_DIFF__FORMULA, |
| 32 | + |
| 33 | + PERF_HPP_DIFF__MAX_INDEX |
| 34 | +}; |
| 35 | + |
| 36 | +struct diff_hpp_fmt { |
| 37 | + struct perf_hpp_fmt fmt; |
| 38 | + int idx; |
| 39 | + char *header; |
| 40 | + int header_width; |
| 41 | +}; |
21 | 42 |
|
22 | 43 | struct data__file { |
23 | 44 | struct perf_session *session; |
@@ -60,6 +81,47 @@ const char *compute_names[COMPUTE_MAX] = { |
60 | 81 |
|
61 | 82 | static int compute; |
62 | 83 |
|
| 84 | +static int compute_2_hpp[COMPUTE_MAX] = { |
| 85 | + [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA, |
| 86 | + [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO, |
| 87 | + [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF, |
| 88 | +}; |
| 89 | + |
| 90 | +#define MAX_COL_WIDTH 70 |
| 91 | + |
| 92 | +static struct header_column { |
| 93 | + const char *name; |
| 94 | + int width; |
| 95 | +} columns[PERF_HPP_DIFF__MAX_INDEX] = { |
| 96 | + [PERF_HPP_DIFF__BASELINE] = { |
| 97 | + .name = "Baseline", |
| 98 | + }, |
| 99 | + [PERF_HPP_DIFF__PERIOD] = { |
| 100 | + .name = "Period", |
| 101 | + .width = 14, |
| 102 | + }, |
| 103 | + [PERF_HPP_DIFF__PERIOD_BASELINE] = { |
| 104 | + .name = "Base period", |
| 105 | + .width = 14, |
| 106 | + }, |
| 107 | + [PERF_HPP_DIFF__DELTA] = { |
| 108 | + .name = "Delta", |
| 109 | + .width = 7, |
| 110 | + }, |
| 111 | + [PERF_HPP_DIFF__RATIO] = { |
| 112 | + .name = "Ratio", |
| 113 | + .width = 14, |
| 114 | + }, |
| 115 | + [PERF_HPP_DIFF__WEIGHTED_DIFF] = { |
| 116 | + .name = "Weighted diff", |
| 117 | + .width = 14, |
| 118 | + }, |
| 119 | + [PERF_HPP_DIFF__FORMULA] = { |
| 120 | + .name = "Formula", |
| 121 | + .width = MAX_COL_WIDTH, |
| 122 | + } |
| 123 | +}; |
| 124 | + |
63 | 125 | static int setup_compute_opt_wdiff(char *opt) |
64 | 126 | { |
65 | 127 | char *w1_str = opt; |
@@ -596,34 +658,246 @@ static const struct option options[] = { |
596 | 658 | OPT_END() |
597 | 659 | }; |
598 | 660 |
|
599 | | -static void ui_init(void) |
| 661 | +static double baseline_percent(struct hist_entry *he) |
600 | 662 | { |
601 | | - /* |
602 | | - * Display baseline/delta/ratio |
603 | | - * formula/periods columns. |
604 | | - */ |
605 | | - perf_hpp__column_enable(PERF_HPP__BASELINE); |
| 663 | + struct hists *hists = he->hists; |
| 664 | + return 100.0 * he->stat.period / hists->stats.total_period; |
| 665 | +} |
606 | 666 |
|
607 | | - switch (compute) { |
608 | | - case COMPUTE_DELTA: |
609 | | - perf_hpp__column_enable(PERF_HPP__DELTA); |
| 667 | +static int hpp__color_baseline(struct perf_hpp_fmt *fmt, |
| 668 | + struct perf_hpp *hpp, struct hist_entry *he) |
| 669 | +{ |
| 670 | + struct diff_hpp_fmt *dfmt = |
| 671 | + container_of(fmt, struct diff_hpp_fmt, fmt); |
| 672 | + double percent = baseline_percent(he); |
| 673 | + char pfmt[20] = " "; |
| 674 | + |
| 675 | + if (!he->dummy) { |
| 676 | + scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1); |
| 677 | + return percent_color_snprintf(hpp->buf, hpp->size, |
| 678 | + pfmt, percent); |
| 679 | + } else |
| 680 | + return scnprintf(hpp->buf, hpp->size, "%*s", |
| 681 | + dfmt->header_width, pfmt); |
| 682 | +} |
| 683 | + |
| 684 | +static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size) |
| 685 | +{ |
| 686 | + double percent = baseline_percent(he); |
| 687 | + const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%"; |
| 688 | + int ret = 0; |
| 689 | + |
| 690 | + if (!he->dummy) |
| 691 | + ret = scnprintf(buf, size, fmt, percent); |
| 692 | + |
| 693 | + return ret; |
| 694 | +} |
| 695 | + |
| 696 | +static void |
| 697 | +hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size) |
| 698 | +{ |
| 699 | + switch (idx) { |
| 700 | + case PERF_HPP_DIFF__PERIOD_BASELINE: |
| 701 | + scnprintf(buf, size, "%" PRIu64, he->stat.period); |
610 | 702 | break; |
611 | | - case COMPUTE_RATIO: |
612 | | - perf_hpp__column_enable(PERF_HPP__RATIO); |
| 703 | + |
| 704 | + default: |
613 | 705 | break; |
614 | | - case COMPUTE_WEIGHTED_DIFF: |
615 | | - perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF); |
| 706 | + } |
| 707 | +} |
| 708 | + |
| 709 | +static void |
| 710 | +hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair, |
| 711 | + int idx, char *buf, size_t size) |
| 712 | +{ |
| 713 | + double diff; |
| 714 | + double ratio; |
| 715 | + s64 wdiff; |
| 716 | + |
| 717 | + switch (idx) { |
| 718 | + case PERF_HPP_DIFF__DELTA: |
| 719 | + if (pair->diff.computed) |
| 720 | + diff = pair->diff.period_ratio_delta; |
| 721 | + else |
| 722 | + diff = perf_diff__compute_delta(he, pair); |
| 723 | + |
| 724 | + if (fabs(diff) >= 0.01) |
| 725 | + scnprintf(buf, size, "%+4.2F%%", diff); |
| 726 | + break; |
| 727 | + |
| 728 | + case PERF_HPP_DIFF__RATIO: |
| 729 | + /* No point for ratio number if we are dummy.. */ |
| 730 | + if (he->dummy) |
| 731 | + break; |
| 732 | + |
| 733 | + if (pair->diff.computed) |
| 734 | + ratio = pair->diff.period_ratio; |
| 735 | + else |
| 736 | + ratio = perf_diff__compute_ratio(he, pair); |
| 737 | + |
| 738 | + if (ratio > 0.0) |
| 739 | + scnprintf(buf, size, "%14.6F", ratio); |
| 740 | + break; |
| 741 | + |
| 742 | + case PERF_HPP_DIFF__WEIGHTED_DIFF: |
| 743 | + /* No point for wdiff number if we are dummy.. */ |
| 744 | + if (he->dummy) |
| 745 | + break; |
| 746 | + |
| 747 | + if (pair->diff.computed) |
| 748 | + wdiff = pair->diff.wdiff; |
| 749 | + else |
| 750 | + wdiff = perf_diff__compute_wdiff(he, pair); |
| 751 | + |
| 752 | + if (wdiff != 0) |
| 753 | + scnprintf(buf, size, "%14ld", wdiff); |
| 754 | + break; |
| 755 | + |
| 756 | + case PERF_HPP_DIFF__FORMULA: |
| 757 | + perf_diff__formula(he, pair, buf, size); |
616 | 758 | break; |
| 759 | + |
| 760 | + case PERF_HPP_DIFF__PERIOD: |
| 761 | + scnprintf(buf, size, "%" PRIu64, pair->stat.period); |
| 762 | + break; |
| 763 | + |
617 | 764 | default: |
618 | 765 | BUG_ON(1); |
619 | 766 | }; |
| 767 | +} |
| 768 | + |
| 769 | +static void |
| 770 | +__hpp__entry_global(struct hist_entry *he, int idx, char *buf, size_t size) |
| 771 | +{ |
| 772 | + struct hist_entry *pair = hist_entry__next_pair(he); |
| 773 | + |
| 774 | + /* baseline is special */ |
| 775 | + if (idx == PERF_HPP_DIFF__BASELINE) |
| 776 | + hpp__entry_baseline(he, buf, size); |
| 777 | + else { |
| 778 | + if (pair) |
| 779 | + hpp__entry_pair(he, pair, idx, buf, size); |
| 780 | + else |
| 781 | + hpp__entry_unpair(he, idx, buf, size); |
| 782 | + } |
| 783 | +} |
| 784 | + |
| 785 | +static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp, |
| 786 | + struct hist_entry *he) |
| 787 | +{ |
| 788 | + struct diff_hpp_fmt *dfmt = |
| 789 | + container_of(_fmt, struct diff_hpp_fmt, fmt); |
| 790 | + char buf[MAX_COL_WIDTH] = " "; |
| 791 | + |
| 792 | + __hpp__entry_global(he, dfmt->idx, buf, MAX_COL_WIDTH); |
| 793 | + |
| 794 | + if (symbol_conf.field_sep) |
| 795 | + return scnprintf(hpp->buf, hpp->size, "%s", buf); |
| 796 | + else |
| 797 | + return scnprintf(hpp->buf, hpp->size, "%*s", |
| 798 | + dfmt->header_width, buf); |
| 799 | +} |
| 800 | + |
| 801 | +static int hpp__header(struct perf_hpp_fmt *fmt, |
| 802 | + struct perf_hpp *hpp) |
| 803 | +{ |
| 804 | + struct diff_hpp_fmt *dfmt = |
| 805 | + container_of(fmt, struct diff_hpp_fmt, fmt); |
| 806 | + |
| 807 | + BUG_ON(!dfmt->header); |
| 808 | + return scnprintf(hpp->buf, hpp->size, dfmt->header); |
| 809 | +} |
| 810 | + |
| 811 | +static int hpp__width(struct perf_hpp_fmt *fmt, |
| 812 | + struct perf_hpp *hpp __maybe_unused) |
| 813 | +{ |
| 814 | + struct diff_hpp_fmt *dfmt = |
| 815 | + container_of(fmt, struct diff_hpp_fmt, fmt); |
| 816 | + |
| 817 | + BUG_ON(dfmt->header_width <= 0); |
| 818 | + return dfmt->header_width; |
| 819 | +} |
| 820 | + |
| 821 | +#define hpp__color_global hpp__entry_global |
| 822 | + |
| 823 | +#define FMT(_i, _entry, _color) \ |
| 824 | + [_i] = { \ |
| 825 | + .fmt = { \ |
| 826 | + .header = hpp__header, \ |
| 827 | + .width = hpp__width, \ |
| 828 | + .entry = hpp__entry_ ## _entry, \ |
| 829 | + .color = hpp__color_ ## _color, \ |
| 830 | + }, \ |
| 831 | + .idx = _i, \ |
| 832 | + } |
| 833 | + |
| 834 | +#define FMT_GLOBAL(_i) FMT(_i, global, global) |
| 835 | +#define FMT_BASELINE(_i) FMT(_i, global, baseline) |
| 836 | + |
| 837 | +static struct diff_hpp_fmt diff_fmt[] = { |
| 838 | + FMT_BASELINE(PERF_HPP_DIFF__BASELINE), |
| 839 | + FMT_GLOBAL(PERF_HPP_DIFF__PERIOD), |
| 840 | + FMT_GLOBAL(PERF_HPP_DIFF__PERIOD_BASELINE), |
| 841 | + FMT_GLOBAL(PERF_HPP_DIFF__DELTA), |
| 842 | + FMT_GLOBAL(PERF_HPP_DIFF__RATIO), |
| 843 | + FMT_GLOBAL(PERF_HPP_DIFF__WEIGHTED_DIFF), |
| 844 | + FMT_GLOBAL(PERF_HPP_DIFF__FORMULA), |
| 845 | +}; |
| 846 | + |
| 847 | +static void init_header(struct diff_hpp_fmt *dfmt) |
| 848 | +{ |
| 849 | +#define MAX_HEADER_NAME 100 |
| 850 | + char buf_indent[MAX_HEADER_NAME]; |
| 851 | + char buf[MAX_HEADER_NAME]; |
| 852 | + const char *header = NULL; |
| 853 | + int width = 0; |
| 854 | + |
| 855 | + BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX); |
| 856 | + header = columns[dfmt->idx].name; |
| 857 | + width = columns[dfmt->idx].width; |
| 858 | + |
| 859 | + /* Only our defined HPP fmts should appear here. */ |
| 860 | + BUG_ON(!header); |
| 861 | + |
| 862 | +#define NAME (data__files_cnt > 2 ? buf : header) |
| 863 | + dfmt->header_width = width; |
| 864 | + width = (int) strlen(NAME); |
| 865 | + if (dfmt->header_width < width) |
| 866 | + dfmt->header_width = width; |
| 867 | + |
| 868 | + scnprintf(buf_indent, MAX_HEADER_NAME, "%*s", |
| 869 | + dfmt->header_width, NAME); |
| 870 | + |
| 871 | + dfmt->header = strdup(buf_indent); |
| 872 | +#undef MAX_HEADER_NAME |
| 873 | +#undef NAME |
| 874 | +} |
| 875 | + |
| 876 | +static void column_enable(unsigned col) |
| 877 | +{ |
| 878 | + struct diff_hpp_fmt *dfmt; |
| 879 | + |
| 880 | + BUG_ON(col >= PERF_HPP_DIFF__MAX_INDEX); |
| 881 | + dfmt = &diff_fmt[col]; |
| 882 | + init_header(dfmt); |
| 883 | + perf_hpp__column_register(&dfmt->fmt); |
| 884 | +} |
| 885 | + |
| 886 | +static void ui_init(void) |
| 887 | +{ |
| 888 | + /* |
| 889 | + * Display baseline/delta/ratio/ |
| 890 | + * formula/periods columns. |
| 891 | + */ |
| 892 | + column_enable(PERF_HPP_DIFF__BASELINE); |
| 893 | + column_enable(compute_2_hpp[compute]); |
620 | 894 |
|
621 | 895 | if (show_formula) |
622 | | - perf_hpp__column_enable(PERF_HPP__FORMULA); |
| 896 | + column_enable(PERF_HPP_DIFF__FORMULA); |
623 | 897 |
|
624 | 898 | if (show_period) { |
625 | | - perf_hpp__column_enable(PERF_HPP__PERIOD); |
626 | | - perf_hpp__column_enable(PERF_HPP__PERIOD_BASELINE); |
| 899 | + column_enable(PERF_HPP_DIFF__PERIOD); |
| 900 | + column_enable(PERF_HPP_DIFF__PERIOD_BASELINE); |
627 | 901 | } |
628 | 902 | } |
629 | 903 |
|
|
0 commit comments