simulate.py revision 7525:722f2ad014a7
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 36import core 37import stats 38from main import options 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(): 50 root = objects.Root.getInstance() 51 52 if not root: 53 fatal("Need to instantiate Root() before calling instantiate()") 54 55 # we need to fix the global frequency 56 ticks.fixGlobalFrequency() 57 58 root.unproxy_all() 59 60 if options.dump_config: 61 ini_file = file(os.path.join(options.outdir, options.dump_config), 'w') 62 root.print_ini(ini_file) 63 ini_file.close() 64 65 # Initialize the global statistics 66 stats.initSimStats() 67 68 # Create the C++ sim objects and connect ports 69 root.createCCObject() 70 root.connectPorts() 71 72 # Do a second pass to finish initializing the sim objects 73 core.initAll() 74 75 # Do a third pass to initialize statistics 76 core.regAllStats() 77 78 # We're done registering statistics. Enable the stats package now. 79 stats.enable() 80 81 # Reset to put the stats in a consistent state. 82 stats.reset() 83 84def doDot(root): 85 dot = pydot.Dot() 86 instance.outputDot(dot) 87 dot.orientation = "portrait" 88 dot.size = "8.5,11" 89 dot.ranksep="equally" 90 dot.rank="samerank" 91 dot.write("config.dot") 92 dot.write_ps("config.ps") 93 94need_resume = [] 95need_startup = True 96def simulate(*args, **kwargs): 97 global need_resume, need_startup 98 99 if need_startup: 100 internal.core.startupAll() 101 need_startup = False 102 103 for root in need_resume: 104 resume(root) 105 need_resume = [] 106 107 return internal.event.simulate(*args, **kwargs) 108 109# Export curTick to user script. 110def curTick(): 111 return internal.core.cvar.curTick 112 113# Python exit handlers happen in reverse order. We want to dump stats last. 114atexit.register(internal.stats.dump) 115 116# register our C++ exit callback function with Python 117atexit.register(internal.core.doExitCleanup) 118 119# This loops until all objects have been fully drained. 120def doDrain(root): 121 all_drained = drain(root) 122 while (not all_drained): 123 all_drained = drain(root) 124 125# Tries to drain all objects. Draining might not be completed unless 126# all objects return that they are drained on the first call. This is 127# because as objects drain they may cause other objects to no longer 128# be drained. 129def drain(root): 130 all_drained = False 131 drain_event = internal.event.createCountedDrain() 132 unready_objects = root.startDrain(drain_event, True) 133 # If we've got some objects that can't drain immediately, then simulate 134 if unready_objects > 0: 135 drain_event.setCount(unready_objects) 136 simulate() 137 else: 138 all_drained = True 139 internal.event.cleanupCountedDrain(drain_event) 140 return all_drained 141 142def resume(root): 143 root.resume() 144 145def checkpoint(dir): 146 root = objects.Root.getInstance() 147 if not isinstance(root, objects.Root): 148 raise TypeError, "Checkpoint must be called on a root object." 149 doDrain(root) 150 print "Writing checkpoint" 151 internal.core.serializeAll(dir) 152 resume(root) 153 154def restoreCheckpoint(dir): 155 root = objects.Root.getInstance() 156 print "Restoring from checkpoint" 157 internal.core.unserializeAll(dir) 158 need_resume.append(root) 159 stats.reset() 160 161def changeToAtomic(system): 162 if not isinstance(system, (objects.Root, objects.System)): 163 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \ 164 (type(system), objects.Root, objects.System) 165 if system.getMemoryMode() != objects.params.atomic: 166 doDrain(system) 167 print "Changing memory mode to atomic" 168 system.changeTiming(objects.params.atomic) 169 170def changeToTiming(system): 171 if not isinstance(system, (objects.Root, objects.System)): 172 raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \ 173 (type(system), objects.Root, objects.System) 174 175 if system.getMemoryMode() != objects.params.timing: 176 doDrain(system) 177 print "Changing memory mode to timing" 178 system.changeTiming(objects.params.timing) 179 180def switchCpus(cpuList): 181 print "switching cpus" 182 if not isinstance(cpuList, list): 183 raise RuntimeError, "Must pass a list to this function" 184 for item in cpuList: 185 if not isinstance(item, tuple) or len(item) != 2: 186 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)" 187 188 for old_cpu, new_cpu in cpuList: 189 if not isinstance(old_cpu, objects.BaseCPU): 190 raise TypeError, "%s is not of type BaseCPU" % old_cpu 191 if not isinstance(new_cpu, objects.BaseCPU): 192 raise TypeError, "%s is not of type BaseCPU" % new_cpu 193 194 # Now all of the CPUs are ready to be switched out 195 for old_cpu, new_cpu in cpuList: 196 old_cpu._ccObject.switchOut() 197 198 for old_cpu, new_cpu in cpuList: 199 new_cpu.takeOverFrom(old_cpu) 200 201from internal.core import disableAllListeners 202