3 #include "util/debug.h"
4 #include "util/branch.h"
6 static bool cross_area(u64 addr1, u64 addr2, int size)
10 align1 = addr1 & ~(size - 1);
11 align2 = addr2 & ~(size - 1);
13 return (align1 != align2) ? true : false;
17 #define AREA_2M (2 * 1024 * 1024)
19 void branch_type_count(struct branch_type_stat *st, struct branch_flags *flags,
22 if (flags->type == PERF_BR_UNKNOWN || from == 0)
25 st->counts[flags->type]++;
27 if (flags->type == PERF_BR_COND) {
34 if (cross_area(from, to, AREA_2M))
36 else if (cross_area(from, to, AREA_4K))
40 const char *branch_type_name(int type)
42 const char *branch_names[PERF_BR_MAX] = {
56 if (type >= 0 && type < PERF_BR_MAX)
57 return branch_names[type];
62 void branch_type_stat_display(FILE *fp, struct branch_type_stat *st)
67 for (i = 0; i < PERF_BR_MAX; i++)
68 total += st->counts[i];
74 fprintf(fp, "\n# Branch Statistics:");
77 if (st->cond_fwd > 0) {
78 fprintf(fp, "\n%8s: %5.1f%%",
80 100.0 * (double)st->cond_fwd / (double)total);
83 if (st->cond_bwd > 0) {
84 fprintf(fp, "\n%8s: %5.1f%%",
86 100.0 * (double)st->cond_bwd / (double)total);
89 if (st->cross_4k > 0) {
90 fprintf(fp, "\n%8s: %5.1f%%",
92 100.0 * (double)st->cross_4k / (double)total);
95 if (st->cross_2m > 0) {
96 fprintf(fp, "\n%8s: %5.1f%%",
98 100.0 * (double)st->cross_2m / (double)total);
101 for (i = 0; i < PERF_BR_MAX; i++) {
102 if (st->counts[i] > 0)
103 fprintf(fp, "\n%8s: %5.1f%%",
106 (double)st->counts[i] / (double)total);
110 static int count_str_scnprintf(int idx, const char *str, char *bf, int size)
112 return scnprintf(bf, size, "%s%s", (idx) ? " " : " (", str);
115 int branch_type_str(struct branch_type_stat *st, char *bf, int size)
117 int i, j = 0, printed = 0;
120 for (i = 0; i < PERF_BR_MAX; i++)
121 total += st->counts[i];
126 if (st->cond_fwd > 0)
127 printed += count_str_scnprintf(j++, "COND_FWD", bf + printed, size - printed);
129 if (st->cond_bwd > 0)
130 printed += count_str_scnprintf(j++, "COND_BWD", bf + printed, size - printed);
132 for (i = 0; i < PERF_BR_MAX; i++) {
133 if (i == PERF_BR_COND)
136 if (st->counts[i] > 0)
137 printed += count_str_scnprintf(j++, branch_type_name(i), bf + printed, size - printed);
140 if (st->cross_4k > 0)
141 printed += count_str_scnprintf(j++, "CROSS_4K", bf + printed, size - printed);
143 if (st->cross_2m > 0)
144 printed += count_str_scnprintf(j++, "CROSS_2M", bf + printed, size - printed);