__init__.py (2889:9e367e03d656) __init__.py (2969:d2f8f9a23082)
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, os, sys
31
32# import the SWIG-wrapped main C++ functions
33import cc_main
34# import a few SWIG-wrapped items (those that are likely to be used
35# directly by user scripts) completely into this module for
36# convenience
37from cc_main import simulate, SimLoopExitEvent
38
39# import the m5 compile options
40import defines
41
42# define this here so we can use it right away if necessary
43def panic(string):
44 print >>sys.stderr, 'panic:', string
45 sys.exit(1)
46
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, os, sys
31
32# import the SWIG-wrapped main C++ functions
33import cc_main
34# import a few SWIG-wrapped items (those that are likely to be used
35# directly by user scripts) completely into this module for
36# convenience
37from cc_main import simulate, SimLoopExitEvent
38
39# import the m5 compile options
40import defines
41
42# define this here so we can use it right away if necessary
43def panic(string):
44 print >>sys.stderr, 'panic:', string
45 sys.exit(1)
46
47def makeList(objOrList):
48 if isinstance(objOrList, list):
49 return objOrList
50 return [objOrList]
51
47# Prepend given directory to system module search path. We may not
48# need this anymore if we can structure our config library more like a
49# Python package.
50def AddToPath(path):
51 # if it's a relative path and we know what directory the current
52 # python script is in, make the path relative to that directory.
53 if not os.path.isabs(path) and sys.path[0]:
54 path = os.path.join(sys.path[0], path)
55 path = os.path.realpath(path)
56 # sys.path[0] should always refer to the current script's directory,
57 # so place the new dir right after that.
58 sys.path.insert(1, path)
59
60# make a SmartDict out of the build options for our local use
61import smartdict
62build_env = smartdict.SmartDict()
63build_env.update(defines.m5_build_env)
64
65# make a SmartDict out of the OS environment too
66env = smartdict.SmartDict()
67env.update(os.environ)
68
69# Function to provide to C++ so it can look up instances based on paths
70def resolveSimObject(name):
71 obj = config.instanceDict[name]
72 return obj.getCCObject()
73
74from main import options, arguments, main
75
76# The final hook to generate .ini files. Called from the user script
77# once the config is built.
78def instantiate(root):
79 config.ticks_per_sec = float(root.clock.frequency)
80 # ugly temporary hack to get output to config.ini
81 sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w')
82 root.print_ini()
83 sys.stdout.close() # close config.ini
84 sys.stdout = sys.__stdout__ # restore to original
85 cc_main.loadIniFile(resolveSimObject) # load config.ini into C++
86 root.createCCObject()
87 root.connectPorts()
88 cc_main.finalInit()
89 noDot = True # temporary until we fix dot
90 if not noDot:
91 dot = pydot.Dot()
92 instance.outputDot(dot)
93 dot.orientation = "portrait"
94 dot.size = "8.5,11"
95 dot.ranksep="equally"
96 dot.rank="samerank"
97 dot.write("config.dot")
98 dot.write_ps("config.ps")
99
100# Export curTick to user script.
101def curTick():
102 return cc_main.cvar.curTick
103
104# register our C++ exit callback function with Python
105atexit.register(cc_main.doExitCleanup)
106
107# This import allows user scripts to reference 'm5.objects.Foo' after
108# just doing an 'import m5' (without an 'import m5.objects'). May not
109# matter since most scripts will probably 'from m5.objects import *'.
110import objects
111
112# This loops until all objects have been fully drained.
113def doDrain(root):
114 all_drained = drain(root)
115 while (not all_drained):
116 all_drained = drain(root)
117
118# Tries to drain all objects. Draining might not be completed unless
119# all objects return that they are drained on the first call. This is
120# because as objects drain they may cause other objects to no longer
121# be drained.
122def drain(root):
123 all_drained = False
124 drain_event = cc_main.createCountedDrain()
125 unready_objects = root.startDrain(drain_event, True)
126 # If we've got some objects that can't drain immediately, then simulate
127 if unready_objects > 0:
128 drain_event.setCount(unready_objects)
129 simulate()
130 else:
131 all_drained = True
132 cc_main.cleanupCountedDrain(drain_event)
133 return all_drained
134
135def resume(root):
136 root.resume()
137
138def checkpoint(root, dir):
139 if not isinstance(root, objects.Root):
140 raise TypeError, "Object is not a root object. Checkpoint must be called on a root object."
141 doDrain(root)
142 print "Writing checkpoint"
143 cc_main.serializeAll(dir)
144 resume(root)
145
146def restoreCheckpoint(root, dir):
147 print "Restoring from checkpoint"
148 cc_main.unserializeAll(dir)
149 resume(root)
150
151def changeToAtomic(system):
152 if not isinstance(system, objects.Root) and not isinstance(system, System):
153 raise TypeError, "Object is not a root or system object. Checkpoint must be "
154 "called on a root object."
155 doDrain(system)
156 print "Changing memory mode to atomic"
157 system.changeTiming(cc_main.SimObject.Atomic)
158 resume(system)
159
160def changeToTiming(system):
161 if not isinstance(system, objects.Root) and not isinstance(system, System):
162 raise TypeError, "Object is not a root or system object. Checkpoint must be "
163 "called on a root object."
164 doDrain(system)
165 print "Changing memory mode to timing"
166 system.changeTiming(cc_main.SimObject.Timing)
167 resume(system)
168
169def switchCpus(cpuList):
170 if not isinstance(cpuList, list):
171 raise RuntimeError, "Must pass a list to this function"
172 for i in cpuList:
173 if not isinstance(i, tuple):
174 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
175
176 [old_cpus, new_cpus] = zip(*cpuList)
177
178 for cpu in old_cpus:
179 if not isinstance(cpu, objects.BaseCPU):
180 raise TypeError, "%s is not of type BaseCPU", cpu
181 for cpu in new_cpus:
182 if not isinstance(cpu, objects.BaseCPU):
183 raise TypeError, "%s is not of type BaseCPU", cpu
184
185 # Drain all of the individual CPUs
186 drain_event = cc_main.createCountedDrain()
187 unready_cpus = 0
188 for old_cpu in old_cpus:
189 unready_cpus += old_cpu.startDrain(drain_event, False)
190 # If we've got some objects that can't drain immediately, then simulate
191 if unready_cpus > 0:
192 drain_event.setCount(unready_cpus)
193 simulate()
194 cc_main.cleanupCountedDrain(drain_event)
195 # Now all of the CPUs are ready to be switched out
196 for old_cpu in old_cpus:
197 old_cpu._ccObject.switchOut()
198 index = 0
199 print "Switching CPUs"
200 for new_cpu in new_cpus:
201 new_cpu.takeOverFrom(old_cpus[index])
202 new_cpu._ccObject.resume()
203 index += 1
52# Prepend given directory to system module search path. We may not
53# need this anymore if we can structure our config library more like a
54# Python package.
55def AddToPath(path):
56 # if it's a relative path and we know what directory the current
57 # python script is in, make the path relative to that directory.
58 if not os.path.isabs(path) and sys.path[0]:
59 path = os.path.join(sys.path[0], path)
60 path = os.path.realpath(path)
61 # sys.path[0] should always refer to the current script's directory,
62 # so place the new dir right after that.
63 sys.path.insert(1, path)
64
65# make a SmartDict out of the build options for our local use
66import smartdict
67build_env = smartdict.SmartDict()
68build_env.update(defines.m5_build_env)
69
70# make a SmartDict out of the OS environment too
71env = smartdict.SmartDict()
72env.update(os.environ)
73
74# Function to provide to C++ so it can look up instances based on paths
75def resolveSimObject(name):
76 obj = config.instanceDict[name]
77 return obj.getCCObject()
78
79from main import options, arguments, main
80
81# The final hook to generate .ini files. Called from the user script
82# once the config is built.
83def instantiate(root):
84 config.ticks_per_sec = float(root.clock.frequency)
85 # ugly temporary hack to get output to config.ini
86 sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w')
87 root.print_ini()
88 sys.stdout.close() # close config.ini
89 sys.stdout = sys.__stdout__ # restore to original
90 cc_main.loadIniFile(resolveSimObject) # load config.ini into C++
91 root.createCCObject()
92 root.connectPorts()
93 cc_main.finalInit()
94 noDot = True # temporary until we fix dot
95 if not noDot:
96 dot = pydot.Dot()
97 instance.outputDot(dot)
98 dot.orientation = "portrait"
99 dot.size = "8.5,11"
100 dot.ranksep="equally"
101 dot.rank="samerank"
102 dot.write("config.dot")
103 dot.write_ps("config.ps")
104
105# Export curTick to user script.
106def curTick():
107 return cc_main.cvar.curTick
108
109# register our C++ exit callback function with Python
110atexit.register(cc_main.doExitCleanup)
111
112# This import allows user scripts to reference 'm5.objects.Foo' after
113# just doing an 'import m5' (without an 'import m5.objects'). May not
114# matter since most scripts will probably 'from m5.objects import *'.
115import objects
116
117# This loops until all objects have been fully drained.
118def doDrain(root):
119 all_drained = drain(root)
120 while (not all_drained):
121 all_drained = drain(root)
122
123# Tries to drain all objects. Draining might not be completed unless
124# all objects return that they are drained on the first call. This is
125# because as objects drain they may cause other objects to no longer
126# be drained.
127def drain(root):
128 all_drained = False
129 drain_event = cc_main.createCountedDrain()
130 unready_objects = root.startDrain(drain_event, True)
131 # If we've got some objects that can't drain immediately, then simulate
132 if unready_objects > 0:
133 drain_event.setCount(unready_objects)
134 simulate()
135 else:
136 all_drained = True
137 cc_main.cleanupCountedDrain(drain_event)
138 return all_drained
139
140def resume(root):
141 root.resume()
142
143def checkpoint(root, dir):
144 if not isinstance(root, objects.Root):
145 raise TypeError, "Object is not a root object. Checkpoint must be called on a root object."
146 doDrain(root)
147 print "Writing checkpoint"
148 cc_main.serializeAll(dir)
149 resume(root)
150
151def restoreCheckpoint(root, dir):
152 print "Restoring from checkpoint"
153 cc_main.unserializeAll(dir)
154 resume(root)
155
156def changeToAtomic(system):
157 if not isinstance(system, objects.Root) and not isinstance(system, System):
158 raise TypeError, "Object is not a root or system object. Checkpoint must be "
159 "called on a root object."
160 doDrain(system)
161 print "Changing memory mode to atomic"
162 system.changeTiming(cc_main.SimObject.Atomic)
163 resume(system)
164
165def changeToTiming(system):
166 if not isinstance(system, objects.Root) and not isinstance(system, System):
167 raise TypeError, "Object is not a root or system object. Checkpoint must be "
168 "called on a root object."
169 doDrain(system)
170 print "Changing memory mode to timing"
171 system.changeTiming(cc_main.SimObject.Timing)
172 resume(system)
173
174def switchCpus(cpuList):
175 if not isinstance(cpuList, list):
176 raise RuntimeError, "Must pass a list to this function"
177 for i in cpuList:
178 if not isinstance(i, tuple):
179 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
180
181 [old_cpus, new_cpus] = zip(*cpuList)
182
183 for cpu in old_cpus:
184 if not isinstance(cpu, objects.BaseCPU):
185 raise TypeError, "%s is not of type BaseCPU", cpu
186 for cpu in new_cpus:
187 if not isinstance(cpu, objects.BaseCPU):
188 raise TypeError, "%s is not of type BaseCPU", cpu
189
190 # Drain all of the individual CPUs
191 drain_event = cc_main.createCountedDrain()
192 unready_cpus = 0
193 for old_cpu in old_cpus:
194 unready_cpus += old_cpu.startDrain(drain_event, False)
195 # If we've got some objects that can't drain immediately, then simulate
196 if unready_cpus > 0:
197 drain_event.setCount(unready_cpus)
198 simulate()
199 cc_main.cleanupCountedDrain(drain_event)
200 # Now all of the CPUs are ready to be switched out
201 for old_cpu in old_cpus:
202 old_cpu._ccObject.switchOut()
203 index = 0
204 print "Switching CPUs"
205 for new_cpu in new_cpus:
206 new_cpu.takeOverFrom(old_cpus[index])
207 new_cpu._ccObject.resume()
208 index += 1