Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / tools / perf / util / branch.c
blob2285b1eb3128d7eb2d9d8b710992cde074fe035e
1 #include "util/map_symbol.h"
2 #include "util/branch.h"
3 #include <linux/kernel.h>
5 static bool cross_area(u64 addr1, u64 addr2, int size)
7 u64 align1, align2;
9 align1 = addr1 & ~(size - 1);
10 align2 = addr2 & ~(size - 1);
12 return (align1 != align2) ? true : false;
15 #define AREA_4K 4096
16 #define AREA_2M (2 * 1024 * 1024)
18 void branch_type_count(struct branch_type_stat *st, struct branch_flags *flags,
19 u64 from, u64 to)
21 if (flags->type == PERF_BR_UNKNOWN || from == 0)
22 return;
24 st->counts[flags->type]++;
26 if (flags->type == PERF_BR_COND) {
27 if (to > from)
28 st->cond_fwd++;
29 else
30 st->cond_bwd++;
33 if (cross_area(from, to, AREA_2M))
34 st->cross_2m++;
35 else if (cross_area(from, to, AREA_4K))
36 st->cross_4k++;
39 const char *branch_type_name(int type)
41 const char *branch_names[PERF_BR_MAX] = {
42 "N/A",
43 "COND",
44 "UNCOND",
45 "IND",
46 "CALL",
47 "IND_CALL",
48 "RET",
49 "SYSCALL",
50 "SYSRET",
51 "COND_CALL",
52 "COND_RET"
55 if (type >= 0 && type < PERF_BR_MAX)
56 return branch_names[type];
58 return NULL;
61 void branch_type_stat_display(FILE *fp, struct branch_type_stat *st)
63 u64 total = 0;
64 int i;
66 for (i = 0; i < PERF_BR_MAX; i++)
67 total += st->counts[i];
69 if (total == 0)
70 return;
72 fprintf(fp, "\n#");
73 fprintf(fp, "\n# Branch Statistics:");
74 fprintf(fp, "\n#");
76 if (st->cond_fwd > 0) {
77 fprintf(fp, "\n%8s: %5.1f%%",
78 "COND_FWD",
79 100.0 * (double)st->cond_fwd / (double)total);
82 if (st->cond_bwd > 0) {
83 fprintf(fp, "\n%8s: %5.1f%%",
84 "COND_BWD",
85 100.0 * (double)st->cond_bwd / (double)total);
88 if (st->cross_4k > 0) {
89 fprintf(fp, "\n%8s: %5.1f%%",
90 "CROSS_4K",
91 100.0 * (double)st->cross_4k / (double)total);
94 if (st->cross_2m > 0) {
95 fprintf(fp, "\n%8s: %5.1f%%",
96 "CROSS_2M",
97 100.0 * (double)st->cross_2m / (double)total);
100 for (i = 0; i < PERF_BR_MAX; i++) {
101 if (st->counts[i] > 0)
102 fprintf(fp, "\n%8s: %5.1f%%",
103 branch_type_name(i),
104 100.0 *
105 (double)st->counts[i] / (double)total);
109 static int count_str_scnprintf(int idx, const char *str, char *bf, int size)
111 return scnprintf(bf, size, "%s%s", (idx) ? " " : " (", str);
114 int branch_type_str(struct branch_type_stat *st, char *bf, int size)
116 int i, j = 0, printed = 0;
117 u64 total = 0;
119 for (i = 0; i < PERF_BR_MAX; i++)
120 total += st->counts[i];
122 if (total == 0)
123 return 0;
125 if (st->cond_fwd > 0)
126 printed += count_str_scnprintf(j++, "COND_FWD", bf + printed, size - printed);
128 if (st->cond_bwd > 0)
129 printed += count_str_scnprintf(j++, "COND_BWD", bf + printed, size - printed);
131 for (i = 0; i < PERF_BR_MAX; i++) {
132 if (i == PERF_BR_COND)
133 continue;
135 if (st->counts[i] > 0)
136 printed += count_str_scnprintf(j++, branch_type_name(i), bf + printed, size - printed);
139 if (st->cross_4k > 0)
140 printed += count_str_scnprintf(j++, "CROSS_4K", bf + printed, size - printed);
142 if (st->cross_2m > 0)
143 printed += count_str_scnprintf(j++, "CROSS_2M", bf + printed, size - printed);
145 return printed;