diff options
| -rw-r--r-- | tools/perf/builtin-stat.c | 1150 | ||||
| -rw-r--r-- | tools/perf/util/Build | 1 | ||||
| -rw-r--r-- | tools/perf/util/stat-display.c | 1166 | ||||
| -rw-r--r-- | tools/perf/util/stat.h | 6 |
4 files changed, 1173 insertions, 1150 deletions
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index f4ddedc8619b..0b0e3961d511 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c | |||
| @@ -88,8 +88,6 @@ | |||
| 88 | #include "sane_ctype.h" | 88 | #include "sane_ctype.h" |
| 89 | 89 | ||
| 90 | #define DEFAULT_SEPARATOR " " | 90 | #define DEFAULT_SEPARATOR " " |
| 91 | #define CNTR_NOT_SUPPORTED "<not supported>" | ||
| 92 | #define CNTR_NOT_COUNTED "<not counted>" | ||
| 93 | #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi" | 91 | #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi" |
| 94 | 92 | ||
| 95 | static void print_counters(struct timespec *ts, int argc, const char **argv); | 93 | static void print_counters(struct timespec *ts, int argc, const char **argv); |
| @@ -189,11 +187,6 @@ static struct perf_stat_config stat_config = { | |||
| 189 | .big_num = true, | 187 | .big_num = true, |
| 190 | }; | 188 | }; |
| 191 | 189 | ||
| 192 | static bool is_duration_time(struct perf_evsel *evsel) | ||
| 193 | { | ||
| 194 | return !strcmp(evsel->name, "duration_time"); | ||
| 195 | } | ||
| 196 | |||
| 197 | static inline void diff_timespec(struct timespec *r, struct timespec *a, | 190 | static inline void diff_timespec(struct timespec *r, struct timespec *a, |
| 198 | struct timespec *b) | 191 | struct timespec *b) |
| 199 | { | 192 | { |
| @@ -643,1149 +636,6 @@ static int run_perf_stat(int argc, const char **argv, int run_idx) | |||
| 643 | return ret; | 636 | return ret; |
| 644 | } | 637 | } |
| 645 | 638 | ||
| 646 | static void print_running(struct perf_stat_config *config, | ||
| 647 | u64 run, u64 ena) | ||
| 648 | { | ||
| 649 | if (config->csv_output) { | ||
| 650 | fprintf(config->output, "%s%" PRIu64 "%s%.2f", | ||
| 651 | config->csv_sep, | ||
| 652 | run, | ||
| 653 | config->csv_sep, | ||
| 654 | ena ? 100.0 * run / ena : 100.0); | ||
| 655 | } else if (run != ena) { | ||
| 656 | fprintf(config->output, " (%.2f%%)", 100.0 * run / ena); | ||
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | static void print_noise_pct(struct perf_stat_config *config, | ||
| 661 | double total, double avg) | ||
| 662 | { | ||
| 663 | double pct = rel_stddev_stats(total, avg); | ||
| 664 | |||
| 665 | if (config->csv_output) | ||
| 666 | fprintf(config->output, "%s%.2f%%", config->csv_sep, pct); | ||
| 667 | else if (pct) | ||
| 668 | fprintf(config->output, " ( +-%6.2f%% )", pct); | ||
| 669 | } | ||
| 670 | |||
| 671 | static void print_noise(struct perf_stat_config *config, | ||
| 672 | struct perf_evsel *evsel, double avg) | ||
| 673 | { | ||
| 674 | struct perf_stat_evsel *ps; | ||
| 675 | |||
| 676 | if (config->run_count == 1) | ||
| 677 | return; | ||
| 678 | |||
| 679 | ps = evsel->stats; | ||
| 680 | print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg); | ||
| 681 | } | ||
| 682 | |||
| 683 | static void aggr_printout(struct perf_stat_config *config, | ||
| 684 | struct perf_evsel *evsel, int id, int nr) | ||
| 685 | { | ||
| 686 | switch (config->aggr_mode) { | ||
| 687 | case AGGR_CORE: | ||
| 688 | fprintf(config->output, "S%d-C%*d%s%*d%s", | ||
| 689 | cpu_map__id_to_socket(id), | ||
| 690 | config->csv_output ? 0 : -8, | ||
| 691 | cpu_map__id_to_cpu(id), | ||
| 692 | config->csv_sep, | ||
| 693 | config->csv_output ? 0 : 4, | ||
| 694 | nr, | ||
| 695 | config->csv_sep); | ||
| 696 | break; | ||
| 697 | case AGGR_SOCKET: | ||
| 698 | fprintf(config->output, "S%*d%s%*d%s", | ||
| 699 | config->csv_output ? 0 : -5, | ||
| 700 | id, | ||
| 701 | config->csv_sep, | ||
| 702 | config->csv_output ? 0 : 4, | ||
| 703 | nr, | ||
| 704 | config->csv_sep); | ||
| 705 | break; | ||
| 706 | case AGGR_NONE: | ||
| 707 | fprintf(config->output, "CPU%*d%s", | ||
| 708 | config->csv_output ? 0 : -4, | ||
| 709 | perf_evsel__cpus(evsel)->map[id], config->csv_sep); | ||
| 710 | break; | ||
| 711 | case AGGR_THREAD: | ||
| 712 | fprintf(config->output, "%*s-%*d%s", | ||
| 713 | config->csv_output ? 0 : 16, | ||
| 714 | thread_map__comm(evsel->threads, id), | ||
| 715 | config->csv_output ? 0 : -8, | ||
| 716 | thread_map__pid(evsel->threads, id), | ||
| 717 | config->csv_sep); | ||
| 718 | break; | ||
| 719 | case AGGR_GLOBAL: | ||
| 720 | case AGGR_UNSET: | ||
| 721 | default: | ||
| 722 | break; | ||
| 723 | } | ||
| 724 | } | ||
| 725 | |||
| 726 | struct outstate { | ||
| 727 | FILE *fh; | ||
| 728 | bool newline; | ||
| 729 | const char *prefix; | ||
| 730 | int nfields; | ||
| 731 | int id, nr; | ||
| 732 | struct perf_evsel *evsel; | ||
| 733 | }; | ||
| 734 | |||
| 735 | #define METRIC_LEN 35 | ||
| 736 | |||
| 737 | static void new_line_std(struct perf_stat_config *config __maybe_unused, | ||
| 738 | void *ctx) | ||
| 739 | { | ||
| 740 | struct outstate *os = ctx; | ||
| 741 | |||
| 742 | os->newline = true; | ||
| 743 | } | ||
| 744 | |||
| 745 | static void do_new_line_std(struct perf_stat_config *config, | ||
| 746 | struct outstate *os) | ||
| 747 | { | ||
| 748 | fputc('\n', os->fh); | ||
| 749 | fputs(os->prefix, os->fh); | ||
| 750 | aggr_printout(config, os->evsel, os->id, os->nr); | ||
| 751 | if (config->aggr_mode == AGGR_NONE) | ||
| 752 | fprintf(os->fh, " "); | ||
| 753 | fprintf(os->fh, " "); | ||
| 754 | } | ||
| 755 | |||
| 756 | static void print_metric_std(struct perf_stat_config *config, | ||
| 757 | void *ctx, const char *color, const char *fmt, | ||
| 758 | const char *unit, double val) | ||
| 759 | { | ||
| 760 | struct outstate *os = ctx; | ||
| 761 | FILE *out = os->fh; | ||
| 762 | int n; | ||
| 763 | bool newline = os->newline; | ||
| 764 | |||
| 765 | os->newline = false; | ||
| 766 | |||
| 767 | if (unit == NULL || fmt == NULL) { | ||
| 768 | fprintf(out, "%-*s", METRIC_LEN, ""); | ||
| 769 | return; | ||
| 770 | } | ||
| 771 | |||
| 772 | if (newline) | ||
| 773 | do_new_line_std(config, os); | ||
| 774 | |||
| 775 | n = fprintf(out, " # "); | ||
| 776 | if (color) | ||
| 777 | n += color_fprintf(out, color, fmt, val); | ||
| 778 | else | ||
| 779 | n += fprintf(out, fmt, val); | ||
| 780 | fprintf(out, " %-*s", METRIC_LEN - n - 1, unit); | ||
| 781 | } | ||
| 782 | |||
| 783 | static void new_line_csv(struct perf_stat_config *config, void *ctx) | ||
| 784 | { | ||
| 785 | struct outstate *os = ctx; | ||
| 786 | int i; | ||
| 787 | |||
| 788 | fputc('\n', os->fh); | ||
| 789 | if (os->prefix) | ||
| 790 | fprintf(os->fh, "%s%s", os->prefix, config->csv_sep); | ||
| 791 | aggr_printout(config, os->evsel, os->id, os->nr); | ||
| 792 | for (i = 0; i < os->nfields; i++) | ||
| 793 | fputs(config->csv_sep, os->fh); | ||
| 794 | } | ||
| 795 | |||
| 796 | static void print_metric_csv(struct perf_stat_config *config __maybe_unused, | ||
| 797 | void *ctx, | ||
| 798 | const char *color __maybe_unused, | ||
| 799 | const char *fmt, const char *unit, double val) | ||
| 800 | { | ||
| 801 | struct outstate *os = ctx; | ||
| 802 | FILE *out = os->fh; | ||
| 803 | char buf[64], *vals, *ends; | ||
| 804 | |||
| 805 | if (unit == NULL || fmt == NULL) { | ||
| 806 | fprintf(out, "%s%s", config->csv_sep, config->csv_sep); | ||
| 807 | return; | ||
| 808 | } | ||
| 809 | snprintf(buf, sizeof(buf), fmt, val); | ||
| 810 | ends = vals = ltrim(buf); | ||
| 811 | while (isdigit(*ends) || *ends == '.') | ||
| 812 | ends++; | ||
| 813 | *ends = 0; | ||
| 814 | while (isspace(*unit)) | ||
| 815 | unit++; | ||
| 816 | fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, unit); | ||
| 817 | } | ||
| 818 | |||
| 819 | /* Filter out some columns that don't work well in metrics only mode */ | ||
| 820 | |||
| 821 | static bool valid_only_metric(const char *unit) | ||
| 822 | { | ||
| 823 | if (!unit) | ||
| 824 | return false; | ||
| 825 | if (strstr(unit, "/sec") || | ||
| 826 | strstr(unit, "hz") || | ||
| 827 | strstr(unit, "Hz") || | ||
| 828 | strstr(unit, "CPUs utilized")) | ||
| 829 | return false; | ||
| 830 | return true; | ||
| 831 | } | ||
| 832 | |||
| 833 | static const char *fixunit(char *buf, struct perf_evsel *evsel, | ||
| 834 | const char *unit) | ||
| 835 | { | ||
| 836 | if (!strncmp(unit, "of all", 6)) { | ||
| 837 | snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel), | ||
| 838 | unit); | ||
| 839 | return buf; | ||
| 840 | } | ||
| 841 | return unit; | ||
| 842 | } | ||
| 843 | |||
| 844 | static void print_metric_only(struct perf_stat_config *config, | ||
| 845 | void *ctx, const char *color, const char *fmt, | ||
| 846 | const char *unit, double val) | ||
| 847 | { | ||
| 848 | struct outstate *os = ctx; | ||
| 849 | FILE *out = os->fh; | ||
| 850 | char buf[1024], str[1024]; | ||
| 851 | unsigned mlen = config->metric_only_len; | ||
| 852 | |||
| 853 | if (!valid_only_metric(unit)) | ||
| 854 | return; | ||
| 855 | unit = fixunit(buf, os->evsel, unit); | ||
| 856 | if (mlen < strlen(unit)) | ||
| 857 | mlen = strlen(unit) + 1; | ||
| 858 | |||
| 859 | if (color) | ||
| 860 | mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1; | ||
| 861 | |||
| 862 | color_snprintf(str, sizeof(str), color ?: "", fmt, val); | ||
| 863 | fprintf(out, "%*s ", mlen, str); | ||
| 864 | } | ||
| 865 | |||
| 866 | static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused, | ||
| 867 | void *ctx, const char *color __maybe_unused, | ||
| 868 | const char *fmt, | ||
| 869 | const char *unit, double val) | ||
| 870 | { | ||
| 871 | struct outstate *os = ctx; | ||
| 872 | FILE *out = os->fh; | ||
| 873 | char buf[64], *vals, *ends; | ||
| 874 | char tbuf[1024]; | ||
| 875 | |||
| 876 | if (!valid_only_metric(unit)) | ||
| 877 | return; | ||
| 878 | unit = fixunit(tbuf, os->evsel, unit); | ||
| 879 | snprintf(buf, sizeof buf, fmt, val); | ||
| 880 | ends = vals = ltrim(buf); | ||
| 881 | while (isdigit(*ends) || *ends == '.') | ||
| 882 | ends++; | ||
| 883 | *ends = 0; | ||
| 884 | fprintf(out, "%s%s", vals, config->csv_sep); | ||
| 885 | } | ||
| 886 | |||
| 887 | static void new_line_metric(struct perf_stat_config *config __maybe_unused, | ||
| 888 | void *ctx __maybe_unused) | ||
| 889 | { | ||
| 890 | } | ||
| 891 | |||
| 892 | static void print_metric_header(struct perf_stat_config *config, | ||
| 893 | void *ctx, const char *color __maybe_unused, | ||
| 894 | const char *fmt __maybe_unused, | ||
| 895 | const char *unit, double val __maybe_unused) | ||
| 896 | { | ||
| 897 | struct outstate *os = ctx; | ||
| 898 | char tbuf[1024]; | ||
| 899 | |||
| 900 | if (!valid_only_metric(unit)) | ||
| 901 | return; | ||
| 902 | unit = fixunit(tbuf, os->evsel, unit); | ||
| 903 | if (config->csv_output) | ||
| 904 | fprintf(os->fh, "%s%s", unit, config->csv_sep); | ||
| 905 | else | ||
| 906 | fprintf(os->fh, "%*s ", config->metric_only_len, unit); | ||
| 907 | } | ||
| 908 | |||
| 909 | static int first_shadow_cpu(struct perf_stat_config *config, | ||
| 910 | struct perf_evsel *evsel, int id) | ||
| 911 | { | ||
| 912 | struct perf_evlist *evlist = evsel->evlist; | ||
| 913 | int i; | ||
| 914 | |||
| 915 | if (!config->aggr_get_id) | ||
| 916 | return 0; | ||
| 917 | |||
| 918 | if (config->aggr_mode == AGGR_NONE) | ||
| 919 | return id; | ||
| 920 | |||
| 921 | if (config->aggr_mode == AGGR_GLOBAL) | ||
| 922 | return 0; | ||
| 923 | |||
| 924 | for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) { | ||
| 925 | int cpu2 = perf_evsel__cpus(evsel)->map[i]; | ||
| 926 | |||
| 927 | if (config->aggr_get_id(config, evlist->cpus, cpu2) == id) | ||
| 928 | return cpu2; | ||
| 929 | } | ||
| 930 | return 0; | ||
| 931 | } | ||
| 932 | |||
| 933 | static void abs_printout(struct perf_stat_config *config, | ||
| 934 | int id, int nr, struct perf_evsel *evsel, double avg) | ||
| 935 | { | ||
| 936 | FILE *output = config->output; | ||
| 937 | double sc = evsel->scale; | ||
| 938 | const char *fmt; | ||
| 939 | |||
| 940 | if (config->csv_output) { | ||
| 941 | fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s"; | ||
| 942 | } else { | ||
| 943 | if (config->big_num) | ||
| 944 | fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s"; | ||
| 945 | else | ||
| 946 | fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s"; | ||
| 947 | } | ||
| 948 | |||
| 949 | aggr_printout(config, evsel, id, nr); | ||
| 950 | |||
| 951 | fprintf(output, fmt, avg, config->csv_sep); | ||
| 952 | |||
| 953 | if (evsel->unit) | ||
| 954 | fprintf(output, "%-*s%s", | ||
| 955 | config->csv_output ? 0 : config->unit_width, | ||
| 956 | evsel->unit, config->csv_sep); | ||
| 957 | |||
| 958 | fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel)); | ||
| 959 | |||
| 960 | if (evsel->cgrp) | ||
| 961 | fprintf(output, "%s%s", config->csv_sep, evsel->cgrp->name); | ||
| 962 | } | ||
| 963 | |||
| 964 | static bool is_mixed_hw_group(struct perf_evsel *counter) | ||
| 965 | { | ||
| 966 | struct perf_evlist *evlist = counter->evlist; | ||
| 967 | u32 pmu_type = counter->attr.type; | ||
| 968 | struct perf_evsel *pos; | ||
| 969 | |||
| 970 | if (counter->nr_members < 2) | ||
| 971 | return false; | ||
| 972 | |||
| 973 | evlist__for_each_entry(evlist, pos) { | ||
| 974 | /* software events can be part of any hardware group */ | ||
| 975 | if (pos->attr.type == PERF_TYPE_SOFTWARE) | ||
| 976 | continue; | ||
| 977 | if (pmu_type == PERF_TYPE_SOFTWARE) { | ||
| 978 | pmu_type = pos->attr.type; | ||
| 979 | continue; | ||
| 980 | } | ||
| 981 | if (pmu_type != pos->attr.type) | ||
| 982 | return true; | ||
| 983 | } | ||
| 984 | |||
| 985 | return false; | ||
| 986 | } | ||
| 987 | |||
| 988 | static void printout(struct perf_stat_config *config, int id, int nr, | ||
| 989 | struct perf_evsel *counter, double uval, | ||
| 990 | char *prefix, u64 run, u64 ena, double noise, | ||
| 991 | struct runtime_stat *st) | ||
| 992 | { | ||
| 993 | struct perf_stat_output_ctx out; | ||
| 994 | struct outstate os = { | ||
| 995 | .fh = config->output, | ||
| 996 | .prefix = prefix ? prefix : "", | ||
| 997 | .id = id, | ||
| 998 | .nr = nr, | ||
| 999 | .evsel = counter, | ||
| 1000 | }; | ||
| 1001 | print_metric_t pm = print_metric_std; | ||
| 1002 | new_line_t nl; | ||
| 1003 | |||
| 1004 | if (config->metric_only) { | ||
| 1005 | nl = new_line_metric; | ||
| 1006 | if (config->csv_output) | ||
| 1007 | pm = print_metric_only_csv; | ||
| 1008 | else | ||
| 1009 | pm = print_metric_only; | ||
| 1010 | } else | ||
| 1011 | nl = new_line_std; | ||
| 1012 | |||
| 1013 | if (config->csv_output && !config->metric_only) { | ||
| 1014 | static int aggr_fields[] = { | ||
| 1015 | [AGGR_GLOBAL] = 0, | ||
| 1016 | [AGGR_THREAD] = 1, | ||
| 1017 | [AGGR_NONE] = 1, | ||
| 1018 | [AGGR_SOCKET] = 2, | ||
| 1019 | [AGGR_CORE] = 2, | ||
| 1020 | }; | ||
| 1021 | |||
| 1022 | pm = print_metric_csv; | ||
| 1023 | nl = new_line_csv; | ||
| 1024 | os.nfields = 3; | ||
| 1025 | os.nfields += aggr_fields[config->aggr_mode]; | ||
| 1026 | if (counter->cgrp) | ||
| 1027 | os.nfields++; | ||
| 1028 | } | ||
| 1029 | if (run == 0 || ena == 0 || counter->counts->scaled == -1) { | ||
| 1030 | if (config->metric_only) { | ||
| 1031 | pm(config, &os, NULL, "", "", 0); | ||
| 1032 | return; | ||
| 1033 | } | ||
| 1034 | aggr_printout(config, counter, id, nr); | ||
| 1035 | |||
| 1036 | fprintf(config->output, "%*s%s", | ||
| 1037 | config->csv_output ? 0 : 18, | ||
| 1038 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, | ||
| 1039 | config->csv_sep); | ||
| 1040 | |||
| 1041 | if (counter->supported) { | ||
| 1042 | config->print_free_counters_hint = 1; | ||
| 1043 | if (is_mixed_hw_group(counter)) | ||
| 1044 | config->print_mixed_hw_group_error = 1; | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | fprintf(config->output, "%-*s%s", | ||
| 1048 | config->csv_output ? 0 : config->unit_width, | ||
| 1049 | counter->unit, config->csv_sep); | ||
| 1050 | |||
| 1051 | fprintf(config->output, "%*s", | ||
| 1052 | config->csv_output ? 0 : -25, | ||
| 1053 | perf_evsel__name(counter)); | ||
| 1054 | |||
| 1055 | if (counter->cgrp) | ||
| 1056 | fprintf(config->output, "%s%s", | ||
| 1057 | config->csv_sep, counter->cgrp->name); | ||
| 1058 | |||
| 1059 | if (!config->csv_output) | ||
| 1060 | pm(config, &os, NULL, NULL, "", 0); | ||
| 1061 | print_noise(config, counter, noise); | ||
| 1062 | print_running(config, run, ena); | ||
| 1063 | if (config->csv_output) | ||
| 1064 | pm(config, &os, NULL, NULL, "", 0); | ||
| 1065 | return; | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | if (!config->metric_only) | ||
| 1069 | abs_printout(config, id, nr, counter, uval); | ||
| 1070 | |||
| 1071 | out.print_metric = pm; | ||
| 1072 | out.new_line = nl; | ||
| 1073 | out.ctx = &os; | ||
| 1074 | out.force_header = false; | ||
| 1075 | |||
| 1076 | if (config->csv_output && !config->metric_only) { | ||
| 1077 | print_noise(config, counter, noise); | ||
| 1078 | print_running(config, run, ena); | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | perf_stat__print_shadow_stats(config, counter, uval, | ||
| 1082 | first_shadow_cpu(config, counter, id), | ||
| 1083 | &out, &config->metric_events, st); | ||
| 1084 | if (!config->csv_output && !config->metric_only) { | ||
| 1085 | print_noise(config, counter, noise); | ||
| 1086 | print_running(config, run, ena); | ||
| 1087 | } | ||
| 1088 | } | ||
| 1089 | |||
| 1090 | static void aggr_update_shadow(struct perf_stat_config *config, | ||
| 1091 | struct perf_evlist *evlist) | ||
| 1092 | { | ||
| 1093 | int cpu, s2, id, s; | ||
| 1094 | u64 val; | ||
| 1095 | struct perf_evsel *counter; | ||
| 1096 | |||
| 1097 | for (s = 0; s < config->aggr_map->nr; s++) { | ||
| 1098 | id = config->aggr_map->map[s]; | ||
| 1099 | evlist__for_each_entry(evlist, counter) { | ||
| 1100 | val = 0; | ||
| 1101 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | ||
| 1102 | s2 = config->aggr_get_id(config, evlist->cpus, cpu); | ||
| 1103 | if (s2 != id) | ||
| 1104 | continue; | ||
| 1105 | val += perf_counts(counter->counts, cpu, 0)->val; | ||
| 1106 | } | ||
| 1107 | perf_stat__update_shadow_stats(counter, val, | ||
| 1108 | first_shadow_cpu(config, counter, id), | ||
| 1109 | &rt_stat); | ||
| 1110 | } | ||
| 1111 | } | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | static void uniquify_event_name(struct perf_evsel *counter) | ||
| 1115 | { | ||
| 1116 | char *new_name; | ||
| 1117 | char *config; | ||
| 1118 | |||
| 1119 | if (counter->uniquified_name || | ||
| 1120 | !counter->pmu_name || !strncmp(counter->name, counter->pmu_name, | ||
| 1121 | strlen(counter->pmu_name))) | ||
| 1122 | return; | ||
| 1123 | |||
| 1124 | config = strchr(counter->name, '/'); | ||
| 1125 | if (config) { | ||
| 1126 | if (asprintf(&new_name, | ||
| 1127 | "%s%s", counter->pmu_name, config) > 0) { | ||
| 1128 | free(counter->name); | ||
| 1129 | counter->name = new_name; | ||
| 1130 | } | ||
| 1131 | } else { | ||
| 1132 | if (asprintf(&new_name, | ||
| 1133 | "%s [%s]", counter->name, counter->pmu_name) > 0) { | ||
| 1134 | free(counter->name); | ||
| 1135 | counter->name = new_name; | ||
| 1136 | } | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | counter->uniquified_name = true; | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | static void collect_all_aliases(struct perf_stat_config *config, struct perf_evsel *counter, | ||
| 1143 | void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data, | ||
| 1144 | bool first), | ||
| 1145 | void *data) | ||
| 1146 | { | ||
| 1147 | struct perf_evlist *evlist = counter->evlist; | ||
| 1148 | struct perf_evsel *alias; | ||
| 1149 | |||
| 1150 | alias = list_prepare_entry(counter, &(evlist->entries), node); | ||
| 1151 | list_for_each_entry_continue (alias, &evlist->entries, node) { | ||
| 1152 | if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) || | ||
| 1153 | alias->scale != counter->scale || | ||
| 1154 | alias->cgrp != counter->cgrp || | ||
| 1155 | strcmp(alias->unit, counter->unit) || | ||
| 1156 | perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter)) | ||
| 1157 | break; | ||
| 1158 | alias->merged_stat = true; | ||
| 1159 | cb(config, alias, data, false); | ||
| 1160 | } | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | static bool collect_data(struct perf_stat_config *config, struct perf_evsel *counter, | ||
| 1164 | void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data, | ||
| 1165 | bool first), | ||
| 1166 | void *data) | ||
| 1167 | { | ||
| 1168 | if (counter->merged_stat) | ||
| 1169 | return false; | ||
| 1170 | cb(config, counter, data, true); | ||
| 1171 | if (config->no_merge) | ||
| 1172 | uniquify_event_name(counter); | ||
| 1173 | else if (counter->auto_merge_stats) | ||
| 1174 | collect_all_aliases(config, counter, cb, data); | ||
| 1175 | return true; | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | struct aggr_data { | ||
| 1179 | u64 ena, run, val; | ||
| 1180 | int id; | ||
| 1181 | int nr; | ||
| 1182 | int cpu; | ||
| 1183 | }; | ||
| 1184 | |||
| 1185 | static void aggr_cb(struct perf_stat_config *config, | ||
| 1186 | struct perf_evsel *counter, void *data, bool first) | ||
| 1187 | { | ||
| 1188 | struct aggr_data *ad = data; | ||
| 1189 | int cpu, s2; | ||
| 1190 | |||
| 1191 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | ||
| 1192 | struct perf_counts_values *counts; | ||
| 1193 | |||
| 1194 | s2 = config->aggr_get_id(config, perf_evsel__cpus(counter), cpu); | ||
| 1195 | if (s2 != ad->id) | ||
| 1196 | continue; | ||
| 1197 | if (first) | ||
| 1198 | ad->nr++; | ||
| 1199 | counts = perf_counts(counter->counts, cpu, 0); | ||
| 1200 | /* | ||
| 1201 | * When any result is bad, make them all to give | ||
| 1202 | * consistent output in interval mode. | ||
| 1203 | */ | ||
| 1204 | if (counts->ena == 0 || counts->run == 0 || | ||
| 1205 | counter->counts->scaled == -1) { | ||
| 1206 | ad->ena = 0; | ||
| 1207 | ad->run = 0; | ||
| 1208 | break; | ||
| 1209 | } | ||
| 1210 | ad->val += counts->val; | ||
| 1211 | ad->ena += counts->ena; | ||
| 1212 | ad->run += counts->run; | ||
| 1213 | } | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | static void print_aggr(struct perf_stat_config *config, | ||
| 1217 | struct perf_evlist *evlist, | ||
| 1218 | char *prefix) | ||
| 1219 | { | ||
| 1220 | bool metric_only = config->metric_only; | ||
| 1221 | FILE *output = config->output; | ||
| 1222 | struct perf_evsel *counter; | ||
| 1223 | int s, id, nr; | ||
| 1224 | double uval; | ||
| 1225 | u64 ena, run, val; | ||
| 1226 | bool first; | ||
| 1227 | |||
| 1228 | if (!(config->aggr_map || config->aggr_get_id)) | ||
| 1229 | return; | ||
| 1230 | |||
| 1231 | aggr_update_shadow(config, evlist); | ||
| 1232 | |||
| 1233 | /* | ||
| 1234 | * With metric_only everything is on a single line. | ||
| 1235 | * Without each counter has its own line. | ||
| 1236 | */ | ||
| 1237 | for (s = 0; s < config->aggr_map->nr; s++) { | ||
| 1238 | struct aggr_data ad; | ||
| 1239 | if (prefix && metric_only) | ||
| 1240 | fprintf(output, "%s", prefix); | ||
| 1241 | |||
| 1242 | ad.id = id = config->aggr_map->map[s]; | ||
| 1243 | first = true; | ||
| 1244 | evlist__for_each_entry(evlist, counter) { | ||
| 1245 | if (is_duration_time(counter)) | ||
| 1246 | continue; | ||
| 1247 | |||
| 1248 | ad.val = ad.ena = ad.run = 0; | ||
| 1249 | ad.nr = 0; | ||
| 1250 | if (!collect_data(config, counter, aggr_cb, &ad)) | ||
| 1251 | continue; | ||
| 1252 | nr = ad.nr; | ||
| 1253 | ena = ad.ena; | ||
| 1254 | run = ad.run; | ||
| 1255 | val = ad.val; | ||
| 1256 | if (first && metric_only) { | ||
| 1257 | first = false; | ||
| 1258 | aggr_printout(config, counter, id, nr); | ||
| 1259 | } | ||
| 1260 | if (prefix && !metric_only) | ||
| 1261 | fprintf(output, "%s", prefix); | ||
| 1262 | |||
| 1263 | uval = val * counter->scale; | ||
| 1264 | printout(config, id, nr, counter, uval, prefix, | ||
| 1265 | run, ena, 1.0, &rt_stat); | ||
| 1266 | if (!metric_only) | ||
| 1267 | fputc('\n', output); | ||
| 1268 | } | ||
| 1269 | if (metric_only) | ||
| 1270 | fputc('\n', output); | ||
| 1271 | } | ||
| 1272 | } | ||
| 1273 | |||
| 1274 | static int cmp_val(const void *a, const void *b) | ||
| 1275 | { | ||
| 1276 | return ((struct perf_aggr_thread_value *)b)->val - | ||
| 1277 | ((struct perf_aggr_thread_value *)a)->val; | ||
| 1278 | } | ||
| 1279 | |||
| 1280 | static struct perf_aggr_thread_value *sort_aggr_thread( | ||
| 1281 | struct perf_evsel *counter, | ||
| 1282 | int nthreads, int ncpus, | ||
| 1283 | int *ret, | ||
| 1284 | struct target *_target) | ||
| 1285 | { | ||
| 1286 | int cpu, thread, i = 0; | ||
| 1287 | double uval; | ||
| 1288 | struct perf_aggr_thread_value *buf; | ||
| 1289 | |||
| 1290 | buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value)); | ||
| 1291 | if (!buf) | ||
| 1292 | return NULL; | ||
| 1293 | |||
| 1294 | for (thread = 0; thread < nthreads; thread++) { | ||
| 1295 | u64 ena = 0, run = 0, val = 0; | ||
| 1296 | |||
| 1297 | for (cpu = 0; cpu < ncpus; cpu++) { | ||
| 1298 | val += perf_counts(counter->counts, cpu, thread)->val; | ||
| 1299 | ena += perf_counts(counter->counts, cpu, thread)->ena; | ||
| 1300 | run += perf_counts(counter->counts, cpu, thread)->run; | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | uval = val * counter->scale; | ||
| 1304 | |||
| 1305 | /* | ||
| 1306 | * Skip value 0 when enabling --per-thread globally, | ||
| 1307 | * otherwise too many 0 output. | ||
| 1308 | */ | ||
| 1309 | if (uval == 0.0 && target__has_per_thread(_target)) | ||
| 1310 | continue; | ||
| 1311 | |||
| 1312 | buf[i].counter = counter; | ||
| 1313 | buf[i].id = thread; | ||
| 1314 | buf[i].uval = uval; | ||
| 1315 | buf[i].val = val; | ||
| 1316 | buf[i].run = run; | ||
| 1317 | buf[i].ena = ena; | ||
| 1318 | i++; | ||
| 1319 | } | ||
| 1320 | |||
| 1321 | qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val); | ||
| 1322 | |||
| 1323 | if (ret) | ||
| 1324 | *ret = i; | ||
| 1325 | |||
| 1326 | return buf; | ||
| 1327 | } | ||
| 1328 | |||
| 1329 | static void print_aggr_thread(struct perf_stat_config *config, | ||
| 1330 | struct target *_target, | ||
| 1331 | struct perf_evsel *counter, char *prefix) | ||
| 1332 | { | ||
| 1333 | FILE *output = config->output; | ||
| 1334 | int nthreads = thread_map__nr(counter->threads); | ||
| 1335 | int ncpus = cpu_map__nr(counter->cpus); | ||
| 1336 | int thread, sorted_threads, id; | ||
| 1337 | struct perf_aggr_thread_value *buf; | ||
| 1338 | |||
| 1339 | buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target); | ||
| 1340 | if (!buf) { | ||
| 1341 | perror("cannot sort aggr thread"); | ||
| 1342 | return; | ||
| 1343 | } | ||
| 1344 | |||
| 1345 | for (thread = 0; thread < sorted_threads; thread++) { | ||
| 1346 | if (prefix) | ||
| 1347 | fprintf(output, "%s", prefix); | ||
| 1348 | |||
| 1349 | id = buf[thread].id; | ||
| 1350 | if (config->stats) | ||
| 1351 | printout(config, id, 0, buf[thread].counter, buf[thread].uval, | ||
| 1352 | prefix, buf[thread].run, buf[thread].ena, 1.0, | ||
| 1353 | &config->stats[id]); | ||
| 1354 | else | ||
| 1355 | printout(config, id, 0, buf[thread].counter, buf[thread].uval, | ||
| 1356 | prefix, buf[thread].run, buf[thread].ena, 1.0, | ||
| 1357 | &rt_stat); | ||
| 1358 | fputc('\n', output); | ||
| 1359 | } | ||
| 1360 | |||
| 1361 | free(buf); | ||
| 1362 | } | ||
| 1363 | |||
| 1364 | struct caggr_data { | ||
| 1365 | double avg, avg_enabled, avg_running; | ||
| 1366 | }; | ||
| 1367 | |||
| 1368 | static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused, | ||
| 1369 | struct perf_evsel *counter, void *data, | ||
| 1370 | bool first __maybe_unused) | ||
| 1371 | { | ||
| 1372 | struct caggr_data *cd = data; | ||
| 1373 | struct perf_stat_evsel *ps = counter->stats; | ||
| 1374 | |||
| 1375 | cd->avg += avg_stats(&ps->res_stats[0]); | ||
| 1376 | cd->avg_enabled += avg_stats(&ps->res_stats[1]); | ||
| 1377 | cd->avg_running += avg_stats(&ps->res_stats[2]); | ||
| 1378 | } | ||
| 1379 | |||
| 1380 | /* | ||
| 1381 | * Print out the results of a single counter: | ||
| 1382 | * aggregated counts in system-wide mode | ||
| 1383 | */ | ||
| 1384 | static void print_counter_aggr(struct perf_stat_config *config, | ||
| 1385 | struct perf_evsel *counter, char *prefix) | ||
| 1386 | { | ||
| 1387 | bool metric_only = config->metric_only; | ||
| 1388 | FILE *output = config->output; | ||
| 1389 | double uval; | ||
| 1390 | struct caggr_data cd = { .avg = 0.0 }; | ||
| 1391 | |||
| 1392 | if (!collect_data(config, counter, counter_aggr_cb, &cd)) | ||
| 1393 | return; | ||
| 1394 | |||
| 1395 | if (prefix && !metric_only) | ||
| 1396 | fprintf(output, "%s", prefix); | ||
| 1397 | |||
| 1398 | uval = cd.avg * counter->scale; | ||
| 1399 | printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, | ||
| 1400 | cd.avg, &rt_stat); | ||
| 1401 | if (!metric_only) | ||
| 1402 | fprintf(output, "\n"); | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | static void counter_cb(struct perf_stat_config *config __maybe_unused, | ||
| 1406 | struct perf_evsel *counter, void *data, | ||
| 1407 | bool first __maybe_unused) | ||
| 1408 | { | ||
| 1409 | struct aggr_data *ad = data; | ||
| 1410 | |||
| 1411 | ad->val += perf_counts(counter->counts, ad->cpu, 0)->val; | ||
| 1412 | ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena; | ||
| 1413 | ad->run += perf_counts(counter->counts, ad->cpu, 0)->run; | ||
| 1414 | } | ||
| 1415 | |||
| 1416 | /* | ||
| 1417 | * Print out the results of a single counter: | ||
| 1418 | * does not use aggregated count in system-wide | ||
| 1419 | */ | ||
| 1420 | static void print_counter(struct perf_stat_config *config, | ||
| 1421 | struct perf_evsel *counter, char *prefix) | ||
| 1422 | { | ||
| 1423 | FILE *output = config->output; | ||
| 1424 | u64 ena, run, val; | ||
| 1425 | double uval; | ||
| 1426 | int cpu; | ||
| 1427 | |||
| 1428 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | ||
| 1429 | struct aggr_data ad = { .cpu = cpu }; | ||
| 1430 | |||
| 1431 | if (!collect_data(config, counter, counter_cb, &ad)) | ||
| 1432 | return; | ||
| 1433 | val = ad.val; | ||
| 1434 | ena = ad.ena; | ||
| 1435 | run = ad.run; | ||
| 1436 | |||
| 1437 | if (prefix) | ||
| 1438 | fprintf(output, "%s", prefix); | ||
| 1439 | |||
| 1440 | uval = val * counter->scale; | ||
| 1441 | printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0, | ||
| 1442 | &rt_stat); | ||
| 1443 | |||
| 1444 | fputc('\n', output); | ||
| 1445 | } | ||
| 1446 | } | ||
| 1447 | |||
| 1448 | static void print_no_aggr_metric(struct perf_stat_config *config, | ||
| 1449 | struct perf_evlist *evlist, | ||
| 1450 | char *prefix) | ||
| 1451 | { | ||
| 1452 | int cpu; | ||
| 1453 | int nrcpus = 0; | ||
| 1454 | struct perf_evsel *counter; | ||
| 1455 | u64 ena, run, val; | ||
| 1456 | double uval; | ||
| 1457 | |||
| 1458 | nrcpus = evlist->cpus->nr; | ||
| 1459 | for (cpu = 0; cpu < nrcpus; cpu++) { | ||
| 1460 | bool first = true; | ||
| 1461 | |||
| 1462 | if (prefix) | ||
| 1463 | fputs(prefix, config->output); | ||
| 1464 | evlist__for_each_entry(evlist, counter) { | ||
| 1465 | if (is_duration_time(counter)) | ||
| 1466 | continue; | ||
| 1467 | if (first) { | ||
| 1468 | aggr_printout(config, counter, cpu, 0); | ||
| 1469 | first = false; | ||
| 1470 | } | ||
| 1471 | val = perf_counts(counter->counts, cpu, 0)->val; | ||
| 1472 | ena = perf_counts(counter->counts, cpu, 0)->ena; | ||
| 1473 | run = perf_counts(counter->counts, cpu, 0)->run; | ||
| 1474 | |||
| 1475 | uval = val * counter->scale; | ||
| 1476 | printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0, | ||
| 1477 | &rt_stat); | ||
| 1478 | } | ||
| 1479 | fputc('\n', config->output); | ||
| 1480 | } | ||
| 1481 | } | ||
| 1482 | |||
| 1483 | static int aggr_header_lens[] = { | ||
| 1484 | [AGGR_CORE] = 18, | ||
| 1485 | [AGGR_SOCKET] = 12, | ||
| 1486 | [AGGR_NONE] = 6, | ||
| 1487 | [AGGR_THREAD] = 24, | ||
| 1488 | [AGGR_GLOBAL] = 0, | ||
| 1489 | }; | ||
| 1490 | |||
| 1491 | static const char *aggr_header_csv[] = { | ||
| 1492 | [AGGR_CORE] = "core,cpus,", | ||
| 1493 | [AGGR_SOCKET] = "socket,cpus", | ||
| 1494 | [AGGR_NONE] = "cpu,", | ||
| 1495 | [AGGR_THREAD] = "comm-pid,", | ||
| 1496 | [AGGR_GLOBAL] = "" | ||
| 1497 | }; | ||
| 1498 | |||
| 1499 | static void print_metric_headers(struct perf_stat_config *config, | ||
| 1500 | struct perf_evlist *evlist, | ||
| 1501 | const char *prefix, bool no_indent) | ||
| 1502 | { | ||
| 1503 | struct perf_stat_output_ctx out; | ||
| 1504 | struct perf_evsel *counter; | ||
| 1505 | struct outstate os = { | ||
| 1506 | .fh = config->output | ||
| 1507 | }; | ||
| 1508 | |||
| 1509 | if (prefix) | ||
| 1510 | fprintf(config->output, "%s", prefix); | ||
| 1511 | |||
| 1512 | if (!config->csv_output && !no_indent) | ||
| 1513 | fprintf(config->output, "%*s", | ||
| 1514 | aggr_header_lens[config->aggr_mode], ""); | ||
| 1515 | if (config->csv_output) { | ||
| 1516 | if (config->interval) | ||
| 1517 | fputs("time,", config->output); | ||
| 1518 | fputs(aggr_header_csv[config->aggr_mode], config->output); | ||
| 1519 | } | ||
| 1520 | |||
| 1521 | /* Print metrics headers only */ | ||
| 1522 | evlist__for_each_entry(evlist, counter) { | ||
| 1523 | if (is_duration_time(counter)) | ||
| 1524 | continue; | ||
| 1525 | os.evsel = counter; | ||
| 1526 | out.ctx = &os; | ||
| 1527 | out.print_metric = print_metric_header; | ||
| 1528 | out.new_line = new_line_metric; | ||
| 1529 | out.force_header = true; | ||
| 1530 | os.evsel = counter; | ||
| 1531 | perf_stat__print_shadow_stats(config, counter, 0, | ||
| 1532 | 0, | ||
| 1533 | &out, | ||
| 1534 | &config->metric_events, | ||
| 1535 | &rt_stat); | ||
| 1536 | } | ||
| 1537 | fputc('\n', config->output); | ||
| 1538 | } | ||
| 1539 | |||
| 1540 | static void print_interval(struct perf_stat_config *config, | ||
| 1541 | struct perf_evlist *evlist, | ||
| 1542 | char *prefix, struct timespec *ts) | ||
| 1543 | { | ||
| 1544 | bool metric_only = config->metric_only; | ||
| 1545 | unsigned int unit_width = config->unit_width; | ||
| 1546 | FILE *output = config->output; | ||
| 1547 | static int num_print_interval; | ||
| 1548 | |||
| 1549 | if (config->interval_clear) | ||
| 1550 | puts(CONSOLE_CLEAR); | ||
| 1551 | |||
| 1552 | sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep); | ||
| 1553 | |||
| 1554 | if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) { | ||
| 1555 | switch (config->aggr_mode) { | ||
| 1556 | case AGGR_SOCKET: | ||
| 1557 | fprintf(output, "# time socket cpus"); | ||
| 1558 | if (!metric_only) | ||
| 1559 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 1560 | break; | ||
| 1561 | case AGGR_CORE: | ||
| 1562 | fprintf(output, "# time core cpus"); | ||
| 1563 | if (!metric_only) | ||
| 1564 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 1565 | break; | ||
| 1566 | case AGGR_NONE: | ||
| 1567 | fprintf(output, "# time CPU "); | ||
| 1568 | if (!metric_only) | ||
| 1569 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 1570 | break; | ||
| 1571 | case AGGR_THREAD: | ||
| 1572 | fprintf(output, "# time comm-pid"); | ||
| 1573 | if (!metric_only) | ||
| 1574 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 1575 | break; | ||
| 1576 | case AGGR_GLOBAL: | ||
| 1577 | default: | ||
| 1578 | fprintf(output, "# time"); | ||
| 1579 | if (!metric_only) | ||
| 1580 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 1581 | case AGGR_UNSET: | ||
| 1582 | break; | ||
| 1583 | } | ||
| 1584 | } | ||
| 1585 | |||
| 1586 | if ((num_print_interval == 0 || config->interval_clear) && metric_only) | ||
| 1587 | print_metric_headers(config, evlist, " ", true); | ||
| 1588 | if (++num_print_interval == 25) | ||
| 1589 | num_print_interval = 0; | ||
| 1590 | } | ||
| 1591 | |||
| 1592 | static void print_header(struct perf_stat_config *config, | ||
| 1593 | struct target *_target, | ||
| 1594 | int argc, const char **argv) | ||
| 1595 | { | ||
| 1596 | FILE *output = config->output; | ||
| 1597 | int i; | ||
| 1598 | |||
| 1599 | fflush(stdout); | ||
| 1600 | |||
| 1601 | if (!config->csv_output) { | ||
| 1602 | fprintf(output, "\n"); | ||
| 1603 | fprintf(output, " Performance counter stats for "); | ||
| 1604 | if (_target->system_wide) | ||
| 1605 | fprintf(output, "\'system wide"); | ||
| 1606 | else if (_target->cpu_list) | ||
| 1607 | fprintf(output, "\'CPU(s) %s", _target->cpu_list); | ||
| 1608 | else if (!target__has_task(_target)) { | ||
| 1609 | fprintf(output, "\'%s", argv ? argv[0] : "pipe"); | ||
| 1610 | for (i = 1; argv && (i < argc); i++) | ||
| 1611 | fprintf(output, " %s", argv[i]); | ||
| 1612 | } else if (_target->pid) | ||
| 1613 | fprintf(output, "process id \'%s", _target->pid); | ||
| 1614 | else | ||
| 1615 | fprintf(output, "thread id \'%s", _target->tid); | ||
| 1616 | |||
| 1617 | fprintf(output, "\'"); | ||
| 1618 | if (config->run_count > 1) | ||
| 1619 | fprintf(output, " (%d runs)", config->run_count); | ||
| 1620 | fprintf(output, ":\n\n"); | ||
| 1621 | } | ||
| 1622 | } | ||
| 1623 | |||
| 1624 | static int get_precision(double num) | ||
| 1625 | { | ||
| 1626 | if (num > 1) | ||
| 1627 | return 0; | ||
| 1628 | |||
| 1629 | return lround(ceil(-log10(num))); | ||
| 1630 | } | ||
| 1631 | |||
| 1632 | static void print_table(struct perf_stat_config *config, | ||
| 1633 | FILE *output, int precision, double avg) | ||
| 1634 | { | ||
| 1635 | char tmp[64]; | ||
| 1636 | int idx, indent = 0; | ||
| 1637 | |||
| 1638 | scnprintf(tmp, 64, " %17.*f", precision, avg); | ||
| 1639 | while (tmp[indent] == ' ') | ||
| 1640 | indent++; | ||
| 1641 | |||
| 1642 | fprintf(output, "%*s# Table of individual measurements:\n", indent, ""); | ||
| 1643 | |||
| 1644 | for (idx = 0; idx < config->run_count; idx++) { | ||
| 1645 | double run = (double) config->walltime_run[idx] / NSEC_PER_SEC; | ||
| 1646 | int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5); | ||
| 1647 | |||
| 1648 | fprintf(output, " %17.*f (%+.*f) ", | ||
| 1649 | precision, run, precision, run - avg); | ||
| 1650 | |||
| 1651 | for (h = 0; h < n; h++) | ||
| 1652 | fprintf(output, "#"); | ||
| 1653 | |||
| 1654 | fprintf(output, "\n"); | ||
| 1655 | } | ||
| 1656 | |||
| 1657 | fprintf(output, "\n%*s# Final result:\n", indent, ""); | ||
| 1658 | } | ||
| 1659 | |||
| 1660 | static double timeval2double(struct timeval *t) | ||
| 1661 | { | ||
| 1662 | return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC; | ||
| 1663 | } | ||
| 1664 | |||
| 1665 | static void print_footer(struct perf_stat_config *config) | ||
| 1666 | { | ||
| 1667 | double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC; | ||
| 1668 | FILE *output = config->output; | ||
| 1669 | int n; | ||
| 1670 | |||
| 1671 | if (!config->null_run) | ||
| 1672 | fprintf(output, "\n"); | ||
| 1673 | |||
| 1674 | if (config->run_count == 1) { | ||
| 1675 | fprintf(output, " %17.9f seconds time elapsed", avg); | ||
| 1676 | |||
| 1677 | if (config->ru_display) { | ||
| 1678 | double ru_utime = timeval2double(&config->ru_data.ru_utime); | ||
| 1679 | double ru_stime = timeval2double(&config->ru_data.ru_stime); | ||
| 1680 | |||
| 1681 | fprintf(output, "\n\n"); | ||
| 1682 | fprintf(output, " %17.9f seconds user\n", ru_utime); | ||
| 1683 | fprintf(output, " %17.9f seconds sys\n", ru_stime); | ||
| 1684 | } | ||
| 1685 | } else { | ||
| 1686 | double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC; | ||
| 1687 | /* | ||
| 1688 | * Display at most 2 more significant | ||
| 1689 | * digits than the stddev inaccuracy. | ||
| 1690 | */ | ||
| 1691 | int precision = get_precision(sd) + 2; | ||
| 1692 | |||
| 1693 | if (config->walltime_run_table) | ||
| 1694 | print_table(config, output, precision, avg); | ||
| 1695 | |||
| 1696 | fprintf(output, " %17.*f +- %.*f seconds time elapsed", | ||
| 1697 | precision, avg, precision, sd); | ||
| 1698 | |||
| 1699 | print_noise_pct(config, sd, avg); | ||
| 1700 | } | ||
| 1701 | fprintf(output, "\n\n"); | ||
| 1702 | |||
| 1703 | if (config->print_free_counters_hint && | ||
| 1704 | sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 && | ||
| 1705 | n > 0) | ||
| 1706 | fprintf(output, | ||
| 1707 | "Some events weren't counted. Try disabling the NMI watchdog:\n" | ||
| 1708 | " echo 0 > /proc/sys/kernel/nmi_watchdog\n" | ||
| 1709 | " perf stat ...\n" | ||
| 1710 | " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); | ||
| 1711 | |||
| 1712 | if (config->print_mixed_hw_group_error) | ||
| 1713 | fprintf(output, | ||
| 1714 | "The events in group usually have to be from " | ||
| 1715 | "the same PMU. Try reorganizing the group.\n"); | ||
| 1716 | } | ||
| 1717 | |||
| 1718 | static void | ||
| 1719 | perf_evlist__print_counters(struct perf_evlist *evlist, | ||
| 1720 | struct perf_stat_config *config, | ||
| 1721 | struct target *_target, | ||
| 1722 | struct timespec *ts, | ||
| 1723 | int argc, const char **argv) | ||
| 1724 | { | ||
| 1725 | bool metric_only = config->metric_only; | ||
| 1726 | int interval = config->interval; | ||
| 1727 | struct perf_evsel *counter; | ||
| 1728 | char buf[64], *prefix = NULL; | ||
| 1729 | |||
| 1730 | if (interval) | ||
| 1731 | print_interval(config, evlist, prefix = buf, ts); | ||
| 1732 | else | ||
| 1733 | print_header(config, _target, argc, argv); | ||
| 1734 | |||
| 1735 | if (metric_only) { | ||
| 1736 | static int num_print_iv; | ||
| 1737 | |||
| 1738 | if (num_print_iv == 0 && !interval) | ||
| 1739 | print_metric_headers(config, evlist, prefix, false); | ||
| 1740 | if (num_print_iv++ == 25) | ||
| 1741 | num_print_iv = 0; | ||
| 1742 | if (config->aggr_mode == AGGR_GLOBAL && prefix) | ||
| 1743 | fprintf(config->output, "%s", prefix); | ||
| 1744 | } | ||
| 1745 | |||
| 1746 | switch (config->aggr_mode) { | ||
| 1747 | case AGGR_CORE: | ||
| 1748 | case AGGR_SOCKET: | ||
| 1749 | print_aggr(config, evlist, prefix); | ||
| 1750 | break; | ||
| 1751 | case AGGR_THREAD: | ||
| 1752 | evlist__for_each_entry(evlist, counter) { | ||
| 1753 | if (is_duration_time(counter)) | ||
| 1754 | continue; | ||
| 1755 | print_aggr_thread(config, _target, counter, prefix); | ||
| 1756 | } | ||
| 1757 | break; | ||
| 1758 | case AGGR_GLOBAL: | ||
| 1759 | evlist__for_each_entry(evlist, counter) { | ||
| 1760 | if (is_duration_time(counter)) | ||
| 1761 | continue; | ||
| 1762 | print_counter_aggr(config, counter, prefix); | ||
| 1763 | } | ||
| 1764 | if (metric_only) | ||
| 1765 | fputc('\n', config->output); | ||
| 1766 | break; | ||
| 1767 | case AGGR_NONE: | ||
| 1768 | if (metric_only) | ||
| 1769 | print_no_aggr_metric(config, evlist, prefix); | ||
| 1770 | else { | ||
| 1771 | evlist__for_each_entry(evlist, counter) { | ||
| 1772 | if (is_duration_time(counter)) | ||
| 1773 | continue; | ||
| 1774 | print_counter(config, counter, prefix); | ||
| 1775 | } | ||
| 1776 | } | ||
| 1777 | break; | ||
| 1778 | case AGGR_UNSET: | ||
| 1779 | default: | ||
| 1780 | break; | ||
| 1781 | } | ||
| 1782 | |||
| 1783 | if (!interval && !config->csv_output) | ||
| 1784 | print_footer(config); | ||
| 1785 | |||
| 1786 | fflush(config->output); | ||
| 1787 | } | ||
| 1788 | |||
| 1789 | static void print_counters(struct timespec *ts, int argc, const char **argv) | 639 | static void print_counters(struct timespec *ts, int argc, const char **argv) |
| 1790 | { | 640 | { |
| 1791 | /* Do not print anything if we record to the pipe. */ | 641 | /* Do not print anything if we record to the pipe. */ |
diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 7efe15b9618d..ecd9f9ceda77 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build | |||
| @@ -73,6 +73,7 @@ libperf-y += vdso.o | |||
| 73 | libperf-y += counts.o | 73 | libperf-y += counts.o |
| 74 | libperf-y += stat.o | 74 | libperf-y += stat.o |
| 75 | libperf-y += stat-shadow.o | 75 | libperf-y += stat-shadow.o |
| 76 | libperf-y += stat-display.o | ||
| 76 | libperf-y += record.o | 77 | libperf-y += record.o |
| 77 | libperf-y += srcline.o | 78 | libperf-y += srcline.o |
| 78 | libperf-y += data.o | 79 | libperf-y += data.o |
diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c new file mode 100644 index 000000000000..e7b4c44ebb62 --- /dev/null +++ b/tools/perf/util/stat-display.c | |||
| @@ -0,0 +1,1166 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <inttypes.h> | ||
| 3 | #include <linux/time64.h> | ||
| 4 | #include <math.h> | ||
| 5 | #include "evlist.h" | ||
| 6 | #include "evsel.h" | ||
| 7 | #include "stat.h" | ||
| 8 | #include "top.h" | ||
| 9 | #include "thread_map.h" | ||
| 10 | #include "cpumap.h" | ||
| 11 | #include "string2.h" | ||
| 12 | #include "sane_ctype.h" | ||
| 13 | #include "cgroup.h" | ||
| 14 | #include <math.h> | ||
| 15 | #include <api/fs/fs.h> | ||
| 16 | |||
| 17 | #define CNTR_NOT_SUPPORTED "<not supported>" | ||
| 18 | #define CNTR_NOT_COUNTED "<not counted>" | ||
| 19 | |||
| 20 | static bool is_duration_time(struct perf_evsel *evsel) | ||
| 21 | { | ||
| 22 | return !strcmp(evsel->name, "duration_time"); | ||
| 23 | } | ||
| 24 | |||
| 25 | static void print_running(struct perf_stat_config *config, | ||
| 26 | u64 run, u64 ena) | ||
| 27 | { | ||
| 28 | if (config->csv_output) { | ||
| 29 | fprintf(config->output, "%s%" PRIu64 "%s%.2f", | ||
| 30 | config->csv_sep, | ||
| 31 | run, | ||
| 32 | config->csv_sep, | ||
| 33 | ena ? 100.0 * run / ena : 100.0); | ||
| 34 | } else if (run != ena) { | ||
| 35 | fprintf(config->output, " (%.2f%%)", 100.0 * run / ena); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | static void print_noise_pct(struct perf_stat_config *config, | ||
| 40 | double total, double avg) | ||
| 41 | { | ||
| 42 | double pct = rel_stddev_stats(total, avg); | ||
| 43 | |||
| 44 | if (config->csv_output) | ||
| 45 | fprintf(config->output, "%s%.2f%%", config->csv_sep, pct); | ||
| 46 | else if (pct) | ||
| 47 | fprintf(config->output, " ( +-%6.2f%% )", pct); | ||
| 48 | } | ||
| 49 | |||
| 50 | static void print_noise(struct perf_stat_config *config, | ||
| 51 | struct perf_evsel *evsel, double avg) | ||
| 52 | { | ||
| 53 | struct perf_stat_evsel *ps; | ||
| 54 | |||
| 55 | if (config->run_count == 1) | ||
| 56 | return; | ||
| 57 | |||
| 58 | ps = evsel->stats; | ||
| 59 | print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg); | ||
| 60 | } | ||
| 61 | |||
| 62 | static void aggr_printout(struct perf_stat_config *config, | ||
| 63 | struct perf_evsel *evsel, int id, int nr) | ||
| 64 | { | ||
| 65 | switch (config->aggr_mode) { | ||
| 66 | case AGGR_CORE: | ||
| 67 | fprintf(config->output, "S%d-C%*d%s%*d%s", | ||
| 68 | cpu_map__id_to_socket(id), | ||
| 69 | config->csv_output ? 0 : -8, | ||
| 70 | cpu_map__id_to_cpu(id), | ||
| 71 | config->csv_sep, | ||
| 72 | config->csv_output ? 0 : 4, | ||
| 73 | nr, | ||
| 74 | config->csv_sep); | ||
| 75 | break; | ||
| 76 | case AGGR_SOCKET: | ||
| 77 | fprintf(config->output, "S%*d%s%*d%s", | ||
| 78 | config->csv_output ? 0 : -5, | ||
| 79 | id, | ||
| 80 | config->csv_sep, | ||
| 81 | config->csv_output ? 0 : 4, | ||
| 82 | nr, | ||
| 83 | config->csv_sep); | ||
| 84 | break; | ||
| 85 | case AGGR_NONE: | ||
| 86 | fprintf(config->output, "CPU%*d%s", | ||
| 87 | config->csv_output ? 0 : -4, | ||
| 88 | perf_evsel__cpus(evsel)->map[id], config->csv_sep); | ||
| 89 | break; | ||
| 90 | case AGGR_THREAD: | ||
| 91 | fprintf(config->output, "%*s-%*d%s", | ||
| 92 | config->csv_output ? 0 : 16, | ||
| 93 | thread_map__comm(evsel->threads, id), | ||
| 94 | config->csv_output ? 0 : -8, | ||
| 95 | thread_map__pid(evsel->threads, id), | ||
| 96 | config->csv_sep); | ||
| 97 | break; | ||
| 98 | case AGGR_GLOBAL: | ||
| 99 | case AGGR_UNSET: | ||
| 100 | default: | ||
| 101 | break; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | struct outstate { | ||
| 106 | FILE *fh; | ||
| 107 | bool newline; | ||
| 108 | const char *prefix; | ||
| 109 | int nfields; | ||
| 110 | int id, nr; | ||
| 111 | struct perf_evsel *evsel; | ||
| 112 | }; | ||
| 113 | |||
| 114 | #define METRIC_LEN 35 | ||
| 115 | |||
| 116 | static void new_line_std(struct perf_stat_config *config __maybe_unused, | ||
| 117 | void *ctx) | ||
| 118 | { | ||
| 119 | struct outstate *os = ctx; | ||
| 120 | |||
| 121 | os->newline = true; | ||
| 122 | } | ||
| 123 | |||
| 124 | static void do_new_line_std(struct perf_stat_config *config, | ||
| 125 | struct outstate *os) | ||
| 126 | { | ||
| 127 | fputc('\n', os->fh); | ||
| 128 | fputs(os->prefix, os->fh); | ||
| 129 | aggr_printout(config, os->evsel, os->id, os->nr); | ||
| 130 | if (config->aggr_mode == AGGR_NONE) | ||
| 131 | fprintf(os->fh, " "); | ||
| 132 | fprintf(os->fh, " "); | ||
| 133 | } | ||
| 134 | |||
| 135 | static void print_metric_std(struct perf_stat_config *config, | ||
| 136 | void *ctx, const char *color, const char *fmt, | ||
| 137 | const char *unit, double val) | ||
| 138 | { | ||
| 139 | struct outstate *os = ctx; | ||
| 140 | FILE *out = os->fh; | ||
| 141 | int n; | ||
| 142 | bool newline = os->newline; | ||
| 143 | |||
| 144 | os->newline = false; | ||
| 145 | |||
| 146 | if (unit == NULL || fmt == NULL) { | ||
| 147 | fprintf(out, "%-*s", METRIC_LEN, ""); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | if (newline) | ||
| 152 | do_new_line_std(config, os); | ||
| 153 | |||
| 154 | n = fprintf(out, " # "); | ||
| 155 | if (color) | ||
| 156 | n += color_fprintf(out, color, fmt, val); | ||
| 157 | else | ||
| 158 | n += fprintf(out, fmt, val); | ||
| 159 | fprintf(out, " %-*s", METRIC_LEN - n - 1, unit); | ||
| 160 | } | ||
| 161 | |||
| 162 | static void new_line_csv(struct perf_stat_config *config, void *ctx) | ||
| 163 | { | ||
| 164 | struct outstate *os = ctx; | ||
| 165 | int i; | ||
| 166 | |||
| 167 | fputc('\n', os->fh); | ||
| 168 | if (os->prefix) | ||
| 169 | fprintf(os->fh, "%s%s", os->prefix, config->csv_sep); | ||
| 170 | aggr_printout(config, os->evsel, os->id, os->nr); | ||
| 171 | for (i = 0; i < os->nfields; i++) | ||
| 172 | fputs(config->csv_sep, os->fh); | ||
| 173 | } | ||
| 174 | |||
| 175 | static void print_metric_csv(struct perf_stat_config *config __maybe_unused, | ||
| 176 | void *ctx, | ||
| 177 | const char *color __maybe_unused, | ||
| 178 | const char *fmt, const char *unit, double val) | ||
| 179 | { | ||
| 180 | struct outstate *os = ctx; | ||
| 181 | FILE *out = os->fh; | ||
| 182 | char buf[64], *vals, *ends; | ||
| 183 | |||
| 184 | if (unit == NULL || fmt == NULL) { | ||
| 185 | fprintf(out, "%s%s", config->csv_sep, config->csv_sep); | ||
| 186 | return; | ||
| 187 | } | ||
| 188 | snprintf(buf, sizeof(buf), fmt, val); | ||
| 189 | ends = vals = ltrim(buf); | ||
| 190 | while (isdigit(*ends) || *ends == '.') | ||
| 191 | ends++; | ||
| 192 | *ends = 0; | ||
| 193 | while (isspace(*unit)) | ||
| 194 | unit++; | ||
| 195 | fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, unit); | ||
| 196 | } | ||
| 197 | |||
| 198 | /* Filter out some columns that don't work well in metrics only mode */ | ||
| 199 | |||
| 200 | static bool valid_only_metric(const char *unit) | ||
| 201 | { | ||
| 202 | if (!unit) | ||
| 203 | return false; | ||
| 204 | if (strstr(unit, "/sec") || | ||
| 205 | strstr(unit, "hz") || | ||
| 206 | strstr(unit, "Hz") || | ||
| 207 | strstr(unit, "CPUs utilized")) | ||
| 208 | return false; | ||
| 209 | return true; | ||
| 210 | } | ||
| 211 | |||
| 212 | static const char *fixunit(char *buf, struct perf_evsel *evsel, | ||
| 213 | const char *unit) | ||
| 214 | { | ||
| 215 | if (!strncmp(unit, "of all", 6)) { | ||
| 216 | snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel), | ||
| 217 | unit); | ||
| 218 | return buf; | ||
| 219 | } | ||
| 220 | return unit; | ||
| 221 | } | ||
| 222 | |||
| 223 | static void print_metric_only(struct perf_stat_config *config, | ||
| 224 | void *ctx, const char *color, const char *fmt, | ||
| 225 | const char *unit, double val) | ||
| 226 | { | ||
| 227 | struct outstate *os = ctx; | ||
| 228 | FILE *out = os->fh; | ||
| 229 | char buf[1024], str[1024]; | ||
| 230 | unsigned mlen = config->metric_only_len; | ||
| 231 | |||
| 232 | if (!valid_only_metric(unit)) | ||
| 233 | return; | ||
| 234 | unit = fixunit(buf, os->evsel, unit); | ||
| 235 | if (mlen < strlen(unit)) | ||
| 236 | mlen = strlen(unit) + 1; | ||
| 237 | |||
| 238 | if (color) | ||
| 239 | mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1; | ||
| 240 | |||
| 241 | color_snprintf(str, sizeof(str), color ?: "", fmt, val); | ||
| 242 | fprintf(out, "%*s ", mlen, str); | ||
| 243 | } | ||
| 244 | |||
| 245 | static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused, | ||
| 246 | void *ctx, const char *color __maybe_unused, | ||
| 247 | const char *fmt, | ||
| 248 | const char *unit, double val) | ||
| 249 | { | ||
| 250 | struct outstate *os = ctx; | ||
| 251 | FILE *out = os->fh; | ||
| 252 | char buf[64], *vals, *ends; | ||
| 253 | char tbuf[1024]; | ||
| 254 | |||
| 255 | if (!valid_only_metric(unit)) | ||
| 256 | return; | ||
| 257 | unit = fixunit(tbuf, os->evsel, unit); | ||
| 258 | snprintf(buf, sizeof buf, fmt, val); | ||
| 259 | ends = vals = ltrim(buf); | ||
| 260 | while (isdigit(*ends) || *ends == '.') | ||
| 261 | ends++; | ||
| 262 | *ends = 0; | ||
| 263 | fprintf(out, "%s%s", vals, config->csv_sep); | ||
| 264 | } | ||
| 265 | |||
| 266 | static void new_line_metric(struct perf_stat_config *config __maybe_unused, | ||
| 267 | void *ctx __maybe_unused) | ||
| 268 | { | ||
| 269 | } | ||
| 270 | |||
| 271 | static void print_metric_header(struct perf_stat_config *config, | ||
| 272 | void *ctx, const char *color __maybe_unused, | ||
| 273 | const char *fmt __maybe_unused, | ||
| 274 | const char *unit, double val __maybe_unused) | ||
| 275 | { | ||
| 276 | struct outstate *os = ctx; | ||
| 277 | char tbuf[1024]; | ||
| 278 | |||
| 279 | if (!valid_only_metric(unit)) | ||
| 280 | return; | ||
| 281 | unit = fixunit(tbuf, os->evsel, unit); | ||
| 282 | if (config->csv_output) | ||
| 283 | fprintf(os->fh, "%s%s", unit, config->csv_sep); | ||
| 284 | else | ||
| 285 | fprintf(os->fh, "%*s ", config->metric_only_len, unit); | ||
| 286 | } | ||
| 287 | |||
| 288 | static int first_shadow_cpu(struct perf_stat_config *config, | ||
| 289 | struct perf_evsel *evsel, int id) | ||
| 290 | { | ||
| 291 | struct perf_evlist *evlist = evsel->evlist; | ||
| 292 | int i; | ||
| 293 | |||
| 294 | if (!config->aggr_get_id) | ||
| 295 | return 0; | ||
| 296 | |||
| 297 | if (config->aggr_mode == AGGR_NONE) | ||
| 298 | return id; | ||
| 299 | |||
| 300 | if (config->aggr_mode == AGGR_GLOBAL) | ||
| 301 | return 0; | ||
| 302 | |||
| 303 | for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) { | ||
| 304 | int cpu2 = perf_evsel__cpus(evsel)->map[i]; | ||
| 305 | |||
| 306 | if (config->aggr_get_id(config, evlist->cpus, cpu2) == id) | ||
| 307 | return cpu2; | ||
| 308 | } | ||
| 309 | return 0; | ||
| 310 | } | ||
| 311 | |||
| 312 | static void abs_printout(struct perf_stat_config *config, | ||
| 313 | int id, int nr, struct perf_evsel *evsel, double avg) | ||
| 314 | { | ||
| 315 | FILE *output = config->output; | ||
| 316 | double sc = evsel->scale; | ||
| 317 | const char *fmt; | ||
| 318 | |||
| 319 | if (config->csv_output) { | ||
| 320 | fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s"; | ||
| 321 | } else { | ||
| 322 | if (config->big_num) | ||
| 323 | fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s"; | ||
| 324 | else | ||
| 325 | fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s"; | ||
| 326 | } | ||
| 327 | |||
| 328 | aggr_printout(config, evsel, id, nr); | ||
| 329 | |||
| 330 | fprintf(output, fmt, avg, config->csv_sep); | ||
| 331 | |||
| 332 | if (evsel->unit) | ||
| 333 | fprintf(output, "%-*s%s", | ||
| 334 | config->csv_output ? 0 : config->unit_width, | ||
| 335 | evsel->unit, config->csv_sep); | ||
| 336 | |||
| 337 | fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel)); | ||
| 338 | |||
| 339 | if (evsel->cgrp) | ||
| 340 | fprintf(output, "%s%s", config->csv_sep, evsel->cgrp->name); | ||
| 341 | } | ||
| 342 | |||
| 343 | static bool is_mixed_hw_group(struct perf_evsel *counter) | ||
| 344 | { | ||
| 345 | struct perf_evlist *evlist = counter->evlist; | ||
| 346 | u32 pmu_type = counter->attr.type; | ||
| 347 | struct perf_evsel *pos; | ||
| 348 | |||
| 349 | if (counter->nr_members < 2) | ||
| 350 | return false; | ||
| 351 | |||
| 352 | evlist__for_each_entry(evlist, pos) { | ||
| 353 | /* software events can be part of any hardware group */ | ||
| 354 | if (pos->attr.type == PERF_TYPE_SOFTWARE) | ||
| 355 | continue; | ||
| 356 | if (pmu_type == PERF_TYPE_SOFTWARE) { | ||
| 357 | pmu_type = pos->attr.type; | ||
| 358 | continue; | ||
| 359 | } | ||
| 360 | if (pmu_type != pos->attr.type) | ||
| 361 | return true; | ||
| 362 | } | ||
| 363 | |||
| 364 | return false; | ||
| 365 | } | ||
| 366 | |||
| 367 | static void printout(struct perf_stat_config *config, int id, int nr, | ||
| 368 | struct perf_evsel *counter, double uval, | ||
| 369 | char *prefix, u64 run, u64 ena, double noise, | ||
| 370 | struct runtime_stat *st) | ||
| 371 | { | ||
| 372 | struct perf_stat_output_ctx out; | ||
| 373 | struct outstate os = { | ||
| 374 | .fh = config->output, | ||
| 375 | .prefix = prefix ? prefix : "", | ||
| 376 | .id = id, | ||
| 377 | .nr = nr, | ||
| 378 | .evsel = counter, | ||
| 379 | }; | ||
| 380 | print_metric_t pm = print_metric_std; | ||
| 381 | new_line_t nl; | ||
| 382 | |||
| 383 | if (config->metric_only) { | ||
| 384 | nl = new_line_metric; | ||
| 385 | if (config->csv_output) | ||
| 386 | pm = print_metric_only_csv; | ||
| 387 | else | ||
| 388 | pm = print_metric_only; | ||
| 389 | } else | ||
| 390 | nl = new_line_std; | ||
| 391 | |||
| 392 | if (config->csv_output && !config->metric_only) { | ||
| 393 | static int aggr_fields[] = { | ||
| 394 | [AGGR_GLOBAL] = 0, | ||
| 395 | [AGGR_THREAD] = 1, | ||
| 396 | [AGGR_NONE] = 1, | ||
| 397 | [AGGR_SOCKET] = 2, | ||
| 398 | [AGGR_CORE] = 2, | ||
| 399 | }; | ||
| 400 | |||
| 401 | pm = print_metric_csv; | ||
| 402 | nl = new_line_csv; | ||
| 403 | os.nfields = 3; | ||
| 404 | os.nfields += aggr_fields[config->aggr_mode]; | ||
| 405 | if (counter->cgrp) | ||
| 406 | os.nfields++; | ||
| 407 | } | ||
| 408 | if (run == 0 || ena == 0 || counter->counts->scaled == -1) { | ||
| 409 | if (config->metric_only) { | ||
| 410 | pm(config, &os, NULL, "", "", 0); | ||
| 411 | return; | ||
| 412 | } | ||
| 413 | aggr_printout(config, counter, id, nr); | ||
| 414 | |||
| 415 | fprintf(config->output, "%*s%s", | ||
| 416 | config->csv_output ? 0 : 18, | ||
| 417 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, | ||
| 418 | config->csv_sep); | ||
| 419 | |||
| 420 | if (counter->supported) { | ||
| 421 | config->print_free_counters_hint = 1; | ||
| 422 | if (is_mixed_hw_group(counter)) | ||
| 423 | config->print_mixed_hw_group_error = 1; | ||
| 424 | } | ||
| 425 | |||
| 426 | fprintf(config->output, "%-*s%s", | ||
| 427 | config->csv_output ? 0 : config->unit_width, | ||
| 428 | counter->unit, config->csv_sep); | ||
| 429 | |||
| 430 | fprintf(config->output, "%*s", | ||
| 431 | config->csv_output ? 0 : -25, | ||
| 432 | perf_evsel__name(counter)); | ||
| 433 | |||
| 434 | if (counter->cgrp) | ||
| 435 | fprintf(config->output, "%s%s", | ||
| 436 | config->csv_sep, counter->cgrp->name); | ||
| 437 | |||
| 438 | if (!config->csv_output) | ||
| 439 | pm(config, &os, NULL, NULL, "", 0); | ||
| 440 | print_noise(config, counter, noise); | ||
| 441 | print_running(config, run, ena); | ||
| 442 | if (config->csv_output) | ||
| 443 | pm(config, &os, NULL, NULL, "", 0); | ||
| 444 | return; | ||
| 445 | } | ||
| 446 | |||
| 447 | if (!config->metric_only) | ||
| 448 | abs_printout(config, id, nr, counter, uval); | ||
| 449 | |||
| 450 | out.print_metric = pm; | ||
| 451 | out.new_line = nl; | ||
| 452 | out.ctx = &os; | ||
| 453 | out.force_header = false; | ||
| 454 | |||
| 455 | if (config->csv_output && !config->metric_only) { | ||
| 456 | print_noise(config, counter, noise); | ||
| 457 | print_running(config, run, ena); | ||
| 458 | } | ||
| 459 | |||
| 460 | perf_stat__print_shadow_stats(config, counter, uval, | ||
| 461 | first_shadow_cpu(config, counter, id), | ||
| 462 | &out, &config->metric_events, st); | ||
| 463 | if (!config->csv_output && !config->metric_only) { | ||
| 464 | print_noise(config, counter, noise); | ||
| 465 | print_running(config, run, ena); | ||
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | static void aggr_update_shadow(struct perf_stat_config *config, | ||
| 470 | struct perf_evlist *evlist) | ||
| 471 | { | ||
| 472 | int cpu, s2, id, s; | ||
| 473 | u64 val; | ||
| 474 | struct perf_evsel *counter; | ||
| 475 | |||
| 476 | for (s = 0; s < config->aggr_map->nr; s++) { | ||
| 477 | id = config->aggr_map->map[s]; | ||
| 478 | evlist__for_each_entry(evlist, counter) { | ||
| 479 | val = 0; | ||
| 480 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | ||
| 481 | s2 = config->aggr_get_id(config, evlist->cpus, cpu); | ||
| 482 | if (s2 != id) | ||
| 483 | continue; | ||
| 484 | val += perf_counts(counter->counts, cpu, 0)->val; | ||
| 485 | } | ||
| 486 | perf_stat__update_shadow_stats(counter, val, | ||
| 487 | first_shadow_cpu(config, counter, id), | ||
| 488 | &rt_stat); | ||
| 489 | } | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | static void uniquify_event_name(struct perf_evsel *counter) | ||
| 494 | { | ||
| 495 | char *new_name; | ||
| 496 | char *config; | ||
| 497 | |||
| 498 | if (counter->uniquified_name || | ||
| 499 | !counter->pmu_name || !strncmp(counter->name, counter->pmu_name, | ||
| 500 | strlen(counter->pmu_name))) | ||
| 501 | return; | ||
| 502 | |||
| 503 | config = strchr(counter->name, '/'); | ||
| 504 | if (config) { | ||
| 505 | if (asprintf(&new_name, | ||
| 506 | "%s%s", counter->pmu_name, config) > 0) { | ||
| 507 | free(counter->name); | ||
| 508 | counter->name = new_name; | ||
| 509 | } | ||
| 510 | } else { | ||
| 511 | if (asprintf(&new_name, | ||
| 512 | "%s [%s]", counter->name, counter->pmu_name) > 0) { | ||
| 513 | free(counter->name); | ||
| 514 | counter->name = new_name; | ||
| 515 | } | ||
| 516 | } | ||
| 517 | |||
| 518 | counter->uniquified_name = true; | ||
| 519 | } | ||
| 520 | |||
| 521 | static void collect_all_aliases(struct perf_stat_config *config, struct perf_evsel *counter, | ||
| 522 | void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data, | ||
| 523 | bool first), | ||
| 524 | void *data) | ||
| 525 | { | ||
| 526 | struct perf_evlist *evlist = counter->evlist; | ||
| 527 | struct perf_evsel *alias; | ||
| 528 | |||
| 529 | alias = list_prepare_entry(counter, &(evlist->entries), node); | ||
| 530 | list_for_each_entry_continue (alias, &evlist->entries, node) { | ||
| 531 | if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) || | ||
| 532 | alias->scale != counter->scale || | ||
| 533 | alias->cgrp != counter->cgrp || | ||
| 534 | strcmp(alias->unit, counter->unit) || | ||
| 535 | perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter)) | ||
| 536 | break; | ||
| 537 | alias->merged_stat = true; | ||
| 538 | cb(config, alias, data, false); | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | static bool collect_data(struct perf_stat_config *config, struct perf_evsel *counter, | ||
| 543 | void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data, | ||
| 544 | bool first), | ||
| 545 | void *data) | ||
| 546 | { | ||
| 547 | if (counter->merged_stat) | ||
| 548 | return false; | ||
| 549 | cb(config, counter, data, true); | ||
| 550 | if (config->no_merge) | ||
| 551 | uniquify_event_name(counter); | ||
| 552 | else if (counter->auto_merge_stats) | ||
| 553 | collect_all_aliases(config, counter, cb, data); | ||
| 554 | return true; | ||
| 555 | } | ||
| 556 | |||
| 557 | struct aggr_data { | ||
| 558 | u64 ena, run, val; | ||
| 559 | int id; | ||
| 560 | int nr; | ||
| 561 | int cpu; | ||
| 562 | }; | ||
| 563 | |||
| 564 | static void aggr_cb(struct perf_stat_config *config, | ||
| 565 | struct perf_evsel *counter, void *data, bool first) | ||
| 566 | { | ||
| 567 | struct aggr_data *ad = data; | ||
| 568 | int cpu, s2; | ||
| 569 | |||
| 570 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | ||
| 571 | struct perf_counts_values *counts; | ||
| 572 | |||
| 573 | s2 = config->aggr_get_id(config, perf_evsel__cpus(counter), cpu); | ||
| 574 | if (s2 != ad->id) | ||
| 575 | continue; | ||
| 576 | if (first) | ||
| 577 | ad->nr++; | ||
| 578 | counts = perf_counts(counter->counts, cpu, 0); | ||
| 579 | /* | ||
| 580 | * When any result is bad, make them all to give | ||
| 581 | * consistent output in interval mode. | ||
| 582 | */ | ||
| 583 | if (counts->ena == 0 || counts->run == 0 || | ||
| 584 | counter->counts->scaled == -1) { | ||
| 585 | ad->ena = 0; | ||
| 586 | ad->run = 0; | ||
| 587 | break; | ||
| 588 | } | ||
| 589 | ad->val += counts->val; | ||
| 590 | ad->ena += counts->ena; | ||
| 591 | ad->run += counts->run; | ||
| 592 | } | ||
| 593 | } | ||
| 594 | |||
| 595 | static void print_aggr(struct perf_stat_config *config, | ||
| 596 | struct perf_evlist *evlist, | ||
| 597 | char *prefix) | ||
| 598 | { | ||
| 599 | bool metric_only = config->metric_only; | ||
| 600 | FILE *output = config->output; | ||
| 601 | struct perf_evsel *counter; | ||
| 602 | int s, id, nr; | ||
| 603 | double uval; | ||
| 604 | u64 ena, run, val; | ||
| 605 | bool first; | ||
| 606 | |||
| 607 | if (!(config->aggr_map || config->aggr_get_id)) | ||
| 608 | return; | ||
| 609 | |||
| 610 | aggr_update_shadow(config, evlist); | ||
| 611 | |||
| 612 | /* | ||
| 613 | * With metric_only everything is on a single line. | ||
| 614 | * Without each counter has its own line. | ||
| 615 | */ | ||
| 616 | for (s = 0; s < config->aggr_map->nr; s++) { | ||
| 617 | struct aggr_data ad; | ||
| 618 | if (prefix && metric_only) | ||
| 619 | fprintf(output, "%s", prefix); | ||
| 620 | |||
| 621 | ad.id = id = config->aggr_map->map[s]; | ||
| 622 | first = true; | ||
| 623 | evlist__for_each_entry(evlist, counter) { | ||
| 624 | if (is_duration_time(counter)) | ||
| 625 | continue; | ||
| 626 | |||
| 627 | ad.val = ad.ena = ad.run = 0; | ||
| 628 | ad.nr = 0; | ||
| 629 | if (!collect_data(config, counter, aggr_cb, &ad)) | ||
| 630 | continue; | ||
| 631 | nr = ad.nr; | ||
| 632 | ena = ad.ena; | ||
| 633 | run = ad.run; | ||
| 634 | val = ad.val; | ||
| 635 | if (first && metric_only) { | ||
| 636 | first = false; | ||
| 637 | aggr_printout(config, counter, id, nr); | ||
| 638 | } | ||
| 639 | if (prefix && !metric_only) | ||
| 640 | fprintf(output, "%s", prefix); | ||
| 641 | |||
| 642 | uval = val * counter->scale; | ||
| 643 | printout(config, id, nr, counter, uval, prefix, | ||
| 644 | run, ena, 1.0, &rt_stat); | ||
| 645 | if (!metric_only) | ||
| 646 | fputc('\n', output); | ||
| 647 | } | ||
| 648 | if (metric_only) | ||
| 649 | fputc('\n', output); | ||
| 650 | } | ||
| 651 | } | ||
| 652 | |||
| 653 | static int cmp_val(const void *a, const void *b) | ||
| 654 | { | ||
| 655 | return ((struct perf_aggr_thread_value *)b)->val - | ||
| 656 | ((struct perf_aggr_thread_value *)a)->val; | ||
| 657 | } | ||
| 658 | |||
| 659 | static struct perf_aggr_thread_value *sort_aggr_thread( | ||
| 660 | struct perf_evsel *counter, | ||
| 661 | int nthreads, int ncpus, | ||
| 662 | int *ret, | ||
| 663 | struct target *_target) | ||
| 664 | { | ||
| 665 | int cpu, thread, i = 0; | ||
| 666 | double uval; | ||
| 667 | struct perf_aggr_thread_value *buf; | ||
| 668 | |||
| 669 | buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value)); | ||
| 670 | if (!buf) | ||
| 671 | return NULL; | ||
| 672 | |||
| 673 | for (thread = 0; thread < nthreads; thread++) { | ||
| 674 | u64 ena = 0, run = 0, val = 0; | ||
| 675 | |||
| 676 | for (cpu = 0; cpu < ncpus; cpu++) { | ||
| 677 | val += perf_counts(counter->counts, cpu, thread)->val; | ||
| 678 | ena += perf_counts(counter->counts, cpu, thread)->ena; | ||
| 679 | run += perf_counts(counter->counts, cpu, thread)->run; | ||
| 680 | } | ||
| 681 | |||
| 682 | uval = val * counter->scale; | ||
| 683 | |||
| 684 | /* | ||
| 685 | * Skip value 0 when enabling --per-thread globally, | ||
| 686 | * otherwise too many 0 output. | ||
| 687 | */ | ||
| 688 | if (uval == 0.0 && target__has_per_thread(_target)) | ||
| 689 | continue; | ||
| 690 | |||
| 691 | buf[i].counter = counter; | ||
| 692 | buf[i].id = thread; | ||
| 693 | buf[i].uval = uval; | ||
| 694 | buf[i].val = val; | ||
| 695 | buf[i].run = run; | ||
| 696 | buf[i].ena = ena; | ||
| 697 | i++; | ||
| 698 | } | ||
| 699 | |||
| 700 | qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val); | ||
| 701 | |||
| 702 | if (ret) | ||
| 703 | *ret = i; | ||
| 704 | |||
| 705 | return buf; | ||
| 706 | } | ||
| 707 | |||
| 708 | static void print_aggr_thread(struct perf_stat_config *config, | ||
| 709 | struct target *_target, | ||
| 710 | struct perf_evsel *counter, char *prefix) | ||
| 711 | { | ||
| 712 | FILE *output = config->output; | ||
| 713 | int nthreads = thread_map__nr(counter->threads); | ||
| 714 | int ncpus = cpu_map__nr(counter->cpus); | ||
| 715 | int thread, sorted_threads, id; | ||
| 716 | struct perf_aggr_thread_value *buf; | ||
| 717 | |||
| 718 | buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target); | ||
| 719 | if (!buf) { | ||
| 720 | perror("cannot sort aggr thread"); | ||
| 721 | return; | ||
| 722 | } | ||
| 723 | |||
| 724 | for (thread = 0; thread < sorted_threads; thread++) { | ||
| 725 | if (prefix) | ||
| 726 | fprintf(output, "%s", prefix); | ||
| 727 | |||
| 728 | id = buf[thread].id; | ||
| 729 | if (config->stats) | ||
| 730 | printout(config, id, 0, buf[thread].counter, buf[thread].uval, | ||
| 731 | prefix, buf[thread].run, buf[thread].ena, 1.0, | ||
| 732 | &config->stats[id]); | ||
| 733 | else | ||
| 734 | printout(config, id, 0, buf[thread].counter, buf[thread].uval, | ||
| 735 | prefix, buf[thread].run, buf[thread].ena, 1.0, | ||
| 736 | &rt_stat); | ||
| 737 | fputc('\n', output); | ||
| 738 | } | ||
| 739 | |||
| 740 | free(buf); | ||
| 741 | } | ||
| 742 | |||
| 743 | struct caggr_data { | ||
| 744 | double avg, avg_enabled, avg_running; | ||
| 745 | }; | ||
| 746 | |||
| 747 | static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused, | ||
| 748 | struct perf_evsel *counter, void *data, | ||
| 749 | bool first __maybe_unused) | ||
| 750 | { | ||
| 751 | struct caggr_data *cd = data; | ||
| 752 | struct perf_stat_evsel *ps = counter->stats; | ||
| 753 | |||
| 754 | cd->avg += avg_stats(&ps->res_stats[0]); | ||
| 755 | cd->avg_enabled += avg_stats(&ps->res_stats[1]); | ||
| 756 | cd->avg_running += avg_stats(&ps->res_stats[2]); | ||
| 757 | } | ||
| 758 | |||
| 759 | /* | ||
| 760 | * Print out the results of a single counter: | ||
| 761 | * aggregated counts in system-wide mode | ||
| 762 | */ | ||
| 763 | static void print_counter_aggr(struct perf_stat_config *config, | ||
| 764 | struct perf_evsel *counter, char *prefix) | ||
| 765 | { | ||
| 766 | bool metric_only = config->metric_only; | ||
| 767 | FILE *output = config->output; | ||
| 768 | double uval; | ||
| 769 | struct caggr_data cd = { .avg = 0.0 }; | ||
| 770 | |||
| 771 | if (!collect_data(config, counter, counter_aggr_cb, &cd)) | ||
| 772 | return; | ||
| 773 | |||
| 774 | if (prefix && !metric_only) | ||
| 775 | fprintf(output, "%s", prefix); | ||
| 776 | |||
| 777 | uval = cd.avg * counter->scale; | ||
| 778 | printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, | ||
| 779 | cd.avg, &rt_stat); | ||
| 780 | if (!metric_only) | ||
| 781 | fprintf(output, "\n"); | ||
| 782 | } | ||
| 783 | |||
| 784 | static void counter_cb(struct perf_stat_config *config __maybe_unused, | ||
| 785 | struct perf_evsel *counter, void *data, | ||
| 786 | bool first __maybe_unused) | ||
| 787 | { | ||
| 788 | struct aggr_data *ad = data; | ||
| 789 | |||
| 790 | ad->val += perf_counts(counter->counts, ad->cpu, 0)->val; | ||
| 791 | ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena; | ||
| 792 | ad->run += perf_counts(counter->counts, ad->cpu, 0)->run; | ||
| 793 | } | ||
| 794 | |||
| 795 | /* | ||
| 796 | * Print out the results of a single counter: | ||
| 797 | * does not use aggregated count in system-wide | ||
| 798 | */ | ||
| 799 | static void print_counter(struct perf_stat_config *config, | ||
| 800 | struct perf_evsel *counter, char *prefix) | ||
| 801 | { | ||
| 802 | FILE *output = config->output; | ||
| 803 | u64 ena, run, val; | ||
| 804 | double uval; | ||
| 805 | int cpu; | ||
| 806 | |||
| 807 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | ||
| 808 | struct aggr_data ad = { .cpu = cpu }; | ||
| 809 | |||
| 810 | if (!collect_data(config, counter, counter_cb, &ad)) | ||
| 811 | return; | ||
| 812 | val = ad.val; | ||
| 813 | ena = ad.ena; | ||
| 814 | run = ad.run; | ||
| 815 | |||
| 816 | if (prefix) | ||
| 817 | fprintf(output, "%s", prefix); | ||
| 818 | |||
| 819 | uval = val * counter->scale; | ||
| 820 | printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0, | ||
| 821 | &rt_stat); | ||
| 822 | |||
| 823 | fputc('\n', output); | ||
| 824 | } | ||
| 825 | } | ||
| 826 | |||
| 827 | static void print_no_aggr_metric(struct perf_stat_config *config, | ||
| 828 | struct perf_evlist *evlist, | ||
| 829 | char *prefix) | ||
| 830 | { | ||
| 831 | int cpu; | ||
| 832 | int nrcpus = 0; | ||
| 833 | struct perf_evsel *counter; | ||
| 834 | u64 ena, run, val; | ||
| 835 | double uval; | ||
| 836 | |||
| 837 | nrcpus = evlist->cpus->nr; | ||
| 838 | for (cpu = 0; cpu < nrcpus; cpu++) { | ||
| 839 | bool first = true; | ||
| 840 | |||
| 841 | if (prefix) | ||
| 842 | fputs(prefix, config->output); | ||
| 843 | evlist__for_each_entry(evlist, counter) { | ||
| 844 | if (is_duration_time(counter)) | ||
| 845 | continue; | ||
| 846 | if (first) { | ||
| 847 | aggr_printout(config, counter, cpu, 0); | ||
| 848 | first = false; | ||
| 849 | } | ||
| 850 | val = perf_counts(counter->counts, cpu, 0)->val; | ||
| 851 | ena = perf_counts(counter->counts, cpu, 0)->ena; | ||
| 852 | run = perf_counts(counter->counts, cpu, 0)->run; | ||
| 853 | |||
| 854 | uval = val * counter->scale; | ||
| 855 | printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0, | ||
| 856 | &rt_stat); | ||
| 857 | } | ||
| 858 | fputc('\n', config->output); | ||
| 859 | } | ||
| 860 | } | ||
| 861 | |||
| 862 | static int aggr_header_lens[] = { | ||
| 863 | [AGGR_CORE] = 18, | ||
| 864 | [AGGR_SOCKET] = 12, | ||
| 865 | [AGGR_NONE] = 6, | ||
| 866 | [AGGR_THREAD] = 24, | ||
| 867 | [AGGR_GLOBAL] = 0, | ||
| 868 | }; | ||
| 869 | |||
| 870 | static const char *aggr_header_csv[] = { | ||
| 871 | [AGGR_CORE] = "core,cpus,", | ||
| 872 | [AGGR_SOCKET] = "socket,cpus", | ||
| 873 | [AGGR_NONE] = "cpu,", | ||
| 874 | [AGGR_THREAD] = "comm-pid,", | ||
| 875 | [AGGR_GLOBAL] = "" | ||
| 876 | }; | ||
| 877 | |||
| 878 | static void print_metric_headers(struct perf_stat_config *config, | ||
| 879 | struct perf_evlist *evlist, | ||
| 880 | const char *prefix, bool no_indent) | ||
| 881 | { | ||
| 882 | struct perf_stat_output_ctx out; | ||
| 883 | struct perf_evsel *counter; | ||
| 884 | struct outstate os = { | ||
| 885 | .fh = config->output | ||
| 886 | }; | ||
| 887 | |||
| 888 | if (prefix) | ||
| 889 | fprintf(config->output, "%s", prefix); | ||
| 890 | |||
| 891 | if (!config->csv_output && !no_indent) | ||
| 892 | fprintf(config->output, "%*s", | ||
| 893 | aggr_header_lens[config->aggr_mode], ""); | ||
| 894 | if (config->csv_output) { | ||
| 895 | if (config->interval) | ||
| 896 | fputs("time,", config->output); | ||
| 897 | fputs(aggr_header_csv[config->aggr_mode], config->output); | ||
| 898 | } | ||
| 899 | |||
| 900 | /* Print metrics headers only */ | ||
| 901 | evlist__for_each_entry(evlist, counter) { | ||
| 902 | if (is_duration_time(counter)) | ||
| 903 | continue; | ||
| 904 | os.evsel = counter; | ||
| 905 | out.ctx = &os; | ||
| 906 | out.print_metric = print_metric_header; | ||
| 907 | out.new_line = new_line_metric; | ||
| 908 | out.force_header = true; | ||
| 909 | os.evsel = counter; | ||
| 910 | perf_stat__print_shadow_stats(config, counter, 0, | ||
| 911 | 0, | ||
| 912 | &out, | ||
| 913 | &config->metric_events, | ||
| 914 | &rt_stat); | ||
| 915 | } | ||
| 916 | fputc('\n', config->output); | ||
| 917 | } | ||
| 918 | |||
| 919 | static void print_interval(struct perf_stat_config *config, | ||
| 920 | struct perf_evlist *evlist, | ||
| 921 | char *prefix, struct timespec *ts) | ||
| 922 | { | ||
| 923 | bool metric_only = config->metric_only; | ||
| 924 | unsigned int unit_width = config->unit_width; | ||
| 925 | FILE *output = config->output; | ||
| 926 | static int num_print_interval; | ||
| 927 | |||
| 928 | if (config->interval_clear) | ||
| 929 | puts(CONSOLE_CLEAR); | ||
| 930 | |||
| 931 | sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep); | ||
| 932 | |||
| 933 | if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) { | ||
| 934 | switch (config->aggr_mode) { | ||
| 935 | case AGGR_SOCKET: | ||
| 936 | fprintf(output, "# time socket cpus"); | ||
| 937 | if (!metric_only) | ||
| 938 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 939 | break; | ||
| 940 | case AGGR_CORE: | ||
| 941 | fprintf(output, "# time core cpus"); | ||
| 942 | if (!metric_only) | ||
| 943 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 944 | break; | ||
| 945 | case AGGR_NONE: | ||
| 946 | fprintf(output, "# time CPU "); | ||
| 947 | if (!metric_only) | ||
| 948 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 949 | break; | ||
| 950 | case AGGR_THREAD: | ||
| 951 | fprintf(output, "# time comm-pid"); | ||
| 952 | if (!metric_only) | ||
| 953 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 954 | break; | ||
| 955 | case AGGR_GLOBAL: | ||
| 956 | default: | ||
| 957 | fprintf(output, "# time"); | ||
| 958 | if (!metric_only) | ||
| 959 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | ||
| 960 | case AGGR_UNSET: | ||
| 961 | break; | ||
| 962 | } | ||
| 963 | } | ||
| 964 | |||
| 965 | if ((num_print_interval == 0 || config->interval_clear) && metric_only) | ||
| 966 | print_metric_headers(config, evlist, " ", true); | ||
| 967 | if (++num_print_interval == 25) | ||
| 968 | num_print_interval = 0; | ||
| 969 | } | ||
| 970 | |||
| 971 | static void print_header(struct perf_stat_config *config, | ||
| 972 | struct target *_target, | ||
| 973 | int argc, const char **argv) | ||
| 974 | { | ||
| 975 | FILE *output = config->output; | ||
| 976 | int i; | ||
| 977 | |||
| 978 | fflush(stdout); | ||
| 979 | |||
| 980 | if (!config->csv_output) { | ||
| 981 | fprintf(output, "\n"); | ||
| 982 | fprintf(output, " Performance counter stats for "); | ||
| 983 | if (_target->system_wide) | ||
| 984 | fprintf(output, "\'system wide"); | ||
| 985 | else if (_target->cpu_list) | ||
| 986 | fprintf(output, "\'CPU(s) %s", _target->cpu_list); | ||
| 987 | else if (!target__has_task(_target)) { | ||
| 988 | fprintf(output, "\'%s", argv ? argv[0] : "pipe"); | ||
| 989 | for (i = 1; argv && (i < argc); i++) | ||
| 990 | fprintf(output, " %s", argv[i]); | ||
| 991 | } else if (_target->pid) | ||
| 992 | fprintf(output, "process id \'%s", _target->pid); | ||
| 993 | else | ||
| 994 | fprintf(output, "thread id \'%s", _target->tid); | ||
| 995 | |||
| 996 | fprintf(output, "\'"); | ||
| 997 | if (config->run_count > 1) | ||
| 998 | fprintf(output, " (%d runs)", config->run_count); | ||
| 999 | fprintf(output, ":\n\n"); | ||
| 1000 | } | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | static int get_precision(double num) | ||
| 1004 | { | ||
| 1005 | if (num > 1) | ||
| 1006 | return 0; | ||
| 1007 | |||
| 1008 | return lround(ceil(-log10(num))); | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | static void print_table(struct perf_stat_config *config, | ||
| 1012 | FILE *output, int precision, double avg) | ||
| 1013 | { | ||
| 1014 | char tmp[64]; | ||
| 1015 | int idx, indent = 0; | ||
| 1016 | |||
| 1017 | scnprintf(tmp, 64, " %17.*f", precision, avg); | ||
| 1018 | while (tmp[indent] == ' ') | ||
| 1019 | indent++; | ||
| 1020 | |||
| 1021 | fprintf(output, "%*s# Table of individual measurements:\n", indent, ""); | ||
| 1022 | |||
| 1023 | for (idx = 0; idx < config->run_count; idx++) { | ||
| 1024 | double run = (double) config->walltime_run[idx] / NSEC_PER_SEC; | ||
| 1025 | int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5); | ||
| 1026 | |||
| 1027 | fprintf(output, " %17.*f (%+.*f) ", | ||
| 1028 | precision, run, precision, run - avg); | ||
| 1029 | |||
| 1030 | for (h = 0; h < n; h++) | ||
| 1031 | fprintf(output, "#"); | ||
| 1032 | |||
| 1033 | fprintf(output, "\n"); | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | fprintf(output, "\n%*s# Final result:\n", indent, ""); | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | static double timeval2double(struct timeval *t) | ||
| 1040 | { | ||
| 1041 | return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | static void print_footer(struct perf_stat_config *config) | ||
| 1045 | { | ||
| 1046 | double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC; | ||
| 1047 | FILE *output = config->output; | ||
| 1048 | int n; | ||
| 1049 | |||
| 1050 | if (!config->null_run) | ||
| 1051 | fprintf(output, "\n"); | ||
| 1052 | |||
| 1053 | if (config->run_count == 1) { | ||
| 1054 | fprintf(output, " %17.9f seconds time elapsed", avg); | ||
| 1055 | |||
| 1056 | if (config->ru_display) { | ||
| 1057 | double ru_utime = timeval2double(&config->ru_data.ru_utime); | ||
| 1058 | double ru_stime = timeval2double(&config->ru_data.ru_stime); | ||
| 1059 | |||
| 1060 | fprintf(output, "\n\n"); | ||
| 1061 | fprintf(output, " %17.9f seconds user\n", ru_utime); | ||
| 1062 | fprintf(output, " %17.9f seconds sys\n", ru_stime); | ||
| 1063 | } | ||
| 1064 | } else { | ||
| 1065 | double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC; | ||
| 1066 | /* | ||
| 1067 | * Display at most 2 more significant | ||
| 1068 | * digits than the stddev inaccuracy. | ||
| 1069 | */ | ||
| 1070 | int precision = get_precision(sd) + 2; | ||
| 1071 | |||
| 1072 | if (config->walltime_run_table) | ||
| 1073 | print_table(config, output, precision, avg); | ||
| 1074 | |||
| 1075 | fprintf(output, " %17.*f +- %.*f seconds time elapsed", | ||
| 1076 | precision, avg, precision, sd); | ||
| 1077 | |||
| 1078 | print_noise_pct(config, sd, avg); | ||
| 1079 | } | ||
| 1080 | fprintf(output, "\n\n"); | ||
| 1081 | |||
| 1082 | if (config->print_free_counters_hint && | ||
| 1083 | sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 && | ||
| 1084 | n > 0) | ||
| 1085 | fprintf(output, | ||
| 1086 | "Some events weren't counted. Try disabling the NMI watchdog:\n" | ||
| 1087 | " echo 0 > /proc/sys/kernel/nmi_watchdog\n" | ||
| 1088 | " perf stat ...\n" | ||
| 1089 | " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); | ||
| 1090 | |||
| 1091 | if (config->print_mixed_hw_group_error) | ||
| 1092 | fprintf(output, | ||
| 1093 | "The events in group usually have to be from " | ||
| 1094 | "the same PMU. Try reorganizing the group.\n"); | ||
| 1095 | } | ||
| 1096 | |||
| 1097 | void | ||
| 1098 | perf_evlist__print_counters(struct perf_evlist *evlist, | ||
| 1099 | struct perf_stat_config *config, | ||
| 1100 | struct target *_target, | ||
| 1101 | struct timespec *ts, | ||
| 1102 | int argc, const char **argv) | ||
| 1103 | { | ||
| 1104 | bool metric_only = config->metric_only; | ||
| 1105 | int interval = config->interval; | ||
| 1106 | struct perf_evsel *counter; | ||
| 1107 | char buf[64], *prefix = NULL; | ||
| 1108 | |||
| 1109 | if (interval) | ||
| 1110 | print_interval(config, evlist, prefix = buf, ts); | ||
| 1111 | else | ||
| 1112 | print_header(config, _target, argc, argv); | ||
| 1113 | |||
| 1114 | if (metric_only) { | ||
| 1115 | static int num_print_iv; | ||
| 1116 | |||
| 1117 | if (num_print_iv == 0 && !interval) | ||
| 1118 | print_metric_headers(config, evlist, prefix, false); | ||
| 1119 | if (num_print_iv++ == 25) | ||
| 1120 | num_print_iv = 0; | ||
| 1121 | if (config->aggr_mode == AGGR_GLOBAL && prefix) | ||
| 1122 | fprintf(config->output, "%s", prefix); | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | switch (config->aggr_mode) { | ||
| 1126 | case AGGR_CORE: | ||
| 1127 | case AGGR_SOCKET: | ||
| 1128 | print_aggr(config, evlist, prefix); | ||
| 1129 | break; | ||
| 1130 | case AGGR_THREAD: | ||
| 1131 | evlist__for_each_entry(evlist, counter) { | ||
| 1132 | if (is_duration_time(counter)) | ||
| 1133 | continue; | ||
| 1134 | print_aggr_thread(config, _target, counter, prefix); | ||
| 1135 | } | ||
| 1136 | break; | ||
| 1137 | case AGGR_GLOBAL: | ||
| 1138 | evlist__for_each_entry(evlist, counter) { | ||
| 1139 | if (is_duration_time(counter)) | ||
| 1140 | continue; | ||
| 1141 | print_counter_aggr(config, counter, prefix); | ||
| 1142 | } | ||
| 1143 | if (metric_only) | ||
| 1144 | fputc('\n', config->output); | ||
| 1145 | break; | ||
| 1146 | case AGGR_NONE: | ||
| 1147 | if (metric_only) | ||
| 1148 | print_no_aggr_metric(config, evlist, prefix); | ||
| 1149 | else { | ||
| 1150 | evlist__for_each_entry(evlist, counter) { | ||
| 1151 | if (is_duration_time(counter)) | ||
| 1152 | continue; | ||
| 1153 | print_counter(config, counter, prefix); | ||
| 1154 | } | ||
| 1155 | } | ||
| 1156 | break; | ||
| 1157 | case AGGR_UNSET: | ||
| 1158 | default: | ||
| 1159 | break; | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | if (!interval && !config->csv_output) | ||
| 1163 | print_footer(config); | ||
| 1164 | |||
| 1165 | fflush(config->output); | ||
| 1166 | } | ||
diff --git a/tools/perf/util/stat.h b/tools/perf/util/stat.h index 8d3354e21e19..3a13a6dc5a62 100644 --- a/tools/perf/util/stat.h +++ b/tools/perf/util/stat.h | |||
| @@ -215,4 +215,10 @@ int perf_stat_synthesize_config(struct perf_stat_config *config, | |||
| 215 | struct perf_evlist *evlist, | 215 | struct perf_evlist *evlist, |
| 216 | perf_event__handler_t process, | 216 | perf_event__handler_t process, |
| 217 | bool attrs); | 217 | bool attrs); |
| 218 | void | ||
| 219 | perf_evlist__print_counters(struct perf_evlist *evlist, | ||
| 220 | struct perf_stat_config *config, | ||
| 221 | struct target *_target, | ||
| 222 | struct timespec *ts, | ||
| 223 | int argc, const char **argv); | ||
| 218 | #endif | 224 | #endif |
