1 # Copyright (c) 2005 The Regents of The University of Michigan
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.
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.
27 # Authors: Nathan Binkert
30 import sys
, os
, time
, atexit
, optparse
32 # import the SWIG-wrapped main C++ functions
34 # import a few SWIG-wrapped items (those that are likely to be used
35 # directly by user scripts) completely into this module for
37 from cc_main
import simulate
, SimLoopExitEvent
39 # import the m5 compile options
42 # define this here so we can use it right away if necessary
44 print >>sys
.stderr
, 'panic:', string
47 # Prepend given directory to system module search path. We may not
48 # need this anymore if we can structure our config library more like a
51 # if it's a relative path and we know what directory the current
52 # python script is in, make the path relative to that directory.
53 if not os
.path
.isabs(path
) and sys
.path
[0]:
54 path
= os
.path
.join(sys
.path
[0], path
)
55 path
= os
.path
.realpath(path
)
56 # sys.path[0] should always refer to the current script's directory,
57 # so place the new dir right after that.
58 sys
.path
.insert(1, path
)
61 # The m5 module's pointer to the parsed options object
65 # User should call this function after calling parse_args() to pass
66 # parsed standard option values back into the m5 module for
68 def setStandardOptions(_options
):
69 # Set module global var
72 # tell C++ about output directory
73 cc_main
.setOutputDir(options
.outdir
)
75 # Callback to set trace flags. Not necessarily the best way to do
76 # things in the long run (particularly if we change how these global
77 # options are handled).
78 def setTraceFlags(option
, opt_str
, value
, parser
):
79 objects
.Trace
.flags
= value
81 def setTraceStart(option
, opt_str
, value
, parser
):
82 objects
.Trace
.start
= value
84 def setTraceFile(option
, opt_str
, value
, parser
):
85 objects
.Trace
.file = value
87 def usePCSymbol(option
, opt_str
, value
, parser
):
88 objects
.ExecutionTrace
.pc_symbol
= value
90 def printCycle(option
, opt_str
, value
, parser
):
91 objects
.ExecutionTrace
.print_cycle
= value
93 def printOp(option
, opt_str
, value
, parser
):
94 objects
.ExecutionTrace
.print_opclass
= value
96 def printThread(option
, opt_str
, value
, parser
):
97 objects
.ExecutionTrace
.print_thread
= value
99 def printEA(option
, opt_str
, value
, parser
):
100 objects
.ExecutionTrace
.print_effaddr
= value
102 def printData(option
, opt_str
, value
, parser
):
103 objects
.ExecutionTrace
.print_data
= value
105 def printFetchseq(option
, opt_str
, value
, parser
):
106 objects
.ExecutionTrace
.print_fetchseq
= value
108 def printCpseq(option
, opt_str
, value
, parser
):
109 objects
.ExecutionTrace
.print_cpseq
= value
111 def dumpOnExit(option
, opt_str
, value
, parser
):
112 objects
.Trace
.dump_on_exit
= value
114 def debugBreak(option
, opt_str
, value
, parser
):
115 objects
.Debug
.break_cycles
= value
117 def statsTextFile(option
, opt_str
, value
, parser
):
118 objects
.Statistics
.text_file
= value
120 # Extra list to help for options that are true or false
121 TrueOrFalse
= ['True', 'False']
122 TorF
= "True | False"
124 # Standard optparse options. Need to be explicitly included by the
125 # user script when it calls optparse.OptionParser().
127 optparse
.make_option("--outdir", type="string", default
="."),
128 optparse
.make_option("--traceflags", type="string", action
="callback",
129 callback
=setTraceFlags
),
130 optparse
.make_option("--tracestart", type="int", action
="callback",
131 callback
=setTraceStart
),
132 optparse
.make_option("--tracefile", type="string", action
="callback",
133 callback
=setTraceFile
),
134 optparse
.make_option("--pcsymbol", type="choice", choices
=TrueOrFalse
,
135 default
="True", metavar
=TorF
,
136 action
="callback", callback
=usePCSymbol
,
137 help="Use PC symbols in trace output"),
138 optparse
.make_option("--printcycle", type="choice", choices
=TrueOrFalse
,
139 default
="True", metavar
=TorF
,
140 action
="callback", callback
=printCycle
,
141 help="Print cycle numbers in trace output"),
142 optparse
.make_option("--printopclass", type="choice",
144 default
="True", metavar
=TorF
,
145 action
="callback", callback
=printOp
,
146 help="Print cycle numbers in trace output"),
147 optparse
.make_option("--printthread", type="choice",
149 default
="True", metavar
=TorF
,
150 action
="callback", callback
=printThread
,
151 help="Print thread number in trace output"),
152 optparse
.make_option("--printeffaddr", type="choice",
154 default
="True", metavar
=TorF
,
155 action
="callback", callback
=printEA
,
156 help="Print effective address in trace output"),
157 optparse
.make_option("--printdata", type="choice",
159 default
="True", metavar
=TorF
,
160 action
="callback", callback
=printData
,
161 help="Print result data in trace output"),
162 optparse
.make_option("--printfetchseq", type="choice",
164 default
="True", metavar
=TorF
,
165 action
="callback", callback
=printFetchseq
,
166 help="Print fetch sequence numbers in trace output"),
167 optparse
.make_option("--printcpseq", type="choice",
169 default
="True", metavar
=TorF
,
170 action
="callback", callback
=printCpseq
,
171 help="Print correct path sequence numbers in trace output"),
172 optparse
.make_option("--dumponexit", type="choice",
174 default
="True", metavar
=TorF
,
175 action
="callback", callback
=dumpOnExit
,
176 help="Dump trace buffer on exit"),
177 optparse
.make_option("--debugbreak", type="int", metavar
="CYCLE",
178 action
="callback", callback
=debugBreak
,
179 help="Cycle to create a breakpoint"),
180 optparse
.make_option("--statsfile", type="string", action
="callback",
181 callback
=statsTextFile
, metavar
="FILE",
182 help="Sets the output file for the statistics")
185 # make a SmartDict out of the build options for our local use
187 build_env
= smartdict
.SmartDict()
188 build_env
.update(defines
.m5_build_env
)
190 # make a SmartDict out of the OS environment too
191 env
= smartdict
.SmartDict()
192 env
.update(os
.environ
)
195 # Function to provide to C++ so it can look up instances based on paths
196 def resolveSimObject(name
):
197 obj
= config
.instanceDict
[name
]
198 return obj
.getCCObject()
200 # The final hook to generate .ini files. Called from the user script
201 # once the config is built.
202 def instantiate(root
):
203 config
.ticks_per_sec
= float(root
.clock
.frequency
)
204 # ugly temporary hack to get output to config.ini
205 sys
.stdout
= file(os
.path
.join(options
.outdir
, 'config.ini'), 'w')
207 sys
.stdout
.close() # close config.ini
208 sys
.stdout
= sys
.__stdout
__ # restore to original
209 cc_main
.loadIniFile(resolveSimObject
) # load config.ini into C++
210 root
.createCCObject()
213 noDot
= True # temporary until we fix dot
216 instance
.outputDot(dot
)
217 dot
.orientation
= "portrait"
219 dot
.ranksep
="equally"
221 dot
.write("config.dot")
222 dot
.write_ps("config.ps")
224 # Export curTick to user script.
226 return cc_main
.cvar
.curTick
228 # register our C++ exit callback function with Python
229 atexit
.register(cc_main
.doExitCleanup
)
231 # This import allows user scripts to reference 'm5.objects.Foo' after
232 # just doing an 'import m5' (without an 'import m5.objects'). May not
233 # matter since most scripts will probably 'from m5.objects import *'.