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
34 # import the SWIG-wrapped main C++ functions
36 # import a few SWIG-wrapped items (those that are likely to be used
37 # directly by user scripts) completely into this module for
41 # import the m5 compile options
44 # define a MaxTick parameter
47 # define this here so we can use it right away if necessary
49 print >>sys
.stderr
, 'panic:', string
52 # force scalars to one-element lists for uniformity
53 def makeList(objOrList
):
54 if isinstance(objOrList
, list):
58 # Prepend given directory to system module search path. We may not
59 # need this anymore if we can structure our config library more like a
62 # if it's a relative path and we know what directory the current
63 # python script is in, make the path relative to that directory.
64 if not os
.path
.isabs(path
) and sys
.path
[0]:
65 path
= os
.path
.join(sys
.path
[0], path
)
66 path
= os
.path
.realpath(path
)
67 # sys.path[0] should always refer to the current script's directory,
68 # so place the new dir right after that.
69 sys
.path
.insert(1, path
)
71 # make a SmartDict out of the build options for our local use
73 build_env
= smartdict
.SmartDict()
74 build_env
.update(defines
.m5_build_env
)
76 # make a SmartDict out of the OS environment too
77 env
= smartdict
.SmartDict()
78 env
.update(os
.environ
)
80 # The final hook to generate .ini files. Called from the user script
81 # once the config is built.
82 def instantiate(root
):
83 # we need to fix the global frequency
84 ticks
.fixGlobalFrequency()
87 # ugly temporary hack to get output to config.ini
88 sys
.stdout
= file(os
.path
.join(options
.outdir
, 'config.ini'), 'w')
90 sys
.stdout
.close() # close config.ini
91 sys
.stdout
= sys
.__stdout
__ # restore to original
93 # load config.ini into C++
94 internal
.core
.loadIniFile(resolveSimObject
)
96 # Initialize the global statistics
97 internal
.stats
.initSimStats()
99 # Create the C++ sim objects and connect ports
100 root
.createCCObject()
103 # Do a second pass to finish initializing the sim objects
104 internal
.sim_object
.initAll()
106 # Do a third pass to initialize statistics
107 internal
.sim_object
.regAllStats()
109 # Check to make sure that the stats package is properly initialized
110 internal
.stats
.check()
112 # Reset to put the stats in a consistent state.
113 internal
.stats
.reset()
117 instance
.outputDot(dot
)
118 dot
.orientation
= "portrait"
120 dot
.ranksep
="equally"
122 dot
.write("config.dot")
123 dot
.write_ps("config.ps")
127 def simulate(*args
, **kwargs
):
128 global need_resume
, need_startup
131 internal
.core
.SimStartup()
134 for root
in need_resume
:
138 return internal
.event
.simulate(*args
, **kwargs
)
140 # Export curTick to user script.
142 return internal
.core
.cvar
.curTick
144 # Python exit handlers happen in reverse order. We want to dump stats last.
145 atexit
.register(internal
.stats
.dump
)
147 # register our C++ exit callback function with Python
148 atexit
.register(internal
.core
.doExitCleanup
)
150 # This loops until all objects have been fully drained.
152 all_drained
= drain(root
)
153 while (not all_drained
):
154 all_drained
= drain(root
)
156 # Tries to drain all objects. Draining might not be completed unless
157 # all objects return that they are drained on the first call. This is
158 # because as objects drain they may cause other objects to no longer
162 drain_event
= internal
.event
.createCountedDrain()
163 unready_objects
= root
.startDrain(drain_event
, True)
164 # If we've got some objects that can't drain immediately, then simulate
165 if unready_objects
> 0:
166 drain_event
.setCount(unready_objects
)
170 internal
.event
.cleanupCountedDrain(drain_event
)
176 def checkpoint(root
, dir):
177 if not isinstance(root
, objects
.Root
):
178 raise TypeError, "Checkpoint must be called on a root object."
180 print "Writing checkpoint"
181 internal
.sim_object
.serializeAll(dir)
184 def restoreCheckpoint(root
, dir):
185 print "Restoring from checkpoint"
186 internal
.sim_object
.unserializeAll(dir)
187 need_resume
.append(root
)
189 def changeToAtomic(system
):
190 if not isinstance(system
, objects
.Root
) and not isinstance(system
, objects
.System
):
191 raise TypeError, "Object is not a root or system object. Checkpoint must be "
192 "called on a root object."
194 print "Changing memory mode to atomic"
195 system
.changeTiming(internal
.sim_object
.SimObject
.Atomic
)
197 def changeToTiming(system
):
198 if not isinstance(system
, objects
.Root
) and not isinstance(system
, objects
.System
):
199 raise TypeError, "Object is not a root or system object. Checkpoint must be "
200 "called on a root object."
202 print "Changing memory mode to timing"
203 system
.changeTiming(internal
.sim_object
.SimObject
.Timing
)
205 def switchCpus(cpuList
):
206 print "switching cpus"
207 if not isinstance(cpuList
, list):
208 raise RuntimeError, "Must pass a list to this function"
210 if not isinstance(i
, tuple):
211 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
213 [old_cpus
, new_cpus
] = zip(*cpuList
)
216 if not isinstance(cpu
, objects
.BaseCPU
):
217 raise TypeError, "%s is not of type BaseCPU" % cpu
219 if not isinstance(cpu
, objects
.BaseCPU
):
220 raise TypeError, "%s is not of type BaseCPU" % cpu
222 # Drain all of the individual CPUs
223 drain_event
= internal
.event
.createCountedDrain()
225 for old_cpu
in old_cpus
:
226 unready_cpus
+= old_cpu
.startDrain(drain_event
, False)
227 # If we've got some objects that can't drain immediately, then simulate
229 drain_event
.setCount(unready_cpus
)
231 internal
.event
.cleanupCountedDrain(drain_event
)
232 # Now all of the CPUs are ready to be switched out
233 for old_cpu
in old_cpus
:
234 old_cpu
._ccObject
.switchOut()
236 for new_cpu
in new_cpus
:
237 new_cpu
.takeOverFrom(old_cpus
[index
])
238 new_cpu
._ccObject
.resume()
242 print 'Dumping stats'
243 internal
.stats
.dump()
246 print 'Resetting stats'
247 internal
.stats
.reset()
249 # Since we have so many mutual imports in this package, we should:
250 # 1. Put all intra-package imports at the *bottom* of the file, unless
251 # they're absolutely needed before that (for top-level statements
252 # or class attributes). Imports of "trivial" packages that don't
253 # import other packages (e.g., 'smartdict') can be at the top.
254 # 2. Never use 'from foo import *' on an intra-package import since
255 # you can get the wrong result if foo is only partially imported
256 # at the point you do that (i.e., because foo is in the middle of
258 from main
import options
261 from SimObject
import resolveSimObject