simulate.py (7532:3f6413fc37a2) simulate.py (7534:c76a14014c27)
1# Copyright (c) 2005 The Regents of The University of Michigan
1# Copyright (c) 2005 The Regents of The University of Michigan
2# Copyright (c) 2010 Advanced Micro Devices, Inc.
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
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.
49def instantiate(ckpt_dir=None):
50 root = objects.Root.getInstance()
51
52 if not root:
53 fatal("Need to instantiate Root() before calling instantiate()")
54
55 # we need to fix the global frequency
56 ticks.fixGlobalFrequency()
57
58 # Make sure SimObject-valued params are in the configuration
59 # hierarchy so we catch them with future descendants() walks
60 for obj in root.descendants(): obj.adoptOrphanParams()
61
62 # Unproxy in sorted order for determinism
63 for obj in root.descendants(): obj.unproxyParams()
64
65 if options.dump_config:
66 ini_file = file(os.path.join(options.outdir, options.dump_config), 'w')
67 # Print ini sections in sorted order for easier diffing
68 for obj in sorted(root.descendants(), key=lambda o: o.path()):
69 obj.print_ini(ini_file)
70 ini_file.close()
71
72 # Initialize the global statistics
73 stats.initSimStats()
74
75 # Create the C++ sim objects and connect ports
76 for obj in root.descendants(): obj.createCCObject()
77 for obj in root.descendants(): obj.connectPorts()
78
79 # Do a second pass to finish initializing the sim objects
80 for obj in root.descendants(): obj.init()
81
82 # Do a third pass to initialize statistics
83 for obj in root.descendants(): obj.regStats()
84 for obj in root.descendants(): obj.regFormulas()
85
86 # We're done registering statistics. Enable the stats package now.
87 stats.enable()
88
89 # Restore checkpoint (if any)
90 if ckpt_dir:
91 ckpt = internal.core.getCheckpoint(ckpt_dir)
92 internal.core.unserializeGlobals(ckpt);
93 for obj in root.descendants(): obj.loadState(ckpt)
94 need_resume.append(root)
95 else:
96 for obj in root.descendants(): obj.initState()
97
98 # Reset to put the stats in a consistent state.
99 stats.reset()
100
101def doDot(root):
102 dot = pydot.Dot()
103 instance.outputDot(dot)
104 dot.orientation = "portrait"
105 dot.size = "8.5,11"
106 dot.ranksep="equally"
107 dot.rank="samerank"
108 dot.write("config.dot")
109 dot.write_ps("config.ps")
110
111need_resume = []
112need_startup = True
113def simulate(*args, **kwargs):
114 global need_resume, need_startup
115
116 if need_startup:
117 root = objects.Root.getInstance()
118 for obj in root.descendants(): obj.startup()
119 need_startup = False
120
121 for root in need_resume:
122 resume(root)
123 need_resume = []
124
125 return internal.event.simulate(*args, **kwargs)
126
127# Export curTick to user script.
128def curTick():
129 return internal.core.cvar.curTick
130
131# Python exit handlers happen in reverse order. We want to dump stats last.
132atexit.register(internal.stats.dump)
133
134# register our C++ exit callback function with Python
135atexit.register(internal.core.doExitCleanup)
136
137# This loops until all objects have been fully drained.
138def doDrain(root):
139 all_drained = drain(root)
140 while (not all_drained):
141 all_drained = drain(root)
142
143# Tries to drain all objects. Draining might not be completed unless
144# all objects return that they are drained on the first call. This is
145# because as objects drain they may cause other objects to no longer
146# be drained.
147def drain(root):
148 all_drained = False
149 drain_event = internal.event.createCountedDrain()
150 unready_objs = sum(obj.drain(drain_event) for obj in root.descendants())
151 # If we've got some objects that can't drain immediately, then simulate
152 if unready_objs > 0:
153 drain_event.setCount(unready_objs)
154 simulate()
155 else:
156 all_drained = True
157 internal.event.cleanupCountedDrain(drain_event)
158 return all_drained
159
160def resume(root):
161 for obj in root.descendants(): obj.resume()
162
163def checkpoint(dir):
164 root = objects.Root.getInstance()
165 if not isinstance(root, objects.Root):
166 raise TypeError, "Checkpoint must be called on a root object."
167 doDrain(root)
168 print "Writing checkpoint"
169 internal.core.serializeAll(dir)
170 resume(root)
171
172def changeToAtomic(system):
173 if not isinstance(system, (objects.Root, objects.System)):
174 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
175 (type(system), objects.Root, objects.System)
176 if system.getMemoryMode() != objects.params.atomic:
177 doDrain(system)
178 print "Changing memory mode to atomic"
179 for obj in system.descendants():
180 obj.changeTiming(objects.params.atomic)
181
182def changeToTiming(system):
183 if not isinstance(system, (objects.Root, objects.System)):
184 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
185 (type(system), objects.Root, objects.System)
186
187 if system.getMemoryMode() != objects.params.timing:
188 doDrain(system)
189 print "Changing memory mode to timing"
190 for obj in system.descendants():
191 obj.changeTiming(objects.params.timing)
192
193def switchCpus(cpuList):
194 print "switching cpus"
195 if not isinstance(cpuList, list):
196 raise RuntimeError, "Must pass a list to this function"
197 for item in cpuList:
198 if not isinstance(item, tuple) or len(item) != 2:
199 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
200
201 for old_cpu, new_cpu in cpuList:
202 if not isinstance(old_cpu, objects.BaseCPU):
203 raise TypeError, "%s is not of type BaseCPU" % old_cpu
204 if not isinstance(new_cpu, objects.BaseCPU):
205 raise TypeError, "%s is not of type BaseCPU" % new_cpu
206
207 # Now all of the CPUs are ready to be switched out
208 for old_cpu, new_cpu in cpuList:
209 old_cpu._ccObject.switchOut()
210
211 for old_cpu, new_cpu in cpuList:
212 new_cpu.takeOverFrom(old_cpu)
213
214from internal.core import disableAllListeners
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;
9# redistributions in binary form must reproduce the above copyright
10# notice, this list of conditions and the following disclaimer in the
11# documentation and/or other materials provided with the distribution;
12# neither the name of the copyright holders nor the names of its
13# contributors may be used to endorse or promote products derived from
14# this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28# Authors: Nathan Binkert
29# Steve Reinhardt
30
31import atexit
32import os
33import sys
34
35# import the SWIG-wrapped main C++ functions
36import internal
37import core
38import stats
39from main import options
40import SimObject
41import ticks
42import objects
43from util import fatal
44
45# define a MaxTick parameter
46MaxTick = 2**63 - 1
47
48# The final hook to generate .ini files. Called from the user script
49# once the config is built.
50def instantiate(ckpt_dir=None):
51 root = objects.Root.getInstance()
52
53 if not root:
54 fatal("Need to instantiate Root() before calling instantiate()")
55
56 # we need to fix the global frequency
57 ticks.fixGlobalFrequency()
58
59 # Make sure SimObject-valued params are in the configuration
60 # hierarchy so we catch them with future descendants() walks
61 for obj in root.descendants(): obj.adoptOrphanParams()
62
63 # Unproxy in sorted order for determinism
64 for obj in root.descendants(): obj.unproxyParams()
65
66 if options.dump_config:
67 ini_file = file(os.path.join(options.outdir, options.dump_config), 'w')
68 # Print ini sections in sorted order for easier diffing
69 for obj in sorted(root.descendants(), key=lambda o: o.path()):
70 obj.print_ini(ini_file)
71 ini_file.close()
72
73 # Initialize the global statistics
74 stats.initSimStats()
75
76 # Create the C++ sim objects and connect ports
77 for obj in root.descendants(): obj.createCCObject()
78 for obj in root.descendants(): obj.connectPorts()
79
80 # Do a second pass to finish initializing the sim objects
81 for obj in root.descendants(): obj.init()
82
83 # Do a third pass to initialize statistics
84 for obj in root.descendants(): obj.regStats()
85 for obj in root.descendants(): obj.regFormulas()
86
87 # We're done registering statistics. Enable the stats package now.
88 stats.enable()
89
90 # Restore checkpoint (if any)
91 if ckpt_dir:
92 ckpt = internal.core.getCheckpoint(ckpt_dir)
93 internal.core.unserializeGlobals(ckpt);
94 for obj in root.descendants(): obj.loadState(ckpt)
95 need_resume.append(root)
96 else:
97 for obj in root.descendants(): obj.initState()
98
99 # Reset to put the stats in a consistent state.
100 stats.reset()
101
102def doDot(root):
103 dot = pydot.Dot()
104 instance.outputDot(dot)
105 dot.orientation = "portrait"
106 dot.size = "8.5,11"
107 dot.ranksep="equally"
108 dot.rank="samerank"
109 dot.write("config.dot")
110 dot.write_ps("config.ps")
111
112need_resume = []
113need_startup = True
114def simulate(*args, **kwargs):
115 global need_resume, need_startup
116
117 if need_startup:
118 root = objects.Root.getInstance()
119 for obj in root.descendants(): obj.startup()
120 need_startup = False
121
122 for root in need_resume:
123 resume(root)
124 need_resume = []
125
126 return internal.event.simulate(*args, **kwargs)
127
128# Export curTick to user script.
129def curTick():
130 return internal.core.cvar.curTick
131
132# Python exit handlers happen in reverse order. We want to dump stats last.
133atexit.register(internal.stats.dump)
134
135# register our C++ exit callback function with Python
136atexit.register(internal.core.doExitCleanup)
137
138# This loops until all objects have been fully drained.
139def doDrain(root):
140 all_drained = drain(root)
141 while (not all_drained):
142 all_drained = drain(root)
143
144# Tries to drain all objects. Draining might not be completed unless
145# all objects return that they are drained on the first call. This is
146# because as objects drain they may cause other objects to no longer
147# be drained.
148def drain(root):
149 all_drained = False
150 drain_event = internal.event.createCountedDrain()
151 unready_objs = sum(obj.drain(drain_event) for obj in root.descendants())
152 # If we've got some objects that can't drain immediately, then simulate
153 if unready_objs > 0:
154 drain_event.setCount(unready_objs)
155 simulate()
156 else:
157 all_drained = True
158 internal.event.cleanupCountedDrain(drain_event)
159 return all_drained
160
161def resume(root):
162 for obj in root.descendants(): obj.resume()
163
164def checkpoint(dir):
165 root = objects.Root.getInstance()
166 if not isinstance(root, objects.Root):
167 raise TypeError, "Checkpoint must be called on a root object."
168 doDrain(root)
169 print "Writing checkpoint"
170 internal.core.serializeAll(dir)
171 resume(root)
172
173def changeToAtomic(system):
174 if not isinstance(system, (objects.Root, objects.System)):
175 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
176 (type(system), objects.Root, objects.System)
177 if system.getMemoryMode() != objects.params.atomic:
178 doDrain(system)
179 print "Changing memory mode to atomic"
180 for obj in system.descendants():
181 obj.changeTiming(objects.params.atomic)
182
183def changeToTiming(system):
184 if not isinstance(system, (objects.Root, objects.System)):
185 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \
186 (type(system), objects.Root, objects.System)
187
188 if system.getMemoryMode() != objects.params.timing:
189 doDrain(system)
190 print "Changing memory mode to timing"
191 for obj in system.descendants():
192 obj.changeTiming(objects.params.timing)
193
194def switchCpus(cpuList):
195 print "switching cpus"
196 if not isinstance(cpuList, list):
197 raise RuntimeError, "Must pass a list to this function"
198 for item in cpuList:
199 if not isinstance(item, tuple) or len(item) != 2:
200 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
201
202 for old_cpu, new_cpu in cpuList:
203 if not isinstance(old_cpu, objects.BaseCPU):
204 raise TypeError, "%s is not of type BaseCPU" % old_cpu
205 if not isinstance(new_cpu, objects.BaseCPU):
206 raise TypeError, "%s is not of type BaseCPU" % new_cpu
207
208 # Now all of the CPUs are ready to be switched out
209 for old_cpu, new_cpu in cpuList:
210 old_cpu._ccObject.switchOut()
211
212 for old_cpu, new_cpu in cpuList:
213 new_cpu.takeOverFrom(old_cpu)
214
215from internal.core import disableAllListeners