3 # Copyright 2018 Google, Inc.
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;
9 # redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution;
12 # neither the name of the copyright holders nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
30 from __future__
import print_function
39 import multiprocessing
.pool
45 script_path
= os
.path
.abspath(inspect
.getfile(inspect
.currentframe()))
46 script_dir
= os
.path
.dirname(script_path
)
47 config_path
= os
.path
.join(script_dir
, 'config.py')
49 systemc_rel_path
= 'systemc'
50 tests_rel_path
= os
.path
.join(systemc_rel_path
, 'tests')
51 json_rel_path
= os
.path
.join(tests_rel_path
, 'tests.json')
56 args
= ['scons'] + list(args
)
57 subprocess
.check_call(args
)
62 def __init__(self
, target
, suffix
, build_dir
, props
):
65 self
.build_dir
= build_dir
68 for key
, val
in props
.iteritems():
69 self
.set_prop(key
, val
)
71 def set_prop(self
, key
, val
):
72 setattr(self
, key
, val
)
76 return os
.path
.join(self
.build_dir
, tests_rel_path
, self
.path
)
79 return os
.path
.join(script_dir
, self
.path
)
82 return os
.path
.join(self
.src_dir(), 'golden')
85 return '.'.join([self
.name
, self
.suffix
])
88 return os
.path
.join(self
.dir(), self
.bin())
91 return os
.path
.join(self
.dir(), 'm5out.' + self
.suffix
)
93 def returncode_file(self
):
94 return os
.path
.join(self
.m5out_dir(), 'returncode')
98 test_phase_classes
= {}
100 class TestPhaseMeta(type):
101 def __init__(cls
, name
, bases
, d
):
102 if not d
.pop('abstract', False):
103 test_phase_classes
[d
['name']] = cls
105 super(TestPhaseMeta
, cls
).__init
__(name
, bases
, d
)
107 class TestPhaseBase(object):
108 __metaclass__
= TestPhaseMeta
111 def __init__(self
, main_args
, *args
):
112 self
.main_args
= main_args
115 def __lt__(self
, other
):
116 return self
.number
< other
.number
118 class CompilePhase(TestPhaseBase
):
122 def run(self
, tests
):
123 targets
= list([test
.full_path() for test
in tests
])
124 scons_args
= list(self
.args
) + targets
127 class RunPhase(TestPhaseBase
):
131 def run(self
, tests
):
132 parser
= argparse
.ArgumentParser()
133 parser
.add_argument('--timeout', type=int, metavar
='SECONDS',
134 help='Time limit for each run in seconds.',
136 parser
.add_argument('-j', type=int, default
=1,
137 help='How many tests to run in parallel.')
138 args
= parser
.parse_args(self
.args
)
142 '--kill-after', str(args
.timeout
* 2),
148 cmd
.extend(timeout_cmd
)
151 '-red', test
.m5out_dir(),
152 '--listener-mode=off',
156 # Ensure the output directory exists.
157 if not os
.path
.exists(test
.m5out_dir()):
158 os
.makedirs(test
.m5out_dir())
160 subprocess
.check_call(cmd
)
161 except subprocess
.CalledProcessError
, error
:
162 returncode
= error
.returncode
165 with
open(test
.returncode_file(), 'w') as rc
:
166 rc
.write('%d\n' % returncode
)
168 runnable
= filter(lambda t
: not t
.compile_only
, tests
)
170 map(run_test
, runnable
)
172 tp
= multiprocessing
.pool
.ThreadPool(args
.j
)
173 map(lambda t
: tp
.apply_async(run_test
, (t
,)), runnable
)
177 class Checker(object):
178 def __init__(self
, ref
, test
, tag
):
184 with
open(self
.text
) as test_f
, open(self
.ref
) as ref_f
:
185 return test_f
.read() == ref_f
.read()
187 class 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
)
193 ref_filt
= merge_filts(
194 r
'^\nInfo: /OSCI/SystemC: Simulation stopped by user.\n',
195 r
'^SystemC Simulation\n'
197 test_filt
= merge_filts(
198 r
'^Global frequency set at \d* ticks per second\n'
201 def __init__(self
, ref
, test
, tag
, out_dir
):
202 super(LogChecker
, self
).__init
__(ref
, test
, tag
)
203 self
.out_dir
= out_dir
205 def apply_filters(self
, data
, filts
):
206 re
.sub(filt
, '', data
)
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())
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),
226 class VerifyPhase(TestPhaseBase
):
230 def reset_status(self
):
234 def passed(self
, test
):
235 self
._passed
.append(test
)
237 def failed(self
, test
, cause
, note
=''):
238 test
.set_prop('note', note
)
239 self
._failed
.setdefault(cause
, []).append(test
)
241 def print_status(self
):
242 total_passed
= len(self
._passed
)
243 total_failed
= sum(map(len, self
._failed
.values()))
245 print('Passed: {passed:4} - Failed: {failed:4}'.format(
246 passed
=total_passed
, failed
=total_failed
))
248 def write_result_file(self
, path
):
250 'passed': map(lambda t
: t
.props
, self
._passed
),
252 cause
: map(lambda t
: t
.props
, tests
) for
253 cause
, tests
in self
._failed
.iteritems()
256 with
open(path
, 'w') as rf
:
257 json
.dump(results
, rf
)
259 def print_results(self
):
262 for path
in sorted(list([ t
.path
for t
in self
._passed
])):
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
274 block
+= ' - ' + test
.note
278 print('\n'.join(causes
))
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.')
287 parser
.add_argument('--print-results', action
='store_true',
288 help='Print a list of tests that passed or failed')
289 args
= parser
.parse_args(self
.args
)
293 runnable
= filter(lambda t
: not t
.compile_only
, tests
)
294 compile_only
= filter(lambda t
: t
.compile_only
, tests
)
296 for test
in compile_only
:
297 if os
.path
.exists(test
.full_path()):
300 self
.failed(test
, 'compile failed')
302 for test
in runnable
:
303 with
open(test
.returncode_file()) as rc
:
304 returncode
= int(rc
.read())
306 if returncode
== 124:
307 self
.failed(test
, 'time out')
309 elif returncode
!= 0:
310 self
.failed(test
, 'abort')
313 out_dir
= test
.m5out_dir()
315 Diff
= collections
.namedtuple(
316 'Diff', 'ref, test, tag, ref_filter')
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
))
329 failed_diffs
= filter(lambda d
: not d
.check(), diffs
)
331 tags
= map(lambda d
: d
.tag
, failed_diffs
)
332 self
.failed(test
, 'failed diffs', ' '.join(tags
))
337 if args
.print_results
:
344 result_path
= os
.path
.join(os
.getcwd(), 'results.json')
345 elif args
.result_file_at
:
346 result_path
= args
.result_file_at
349 self
.write_result_file(result_path
)
352 parser
= argparse
.ArgumentParser(description
='SystemC test utility')
354 parser
.add_argument('build_dir', metavar
='BUILD_DIR',
355 help='The build directory (ie. build/ARM).')
357 parser
.add_argument('--update-json', action
='store_true',
358 help='Update the json manifest of tests.')
360 parser
.add_argument('--flavor', choices
=['debug', 'opt', 'fast'],
362 help='Flavor of binary to test.')
364 parser
.add_argument('--list', action
='store_true',
365 help='List the available tests')
367 filter_opts
= parser
.add_mutually_exclusive_group()
368 filter_opts
.add_argument('--filter', default
='True',
369 help='Python expression which filters tests based '
370 'on their properties')
371 filter_opts
.add_argument('--filter-file', default
=None,
372 type=argparse
.FileType('r'),
373 help='Same as --filter, but read from a file')
375 def collect_phases(args
):
376 phase_groups
= [list(g
) for k
, g
in
377 itertools
.groupby(args
, lambda x
: x
!= '--phase') if k
]
378 main_args
= parser
.parse_args(phase_groups
[0][1:])
381 for group
in phase_groups
[1:]:
384 raise RuntimeException('Phase %s specified more than once' % name
)
385 phase
= test_phase_classes
[name
]
386 phases
.append(phase(main_args
, *group
[1:]))
388 return main_args
, phases
390 main_args
, phases
= collect_phases(sys
.argv
)
394 CompilePhase(main_args
),
396 VerifyPhase(main_args
)
401 json_path
= os
.path
.join(main_args
.build_dir
, json_rel_path
)
403 if main_args
.update_json
:
404 scons(os
.path
.join(json_path
))
406 with
open(json_path
) as f
:
407 test_data
= json
.load(f
)
409 if main_args
.filter_file
:
410 f
= main_args
.filter_file
411 filt
= compile(f
.read(), f
.name
, 'eval')
413 filt
= compile(main_args
.filter, '<string>', 'eval')
416 target
: props
for (target
, props
) in
417 test_data
.iteritems() if eval(filt
, dict(props
))
421 for target
, props
in sorted(filtered_tests
.iteritems()):
422 print('%s.%s' % (target
, main_args
.flavor
))
423 for key
, val
in props
.iteritems():
424 print(' %s: %s' % (key
, val
))
425 print('Total tests: %d' % len(filtered_tests
))
427 tests_to_run
= list([
428 Test(target
, main_args
.flavor
, main_args
.build_dir
, props
) for
429 target
, props
in sorted(filtered_tests
.iteritems())
433 phase
.run(tests_to_run
)