SConscript revision 2929
12929Sktlim@umich.edu# -*- mode:python -*-
22929Sktlim@umich.edu
32929Sktlim@umich.edu# Copyright (c) 2004-2005 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.
282929Sktlim@umich.edu
292929Sktlim@umich.eduimport os
302929Sktlim@umich.eduimport sys
312929Sktlim@umich.eduimport glob
322929Sktlim@umich.edufrom SCons.Script.SConscript import SConsEnvironment
332929Sktlim@umich.edu
342929Sktlim@umich.eduImport('env')
352929Sktlim@umich.edu
362929Sktlim@umich.eduenv['DIFFOUT'] = File('diff-out')
372929Sktlim@umich.edu
382929Sktlim@umich.edu# Dict that accumulates lists of tests by category (quick, medium, long)
392929Sktlim@umich.eduenv.Tests = {}
402929Sktlim@umich.edu
412929Sktlim@umich.edudef contents(node):
422929Sktlim@umich.edu    return file(str(node)).read()
432929Sktlim@umich.edu
442929Sktlim@umich.edudef check_test(target, source, env):
452929Sktlim@umich.edu    """Check output from running test.
462929Sktlim@umich.edu
472929Sktlim@umich.edu    Targets are as follows:
482929Sktlim@umich.edu    target[0] : outdiff
492929Sktlim@umich.edu    target[1] : statsdiff
502929Sktlim@umich.edu    target[2] : status
512929Sktlim@umich.edu
522929Sktlim@umich.edu    """
532929Sktlim@umich.edu    # make sure target files are all gone
542929Sktlim@umich.edu    for t in target:
552929Sktlim@umich.edu        if os.path.exists(t.abspath):
562929Sktlim@umich.edu            Execute(Delete(t.abspath))
572929Sktlim@umich.edu    # Run diff on output & ref directories to find differences.
582929Sktlim@umich.edu    # Exclude m5stats.txt since we will use diff-out on that.
592929Sktlim@umich.edu    Execute(env.subst('diff -ubr ${SOURCES[0].dir} ${SOURCES[1].dir} ' +
602929Sktlim@umich.edu                      '-I "^command line:" ' +		# for stdout file
612929Sktlim@umich.edu                      '-I "^M5 compiled on" ' +		# for stderr file
622929Sktlim@umich.edu                      '-I "^M5 simulation started" ' +	# for stderr file
632929Sktlim@umich.edu                      '-I "^Simulation complete at" ' +	# for stderr file
642929Sktlim@umich.edu                      '-I "^Listening for" ' +		# for stderr file
652929Sktlim@umich.edu                      '--exclude=m5stats.txt --exclude=SCCS ' +
662929Sktlim@umich.edu                      '--exclude=${TARGETS[0].file} ' +
672929Sktlim@umich.edu                      '> ${TARGETS[0]}', target=target, source=source), None)
682929Sktlim@umich.edu    print "===== Output differences ====="
692929Sktlim@umich.edu    print contents(target[0])
702929Sktlim@umich.edu    # Run diff-out on m5stats.txt file
712929Sktlim@umich.edu    status = Execute(env.subst('$DIFFOUT $SOURCES > ${TARGETS[1]}',
722929Sktlim@umich.edu                               target=target, source=source),
732929Sktlim@umich.edu                     strfunction=None)
742929Sktlim@umich.edu    print "===== Statistics differences ====="
752929Sktlim@umich.edu    print contents(target[1])
762929Sktlim@umich.edu    # Generate status file contents based on exit status of diff-out
772929Sktlim@umich.edu    if status == 0:
782929Sktlim@umich.edu        status_str = "passed."
792929Sktlim@umich.edu    else:
802929Sktlim@umich.edu        status_str = "FAILED!"
812929Sktlim@umich.edu    f = file(str(target[2]), 'w')
822929Sktlim@umich.edu    print >>f, env.subst('${TARGETS[2].dir}', target=target, source=source), \
832929Sktlim@umich.edu          status_str
842929Sktlim@umich.edu    f.close()
852929Sktlim@umich.edu    # done
862929Sktlim@umich.edu    return 0
872929Sktlim@umich.edu
882929Sktlim@umich.edudef check_test_string(target, source, env):
892929Sktlim@umich.edu    return env.subst("Comparing outputs in ${TARGETS[0].dir}.",
902929Sktlim@umich.edu                     target=target, source=source)
912929Sktlim@umich.edu
922929Sktlim@umich.edutestAction = env.Action(check_test, check_test_string)
932929Sktlim@umich.edu
942929Sktlim@umich.edudef print_test(target, source, env):
952929Sktlim@umich.edu    print '***** ' + contents(source[0])
962929Sktlim@umich.edu    return 0
972929Sktlim@umich.edu
982929Sktlim@umich.eduprintAction = env.Action(print_test, strfunction = None)
992929Sktlim@umich.edu
1002929Sktlim@umich.edudef update_test(target, source, env):
1012929Sktlim@umich.edu    """Update reference test outputs.
1022929Sktlim@umich.edu
1032929Sktlim@umich.edu    Target is phony.  First two sources are the ref & new m5stats.txt
1042929Sktlim@umich.edu    files, respectively.  We actually copy everything in the
1052929Sktlim@umich.edu    respective directories except the status & diff output files.
1062929Sktlim@umich.edu
1072929Sktlim@umich.edu    """
1082929Sktlim@umich.edu    dest_dir = str(source[0].get_dir())
1092929Sktlim@umich.edu    src_dir = str(source[1].get_dir())
1102929Sktlim@umich.edu    dest_files = os.listdir(dest_dir)
1112929Sktlim@umich.edu    src_files = os.listdir(src_dir)
1122929Sktlim@umich.edu    # Exclude status & diff outputs
1132929Sktlim@umich.edu    for f in ('outdiff', 'statsdiff', 'status'):
1142929Sktlim@umich.edu        if f in src_files:
1152929Sktlim@umich.edu            src_files.remove(f)
1162929Sktlim@umich.edu    for f in src_files:
1172929Sktlim@umich.edu        if f in dest_files:
1182929Sktlim@umich.edu            print "  Replacing file", f
1192929Sktlim@umich.edu            dest_files.remove(f)
1202929Sktlim@umich.edu        else:
1212929Sktlim@umich.edu            print "  Creating new file", f
1222929Sktlim@umich.edu        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
1232929Sktlim@umich.edu        copyAction.strfunction = None
1242929Sktlim@umich.edu        Execute(copyAction)
1252929Sktlim@umich.edu    # warn about any files in dest not overwritten (other than SCCS dir)
1262929Sktlim@umich.edu    if 'SCCS' in dest_files:
1272929Sktlim@umich.edu        dest_files.remove('SCCS')
1282929Sktlim@umich.edu    if dest_files:
1292929Sktlim@umich.edu        print "Warning: file(s) in", dest_dir, "not updated:",
1302929Sktlim@umich.edu        print ', '.join(dest_files)
1312929Sktlim@umich.edu    return 0
1322929Sktlim@umich.edu
1332929Sktlim@umich.edudef update_test_string(target, source, env):
1342929Sktlim@umich.edu    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
1352929Sktlim@umich.edu                     target=target, source=source)
1362929Sktlim@umich.edu
1372929Sktlim@umich.eduupdateAction = env.Action(update_test, update_test_string)
1382929Sktlim@umich.edu
1392929Sktlim@umich.edudef test_builder(env, category, cpu_list=[], os_list=[], refdir='ref', timeout=15):
1402929Sktlim@umich.edu    """Define a test.
1412929Sktlim@umich.edu
1422929Sktlim@umich.edu    Args:
1432929Sktlim@umich.edu    category -- string describing test category (e.g., 'quick')
1442929Sktlim@umich.edu    cpu_list -- list of CPUs to runs this test on (blank means all compiled CPUs)
1452929Sktlim@umich.edu    os_list -- list of OSs to run this test on
1462929Sktlim@umich.edu    refdir -- subdirectory containing reference output (default 'ref')
1472929Sktlim@umich.edu    timeout -- test timeout in minutes (only enforced on pool)
1482929Sktlim@umich.edu
1492929Sktlim@umich.edu    """
1502929Sktlim@umich.edu
1512929Sktlim@umich.edu    default_refdir = False
1522929Sktlim@umich.edu    if refdir == 'ref':
1532929Sktlim@umich.edu        default_refdir = True
1542929Sktlim@umich.edu    if len(cpu_list) == 0:
1552929Sktlim@umich.edu        cpu_list = env['CPU_MODELS']
1562929Sktlim@umich.edu#    if len(os_list) == 0:
1572929Sktlim@umich.edu#        raise RuntimeError, "No OS specified"
1582929Sktlim@umich.edu#    else:
1592929Sktlim@umich.edu#        for test_os in os_list:
1602929Sktlim@umich.edu#            build_cpu_test(env, category, test_os, cpu_list, refdir, timeout)
1612929Sktlim@umich.edu    # Loop through CPU models and generate proper options, ref directories for each
1622929Sktlim@umich.edu    for cpu in cpu_list:
1632929Sktlim@umich.edu        test_os = ''
1642929Sktlim@umich.edu        if cpu == "AtomicSimpleCPU":
1652929Sktlim@umich.edu            cpu_option = ('','atomic/')
1662929Sktlim@umich.edu        elif cpu == "TimingSimpleCPU":
1672929Sktlim@umich.edu            cpu_option = ('--timing','timing/')
1682929Sktlim@umich.edu        elif cpu == "O3CPU":
1692929Sktlim@umich.edu            cpu_option = ('--detailed','detailed/')
1702929Sktlim@umich.edu        else:
1712929Sktlim@umich.edu            raise TypeError, "Unknown CPU model specified"
1722929Sktlim@umich.edu
1732929Sktlim@umich.edu        if default_refdir:
1742929Sktlim@umich.edu            # Reference stats located in ref/arch/os/cpu or ref/arch/cpu if no OS specified
1752929Sktlim@umich.edu            test_refdir = os.path.join(refdir, env['TARGET_ISA'])
1762929Sktlim@umich.edu            if test_os != '':
1772929Sktlim@umich.edu                test_refdir = os.path.join(test_refdir, test_os)
1782929Sktlim@umich.edu            cpu_refdir = os.path.join(test_refdir, cpu_option[1])
1792929Sktlim@umich.edu
1802929Sktlim@umich.edu        ref_stats = os.path.join(cpu_refdir, 'm5stats.txt')
1812929Sktlim@umich.edu
1822929Sktlim@umich.edu        # base command for running test
1832929Sktlim@umich.edu        base_cmd = '${SOURCES[0]} -d $TARGET.dir ${SOURCES[1]}'
1842929Sktlim@umich.edu        base_cmd = base_cmd + ' ' + cpu_option[0]
1852929Sktlim@umich.edu        # stdout and stderr files
1862929Sktlim@umich.edu        cmd_stdout = '${TARGETS[0]}'
1872929Sktlim@umich.edu        cmd_stderr = '${TARGETS[1]}'
1882929Sktlim@umich.edu
1892929Sktlim@umich.edu        stdout_string = cpu_option[1] + 'stdout'
1902929Sktlim@umich.edu        stderr_string = cpu_option[1] + 'stderr'
1912929Sktlim@umich.edu        m5stats_string = cpu_option[1] + 'm5stats.txt'
1922929Sktlim@umich.edu        outdiff_string =  cpu_option[1] + 'outdiff'
1932929Sktlim@umich.edu        statsdiff_string = cpu_option[1] + 'statsdiff'
1942929Sktlim@umich.edu        status_string = cpu_option[1] + 'status'
1952929Sktlim@umich.edu
1962929Sktlim@umich.edu        # Prefix test run with batch job submission command if appropriate.
1972929Sktlim@umich.edu        # Output redirection is also different for batch runs.
1982929Sktlim@umich.edu        # Batch command also supports timeout arg (in seconds, not minutes).
1992929Sktlim@umich.edu        if env['BATCH']:
2002929Sktlim@umich.edu            cmd = [env['BATCH_CMD'], '-t', str(timeout * 60),
2012929Sktlim@umich.edu                   '-o', cmd_stdout, '-e', cmd_stderr, base_cmd]
2022929Sktlim@umich.edu        else:
2032929Sktlim@umich.edu            cmd = [base_cmd, '>', cmd_stdout, '2>', cmd_stderr]
2042929Sktlim@umich.edu            
2052929Sktlim@umich.edu        env.Command([stdout_string, stderr_string, m5stats_string], [env.M5Binary, 'run.py'],
2062929Sktlim@umich.edu                    ' '.join(cmd))
2072929Sktlim@umich.edu
2082929Sktlim@umich.edu        # order of targets is important... see check_test
2092929Sktlim@umich.edu        env.Command([outdiff_string, statsdiff_string, status_string],
2102929Sktlim@umich.edu                    [ref_stats, m5stats_string],
2112929Sktlim@umich.edu                    testAction)
2122929Sktlim@umich.edu
2132929Sktlim@umich.edu        # phony target to echo status
2142929Sktlim@umich.edu        if env['update_ref']:
2152929Sktlim@umich.edu            p = env.Command(cpu_option[1] + '_update', [ref_stats, m5stats_string, status_string],
2162929Sktlim@umich.edu                            updateAction)
2172929Sktlim@umich.edu        else:
2182929Sktlim@umich.edu            p = env.Command(cpu_option[1] + '_print', [status_string], printAction)
2192929Sktlim@umich.edu        env.AlwaysBuild(p)
2202929Sktlim@umich.edu
2212929Sktlim@umich.edu        env.Tests.setdefault(category, [])
2222929Sktlim@umich.edu        env.Tests[category] += p
2232929Sktlim@umich.edu
2242929Sktlim@umich.edu# Make test_builder a "wrapper" function.  See SCons wiki page at
2252929Sktlim@umich.edu# http://www.scons.org/cgi-bin/wiki/WrapperFunctions.
2262929Sktlim@umich.eduSConsEnvironment.Test = test_builder
2272929Sktlim@umich.edu
2282929Sktlim@umich.educwd = os.getcwd()
2292929Sktlim@umich.eduos.chdir(str(Dir('.').srcdir))
2302929Sktlim@umich.eduscripts = glob.glob('*/SConscript')
2312929Sktlim@umich.eduos.chdir(cwd)
2322929Sktlim@umich.edu
2332929Sktlim@umich.edufor s in scripts:
2342929Sktlim@umich.edu    SConscript(s, exports = 'env', duplicate = False)
2352929Sktlim@umich.edu
2362929Sktlim@umich.edu# Set up phony commands for various test categories
2372929Sktlim@umich.eduallTests = []
2382929Sktlim@umich.edufor (key, val) in env.Tests.iteritems():
2392929Sktlim@umich.edu    env.Command(key, val, env.NoAction)
2402929Sktlim@umich.edu    allTests += val
2412929Sktlim@umich.edu
2422929Sktlim@umich.edu# The 'all' target is redundant since just specifying the test
2432929Sktlim@umich.edu# directory name (e.g., ALPHA_SE/test/opt) has the same effect.
2442929Sktlim@umich.eduenv.Command('all', allTests, env.NoAction)
245