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 --- 13 unchanged lines hidden (view full) --- 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 36# import a few SWIG-wrapped items (those that are likely to be used 37# directly by user scripts) completely into this module for 38# convenience |
39from internal.event import SimLoopExitEvent |
40 41# import the m5 compile options 42import defines 43 44# define a MaxTick parameter 45MaxTick = 2**63 - 1 46 47# define this here so we can use it right away if necessary --- 34 unchanged lines hidden (view full) --- 82def instantiate(root): 83 params.ticks_per_sec = float(root.clock.frequency) 84 root.unproxy_all() 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 91 # load config.ini into C++ 92 internal.core.loadIniFile(resolveSimObject) 93 94 # Initialize the global statistics 95 internal.stats.initSimStats() 96 |
97 root.createCCObject() 98 root.connectPorts() |
99 |
100 # Do a second pass to finish initializing the sim objects 101 internal.sim_object.initAll() 102 103 # Do a third pass to initialize statistics 104 internal.sim_object.regAllStats() 105 106 # Check to make sure that the stats package is properly initialized 107 internal.stats.check() 108 109 # Reset to put the stats in a consistent state. 110 internal.stats.reset() 111 112def doDot(root): 113 dot = pydot.Dot() 114 instance.outputDot(dot) 115 dot.orientation = "portrait" 116 dot.size = "8.5,11" 117 dot.ranksep="equally" 118 dot.rank="samerank" 119 dot.write("config.dot") 120 dot.write_ps("config.ps") 121 122need_resume = [] 123need_startup = True 124def simulate(*args, **kwargs): 125 global need_resume, need_startup 126 127 if need_startup: 128 internal.core.SimStartup() 129 need_startup = False 130 131 for root in need_resume: 132 resume(root) 133 need_resume = [] 134 135 return internal.event.simulate(*args, **kwargs) 136 |
137# Export curTick to user script. 138def curTick(): |
139 return internal.event.cvar.curTick |
140 141# register our C++ exit callback function with Python |
142atexit.register(internal.core.doExitCleanup) 143atexit.register(internal.stats.dump) |
144 145# This loops until all objects have been fully drained. 146def doDrain(root): 147 all_drained = drain(root) 148 while (not all_drained): 149 all_drained = drain(root) 150 151# Tries to drain all objects. Draining might not be completed unless 152# all objects return that they are drained on the first call. This is 153# because as objects drain they may cause other objects to no longer 154# be drained. 155def drain(root): 156 all_drained = False |
157 drain_event = internal.event.createCountedDrain() |
158 unready_objects = root.startDrain(drain_event, True) 159 # If we've got some objects that can't drain immediately, then simulate 160 if unready_objects > 0: 161 drain_event.setCount(unready_objects) 162 simulate() 163 else: 164 all_drained = True |
165 internal.event.cleanupCountedDrain(drain_event) |
166 return all_drained 167 168def resume(root): 169 root.resume() 170 171def checkpoint(root, dir): 172 if not isinstance(root, objects.Root): |
173 raise TypeError, "Checkpoint must be called on a root object." |
174 doDrain(root) 175 print "Writing checkpoint" |
176 internal.sim_object.serializeAll(dir) |
177 resume(root) 178 179def restoreCheckpoint(root, dir): 180 print "Restoring from checkpoint" |
181 internal.sim_object.unserializeAll(dir) 182 need_resume.append(root) |
183 184def changeToAtomic(system): 185 if not isinstance(system, objects.Root) and not isinstance(system, objects.System): 186 raise TypeError, "Object is not a root or system object. Checkpoint must be " 187 "called on a root object." 188 doDrain(system) 189 print "Changing memory mode to atomic" |
190 system.changeTiming(internal.sim_object.SimObject.Atomic) |
191 192def changeToTiming(system): 193 if not isinstance(system, objects.Root) and not isinstance(system, objects.System): 194 raise TypeError, "Object is not a root or system object. Checkpoint must be " 195 "called on a root object." 196 doDrain(system) 197 print "Changing memory mode to timing" |
198 system.changeTiming(internal.sim_object.SimObject.Timing) |
199 200def switchCpus(cpuList): 201 print "switching cpus" 202 if not isinstance(cpuList, list): 203 raise RuntimeError, "Must pass a list to this function" 204 for i in cpuList: 205 if not isinstance(i, tuple): 206 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)" 207 208 [old_cpus, new_cpus] = zip(*cpuList) 209 210 for cpu in old_cpus: 211 if not isinstance(cpu, objects.BaseCPU): 212 raise TypeError, "%s is not of type BaseCPU" % cpu 213 for cpu in new_cpus: 214 if not isinstance(cpu, objects.BaseCPU): 215 raise TypeError, "%s is not of type BaseCPU" % cpu 216 217 # Drain all of the individual CPUs |
218 drain_event = internal.event.createCountedDrain() |
219 unready_cpus = 0 220 for old_cpu in old_cpus: 221 unready_cpus += old_cpu.startDrain(drain_event, False) 222 # If we've got some objects that can't drain immediately, then simulate 223 if unready_cpus > 0: 224 drain_event.setCount(unready_cpus) 225 simulate() |
226 internal.event.cleanupCountedDrain(drain_event) |
227 # Now all of the CPUs are ready to be switched out 228 for old_cpu in old_cpus: 229 old_cpu._ccObject.switchOut() 230 index = 0 231 for new_cpu in new_cpus: 232 new_cpu.takeOverFrom(old_cpus[index]) 233 new_cpu._ccObject.resume() 234 index += 1 235 |
236def dumpStats(): 237 print 'Dumping stats' 238 internal.stats.dump() 239 240def resetStats(): 241 print 'Resetting stats' 242 internal.stats.reset() 243 |
244# Since we have so many mutual imports in this package, we should: 245# 1. Put all intra-package imports at the *bottom* of the file, unless 246# they're absolutely needed before that (for top-level statements 247# or class attributes). Imports of "trivial" packages that don't 248# import other packages (e.g., 'smartdict') can be at the top. 249# 2. Never use 'from foo import *' on an intra-package import since 250# you can get the wrong result if foo is only partially imported 251# at the point you do that (i.e., because foo is in the middle of 252# importing *you*). 253from main import options 254import objects 255import params 256from SimObject import resolveSimObject |