simulate.py (6001:00251eb95de7) simulate.py (6654:4c84e771cca7)
1# Copyright (c) 2005 The Regents of The University of Michigan
2# All rights reserved.
3#
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.
14#
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.
26#
27# Authors: Nathan Binkert
28# Steve Reinhardt
29
30import atexit
31import os
32import sys
33
34# import the SWIG-wrapped main C++ functions
35import internal
36import core
37import stats
38from main import options
39import SimObject
40import ticks
41import objects
42
1# Copyright (c) 2005 The Regents of The University of Michigan
2# All rights reserved.
3#
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.
14#
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.
26#
27# Authors: Nathan Binkert
28# Steve Reinhardt
29
30import atexit
31import os
32import sys
33
34# import the SWIG-wrapped main C++ functions
35import internal
36import core
37import stats
38from main import options
39import SimObject
40import ticks
41import objects
42
43# define a MaxTick parameter
44MaxTick = 2**63 - 1
45
43# The final hook to generate .ini files. Called from the user script
44# once the config is built.
45def instantiate(root):
46 # we need to fix the global frequency
47 ticks.fixGlobalFrequency()
48
49 root.unproxy_all()
50
51 if options.dump_config:
52 ini_file = file(os.path.join(options.outdir, options.dump_config), 'w')
53 root.print_ini(ini_file)
54 ini_file.close()
55
56 # Initialize the global statistics
57 stats.initSimStats()
58
59 # Create the C++ sim objects and connect ports
60 root.createCCObject()
61 root.connectPorts()
62
63 # Do a second pass to finish initializing the sim objects
64 core.initAll()
65
66 # Do a third pass to initialize statistics
67 core.regAllStats()
68
69 # We're done registering statistics. Enable the stats package now.
70 stats.enable()
71
72 # Reset to put the stats in a consistent state.
73 stats.reset()
74
75def doDot(root):
76 dot = pydot.Dot()
77 instance.outputDot(dot)
78 dot.orientation = "portrait"
79 dot.size = "8.5,11"
80 dot.ranksep="equally"
81 dot.rank="samerank"
82 dot.write("config.dot")
83 dot.write_ps("config.ps")
84
85need_resume = []
86need_startup = True
87def simulate(*args, **kwargs):
88 global need_resume, need_startup
89
90 if need_startup:
91 internal.core.SimStartup()
92 need_startup = False
93
94 for root in need_resume:
95 resume(root)
96 need_resume = []
97
98 return internal.event.simulate(*args, **kwargs)
99
100# Export curTick to user script.
101def curTick():
102 return internal.core.cvar.curTick
103
104# Python exit handlers happen in reverse order. We want to dump stats last.
105atexit.register(internal.stats.dump)
106
107# register our C++ exit callback function with Python
108atexit.register(internal.core.doExitCleanup)
109
110# This loops until all objects have been fully drained.
111def doDrain(root):
112 all_drained = drain(root)
113 while (not all_drained):
114 all_drained = drain(root)
115
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
119# be drained.
120def drain(root):
121 all_drained = False
122 drain_event = internal.event.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)
127 simulate()
128 else:
129 all_drained = True
130 internal.event.cleanupCountedDrain(drain_event)
131 return all_drained
132
133def resume(root):
134 root.resume()
135
136def checkpoint(root, dir):
137 if not isinstance(root, objects.Root):
138 raise TypeError, "Checkpoint must be called on a root object."
139 doDrain(root)
140 print "Writing checkpoint"
141 internal.core.serializeAll(dir)
142 resume(root)
143
144def restoreCheckpoint(root, dir):
145 print "Restoring from checkpoint"
146 internal.core.unserializeAll(dir)
147 need_resume.append(root)
148
149def changeToAtomic(system):
150 if not isinstance(system, (objects.Root, objects.System)):
151 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
152 (type(system), objects.Root, objects.System)
153 if system.getMemoryMode() != objects.params.atomic:
154 doDrain(system)
155 print "Changing memory mode to atomic"
156 system.changeTiming(objects.params.atomic)
157
158def changeToTiming(system):
159 if not isinstance(system, (objects.Root, objects.System)):
160 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
161 (type(system), objects.Root, objects.System)
162
163 if system.getMemoryMode() != objects.params.timing:
164 doDrain(system)
165 print "Changing memory mode to timing"
166 system.changeTiming(objects.params.timing)
167
168def switchCpus(cpuList):
169 print "switching cpus"
170 if not isinstance(cpuList, list):
171 raise RuntimeError, "Must pass a list to this function"
172 for item in cpuList:
173 if not isinstance(item, tuple) or len(item) != 2:
174 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
175
176 for old_cpu, new_cpu in cpuList:
177 if not isinstance(old_cpu, objects.BaseCPU):
178 raise TypeError, "%s is not of type BaseCPU" % old_cpu
179 if not isinstance(new_cpu, objects.BaseCPU):
180 raise TypeError, "%s is not of type BaseCPU" % new_cpu
181
182 # Now all of the CPUs are ready to be switched out
183 for old_cpu, new_cpu in cpuList:
184 old_cpu._ccObject.switchOut()
185
186 for old_cpu, new_cpu in cpuList:
187 new_cpu.takeOverFrom(old_cpu)
188
189from internal.core import disableAllListeners
46# The final hook to generate .ini files. Called from the user script
47# once the config is built.
48def instantiate(root):
49 # we need to fix the global frequency
50 ticks.fixGlobalFrequency()
51
52 root.unproxy_all()
53
54 if options.dump_config:
55 ini_file = file(os.path.join(options.outdir, options.dump_config), 'w')
56 root.print_ini(ini_file)
57 ini_file.close()
58
59 # Initialize the global statistics
60 stats.initSimStats()
61
62 # Create the C++ sim objects and connect ports
63 root.createCCObject()
64 root.connectPorts()
65
66 # Do a second pass to finish initializing the sim objects
67 core.initAll()
68
69 # Do a third pass to initialize statistics
70 core.regAllStats()
71
72 # We're done registering statistics. Enable the stats package now.
73 stats.enable()
74
75 # Reset to put the stats in a consistent state.
76 stats.reset()
77
78def doDot(root):
79 dot = pydot.Dot()
80 instance.outputDot(dot)
81 dot.orientation = "portrait"
82 dot.size = "8.5,11"
83 dot.ranksep="equally"
84 dot.rank="samerank"
85 dot.write("config.dot")
86 dot.write_ps("config.ps")
87
88need_resume = []
89need_startup = True
90def simulate(*args, **kwargs):
91 global need_resume, need_startup
92
93 if need_startup:
94 internal.core.SimStartup()
95 need_startup = False
96
97 for root in need_resume:
98 resume(root)
99 need_resume = []
100
101 return internal.event.simulate(*args, **kwargs)
102
103# Export curTick to user script.
104def curTick():
105 return internal.core.cvar.curTick
106
107# Python exit handlers happen in reverse order. We want to dump stats last.
108atexit.register(internal.stats.dump)
109
110# register our C++ exit callback function with Python
111atexit.register(internal.core.doExitCleanup)
112
113# This loops until all objects have been fully drained.
114def doDrain(root):
115 all_drained = drain(root)
116 while (not all_drained):
117 all_drained = drain(root)
118
119# Tries to drain all objects. Draining might not be completed unless
120# all objects return that they are drained on the first call. This is
121# because as objects drain they may cause other objects to no longer
122# be drained.
123def drain(root):
124 all_drained = False
125 drain_event = internal.event.createCountedDrain()
126 unready_objects = root.startDrain(drain_event, True)
127 # If we've got some objects that can't drain immediately, then simulate
128 if unready_objects > 0:
129 drain_event.setCount(unready_objects)
130 simulate()
131 else:
132 all_drained = True
133 internal.event.cleanupCountedDrain(drain_event)
134 return all_drained
135
136def resume(root):
137 root.resume()
138
139def checkpoint(root, dir):
140 if not isinstance(root, objects.Root):
141 raise TypeError, "Checkpoint must be called on a root object."
142 doDrain(root)
143 print "Writing checkpoint"
144 internal.core.serializeAll(dir)
145 resume(root)
146
147def restoreCheckpoint(root, dir):
148 print "Restoring from checkpoint"
149 internal.core.unserializeAll(dir)
150 need_resume.append(root)
151
152def changeToAtomic(system):
153 if not isinstance(system, (objects.Root, objects.System)):
154 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
155 (type(system), objects.Root, objects.System)
156 if system.getMemoryMode() != objects.params.atomic:
157 doDrain(system)
158 print "Changing memory mode to atomic"
159 system.changeTiming(objects.params.atomic)
160
161def changeToTiming(system):
162 if not isinstance(system, (objects.Root, objects.System)):
163 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
164 (type(system), objects.Root, objects.System)
165
166 if system.getMemoryMode() != objects.params.timing:
167 doDrain(system)
168 print "Changing memory mode to timing"
169 system.changeTiming(objects.params.timing)
170
171def switchCpus(cpuList):
172 print "switching cpus"
173 if not isinstance(cpuList, list):
174 raise RuntimeError, "Must pass a list to this function"
175 for item in cpuList:
176 if not isinstance(item, tuple) or len(item) != 2:
177 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
178
179 for old_cpu, new_cpu in cpuList:
180 if not isinstance(old_cpu, objects.BaseCPU):
181 raise TypeError, "%s is not of type BaseCPU" % old_cpu
182 if not isinstance(new_cpu, objects.BaseCPU):
183 raise TypeError, "%s is not of type BaseCPU" % new_cpu
184
185 # Now all of the CPUs are ready to be switched out
186 for old_cpu, new_cpu in cpuList:
187 old_cpu._ccObject.switchOut()
188
189 for old_cpu, new_cpu in cpuList:
190 new_cpu.takeOverFrom(old_cpu)
191
192from internal.core import disableAllListeners