forked from oracle/graalpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmx_graalpython_bisect.py
More file actions
289 lines (256 loc) · 11.7 KB
/
mx_graalpython_bisect.py
File metadata and controls
289 lines (256 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# The Universal Permissive License (UPL), Version 1.0
#
# Subject to the condition set forth below, permission is hereby granted to any
# person obtaining a copy of this software, associated documentation and/or
# data (collectively the "Software"), free of charge and under any and all
# copyright rights in the Software, and any and all patent rights owned or
# freely licensable by each licensor hereunder covering either (i) the
# unmodified Software as contributed to or provided by such licensor, or (ii)
# the Larger Works (as defined below), to deal in both
#
# (a) the Software, and
#
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
# one is included with the Software each a "Larger Work" to which the Software
# is contributed by such licensors),
#
# without restriction, including without limitation the rights to copy, create
# derivative works of, display, perform, and distribute the Software and make,
# use, sell, offer for sale, import, export, have made, and have sold the
# Software and the Larger Work(s), and to sublicense the foregoing rights on
# either these or other terms.
#
# This license is subject to the following condition:
#
# The above copyright notice and either this complete permission notice or at a
# minimum a reference to the UPL must be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import os
import re
import shlex
import sys
import types
import mx
def get_suite(name):
suite_name = name.lstrip('/')
suite = mx.suite(suite_name, fatalIfMissing=False)
if not suite:
suite = mx.primary_suite().import_suite(suite_name, version=None, urlinfos=None, in_subdir=name.startswith('/'))
assert suite
return suite
def get_downstream_suite(suite):
downstreams = {
'graalpython-apptests': 'graalpython',
'graalpython-extensions': 'graalpython',
'graalpython': '/vm',
'vm': '/vm-enterprise',
}
downstream = downstreams.get(suite.name)
if downstream:
return get_suite(downstream)
def get_commit(suite, ref='HEAD'):
if not suite:
return None
return suite.vc.git_command(suite.vc_dir, ['rev-parse', ref], abortOnError=True).strip()
def get_message(suite, commit):
return suite.vc.git_command(suite.vc_dir, ['log', '--format=%s', '-n', '1', commit]).strip()
def run_bisect_benchmark(suite, bad, good, callback, threshold=None):
git_dir = suite.vc_dir
commits = suite.vc.git_command(
git_dir,
['log', '--first-parent', '--format=format:%H', '{}^..{}'.format(good, bad)],
abortOnError=True,
).splitlines()
if not commits:
raise RuntimeError("No merge commits found in the range. Did you swap good and bad?")
downstream_suite = get_downstream_suite(suite)
values = [None] * len(commits)
if threshold is None:
bad_index = 0
good_index = len(commits) - 1
values[bad_index] = callback(suite, bad)
downstream_bad = get_commit(downstream_suite)
values[good_index] = callback(suite, good)
downstream_good = get_commit(downstream_suite)
threshold = (values[bad_index] + values[good_index]) / 2
if values[good_index] * 1.03 > values[bad_index]:
raise RuntimeError(
"Didn't detect a regression - less that 3% difference between good value "
"{} and bad value {}".format(values[good_index], values[bad_index])
)
else:
bad_index = -1
good_index = len(commits)
downstream_bad = None
downstream_good = None
while True:
index = bad_index + ((good_index - bad_index) // 2)
if index in [bad_index, good_index]:
assert good_index - bad_index == 1
break
commit = commits[index]
values[index] = callback(suite, commit)
if values[index] < threshold:
good_index = index
downstream_good = get_commit(downstream_suite)
else:
bad_index = index
downstream_bad = get_commit(downstream_suite)
subresults = {}
if downstream_bad and downstream_good and downstream_bad != downstream_good:
subresult = run_bisect_benchmark(downstream_suite, downstream_bad, downstream_good, callback, threshold)
subresults[bad_index] = subresult
return BisectResult(suite, commits, values, good_index, bad_index, subresults)
class BisectResult:
def __init__(self, suite, commits, values, good_index, bad_index, subresults):
self.suite = suite
self.commits = commits
self.values = values
self.good_index = good_index
self.bad_index = bad_index
self.subresults = subresults
@property
def repo_name(self):
return os.path.basename(self.suite.vc_dir)
@property
def good_commit(self):
try:
return self.commits[self.good_index]
except IndexError:
return None
@property
def bad_commit(self):
try:
return self.commits[self.bad_index]
except IndexError:
return None
def visualize(self, level=1):
level_marker = '=' * level
out = ["{} {}".format(level_marker, self.repo_name)]
for index, (commit, value) in enumerate(zip(self.commits, self.values)):
if value is not None:
out.append("{} {} {:6.6} s {}".format(level_marker, commit, value, get_message(self.suite, commit)))
if self.subresults and index in self.subresults:
out.append(self.subresults[index].visualize(level + 1))
return '\n'.join(out)
def summarize(self):
if self.bad_commit and self.good_commit:
for subresult in self.subresults.values():
sub = subresult.summarize()
if sub:
return sub
return ("Detected bad commit in {} repository:\n{} {}"
.format(self.repo_name, self.bad_commit, get_message(self.suite, self.bad_commit)))
return ''
def _bisect_benchmark(argv, initial_branch, email_to):
if 'BISECT_BENCHMARK_CONFIG' in os.environ:
import configparser
cp = configparser.ConfigParser()
cp.read(os.environ['BISECT_BENCHMARK_CONFIG'])
sec = cp['bisect-benchmark']
args = types.SimpleNamespace()
args.bad = sec['bad']
args.good = sec['good']
args.build_command = sec['build_command']
args.benchmark_command = sec['benchmark_command']
args.benchmark_criterion = sec.get('benchmark_criterion', 'BEST')
args.enterprise = sec.getboolean('enterprise', False)
else:
parser = argparse.ArgumentParser()
parser.add_argument('bad', help="Bad commit for bisection")
parser.add_argument('good', help="Good commit for bisection")
parser.add_argument('build_command', help="Command to run in order to build the configuration")
parser.add_argument('benchmark_command',
help="Command to run in order to run the benchmark. Output needs to be in mx's format")
parser.add_argument('--benchmark-criterion', default='BEST',
help="Which result parameter should be used for comparisons")
parser.add_argument('--enterprise', action='store_true', help="Whether to checkout graal-enterprise")
args = parser.parse_args(argv)
primary_suite = mx.primary_suite()
fetched_enterprise = [False]
def benchmark_callback(suite, commit):
suite.vc.update_to_branch(suite.vc_dir, commit)
mx.run_mx(['sforceimports'], suite=suite)
if args.enterprise and suite.name != 'vm-enterprise':
checkout_args = ['--dynamicimports', '/vm-enterprise', 'checkout-downstream', 'vm', 'vm-enterprise']
if fetched_enterprise[0]:
checkout_args.append('--no-fetch')
mx.run_mx(checkout_args, out=mx.OutputCapture())
mx.run_mx(['--env', 'ee', 'sforceimports'], suite=get_suite('/vm-enterprise'))
fetched_enterprise[0] = True
elif suite.name != 'vm':
mx.run_mx(['--env', 'ce', 'sforceimports'], suite=get_suite('/vm'))
suite.vc.update_to_branch(suite.vc_dir, commit)
mx.run_mx(['sforceimports'], suite=suite)
env = os.environ.copy()
if 'CI' not in os.environ:
env['MX_ALT_OUTPUT_ROOT'] = 'mxbuild-{}'.format(commit)
retcode = mx.run(shlex.split(args.build_command), env=env, nonZeroIsFatal=False)
if retcode:
raise RuntimeError("Failed to execute the build command for {}".format(commit))
output = mx.OutputCapture()
retcode = mx.run(shlex.split(args.benchmark_command), env=env, out=mx.TeeOutputCapture(output),
nonZeroIsFatal=False)
if retcode:
raise RuntimeError("Failed to execute benchmark for {}".format(commit))
match = re.search(r'{}.*duration: ([\d.]+)'.format(re.escape(args.benchmark_criterion)), output.data)
if not match:
raise RuntimeError("Failed to get result from the benchmark")
return float(match.group(1))
bad = get_commit(primary_suite, args.bad)
good = get_commit(primary_suite, args.good)
result = run_bisect_benchmark(primary_suite, bad, good, benchmark_callback)
visualization = result.visualize()
summary = result.summarize()
print()
print(visualization)
print()
print(summary)
if 'CI' not in os.environ:
print("You can rerun a benchmark for a particular commit using:\nMX_ALT_OUTPUT_ROOT=mxbuild-$commit {}".format(
args.benchmark_command))
send_email(
initial_branch,
email_to,
"Bisection job has finished successfully.\n{}\n".format(summary)
+ "Note I'm just a script and I don't validate statistical significance of the above result.\n"
+ "Please take a moment to also inspect the detailed results below.\n\n{}\n\n".format(visualization)
+ os.environ.get('BUILD_URL', 'Unknown URL')
)
def bisect_benchmark(argv):
suite = mx.primary_suite()
initial_branch = suite.vc.git_command(suite.vc_dir, ['rev-parse', '--abbrev-ref', 'HEAD']).strip()
email_to = suite.vc.git_command(suite.vc_dir, ['log', '--format=%cE', '-n', '1']).strip()
try:
_bisect_benchmark(argv, initial_branch, email_to)
except Exception:
send_email(initial_branch, email_to, "Job failed.\n {}".format(os.environ.get('BUILD_URL', 'Unknown URL')))
raise
def send_email(initial_branch, email_to, content):
if 'BISECT_EMAIL_SMTP_SERVER' in os.environ:
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['Subject'] = "Bisection result for {}".format(initial_branch)
msg['From'] = os.environ['BISECT_EMAIL_FROM']
validate_to = os.environ['BISECT_EMAIL_TO_PATTERN']
if not re.match(validate_to, email_to):
sys.exit("Email {} not allowed, aborting sending".format(email_to))
msg['To'] = email_to
msg.set_content(content)
print(msg)
smtp = smtplib.SMTP(os.environ['BISECT_EMAIL_SMTP_SERVER'])
smtp.send_message(msg)
smtp.quit()