0001 #include "util/map_symbol.h"
0002 #include "util/branch.h"
0003 #include <linux/kernel.h>
0004
0005 static bool cross_area(u64 addr1, u64 addr2, int size)
0006 {
0007 u64 align1, align2;
0008
0009 align1 = addr1 & ~(size - 1);
0010 align2 = addr2 & ~(size - 1);
0011
0012 return (align1 != align2) ? true : false;
0013 }
0014
0015 #define AREA_4K 4096
0016 #define AREA_2M (2 * 1024 * 1024)
0017
0018 void branch_type_count(struct branch_type_stat *st, struct branch_flags *flags,
0019 u64 from, u64 to)
0020 {
0021 if (flags->type == PERF_BR_UNKNOWN || from == 0)
0022 return;
0023
0024 st->counts[flags->type]++;
0025
0026 if (flags->type == PERF_BR_COND) {
0027 if (to > from)
0028 st->cond_fwd++;
0029 else
0030 st->cond_bwd++;
0031 }
0032
0033 if (cross_area(from, to, AREA_2M))
0034 st->cross_2m++;
0035 else if (cross_area(from, to, AREA_4K))
0036 st->cross_4k++;
0037 }
0038
0039 const char *branch_type_name(int type)
0040 {
0041 const char *branch_names[PERF_BR_MAX] = {
0042 "N/A",
0043 "COND",
0044 "UNCOND",
0045 "IND",
0046 "CALL",
0047 "IND_CALL",
0048 "RET",
0049 "SYSCALL",
0050 "SYSRET",
0051 "COND_CALL",
0052 "COND_RET",
0053 "ERET",
0054 "IRQ"
0055 };
0056
0057 if (type >= 0 && type < PERF_BR_MAX)
0058 return branch_names[type];
0059
0060 return NULL;
0061 }
0062
0063 void branch_type_stat_display(FILE *fp, struct branch_type_stat *st)
0064 {
0065 u64 total = 0;
0066 int i;
0067
0068 for (i = 0; i < PERF_BR_MAX; i++)
0069 total += st->counts[i];
0070
0071 if (total == 0)
0072 return;
0073
0074 fprintf(fp, "\n#");
0075 fprintf(fp, "\n# Branch Statistics:");
0076 fprintf(fp, "\n#");
0077
0078 if (st->cond_fwd > 0) {
0079 fprintf(fp, "\n%8s: %5.1f%%",
0080 "COND_FWD",
0081 100.0 * (double)st->cond_fwd / (double)total);
0082 }
0083
0084 if (st->cond_bwd > 0) {
0085 fprintf(fp, "\n%8s: %5.1f%%",
0086 "COND_BWD",
0087 100.0 * (double)st->cond_bwd / (double)total);
0088 }
0089
0090 if (st->cross_4k > 0) {
0091 fprintf(fp, "\n%8s: %5.1f%%",
0092 "CROSS_4K",
0093 100.0 * (double)st->cross_4k / (double)total);
0094 }
0095
0096 if (st->cross_2m > 0) {
0097 fprintf(fp, "\n%8s: %5.1f%%",
0098 "CROSS_2M",
0099 100.0 * (double)st->cross_2m / (double)total);
0100 }
0101
0102 for (i = 0; i < PERF_BR_MAX; i++) {
0103 if (st->counts[i] > 0)
0104 fprintf(fp, "\n%8s: %5.1f%%",
0105 branch_type_name(i),
0106 100.0 *
0107 (double)st->counts[i] / (double)total);
0108 }
0109 }
0110
0111 static int count_str_scnprintf(int idx, const char *str, char *bf, int size)
0112 {
0113 return scnprintf(bf, size, "%s%s", (idx) ? " " : " (", str);
0114 }
0115
0116 int branch_type_str(struct branch_type_stat *st, char *bf, int size)
0117 {
0118 int i, j = 0, printed = 0;
0119 u64 total = 0;
0120
0121 for (i = 0; i < PERF_BR_MAX; i++)
0122 total += st->counts[i];
0123
0124 if (total == 0)
0125 return 0;
0126
0127 if (st->cond_fwd > 0)
0128 printed += count_str_scnprintf(j++, "COND_FWD", bf + printed, size - printed);
0129
0130 if (st->cond_bwd > 0)
0131 printed += count_str_scnprintf(j++, "COND_BWD", bf + printed, size - printed);
0132
0133 for (i = 0; i < PERF_BR_MAX; i++) {
0134 if (i == PERF_BR_COND)
0135 continue;
0136
0137 if (st->counts[i] > 0)
0138 printed += count_str_scnprintf(j++, branch_type_name(i), bf + printed, size - printed);
0139 }
0140
0141 if (st->cross_4k > 0)
0142 printed += count_str_scnprintf(j++, "CROSS_4K", bf + printed, size - printed);
0143
0144 if (st->cross_2m > 0)
0145 printed += count_str_scnprintf(j++, "CROSS_2M", bf + printed, size - printed);
0146
0147 return printed;
0148 }