main.py revision 5473
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
355467Snate@binkert.orgfrom util import attrdict
365471Snate@binkert.orgimport config
374850Snate@binkert.orgimport defines
385470Snate@binkert.orgfrom options import OptionParser
394053Sbinkertn@umich.eduimport traceflags
402889Sbinkertn@umich.edu
412889Sbinkertn@umich.edu__all__ = [ 'options', 'arguments', 'main' ]
422889Sbinkertn@umich.edu
434053Sbinkertn@umich.edudef print_list(items, indent=4):
444053Sbinkertn@umich.edu    line = ' ' * indent
454053Sbinkertn@umich.edu    for i,item in enumerate(items):
464053Sbinkertn@umich.edu        if len(line) + len(item) > 76:
474053Sbinkertn@umich.edu            print line
484053Sbinkertn@umich.edu            line = ' ' * indent
494053Sbinkertn@umich.edu
504053Sbinkertn@umich.edu        if i < len(items) - 1:
514053Sbinkertn@umich.edu            line += '%s, ' % item
524053Sbinkertn@umich.edu        else:
534053Sbinkertn@umich.edu            line += item
544053Sbinkertn@umich.edu            print line
554053Sbinkertn@umich.edu
565470Snate@binkert.orgusage="%prog [m5 options] script.py [script options]"
575470Snate@binkert.orgversion="%prog 2.0"
585470Snate@binkert.orgbrief_copyright='''
595470Snate@binkert.orgCopyright (c) 2001-2008
605470Snate@binkert.orgThe Regents of The University of Michigan
615470Snate@binkert.orgAll Rights Reserved
625470Snate@binkert.org'''
632889Sbinkertn@umich.edu
645470Snate@binkert.orgoptions = OptionParser(usage=usage, version=version,
655470Snate@binkert.org                       description=brief_copyright)
665470Snate@binkert.orgadd_option = options.add_option
675470Snate@binkert.orgset_group = options.set_group
685470Snate@binkert.orgusage = options.usage
692889Sbinkertn@umich.edu
702889Sbinkertn@umich.edu# Help options
712889Sbinkertn@umich.eduadd_option('-A', "--authors", action="store_true", default=False,
722889Sbinkertn@umich.edu    help="Show author information")
734850Snate@binkert.orgadd_option('-B', "--build-info", action="store_true", default=False,
744850Snate@binkert.org    help="Show build information")
752889Sbinkertn@umich.eduadd_option('-C', "--copyright", action="store_true", default=False,
762889Sbinkertn@umich.edu    help="Show full copyright information")
772889Sbinkertn@umich.eduadd_option('-R', "--readme", action="store_true", default=False,
782889Sbinkertn@umich.edu    help="Show the readme")
792889Sbinkertn@umich.eduadd_option('-N', "--release-notes", action="store_true", default=False,
802889Sbinkertn@umich.edu    help="Show the release notes")
812889Sbinkertn@umich.edu
822889Sbinkertn@umich.edu# Options for configuring the base simulator
832889Sbinkertn@umich.eduadd_option('-d', "--outdir", metavar="DIR", default=".",
842889Sbinkertn@umich.edu    help="Set the output directory to DIR [Default: %default]")
852889Sbinkertn@umich.eduadd_option('-i', "--interactive", action="store_true", default=False,
862889Sbinkertn@umich.edu    help="Invoke the interactive interpreter after running the script")
872899Sbinkertn@umich.eduadd_option("--pdb", action="store_true", default=False,
882899Sbinkertn@umich.edu    help="Invoke the python debugger before running the script")
892889Sbinkertn@umich.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
902889Sbinkertn@umich.edu    help="Prepend PATH to the system path when invoking the script")
912889Sbinkertn@umich.eduadd_option('-q', "--quiet", action="count", default=0,
922889Sbinkertn@umich.edu    help="Reduce verbosity")
932889Sbinkertn@umich.eduadd_option('-v', "--verbose", action="count", default=0,
942889Sbinkertn@umich.edu    help="Increase verbosity")
952889Sbinkertn@umich.edu
962889Sbinkertn@umich.edu# Statistics options
972889Sbinkertn@umich.eduset_group("Statistics Options")
982889Sbinkertn@umich.eduadd_option("--stats-file", metavar="FILE", default="m5stats.txt",
992889Sbinkertn@umich.edu    help="Sets the output file for statistics [Default: %default]")
1002889Sbinkertn@umich.edu
1012889Sbinkertn@umich.edu# Debugging options
1022889Sbinkertn@umich.eduset_group("Debugging Options")
1032889Sbinkertn@umich.eduadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
1042889Sbinkertn@umich.edu    help="Cycle to create a breakpoint")
1052889Sbinkertn@umich.edu
1062889Sbinkertn@umich.edu# Tracing options
1072889Sbinkertn@umich.eduset_group("Trace Options")
1084053Sbinkertn@umich.eduadd_option("--trace-help", action='store_true',
1094053Sbinkertn@umich.edu    help="Print help on trace flags")
1102889Sbinkertn@umich.eduadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
1114053Sbinkertn@umich.edu    help="Sets the flags for tracing (-FLAG disables a flag)")
1124044Sbinkertn@umich.eduadd_option("--trace-start", metavar="TIME", type='int',
1134044Sbinkertn@umich.edu    help="Start tracing at TIME (must be in ticks)")
1142889Sbinkertn@umich.eduadd_option("--trace-file", metavar="FILE", default="cout",
1152889Sbinkertn@umich.edu    help="Sets the output file for tracing [Default: %default]")
1162889Sbinkertn@umich.eduadd_option("--trace-ignore", metavar="EXPR", action='append', split=':',
1172889Sbinkertn@umich.edu    help="Ignore EXPR sim objects")
1182889Sbinkertn@umich.edu
1195473Snate@binkert.org# Help options
1205473Snate@binkert.orgset_group("Help Options")
1215473Snate@binkert.orgadd_option("--list-sim-objects", action='store_true', default=False,
1225473Snate@binkert.org    help="List all built-in SimObjects, their parameters and default values")
1235473Snate@binkert.org
1242889Sbinkertn@umich.edudef main():
1254042Sbinkertn@umich.edu    import defines
1264167Sbinkertn@umich.edu    import event
1274042Sbinkertn@umich.edu    import info
1283624Sbinkertn@umich.edu    import internal
1292889Sbinkertn@umich.edu
1305472Snate@binkert.org    # load the options.py config file to allow people to set their own
1315472Snate@binkert.org    # default options
1325472Snate@binkert.org    options_file = config.get('options.py')
1335472Snate@binkert.org    if options_file:
1345472Snate@binkert.org        scope = { 'options' : options }
1355472Snate@binkert.org        execfile(options_file, scope)
1365472Snate@binkert.org
1375470Snate@binkert.org    arguments = options.parse_args()
1382889Sbinkertn@umich.edu
1392889Sbinkertn@umich.edu    done = False
1404850Snate@binkert.org
1414850Snate@binkert.org    if options.build_info:
1424850Snate@binkert.org        done = True
1434850Snate@binkert.org        print 'Build information:'
1444850Snate@binkert.org        print
1454850Snate@binkert.org        print 'compiled %s' % internal.core.cvar.compileDate;
1464850Snate@binkert.org        print 'started %s' % datetime.datetime.now().ctime()
1474850Snate@binkert.org        print 'executing on %s' % socket.gethostname()
1484850Snate@binkert.org        print 'build options:'
1494850Snate@binkert.org        keys = defines.m5_build_env.keys()
1504850Snate@binkert.org        keys.sort()
1514850Snate@binkert.org        for key in keys:
1524850Snate@binkert.org            val = defines.m5_build_env[key]
1534850Snate@binkert.org            print '    %s = %s' % (key, val)
1544850Snate@binkert.org        print
1554850Snate@binkert.org
1562889Sbinkertn@umich.edu    if options.copyright:
1572889Sbinkertn@umich.edu        done = True
1582889Sbinkertn@umich.edu        print info.LICENSE
1592889Sbinkertn@umich.edu        print
1602889Sbinkertn@umich.edu
1612889Sbinkertn@umich.edu    if options.authors:
1622889Sbinkertn@umich.edu        done = True
1632889Sbinkertn@umich.edu        print 'Author information:'
1642889Sbinkertn@umich.edu        print
1652889Sbinkertn@umich.edu        print info.AUTHORS
1662889Sbinkertn@umich.edu        print
1672889Sbinkertn@umich.edu
1682889Sbinkertn@umich.edu    if options.readme:
1692889Sbinkertn@umich.edu        done = True
1702889Sbinkertn@umich.edu        print 'Readme:'
1712889Sbinkertn@umich.edu        print
1722889Sbinkertn@umich.edu        print info.README
1732889Sbinkertn@umich.edu        print
1742889Sbinkertn@umich.edu
1752889Sbinkertn@umich.edu    if options.release_notes:
1762889Sbinkertn@umich.edu        done = True
1772889Sbinkertn@umich.edu        print 'Release Notes:'
1782889Sbinkertn@umich.edu        print
1792889Sbinkertn@umich.edu        print info.RELEASE_NOTES
1802889Sbinkertn@umich.edu        print
1812889Sbinkertn@umich.edu
1824053Sbinkertn@umich.edu    if options.trace_help:
1834053Sbinkertn@umich.edu        done = True
1844053Sbinkertn@umich.edu        print "Base Flags:"
1854053Sbinkertn@umich.edu        print_list(traceflags.baseFlags, indent=4)
1864053Sbinkertn@umich.edu        print
1874053Sbinkertn@umich.edu        print "Compound Flags:"
1884053Sbinkertn@umich.edu        for flag in traceflags.compoundFlags:
1894053Sbinkertn@umich.edu            if flag == 'All':
1904053Sbinkertn@umich.edu                continue
1914053Sbinkertn@umich.edu            print "    %s:" % flag
1924053Sbinkertn@umich.edu            print_list(traceflags.compoundFlagMap[flag], indent=8)
1934053Sbinkertn@umich.edu            print
1944053Sbinkertn@umich.edu
1955473Snate@binkert.org    if options.list_sim_objects:
1965473Snate@binkert.org        import SimObject
1975473Snate@binkert.org        done = True
1985473Snate@binkert.org        print "SimObjects:"
1995473Snate@binkert.org        objects = SimObject.allClasses.keys()
2005473Snate@binkert.org        objects.sort()
2015473Snate@binkert.org        for name in objects:
2025473Snate@binkert.org            obj = SimObject.allClasses[name]
2035473Snate@binkert.org            print "    %s" % obj
2045473Snate@binkert.org            params = obj._params.keys()
2055473Snate@binkert.org            params.sort()
2065473Snate@binkert.org            for pname in params:
2075473Snate@binkert.org                param = obj._params[pname]
2085473Snate@binkert.org                default = getattr(param, 'default', '')
2095473Snate@binkert.org                print "        %s" % pname
2105473Snate@binkert.org                if default:
2115473Snate@binkert.org                    print "            default: %s" % default
2125473Snate@binkert.org                print "            desc: %s" % param.desc
2135473Snate@binkert.org                print
2145473Snate@binkert.org            print
2155473Snate@binkert.org
2162889Sbinkertn@umich.edu    if done:
2172889Sbinkertn@umich.edu        sys.exit(0)
2182889Sbinkertn@umich.edu
2195470Snate@binkert.org    # setting verbose and quiet at the same time doesn't make sense
2205470Snate@binkert.org    if options.verbose > 0 and options.quiet > 0:
2215470Snate@binkert.org        options.usage(2)
2225470Snate@binkert.org
2235470Snate@binkert.org    verbose = options.verbose - options.quiet
2242889Sbinkertn@umich.edu    if options.verbose >= 0:
2252889Sbinkertn@umich.edu        print "M5 Simulator System"
2262889Sbinkertn@umich.edu        print brief_copyright
2272889Sbinkertn@umich.edu        print
2284123Sbinkertn@umich.edu        print "M5 compiled %s" % internal.core.cvar.compileDate;
2294850Snate@binkert.org        print "M5 started %s" % datetime.datetime.now().ctime()
2302889Sbinkertn@umich.edu        print "M5 executing on %s" % socket.gethostname()
2315456Ssaidi@eecs.umich.edu
2325456Ssaidi@eecs.umich.edu        print "M5 revision %s" % internal.core.cvar.hgRev
2335456Ssaidi@eecs.umich.edu        print "M5 commit date %s" % internal.core.cvar.hgDate
2345456Ssaidi@eecs.umich.edu
2352967Sktlim@umich.edu        print "command line:",
2362967Sktlim@umich.edu        for argv in sys.argv:
2372967Sktlim@umich.edu            print argv,
2382967Sktlim@umich.edu        print
2392889Sbinkertn@umich.edu
2402889Sbinkertn@umich.edu    # check to make sure we can find the listed script
2412889Sbinkertn@umich.edu    if not arguments or not os.path.isfile(arguments[0]):
2422922Sktlim@umich.edu        if arguments and not os.path.isfile(arguments[0]):
2432922Sktlim@umich.edu            print "Script %s not found" % arguments[0]
2444053Sbinkertn@umich.edu
2455470Snate@binkert.org        options.usage(2)
2462889Sbinkertn@umich.edu
2472889Sbinkertn@umich.edu    # tell C++ about output directory
2484123Sbinkertn@umich.edu    internal.core.setOutputDir(options.outdir)
2492889Sbinkertn@umich.edu
2502889Sbinkertn@umich.edu    # update the system path with elements from the -p option
2512889Sbinkertn@umich.edu    sys.path[0:0] = options.path
2522889Sbinkertn@umich.edu
2532889Sbinkertn@umich.edu    import objects
2542889Sbinkertn@umich.edu
2552889Sbinkertn@umich.edu    # set stats options
2564078Sbinkertn@umich.edu    internal.stats.initText(options.stats_file)
2572889Sbinkertn@umich.edu
2582889Sbinkertn@umich.edu    # set debugging options
2593645Sbinkertn@umich.edu    for when in options.debug_break:
2603645Sbinkertn@umich.edu        internal.debug.schedBreakCycle(int(when))
2612889Sbinkertn@umich.edu
2624053Sbinkertn@umich.edu    on_flags = []
2634053Sbinkertn@umich.edu    off_flags = []
2644042Sbinkertn@umich.edu    for flag in options.trace_flags:
2654053Sbinkertn@umich.edu        off = False
2664053Sbinkertn@umich.edu        if flag.startswith('-'):
2674053Sbinkertn@umich.edu            flag = flag[1:]
2684053Sbinkertn@umich.edu            off = True
2694053Sbinkertn@umich.edu        if flag not in traceflags.allFlags:
2704053Sbinkertn@umich.edu            print >>sys.stderr, "invalid trace flag '%s'" % flag
2714053Sbinkertn@umich.edu            sys.exit(1)
2724053Sbinkertn@umich.edu
2734053Sbinkertn@umich.edu        if off:
2744053Sbinkertn@umich.edu            off_flags.append(flag)
2754053Sbinkertn@umich.edu        else:
2764053Sbinkertn@umich.edu            on_flags.append(flag)
2774053Sbinkertn@umich.edu
2784053Sbinkertn@umich.edu    for flag in on_flags:
2794046Sbinkertn@umich.edu        internal.trace.set(flag)
2804042Sbinkertn@umich.edu
2814053Sbinkertn@umich.edu    for flag in off_flags:
2824053Sbinkertn@umich.edu        internal.trace.clear(flag)
2834053Sbinkertn@umich.edu
2844074Sbinkertn@umich.edu    if options.trace_start:
2854042Sbinkertn@umich.edu        def enable_trace():
2864074Sbinkertn@umich.edu            internal.trace.cvar.enabled = True
2874167Sbinkertn@umich.edu        event.create(enable_trace, int(options.trace_start))
2884074Sbinkertn@umich.edu    else:
2894087Sbinkertn@umich.edu        internal.trace.cvar.enabled = True
2904042Sbinkertn@umich.edu
2914046Sbinkertn@umich.edu    internal.trace.output(options.trace_file)
2924042Sbinkertn@umich.edu
2934042Sbinkertn@umich.edu    for ignore in options.trace_ignore:
2944042Sbinkertn@umich.edu        internal.trace.ignore(ignore)
2952889Sbinkertn@umich.edu
2962889Sbinkertn@umich.edu    sys.argv = arguments
2972889Sbinkertn@umich.edu    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
2982891Sbinkertn@umich.edu
2993887Sbinkertn@umich.edu    scope = { '__file__' : sys.argv[0],
3003887Sbinkertn@umich.edu              '__name__' : '__m5_main__' }
3012899Sbinkertn@umich.edu
3022899Sbinkertn@umich.edu    # we want readline if we're doing anything interactive
3032899Sbinkertn@umich.edu    if options.interactive or options.pdb:
3044042Sbinkertn@umich.edu        exec "import readline" in scope
3052899Sbinkertn@umich.edu
3062899Sbinkertn@umich.edu    # if pdb was requested, execfile the thing under pdb, otherwise,
3072899Sbinkertn@umich.edu    # just do the execfile normally
3082899Sbinkertn@umich.edu    if options.pdb:
3092899Sbinkertn@umich.edu        from pdb import Pdb
3102899Sbinkertn@umich.edu        debugger = Pdb()
3112899Sbinkertn@umich.edu        debugger.run('execfile("%s")' % sys.argv[0], scope)
3122899Sbinkertn@umich.edu    else:
3132899Sbinkertn@umich.edu        execfile(sys.argv[0], scope)
3142889Sbinkertn@umich.edu
3152889Sbinkertn@umich.edu    # once the script is done
3162889Sbinkertn@umich.edu    if options.interactive:
3172889Sbinkertn@umich.edu        interact = code.InteractiveConsole(scope)
3182889Sbinkertn@umich.edu        interact.interact("M5 Interactive Console")
3192889Sbinkertn@umich.edu
3202889Sbinkertn@umich.eduif __name__ == '__main__':
3212889Sbinkertn@umich.edu    from pprint import pprint
3222889Sbinkertn@umich.edu
3232889Sbinkertn@umich.edu    parse_args()
3242889Sbinkertn@umich.edu
3252889Sbinkertn@umich.edu    print 'opts:'
3262889Sbinkertn@umich.edu    pprint(options, indent=4)
3272889Sbinkertn@umich.edu    print
3282889Sbinkertn@umich.edu
3292889Sbinkertn@umich.edu    print 'args:'
3302889Sbinkertn@umich.edu    pprint(arguments, indent=4)
331