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 from main
import options
41 # The final hook to generate .ini files. Called from the user script
42 # once the config is built.
43 def instantiate(root
):
44 # we need to fix the global frequency
45 ticks
.fixGlobalFrequency()
48 # ugly temporary hack to get output to config.ini
49 sys
.stdout
= file(os
.path
.join(options
.outdir
, 'config.ini'), 'w')
51 sys
.stdout
.close() # close config.ini
52 sys
.stdout
= sys
.__stdout
__ # restore to original
54 # Initialize the global statistics
55 internal
.stats
.initSimStats()
57 # Create the C++ sim objects and connect ports
61 # Do a second pass to finish initializing the sim objects
62 internal
.sim_object
.initAll()
64 # Do a third pass to initialize statistics
65 internal
.sim_object
.regAllStats()
67 # Check to make sure that the stats package is properly initialized
68 internal
.stats
.check()
70 # Reset to put the stats in a consistent state.
71 internal
.stats
.reset()
75 instance
.outputDot(dot
)
76 dot
.orientation
= "portrait"
80 dot
.write("config.dot")
81 dot
.write_ps("config.ps")
85 def simulate(*args
, **kwargs
):
86 global need_resume
, need_startup
89 internal
.core
.SimStartup()
92 for root
in need_resume
:
96 return internal
.event
.simulate(*args
, **kwargs
)
98 # Export curTick to user script.
100 return internal
.core
.cvar
.curTick
102 # Python exit handlers happen in reverse order. We want to dump stats last.
103 atexit
.register(internal
.stats
.dump
)
105 # register our C++ exit callback function with Python
106 atexit
.register(internal
.core
.doExitCleanup
)
108 # This loops until all objects have been fully drained.
110 all_drained
= drain(root
)
111 while (not all_drained
):
112 all_drained
= drain(root
)
114 # Tries to drain all objects. Draining might not be completed unless
115 # all objects return that they are drained on the first call. This is
116 # because as objects drain they may cause other objects to no longer
120 drain_event
= internal
.event
.createCountedDrain()
121 unready_objects
= root
.startDrain(drain_event
, True)
122 # If we've got some objects that can't drain immediately, then simulate
123 if unready_objects
> 0:
124 drain_event
.setCount(unready_objects
)
128 internal
.event
.cleanupCountedDrain(drain_event
)
134 def checkpoint(root
, dir):
135 if not isinstance(root
, objects
.Root
):
136 raise TypeError, "Checkpoint must be called on a root object."
138 print "Writing checkpoint"
139 internal
.sim_object
.serializeAll(dir)
142 def restoreCheckpoint(root
, dir):
143 print "Restoring from checkpoint"
144 internal
.sim_object
.unserializeAll(dir)
145 need_resume
.append(root
)
147 def changeToAtomic(system
):
148 if not isinstance(system
, (objects
.Root
, objects
.System
)):
149 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
150 (type(system
), objects
.Root
, objects
.System
)
151 if system
.getMemoryMode() != internal
.sim_object
.SimObject
.Atomic
:
153 print "Changing memory mode to atomic"
154 system
.changeTiming(internal
.sim_object
.SimObject
.Atomic
)
156 def changeToTiming(system
):
157 if not isinstance(system
, (objects
.Root
, objects
.System
)):
158 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
159 (type(system
), objects
.Root
, objects
.System
)
161 if system
.getMemoryMode() != internal
.sim_object
.SimObject
.Timing
:
163 print "Changing memory mode to timing"
164 system
.changeTiming(internal
.sim_object
.SimObject
.Timing
)
166 def switchCpus(cpuList
):
167 print "switching cpus"
168 if not isinstance(cpuList
, list):
169 raise RuntimeError, "Must pass a list to this function"
171 if not isinstance(i
, tuple):
172 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
174 [old_cpus
, new_cpus
] = zip(*cpuList
)
177 if not isinstance(cpu
, objects
.BaseCPU
):
178 raise TypeError, "%s is not of type BaseCPU" % cpu
180 if not isinstance(cpu
, objects
.BaseCPU
):
181 raise TypeError, "%s is not of type BaseCPU" % cpu
183 # Drain all of the individual CPUs
184 drain_event
= internal
.event
.createCountedDrain()
186 for old_cpu
in old_cpus
:
187 unready_cpus
+= old_cpu
.startDrain(drain_event
, False)
188 # If we've got some objects that can't drain immediately, then simulate
190 drain_event
.setCount(unready_cpus
)
192 internal
.event
.cleanupCountedDrain(drain_event
)
193 # Now all of the CPUs are ready to be switched out
194 for old_cpu
in old_cpus
:
195 old_cpu
._ccObject
.switchOut()
197 for new_cpu
in new_cpus
:
198 new_cpu
.takeOverFrom(old_cpus
[index
])
199 new_cpu
._ccObject
.resume()