main.py revision 4850
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 optparse 324850Snate@binkert.orgimport os 334850Snate@binkert.orgimport socket 344850Snate@binkert.orgimport sys 354850Snate@binkert.org 362889Sbinkertn@umich.edufrom attrdict import attrdict 374850Snate@binkert.orgimport defines 384053Sbinkertn@umich.eduimport traceflags 392889Sbinkertn@umich.edu 402889Sbinkertn@umich.edu__all__ = [ 'options', 'arguments', 'main' ] 412889Sbinkertn@umich.edu 422889Sbinkertn@umich.eduusage="%prog [m5 options] script.py [script options]" 432889Sbinkertn@umich.eduversion="%prog 2.0" 442889Sbinkertn@umich.edubrief_copyright=''' 452889Sbinkertn@umich.eduCopyright (c) 2001-2006 462889Sbinkertn@umich.eduThe Regents of The University of Michigan 472889Sbinkertn@umich.eduAll Rights Reserved 482889Sbinkertn@umich.edu''' 492889Sbinkertn@umich.edu 504053Sbinkertn@umich.edudef print_list(items, indent=4): 514053Sbinkertn@umich.edu line = ' ' * indent 524053Sbinkertn@umich.edu for i,item in enumerate(items): 534053Sbinkertn@umich.edu if len(line) + len(item) > 76: 544053Sbinkertn@umich.edu print line 554053Sbinkertn@umich.edu line = ' ' * indent 564053Sbinkertn@umich.edu 574053Sbinkertn@umich.edu if i < len(items) - 1: 584053Sbinkertn@umich.edu line += '%s, ' % item 594053Sbinkertn@umich.edu else: 604053Sbinkertn@umich.edu line += item 614053Sbinkertn@umich.edu print line 624053Sbinkertn@umich.edu 632889Sbinkertn@umich.edu# there's only one option parsing done, so make it global and add some 642889Sbinkertn@umich.edu# helper functions to make it work well. 652889Sbinkertn@umich.eduparser = optparse.OptionParser(usage=usage, version=version, 662889Sbinkertn@umich.edu description=brief_copyright, 672889Sbinkertn@umich.edu formatter=optparse.TitledHelpFormatter()) 682890Sbinkertn@umich.eduparser.disable_interspersed_args() 692889Sbinkertn@umich.edu 702889Sbinkertn@umich.edu# current option group 712889Sbinkertn@umich.edugroup = None 722889Sbinkertn@umich.edu 732889Sbinkertn@umich.edudef set_group(*args, **kwargs): 742889Sbinkertn@umich.edu '''set the current option group''' 752889Sbinkertn@umich.edu global group 762889Sbinkertn@umich.edu if not args and not kwargs: 772889Sbinkertn@umich.edu group = None 782889Sbinkertn@umich.edu else: 792889Sbinkertn@umich.edu group = parser.add_option_group(*args, **kwargs) 802889Sbinkertn@umich.edu 812889Sbinkertn@umich.educlass splitter(object): 822889Sbinkertn@umich.edu def __init__(self, split): 832889Sbinkertn@umich.edu self.split = split 842889Sbinkertn@umich.edu def __call__(self, option, opt_str, value, parser): 852889Sbinkertn@umich.edu getattr(parser.values, option.dest).extend(value.split(self.split)) 862889Sbinkertn@umich.edu 872889Sbinkertn@umich.edudef add_option(*args, **kwargs): 882889Sbinkertn@umich.edu '''add an option to the current option group, or global none set''' 892889Sbinkertn@umich.edu 902889Sbinkertn@umich.edu # if action=split, but allows the option arguments 912889Sbinkertn@umich.edu # themselves to be lists separated by the split variable''' 922889Sbinkertn@umich.edu 932889Sbinkertn@umich.edu if kwargs.get('action', None) == 'append' and 'split' in kwargs: 942889Sbinkertn@umich.edu split = kwargs.pop('split') 952889Sbinkertn@umich.edu kwargs['default'] = [] 962889Sbinkertn@umich.edu kwargs['type'] = 'string' 972889Sbinkertn@umich.edu kwargs['action'] = 'callback' 982889Sbinkertn@umich.edu kwargs['callback'] = splitter(split) 992889Sbinkertn@umich.edu 1002889Sbinkertn@umich.edu if group: 1012889Sbinkertn@umich.edu return group.add_option(*args, **kwargs) 1022889Sbinkertn@umich.edu 1032889Sbinkertn@umich.edu return parser.add_option(*args, **kwargs) 1042889Sbinkertn@umich.edu 1052889Sbinkertn@umich.edudef bool_option(name, default, help): 1062889Sbinkertn@umich.edu '''add a boolean option called --name and --no-name. 1072889Sbinkertn@umich.edu Display help depending on which is the default''' 1082889Sbinkertn@umich.edu 1092889Sbinkertn@umich.edu tname = '--%s' % name 1102889Sbinkertn@umich.edu fname = '--no-%s' % name 1112889Sbinkertn@umich.edu dest = name.replace('-', '_') 1122889Sbinkertn@umich.edu if default: 1132889Sbinkertn@umich.edu thelp = optparse.SUPPRESS_HELP 1142889Sbinkertn@umich.edu fhelp = help 1152889Sbinkertn@umich.edu else: 1162889Sbinkertn@umich.edu thelp = help 1172889Sbinkertn@umich.edu fhelp = optparse.SUPPRESS_HELP 1182889Sbinkertn@umich.edu 1192889Sbinkertn@umich.edu add_option(tname, action="store_true", default=default, help=thelp) 1202889Sbinkertn@umich.edu add_option(fname, action="store_false", dest=dest, help=fhelp) 1212889Sbinkertn@umich.edu 1222889Sbinkertn@umich.edu# Help options 1232889Sbinkertn@umich.eduadd_option('-A', "--authors", action="store_true", default=False, 1242889Sbinkertn@umich.edu help="Show author information") 1254850Snate@binkert.orgadd_option('-B', "--build-info", action="store_true", default=False, 1264850Snate@binkert.org help="Show build information") 1272889Sbinkertn@umich.eduadd_option('-C', "--copyright", action="store_true", default=False, 1282889Sbinkertn@umich.edu help="Show full copyright information") 1292889Sbinkertn@umich.eduadd_option('-R', "--readme", action="store_true", default=False, 1302889Sbinkertn@umich.edu help="Show the readme") 1312889Sbinkertn@umich.eduadd_option('-N', "--release-notes", action="store_true", default=False, 1322889Sbinkertn@umich.edu help="Show the release notes") 1332889Sbinkertn@umich.edu 1342889Sbinkertn@umich.edu# Options for configuring the base simulator 1352889Sbinkertn@umich.eduadd_option('-d', "--outdir", metavar="DIR", default=".", 1362889Sbinkertn@umich.edu help="Set the output directory to DIR [Default: %default]") 1372889Sbinkertn@umich.eduadd_option('-i', "--interactive", action="store_true", default=False, 1382889Sbinkertn@umich.edu help="Invoke the interactive interpreter after running the script") 1392899Sbinkertn@umich.eduadd_option("--pdb", action="store_true", default=False, 1402899Sbinkertn@umich.edu help="Invoke the python debugger before running the script") 1412889Sbinkertn@umich.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':', 1422889Sbinkertn@umich.edu help="Prepend PATH to the system path when invoking the script") 1432889Sbinkertn@umich.eduadd_option('-q', "--quiet", action="count", default=0, 1442889Sbinkertn@umich.edu help="Reduce verbosity") 1452889Sbinkertn@umich.eduadd_option('-v', "--verbose", action="count", default=0, 1462889Sbinkertn@umich.edu help="Increase verbosity") 1472889Sbinkertn@umich.edu 1482889Sbinkertn@umich.edu# Statistics options 1492889Sbinkertn@umich.eduset_group("Statistics Options") 1502889Sbinkertn@umich.eduadd_option("--stats-file", metavar="FILE", default="m5stats.txt", 1512889Sbinkertn@umich.edu help="Sets the output file for statistics [Default: %default]") 1522889Sbinkertn@umich.edu 1532889Sbinkertn@umich.edu# Debugging options 1542889Sbinkertn@umich.eduset_group("Debugging Options") 1552889Sbinkertn@umich.eduadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',', 1562889Sbinkertn@umich.edu help="Cycle to create a breakpoint") 1572889Sbinkertn@umich.edu 1582889Sbinkertn@umich.edu# Tracing options 1592889Sbinkertn@umich.eduset_group("Trace Options") 1604053Sbinkertn@umich.eduadd_option("--trace-help", action='store_true', 1614053Sbinkertn@umich.edu help="Print help on trace flags") 1622889Sbinkertn@umich.eduadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',', 1634053Sbinkertn@umich.edu help="Sets the flags for tracing (-FLAG disables a flag)") 1644044Sbinkertn@umich.eduadd_option("--trace-start", metavar="TIME", type='int', 1654044Sbinkertn@umich.edu help="Start tracing at TIME (must be in ticks)") 1662889Sbinkertn@umich.eduadd_option("--trace-file", metavar="FILE", default="cout", 1672889Sbinkertn@umich.edu help="Sets the output file for tracing [Default: %default]") 1682889Sbinkertn@umich.eduadd_option("--trace-ignore", metavar="EXPR", action='append', split=':', 1692889Sbinkertn@umich.edu help="Ignore EXPR sim objects") 1702889Sbinkertn@umich.edu 1712889Sbinkertn@umich.eduoptions = attrdict() 1722889Sbinkertn@umich.eduarguments = [] 1732889Sbinkertn@umich.edu 1742889Sbinkertn@umich.edudef usage(exitcode=None): 1752903Ssaidi@eecs.umich.edu parser.print_help() 1762889Sbinkertn@umich.edu if exitcode is not None: 1772889Sbinkertn@umich.edu sys.exit(exitcode) 1782889Sbinkertn@umich.edu 1792889Sbinkertn@umich.edudef parse_args(): 1802889Sbinkertn@umich.edu _opts,args = parser.parse_args() 1812889Sbinkertn@umich.edu opts = attrdict(_opts.__dict__) 1822889Sbinkertn@umich.edu 1832889Sbinkertn@umich.edu # setting verbose and quiet at the same time doesn't make sense 1842889Sbinkertn@umich.edu if opts.verbose > 0 and opts.quiet > 0: 1852889Sbinkertn@umich.edu usage(2) 1862889Sbinkertn@umich.edu 1872889Sbinkertn@umich.edu # store the verbosity in a single variable. 0 is default, 1882889Sbinkertn@umich.edu # negative numbers represent quiet and positive values indicate verbose 1892889Sbinkertn@umich.edu opts.verbose -= opts.quiet 1902889Sbinkertn@umich.edu 1912889Sbinkertn@umich.edu del opts.quiet 1922889Sbinkertn@umich.edu 1932889Sbinkertn@umich.edu options.update(opts) 1942889Sbinkertn@umich.edu arguments.extend(args) 1952889Sbinkertn@umich.edu return opts,args 1962889Sbinkertn@umich.edu 1972889Sbinkertn@umich.edudef main(): 1984042Sbinkertn@umich.edu import defines 1994167Sbinkertn@umich.edu import event 2004042Sbinkertn@umich.edu import info 2013624Sbinkertn@umich.edu import internal 2022889Sbinkertn@umich.edu 2032889Sbinkertn@umich.edu parse_args() 2042889Sbinkertn@umich.edu 2052889Sbinkertn@umich.edu done = False 2064850Snate@binkert.org 2074850Snate@binkert.org if options.build_info: 2084850Snate@binkert.org done = True 2094850Snate@binkert.org print 'Build information:' 2104850Snate@binkert.org print 2114850Snate@binkert.org print 'compiled %s' % internal.core.cvar.compileDate; 2124850Snate@binkert.org print 'started %s' % datetime.datetime.now().ctime() 2134850Snate@binkert.org print 'executing on %s' % socket.gethostname() 2144850Snate@binkert.org print 'build options:' 2154850Snate@binkert.org keys = defines.m5_build_env.keys() 2164850Snate@binkert.org keys.sort() 2174850Snate@binkert.org for key in keys: 2184850Snate@binkert.org val = defines.m5_build_env[key] 2194850Snate@binkert.org print ' %s = %s' % (key, val) 2204850Snate@binkert.org print 2214850Snate@binkert.org 2222889Sbinkertn@umich.edu if options.copyright: 2232889Sbinkertn@umich.edu done = True 2242889Sbinkertn@umich.edu print info.LICENSE 2252889Sbinkertn@umich.edu print 2262889Sbinkertn@umich.edu 2272889Sbinkertn@umich.edu if options.authors: 2282889Sbinkertn@umich.edu done = True 2292889Sbinkertn@umich.edu print 'Author information:' 2302889Sbinkertn@umich.edu print 2312889Sbinkertn@umich.edu print info.AUTHORS 2322889Sbinkertn@umich.edu print 2332889Sbinkertn@umich.edu 2342889Sbinkertn@umich.edu if options.readme: 2352889Sbinkertn@umich.edu done = True 2362889Sbinkertn@umich.edu print 'Readme:' 2372889Sbinkertn@umich.edu print 2382889Sbinkertn@umich.edu print info.README 2392889Sbinkertn@umich.edu print 2402889Sbinkertn@umich.edu 2412889Sbinkertn@umich.edu if options.release_notes: 2422889Sbinkertn@umich.edu done = True 2432889Sbinkertn@umich.edu print 'Release Notes:' 2442889Sbinkertn@umich.edu print 2452889Sbinkertn@umich.edu print info.RELEASE_NOTES 2462889Sbinkertn@umich.edu print 2472889Sbinkertn@umich.edu 2484053Sbinkertn@umich.edu if options.trace_help: 2494053Sbinkertn@umich.edu done = True 2504053Sbinkertn@umich.edu print "Base Flags:" 2514053Sbinkertn@umich.edu print_list(traceflags.baseFlags, indent=4) 2524053Sbinkertn@umich.edu print 2534053Sbinkertn@umich.edu print "Compound Flags:" 2544053Sbinkertn@umich.edu for flag in traceflags.compoundFlags: 2554053Sbinkertn@umich.edu if flag == 'All': 2564053Sbinkertn@umich.edu continue 2574053Sbinkertn@umich.edu print " %s:" % flag 2584053Sbinkertn@umich.edu print_list(traceflags.compoundFlagMap[flag], indent=8) 2594053Sbinkertn@umich.edu print 2604053Sbinkertn@umich.edu 2612889Sbinkertn@umich.edu if done: 2622889Sbinkertn@umich.edu sys.exit(0) 2632889Sbinkertn@umich.edu 2642889Sbinkertn@umich.edu if options.verbose >= 0: 2652889Sbinkertn@umich.edu print "M5 Simulator System" 2662889Sbinkertn@umich.edu print brief_copyright 2672889Sbinkertn@umich.edu print 2684123Sbinkertn@umich.edu print "M5 compiled %s" % internal.core.cvar.compileDate; 2694850Snate@binkert.org print "M5 started %s" % datetime.datetime.now().ctime() 2702889Sbinkertn@umich.edu print "M5 executing on %s" % socket.gethostname() 2712967Sktlim@umich.edu print "command line:", 2722967Sktlim@umich.edu for argv in sys.argv: 2732967Sktlim@umich.edu print argv, 2742967Sktlim@umich.edu print 2752889Sbinkertn@umich.edu 2762889Sbinkertn@umich.edu # check to make sure we can find the listed script 2772889Sbinkertn@umich.edu if not arguments or not os.path.isfile(arguments[0]): 2782922Sktlim@umich.edu if arguments and not os.path.isfile(arguments[0]): 2792922Sktlim@umich.edu print "Script %s not found" % arguments[0] 2804053Sbinkertn@umich.edu 2812889Sbinkertn@umich.edu usage(2) 2822889Sbinkertn@umich.edu 2832889Sbinkertn@umich.edu # tell C++ about output directory 2844123Sbinkertn@umich.edu internal.core.setOutputDir(options.outdir) 2852889Sbinkertn@umich.edu 2862889Sbinkertn@umich.edu # update the system path with elements from the -p option 2872889Sbinkertn@umich.edu sys.path[0:0] = options.path 2882889Sbinkertn@umich.edu 2892889Sbinkertn@umich.edu import objects 2902889Sbinkertn@umich.edu 2912889Sbinkertn@umich.edu # set stats options 2924078Sbinkertn@umich.edu internal.stats.initText(options.stats_file) 2932889Sbinkertn@umich.edu 2942889Sbinkertn@umich.edu # set debugging options 2953645Sbinkertn@umich.edu for when in options.debug_break: 2963645Sbinkertn@umich.edu internal.debug.schedBreakCycle(int(when)) 2972889Sbinkertn@umich.edu 2984053Sbinkertn@umich.edu on_flags = [] 2994053Sbinkertn@umich.edu off_flags = [] 3004042Sbinkertn@umich.edu for flag in options.trace_flags: 3014053Sbinkertn@umich.edu off = False 3024053Sbinkertn@umich.edu if flag.startswith('-'): 3034053Sbinkertn@umich.edu flag = flag[1:] 3044053Sbinkertn@umich.edu off = True 3054053Sbinkertn@umich.edu if flag not in traceflags.allFlags: 3064053Sbinkertn@umich.edu print >>sys.stderr, "invalid trace flag '%s'" % flag 3074053Sbinkertn@umich.edu sys.exit(1) 3084053Sbinkertn@umich.edu 3094053Sbinkertn@umich.edu if off: 3104053Sbinkertn@umich.edu off_flags.append(flag) 3114053Sbinkertn@umich.edu else: 3124053Sbinkertn@umich.edu on_flags.append(flag) 3134053Sbinkertn@umich.edu 3144053Sbinkertn@umich.edu for flag in on_flags: 3154046Sbinkertn@umich.edu internal.trace.set(flag) 3164042Sbinkertn@umich.edu 3174053Sbinkertn@umich.edu for flag in off_flags: 3184053Sbinkertn@umich.edu internal.trace.clear(flag) 3194053Sbinkertn@umich.edu 3204074Sbinkertn@umich.edu if options.trace_start: 3214042Sbinkertn@umich.edu def enable_trace(): 3224074Sbinkertn@umich.edu internal.trace.cvar.enabled = True 3234167Sbinkertn@umich.edu event.create(enable_trace, int(options.trace_start)) 3244074Sbinkertn@umich.edu else: 3254087Sbinkertn@umich.edu internal.trace.cvar.enabled = True 3264042Sbinkertn@umich.edu 3274046Sbinkertn@umich.edu internal.trace.output(options.trace_file) 3284042Sbinkertn@umich.edu 3294042Sbinkertn@umich.edu for ignore in options.trace_ignore: 3304042Sbinkertn@umich.edu internal.trace.ignore(ignore) 3312889Sbinkertn@umich.edu 3322889Sbinkertn@umich.edu sys.argv = arguments 3332889Sbinkertn@umich.edu sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path 3342891Sbinkertn@umich.edu 3353887Sbinkertn@umich.edu scope = { '__file__' : sys.argv[0], 3363887Sbinkertn@umich.edu '__name__' : '__m5_main__' } 3372899Sbinkertn@umich.edu 3382899Sbinkertn@umich.edu # we want readline if we're doing anything interactive 3392899Sbinkertn@umich.edu if options.interactive or options.pdb: 3404042Sbinkertn@umich.edu exec "import readline" in scope 3412899Sbinkertn@umich.edu 3422899Sbinkertn@umich.edu # if pdb was requested, execfile the thing under pdb, otherwise, 3432899Sbinkertn@umich.edu # just do the execfile normally 3442899Sbinkertn@umich.edu if options.pdb: 3452899Sbinkertn@umich.edu from pdb import Pdb 3462899Sbinkertn@umich.edu debugger = Pdb() 3472899Sbinkertn@umich.edu debugger.run('execfile("%s")' % sys.argv[0], scope) 3482899Sbinkertn@umich.edu else: 3492899Sbinkertn@umich.edu execfile(sys.argv[0], scope) 3502889Sbinkertn@umich.edu 3512889Sbinkertn@umich.edu # once the script is done 3522889Sbinkertn@umich.edu if options.interactive: 3532889Sbinkertn@umich.edu interact = code.InteractiveConsole(scope) 3542889Sbinkertn@umich.edu interact.interact("M5 Interactive Console") 3552889Sbinkertn@umich.edu 3562889Sbinkertn@umich.eduif __name__ == '__main__': 3572889Sbinkertn@umich.edu from pprint import pprint 3582889Sbinkertn@umich.edu 3592889Sbinkertn@umich.edu parse_args() 3602889Sbinkertn@umich.edu 3612889Sbinkertn@umich.edu print 'opts:' 3622889Sbinkertn@umich.edu pprint(options, indent=4) 3632889Sbinkertn@umich.edu print 3642889Sbinkertn@umich.edu 3652889Sbinkertn@umich.edu print 'args:' 3662889Sbinkertn@umich.edu pprint(arguments, indent=4) 367