1#!/usr/bin/env python2
2#
3# Copyright 2018 Google, Inc.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;

--- 16 unchanged lines hidden (view full) ---

25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28# Authors: Gabe Black
29
30from __future__ import print_function
31
32import argparse
33import collections
34import difflib
35import functools
36import inspect
37import itertools
38import json
39import multiprocessing.pool
40import os
41import re
42import subprocess
43import sys
44
45script_path = os.path.abspath(inspect.getfile(inspect.currentframe()))
46script_dir = os.path.dirname(script_path)
47config_path = os.path.join(script_dir, 'config.py')
48
49systemc_rel_path = 'systemc'

--- 8 unchanged lines hidden (view full) ---

58
59
60
61class Test(object):
62 def __init__(self, target, suffix, build_dir, props):
63 self.target = target
64 self.suffix = suffix
65 self.build_dir = build_dir
66 self.props = {}
67
68 for key, val in props.iteritems():
65 setattr(self, key, val)
69 self.set_prop(key, val)
70
71 def set_prop(self, key, val):
72 setattr(self, key, val)
73 self.props[key] = val
74
75 def dir(self):
76 return os.path.join(self.build_dir, tests_rel_path, self.path)
77
78 def src_dir(self):
79 return os.path.join(script_dir, self.path)
80
81 def golden_dir(self):
82 return os.path.join(self.src_dir(), 'golden')

--- 62 unchanged lines hidden (view full) ---

145 def run_test(test):
146 cmd = []
147 if args.timeout:
148 cmd.extend(timeout_cmd)
149 cmd.extend([
150 test.full_path(),
151 '-red', test.m5out_dir(),
152 '--listener-mode=off',
153 '--quiet',
154 config_path
155 ])
156 # Ensure the output directory exists.
157 if not os.path.exists(test.m5out_dir()):
158 os.makedirs(test.m5out_dir())
159 try:
160 subprocess.check_call(cmd)
161 except subprocess.CalledProcessError, error:

--- 7 unchanged lines hidden (view full) ---

169 if args.j == 1:
170 map(run_test, runnable)
171 else:
172 tp = multiprocessing.pool.ThreadPool(args.j)
173 map(lambda t: tp.apply_async(run_test, (t,)), runnable)
174 tp.close()
175 tp.join()
176
177class Checker(object):
178 def __init__(self, ref, test, tag):
179 self.ref = ref
180 self.test = test
181 self.tag = tag
182
183 def check(self):
184 with open(self.text) as test_f, open(self.ref) as ref_f:
185 return test_f.read() == ref_f.read()
186
187class LogChecker(Checker):
188 def merge_filts(*filts):
189 filts = map(lambda f: '(' + f + ')', filts)
190 filts = '|'.join(filts)
191 return re.compile(filts, flags=re.MULTILINE)
192
193 ref_filt = merge_filts(
194 r'^\nInfo: /OSCI/SystemC: Simulation stopped by user.\n',
195 r'^SystemC Simulation\n'
196 )
197 test_filt = merge_filts(
198 r'^Global frequency set at \d* ticks per second\n'
199 )
200
201 def __init__(self, ref, test, tag, out_dir):
202 super(LogChecker, self).__init__(ref, test, tag)
203 self.out_dir = out_dir
204
205 def apply_filters(self, data, filts):
206 re.sub(filt, '', data)
207
208 def check(self):
209 test_file = os.path.basename(self.test)
210 ref_file = os.path.basename(self.ref)
211 with open(self.test) as test_f, open(self.ref) as ref_f:
212 test = re.sub(self.test_filt, '', test_f.read())
213 ref = re.sub(self.ref_filt, '', ref_f.read())
214 if test != ref:
215 diff_file = '.'.join([ref_file, 'diff'])
216 diff_path = os.path.join(self.out_dir, diff_file)
217 with open(diff_path, 'w') as diff_f:
218 for line in difflib.unified_diff(
219 ref.splitlines(True), test.splitlines(True),
220 fromfile=ref_file,
221 tofile=test_file):
222 diff_f.write(line)
223 return False
224 return True
225
226class VerifyPhase(TestPhaseBase):
227 name = 'verify'
228 number = 3
229
230 def reset_status(self):
231 self._passed = []
232 self._failed = {}
233
234 def passed(self, test):
235 self._passed.append(test)
236
179 def failed(self, test, cause):
237 def failed(self, test, cause, note=''):
238 test.set_prop('note', note)
239 self._failed.setdefault(cause, []).append(test)
240
241 def print_status(self):
242 total_passed = len(self._passed)
243 total_failed = sum(map(len, self._failed.values()))
244 print()
245 print('Passed: {passed:4} - Failed: {failed:4}'.format(
246 passed=total_passed, failed=total_failed))
247
248 def write_result_file(self, path):
190 passed = map(lambda t: t.path, self._passed)
191 passed.sort()
192 failed = {
193 cause: map(lambda t: t.path, tests) for
249 results = {
250 'passed': map(lambda t: t.props, self._passed),
251 'failed': {
252 cause: map(lambda t: t.props, tests) for
253 cause, tests in self._failed.iteritems()
254 }
255 }
196 for tests in failed.values():
197 tests.sort()
198 results = { 'passed': passed, 'failed': failed }
256 with open(path, 'w') as rf:
257 json.dump(results, rf)
258
259 def print_results(self):
203 passed = map(lambda t: t.path, self._passed)
204 passed.sort()
205 failed = {
206 cause: map(lambda t: t.path, tests) for
207 cause, tests in self._failed.iteritems()
208 }
209 for tests in failed.values():
210 tests.sort()
211
260 print()
261 print('Passed:')
214 map(lambda t: print(' ', t), passed)
262 for path in sorted(list([ t.path for t in self._passed ])):
263 print(' ', path)
264
265 print()
266 print('Failed:')
218 categories = failed.items()
219 categories.sort()
267
221 def cat_str((cause, tests)):
222 heading = ' ' + cause.capitalize() + ':\n'
223 test_lines = [' ' + test + '\n'for test in tests]
224 return heading + ''.join(test_lines)
225 blocks = map(cat_str, categories)
268 causes = []
269 for cause, tests in sorted(self._failed.items()):
270 block = ' ' + cause.capitalize() + ':\n'
271 for test in sorted(tests, key=lambda t: t.path):
272 block += ' ' + test.path
273 if test.note:
274 block += ' - ' + test.note
275 block += '\n'
276 causes.append(block)
277
227 print('\n'.join(blocks))
278 print('\n'.join(causes))
279
280 def run(self, tests):
281 parser = argparse.ArgumentParser()
282 result_opts = parser.add_mutually_exclusive_group()
283 result_opts.add_argument('--result-file', action='store_true',
284 help='Create a results.json file in the current directory.')
285 result_opts.add_argument('--result-file-at', metavar='PATH',
286 help='Create a results json file at the given path.')

--- 11 unchanged lines hidden (view full) ---

298 self.passed(test)
299 else:
300 self.failed(test, 'compile failed')
301
302 for test in runnable:
303 with open(test.returncode_file()) as rc:
304 returncode = int(rc.read())
305
255 if returncode == 0:
256 self.passed(test)
257 elif returncode == 124:
306 if returncode == 124:
307 self.failed(test, 'time out')
259 else:
308 continue
309 elif returncode != 0:
310 self.failed(test, 'abort')
311 continue
312
313 out_dir = test.m5out_dir()
314
315 Diff = collections.namedtuple(
316 'Diff', 'ref, test, tag, ref_filter')
317
318 diffs = []
319
320 log_file = '.'.join([test.name, 'log'])
321 log_path = os.path.join(test.golden_dir(), log_file)
322 simout_path = os.path.join(out_dir, 'simout')
323 if not os.path.exists(simout_path):
324 self.failed(test, 'no log output')
325 if os.path.exists(log_path):
326 diffs.append(LogChecker(
327 log_path, simout_path, log_file, out_dir))
328
329 failed_diffs = filter(lambda d: not d.check(), diffs)
330 if failed_diffs:
331 tags = map(lambda d: d.tag, failed_diffs)
332 self.failed(test, 'failed diffs', ' '.join(tags))
333 continue
334
335 self.passed(test)
336
337 if args.print_results:
338 self.print_results()
339
340 self.print_status()
341
342 result_path = None
343 if args.result_file:
344 result_path = os.path.join(os.getcwd(), 'results.json')

--- 89 unchanged lines hidden ---