main.py revision 3131
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 292889Sbinkertn@umich.eduimport code, optparse, os, socket, sys 302889Sbinkertn@umich.edufrom datetime import datetime 312889Sbinkertn@umich.edufrom attrdict import attrdict 322889Sbinkertn@umich.edu 332889Sbinkertn@umich.edutry: 342889Sbinkertn@umich.edu import info 352889Sbinkertn@umich.eduexcept ImportError: 362889Sbinkertn@umich.edu info = None 372889Sbinkertn@umich.edu 382889Sbinkertn@umich.edu__all__ = [ 'options', 'arguments', 'main' ] 392889Sbinkertn@umich.edu 402889Sbinkertn@umich.eduusage="%prog [m5 options] script.py [script options]" 412889Sbinkertn@umich.eduversion="%prog 2.0" 422889Sbinkertn@umich.edubrief_copyright=''' 432889Sbinkertn@umich.eduCopyright (c) 2001-2006 442889Sbinkertn@umich.eduThe Regents of The University of Michigan 452889Sbinkertn@umich.eduAll Rights Reserved 462889Sbinkertn@umich.edu''' 472889Sbinkertn@umich.edu 482889Sbinkertn@umich.edu# there's only one option parsing done, so make it global and add some 492889Sbinkertn@umich.edu# helper functions to make it work well. 502889Sbinkertn@umich.eduparser = optparse.OptionParser(usage=usage, version=version, 512889Sbinkertn@umich.edu description=brief_copyright, 522889Sbinkertn@umich.edu formatter=optparse.TitledHelpFormatter()) 532890Sbinkertn@umich.eduparser.disable_interspersed_args() 542889Sbinkertn@umich.edu 552889Sbinkertn@umich.edu# current option group 562889Sbinkertn@umich.edugroup = None 572889Sbinkertn@umich.edu 582889Sbinkertn@umich.edudef set_group(*args, **kwargs): 592889Sbinkertn@umich.edu '''set the current option group''' 602889Sbinkertn@umich.edu global group 612889Sbinkertn@umich.edu if not args and not kwargs: 622889Sbinkertn@umich.edu group = None 632889Sbinkertn@umich.edu else: 642889Sbinkertn@umich.edu group = parser.add_option_group(*args, **kwargs) 652889Sbinkertn@umich.edu 662889Sbinkertn@umich.educlass splitter(object): 672889Sbinkertn@umich.edu def __init__(self, split): 682889Sbinkertn@umich.edu self.split = split 692889Sbinkertn@umich.edu def __call__(self, option, opt_str, value, parser): 702889Sbinkertn@umich.edu getattr(parser.values, option.dest).extend(value.split(self.split)) 712889Sbinkertn@umich.edu 722889Sbinkertn@umich.edudef add_option(*args, **kwargs): 732889Sbinkertn@umich.edu '''add an option to the current option group, or global none set''' 742889Sbinkertn@umich.edu 752889Sbinkertn@umich.edu # if action=split, but allows the option arguments 762889Sbinkertn@umich.edu # themselves to be lists separated by the split variable''' 772889Sbinkertn@umich.edu 782889Sbinkertn@umich.edu if kwargs.get('action', None) == 'append' and 'split' in kwargs: 792889Sbinkertn@umich.edu split = kwargs.pop('split') 802889Sbinkertn@umich.edu kwargs['default'] = [] 812889Sbinkertn@umich.edu kwargs['type'] = 'string' 822889Sbinkertn@umich.edu kwargs['action'] = 'callback' 832889Sbinkertn@umich.edu kwargs['callback'] = splitter(split) 842889Sbinkertn@umich.edu 852889Sbinkertn@umich.edu if group: 862889Sbinkertn@umich.edu return group.add_option(*args, **kwargs) 872889Sbinkertn@umich.edu 882889Sbinkertn@umich.edu return parser.add_option(*args, **kwargs) 892889Sbinkertn@umich.edu 902889Sbinkertn@umich.edudef bool_option(name, default, help): 912889Sbinkertn@umich.edu '''add a boolean option called --name and --no-name. 922889Sbinkertn@umich.edu Display help depending on which is the default''' 932889Sbinkertn@umich.edu 942889Sbinkertn@umich.edu tname = '--%s' % name 952889Sbinkertn@umich.edu fname = '--no-%s' % name 962889Sbinkertn@umich.edu dest = name.replace('-', '_') 972889Sbinkertn@umich.edu if default: 982889Sbinkertn@umich.edu thelp = optparse.SUPPRESS_HELP 992889Sbinkertn@umich.edu fhelp = help 1002889Sbinkertn@umich.edu else: 1012889Sbinkertn@umich.edu thelp = help 1022889Sbinkertn@umich.edu fhelp = optparse.SUPPRESS_HELP 1032889Sbinkertn@umich.edu 1042889Sbinkertn@umich.edu add_option(tname, action="store_true", default=default, help=thelp) 1052889Sbinkertn@umich.edu add_option(fname, action="store_false", dest=dest, help=fhelp) 1062889Sbinkertn@umich.edu 1072889Sbinkertn@umich.edu# Help options 1082889Sbinkertn@umich.eduadd_option('-A', "--authors", action="store_true", default=False, 1092889Sbinkertn@umich.edu help="Show author information") 1102889Sbinkertn@umich.eduadd_option('-C', "--copyright", action="store_true", default=False, 1112889Sbinkertn@umich.edu help="Show full copyright information") 1122889Sbinkertn@umich.eduadd_option('-R', "--readme", action="store_true", default=False, 1132889Sbinkertn@umich.edu help="Show the readme") 1142889Sbinkertn@umich.eduadd_option('-N', "--release-notes", action="store_true", default=False, 1152889Sbinkertn@umich.edu help="Show the release notes") 1162889Sbinkertn@umich.edu 1172889Sbinkertn@umich.edu# Options for configuring the base simulator 1182889Sbinkertn@umich.eduadd_option('-d', "--outdir", metavar="DIR", default=".", 1192889Sbinkertn@umich.edu help="Set the output directory to DIR [Default: %default]") 1202889Sbinkertn@umich.eduadd_option('-i', "--interactive", action="store_true", default=False, 1212889Sbinkertn@umich.edu help="Invoke the interactive interpreter after running the script") 1222899Sbinkertn@umich.eduadd_option("--pdb", action="store_true", default=False, 1232899Sbinkertn@umich.edu help="Invoke the python debugger before running the script") 1242889Sbinkertn@umich.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':', 1252889Sbinkertn@umich.edu help="Prepend PATH to the system path when invoking the script") 1262889Sbinkertn@umich.eduadd_option('-q', "--quiet", action="count", default=0, 1272889Sbinkertn@umich.edu help="Reduce verbosity") 1282889Sbinkertn@umich.eduadd_option('-v', "--verbose", action="count", default=0, 1292889Sbinkertn@umich.edu help="Increase verbosity") 1302889Sbinkertn@umich.edu 1312889Sbinkertn@umich.edu# Statistics options 1322889Sbinkertn@umich.eduset_group("Statistics Options") 1332889Sbinkertn@umich.eduadd_option("--stats-file", metavar="FILE", default="m5stats.txt", 1342889Sbinkertn@umich.edu help="Sets the output file for statistics [Default: %default]") 1352889Sbinkertn@umich.edu 1362889Sbinkertn@umich.edu# Debugging options 1372889Sbinkertn@umich.eduset_group("Debugging Options") 1382889Sbinkertn@umich.eduadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',', 1392889Sbinkertn@umich.edu help="Cycle to create a breakpoint") 1402889Sbinkertn@umich.edu 1412889Sbinkertn@umich.edu# Tracing options 1422889Sbinkertn@umich.eduset_group("Trace Options") 1432889Sbinkertn@umich.eduadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',', 1442889Sbinkertn@umich.edu help="Sets the flags for tracing") 1452889Sbinkertn@umich.eduadd_option("--trace-start", metavar="TIME", default='0s', 1462889Sbinkertn@umich.edu help="Start tracing at TIME (must have units)") 1473127Sktlim@umich.eduadd_option("--trace-cycle", metavar="CYCLE", default='0', 1483127Sktlim@umich.edu help="Start tracing at CYCLE") 1492889Sbinkertn@umich.eduadd_option("--trace-file", metavar="FILE", default="cout", 1502889Sbinkertn@umich.edu help="Sets the output file for tracing [Default: %default]") 1512889Sbinkertn@umich.eduadd_option("--trace-circlebuf", metavar="SIZE", type="int", default=0, 1522889Sbinkertn@umich.edu help="If SIZE is non-zero, turn on the circular buffer with SIZE lines") 1532889Sbinkertn@umich.eduadd_option("--no-trace-circlebuf", action="store_const", const=0, 1542889Sbinkertn@umich.edu dest='trace_circlebuf', help=optparse.SUPPRESS_HELP) 1552889Sbinkertn@umich.edubool_option("trace-dumponexit", default=False, 1562889Sbinkertn@umich.edu help="Dump trace buffer on exit") 1572889Sbinkertn@umich.eduadd_option("--trace-ignore", metavar="EXPR", action='append', split=':', 1582889Sbinkertn@umich.edu help="Ignore EXPR sim objects") 1592889Sbinkertn@umich.edu 1602889Sbinkertn@umich.edu# Execution Trace options 1612889Sbinkertn@umich.eduset_group("Execution Trace Options") 1622889Sbinkertn@umich.edubool_option("speculative", default=True, 1632889Sbinkertn@umich.edu help="Don't capture speculative instructions") 1642889Sbinkertn@umich.edubool_option("print-cycle", default=True, 1652889Sbinkertn@umich.edu help="Don't print cycle numbers in trace output") 1662889Sbinkertn@umich.edubool_option("print-symbol", default=True, 1672889Sbinkertn@umich.edu help="Disable PC symbols in trace output") 1682889Sbinkertn@umich.edubool_option("print-opclass", default=True, 1692889Sbinkertn@umich.edu help="Don't print op class type in trace output") 1702889Sbinkertn@umich.edubool_option("print-thread", default=True, 1712889Sbinkertn@umich.edu help="Don't print thread number in trace output") 1722889Sbinkertn@umich.edubool_option("print-effaddr", default=True, 1732889Sbinkertn@umich.edu help="Don't print effective address in trace output") 1742889Sbinkertn@umich.edubool_option("print-data", default=True, 1752889Sbinkertn@umich.edu help="Don't print result data in trace output") 1762889Sbinkertn@umich.edubool_option("print-iregs", default=False, 1772889Sbinkertn@umich.edu help="Print fetch sequence numbers in trace output") 1782889Sbinkertn@umich.edubool_option("print-fetch-seq", default=False, 1792889Sbinkertn@umich.edu help="Print fetch sequence numbers in trace output") 1802889Sbinkertn@umich.edubool_option("print-cpseq", default=False, 1812889Sbinkertn@umich.edu help="Print correct path sequence numbers in trace output") 1822973Sgblack@eecs.umich.edu#bool_option("print-reg-delta", default=False, 1832973Sgblack@eecs.umich.edu# help="Print which registers changed to what in trace output") 1842889Sbinkertn@umich.edu 1852889Sbinkertn@umich.eduoptions = attrdict() 1862889Sbinkertn@umich.eduarguments = [] 1872889Sbinkertn@umich.edu 1882889Sbinkertn@umich.edudef usage(exitcode=None): 1892903Ssaidi@eecs.umich.edu parser.print_help() 1902889Sbinkertn@umich.edu if exitcode is not None: 1912889Sbinkertn@umich.edu sys.exit(exitcode) 1922889Sbinkertn@umich.edu 1932889Sbinkertn@umich.edudef parse_args(): 1942889Sbinkertn@umich.edu _opts,args = parser.parse_args() 1952889Sbinkertn@umich.edu opts = attrdict(_opts.__dict__) 1962889Sbinkertn@umich.edu 1972889Sbinkertn@umich.edu # setting verbose and quiet at the same time doesn't make sense 1982889Sbinkertn@umich.edu if opts.verbose > 0 and opts.quiet > 0: 1992889Sbinkertn@umich.edu usage(2) 2002889Sbinkertn@umich.edu 2012889Sbinkertn@umich.edu # store the verbosity in a single variable. 0 is default, 2022889Sbinkertn@umich.edu # negative numbers represent quiet and positive values indicate verbose 2032889Sbinkertn@umich.edu opts.verbose -= opts.quiet 2042889Sbinkertn@umich.edu 2052889Sbinkertn@umich.edu del opts.quiet 2062889Sbinkertn@umich.edu 2072889Sbinkertn@umich.edu options.update(opts) 2082889Sbinkertn@umich.edu arguments.extend(args) 2092889Sbinkertn@umich.edu return opts,args 2102889Sbinkertn@umich.edu 2112889Sbinkertn@umich.edudef main(): 2122889Sbinkertn@umich.edu import cc_main 2132889Sbinkertn@umich.edu 2142889Sbinkertn@umich.edu parse_args() 2152889Sbinkertn@umich.edu 2162889Sbinkertn@umich.edu done = False 2172889Sbinkertn@umich.edu if options.copyright: 2182889Sbinkertn@umich.edu done = True 2192889Sbinkertn@umich.edu print info.LICENSE 2202889Sbinkertn@umich.edu print 2212889Sbinkertn@umich.edu 2222889Sbinkertn@umich.edu if options.authors: 2232889Sbinkertn@umich.edu done = True 2242889Sbinkertn@umich.edu print 'Author information:' 2252889Sbinkertn@umich.edu print 2262889Sbinkertn@umich.edu print info.AUTHORS 2272889Sbinkertn@umich.edu print 2282889Sbinkertn@umich.edu 2292889Sbinkertn@umich.edu if options.readme: 2302889Sbinkertn@umich.edu done = True 2312889Sbinkertn@umich.edu print 'Readme:' 2322889Sbinkertn@umich.edu print 2332889Sbinkertn@umich.edu print info.README 2342889Sbinkertn@umich.edu print 2352889Sbinkertn@umich.edu 2362889Sbinkertn@umich.edu if options.release_notes: 2372889Sbinkertn@umich.edu done = True 2382889Sbinkertn@umich.edu print 'Release Notes:' 2392889Sbinkertn@umich.edu print 2402889Sbinkertn@umich.edu print info.RELEASE_NOTES 2412889Sbinkertn@umich.edu print 2422889Sbinkertn@umich.edu 2432889Sbinkertn@umich.edu if done: 2442889Sbinkertn@umich.edu sys.exit(0) 2452889Sbinkertn@umich.edu 2462889Sbinkertn@umich.edu if options.verbose >= 0: 2472889Sbinkertn@umich.edu print "M5 Simulator System" 2482889Sbinkertn@umich.edu print brief_copyright 2492889Sbinkertn@umich.edu print 2502889Sbinkertn@umich.edu print "M5 compiled %s" % cc_main.cvar.compileDate; 2512889Sbinkertn@umich.edu print "M5 started %s" % datetime.now().ctime() 2522889Sbinkertn@umich.edu print "M5 executing on %s" % socket.gethostname() 2532967Sktlim@umich.edu print "command line:", 2542967Sktlim@umich.edu for argv in sys.argv: 2552967Sktlim@umich.edu print argv, 2562967Sktlim@umich.edu print 2572889Sbinkertn@umich.edu 2582889Sbinkertn@umich.edu # check to make sure we can find the listed script 2592889Sbinkertn@umich.edu if not arguments or not os.path.isfile(arguments[0]): 2602922Sktlim@umich.edu if arguments and not os.path.isfile(arguments[0]): 2612922Sktlim@umich.edu print "Script %s not found" % arguments[0] 2622889Sbinkertn@umich.edu usage(2) 2632889Sbinkertn@umich.edu 2642889Sbinkertn@umich.edu # tell C++ about output directory 2652889Sbinkertn@umich.edu cc_main.setOutputDir(options.outdir) 2662889Sbinkertn@umich.edu 2672889Sbinkertn@umich.edu # update the system path with elements from the -p option 2682889Sbinkertn@umich.edu sys.path[0:0] = options.path 2692889Sbinkertn@umich.edu 2702889Sbinkertn@umich.edu import objects 2712889Sbinkertn@umich.edu 2722889Sbinkertn@umich.edu # set stats options 2732889Sbinkertn@umich.edu objects.Statistics.text_file = options.stats_file 2742889Sbinkertn@umich.edu 2752889Sbinkertn@umich.edu # set debugging options 2762889Sbinkertn@umich.edu objects.Debug.break_cycles = options.debug_break 2772889Sbinkertn@umich.edu 2782889Sbinkertn@umich.edu # set tracing options 2792889Sbinkertn@umich.edu objects.Trace.flags = options.trace_flags 2802889Sbinkertn@umich.edu objects.Trace.start = options.trace_start 2813131Sktlim@umich.edu objects.Trace.cycle = options.trace_cycle 2822889Sbinkertn@umich.edu objects.Trace.file = options.trace_file 2832889Sbinkertn@umich.edu objects.Trace.bufsize = options.trace_circlebuf 2842889Sbinkertn@umich.edu objects.Trace.dump_on_exit = options.trace_dumponexit 2852889Sbinkertn@umich.edu objects.Trace.ignore = options.trace_ignore 2862889Sbinkertn@umich.edu 2872889Sbinkertn@umich.edu # set execution trace options 2882889Sbinkertn@umich.edu objects.ExecutionTrace.speculative = options.speculative 2892889Sbinkertn@umich.edu objects.ExecutionTrace.print_cycle = options.print_cycle 2902889Sbinkertn@umich.edu objects.ExecutionTrace.pc_symbol = options.print_symbol 2912889Sbinkertn@umich.edu objects.ExecutionTrace.print_opclass = options.print_opclass 2922889Sbinkertn@umich.edu objects.ExecutionTrace.print_thread = options.print_thread 2932889Sbinkertn@umich.edu objects.ExecutionTrace.print_effaddr = options.print_effaddr 2942889Sbinkertn@umich.edu objects.ExecutionTrace.print_data = options.print_data 2952889Sbinkertn@umich.edu objects.ExecutionTrace.print_iregs = options.print_iregs 2962889Sbinkertn@umich.edu objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq 2972889Sbinkertn@umich.edu objects.ExecutionTrace.print_cpseq = options.print_cpseq 2982973Sgblack@eecs.umich.edu #objects.ExecutionTrace.print_reg_delta = options.print_reg_delta 2992889Sbinkertn@umich.edu 3002889Sbinkertn@umich.edu sys.argv = arguments 3012889Sbinkertn@umich.edu sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path 3022891Sbinkertn@umich.edu 3032891Sbinkertn@umich.edu scope = { '__file__' : sys.argv[0] } 3042899Sbinkertn@umich.edu 3052899Sbinkertn@umich.edu # we want readline if we're doing anything interactive 3062899Sbinkertn@umich.edu if options.interactive or options.pdb: 3072899Sbinkertn@umich.edu exec("import readline", scope) 3082899Sbinkertn@umich.edu 3092899Sbinkertn@umich.edu # if pdb was requested, execfile the thing under pdb, otherwise, 3102899Sbinkertn@umich.edu # just do the execfile normally 3112899Sbinkertn@umich.edu if options.pdb: 3122899Sbinkertn@umich.edu from pdb import Pdb 3132899Sbinkertn@umich.edu debugger = Pdb() 3142899Sbinkertn@umich.edu debugger.run('execfile("%s")' % sys.argv[0], scope) 3152899Sbinkertn@umich.edu else: 3162899Sbinkertn@umich.edu execfile(sys.argv[0], scope) 3172889Sbinkertn@umich.edu 3182889Sbinkertn@umich.edu # once the script is done 3192889Sbinkertn@umich.edu if options.interactive: 3202889Sbinkertn@umich.edu interact = code.InteractiveConsole(scope) 3212889Sbinkertn@umich.edu interact.interact("M5 Interactive Console") 3222889Sbinkertn@umich.edu 3232889Sbinkertn@umich.eduif __name__ == '__main__': 3242889Sbinkertn@umich.edu from pprint import pprint 3252889Sbinkertn@umich.edu 3262889Sbinkertn@umich.edu parse_args() 3272889Sbinkertn@umich.edu 3282889Sbinkertn@umich.edu print 'opts:' 3292889Sbinkertn@umich.edu pprint(options, indent=4) 3302889Sbinkertn@umich.edu print 3312889Sbinkertn@umich.edu 3322889Sbinkertn@umich.edu print 'args:' 3332889Sbinkertn@umich.edu pprint(arguments, indent=4) 334