SConscript revision 6994
12155SN/A# -*- mode:python -*-
22155SN/A
32155SN/A# Copyright (c) 2004-2006 The Regents of The University of Michigan
42155SN/A# All rights reserved.
52155SN/A#
62155SN/A# Redistribution and use in source and binary forms, with or without
72155SN/A# modification, are permitted provided that the following conditions are
82155SN/A# met: redistributions of source code must retain the above copyright
92155SN/A# notice, this list of conditions and the following disclaimer;
102155SN/A# redistributions in binary form must reproduce the above copyright
112155SN/A# notice, this list of conditions and the following disclaimer in the
122155SN/A# documentation and/or other materials provided with the distribution;
132155SN/A# neither the name of the copyright holders nor the names of its
142155SN/A# contributors may be used to endorse or promote products derived from
152155SN/A# this software without specific prior written permission.
162155SN/A#
172155SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182155SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192155SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202155SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212155SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222155SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232155SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242155SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252155SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262155SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272155SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
292665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt
302155SN/A#          Kevin Lim
314202Sbinkertn@umich.edu
322155SN/Aimport os, signal
332178SN/Aimport sys
342178SN/Aimport glob
352178SN/Afrom SCons.Script.SConscript import SConsEnvironment
362178SN/A
372178SN/AImport('env')
382178SN/A
392178SN/Aenv['DIFFOUT'] = File('diff-out')
402178SN/A
412178SN/A# Dict that accumulates lists of tests by category (quick, medium, long)
422178SN/Aenv.Tests = {}
432178SN/A
442178SN/Adef contents(node):
452155SN/A    return file(str(node)).read()
462178SN/A
472155SN/A# functions to parse return value from scons Execute()... not the same
482155SN/A# as wait() etc., so python built-in os funcs don't work.
492178SN/Adef signaled(status):
502155SN/A    return (status & 0x80) != 0;
515865Sksewell@umich.edu
525865Sksewell@umich.edudef signum(status):
533918Ssaidi@eecs.umich.edu    return (status & 0x7f);
545865Sksewell@umich.edu
552623SN/A# List of signals that indicate that we should retry the test rather
563918Ssaidi@eecs.umich.edu# than consider it failed.
575865Sksewell@umich.eduretry_signals = (signal.SIGTERM, signal.SIGKILL, signal.SIGINT,
585865Sksewell@umich.edu                 signal.SIGQUIT, signal.SIGHUP)
592155SN/A
602155SN/A# regular expressions of lines to ignore when diffing outputs
612292SN/Aoutput_ignore_regexes = (
623918Ssaidi@eecs.umich.edu    '^command line:',		# for stdout file
632292SN/A    '^M5 compiled ',		# for stderr file
642292SN/A    '^M5 started ',		# for stderr file
652292SN/A    '^M5 executing on ',	# for stderr file
663918Ssaidi@eecs.umich.edu    '^Simulation complete at',	# for stderr file
672292SN/A    '^Listening for',		# for stderr file
682292SN/A    'listening for remote gdb',	# for stderr file
692766Sktlim@umich.edu    )
702766Sktlim@umich.edu
712766Sktlim@umich.eduoutput_ignore_args = ' '.join(["-I '"+s+"'" for s in output_ignore_regexes])
722921Sktlim@umich.edu
732921Sktlim@umich.eduoutput_ignore_args += ' --exclude=stats.txt --exclude=outdiff'
742766Sktlim@umich.edu
752766Sktlim@umich.edudef run_test(target, source, env):
765529Snate@binkert.org    """Check output from running test.
772766Sktlim@umich.edu
784762Snate@binkert.org    Targets are as follows:
792155SN/A    target[0] : status
802155SN/A
812155SN/A    Sources are:
822155SN/A    source[0] : M5 binary
832155SN/A    source[1] : tests/run.py script
842155SN/A    source[2] : reference stats file
852766Sktlim@umich.edu
862155SN/A    """
875865Sksewell@umich.edu    # make sure target files are all gone
882155SN/A    for t in target:
892155SN/A        if os.path.exists(t.abspath):
902155SN/A            env.Execute(Delete(t.abspath))
912155SN/A
922178SN/A    tgt_dir = os.path.dirname(str(target[0]))
932178SN/A
942178SN/A    # Base command for running test.  We mess around with indirectly
952766Sktlim@umich.edu    # referring to files via SOURCES and TARGETS so that scons can mess
962178SN/A    # with paths all it wants to and we still get the right files.
972178SN/A    cmd = '${SOURCES[0]} -d %s -re ${SOURCES[1]} %s' % (tgt_dir, tgt_dir)
982178SN/A
992178SN/A    # Prefix test run with batch job submission command if appropriate.
1002766Sktlim@umich.edu    # Batch command also supports timeout arg (in seconds, not minutes).
1012766Sktlim@umich.edu    timeout = 15 * 60 # used to be a param, probably should be again
1022766Sktlim@umich.edu    if env['BATCH']:
1032788Sktlim@umich.edu        cmd = '%s -t %d %s' % (env['BATCH_CMD'], timeout, cmd)
1042178SN/A
1052733Sktlim@umich.edu    status = env.Execute(env.subst(cmd, target=target, source=source))
1062733Sktlim@umich.edu    if status == 0:
1072817Sksewell@umich.edu        # M5 terminated normally.
1082733Sktlim@umich.edu        # Run diff on output & ref directories to find differences.
1094486Sbinkertn@umich.edu        # Exclude the stats file since we will use diff-out on that.
1104486Sbinkertn@umich.edu        outdiff = os.path.join(tgt_dir, 'outdiff')
1114776Sgblack@eecs.umich.edu        diffcmd = 'diff -ubr %s ${SOURCES[2].dir} %s > %s' \
1124776Sgblack@eecs.umich.edu                  % (output_ignore_args, tgt_dir, outdiff)
1134486Sbinkertn@umich.edu        env.Execute(env.subst(diffcmd, target=target, source=source))
1144202Sbinkertn@umich.edu        print "===== Output differences ====="
1154202Sbinkertn@umich.edu        print contents(outdiff)
1164202Sbinkertn@umich.edu        # Run diff-out on stats.txt file
1174202Sbinkertn@umich.edu        statsdiff = os.path.join(tgt_dir, 'statsdiff')
1184202Sbinkertn@umich.edu        diffcmd = '$DIFFOUT ${SOURCES[2]} %s > %s' \
1194776Sgblack@eecs.umich.edu                  % (os.path.join(tgt_dir, 'stats.txt'), statsdiff)
1204202Sbinkertn@umich.edu        diffcmd = env.subst(diffcmd, target=target, source=source)
1214202Sbinkertn@umich.edu        status = env.Execute(diffcmd, strfunction=None)
1224202Sbinkertn@umich.edu        print "===== Statistics differences ====="
1234202Sbinkertn@umich.edu        print contents(statsdiff)
1245217Ssaidi@eecs.umich.edu
1254202Sbinkertn@umich.edu    else: # m5 exit status != 0
1262155SN/A        # M5 did not terminate properly, so no need to check the output
1274202Sbinkertn@umich.edu        if signaled(status):
1284486Sbinkertn@umich.edu            print 'M5 terminated with signal', signum(status)
1294486Sbinkertn@umich.edu            if signum(status) in retry_signals:
1304202Sbinkertn@umich.edu                # Consider the test incomplete; don't create a 'status' output.
1314202Sbinkertn@umich.edu                # Hand the return status to scons and let scons decide what
1322821Sktlim@umich.edu                # to do about it (typically terminate unless run with -k).
1334776Sgblack@eecs.umich.edu                return status
1344776Sgblack@eecs.umich.edu        else:
1354776Sgblack@eecs.umich.edu            print 'M5 exited with non-zero status', status
1364776Sgblack@eecs.umich.edu        # complete but failed execution (call to exit() with non-zero
1374776Sgblack@eecs.umich.edu        # status, SIGABORT due to assertion failure, etc.)... fall through
1384776Sgblack@eecs.umich.edu        # and generate FAILED status as if output comparison had failed
1394776Sgblack@eecs.umich.edu
1404776Sgblack@eecs.umich.edu    # Generate status file contents based on exit status of m5 or diff-out
1412766Sktlim@umich.edu    if status == 0:
1424202Sbinkertn@umich.edu        status_str = "passed."
1435192Ssaidi@eecs.umich.edu    else:
1442733Sktlim@umich.edu        status_str = "FAILED!"
1452733Sktlim@umich.edu    f = file(str(target[0]), 'w')
1462733Sktlim@umich.edu    print >>f, tgt_dir, status_str
1472733Sktlim@umich.edu    f.close()
1482733Sktlim@umich.edu    # done
1492874Sktlim@umich.edu    return 0
1502874Sktlim@umich.edu
1512874Sktlim@umich.edudef run_test_string(target, source, env):
1524202Sbinkertn@umich.edu    return env.subst("Running test in ${TARGETS[0].dir}.",
1532733Sktlim@umich.edu                     target=target, source=source)
1545192Ssaidi@eecs.umich.edu
1555192Ssaidi@eecs.umich.edutestAction = env.Action(run_test, run_test_string)
1565192Ssaidi@eecs.umich.edu
1575217Ssaidi@eecs.umich.edudef print_test(target, source, env):
1585192Ssaidi@eecs.umich.edu    print '***** ' + contents(source[0])
1595192Ssaidi@eecs.umich.edu    return 0
1605192Ssaidi@eecs.umich.edu
1615192Ssaidi@eecs.umich.eduprintAction = env.Action(print_test, strfunction = None)
1625192Ssaidi@eecs.umich.edu
1635192Ssaidi@eecs.umich.edu# Static vars for update_test:
1645192Ssaidi@eecs.umich.edu# - long-winded message about ignored sources
1655192Ssaidi@eecs.umich.eduignore_msg = '''
1665192Ssaidi@eecs.umich.eduNote: The following file(s) will not be copied.  New non-standard
1675192Ssaidi@eecs.umich.edu      output files must be copied manually once before update_ref will
1685192Ssaidi@eecs.umich.edu      recognize them as outputs.  Otherwise they are assumed to be
1695192Ssaidi@eecs.umich.edu      inputs and are ignored.
1705192Ssaidi@eecs.umich.edu'''
1715784Sgblack@eecs.umich.edu# - reference files always needed
1725784Sgblack@eecs.umich.eduneeded_files = set(['simout', 'simerr', 'stats.txt', 'config.ini'])
1735192Ssaidi@eecs.umich.edu# - source files we always want to ignore
1745192Ssaidi@eecs.umich.eduknown_ignores = set(['status', 'outdiff', 'statsdiff'])
1755192Ssaidi@eecs.umich.edu
1765192Ssaidi@eecs.umich.edudef update_test(target, source, env):
1775192Ssaidi@eecs.umich.edu    """Update reference test outputs.
1785192Ssaidi@eecs.umich.edu
1795784Sgblack@eecs.umich.edu    Target is phony.  First two sources are the ref & new stats.txt file
180    files, respectively.  We actually copy everything in the
181    respective directories except the status & diff output files.
182
183    """
184    dest_dir = str(source[0].get_dir())
185    src_dir = str(source[1].get_dir())
186    dest_files = set(os.listdir(dest_dir))
187    src_files = set(os.listdir(src_dir))
188    # Copy all of the required files plus any existing dest files.
189    wanted_files = needed_files | dest_files
190    missing_files = wanted_files - src_files
191    if len(missing_files) > 0:
192        print "  WARNING: the following file(s) are missing " \
193              "and will not be updated:"
194        print "    ", " ,".join(missing_files)
195    copy_files = wanted_files - missing_files
196    warn_ignored_files = (src_files - copy_files) - known_ignores
197    if len(warn_ignored_files) > 0:
198        print ignore_msg,
199        print "       ", ", ".join(warn_ignored_files)
200    for f in copy_files:
201        if f in dest_files:
202            print "  Replacing file", f
203            dest_files.remove(f)
204        else:
205            print "  Creating new file", f
206        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
207        copyAction.strfunction = None
208        env.Execute(copyAction)
209    return 0
210
211def update_test_string(target, source, env):
212    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
213                     target=target, source=source)
214
215updateAction = env.Action(update_test, update_test_string)
216
217def test_builder(env, ref_dir):
218    """Define a test."""
219
220    (category, name, _ref, isa, opsys, config) = ref_dir.split('/')
221    assert(_ref == 'ref')
222
223    # target path (where test output goes) is the same except without
224    # the 'ref' component
225    tgt_dir = os.path.join(category, name, isa, opsys, config)
226
227    # prepend file name with tgt_dir
228    def tgt(f):
229        return os.path.join(tgt_dir, f)
230
231    ref_stats = os.path.join(ref_dir, 'stats.txt')
232    new_stats = tgt('stats.txt')
233    status_file = tgt('status')
234
235    env.Command([status_file],
236                [env.M5Binary, 'run.py', ref_stats],
237                testAction)
238
239    # phony target to echo status
240    if env['update_ref']:
241        p = env.Command(tgt('_update'),
242                        [ref_stats, new_stats, status_file],
243                        updateAction)
244    else:
245        p = env.Command(tgt('_print'), [status_file], printAction)
246
247    env.AlwaysBuild(p)
248
249
250# Figure out applicable configs based on build type
251configs = []
252if env['FULL_SYSTEM']:
253    if env['TARGET_ISA'] == 'alpha':
254        configs += ['tsunami-simple-atomic',
255                    'tsunami-simple-timing',
256                    'tsunami-simple-atomic-dual',
257                    'tsunami-simple-timing-dual',
258                    'twosys-tsunami-simple-atomic',
259                    'tsunami-o3', 'tsunami-o3-dual']
260    if env['TARGET_ISA'] == 'sparc':
261        configs += ['t1000-simple-atomic',
262                    't1000-simple-timing']
263
264else:
265    configs += ['simple-atomic', 'simple-timing', 'o3-timing', 'memtest',
266                'simple-atomic-mp', 'simple-timing-mp', 'o3-timing-mp',
267                'inorder-timing', 'rubytest']
268
269if env['RUBY']:
270    # With Ruby, A protocol must be specified in the environment
271    assert(env['PROTOCOL'])
272
273    #
274    # Is there a way to determine what is Protocol EnumVariable
275    # default and eliminate the need to hard code the default protocol below?
276    #
277    # If the binary includes the default ruby protocol, run both ruby and
278    # non-ruby versions of the tests.  Otherwise just run the ruby versions.
279    #
280    if env['PROTOCOL'] == 'MI_example':
281        configs += [c + "-ruby" for c in configs]
282    else:
283        configs = [c + "-ruby-" + env['PROTOCOL'] for c in configs]
284
285cwd = os.getcwd()
286os.chdir(str(Dir('.').srcdir))
287for config in configs:
288    dirs = glob.glob('*/*/ref/%s/*/%s' % (env['TARGET_ISA'], config))
289    for d in dirs:
290        if not os.path.exists(os.path.join(d, 'skip')):
291            test_builder(env, d)
292os.chdir(cwd)
293