verify.py revision 13240
112870Sgabeblack@google.com#!/usr/bin/env python2
212870Sgabeblack@google.com#
312870Sgabeblack@google.com# Copyright 2018 Google, Inc.
412870Sgabeblack@google.com#
512870Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
612870Sgabeblack@google.com# modification, are permitted provided that the following conditions are
712870Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
812870Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
912870Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
1012870Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
1112870Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
1212870Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
1312870Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
1412870Sgabeblack@google.com# this software without specific prior written permission.
1512870Sgabeblack@google.com#
1612870Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712870Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812870Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912870Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012870Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112870Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212870Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312870Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412870Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512870Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612870Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712870Sgabeblack@google.com#
2812870Sgabeblack@google.com# Authors: Gabe Black
2912870Sgabeblack@google.com
3012870Sgabeblack@google.comfrom __future__ import print_function
3112870Sgabeblack@google.com
3212870Sgabeblack@google.comimport argparse
3313003Sgabeblack@google.comimport collections
3413003Sgabeblack@google.comimport difflib
3512870Sgabeblack@google.comimport functools
3612870Sgabeblack@google.comimport inspect
3712870Sgabeblack@google.comimport itertools
3812870Sgabeblack@google.comimport json
3913000Sgabeblack@google.comimport multiprocessing.pool
4012870Sgabeblack@google.comimport os
4113003Sgabeblack@google.comimport re
4212870Sgabeblack@google.comimport subprocess
4312870Sgabeblack@google.comimport sys
4412870Sgabeblack@google.com
4512870Sgabeblack@google.comscript_path = os.path.abspath(inspect.getfile(inspect.currentframe()))
4612870Sgabeblack@google.comscript_dir = os.path.dirname(script_path)
4712870Sgabeblack@google.comconfig_path = os.path.join(script_dir, 'config.py')
4812870Sgabeblack@google.com
4912870Sgabeblack@google.comsystemc_rel_path = 'systemc'
5012870Sgabeblack@google.comtests_rel_path = os.path.join(systemc_rel_path, 'tests')
5112870Sgabeblack@google.comjson_rel_path = os.path.join(tests_rel_path, 'tests.json')
5212870Sgabeblack@google.com
5312870Sgabeblack@google.com
5412870Sgabeblack@google.com
5512870Sgabeblack@google.comdef scons(*args):
5612870Sgabeblack@google.com    args = ['scons'] + list(args)
5712870Sgabeblack@google.com    subprocess.check_call(args)
5812870Sgabeblack@google.com
5912870Sgabeblack@google.com
6012870Sgabeblack@google.com
6112870Sgabeblack@google.comclass Test(object):
6212870Sgabeblack@google.com    def __init__(self, target, suffix, build_dir, props):
6312870Sgabeblack@google.com        self.target = target
6412870Sgabeblack@google.com        self.suffix = suffix
6512870Sgabeblack@google.com        self.build_dir = build_dir
6613003Sgabeblack@google.com        self.props = {}
6712870Sgabeblack@google.com
6812870Sgabeblack@google.com        for key, val in props.iteritems():
6913003Sgabeblack@google.com            self.set_prop(key, val)
7013003Sgabeblack@google.com
7113003Sgabeblack@google.com    def set_prop(self, key, val):
7213003Sgabeblack@google.com        setattr(self, key, val)
7313003Sgabeblack@google.com        self.props[key] = val
7412870Sgabeblack@google.com
7512870Sgabeblack@google.com    def dir(self):
7612870Sgabeblack@google.com        return os.path.join(self.build_dir, tests_rel_path, self.path)
7712870Sgabeblack@google.com
7812870Sgabeblack@google.com    def src_dir(self):
7912897Sgabeblack@google.com        return os.path.join(script_dir, self.path)
8012870Sgabeblack@google.com
8113134Sgabeblack@google.com    def expected_returncode_file(self):
8213134Sgabeblack@google.com        return os.path.join(self.src_dir(), 'expected_returncode')
8313134Sgabeblack@google.com
8412870Sgabeblack@google.com    def golden_dir(self):
8512870Sgabeblack@google.com        return os.path.join(self.src_dir(), 'golden')
8612870Sgabeblack@google.com
8712870Sgabeblack@google.com    def bin(self):
8812870Sgabeblack@google.com        return '.'.join([self.name, self.suffix])
8912870Sgabeblack@google.com
9012870Sgabeblack@google.com    def full_path(self):
9112870Sgabeblack@google.com        return os.path.join(self.dir(), self.bin())
9212870Sgabeblack@google.com
9312870Sgabeblack@google.com    def m5out_dir(self):
9412870Sgabeblack@google.com        return os.path.join(self.dir(), 'm5out.' + self.suffix)
9512870Sgabeblack@google.com
9613002Sgabeblack@google.com    def returncode_file(self):
9713002Sgabeblack@google.com        return os.path.join(self.m5out_dir(), 'returncode')
9813002Sgabeblack@google.com
9912870Sgabeblack@google.com
10012870Sgabeblack@google.com
10112870Sgabeblack@google.comtest_phase_classes = {}
10212870Sgabeblack@google.com
10312870Sgabeblack@google.comclass TestPhaseMeta(type):
10412870Sgabeblack@google.com    def __init__(cls, name, bases, d):
10512870Sgabeblack@google.com        if not d.pop('abstract', False):
10612870Sgabeblack@google.com            test_phase_classes[d['name']] = cls
10712870Sgabeblack@google.com
10812870Sgabeblack@google.com        super(TestPhaseMeta, cls).__init__(name, bases, d)
10912870Sgabeblack@google.com
11012870Sgabeblack@google.comclass TestPhaseBase(object):
11112870Sgabeblack@google.com    __metaclass__ = TestPhaseMeta
11212870Sgabeblack@google.com    abstract = True
11312870Sgabeblack@google.com
11412870Sgabeblack@google.com    def __init__(self, main_args, *args):
11512870Sgabeblack@google.com        self.main_args = main_args
11612870Sgabeblack@google.com        self.args = args
11712870Sgabeblack@google.com
11812870Sgabeblack@google.com    def __lt__(self, other):
11912870Sgabeblack@google.com        return self.number < other.number
12012870Sgabeblack@google.com
12112870Sgabeblack@google.comclass CompilePhase(TestPhaseBase):
12212870Sgabeblack@google.com    name = 'compile'
12312870Sgabeblack@google.com    number = 1
12412870Sgabeblack@google.com
12512870Sgabeblack@google.com    def run(self, tests):
12612870Sgabeblack@google.com        targets = list([test.full_path() for test in tests])
12713183Sgabeblack@google.com
12813183Sgabeblack@google.com        parser = argparse.ArgumentParser()
12913183Sgabeblack@google.com        parser.add_argument('-j', type=int, default=0)
13013183Sgabeblack@google.com        args, leftovers = parser.parse_known_args(self.args)
13113183Sgabeblack@google.com        if args.j == 0:
13213183Sgabeblack@google.com            self.args = ('-j', str(self.main_args.j)) + self.args
13313183Sgabeblack@google.com
13413034Sgabeblack@google.com        scons_args = [ 'USE_SYSTEMC=1' ] + list(self.args) + targets
13512870Sgabeblack@google.com        scons(*scons_args)
13612870Sgabeblack@google.com
13712870Sgabeblack@google.comclass RunPhase(TestPhaseBase):
13812870Sgabeblack@google.com    name = 'execute'
13912870Sgabeblack@google.com    number = 2
14012870Sgabeblack@google.com
14112870Sgabeblack@google.com    def run(self, tests):
14213000Sgabeblack@google.com        parser = argparse.ArgumentParser()
14313000Sgabeblack@google.com        parser.add_argument('--timeout', type=int, metavar='SECONDS',
14413183Sgabeblack@google.com                            help='Time limit for each run in seconds, '
14513183Sgabeblack@google.com                            '0 to disable.',
14613183Sgabeblack@google.com                            default=60)
14713183Sgabeblack@google.com        parser.add_argument('-j', type=int, default=0,
14813000Sgabeblack@google.com                help='How many tests to run in parallel.')
14913000Sgabeblack@google.com        args = parser.parse_args(self.args)
15013000Sgabeblack@google.com
15113000Sgabeblack@google.com        timeout_cmd = [
15213000Sgabeblack@google.com            'timeout',
15313000Sgabeblack@google.com            '--kill-after', str(args.timeout * 2),
15413000Sgabeblack@google.com            str(args.timeout)
15513000Sgabeblack@google.com        ]
15613100Sgabeblack@google.com        curdir = os.getcwd()
15713000Sgabeblack@google.com        def run_test(test):
15813000Sgabeblack@google.com            cmd = []
15913000Sgabeblack@google.com            if args.timeout:
16013000Sgabeblack@google.com                cmd.extend(timeout_cmd)
16113000Sgabeblack@google.com            cmd.extend([
16212870Sgabeblack@google.com                test.full_path(),
16313181Sgabeblack@google.com                '-rd', os.path.abspath(test.m5out_dir()),
16412870Sgabeblack@google.com                '--listener-mode=off',
16513003Sgabeblack@google.com                '--quiet',
16613100Sgabeblack@google.com                config_path,
16713100Sgabeblack@google.com                '--working-dir',
16813100Sgabeblack@google.com                os.path.dirname(test.src_dir())
16913000Sgabeblack@google.com            ])
17013002Sgabeblack@google.com            # Ensure the output directory exists.
17113002Sgabeblack@google.com            if not os.path.exists(test.m5out_dir()):
17213002Sgabeblack@google.com                os.makedirs(test.m5out_dir())
17313001Sgabeblack@google.com            try:
17413001Sgabeblack@google.com                subprocess.check_call(cmd)
17513001Sgabeblack@google.com            except subprocess.CalledProcessError, error:
17613001Sgabeblack@google.com                returncode = error.returncode
17713001Sgabeblack@google.com            else:
17813001Sgabeblack@google.com                returncode = 0
17913100Sgabeblack@google.com            os.chdir(curdir)
18013002Sgabeblack@google.com            with open(test.returncode_file(), 'w') as rc:
18113001Sgabeblack@google.com                rc.write('%d\n' % returncode)
18213000Sgabeblack@google.com
18313183Sgabeblack@google.com        j = self.main_args.j if args.j == 0 else args.j
18413183Sgabeblack@google.com
18513000Sgabeblack@google.com        runnable = filter(lambda t: not t.compile_only, tests)
18613183Sgabeblack@google.com        if j == 1:
18713000Sgabeblack@google.com            map(run_test, runnable)
18813000Sgabeblack@google.com        else:
18913183Sgabeblack@google.com            tp = multiprocessing.pool.ThreadPool(j)
19013000Sgabeblack@google.com            map(lambda t: tp.apply_async(run_test, (t,)), runnable)
19113000Sgabeblack@google.com            tp.close()
19213000Sgabeblack@google.com            tp.join()
19312870Sgabeblack@google.com
19413003Sgabeblack@google.comclass Checker(object):
19513003Sgabeblack@google.com    def __init__(self, ref, test, tag):
19613003Sgabeblack@google.com        self.ref = ref
19713003Sgabeblack@google.com        self.test = test
19813003Sgabeblack@google.com        self.tag = tag
19913003Sgabeblack@google.com
20013003Sgabeblack@google.com    def check(self):
20113240Sgabeblack@google.com        with open(self.test) as test_f, open(self.ref) as ref_f:
20213003Sgabeblack@google.com            return test_f.read() == ref_f.read()
20313003Sgabeblack@google.com
20413055Sgabeblack@google.comdef tagged_filt(tag, num):
20513178Sgabeblack@google.com    return (r'\n{}: \({}{}\) .*\n(In file: .*\n)?'
20613055Sgabeblack@google.com            r'(In process: [\w.]* @ .*\n)?').format(tag, tag[0], num)
20713055Sgabeblack@google.com
20813137Sgabeblack@google.comdef error_filt(num):
20913137Sgabeblack@google.com    return tagged_filt('Error', num)
21013137Sgabeblack@google.com
21113055Sgabeblack@google.comdef warning_filt(num):
21213055Sgabeblack@google.com    return tagged_filt('Warning', num)
21313055Sgabeblack@google.com
21413055Sgabeblack@google.comdef info_filt(num):
21513055Sgabeblack@google.com    return tagged_filt('Info', num)
21613055Sgabeblack@google.com
21713003Sgabeblack@google.comclass LogChecker(Checker):
21813003Sgabeblack@google.com    def merge_filts(*filts):
21913003Sgabeblack@google.com        filts = map(lambda f: '(' + f + ')', filts)
22013003Sgabeblack@google.com        filts = '|'.join(filts)
22113003Sgabeblack@google.com        return re.compile(filts, flags=re.MULTILINE)
22213003Sgabeblack@google.com
22313139Sgabeblack@google.com    # The reporting mechanism will print the actual filename when running in
22413139Sgabeblack@google.com    # gem5, and the "golden" output will say "<removed by verify.py>". We want
22513139Sgabeblack@google.com    # to strip out both versions to make comparing the output sensible.
22613139Sgabeblack@google.com    in_file_filt = r'^In file: ((<removed by verify\.pl>)|([a-zA-Z0-9.:_/]*))$'
22713139Sgabeblack@google.com
22813003Sgabeblack@google.com    ref_filt = merge_filts(
22913003Sgabeblack@google.com        r'^\nInfo: /OSCI/SystemC: Simulation stopped by user.\n',
23013004Sgabeblack@google.com        r'^SystemC Simulation\n',
23113055Sgabeblack@google.com        r'^\nInfo: \(I804\) /IEEE_Std_1666/deprecated: ' +
23213055Sgabeblack@google.com        r'You can turn off(.*\n){7}',
23313056Sgabeblack@google.com        r'^\nInfo: \(I804\) /IEEE_Std_1666/deprecated: \n' +
23413056Sgabeblack@google.com        r'    sc_clock\(const char(.*\n){3}',
23513037Sgabeblack@google.com        warning_filt(540),
23613010Sgabeblack@google.com        warning_filt(571),
23713055Sgabeblack@google.com        info_filt(804),
23813139Sgabeblack@google.com        in_file_filt,
23913003Sgabeblack@google.com    )
24013003Sgabeblack@google.com    test_filt = merge_filts(
24113055Sgabeblack@google.com        r'^Global frequency set at \d* ticks per second\n',
24213181Sgabeblack@google.com        r'^info: Entering event queue @ \d*\.  Starting simulation\.\.\.\n',
24313181Sgabeblack@google.com        r'warn: [^(]+\([^)]*\)( \[with [^]]*\])? not implemented\.\n',
24413213Sgabeblack@google.com        r'warn: Ignoring request to set stack size\.\n',
24513055Sgabeblack@google.com        info_filt(804),
24613139Sgabeblack@google.com        in_file_filt,
24713003Sgabeblack@google.com    )
24813003Sgabeblack@google.com
24913003Sgabeblack@google.com    def __init__(self, ref, test, tag, out_dir):
25013003Sgabeblack@google.com        super(LogChecker, self).__init__(ref, test, tag)
25113003Sgabeblack@google.com        self.out_dir = out_dir
25213003Sgabeblack@google.com
25313003Sgabeblack@google.com    def apply_filters(self, data, filts):
25413003Sgabeblack@google.com        re.sub(filt, '', data)
25513003Sgabeblack@google.com
25613003Sgabeblack@google.com    def check(self):
25713003Sgabeblack@google.com        test_file = os.path.basename(self.test)
25813003Sgabeblack@google.com        ref_file = os.path.basename(self.ref)
25913003Sgabeblack@google.com        with open(self.test) as test_f, open(self.ref) as ref_f:
26013003Sgabeblack@google.com            test = re.sub(self.test_filt, '', test_f.read())
26113003Sgabeblack@google.com            ref = re.sub(self.ref_filt, '', ref_f.read())
26213005Sgabeblack@google.com            diff_file = '.'.join([ref_file, 'diff'])
26313005Sgabeblack@google.com            diff_path = os.path.join(self.out_dir, diff_file)
26413003Sgabeblack@google.com            if test != ref:
26513003Sgabeblack@google.com                with open(diff_path, 'w') as diff_f:
26613003Sgabeblack@google.com                    for line in difflib.unified_diff(
26713003Sgabeblack@google.com                            ref.splitlines(True), test.splitlines(True),
26813003Sgabeblack@google.com                            fromfile=ref_file,
26913003Sgabeblack@google.com                            tofile=test_file):
27013003Sgabeblack@google.com                        diff_f.write(line)
27113003Sgabeblack@google.com                return False
27213005Sgabeblack@google.com            else:
27313005Sgabeblack@google.com                if os.path.exists(diff_path):
27413005Sgabeblack@google.com                    os.unlink(diff_path)
27513003Sgabeblack@google.com        return True
27613003Sgabeblack@google.com
27713009Sgabeblack@google.comclass GoldenDir(object):
27813009Sgabeblack@google.com    def __init__(self, path, platform):
27913009Sgabeblack@google.com        self.path = path
28013009Sgabeblack@google.com        self.platform = platform
28113009Sgabeblack@google.com
28213009Sgabeblack@google.com        contents = os.listdir(path)
28313009Sgabeblack@google.com        suffix = '.' + platform
28413009Sgabeblack@google.com        suffixed = filter(lambda c: c.endswith(suffix), contents)
28513009Sgabeblack@google.com        bases = map(lambda t: t[:-len(platform)], suffixed)
28613009Sgabeblack@google.com        common = filter(lambda t: not t.startswith(tuple(bases)), contents)
28713009Sgabeblack@google.com
28813009Sgabeblack@google.com        self.entries = {}
28913009Sgabeblack@google.com        class Entry(object):
29013009Sgabeblack@google.com            def __init__(self, e_path):
29113009Sgabeblack@google.com                self.used = False
29213009Sgabeblack@google.com                self.path = os.path.join(path, e_path)
29313009Sgabeblack@google.com
29413009Sgabeblack@google.com            def use(self):
29513009Sgabeblack@google.com                self.used = True
29613009Sgabeblack@google.com
29713009Sgabeblack@google.com        for entry in contents:
29813009Sgabeblack@google.com            self.entries[entry] = Entry(entry)
29913009Sgabeblack@google.com
30013009Sgabeblack@google.com    def entry(self, name):
30113009Sgabeblack@google.com        def match(n):
30213009Sgabeblack@google.com            return (n == name) or n.startswith(name + '.')
30313009Sgabeblack@google.com        matches = { n: e for n, e in self.entries.items() if match(n) }
30413009Sgabeblack@google.com
30513009Sgabeblack@google.com        for match in matches.values():
30613009Sgabeblack@google.com            match.use()
30713009Sgabeblack@google.com
30813009Sgabeblack@google.com        platform_name = '.'.join([ name, self.platform ])
30913009Sgabeblack@google.com        if platform_name in matches:
31013009Sgabeblack@google.com            return matches[platform_name].path
31113009Sgabeblack@google.com        if name in matches:
31213009Sgabeblack@google.com            return matches[name].path
31313009Sgabeblack@google.com        else:
31413009Sgabeblack@google.com            return None
31513009Sgabeblack@google.com
31613009Sgabeblack@google.com    def unused(self):
31713009Sgabeblack@google.com        items = self.entries.items()
31813009Sgabeblack@google.com        items = filter(lambda i: not i[1].used, items)
31913009Sgabeblack@google.com
32013009Sgabeblack@google.com        items.sort()
32113009Sgabeblack@google.com        sources = []
32213009Sgabeblack@google.com        i = 0
32313009Sgabeblack@google.com        while i < len(items):
32413009Sgabeblack@google.com            root = items[i][0]
32513009Sgabeblack@google.com            sources.append(root)
32613009Sgabeblack@google.com            i += 1
32713009Sgabeblack@google.com            while i < len(items) and items[i][0].startswith(root):
32813009Sgabeblack@google.com                i += 1
32913009Sgabeblack@google.com        return sources
33013009Sgabeblack@google.com
33112870Sgabeblack@google.comclass VerifyPhase(TestPhaseBase):
33212870Sgabeblack@google.com    name = 'verify'
33312870Sgabeblack@google.com    number = 3
33412870Sgabeblack@google.com
33513002Sgabeblack@google.com    def reset_status(self):
33613002Sgabeblack@google.com        self._passed = []
33713002Sgabeblack@google.com        self._failed = {}
33813002Sgabeblack@google.com
33913002Sgabeblack@google.com    def passed(self, test):
34013002Sgabeblack@google.com        self._passed.append(test)
34113002Sgabeblack@google.com
34213003Sgabeblack@google.com    def failed(self, test, cause, note=''):
34313003Sgabeblack@google.com        test.set_prop('note', note)
34413002Sgabeblack@google.com        self._failed.setdefault(cause, []).append(test)
34513002Sgabeblack@google.com
34613002Sgabeblack@google.com    def print_status(self):
34713002Sgabeblack@google.com        total_passed = len(self._passed)
34813002Sgabeblack@google.com        total_failed = sum(map(len, self._failed.values()))
34913002Sgabeblack@google.com        print()
35013002Sgabeblack@google.com        print('Passed: {passed:4} - Failed: {failed:4}'.format(
35113002Sgabeblack@google.com                  passed=total_passed, failed=total_failed))
35213002Sgabeblack@google.com
35313002Sgabeblack@google.com    def write_result_file(self, path):
35413003Sgabeblack@google.com        results = {
35513003Sgabeblack@google.com            'passed': map(lambda t: t.props, self._passed),
35613003Sgabeblack@google.com            'failed': {
35713003Sgabeblack@google.com                cause: map(lambda t: t.props, tests) for
35813002Sgabeblack@google.com                       cause, tests in self._failed.iteritems()
35913003Sgabeblack@google.com            }
36013002Sgabeblack@google.com        }
36113002Sgabeblack@google.com        with open(path, 'w') as rf:
36213002Sgabeblack@google.com            json.dump(results, rf)
36313002Sgabeblack@google.com
36413002Sgabeblack@google.com    def print_results(self):
36513002Sgabeblack@google.com        print()
36613002Sgabeblack@google.com        print('Passed:')
36713003Sgabeblack@google.com        for path in sorted(list([ t.path for t in self._passed ])):
36813003Sgabeblack@google.com            print('    ', path)
36913002Sgabeblack@google.com
37013002Sgabeblack@google.com        print()
37113002Sgabeblack@google.com        print('Failed:')
37213002Sgabeblack@google.com
37313003Sgabeblack@google.com        causes = []
37413003Sgabeblack@google.com        for cause, tests in sorted(self._failed.items()):
37513003Sgabeblack@google.com            block = '  ' + cause.capitalize() + ':\n'
37613003Sgabeblack@google.com            for test in sorted(tests, key=lambda t: t.path):
37713003Sgabeblack@google.com                block += '    ' + test.path
37813003Sgabeblack@google.com                if test.note:
37913003Sgabeblack@google.com                    block += ' - ' + test.note
38013003Sgabeblack@google.com                block += '\n'
38113003Sgabeblack@google.com            causes.append(block)
38213002Sgabeblack@google.com
38313003Sgabeblack@google.com        print('\n'.join(causes))
38413002Sgabeblack@google.com
38512870Sgabeblack@google.com    def run(self, tests):
38613002Sgabeblack@google.com        parser = argparse.ArgumentParser()
38713002Sgabeblack@google.com        result_opts = parser.add_mutually_exclusive_group()
38813002Sgabeblack@google.com        result_opts.add_argument('--result-file', action='store_true',
38913002Sgabeblack@google.com                help='Create a results.json file in the current directory.')
39013002Sgabeblack@google.com        result_opts.add_argument('--result-file-at', metavar='PATH',
39113002Sgabeblack@google.com                help='Create a results json file at the given path.')
39213183Sgabeblack@google.com        parser.add_argument('--no-print-results', action='store_true',
39313183Sgabeblack@google.com                help='Don\'t print a list of tests that passed or failed')
39413002Sgabeblack@google.com        args = parser.parse_args(self.args)
39512870Sgabeblack@google.com
39613002Sgabeblack@google.com        self.reset_status()
39713002Sgabeblack@google.com
39813002Sgabeblack@google.com        runnable = filter(lambda t: not t.compile_only, tests)
39913002Sgabeblack@google.com        compile_only = filter(lambda t: t.compile_only, tests)
40013002Sgabeblack@google.com
40113002Sgabeblack@google.com        for test in compile_only:
40213002Sgabeblack@google.com            if os.path.exists(test.full_path()):
40313002Sgabeblack@google.com                self.passed(test)
40413002Sgabeblack@google.com            else:
40513002Sgabeblack@google.com                self.failed(test, 'compile failed')
40613002Sgabeblack@google.com
40713002Sgabeblack@google.com        for test in runnable:
40813002Sgabeblack@google.com            with open(test.returncode_file()) as rc:
40913002Sgabeblack@google.com                returncode = int(rc.read())
41013002Sgabeblack@google.com
41113134Sgabeblack@google.com            expected_returncode = 0
41213134Sgabeblack@google.com            if os.path.exists(test.expected_returncode_file()):
41313134Sgabeblack@google.com                with open(test.expected_returncode_file()) as erc:
41413134Sgabeblack@google.com                    expected_returncode = int(erc.read())
41513134Sgabeblack@google.com
41613003Sgabeblack@google.com            if returncode == 124:
41713002Sgabeblack@google.com                self.failed(test, 'time out')
41813003Sgabeblack@google.com                continue
41913134Sgabeblack@google.com            elif returncode != expected_returncode:
42013134Sgabeblack@google.com                if expected_returncode == 0:
42113134Sgabeblack@google.com                    self.failed(test, 'abort')
42213134Sgabeblack@google.com                else:
42313134Sgabeblack@google.com                    self.failed(test, 'missed abort')
42413003Sgabeblack@google.com                continue
42513003Sgabeblack@google.com
42613003Sgabeblack@google.com            out_dir = test.m5out_dir()
42713003Sgabeblack@google.com
42813003Sgabeblack@google.com            Diff = collections.namedtuple(
42913003Sgabeblack@google.com                    'Diff', 'ref, test, tag, ref_filter')
43013003Sgabeblack@google.com
43113003Sgabeblack@google.com            diffs = []
43213003Sgabeblack@google.com
43313009Sgabeblack@google.com            gd = GoldenDir(test.golden_dir(), 'linux64')
43413009Sgabeblack@google.com
43513009Sgabeblack@google.com            missing = []
43613003Sgabeblack@google.com            log_file = '.'.join([test.name, 'log'])
43713009Sgabeblack@google.com            log_path = gd.entry(log_file)
43813003Sgabeblack@google.com            simout_path = os.path.join(out_dir, 'simout')
43913003Sgabeblack@google.com            if not os.path.exists(simout_path):
44013009Sgabeblack@google.com                missing.append('log output')
44113009Sgabeblack@google.com            elif log_path:
44213009Sgabeblack@google.com                diffs.append(LogChecker(log_path, simout_path,
44313009Sgabeblack@google.com                                        log_file, out_dir))
44413009Sgabeblack@google.com
44513009Sgabeblack@google.com            for name in gd.unused():
44613009Sgabeblack@google.com                test_path = os.path.join(out_dir, name)
44713009Sgabeblack@google.com                ref_path = gd.entry(name)
44813009Sgabeblack@google.com                if not os.path.exists(test_path):
44913009Sgabeblack@google.com                    missing.append(name)
45013009Sgabeblack@google.com                else:
45113009Sgabeblack@google.com                    diffs.append(Checker(ref_path, test_path, name))
45213009Sgabeblack@google.com
45313009Sgabeblack@google.com            if missing:
45413009Sgabeblack@google.com                self.failed(test, 'missing output', ' '.join(missing))
45513009Sgabeblack@google.com                continue
45613003Sgabeblack@google.com
45713003Sgabeblack@google.com            failed_diffs = filter(lambda d: not d.check(), diffs)
45813003Sgabeblack@google.com            if failed_diffs:
45913003Sgabeblack@google.com                tags = map(lambda d: d.tag, failed_diffs)
46013003Sgabeblack@google.com                self.failed(test, 'failed diffs', ' '.join(tags))
46113003Sgabeblack@google.com                continue
46213003Sgabeblack@google.com
46313003Sgabeblack@google.com            self.passed(test)
46413002Sgabeblack@google.com
46513183Sgabeblack@google.com        if not args.no_print_results:
46613002Sgabeblack@google.com            self.print_results()
46713002Sgabeblack@google.com
46813002Sgabeblack@google.com        self.print_status()
46913002Sgabeblack@google.com
47013002Sgabeblack@google.com        result_path = None
47113002Sgabeblack@google.com        if args.result_file:
47213002Sgabeblack@google.com            result_path = os.path.join(os.getcwd(), 'results.json')
47313002Sgabeblack@google.com        elif args.result_file_at:
47413002Sgabeblack@google.com            result_path = args.result_file_at
47513002Sgabeblack@google.com
47613002Sgabeblack@google.com        if result_path:
47713002Sgabeblack@google.com            self.write_result_file(result_path)
47812870Sgabeblack@google.com
47912870Sgabeblack@google.com
48012870Sgabeblack@google.comparser = argparse.ArgumentParser(description='SystemC test utility')
48112870Sgabeblack@google.com
48212870Sgabeblack@google.comparser.add_argument('build_dir', metavar='BUILD_DIR',
48312870Sgabeblack@google.com                    help='The build directory (ie. build/ARM).')
48412870Sgabeblack@google.com
48512870Sgabeblack@google.comparser.add_argument('--update-json', action='store_true',
48612870Sgabeblack@google.com                    help='Update the json manifest of tests.')
48712870Sgabeblack@google.com
48812870Sgabeblack@google.comparser.add_argument('--flavor', choices=['debug', 'opt', 'fast'],
48912870Sgabeblack@google.com                    default='opt',
49012870Sgabeblack@google.com                    help='Flavor of binary to test.')
49112870Sgabeblack@google.com
49212870Sgabeblack@google.comparser.add_argument('--list', action='store_true',
49312870Sgabeblack@google.com                    help='List the available tests')
49412870Sgabeblack@google.com
49513183Sgabeblack@google.comparser.add_argument('-j', type=int, default=1,
49613183Sgabeblack@google.com                    help='Default level of parallelism, can be overriden '
49713183Sgabeblack@google.com                    'for individual stages')
49813183Sgabeblack@google.com
49912903Sgabeblack@google.comfilter_opts = parser.add_mutually_exclusive_group()
50012903Sgabeblack@google.comfilter_opts.add_argument('--filter', default='True',
50112903Sgabeblack@google.com                         help='Python expression which filters tests based '
50212903Sgabeblack@google.com                         'on their properties')
50312903Sgabeblack@google.comfilter_opts.add_argument('--filter-file', default=None,
50412903Sgabeblack@google.com                         type=argparse.FileType('r'),
50512903Sgabeblack@google.com                         help='Same as --filter, but read from a file')
50612870Sgabeblack@google.com
50712870Sgabeblack@google.comdef collect_phases(args):
50812870Sgabeblack@google.com    phase_groups = [list(g) for k, g in
50912870Sgabeblack@google.com                    itertools.groupby(args, lambda x: x != '--phase') if k]
51012870Sgabeblack@google.com    main_args = parser.parse_args(phase_groups[0][1:])
51112870Sgabeblack@google.com    phases = []
51212870Sgabeblack@google.com    names = []
51312870Sgabeblack@google.com    for group in phase_groups[1:]:
51412870Sgabeblack@google.com        name = group[0]
51512870Sgabeblack@google.com        if name in names:
51612870Sgabeblack@google.com            raise RuntimeException('Phase %s specified more than once' % name)
51712870Sgabeblack@google.com        phase = test_phase_classes[name]
51812870Sgabeblack@google.com        phases.append(phase(main_args, *group[1:]))
51912870Sgabeblack@google.com    phases.sort()
52012870Sgabeblack@google.com    return main_args, phases
52112870Sgabeblack@google.com
52212870Sgabeblack@google.commain_args, phases = collect_phases(sys.argv)
52312870Sgabeblack@google.com
52412870Sgabeblack@google.comif len(phases) == 0:
52512870Sgabeblack@google.com    phases = [
52612870Sgabeblack@google.com        CompilePhase(main_args),
52712870Sgabeblack@google.com        RunPhase(main_args),
52812870Sgabeblack@google.com        VerifyPhase(main_args)
52912870Sgabeblack@google.com    ]
53012870Sgabeblack@google.com
53112870Sgabeblack@google.com
53212870Sgabeblack@google.com
53312870Sgabeblack@google.comjson_path = os.path.join(main_args.build_dir, json_rel_path)
53412870Sgabeblack@google.com
53512870Sgabeblack@google.comif main_args.update_json:
53612870Sgabeblack@google.com    scons(os.path.join(json_path))
53712870Sgabeblack@google.com
53812870Sgabeblack@google.comwith open(json_path) as f:
53912870Sgabeblack@google.com    test_data = json.load(f)
54012870Sgabeblack@google.com
54112903Sgabeblack@google.com    if main_args.filter_file:
54212903Sgabeblack@google.com        f = main_args.filter_file
54312903Sgabeblack@google.com        filt = compile(f.read(), f.name, 'eval')
54412903Sgabeblack@google.com    else:
54512903Sgabeblack@google.com        filt = compile(main_args.filter, '<string>', 'eval')
54612903Sgabeblack@google.com
54712903Sgabeblack@google.com    filtered_tests = {
54812903Sgabeblack@google.com        target: props for (target, props) in
54912903Sgabeblack@google.com                    test_data.iteritems() if eval(filt, dict(props))
55012903Sgabeblack@google.com    }
55112903Sgabeblack@google.com
55213101Sgabeblack@google.com    if len(filtered_tests) == 0:
55313101Sgabeblack@google.com        print('All tests were filtered out.')
55413101Sgabeblack@google.com        exit()
55513101Sgabeblack@google.com
55612870Sgabeblack@google.com    if main_args.list:
55712903Sgabeblack@google.com        for target, props in sorted(filtered_tests.iteritems()):
55812870Sgabeblack@google.com            print('%s.%s' % (target, main_args.flavor))
55912870Sgabeblack@google.com            for key, val in props.iteritems():
56012870Sgabeblack@google.com                print('    %s: %s' % (key, val))
56112937Sgabeblack@google.com        print('Total tests: %d' % len(filtered_tests))
56212870Sgabeblack@google.com    else:
56312903Sgabeblack@google.com        tests_to_run = list([
56412903Sgabeblack@google.com            Test(target, main_args.flavor, main_args.build_dir, props) for
56512903Sgabeblack@google.com                target, props in sorted(filtered_tests.iteritems())
56612903Sgabeblack@google.com        ])
56712870Sgabeblack@google.com
56812870Sgabeblack@google.com        for phase in phases:
56912870Sgabeblack@google.com            phase.run(tests_to_run)
570