main.py revision 5802
1# Copyright (c) 2005 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright 9# notice, this list of conditions and the following disclaimer in the 10# documentation and/or other materials provided with the distribution; 11# neither the name of the copyright holders nor the names of its 12# contributors may be used to endorse or promote products derived from 13# this software without specific prior written permission. 14# 15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26# 27# Authors: Nathan Binkert 28 29import code 30import datetime 31import os 32import socket 33import sys 34 35from util import attrdict 36import config 37from options import OptionParser 38 39__all__ = [ 'options', 'arguments', 'main' ] 40 41def print_list(items, indent=4): 42 line = ' ' * indent 43 for i,item in enumerate(items): 44 if len(line) + len(item) > 76: 45 print line 46 line = ' ' * indent 47 48 if i < len(items) - 1: 49 line += '%s, ' % item 50 else: 51 line += item 52 print line 53 54usage="%prog [m5 options] script.py [script options]" 55version="%prog 2.0" 56brief_copyright=''' 57Copyright (c) 2001-2008 58The Regents of The University of Michigan 59All Rights Reserved 60''' 61 62options = OptionParser(usage=usage, version=version, 63 description=brief_copyright) 64add_option = options.add_option 65set_group = options.set_group 66usage = options.usage 67 68# Help options 69add_option('-A', "--authors", action="store_true", default=False, 70 help="Show author information") 71add_option('-B', "--build-info", action="store_true", default=False, 72 help="Show build information") 73add_option('-C', "--copyright", action="store_true", default=False, 74 help="Show full copyright information") 75add_option('-R', "--readme", action="store_true", default=False, 76 help="Show the readme") 77add_option('-N', "--release-notes", action="store_true", default=False, 78 help="Show the release notes") 79 80# Options for configuring the base simulator 81add_option('-d', "--outdir", metavar="DIR", default="m5out", 82 help="Set the output directory to DIR [Default: %default]") 83add_option('-r', "--redirect-stdout", action="store_true", default=False, 84 help="Redirect stdout (& stderr, without -e) to file") 85add_option('-e', "--redirect-stderr", action="store_true", default=False, 86 help="Redirect stderr to file") 87add_option("--stdout-file", metavar="FILE", default="simout", 88 help="Filename for -r redirection [Default: %default]") 89add_option("--stderr-file", metavar="FILE", default="simerr", 90 help="Filename for -e redirection [Default: %default]") 91add_option('-i', "--interactive", action="store_true", default=False, 92 help="Invoke the interactive interpreter after running the script") 93add_option("--pdb", action="store_true", default=False, 94 help="Invoke the python debugger before running the script") 95add_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':', 96 help="Prepend PATH to the system path when invoking the script") 97add_option('-q', "--quiet", action="count", default=0, 98 help="Reduce verbosity") 99add_option('-v', "--verbose", action="count", default=0, 100 help="Increase verbosity") 101 102# Statistics options 103set_group("Statistics Options") 104add_option("--stats-file", metavar="FILE", default="stats.txt", 105 help="Sets the output file for statistics [Default: %default]") 106 107# Configuration Options 108set_group("Configuration Options") 109add_option("--dump-config", metavar="FILE", default="config.ini", 110 help="Dump configuration output file [Default: %default]") 111 112# Debugging options 113set_group("Debugging Options") 114add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',', 115 help="Cycle to create a breakpoint") 116add_option("--remote-gdb-port", type='int', default=7000, 117 help="Remote gdb base port") 118 119# Tracing options 120set_group("Trace Options") 121add_option("--trace-help", action='store_true', 122 help="Print help on trace flags") 123add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',', 124 help="Sets the flags for tracing (-FLAG disables a flag)") 125add_option("--trace-start", metavar="TIME", type='int', 126 help="Start tracing at TIME (must be in ticks)") 127add_option("--trace-file", metavar="FILE", default="cout", 128 help="Sets the output file for tracing [Default: %default]") 129add_option("--trace-ignore", metavar="EXPR", action='append', split=':', 130 help="Ignore EXPR sim objects") 131 132# Help options 133set_group("Help Options") 134add_option("--list-sim-objects", action='store_true', default=False, 135 help="List all built-in SimObjects, their parameters and default values") 136 137def main(): 138 import core 139 import debug 140 import defines 141 import event 142 import info 143 import stats 144 import trace 145 146 def check_tracing(): 147 if defines.TRACING_ON: 148 return 149 150 fatal("Tracing is not enabled. Compile with TRACING_ON") 151 152 # load the options.py config file to allow people to set their own 153 # default options 154 options_file = config.get('options.py') 155 if options_file: 156 scope = { 'options' : options } 157 execfile(options_file, scope) 158 159 arguments = options.parse_args() 160 161 if not os.path.isdir(options.outdir): 162 os.makedirs(options.outdir) 163 164 # These filenames are used only if the redirect_std* options are set 165 stdout_file = os.path.join(options.outdir, options.stdout_file) 166 stderr_file = os.path.join(options.outdir, options.stderr_file) 167 168 # Print redirection notices here before doing any redirection 169 if options.redirect_stdout and not options.redirect_stderr: 170 print "Redirecting stdout and stderr to", stdout_file 171 else: 172 if options.redirect_stdout: 173 print "Redirecting stdout to", stdout_file 174 if options.redirect_stderr: 175 print "Redirecting stderr to", stderr_file 176 177 # Now redirect stdout/stderr as desired 178 if options.redirect_stdout: 179 redir_fd = os.open(stdout_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC) 180 os.dup2(redir_fd, sys.stdout.fileno()) 181 if not options.redirect_stderr: 182 os.dup2(redir_fd, sys.stderr.fileno()) 183 184 if options.redirect_stderr: 185 redir_fd = os.open(stderr_file, os. O_WRONLY | os.O_CREAT | os.O_TRUNC) 186 os.dup2(redir_fd, sys.stderr.fileno()) 187 188 done = False 189 190 if options.build_info: 191 done = True 192 print 'Build information:' 193 print 194 print 'compiled %s' % defines.compileDate; 195 print "revision %s:%s" % (defines.hgRev, defines.hgId) 196 print "commit date %s" % defines.hgDate 197 print 'build options:' 198 keys = defines.buildEnv.keys() 199 keys.sort() 200 for key in keys: 201 val = defines.buildEnv[key] 202 print ' %s = %s' % (key, val) 203 print 204 205 if options.copyright: 206 done = True 207 print info.LICENSE 208 print 209 210 if options.authors: 211 done = True 212 print 'Author information:' 213 print 214 print info.AUTHORS 215 print 216 217 if options.readme: 218 done = True 219 print 'Readme:' 220 print 221 print info.README 222 print 223 224 if options.release_notes: 225 done = True 226 print 'Release Notes:' 227 print 228 print info.RELEASE_NOTES 229 print 230 231 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