main.py revision 4087
112027Sjungma@eit.uni-kl.de# Copyright (c) 2005 The Regents of The University of Michigan 212027Sjungma@eit.uni-kl.de# All rights reserved. 312027Sjungma@eit.uni-kl.de# 412027Sjungma@eit.uni-kl.de# Redistribution and use in source and binary forms, with or without 512027Sjungma@eit.uni-kl.de# modification, are permitted provided that the following conditions are 612027Sjungma@eit.uni-kl.de# met: redistributions of source code must retain the above copyright 712027Sjungma@eit.uni-kl.de# notice, this list of conditions and the following disclaimer; 812027Sjungma@eit.uni-kl.de# redistributions in binary form must reproduce the above copyright 912027Sjungma@eit.uni-kl.de# notice, this list of conditions and the following disclaimer in the 1012027Sjungma@eit.uni-kl.de# documentation and/or other materials provided with the distribution; 1112027Sjungma@eit.uni-kl.de# neither the name of the copyright holders nor the names of its 1212027Sjungma@eit.uni-kl.de# contributors may be used to endorse or promote products derived from 1312027Sjungma@eit.uni-kl.de# this software without specific prior written permission. 1412027Sjungma@eit.uni-kl.de# 1512027Sjungma@eit.uni-kl.de# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612027Sjungma@eit.uni-kl.de# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712027Sjungma@eit.uni-kl.de# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812027Sjungma@eit.uni-kl.de# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912027Sjungma@eit.uni-kl.de# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012027Sjungma@eit.uni-kl.de# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112027Sjungma@eit.uni-kl.de# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212027Sjungma@eit.uni-kl.de# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312027Sjungma@eit.uni-kl.de# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412027Sjungma@eit.uni-kl.de# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512027Sjungma@eit.uni-kl.de# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612027Sjungma@eit.uni-kl.de# 2712027Sjungma@eit.uni-kl.de# Authors: Nathan Binkert 2812027Sjungma@eit.uni-kl.de 2912027Sjungma@eit.uni-kl.deimport code, optparse, os, socket, sys 3012027Sjungma@eit.uni-kl.defrom datetime import datetime 3112027Sjungma@eit.uni-kl.defrom attrdict import attrdict 3212027Sjungma@eit.uni-kl.deimport traceflags 3312027Sjungma@eit.uni-kl.de 3412027Sjungma@eit.uni-kl.de__all__ = [ 'options', 'arguments', 'main' ] 3512027Sjungma@eit.uni-kl.de 3612027Sjungma@eit.uni-kl.deusage="%prog [m5 options] script.py [script options]" 3712027Sjungma@eit.uni-kl.deversion="%prog 2.0" 3812027Sjungma@eit.uni-kl.debrief_copyright=''' 3912027Sjungma@eit.uni-kl.deCopyright (c) 2001-2006 4012027Sjungma@eit.uni-kl.deThe Regents of The University of Michigan 4112027Sjungma@eit.uni-kl.deAll Rights Reserved 4212027Sjungma@eit.uni-kl.de''' 4312027Sjungma@eit.uni-kl.de 4412027Sjungma@eit.uni-kl.dedef print_list(items, indent=4): 4512027Sjungma@eit.uni-kl.de line = ' ' * indent 4612027Sjungma@eit.uni-kl.de for i,item in enumerate(items): 4712027Sjungma@eit.uni-kl.de if len(line) + len(item) > 76: 4812027Sjungma@eit.uni-kl.de print line 4912027Sjungma@eit.uni-kl.de line = ' ' * indent 5012027Sjungma@eit.uni-kl.de 5112027Sjungma@eit.uni-kl.de if i < len(items) - 1: 5212027Sjungma@eit.uni-kl.de line += '%s, ' % item 5312027Sjungma@eit.uni-kl.de else: 5412027Sjungma@eit.uni-kl.de line += item 5512027Sjungma@eit.uni-kl.de print line 5612027Sjungma@eit.uni-kl.de 5712027Sjungma@eit.uni-kl.de# there's only one option parsing done, so make it global and add some 5812027Sjungma@eit.uni-kl.de# helper functions to make it work well. 5912027Sjungma@eit.uni-kl.deparser = optparse.OptionParser(usage=usage, version=version, 6012027Sjungma@eit.uni-kl.de description=brief_copyright, 6112027Sjungma@eit.uni-kl.de formatter=optparse.TitledHelpFormatter()) 6212027Sjungma@eit.uni-kl.deparser.disable_interspersed_args() 6312027Sjungma@eit.uni-kl.de 6412027Sjungma@eit.uni-kl.de# current option group 6512027Sjungma@eit.uni-kl.degroup = None 6612027Sjungma@eit.uni-kl.de 6712027Sjungma@eit.uni-kl.dedef set_group(*args, **kwargs): 6812027Sjungma@eit.uni-kl.de '''set the current option group''' 6912027Sjungma@eit.uni-kl.de global group 7012027Sjungma@eit.uni-kl.de if not args and not kwargs: 7112027Sjungma@eit.uni-kl.de group = None 7212027Sjungma@eit.uni-kl.de else: 7312027Sjungma@eit.uni-kl.de group = parser.add_option_group(*args, **kwargs) 7412027Sjungma@eit.uni-kl.de 7512027Sjungma@eit.uni-kl.declass splitter(object): 7612027Sjungma@eit.uni-kl.de def __init__(self, split): 7712027Sjungma@eit.uni-kl.de self.split = split 7812027Sjungma@eit.uni-kl.de def __call__(self, option, opt_str, value, parser): 7912027Sjungma@eit.uni-kl.de getattr(parser.values, option.dest).extend(value.split(self.split)) 8012027Sjungma@eit.uni-kl.de 8112027Sjungma@eit.uni-kl.dedef add_option(*args, **kwargs): 8212027Sjungma@eit.uni-kl.de '''add an option to the current option group, or global none set''' 8312027Sjungma@eit.uni-kl.de 8412027Sjungma@eit.uni-kl.de # if action=split, but allows the option arguments 8512027Sjungma@eit.uni-kl.de # themselves to be lists separated by the split variable''' 8612027Sjungma@eit.uni-kl.de 8712027Sjungma@eit.uni-kl.de if kwargs.get('action', None) == 'append' and 'split' in kwargs: 8812027Sjungma@eit.uni-kl.de split = kwargs.pop('split') 8912027Sjungma@eit.uni-kl.de kwargs['default'] = [] 9012027Sjungma@eit.uni-kl.de kwargs['type'] = 'string' 9112027Sjungma@eit.uni-kl.de kwargs['action'] = 'callback' 9212027Sjungma@eit.uni-kl.de kwargs['callback'] = splitter(split) 9312027Sjungma@eit.uni-kl.de 9412027Sjungma@eit.uni-kl.de if group: 9512027Sjungma@eit.uni-kl.de return group.add_option(*args, **kwargs) 9612027Sjungma@eit.uni-kl.de 9712027Sjungma@eit.uni-kl.de return parser.add_option(*args, **kwargs) 9812027Sjungma@eit.uni-kl.de 9912027Sjungma@eit.uni-kl.dedef bool_option(name, default, help): 10012027Sjungma@eit.uni-kl.de '''add a boolean option called --name and --no-name. 10112027Sjungma@eit.uni-kl.de Display help depending on which is the default''' 10212027Sjungma@eit.uni-kl.de 10312027Sjungma@eit.uni-kl.de tname = '--%s' % name 10412027Sjungma@eit.uni-kl.de fname = '--no-%s' % name 10512027Sjungma@eit.uni-kl.de dest = name.replace('-', '_') 10612027Sjungma@eit.uni-kl.de if default: 10712027Sjungma@eit.uni-kl.de thelp = optparse.SUPPRESS_HELP 10812027Sjungma@eit.uni-kl.de fhelp = help 10912027Sjungma@eit.uni-kl.de else: 11012027Sjungma@eit.uni-kl.de thelp = help 11112027Sjungma@eit.uni-kl.de fhelp = optparse.SUPPRESS_HELP 11212027Sjungma@eit.uni-kl.de 11312027Sjungma@eit.uni-kl.de add_option(tname, action="store_true", default=default, help=thelp) 11412027Sjungma@eit.uni-kl.de add_option(fname, action="store_false", dest=dest, help=fhelp) 11512027Sjungma@eit.uni-kl.de 11612027Sjungma@eit.uni-kl.de# Help options 11712027Sjungma@eit.uni-kl.deadd_option('-A', "--authors", action="store_true", default=False, 11812027Sjungma@eit.uni-kl.de help="Show author information") 11912027Sjungma@eit.uni-kl.deadd_option('-C', "--copyright", action="store_true", default=False, 12012027Sjungma@eit.uni-kl.de help="Show full copyright information") 12112027Sjungma@eit.uni-kl.deadd_option('-R', "--readme", action="store_true", default=False, 12212027Sjungma@eit.uni-kl.de help="Show the readme") 12312027Sjungma@eit.uni-kl.deadd_option('-N', "--release-notes", action="store_true", default=False, 12412027Sjungma@eit.uni-kl.de help="Show the release notes") 12512027Sjungma@eit.uni-kl.de 12612027Sjungma@eit.uni-kl.de# Options for configuring the base simulator 12712027Sjungma@eit.uni-kl.deadd_option('-d', "--outdir", metavar="DIR", default=".", 12812027Sjungma@eit.uni-kl.de help="Set the output directory to DIR [Default: %default]") 12912027Sjungma@eit.uni-kl.deadd_option('-i', "--interactive", action="store_true", default=False, 13012027Sjungma@eit.uni-kl.de help="Invoke the interactive interpreter after running the script") 13112027Sjungma@eit.uni-kl.deadd_option("--pdb", action="store_true", default=False, 13212027Sjungma@eit.uni-kl.de help="Invoke the python debugger before running the script") 13312027Sjungma@eit.uni-kl.deadd_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':', 13412027Sjungma@eit.uni-kl.de help="Prepend PATH to the system path when invoking the script") 13512027Sjungma@eit.uni-kl.deadd_option('-q', "--quiet", action="count", default=0, 13612027Sjungma@eit.uni-kl.de help="Reduce verbosity") 13712027Sjungma@eit.uni-kl.deadd_option('-v', "--verbose", action="count", default=0, 13812027Sjungma@eit.uni-kl.de help="Increase verbosity") 13912027Sjungma@eit.uni-kl.de 14012027Sjungma@eit.uni-kl.de# Statistics options 14112027Sjungma@eit.uni-kl.deset_group("Statistics Options") 14212027Sjungma@eit.uni-kl.deadd_option("--stats-file", metavar="FILE", default="m5stats.txt", 14312027Sjungma@eit.uni-kl.de help="Sets the output file for statistics [Default: %default]") 14412027Sjungma@eit.uni-kl.de 145# Debugging options 146set_group("Debugging Options") 147add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',', 148 help="Cycle to create a breakpoint") 149 150# Tracing options 151set_group("Trace Options") 152add_option("--trace-help", action='store_true', 153 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 163options = attrdict() 164arguments = [] 165 166def usage(exitcode=None): 167 parser.print_help() 168 if exitcode is not None: 169 sys.exit(exitcode) 170 171def parse_args(): 172 _opts,args = parser.parse_args() 173 opts = attrdict(_opts.__dict__) 174 175 # setting verbose and quiet at the same time doesn't make sense 176 if opts.verbose > 0 and opts.quiet > 0: 177 usage(2) 178 179 # store the verbosity in a single variable. 0 is default, 180 # negative numbers represent quiet and positive values indicate verbose 181 opts.verbose -= opts.quiet 182 183 del opts.quiet 184 185 options.update(opts) 186 arguments.extend(args) 187 return opts,args 188 189def main(): 190 import defines 191 import info 192 import internal 193 194 parse_args() 195 196 done = False 197 if options.copyright: 198 done = True 199 print info.LICENSE 200 print 201 202 if options.authors: 203 done = True 204 print 'Author information:' 205 print 206 print info.AUTHORS 207 print 208 209 if options.readme: 210 done = True 211 print 'Readme:' 212 print 213 print info.README 214 print 215 216 if options.release_notes: 217 done = True 218 print 'Release Notes:' 219 print 220 print info.RELEASE_NOTES 221 print 222 223 if options.trace_help: 224 done = True 225 print "Base Flags:" 226 print_list(traceflags.baseFlags, indent=4) 227 print 228 print "Compound Flags:" 229 for flag in traceflags.compoundFlags: 230 if flag == 'All': 231 continue 232 print " %s:" % flag 233 print_list(traceflags.compoundFlagMap[flag], indent=8) 234 print 235 236 if done: 237 sys.exit(0) 238 239 if options.verbose >= 0: 240 print "M5 Simulator System" 241 print brief_copyright 242 print 243 print "M5 compiled %s" % internal.main.cvar.compileDate; 244 print "M5 started %s" % datetime.now().ctime() 245 print "M5 executing on %s" % socket.gethostname() 246 print "command line:", 247 for argv in sys.argv: 248 print argv, 249 print 250 251 # check to make sure we can find the listed script 252 if not arguments or not os.path.isfile(arguments[0]): 253 if arguments and not os.path.isfile(arguments[0]): 254 print "Script %s not found" % arguments[0] 255 256 usage(2) 257 258 # tell C++ about output directory 259 internal.main.setOutputDir(options.outdir) 260 261 # update the system path with elements from the -p option 262 sys.path[0:0] = options.path 263 264 import objects 265 266 # set stats options 267 internal.stats.initText(options.stats_file) 268 269 # set debugging options 270 for when in options.debug_break: 271 internal.debug.schedBreakCycle(int(when)) 272 273 on_flags = [] 274 off_flags = [] 275 for flag in options.trace_flags: 276 off = False 277 if flag.startswith('-'): 278 flag = flag[1:] 279 off = True 280 if flag not in traceflags.allFlags: 281 print >>sys.stderr, "invalid trace flag '%s'" % flag 282 sys.exit(1) 283 284 if off: 285 off_flags.append(flag) 286 else: 287 on_flags.append(flag) 288 289 for flag in on_flags: 290 internal.trace.set(flag) 291 292 for flag in off_flags: 293 internal.trace.clear(flag) 294 295 if options.trace_start: 296 def enable_trace(): 297 internal.trace.cvar.enabled = True 298 internal.event.create(enable_trace, int(options.trace_start)) 299 else: 300 internal.trace.cvar.enabled = True 301 302 internal.trace.output(options.trace_file) 303 304 for ignore in options.trace_ignore: 305 internal.trace.ignore(ignore) 306 307 sys.argv = arguments 308 sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path 309 310 scope = { '__file__' : sys.argv[0], 311 '__name__' : '__m5_main__' } 312 313 # we want readline if we're doing anything interactive 314 if options.interactive or options.pdb: 315 exec "import readline" in scope 316 317 # if pdb was requested, execfile the thing under pdb, otherwise, 318 # just do the execfile normally 319 if options.pdb: 320 from pdb import Pdb 321 debugger = Pdb() 322 debugger.run('execfile("%s")' % sys.argv[0], scope) 323 else: 324 execfile(sys.argv[0], scope) 325 326 # once the script is done 327 if options.interactive: 328 interact = code.InteractiveConsole(scope) 329 interact.interact("M5 Interactive Console") 330 331if __name__ == '__main__': 332 from pprint import pprint 333 334 parse_args() 335 336 print 'opts:' 337 pprint(options, indent=4) 338 print 339 340 print 'args:' 341 pprint(arguments, indent=4) 342