analyze_brprob.py revision 1.1.1.1.4.3 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.3 martin hot_threshold = 10
75 1.1.1.1.4.2 christos
76 1.1.1.1.4.2 christos def percentage(a, b):
77 1.1.1.1.4.2 christos return 100.0 * a / b
78 1.1.1.1.4.2 christos
79 1.1.1.1.4.2 christos def average(values):
80 1.1.1.1.4.2 christos return 1.0 * sum(values) / len(values)
81 1.1.1.1.4.2 christos
82 1.1.1.1.4.2 christos def average_cutoff(values, cut):
83 1.1.1.1.4.2 christos l = len(values)
84 1.1.1.1.4.2 christos skip = floor(l * cut / 2)
85 1.1.1.1.4.2 christos if skip > 0:
86 1.1.1.1.4.2 christos values.sort()
87 1.1.1.1.4.2 christos values = values[skip:-skip]
88 1.1.1.1.4.2 christos return average(values)
89 1.1.1.1.4.2 christos
90 1.1.1.1.4.2 christos def median(values):
91 1.1.1.1.4.2 christos values.sort()
92 1.1.1.1.4.2 christos return values[int(len(values) / 2)]
93 1.1.1.1.4.2 christos
94 1.1.1.1.4.3 martin class PredictDefFile:
95 1.1.1.1.4.3 martin def __init__(self, path):
96 1.1.1.1.4.3 martin self.path = path
97 1.1.1.1.4.3 martin self.predictors = {}
98 1.1.1.1.4.3 martin
99 1.1.1.1.4.3 martin def parse_and_modify(self, heuristics, write_def_file):
100 1.1.1.1.4.3 martin lines = [x.rstrip() for x in open(self.path).readlines()]
101 1.1.1.1.4.3 martin
102 1.1.1.1.4.3 martin p = None
103 1.1.1.1.4.3 martin modified_lines = []
104 1.1.1.1.4.3 martin for l in lines:
105 1.1.1.1.4.3 martin if l.startswith('DEF_PREDICTOR'):
106 1.1.1.1.4.3 martin m = re.match('.*"(.*)".*', l)
107 1.1.1.1.4.3 martin p = m.group(1)
108 1.1.1.1.4.3 martin elif l == '':
109 1.1.1.1.4.3 martin p = None
110 1.1.1.1.4.3 martin
111 1.1.1.1.4.3 martin if p != None:
112 1.1.1.1.4.3 martin heuristic = [x for x in heuristics if x.name == p]
113 1.1.1.1.4.3 martin heuristic = heuristic[0] if len(heuristic) == 1 else None
114 1.1.1.1.4.3 martin
115 1.1.1.1.4.3 martin m = re.match('.*HITRATE \(([^)]*)\).*', l)
116 1.1.1.1.4.3 martin if (m != None):
117 1.1.1.1.4.3 martin self.predictors[p] = int(m.group(1))
118 1.1.1.1.4.3 martin
119 1.1.1.1.4.3 martin # modify the line
120 1.1.1.1.4.3 martin if heuristic != None:
121 1.1.1.1.4.3 martin new_line = (l[:m.start(1)]
122 1.1.1.1.4.3 martin + str(round(heuristic.get_hitrate()))
123 1.1.1.1.4.3 martin + l[m.end(1):])
124 1.1.1.1.4.3 martin l = new_line
125 1.1.1.1.4.3 martin p = None
126 1.1.1.1.4.3 martin elif 'PROB_VERY_LIKELY' in l:
127 1.1.1.1.4.3 martin self.predictors[p] = 100
128 1.1.1.1.4.3 martin modified_lines.append(l)
129 1.1.1.1.4.3 martin
130 1.1.1.1.4.3 martin # save the file
131 1.1.1.1.4.3 martin if write_def_file:
132 1.1.1.1.4.3 martin with open(self.path, 'w+') as f:
133 1.1.1.1.4.3 martin for l in modified_lines:
134 1.1.1.1.4.3 martin f.write(l + '\n')
135 1.1.1.1.4.3 martin class Heuristics:
136 1.1.1.1.4.3 martin def __init__(self, count, hits, fits):
137 1.1.1.1.4.3 martin self.count = count
138 1.1.1.1.4.3 martin self.hits = hits
139 1.1.1.1.4.3 martin self.fits = fits
140 1.1.1.1.4.3 martin
141 1.1.1.1.4.2 christos class Summary:
142 1.1.1.1.4.2 christos def __init__(self, name):
143 1.1.1.1.4.2 christos self.name = name
144 1.1.1.1.4.3 martin self.edges= []
145 1.1.1.1.4.3 martin
146 1.1.1.1.4.3 martin def branches(self):
147 1.1.1.1.4.3 martin return len(self.edges)
148 1.1.1.1.4.3 martin
149 1.1.1.1.4.3 martin def hits(self):
150 1.1.1.1.4.3 martin return sum([x.hits for x in self.edges])
151 1.1.1.1.4.3 martin
152 1.1.1.1.4.3 martin def fits(self):
153 1.1.1.1.4.3 martin return sum([x.fits for x in self.edges])
154 1.1.1.1.4.3 martin
155 1.1.1.1.4.3 martin def count(self):
156 1.1.1.1.4.3 martin return sum([x.count for x in self.edges])
157 1.1.1.1.4.3 martin
158 1.1.1.1.4.3 martin def successfull_branches(self):
159 1.1.1.1.4.3 martin return len([x for x in self.edges if 2 * x.hits >= x.count])
160 1.1.1.1.4.2 christos
161 1.1.1.1.4.2 christos def get_hitrate(self):
162 1.1.1.1.4.3 martin return 100.0 * self.hits() / self.count()
163 1.1.1.1.4.2 christos
164 1.1.1.1.4.2 christos def get_branch_hitrate(self):
165 1.1.1.1.4.3 martin return 100.0 * self.successfull_branches() / self.branches()
166 1.1.1.1.4.2 christos
167 1.1.1.1.4.2 christos def count_formatted(self):
168 1.1.1.1.4.3 martin v = self.count()
169 1.1.1.1.4.3 martin for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']:
170 1.1.1.1.4.2 christos if v < 1000:
171 1.1.1.1.4.2 christos return "%3.2f%s" % (v, unit)
172 1.1.1.1.4.2 christos v /= 1000.0
173 1.1.1.1.4.2 christos return "%.1f%s" % (v, 'Y')
174 1.1.1.1.4.2 christos
175 1.1.1.1.4.3 martin def count(self):
176 1.1.1.1.4.3 martin return sum([x.count for x in self.edges])
177 1.1.1.1.4.3 martin
178 1.1.1.1.4.3 martin def print(self, branches_max, count_max, predict_def):
179 1.1.1.1.4.3 martin # filter out most hot edges (if requested)
180 1.1.1.1.4.3 martin self.edges = sorted(self.edges, reverse = True, key = lambda x: x.count)
181 1.1.1.1.4.3 martin if args.coverage_threshold != None:
182 1.1.1.1.4.3 martin threshold = args.coverage_threshold * self.count() / 100
183 1.1.1.1.4.3 martin edges = [x for x in self.edges if x.count < threshold]
184 1.1.1.1.4.3 martin if len(edges) != 0:
185 1.1.1.1.4.3 martin self.edges = edges
186 1.1.1.1.4.3 martin
187 1.1.1.1.4.3 martin predicted_as = None
188 1.1.1.1.4.3 martin if predict_def != None and self.name in predict_def.predictors:
189 1.1.1.1.4.3 martin predicted_as = predict_def.predictors[self.name]
190 1.1.1.1.4.3 martin
191 1.1.1.1.4.2 christos print('%-40s %8i %5.1f%% %11.2f%% %7.2f%% / %6.2f%% %14i %8s %5.1f%%' %
192 1.1.1.1.4.3 martin (self.name, self.branches(),
193 1.1.1.1.4.3 martin percentage(self.branches(), branches_max),
194 1.1.1.1.4.2 christos self.get_branch_hitrate(),
195 1.1.1.1.4.2 christos self.get_hitrate(),
196 1.1.1.1.4.3 martin percentage(self.fits(), self.count()),
197 1.1.1.1.4.3 martin self.count(), self.count_formatted(),
198 1.1.1.1.4.3 martin percentage(self.count(), count_max)), end = '')
199 1.1.1.1.4.3 martin
200 1.1.1.1.4.3 martin if predicted_as != None:
201 1.1.1.1.4.3 martin print('%12i%% %5.1f%%' % (predicted_as,
202 1.1.1.1.4.3 martin self.get_hitrate() - predicted_as), end = '')
203 1.1.1.1.4.3 martin else:
204 1.1.1.1.4.3 martin print(' ' * 20, end = '')
205 1.1.1.1.4.3 martin
206 1.1.1.1.4.3 martin # print details about the most important edges
207 1.1.1.1.4.3 martin if args.coverage_threshold == None:
208 1.1.1.1.4.3 martin edges = [x for x in self.edges[:100] if x.count * hot_threshold > self.count()]
209 1.1.1.1.4.3 martin if args.verbose:
210 1.1.1.1.4.3 martin for c in edges:
211 1.1.1.1.4.3 martin r = 100.0 * c.count / self.count()
212 1.1.1.1.4.3 martin print(' %.0f%%:%d' % (r, c.count), end = '')
213 1.1.1.1.4.3 martin elif len(edges) > 0:
214 1.1.1.1.4.3 martin print(' %0.0f%%:%d' % (100.0 * sum([x.count for x in edges]) / self.count(), len(edges)), end = '')
215 1.1.1.1.4.3 martin
216 1.1.1.1.4.3 martin print()
217 1.1.1.1.4.2 christos
218 1.1.1.1.4.2 christos class Profile:
219 1.1.1.1.4.2 christos def __init__(self, filename):
220 1.1.1.1.4.2 christos self.filename = filename
221 1.1.1.1.4.2 christos self.heuristics = {}
222 1.1.1.1.4.2 christos self.niter_vector = []
223 1.1.1.1.4.2 christos
224 1.1.1.1.4.2 christos def add(self, name, prediction, count, hits):
225 1.1.1.1.4.2 christos if not name in self.heuristics:
226 1.1.1.1.4.2 christos self.heuristics[name] = Summary(name)
227 1.1.1.1.4.2 christos
228 1.1.1.1.4.2 christos s = self.heuristics[name]
229 1.1.1.1.4.2 christos
230 1.1.1.1.4.2 christos if prediction < 50:
231 1.1.1.1.4.2 christos hits = count - hits
232 1.1.1.1.4.2 christos remaining = count - hits
233 1.1.1.1.4.3 martin fits = max(hits, remaining)
234 1.1.1.1.4.2 christos
235 1.1.1.1.4.3 martin s.edges.append(Heuristics(count, hits, fits))
236 1.1.1.1.4.2 christos
237 1.1.1.1.4.2 christos def add_loop_niter(self, niter):
238 1.1.1.1.4.2 christos if niter > 0:
239 1.1.1.1.4.2 christos self.niter_vector.append(niter)
240 1.1.1.1.4.2 christos
241 1.1.1.1.4.2 christos def branches_max(self):
242 1.1.1.1.4.3 martin return max([v.branches() for k, v in self.heuristics.items()])
243 1.1.1.1.4.2 christos
244 1.1.1.1.4.2 christos def count_max(self):
245 1.1.1.1.4.3 martin return max([v.count() for k, v in self.heuristics.items()])
246 1.1.1.1.4.2 christos
247 1.1.1.1.4.3 martin def print_group(self, sorting, group_name, heuristics, predict_def):
248 1.1.1.1.4.2 christos count_max = self.count_max()
249 1.1.1.1.4.2 christos branches_max = self.branches_max()
250 1.1.1.1.4.2 christos
251 1.1.1.1.4.3 martin sorter = lambda x: x.branches()
252 1.1.1.1.4.2 christos if sorting == 'branch-hitrate':
253 1.1.1.1.4.2 christos sorter = lambda x: x.get_branch_hitrate()
254 1.1.1.1.4.2 christos elif sorting == 'hitrate':
255 1.1.1.1.4.2 christos sorter = lambda x: x.get_hitrate()
256 1.1.1.1.4.2 christos elif sorting == 'coverage':
257 1.1.1.1.4.2 christos sorter = lambda x: x.count
258 1.1.1.1.4.2 christos elif sorting == 'name':
259 1.1.1.1.4.2 christos sorter = lambda x: x.name.lower()
260 1.1.1.1.4.2 christos
261 1.1.1.1.4.3 martin print('%-40s %8s %6s %12s %18s %14s %8s %6s %12s %6s %s' %
262 1.1.1.1.4.2 christos ('HEURISTICS', 'BRANCHES', '(REL)',
263 1.1.1.1.4.3 martin 'BR. HITRATE', 'HITRATE', 'COVERAGE', 'COVERAGE', '(REL)',
264 1.1.1.1.4.3 martin 'predict.def', '(REL)', 'HOT branches (>%d%%)' % hot_threshold))
265 1.1.1.1.4.2 christos for h in sorted(heuristics, key = sorter):
266 1.1.1.1.4.3 martin h.print(branches_max, count_max, predict_def)
267 1.1.1.1.4.2 christos
268 1.1.1.1.4.2 christos def dump(self, sorting):
269 1.1.1.1.4.2 christos heuristics = self.heuristics.values()
270 1.1.1.1.4.2 christos if len(heuristics) == 0:
271 1.1.1.1.4.2 christos print('No heuristics available')
272 1.1.1.1.4.2 christos return
273 1.1.1.1.4.2 christos
274 1.1.1.1.4.3 martin predict_def = None
275 1.1.1.1.4.3 martin if args.def_file != None:
276 1.1.1.1.4.3 martin predict_def = PredictDefFile(args.def_file)
277 1.1.1.1.4.3 martin predict_def.parse_and_modify(heuristics, args.write_def_file)
278 1.1.1.1.4.3 martin
279 1.1.1.1.4.2 christos special = list(filter(lambda x: x.name in counter_aggregates,
280 1.1.1.1.4.2 christos heuristics))
281 1.1.1.1.4.2 christos normal = list(filter(lambda x: x.name not in counter_aggregates,
282 1.1.1.1.4.2 christos heuristics))
283 1.1.1.1.4.2 christos
284 1.1.1.1.4.3 martin self.print_group(sorting, 'HEURISTICS', normal, predict_def)
285 1.1.1.1.4.2 christos print()
286 1.1.1.1.4.3 martin self.print_group(sorting, 'HEURISTIC AGGREGATES', special, predict_def)
287 1.1.1.1.4.2 christos
288 1.1.1.1.4.2 christos if len(self.niter_vector) > 0:
289 1.1.1.1.4.2 christos print ('\nLoop count: %d' % len(self.niter_vector)),
290 1.1.1.1.4.2 christos print(' avg. # of iter: %.2f' % average(self.niter_vector))
291 1.1.1.1.4.2 christos print(' median # of iter: %.2f' % median(self.niter_vector))
292 1.1.1.1.4.2 christos for v in [1, 5, 10, 20, 30]:
293 1.1.1.1.4.2 christos cut = 0.01 * v
294 1.1.1.1.4.2 christos print(' avg. (%d%% cutoff) # of iter: %.2f'
295 1.1.1.1.4.2 christos % (v, average_cutoff(self.niter_vector, cut)))
296 1.1.1.1.4.2 christos
297 1.1.1.1.4.2 christos parser = argparse.ArgumentParser()
298 1.1.1.1.4.2 christos parser.add_argument('dump_file', metavar = 'dump_file',
299 1.1.1.1.4.2 christos help = 'IPA profile dump file')
300 1.1.1.1.4.2 christos parser.add_argument('-s', '--sorting', dest = 'sorting',
301 1.1.1.1.4.2 christos choices = ['branches', 'branch-hitrate', 'hitrate', 'coverage', 'name'],
302 1.1.1.1.4.2 christos default = 'branches')
303 1.1.1.1.4.3 martin parser.add_argument('-d', '--def-file', help = 'path to predict.def')
304 1.1.1.1.4.3 martin parser.add_argument('-w', '--write-def-file', action = 'store_true',
305 1.1.1.1.4.3 martin help = 'Modify predict.def file in order to set new numbers')
306 1.1.1.1.4.3 martin parser.add_argument('-c', '--coverage-threshold', type = int,
307 1.1.1.1.4.3 martin help = 'Ignore edges that have percentage coverage >= coverage-threshold')
308 1.1.1.1.4.3 martin parser.add_argument('-v', '--verbose', action = 'store_true', help = 'Print verbose informations')
309 1.1.1.1.4.2 christos
310 1.1.1.1.4.2 christos args = parser.parse_args()
311 1.1.1.1.4.2 christos
312 1.1.1.1.4.3 martin profile = Profile(args.dump_file)
313 1.1.1.1.4.2 christos loop_niter_str = ';; profile-based iteration count: '
314 1.1.1.1.4.3 martin
315 1.1.1.1.4.3 martin for l in open(args.dump_file):
316 1.1.1.1.4.3 martin if l.startswith(';;heuristics;'):
317 1.1.1.1.4.3 martin parts = l.strip().split(';')
318 1.1.1.1.4.3 martin assert len(parts) == 8
319 1.1.1.1.4.3 martin name = parts[3]
320 1.1.1.1.4.3 martin prediction = float(parts[6])
321 1.1.1.1.4.3 martin count = int(parts[4])
322 1.1.1.1.4.3 martin hits = int(parts[5])
323 1.1.1.1.4.2 christos
324 1.1.1.1.4.2 christos profile.add(name, prediction, count, hits)
325 1.1.1.1.4.2 christos elif l.startswith(loop_niter_str):
326 1.1.1.1.4.2 christos v = int(l[len(loop_niter_str):])
327 1.1.1.1.4.2 christos profile.add_loop_niter(v)
328 1.1.1.1.4.2 christos
329 1.1.1.1.4.2 christos profile.dump(args.sorting)
330