main.py revision 8333
12889Sbinkertn@umich.edu# Copyright (c) 2005 The Regents of The University of Michigan
22889Sbinkertn@umich.edu# All rights reserved.
32889Sbinkertn@umich.edu#
42889Sbinkertn@umich.edu# Redistribution and use in source and binary forms, with or without
52889Sbinkertn@umich.edu# modification, are permitted provided that the following conditions are
62889Sbinkertn@umich.edu# met: redistributions of source code must retain the above copyright
72889Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer;
82889Sbinkertn@umich.edu# redistributions in binary form must reproduce the above copyright
92889Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer in the
102889Sbinkertn@umich.edu# documentation and/or other materials provided with the distribution;
112889Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its
122889Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from
132889Sbinkertn@umich.edu# this software without specific prior written permission.
142889Sbinkertn@umich.edu#
152889Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
162889Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
172889Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
182889Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
192889Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
202889Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
212889Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222889Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232889Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242889Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
252889Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262889Sbinkertn@umich.edu#
272889Sbinkertn@umich.edu# Authors: Nathan Binkert
282889Sbinkertn@umich.edu
294850Snate@binkert.orgimport code
304850Snate@binkert.orgimport datetime
314850Snate@binkert.orgimport os
324850Snate@binkert.orgimport socket
334850Snate@binkert.orgimport sys
344850Snate@binkert.org
352889Sbinkertn@umich.edu__all__ = [ 'options', 'arguments', 'main' ]
362889Sbinkertn@umich.edu
378327Sgblack@eecs.umich.eduusage="%prog [gem5 options] script.py [script options]"
385470Snate@binkert.orgversion="%prog 2.0"
398333Snate@binkert.orgbrief_copyright=\
408333Snate@binkert.org    "gem5 is copyrighted software; use the --copyright option for details."
412889Sbinkertn@umich.edu
428234Snate@binkert.orgdef parse_options():
438234Snate@binkert.org    import config
448234Snate@binkert.org    from options import OptionParser
452889Sbinkertn@umich.edu
468234Snate@binkert.org    options = OptionParser(usage=usage, version=version,
478234Snate@binkert.org                           description=brief_copyright)
488234Snate@binkert.org    option = options.add_option
498234Snate@binkert.org    group = options.set_group
502889Sbinkertn@umich.edu
518234Snate@binkert.org    # Help options
528234Snate@binkert.org    option('-B', "--build-info", action="store_true", default=False,
538234Snate@binkert.org        help="Show build information")
548234Snate@binkert.org    option('-C', "--copyright", action="store_true", default=False,
558234Snate@binkert.org        help="Show full copyright information")
568234Snate@binkert.org    option('-R', "--readme", action="store_true", default=False,
578234Snate@binkert.org        help="Show the readme")
582889Sbinkertn@umich.edu
598234Snate@binkert.org    # Options for configuring the base simulator
608234Snate@binkert.org    option('-d', "--outdir", metavar="DIR", default="m5out",
618234Snate@binkert.org        help="Set the output directory to DIR [Default: %default]")
628234Snate@binkert.org    option('-r', "--redirect-stdout", action="store_true", default=False,
638234Snate@binkert.org        help="Redirect stdout (& stderr, without -e) to file")
648234Snate@binkert.org    option('-e', "--redirect-stderr", action="store_true", default=False,
658234Snate@binkert.org        help="Redirect stderr to file")
668234Snate@binkert.org    option("--stdout-file", metavar="FILE", default="simout",
678234Snate@binkert.org        help="Filename for -r redirection [Default: %default]")
688234Snate@binkert.org    option("--stderr-file", metavar="FILE", default="simerr",
698234Snate@binkert.org        help="Filename for -e redirection [Default: %default]")
708234Snate@binkert.org    option('-i', "--interactive", action="store_true", default=False,
718234Snate@binkert.org        help="Invoke the interactive interpreter after running the script")
728234Snate@binkert.org    option("--pdb", action="store_true", default=False,
738234Snate@binkert.org        help="Invoke the python debugger before running the script")
748234Snate@binkert.org    option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
758234Snate@binkert.org        help="Prepend PATH to the system path when invoking the script")
768234Snate@binkert.org    option('-q', "--quiet", action="count", default=0,
778234Snate@binkert.org        help="Reduce verbosity")
788234Snate@binkert.org    option('-v', "--verbose", action="count", default=0,
798234Snate@binkert.org        help="Increase verbosity")
802889Sbinkertn@umich.edu
818234Snate@binkert.org    # Statistics options
828234Snate@binkert.org    group("Statistics Options")
838234Snate@binkert.org    option("--stats-file", metavar="FILE", default="stats.txt",
848234Snate@binkert.org        help="Sets the output file for statistics [Default: %default]")
855773Snate@binkert.org
868234Snate@binkert.org    # Configuration Options
878234Snate@binkert.org    group("Configuration Options")
888234Snate@binkert.org    option("--dump-config", metavar="FILE", default="config.ini",
898234Snate@binkert.org        help="Dump configuration output file [Default: %default]")
902889Sbinkertn@umich.edu
918234Snate@binkert.org    # Debugging options
928234Snate@binkert.org    group("Debugging Options")
938234Snate@binkert.org    option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
948234Snate@binkert.org        help="Cycle to create a breakpoint")
958234Snate@binkert.org    option("--debug-help", action='store_true',
968234Snate@binkert.org        help="Print help on trace flags")
978234Snate@binkert.org    option("--debug-flags", metavar="FLAG[,FLAG]", action='append', split=',',
988234Snate@binkert.org        help="Sets the flags for tracing (-FLAG disables a flag)")
998234Snate@binkert.org    option("--remote-gdb-port", type='int', default=7000,
1008234Snate@binkert.org        help="Remote gdb base port (set to 0 to disable listening)")
1012889Sbinkertn@umich.edu
1028234Snate@binkert.org    # Tracing options
1038234Snate@binkert.org    group("Trace Options")
1048234Snate@binkert.org    option("--trace-start", metavar="TIME", type='int',
1058234Snate@binkert.org        help="Start tracing at TIME (must be in ticks)")
1068234Snate@binkert.org    option("--trace-file", metavar="FILE", default="cout",
1078234Snate@binkert.org        help="Sets the output file for tracing [Default: %default]")
1088234Snate@binkert.org    option("--trace-ignore", metavar="EXPR", action='append', split=':',
1098234Snate@binkert.org        help="Ignore EXPR sim objects")
1105473Snate@binkert.org
1118234Snate@binkert.org    # Help options
1128234Snate@binkert.org    group("Help Options")
1138234Snate@binkert.org    option("--list-sim-objects", action='store_true', default=False,
1148234Snate@binkert.org        help="List all built-in SimObjects, their params and default values")
1156171Snate@binkert.org
1168234Snate@binkert.org    # load the options.py config file to allow people to set their own
1178234Snate@binkert.org    # default options
1188234Snate@binkert.org    options_file = config.get('options.py')
1198234Snate@binkert.org    if options_file:
1208234Snate@binkert.org        scope = { 'options' : options }
1218234Snate@binkert.org        execfile(options_file, scope)
1228234Snate@binkert.org
1238234Snate@binkert.org    arguments = options.parse_args()
1248234Snate@binkert.org
1258234Snate@binkert.org    return options,arguments
1266171Snate@binkert.org
1278219Snate@binkert.orgdef interact(scope):
1288327Sgblack@eecs.umich.edu    banner = "gem5 Interactive Console"
1298219Snate@binkert.org    sys.argv = []
1308219Snate@binkert.org    try:
1318219Snate@binkert.org        from IPython.Shell import IPShellEmbed
1328219Snate@binkert.org        ipshell = IPShellEmbed(banner=banner,user_ns=scope)
1338219Snate@binkert.org        ipshell()
1348219Snate@binkert.org    except ImportError:
1358219Snate@binkert.org        code.InteractiveConsole(scope).interact(banner)
1368219Snate@binkert.org
1378234Snate@binkert.orgdef main(*args):
1388245Snate@binkert.org    import m5
1398245Snate@binkert.org
1405801Snate@binkert.org    import core
1415801Snate@binkert.org    import debug
1425801Snate@binkert.org    import defines
1434167Sbinkertn@umich.edu    import event
1444042Sbinkertn@umich.edu    import info
1455801Snate@binkert.org    import stats
1465799Snate@binkert.org    import trace
1475799Snate@binkert.org
1488234Snate@binkert.org    from util import fatal
1498234Snate@binkert.org
1508234Snate@binkert.org    if len(args) == 0:
1518234Snate@binkert.org        options, arguments = parse_options()
1528234Snate@binkert.org    elif len(args) == 2:
1538234Snate@binkert.org        options, arguments = args
1548234Snate@binkert.org    else:
1558234Snate@binkert.org        raise TypeError, "main() takes 0 or 2 arguments (%d given)" % len(args)
1568234Snate@binkert.org
1578245Snate@binkert.org    m5.options = options
1588245Snate@binkert.org
1595799Snate@binkert.org    def check_tracing():
1605799Snate@binkert.org        if defines.TRACING_ON:
1615799Snate@binkert.org            return
1625799Snate@binkert.org
1635802Snate@binkert.org        fatal("Tracing is not enabled.  Compile with TRACING_ON")
1642889Sbinkertn@umich.edu
1655524Sstever@gmail.com    if not os.path.isdir(options.outdir):
1665524Sstever@gmail.com        os.makedirs(options.outdir)
1675524Sstever@gmail.com
1685524Sstever@gmail.com    # These filenames are used only if the redirect_std* options are set
1695524Sstever@gmail.com    stdout_file = os.path.join(options.outdir, options.stdout_file)
1705524Sstever@gmail.com    stderr_file = os.path.join(options.outdir, options.stderr_file)
1715524Sstever@gmail.com
1725524Sstever@gmail.com    # Print redirection notices here before doing any redirection
1735524Sstever@gmail.com    if options.redirect_stdout and not options.redirect_stderr:
1745524Sstever@gmail.com        print "Redirecting stdout and stderr to", stdout_file
1755524Sstever@gmail.com    else:
1765524Sstever@gmail.com        if options.redirect_stdout:
1775524Sstever@gmail.com            print "Redirecting stdout to", stdout_file
1785524Sstever@gmail.com        if options.redirect_stderr:
1795524Sstever@gmail.com            print "Redirecting stderr to", stderr_file
1805524Sstever@gmail.com
1815524Sstever@gmail.com    # Now redirect stdout/stderr as desired
1825524Sstever@gmail.com    if options.redirect_stdout:
1835524Sstever@gmail.com        redir_fd = os.open(stdout_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
1845524Sstever@gmail.com        os.dup2(redir_fd, sys.stdout.fileno())
1855524Sstever@gmail.com        if not options.redirect_stderr:
1865524Sstever@gmail.com            os.dup2(redir_fd, sys.stderr.fileno())
1875524Sstever@gmail.com
1885524Sstever@gmail.com    if options.redirect_stderr:
1895524Sstever@gmail.com        redir_fd = os.open(stderr_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
1905524Sstever@gmail.com        os.dup2(redir_fd, sys.stderr.fileno())
1915524Sstever@gmail.com
1922889Sbinkertn@umich.edu    done = False
1934850Snate@binkert.org
1944850Snate@binkert.org    if options.build_info:
1954850Snate@binkert.org        done = True
1964850Snate@binkert.org        print 'Build information:'
1974850Snate@binkert.org        print
1985801Snate@binkert.org        print 'compiled %s' % defines.compileDate;
1994850Snate@binkert.org        print 'build options:'
2005801Snate@binkert.org        keys = defines.buildEnv.keys()
2014850Snate@binkert.org        keys.sort()
2024850Snate@binkert.org        for key in keys:
2035801Snate@binkert.org            val = defines.buildEnv[key]
2044850Snate@binkert.org            print '    %s = %s' % (key, val)
2054850Snate@binkert.org        print
2064850Snate@binkert.org
2072889Sbinkertn@umich.edu    if options.copyright:
2082889Sbinkertn@umich.edu        done = True
2098333Snate@binkert.org        print info.COPYING
2102889Sbinkertn@umich.edu        print
2112889Sbinkertn@umich.edu
2122889Sbinkertn@umich.edu    if options.readme:
2132889Sbinkertn@umich.edu        done = True
2142889Sbinkertn@umich.edu        print 'Readme:'
2152889Sbinkertn@umich.edu        print
2162889Sbinkertn@umich.edu        print info.README
2172889Sbinkertn@umich.edu        print
2182889Sbinkertn@umich.edu
2198232Snate@binkert.org    if options.debug_help:
2204053Sbinkertn@umich.edu        done = True
2215799Snate@binkert.org        check_tracing()
2228232Snate@binkert.org        debug.help()
2234053Sbinkertn@umich.edu
2245473Snate@binkert.org    if options.list_sim_objects:
2255473Snate@binkert.org        import SimObject
2265473Snate@binkert.org        done = True
2275473Snate@binkert.org        print "SimObjects:"
2285473Snate@binkert.org        objects = SimObject.allClasses.keys()
2295473Snate@binkert.org        objects.sort()
2305473Snate@binkert.org        for name in objects:
2315473Snate@binkert.org            obj = SimObject.allClasses[name]
2325473Snate@binkert.org            print "    %s" % obj
2335473Snate@binkert.org            params = obj._params.keys()
2345473Snate@binkert.org            params.sort()
2355473Snate@binkert.org            for pname in params:
2365473Snate@binkert.org                param = obj._params[pname]
2375473Snate@binkert.org                default = getattr(param, 'default', '')
2385473Snate@binkert.org                print "        %s" % pname
2395473Snate@binkert.org                if default:
2405473Snate@binkert.org                    print "            default: %s" % default
2415473Snate@binkert.org                print "            desc: %s" % param.desc
2425473Snate@binkert.org                print
2435473Snate@binkert.org            print
2445473Snate@binkert.org
2452889Sbinkertn@umich.edu    if done:
2462889Sbinkertn@umich.edu        sys.exit(0)
2472889Sbinkertn@umich.edu
2485470Snate@binkert.org    # setting verbose and quiet at the same time doesn't make sense
2495470Snate@binkert.org    if options.verbose > 0 and options.quiet > 0:
2505470Snate@binkert.org        options.usage(2)
2515470Snate@binkert.org
2525470Snate@binkert.org    verbose = options.verbose - options.quiet
2532889Sbinkertn@umich.edu    if options.verbose >= 0:
2548333Snate@binkert.org        print "gem5 Simulator System.  http://gem5.org"
2552889Sbinkertn@umich.edu        print brief_copyright
2562889Sbinkertn@umich.edu        print
2575801Snate@binkert.org
2588327Sgblack@eecs.umich.edu        print "gem5 compiled %s" % defines.compileDate;
2595456Ssaidi@eecs.umich.edu
2608327Sgblack@eecs.umich.edu        print "gem5 started %s" % \
2618327Sgblack@eecs.umich.edu            datetime.datetime.now().strftime("%b %e %Y %X")
2628327Sgblack@eecs.umich.edu        print "gem5 executing on %s" % socket.gethostname()
2635528Sstever@gmail.com
2642967Sktlim@umich.edu        print "command line:",
2652967Sktlim@umich.edu        for argv in sys.argv:
2662967Sktlim@umich.edu            print argv,
2672967Sktlim@umich.edu        print
2682889Sbinkertn@umich.edu
2692889Sbinkertn@umich.edu    # check to make sure we can find the listed script
2702889Sbinkertn@umich.edu    if not arguments or not os.path.isfile(arguments[0]):
2712922Sktlim@umich.edu        if arguments and not os.path.isfile(arguments[0]):
2722922Sktlim@umich.edu            print "Script %s not found" % arguments[0]
2734053Sbinkertn@umich.edu
2745470Snate@binkert.org        options.usage(2)
2752889Sbinkertn@umich.edu
2762889Sbinkertn@umich.edu    # tell C++ about output directory
2775801Snate@binkert.org    core.setOutputDir(options.outdir)
2782889Sbinkertn@umich.edu
2792889Sbinkertn@umich.edu    # update the system path with elements from the -p option
2802889Sbinkertn@umich.edu    sys.path[0:0] = options.path
2812889Sbinkertn@umich.edu
2822889Sbinkertn@umich.edu    # set stats options
2835801Snate@binkert.org    stats.initText(options.stats_file)
2842889Sbinkertn@umich.edu
2852889Sbinkertn@umich.edu    # set debugging options
2865801Snate@binkert.org    debug.setRemoteGDBPort(options.remote_gdb_port)
2873645Sbinkertn@umich.edu    for when in options.debug_break:
2885801Snate@binkert.org        debug.schedBreakCycle(int(when))
2892889Sbinkertn@umich.edu
2908232Snate@binkert.org    if options.debug_flags:
2915799Snate@binkert.org        check_tracing()
2924053Sbinkertn@umich.edu
2935586Snate@binkert.org        on_flags = []
2945586Snate@binkert.org        off_flags = []
2958232Snate@binkert.org        for flag in options.debug_flags:
2965586Snate@binkert.org            off = False
2975586Snate@binkert.org            if flag.startswith('-'):
2985586Snate@binkert.org                flag = flag[1:]
2995586Snate@binkert.org                off = True
3008232Snate@binkert.org
3018232Snate@binkert.org            if flag not in debug.flags:
3028232Snate@binkert.org                print >>sys.stderr, "invalid debug flag '%s'" % flag
3035586Snate@binkert.org                sys.exit(1)
3044053Sbinkertn@umich.edu
3055586Snate@binkert.org            if off:
3068232Snate@binkert.org                debug.flags[flag].disable()
3075586Snate@binkert.org            else:
3088232Snate@binkert.org                debug.flags[flag].enable()
3094053Sbinkertn@umich.edu
3104074Sbinkertn@umich.edu    if options.trace_start:
3115799Snate@binkert.org        check_tracing()
3125950Ssaidi@eecs.umich.edu        e = event.create(trace.enable, event.Event.Trace_Enable_Pri)
3135606Snate@binkert.org        event.mainq.schedule(e, options.trace_start)
3144074Sbinkertn@umich.edu    else:
3155799Snate@binkert.org        trace.enable()
3164042Sbinkertn@umich.edu
3175799Snate@binkert.org    trace.output(options.trace_file)
3184042Sbinkertn@umich.edu
3194042Sbinkertn@umich.edu    for ignore in options.trace_ignore:
3205799Snate@binkert.org        check_tracing()
3215799Snate@binkert.org        trace.ignore(ignore)
3222889Sbinkertn@umich.edu
3232889Sbinkertn@umich.edu    sys.argv = arguments
3242889Sbinkertn@umich.edu    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
3252891Sbinkertn@umich.edu
3265604Snate@binkert.org    filename = sys.argv[0]
3275604Snate@binkert.org    filedata = file(filename, 'r').read()
3285604Snate@binkert.org    filecode = compile(filedata, filename, 'exec')
3295604Snate@binkert.org    scope = { '__file__' : filename,
3303887Sbinkertn@umich.edu              '__name__' : '__m5_main__' }
3312899Sbinkertn@umich.edu
3322899Sbinkertn@umich.edu    # we want readline if we're doing anything interactive
3332899Sbinkertn@umich.edu    if options.interactive or options.pdb:
3344042Sbinkertn@umich.edu        exec "import readline" in scope
3352899Sbinkertn@umich.edu
3362899Sbinkertn@umich.edu    # if pdb was requested, execfile the thing under pdb, otherwise,
3372899Sbinkertn@umich.edu    # just do the execfile normally
3382899Sbinkertn@umich.edu    if options.pdb:
3395604Snate@binkert.org        import pdb
3405604Snate@binkert.org        import traceback
3415604Snate@binkert.org
3425604Snate@binkert.org        pdb = pdb.Pdb()
3435604Snate@binkert.org        try:
3445604Snate@binkert.org            pdb.run(filecode, scope)
3455604Snate@binkert.org        except SystemExit:
3465604Snate@binkert.org            print "The program exited via sys.exit(). Exit status: ",
3475604Snate@binkert.org            print sys.exc_info()[1]
3485604Snate@binkert.org        except:
3495604Snate@binkert.org            traceback.print_exc()
3505604Snate@binkert.org            print "Uncaught exception. Entering post mortem debugging"
3515604Snate@binkert.org            t = sys.exc_info()[2]
3525604Snate@binkert.org            while t.tb_next is not None:
3535604Snate@binkert.org                t = t.tb_next
3545604Snate@binkert.org                pdb.interaction(t.tb_frame,t)
3552899Sbinkertn@umich.edu    else:
3565604Snate@binkert.org        exec filecode in scope
3572889Sbinkertn@umich.edu
3582889Sbinkertn@umich.edu    # once the script is done
3592889Sbinkertn@umich.edu    if options.interactive:
3608219Snate@binkert.org        interact(scope)
3612889Sbinkertn@umich.edu
3622889Sbinkertn@umich.eduif __name__ == '__main__':
3632889Sbinkertn@umich.edu    from pprint import pprint
3642889Sbinkertn@umich.edu
3658234Snate@binkert.org    options, arguments = parse_options()
3668234Snate@binkert.org
3672889Sbinkertn@umich.edu    print 'opts:'
3682889Sbinkertn@umich.edu    pprint(options, indent=4)
3692889Sbinkertn@umich.edu    print
3702889Sbinkertn@umich.edu
3712889Sbinkertn@umich.edu    print 'args:'
3722889Sbinkertn@umich.edu    pprint(arguments, indent=4)
373