config: Add an Energy param type.
[gem5.git] / src / python / m5 / main.py
index cdaecc1ccd7bfca9903e35292237016a83d09261..ad452886a88714991750f8a8578a8ae79f0834b3 100644 (file)
@@ -1,3 +1,15 @@
+# Copyright (c) 2016 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder.  You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
 # Copyright (c) 2005 The Regents of The University of Michigan
 # All rights reserved.
 #
@@ -34,13 +46,10 @@ import sys
 
 __all__ = [ 'options', 'arguments', 'main' ]
 
-usage="%prog [m5 options] script.py [script options]"
+usage="%prog [gem5 options] script.py [script options]"
 version="%prog 2.0"
-brief_copyright='''
-Copyright (c) 2001-2008
-The Regents of The University of Michigan
-All Rights Reserved
-'''
+brief_copyright=\
+    "gem5 is copyrighted software; use the --copyright option for details."
 
 def parse_options():
     import config
@@ -51,9 +60,9 @@ def parse_options():
     option = options.add_option
     group = options.set_group
 
+    listener_modes = ( "on", "off", "auto" )
+
     # Help options
-    option('-A', "--authors", action="store_true", default=False,
-        help="Show author information")
     option('-B', "--build-info", action="store_true", default=False,
         help="Show build information")
     option('-C', "--copyright", action="store_true", default=False,
@@ -72,6 +81,13 @@ def parse_options():
         help="Filename for -r redirection [Default: %default]")
     option("--stderr-file", metavar="FILE", default="simerr",
         help="Filename for -e redirection [Default: %default]")
+    option("--listener-mode", metavar="{on,off,auto}",
+        choices=listener_modes, default="auto",
+        help="Port (e.g., gdb) listener mode (auto: Enable if running " \
+        "interactively) [Default: %default]")
+    option("--listener-loopback-only", action="store_true", default=False,
+        help="Port listeners will only accept connections over the " \
+        "loopback device")
     option('-i', "--interactive", action="store_true", default=False,
         help="Invoke the interactive interpreter after running the script")
     option("--pdb", action="store_true", default=False,
@@ -92,31 +108,34 @@ def parse_options():
     group("Configuration Options")
     option("--dump-config", metavar="FILE", default="config.ini",
         help="Dump configuration output file [Default: %default]")
+    option("--json-config", metavar="FILE", default="config.json",
+        help="Create JSON output of the configuration [Default: %default]")
+    option("--dot-config", metavar="FILE", default="config.dot",
+        help="Create DOT & pdf outputs of the configuration [Default: %default]")
+    option("--dot-dvfs-config", metavar="FILE", default=None,
+        help="Create DOT & pdf outputs of the DVFS configuration" + \
+             " [Default: %default]")
 
     # Debugging options
     group("Debugging Options")
-    option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
-        help="Cycle to create a breakpoint")
+    option("--debug-break", metavar="TICK[,TICK]", action='append', split=',',
+        help="Create breakpoint(s) at TICK(s) " \
+             "(kills process if no debugger attached)")
     option("--debug-help", action='store_true',
-        help="Print help on trace flags")
+        help="Print help on debug flags")
     option("--debug-flags", metavar="FLAG[,FLAG]", action='append', split=',',
-        help="Sets the flags for tracing (-FLAG disables a flag)")
+        help="Sets the flags for debug output (-FLAG disables a flag)")
+    option("--debug-start", metavar="TICK", type='int',
+        help="Start debug output at TICK")
+    option("--debug-end", metavar="TICK", type='int',
+        help="End debug output at TICK")
+    option("--debug-file", metavar="FILE", default="cout",
+        help="Sets the output file for debug [Default: %default]")
+    option("--debug-ignore", metavar="EXPR", action='append', split=':',
+        help="Ignore EXPR sim objects")
     option("--remote-gdb-port", type='int', default=7000,
         help="Remote gdb base port (set to 0 to disable listening)")
 
-    # Tracing options
-    group("Trace Options")
-    option("--trace-help", action='store_true',
-        help="Print help on trace flags")
-    option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
-        help="Sets the flags for tracing (-FLAG disables a flag)")
-    option("--trace-start", metavar="TIME", type='int',
-        help="Start tracing at TIME (must be in ticks)")
-    option("--trace-file", metavar="FILE", default="cout",
-        help="Sets the output file for tracing [Default: %default]")
-    option("--trace-ignore", metavar="EXPR", action='append', split=':',
-        help="Ignore EXPR sim objects")
-
     # Help options
     group("Help Options")
     option("--list-sim-objects", action='store_true', default=False,
@@ -130,20 +149,49 @@ def parse_options():
         execfile(options_file, scope)
 
     arguments = options.parse_args()
-
     return options,arguments
 
 def interact(scope):
-    banner = "M5 Interactive Console"
-    sys.argv = []
+    banner = "gem5 Interactive Console"
+
+    ipshell = None
+    prompt_in1 = "gem5 \\#> "
+    prompt_out = "gem5 \\#: "
+
+    # Is IPython version 0.10 or earlier available?
     try:
         from IPython.Shell import IPShellEmbed
-        ipshell = IPShellEmbed(banner=banner,user_ns=scope)
-        ipshell()
+        ipshell = IPShellEmbed(argv=["-prompt_in1", prompt_in1,
+                                     "-prompt_out", prompt_out],
+                               banner=banner, user_ns=scope)
     except ImportError:
+        pass
+
+    # Is IPython version 0.11 or later available?
+    if not ipshell:
+        try:
+            import IPython
+            from IPython.config.loader import Config
+            from IPython.terminal.embed import InteractiveShellEmbed
+
+            cfg = Config()
+            cfg.PromptManager.in_template = prompt_in1
+            cfg.PromptManager.out_template = prompt_out
+            ipshell = InteractiveShellEmbed(config=cfg, user_ns=scope,
+                                            banner1=banner)
+        except ImportError:
+            pass
+
+    if ipshell:
+        ipshell()
+    else:
+        # Use the Python shell in the standard library if IPython
+        # isn't available.
         code.InteractiveConsole(scope).interact(banner)
 
 def main(*args):
+    import m5
+
     import core
     import debug
     import defines
@@ -152,9 +200,8 @@ def main(*args):
     import stats
     import trace
 
-    from util import fatal
+    from util import inform, fatal, panic, isInteractive
 
-    global options
     if len(args) == 0:
         options, arguments = parse_options()
     elif len(args) == 2:
@@ -162,12 +209,18 @@ def main(*args):
     else:
         raise TypeError, "main() takes 0 or 2 arguments (%d given)" % len(args)
 
+    m5.options = options
+
     def check_tracing():
         if defines.TRACING_ON:
             return
 
         fatal("Tracing is not enabled.  Compile with TRACING_ON")
 
+    # Set the main event queue for the main thread.
+    event.mainq = event.getEventQueue(0)
+    event.setEventQueue(event.mainq)
+
     if not os.path.isdir(options.outdir):
         os.makedirs(options.outdir)
 
@@ -212,14 +265,7 @@ def main(*args):
 
     if options.copyright:
         done = True
-        print info.LICENSE
-        print
-
-    if options.authors:
-        done = True
-        print 'Author information:'
-        print
-        print info.AUTHORS
+        print info.COPYING
         print
 
     if options.readme:
@@ -263,19 +309,21 @@ def main(*args):
         options.usage(2)
 
     verbose = options.verbose - options.quiet
-    if options.verbose >= 0:
-        print "M5 Simulator System"
+    if verbose >= 0:
+        print "gem5 Simulator System.  http://gem5.org"
         print brief_copyright
         print
 
-        print "M5 compiled %s" % defines.compileDate;
+        print "gem5 compiled %s" % defines.compileDate;
 
-        print "M5 started %s" % datetime.datetime.now().strftime("%b %e %Y %X")
-        print "M5 executing on %s" % socket.gethostname()
+        print "gem5 started %s" % \
+            datetime.datetime.now().strftime("%b %e %Y %X")
+        print "gem5 executing on %s, pid %d" % \
+            (socket.gethostname(), os.getpid())
 
-        print "command line:",
-        for argv in sys.argv:
-            print argv,
+        # in Python 3 pipes.quote() is moved to shlex.quote()
+        import pipes
+        print "command line:", " ".join(map(pipes.quote, sys.argv))
         print
 
     # check to make sure we can find the listed script
@@ -292,12 +340,28 @@ def main(*args):
     sys.path[0:0] = options.path
 
     # set stats options
-    stats.initText(options.stats_file)
+    stats.addStatVisitor(options.stats_file)
+
+    # Disable listeners unless running interactively or explicitly
+    # enabled
+    if options.listener_mode == "off":
+        m5.disableAllListeners()
+    elif options.listener_mode == "auto":
+        if not isInteractive():
+            inform("Standard input is not a terminal, disabling listeners.")
+            m5.disableAllListeners()
+    elif options.listener_mode == "on":
+        pass
+    else:
+        panic("Unhandled listener mode: %s" % options.listener_mode)
+
+    if options.listener_loopback_only:
+        m5.listenersLoopbackOnly()
 
     # set debugging options
     debug.setRemoteGDBPort(options.remote_gdb_port)
     for when in options.debug_break:
-        debug.schedBreakCycle(int(when))
+        debug.schedBreak(int(when))
 
     if options.debug_flags:
         check_tracing()
@@ -319,16 +383,21 @@ def main(*args):
             else:
                 debug.flags[flag].enable()
 
-    if options.trace_start:
+    if options.debug_start:
         check_tracing()
-        e = event.create(trace.enable, event.Event.Trace_Enable_Pri)
-        event.mainq.schedule(e, options.trace_start)
+        e = event.create(trace.enable, event.Event.Debug_Enable_Pri)
+        event.mainq.schedule(e, options.debug_start)
     else:
         trace.enable()
 
-    trace.output(options.trace_file)
+    if options.debug_end:
+        check_tracing()
+        e = event.create(trace.disable, event.Event.Debug_Enable_Pri)
+        event.mainq.schedule(e, options.debug_end)
+
+    trace.output(options.debug_file)
 
-    for ignore in options.trace_ignore:
+    for ignore in options.debug_ignore:
         check_tracing()
         trace.ignore(ignore)
 
@@ -341,10 +410,6 @@ def main(*args):
     scope = { '__file__' : filename,
               '__name__' : '__m5_main__' }
 
-    # we want readline if we're doing anything interactive
-    if options.interactive or options.pdb:
-        exec "import readline" in scope
-
     # if pdb was requested, execfile the thing under pdb, otherwise,
     # just do the execfile normally
     if options.pdb: