main.py revision 5473
112855Sgabeblack@google.com# Copyright (c) 2005 The Regents of The University of Michigan
212855Sgabeblack@google.com# All rights reserved.
312855Sgabeblack@google.com#
412855Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
512855Sgabeblack@google.com# modification, are permitted provided that the following conditions are
612855Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
712855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
812855Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
912855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
1012855Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
1112855Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
1212855Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
1312855Sgabeblack@google.com# this software without specific prior written permission.
1412855Sgabeblack@google.com#
1512855Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612855Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712855Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812855Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912855Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012855Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112855Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212855Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312855Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412855Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512855Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612855Sgabeblack@google.com#
2712855Sgabeblack@google.com# Authors: Nathan Binkert
2812855Sgabeblack@google.com
2912855Sgabeblack@google.comimport code
3012855Sgabeblack@google.comimport datetime
3112855Sgabeblack@google.comimport os
3212855Sgabeblack@google.comimport socket
3312855Sgabeblack@google.comimport sys
3412855Sgabeblack@google.com
3512855Sgabeblack@google.comfrom util import attrdict
3612855Sgabeblack@google.comimport config
3712855Sgabeblack@google.comimport defines
3812855Sgabeblack@google.comfrom options import OptionParser
3912855Sgabeblack@google.comimport traceflags
4012855Sgabeblack@google.com
4112855Sgabeblack@google.com__all__ = [ 'options', 'arguments', 'main' ]
4212855Sgabeblack@google.com
4312855Sgabeblack@google.comdef print_list(items, indent=4):
4412855Sgabeblack@google.com    line = ' ' * indent
4512855Sgabeblack@google.com    for i,item in enumerate(items):
4612855Sgabeblack@google.com        if len(line) + len(item) > 76:
4712855Sgabeblack@google.com            print line
4812855Sgabeblack@google.com            line = ' ' * indent
4912855Sgabeblack@google.com
5012855Sgabeblack@google.com        if i < len(items) - 1:
5112855Sgabeblack@google.com            line += '%s, ' % item
5212855Sgabeblack@google.com        else:
5312855Sgabeblack@google.com            line += item
5412855Sgabeblack@google.com            print line
5512855Sgabeblack@google.com
5612855Sgabeblack@google.comusage="%prog [m5 options] script.py [script options]"
5712855Sgabeblack@google.comversion="%prog 2.0"
5812855Sgabeblack@google.combrief_copyright='''
5912855Sgabeblack@google.comCopyright (c) 2001-2008
6012855Sgabeblack@google.comThe Regents of The University of Michigan
6112855Sgabeblack@google.comAll Rights Reserved
6212855Sgabeblack@google.com'''
6312855Sgabeblack@google.com
6412855Sgabeblack@google.comoptions = OptionParser(usage=usage, version=version,
6512855Sgabeblack@google.com                       description=brief_copyright)
6612855Sgabeblack@google.comadd_option = options.add_option
6712855Sgabeblack@google.comset_group = options.set_group
6812855Sgabeblack@google.comusage = options.usage
6912855Sgabeblack@google.com
7012855Sgabeblack@google.com# Help options
7112855Sgabeblack@google.comadd_option('-A', "--authors", action="store_true", default=False,
7212855Sgabeblack@google.com    help="Show author information")
7312855Sgabeblack@google.comadd_option('-B', "--build-info", action="store_true", default=False,
7412855Sgabeblack@google.com    help="Show build information")
7512855Sgabeblack@google.comadd_option('-C', "--copyright", action="store_true", default=False,
76    help="Show full copyright information")
77add_option('-R', "--readme", action="store_true", default=False,
78    help="Show the readme")
79add_option('-N', "--release-notes", action="store_true", default=False,
80    help="Show the release notes")
81
82# Options for configuring the base simulator
83add_option('-d', "--outdir", metavar="DIR", default=".",
84    help="Set the output directory to DIR [Default: %default]")
85add_option('-i', "--interactive", action="store_true", default=False,
86    help="Invoke the interactive interpreter after running the script")
87add_option("--pdb", action="store_true", default=False,
88    help="Invoke the python debugger before running the script")
89add_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
90    help="Prepend PATH to the system path when invoking the script")
91add_option('-q', "--quiet", action="count", default=0,
92    help="Reduce verbosity")
93add_option('-v', "--verbose", action="count", default=0,
94    help="Increase verbosity")
95
96# Statistics options
97set_group("Statistics Options")
98add_option("--stats-file", metavar="FILE", default="m5stats.txt",
99    help="Sets the output file for statistics [Default: %default]")
100
101# Debugging options
102set_group("Debugging Options")
103add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
104    help="Cycle to create a breakpoint")
105
106# Tracing options
107set_group("Trace Options")
108add_option("--trace-help", action='store_true',
109    help="Print help on trace flags")
110add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
111    help="Sets the flags for tracing (-FLAG disables a flag)")
112add_option("--trace-start", metavar="TIME", type='int',
113    help="Start tracing at TIME (must be in ticks)")
114add_option("--trace-file", metavar="FILE", default="cout",
115    help="Sets the output file for tracing [Default: %default]")
116add_option("--trace-ignore", metavar="EXPR", action='append', split=':',
117    help="Ignore EXPR sim objects")
118
119# Help options
120set_group("Help Options")
121add_option("--list-sim-objects", action='store_true', default=False,
122    help="List all built-in SimObjects, their parameters and default values")
123
124def main():
125    import defines
126    import event
127    import info
128    import internal
129
130    # load the options.py config file to allow people to set their own
131    # default options
132    options_file = config.get('options.py')
133    if options_file:
134        scope = { 'options' : options }
135        execfile(options_file, scope)
136
137    arguments = options.parse_args()
138
139    done = False
140
141    if options.build_info:
142        done = True
143        print 'Build information:'
144        print
145        print 'compiled %s' % internal.core.cvar.compileDate;
146        print 'started %s' % datetime.datetime.now().ctime()
147        print 'executing on %s' % socket.gethostname()
148        print 'build options:'
149        keys = defines.m5_build_env.keys()
150        keys.sort()
151        for key in keys:
152            val = defines.m5_build_env[key]
153            print '    %s = %s' % (key, val)
154        print
155
156    if options.copyright:
157        done = True
158        print info.LICENSE
159        print
160
161    if options.authors:
162        done = True
163        print 'Author information:'
164        print
165        print info.AUTHORS
166        print
167
168    if options.readme:
169        done = True
170        print 'Readme:'
171        print
172        print info.README
173        print
174
175    if options.release_notes:
176        done = True
177        print 'Release Notes:'
178        print
179        print info.RELEASE_NOTES
180        print
181
182    if options.trace_help:
183        done = True
184        print "Base Flags:"
185        print_list(traceflags.baseFlags, indent=4)
186        print
187        print "Compound Flags:"
188        for flag in traceflags.compoundFlags:
189            if flag == 'All':
190                continue
191            print "    %s:" % flag
192            print_list(traceflags.compoundFlagMap[flag], indent=8)
193            print
194
195    if options.list_sim_objects:
196        import SimObject
197        done = True
198        print "SimObjects:"
199        objects = SimObject.allClasses.keys()
200        objects.sort()
201        for name in objects:
202            obj = SimObject.allClasses[name]
203            print "    %s" % obj
204            params = obj._params.keys()
205            params.sort()
206            for pname in params:
207                param = obj._params[pname]
208                default = getattr(param, 'default', '')
209                print "        %s" % pname
210                if default:
211                    print "            default: %s" % default
212                print "            desc: %s" % param.desc
213                print
214            print
215
216    if done:
217        sys.exit(0)
218
219    # setting verbose and quiet at the same time doesn't make sense
220    if options.verbose > 0 and options.quiet > 0:
221        options.usage(2)
222
223    verbose = options.verbose - options.quiet
224    if options.verbose >= 0:
225        print "M5 Simulator System"
226        print brief_copyright
227        print
228        print "M5 compiled %s" % internal.core.cvar.compileDate;
229        print "M5 started %s" % datetime.datetime.now().ctime()
230        print "M5 executing on %s" % socket.gethostname()
231
232        print "M5 revision %s" % internal.core.cvar.hgRev
233        print "M5 commit date %s" % internal.core.cvar.hgDate
234
235        print "command line:",
236        for argv in sys.argv:
237            print argv,
238        print
239
240    # check to make sure we can find the listed script
241    if not arguments or not os.path.isfile(arguments[0]):
242        if arguments and not os.path.isfile(arguments[0]):
243            print "Script %s not found" % arguments[0]
244
245        options.usage(2)
246
247    # tell C++ about output directory
248    internal.core.setOutputDir(options.outdir)
249
250    # update the system path with elements from the -p option
251    sys.path[0:0] = options.path
252
253    import objects
254
255    # set stats options
256    internal.stats.initText(options.stats_file)
257
258    # set debugging options
259    for when in options.debug_break:
260        internal.debug.schedBreakCycle(int(when))
261
262    on_flags = []
263    off_flags = []
264    for flag in options.trace_flags:
265        off = False
266        if flag.startswith('-'):
267            flag = flag[1:]
268            off = True
269        if flag not in traceflags.allFlags:
270            print >>sys.stderr, "invalid trace flag '%s'" % flag
271            sys.exit(1)
272
273        if off:
274            off_flags.append(flag)
275        else:
276            on_flags.append(flag)
277
278    for flag in on_flags:
279        internal.trace.set(flag)
280
281    for flag in off_flags:
282        internal.trace.clear(flag)
283
284    if options.trace_start:
285        def enable_trace():
286            internal.trace.cvar.enabled = True
287        event.create(enable_trace, int(options.trace_start))
288    else:
289        internal.trace.cvar.enabled = True
290
291    internal.trace.output(options.trace_file)
292
293    for ignore in options.trace_ignore:
294        internal.trace.ignore(ignore)
295
296    sys.argv = arguments
297    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
298
299    scope = { '__file__' : sys.argv[0],
300              '__name__' : '__m5_main__' }
301
302    # we want readline if we're doing anything interactive
303    if options.interactive or options.pdb:
304        exec "import readline" in scope
305
306    # if pdb was requested, execfile the thing under pdb, otherwise,
307    # just do the execfile normally
308    if options.pdb:
309        from pdb import Pdb
310        debugger = Pdb()
311        debugger.run('execfile("%s")' % sys.argv[0], scope)
312    else:
313        execfile(sys.argv[0], scope)
314
315    # once the script is done
316    if options.interactive:
317        interact = code.InteractiveConsole(scope)
318        interact.interact("M5 Interactive Console")
319
320if __name__ == '__main__':
321    from pprint import pprint
322
323    parse_args()
324
325    print 'opts:'
326    pprint(options, indent=4)
327    print
328
329    print 'args:'
330    pprint(arguments, indent=4)
331