main.py revision 8219
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
356654Snate@binkert.orgfrom util import attrdict, fatal
365471Snate@binkert.orgimport config
375470Snate@binkert.orgfrom options import OptionParser
382889Sbinkertn@umich.edu
392889Sbinkertn@umich.edu__all__ = [ 'options', 'arguments', 'main' ]
402889Sbinkertn@umich.edu
415470Snate@binkert.orgusage="%prog [m5 options] script.py [script options]"
425470Snate@binkert.orgversion="%prog 2.0"
435470Snate@binkert.orgbrief_copyright='''
445470Snate@binkert.orgCopyright (c) 2001-2008
455470Snate@binkert.orgThe Regents of The University of Michigan
465470Snate@binkert.orgAll Rights Reserved
475470Snate@binkert.org'''
482889Sbinkertn@umich.edu
495470Snate@binkert.orgoptions = OptionParser(usage=usage, version=version,
505470Snate@binkert.org                       description=brief_copyright)
515470Snate@binkert.orgadd_option = options.add_option
525470Snate@binkert.orgset_group = options.set_group
535470Snate@binkert.orgusage = options.usage
542889Sbinkertn@umich.edu
552889Sbinkertn@umich.edu# Help options
562889Sbinkertn@umich.eduadd_option('-A', "--authors", action="store_true", default=False,
572889Sbinkertn@umich.edu    help="Show author information")
584850Snate@binkert.orgadd_option('-B', "--build-info", action="store_true", default=False,
594850Snate@binkert.org    help="Show build information")
602889Sbinkertn@umich.eduadd_option('-C', "--copyright", action="store_true", default=False,
612889Sbinkertn@umich.edu    help="Show full copyright information")
622889Sbinkertn@umich.eduadd_option('-R', "--readme", action="store_true", default=False,
632889Sbinkertn@umich.edu    help="Show the readme")
642889Sbinkertn@umich.edu
652889Sbinkertn@umich.edu# Options for configuring the base simulator
665773Snate@binkert.orgadd_option('-d', "--outdir", metavar="DIR", default="m5out",
672889Sbinkertn@umich.edu    help="Set the output directory to DIR [Default: %default]")
685524Sstever@gmail.comadd_option('-r', "--redirect-stdout", action="store_true", default=False,
695524Sstever@gmail.com           help="Redirect stdout (& stderr, without -e) to file")
705524Sstever@gmail.comadd_option('-e', "--redirect-stderr", action="store_true", default=False,
715524Sstever@gmail.com           help="Redirect stderr to file")
725524Sstever@gmail.comadd_option("--stdout-file", metavar="FILE", default="simout",
735524Sstever@gmail.com           help="Filename for -r redirection [Default: %default]")
745524Sstever@gmail.comadd_option("--stderr-file", metavar="FILE", default="simerr",
755524Sstever@gmail.com           help="Filename for -e redirection [Default: %default]")
762889Sbinkertn@umich.eduadd_option('-i', "--interactive", action="store_true", default=False,
772889Sbinkertn@umich.edu    help="Invoke the interactive interpreter after running the script")
782899Sbinkertn@umich.eduadd_option("--pdb", action="store_true", default=False,
792899Sbinkertn@umich.edu    help="Invoke the python debugger before running the script")
802889Sbinkertn@umich.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
812889Sbinkertn@umich.edu    help="Prepend PATH to the system path when invoking the script")
822889Sbinkertn@umich.eduadd_option('-q', "--quiet", action="count", default=0,
832889Sbinkertn@umich.edu    help="Reduce verbosity")
842889Sbinkertn@umich.eduadd_option('-v', "--verbose", action="count", default=0,
852889Sbinkertn@umich.edu    help="Increase verbosity")
862889Sbinkertn@umich.edu
872889Sbinkertn@umich.edu# Statistics options
882889Sbinkertn@umich.eduset_group("Statistics Options")
895773Snate@binkert.orgadd_option("--stats-file", metavar="FILE", default="stats.txt",
902889Sbinkertn@umich.edu    help="Sets the output file for statistics [Default: %default]")
912889Sbinkertn@umich.edu
925773Snate@binkert.org# Configuration Options
935773Snate@binkert.orgset_group("Configuration Options")
945773Snate@binkert.orgadd_option("--dump-config", metavar="FILE", default="config.ini",
955773Snate@binkert.org    help="Dump configuration output file [Default: %default]")
965773Snate@binkert.org
972889Sbinkertn@umich.edu# Debugging options
982889Sbinkertn@umich.eduset_group("Debugging Options")
992889Sbinkertn@umich.eduadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
1002889Sbinkertn@umich.edu    help="Cycle to create a breakpoint")
1015512SMichael.Adler@intel.comadd_option("--remote-gdb-port", type='int', default=7000,
1027445Ssteve.reinhardt@amd.com    help="Remote gdb base port (set to 0 to disable listening)")
1032889Sbinkertn@umich.edu
1042889Sbinkertn@umich.edu# Tracing options
1052889Sbinkertn@umich.eduset_group("Trace Options")
1064053Sbinkertn@umich.eduadd_option("--trace-help", action='store_true',
1074053Sbinkertn@umich.edu    help="Print help on trace flags")
1082889Sbinkertn@umich.eduadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
1094053Sbinkertn@umich.edu    help="Sets the flags for tracing (-FLAG disables a flag)")
1104044Sbinkertn@umich.eduadd_option("--trace-start", metavar="TIME", type='int',
1114044Sbinkertn@umich.edu    help="Start tracing at TIME (must be in ticks)")
1122889Sbinkertn@umich.eduadd_option("--trace-file", metavar="FILE", default="cout",
1132889Sbinkertn@umich.edu    help="Sets the output file for tracing [Default: %default]")
1142889Sbinkertn@umich.eduadd_option("--trace-ignore", metavar="EXPR", action='append', split=':',
1152889Sbinkertn@umich.edu    help="Ignore EXPR sim objects")
1162889Sbinkertn@umich.edu
1175473Snate@binkert.org# Help options
1185473Snate@binkert.orgset_group("Help Options")
1195473Snate@binkert.orgadd_option("--list-sim-objects", action='store_true', default=False,
1205473Snate@binkert.org    help="List all built-in SimObjects, their parameters and default values")
1215473Snate@binkert.org
1226171Snate@binkert.org# load the options.py config file to allow people to set their own
1236171Snate@binkert.org# default options
1246171Snate@binkert.orgoptions_file = config.get('options.py')
1256171Snate@binkert.orgif options_file:
1266171Snate@binkert.org    scope = { 'options' : options }
1276171Snate@binkert.org    execfile(options_file, scope)
1286171Snate@binkert.org
1296171Snate@binkert.orgarguments = options.parse_args()
1306171Snate@binkert.org
1318219Snate@binkert.orgdef interact(scope):
1328219Snate@binkert.org    banner = "M5 Interactive Console"
1338219Snate@binkert.org    sys.argv = []
1348219Snate@binkert.org    try:
1358219Snate@binkert.org        from IPython.Shell import IPShellEmbed
1368219Snate@binkert.org        ipshell = IPShellEmbed(banner=banner,user_ns=scope)
1378219Snate@binkert.org        ipshell()
1388219Snate@binkert.org    except ImportError:
1398219Snate@binkert.org        code.InteractiveConsole(scope).interact(banner)
1408219Snate@binkert.org
1412889Sbinkertn@umich.edudef main():
1425801Snate@binkert.org    import core
1435801Snate@binkert.org    import debug
1445801Snate@binkert.org    import defines
1454167Sbinkertn@umich.edu    import event
1464042Sbinkertn@umich.edu    import info
1475801Snate@binkert.org    import stats
1485799Snate@binkert.org    import trace
1495799Snate@binkert.org
1505799Snate@binkert.org    def check_tracing():
1515799Snate@binkert.org        if defines.TRACING_ON:
1525799Snate@binkert.org            return
1535799Snate@binkert.org
1545802Snate@binkert.org        fatal("Tracing is not enabled.  Compile with TRACING_ON")
1552889Sbinkertn@umich.edu
1565524Sstever@gmail.com    if not os.path.isdir(options.outdir):
1575524Sstever@gmail.com        os.makedirs(options.outdir)
1585524Sstever@gmail.com
1595524Sstever@gmail.com    # These filenames are used only if the redirect_std* options are set
1605524Sstever@gmail.com    stdout_file = os.path.join(options.outdir, options.stdout_file)
1615524Sstever@gmail.com    stderr_file = os.path.join(options.outdir, options.stderr_file)
1625524Sstever@gmail.com
1635524Sstever@gmail.com    # Print redirection notices here before doing any redirection
1645524Sstever@gmail.com    if options.redirect_stdout and not options.redirect_stderr:
1655524Sstever@gmail.com        print "Redirecting stdout and stderr to", stdout_file
1665524Sstever@gmail.com    else:
1675524Sstever@gmail.com        if options.redirect_stdout:
1685524Sstever@gmail.com            print "Redirecting stdout to", stdout_file
1695524Sstever@gmail.com        if options.redirect_stderr:
1705524Sstever@gmail.com            print "Redirecting stderr to", stderr_file
1715524Sstever@gmail.com
1725524Sstever@gmail.com    # Now redirect stdout/stderr as desired
1735524Sstever@gmail.com    if options.redirect_stdout:
1745524Sstever@gmail.com        redir_fd = os.open(stdout_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
1755524Sstever@gmail.com        os.dup2(redir_fd, sys.stdout.fileno())
1765524Sstever@gmail.com        if not options.redirect_stderr:
1775524Sstever@gmail.com            os.dup2(redir_fd, sys.stderr.fileno())
1785524Sstever@gmail.com
1795524Sstever@gmail.com    if options.redirect_stderr:
1805524Sstever@gmail.com        redir_fd = os.open(stderr_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
1815524Sstever@gmail.com        os.dup2(redir_fd, sys.stderr.fileno())
1825524Sstever@gmail.com
1832889Sbinkertn@umich.edu    done = False
1844850Snate@binkert.org
1854850Snate@binkert.org    if options.build_info:
1864850Snate@binkert.org        done = True
1874850Snate@binkert.org        print 'Build information:'
1884850Snate@binkert.org        print
1895801Snate@binkert.org        print 'compiled %s' % defines.compileDate;
1904850Snate@binkert.org        print 'build options:'
1915801Snate@binkert.org        keys = defines.buildEnv.keys()
1924850Snate@binkert.org        keys.sort()
1934850Snate@binkert.org        for key in keys:
1945801Snate@binkert.org            val = defines.buildEnv[key]
1954850Snate@binkert.org            print '    %s = %s' % (key, val)
1964850Snate@binkert.org        print
1974850Snate@binkert.org
1982889Sbinkertn@umich.edu    if options.copyright:
1992889Sbinkertn@umich.edu        done = True
2002889Sbinkertn@umich.edu        print info.LICENSE
2012889Sbinkertn@umich.edu        print
2022889Sbinkertn@umich.edu
2032889Sbinkertn@umich.edu    if options.authors:
2042889Sbinkertn@umich.edu        done = True
2052889Sbinkertn@umich.edu        print 'Author information:'
2062889Sbinkertn@umich.edu        print
2072889Sbinkertn@umich.edu        print info.AUTHORS
2082889Sbinkertn@umich.edu        print
2092889Sbinkertn@umich.edu
2102889Sbinkertn@umich.edu    if options.readme:
2112889Sbinkertn@umich.edu        done = True
2122889Sbinkertn@umich.edu        print 'Readme:'
2132889Sbinkertn@umich.edu        print
2142889Sbinkertn@umich.edu        print info.README
2152889Sbinkertn@umich.edu        print
2162889Sbinkertn@umich.edu
2174053Sbinkertn@umich.edu    if options.trace_help:
2184053Sbinkertn@umich.edu        done = True
2195799Snate@binkert.org        check_tracing()
2205799Snate@binkert.org        trace.help()
2214053Sbinkertn@umich.edu
2225473Snate@binkert.org    if options.list_sim_objects:
2235473Snate@binkert.org        import SimObject
2245473Snate@binkert.org        done = True
2255473Snate@binkert.org        print "SimObjects:"
2265473Snate@binkert.org        objects = SimObject.allClasses.keys()
2275473Snate@binkert.org        objects.sort()
2285473Snate@binkert.org        for name in objects:
2295473Snate@binkert.org            obj = SimObject.allClasses[name]
2305473Snate@binkert.org            print "    %s" % obj
2315473Snate@binkert.org            params = obj._params.keys()
2325473Snate@binkert.org            params.sort()
2335473Snate@binkert.org            for pname in params:
2345473Snate@binkert.org                param = obj._params[pname]
2355473Snate@binkert.org                default = getattr(param, 'default', '')
2365473Snate@binkert.org                print "        %s" % pname
2375473Snate@binkert.org                if default:
2385473Snate@binkert.org                    print "            default: %s" % default
2395473Snate@binkert.org                print "            desc: %s" % param.desc
2405473Snate@binkert.org                print
2415473Snate@binkert.org            print
2425473Snate@binkert.org
2432889Sbinkertn@umich.edu    if done:
2442889Sbinkertn@umich.edu        sys.exit(0)
2452889Sbinkertn@umich.edu
2465470Snate@binkert.org    # setting verbose and quiet at the same time doesn't make sense
2475470Snate@binkert.org    if options.verbose > 0 and options.quiet > 0:
2485470Snate@binkert.org        options.usage(2)
2495470Snate@binkert.org
2505470Snate@binkert.org    verbose = options.verbose - options.quiet
2512889Sbinkertn@umich.edu    if options.verbose >= 0:
2522889Sbinkertn@umich.edu        print "M5 Simulator System"
2532889Sbinkertn@umich.edu        print brief_copyright
2542889Sbinkertn@umich.edu        print
2555801Snate@binkert.org
2565801Snate@binkert.org        print "M5 compiled %s" % defines.compileDate;
2575456Ssaidi@eecs.umich.edu
2585528Sstever@gmail.com        print "M5 started %s" % datetime.datetime.now().strftime("%b %e %Y %X")
2595528Sstever@gmail.com        print "M5 executing on %s" % socket.gethostname()
2605528Sstever@gmail.com
2612967Sktlim@umich.edu        print "command line:",
2622967Sktlim@umich.edu        for argv in sys.argv:
2632967Sktlim@umich.edu            print argv,
2642967Sktlim@umich.edu        print
2652889Sbinkertn@umich.edu
2662889Sbinkertn@umich.edu    # check to make sure we can find the listed script
2672889Sbinkertn@umich.edu    if not arguments or not os.path.isfile(arguments[0]):
2682922Sktlim@umich.edu        if arguments and not os.path.isfile(arguments[0]):
2692922Sktlim@umich.edu            print "Script %s not found" % arguments[0]
2704053Sbinkertn@umich.edu
2715470Snate@binkert.org        options.usage(2)
2722889Sbinkertn@umich.edu
2732889Sbinkertn@umich.edu    # tell C++ about output directory
2745801Snate@binkert.org    core.setOutputDir(options.outdir)
2752889Sbinkertn@umich.edu
2762889Sbinkertn@umich.edu    # update the system path with elements from the -p option
2772889Sbinkertn@umich.edu    sys.path[0:0] = options.path
2782889Sbinkertn@umich.edu
2792889Sbinkertn@umich.edu    # set stats options
2805801Snate@binkert.org    stats.initText(options.stats_file)
2812889Sbinkertn@umich.edu
2822889Sbinkertn@umich.edu    # set debugging options
2835801Snate@binkert.org    debug.setRemoteGDBPort(options.remote_gdb_port)
2843645Sbinkertn@umich.edu    for when in options.debug_break:
2855801Snate@binkert.org        debug.schedBreakCycle(int(when))
2862889Sbinkertn@umich.edu
2875586Snate@binkert.org    if options.trace_flags:
2885799Snate@binkert.org        check_tracing()
2894053Sbinkertn@umich.edu
2905586Snate@binkert.org        on_flags = []
2915586Snate@binkert.org        off_flags = []
2925586Snate@binkert.org        for flag in options.trace_flags:
2935586Snate@binkert.org            off = False
2945586Snate@binkert.org            if flag.startswith('-'):
2955586Snate@binkert.org                flag = flag[1:]
2965586Snate@binkert.org                off = True
2975799Snate@binkert.org            if flag not in trace.flags.all and flag != "All":
2985586Snate@binkert.org                print >>sys.stderr, "invalid trace flag '%s'" % flag
2995586Snate@binkert.org                sys.exit(1)
3004053Sbinkertn@umich.edu
3015586Snate@binkert.org            if off:
3025586Snate@binkert.org                off_flags.append(flag)
3035586Snate@binkert.org            else:
3045586Snate@binkert.org                on_flags.append(flag)
3054042Sbinkertn@umich.edu
3065586Snate@binkert.org        for flag in on_flags:
3075799Snate@binkert.org            trace.set(flag)
3085586Snate@binkert.org
3095586Snate@binkert.org        for flag in off_flags:
3105799Snate@binkert.org            trace.clear(flag)
3114053Sbinkertn@umich.edu
3124074Sbinkertn@umich.edu    if options.trace_start:
3135799Snate@binkert.org        check_tracing()
3145950Ssaidi@eecs.umich.edu        e = event.create(trace.enable, event.Event.Trace_Enable_Pri)
3155606Snate@binkert.org        event.mainq.schedule(e, options.trace_start)
3164074Sbinkertn@umich.edu    else:
3175799Snate@binkert.org        trace.enable()
3184042Sbinkertn@umich.edu
3195799Snate@binkert.org    trace.output(options.trace_file)
3204042Sbinkertn@umich.edu
3214042Sbinkertn@umich.edu    for ignore in options.trace_ignore:
3225799Snate@binkert.org        check_tracing()
3235799Snate@binkert.org        trace.ignore(ignore)
3242889Sbinkertn@umich.edu
3252889Sbinkertn@umich.edu    sys.argv = arguments
3262889Sbinkertn@umich.edu    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
3272891Sbinkertn@umich.edu
3285604Snate@binkert.org    filename = sys.argv[0]
3295604Snate@binkert.org    filedata = file(filename, 'r').read()
3305604Snate@binkert.org    filecode = compile(filedata, filename, 'exec')
3315604Snate@binkert.org    scope = { '__file__' : filename,
3323887Sbinkertn@umich.edu              '__name__' : '__m5_main__' }
3332899Sbinkertn@umich.edu
3342899Sbinkertn@umich.edu    # we want readline if we're doing anything interactive
3352899Sbinkertn@umich.edu    if options.interactive or options.pdb:
3364042Sbinkertn@umich.edu        exec "import readline" in scope
3372899Sbinkertn@umich.edu
3382899Sbinkertn@umich.edu    # if pdb was requested, execfile the thing under pdb, otherwise,
3392899Sbinkertn@umich.edu    # just do the execfile normally
3402899Sbinkertn@umich.edu    if options.pdb:
3415604Snate@binkert.org        import pdb
3425604Snate@binkert.org        import traceback
3435604Snate@binkert.org
3445604Snate@binkert.org        pdb = pdb.Pdb()
3455604Snate@binkert.org        try:
3465604Snate@binkert.org            pdb.run(filecode, scope)
3475604Snate@binkert.org        except SystemExit:
3485604Snate@binkert.org            print "The program exited via sys.exit(). Exit status: ",
3495604Snate@binkert.org            print sys.exc_info()[1]
3505604Snate@binkert.org        except:
3515604Snate@binkert.org            traceback.print_exc()
3525604Snate@binkert.org            print "Uncaught exception. Entering post mortem debugging"
3535604Snate@binkert.org            t = sys.exc_info()[2]
3545604Snate@binkert.org            while t.tb_next is not None:
3555604Snate@binkert.org                t = t.tb_next
3565604Snate@binkert.org                pdb.interaction(t.tb_frame,t)
3572899Sbinkertn@umich.edu    else:
3585604Snate@binkert.org        exec filecode in scope
3592889Sbinkertn@umich.edu
3602889Sbinkertn@umich.edu    # once the script is done
3612889Sbinkertn@umich.edu    if options.interactive:
3628219Snate@binkert.org        interact(scope)
3632889Sbinkertn@umich.edu
3642889Sbinkertn@umich.eduif __name__ == '__main__':
3652889Sbinkertn@umich.edu    from pprint import pprint
3662889Sbinkertn@umich.edu
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