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