main.py revision 2967
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)") 1472889Sbinkertn@umich.eduadd_option("--trace-file", metavar="FILE", default="cout", 1482889Sbinkertn@umich.edu help="Sets the output file for tracing [Default: %default]") 1492889Sbinkertn@umich.eduadd_option("--trace-circlebuf", metavar="SIZE", type="int", default=0, 1502889Sbinkertn@umich.edu help="If SIZE is non-zero, turn on the circular buffer with SIZE lines") 1512889Sbinkertn@umich.eduadd_option("--no-trace-circlebuf", action="store_const", const=0, 1522889Sbinkertn@umich.edu dest='trace_circlebuf', help=optparse.SUPPRESS_HELP) 1532889Sbinkertn@umich.edubool_option("trace-dumponexit", default=False, 1542889Sbinkertn@umich.edu help="Dump trace buffer on exit") 1552889Sbinkertn@umich.eduadd_option("--trace-ignore", metavar="EXPR", action='append', split=':', 1562889Sbinkertn@umich.edu help="Ignore EXPR sim objects") 1572889Sbinkertn@umich.edu 1582889Sbinkertn@umich.edu# Execution Trace options 1592889Sbinkertn@umich.eduset_group("Execution Trace Options") 1602889Sbinkertn@umich.edubool_option("speculative", default=True, 1612889Sbinkertn@umich.edu help="Don't capture speculative instructions") 1622889Sbinkertn@umich.edubool_option("print-cycle", default=True, 1632889Sbinkertn@umich.edu help="Don't print cycle numbers in trace output") 1642889Sbinkertn@umich.edubool_option("print-symbol", default=True, 1652889Sbinkertn@umich.edu help="Disable PC symbols in trace output") 1662889Sbinkertn@umich.edubool_option("print-opclass", default=True, 1672889Sbinkertn@umich.edu help="Don't print op class type in trace output") 1682889Sbinkertn@umich.edubool_option("print-thread", default=True, 1692889Sbinkertn@umich.edu help="Don't print thread number in trace output") 1702889Sbinkertn@umich.edubool_option("print-effaddr", default=True, 1712889Sbinkertn@umich.edu help="Don't print effective address in trace output") 1722889Sbinkertn@umich.edubool_option("print-data", default=True, 1732889Sbinkertn@umich.edu help="Don't print result data in trace output") 1742889Sbinkertn@umich.edubool_option("print-iregs", default=False, 1752889Sbinkertn@umich.edu help="Print fetch sequence numbers in trace output") 1762889Sbinkertn@umich.edubool_option("print-fetch-seq", default=False, 1772889Sbinkertn@umich.edu help="Print fetch sequence numbers in trace output") 1782889Sbinkertn@umich.edubool_option("print-cpseq", default=False, 1792889Sbinkertn@umich.edu help="Print correct path sequence numbers in trace output") 1802889Sbinkertn@umich.edu 1812889Sbinkertn@umich.eduoptions = attrdict() 1822889Sbinkertn@umich.eduarguments = [] 1832889Sbinkertn@umich.edu 1842889Sbinkertn@umich.edudef usage(exitcode=None): 1852903Ssaidi@eecs.umich.edu parser.print_help() 1862889Sbinkertn@umich.edu if exitcode is not None: 1872889Sbinkertn@umich.edu sys.exit(exitcode) 1882889Sbinkertn@umich.edu 1892889Sbinkertn@umich.edudef parse_args(): 1902889Sbinkertn@umich.edu _opts,args = parser.parse_args() 1912889Sbinkertn@umich.edu opts = attrdict(_opts.__dict__) 1922889Sbinkertn@umich.edu 1932889Sbinkertn@umich.edu # setting verbose and quiet at the same time doesn't make sense 1942889Sbinkertn@umich.edu if opts.verbose > 0 and opts.quiet > 0: 1952889Sbinkertn@umich.edu usage(2) 1962889Sbinkertn@umich.edu 1972889Sbinkertn@umich.edu # store the verbosity in a single variable. 0 is default, 1982889Sbinkertn@umich.edu # negative numbers represent quiet and positive values indicate verbose 1992889Sbinkertn@umich.edu opts.verbose -= opts.quiet 2002889Sbinkertn@umich.edu 2012889Sbinkertn@umich.edu del opts.quiet 2022889Sbinkertn@umich.edu 2032889Sbinkertn@umich.edu options.update(opts) 2042889Sbinkertn@umich.edu arguments.extend(args) 2052889Sbinkertn@umich.edu return opts,args 2062889Sbinkertn@umich.edu 2072889Sbinkertn@umich.edudef main(): 2082889Sbinkertn@umich.edu import cc_main 2092889Sbinkertn@umich.edu 2102889Sbinkertn@umich.edu parse_args() 2112889Sbinkertn@umich.edu 2122889Sbinkertn@umich.edu done = False 2132889Sbinkertn@umich.edu if options.copyright: 2142889Sbinkertn@umich.edu done = True 2152889Sbinkertn@umich.edu print info.LICENSE 2162889Sbinkertn@umich.edu print 2172889Sbinkertn@umich.edu 2182889Sbinkertn@umich.edu if options.authors: 2192889Sbinkertn@umich.edu done = True 2202889Sbinkertn@umich.edu print 'Author information:' 2212889Sbinkertn@umich.edu print 2222889Sbinkertn@umich.edu print info.AUTHORS 2232889Sbinkertn@umich.edu print 2242889Sbinkertn@umich.edu 2252889Sbinkertn@umich.edu if options.readme: 2262889Sbinkertn@umich.edu done = True 2272889Sbinkertn@umich.edu print 'Readme:' 2282889Sbinkertn@umich.edu print 2292889Sbinkertn@umich.edu print info.README 2302889Sbinkertn@umich.edu print 2312889Sbinkertn@umich.edu 2322889Sbinkertn@umich.edu if options.release_notes: 2332889Sbinkertn@umich.edu done = True 2342889Sbinkertn@umich.edu print 'Release Notes:' 2352889Sbinkertn@umich.edu print 2362889Sbinkertn@umich.edu print info.RELEASE_NOTES 2372889Sbinkertn@umich.edu print 2382889Sbinkertn@umich.edu 2392889Sbinkertn@umich.edu if done: 2402889Sbinkertn@umich.edu sys.exit(0) 2412889Sbinkertn@umich.edu 2422889Sbinkertn@umich.edu if options.verbose >= 0: 2432889Sbinkertn@umich.edu print "M5 Simulator System" 2442889Sbinkertn@umich.edu print brief_copyright 2452889Sbinkertn@umich.edu print 2462889Sbinkertn@umich.edu print "M5 compiled %s" % cc_main.cvar.compileDate; 2472889Sbinkertn@umich.edu print "M5 started %s" % datetime.now().ctime() 2482889Sbinkertn@umich.edu print "M5 executing on %s" % socket.gethostname() 2492967Sktlim@umich.edu print "command line:", 2502967Sktlim@umich.edu for argv in sys.argv: 2512967Sktlim@umich.edu print argv, 2522967Sktlim@umich.edu print 2532889Sbinkertn@umich.edu 2542889Sbinkertn@umich.edu # check to make sure we can find the listed script 2552889Sbinkertn@umich.edu if not arguments or not os.path.isfile(arguments[0]): 2562922Sktlim@umich.edu if arguments and not os.path.isfile(arguments[0]): 2572922Sktlim@umich.edu print "Script %s not found" % arguments[0] 2582889Sbinkertn@umich.edu usage(2) 2592889Sbinkertn@umich.edu 2602889Sbinkertn@umich.edu # tell C++ about output directory 2612889Sbinkertn@umich.edu cc_main.setOutputDir(options.outdir) 2622889Sbinkertn@umich.edu 2632889Sbinkertn@umich.edu # update the system path with elements from the -p option 2642889Sbinkertn@umich.edu sys.path[0:0] = options.path 2652889Sbinkertn@umich.edu 2662889Sbinkertn@umich.edu import objects 2672889Sbinkertn@umich.edu 2682889Sbinkertn@umich.edu # set stats options 2692889Sbinkertn@umich.edu objects.Statistics.text_file = options.stats_file 2702889Sbinkertn@umich.edu 2712889Sbinkertn@umich.edu # set debugging options 2722889Sbinkertn@umich.edu objects.Debug.break_cycles = options.debug_break 2732889Sbinkertn@umich.edu 2742889Sbinkertn@umich.edu # set tracing options 2752889Sbinkertn@umich.edu objects.Trace.flags = options.trace_flags 2762889Sbinkertn@umich.edu objects.Trace.start = options.trace_start 2772889Sbinkertn@umich.edu objects.Trace.file = options.trace_file 2782889Sbinkertn@umich.edu objects.Trace.bufsize = options.trace_circlebuf 2792889Sbinkertn@umich.edu objects.Trace.dump_on_exit = options.trace_dumponexit 2802889Sbinkertn@umich.edu objects.Trace.ignore = options.trace_ignore 2812889Sbinkertn@umich.edu 2822889Sbinkertn@umich.edu # set execution trace options 2832889Sbinkertn@umich.edu objects.ExecutionTrace.speculative = options.speculative 2842889Sbinkertn@umich.edu objects.ExecutionTrace.print_cycle = options.print_cycle 2852889Sbinkertn@umich.edu objects.ExecutionTrace.pc_symbol = options.print_symbol 2862889Sbinkertn@umich.edu objects.ExecutionTrace.print_opclass = options.print_opclass 2872889Sbinkertn@umich.edu objects.ExecutionTrace.print_thread = options.print_thread 2882889Sbinkertn@umich.edu objects.ExecutionTrace.print_effaddr = options.print_effaddr 2892889Sbinkertn@umich.edu objects.ExecutionTrace.print_data = options.print_data 2902889Sbinkertn@umich.edu objects.ExecutionTrace.print_iregs = options.print_iregs 2912889Sbinkertn@umich.edu objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq 2922889Sbinkertn@umich.edu objects.ExecutionTrace.print_cpseq = options.print_cpseq 2932889Sbinkertn@umich.edu 2942889Sbinkertn@umich.edu sys.argv = arguments 2952889Sbinkertn@umich.edu sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path 2962891Sbinkertn@umich.edu 2972891Sbinkertn@umich.edu scope = { '__file__' : sys.argv[0] } 2982899Sbinkertn@umich.edu 2992899Sbinkertn@umich.edu # we want readline if we're doing anything interactive 3002899Sbinkertn@umich.edu if options.interactive or options.pdb: 3012899Sbinkertn@umich.edu exec("import readline", scope) 3022899Sbinkertn@umich.edu 3032899Sbinkertn@umich.edu # if pdb was requested, execfile the thing under pdb, otherwise, 3042899Sbinkertn@umich.edu # just do the execfile normally 3052899Sbinkertn@umich.edu if options.pdb: 3062899Sbinkertn@umich.edu from pdb import Pdb 3072899Sbinkertn@umich.edu debugger = Pdb() 3082899Sbinkertn@umich.edu debugger.run('execfile("%s")' % sys.argv[0], scope) 3092899Sbinkertn@umich.edu else: 3102899Sbinkertn@umich.edu execfile(sys.argv[0], scope) 3112889Sbinkertn@umich.edu 3122889Sbinkertn@umich.edu # once the script is done 3132889Sbinkertn@umich.edu if options.interactive: 3142889Sbinkertn@umich.edu interact = code.InteractiveConsole(scope) 3152889Sbinkertn@umich.edu interact.interact("M5 Interactive Console") 3162889Sbinkertn@umich.edu 3172889Sbinkertn@umich.eduif __name__ == '__main__': 3182889Sbinkertn@umich.edu from pprint import pprint 3192889Sbinkertn@umich.edu 3202889Sbinkertn@umich.edu parse_args() 3212889Sbinkertn@umich.edu 3222889Sbinkertn@umich.edu print 'opts:' 3232889Sbinkertn@umich.edu pprint(options, indent=4) 3242889Sbinkertn@umich.edu print 3252889Sbinkertn@umich.edu 3262889Sbinkertn@umich.edu print 'args:' 3272889Sbinkertn@umich.edu pprint(arguments, indent=4) 328