main.py revision 4078
14120Sgblack@eecs.umich.edu# Copyright (c) 2005 The Regents of The University of Michigan 24120Sgblack@eecs.umich.edu# All rights reserved. 34120Sgblack@eecs.umich.edu# 44120Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without 54120Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are 64120Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright 74120Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer; 84120Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright 94120Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the 104120Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution; 114120Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its 124120Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from 134120Sgblack@eecs.umich.edu# this software without specific prior written permission. 144120Sgblack@eecs.umich.edu# 154120Sgblack@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 164120Sgblack@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 174120Sgblack@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 184120Sgblack@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 194120Sgblack@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 204120Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 214120Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 224120Sgblack@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 234120Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 244120Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 254120Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 264120Sgblack@eecs.umich.edu# 274120Sgblack@eecs.umich.edu# Authors: Nathan Binkert 284120Sgblack@eecs.umich.edu 294120Sgblack@eecs.umich.eduimport code, optparse, os, socket, sys 304120Sgblack@eecs.umich.edufrom datetime import datetime 314120Sgblack@eecs.umich.edufrom attrdict import attrdict 324120Sgblack@eecs.umich.eduimport traceflags 334120Sgblack@eecs.umich.edu 344120Sgblack@eecs.umich.edu__all__ = [ 'options', 'arguments', 'main' ] 354120Sgblack@eecs.umich.edu 364120Sgblack@eecs.umich.eduusage="%prog [m5 options] script.py [script options]" 374120Sgblack@eecs.umich.eduversion="%prog 2.0" 384120Sgblack@eecs.umich.edubrief_copyright=''' 394120Sgblack@eecs.umich.eduCopyright (c) 2001-2006 404120Sgblack@eecs.umich.eduThe Regents of The University of Michigan 414120Sgblack@eecs.umich.eduAll Rights Reserved 424120Sgblack@eecs.umich.edu''' 434120Sgblack@eecs.umich.edu 444120Sgblack@eecs.umich.edudef print_list(items, indent=4): 454120Sgblack@eecs.umich.edu line = ' ' * indent 464120Sgblack@eecs.umich.edu for i,item in enumerate(items): 474120Sgblack@eecs.umich.edu if len(line) + len(item) > 76: 484120Sgblack@eecs.umich.edu print line 494120Sgblack@eecs.umich.edu line = ' ' * indent 504120Sgblack@eecs.umich.edu 514120Sgblack@eecs.umich.edu if i < len(items) - 1: 524120Sgblack@eecs.umich.edu line += '%s, ' % item 534120Sgblack@eecs.umich.edu else: 544120Sgblack@eecs.umich.edu line += item 554120Sgblack@eecs.umich.edu print line 564120Sgblack@eecs.umich.edu 574120Sgblack@eecs.umich.edu# there's only one option parsing done, so make it global and add some 584120Sgblack@eecs.umich.edu# helper functions to make it work well. 594120Sgblack@eecs.umich.eduparser = optparse.OptionParser(usage=usage, version=version, 604120Sgblack@eecs.umich.edu description=brief_copyright, 614139Sgblack@eecs.umich.edu formatter=optparse.TitledHelpFormatter()) 624135Sgblack@eecs.umich.eduparser.disable_interspersed_args() 634120Sgblack@eecs.umich.edu 644120Sgblack@eecs.umich.edu# current option group 654120Sgblack@eecs.umich.edugroup = None 665114Sgblack@eecs.umich.edu 675114Sgblack@eecs.umich.edudef set_group(*args, **kwargs): 684135Sgblack@eecs.umich.edu '''set the current option group''' 694365Sgblack@eecs.umich.edu global group 705114Sgblack@eecs.umich.edu if not args and not kwargs: 715114Sgblack@eecs.umich.edu group = None 725139Sgblack@eecs.umich.edu else: 735114Sgblack@eecs.umich.edu group = parser.add_option_group(*args, **kwargs) 745139Sgblack@eecs.umich.edu 755139Sgblack@eecs.umich.educlass splitter(object): 765139Sgblack@eecs.umich.edu def __init__(self, split): 775114Sgblack@eecs.umich.edu self.split = split 785114Sgblack@eecs.umich.edu def __call__(self, option, opt_str, value, parser): 795114Sgblack@eecs.umich.edu getattr(parser.values, option.dest).extend(value.split(self.split)) 804729Sgblack@eecs.umich.edu 814365Sgblack@eecs.umich.edudef add_option(*args, **kwargs): 825114Sgblack@eecs.umich.edu '''add an option to the current option group, or global none set''' 834365Sgblack@eecs.umich.edu 844365Sgblack@eecs.umich.edu # if action=split, but allows the option arguments 855114Sgblack@eecs.umich.edu # themselves to be lists separated by the split variable''' 865114Sgblack@eecs.umich.edu 875114Sgblack@eecs.umich.edu if kwargs.get('action', None) == 'append' and 'split' in kwargs: 885114Sgblack@eecs.umich.edu split = kwargs.pop('split') 895114Sgblack@eecs.umich.edu kwargs['default'] = [] 905114Sgblack@eecs.umich.edu kwargs['type'] = 'string' 915114Sgblack@eecs.umich.edu kwargs['action'] = 'callback' 925114Sgblack@eecs.umich.edu kwargs['callback'] = splitter(split) 935114Sgblack@eecs.umich.edu 945114Sgblack@eecs.umich.edu if group: 955114Sgblack@eecs.umich.edu return group.add_option(*args, **kwargs) 965114Sgblack@eecs.umich.edu 975114Sgblack@eecs.umich.edu return parser.add_option(*args, **kwargs) 985114Sgblack@eecs.umich.edu 995114Sgblack@eecs.umich.edudef bool_option(name, default, help): 1005114Sgblack@eecs.umich.edu '''add a boolean option called --name and --no-name. 1015139Sgblack@eecs.umich.edu Display help depending on which is the default''' 1025139Sgblack@eecs.umich.edu 1035139Sgblack@eecs.umich.edu tname = '--%s' % name 1045114Sgblack@eecs.umich.edu fname = '--no-%s' % name 1055114Sgblack@eecs.umich.edu dest = name.replace('-', '_') 1065114Sgblack@eecs.umich.edu if default: 1075114Sgblack@eecs.umich.edu thelp = optparse.SUPPRESS_HELP 1085114Sgblack@eecs.umich.edu fhelp = help 1095114Sgblack@eecs.umich.edu else: 1105114Sgblack@eecs.umich.edu thelp = help 1115114Sgblack@eecs.umich.edu fhelp = optparse.SUPPRESS_HELP 1125139Sgblack@eecs.umich.edu 1135139Sgblack@eecs.umich.edu add_option(tname, action="store_true", default=default, help=thelp) 1145139Sgblack@eecs.umich.edu add_option(fname, action="store_false", dest=dest, help=fhelp) 1155114Sgblack@eecs.umich.edu 1165114Sgblack@eecs.umich.edu# Help options 1175114Sgblack@eecs.umich.eduadd_option('-A', "--authors", action="store_true", default=False, 1185124Sgblack@eecs.umich.edu help="Show author information") 1195114Sgblack@eecs.umich.eduadd_option('-C', "--copyright", action="store_true", default=False, 1205114Sgblack@eecs.umich.edu help="Show full copyright information") 1215114Sgblack@eecs.umich.eduadd_option('-R', "--readme", action="store_true", default=False, 1225114Sgblack@eecs.umich.edu help="Show the readme") 1235114Sgblack@eecs.umich.eduadd_option('-N', "--release-notes", action="store_true", default=False, 1245114Sgblack@eecs.umich.edu help="Show the release notes") 1255114Sgblack@eecs.umich.edu 1265139Sgblack@eecs.umich.edu# Options for configuring the base simulator 1275139Sgblack@eecs.umich.eduadd_option('-d', "--outdir", metavar="DIR", default=".", 1285139Sgblack@eecs.umich.edu help="Set the output directory to DIR [Default: %default]") 1295114Sgblack@eecs.umich.eduadd_option('-i', "--interactive", action="store_true", default=False, 1305114Sgblack@eecs.umich.edu help="Invoke the interactive interpreter after running the script") 1315114Sgblack@eecs.umich.eduadd_option("--pdb", action="store_true", default=False, 1325124Sgblack@eecs.umich.edu help="Invoke the python debugger before running the script") 1335114Sgblack@eecs.umich.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':', 1345114Sgblack@eecs.umich.edu help="Prepend PATH to the system path when invoking the script") 1355114Sgblack@eecs.umich.eduadd_option('-q', "--quiet", action="count", default=0, 1365114Sgblack@eecs.umich.edu help="Reduce verbosity") 1375114Sgblack@eecs.umich.eduadd_option('-v', "--verbose", action="count", default=0, 1385114Sgblack@eecs.umich.edu help="Increase verbosity") 1395114Sgblack@eecs.umich.edu 1405139Sgblack@eecs.umich.edu# Statistics options 1415139Sgblack@eecs.umich.eduset_group("Statistics Options") 1425139Sgblack@eecs.umich.eduadd_option("--stats-file", metavar="FILE", default="m5stats.txt", 1435114Sgblack@eecs.umich.edu help="Sets the output file for statistics [Default: %default]") 1445114Sgblack@eecs.umich.edu 1455114Sgblack@eecs.umich.edu# Debugging options 1465124Sgblack@eecs.umich.eduset_group("Debugging Options") 1475114Sgblack@eecs.umich.eduadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',', 1484135Sgblack@eecs.umich.edu help="Cycle to create a breakpoint") 1494150Sgblack@eecs.umich.edu 1504365Sgblack@eecs.umich.edu# Tracing options 1514365Sgblack@eecs.umich.eduset_group("Trace Options") 1524365Sgblack@eecs.umich.eduadd_option("--trace-help", action='store_true', 1534729Sgblack@eecs.umich.edu help="Print help on trace flags") 1544365Sgblack@eecs.umich.eduadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',', 1554365Sgblack@eecs.umich.edu help="Sets the flags for tracing (-FLAG disables a flag)") 1564365Sgblack@eecs.umich.eduadd_option("--trace-start", metavar="TIME", type='int', 1574365Sgblack@eecs.umich.edu help="Start tracing at TIME (must be in ticks)") 1584365Sgblack@eecs.umich.eduadd_option("--trace-file", metavar="FILE", default="cout", 1594365Sgblack@eecs.umich.edu help="Sets the output file for tracing [Default: %default]") 1604365Sgblack@eecs.umich.eduadd_option("--trace-ignore", metavar="EXPR", action='append', split=':', 1614365Sgblack@eecs.umich.edu help="Ignore EXPR sim objects") 1624365Sgblack@eecs.umich.edu 1634365Sgblack@eecs.umich.eduoptions = attrdict() 1644150Sgblack@eecs.umich.eduarguments = [] 1654150Sgblack@eecs.umich.edu 1664150Sgblack@eecs.umich.edudef usage(exitcode=None): 1674150Sgblack@eecs.umich.edu parser.print_help() 1685114Sgblack@eecs.umich.edu if exitcode is not None: 1695114Sgblack@eecs.umich.edu sys.exit(exitcode) 1705114Sgblack@eecs.umich.edu 1715114Sgblack@eecs.umich.edudef parse_args(): 1725114Sgblack@eecs.umich.edu _opts,args = parser.parse_args() 1735114Sgblack@eecs.umich.edu opts = attrdict(_opts.__dict__) 1745114Sgblack@eecs.umich.edu 1755114Sgblack@eecs.umich.edu # setting verbose and quiet at the same time doesn't make sense 1765114Sgblack@eecs.umich.edu if opts.verbose > 0 and opts.quiet > 0: 1775114Sgblack@eecs.umich.edu usage(2) 1785114Sgblack@eecs.umich.edu 1795114Sgblack@eecs.umich.edu # store the verbosity in a single variable. 0 is default, 1805114Sgblack@eecs.umich.edu # negative numbers represent quiet and positive values indicate verbose 1815114Sgblack@eecs.umich.edu opts.verbose -= opts.quiet 1825114Sgblack@eecs.umich.edu 1835114Sgblack@eecs.umich.edu del opts.quiet 1845114Sgblack@eecs.umich.edu 1855114Sgblack@eecs.umich.edu options.update(opts) 1865114Sgblack@eecs.umich.edu arguments.extend(args) 1875114Sgblack@eecs.umich.edu return opts,args 1885114Sgblack@eecs.umich.edu 1895114Sgblack@eecs.umich.edudef main(): 1905114Sgblack@eecs.umich.edu import defines 1915114Sgblack@eecs.umich.edu import info 1925114Sgblack@eecs.umich.edu import internal 1935114Sgblack@eecs.umich.edu 1945114Sgblack@eecs.umich.edu parse_args() 1955114Sgblack@eecs.umich.edu 1965114Sgblack@eecs.umich.edu done = False 1975114Sgblack@eecs.umich.edu if options.copyright: 1985114Sgblack@eecs.umich.edu done = True 1995114Sgblack@eecs.umich.edu print info.LICENSE 2005114Sgblack@eecs.umich.edu print 2015114Sgblack@eecs.umich.edu 2025114Sgblack@eecs.umich.edu if options.authors: 2035114Sgblack@eecs.umich.edu done = True 2045114Sgblack@eecs.umich.edu print 'Author information:' 2055114Sgblack@eecs.umich.edu print 2065114Sgblack@eecs.umich.edu print info.AUTHORS 2075114Sgblack@eecs.umich.edu print 2085114Sgblack@eecs.umich.edu 2095114Sgblack@eecs.umich.edu if options.readme: 2105114Sgblack@eecs.umich.edu done = True 2115114Sgblack@eecs.umich.edu print 'Readme:' 2125114Sgblack@eecs.umich.edu print 2135114Sgblack@eecs.umich.edu print info.README 2145114Sgblack@eecs.umich.edu print 2155114Sgblack@eecs.umich.edu 2165114Sgblack@eecs.umich.edu if options.release_notes: 2175114Sgblack@eecs.umich.edu done = True 2185114Sgblack@eecs.umich.edu print 'Release Notes:' 2195114Sgblack@eecs.umich.edu print 2205114Sgblack@eecs.umich.edu print info.RELEASE_NOTES 2215114Sgblack@eecs.umich.edu print 2225114Sgblack@eecs.umich.edu 2235114Sgblack@eecs.umich.edu if options.trace_help: 2245114Sgblack@eecs.umich.edu done = True 2255114Sgblack@eecs.umich.edu print "Base Flags:" 2265114Sgblack@eecs.umich.edu print_list(traceflags.baseFlags, indent=4) 2275114Sgblack@eecs.umich.edu print 2285114Sgblack@eecs.umich.edu print "Compound Flags:" 2295114Sgblack@eecs.umich.edu for flag in traceflags.compoundFlags: 2305114Sgblack@eecs.umich.edu if flag == 'All': 2315114Sgblack@eecs.umich.edu continue 2325114Sgblack@eecs.umich.edu print " %s:" % flag 2335114Sgblack@eecs.umich.edu print_list(traceflags.compoundFlagMap[flag], indent=8) 2345114Sgblack@eecs.umich.edu print 2355114Sgblack@eecs.umich.edu 2365114Sgblack@eecs.umich.edu if done: 2375114Sgblack@eecs.umich.edu sys.exit(0) 2385114Sgblack@eecs.umich.edu 2395114Sgblack@eecs.umich.edu if options.verbose >= 0: 2405114Sgblack@eecs.umich.edu print "M5 Simulator System" 2415114Sgblack@eecs.umich.edu print brief_copyright 2425114Sgblack@eecs.umich.edu print 2435114Sgblack@eecs.umich.edu print "M5 compiled %s" % internal.main.cvar.compileDate; 2445114Sgblack@eecs.umich.edu print "M5 started %s" % datetime.now().ctime() 2455114Sgblack@eecs.umich.edu print "M5 executing on %s" % socket.gethostname() 2465114Sgblack@eecs.umich.edu print "command line:", 2475114Sgblack@eecs.umich.edu for argv in sys.argv: 2485114Sgblack@eecs.umich.edu print argv, 2495114Sgblack@eecs.umich.edu print 2505114Sgblack@eecs.umich.edu 2515114Sgblack@eecs.umich.edu # check to make sure we can find the listed script 2525114Sgblack@eecs.umich.edu if not arguments or not os.path.isfile(arguments[0]): 2535114Sgblack@eecs.umich.edu if arguments and not os.path.isfile(arguments[0]): 2545114Sgblack@eecs.umich.edu print "Script %s not found" % arguments[0] 2555114Sgblack@eecs.umich.edu 2565114Sgblack@eecs.umich.edu usage(2) 2575114Sgblack@eecs.umich.edu 2585114Sgblack@eecs.umich.edu # tell C++ about output directory 2595114Sgblack@eecs.umich.edu internal.main.setOutputDir(options.outdir) 2605114Sgblack@eecs.umich.edu 2615114Sgblack@eecs.umich.edu # update the system path with elements from the -p option 2625114Sgblack@eecs.umich.edu sys.path[0:0] = options.path 2635114Sgblack@eecs.umich.edu 2645114Sgblack@eecs.umich.edu import objects 2655114Sgblack@eecs.umich.edu 2665114Sgblack@eecs.umich.edu # set stats options 2675114Sgblack@eecs.umich.edu internal.stats.initText(options.stats_file) 2685114Sgblack@eecs.umich.edu 2695114Sgblack@eecs.umich.edu # set debugging options 2705114Sgblack@eecs.umich.edu for when in options.debug_break: 2715114Sgblack@eecs.umich.edu internal.debug.schedBreakCycle(int(when)) 2725114Sgblack@eecs.umich.edu 2735114Sgblack@eecs.umich.edu on_flags = [] 2745114Sgblack@eecs.umich.edu off_flags = [] 2755114Sgblack@eecs.umich.edu for flag in options.trace_flags: 2765114Sgblack@eecs.umich.edu off = False 2775114Sgblack@eecs.umich.edu if flag.startswith('-'): 2785114Sgblack@eecs.umich.edu flag = flag[1:] 2795114Sgblack@eecs.umich.edu off = True 2805114Sgblack@eecs.umich.edu if flag not in traceflags.allFlags: 2815114Sgblack@eecs.umich.edu print >>sys.stderr, "invalid trace flag '%s'" % flag 2825114Sgblack@eecs.umich.edu sys.exit(1) 2835114Sgblack@eecs.umich.edu 2845114Sgblack@eecs.umich.edu if off: 2855114Sgblack@eecs.umich.edu off_flags.append(flag) 2865114Sgblack@eecs.umich.edu else: 2875114Sgblack@eecs.umich.edu on_flags.append(flag) 2885114Sgblack@eecs.umich.edu 2895114Sgblack@eecs.umich.edu for flag in on_flags: 2905114Sgblack@eecs.umich.edu internal.trace.set(flag) 2915114Sgblack@eecs.umich.edu 2925114Sgblack@eecs.umich.edu for flag in off_flags: 2935114Sgblack@eecs.umich.edu internal.trace.clear(flag) 2945114Sgblack@eecs.umich.edu 2955114Sgblack@eecs.umich.edu if options.trace_start: 2965114Sgblack@eecs.umich.edu def enable_trace(): 2975114Sgblack@eecs.umich.edu internal.trace.cvar.enabled = True 2985114Sgblack@eecs.umich.edu internal.event.create(enable_trace, int(options.trace_start)) 2995139Sgblack@eecs.umich.edu else: 3005139Sgblack@eecs.umich.edu internal.trace.enabled = True 3015114Sgblack@eecs.umich.edu 3025114Sgblack@eecs.umich.edu internal.trace.output(options.trace_file) 3035114Sgblack@eecs.umich.edu 3045114Sgblack@eecs.umich.edu for ignore in options.trace_ignore: 3055114Sgblack@eecs.umich.edu internal.trace.ignore(ignore) 3065114Sgblack@eecs.umich.edu 3075114Sgblack@eecs.umich.edu sys.argv = arguments 3085114Sgblack@eecs.umich.edu sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path 3095114Sgblack@eecs.umich.edu 3105114Sgblack@eecs.umich.edu scope = { '__file__' : sys.argv[0], 3115114Sgblack@eecs.umich.edu '__name__' : '__m5_main__' } 3125114Sgblack@eecs.umich.edu 3135114Sgblack@eecs.umich.edu # we want readline if we're doing anything interactive 3145114Sgblack@eecs.umich.edu if options.interactive or options.pdb: 3155114Sgblack@eecs.umich.edu exec "import readline" in scope 3165114Sgblack@eecs.umich.edu 3175114Sgblack@eecs.umich.edu # if pdb was requested, execfile the thing under pdb, otherwise, 3185114Sgblack@eecs.umich.edu # just do the execfile normally 3195114Sgblack@eecs.umich.edu if options.pdb: 3205652Sgblack@eecs.umich.edu from pdb import Pdb 3215114Sgblack@eecs.umich.edu debugger = Pdb() 3225114Sgblack@eecs.umich.edu debugger.run('execfile("%s")' % sys.argv[0], scope) 3235114Sgblack@eecs.umich.edu else: 3245114Sgblack@eecs.umich.edu execfile(sys.argv[0], scope) 3255114Sgblack@eecs.umich.edu 3265114Sgblack@eecs.umich.edu # once the script is done 3275114Sgblack@eecs.umich.edu if options.interactive: 3285652Sgblack@eecs.umich.edu interact = code.InteractiveConsole(scope) 3295114Sgblack@eecs.umich.edu interact.interact("M5 Interactive Console") 3305114Sgblack@eecs.umich.edu 3315114Sgblack@eecs.umich.eduif __name__ == '__main__': 3325114Sgblack@eecs.umich.edu from pprint import pprint 3335114Sgblack@eecs.umich.edu 3345114Sgblack@eecs.umich.edu parse_args() 3355114Sgblack@eecs.umich.edu 3365652Sgblack@eecs.umich.edu print 'opts:' 3375114Sgblack@eecs.umich.edu pprint(options, indent=4) 3385114Sgblack@eecs.umich.edu print 3395114Sgblack@eecs.umich.edu 3405114Sgblack@eecs.umich.edu print 'args:' 3415114Sgblack@eecs.umich.edu pprint(arguments, indent=4) 3425114Sgblack@eecs.umich.edu