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