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 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 # Callback to set trace flags. Not necessarily the best way to do
62 # things in the long run (particularly if we change how these global
63 # options are handled).
64 def setTraceFlags(option
, opt_str
, value
, parser
):
65 objects
.Trace
.flags
= value
67 def setTraceStart(option
, opt_str
, value
, parser
):
68 objects
.Trace
.start
= value
70 def clearPCSymbol(option
, opt_str
, value
, parser
):
71 objects
.ExecutionTrace
.pc_symbol
= False
73 def clearPrintCycle(option
, opt_str
, value
, parser
):
74 objects
.ExecutionTrace
.print_cycle
= False
76 def statsTextFile(option
, opt_str
, value
, parser
):
77 objects
.Statistics
.text_file
= value
79 # Standard optparse options. Need to be explicitly included by the
80 # user script when it calls optparse.OptionParser().
82 optparse
.make_option("--traceflags", type="string", action
="callback",
83 callback
=setTraceFlags
),
84 optparse
.make_option("--tracestart", type="int", action
="callback",
85 callback
=setTraceStart
),
86 optparse
.make_option("--nopcsymbol", action
="callback",
87 callback
=clearPCSymbol
,
88 help="Turn off printing PC symbols in trace output"),
89 optparse
.make_option("--noprintcycle", action
="callback",
90 callback
=clearPrintCycle
,
91 help="Turn off printing cycles in trace output"),
92 optparse
.make_option("--statsfile", type="string", action
="callback",
93 callback
=statsTextFile
, metavar
="FILE",
94 help="Sets the output file for the statistics")
97 # make a SmartDict out of the build options for our local use
99 build_env
= smartdict
.SmartDict()
100 build_env
.update(defines
.m5_build_env
)
102 # make a SmartDict out of the OS environment too
103 env
= smartdict
.SmartDict()
104 env
.update(os
.environ
)
107 # Function to provide to C++ so it can look up instances based on paths
108 def resolveSimObject(name
):
109 obj
= config
.instanceDict
[name
]
110 return obj
.getCCObject()
112 # The final hook to generate .ini files. Called from the user script
113 # once the config is built.
114 def instantiate(root
):
115 config
.ticks_per_sec
= float(root
.clock
.frequency
)
116 # ugly temporary hack to get output to config.ini
117 sys
.stdout
= file('config.ini', 'w')
119 sys
.stdout
.close() # close config.ini
120 sys
.stdout
= sys
.__stdout
__ # restore to original
121 main
.loadIniFile(resolveSimObject
) # load config.ini into C++
122 root
.createCCObject()
125 noDot
= True # temporary until we fix dot
128 instance
.outputDot(dot
)
129 dot
.orientation
= "portrait"
131 dot
.ranksep
="equally"
133 dot
.write("config.dot")
134 dot
.write_ps("config.ps")
136 # Export curTick to user script.
138 return main
.cvar
.curTick
140 # register our C++ exit callback function with Python
141 atexit
.register(main
.doExitCleanup
)
143 # This import allows user scripts to reference 'm5.objects.Foo' after
144 # just doing an 'import m5' (without an 'import m5.objects'). May not
145 # matter since most scripts will probably 'from m5.objects import *'.