main.py (2889:9e367e03d656) main.py (2890:e9a860453062)
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())
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()
53
54# current option group
55group = None
56
57def set_group(*args, **kwargs):
58 '''set the current option group'''
59 global group
60 if not args and not kwargs:
61 group = None
62 else:
63 group = parser.add_option_group(*args, **kwargs)
64
65class splitter(object):
66 def __init__(self, split):
67 self.split = split
68 def __call__(self, option, opt_str, value, parser):
69 getattr(parser.values, option.dest).extend(value.split(self.split))
70
71def add_option(*args, **kwargs):
72 '''add an option to the current option group, or global none set'''
73
74 # if action=split, but allows the option arguments
75 # themselves to be lists separated by the split variable'''
76
77 if kwargs.get('action', None) == 'append' and 'split' in kwargs:
78 split = kwargs.pop('split')
79 kwargs['default'] = []
80 kwargs['type'] = 'string'
81 kwargs['action'] = 'callback'
82 kwargs['callback'] = splitter(split)
83
84 if group:
85 return group.add_option(*args, **kwargs)
86
87 return parser.add_option(*args, **kwargs)
88
89def bool_option(name, default, help):
90 '''add a boolean option called --name and --no-name.
91 Display help depending on which is the default'''
92
93 tname = '--%s' % name
94 fname = '--no-%s' % name
95 dest = name.replace('-', '_')
96 if default:
97 thelp = optparse.SUPPRESS_HELP
98 fhelp = help
99 else:
100 thelp = help
101 fhelp = optparse.SUPPRESS_HELP
102
103 add_option(tname, action="store_true", default=default, help=thelp)
104 add_option(fname, action="store_false", dest=dest, help=fhelp)
105
106# Help options
107add_option('-A', "--authors", action="store_true", default=False,
108 help="Show author information")
109add_option('-C', "--copyright", action="store_true", default=False,
110 help="Show full copyright information")
111add_option('-R', "--readme", action="store_true", default=False,
112 help="Show the readme")
113add_option('-N', "--release-notes", action="store_true", default=False,
114 help="Show the release notes")
115
116# Options for configuring the base simulator
117add_option('-d', "--outdir", metavar="DIR", default=".",
118 help="Set the output directory to DIR [Default: %default]")
119add_option('-i', "--interactive", action="store_true", default=False,
120 help="Invoke the interactive interpreter after running the script")
121add_option('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
122 help="Prepend PATH to the system path when invoking the script")
123add_option('-q', "--quiet", action="count", default=0,
124 help="Reduce verbosity")
125add_option('-v', "--verbose", action="count", default=0,
126 help="Increase verbosity")
127
128# Statistics options
129set_group("Statistics Options")
130add_option("--stats-file", metavar="FILE", default="m5stats.txt",
131 help="Sets the output file for statistics [Default: %default]")
132
133# Debugging options
134set_group("Debugging Options")
135add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
136 help="Cycle to create a breakpoint")
137
138# Tracing options
139set_group("Trace Options")
140add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
141 help="Sets the flags for tracing")
142add_option("--trace-start", metavar="TIME", default='0s',
143 help="Start tracing at TIME (must have units)")
144add_option("--trace-file", metavar="FILE", default="cout",
145 help="Sets the output file for tracing [Default: %default]")
146add_option("--trace-circlebuf", metavar="SIZE", type="int", default=0,
147 help="If SIZE is non-zero, turn on the circular buffer with SIZE lines")
148add_option("--no-trace-circlebuf", action="store_const", const=0,
149 dest='trace_circlebuf', help=optparse.SUPPRESS_HELP)
150bool_option("trace-dumponexit", default=False,
151 help="Dump trace buffer on exit")
152add_option("--trace-ignore", metavar="EXPR", action='append', split=':',
153 help="Ignore EXPR sim objects")
154
155# Execution Trace options
156set_group("Execution Trace Options")
157bool_option("speculative", default=True,
158 help="Don't capture speculative instructions")
159bool_option("print-cycle", default=True,
160 help="Don't print cycle numbers in trace output")
161bool_option("print-symbol", default=True,
162 help="Disable PC symbols in trace output")
163bool_option("print-opclass", default=True,
164 help="Don't print op class type in trace output")
165bool_option("print-thread", default=True,
166 help="Don't print thread number in trace output")
167bool_option("print-effaddr", default=True,
168 help="Don't print effective address in trace output")
169bool_option("print-data", default=True,
170 help="Don't print result data in trace output")
171bool_option("print-iregs", default=False,
172 help="Print fetch sequence numbers in trace output")
173bool_option("print-fetch-seq", default=False,
174 help="Print fetch sequence numbers in trace output")
175bool_option("print-cpseq", default=False,
176 help="Print correct path sequence numbers in trace output")
177
178options = attrdict()
179arguments = []
180
181def usage(exitcode=None):
182 print parser.help
183 if exitcode is not None:
184 sys.exit(exitcode)
185
186def parse_args():
187 _opts,args = parser.parse_args()
188 opts = attrdict(_opts.__dict__)
189
190 # setting verbose and quiet at the same time doesn't make sense
191 if opts.verbose > 0 and opts.quiet > 0:
192 usage(2)
193
194 # store the verbosity in a single variable. 0 is default,
195 # negative numbers represent quiet and positive values indicate verbose
196 opts.verbose -= opts.quiet
197
198 del opts.quiet
199
200 options.update(opts)
201 arguments.extend(args)
202 return opts,args
203
204def main():
205 import cc_main
206
207 parse_args()
208
209 done = False
210 if options.copyright:
211 done = True
212 print info.LICENSE
213 print
214
215 if options.authors:
216 done = True
217 print 'Author information:'
218 print
219 print info.AUTHORS
220 print
221
222 if options.readme:
223 done = True
224 print 'Readme:'
225 print
226 print info.README
227 print
228
229 if options.release_notes:
230 done = True
231 print 'Release Notes:'
232 print
233 print info.RELEASE_NOTES
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" % cc_main.cvar.compileDate;
244 print "M5 started %s" % datetime.now().ctime()
245 print "M5 executing on %s" % socket.gethostname()
246
247 # check to make sure we can find the listed script
248 if not arguments or not os.path.isfile(arguments[0]):
249 usage(2)
250
251 # tell C++ about output directory
252 cc_main.setOutputDir(options.outdir)
253
254 # update the system path with elements from the -p option
255 sys.path[0:0] = options.path
256
257 import objects
258
259 # set stats options
260 objects.Statistics.text_file = options.stats_file
261
262 # set debugging options
263 objects.Debug.break_cycles = options.debug_break
264
265 # set tracing options
266 objects.Trace.flags = options.trace_flags
267 objects.Trace.start = options.trace_start
268 objects.Trace.file = options.trace_file
269 objects.Trace.bufsize = options.trace_circlebuf
270 objects.Trace.dump_on_exit = options.trace_dumponexit
271 objects.Trace.ignore = options.trace_ignore
272
273 # set execution trace options
274 objects.ExecutionTrace.speculative = options.speculative
275 objects.ExecutionTrace.print_cycle = options.print_cycle
276 objects.ExecutionTrace.pc_symbol = options.print_symbol
277 objects.ExecutionTrace.print_opclass = options.print_opclass
278 objects.ExecutionTrace.print_thread = options.print_thread
279 objects.ExecutionTrace.print_effaddr = options.print_effaddr
280 objects.ExecutionTrace.print_data = options.print_data
281 objects.ExecutionTrace.print_iregs = options.print_iregs
282 objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq
283 objects.ExecutionTrace.print_cpseq = options.print_cpseq
284
285 scope = { '__file__' : sys.argv[0] }
286 sys.argv = arguments
287 sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
288 exec("import readline", scope)
289 execfile(sys.argv[0], scope)
290
291 # once the script is done
292 if options.interactive:
293 interact = code.InteractiveConsole(scope)
294 interact.interact("M5 Interactive Console")
295
296if __name__ == '__main__':
297 from pprint import pprint
298
299 parse_args()
300
301 print 'opts:'
302 pprint(options, indent=4)
303 print
304
305 print 'args:'
306 pprint(arguments, indent=4)
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('-p', "--path", metavar="PATH[:PATH]", action='append', split=':',
123 help="Prepend PATH to the system path when invoking the script")
124add_option('-q', "--quiet", action="count", default=0,
125 help="Reduce verbosity")
126add_option('-v', "--verbose", action="count", default=0,
127 help="Increase verbosity")
128
129# Statistics options
130set_group("Statistics Options")
131add_option("--stats-file", metavar="FILE", default="m5stats.txt",
132 help="Sets the output file for statistics [Default: %default]")
133
134# Debugging options
135set_group("Debugging Options")
136add_option("--debug-break", metavar="TIME[,TIME]", action='append', split=',',
137 help="Cycle to create a breakpoint")
138
139# Tracing options
140set_group("Trace Options")
141add_option("--trace-flags", metavar="FLAG[,FLAG]", action='append', split=',',
142 help="Sets the flags for tracing")
143add_option("--trace-start", metavar="TIME", default='0s',
144 help="Start tracing at TIME (must have units)")
145add_option("--trace-file", metavar="FILE", default="cout",
146 help="Sets the output file for tracing [Default: %default]")
147add_option("--trace-circlebuf", metavar="SIZE", type="int", default=0,
148 help="If SIZE is non-zero, turn on the circular buffer with SIZE lines")
149add_option("--no-trace-circlebuf", action="store_const", const=0,
150 dest='trace_circlebuf', help=optparse.SUPPRESS_HELP)
151bool_option("trace-dumponexit", default=False,
152 help="Dump trace buffer on exit")
153add_option("--trace-ignore", metavar="EXPR", action='append', split=':',
154 help="Ignore EXPR sim objects")
155
156# Execution Trace options
157set_group("Execution Trace Options")
158bool_option("speculative", default=True,
159 help="Don't capture speculative instructions")
160bool_option("print-cycle", default=True,
161 help="Don't print cycle numbers in trace output")
162bool_option("print-symbol", default=True,
163 help="Disable PC symbols in trace output")
164bool_option("print-opclass", default=True,
165 help="Don't print op class type in trace output")
166bool_option("print-thread", default=True,
167 help="Don't print thread number in trace output")
168bool_option("print-effaddr", default=True,
169 help="Don't print effective address in trace output")
170bool_option("print-data", default=True,
171 help="Don't print result data in trace output")
172bool_option("print-iregs", default=False,
173 help="Print fetch sequence numbers in trace output")
174bool_option("print-fetch-seq", default=False,
175 help="Print fetch sequence numbers in trace output")
176bool_option("print-cpseq", default=False,
177 help="Print correct path sequence numbers in trace output")
178
179options = attrdict()
180arguments = []
181
182def usage(exitcode=None):
183 print parser.help
184 if exitcode is not None:
185 sys.exit(exitcode)
186
187def parse_args():
188 _opts,args = parser.parse_args()
189 opts = attrdict(_opts.__dict__)
190
191 # setting verbose and quiet at the same time doesn't make sense
192 if opts.verbose > 0 and opts.quiet > 0:
193 usage(2)
194
195 # store the verbosity in a single variable. 0 is default,
196 # negative numbers represent quiet and positive values indicate verbose
197 opts.verbose -= opts.quiet
198
199 del opts.quiet
200
201 options.update(opts)
202 arguments.extend(args)
203 return opts,args
204
205def main():
206 import cc_main
207
208 parse_args()
209
210 done = False
211 if options.copyright:
212 done = True
213 print info.LICENSE
214 print
215
216 if options.authors:
217 done = True
218 print 'Author information:'
219 print
220 print info.AUTHORS
221 print
222
223 if options.readme:
224 done = True
225 print 'Readme:'
226 print
227 print info.README
228 print
229
230 if options.release_notes:
231 done = True
232 print 'Release Notes:'
233 print
234 print info.RELEASE_NOTES
235 print
236
237 if done:
238 sys.exit(0)
239
240 if options.verbose >= 0:
241 print "M5 Simulator System"
242 print brief_copyright
243 print
244 print "M5 compiled %s" % cc_main.cvar.compileDate;
245 print "M5 started %s" % datetime.now().ctime()
246 print "M5 executing on %s" % socket.gethostname()
247
248 # check to make sure we can find the listed script
249 if not arguments or not os.path.isfile(arguments[0]):
250 usage(2)
251
252 # tell C++ about output directory
253 cc_main.setOutputDir(options.outdir)
254
255 # update the system path with elements from the -p option
256 sys.path[0:0] = options.path
257
258 import objects
259
260 # set stats options
261 objects.Statistics.text_file = options.stats_file
262
263 # set debugging options
264 objects.Debug.break_cycles = options.debug_break
265
266 # set tracing options
267 objects.Trace.flags = options.trace_flags
268 objects.Trace.start = options.trace_start
269 objects.Trace.file = options.trace_file
270 objects.Trace.bufsize = options.trace_circlebuf
271 objects.Trace.dump_on_exit = options.trace_dumponexit
272 objects.Trace.ignore = options.trace_ignore
273
274 # set execution trace options
275 objects.ExecutionTrace.speculative = options.speculative
276 objects.ExecutionTrace.print_cycle = options.print_cycle
277 objects.ExecutionTrace.pc_symbol = options.print_symbol
278 objects.ExecutionTrace.print_opclass = options.print_opclass
279 objects.ExecutionTrace.print_thread = options.print_thread
280 objects.ExecutionTrace.print_effaddr = options.print_effaddr
281 objects.ExecutionTrace.print_data = options.print_data
282 objects.ExecutionTrace.print_iregs = options.print_iregs
283 objects.ExecutionTrace.print_fetchseq = options.print_fetch_seq
284 objects.ExecutionTrace.print_cpseq = options.print_cpseq
285
286 scope = { '__file__' : sys.argv[0] }
287 sys.argv = arguments
288 sys.path = [ os.path.dirname(sys.argv[0]) ] + sys.path
289 exec("import readline", scope)
290 execfile(sys.argv[0], scope)
291
292 # once the script is done
293 if options.interactive:
294 interact = code.InteractiveConsole(scope)
295 interact.interact("M5 Interactive Console")
296
297if __name__ == '__main__':
298 from pprint import pprint
299
300 parse_args()
301
302 print 'opts:'
303 pprint(options, indent=4)
304 print
305
306 print 'args:'
307 pprint(arguments, indent=4)