SConscript revision 6166
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#
292665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt
30955SN/A#          Kevin Lim
31955SN/A
32955SN/Aimport os, signal
333583Sbinkertn@umich.eduimport sys
34955SN/Aimport glob
35955SN/Afrom SCons.Script.SConscript import SConsEnvironment
36955SN/A
37955SN/AImport('env')
38955SN/A
39955SN/Aenv['DIFFOUT'] = File('diff-out')
40955SN/A
41955SN/A# Dict that accumulates lists of tests by category (quick, medium, long)
42955SN/Aenv.Tests = {}
43955SN/A
44955SN/Adef contents(node):
45955SN/A    return file(str(node)).read()
46955SN/A
47955SN/A# functions to parse return value from scons Execute()... not the same
482023SN/A# as wait() etc., so python built-in os funcs don't work.
49955SN/Adef signaled(status):
503089Ssaidi@eecs.umich.edu    return (status & 0x80) != 0;
51955SN/A
52955SN/Adef signum(status):
53955SN/A    return (status & 0x7f);
54955SN/A
55955SN/A# List of signals that indicate that we should retry the test rather
56955SN/A# than consider it failed.
57955SN/Aretry_signals = (signal.SIGTERM, signal.SIGKILL, signal.SIGINT,
58955SN/A                 signal.SIGQUIT, signal.SIGHUP)
591031SN/A
60955SN/A# regular expressions of lines to ignore when diffing outputs
611388SN/Aoutput_ignore_regexes = (
62955SN/A    '^command line:',		# for stdout file
63955SN/A    '^M5 compiled ',		# for stderr file
641296SN/A    '^M5 started ',		# for stderr file
65955SN/A    '^M5 executing on ',	# for stderr file
66955SN/A    '^Simulation complete at',	# for stderr file
67955SN/A    '^Listening for',		# for stderr file
68955SN/A    'listening for remote gdb',	# for stderr file
69955SN/A    )
70955SN/A
71955SN/Aoutput_ignore_args = ' '.join(["-I '"+s+"'" for s in output_ignore_regexes])
72955SN/A
73955SN/Aoutput_ignore_args += ' --exclude=stats.txt --exclude=outdiff'
74955SN/A
75955SN/Adef run_test(target, source, env):
76955SN/A    """Check output from running test.
773584Ssaidi@eecs.umich.edu
78955SN/A    Targets are as follows:
79955SN/A    target[0] : status
80955SN/A
81955SN/A    Sources are:
82955SN/A    source[0] : M5 binary
83955SN/A    source[1] : tests/run.py script
84955SN/A    source[2] : reference stats file
852325SN/A
861717SN/A    """
872652Ssaidi@eecs.umich.edu    # make sure target files are all gone
88955SN/A    for t in target:
892736Sktlim@umich.edu        if os.path.exists(t.abspath):
902410SN/A            env.Execute(Delete(t.abspath))
91955SN/A
922290SN/A    tgt_dir = os.path.dirname(str(target[0]))
93955SN/A
942683Sktlim@umich.edu    # Base command for running test.  We mess around with indirectly
952683Sktlim@umich.edu    # referring to files via SOURCES and TARGETS so that scons can mess
962669Sktlim@umich.edu    # with paths all it wants to and we still get the right files.
972568SN/A    cmd = '${SOURCES[0]} -d %s -re ${SOURCES[1]} %s' % (tgt_dir, tgt_dir)
982568SN/A
993012Ssaidi@eecs.umich.edu    # Prefix test run with batch job submission command if appropriate.
1002462SN/A    # Batch command also supports timeout arg (in seconds, not minutes).
1012568SN/A    timeout = 15 * 60 # used to be a param, probably should be again
1022395SN/A    if env['BATCH']:
1032405SN/A        cmd = '%s -t %d %s' % (env['BATCH_CMD'], timeout, cmd)
1042914Ssaidi@eecs.umich.edu
105955SN/A    status = env.Execute(env.subst(cmd, target=target, source=source))
1062811Srdreslin@umich.edu    if status == 0:
1072811Srdreslin@umich.edu        # M5 terminated normally.
1082811Srdreslin@umich.edu        # Run diff on output & ref directories to find differences.
1092811Srdreslin@umich.edu        # Exclude the stats file since we will use diff-out on that.
1102811Srdreslin@umich.edu        outdiff = os.path.join(tgt_dir, 'outdiff')
1113719Sstever@eecs.umich.edu        diffcmd = 'diff -ubr %s ${SOURCES[2].dir} %s > %s' \
1122811Srdreslin@umich.edu                  % (output_ignore_args, tgt_dir, outdiff)
1132811Srdreslin@umich.edu        env.Execute(env.subst(diffcmd, target=target, source=source))
1142811Srdreslin@umich.edu        print "===== Output differences ====="
1152811Srdreslin@umich.edu        print contents(outdiff)
1162811Srdreslin@umich.edu        # Run diff-out on stats.txt file
1172811Srdreslin@umich.edu        statsdiff = os.path.join(tgt_dir, 'statsdiff')
1182811Srdreslin@umich.edu        diffcmd = '$DIFFOUT ${SOURCES[2]} %s > %s' \
1192811Srdreslin@umich.edu                  % (os.path.join(tgt_dir, 'stats.txt'), statsdiff)
1202811Srdreslin@umich.edu        diffcmd = env.subst(diffcmd, target=target, source=source)
1212814Srdreslin@umich.edu        status = env.Execute(diffcmd, strfunction=None)
1222811Srdreslin@umich.edu        print "===== Statistics differences ====="
1232811Srdreslin@umich.edu        print contents(statsdiff)
1242811Srdreslin@umich.edu
1252811Srdreslin@umich.edu    else: # m5 exit status != 0
1262811Srdreslin@umich.edu        # M5 did not terminate properly, so no need to check the output
1272811Srdreslin@umich.edu        if signaled(status):
1282811Srdreslin@umich.edu            print 'M5 terminated with signal', signum(status)
1292813Srdreslin@umich.edu            if signum(status) in retry_signals:
1302813Srdreslin@umich.edu                # Consider the test incomplete; don't create a 'status' output.
1313868Sbinkertn@umich.edu                # Hand the return status to scons and let scons decide what
1323645Sbinkertn@umich.edu                # to do about it (typically terminate unless run with -k).
1333624Sbinkertn@umich.edu                return status
1343871Sbinkertn@umich.edu        else:
1353871Sbinkertn@umich.edu            print 'M5 exited with non-zero status', status
1363624Sbinkertn@umich.edu        # complete but failed execution (call to exit() with non-zero
137955SN/A        # status, SIGABORT due to assertion failure, etc.)... fall through
138955SN/A        # and generate FAILED status as if output comparison had failed
139955SN/A
1402090SN/A    # Generate status file contents based on exit status of m5 or diff-out
141955SN/A    if status == 0:
142955SN/A        status_str = "passed."
1431696SN/A    else:
144955SN/A        status_str = "FAILED!"
145955SN/A    f = file(str(target[0]), 'w')
146955SN/A    print >>f, tgt_dir, status_str
1471127SN/A    f.close()
148955SN/A    # done
149955SN/A    return 0
1502379SN/A
151955SN/Adef run_test_string(target, source, env):
152955SN/A    return env.subst("Running test in ${TARGETS[0].dir}.",
153955SN/A                     target=target, source=source)
1542422SN/A
1552422SN/AtestAction = env.Action(run_test, run_test_string)
1562422SN/A
1572422SN/Adef print_test(target, source, env):
1582422SN/A    print '***** ' + contents(source[0])
1592422SN/A    return 0
1602422SN/A
1612397SN/AprintAction = env.Action(print_test, strfunction = None)
1622397SN/A
1632422SN/A# Static vars for update_test:
1642422SN/A# - long-winded message about ignored sources
165955SN/Aignore_msg = '''
166955SN/ANote: The following file(s) will not be copied.  New non-standard
167955SN/A      output files must be copied manually once before update_ref will
168955SN/A      recognize them as outputs.  Otherwise they are assumed to be
169955SN/A      inputs and are ignored.
170955SN/A'''
171955SN/A# - reference files always needed
172955SN/Aneeded_files = set(['simout', 'simerr', 'stats.txt', 'config.ini'])
1731078SN/A# - source files we always want to ignore
174955SN/Aknown_ignores = set(['status', 'outdiff', 'statsdiff'])
175955SN/A
176955SN/Adef update_test(target, source, env):
177955SN/A    """Update reference test outputs.
1781917SN/A
179955SN/A    Target is phony.  First two sources are the ref & new stats.txt file
180955SN/A    files, respectively.  We actually copy everything in the
1811730SN/A    respective directories except the status & diff output files.
182955SN/A
1832521SN/A    """
1842521SN/A    dest_dir = str(source[0].get_dir())
1852507SN/A    src_dir = str(source[1].get_dir())
1862507SN/A    dest_files = set(os.listdir(dest_dir))
1872989Ssaidi@eecs.umich.edu    src_files = set(os.listdir(src_dir))
1883408Ssaidi@eecs.umich.edu    # Copy all of the required files plus any existing dest files.
1892507SN/A    wanted_files = needed_files | dest_files
1902507SN/A    missing_files = wanted_files - src_files
1912507SN/A    if len(missing_files) > 0:
192955SN/A        print "  WARNING: the following file(s) are missing " \
193955SN/A              "and will not be updated:"
194955SN/A        print "    ", " ,".join(missing_files)
195955SN/A    copy_files = wanted_files - missing_files
196955SN/A    warn_ignored_files = (src_files - copy_files) - known_ignores
197955SN/A    if len(warn_ignored_files) > 0:
198955SN/A        print ignore_msg,
199955SN/A        print "       ", ", ".join(warn_ignored_files)
2002520SN/A    for f in copy_files:
2012517SN/A        if f in dest_files:
2022253SN/A            print "  Replacing file", f
2032253SN/A            dest_files.remove(f)
2042253SN/A        else:
2052253SN/A            print "  Creating new file", f
2062553SN/A        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
2072553SN/A        copyAction.strfunction = None
2082553SN/A        env.Execute(copyAction)
2092553SN/A    return 0
2102507SN/A
2112400SN/Adef update_test_string(target, source, env):
2122400SN/A    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
213955SN/A                     target=target, source=source)
214955SN/A
2152667Sstever@eecs.umich.eduupdateAction = env.Action(update_test, update_test_string)
2162667Sstever@eecs.umich.edu
2172667Sstever@eecs.umich.edudef test_builder(env, ref_dir):
2182667Sstever@eecs.umich.edu    """Define a test."""
2192667Sstever@eecs.umich.edu
2202667Sstever@eecs.umich.edu    (category, name, _ref, isa, opsys, config) = ref_dir.split('/')
2212037SN/A    assert(_ref == 'ref')
2222037SN/A
2232037SN/A    # target path (where test output goes) is the same except without
2243534Sgblack@eecs.umich.edu    # the 'ref' component
2252139SN/A    tgt_dir = os.path.join(category, name, isa, opsys, config)
2263534Sgblack@eecs.umich.edu
2273534Sgblack@eecs.umich.edu    # prepend file name with tgt_dir
2283542Sgblack@eecs.umich.edu    def tgt(f):
2293583Sbinkertn@umich.edu        return os.path.join(tgt_dir, f)
2303583Sbinkertn@umich.edu
2313542Sgblack@eecs.umich.edu    ref_stats = os.path.join(ref_dir, 'stats.txt')
2323499Ssaidi@eecs.umich.edu    new_stats = tgt('stats.txt')
2333583Sbinkertn@umich.edu    status_file = tgt('status')
2343583Sbinkertn@umich.edu
2353547Sgblack@eecs.umich.edu    env.Command([status_file],
2362155SN/A                [env.M5Binary, 'run.py', ref_stats],
237955SN/A                testAction)
2382155SN/A
239955SN/A    # phony target to echo status
2403583Sbinkertn@umich.edu    if env['update_ref']:
2413583Sbinkertn@umich.edu        p = env.Command(tgt('_update'),
2423583Sbinkertn@umich.edu                        [ref_stats, new_stats, status_file],
2433583Sbinkertn@umich.edu                        updateAction)
2443583Sbinkertn@umich.edu    else:
245955SN/A        p = env.Command(tgt('_print'), [status_file], printAction)
246955SN/A
247955SN/A    env.AlwaysBuild(p)
248955SN/A
249955SN/A
2501858SN/A# Figure out applicable configs based on build type
251955SN/Aconfigs = []
2521858SN/Aif env['FULL_SYSTEM']:
2531858SN/A    if env['TARGET_ISA'] == 'alpha':
2541858SN/A        configs += ['tsunami-simple-atomic',
2551085SN/A                    'tsunami-simple-timing',
256955SN/A                    'tsunami-simple-atomic-dual',
257955SN/A                    'tsunami-simple-timing-dual',
258955SN/A                    'twosys-tsunami-simple-atomic',
259955SN/A                    'tsunami-o3', 'tsunami-o3-dual']
260955SN/A    if env['TARGET_ISA'] == 'sparc':
261955SN/A        configs += ['t1000-simple-atomic',
262955SN/A                    't1000-simple-timing']
263955SN/A
264955SN/Aelse:
265955SN/A    configs += ['simple-atomic', 'simple-timing', 'o3-timing', 'memtest',
266955SN/A                'simple-atomic-mp', 'simple-timing-mp', 'o3-timing-mp']
267955SN/A
2682667Sstever@eecs.umich.edu# Hack for Ruby
2691045SN/Aconfigs += [c + '-ruby' for c in configs]
270955SN/A
271955SN/Acwd = os.getcwd()
272955SN/Aos.chdir(str(Dir('.').srcdir))
273955SN/Afor config in configs:
2741108SN/A    dirs = glob.glob('*/*/ref/%s/*/%s' % (env['TARGET_ISA'], config))
275955SN/A    for d in dirs:
276955SN/A        test_builder(env, d)
277955SN/Aos.chdir(cwd)
278955SN/A