annotate contrib/analyze_brprob.py @ 111:04ced10e8804

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