main.py revision 5470:ad060d1f1037
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 defines
37from options import OptionParser
38import traceflags
39
40__all__ = [ 'options', 'arguments', 'main' ]
41
42def print_list(items, indent=4):
43    line = ' ' * indent
44    for i,item in enumerate(items):
45        if len(line) + len(item) > 76:
46            print line
47            line = ' ' * indent
48
49        if i < len(items) - 1:
50            line += '%s, ' % item
51        else:
52            line += item
53            print line
54
55usage="%prog [m5 options] script.py [script options]"
56version="%prog 2.0"
57brief_copyright='''
58Copyright (c) 2001-2008
59The Regents of The University of Michigan
60All Rights Reserved
61'''
62
63options = OptionParser(usage=usage, version=version,
64                       description=brief_copyright)
65add_option = options.add_option
66set_group = options.set_group
67usage = options.usage
68
69# Help options
70add_option('-A', "--authors", action="store_true", default=False,
71    help="Show author information")
72add_option('-B', "--build-info", action="store_true", default=False,
73    help="Show build information")
74add_option('-C', "--copyright", action="store_true", default=False,
75    help="Show full copyright information")
76add_option('-R', "--readme", action="store_true", default=False,
77    help="Show the readme")
78add_option('-N', "--release-notes", action="store_true", default=False,
79    help="Show the release notes")
80
81# Options for configuring the base simulator
82add_option('-d', "--outdir", metavar="DIR", default=".",
83    help="Set the output directory to DIR [Default: %default]")
84add_option('-i', "--interactive", action="store_true", default=False,
85    help="Invoke the interactive interpreter after running the script")
86add_option("--pdb", action="store_true", default=False,
87    help="Invoke the python debugger before running the script")
88add_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
89    help="Prepend PATH to the system path when invoking the script")
90add_option('-q', "--quiet", action="count", default=0,
91    help="Reduce verbosity")
92add_option('-v', "--verbose", action="count", default=0,
93    help="Increase verbosity")
94
95# Statistics options
96set_group("Statistics Options")
97add_option("--stats-file", metavar="FILE", default="m5stats.txt",
98    help="Sets the output file for statistics [Default: %default]")
99
100# Debugging options
101set_group("Debugging Options")
102add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
103    help="Cycle to create a breakpoint")
104
105# Tracing options
106set_group("Trace Options")
107add_option("--trace-help", action='store_true',
108    help="Print help on trace flags")
109add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
110    help="Sets the flags for tracing (-FLAG disables a flag)")
111add_option("--trace-start", metavar="TIME", type='int',
112    help="Start tracing at TIME (must be in ticks)")
113add_option("--trace-file", metavar="FILE", default="cout",
114    help="Sets the output file for tracing [Default: %default]")
115add_option("--trace-ignore", metavar="EXPR", action='append', split=':',
116    help="Ignore EXPR sim objects")
117
118def main():
119    import defines
120    import event
121    import info
122    import internal
123
124    arguments = options.parse_args()
125
126    done = False
127
128    if options.build_info:
129        done = True
130        print 'Build information:'
131        print
132        print 'compiled %s' % internal.core.cvar.compileDate;
133        print 'started %s' % datetime.datetime.now().ctime()
134        print 'executing on %s' % socket.gethostname()
135        print 'build options:'
136        keys = defines.m5_build_env.keys()
137        keys.sort()
138        for key in keys:
139            val = defines.m5_build_env[key]
140            print '    %s = %s' % (key, val)
141        print
142
143    if options.copyright:
144        done = True
145        print info.LICENSE
146        print
147
148    if options.authors:
149        done = True
150        print 'Author information:'
151        print
152        print info.AUTHORS
153        print
154
155    if options.readme:
156        done = True
157        print 'Readme:'
158        print
159        print info.README
160        print
161
162    if options.release_notes:
163        done = True
164        print 'Release Notes:'
165        print
166        print info.RELEASE_NOTES
167        print
168
169    if options.trace_help:
170        done = True
171        print "Base Flags:"
172        print_list(traceflags.baseFlags, indent=4)
173        print
174        print "Compound Flags:"
175        for flag in traceflags.compoundFlags:
176            if flag == 'All':
177                continue
178            print "    %s:" % flag
179            print_list(traceflags.compoundFlagMap[flag], indent=8)
180            print
181
182    if done:
183        sys.exit(0)
184
185    # setting verbose and quiet at the same time doesn't make sense
186    if options.verbose > 0 and options.quiet > 0:
187        options.usage(2)
188
189    verbose = options.verbose - options.quiet
190    if options.verbose >= 0:
191        print "M5 Simulator System"
192        print brief_copyright
193        print
194        print "M5 compiled %s" % internal.core.cvar.compileDate;
195        print "M5 started %s" % datetime.datetime.now().ctime()
196        print "M5 executing on %s" % socket.gethostname()
197
198        print "M5 revision %s" % internal.core.cvar.hgRev
199        print "M5 commit date %s" % internal.core.cvar.hgDate
200
201        print "command line:",
202        for argv in sys.argv:
203            print argv,
204        print
205
206    # check to make sure we can find the listed script
207    if not arguments or not os.path.isfile(arguments[0]):
208        if arguments and not os.path.isfile(arguments[0]):
209            print "Script %s not found" % arguments[0]
210
211        options.usage(2)
212
213    # tell C++ about output directory
214    internal.core.setOutputDir(options.outdir)
215
216    # update the system path with elements from the -p option
217    sys.path[0:0] = options.path
218
219    import objects
220
221    # set stats options
222    internal.stats.initText(options.stats_file)
223
224    # set debugging options
225    for when in options.debug_break:
226        internal.debug.schedBreakCycle(int(when))
227
228    on_flags = []
229    off_flags = []
230    for flag in options.trace_flags:
231        off = False
232        if flag.startswith('-'):
233            flag = flag[1:]
234            off = True
235        if flag not in traceflags.allFlags:
236            print >>sys.stderr, "invalid trace flag '%s'" % flag
237            sys.exit(1)
238
239        if off:
240            off_flags.append(flag)
241        else:
242            on_flags.append(flag)
243
244    for flag in on_flags:
245        internal.trace.set(flag)
246
247    for flag in off_flags:
248        internal.trace.clear(flag)
249
250    if options.trace_start:
251        def enable_trace():
252            internal.trace.cvar.enabled = True
253        event.create(enable_trace, int(options.trace_start))
254    else:
255        internal.trace.cvar.enabled = True
256
257    internal.trace.output(options.trace_file)
258
259    for ignore in options.trace_ignore:
260        internal.trace.ignore(ignore)
261
262    sys.argv = arguments
263    sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
264
265    scope = { '__file__' : sys.argv[0],
266              '__name__' : '__m5_main__' }
267
268    # we want readline if we're doing anything interactive
269    if options.interactive or options.pdb:
270        exec "import readline" in scope
271
272    # if pdb was requested, execfile the thing under pdb, otherwise,
273    # just do the execfile normally
274    if options.pdb:
275        from pdb import Pdb
276        debugger = Pdb()
277        debugger.run('execfile("%s")' % sys.argv[0], scope)
278    else:
279        execfile(sys.argv[0], scope)
280
281    # once the script is done
282    if options.interactive:
283        interact = code.InteractiveConsole(scope)
284        interact.interact("M5 Interactive Console")
285
286if __name__ == '__main__':
287    from pprint import pprint
288
289    parse_args()
290
291    print 'opts:'
292    pprint(options, indent=4)
293    print
294
295    print 'args:'
296    pprint(arguments, indent=4)
297