main.py revision 4053:ee914b22709e
11046SN/A# Copyright (c) 2005 The Regents of The University of Michigan
21046SN/A# All rights reserved.
31762SN/A#
41046SN/A# Redistribution and use in source and binary forms, with or without
51046SN/A# modification, are permitted provided that the following conditions are
61046SN/A# met: redistributions of source code must retain the above copyright
71046SN/A# notice, this list of conditions and the following disclaimer;
81046SN/A# redistributions in binary form must reproduce the above copyright
91046SN/A# notice, this list of conditions and the following disclaimer in the
101046SN/A# documentation and/or other materials provided with the distribution;
111046SN/A# neither the name of the copyright holders nor the names of its
121046SN/A# contributors may be used to endorse or promote products derived from
131046SN/A# this software without specific prior written permission.
141046SN/A#
151046SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
161046SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
171046SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
181046SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
191046SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
201046SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
211046SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221046SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231046SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241046SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
251046SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261046SN/A#
271046SN/A# Authors: Nathan Binkert
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.eduimport code, optparse, os, socket, sys
302665Ssaidi@eecs.umich.edufrom datetime import datetime
311046SN/Afrom attrdict import attrdict
321530SN/Aimport traceflags
332655Sstever@eecs.umich.edu
342655Sstever@eecs.umich.edu__all__ = [ 'options', 'arguments', 'main' ]
352655Sstever@eecs.umich.edu
362655Sstever@eecs.umich.eduusage="%prog [m5 options] script.py [script options]"
372655Sstever@eecs.umich.eduversion="%prog 2.0"
381530SN/Abrief_copyright='''
391530SN/ACopyright (c) 2001-2006
401530SN/AThe Regents of The University of Michigan
412667Sstever@eecs.umich.eduAll Rights Reserved
422667Sstever@eecs.umich.edu'''
431046SN/A
442655Sstever@eecs.umich.edudef print_list(items, indent=4):
452655Sstever@eecs.umich.edu    line = ' ' * indent
462655Sstever@eecs.umich.edu    for i,item in enumerate(items):
472655Sstever@eecs.umich.edu        if len(line) + len(item) > 76:
482655Sstever@eecs.umich.edu            print line
491046SN/A            line = ' ' * indent
502655Sstever@eecs.umich.edu
512655Sstever@eecs.umich.edu        if i < len(items) - 1:
522655Sstever@eecs.umich.edu            line += '%s, ' % item
532655Sstever@eecs.umich.edu        else:
541046SN/A            line += item
552655Sstever@eecs.umich.edu            print line
562655Sstever@eecs.umich.edu
572655Sstever@eecs.umich.edu# there's only one option parsing done, so make it global and add some
582667Sstever@eecs.umich.edu# helper functions to make it work well.
592655Sstever@eecs.umich.eduparser = optparse.OptionParser(usage=usage, version=version,
602655Sstever@eecs.umich.edu                               description=brief_copyright,
612655Sstever@eecs.umich.edu                               formatter=optparse.TitledHelpFormatter())
622655Sstever@eecs.umich.eduparser.disable_interspersed_args()
632655Sstever@eecs.umich.edu
642655Sstever@eecs.umich.edu# current option group
652655Sstever@eecs.umich.edugroup = None
662655Sstever@eecs.umich.edu
671046SN/Adef set_group(*args, **kwargs):
682655Sstever@eecs.umich.edu    '''set the current option group'''
692655Sstever@eecs.umich.edu    global group
702667Sstever@eecs.umich.edu    if not args and not kwargs:
711046SN/A        group = None
722655Sstever@eecs.umich.edu    else:
731046SN/A        group = parser.add_option_group(*args, **kwargs)
742655Sstever@eecs.umich.edu
752655Sstever@eecs.umich.educlass splitter(object):
761439SN/A    def __init__(self, split):
771530SN/A        self.split = split
782889Sbinkertn@umich.edu    def __call__(self, option, opt_str, value, parser):
791530SN/A        getattr(parser.values, option.dest).extend(value.split(self.split))
801439SN/A
811858SN/Adef add_option(*args, **kwargs):
822667Sstever@eecs.umich.edu    '''add an option to the current option group, or global none set'''
831858SN/A
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:
952655Sstever@eecs.umich.edu        return group.add_option(*args, **kwargs)
962655Sstever@eecs.umich.edu
972667Sstever@eecs.umich.edu    return parser.add_option(*args, **kwargs)
982889Sbinkertn@umich.edu
992655Sstever@eecs.umich.edudef bool_option(name, default, help):
1004053Sbinkertn@umich.edu    '''add a boolean option called --name and --no-name.
1012655Sstever@eecs.umich.edu    Display help depending on which is the default'''
1024086Sbinkertn@umich.edu
1034086Sbinkertn@umich.edu    tname = '--%s' % name
1044086Sbinkertn@umich.edu    fname = '--no-%s' % name
1054086Sbinkertn@umich.edu    dest = name.replace('-', '_')
1063869Sbinkertn@umich.edu    if default:
1073869Sbinkertn@umich.edu        thelp = optparse.SUPPRESS_HELP
1084086Sbinkertn@umich.edu        fhelp = help
1093645Sbinkertn@umich.edu    else:
1104123Sbinkertn@umich.edu        thelp = help
1113869Sbinkertn@umich.edu        fhelp = optparse.SUPPRESS_HELP
1123871Sbinkertn@umich.edu
1134045Sbinkertn@umich.edu    add_option(tname, action="store_true", default=default, help=thelp)
1144123Sbinkertn@umich.edu    add_option(fname, action="store_false", dest=dest, help=fhelp)
1154078Sbinkertn@umich.edu
1164042Sbinkertn@umich.edu# Help options
1172667Sstever@eecs.umich.eduadd_option('-A', "--authors", action="store_true", default=False,
1184086Sbinkertn@umich.edu    help="Show author information")
1194086Sbinkertn@umich.eduadd_option('-C', "--copyright", action="store_true", default=False,
1204086Sbinkertn@umich.edu    help="Show full copyright information")
1214086Sbinkertn@umich.eduadd_option('-R', "--readme", action="store_true", default=False,
1224086Sbinkertn@umich.edu    help="Show the readme")
1234086Sbinkertn@umich.eduadd_option('-N', "--release-notes", action="store_true", default=False,
1244086Sbinkertn@umich.edu    help="Show the release notes")
1254086Sbinkertn@umich.edu
1264086Sbinkertn@umich.edu# Options for configuring the base simulator
1274086Sbinkertn@umich.eduadd_option('-d', "--outdir", metavar="DIR", default=".",
1284086Sbinkertn@umich.edu    help="Set the output directory to DIR [Default: %default]")
1294086Sbinkertn@umich.eduadd_option('-i', "--interactive", action="store_true", default=False,
1304086Sbinkertn@umich.edu    help="Invoke the interactive interpreter after running the script")
1314086Sbinkertn@umich.eduadd_option("--pdb", action="store_true", default=False,
1324086Sbinkertn@umich.edu    help="Invoke the python debugger before running the script")
1334086Sbinkertn@umich.eduadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
1344086Sbinkertn@umich.edu    help="Prepend PATH to the system path when invoking the script")
1354086Sbinkertn@umich.eduadd_option('-q', "--quiet", action="count", default=0,
1364086Sbinkertn@umich.edu    help="Reduce verbosity")
1374086Sbinkertn@umich.eduadd_option('-v', "--verbose", action="count", default=0,
1384086Sbinkertn@umich.edu    help="Increase verbosity")
1394086Sbinkertn@umich.edu
1404086Sbinkertn@umich.edu# Statistics options
1414086Sbinkertn@umich.eduset_group("Statistics Options")
1424086Sbinkertn@umich.eduadd_option("--stats-file", metavar="FILE", default="m5stats.txt",
1434086Sbinkertn@umich.edu    help="Sets the output file for statistics [Default: %default]")
1442655Sstever@eecs.umich.edu
1452655Sstever@eecs.umich.edu# Debugging options
1462655Sstever@eecs.umich.eduset_group("Debugging Options")
1472655Sstever@eecs.umich.eduadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
1482655Sstever@eecs.umich.edu    help="Cycle to create a breakpoint")
1492655Sstever@eecs.umich.edu
1502655Sstever@eecs.umich.edu# Tracing options
1512655Sstever@eecs.umich.eduset_group("Trace Options")
1522655Sstever@eecs.umich.eduadd_option("--trace-help", action='store_true',
1532655Sstever@eecs.umich.edu    help="Print help on trace flags")
154add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
155    help="Sets the flags for tracing (-FLAG disables a flag)")
156add_option("--trace-start", metavar="TIME", type='int',
157    help="Start tracing at TIME (must be in ticks)")
158add_option("--trace-file", metavar="FILE", default="cout",
159    help="Sets the output file for tracing [Default: %default]")
160add_option("--trace-ignore", metavar="EXPR", action='append', split=':',
161    help="Ignore EXPR sim objects")
162
163# Execution Trace options
164set_group("Execution Trace Options")
165bool_option("speculative", default=True,
166    help="Don't capture speculative instructions")
167bool_option("print-cycle", default=True,
168    help="Don't print cycle numbers in trace output")
169bool_option("print-symbol", default=True,
170    help="Disable PC symbols in trace output")
171bool_option("print-opclass", default=True,
172    help="Don't print op class type in trace output")
173bool_option("print-thread", default=True,
174    help="Don't print thread number in trace output")
175bool_option("print-effaddr", default=True,
176    help="Don't print effective address in trace output")
177bool_option("print-data", default=True,
178    help="Don't print result data in trace output")
179bool_option("print-iregs", default=False,
180    help="Print fetch sequence numbers in trace output")
181bool_option("print-fetch-seq", default=False,
182    help="Print fetch sequence numbers in trace output")
183bool_option("print-cpseq", default=False,
184    help="Print correct path sequence numbers in trace output")
185#bool_option("print-reg-delta", default=False,
186#    help="Print which registers changed to what in trace output")
187bool_option("legion-lock", default=False,
188    help="Compare simulator state with Legion simulator every cycle")
189
190options = attrdict()
191arguments = []
192
193def usage(exitcode=None):
194    parser.print_help()
195    if exitcode is not None:
196        sys.exit(exitcode)
197
198def parse_args():
199    _opts,args = parser.parse_args()
200    opts = attrdict(_opts.__dict__)
201
202    # setting verbose and quiet at the same time doesn't make sense
203    if opts.verbose > 0 and opts.quiet > 0:
204        usage(2)
205
206    # store the verbosity in a single variable.  0 is default,
207    # negative numbers represent quiet and positive values indicate verbose
208    opts.verbose -= opts.quiet
209
210    del opts.quiet
211
212    options.update(opts)
213    arguments.extend(args)
214    return opts,args
215
216def main():
217    import defines
218    import info
219    import internal
220
221    parse_args()
222
223    done = False
224    if options.copyright:
225        done = True
226        print info.LICENSE
227        print
228
229    if options.authors:
230        done = True
231        print 'Author information:'
232        print
233        print info.AUTHORS
234        print
235
236    if options.readme:
237        done = True
238        print 'Readme:'
239        print
240        print info.README
241        print
242
243    if options.release_notes:
244        done = True
245        print 'Release Notes:'
246        print
247        print info.RELEASE_NOTES
248        print
249
250    if options.trace_help:
251        done = True
252        print "Base Flags:"
253        print_list(traceflags.baseFlags, indent=4)
254        print
255        print "Compound Flags:"
256        for flag in traceflags.compoundFlags:
257            if flag == 'All':
258                continue
259            print "    %s:" % flag
260            print_list(traceflags.compoundFlagMap[flag], indent=8)
261            print
262
263    if done:
264        sys.exit(0)
265
266    if options.verbose >= 0:
267        print "M5 Simulator System"
268        print brief_copyright
269        print
270        print "M5 compiled %s" % internal.main.cvar.compileDate;
271        print "M5 started %s" % datetime.now().ctime()
272        print "M5 executing on %s" % socket.gethostname()
273        print "command line:",
274        for argv in sys.argv:
275            print argv,
276        print
277
278    # check to make sure we can find the listed script
279    if not arguments or not os.path.isfile(arguments[0]):
280        if arguments and not os.path.isfile(arguments[0]):
281            print "Script %s not found" % arguments[0]
282
283        usage(2)
284
285    # tell C++ about output directory
286    internal.main.setOutputDir(options.outdir)
287
288    # update the system path with elements from the -p option
289    sys.path[0:0] = options.path
290
291    import objects
292
293    # set stats options
294    objects.Statistics.text_file = options.stats_file
295
296    # set debugging options
297    for when in options.debug_break:
298        internal.debug.schedBreakCycle(int(when))
299
300    on_flags = []
301    off_flags = []
302    for flag in options.trace_flags:
303        off = False
304        if flag.startswith('-'):
305            flag = flag[1:]
306            off = True
307        if flag not in traceflags.allFlags:
308            print >>sys.stderr, "invalid trace flag '%s'" % flag
309            sys.exit(1)
310
311        if off:
312            off_flags.append(flag)
313        else:
314            on_flags.append(flag)
315
316    for flag in on_flags:
317        internal.trace.set(flag)
318
319    for flag in off_flags:
320        internal.trace.clear(flag)
321
322    if options.trace_start is not None:
323        internal.trace.enabled = False
324        def enable_trace():
325            internal.event.enabled = True
326        internal.event.create(enable_trace, options.trace_start)
327
328    internal.trace.output(options.trace_file)
329
330    for ignore in options.trace_ignore:
331        internal.trace.ignore(ignore)
332
333    # set execution trace options
334    objects.ExecutionTrace.speculative = options.speculative
335    objects.ExecutionTrace.print_cycle = options.print_cycle
336    objects.ExecutionTrace.pc_symbol = options.print_symbol
337    objects.ExecutionTrace.print_opclass = options.print_opclass
338    objects.ExecutionTrace.print_thread = options.print_thread
339    objects.ExecutionTrace.print_effaddr = options.print_effaddr
340    objects.ExecutionTrace.print_data = options.print_data
341    objects.ExecutionTrace.print_iregs = options.print_iregs
342    objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq
343    objects.ExecutionTrace.print_cpseq = options.print_cpseq
344    #objects.ExecutionTrace.print_reg_delta = options.print_reg_delta
345    objects.ExecutionTrace.legion_lockstep = options.legion_lock
346
347    sys.argv = arguments
348    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
349
350    scope = { '__file__' : sys.argv[0],
351              '__name__' : '__m5_main__' }
352
353    # we want readline if we're doing anything interactive
354    if options.interactive or options.pdb:
355        exec "import readline" in scope
356
357    # if pdb was requested, execfile the thing under pdb, otherwise,
358    # just do the execfile normally
359    if options.pdb:
360        from pdb import Pdb
361        debugger = Pdb()
362        debugger.run('execfile("%s")' % sys.argv[0], scope)
363    else:
364        execfile(sys.argv[0], scope)
365
366    # once the script is done
367    if options.interactive:
368        interact = code.InteractiveConsole(scope)
369        interact.interact("M5 Interactive Console")
370
371if __name__ == '__main__':
372    from pprint import pprint
373
374    parse_args()
375
376    print 'opts:'
377    pprint(options, indent=4)
378    print
379
380    print 'args:'
381    pprint(arguments, indent=4)
382