SConscript revision 7966
16145Snate@binkert.org# -*- mode:python -*-
26145Snate@binkert.org
36145Snate@binkert.org# Copyright (c) 2004-2006 The Regents of The University of Michigan
46145Snate@binkert.org# All rights reserved.
56145Snate@binkert.org#
66145Snate@binkert.org# Redistribution and use in source and binary forms, with or without
76145Snate@binkert.org# modification, are permitted provided that the following conditions are
86145Snate@binkert.org# met: redistributions of source code must retain the above copyright
96145Snate@binkert.org# notice, this list of conditions and the following disclaimer;
106145Snate@binkert.org# redistributions in binary form must reproduce the above copyright
116145Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
126145Snate@binkert.org# documentation and/or other materials provided with the distribution;
136145Snate@binkert.org# neither the name of the copyright holders nor the names of its
146145Snate@binkert.org# contributors may be used to endorse or promote products derived from
156145Snate@binkert.org# this software without specific prior written permission.
166145Snate@binkert.org#
176145Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186145Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196145Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206145Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216145Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226145Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236145Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246145Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256145Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266145Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276145Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286145Snate@binkert.org#
297048Snate@binkert.org# Authors: Steve Reinhardt
3011793Sbrandon.potter@amd.com#          Kevin Lim
319171Snilay@cs.wisc.edu
326145Snate@binkert.orgimport os, signal
337055Snate@binkert.orgimport sys, time
347055Snate@binkert.orgimport glob
357048Snate@binkert.orgfrom SCons.Script.SConscript import SConsEnvironment
367048Snate@binkert.org
3711061Snilay@cs.wisc.eduImport('env')
387048Snate@binkert.org
396145Snate@binkert.orgenv['DIFFOUT'] = File('diff-out')
406145Snate@binkert.org
416145Snate@binkert.org# Dict that accumulates lists of tests by category (quick, medium, long)
426145Snate@binkert.orgenv.Tests = {}
436145Snate@binkert.org
4411025Snilay@cs.wisc.edudef contents(node):
457048Snate@binkert.org    return file(str(node)).read()
467048Snate@binkert.org
476145Snate@binkert.org# functions to parse return value from scons Execute()... not the same
487048Snate@binkert.org# as wait() etc., so python built-in os funcs don't work.
497048Snate@binkert.orgdef signaled(status):
507048Snate@binkert.org    return (status & 0x80) != 0;
517048Snate@binkert.org
527048Snate@binkert.orgdef signum(status):
537048Snate@binkert.org    return (status & 0x7f);
547048Snate@binkert.org
556145Snate@binkert.org# List of signals that indicate that we should retry the test rather
566145Snate@binkert.org# than consider it failed.
576145Snate@binkert.orgretry_signals = (signal.SIGTERM, signal.SIGKILL, signal.SIGINT,
586145Snate@binkert.org                 signal.SIGQUIT, signal.SIGHUP)
596145Snate@binkert.org
606145Snate@binkert.org# regular expressions of lines to ignore when diffing outputs
617048Snate@binkert.orgoutput_ignore_regexes = (
627048Snate@binkert.org    '^command line:',		# for stdout file
636145Snate@binkert.org    '^M5 compiled ',		# for stderr file
647048Snate@binkert.org    '^M5 started ',		# for stderr file
657048Snate@binkert.org    '^M5 executing on ',	# for stderr file
667048Snate@binkert.org    '^Simulation complete at',	# for stderr file
677048Snate@binkert.org    '^Listening for',		# for stderr file
687048Snate@binkert.org    'listening for remote gdb',	# for stderr file
697048Snate@binkert.org    )
706145Snate@binkert.org
716145Snate@binkert.orgoutput_ignore_args = ' '.join(["-I '"+s+"'" for s in output_ignore_regexes])
727048Snate@binkert.org
737048Snate@binkert.orgoutput_ignore_args += ' --exclude=stats.txt --exclude=outdiff'
746145Snate@binkert.org
757048Snate@binkert.orgdef run_test(target, source, env):
767048Snate@binkert.org    """Check output from running test.
777048Snate@binkert.org
787048Snate@binkert.org    Targets are as follows:
797048Snate@binkert.org    target[0] : status
807048Snate@binkert.org
817048Snate@binkert.org    Sources are:
827048Snate@binkert.org    source[0] : M5 binary
836145Snate@binkert.org    source[1] : tests/run.py script
846145Snate@binkert.org    source[2] : reference stats file
857048Snate@binkert.org
867048Snate@binkert.org    """
876145Snate@binkert.org    # make sure target files are all gone
887048Snate@binkert.org    for t in target:
897048Snate@binkert.org        if os.path.exists(t.abspath):
907048Snate@binkert.org            env.Execute(Delete(t.abspath))
917048Snate@binkert.org
927048Snate@binkert.org    tgt_dir = os.path.dirname(str(target[0]))
936145Snate@binkert.org
946145Snate@binkert.org    # Base command for running test.  We mess around with indirectly
957048Snate@binkert.org    # referring to files via SOURCES and TARGETS so that scons can mess
967048Snate@binkert.org    # with paths all it wants to and we still get the right files.
976145Snate@binkert.org    cmd = '${SOURCES[0]} -d %s -re ${SOURCES[1]} %s' % (tgt_dir, tgt_dir)
987048Snate@binkert.org
997048Snate@binkert.org    # Prefix test run with batch job submission command if appropriate.
1007048Snate@binkert.org    # Batch command also supports timeout arg (in seconds, not minutes).
1017048Snate@binkert.org    timeout = 15 * 60 # used to be a param, probably should be again
1027048Snate@binkert.org    if env['BATCH']:
1037048Snate@binkert.org        cmd = '%s -t %d %s' % (env['BATCH_CMD'], timeout, cmd)
1046145Snate@binkert.org
1056145Snate@binkert.org    pre_exec_time = time.time()
1067048Snate@binkert.org    status = env.Execute(env.subst(cmd, target=target, source=source))
1077048Snate@binkert.org    if status == 0:
1086145Snate@binkert.org        # M5 terminated normally.
1099171Snilay@cs.wisc.edu        # Run diff on output & ref directories to find differences.
1106145Snate@binkert.org        # Exclude the stats file since we will use diff-out on that.
1117048Snate@binkert.org
1126145Snate@binkert.org        # NFS file systems can be annoying and not have updated yet
1137048Snate@binkert.org        # wait until we see the file modified
1147048Snate@binkert.org        statsdiff = os.path.join(tgt_dir, 'statsdiff')
1157048Snate@binkert.org        m_time = 0
1167048Snate@binkert.org        nap = 0
1176145Snate@binkert.org        while m_time < pre_exec_time and nap < 10:
1187048Snate@binkert.org            try:
1197048Snate@binkert.org                m_time = os.stat(statsdiff).st_mtime
1207048Snate@binkert.org            except OSError:
1217048Snate@binkert.org                pass
1226145Snate@binkert.org            time.sleep(1)
1237048Snate@binkert.org            nap += 1
1247048Snate@binkert.org
1256145Snate@binkert.org        outdiff = os.path.join(tgt_dir, 'outdiff')
1266145Snate@binkert.org        diffcmd = 'diff -ubrs %s ${SOURCES[2].dir} %s > %s' \
1277048Snate@binkert.org                  % (output_ignore_args, tgt_dir, outdiff)
1287048Snate@binkert.org        env.Execute(env.subst(diffcmd, target=target, source=source))
1296145Snate@binkert.org        print "===== Output differences ====="
1307048Snate@binkert.org        print contents(outdiff)
13110302Snilay@cs.wisc.edu        # Run diff-out on stats.txt file
1327048Snate@binkert.org        diffcmd = '$DIFFOUT ${SOURCES[2]} %s > %s' \
1337048Snate@binkert.org                  % (os.path.join(tgt_dir, 'stats.txt'), statsdiff)
1347048Snate@binkert.org        diffcmd = env.subst(diffcmd, target=target, source=source)
1357048Snate@binkert.org        status = env.Execute(diffcmd, strfunction=None)
1366145Snate@binkert.org        print "===== Statistics differences ====="
1377048Snate@binkert.org        print contents(statsdiff)
1387048Snate@binkert.org
1397048Snate@binkert.org    else: # m5 exit status != 0
1407048Snate@binkert.org        # M5 did not terminate properly, so no need to check the output
1417048Snate@binkert.org        if signaled(status):
1427048Snate@binkert.org            print 'M5 terminated with signal', signum(status)
1436145Snate@binkert.org            if signum(status) in retry_signals:
1447048Snate@binkert.org                # Consider the test incomplete; don't create a 'status' output.
1457048Snate@binkert.org                # Hand the return status to scons and let scons decide what
1467048Snate@binkert.org                # to do about it (typically terminate unless run with -k).
1477048Snate@binkert.org                return status
1487048Snate@binkert.org        else:
1497048Snate@binkert.org            print 'M5 exited with non-zero status', status
1507048Snate@binkert.org        # complete but failed execution (call to exit() with non-zero
1516145Snate@binkert.org        # status, SIGABORT due to assertion failure, etc.)... fall through
1527048Snate@binkert.org        # and generate FAILED status as if output comparison had failed
1537048Snate@binkert.org
1547048Snate@binkert.org    # Generate status file contents based on exit status of m5 or diff-out
1557048Snate@binkert.org    if status == 0:
1567048Snate@binkert.org        status_str = "passed."
1577048Snate@binkert.org    else:
1586145Snate@binkert.org        status_str = "FAILED!"
159    f = file(str(target[0]), 'w')
160    print >>f, tgt_dir, status_str
161    f.close()
162    # done
163    return 0
164
165def run_test_string(target, source, env):
166    return env.subst("Running test in ${TARGETS[0].dir}.",
167                     target=target, source=source)
168
169testAction = env.Action(run_test, run_test_string)
170
171def print_test(target, source, env):
172    print '***** ' + contents(source[0])
173    return 0
174
175printAction = env.Action(print_test, strfunction = None)
176
177# Static vars for update_test:
178# - long-winded message about ignored sources
179ignore_msg = '''
180Note: The following file(s) will not be copied.  New non-standard
181      output files must be copied manually once before update_ref will
182      recognize them as outputs.  Otherwise they are assumed to be
183      inputs and are ignored.
184'''
185# - reference files always needed
186needed_files = set(['simout', 'simerr', 'stats.txt', 'config.ini'])
187# - source files we always want to ignore
188known_ignores = set(['status', 'outdiff', 'statsdiff'])
189
190def update_test(target, source, env):
191    """Update reference test outputs.
192
193    Target is phony.  First two sources are the ref & new stats.txt file
194    files, respectively.  We actually copy everything in the
195    respective directories except the status & diff output files.
196
197    """
198    dest_dir = str(source[0].get_dir())
199    src_dir = str(source[1].get_dir())
200    dest_files = set(os.listdir(dest_dir))
201    src_files = set(os.listdir(src_dir))
202    # Copy all of the required files plus any existing dest files.
203    wanted_files = needed_files | dest_files
204    missing_files = wanted_files - src_files
205    if len(missing_files) > 0:
206        print "  WARNING: the following file(s) are missing " \
207              "and will not be updated:"
208        print "    ", " ,".join(missing_files)
209    copy_files = wanted_files - missing_files
210    warn_ignored_files = (src_files - copy_files) - known_ignores
211    if len(warn_ignored_files) > 0:
212        print ignore_msg,
213        print "       ", ", ".join(warn_ignored_files)
214    for f in copy_files:
215        if f in dest_files:
216            print "  Replacing file", f
217            dest_files.remove(f)
218        else:
219            print "  Creating new file", f
220        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
221        copyAction.strfunction = None
222        env.Execute(copyAction)
223    return 0
224
225def update_test_string(target, source, env):
226    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
227                     target=target, source=source)
228
229updateAction = env.Action(update_test, update_test_string)
230
231def test_builder(env, ref_dir):
232    """Define a test."""
233
234    (category, name, _ref, isa, opsys, config) = ref_dir.split('/')
235    assert(_ref == 'ref')
236
237    # target path (where test output goes) is the same except without
238    # the 'ref' component
239    tgt_dir = os.path.join(category, name, isa, opsys, config)
240
241    # prepend file name with tgt_dir
242    def tgt(f):
243        return os.path.join(tgt_dir, f)
244
245    ref_stats = os.path.join(ref_dir, 'stats.txt')
246    new_stats = tgt('stats.txt')
247    status_file = tgt('status')
248
249    env.Command([status_file],
250                [env.M5Binary, 'run.py', ref_stats],
251                testAction)
252
253    # phony target to echo status
254    if env['update_ref']:
255        p = env.Command(tgt('_update'),
256                        [ref_stats, new_stats, status_file],
257                        updateAction)
258    else:
259        p = env.Command(tgt('_print'), [status_file], printAction)
260
261    env.AlwaysBuild(p)
262
263
264# Figure out applicable configs based on build type
265configs = []
266if env['FULL_SYSTEM']:
267    if env['TARGET_ISA'] == 'alpha':
268        configs += ['tsunami-simple-atomic',
269                    'tsunami-simple-timing',
270                    'tsunami-simple-atomic-dual',
271                    'tsunami-simple-timing-dual',
272                    'twosys-tsunami-simple-atomic',
273                    'tsunami-o3', 'tsunami-o3-dual']
274    if env['TARGET_ISA'] == 'sparc':
275        configs += ['t1000-simple-atomic',
276                    't1000-simple-timing']
277    if env['TARGET_ISA'] == 'arm':
278        configs += ['realview-simple-atomic',
279                    'realview-simple-timing']
280    if env['TARGET_ISA'] == 'x86':
281        configs += ['pc-simple-atomic',
282                    'pc-simple-timing']
283
284else:
285    configs += ['simple-atomic', 'simple-timing', 'o3-timing', 'memtest',
286                'simple-atomic-mp', 'simple-timing-mp', 'o3-timing-mp',
287                'inorder-timing', 'rubytest']
288
289if env['RUBY']:
290    # With Ruby, A protocol must be specified in the environment
291    assert(env['PROTOCOL'])
292
293    #
294    # Is there a way to determine what is Protocol EnumVariable
295    # default and eliminate the need to hard code the default protocol below?
296    #
297    # If the binary includes the default ruby protocol, run both ruby and
298    # non-ruby versions of the tests.  Otherwise just run the ruby versions.
299    #
300    if env['PROTOCOL'] == 'MI_example':
301        configs += [c + "-ruby" for c in configs]
302    else:
303        configs = [c + "-ruby-" + env['PROTOCOL'] for c in configs]
304
305cwd = os.getcwd()
306os.chdir(str(Dir('.').srcdir))
307for config in configs:
308    dirs = glob.glob('*/*/ref/%s/*/%s' % (env['TARGET_ISA'], config))
309    for d in dirs:
310        if not os.path.exists(os.path.join(d, 'skip')):
311            test_builder(env, d)
312os.chdir(cwd)
313