main.py revision 4087:ad2f343e3d45
12SN/A# Copyright (c) 2005 The Regents of The University of Michigan 21762SN/A# All rights reserved. 32SN/A# 42SN/A# Redistribution and use in source and binary forms, with or without 52SN/A# modification, are permitted provided that the following conditions are 62SN/A# met: redistributions of source code must retain the above copyright 72SN/A# notice, this list of conditions and the following disclaimer; 82SN/A# redistributions in binary form must reproduce the above copyright 92SN/A# notice, this list of conditions and the following disclaimer in the 102SN/A# documentation and/or other materials provided with the distribution; 112SN/A# neither the name of the copyright holders nor the names of its 122SN/A# contributors may be used to endorse or promote products derived from 132SN/A# this software without specific prior written permission. 142SN/A# 152SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 162SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 172SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 182SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 192SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 202SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 212SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 222SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 232SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 242SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 252SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 262SN/A# 272665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert 282665Ssaidi@eecs.umich.edu 292665Ssaidi@eecs.umich.eduimport code, optparse, os, socket, sys 302SN/Afrom datetime import datetime 312SN/Afrom attrdict import attrdict 321717SN/Aimport traceflags 331717SN/A 342SN/A__all__ = [ 'options', 'arguments', 'main' ] 352SN/A 362SN/Ausage="%prog [m5 options] script.py [script options]" 374182Sgblack@eecs.umich.eduversion="%prog 2.0" 385664Sgblack@eecs.umich.edubrief_copyright=''' 39707SN/ACopyright (c) 2001-2006 401858SN/AThe Regents of The University of Michigan 4156SN/AAll Rights Reserved 424776Sgblack@eecs.umich.edu''' 432856Srdreslin@umich.edu 442SN/Adef print_list(items, indent=4): 453520Sgblack@eecs.umich.edu line = ' ' * indent 463520Sgblack@eecs.umich.edu for i,item in enumerate(items): 473520Sgblack@eecs.umich.edu if len(line) + len(item) > 76: 483520Sgblack@eecs.umich.edu print line 495529Snate@binkert.org line = ' ' * indent 502190SN/A 512315SN/A if i < len(items) - 1: 522680Sktlim@umich.edu line += '%s, ' % item 532SN/A else: 542856Srdreslin@umich.edu line += item 552SN/A print line 564182Sgblack@eecs.umich.edu 574182Sgblack@eecs.umich.edu# there's only one option parsing done, so make it global and add some 584182Sgblack@eecs.umich.edu# helper functions to make it work well. 594182Sgblack@eecs.umich.eduparser = optparse.OptionParser(usage=usage, version=version, 604182Sgblack@eecs.umich.edu description=brief_copyright, 612356SN/A formatter=optparse.TitledHelpFormatter()) 622356SN/Aparser.disable_interspersed_args() 632356SN/A 642356SN/A# current option group 652356SN/Agroup = None 662356SN/A 672356SN/Adef set_group(*args, **kwargs): 682356SN/A '''set the current option group''' 695606Snate@binkert.org global group 702356SN/A if not args and not kwargs: 712356SN/A group = None 722356SN/A else: 735336Shines@cs.fsu.edu group = parser.add_option_group(*args, **kwargs) 742356SN/A 752356SN/Aclass splitter(object): 762856Srdreslin@umich.edu def __init__(self, split): 772SN/A self.split = split 781634SN/A def __call__(self, option, opt_str, value, parser): 791634SN/A getattr(parser.values, option.dest).extend(value.split(self.split)) 801695SN/A 813814Ssaidi@eecs.umich.edudef add_option(*args, **kwargs): 823814Ssaidi@eecs.umich.edu '''add an option to the current option group, or global none set''' 835712Shsul@eecs.umich.edu 845712Shsul@eecs.umich.edu # if action=split, but allows the option arguments 855712Shsul@eecs.umich.edu # themselves to be lists separated by the split variable''' 865712Shsul@eecs.umich.edu 875712Shsul@eecs.umich.edu if kwargs.get('action', None) == 'append' and 'split' in kwargs: 881634SN/A split = kwargs.pop('split') 891634SN/A kwargs['default'] = [] 905712Shsul@eecs.umich.edu kwargs['type'] = 'string' 915712Shsul@eecs.umich.edu kwargs['action'] = 'callback' 925712Shsul@eecs.umich.edu kwargs['callback'] = splitter(split) 932359SN/A 941695SN/A if group: 955100Ssaidi@eecs.umich.edu return group.add_option(*args, **kwargs) 961695SN/A 975099Ssaidi@eecs.umich.edu return parser.add_option(*args, **kwargs) 983814Ssaidi@eecs.umich.edu 993814Ssaidi@eecs.umich.edudef bool_option(name, default, help): 1001634SN/A '''add a boolean option called --name and --no-name. 1013495Sktlim@umich.edu Display help depending on which is the default''' 1023495Sktlim@umich.edu 1033495Sktlim@umich.edu tname = '--%s' % name 1043495Sktlim@umich.edu fname = '--no-%s' % name 1053495Sktlim@umich.edu dest = name.replace('-', '_') 1063495Sktlim@umich.edu if default: 1073495Sktlim@umich.edu thelp = optparse.SUPPRESS_HELP 1083495Sktlim@umich.edu fhelp = help 1093495Sktlim@umich.edu else: 1103495Sktlim@umich.edu thelp = help 1113495Sktlim@umich.edu fhelp = optparse.SUPPRESS_HELP 1123495Sktlim@umich.edu 1133495Sktlim@umich.edu add_option(tname, action="store_true", default=default, help=thelp) 1143495Sktlim@umich.edu add_option(fname, action="store_false", dest=dest, help=fhelp) 1155664Sgblack@eecs.umich.edu 1165664Sgblack@eecs.umich.edu# Help options 1171858SN/Aadd_option('-A', "--authors", action="store_true", default=False, 1182SN/A help="Show author information") 1195704Snate@binkert.orgadd_option('-C', "--copyright", action="store_true", default=False, 1202SN/A help="Show full copyright information") 1212SN/Aadd_option('-R', "--readme", action="store_true", default=False, 1225645Sgblack@eecs.umich.edu help="Show the readme") 1235645Sgblack@eecs.umich.eduadd_option('-N', "--release-notes", action="store_true", default=False, 1245645Sgblack@eecs.umich.edu help="Show the release notes") 1255647Sgblack@eecs.umich.edu 1265645Sgblack@eecs.umich.edu# Options for configuring the base simulator 1275645Sgblack@eecs.umich.eduadd_option('-d', "--outdir", metavar="DIR", default=".", 1285704Snate@binkert.org help="Set the output directory to DIR [Default: %default]") 1295704Snate@binkert.orgadd_option('-i', "--interactive", action="store_true", default=False, 1305704Snate@binkert.org help="Invoke the interactive interpreter after running the script") 1312SN/Aadd_option("--pdb", action="store_true", default=False, 1325704Snate@binkert.org help="Invoke the python debugger before running the script") 1335704Snate@binkert.orgadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':', 1345704Snate@binkert.org help="Prepend PATH to the system path when invoking the script") 1355704Snate@binkert.orgadd_option('-q', "--quiet", action="count", default=0, 1365704Snate@binkert.org help="Reduce verbosity") 1371917SN/Aadd_option('-v', "--verbose", action="count", default=0, 1381917SN/A help="Increase verbosity") 1391917SN/A 1401917SN/A# Statistics options 1411917SN/Aset_group("Statistics Options") 1425536Srstrong@hp.comadd_option("--stats-file", metavar="FILE", default="m5stats.txt", 1431917SN/A help="Sets the output file for statistics [Default: %default]") 1441917SN/A 1455536Srstrong@hp.com# Debugging options 1461917SN/Aset_group("Debugging Options") 1471917SN/Aadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',', 1481917SN/A help="Cycle to create a breakpoint") 1492SN/A 1502SN/A# Tracing options 1512SN/Aset_group("Trace Options") 1522680Sktlim@umich.eduadd_option("--trace-help", action='store_true', 1534182Sgblack@eecs.umich.edu help="Print help on trace flags") 1542SN/Aadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',', 1554776Sgblack@eecs.umich.edu help="Sets the flags for tracing (-FLAG disables a flag)") 1564776Sgblack@eecs.umich.eduadd_option("--trace-start", metavar="TIME", type='int', 1572SN/A help="Start tracing at TIME (must be in ticks)") 158393SN/Aadd_option("--trace-file", metavar="FILE", default="cout", 1594776Sgblack@eecs.umich.edu help="Sets the output file for tracing [Default: %default]") 1604776Sgblack@eecs.umich.eduadd_option("--trace-ignore", metavar="EXPR", action='append', split=':', 1614776Sgblack@eecs.umich.edu help="Ignore EXPR sim objects") 162393SN/A 163393SN/Aoptions = attrdict() 164393SN/Aarguments = [] 165393SN/A 166393SN/Adef usage(exitcode=None): 167393SN/A parser.print_help() 168393SN/A if exitcode is not None: 169393SN/A sys.exit(exitcode) 170393SN/A 171393SN/Adef parse_args(): 172393SN/A _opts,args = parser.parse_args() 173393SN/A opts = attrdict(_opts.__dict__) 174393SN/A 1752SN/A # setting verbose and quiet at the same time doesn't make sense 1764000Ssaidi@eecs.umich.edu if opts.verbose > 0 and opts.quiet > 0: 1774000Ssaidi@eecs.umich.edu usage(2) 1784000Ssaidi@eecs.umich.edu 1794000Ssaidi@eecs.umich.edu # store the verbosity in a single variable. 0 is default, 1804000Ssaidi@eecs.umich.edu # negative numbers represent quiet and positive values indicate verbose 1814000Ssaidi@eecs.umich.edu opts.verbose -= opts.quiet 1822SN/A 1835529Snate@binkert.org del opts.quiet 1845529Snate@binkert.org 1855529Snate@binkert.org options.update(opts) 1861400SN/A arguments.extend(args) 1871191SN/A return opts,args 1882SN/A 1891129SN/Adef main(): 1901917SN/A import defines 1912SN/A import info 1922SN/A import internal 1932103SN/A 1942103SN/A parse_args() 1952680Sktlim@umich.edu 196180SN/A done = False 1971492SN/A if options.copyright: 1981492SN/A done = True 1992798Sktlim@umich.edu print info.LICENSE 200180SN/A print 201180SN/A 202180SN/A if options.authors: 2034192Sktlim@umich.edu done = True 204180SN/A print 'Author information:' 205124SN/A print 206124SN/A print info.AUTHORS 207124SN/A print 208124SN/A 2092SN/A if options.readme: 2102SN/A done = True 2115529Snate@binkert.org print 'Readme:' 2125529Snate@binkert.org print 213124SN/A print info.README 214124SN/A print 215124SN/A 216124SN/A if options.release_notes: 217124SN/A done = True 218503SN/A print 'Release Notes:' 2192SN/A print 220124SN/A print info.RELEASE_NOTES 221124SN/A print 222124SN/A 223124SN/A if options.trace_help: 224124SN/A done = True 225124SN/A print "Base Flags:" 226124SN/A print_list(traceflags.baseFlags, indent=4) 2272SN/A print 228921SN/A print "Compound Flags:" 2293661Srdreslin@umich.edu for flag in traceflags.compoundFlags: 2303661Srdreslin@umich.edu if flag == 'All': 2312378SN/A continue 232921SN/A print " %s:" % flag 233921SN/A print_list(traceflags.compoundFlagMap[flag], indent=8) 234921SN/A print 235921SN/A 236921SN/A if done: 237921SN/A sys.exit(0) 238921SN/A 239921SN/A if options.verbose >= 0: 240921SN/A print "M5 Simulator System" 241921SN/A print brief_copyright 242921SN/A print 243921SN/A print "M5 compiled %s" % internal.main.cvar.compileDate; 244921SN/A print "M5 started %s" % datetime.now().ctime() 2452SN/A print "M5 executing on %s" % socket.gethostname() 2462SN/A print "command line:", 247124SN/A for argv in sys.argv: 248124SN/A print argv, 249124SN/A print 250124SN/A 2512SN/A # check to make sure we can find the listed script 2522SN/A if not arguments or not os.path.isfile(arguments[0]): 253707SN/A if arguments and not os.path.isfile(arguments[0]): 254707SN/A print "Script %s not found" % arguments[0] 2551191SN/A 2561191SN/A usage(2) 2571191SN/A 2581191SN/A # tell C++ about output directory 2591191SN/A internal.main.setOutputDir(options.outdir) 2601191SN/A 2611191SN/A # update the system path with elements from the -p option 2621191SN/A sys.path[0:0] = options.path 2631191SN/A 2641191SN/A import objects 2651191SN/A 2661191SN/A # set stats options 2671191SN/A internal.stats.initText(options.stats_file) 2681191SN/A 2691191SN/A # set debugging options 2701191SN/A for when in options.debug_break: 2711191SN/A internal.debug.schedBreakCycle(int(when)) 2722SN/A 2732SN/A on_flags = [] 2742SN/A off_flags = [] 2752SN/A for flag in options.trace_flags: 2762SN/A off = False 277707SN/A if flag.startswith('-'): 278707SN/A flag = flag[1:] 279707SN/A off = True 280707SN/A if flag not in traceflags.allFlags: 281707SN/A print >>sys.stderr, "invalid trace flag '%s'" % flag 282707SN/A sys.exit(1) 283707SN/A 284707SN/A if off: 285707SN/A off_flags.append(flag) 286707SN/A else: 287707SN/A on_flags.append(flag) 288707SN/A 289707SN/A for flag in on_flags: 290729SN/A internal.trace.set(flag) 2912SN/A 2922SN/A for flag in off_flags: 2931717SN/A internal.trace.clear(flag) 294 295 if options.trace_start: 296 def enable_trace(): 297 internal.trace.cvar.enabled = True 298 internal.event.create(enable_trace, int(options.trace_start)) 299 else: 300 internal.trace.cvar.enabled = True 301 302 internal.trace.output(options.trace_file) 303 304 for ignore in options.trace_ignore: 305 internal.trace.ignore(ignore) 306 307 sys.argv = arguments 308 sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path 309 310 scope = { '__file__' : sys.argv[0], 311 '__name__' : '__m5_main__' } 312 313 # we want readline if we're doing anything interactive 314 if options.interactive or options.pdb: 315 exec "import readline" in scope 316 317 # if pdb was requested, execfile the thing under pdb, otherwise, 318 # just do the execfile normally 319 if options.pdb: 320 from pdb import Pdb 321 debugger = Pdb() 322 debugger.run('execfile("%s")' % sys.argv[0], scope) 323 else: 324 execfile(sys.argv[0], scope) 325 326 # once the script is done 327 if options.interactive: 328 interact = code.InteractiveConsole(scope) 329 interact.interact("M5 Interactive Console") 330 331if __name__ == '__main__': 332 from pprint import pprint 333 334 parse_args() 335 336 print 'opts:' 337 pprint(options, indent=4) 338 print 339 340 print 'args:' 341 pprint(arguments, indent=4) 342