SConscript revision 2953
112276Sanouk.vanlaer@arm.com# -*- mode:python -*-
28839Sandreas.hansson@arm.com
38839Sandreas.hansson@arm.com# Copyright (c) 2004-2006 The Regents of The University of Michigan
48839Sandreas.hansson@arm.com# All rights reserved.
58839Sandreas.hansson@arm.com#
68839Sandreas.hansson@arm.com# Redistribution and use in source and binary forms, with or without
78839Sandreas.hansson@arm.com# modification, are permitted provided that the following conditions are
88839Sandreas.hansson@arm.com# met: redistributions of source code must retain the above copyright
98839Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer;
108839Sandreas.hansson@arm.com# redistributions in binary form must reproduce the above copyright
118839Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer in the
128839Sandreas.hansson@arm.com# documentation and/or other materials provided with the distribution;
135335Shines@cs.fsu.edu# neither the name of the copyright holders nor the names of its
147897Shestness@cs.utexas.edu# contributors may be used to endorse or promote products derived from
154486Sbinkertn@umich.edu# this software without specific prior written permission.
164486Sbinkertn@umich.edu#
174486Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
184486Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
194486Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
204486Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
214486Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
224486Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
234486Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244486Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254486Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264486Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
274486Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284486Sbinkertn@umich.edu#
294486Sbinkertn@umich.edu# Authors: Steve Reinhardt
304486Sbinkertn@umich.edu#          Kevin Lim
314486Sbinkertn@umich.edu
324486Sbinkertn@umich.eduimport os
334486Sbinkertn@umich.eduimport sys
344486Sbinkertn@umich.eduimport glob
354486Sbinkertn@umich.edufrom SCons.Script.SConscript import SConsEnvironment
364486Sbinkertn@umich.edu
374486Sbinkertn@umich.eduImport('env')
384486Sbinkertn@umich.edu
394486Sbinkertn@umich.eduenv['DIFFOUT'] = File('diff-out')
404486Sbinkertn@umich.edu
417897Shestness@cs.utexas.edu# Dict that accumulates lists of tests by category (quick, medium, long)
428839Sandreas.hansson@arm.comenv.Tests = {}
434486Sbinkertn@umich.edu
446654Snate@binkert.orgdef contents(node):
456654Snate@binkert.org    return file(str(node)).read()
4611988Sandreas.sandberg@arm.com
476654Snate@binkert.orgdef check_test(target, source, env):
483102SN/A    """Check output from running test.
493102SN/A
506654Snate@binkert.org    Targets are as follows:
5110720Sandreas.hansson@arm.com    target[0] : outdiff
524776Sgblack@eecs.umich.edu    target[1] : statsdiff
5310663SAli.Saidi@ARM.com    target[2] : status
546654Snate@binkert.org
559793Sakash.bagdia@arm.com    """
562667SN/A    # make sure target files are all gone
574776Sgblack@eecs.umich.edu    for t in target:
584776Sgblack@eecs.umich.edu        if os.path.exists(t.abspath):
596654Snate@binkert.org            Execute(Delete(t.abspath))
606023Snate@binkert.org    # Run diff on output & ref directories to find differences.
618745Sgblack@eecs.umich.edu    # Exclude m5stats.txt since we will use diff-out on that.
629384SAndreas.Sandberg@arm.com    Execute(env.subst('diff -ubr ${SOURCES[0].dir} ${SOURCES[1].dir} ' +
639384SAndreas.Sandberg@arm.com                      '-I "^command line:" ' +		# for stdout file
646654Snate@binkert.org                      '-I "^M5 compiled on" ' +		# for stderr file
656022Sgblack@eecs.umich.edu                      '-I "^M5 simulation started" ' +	# for stderr file
668745Sgblack@eecs.umich.edu                      '-I "^Simulation complete at" ' +	# for stderr file
679384SAndreas.Sandberg@arm.com                      '-I "^Listening for" ' +		# for stderr file
689384SAndreas.Sandberg@arm.com                      '--exclude=m5stats.txt --exclude=SCCS ' +
696654Snate@binkert.org                      '--exclude=${TARGETS[0].file} ' +
706022Sgblack@eecs.umich.edu                      '> ${TARGETS[0]}', target=target, source=source), None)
718745Sgblack@eecs.umich.edu    print "===== Output differences ====="
729384SAndreas.Sandberg@arm.com    print contents(target[0])
739384SAndreas.Sandberg@arm.com    # Run diff-out on m5stats.txt file
746654Snate@binkert.org    status = Execute(env.subst('$DIFFOUT $SOURCES > ${TARGETS[1]}',
756022Sgblack@eecs.umich.edu                               target=target, source=source),
768745Sgblack@eecs.umich.edu                     strfunction=None)
779384SAndreas.Sandberg@arm.com    print "===== Statistics differences ====="
789384SAndreas.Sandberg@arm.com    print contents(target[1])
796654Snate@binkert.org    # Generate status file contents based on exit status of diff-out
8010037SARM gem5 Developers    if status == 0:
818745Sgblack@eecs.umich.edu        status_str = "passed."
829384SAndreas.Sandberg@arm.com    else:
839384SAndreas.Sandberg@arm.com        status_str = "FAILED!"
846691Stjones1@inf.ed.ac.uk    f = file(str(target[2]), 'w')
856691Stjones1@inf.ed.ac.uk    print >>f, env.subst('${TARGETS[2].dir}', target=target, source=source), \
868745Sgblack@eecs.umich.edu          status_str
879384SAndreas.Sandberg@arm.com    f.close()
889384SAndreas.Sandberg@arm.com    # done
8911723Sar4jc@virginia.edu    return 0
9011723Sar4jc@virginia.edu
9111723Sar4jc@virginia.edudef check_test_string(target, source, env):
9211723Sar4jc@virginia.edu    return env.subst("Comparing outputs in ${TARGETS[0].dir}.",
9311723Sar4jc@virginia.edu                     target=target, source=source)
944486Sbinkertn@umich.edu
955529Snate@binkert.orgtestAction = env.Action(check_test, check_test_string)
961366SN/A
971310SN/Adef print_test(target, source, env):
989338SAndreas.Sandberg@arm.com    print '***** ' + contents(source[0])
999254SAndreas.Sandberg@arm.com    return 0
10011988Sandreas.sandberg@arm.com
10111988Sandreas.sandberg@arm.comprintAction = env.Action(print_test, strfunction = None)
10211988Sandreas.sandberg@arm.com
10311988Sandreas.sandberg@arm.comdef update_test(target, source, env):
10411988Sandreas.sandberg@arm.com    """Update reference test outputs.
10511988Sandreas.sandberg@arm.com
10611988Sandreas.sandberg@arm.com    Target is phony.  First two sources are the ref & new m5stats.txt
10711988Sandreas.sandberg@arm.com    files, respectively.  We actually copy everything in the
10811988Sandreas.sandberg@arm.com    respective directories except the status & diff output files.
10911988Sandreas.sandberg@arm.com
1109254SAndreas.Sandberg@arm.com    """
1119518SAndreas.Sandberg@ARM.com    dest_dir = str(source[0].get_dir())
1129518SAndreas.Sandberg@ARM.com    src_dir = str(source[1].get_dir())
1139518SAndreas.Sandberg@ARM.com    dest_files = os.listdir(dest_dir)
1149518SAndreas.Sandberg@ARM.com    src_files = os.listdir(src_dir)
1159518SAndreas.Sandberg@ARM.com    # Exclude status & diff outputs
1169518SAndreas.Sandberg@ARM.com    for f in ('outdiff', 'statsdiff', 'status'):
1179518SAndreas.Sandberg@ARM.com        if f in src_files:
1189518SAndreas.Sandberg@ARM.com            src_files.remove(f)
1199518SAndreas.Sandberg@ARM.com    for f in src_files:
1209518SAndreas.Sandberg@ARM.com        if f in dest_files:
1219518SAndreas.Sandberg@ARM.com            print "  Replacing file", f
1229518SAndreas.Sandberg@ARM.com            dest_files.remove(f)
1239518SAndreas.Sandberg@ARM.com        else:
1249518SAndreas.Sandberg@ARM.com            print "  Creating new file", f
1259518SAndreas.Sandberg@ARM.com        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
1269518SAndreas.Sandberg@ARM.com        copyAction.strfunction = None
1279518SAndreas.Sandberg@ARM.com        Execute(copyAction)
1289518SAndreas.Sandberg@ARM.com    # warn about any files in dest not overwritten (other than SCCS dir)
1299518SAndreas.Sandberg@ARM.com    if 'SCCS' in dest_files:
1309254SAndreas.Sandberg@arm.com        dest_files.remove('SCCS')
1319254SAndreas.Sandberg@arm.com    if dest_files:
1329254SAndreas.Sandberg@arm.com        print "Warning: file(s) in", dest_dir, "not updated:",
1339254SAndreas.Sandberg@arm.com        print ', '.join(dest_files)
1342901SN/A    return 0
1355712Shsul@eecs.umich.edu
13610190Sakash.bagdia@arm.comdef update_test_string(target, source, env):
1375529Snate@binkert.org    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
13812276Sanouk.vanlaer@arm.com                     target=target, source=source)
13912276Sanouk.vanlaer@arm.com
1405529Snate@binkert.orgupdateAction = env.Action(update_test, update_test_string)
14112277Sjose.marinho@arm.com
14212277Sjose.marinho@arm.comdef test_builder(env, category, cpu_list=[], os_list=[], refdir='ref',
14312277Sjose.marinho@arm.com                 timeout=15):
14412277Sjose.marinho@arm.com    """Define a test.
1455529Snate@binkert.org
1469161Sandreas.hansson@arm.com    Args:
1475529Snate@binkert.org    category -- string describing test category (e.g., 'quick')
1485821Ssaidi@eecs.umich.edu    cpu_list -- list of CPUs to runs this test on (blank means all compiled CPUs)
1493170SN/A    os_list -- list of OSs to run this test on
15011877Sbrandon.potter@amd.com    refdir -- subdirectory containing reference output (default 'ref')
15111877Sbrandon.potter@amd.com    timeout -- test timeout in minutes (only enforced on pool)
1525780Ssteve.reinhardt@amd.com
1535780Ssteve.reinhardt@amd.com    """
1545780Ssteve.reinhardt@amd.com
1555780Ssteve.reinhardt@amd.com    default_refdir = False
1565780Ssteve.reinhardt@amd.com    if refdir == 'ref':
1578784Sgblack@eecs.umich.edu        default_refdir = True
1588784Sgblack@eecs.umich.edu    valid_cpu_list = []
1598784Sgblack@eecs.umich.edu    if len(cpu_list) == 0:
16012122Sjose.marinho@arm.com        valid_cpu_list = env['CPU_MODELS']
16112122Sjose.marinho@arm.com    else:
16212122Sjose.marinho@arm.com        for i in cpu_list:
1638793Sgblack@eecs.umich.edu            if i in env['CPU_MODELS']:
1641310SN/A                valid_cpu_list.append(i)
1656654Snate@binkert.org    cpu_list = valid_cpu_list
1666022Sgblack@eecs.umich.edu    if env['TEST_CPU_MODELS']:
1676022Sgblack@eecs.umich.edu        valid_cpu_list = []
16811150Smitch.hayenga@arm.com        for i in env['TEST_CPU_MODELS']:
16911150Smitch.hayenga@arm.com            if i in cpu_list:
1709384SAndreas.Sandberg@arm.com                valid_cpu_list.append(i)
1716654Snate@binkert.org        cpu_list = valid_cpu_list
1726023Snate@binkert.org# Code commented out that shows the general structure if we want to test
1736023Snate@binkert.org# different OS's as well.
17411150Smitch.hayenga@arm.com#    if len(os_list) == 0:
17511150Smitch.hayenga@arm.com#        for test_cpu in cpu_list:
1769384SAndreas.Sandberg@arm.com#            build_cpu_test(env, category, '', test_cpu, refdir, timeout)
1776654Snate@binkert.org#    else:
1786022Sgblack@eecs.umich.edu#        for test_os in os_list:
1796022Sgblack@eecs.umich.edu#            for test_cpu in cpu_list:
18011150Smitch.hayenga@arm.com#                build_cpu_test(env, category, test_os, test_cpu, refdir,
1819384SAndreas.Sandberg@arm.com#                               timeout)
1826654Snate@binkert.org    # Loop through CPU models and generate proper options, ref directories
1836022Sgblack@eecs.umich.edu    for cpu in cpu_list:
1846022Sgblack@eecs.umich.edu        test_os = ''
18511150Smitch.hayenga@arm.com        if cpu == "AtomicSimpleCPU":
18611150Smitch.hayenga@arm.com            cpu_option = ('','atomic/')
1879384SAndreas.Sandberg@arm.com        elif cpu == "TimingSimpleCPU":
1886654Snate@binkert.org            cpu_option = ('--timing','timing/')
1896116Snate@binkert.org        elif cpu == "O3CPU":
1906116Snate@binkert.org            cpu_option = ('--detailed','detailed/')
19110037SARM gem5 Developers        else:
19210037SARM gem5 Developers            raise TypeError, "Unknown CPU model specified"
19311150Smitch.hayenga@arm.com
19411150Smitch.hayenga@arm.com        if default_refdir:
1959384SAndreas.Sandberg@arm.com            # Reference stats located in ref/arch/os/cpu or ref/arch/cpu
1966691Stjones1@inf.ed.ac.uk            # if no OS specified
1976691Stjones1@inf.ed.ac.uk            test_refdir = os.path.join(refdir, env['TARGET_ISA'])
1986691Stjones1@inf.ed.ac.uk            if test_os != '':
1996691Stjones1@inf.ed.ac.uk                test_refdir = os.path.join(test_refdir, test_os)
20011150Smitch.hayenga@arm.com            cpu_refdir = os.path.join(test_refdir, cpu_option[1])
20111150Smitch.hayenga@arm.com
2029384SAndreas.Sandberg@arm.com        ref_stats = os.path.join(cpu_refdir, 'm5stats.txt')
20311723Sar4jc@virginia.edu
20411723Sar4jc@virginia.edu        # base command for running test
20511723Sar4jc@virginia.edu        base_cmd = '${SOURCES[0]} -d $TARGET.dir ${SOURCES[1]}'
20611723Sar4jc@virginia.edu        base_cmd = base_cmd + ' ' + cpu_option[0]
20711723Sar4jc@virginia.edu        # stdout and stderr files
20811723Sar4jc@virginia.edu        cmd_stdout = '${TARGETS[0]}'
2094997Sgblack@eecs.umich.edu        cmd_stderr = '${TARGETS[1]}'
2104997Sgblack@eecs.umich.edu
2116654Snate@binkert.org        stdout_string = cpu_option[1] + 'stdout'
2124997Sgblack@eecs.umich.edu        stderr_string = cpu_option[1] + 'stderr'
2134997Sgblack@eecs.umich.edu        m5stats_string = cpu_option[1] + 'm5stats.txt'
2141310SN/A        outdiff_string =  cpu_option[1] + 'outdiff'
2151310SN/A        statsdiff_string = cpu_option[1] + 'statsdiff'
2161310SN/A        status_string = cpu_option[1] + 'status'
2171310SN/A
2189647Sdam.sunwoo@arm.com        # Prefix test run with batch job submission command if appropriate.
2199647Sdam.sunwoo@arm.com        # Output redirection is also different for batch runs.
2201310SN/A        # Batch command also supports timeout arg (in seconds, not minutes).
2211310SN/A        if env['BATCH']:
2221310SN/A            cmd = [env['BATCH_CMD'], '-t', str(timeout * 60),
2231310SN/A                   '-o', cmd_stdout, '-e', cmd_stderr, base_cmd]
2249180Sandreas.hansson@arm.com        else:
2259180Sandreas.hansson@arm.com            cmd = [base_cmd, '>', cmd_stdout, '2>', cmd_stderr]
2261310SN/A            
2279433SAndreas.Sandberg@ARM.com        env.Command([stdout_string, stderr_string, m5stats_string],
2289433SAndreas.Sandberg@ARM.com                    [env.M5Binary, 'run.py'], ' '.join(cmd))
2299433SAndreas.Sandberg@ARM.com
2301634SN/A        # order of targets is important... see check_test
2314776Sgblack@eecs.umich.edu        env.Command([outdiff_string, statsdiff_string, status_string],
2324776Sgblack@eecs.umich.edu                    [ref_stats, m5stats_string],
2338839Sandreas.hansson@arm.com                    testAction)
2348839Sandreas.hansson@arm.com
2358707Sandreas.hansson@arm.com        # phony target to echo status
2368707Sandreas.hansson@arm.com        if env['update_ref']:
2378756Sgblack@eecs.umich.edu            p = env.Command(cpu_option[1] + '_update',
2388707Sandreas.hansson@arm.com                            [ref_stats, m5stats_string, status_string],
2397876Sgblack@eecs.umich.edu                            updateAction)
2408839Sandreas.hansson@arm.com        else:
2418839Sandreas.hansson@arm.com            p = env.Command(cpu_option[1] + '_print', [status_string],
2428745Sgblack@eecs.umich.edu                            printAction)
24311150Smitch.hayenga@arm.com        env.AlwaysBuild(p)
24411150Smitch.hayenga@arm.com
24511150Smitch.hayenga@arm.com        env.Tests.setdefault(category, [])
2462998SN/A        env.Tests[category] += p
2478863Snilay@cs.wisc.edu
2488863Snilay@cs.wisc.edu# Make test_builder a "wrapper" function.  See SCons wiki page at
24911150Smitch.hayenga@arm.com# http://www.scons.org/cgi-bin/wiki/WrapperFunctions.
2508863Snilay@cs.wisc.eduSConsEnvironment.Test = test_builder
25111150Smitch.hayenga@arm.com
2528863Snilay@cs.wisc.educwd = os.getcwd()
2539793Sakash.bagdia@arm.comos.chdir(str(Dir('.').srcdir))
2549793Sakash.bagdia@arm.comscripts = glob.glob('*/SConscript')
2559793Sakash.bagdia@arm.comos.chdir(cwd)
25611150Smitch.hayenga@arm.com
2579544Sandreas.hansson@arm.comfor s in scripts:
25811150Smitch.hayenga@arm.com    SConscript(s, exports = 'env', duplicate = False)
2599544Sandreas.hansson@arm.com
2608863Snilay@cs.wisc.edu# Set up phony commands for various test categories
26111150Smitch.hayenga@arm.comallTests = []
2628863Snilay@cs.wisc.edufor (key, val) in env.Tests.iteritems():
26311150Smitch.hayenga@arm.com    env.Command(key, val, env.NoAction)
2648863Snilay@cs.wisc.edu    allTests += val
26511150Smitch.hayenga@arm.com
26611723Sar4jc@virginia.edu# The 'all' target is redundant since just specifying the test
26711723Sar4jc@virginia.edu# directory name (e.g., ALPHA_SE/test/opt) has the same effect.
26811723Sar4jc@virginia.eduenv.Command('all', allTests, env.NoAction)
2698863Snilay@cs.wisc.edu