run.py revision 9851
19401SAndreas.Sandberg@ARM.com# Copyright (c) 2012 ARM Limited
29401SAndreas.Sandberg@ARM.com# All rights reserved
39401SAndreas.Sandberg@ARM.com#
49401SAndreas.Sandberg@ARM.com# The license below extends only to copyright in the software and shall
59401SAndreas.Sandberg@ARM.com# not be construed as granting a license to any other intellectual
69401SAndreas.Sandberg@ARM.com# property including but not limited to intellectual property relating
79401SAndreas.Sandberg@ARM.com# to a hardware implementation of the functionality of the software
89401SAndreas.Sandberg@ARM.com# licensed hereunder.  You may use the software subject to the license
99401SAndreas.Sandberg@ARM.com# terms below provided that you ensure that this notice is replicated
109401SAndreas.Sandberg@ARM.com# unmodified and in its entirety in all distributions of the software,
119401SAndreas.Sandberg@ARM.com# modified or unmodified, in source code or in binary form.
129401SAndreas.Sandberg@ARM.com#
134977Ssaidi@eecs.umich.edu# Copyright (c) 2006-2007 The Regents of The University of Michigan
142997Sstever@eecs.umich.edu# All rights reserved.
152997Sstever@eecs.umich.edu#
162997Sstever@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
172997Sstever@eecs.umich.edu# modification, are permitted provided that the following conditions are
182997Sstever@eecs.umich.edu# met: redistributions of source code must retain the above copyright
192997Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
202997Sstever@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
212997Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
222997Sstever@eecs.umich.edu# documentation and/or other materials provided with the distribution;
232997Sstever@eecs.umich.edu# neither the name of the copyright holders nor the names of its
242997Sstever@eecs.umich.edu# contributors may be used to endorse or promote products derived from
252997Sstever@eecs.umich.edu# this software without specific prior written permission.
262997Sstever@eecs.umich.edu#
272997Sstever@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
282997Sstever@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
292997Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
302997Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
312997Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
322997Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
332997Sstever@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
342997Sstever@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
352997Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
362997Sstever@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
372997Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
382997Sstever@eecs.umich.edu#
392997Sstever@eecs.umich.edu# Authors: Steve Reinhardt
402997Sstever@eecs.umich.edu
415523Snate@binkert.orgimport os
425523Snate@binkert.orgimport sys
436928SBrad.Beckmann@amd.comimport re
446928SBrad.Beckmann@amd.comimport string
456289Snate@binkert.org
466289Snate@binkert.orgfrom os.path import join as joinpath
479654SAndreas.Sandberg@ARM.comimport os.path
489654SAndreas.Sandberg@ARM.comimport os
496289Snate@binkert.org
505523Snate@binkert.orgimport m5
515523Snate@binkert.org
529401SAndreas.Sandberg@ARM.comdef skip_test(reason=""):
539401SAndreas.Sandberg@ARM.com    """Signal that a test should be skipped and optionally print why.
549401SAndreas.Sandberg@ARM.com
559401SAndreas.Sandberg@ARM.com    Keyword arguments:
569401SAndreas.Sandberg@ARM.com      reason -- Reason why the test failed. Output is omitted if empty.
579401SAndreas.Sandberg@ARM.com    """
589401SAndreas.Sandberg@ARM.com
599401SAndreas.Sandberg@ARM.com    if reason:
609401SAndreas.Sandberg@ARM.com        print "Skipping test: %s" % reason
619401SAndreas.Sandberg@ARM.com    sys.exit(2)
629401SAndreas.Sandberg@ARM.com
639401SAndreas.Sandberg@ARM.comdef has_sim_object(name):
649401SAndreas.Sandberg@ARM.com    """Test if a SimObject exists in the simulator.
659401SAndreas.Sandberg@ARM.com
669401SAndreas.Sandberg@ARM.com    Arguments:
679401SAndreas.Sandberg@ARM.com      name -- Name of SimObject (string)
689401SAndreas.Sandberg@ARM.com
699401SAndreas.Sandberg@ARM.com    Returns: True if the object exists, False otherwise.
709401SAndreas.Sandberg@ARM.com    """
719401SAndreas.Sandberg@ARM.com
729401SAndreas.Sandberg@ARM.com    try:
739401SAndreas.Sandberg@ARM.com        cls = getattr(m5.objects, name)
749401SAndreas.Sandberg@ARM.com        return issubclass(cls, m5.objects.SimObject)
759401SAndreas.Sandberg@ARM.com    except AttributeError:
769401SAndreas.Sandberg@ARM.com        return False
779401SAndreas.Sandberg@ARM.com
789401SAndreas.Sandberg@ARM.comdef require_sim_object(name, fatal=False):
799401SAndreas.Sandberg@ARM.com    """Test if a SimObject exists and abort/skip test if not.
809401SAndreas.Sandberg@ARM.com
819401SAndreas.Sandberg@ARM.com    Arguments:
829401SAndreas.Sandberg@ARM.com      name -- Name of SimObject (string)
839401SAndreas.Sandberg@ARM.com
849401SAndreas.Sandberg@ARM.com    Keyword arguments:
859401SAndreas.Sandberg@ARM.com      fatal -- Set to True to indicate that the test should fail
869401SAndreas.Sandberg@ARM.com               instead of being skipped.
879401SAndreas.Sandberg@ARM.com    """
889401SAndreas.Sandberg@ARM.com
899401SAndreas.Sandberg@ARM.com    if has_sim_object(name):
909401SAndreas.Sandberg@ARM.com        return
919401SAndreas.Sandberg@ARM.com    else:
929401SAndreas.Sandberg@ARM.com        msg = "Test requires the '%s' SimObject." % name
939401SAndreas.Sandberg@ARM.com        if fatal:
949401SAndreas.Sandberg@ARM.com            m5.fatal(msg)
959401SAndreas.Sandberg@ARM.com        else:
969401SAndreas.Sandberg@ARM.com            skip_test(msg)
979401SAndreas.Sandberg@ARM.com
989654SAndreas.Sandberg@ARM.com
999654SAndreas.Sandberg@ARM.comdef require_file(path, fatal=False, mode=os.F_OK):
1009654SAndreas.Sandberg@ARM.com    """Test if a file exists and abort/skip test if not.
1019654SAndreas.Sandberg@ARM.com
1029654SAndreas.Sandberg@ARM.com    Arguments:
1039654SAndreas.Sandberg@ARM.com      path -- File to test for.
1049654SAndreas.Sandberg@ARM.com
1059654SAndreas.Sandberg@ARM.com    Keyword arguments:
1069654SAndreas.Sandberg@ARM.com      fatal -- Set to True to indicate that the test should fail
1079654SAndreas.Sandberg@ARM.com               instead of being skipped.
1089654SAndreas.Sandberg@ARM.com      modes -- Mode to test for, default to existence. See the
1099654SAndreas.Sandberg@ARM.com               Python documentation for os.access().
1109654SAndreas.Sandberg@ARM.com    """
1119654SAndreas.Sandberg@ARM.com
1129654SAndreas.Sandberg@ARM.com    if os.access(path, mode):
1139654SAndreas.Sandberg@ARM.com        return
1149654SAndreas.Sandberg@ARM.com    else:
1159654SAndreas.Sandberg@ARM.com        msg = "Test requires '%s'" % path
1169654SAndreas.Sandberg@ARM.com        if not os.path.exists(path):
1179654SAndreas.Sandberg@ARM.com            msg += " which does not exist."
1189654SAndreas.Sandberg@ARM.com        else:
1199654SAndreas.Sandberg@ARM.com            msg += " which has incorrect permissions."
1209654SAndreas.Sandberg@ARM.com
1219654SAndreas.Sandberg@ARM.com        if fatal:
1229654SAndreas.Sandberg@ARM.com            m5.fatal(msg)
1239654SAndreas.Sandberg@ARM.com        else:
1249654SAndreas.Sandberg@ARM.com            skip_test(msg)
1259654SAndreas.Sandberg@ARM.com
1269654SAndreas.Sandberg@ARM.comdef require_kvm(kvm_dev="/dev/kvm", fatal=False):
1279654SAndreas.Sandberg@ARM.com    """Test if KVM is available.
1289654SAndreas.Sandberg@ARM.com
1299654SAndreas.Sandberg@ARM.com    Keyword arguments:
1309654SAndreas.Sandberg@ARM.com      kvm_dev -- Device to test (normally /dev/kvm)
1319654SAndreas.Sandberg@ARM.com      fatal -- Set to True to indicate that the test should fail
1329654SAndreas.Sandberg@ARM.com               instead of being skipped.
1339654SAndreas.Sandberg@ARM.com    """
1349654SAndreas.Sandberg@ARM.com
1359654SAndreas.Sandberg@ARM.com    require_sim_object("BaseKvmCPU", fatal=fatal)
1369654SAndreas.Sandberg@ARM.com    require_file(kvm_dev, fatal=fatal, mode=os.R_OK | os.W_OK)
1379654SAndreas.Sandberg@ARM.com
1389447SAndreas.Sandberg@ARM.comdef run_test(root):
1399447SAndreas.Sandberg@ARM.com    """Default run_test implementations. Scripts can override it."""
1409447SAndreas.Sandberg@ARM.com
1419447SAndreas.Sandberg@ARM.com    # instantiate configuration
1429447SAndreas.Sandberg@ARM.com    m5.instantiate()
1439447SAndreas.Sandberg@ARM.com
1449447SAndreas.Sandberg@ARM.com    # simulate until program terminates
1459447SAndreas.Sandberg@ARM.com    exit_event = m5.simulate(maxtick)
1469447SAndreas.Sandberg@ARM.com    print 'Exiting @ tick', m5.curTick(), 'because', exit_event.getCause()
1479447SAndreas.Sandberg@ARM.com
1485523Snate@binkert.org# Since we're in batch mode, dont allow tcp socket connections
1495523Snate@binkert.orgm5.disableAllListeners()
1502997Sstever@eecs.umich.edu
1512997Sstever@eecs.umich.edu# single "path" arg encodes everything we need to know about test
1528802Sgblack@eecs.umich.edu(category, mode, name, isa, opsys, config) = sys.argv[1].split('/')[-6:]
1532997Sstever@eecs.umich.edu
1542997Sstever@eecs.umich.edu# find path to directory containing this file
1552997Sstever@eecs.umich.edutests_root = os.path.dirname(__file__)
1566874SSteve.Reinhardt@amd.comtest_progs = os.environ.get('M5_TEST_PROGS', '/dist/m5/regression/test-progs')
1576874SSteve.Reinhardt@amd.comif not os.path.isdir(test_progs):
1586289Snate@binkert.org    test_progs = joinpath(tests_root, 'test-progs')
1592998Sstever@eecs.umich.edu
1602998Sstever@eecs.umich.edu# generate path to binary file
1612998Sstever@eecs.umich.edudef binpath(app, file=None):
1622998Sstever@eecs.umich.edu    # executable has same name as app unless specified otherwise
1632998Sstever@eecs.umich.edu    if not file:
1642998Sstever@eecs.umich.edu        file = app
1656289Snate@binkert.org    return joinpath(test_progs, app, 'bin', isa, opsys, file)
1662997Sstever@eecs.umich.edu
1673475Sktlim@umich.edu# generate path to input file
1683475Sktlim@umich.edudef inputpath(app, file=None):
1693475Sktlim@umich.edu    # input file has same name as app unless specified otherwise
1703475Sktlim@umich.edu    if not file:
1713475Sktlim@umich.edu        file = app
1726289Snate@binkert.org    return joinpath(test_progs, app, 'input', file)
1733475Sktlim@umich.edu
1742997Sstever@eecs.umich.edu# build configuration
1756289Snate@binkert.orgsys.path.append(joinpath(tests_root, 'configs'))
1766928SBrad.Beckmann@amd.comtest_filename = config
1776928SBrad.Beckmann@amd.com# for ruby configurations, remove the protocol name from the test filename
1786928SBrad.Beckmann@amd.comif re.search('-ruby', test_filename):
1796928SBrad.Beckmann@amd.com    test_filename = test_filename.split('-ruby')[0]+'-ruby'
1806928SBrad.Beckmann@amd.comexecfile(joinpath(tests_root, 'configs', test_filename + '.py'))
1812997Sstever@eecs.umich.edu
1822997Sstever@eecs.umich.edu# set default maxtick... script can override
1832998Sstever@eecs.umich.edu# -1 means run forever
1845523Snate@binkert.orgmaxtick = m5.MaxTick
1852997Sstever@eecs.umich.edu
1862997Sstever@eecs.umich.edu# tweak configuration for specific test
1878802Sgblack@eecs.umich.edusys.path.append(joinpath(tests_root, category, mode, name))
1888802Sgblack@eecs.umich.eduexecfile(joinpath(tests_root, category, mode, name, 'test.py'))
1892997Sstever@eecs.umich.edu
1909384SAndreas.Sandberg@arm.com# Initialize all CPUs in a system
1919384SAndreas.Sandberg@arm.comdef initCPUs(sys):
1929384SAndreas.Sandberg@arm.com    def initCPU(cpu):
1939384SAndreas.Sandberg@arm.com        # We might actually have a MemTest object or something similar
1949384SAndreas.Sandberg@arm.com        # here that just pretends to be a CPU.
1959851Sandreas.hansson@arm.com        try:
1969384SAndreas.Sandberg@arm.com            cpu.createThreads()
1979851Sandreas.hansson@arm.com        except:
1989851Sandreas.hansson@arm.com            pass
1999384SAndreas.Sandberg@arm.com
2009384SAndreas.Sandberg@arm.com    # The CPU attribute doesn't exist in some cases, e.g. the Ruby
2019384SAndreas.Sandberg@arm.com    # testers.
2029384SAndreas.Sandberg@arm.com    if not hasattr(sys, "cpu"):
2039384SAndreas.Sandberg@arm.com        return
2049384SAndreas.Sandberg@arm.com
2059384SAndreas.Sandberg@arm.com    # The CPU can either be a list of CPUs or a single object.
2069384SAndreas.Sandberg@arm.com    if isinstance(sys.cpu, list):
2079384SAndreas.Sandberg@arm.com        [ initCPU(cpu) for cpu in sys.cpu ]
2089384SAndreas.Sandberg@arm.com    else:
2099384SAndreas.Sandberg@arm.com        initCPU(sys.cpu)
2109384SAndreas.Sandberg@arm.com
2119384SAndreas.Sandberg@arm.com# We might be creating a single system or a dual system. Try
2129384SAndreas.Sandberg@arm.com# initializing the CPUs in all known system attributes.
2139384SAndreas.Sandberg@arm.comfor sysattr in [ "system", "testsys", "drivesys" ]:
2149384SAndreas.Sandberg@arm.com    if hasattr(root, sysattr):
2159384SAndreas.Sandberg@arm.com        initCPUs(getattr(root, sysattr))
2169384SAndreas.Sandberg@arm.com
2179447SAndreas.Sandberg@ARM.comrun_test(root)
218