SConscript revision 2932
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2006 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
294762Snate@binkert.org# Authors: Steve Reinhardt
30955SN/A#          Kevin Lim
3112563Sgabeblack@google.com
3212563Sgabeblack@google.comimport os
335522Snate@binkert.orgimport sys
346143Snate@binkert.orgimport glob
3512371Sgabeblack@google.comfrom SCons.Script.SConscript import SConsEnvironment
364762Snate@binkert.org
375522Snate@binkert.orgImport('env')
38955SN/A
395522Snate@binkert.orgenv['DIFFOUT'] = File('diff-out')
4011974Sgabeblack@google.com
41955SN/A# Dict that accumulates lists of tests by category (quick, medium, long)
425522Snate@binkert.orgenv.Tests = {}
434202Sbinkertn@umich.edu
445742Snate@binkert.orgdef contents(node):
45955SN/A    return file(str(node)).read()
464381Sbinkertn@umich.edu
474381Sbinkertn@umich.edudef check_test(target, source, env):
4812246Sgabeblack@google.com    """Check output from running test.
4912246Sgabeblack@google.com
508334Snate@binkert.org    Targets are as follows:
51955SN/A    target[0] : outdiff
52955SN/A    target[1] : statsdiff
534202Sbinkertn@umich.edu    target[2] : status
54955SN/A
554382Sbinkertn@umich.edu    """
564382Sbinkertn@umich.edu    # make sure target files are all gone
574382Sbinkertn@umich.edu    for t in target:
586654Snate@binkert.org        if os.path.exists(t.abspath):
595517Snate@binkert.org            Execute(Delete(t.abspath))
608614Sgblack@eecs.umich.edu    # Run diff on output & ref directories to find differences.
617674Snate@binkert.org    # Exclude m5stats.txt since we will use diff-out on that.
626143Snate@binkert.org    Execute(env.subst('diff -ubr ${SOURCES[0].dir} ${SOURCES[1].dir} ' +
636143Snate@binkert.org                      '-I "^command line:" ' +		# for stdout file
646143Snate@binkert.org                      '-I "^M5 compiled on" ' +		# for stderr file
6512302Sgabeblack@google.com                      '-I "^M5 simulation started" ' +	# for stderr file
6612302Sgabeblack@google.com                      '-I "^Simulation complete at" ' +	# for stderr file
6712302Sgabeblack@google.com                      '-I "^Listening for" ' +		# for stderr file
6812371Sgabeblack@google.com                      '--exclude=m5stats.txt --exclude=SCCS ' +
6912371Sgabeblack@google.com                      '--exclude=${TARGETS[0].file} ' +
7012371Sgabeblack@google.com                      '> ${TARGETS[0]}', target=target, source=source), None)
7112371Sgabeblack@google.com    print "===== Output differences ====="
7212371Sgabeblack@google.com    print contents(target[0])
7312371Sgabeblack@google.com    # Run diff-out on m5stats.txt file
7412371Sgabeblack@google.com    status = Execute(env.subst('$DIFFOUT $SOURCES > ${TARGETS[1]}',
7512371Sgabeblack@google.com                               target=target, source=source),
7612371Sgabeblack@google.com                     strfunction=None)
7712371Sgabeblack@google.com    print "===== Statistics differences ====="
7812371Sgabeblack@google.com    print contents(target[1])
7912371Sgabeblack@google.com    # Generate status file contents based on exit status of diff-out
8012371Sgabeblack@google.com    if status == 0:
8112371Sgabeblack@google.com        status_str = "passed."
8212371Sgabeblack@google.com    else:
8312371Sgabeblack@google.com        status_str = "FAILED!"
8412371Sgabeblack@google.com    f = file(str(target[2]), 'w')
8512371Sgabeblack@google.com    print >>f, env.subst('${TARGETS[2].dir}', target=target, source=source), \
8612371Sgabeblack@google.com          status_str
8712371Sgabeblack@google.com    f.close()
8812371Sgabeblack@google.com    # done
8912371Sgabeblack@google.com    return 0
9012371Sgabeblack@google.com
9112371Sgabeblack@google.comdef check_test_string(target, source, env):
9212371Sgabeblack@google.com    return env.subst("Comparing outputs in ${TARGETS[0].dir}.",
9312371Sgabeblack@google.com                     target=target, source=source)
9412371Sgabeblack@google.com
9512371Sgabeblack@google.comtestAction = env.Action(check_test, check_test_string)
9612371Sgabeblack@google.com
9712371Sgabeblack@google.comdef print_test(target, source, env):
9812371Sgabeblack@google.com    print '***** ' + contents(source[0])
9912371Sgabeblack@google.com    return 0
10012371Sgabeblack@google.com
10112371Sgabeblack@google.comprintAction = env.Action(print_test, strfunction = None)
10212371Sgabeblack@google.com
10312371Sgabeblack@google.comdef update_test(target, source, env):
10412371Sgabeblack@google.com    """Update reference test outputs.
10512371Sgabeblack@google.com
10612371Sgabeblack@google.com    Target is phony.  First two sources are the ref & new m5stats.txt
10712371Sgabeblack@google.com    files, respectively.  We actually copy everything in the
10812371Sgabeblack@google.com    respective directories except the status & diff output files.
10912371Sgabeblack@google.com
11012371Sgabeblack@google.com    """
11112371Sgabeblack@google.com    dest_dir = str(source[0].get_dir())
11212371Sgabeblack@google.com    src_dir = str(source[1].get_dir())
11312371Sgabeblack@google.com    dest_files = os.listdir(dest_dir)
11412371Sgabeblack@google.com    src_files = os.listdir(src_dir)
11512302Sgabeblack@google.com    # Exclude status & diff outputs
11612371Sgabeblack@google.com    for f in ('outdiff', 'statsdiff', 'status'):
11712302Sgabeblack@google.com        if f in src_files:
11812371Sgabeblack@google.com            src_files.remove(f)
11912302Sgabeblack@google.com    for f in src_files:
12012302Sgabeblack@google.com        if f in dest_files:
12112371Sgabeblack@google.com            print "  Replacing file", f
12212371Sgabeblack@google.com            dest_files.remove(f)
12312371Sgabeblack@google.com        else:
12412371Sgabeblack@google.com            print "  Creating new file", f
12512302Sgabeblack@google.com        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
12612371Sgabeblack@google.com        copyAction.strfunction = None
12712371Sgabeblack@google.com        Execute(copyAction)
12812371Sgabeblack@google.com    # warn about any files in dest not overwritten (other than SCCS dir)
12912371Sgabeblack@google.com    if 'SCCS' in dest_files:
13011983Sgabeblack@google.com        dest_files.remove('SCCS')
1316143Snate@binkert.org    if dest_files:
1328233Snate@binkert.org        print "Warning: file(s) in", dest_dir, "not updated:",
13312302Sgabeblack@google.com        print ', '.join(dest_files)
1346143Snate@binkert.org    return 0
1356143Snate@binkert.org
13612302Sgabeblack@google.comdef update_test_string(target, source, env):
1374762Snate@binkert.org    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
1386143Snate@binkert.org                     target=target, source=source)
1398233Snate@binkert.org
1408233Snate@binkert.orgupdateAction = env.Action(update_test, update_test_string)
14112302Sgabeblack@google.com
14212302Sgabeblack@google.comdef test_builder(env, category, cpu_list=[], os_list=[], refdir='ref',
1436143Snate@binkert.org                 timeout=15):
14412362Sgabeblack@google.com    """Define a test.
14512362Sgabeblack@google.com
14612362Sgabeblack@google.com    Args:
14712362Sgabeblack@google.com    category -- string describing test category (e.g., 'quick')
14812302Sgabeblack@google.com    cpu_list -- list of CPUs to runs this test on (blank means all compiled CPUs)
14912302Sgabeblack@google.com    os_list -- list of OSs to run this test on
15012302Sgabeblack@google.com    refdir -- subdirectory containing reference output (default 'ref')
15112302Sgabeblack@google.com    timeout -- test timeout in minutes (only enforced on pool)
15212302Sgabeblack@google.com
15312363Sgabeblack@google.com    """
15412363Sgabeblack@google.com
15512363Sgabeblack@google.com    default_refdir = False
15612363Sgabeblack@google.com    if refdir == 'ref':
15712302Sgabeblack@google.com        default_refdir = True
15812363Sgabeblack@google.com    if len(cpu_list) == 0:
15912363Sgabeblack@google.com        cpu_list = env['CPU_MODELS']
16012363Sgabeblack@google.com    if env['TEST_CPU_MODELS']:
16112363Sgabeblack@google.com        temp_cpu_list = []
16212363Sgabeblack@google.com        for i in env['TEST_CPU_MODELS']:
1638233Snate@binkert.org            if i in cpu_list:
1646143Snate@binkert.org                temp_cpu_list.append(i)
1656143Snate@binkert.org        cpu_list = temp_cpu_list
1666143Snate@binkert.org# Code commented out that shows the general structure if we want to test
1676143Snate@binkert.org# different OS's as well.
1686143Snate@binkert.org#    if len(os_list) == 0:
1696143Snate@binkert.org#        for test_cpu in cpu_list:
1706143Snate@binkert.org#            build_cpu_test(env, category, '', test_cpu, refdir, timeout)
1716143Snate@binkert.org#    else:
1726143Snate@binkert.org#        for test_os in os_list:
1737065Snate@binkert.org#            for test_cpu in cpu_list:
1746143Snate@binkert.org#                build_cpu_test(env, category, test_os, test_cpu, refdir,
17512362Sgabeblack@google.com#                               timeout)
17612362Sgabeblack@google.com    # Loop through CPU models and generate proper options, ref directories
17712362Sgabeblack@google.com    for cpu in cpu_list:
17812362Sgabeblack@google.com        test_os = ''
17912362Sgabeblack@google.com        if cpu == "AtomicSimpleCPU":
18012362Sgabeblack@google.com            cpu_option = ('','atomic/')
18112362Sgabeblack@google.com        elif cpu == "TimingSimpleCPU":
18212362Sgabeblack@google.com            cpu_option = ('--timing','timing/')
18312362Sgabeblack@google.com        elif cpu == "O3CPU":
18412362Sgabeblack@google.com            cpu_option = ('--detailed','detailed/')
18512362Sgabeblack@google.com        else:
18612362Sgabeblack@google.com            raise TypeError, "Unknown CPU model specified"
1878233Snate@binkert.org
1888233Snate@binkert.org        if default_refdir:
1898233Snate@binkert.org            # Reference stats located in ref/arch/os/cpu or ref/arch/cpu
1908233Snate@binkert.org            # if no OS specified
1918233Snate@binkert.org            test_refdir = os.path.join(refdir, env['TARGET_ISA'])
1928233Snate@binkert.org            if test_os != '':
1938233Snate@binkert.org                test_refdir = os.path.join(test_refdir, test_os)
1948233Snate@binkert.org            cpu_refdir = os.path.join(test_refdir, cpu_option[1])
1958233Snate@binkert.org
1968233Snate@binkert.org        ref_stats = os.path.join(cpu_refdir, 'm5stats.txt')
1978233Snate@binkert.org
1988233Snate@binkert.org        # base command for running test
1998233Snate@binkert.org        base_cmd = '${SOURCES[0]} -d $TARGET.dir ${SOURCES[1]}'
2008233Snate@binkert.org        base_cmd = base_cmd + ' ' + cpu_option[0]
2018233Snate@binkert.org        # stdout and stderr files
2028233Snate@binkert.org        cmd_stdout = '${TARGETS[0]}'
2038233Snate@binkert.org        cmd_stderr = '${TARGETS[1]}'
2048233Snate@binkert.org
2058233Snate@binkert.org        stdout_string = cpu_option[1] + 'stdout'
2068233Snate@binkert.org        stderr_string = cpu_option[1] + 'stderr'
2078233Snate@binkert.org        m5stats_string = cpu_option[1] + 'm5stats.txt'
2086143Snate@binkert.org        outdiff_string =  cpu_option[1] + 'outdiff'
2096143Snate@binkert.org        statsdiff_string = cpu_option[1] + 'statsdiff'
2106143Snate@binkert.org        status_string = cpu_option[1] + 'status'
2116143Snate@binkert.org
2126143Snate@binkert.org        # Prefix test run with batch job submission command if appropriate.
2136143Snate@binkert.org        # Output redirection is also different for batch runs.
2149982Satgutier@umich.edu        # Batch command also supports timeout arg (in seconds, not minutes).
2156143Snate@binkert.org        if env['BATCH']:
21612302Sgabeblack@google.com            cmd = [env['BATCH_CMD'], '-t', str(timeout * 60),
21712302Sgabeblack@google.com                   '-o', cmd_stdout, '-e', cmd_stderr, base_cmd]
21812302Sgabeblack@google.com        else:
21912302Sgabeblack@google.com            cmd = [base_cmd, '>', cmd_stdout, '2>', cmd_stderr]
22012302Sgabeblack@google.com            
22112302Sgabeblack@google.com        env.Command([stdout_string, stderr_string, m5stats_string],
22212302Sgabeblack@google.com                    [env.M5Binary, 'run.py'], ' '.join(cmd))
22312302Sgabeblack@google.com
22411983Sgabeblack@google.com        # order of targets is important... see check_test
22511983Sgabeblack@google.com        env.Command([outdiff_string, statsdiff_string, status_string],
22611983Sgabeblack@google.com                    [ref_stats, m5stats_string],
22712302Sgabeblack@google.com                    testAction)
22812302Sgabeblack@google.com
22912302Sgabeblack@google.com        # phony target to echo status
23012302Sgabeblack@google.com        if env['update_ref']:
23112302Sgabeblack@google.com            p = env.Command(cpu_option[1] + '_update',
23212302Sgabeblack@google.com                            [ref_stats, m5stats_string, status_string],
23311983Sgabeblack@google.com                            updateAction)
2346143Snate@binkert.org        else:
23512305Sgabeblack@google.com            p = env.Command(cpu_option[1] + '_print', [status_string],
23612302Sgabeblack@google.com                            printAction)
23712302Sgabeblack@google.com        env.AlwaysBuild(p)
23812302Sgabeblack@google.com
2396143Snate@binkert.org        env.Tests.setdefault(category, [])
2406143Snate@binkert.org        env.Tests[category] += p
2416143Snate@binkert.org
2425522Snate@binkert.org# Make test_builder a "wrapper" function.  See SCons wiki page at
2436143Snate@binkert.org# http://www.scons.org/cgi-bin/wiki/WrapperFunctions.
2446143Snate@binkert.orgSConsEnvironment.Test = test_builder
2456143Snate@binkert.org
2469982Satgutier@umich.educwd = os.getcwd()
24712302Sgabeblack@google.comos.chdir(str(Dir('.').srcdir))
24812302Sgabeblack@google.comscripts = glob.glob('*/SConscript')
24912302Sgabeblack@google.comos.chdir(cwd)
2506143Snate@binkert.org
2516143Snate@binkert.orgfor s in scripts:
2526143Snate@binkert.org    SConscript(s, exports = 'env', duplicate = False)
2536143Snate@binkert.org
2545522Snate@binkert.org# Set up phony commands for various test categories
2555522Snate@binkert.orgallTests = []
2565522Snate@binkert.orgfor (key, val) in env.Tests.iteritems():
2575522Snate@binkert.org    env.Command(key, val, env.NoAction)
2585604Snate@binkert.org    allTests += val
2595604Snate@binkert.org
2606143Snate@binkert.org# The 'all' target is redundant since just specifying the test
2616143Snate@binkert.org# directory name (e.g., ALPHA_SE/test/opt) has the same effect.
2624762Snate@binkert.orgenv.Command('all', allTests, env.NoAction)
2634762Snate@binkert.org