SConscript revision 8498
12929Sktlim@umich.edu# -*- mode:python -*-
22929Sktlim@umich.edu
32932Sktlim@umich.edu# Copyright (c) 2004-2006 The Regents of The University of Michigan
42929Sktlim@umich.edu# All rights reserved.
52929Sktlim@umich.edu#
62929Sktlim@umich.edu# Redistribution and use in source and binary forms, with or without
72929Sktlim@umich.edu# modification, are permitted provided that the following conditions are
82929Sktlim@umich.edu# met: redistributions of source code must retain the above copyright
92929Sktlim@umich.edu# notice, this list of conditions and the following disclaimer;
102929Sktlim@umich.edu# redistributions in binary form must reproduce the above copyright
112929Sktlim@umich.edu# notice, this list of conditions and the following disclaimer in the
122929Sktlim@umich.edu# documentation and/or other materials provided with the distribution;
132929Sktlim@umich.edu# neither the name of the copyright holders nor the names of its
142929Sktlim@umich.edu# contributors may be used to endorse or promote products derived from
152929Sktlim@umich.edu# this software without specific prior written permission.
162929Sktlim@umich.edu#
172929Sktlim@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182929Sktlim@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192929Sktlim@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202929Sktlim@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212929Sktlim@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222929Sktlim@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232929Sktlim@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242929Sktlim@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252929Sktlim@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262929Sktlim@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272929Sktlim@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282932Sktlim@umich.edu#
292932Sktlim@umich.edu# Authors: Steve Reinhardt
302932Sktlim@umich.edu#          Kevin Lim
312929Sktlim@umich.edu
326007Ssteve.reinhardt@amd.comimport os, signal
332929Sktlim@umich.eduimport sys, time
342929Sktlim@umich.eduimport glob
352929Sktlim@umich.edufrom SCons.Script.SConscript import SConsEnvironment
362929Sktlim@umich.edu
372929Sktlim@umich.eduImport('env')
382929Sktlim@umich.edu
392929Sktlim@umich.eduenv['DIFFOUT'] = File('diff-out')
402929Sktlim@umich.edu
412929Sktlim@umich.edu# Dict that accumulates lists of tests by category (quick, medium, long)
422929Sktlim@umich.eduenv.Tests = {}
432929Sktlim@umich.edu
442929Sktlim@umich.edudef contents(node):
452929Sktlim@umich.edu    return file(str(node)).read()
462929Sktlim@umich.edu
476007Ssteve.reinhardt@amd.com# functions to parse return value from scons Execute()... not the same
486007Ssteve.reinhardt@amd.com# as wait() etc., so python built-in os funcs don't work.
496007Ssteve.reinhardt@amd.comdef signaled(status):
506007Ssteve.reinhardt@amd.com    return (status & 0x80) != 0;
516007Ssteve.reinhardt@amd.com
526007Ssteve.reinhardt@amd.comdef signum(status):
536007Ssteve.reinhardt@amd.com    return (status & 0x7f);
546007Ssteve.reinhardt@amd.com
556007Ssteve.reinhardt@amd.com# List of signals that indicate that we should retry the test rather
566007Ssteve.reinhardt@amd.com# than consider it failed.
576007Ssteve.reinhardt@amd.comretry_signals = (signal.SIGTERM, signal.SIGKILL, signal.SIGINT,
586007Ssteve.reinhardt@amd.com                 signal.SIGQUIT, signal.SIGHUP)
596007Ssteve.reinhardt@amd.com
606007Ssteve.reinhardt@amd.com# regular expressions of lines to ignore when diffing outputs
616007Ssteve.reinhardt@amd.comoutput_ignore_regexes = (
626007Ssteve.reinhardt@amd.com    '^command line:',		# for stdout file
636007Ssteve.reinhardt@amd.com    '^M5 compiled ',		# for stderr file
646007Ssteve.reinhardt@amd.com    '^M5 started ',		# for stderr file
656007Ssteve.reinhardt@amd.com    '^M5 executing on ',	# for stderr file
666007Ssteve.reinhardt@amd.com    '^Simulation complete at',	# for stderr file
676007Ssteve.reinhardt@amd.com    '^Listening for',		# for stderr file
686007Ssteve.reinhardt@amd.com    'listening for remote gdb',	# for stderr file
696007Ssteve.reinhardt@amd.com    )
706007Ssteve.reinhardt@amd.com
716007Ssteve.reinhardt@amd.comoutput_ignore_args = ' '.join(["-I '"+s+"'" for s in output_ignore_regexes])
726007Ssteve.reinhardt@amd.com
736007Ssteve.reinhardt@amd.comoutput_ignore_args += ' --exclude=stats.txt --exclude=outdiff'
746007Ssteve.reinhardt@amd.com
756007Ssteve.reinhardt@amd.comdef run_test(target, source, env):
762929Sktlim@umich.edu    """Check output from running test.
772929Sktlim@umich.edu
782929Sktlim@umich.edu    Targets are as follows:
796007Ssteve.reinhardt@amd.com    target[0] : status
806007Ssteve.reinhardt@amd.com
816007Ssteve.reinhardt@amd.com    Sources are:
826007Ssteve.reinhardt@amd.com    source[0] : M5 binary
836007Ssteve.reinhardt@amd.com    source[1] : tests/run.py script
846007Ssteve.reinhardt@amd.com    source[2] : reference stats file
852929Sktlim@umich.edu
862929Sktlim@umich.edu    """
872929Sktlim@umich.edu    # make sure target files are all gone
882929Sktlim@umich.edu    for t in target:
892929Sktlim@umich.edu        if os.path.exists(t.abspath):
906011Ssteve.reinhardt@amd.com            env.Execute(Delete(t.abspath))
916007Ssteve.reinhardt@amd.com
926007Ssteve.reinhardt@amd.com    tgt_dir = os.path.dirname(str(target[0]))
936007Ssteve.reinhardt@amd.com
946007Ssteve.reinhardt@amd.com    # Base command for running test.  We mess around with indirectly
956007Ssteve.reinhardt@amd.com    # referring to files via SOURCES and TARGETS so that scons can mess
966007Ssteve.reinhardt@amd.com    # with paths all it wants to and we still get the right files.
976007Ssteve.reinhardt@amd.com    cmd = '${SOURCES[0]} -d %s -re ${SOURCES[1]} %s' % (tgt_dir, tgt_dir)
986007Ssteve.reinhardt@amd.com
996007Ssteve.reinhardt@amd.com    # Prefix test run with batch job submission command if appropriate.
1006007Ssteve.reinhardt@amd.com    # Batch command also supports timeout arg (in seconds, not minutes).
1016007Ssteve.reinhardt@amd.com    timeout = 15 * 60 # used to be a param, probably should be again
1026007Ssteve.reinhardt@amd.com    if env['BATCH']:
1036007Ssteve.reinhardt@amd.com        cmd = '%s -t %d %s' % (env['BATCH_CMD'], timeout, cmd)
1046007Ssteve.reinhardt@amd.com
1056011Ssteve.reinhardt@amd.com    pre_exec_time = time.time()
1066007Ssteve.reinhardt@amd.com    status = env.Execute(env.subst(cmd, target=target, source=source))
1076007Ssteve.reinhardt@amd.com    if status == 0:
1086007Ssteve.reinhardt@amd.com        # M5 terminated normally.
1096007Ssteve.reinhardt@amd.com        # Run diff on output & ref directories to find differences.
1106007Ssteve.reinhardt@amd.com        # Exclude the stats file since we will use diff-out on that.
1117685Ssteve.reinhardt@amd.com
1126007Ssteve.reinhardt@amd.com        # NFS file systems can be annoying and not have updated yet
1136011Ssteve.reinhardt@amd.com        # wait until we see the file modified
1146007Ssteve.reinhardt@amd.com        statsdiff = os.path.join(tgt_dir, 'statsdiff')
1156007Ssteve.reinhardt@amd.com        m_time = 0
1166007Ssteve.reinhardt@amd.com        nap = 0
1176007Ssteve.reinhardt@amd.com        while m_time < pre_exec_time and nap < 10:
1186007Ssteve.reinhardt@amd.com            try:
1196007Ssteve.reinhardt@amd.com                m_time = os.stat(statsdiff).st_mtime
1206007Ssteve.reinhardt@amd.com            except OSError:
1216011Ssteve.reinhardt@amd.com                pass
1226007Ssteve.reinhardt@amd.com            time.sleep(1)
1236007Ssteve.reinhardt@amd.com            nap += 1
1246007Ssteve.reinhardt@amd.com
1256007Ssteve.reinhardt@amd.com        outdiff = os.path.join(tgt_dir, 'outdiff')
1266007Ssteve.reinhardt@amd.com        diffcmd = 'diff -ubrs %s ${SOURCES[2].dir} %s > %s' \
1276008Ssteve.reinhardt@amd.com                  % (output_ignore_args, tgt_dir, outdiff)
1286007Ssteve.reinhardt@amd.com        env.Execute(env.subst(diffcmd, target=target, source=source))
1296008Ssteve.reinhardt@amd.com        print "===== Output differences ====="
1306008Ssteve.reinhardt@amd.com        print contents(outdiff)
1316008Ssteve.reinhardt@amd.com        # Run diff-out on stats.txt file
1326008Ssteve.reinhardt@amd.com        diffcmd = '$DIFFOUT ${SOURCES[2]} %s > %s' \
1336008Ssteve.reinhardt@amd.com                  % (os.path.join(tgt_dir, 'stats.txt'), statsdiff)
1346008Ssteve.reinhardt@amd.com        diffcmd = env.subst(diffcmd, target=target, source=source)
1356008Ssteve.reinhardt@amd.com        status = env.Execute(diffcmd, strfunction=None)
1366007Ssteve.reinhardt@amd.com        print "===== Statistics differences ====="
1376007Ssteve.reinhardt@amd.com        print contents(statsdiff)
1386007Ssteve.reinhardt@amd.com
1396007Ssteve.reinhardt@amd.com    else: # m5 exit status != 0
1406007Ssteve.reinhardt@amd.com        # M5 did not terminate properly, so no need to check the output
1412929Sktlim@umich.edu        if signaled(status):
1422929Sktlim@umich.edu            print 'M5 terminated with signal', signum(status)
1432929Sktlim@umich.edu            if signum(status) in retry_signals:
1442929Sktlim@umich.edu                # Consider the test incomplete; don't create a 'status' output.
1456007Ssteve.reinhardt@amd.com                # Hand the return status to scons and let scons decide what
1466007Ssteve.reinhardt@amd.com                # to do about it (typically terminate unless run with -k).
1472929Sktlim@umich.edu                return status
1482929Sktlim@umich.edu        else:
1492929Sktlim@umich.edu            print 'M5 exited with non-zero status', status
1502929Sktlim@umich.edu        # complete but failed execution (call to exit() with non-zero
1516007Ssteve.reinhardt@amd.com        # status, SIGABORT due to assertion failure, etc.)... fall through
1526007Ssteve.reinhardt@amd.com        # and generate FAILED status as if output comparison had failed
1532929Sktlim@umich.edu
1542929Sktlim@umich.edu    # Generate status file contents based on exit status of m5 or diff-out
1556007Ssteve.reinhardt@amd.com    if status == 0:
1562929Sktlim@umich.edu        status_str = "passed."
1572929Sktlim@umich.edu    else:
1582929Sktlim@umich.edu        status_str = "FAILED!"
1592929Sktlim@umich.edu    f = file(str(target[0]), 'w')
1602929Sktlim@umich.edu    print >>f, tgt_dir, status_str
1612929Sktlim@umich.edu    f.close()
1622929Sktlim@umich.edu    # done
1634937Sstever@gmail.com    return 0
1644937Sstever@gmail.com
1654937Sstever@gmail.comdef run_test_string(target, source, env):
1664937Sstever@gmail.com    return env.subst("Running test in ${TARGETS[0].dir}.",
1674937Sstever@gmail.com                     target=target, source=source)
1684937Sstever@gmail.com
1694937Sstever@gmail.comtestAction = env.Action(run_test, run_test_string)
1704937Sstever@gmail.com
1714937Sstever@gmail.comdef print_test(target, source, env):
1725773Snate@binkert.org    print '***** ' + contents(source[0])
1734937Sstever@gmail.com    return 0
1744937Sstever@gmail.com
1754937Sstever@gmail.comprintAction = env.Action(print_test, strfunction = None)
1762929Sktlim@umich.edu
1772929Sktlim@umich.edu# Static vars for update_test:
1782929Sktlim@umich.edu# - long-winded message about ignored sources
1795773Snate@binkert.orgignore_msg = '''
1802929Sktlim@umich.eduNote: The following file(s) will not be copied.  New non-standard
1812929Sktlim@umich.edu      output files must be copied manually once before --update-ref will
1822929Sktlim@umich.edu      recognize them as outputs.  Otherwise they are assumed to be
1832929Sktlim@umich.edu      inputs and are ignored.
1842929Sktlim@umich.edu'''
1852929Sktlim@umich.edu# - reference files always needed
1864937Sstever@gmail.comneeded_files = set(['simout', 'simerr', 'stats.txt', 'config.ini'])
1874937Sstever@gmail.com# - source files we always want to ignore
1884937Sstever@gmail.comknown_ignores = set(['status', 'outdiff', 'statsdiff'])
1894937Sstever@gmail.com
1904937Sstever@gmail.comdef update_test(target, source, env):
1914937Sstever@gmail.com    """Update reference test outputs.
1924937Sstever@gmail.com
1934937Sstever@gmail.com    Target is phony.  First two sources are the ref & new stats.txt file
1944937Sstever@gmail.com    files, respectively.  We actually copy everything in the
1954937Sstever@gmail.com    respective directories except the status & diff output files.
1964937Sstever@gmail.com
1974937Sstever@gmail.com    """
1984937Sstever@gmail.com    dest_dir = str(source[0].get_dir())
1994937Sstever@gmail.com    src_dir = str(source[1].get_dir())
2004937Sstever@gmail.com    dest_files = set(os.listdir(dest_dir))
2012929Sktlim@umich.edu    src_files = set(os.listdir(src_dir))
2022929Sktlim@umich.edu    # Copy all of the required files plus any existing dest files.
2032929Sktlim@umich.edu    wanted_files = needed_files | dest_files
2042929Sktlim@umich.edu    missing_files = wanted_files - src_files
2052929Sktlim@umich.edu    if len(missing_files) > 0:
2062929Sktlim@umich.edu        print "  WARNING: the following file(s) are missing " \
2072929Sktlim@umich.edu              "and will not be updated:"
2086011Ssteve.reinhardt@amd.com        print "    ", " ,".join(missing_files)
2092929Sktlim@umich.edu    copy_files = wanted_files - missing_files
2102929Sktlim@umich.edu    warn_ignored_files = (src_files - copy_files) - known_ignores
2112929Sktlim@umich.edu    if len(warn_ignored_files) > 0:
2122929Sktlim@umich.edu        print ignore_msg,
2132929Sktlim@umich.edu        print "       ", ", ".join(warn_ignored_files)
2142929Sktlim@umich.edu    for f in copy_files:
2152929Sktlim@umich.edu        if f in dest_files:
2162929Sktlim@umich.edu            print "  Replacing file", f
2172997Sstever@eecs.umich.edu            dest_files.remove(f)
2182997Sstever@eecs.umich.edu        else:
2192929Sktlim@umich.edu            print "  Creating new file", f
2202997Sstever@eecs.umich.edu        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
2212997Sstever@eecs.umich.edu        copyAction.strfunction = None
2222929Sktlim@umich.edu        env.Execute(copyAction)
2232997Sstever@eecs.umich.edu    return 0
2242997Sstever@eecs.umich.edu
2252997Sstever@eecs.umich.edudef update_test_string(target, source, env):
2262929Sktlim@umich.edu    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
2272997Sstever@eecs.umich.edu                     target=target, source=source)
2282997Sstever@eecs.umich.edu
2292997Sstever@eecs.umich.eduupdateAction = env.Action(update_test, update_test_string)
2302997Sstever@eecs.umich.edu
2315773Snate@binkert.orgdef test_builder(env, ref_dir):
2325773Snate@binkert.org    """Define a test."""
2332997Sstever@eecs.umich.edu
2342997Sstever@eecs.umich.edu    (category, name, _ref, isa, opsys, config) = ref_dir.split('/')
2356007Ssteve.reinhardt@amd.com    assert(_ref == 'ref')
2366007Ssteve.reinhardt@amd.com
2372997Sstever@eecs.umich.edu    # target path (where test output goes) is the same except without
2382929Sktlim@umich.edu    # the 'ref' component
2392997Sstever@eecs.umich.edu    tgt_dir = os.path.join(category, name, isa, opsys, config)
2402997Sstever@eecs.umich.edu
2412997Sstever@eecs.umich.edu    # prepend file name with tgt_dir
2422997Sstever@eecs.umich.edu    def tgt(f):
2432997Sstever@eecs.umich.edu        return os.path.join(tgt_dir, f)
2442997Sstever@eecs.umich.edu
2452997Sstever@eecs.umich.edu    ref_stats = os.path.join(ref_dir, 'stats.txt')
2462929Sktlim@umich.edu    new_stats = tgt('stats.txt')
2472997Sstever@eecs.umich.edu    status_file = tgt('status')
2482929Sktlim@umich.edu
2492929Sktlim@umich.edu    env.Command([status_file],
2503005Sstever@eecs.umich.edu                [env.M5Binary, 'run.py', ref_stats],
2513005Sstever@eecs.umich.edu                testAction)
2523005Sstever@eecs.umich.edu
2533005Sstever@eecs.umich.edu    # phony target to echo status
2546025Snate@binkert.org    if GetOption('update_ref'):
2556025Snate@binkert.org        p = env.Command(tgt('_update'),
2566025Snate@binkert.org                        [ref_stats, new_stats, status_file],
2576025Snate@binkert.org                        updateAction)
2586025Snate@binkert.org    else:
2596025Snate@binkert.org        p = env.Command(tgt('_print'), [status_file], printAction)
2604130Ssaidi@eecs.umich.edu
2614130Ssaidi@eecs.umich.edu    env.AlwaysBuild(p)
2624130Ssaidi@eecs.umich.edu
2633691Shsul@eecs.umich.edu
2643005Sstever@eecs.umich.edu# Figure out applicable configs based on build type
2655721Shsul@eecs.umich.educonfigs = []
2666194Sksewell@umich.eduif env['FULL_SYSTEM']:
2676928SBrad.Beckmann@amd.com    if env['TARGET_ISA'] == 'alpha':
2683005Sstever@eecs.umich.edu        configs += ['tsunami-simple-atomic',
2696168Snate@binkert.org                    'tsunami-simple-timing',
2706928SBrad.Beckmann@amd.com                    'tsunami-simple-atomic-dual',
2716928SBrad.Beckmann@amd.com                    'tsunami-simple-timing-dual',
2726928SBrad.Beckmann@amd.com                    'twosys-tsunami-simple-atomic',
2736928SBrad.Beckmann@amd.com                    'tsunami-o3', 'tsunami-o3-dual',
2746928SBrad.Beckmann@amd.com                    'tsunami-inorder']
2756928SBrad.Beckmann@amd.com    if env['TARGET_ISA'] == 'sparc':
2766928SBrad.Beckmann@amd.com        configs += ['t1000-simple-atomic',
2776928SBrad.Beckmann@amd.com                    't1000-simple-timing']
2786928SBrad.Beckmann@amd.com    if env['TARGET_ISA'] == 'arm':
2796928SBrad.Beckmann@amd.com        configs += ['realview-simple-atomic',
2806928SBrad.Beckmann@amd.com                    'realview-simple-timing',
2816928SBrad.Beckmann@amd.com                    'realview-o3']
2826928SBrad.Beckmann@amd.com    if env['TARGET_ISA'] == 'x86':
2836928SBrad.Beckmann@amd.com        configs += ['pc-simple-atomic',
2846166Ssteve.reinhardt@amd.com                    'pc-simple-timing',
2852929Sktlim@umich.edu                    'pc-o3-timing']
2862929Sktlim@umich.edu
2873005Sstever@eecs.umich.eduelse:
2882997Sstever@eecs.umich.edu    configs += ['simple-atomic', 'simple-timing', 'o3-timing', 'memtest',
2892997Sstever@eecs.umich.edu                'simple-atomic-mp', 'simple-timing-mp', 'o3-timing-mp',
2906293Ssteve.reinhardt@amd.com                'inorder-timing', 'rubytest']
2916293Ssteve.reinhardt@amd.com
2922929Sktlim@umich.eduif env['PROTOCOL'] != 'None':
293    if env['PROTOCOL'] == 'MI_example':
294        configs += [c + "-ruby" for c in configs]
295    else:
296        configs = [c + "-ruby-" + env['PROTOCOL'] for c in configs]
297
298cwd = os.getcwd()
299os.chdir(str(Dir('.').srcdir))
300for config in configs:
301    dirs = glob.glob('*/*/ref/%s/*/%s' % (env['TARGET_ISA'], config))
302    for d in dirs:
303        if not os.path.exists(os.path.join(d, 'skip')):
304            test_builder(env, d)
305os.chdir(cwd)
306