main.py revision 5801
19257Ssascha.bischoff@arm.com# Copyright (c) 2005 The Regents of The University of Michigan
29257Ssascha.bischoff@arm.com# All rights reserved.
39257Ssascha.bischoff@arm.com#
49257Ssascha.bischoff@arm.com# Redistribution and use in source and binary forms, with or without
59257Ssascha.bischoff@arm.com# modification, are permitted provided that the following conditions are
69257Ssascha.bischoff@arm.com# met: redistributions of source code must retain the above copyright
79257Ssascha.bischoff@arm.com# notice, this list of conditions and the following disclaimer;
89257Ssascha.bischoff@arm.com# redistributions in binary form must reproduce the above copyright
99257Ssascha.bischoff@arm.com# notice, this list of conditions and the following disclaimer in the
109257Ssascha.bischoff@arm.com# documentation and/or other materials provided with the distribution;
119257Ssascha.bischoff@arm.com# neither the name of the copyright holders nor the names of its
129257Ssascha.bischoff@arm.com# contributors may be used to endorse or promote products derived from
139257Ssascha.bischoff@arm.com# this software without specific prior written permission.
149257Ssascha.bischoff@arm.com#
159257Ssascha.bischoff@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
169257Ssascha.bischoff@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
179257Ssascha.bischoff@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
189257Ssascha.bischoff@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
199257Ssascha.bischoff@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
209257Ssascha.bischoff@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
219257Ssascha.bischoff@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
229257Ssascha.bischoff@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
239257Ssascha.bischoff@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
249257Ssascha.bischoff@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
259257Ssascha.bischoff@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
269257Ssascha.bischoff@arm.com#
279257Ssascha.bischoff@arm.com# Authors: Nathan Binkert
289257Ssascha.bischoff@arm.com
299257Ssascha.bischoff@arm.comimport code
309257Ssascha.bischoff@arm.comimport datetime
319257Ssascha.bischoff@arm.comimport os
329257Ssascha.bischoff@arm.comimport socket
339257Ssascha.bischoff@arm.comimport sys
349257Ssascha.bischoff@arm.com
359257Ssascha.bischoff@arm.comfrom util import attrdict
369257Ssascha.bischoff@arm.comimport config
379257Ssascha.bischoff@arm.comfrom options import OptionParser
389257Ssascha.bischoff@arm.com
399257Ssascha.bischoff@arm.com__all__ = [ 'options', 'arguments', 'main' ]
409257Ssascha.bischoff@arm.com
419257Ssascha.bischoff@arm.comdef print_list(items, indent=4):
429257Ssascha.bischoff@arm.com    line = ' ' * indent
439257Ssascha.bischoff@arm.com    for i,item in enumerate(items):
449257Ssascha.bischoff@arm.com        if len(line) + len(item) > 76:
459257Ssascha.bischoff@arm.com            print line
469257Ssascha.bischoff@arm.com            line = ' ' * indent
479257Ssascha.bischoff@arm.com
489257Ssascha.bischoff@arm.com        if i < len(items) - 1:
499257Ssascha.bischoff@arm.com            line += '%s, ' % item
509257Ssascha.bischoff@arm.com        else:
519257Ssascha.bischoff@arm.com            line += item
529257Ssascha.bischoff@arm.com            print line
539257Ssascha.bischoff@arm.com
549257Ssascha.bischoff@arm.comusage="%prog [m5 options] script.py [script options]"
559257Ssascha.bischoff@arm.comversion="%prog 2.0"
569257Ssascha.bischoff@arm.combrief_copyright='''
579257Ssascha.bischoff@arm.comCopyright (c) 2001-2008
589257Ssascha.bischoff@arm.comThe Regents of The University of Michigan
599257Ssascha.bischoff@arm.comAll Rights Reserved
609257Ssascha.bischoff@arm.com'''
619257Ssascha.bischoff@arm.com
629257Ssascha.bischoff@arm.comoptions = OptionParser(usage=usage, version=version,
639257Ssascha.bischoff@arm.com                       description=brief_copyright)
649257Ssascha.bischoff@arm.comadd_option = options.add_option
659257Ssascha.bischoff@arm.comset_group = options.set_group
669257Ssascha.bischoff@arm.comusage = options.usage
679257Ssascha.bischoff@arm.com
689257Ssascha.bischoff@arm.com# Help options
699257Ssascha.bischoff@arm.comadd_option('-A', "--authors", action="store_true", default=False,
709257Ssascha.bischoff@arm.com    help="Show author information")
719257Ssascha.bischoff@arm.comadd_option('-B', "--build-info", action="store_true", default=False,
729257Ssascha.bischoff@arm.com    help="Show build information")
739257Ssascha.bischoff@arm.comadd_option('-C', "--copyright", action="store_true", default=False,
749257Ssascha.bischoff@arm.com    help="Show full copyright information")
759257Ssascha.bischoff@arm.comadd_option('-R', "--readme", action="store_true", default=False,
769257Ssascha.bischoff@arm.com    help="Show the readme")
779257Ssascha.bischoff@arm.comadd_option('-N', "--release-notes", action="store_true", default=False,
789257Ssascha.bischoff@arm.com    help="Show the release notes")
799257Ssascha.bischoff@arm.com
809257Ssascha.bischoff@arm.com# Options for configuring the base simulator
819257Ssascha.bischoff@arm.comadd_option('-d', "--outdir", metavar="DIR", default="m5out",
829257Ssascha.bischoff@arm.com    help="Set the output directory to DIR [Default: %default]")
839257Ssascha.bischoff@arm.comadd_option('-r', "--redirect-stdout", action="store_true", default=False,
849257Ssascha.bischoff@arm.com           help="Redirect stdout (& stderr, without -e) to file")
859257Ssascha.bischoff@arm.comadd_option('-e', "--redirect-stderr", action="store_true", default=False,
869257Ssascha.bischoff@arm.com           help="Redirect stderr to file")
879257Ssascha.bischoff@arm.comadd_option("--stdout-file", metavar="FILE", default="simout",
889257Ssascha.bischoff@arm.com           help="Filename for -r redirection [Default: %default]")
899257Ssascha.bischoff@arm.comadd_option("--stderr-file", metavar="FILE", default="simerr",
909257Ssascha.bischoff@arm.com           help="Filename for -e redirection [Default: %default]")
919257Ssascha.bischoff@arm.comadd_option('-i', "--interactive", action="store_true", default=False,
929257Ssascha.bischoff@arm.com    help="Invoke the interactive interpreter after running the script")
939257Ssascha.bischoff@arm.comadd_option("--pdb", action="store_true", default=False,
949257Ssascha.bischoff@arm.com    help="Invoke the python debugger before running the script")
959257Ssascha.bischoff@arm.comadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
969257Ssascha.bischoff@arm.com    help="Prepend PATH to the system path when invoking the script")
979257Ssascha.bischoff@arm.comadd_option('-q', "--quiet", action="count", default=0,
989257Ssascha.bischoff@arm.com    help="Reduce verbosity")
999257Ssascha.bischoff@arm.comadd_option('-v', "--verbose", action="count", default=0,
1009257Ssascha.bischoff@arm.com    help="Increase verbosity")
1019257Ssascha.bischoff@arm.com
1029257Ssascha.bischoff@arm.com# Statistics options
1039257Ssascha.bischoff@arm.comset_group("Statistics Options")
1049257Ssascha.bischoff@arm.comadd_option("--stats-file", metavar="FILE", default="stats.txt",
1059257Ssascha.bischoff@arm.com    help="Sets the output file for statistics [Default: %default]")
1069257Ssascha.bischoff@arm.com
1079257Ssascha.bischoff@arm.com# Configuration Options
1089257Ssascha.bischoff@arm.comset_group("Configuration Options")
1099257Ssascha.bischoff@arm.comadd_option("--dump-config", metavar="FILE", default="config.ini",
1109257Ssascha.bischoff@arm.com    help="Dump configuration output file [Default: %default]")
1119257Ssascha.bischoff@arm.com
1129257Ssascha.bischoff@arm.com# Debugging options
1139257Ssascha.bischoff@arm.comset_group("Debugging Options")
1149257Ssascha.bischoff@arm.comadd_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
1159257Ssascha.bischoff@arm.com    help="Cycle to create a breakpoint")
1169257Ssascha.bischoff@arm.comadd_option("--remote-gdb-port", type='int', default=7000,
1179257Ssascha.bischoff@arm.com    help="Remote gdb base port")
1189257Ssascha.bischoff@arm.com
1199257Ssascha.bischoff@arm.com# Tracing options
1209257Ssascha.bischoff@arm.comset_group("Trace Options")
1219257Ssascha.bischoff@arm.comadd_option("--trace-help", action='store_true',
1229257Ssascha.bischoff@arm.com    help="Print help on trace flags")
1239257Ssascha.bischoff@arm.comadd_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
1249257Ssascha.bischoff@arm.com    help="Sets the flags for tracing (-FLAG disables a flag)")
1259257Ssascha.bischoff@arm.comadd_option("--trace-start", metavar="TIME", type='int',
1269257Ssascha.bischoff@arm.com    help="Start tracing at TIME (must be in ticks)")
1279257Ssascha.bischoff@arm.comadd_option("--trace-file", metavar="FILE", default="cout",
1289257Ssascha.bischoff@arm.com    help="Sets the output file for tracing [Default: %default]")
1299257Ssascha.bischoff@arm.comadd_option("--trace-ignore", metavar="EXPR", action='append', split=':',
1309257Ssascha.bischoff@arm.com    help="Ignore EXPR sim objects")
1319257Ssascha.bischoff@arm.com
1329257Ssascha.bischoff@arm.com# Help options
1339257Ssascha.bischoff@arm.comset_group("Help Options")
1349257Ssascha.bischoff@arm.comadd_option("--list-sim-objects", action='store_true', default=False,
1359257Ssascha.bischoff@arm.com    help="List all built-in SimObjects, their parameters and default values")
1369257Ssascha.bischoff@arm.com
1379257Ssascha.bischoff@arm.comdef main():
1389257Ssascha.bischoff@arm.com    import core
1399257Ssascha.bischoff@arm.com    import debug
1409257Ssascha.bischoff@arm.com    import defines
1419257Ssascha.bischoff@arm.com    import event
1429257Ssascha.bischoff@arm.com    import info
1439257Ssascha.bischoff@arm.com    import stats
1449257Ssascha.bischoff@arm.com    import trace
1459257Ssascha.bischoff@arm.com
1469257Ssascha.bischoff@arm.com    def check_tracing():
1479257Ssascha.bischoff@arm.com        if defines.TRACING_ON:
1489257Ssascha.bischoff@arm.com            return
1499257Ssascha.bischoff@arm.com
1509257Ssascha.bischoff@arm.com        panic("Tracing is not enabled.  Compile with TRACING_ON")
1519257Ssascha.bischoff@arm.com
1529257Ssascha.bischoff@arm.com    # load the options.py config file to allow people to set their own
1539257Ssascha.bischoff@arm.com    # default options
1549257Ssascha.bischoff@arm.com    options_file = config.get('options.py')
1559257Ssascha.bischoff@arm.com    if options_file:
1569257Ssascha.bischoff@arm.com        scope = { 'options' : options }
1579257Ssascha.bischoff@arm.com        execfile(options_file, scope)
1589257Ssascha.bischoff@arm.com
1599257Ssascha.bischoff@arm.com    arguments = options.parse_args()
1609257Ssascha.bischoff@arm.com
1619257Ssascha.bischoff@arm.com    if not os.path.isdir(options.outdir):
1629257Ssascha.bischoff@arm.com        os.makedirs(options.outdir)
1639257Ssascha.bischoff@arm.com
1649257Ssascha.bischoff@arm.com    # These filenames are used only if the redirect_std* options are set
1659257Ssascha.bischoff@arm.com    stdout_file = os.path.join(options.outdir, options.stdout_file)
1669257Ssascha.bischoff@arm.com    stderr_file = os.path.join(options.outdir, options.stderr_file)
1679257Ssascha.bischoff@arm.com
1689257Ssascha.bischoff@arm.com    # Print redirection notices here before doing any redirection
1699257Ssascha.bischoff@arm.com    if options.redirect_stdout and not options.redirect_stderr:
1709257Ssascha.bischoff@arm.com        print "Redirecting stdout and stderr to", stdout_file
1719257Ssascha.bischoff@arm.com    else:
1729257Ssascha.bischoff@arm.com        if options.redirect_stdout:
1739257Ssascha.bischoff@arm.com            print "Redirecting stdout to", stdout_file
1749257Ssascha.bischoff@arm.com        if options.redirect_stderr:
1759257Ssascha.bischoff@arm.com            print "Redirecting stderr to", stderr_file
1769257Ssascha.bischoff@arm.com
1779257Ssascha.bischoff@arm.com    # Now redirect stdout/stderr as desired
1789257Ssascha.bischoff@arm.com    if options.redirect_stdout:
1799257Ssascha.bischoff@arm.com        redir_fd = os.open(stdout_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
1809257Ssascha.bischoff@arm.com        os.dup2(redir_fd, sys.stdout.fileno())
1819257Ssascha.bischoff@arm.com        if not options.redirect_stderr:
1829257Ssascha.bischoff@arm.com            os.dup2(redir_fd, sys.stderr.fileno())
1839257Ssascha.bischoff@arm.com
1849257Ssascha.bischoff@arm.com    if options.redirect_stderr:
1859257Ssascha.bischoff@arm.com        redir_fd = os.open(stderr_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC)
1869257Ssascha.bischoff@arm.com        os.dup2(redir_fd, sys.stderr.fileno())
1879257Ssascha.bischoff@arm.com
1889257Ssascha.bischoff@arm.com    done = False
1899257Ssascha.bischoff@arm.com
1909257Ssascha.bischoff@arm.com    if options.build_info:
1919257Ssascha.bischoff@arm.com        done = True
1929257Ssascha.bischoff@arm.com        print 'Build information:'
1939257Ssascha.bischoff@arm.com        print
1949257Ssascha.bischoff@arm.com        print 'compiled %s' % defines.compileDate;
1959257Ssascha.bischoff@arm.com        print "revision %s:%s" % (defines.hgRev, defines.hgId)
1969257Ssascha.bischoff@arm.com        print "commit date %s" % defines.hgDate
1979257Ssascha.bischoff@arm.com        print 'build options:'
1989257Ssascha.bischoff@arm.com        keys = defines.buildEnv.keys()
1999257Ssascha.bischoff@arm.com        keys.sort()
2009257Ssascha.bischoff@arm.com        for key in keys:
2019257Ssascha.bischoff@arm.com            val = defines.buildEnv[key]
2029257Ssascha.bischoff@arm.com            print '    %s = %s' % (key, val)
2039257Ssascha.bischoff@arm.com        print
2049257Ssascha.bischoff@arm.com
2059257Ssascha.bischoff@arm.com    if options.copyright:
2069257Ssascha.bischoff@arm.com        done = True
2079257Ssascha.bischoff@arm.com        print info.LICENSE
2089257Ssascha.bischoff@arm.com        print
2099257Ssascha.bischoff@arm.com
2109257Ssascha.bischoff@arm.com    if options.authors:
2119257Ssascha.bischoff@arm.com        done = True
2129257Ssascha.bischoff@arm.com        print 'Author information:'
2139257Ssascha.bischoff@arm.com        print
2149257Ssascha.bischoff@arm.com        print info.AUTHORS
2159257Ssascha.bischoff@arm.com        print
2169257Ssascha.bischoff@arm.com
2179257Ssascha.bischoff@arm.com    if options.readme:
2189257Ssascha.bischoff@arm.com        done = True
2199257Ssascha.bischoff@arm.com        print 'Readme:'
2209257Ssascha.bischoff@arm.com        print
2219257Ssascha.bischoff@arm.com        print info.README
2229257Ssascha.bischoff@arm.com        print
2239257Ssascha.bischoff@arm.com
2249257Ssascha.bischoff@arm.com    if options.release_notes:
2259257Ssascha.bischoff@arm.com        done = True
2269257Ssascha.bischoff@arm.com        print 'Release Notes:'
2279257Ssascha.bischoff@arm.com        print
2289257Ssascha.bischoff@arm.com        print info.RELEASE_NOTES
2299257Ssascha.bischoff@arm.com        print
2309257Ssascha.bischoff@arm.com
2319257Ssascha.bischoff@arm.com    if options.trace_help:
232        done = True
233        check_tracing()
234        trace.help()
235
236    if options.list_sim_objects:
237        import SimObject
238        done = True
239        print "SimObjects:"
240        objects = SimObject.allClasses.keys()
241        objects.sort()
242        for name in objects:
243            obj = SimObject.allClasses[name]
244            print "    %s" % obj
245            params = obj._params.keys()
246            params.sort()
247            for pname in params:
248                param = obj._params[pname]
249                default = getattr(param, 'default', '')
250                print "        %s" % pname
251                if default:
252                    print "            default: %s" % default
253                print "            desc: %s" % param.desc
254                print
255            print
256
257    if done:
258        sys.exit(0)
259
260    # setting verbose and quiet at the same time doesn't make sense
261    if options.verbose > 0 and options.quiet > 0:
262        options.usage(2)
263
264    verbose = options.verbose - options.quiet
265    if options.verbose >= 0:
266        print "M5 Simulator System"
267        print brief_copyright
268        print
269
270        print "M5 compiled %s" % defines.compileDate;
271        print "M5 revision %s:%s" % (defines.hgRev, defines.hgId)
272        print "M5 commit date %s" % defines.hgDate
273
274        print "M5 started %s" % datetime.datetime.now().strftime("%b %e %Y %X")
275        print "M5 executing on %s" % socket.gethostname()
276
277        print "command line:",
278        for argv in sys.argv:
279            print argv,
280        print
281
282    # check to make sure we can find the listed script
283    if not arguments or not os.path.isfile(arguments[0]):
284        if arguments and not os.path.isfile(arguments[0]):
285            print "Script %s not found" % arguments[0]
286
287        options.usage(2)
288
289    # tell C++ about output directory
290    core.setOutputDir(options.outdir)
291
292    # update the system path with elements from the -p option
293    sys.path[0:0] = options.path
294
295    # set stats options
296    stats.initText(options.stats_file)
297
298    # set debugging options
299    debug.setRemoteGDBPort(options.remote_gdb_port)
300    for when in options.debug_break:
301        debug.schedBreakCycle(int(when))
302
303    if options.trace_flags:
304        check_tracing()
305
306        on_flags = []
307        off_flags = []
308        for flag in options.trace_flags:
309            off = False
310            if flag.startswith('-'):
311                flag = flag[1:]
312                off = True
313            if flag not in trace.flags.all and flag != "All":
314                print >>sys.stderr, "invalid trace flag '%s'" % flag
315                sys.exit(1)
316
317            if off:
318                off_flags.append(flag)
319            else:
320                on_flags.append(flag)
321
322        for flag in on_flags:
323            trace.set(flag)
324
325        for flag in off_flags:
326            trace.clear(flag)
327
328    if options.trace_start:
329        check_tracing()
330        e = event.create(trace.enable)
331        event.mainq.schedule(e, options.trace_start)
332    else:
333        trace.enable()
334
335    trace.output(options.trace_file)
336
337    for ignore in options.trace_ignore:
338        check_tracing()
339        trace.ignore(ignore)
340
341    sys.argv = arguments
342    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
343
344    filename = sys.argv[0]
345    filedata = file(filename, 'r').read()
346    filecode = compile(filedata, filename, 'exec')
347    scope = { '__file__' : filename,
348              '__name__' : '__m5_main__' }
349
350    # we want readline if we're doing anything interactive
351    if options.interactive or options.pdb:
352        exec "import readline" in scope
353
354    # if pdb was requested, execfile the thing under pdb, otherwise,
355    # just do the execfile normally
356    if options.pdb:
357        import pdb
358        import traceback
359
360        pdb = pdb.Pdb()
361        try:
362            pdb.run(filecode, scope)
363        except SystemExit:
364            print "The program exited via sys.exit(). Exit status: ",
365            print sys.exc_info()[1]
366        except:
367            traceback.print_exc()
368            print "Uncaught exception. Entering post mortem debugging"
369            t = sys.exc_info()[2]
370            while t.tb_next is not None:
371                t = t.tb_next
372                pdb.interaction(t.tb_frame,t)
373    else:
374        exec filecode in scope
375
376    # once the script is done
377    if options.interactive:
378        interact = code.InteractiveConsole(scope)
379        interact.interact("M5 Interactive Console")
380
381if __name__ == '__main__':
382    from pprint import pprint
383
384    # load the options.py config file to allow people to set their own
385    # default options
386    options_file = config.get('options.py')
387    if options_file:
388        scope = { 'options' : options }
389        execfile(options_file, scope)
390
391    arguments = options.parse_args()
392
393    print 'opts:'
394    pprint(options, indent=4)
395    print
396
397    print 'args:'
398    pprint(arguments, indent=4)
399