main.py revision 6654
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 356654Snate@binkert.orgfrom util import attrdict, fatal 365471Snate@binkert.orgimport config 375470Snate@binkert.orgfrom options import OptionParser 382889Sbinkertn@umich.edu 392889Sbinkertn@umich.edu__all__ = [ 'options', 'arguments', 'main' ] 402889Sbinkertn@umich.edu 415470Snate@binkert.orgusage="%prog [m5 options] script.py [script options]" 425470Snate@binkert.orgversion="%prog 2.0" 435470Snate@binkert.orgbrief_copyright=''' 445470Snate@binkert.orgCopyright (c) 2001-2008 455470Snate@binkert.orgThe Regents of The University of Michigan 465470Snate@binkert.orgAll Rights Reserved 475470Snate@binkert.org''' 482889Sbinkertn@umich.edu 495470Snate@binkert.orgoptions = OptionParser(usage=usage, version=version, 505470Snate@binkert.org description=brief_copyright) 515470Snate@binkert.orgadd_option = options.add_option 525470Snate@binkert.orgset_group = options.set_group 535470Snate@binkert.orgusage = options.usage 542889Sbinkertn@umich.edu 552889Sbinkertn@umich.edu# Help options 562889Sbinkertn@umich.eduadd_option('-A', "--authors", action="store_true", default=False, 572889Sbinkertn@umich.edu help="Show author information") 584850Snate@binkert.orgadd_option('-B', "--build-info", action="store_true", default=False, 594850Snate@binkert.org help="Show build information") 602889Sbinkertn@umich.eduadd_option('-C', "--copyright", action="store_true", default=False, 612889Sbinkertn@umich.edu help="Show full copyright information") 622889Sbinkertn@umich.eduadd_option('-R', "--readme", action="store_true", default=False, 632889Sbinkertn@umich.edu help="Show the readme") 642889Sbinkertn@umich.eduadd_option('-N', "--release-notes", action="store_true", default=False, 652889Sbinkertn@umich.edu help="Show the release notes") 662889Sbinkertn@umich.edu 672889Sbinkertn@umich.edu# Options for configuring the base simulator 685773Snate@binkert.orgadd_option('-d', "--outdir", metavar="DIR", default="m5out", 692889Sbinkertn@umich.edu help="Set the output directory to DIR [Default: %default]") 705524Sstever@gmail.comadd_option('-r', "--redirect-stdout", action="store_true", default=False, 715524Sstever@gmail.com help="Redirect stdout (& stderr, without -e) to file") 725524Sstever@gmail.comadd_option('-e', "--redirect-stderr", action="store_true", default=False, 735524Sstever@gmail.com help="Redirect stderr to file") 745524Sstever@gmail.comadd_option("--stdout-file", metavar="FILE", default="simout", 755524Sstever@gmail.com help="Filename for -r redirection [Default: %default]") 765524Sstever@gmail.comadd_option("--stderr-file", metavar="FILE", default="simerr", 775524Sstever@gmail.com help="Filename for -e redirection [Default: %default]") 782889Sbinkertn@umich.eduadd_option('-i', "--interactive", action="store_true", default=False, 792889Sbinkertn@umich.edu help="Invoke the interactive interpreter after running the script") 802899Sbinkertn@umich.eduadd_option("--pdb", action="store_true", default=False, 812899Sbinkertn@umich.edu help="Invoke the python debugger before running the script") 822889Sbinkertn@umich.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':', 832889Sbinkertn@umich.edu help="Prepend PATH to the system path when invoking the script") 842889Sbinkertn@umich.eduadd_option('-q', "--quiet", action="count", default=0, 852889Sbinkertn@umich.edu help="Reduce verbosity") 862889Sbinkertn@umich.eduadd_option('-v', "--verbose", action="count", default=0, 872889Sbinkertn@umich.edu help="Increase verbosity") 882889Sbinkertn@umich.edu 892889Sbinkertn@umich.edu# Statistics options 902889Sbinkertn@umich.eduset_group("Statistics Options") 915773Snate@binkert.orgadd_option("--stats-file", metavar="FILE", default="stats.txt", 922889Sbinkertn@umich.edu help="Sets the output file for statistics [Default: %default]") 932889Sbinkertn@umich.edu 945773Snate@binkert.org# Configuration Options 955773Snate@binkert.orgset_group("Configuration Options") 965773Snate@binkert.orgadd_option("--dump-config", metavar="FILE", default="config.ini", 975773Snate@binkert.org help="Dump configuration output file [Default: %default]") 985773Snate@binkert.org 992889Sbinkertn@umich.edu# Debugging options 1002889Sbinkertn@umich.eduset_group("Debugging Options") 1012889Sbinkertn@umich.eduadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',', 1022889Sbinkertn@umich.edu help="Cycle to create a breakpoint") 1035512SMichael.Adler@intel.comadd_option("--remote-gdb-port", type='int', default=7000, 1045512SMichael.Adler@intel.com help="Remote gdb base port") 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 1246171Snate@binkert.org# load the options.py config file to allow people to set their own 1256171Snate@binkert.org# default options 1266171Snate@binkert.orgoptions_file = config.get('options.py') 1276171Snate@binkert.orgif options_file: 1286171Snate@binkert.org scope = { 'options' : options } 1296171Snate@binkert.org execfile(options_file, scope) 1306171Snate@binkert.org 1316171Snate@binkert.orgarguments = options.parse_args() 1326171Snate@binkert.org 1332889Sbinkertn@umich.edudef main(): 1345801Snate@binkert.org import core 1355801Snate@binkert.org import debug 1365801Snate@binkert.org import defines 1374167Sbinkertn@umich.edu import event 1384042Sbinkertn@umich.edu import info 1395801Snate@binkert.org import stats 1405799Snate@binkert.org import trace 1415799Snate@binkert.org 1425799Snate@binkert.org def check_tracing(): 1435799Snate@binkert.org if defines.TRACING_ON: 1445799Snate@binkert.org return 1455799Snate@binkert.org 1465802Snate@binkert.org fatal("Tracing is not enabled. Compile with TRACING_ON") 1472889Sbinkertn@umich.edu 1485524Sstever@gmail.com if not os.path.isdir(options.outdir): 1495524Sstever@gmail.com os.makedirs(options.outdir) 1505524Sstever@gmail.com 1515524Sstever@gmail.com # These filenames are used only if the redirect_std* options are set 1525524Sstever@gmail.com stdout_file = os.path.join(options.outdir, options.stdout_file) 1535524Sstever@gmail.com stderr_file = os.path.join(options.outdir, options.stderr_file) 1545524Sstever@gmail.com 1555524Sstever@gmail.com # Print redirection notices here before doing any redirection 1565524Sstever@gmail.com if options.redirect_stdout and not options.redirect_stderr: 1575524Sstever@gmail.com print "Redirecting stdout and stderr to", stdout_file 1585524Sstever@gmail.com else: 1595524Sstever@gmail.com if options.redirect_stdout: 1605524Sstever@gmail.com print "Redirecting stdout to", stdout_file 1615524Sstever@gmail.com if options.redirect_stderr: 1625524Sstever@gmail.com print "Redirecting stderr to", stderr_file 1635524Sstever@gmail.com 1645524Sstever@gmail.com # Now redirect stdout/stderr as desired 1655524Sstever@gmail.com if options.redirect_stdout: 1665524Sstever@gmail.com redir_fd = os.open(stdout_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC) 1675524Sstever@gmail.com os.dup2(redir_fd, sys.stdout.fileno()) 1685524Sstever@gmail.com if not options.redirect_stderr: 1695524Sstever@gmail.com os.dup2(redir_fd, sys.stderr.fileno()) 1705524Sstever@gmail.com 1715524Sstever@gmail.com if options.redirect_stderr: 1725524Sstever@gmail.com redir_fd = os.open(stderr_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC) 1735524Sstever@gmail.com os.dup2(redir_fd, sys.stderr.fileno()) 1745524Sstever@gmail.com 1752889Sbinkertn@umich.edu done = False 1764850Snate@binkert.org 1774850Snate@binkert.org if options.build_info: 1784850Snate@binkert.org done = True 1794850Snate@binkert.org print 'Build information:' 1804850Snate@binkert.org print 1815801Snate@binkert.org print 'compiled %s' % defines.compileDate; 1825824Ssaidi@eecs.umich.edu print "revision %s" % defines.hgRev 1834850Snate@binkert.org print 'build options:' 1845801Snate@binkert.org keys = defines.buildEnv.keys() 1854850Snate@binkert.org keys.sort() 1864850Snate@binkert.org for key in keys: 1875801Snate@binkert.org val = defines.buildEnv[key] 1884850Snate@binkert.org print ' %s = %s' % (key, val) 1894850Snate@binkert.org print 1904850Snate@binkert.org 1912889Sbinkertn@umich.edu if options.copyright: 1922889Sbinkertn@umich.edu done = True 1932889Sbinkertn@umich.edu print info.LICENSE 1942889Sbinkertn@umich.edu print 1952889Sbinkertn@umich.edu 1962889Sbinkertn@umich.edu if options.authors: 1972889Sbinkertn@umich.edu done = True 1982889Sbinkertn@umich.edu print 'Author information:' 1992889Sbinkertn@umich.edu print 2002889Sbinkertn@umich.edu print info.AUTHORS 2012889Sbinkertn@umich.edu print 2022889Sbinkertn@umich.edu 2032889Sbinkertn@umich.edu if options.readme: 2042889Sbinkertn@umich.edu done = True 2052889Sbinkertn@umich.edu print 'Readme:' 2062889Sbinkertn@umich.edu print 2072889Sbinkertn@umich.edu print info.README 2082889Sbinkertn@umich.edu print 2092889Sbinkertn@umich.edu 2102889Sbinkertn@umich.edu if options.release_notes: 2112889Sbinkertn@umich.edu done = True 2122889Sbinkertn@umich.edu print 'Release Notes:' 2132889Sbinkertn@umich.edu print 2142889Sbinkertn@umich.edu print info.RELEASE_NOTES 2152889Sbinkertn@umich.edu print 2162889Sbinkertn@umich.edu 2174053Sbinkertn@umich.edu if options.trace_help: 2184053Sbinkertn@umich.edu done = True 2195799Snate@binkert.org check_tracing() 2205799Snate@binkert.org trace.help() 2214053Sbinkertn@umich.edu 2225473Snate@binkert.org if options.list_sim_objects: 2235473Snate@binkert.org import SimObject 2245473Snate@binkert.org done = True 2255473Snate@binkert.org print "SimObjects:" 2265473Snate@binkert.org objects = SimObject.allClasses.keys() 2275473Snate@binkert.org objects.sort() 2285473Snate@binkert.org for name in objects: 2295473Snate@binkert.org obj = SimObject.allClasses[name] 2305473Snate@binkert.org print " %s" % obj 2315473Snate@binkert.org params = obj._params.keys() 2325473Snate@binkert.org params.sort() 2335473Snate@binkert.org for pname in params: 2345473Snate@binkert.org param = obj._params[pname] 2355473Snate@binkert.org default = getattr(param, 'default', '') 2365473Snate@binkert.org print " %s" % pname 2375473Snate@binkert.org if default: 2385473Snate@binkert.org print " default: %s" % default 2395473Snate@binkert.org print " desc: %s" % param.desc 2405473Snate@binkert.org print 2415473Snate@binkert.org print 2425473Snate@binkert.org 2432889Sbinkertn@umich.edu if done: 2442889Sbinkertn@umich.edu sys.exit(0) 2452889Sbinkertn@umich.edu 2465470Snate@binkert.org # setting verbose and quiet at the same time doesn't make sense 2475470Snate@binkert.org if options.verbose > 0 and options.quiet > 0: 2485470Snate@binkert.org options.usage(2) 2495470Snate@binkert.org 2505470Snate@binkert.org verbose = options.verbose - options.quiet 2512889Sbinkertn@umich.edu if options.verbose >= 0: 2522889Sbinkertn@umich.edu print "M5 Simulator System" 2532889Sbinkertn@umich.edu print brief_copyright 2542889Sbinkertn@umich.edu print 2555801Snate@binkert.org 2565801Snate@binkert.org print "M5 compiled %s" % defines.compileDate; 2575824Ssaidi@eecs.umich.edu print "M5 revision %s" % defines.hgRev 2585456Ssaidi@eecs.umich.edu 2595528Sstever@gmail.com print "M5 started %s" % datetime.datetime.now().strftime("%b %e %Y %X") 2605528Sstever@gmail.com print "M5 executing on %s" % socket.gethostname() 2615528Sstever@gmail.com 2622967Sktlim@umich.edu print "command line:", 2632967Sktlim@umich.edu for argv in sys.argv: 2642967Sktlim@umich.edu print argv, 2652967Sktlim@umich.edu print 2662889Sbinkertn@umich.edu 2672889Sbinkertn@umich.edu # check to make sure we can find the listed script 2682889Sbinkertn@umich.edu if not arguments or not os.path.isfile(arguments[0]): 2692922Sktlim@umich.edu if arguments and not os.path.isfile(arguments[0]): 2702922Sktlim@umich.edu print "Script %s not found" % arguments[0] 2714053Sbinkertn@umich.edu 2725470Snate@binkert.org options.usage(2) 2732889Sbinkertn@umich.edu 2742889Sbinkertn@umich.edu # tell C++ about output directory 2755801Snate@binkert.org core.setOutputDir(options.outdir) 2762889Sbinkertn@umich.edu 2772889Sbinkertn@umich.edu # update the system path with elements from the -p option 2782889Sbinkertn@umich.edu sys.path[0:0] = options.path 2792889Sbinkertn@umich.edu 2802889Sbinkertn@umich.edu # set stats options 2815801Snate@binkert.org stats.initText(options.stats_file) 2822889Sbinkertn@umich.edu 2832889Sbinkertn@umich.edu # set debugging options 2845801Snate@binkert.org debug.setRemoteGDBPort(options.remote_gdb_port) 2853645Sbinkertn@umich.edu for when in options.debug_break: 2865801Snate@binkert.org debug.schedBreakCycle(int(when)) 2872889Sbinkertn@umich.edu 2885586Snate@binkert.org if options.trace_flags: 2895799Snate@binkert.org check_tracing() 2904053Sbinkertn@umich.edu 2915586Snate@binkert.org on_flags = [] 2925586Snate@binkert.org off_flags = [] 2935586Snate@binkert.org for flag in options.trace_flags: 2945586Snate@binkert.org off = False 2955586Snate@binkert.org if flag.startswith('-'): 2965586Snate@binkert.org flag = flag[1:] 2975586Snate@binkert.org off = True 2985799Snate@binkert.org if flag not in trace.flags.all and flag != "All": 2995586Snate@binkert.org print >>sys.stderr, "invalid trace flag '%s'" % flag 3005586Snate@binkert.org sys.exit(1) 3014053Sbinkertn@umich.edu 3025586Snate@binkert.org if off: 3035586Snate@binkert.org off_flags.append(flag) 3045586Snate@binkert.org else: 3055586Snate@binkert.org on_flags.append(flag) 3064042Sbinkertn@umich.edu 3075586Snate@binkert.org for flag in on_flags: 3085799Snate@binkert.org trace.set(flag) 3095586Snate@binkert.org 3105586Snate@binkert.org for flag in off_flags: 3115799Snate@binkert.org trace.clear(flag) 3124053Sbinkertn@umich.edu 3134074Sbinkertn@umich.edu if options.trace_start: 3145799Snate@binkert.org check_tracing() 3155950Ssaidi@eecs.umich.edu e = event.create(trace.enable, event.Event.Trace_Enable_Pri) 3165606Snate@binkert.org event.mainq.schedule(e, options.trace_start) 3174074Sbinkertn@umich.edu else: 3185799Snate@binkert.org trace.enable() 3194042Sbinkertn@umich.edu 3205799Snate@binkert.org trace.output(options.trace_file) 3214042Sbinkertn@umich.edu 3224042Sbinkertn@umich.edu for ignore in options.trace_ignore: 3235799Snate@binkert.org check_tracing() 3245799Snate@binkert.org trace.ignore(ignore) 3252889Sbinkertn@umich.edu 3262889Sbinkertn@umich.edu sys.argv = arguments 3272889Sbinkertn@umich.edu sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path 3282891Sbinkertn@umich.edu 3295604Snate@binkert.org filename = sys.argv[0] 3305604Snate@binkert.org filedata = file(filename, 'r').read() 3315604Snate@binkert.org filecode = compile(filedata, filename, 'exec') 3325604Snate@binkert.org scope = { '__file__' : filename, 3333887Sbinkertn@umich.edu '__name__' : '__m5_main__' } 3342899Sbinkertn@umich.edu 3352899Sbinkertn@umich.edu # we want readline if we're doing anything interactive 3362899Sbinkertn@umich.edu if options.interactive or options.pdb: 3374042Sbinkertn@umich.edu exec "import readline" in scope 3382899Sbinkertn@umich.edu 3392899Sbinkertn@umich.edu # if pdb was requested, execfile the thing under pdb, otherwise, 3402899Sbinkertn@umich.edu # just do the execfile normally 3412899Sbinkertn@umich.edu if options.pdb: 3425604Snate@binkert.org import pdb 3435604Snate@binkert.org import traceback 3445604Snate@binkert.org 3455604Snate@binkert.org pdb = pdb.Pdb() 3465604Snate@binkert.org try: 3475604Snate@binkert.org pdb.run(filecode, scope) 3485604Snate@binkert.org except SystemExit: 3495604Snate@binkert.org print "The program exited via sys.exit(). Exit status: ", 3505604Snate@binkert.org print sys.exc_info()[1] 3515604Snate@binkert.org except: 3525604Snate@binkert.org traceback.print_exc() 3535604Snate@binkert.org print "Uncaught exception. Entering post mortem debugging" 3545604Snate@binkert.org t = sys.exc_info()[2] 3555604Snate@binkert.org while t.tb_next is not None: 3565604Snate@binkert.org t = t.tb_next 3575604Snate@binkert.org pdb.interaction(t.tb_frame,t) 3582899Sbinkertn@umich.edu else: 3595604Snate@binkert.org exec filecode in scope 3602889Sbinkertn@umich.edu 3612889Sbinkertn@umich.edu # once the script is done 3622889Sbinkertn@umich.edu if options.interactive: 3632889Sbinkertn@umich.edu interact = code.InteractiveConsole(scope) 3642889Sbinkertn@umich.edu interact.interact("M5 Interactive Console") 3652889Sbinkertn@umich.edu 3662889Sbinkertn@umich.eduif __name__ == '__main__': 3672889Sbinkertn@umich.edu from pprint import pprint 3682889Sbinkertn@umich.edu 3692889Sbinkertn@umich.edu print 'opts:' 3702889Sbinkertn@umich.edu pprint(options, indent=4) 3712889Sbinkertn@umich.edu print 3722889Sbinkertn@umich.edu 3732889Sbinkertn@umich.edu print 'args:' 3742889Sbinkertn@umich.edu pprint(arguments, indent=4) 375