__init__.py (3102:225b76c8ac68) __init__.py (3105:993f1abefd67)
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
47# force scalars to one-element lists for uniformity
47def makeList(objOrList):
48 if isinstance(objOrList, list):
49 return objOrList
50 return [objOrList]
51
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# The final hook to generate .ini files. Called from the user script
75# once the config is built.
76def instantiate(root):
77 params.ticks_per_sec = float(root.clock.frequency)
48def makeList(objOrList):
49 if isinstance(objOrList, list):
50 return objOrList
51 return [objOrList]
52
53# Prepend given directory to system module search path. We may not
54# need this anymore if we can structure our config library more like a
55# Python package.
56def AddToPath(path):
57 # if it's a relative path and we know what directory the current
58 # python script is in, make the path relative to that directory.
59 if not os.path.isabs(path) and sys.path[0]:
60 path = os.path.join(sys.path[0], path)
61 path = os.path.realpath(path)
62 # sys.path[0] should always refer to the current script's directory,
63 # so place the new dir right after that.
64 sys.path.insert(1, path)
65
66# make a SmartDict out of the build options for our local use
67import smartdict
68build_env = smartdict.SmartDict()
69build_env.update(defines.m5_build_env)
70
71# make a SmartDict out of the OS environment too
72env = smartdict.SmartDict()
73env.update(os.environ)
74
75# The final hook to generate .ini files. Called from the user script
76# once the config is built.
77def instantiate(root):
78 params.ticks_per_sec = float(root.clock.frequency)
79 root.unproxy_all()
78 # ugly temporary hack to get output to config.ini
79 sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w')
80 root.print_ini()
81 sys.stdout.close() # close config.ini
82 sys.stdout = sys.__stdout__ # restore to original
83 cc_main.loadIniFile(resolveSimObject) # load config.ini into C++
84 root.createCCObject()
85 root.connectPorts()
86 cc_main.finalInit()
87 noDot = True # temporary until we fix dot
88 if not noDot:
89 dot = pydot.Dot()
90 instance.outputDot(dot)
91 dot.orientation = "portrait"
92 dot.size = "8.5,11"
93 dot.ranksep="equally"
94 dot.rank="samerank"
95 dot.write("config.dot")
96 dot.write_ps("config.ps")
97
98# Export curTick to user script.
99def curTick():
100 return cc_main.cvar.curTick
101
102# register our C++ exit callback function with Python
103atexit.register(cc_main.doExitCleanup)
104
105# This loops until all objects have been fully drained.
106def doDrain(root):
107 all_drained = drain(root)
108 while (not all_drained):
109 all_drained = drain(root)
110
111# Tries to drain all objects. Draining might not be completed unless
112# all objects return that they are drained on the first call. This is
113# because as objects drain they may cause other objects to no longer
114# be drained.
115def drain(root):
116 all_drained = False
117 drain_event = cc_main.createCountedDrain()
118 unready_objects = root.startDrain(drain_event, True)
119 # If we've got some objects that can't drain immediately, then simulate
120 if unready_objects > 0:
121 drain_event.setCount(unready_objects)
122 simulate()
123 else:
124 all_drained = True
125 cc_main.cleanupCountedDrain(drain_event)
126 return all_drained
127
128def resume(root):
129 root.resume()
130
131def checkpoint(root, dir):
132 if not isinstance(root, objects.Root):
133 raise TypeError, "Object is not a root object. Checkpoint must be called on a root object."
134 doDrain(root)
135 print "Writing checkpoint"
136 cc_main.serializeAll(dir)
137 resume(root)
138
139def restoreCheckpoint(root, dir):
140 print "Restoring from checkpoint"
141 cc_main.unserializeAll(dir)
142 resume(root)
143
144def changeToAtomic(system):
145 if not isinstance(system, objects.Root) and not isinstance(system, System):
146 raise TypeError, "Object is not a root or system object. Checkpoint must be "
147 "called on a root object."
148 doDrain(system)
149 print "Changing memory mode to atomic"
150 system.changeTiming(cc_main.SimObject.Atomic)
151 resume(system)
152
153def changeToTiming(system):
154 if not isinstance(system, objects.Root) and not isinstance(system, System):
155 raise TypeError, "Object is not a root or system object. Checkpoint must be "
156 "called on a root object."
157 doDrain(system)
158 print "Changing memory mode to timing"
159 system.changeTiming(cc_main.SimObject.Timing)
160 resume(system)
161
162def switchCpus(cpuList):
163 if not isinstance(cpuList, list):
164 raise RuntimeError, "Must pass a list to this function"
165 for i in cpuList:
166 if not isinstance(i, tuple):
167 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
168
169 [old_cpus, new_cpus] = zip(*cpuList)
170
171 for cpu in old_cpus:
172 if not isinstance(cpu, objects.BaseCPU):
173 raise TypeError, "%s is not of type BaseCPU", cpu
174 for cpu in new_cpus:
175 if not isinstance(cpu, objects.BaseCPU):
176 raise TypeError, "%s is not of type BaseCPU", cpu
177
178 # Drain all of the individual CPUs
179 drain_event = cc_main.createCountedDrain()
180 unready_cpus = 0
181 for old_cpu in old_cpus:
182 unready_cpus += old_cpu.startDrain(drain_event, False)
183 # If we've got some objects that can't drain immediately, then simulate
184 if unready_cpus > 0:
185 drain_event.setCount(unready_cpus)
186 simulate()
187 cc_main.cleanupCountedDrain(drain_event)
188 # Now all of the CPUs are ready to be switched out
189 for old_cpu in old_cpus:
190 old_cpu._ccObject.switchOut()
191 index = 0
192 print "Switching CPUs"
193 for new_cpu in new_cpus:
194 new_cpu.takeOverFrom(old_cpus[index])
195 new_cpu._ccObject.resume()
196 index += 1
197
198# Since we have so many mutual imports in this package, we should:
199# 1. Put all intra-package imports at the *bottom* of the file, unless
200# they're absolutely needed before that (for top-level statements
201# or class attributes). Imports of "trivial" packages that don't
202# import other packages (e.g., 'smartdict') can be at the top.
203# 2. Never use 'from foo import *' on an intra-package import since
204# you can get the wrong result if foo is only partially imported
205# at the point you do that (i.e., because foo is in the middle of
206# importing *you*).
207from main import options
208import objects
209import params
210from SimObject import resolveSimObject
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 loops until all objects have been fully drained.
108def doDrain(root):
109 all_drained = drain(root)
110 while (not all_drained):
111 all_drained = drain(root)
112
113# Tries to drain all objects. Draining might not be completed unless
114# all objects return that they are drained on the first call. This is
115# because as objects drain they may cause other objects to no longer
116# be drained.
117def drain(root):
118 all_drained = False
119 drain_event = cc_main.createCountedDrain()
120 unready_objects = root.startDrain(drain_event, True)
121 # If we've got some objects that can't drain immediately, then simulate
122 if unready_objects > 0:
123 drain_event.setCount(unready_objects)
124 simulate()
125 else:
126 all_drained = True
127 cc_main.cleanupCountedDrain(drain_event)
128 return all_drained
129
130def resume(root):
131 root.resume()
132
133def checkpoint(root, dir):
134 if not isinstance(root, objects.Root):
135 raise TypeError, "Object is not a root object. Checkpoint must be called on a root object."
136 doDrain(root)
137 print "Writing checkpoint"
138 cc_main.serializeAll(dir)
139 resume(root)
140
141def restoreCheckpoint(root, dir):
142 print "Restoring from checkpoint"
143 cc_main.unserializeAll(dir)
144 resume(root)
145
146def changeToAtomic(system):
147 if not isinstance(system, objects.Root) and not isinstance(system, System):
148 raise TypeError, "Object is not a root or system object. Checkpoint must be "
149 "called on a root object."
150 doDrain(system)
151 print "Changing memory mode to atomic"
152 system.changeTiming(cc_main.SimObject.Atomic)
153 resume(system)
154
155def changeToTiming(system):
156 if not isinstance(system, objects.Root) and not isinstance(system, System):
157 raise TypeError, "Object is not a root or system object. Checkpoint must be "
158 "called on a root object."
159 doDrain(system)
160 print "Changing memory mode to timing"
161 system.changeTiming(cc_main.SimObject.Timing)
162 resume(system)
163
164def switchCpus(cpuList):
165 if not isinstance(cpuList, list):
166 raise RuntimeError, "Must pass a list to this function"
167 for i in cpuList:
168 if not isinstance(i, tuple):
169 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
170
171 [old_cpus, new_cpus] = zip(*cpuList)
172
173 for cpu in old_cpus:
174 if not isinstance(cpu, objects.BaseCPU):
175 raise TypeError, "%s is not of type BaseCPU", cpu
176 for cpu in new_cpus:
177 if not isinstance(cpu, objects.BaseCPU):
178 raise TypeError, "%s is not of type BaseCPU", cpu
179
180 # Drain all of the individual CPUs
181 drain_event = cc_main.createCountedDrain()
182 unready_cpus = 0
183 for old_cpu in old_cpus:
184 unready_cpus += old_cpu.startDrain(drain_event, False)
185 # If we've got some objects that can't drain immediately, then simulate
186 if unready_cpus > 0:
187 drain_event.setCount(unready_cpus)
188 simulate()
189 cc_main.cleanupCountedDrain(drain_event)
190 # Now all of the CPUs are ready to be switched out
191 for old_cpu in old_cpus:
192 old_cpu._ccObject.switchOut()
193 index = 0
194 print "Switching CPUs"
195 for new_cpu in new_cpus:
196 new_cpu.takeOverFrom(old_cpus[index])
197 new_cpu._ccObject.resume()
198 index += 1
199
200# Since we have so many mutual imports in this package, we should:
201# 1. Put all intra-package imports at the *bottom* of the file, unless
202# they're absolutely needed before that (for top-level statements
203# or class attributes). Imports of "trivial" packages that don't
204# import other packages (e.g., 'smartdict') can be at the top.
205# 2. Never use 'from foo import *' on an intra-package import since
206# you can get the wrong result if foo is only partially imported
207# at the point you do that (i.e., because foo is in the middle of
208# importing *you*).
209from main import options
210import objects
211import params
212from SimObject import resolveSimObject