SConscript revision 10218
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
315522Snate@binkert.org
326143Snate@binkert.orgimport os, signal
334762Snate@binkert.orgimport sys, time
345522Snate@binkert.orgimport glob
35955SN/Afrom SCons.Script.SConscript import SConsEnvironment
365522Snate@binkert.org
37955SN/AImport('env')
385522Snate@binkert.org
394202Sbinkertn@umich.eduenv['DIFFOUT'] = File('diff-out')
405742Snate@binkert.org
41955SN/A# get the termcap from the environment
424381Sbinkertn@umich.edutermcap = env['TERMCAP']
434381Sbinkertn@umich.edu
44955SN/A# Dict that accumulates lists of tests by category (quick, medium, long)
45955SN/Aenv.Tests = {}
46955SN/A
474202Sbinkertn@umich.edudef contents(node):
48955SN/A    return file(str(node)).read()
494382Sbinkertn@umich.edu
504382Sbinkertn@umich.edu# functions to parse return value from scons Execute()... not the same
514382Sbinkertn@umich.edu# as wait() etc., so python built-in os funcs don't work.
526108Snate@binkert.orgdef signaled(status):
535517Snate@binkert.org    return (status & 0x80) != 0;
546143Snate@binkert.org
556143Snate@binkert.orgdef signum(status):
566143Snate@binkert.org    return (status & 0x7f);
576143Snate@binkert.org
586143Snate@binkert.org# List of signals that indicate that we should retry the test rather
596143Snate@binkert.org# than consider it failed.
606143Snate@binkert.orgretry_signals = (signal.SIGTERM, signal.SIGKILL, signal.SIGINT,
616143Snate@binkert.org                 signal.SIGQUIT, signal.SIGHUP)
626143Snate@binkert.org
636143Snate@binkert.org# regular expressions of lines to ignore when diffing outputs
646143Snate@binkert.orgoutput_ignore_regexes = (
656143Snate@binkert.org    '^command line:',		# for stdout file
666143Snate@binkert.org    '^gem5 compiled ',		# for stderr file
676143Snate@binkert.org    '^gem5 started ',		# for stderr file
686143Snate@binkert.org    '^gem5 executing on ',	# for stderr file
694762Snate@binkert.org    '^Simulation complete at',	# for stderr file
706143Snate@binkert.org    '^Listening for',		# for stderr file
716143Snate@binkert.org    'listening for remote gdb',	# for stderr file
726143Snate@binkert.org    )
736143Snate@binkert.org
746143Snate@binkert.orgoutput_ignore_args = ' '.join(["-I '"+s+"'" for s in output_ignore_regexes])
756143Snate@binkert.org
766143Snate@binkert.orgoutput_ignore_args += ' --exclude=stats.txt --exclude=outdiff'
776143Snate@binkert.org
786143Snate@binkert.orgdef run_test(target, source, env):
796143Snate@binkert.org    """Check output from running test.
806143Snate@binkert.org
816143Snate@binkert.org    Targets are as follows:
826143Snate@binkert.org    target[0] : status
836143Snate@binkert.org
846143Snate@binkert.org    Sources are:
856143Snate@binkert.org    source[0] : gem5 binary
866143Snate@binkert.org    source[1] : tests/run.py script
876143Snate@binkert.org    source[2] : reference stats file
886143Snate@binkert.org
896143Snate@binkert.org    """
906143Snate@binkert.org    # make sure target files are all gone
916143Snate@binkert.org    for t in target:
926143Snate@binkert.org        if os.path.exists(t.abspath):
936143Snate@binkert.org            env.Execute(Delete(t.abspath))
946143Snate@binkert.org
956143Snate@binkert.org    tgt_dir = os.path.dirname(str(target[0]))
966143Snate@binkert.org
976143Snate@binkert.org    # Base command for running test.  We mess around with indirectly
986143Snate@binkert.org    # referring to files via SOURCES and TARGETS so that scons can mess
996143Snate@binkert.org    # with paths all it wants to and we still get the right files.
1006143Snate@binkert.org    cmd = '${SOURCES[0]} -d %s -re ${SOURCES[1]} %s' % (tgt_dir, tgt_dir)
1016143Snate@binkert.org
1026143Snate@binkert.org    # Prefix test run with batch job submission command if appropriate.
1036143Snate@binkert.org    # Batch command also supports timeout arg (in seconds, not minutes).
1046143Snate@binkert.org    timeout = 15 * 60 # used to be a param, probably should be again
1056143Snate@binkert.org    if env['BATCH']:
1066143Snate@binkert.org        cmd = '%s -t %d %s' % (env['BATCH_CMD'], timeout, cmd)
1076143Snate@binkert.org
1086143Snate@binkert.org    # Create a default value for the status string, changed as needed
1096143Snate@binkert.org    # based on the status.
1106143Snate@binkert.org    status_str = "passed."
1116143Snate@binkert.org
1126143Snate@binkert.org    pre_exec_time = time.time()
1135522Snate@binkert.org    status = env.Execute(env.subst(cmd, target=target, source=source))
1146143Snate@binkert.org    if status == 0:
1156143Snate@binkert.org        # gem5 terminated normally.
1166143Snate@binkert.org        # Run diff on output & ref directories to find differences.
1176143Snate@binkert.org        # Exclude the stats file since we will use diff-out on that.
1186143Snate@binkert.org
1196143Snate@binkert.org        # NFS file systems can be annoying and not have updated yet
1206143Snate@binkert.org        # wait until we see the file modified
1216143Snate@binkert.org        statsdiff = os.path.join(tgt_dir, 'statsdiff')
1226143Snate@binkert.org        m_time = 0
1236143Snate@binkert.org        nap = 0
1245522Snate@binkert.org        while m_time < pre_exec_time and nap < 10:
1255522Snate@binkert.org            try:
1265522Snate@binkert.org                m_time = os.stat(statsdiff).st_mtime
1275522Snate@binkert.org            except OSError:
1285604Snate@binkert.org                pass
1295604Snate@binkert.org            time.sleep(1)
1306143Snate@binkert.org            nap += 1
1316143Snate@binkert.org
1324762Snate@binkert.org        outdiff = os.path.join(tgt_dir, 'outdiff')
1334762Snate@binkert.org        # tack 'true' on the end so scons doesn't report diff's
1346143Snate@binkert.org        # non-zero exit code as a build error
1356143Snate@binkert.org        diffcmd = 'diff -ubrs %s ${SOURCES[2].dir} %s > %s; true' \
1366143Snate@binkert.org                  % (output_ignore_args, tgt_dir, outdiff)
1376143Snate@binkert.org        env.Execute(env.subst(diffcmd, target=target, source=source))
1384762Snate@binkert.org        print "===== Output differences ====="
1396143Snate@binkert.org        print contents(outdiff)
1406143Snate@binkert.org        # Run diff-out on stats.txt file
1416143Snate@binkert.org        diffcmd = '$DIFFOUT ${SOURCES[2]} %s > %s' \
1426143Snate@binkert.org                  % (os.path.join(tgt_dir, 'stats.txt'), statsdiff)
1436143Snate@binkert.org        diffcmd = env.subst(diffcmd, target=target, source=source)
1446143Snate@binkert.org        diff_status = env.Execute(diffcmd, strfunction=None)
1456143Snate@binkert.org        # If there is a difference, change the status string to say so
1466143Snate@binkert.org        if diff_status != 0:
1475604Snate@binkert.org            status_str = "CHANGED!"
1486143Snate@binkert.org        print "===== Statistics differences ====="
1496143Snate@binkert.org        print contents(statsdiff)
1506143Snate@binkert.org
1514762Snate@binkert.org    else: # gem5 exit status != 0
1526143Snate@binkert.org        # Consider it a failed test unless the exit status is 2
1534762Snate@binkert.org        status_str = "FAILED!"
1544762Snate@binkert.org        # gem5 did not terminate properly, so no need to check the output
1554762Snate@binkert.org        if signaled(status):
1566143Snate@binkert.org            print 'gem5 terminated with signal', signum(status)
1576143Snate@binkert.org            if signum(status) in retry_signals:
1584762Snate@binkert.org                # Consider the test incomplete; don't create a 'status' output.
1596143Snate@binkert.org                # Hand the return status to scons and let scons decide what
1606143Snate@binkert.org                # to do about it (typically terminate unless run with -k).
1616143Snate@binkert.org                return status
1626143Snate@binkert.org        elif status == 2:
1634762Snate@binkert.org            # The test was skipped, change the status string to say so
1646143Snate@binkert.org            status_str = "skipped."
1654762Snate@binkert.org        else:
1666143Snate@binkert.org            print 'gem5 exited with non-zero status', status
1674762Snate@binkert.org        # complete but failed execution (call to exit() with non-zero
1686143Snate@binkert.org        # status, SIGABORT due to assertion failure, etc.)... fall through
1696143Snate@binkert.org        # and generate FAILED status as if output comparison had failed
1706143Snate@binkert.org
1716143Snate@binkert.org    # Generate status file contents based on exit status of gem5 and diff-out
1726143Snate@binkert.org    f = file(str(target[0]), 'w')
1736143Snate@binkert.org    print >>f, tgt_dir, status_str
1746143Snate@binkert.org    f.close()
1756143Snate@binkert.org    # done
1766143Snate@binkert.org    return 0
1776143Snate@binkert.org
1786143Snate@binkert.orgdef run_test_string(target, source, env):
1796143Snate@binkert.org    return env.subst("Running test in ${TARGETS[0].dir}.",
1806143Snate@binkert.org                     target=target, source=source)
181955SN/A
1825584Snate@binkert.orgtestAction = env.Action(run_test, run_test_string)
1835584Snate@binkert.org
1845584Snate@binkert.orgdef print_test(target, source, env):
1855584Snate@binkert.org    # print the status with colours to make it easier to see what
1866143Snate@binkert.org    # passed and what failed
1876143Snate@binkert.org    line = contents(source[0])
1886143Snate@binkert.org
1895584Snate@binkert.org    # split the line to words and get the last one
1904382Sbinkertn@umich.edu    words = line.split()
1914202Sbinkertn@umich.edu    status = words[-1]
1924382Sbinkertn@umich.edu
1934382Sbinkertn@umich.edu    # if the test failed make it red, if it passed make it green, and
1944382Sbinkertn@umich.edu    # skip the punctuation
1955584Snate@binkert.org    if status == "FAILED!":
1964382Sbinkertn@umich.edu        status = termcap.Red + status[:-1] + termcap.Normal + status[-1]
1974382Sbinkertn@umich.edu    elif status == "CHANGED!":
1984382Sbinkertn@umich.edu        status = termcap.Yellow + status[:-1] + termcap.Normal + status[-1]
1995192Ssaidi@eecs.umich.edu    elif status == "passed.":
2005192Ssaidi@eecs.umich.edu        status = termcap.Green + status[:-1] + termcap.Normal + status[-1]
2015799Snate@binkert.org    elif status == "skipped.":
2025799Snate@binkert.org        status = termcap.Cyan + status[:-1] + termcap.Normal + status[-1]
2035799Snate@binkert.org
2045192Ssaidi@eecs.umich.edu    # put it back in the list and join with space
2055799Snate@binkert.org    words[-1] = status
2065192Ssaidi@eecs.umich.edu    line = " ".join(words)
2075799Snate@binkert.org
2085799Snate@binkert.org    print '***** ' + line
2095192Ssaidi@eecs.umich.edu    return 0
2105192Ssaidi@eecs.umich.edu
2115192Ssaidi@eecs.umich.eduprintAction = env.Action(print_test, strfunction = None)
2125192Ssaidi@eecs.umich.edu
2135799Snate@binkert.org# Static vars for update_test:
2145192Ssaidi@eecs.umich.edu# - long-winded message about ignored sources
2155799Snate@binkert.orgignore_msg = '''
2165192Ssaidi@eecs.umich.eduNote: The following file(s) will not be copied.  New non-standard
2175192Ssaidi@eecs.umich.edu      output files must be copied manually once before --update-ref will
2185192Ssaidi@eecs.umich.edu      recognize them as outputs.  Otherwise they are assumed to be
2195799Snate@binkert.org      inputs and are ignored.
2205192Ssaidi@eecs.umich.edu'''
2215192Ssaidi@eecs.umich.edu# - reference files always needed
2225192Ssaidi@eecs.umich.eduneeded_files = set(['simout', 'simerr', 'stats.txt', 'config.ini'])
2235192Ssaidi@eecs.umich.edu# - source files we always want to ignore
2245192Ssaidi@eecs.umich.eduknown_ignores = set(['status', 'outdiff', 'statsdiff'])
2255192Ssaidi@eecs.umich.edu
2264382Sbinkertn@umich.edudef update_test(target, source, env):
2274382Sbinkertn@umich.edu    """Update reference test outputs.
2284382Sbinkertn@umich.edu
2292667Sstever@eecs.umich.edu    Target is phony.  First two sources are the ref & new stats.txt file
2302667Sstever@eecs.umich.edu    files, respectively.  We actually copy everything in the
2312667Sstever@eecs.umich.edu    respective directories except the status & diff output files.
2322667Sstever@eecs.umich.edu
2332667Sstever@eecs.umich.edu    """
2342667Sstever@eecs.umich.edu    dest_dir = str(source[0].get_dir())
2355742Snate@binkert.org    src_dir = str(source[1].get_dir())
2365742Snate@binkert.org    dest_files = set(os.listdir(dest_dir))
2375742Snate@binkert.org    src_files = set(os.listdir(src_dir))
2382037SN/A    # Copy all of the required files plus any existing dest files.
2392037SN/A    wanted_files = needed_files | dest_files
2402037SN/A    missing_files = wanted_files - src_files
2415793Snate@binkert.org    if len(missing_files) > 0:
2425793Snate@binkert.org        print "  WARNING: the following file(s) are missing " \
2435793Snate@binkert.org              "and will not be updated:"
2445793Snate@binkert.org        print "    ", " ,".join(missing_files)
2455793Snate@binkert.org    copy_files = wanted_files - missing_files
2464382Sbinkertn@umich.edu    warn_ignored_files = (src_files - copy_files) - known_ignores
2474762Snate@binkert.org    if len(warn_ignored_files) > 0:
2485344Sstever@gmail.com        print ignore_msg,
2494382Sbinkertn@umich.edu        print "       ", ", ".join(warn_ignored_files)
2505341Sstever@gmail.com    for f in copy_files:
2515742Snate@binkert.org        if f in dest_files:
2525742Snate@binkert.org            print "  Replacing file", f
2535742Snate@binkert.org            dest_files.remove(f)
2545742Snate@binkert.org        else:
2555742Snate@binkert.org            print "  Creating new file", f
2564762Snate@binkert.org        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
2575742Snate@binkert.org        copyAction.strfunction = None
2585742Snate@binkert.org        env.Execute(copyAction)
2595742Snate@binkert.org    return 0
2605742Snate@binkert.org
2615742Snate@binkert.orgdef update_test_string(target, source, env):
2625742Snate@binkert.org    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
2635742Snate@binkert.org                     target=target, source=source)
2645341Sstever@gmail.com
2655742Snate@binkert.orgupdateAction = env.Action(update_test, update_test_string)
2665341Sstever@gmail.com
2674773Snate@binkert.orgdef test_builder(env, ref_dir):
2686108Snate@binkert.org    """Define a test."""
2691858SN/A
2701085SN/A    path = list(ref_dir.split('/'))
2714382Sbinkertn@umich.edu
2724382Sbinkertn@umich.edu    # target path (where test output goes) consists of category, mode,
2734762Snate@binkert.org    # name, isa, opsys, and config (skips the 'ref' component)
2744762Snate@binkert.org    assert(path.pop(-4) == 'ref')
2754762Snate@binkert.org    tgt_dir = os.path.join(*path[-6:])
2765517Snate@binkert.org
2775517Snate@binkert.org    # local closure for prepending target path to filename
2785517Snate@binkert.org    def tgt(f):
2795517Snate@binkert.org        return os.path.join(tgt_dir, f)
2805517Snate@binkert.org
2815517Snate@binkert.org    ref_stats = os.path.join(ref_dir, 'stats.txt')
2825517Snate@binkert.org    new_stats = tgt('stats.txt')
2835517Snate@binkert.org    status_file = tgt('status')
2845517Snate@binkert.org
2855517Snate@binkert.org    env.Command([status_file, new_stats],
2865517Snate@binkert.org                [env.M5Binary, 'run.py', ref_stats],
2875517Snate@binkert.org                testAction)
2885517Snate@binkert.org
2895517Snate@binkert.org    # phony target to echo status
2905517Snate@binkert.org    if GetOption('update_ref'):
2915517Snate@binkert.org        p = env.Command(tgt('_update'),
2925517Snate@binkert.org                        [ref_stats, new_stats, status_file],
2935798Snate@binkert.org                        updateAction)
2945517Snate@binkert.org    else:
2955517Snate@binkert.org        p = env.Command(tgt('_print'), [status_file], printAction)
2965517Snate@binkert.org
2975517Snate@binkert.org    env.AlwaysBuild(p)
2985517Snate@binkert.org
2995517Snate@binkert.org
3005517Snate@binkert.org# Figure out applicable configs based on build type
3015517Snate@binkert.orgconfigs = []
3026143Snate@binkert.orgif env['TARGET_ISA'] == 'alpha':
3036143Snate@binkert.org    configs += ['tsunami-simple-atomic',
3045517Snate@binkert.org                'tsunami-simple-timing',
3055517Snate@binkert.org                'tsunami-simple-atomic-dual',
3065517Snate@binkert.org                'tsunami-simple-timing-dual',
3075517Snate@binkert.org                'twosys-tsunami-simple-atomic',
3085517Snate@binkert.org                'tsunami-o3', 'tsunami-o3-dual',
3095517Snate@binkert.org                'tsunami-inorder',
3105517Snate@binkert.org                'tsunami-switcheroo-full']
3115517Snate@binkert.orgif env['TARGET_ISA'] == 'sparc':
3125517Snate@binkert.org    configs += ['t1000-simple-atomic',
3135517Snate@binkert.org                't1000-simple-timing']
3145517Snate@binkert.orgif env['TARGET_ISA'] == 'arm':
3155517Snate@binkert.org    configs += ['simple-atomic-dummychecker',
3165517Snate@binkert.org                'o3-timing-checker',
3175517Snate@binkert.org                'realview-simple-atomic',
3185798Snate@binkert.org                'realview-simple-atomic-dual',
3195798Snate@binkert.org                'realview-simple-timing',
3205517Snate@binkert.org                'realview-simple-timing-dual',
3215517Snate@binkert.org                'realview-o3',
3226143Snate@binkert.org                'realview-o3-checker',
3236143Snate@binkert.org                'realview-o3-dual',
3246143Snate@binkert.org                'realview-switcheroo-atomic',
3256143Snate@binkert.org                'realview-switcheroo-timing',
3265517Snate@binkert.org                'realview-switcheroo-o3',
3276143Snate@binkert.org                'realview-switcheroo-full']
3285517Snate@binkert.orgif env['TARGET_ISA'] == 'x86':
3295517Snate@binkert.org    configs += ['pc-simple-atomic',
3305517Snate@binkert.org                'pc-simple-timing',
3315517Snate@binkert.org                'pc-o3-timing',
3325517Snate@binkert.org                'pc-switcheroo-full']
3335517Snate@binkert.org
3346143Snate@binkert.orgconfigs += ['simple-atomic', 'simple-timing', 'o3-timing', 'memtest',
3356143Snate@binkert.org            'simple-atomic-mp', 'simple-timing-mp', 'o3-timing-mp',
3365517Snate@binkert.org            'inorder-timing', 'rubytest', 'tgen-simple-mem',
3374762Snate@binkert.org            'tgen-dram-ctrl']
3385517Snate@binkert.org
3394762Snate@binkert.orgif env['PROTOCOL'] != 'None':
3405517Snate@binkert.org    if env['PROTOCOL'] == 'MI_example':
3415517Snate@binkert.org        configs += [c + "-ruby" for c in configs]
3426143Snate@binkert.org    else:
3436143Snate@binkert.org        configs = [c + "-ruby-" + env['PROTOCOL'] for c in configs]
3445517Snate@binkert.org
3455517Snate@binkert.orgsrc = Dir('.').srcdir
3465517Snate@binkert.orgfor config in configs:
3475517Snate@binkert.org    dirs = src.glob('*/*/*/ref/%s/*/%s' % (env['TARGET_ISA'], config))
3485517Snate@binkert.org    for d in dirs:
3495517Snate@binkert.org        d = str(d)
3505517Snate@binkert.org        if not os.path.exists(os.path.join(d, 'skip')):
3515517Snate@binkert.org            test_builder(env, d)
3525517Snate@binkert.org