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 atexit
, os
, sys
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 internal
.main
import simulate
, SimLoopExitEvent
39 # import the m5 compile options
42 # define a MaxTick parameter
45 # define this here so we can use it right away if necessary
47 print >>sys
.stderr
, 'panic:', string
50 # force scalars to one-element lists for uniformity
51 def makeList(objOrList
):
52 if isinstance(objOrList
, list):
56 # Prepend given directory to system module search path. We may not
57 # need this anymore if we can structure our config library more like a
60 # if it's a relative path and we know what directory the current
61 # python script is in, make the path relative to that directory.
62 if not os
.path
.isabs(path
) and sys
.path
[0]:
63 path
= os
.path
.join(sys
.path
[0], path
)
64 path
= os
.path
.realpath(path
)
65 # sys.path[0] should always refer to the current script's directory,
66 # so place the new dir right after that.
67 sys
.path
.insert(1, path
)
69 # make a SmartDict out of the build options for our local use
71 build_env
= smartdict
.SmartDict()
72 build_env
.update(defines
.m5_build_env
)
74 # make a SmartDict out of the OS environment too
75 env
= smartdict
.SmartDict()
76 env
.update(os
.environ
)
78 # The final hook to generate .ini files. Called from the user script
79 # once the config is built.
80 def instantiate(root
):
81 params
.ticks_per_sec
= float(root
.clock
.frequency
)
83 # ugly temporary hack to get output to config.ini
84 sys
.stdout
= file(os
.path
.join(options
.outdir
, 'config.ini'), 'w')
86 sys
.stdout
.close() # close config.ini
87 sys
.stdout
= sys
.__stdout
__ # restore to original
88 internal
.main
.loadIniFile(resolveSimObject
) # load config.ini into C++
91 internal
.main
.finalInit()
92 noDot
= True # temporary until we fix dot
95 instance
.outputDot(dot
)
96 dot
.orientation
= "portrait"
100 dot
.write("config.dot")
101 dot
.write_ps("config.ps")
103 # Export curTick to user script.
105 return internal
.main
.cvar
.curTick
107 # register our C++ exit callback function with Python
108 atexit
.register(internal
.main
.doExitCleanup
)
110 # This loops until all objects have been fully drained.
112 all_drained
= drain(root
)
113 while (not all_drained
):
114 all_drained
= drain(root
)
116 # Tries to drain all objects. Draining might not be completed unless
117 # all objects return that they are drained on the first call. This is
118 # because as objects drain they may cause other objects to no longer
122 drain_event
= internal
.main
.createCountedDrain()
123 unready_objects
= root
.startDrain(drain_event
, True)
124 # If we've got some objects that can't drain immediately, then simulate
125 if unready_objects
> 0:
126 drain_event
.setCount(unready_objects
)
130 internal
.main
.cleanupCountedDrain(drain_event
)
136 def checkpoint(root
, dir):
137 if not isinstance(root
, objects
.Root
):
138 raise TypeError, "Object is not a root object. Checkpoint must be called on a root object."
140 print "Writing checkpoint"
141 internal
.main
.serializeAll(dir)
144 def restoreCheckpoint(root
, dir):
145 print "Restoring from checkpoint"
146 internal
.main
.unserializeAll(dir)
149 def changeToAtomic(system
):
150 if not isinstance(system
, objects
.Root
) and not isinstance(system
, objects
.System
):
151 raise TypeError, "Object is not a root or system object. Checkpoint must be "
152 "called on a root object."
154 print "Changing memory mode to atomic"
155 system
.changeTiming(internal
.main
.SimObject
.Atomic
)
157 def changeToTiming(system
):
158 if not isinstance(system
, objects
.Root
) and not isinstance(system
, objects
.System
):
159 raise TypeError, "Object is not a root or system object. Checkpoint must be "
160 "called on a root object."
162 print "Changing memory mode to timing"
163 system
.changeTiming(internal
.main
.SimObject
.Timing
)
165 def switchCpus(cpuList
):
166 print "switching cpus"
167 if not isinstance(cpuList
, list):
168 raise RuntimeError, "Must pass a list to this function"
170 if not isinstance(i
, tuple):
171 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
173 [old_cpus
, new_cpus
] = zip(*cpuList
)
176 if not isinstance(cpu
, objects
.BaseCPU
):
177 raise TypeError, "%s is not of type BaseCPU" % cpu
179 if not isinstance(cpu
, objects
.BaseCPU
):
180 raise TypeError, "%s is not of type BaseCPU" % cpu
182 # Drain all of the individual CPUs
183 drain_event
= internal
.main
.createCountedDrain()
185 for old_cpu
in old_cpus
:
186 unready_cpus
+= old_cpu
.startDrain(drain_event
, False)
187 # If we've got some objects that can't drain immediately, then simulate
189 drain_event
.setCount(unready_cpus
)
191 internal
.main
.cleanupCountedDrain(drain_event
)
192 # Now all of the CPUs are ready to be switched out
193 for old_cpu
in old_cpus
:
194 old_cpu
._ccObject
.switchOut()
196 for new_cpu
in new_cpus
:
197 new_cpu
.takeOverFrom(old_cpus
[index
])
198 new_cpu
._ccObject
.resume()
201 # Since we have so many mutual imports in this package, we should:
202 # 1. Put all intra-package imports at the *bottom* of the file, unless
203 # they're absolutely needed before that (for top-level statements
204 # or class attributes). Imports of "trivial" packages that don't
205 # import other packages (e.g., 'smartdict') can be at the top.
206 # 2. Never use 'from foo import *' on an intra-package import since
207 # you can get the wrong result if foo is only partially imported
208 # at the point you do that (i.e., because foo is in the middle of
210 from main
import options
213 from SimObject
import resolveSimObject