1# Copyright (c) 2017 Mark D. Hill and David A. Wood
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Sean Wilson
28
29'''
30New version of the run.py script. For this, all dependencies should be
31handled outside of the script.
32
33.. warning:: This script is NOT the recommended way to handle configurations
34    for new tests. This exists for legacy support only. New Tests should
35    either use configs from the normal gem5 configs or create their own for
36    a test.
37'''
38import argparse
39import sys
40import os
41from os.path import abspath, join as joinpath, dirname
42
43import m5
44
45# Add the normal gem5 config path to system path.
46# This requirement should be removed if possible from all legacy scripts, but
47# I've left it here for now.
48sys.path.insert(0, abspath(joinpath(dirname(__file__), '../../configs')))
49
50# set default maxtick... script can override
51# -1 means run forever
52maxtick = m5.MaxTick
53
54def run_test(root):
55    """Default run_test implementations. Scripts can override it."""
56
57    # instantiate configuration
58    m5.instantiate()
59
60    # simulate until program terminates
61    exit_event = m5.simulate(maxtick)
62    print 'Exiting @ tick', m5.curTick(), 'because', exit_event.getCause()
63
64test_progs = os.environ.get('M5_TEST_PROGS', '/dist/m5/regression/test-progs')
65
66# Since we're in batch mode, dont allow tcp socket connections
67m5.disableAllListeners()
68
69parser = argparse.ArgumentParser()
70parser.add_argument('--cmd',
71                    action='store',
72                    type=str,
73                    help='Command to pass to the test system')
74parser.add_argument('--executable',
75                    action='store',
76                    type=str,
77                    help='Executable to pass to the test system')
78parser.add_argument('--config',
79                    action='append',
80                    type=str,
81                    help='A config file to initialize the system with.'\
82                    + ' If more than one given, loads them in order given.')
83args = parser.parse_args()
84
85executable = args.executable
86
87for config in args.config:
88    exec(compile(open(config).read(), config, 'exec'))
89
90# Initialize all CPUs in a system
91def initCPUs(sys):
92    def initCPU(cpu):
93        # We might actually have a MemTest object or something similar
94        # here that just pretends to be a CPU.
95        try:
96            cpu.createThreads()
97        except:
98            pass
99
100    # The CPU attribute doesn't exist in some cases, e.g. the Ruby testers.
101    if not hasattr(sys, "cpu"):
102        return
103
104    # The CPU can either be a list of CPUs or a single object.
105    if isinstance(sys.cpu, list):
106        [ initCPU(cpu) for cpu in sys.cpu ]
107    else:
108        initCPU(sys.cpu)
109
110# TODO: Might want to automatically place the cmd and executable on the
111# cpu[0].workload, although I think most legacy configs do this automatically
112# or somewhere in their `test.py` config.
113
114
115# We might be creating a single system or a dual system. Try
116# initializing the CPUs in all known system attributes.
117for sysattr in [ "system", "testsys", "drivesys" ]:
118    if hasattr(root, sysattr):
119        initCPUs(getattr(root, sysattr))
120
121run_test(root)
122