analyze_brprob.py revision 1.1.1.1.4.2 1 1.1.1.1.4.2 christos #!/usr/bin/env python3
2 1.1.1.1.4.2 christos #
3 1.1.1.1.4.2 christos # Script to analyze results of our branch prediction heuristics
4 1.1.1.1.4.2 christos #
5 1.1.1.1.4.2 christos # This file is part of GCC.
6 1.1.1.1.4.2 christos #
7 1.1.1.1.4.2 christos # GCC is free software; you can redistribute it and/or modify it under
8 1.1.1.1.4.2 christos # the terms of the GNU General Public License as published by the Free
9 1.1.1.1.4.2 christos # Software Foundation; either version 3, or (at your option) any later
10 1.1.1.1.4.2 christos # version.
11 1.1.1.1.4.2 christos #
12 1.1.1.1.4.2 christos # GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 1.1.1.1.4.2 christos # WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 1.1.1.1.4.2 christos # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 1.1.1.1.4.2 christos # for more details.
16 1.1.1.1.4.2 christos #
17 1.1.1.1.4.2 christos # You should have received a copy of the GNU General Public License
18 1.1.1.1.4.2 christos # along with GCC; see the file COPYING3. If not see
19 1.1.1.1.4.2 christos # <http://www.gnu.org/licenses/>. */
20 1.1.1.1.4.2 christos #
21 1.1.1.1.4.2 christos #
22 1.1.1.1.4.2 christos #
23 1.1.1.1.4.2 christos # This script is used to calculate two basic properties of the branch prediction
24 1.1.1.1.4.2 christos # heuristics - coverage and hitrate. Coverage is number of executions
25 1.1.1.1.4.2 christos # of a given branch matched by the heuristics and hitrate is probability
26 1.1.1.1.4.2 christos # that once branch is predicted as taken it is really taken.
27 1.1.1.1.4.2 christos #
28 1.1.1.1.4.2 christos # These values are useful to determine the quality of given heuristics.
29 1.1.1.1.4.2 christos # Hitrate may be directly used in predict.def.
30 1.1.1.1.4.2 christos #
31 1.1.1.1.4.2 christos # Usage:
32 1.1.1.1.4.2 christos # Step 1: Compile and profile your program. You need to use -fprofile-generate
33 1.1.1.1.4.2 christos # flag to get the profiles.
34 1.1.1.1.4.2 christos # Step 2: Make a reference run of the intrumented application.
35 1.1.1.1.4.2 christos # Step 3: Compile the program with collected profile and dump IPA profiles
36 1.1.1.1.4.2 christos # (-fprofile-use -fdump-ipa-profile-details)
37 1.1.1.1.4.2 christos # Step 4: Collect all generated dump files:
38 1.1.1.1.4.2 christos # find . -name '*.profile' | xargs cat > dump_file
39 1.1.1.1.4.2 christos # Step 5: Run the script:
40 1.1.1.1.4.2 christos # ./analyze_brprob.py dump_file
41 1.1.1.1.4.2 christos # and read results. Basically the following table is printed:
42 1.1.1.1.4.2 christos #
43 1.1.1.1.4.2 christos # HEURISTICS BRANCHES (REL) HITRATE COVERAGE (REL)
44 1.1.1.1.4.2 christos # early return (on trees) 3 0.2% 35.83% / 93.64% 66360 0.0%
45 1.1.1.1.4.2 christos # guess loop iv compare 8 0.6% 53.35% / 53.73% 11183344 0.0%
46 1.1.1.1.4.2 christos # call 18 1.4% 31.95% / 69.95% 51880179 0.2%
47 1.1.1.1.4.2 christos # loop guard 23 1.8% 84.13% / 84.85% 13749065956 42.2%
48 1.1.1.1.4.2 christos # opcode values positive (on trees) 42 3.3% 15.71% / 84.81% 6771097902 20.8%
49 1.1.1.1.4.2 christos # opcode values nonequal (on trees) 226 17.6% 72.48% / 72.84% 844753864 2.6%
50 1.1.1.1.4.2 christos # loop exit 231 18.0% 86.97% / 86.98% 8952666897 27.5%
51 1.1.1.1.4.2 christos # loop iterations 239 18.6% 91.10% / 91.10% 3062707264 9.4%
52 1.1.1.1.4.2 christos # DS theory 281 21.9% 82.08% / 83.39% 7787264075 23.9%
53 1.1.1.1.4.2 christos # no prediction 293 22.9% 46.92% / 70.70% 2293267840 7.0%
54 1.1.1.1.4.2 christos # guessed loop iterations 313 24.4% 76.41% / 76.41% 10782750177 33.1%
55 1.1.1.1.4.2 christos # first match 708 55.2% 82.30% / 82.31% 22489588691 69.0%
56 1.1.1.1.4.2 christos # combined 1282 100.0% 79.76% / 81.75% 32570120606 100.0%
57 1.1.1.1.4.2 christos #
58 1.1.1.1.4.2 christos #
59 1.1.1.1.4.2 christos # The heuristics called "first match" is a heuristics used by GCC branch
60 1.1.1.1.4.2 christos # prediction pass and it predicts 55.2% branches correctly. As you can,
61 1.1.1.1.4.2 christos # the heuristics has very good covertage (69.05%). On the other hand,
62 1.1.1.1.4.2 christos # "opcode values nonequal (on trees)" heuristics has good hirate, but poor
63 1.1.1.1.4.2 christos # coverage.
64 1.1.1.1.4.2 christos
65 1.1.1.1.4.2 christos import sys
66 1.1.1.1.4.2 christos import os
67 1.1.1.1.4.2 christos import re
68 1.1.1.1.4.2 christos import argparse
69 1.1.1.1.4.2 christos
70 1.1.1.1.4.2 christos from math import *
71 1.1.1.1.4.2 christos
72 1.1.1.1.4.2 christos counter_aggregates = set(['combined', 'first match', 'DS theory',
73 1.1.1.1.4.2 christos 'no prediction'])
74 1.1.1.1.4.2 christos
75 1.1.1.1.4.2 christos def percentage(a, b):
76 1.1.1.1.4.2 christos return 100.0 * a / b
77 1.1.1.1.4.2 christos
78 1.1.1.1.4.2 christos def average(values):
79 1.1.1.1.4.2 christos return 1.0 * sum(values) / len(values)
80 1.1.1.1.4.2 christos
81 1.1.1.1.4.2 christos def average_cutoff(values, cut):
82 1.1.1.1.4.2 christos l = len(values)
83 1.1.1.1.4.2 christos skip = floor(l * cut / 2)
84 1.1.1.1.4.2 christos if skip > 0:
85 1.1.1.1.4.2 christos values.sort()
86 1.1.1.1.4.2 christos values = values[skip:-skip]
87 1.1.1.1.4.2 christos return average(values)
88 1.1.1.1.4.2 christos
89 1.1.1.1.4.2 christos def median(values):
90 1.1.1.1.4.2 christos values.sort()
91 1.1.1.1.4.2 christos return values[int(len(values) / 2)]
92 1.1.1.1.4.2 christos
93 1.1.1.1.4.2 christos class Summary:
94 1.1.1.1.4.2 christos def __init__(self, name):
95 1.1.1.1.4.2 christos self.name = name
96 1.1.1.1.4.2 christos self.branches = 0
97 1.1.1.1.4.2 christos self.successfull_branches = 0
98 1.1.1.1.4.2 christos self.count = 0
99 1.1.1.1.4.2 christos self.hits = 0
100 1.1.1.1.4.2 christos self.fits = 0
101 1.1.1.1.4.2 christos
102 1.1.1.1.4.2 christos def get_hitrate(self):
103 1.1.1.1.4.2 christos return 100.0 * self.hits / self.count
104 1.1.1.1.4.2 christos
105 1.1.1.1.4.2 christos def get_branch_hitrate(self):
106 1.1.1.1.4.2 christos return 100.0 * self.successfull_branches / self.branches
107 1.1.1.1.4.2 christos
108 1.1.1.1.4.2 christos def count_formatted(self):
109 1.1.1.1.4.2 christos v = self.count
110 1.1.1.1.4.2 christos for unit in ['','K','M','G','T','P','E','Z']:
111 1.1.1.1.4.2 christos if v < 1000:
112 1.1.1.1.4.2 christos return "%3.2f%s" % (v, unit)
113 1.1.1.1.4.2 christos v /= 1000.0
114 1.1.1.1.4.2 christos return "%.1f%s" % (v, 'Y')
115 1.1.1.1.4.2 christos
116 1.1.1.1.4.2 christos def print(self, branches_max, count_max):
117 1.1.1.1.4.2 christos print('%-40s %8i %5.1f%% %11.2f%% %7.2f%% / %6.2f%% %14i %8s %5.1f%%' %
118 1.1.1.1.4.2 christos (self.name, self.branches,
119 1.1.1.1.4.2 christos percentage(self.branches, branches_max),
120 1.1.1.1.4.2 christos self.get_branch_hitrate(),
121 1.1.1.1.4.2 christos self.get_hitrate(),
122 1.1.1.1.4.2 christos percentage(self.fits, self.count),
123 1.1.1.1.4.2 christos self.count, self.count_formatted(),
124 1.1.1.1.4.2 christos percentage(self.count, count_max)))
125 1.1.1.1.4.2 christos
126 1.1.1.1.4.2 christos class Profile:
127 1.1.1.1.4.2 christos def __init__(self, filename):
128 1.1.1.1.4.2 christos self.filename = filename
129 1.1.1.1.4.2 christos self.heuristics = {}
130 1.1.1.1.4.2 christos self.niter_vector = []
131 1.1.1.1.4.2 christos
132 1.1.1.1.4.2 christos def add(self, name, prediction, count, hits):
133 1.1.1.1.4.2 christos if not name in self.heuristics:
134 1.1.1.1.4.2 christos self.heuristics[name] = Summary(name)
135 1.1.1.1.4.2 christos
136 1.1.1.1.4.2 christos s = self.heuristics[name]
137 1.1.1.1.4.2 christos s.branches += 1
138 1.1.1.1.4.2 christos
139 1.1.1.1.4.2 christos s.count += count
140 1.1.1.1.4.2 christos if prediction < 50:
141 1.1.1.1.4.2 christos hits = count - hits
142 1.1.1.1.4.2 christos remaining = count - hits
143 1.1.1.1.4.2 christos if hits >= remaining:
144 1.1.1.1.4.2 christos s.successfull_branches += 1
145 1.1.1.1.4.2 christos
146 1.1.1.1.4.2 christos s.hits += hits
147 1.1.1.1.4.2 christos s.fits += max(hits, remaining)
148 1.1.1.1.4.2 christos
149 1.1.1.1.4.2 christos def add_loop_niter(self, niter):
150 1.1.1.1.4.2 christos if niter > 0:
151 1.1.1.1.4.2 christos self.niter_vector.append(niter)
152 1.1.1.1.4.2 christos
153 1.1.1.1.4.2 christos def branches_max(self):
154 1.1.1.1.4.2 christos return max([v.branches for k, v in self.heuristics.items()])
155 1.1.1.1.4.2 christos
156 1.1.1.1.4.2 christos def count_max(self):
157 1.1.1.1.4.2 christos return max([v.count for k, v in self.heuristics.items()])
158 1.1.1.1.4.2 christos
159 1.1.1.1.4.2 christos def print_group(self, sorting, group_name, heuristics):
160 1.1.1.1.4.2 christos count_max = self.count_max()
161 1.1.1.1.4.2 christos branches_max = self.branches_max()
162 1.1.1.1.4.2 christos
163 1.1.1.1.4.2 christos sorter = lambda x: x.branches
164 1.1.1.1.4.2 christos if sorting == 'branch-hitrate':
165 1.1.1.1.4.2 christos sorter = lambda x: x.get_branch_hitrate()
166 1.1.1.1.4.2 christos elif sorting == 'hitrate':
167 1.1.1.1.4.2 christos sorter = lambda x: x.get_hitrate()
168 1.1.1.1.4.2 christos elif sorting == 'coverage':
169 1.1.1.1.4.2 christos sorter = lambda x: x.count
170 1.1.1.1.4.2 christos elif sorting == 'name':
171 1.1.1.1.4.2 christos sorter = lambda x: x.name.lower()
172 1.1.1.1.4.2 christos
173 1.1.1.1.4.2 christos print('%-40s %8s %6s %12s %18s %14s %8s %6s' %
174 1.1.1.1.4.2 christos ('HEURISTICS', 'BRANCHES', '(REL)',
175 1.1.1.1.4.2 christos 'BR. HITRATE', 'HITRATE', 'COVERAGE', 'COVERAGE', '(REL)'))
176 1.1.1.1.4.2 christos for h in sorted(heuristics, key = sorter):
177 1.1.1.1.4.2 christos h.print(branches_max, count_max)
178 1.1.1.1.4.2 christos
179 1.1.1.1.4.2 christos def dump(self, sorting):
180 1.1.1.1.4.2 christos heuristics = self.heuristics.values()
181 1.1.1.1.4.2 christos if len(heuristics) == 0:
182 1.1.1.1.4.2 christos print('No heuristics available')
183 1.1.1.1.4.2 christos return
184 1.1.1.1.4.2 christos
185 1.1.1.1.4.2 christos special = list(filter(lambda x: x.name in counter_aggregates,
186 1.1.1.1.4.2 christos heuristics))
187 1.1.1.1.4.2 christos normal = list(filter(lambda x: x.name not in counter_aggregates,
188 1.1.1.1.4.2 christos heuristics))
189 1.1.1.1.4.2 christos
190 1.1.1.1.4.2 christos self.print_group(sorting, 'HEURISTICS', normal)
191 1.1.1.1.4.2 christos print()
192 1.1.1.1.4.2 christos self.print_group(sorting, 'HEURISTIC AGGREGATES', special)
193 1.1.1.1.4.2 christos
194 1.1.1.1.4.2 christos if len(self.niter_vector) > 0:
195 1.1.1.1.4.2 christos print ('\nLoop count: %d' % len(self.niter_vector)),
196 1.1.1.1.4.2 christos print(' avg. # of iter: %.2f' % average(self.niter_vector))
197 1.1.1.1.4.2 christos print(' median # of iter: %.2f' % median(self.niter_vector))
198 1.1.1.1.4.2 christos for v in [1, 5, 10, 20, 30]:
199 1.1.1.1.4.2 christos cut = 0.01 * v
200 1.1.1.1.4.2 christos print(' avg. (%d%% cutoff) # of iter: %.2f'
201 1.1.1.1.4.2 christos % (v, average_cutoff(self.niter_vector, cut)))
202 1.1.1.1.4.2 christos
203 1.1.1.1.4.2 christos parser = argparse.ArgumentParser()
204 1.1.1.1.4.2 christos parser.add_argument('dump_file', metavar = 'dump_file',
205 1.1.1.1.4.2 christos help = 'IPA profile dump file')
206 1.1.1.1.4.2 christos parser.add_argument('-s', '--sorting', dest = 'sorting',
207 1.1.1.1.4.2 christos choices = ['branches', 'branch-hitrate', 'hitrate', 'coverage', 'name'],
208 1.1.1.1.4.2 christos default = 'branches')
209 1.1.1.1.4.2 christos
210 1.1.1.1.4.2 christos args = parser.parse_args()
211 1.1.1.1.4.2 christos
212 1.1.1.1.4.2 christos profile = Profile(sys.argv[1])
213 1.1.1.1.4.2 christos r = re.compile(' (.*) heuristics( of edge [0-9]*->[0-9]*)?( \\(.*\\))?: (.*)%.*exec ([0-9]*) hit ([0-9]*)')
214 1.1.1.1.4.2 christos loop_niter_str = ';; profile-based iteration count: '
215 1.1.1.1.4.2 christos for l in open(args.dump_file).readlines():
216 1.1.1.1.4.2 christos m = r.match(l)
217 1.1.1.1.4.2 christos if m != None and m.group(3) == None:
218 1.1.1.1.4.2 christos name = m.group(1)
219 1.1.1.1.4.2 christos prediction = float(m.group(4))
220 1.1.1.1.4.2 christos count = int(m.group(5))
221 1.1.1.1.4.2 christos hits = int(m.group(6))
222 1.1.1.1.4.2 christos
223 1.1.1.1.4.2 christos profile.add(name, prediction, count, hits)
224 1.1.1.1.4.2 christos elif l.startswith(loop_niter_str):
225 1.1.1.1.4.2 christos v = int(l[len(loop_niter_str):])
226 1.1.1.1.4.2 christos profile.add_loop_niter(v)
227 1.1.1.1.4.2 christos
228 1.1.1.1.4.2 christos profile.dump(args.sorting)
229