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