main.py revision 4087
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
324053Sbinkertn@umich.eduimport traceflags
332889Sbinkertn@umich.edu
342889Sbinkertn@umich.edu__all__ = [ 'options', 'arguments', 'main' ]
352889Sbinkertn@umich.edu
362889Sbinkertn@umich.eduusage="%prog [m5 options] script.py [script options]"
372889Sbinkertn@umich.eduversion="%prog 2.0"
382889Sbinkertn@umich.edubrief_copyright='''
392889Sbinkertn@umich.eduCopyright (c) 2001-2006
402889Sbinkertn@umich.eduThe Regents of The University of Michigan
412889Sbinkertn@umich.eduAll Rights Reserved
422889Sbinkertn@umich.edu'''
432889Sbinkertn@umich.edu
444053Sbinkertn@umich.edudef print_list(items, indent=4):
454053Sbinkertn@umich.edu    line = ' ' * indent
464053Sbinkertn@umich.edu    for i,item in enumerate(items):
474053Sbinkertn@umich.edu        if len(line) + len(item) > 76:
484053Sbinkertn@umich.edu            print line
494053Sbinkertn@umich.edu            line = ' ' * indent
504053Sbinkertn@umich.edu
514053Sbinkertn@umich.edu        if i < len(items) - 1:
524053Sbinkertn@umich.edu            line += '%s, ' % item
534053Sbinkertn@umich.edu        else:
544053Sbinkertn@umich.edu            line += item
554053Sbinkertn@umich.edu            print line
564053Sbinkertn@umich.edu
572889Sbinkertn@umich.edu# there's only one option parsing done, so make it global and add some
582889Sbinkertn@umich.edu# helper functions to make it work well.
592889Sbinkertn@umich.eduparser = optparse.OptionParser(usage=usage, version=version,
602889Sbinkertn@umich.edu                               description=brief_copyright,
612889Sbinkertn@umich.edu                               formatter=optparse.TitledHelpFormatter())
622890Sbinkertn@umich.eduparser.disable_interspersed_args()
632889Sbinkertn@umich.edu
642889Sbinkertn@umich.edu# current option group
652889Sbinkertn@umich.edugroup = None
662889Sbinkertn@umich.edu
672889Sbinkertn@umich.edudef set_group(*args, **kwargs):
682889Sbinkertn@umich.edu    '''set the current option group'''
692889Sbinkertn@umich.edu    global group
702889Sbinkertn@umich.edu    if not args and not kwargs:
712889Sbinkertn@umich.edu        group = None
722889Sbinkertn@umich.edu    else:
732889Sbinkertn@umich.edu        group = parser.add_option_group(*args, **kwargs)
742889Sbinkertn@umich.edu
752889Sbinkertn@umich.educlass splitter(object):
762889Sbinkertn@umich.edu    def __init__(self, split):
772889Sbinkertn@umich.edu        self.split = split
782889Sbinkertn@umich.edu    def __call__(self, option, opt_str, value, parser):
792889Sbinkertn@umich.edu        getattr(parser.values, option.dest).extend(value.split(self.split))
802889Sbinkertn@umich.edu
812889Sbinkertn@umich.edudef add_option(*args, **kwargs):
822889Sbinkertn@umich.edu    '''add an option to the current option group, or global none set'''
832889Sbinkertn@umich.edu
842889Sbinkertn@umich.edu    # if action=split, but allows the option arguments
852889Sbinkertn@umich.edu    # themselves to be lists separated by the split variable'''
862889Sbinkertn@umich.edu
872889Sbinkertn@umich.edu    if kwargs.get('action', None) == 'append' and 'split' in kwargs:
882889Sbinkertn@umich.edu        split = kwargs.pop('split')
892889Sbinkertn@umich.edu        kwargs['default'] = []
902889Sbinkertn@umich.edu        kwargs['type'] = 'string'
912889Sbinkertn@umich.edu        kwargs['action'] = 'callback'
922889Sbinkertn@umich.edu        kwargs['callback'] = splitter(split)
932889Sbinkertn@umich.edu
942889Sbinkertn@umich.edu    if group:
952889Sbinkertn@umich.edu        return group.add_option(*args, **kwargs)
962889Sbinkertn@umich.edu
972889Sbinkertn@umich.edu    return parser.add_option(*args, **kwargs)
982889Sbinkertn@umich.edu
992889Sbinkertn@umich.edudef bool_option(name, default, help):
1002889Sbinkertn@umich.edu    '''add a boolean option called --name and --no-name.
1012889Sbinkertn@umich.edu    Display help depending on which is the default'''
1022889Sbinkertn@umich.edu
1032889Sbinkertn@umich.edu    tname = '--%s' % name
1042889Sbinkertn@umich.edu    fname = '--no-%s' % name
1052889Sbinkertn@umich.edu    dest = name.replace('-', '_')
1062889Sbinkertn@umich.edu    if default:
1072889Sbinkertn@umich.edu        thelp = optparse.SUPPRESS_HELP
1082889Sbinkertn@umich.edu        fhelp = help
1092889Sbinkertn@umich.edu    else:
1102889Sbinkertn@umich.edu        thelp = help
1112889Sbinkertn@umich.edu        fhelp = optparse.SUPPRESS_HELP
1122889Sbinkertn@umich.edu
1132889Sbinkertn@umich.edu    add_option(tname, action="store_true", default=default, help=thelp)
1142889Sbinkertn@umich.edu    add_option(fname, action="store_false", dest=dest, help=fhelp)
1152889Sbinkertn@umich.edu
1162889Sbinkertn@umich.edu# Help options
1172889Sbinkertn@umich.eduadd_option('-A', "--authors", action="store_true", default=False,
1182889Sbinkertn@umich.edu    help="Show author information")
1192889Sbinkertn@umich.eduadd_option('-C', "--copyright", action="store_true", default=False,
1202889Sbinkertn@umich.edu    help="Show full copyright information")
1212889Sbinkertn@umich.eduadd_option('-R', "--readme", action="store_true", default=False,
1222889Sbinkertn@umich.edu    help="Show the readme")
1232889Sbinkertn@umich.eduadd_option('-N', "--release-notes", action="store_true", default=False,
1242889Sbinkertn@umich.edu    help="Show the release notes")
1252889Sbinkertn@umich.edu
1262889Sbinkertn@umich.edu# Options for configuring the base simulator
1272889Sbinkertn@umich.eduadd_option('-d', "--outdir", metavar="DIR", default=".",
1282889Sbinkertn@umich.edu    help="Set the output directory to DIR [Default: %default]")
1292889Sbinkertn@umich.eduadd_option('-i', "--interactive", action="store_true", default=False,
1302889Sbinkertn@umich.edu    help="Invoke the interactive interpreter after running the script")
1312899Sbinkertn@umich.eduadd_option("--pdb", action="store_true", default=False,
1322899Sbinkertn@umich.edu    help="Invoke the python debugger before running the script")
1332889Sbinkertn@umich.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
1342889Sbinkertn@umich.edu    help="Prepend PATH to the system path when invoking the script")
1352889Sbinkertn@umich.eduadd_option('-q', "--quiet", action="count", default=0,
1362889Sbinkertn@umich.edu    help="Reduce verbosity")
1372889Sbinkertn@umich.eduadd_option('-v', "--verbose", action="count", default=0,
1382889Sbinkertn@umich.edu    help="Increase verbosity")
1392889Sbinkertn@umich.edu
1402889Sbinkertn@umich.edu# Statistics options
1412889Sbinkertn@umich.eduset_group("Statistics Options")
1422889Sbinkertn@umich.eduadd_option("--stats-file", metavar="FILE", default="m5stats.txt",
1432889Sbinkertn@umich.edu    help="Sets the output file for statistics [Default: %default]")
1442889Sbinkertn@umich.edu
1452889Sbinkertn@umich.edu# Debugging options
1462889Sbinkertn@umich.eduset_group("Debugging Options")
1472889Sbinkertn@umich.eduadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
1482889Sbinkertn@umich.edu    help="Cycle to create a breakpoint")
1492889Sbinkertn@umich.edu
1502889Sbinkertn@umich.edu# Tracing options
1512889Sbinkertn@umich.eduset_group("Trace Options")
1524053Sbinkertn@umich.eduadd_option("--trace-help", action='store_true',
1534053Sbinkertn@umich.edu    help="Print help on trace flags")
1542889Sbinkertn@umich.eduadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
1554053Sbinkertn@umich.edu    help="Sets the flags for tracing (-FLAG disables a flag)")
1564044Sbinkertn@umich.eduadd_option("--trace-start", metavar="TIME", type='int',
1574044Sbinkertn@umich.edu    help="Start tracing at TIME (must be in ticks)")
1582889Sbinkertn@umich.eduadd_option("--trace-file", metavar="FILE", default="cout",
1592889Sbinkertn@umich.edu    help="Sets the output file for tracing [Default: %default]")
1602889Sbinkertn@umich.eduadd_option("--trace-ignore", metavar="EXPR", action='append', split=':',
1612889Sbinkertn@umich.edu    help="Ignore EXPR sim objects")
1622889Sbinkertn@umich.edu
1632889Sbinkertn@umich.eduoptions = attrdict()
1642889Sbinkertn@umich.eduarguments = []
1652889Sbinkertn@umich.edu
1662889Sbinkertn@umich.edudef usage(exitcode=None):
1672903Ssaidi@eecs.umich.edu    parser.print_help()
1682889Sbinkertn@umich.edu    if exitcode is not None:
1692889Sbinkertn@umich.edu        sys.exit(exitcode)
1702889Sbinkertn@umich.edu
1712889Sbinkertn@umich.edudef parse_args():
1722889Sbinkertn@umich.edu    _opts,args = parser.parse_args()
1732889Sbinkertn@umich.edu    opts = attrdict(_opts.__dict__)
1742889Sbinkertn@umich.edu
1752889Sbinkertn@umich.edu    # setting verbose and quiet at the same time doesn't make sense
1762889Sbinkertn@umich.edu    if opts.verbose > 0 and opts.quiet > 0:
1772889Sbinkertn@umich.edu        usage(2)
1782889Sbinkertn@umich.edu
1792889Sbinkertn@umich.edu    # store the verbosity in a single variable.  0 is default,
1802889Sbinkertn@umich.edu    # negative numbers represent quiet and positive values indicate verbose
1812889Sbinkertn@umich.edu    opts.verbose -= opts.quiet
1822889Sbinkertn@umich.edu
1832889Sbinkertn@umich.edu    del opts.quiet
1842889Sbinkertn@umich.edu
1852889Sbinkertn@umich.edu    options.update(opts)
1862889Sbinkertn@umich.edu    arguments.extend(args)
1872889Sbinkertn@umich.edu    return opts,args
1882889Sbinkertn@umich.edu
1892889Sbinkertn@umich.edudef main():
1904042Sbinkertn@umich.edu    import defines
1914042Sbinkertn@umich.edu    import info
1923624Sbinkertn@umich.edu    import internal
1932889Sbinkertn@umich.edu
1942889Sbinkertn@umich.edu    parse_args()
1952889Sbinkertn@umich.edu
1962889Sbinkertn@umich.edu    done = False
1972889Sbinkertn@umich.edu    if options.copyright:
1982889Sbinkertn@umich.edu        done = True
1992889Sbinkertn@umich.edu        print info.LICENSE
2002889Sbinkertn@umich.edu        print
2012889Sbinkertn@umich.edu
2022889Sbinkertn@umich.edu    if options.authors:
2032889Sbinkertn@umich.edu        done = True
2042889Sbinkertn@umich.edu        print 'Author information:'
2052889Sbinkertn@umich.edu        print
2062889Sbinkertn@umich.edu        print info.AUTHORS
2072889Sbinkertn@umich.edu        print
2082889Sbinkertn@umich.edu
2092889Sbinkertn@umich.edu    if options.readme:
2102889Sbinkertn@umich.edu        done = True
2112889Sbinkertn@umich.edu        print 'Readme:'
2122889Sbinkertn@umich.edu        print
2132889Sbinkertn@umich.edu        print info.README
2142889Sbinkertn@umich.edu        print
2152889Sbinkertn@umich.edu
2162889Sbinkertn@umich.edu    if options.release_notes:
2172889Sbinkertn@umich.edu        done = True
2182889Sbinkertn@umich.edu        print 'Release Notes:'
2192889Sbinkertn@umich.edu        print
2202889Sbinkertn@umich.edu        print info.RELEASE_NOTES
2212889Sbinkertn@umich.edu        print
2222889Sbinkertn@umich.edu
2234053Sbinkertn@umich.edu    if options.trace_help:
2244053Sbinkertn@umich.edu        done = True
2254053Sbinkertn@umich.edu        print "Base Flags:"
2264053Sbinkertn@umich.edu        print_list(traceflags.baseFlags, indent=4)
2274053Sbinkertn@umich.edu        print
2284053Sbinkertn@umich.edu        print "Compound Flags:"
2294053Sbinkertn@umich.edu        for flag in traceflags.compoundFlags:
2304053Sbinkertn@umich.edu            if flag == 'All':
2314053Sbinkertn@umich.edu                continue
2324053Sbinkertn@umich.edu            print "    %s:" % flag
2334053Sbinkertn@umich.edu            print_list(traceflags.compoundFlagMap[flag], indent=8)
2344053Sbinkertn@umich.edu            print
2354053Sbinkertn@umich.edu
2362889Sbinkertn@umich.edu    if done:
2372889Sbinkertn@umich.edu        sys.exit(0)
2382889Sbinkertn@umich.edu
2392889Sbinkertn@umich.edu    if options.verbose >= 0:
2402889Sbinkertn@umich.edu        print "M5 Simulator System"
2412889Sbinkertn@umich.edu        print brief_copyright
2422889Sbinkertn@umich.edu        print
2433624Sbinkertn@umich.edu        print "M5 compiled %s" % internal.main.cvar.compileDate;
2442889Sbinkertn@umich.edu        print "M5 started %s" % datetime.now().ctime()
2452889Sbinkertn@umich.edu        print "M5 executing on %s" % socket.gethostname()
2462967Sktlim@umich.edu        print "command line:",
2472967Sktlim@umich.edu        for argv in sys.argv:
2482967Sktlim@umich.edu            print argv,
2492967Sktlim@umich.edu        print
2502889Sbinkertn@umich.edu
2512889Sbinkertn@umich.edu    # check to make sure we can find the listed script
2522889Sbinkertn@umich.edu    if not arguments or not os.path.isfile(arguments[0]):
2532922Sktlim@umich.edu        if arguments and not os.path.isfile(arguments[0]):
2542922Sktlim@umich.edu            print "Script %s not found" % arguments[0]
2554053Sbinkertn@umich.edu
2562889Sbinkertn@umich.edu        usage(2)
2572889Sbinkertn@umich.edu
2582889Sbinkertn@umich.edu    # tell C++ about output directory
2593624Sbinkertn@umich.edu    internal.main.setOutputDir(options.outdir)
2602889Sbinkertn@umich.edu
2612889Sbinkertn@umich.edu    # update the system path with elements from the -p option
2622889Sbinkertn@umich.edu    sys.path[0:0] = options.path
2632889Sbinkertn@umich.edu
2642889Sbinkertn@umich.edu    import objects
2652889Sbinkertn@umich.edu
2662889Sbinkertn@umich.edu    # set stats options
2674078Sbinkertn@umich.edu    internal.stats.initText(options.stats_file)
2682889Sbinkertn@umich.edu
2692889Sbinkertn@umich.edu    # set debugging options
2703645Sbinkertn@umich.edu    for when in options.debug_break:
2713645Sbinkertn@umich.edu        internal.debug.schedBreakCycle(int(when))
2722889Sbinkertn@umich.edu
2734053Sbinkertn@umich.edu    on_flags = []
2744053Sbinkertn@umich.edu    off_flags = []
2754042Sbinkertn@umich.edu    for flag in options.trace_flags:
2764053Sbinkertn@umich.edu        off = False
2774053Sbinkertn@umich.edu        if flag.startswith('-'):
2784053Sbinkertn@umich.edu            flag = flag[1:]
2794053Sbinkertn@umich.edu            off = True
2804053Sbinkertn@umich.edu        if flag not in traceflags.allFlags:
2814053Sbinkertn@umich.edu            print >>sys.stderr, "invalid trace flag '%s'" % flag
2824053Sbinkertn@umich.edu            sys.exit(1)
2834053Sbinkertn@umich.edu
2844053Sbinkertn@umich.edu        if off:
2854053Sbinkertn@umich.edu            off_flags.append(flag)
2864053Sbinkertn@umich.edu        else:
2874053Sbinkertn@umich.edu            on_flags.append(flag)
2884053Sbinkertn@umich.edu
2894053Sbinkertn@umich.edu    for flag in on_flags:
2904046Sbinkertn@umich.edu        internal.trace.set(flag)
2914042Sbinkertn@umich.edu
2924053Sbinkertn@umich.edu    for flag in off_flags:
2934053Sbinkertn@umich.edu        internal.trace.clear(flag)
2944053Sbinkertn@umich.edu
2954074Sbinkertn@umich.edu    if options.trace_start:
2964042Sbinkertn@umich.edu        def enable_trace():
2974074Sbinkertn@umich.edu            internal.trace.cvar.enabled = True
2984074Sbinkertn@umich.edu        internal.event.create(enable_trace, int(options.trace_start))
2994074Sbinkertn@umich.edu    else:
3004087Sbinkertn@umich.edu        internal.trace.cvar.enabled = True
3014042Sbinkertn@umich.edu
3024046Sbinkertn@umich.edu    internal.trace.output(options.trace_file)
3034042Sbinkertn@umich.edu
3044042Sbinkertn@umich.edu    for ignore in options.trace_ignore:
3054042Sbinkertn@umich.edu        internal.trace.ignore(ignore)
3062889Sbinkertn@umich.edu
3072889Sbinkertn@umich.edu    sys.argv = arguments
3082889Sbinkertn@umich.edu    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
3092891Sbinkertn@umich.edu
3103887Sbinkertn@umich.edu    scope = { '__file__' : sys.argv[0],
3113887Sbinkertn@umich.edu              '__name__' : '__m5_main__' }
3122899Sbinkertn@umich.edu
3132899Sbinkertn@umich.edu    # we want readline if we're doing anything interactive
3142899Sbinkertn@umich.edu    if options.interactive or options.pdb:
3154042Sbinkertn@umich.edu        exec "import readline" in scope
3162899Sbinkertn@umich.edu
3172899Sbinkertn@umich.edu    # if pdb was requested, execfile the thing under pdb, otherwise,
3182899Sbinkertn@umich.edu    # just do the execfile normally
3192899Sbinkertn@umich.edu    if options.pdb:
3202899Sbinkertn@umich.edu        from pdb import Pdb
3212899Sbinkertn@umich.edu        debugger = Pdb()
3222899Sbinkertn@umich.edu        debugger.run('execfile("%s")' % sys.argv[0], scope)
3232899Sbinkertn@umich.edu    else:
3242899Sbinkertn@umich.edu        execfile(sys.argv[0], scope)
3252889Sbinkertn@umich.edu
3262889Sbinkertn@umich.edu    # once the script is done
3272889Sbinkertn@umich.edu    if options.interactive:
3282889Sbinkertn@umich.edu        interact = code.InteractiveConsole(scope)
3292889Sbinkertn@umich.edu        interact.interact("M5 Interactive Console")
3302889Sbinkertn@umich.edu
3312889Sbinkertn@umich.eduif __name__ == '__main__':
3322889Sbinkertn@umich.edu    from pprint import pprint
3332889Sbinkertn@umich.edu
3342889Sbinkertn@umich.edu    parse_args()
3352889Sbinkertn@umich.edu
3362889Sbinkertn@umich.edu    print 'opts:'
3372889Sbinkertn@umich.edu    pprint(options, indent=4)
3382889Sbinkertn@umich.edu    print
3392889Sbinkertn@umich.edu
3402889Sbinkertn@umich.edu    print 'args:'
3412889Sbinkertn@umich.edu    pprint(arguments, indent=4)
342