__init__.py revision 4165:7382285a50e7
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 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 48def panic(string): 49 print >>sys.stderr, 'panic:', string 50 sys.exit(1) 51 52# force scalars to one-element lists for uniformity 53def makeList(objOrList): 54 if isinstance(objOrList, list): 55 return objOrList 56 return [objOrList] 57 58# Prepend given directory to system module search path. We may not 59# need this anymore if we can structure our config library more like a 60# Python package. 61def AddToPath(path): 62 # if it's a relative path and we know what directory the current 63 # python script is in, make the path relative to that directory. 64 if not os.path.isabs(path) and sys.path[0]: 65 path = os.path.join(sys.path[0], path) 66 path = os.path.realpath(path) 67 # sys.path[0] should always refer to the current script's directory, 68 # so place the new dir right after that. 69 sys.path.insert(1, path) 70 71# make a SmartDict out of the build options for our local use 72import smartdict 73build_env = smartdict.SmartDict() 74build_env.update(defines.m5_build_env) 75 76# make a SmartDict out of the OS environment too 77env = smartdict.SmartDict() 78env.update(os.environ) 79 80# The final hook to generate .ini files. Called from the user script 81# once the config is built. 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# Python exit handlers happen in reverse order. We want to dump stats last. 142atexit.register(internal.stats.dump) 143 144# register our C++ exit callback function with Python 145atexit.register(internal.core.doExitCleanup) 146 147# This loops until all objects have been fully drained. 148def doDrain(root): 149 all_drained = drain(root) 150 while (not all_drained): 151 all_drained = drain(root) 152 153# Tries to drain all objects. Draining might not be completed unless 154# all objects return that they are drained on the first call. This is 155# because as objects drain they may cause other objects to no longer 156# be drained. 157def drain(root): 158 all_drained = False 159 drain_event = internal.event.createCountedDrain() 160 unready_objects = root.startDrain(drain_event, True) 161 # If we've got some objects that can't drain immediately, then simulate 162 if unready_objects > 0: 163 drain_event.setCount(unready_objects) 164 simulate() 165 else: 166 all_drained = True 167 internal.event.cleanupCountedDrain(drain_event) 168 return all_drained 169 170def resume(root): 171 root.resume() 172 173def checkpoint(root, dir): 174 if not isinstance(root, objects.Root): 175 raise TypeError, "Checkpoint must be called on a root object." 176 doDrain(root) 177 print "Writing checkpoint" 178 internal.sim_object.serializeAll(dir) 179 resume(root) 180 181def restoreCheckpoint(root, dir): 182 print "Restoring from checkpoint" 183 internal.sim_object.unserializeAll(dir) 184 need_resume.append(root) 185 186def changeToAtomic(system): 187 if not isinstance(system, objects.Root) and not isinstance(system, objects.System): 188 raise TypeError, "Object is not a root or system object. Checkpoint must be " 189 "called on a root object." 190 doDrain(system) 191 print "Changing memory mode to atomic" 192 system.changeTiming(internal.sim_object.SimObject.Atomic) 193 194def changeToTiming(system): 195 if not isinstance(system, objects.Root) and not isinstance(system, objects.System): 196 raise TypeError, "Object is not a root or system object. Checkpoint must be " 197 "called on a root object." 198 doDrain(system) 199 print "Changing memory mode to timing" 200 system.changeTiming(internal.sim_object.SimObject.Timing) 201 202def switchCpus(cpuList): 203 print "switching cpus" 204 if not isinstance(cpuList, list): 205 raise RuntimeError, "Must pass a list to this function" 206 for i in cpuList: 207 if not isinstance(i, tuple): 208 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)" 209 210 [old_cpus, new_cpus] = zip(*cpuList) 211 212 for cpu in old_cpus: 213 if not isinstance(cpu, objects.BaseCPU): 214 raise TypeError, "%s is not of type BaseCPU" % cpu 215 for cpu in new_cpus: 216 if not isinstance(cpu, objects.BaseCPU): 217 raise TypeError, "%s is not of type BaseCPU" % cpu 218 219 # Drain all of the individual CPUs 220 drain_event = internal.event.createCountedDrain() 221 unready_cpus = 0 222 for old_cpu in old_cpus: 223 unready_cpus += old_cpu.startDrain(drain_event, False) 224 # If we've got some objects that can't drain immediately, then simulate 225 if unready_cpus > 0: 226 drain_event.setCount(unready_cpus) 227 simulate() 228 internal.event.cleanupCountedDrain(drain_event) 229 # Now all of the CPUs are ready to be switched out 230 for old_cpu in old_cpus: 231 old_cpu._ccObject.switchOut() 232 index = 0 233 for new_cpu in new_cpus: 234 new_cpu.takeOverFrom(old_cpus[index]) 235 new_cpu._ccObject.resume() 236 index += 1 237 238def dumpStats(): 239 print 'Dumping stats' 240 internal.stats.dump() 241 242def resetStats(): 243 print 'Resetting stats' 244 internal.stats.reset() 245 246# Since we have so many mutual imports in this package, we should: 247# 1. Put all intra-package imports at the *bottom* of the file, unless 248# they're absolutely needed before that (for top-level statements 249# or class attributes). Imports of "trivial" packages that don't 250# import other packages (e.g., 'smartdict') can be at the top. 251# 2. Never use 'from foo import *' on an intra-package import since 252# you can get the wrong result if foo is only partially imported 253# at the point you do that (i.e., because foo is in the middle of 254# importing *you*). 255from main import options 256import objects 257import params 258from SimObject import resolveSimObject 259