main.py (3887:4a565b35bdee) main.py (4042:dbd98b2264ed)
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, optparse, os, socket, sys
30from datetime import datetime
31from attrdict import attrdict
32
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, optparse, os, socket, sys
30from datetime import datetime
31from attrdict import attrdict
32
33try:
34 import info
35except ImportError:
36 info = None
37
38__all__ = [ 'options', 'arguments', 'main' ]
39
40usage="%prog [m5 options] script.py [script options]"
41version="%prog 2.0"
42brief_copyright='''
43Copyright (c) 2001-2006
44The Regents of The University of Michigan
45All Rights Reserved
46'''
47
48# there's only one option parsing done, so make it global and add some
49# helper functions to make it work well.
50parser = optparse.OptionParser(usage=usage, version=version,
51 description=brief_copyright,
52 formatter=optparse.TitledHelpFormatter())
53parser.disable_interspersed_args()
54
55# current option group
56group = None
57
58def set_group(*args, **kwargs):
59 '''set the current option group'''
60 global group
61 if not args and not kwargs:
62 group = None
63 else:
64 group = parser.add_option_group(*args, **kwargs)
65
66class splitter(object):
67 def __init__(self, split):
68 self.split = split
69 def __call__(self, option, opt_str, value, parser):
70 getattr(parser.values, option.dest).extend(value.split(self.split))
71
72def add_option(*args, **kwargs):
73 '''add an option to the current option group, or global none set'''
74
75 # if action=split, but allows the option arguments
76 # themselves to be lists separated by the split variable'''
77
78 if kwargs.get('action', None) == 'append' and 'split' in kwargs:
79 split = kwargs.pop('split')
80 kwargs['default'] = []
81 kwargs['type'] = 'string'
82 kwargs['action'] = 'callback'
83 kwargs['callback'] = splitter(split)
84
85 if group:
86 return group.add_option(*args, **kwargs)
87
88 return parser.add_option(*args, **kwargs)
89
90def bool_option(name, default, help):
91 '''add a boolean option called --name and --no-name.
92 Display help depending on which is the default'''
93
94 tname = '--%s' % name
95 fname = '--no-%s' % name
96 dest = name.replace('-', '_')
97 if default:
98 thelp = optparse.SUPPRESS_HELP
99 fhelp = help
100 else:
101 thelp = help
102 fhelp = optparse.SUPPRESS_HELP
103
104 add_option(tname, action="store_true", default=default, help=thelp)
105 add_option(fname, action="store_false", dest=dest, help=fhelp)
106
107# Help options
108add_option('-A', "--authors", action="store_true", default=False,
109 help="Show author information")
110add_option('-C', "--copyright", action="store_true", default=False,
111 help="Show full copyright information")
112add_option('-R', "--readme", action="store_true", default=False,
113 help="Show the readme")
114add_option('-N', "--release-notes", action="store_true", default=False,
115 help="Show the release notes")
116
117# Options for configuring the base simulator
118add_option('-d', "--outdir", metavar="DIR", default=".",
119 help="Set the output directory to DIR [Default: %default]")
120add_option('-i', "--interactive", action="store_true", default=False,
121 help="Invoke the interactive interpreter after running the script")
122add_option("--pdb", action="store_true", default=False,
123 help="Invoke the python debugger before running the script")
124add_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
125 help="Prepend PATH to the system path when invoking the script")
126add_option('-q', "--quiet", action="count", default=0,
127 help="Reduce verbosity")
128add_option('-v', "--verbose", action="count", default=0,
129 help="Increase verbosity")
130
131# Statistics options
132set_group("Statistics Options")
133add_option("--stats-file", metavar="FILE", default="m5stats.txt",
134 help="Sets the output file for statistics [Default: %default]")
135
136# Debugging options
137set_group("Debugging Options")
138add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
139 help="Cycle to create a breakpoint")
140
141# Tracing options
142set_group("Trace Options")
143add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
144 help="Sets the flags for tracing")
145add_option("--trace-start", metavar="TIME", default='0s',
146 help="Start tracing at TIME (must have units)")
33__all__ = [ 'options', 'arguments', 'main' ]
34
35usage="%prog [m5 options] script.py [script options]"
36version="%prog 2.0"
37brief_copyright='''
38Copyright (c) 2001-2006
39The Regents of The University of Michigan
40All Rights Reserved
41'''
42
43# there's only one option parsing done, so make it global and add some
44# helper functions to make it work well.
45parser = optparse.OptionParser(usage=usage, version=version,
46 description=brief_copyright,
47 formatter=optparse.TitledHelpFormatter())
48parser.disable_interspersed_args()
49
50# current option group
51group = None
52
53def set_group(*args, **kwargs):
54 '''set the current option group'''
55 global group
56 if not args and not kwargs:
57 group = None
58 else:
59 group = parser.add_option_group(*args, **kwargs)
60
61class splitter(object):
62 def __init__(self, split):
63 self.split = split
64 def __call__(self, option, opt_str, value, parser):
65 getattr(parser.values, option.dest).extend(value.split(self.split))
66
67def add_option(*args, **kwargs):
68 '''add an option to the current option group, or global none set'''
69
70 # if action=split, but allows the option arguments
71 # themselves to be lists separated by the split variable'''
72
73 if kwargs.get('action', None) == 'append' and 'split' in kwargs:
74 split = kwargs.pop('split')
75 kwargs['default'] = []
76 kwargs['type'] = 'string'
77 kwargs['action'] = 'callback'
78 kwargs['callback'] = splitter(split)
79
80 if group:
81 return group.add_option(*args, **kwargs)
82
83 return parser.add_option(*args, **kwargs)
84
85def bool_option(name, default, help):
86 '''add a boolean option called --name and --no-name.
87 Display help depending on which is the default'''
88
89 tname = '--%s' % name
90 fname = '--no-%s' % name
91 dest = name.replace('-', '_')
92 if default:
93 thelp = optparse.SUPPRESS_HELP
94 fhelp = help
95 else:
96 thelp = help
97 fhelp = optparse.SUPPRESS_HELP
98
99 add_option(tname, action="store_true", default=default, help=thelp)
100 add_option(fname, action="store_false", dest=dest, help=fhelp)
101
102# Help options
103add_option('-A', "--authors", action="store_true", default=False,
104 help="Show author information")
105add_option('-C', "--copyright", action="store_true", default=False,
106 help="Show full copyright information")
107add_option('-R', "--readme", action="store_true", default=False,
108 help="Show the readme")
109add_option('-N', "--release-notes", action="store_true", default=False,
110 help="Show the release notes")
111
112# Options for configuring the base simulator
113add_option('-d', "--outdir", metavar="DIR", default=".",
114 help="Set the output directory to DIR [Default: %default]")
115add_option('-i', "--interactive", action="store_true", default=False,
116 help="Invoke the interactive interpreter after running the script")
117add_option("--pdb", action="store_true", default=False,
118 help="Invoke the python debugger before running the script")
119add_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
120 help="Prepend PATH to the system path when invoking the script")
121add_option('-q', "--quiet", action="count", default=0,
122 help="Reduce verbosity")
123add_option('-v', "--verbose", action="count", default=0,
124 help="Increase verbosity")
125
126# Statistics options
127set_group("Statistics Options")
128add_option("--stats-file", metavar="FILE", default="m5stats.txt",
129 help="Sets the output file for statistics [Default: %default]")
130
131# Debugging options
132set_group("Debugging Options")
133add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
134 help="Cycle to create a breakpoint")
135
136# Tracing options
137set_group("Trace Options")
138add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
139 help="Sets the flags for tracing")
140add_option("--trace-start", metavar="TIME", default='0s',
141 help="Start tracing at TIME (must have units)")
147add_option("--trace-cycle", metavar="CYCLE", default='0',
148 help="Start tracing at CYCLE")
149add_option("--trace-file", metavar="FILE", default="cout",
150 help="Sets the output file for tracing [Default: %default]")
151add_option("--trace-circlebuf", metavar="SIZE", type="int", default=0,
152 help="If SIZE is non-zero, turn on the circular buffer with SIZE lines")
153add_option("--no-trace-circlebuf", action="store_const", const=0,
154 dest='trace_circlebuf', help=optparse.SUPPRESS_HELP)
155bool_option("trace-dumponexit", default=False,
156 help="Dump trace buffer on exit")
157add_option("--trace-ignore", metavar="EXPR", action='append', split=':',
158 help="Ignore EXPR sim objects")
159
160# Execution Trace options
161set_group("Execution Trace Options")
162bool_option("speculative", default=True,
163 help="Don't capture speculative instructions")
164bool_option("print-cycle", default=True,
165 help="Don't print cycle numbers in trace output")
166bool_option("print-symbol", default=True,
167 help="Disable PC symbols in trace output")
168bool_option("print-opclass", default=True,
169 help="Don't print op class type in trace output")
170bool_option("print-thread", default=True,
171 help="Don't print thread number in trace output")
172bool_option("print-effaddr", default=True,
173 help="Don't print effective address in trace output")
174bool_option("print-data", default=True,
175 help="Don't print result data in trace output")
176bool_option("print-iregs", default=False,
177 help="Print fetch sequence numbers in trace output")
178bool_option("print-fetch-seq", default=False,
179 help="Print fetch sequence numbers in trace output")
180bool_option("print-cpseq", default=False,
181 help="Print correct path sequence numbers in trace output")
182#bool_option("print-reg-delta", default=False,
183# help="Print which registers changed to what in trace output")
184bool_option("legion-lock", default=False,
185 help="Compare simulator state with Legion simulator every cycle")
186
187options = attrdict()
188arguments = []
189
190def usage(exitcode=None):
191 parser.print_help()
192 if exitcode is not None:
193 sys.exit(exitcode)
194
195def parse_args():
196 _opts,args = parser.parse_args()
197 opts = attrdict(_opts.__dict__)
198
199 # setting verbose and quiet at the same time doesn't make sense
200 if opts.verbose > 0 and opts.quiet > 0:
201 usage(2)
202
203 # store the verbosity in a single variable. 0 is default,
204 # negative numbers represent quiet and positive values indicate verbose
205 opts.verbose -= opts.quiet
206
207 del opts.quiet
208
209 options.update(opts)
210 arguments.extend(args)
211 return opts,args
212
213def main():
142add_option("--trace-file", metavar="FILE", default="cout",
143 help="Sets the output file for tracing [Default: %default]")
144add_option("--trace-circlebuf", metavar="SIZE", type="int", default=0,
145 help="If SIZE is non-zero, turn on the circular buffer with SIZE lines")
146add_option("--no-trace-circlebuf", action="store_const", const=0,
147 dest='trace_circlebuf', help=optparse.SUPPRESS_HELP)
148bool_option("trace-dumponexit", default=False,
149 help="Dump trace buffer on exit")
150add_option("--trace-ignore", metavar="EXPR", action='append', split=':',
151 help="Ignore EXPR sim objects")
152
153# Execution Trace options
154set_group("Execution Trace Options")
155bool_option("speculative", default=True,
156 help="Don't capture speculative instructions")
157bool_option("print-cycle", default=True,
158 help="Don't print cycle numbers in trace output")
159bool_option("print-symbol", default=True,
160 help="Disable PC symbols in trace output")
161bool_option("print-opclass", default=True,
162 help="Don't print op class type in trace output")
163bool_option("print-thread", default=True,
164 help="Don't print thread number in trace output")
165bool_option("print-effaddr", default=True,
166 help="Don't print effective address in trace output")
167bool_option("print-data", default=True,
168 help="Don't print result data in trace output")
169bool_option("print-iregs", default=False,
170 help="Print fetch sequence numbers in trace output")
171bool_option("print-fetch-seq", default=False,
172 help="Print fetch sequence numbers in trace output")
173bool_option("print-cpseq", default=False,
174 help="Print correct path sequence numbers in trace output")
175#bool_option("print-reg-delta", default=False,
176# help="Print which registers changed to what in trace output")
177bool_option("legion-lock", default=False,
178 help="Compare simulator state with Legion simulator every cycle")
179
180options = attrdict()
181arguments = []
182
183def usage(exitcode=None):
184 parser.print_help()
185 if exitcode is not None:
186 sys.exit(exitcode)
187
188def parse_args():
189 _opts,args = parser.parse_args()
190 opts = attrdict(_opts.__dict__)
191
192 # setting verbose and quiet at the same time doesn't make sense
193 if opts.verbose > 0 and opts.quiet > 0:
194 usage(2)
195
196 # store the verbosity in a single variable. 0 is default,
197 # negative numbers represent quiet and positive values indicate verbose
198 opts.verbose -= opts.quiet
199
200 del opts.quiet
201
202 options.update(opts)
203 arguments.extend(args)
204 return opts,args
205
206def main():
207 import defines
208 import info
214 import internal
215
216 parse_args()
217
218 done = False
219 if options.copyright:
220 done = True
221 print info.LICENSE
222 print
223
224 if options.authors:
225 done = True
226 print 'Author information:'
227 print
228 print info.AUTHORS
229 print
230
231 if options.readme:
232 done = True
233 print 'Readme:'
234 print
235 print info.README
236 print
237
238 if options.release_notes:
239 done = True
240 print 'Release Notes:'
241 print
242 print info.RELEASE_NOTES
243 print
244
245 if done:
246 sys.exit(0)
247
248 if options.verbose >= 0:
249 print "M5 Simulator System"
250 print brief_copyright
251 print
252 print "M5 compiled %s" % internal.main.cvar.compileDate;
253 print "M5 started %s" % datetime.now().ctime()
254 print "M5 executing on %s" % socket.gethostname()
255 print "command line:",
256 for argv in sys.argv:
257 print argv,
258 print
259
260 # check to make sure we can find the listed script
261 if not arguments or not os.path.isfile(arguments[0]):
262 if arguments and not os.path.isfile(arguments[0]):
263 print "Script %s not found" % arguments[0]
264 usage(2)
265
266 # tell C++ about output directory
267 internal.main.setOutputDir(options.outdir)
268
269 # update the system path with elements from the -p option
270 sys.path[0:0] = options.path
271
272 import objects
273
274 # set stats options
275 objects.Statistics.text_file = options.stats_file
276
277 # set debugging options
278 for when in options.debug_break:
279 internal.debug.schedBreakCycle(int(when))
280
209 import internal
210
211 parse_args()
212
213 done = False
214 if options.copyright:
215 done = True
216 print info.LICENSE
217 print
218
219 if options.authors:
220 done = True
221 print 'Author information:'
222 print
223 print info.AUTHORS
224 print
225
226 if options.readme:
227 done = True
228 print 'Readme:'
229 print
230 print info.README
231 print
232
233 if options.release_notes:
234 done = True
235 print 'Release Notes:'
236 print
237 print info.RELEASE_NOTES
238 print
239
240 if done:
241 sys.exit(0)
242
243 if options.verbose >= 0:
244 print "M5 Simulator System"
245 print brief_copyright
246 print
247 print "M5 compiled %s" % internal.main.cvar.compileDate;
248 print "M5 started %s" % datetime.now().ctime()
249 print "M5 executing on %s" % socket.gethostname()
250 print "command line:",
251 for argv in sys.argv:
252 print argv,
253 print
254
255 # check to make sure we can find the listed script
256 if not arguments or not os.path.isfile(arguments[0]):
257 if arguments and not os.path.isfile(arguments[0]):
258 print "Script %s not found" % arguments[0]
259 usage(2)
260
261 # tell C++ about output directory
262 internal.main.setOutputDir(options.outdir)
263
264 # update the system path with elements from the -p option
265 sys.path[0:0] = options.path
266
267 import objects
268
269 # set stats options
270 objects.Statistics.text_file = options.stats_file
271
272 # set debugging options
273 for when in options.debug_break:
274 internal.debug.schedBreakCycle(int(when))
275
281 # set tracing options
282 objects.Trace.flags = options.trace_flags
283 objects.Trace.start = options.trace_start
284 objects.Trace.cycle = options.trace_cycle
285 objects.Trace.file = options.trace_file
286 objects.Trace.bufsize = options.trace_circlebuf
287 objects.Trace.dump_on_exit = options.trace_dumponexit
288 objects.Trace.ignore = options.trace_ignore
276 for flag in options.trace_flags:
277 internal.trace.setFlag(flag)
289
278
279 if options.trace_start is not None:
280 internal.trace.enabled = False
281 def enable_trace():
282 internal.event.enabled = True
283 internal.event.create(enable_trace, options.trace_start)
284
285 if options.trace_file is not None:
286 internal.trace.file(options.trace_file)
287
288 if options.trace_bufsize is not None:
289 internal.trace.buffer_size(options.bufsize)
290
291 #if options.trace_dumponexit:
292 # internal.trace.dumpOnExit = True
293
294 for ignore in options.trace_ignore:
295 internal.trace.ignore(ignore)
296
290 # set execution trace options
291 objects.ExecutionTrace.speculative = options.speculative
292 objects.ExecutionTrace.print_cycle = options.print_cycle
293 objects.ExecutionTrace.pc_symbol = options.print_symbol
294 objects.ExecutionTrace.print_opclass = options.print_opclass
295 objects.ExecutionTrace.print_thread = options.print_thread
296 objects.ExecutionTrace.print_effaddr = options.print_effaddr
297 objects.ExecutionTrace.print_data = options.print_data
298 objects.ExecutionTrace.print_iregs = options.print_iregs
299 objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq
300 objects.ExecutionTrace.print_cpseq = options.print_cpseq
301 #objects.ExecutionTrace.print_reg_delta = options.print_reg_delta
302 objects.ExecutionTrace.legion_lockstep = options.legion_lock
303
304 sys.argv = arguments
305 sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
306
307 scope = { '__file__' : sys.argv[0],
308 '__name__' : '__m5_main__' }
309
310 # we want readline if we're doing anything interactive
311 if options.interactive or options.pdb:
297 # set execution trace options
298 objects.ExecutionTrace.speculative = options.speculative
299 objects.ExecutionTrace.print_cycle = options.print_cycle
300 objects.ExecutionTrace.pc_symbol = options.print_symbol
301 objects.ExecutionTrace.print_opclass = options.print_opclass
302 objects.ExecutionTrace.print_thread = options.print_thread
303 objects.ExecutionTrace.print_effaddr = options.print_effaddr
304 objects.ExecutionTrace.print_data = options.print_data
305 objects.ExecutionTrace.print_iregs = options.print_iregs
306 objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq
307 objects.ExecutionTrace.print_cpseq = options.print_cpseq
308 #objects.ExecutionTrace.print_reg_delta = options.print_reg_delta
309 objects.ExecutionTrace.legion_lockstep = options.legion_lock
310
311 sys.argv = arguments
312 sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
313
314 scope = { '__file__' : sys.argv[0],
315 '__name__' : '__m5_main__' }
316
317 # we want readline if we're doing anything interactive
318 if options.interactive or options.pdb:
312 exec("import readline", scope)
319 exec "import readline" in scope
313
314 # if pdb was requested, execfile the thing under pdb, otherwise,
315 # just do the execfile normally
316 if options.pdb:
317 from pdb import Pdb
318 debugger = Pdb()
319 debugger.run('execfile("%s")' % sys.argv[0], scope)
320 else:
321 execfile(sys.argv[0], scope)
322
323 # once the script is done
324 if options.interactive:
325 interact = code.InteractiveConsole(scope)
326 interact.interact("M5 Interactive Console")
327
328if __name__ == '__main__':
329 from pprint import pprint
330
331 parse_args()
332
333 print 'opts:'
334 pprint(options, indent=4)
335 print
336
337 print 'args:'
338 pprint(arguments, indent=4)
320
321 # if pdb was requested, execfile the thing under pdb, otherwise,
322 # just do the execfile normally
323 if options.pdb:
324 from pdb import Pdb
325 debugger = Pdb()
326 debugger.run('execfile("%s")' % sys.argv[0], scope)
327 else:
328 execfile(sys.argv[0], scope)
329
330 # once the script is done
331 if options.interactive:
332 interact = code.InteractiveConsole(scope)
333 interact.interact("M5 Interactive Console")
334
335if __name__ == '__main__':
336 from pprint import pprint
337
338 parse_args()
339
340 print 'opts:'
341 pprint(options, indent=4)
342 print
343
344 print 'args:'
345 pprint(arguments, indent=4)