__init__.py revision 2865
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 sys, os, time, atexit, optparse 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, setCheckpointDir 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# Prepend given directory to system module search path. We may not 48# need this anymore if we can structure our config library more like a 49# Python package. 50def AddToPath(path): 51 # if it's a relative path and we know what directory the current 52 # python script is in, make the path relative to that directory. 53 if not os.path.isabs(path) and sys.path[0]: 54 path = os.path.join(sys.path[0], path) 55 path = os.path.realpath(path) 56 # sys.path[0] should always refer to the current script's directory, 57 # so place the new dir right after that. 58 sys.path.insert(1, path) 59 60 61# The m5 module's pointer to the parsed options object 62options = None 63 64 65# User should call this function after calling parse_args() to pass 66# parsed standard option values back into the m5 module for 67# processing. 68def setStandardOptions(_options): 69 # Set module global var 70 global options 71 options = _options 72 # tell C++ about output directory 73 cc_main.setOutputDir(options.outdir) 74 75# Callback to set trace flags. Not necessarily the best way to do 76# things in the long run (particularly if we change how these global 77# options are handled). 78def setTraceFlags(option, opt_str, value, parser): 79 objects.Trace.flags = value 80 81def setTraceStart(option, opt_str, value, parser): 82 objects.Trace.start = value 83 84def setTraceFile(option, opt_str, value, parser): 85 objects.Trace.file = value 86 87def noPCSymbol(option, opt_str, value, parser): 88 objects.ExecutionTrace.pc_symbol = False 89 90def noPrintCycle(option, opt_str, value, parser): 91 objects.ExecutionTrace.print_cycle = False 92 93def noPrintOpclass(option, opt_str, value, parser): 94 objects.ExecutionTrace.print_opclass = False 95 96def noPrintThread(option, opt_str, value, parser): 97 objects.ExecutionTrace.print_thread = False 98 99def noPrintEA(option, opt_str, value, parser): 100 objects.ExecutionTrace.print_effaddr = False 101 102def noPrintData(option, opt_str, value, parser): 103 objects.ExecutionTrace.print_data = False 104 105def printFetchseq(option, opt_str, value, parser): 106 objects.ExecutionTrace.print_fetchseq = True 107 108def printCpseq(option, opt_str, value, parser): 109 objects.ExecutionTrace.print_cpseq = True 110 111def dumpOnExit(option, opt_str, value, parser): 112 objects.Trace.dump_on_exit = True 113 114def debugBreak(option, opt_str, value, parser): 115 objects.Debug.break_cycles = value 116 117def statsTextFile(option, opt_str, value, parser): 118 objects.Statistics.text_file = value 119 120# Standard optparse options. Need to be explicitly included by the 121# user script when it calls optparse.OptionParser(). 122standardOptions = [ 123 optparse.make_option("--outdir", type="string", default="."), 124 optparse.make_option("--traceflags", type="string", action="callback", 125 callback=setTraceFlags), 126 optparse.make_option("--tracestart", type="int", action="callback", 127 callback=setTraceStart), 128 optparse.make_option("--tracefile", type="string", action="callback", 129 callback=setTraceFile), 130 optparse.make_option("--nopcsymbol", 131 action="callback", callback=noPCSymbol, 132 help="Disable PC symbols in trace output"), 133 optparse.make_option("--noprintcycle", 134 action="callback", callback=noPrintCycle, 135 help="Don't print cycle numbers in trace output"), 136 optparse.make_option("--noprintopclass", 137 action="callback", callback=noPrintOpclass, 138 help="Don't print op class type in trace output"), 139 optparse.make_option("--noprintthread", 140 action="callback", callback=noPrintThread, 141 help="Don't print thread number in trace output"), 142 optparse.make_option("--noprinteffaddr", 143 action="callback", callback=noPrintEA, 144 help="Don't print effective address in trace output"), 145 optparse.make_option("--noprintdata", 146 action="callback", callback=noPrintData, 147 help="Don't print result data in trace output"), 148 optparse.make_option("--printfetchseq", 149 action="callback", callback=printFetchseq, 150 help="Print fetch sequence numbers in trace output"), 151 optparse.make_option("--printcpseq", 152 action="callback", callback=printCpseq, 153 help="Print correct path sequence numbers in trace output"), 154 optparse.make_option("--dumponexit", 155 action="callback", callback=dumpOnExit, 156 help="Dump trace buffer on exit"), 157 optparse.make_option("--debugbreak", type="int", metavar="CYCLE", 158 action="callback", callback=debugBreak, 159 help="Cycle to create a breakpoint"), 160 optparse.make_option("--statsfile", type="string", action="callback", 161 callback=statsTextFile, metavar="FILE", 162 help="Sets the output file for the statistics") 163 ] 164 165# make a SmartDict out of the build options for our local use 166import smartdict 167build_env = smartdict.SmartDict() 168build_env.update(defines.m5_build_env) 169 170# make a SmartDict out of the OS environment too 171env = smartdict.SmartDict() 172env.update(os.environ) 173 174 175# Function to provide to C++ so it can look up instances based on paths 176def resolveSimObject(name): 177 obj = config.instanceDict[name] 178 return obj.getCCObject() 179 180# The final hook to generate .ini files. Called from the user script 181# once the config is built. 182def instantiate(root): 183 config.ticks_per_sec = float(root.clock.frequency) 184 # ugly temporary hack to get output to config.ini 185 sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w') 186 root.print_ini() 187 sys.stdout.close() # close config.ini 188 sys.stdout = sys.__stdout__ # restore to original 189 cc_main.loadIniFile(resolveSimObject) # load config.ini into C++ 190 root.createCCObject() 191 root.connectPorts() 192 cc_main.finalInit() 193 noDot = True # temporary until we fix dot 194 if not noDot: 195 dot = pydot.Dot() 196 instance.outputDot(dot) 197 dot.orientation = "portrait" 198 dot.size = "8.5,11" 199 dot.ranksep="equally" 200 dot.rank="samerank" 201 dot.write("config.dot") 202 dot.write_ps("config.ps") 203 204# Export curTick to user script. 205def curTick(): 206 return cc_main.cvar.curTick 207 208# register our C++ exit callback function with Python 209atexit.register(cc_main.doExitCleanup) 210 211# This import allows user scripts to reference 'm5.objects.Foo' after 212# just doing an 'import m5' (without an 'import m5.objects'). May not 213# matter since most scripts will probably 'from m5.objects import *'. 214import objects 215 216# This loops until all objects have been fully drained. 217def doDrain(root): 218 all_drained = drain(root) 219 while (not all_drained): 220 all_drained = drain(root) 221 222# Tries to drain all objects. Draining might not be completed unless 223# all objects return that they are drained on the first call. This is 224# because as objects drain they may cause other objects to no longer 225# be drained. 226def drain(root): 227 all_drained = False 228 drain_event = cc_main.createCountedDrain() 229 unready_objects = root.startDrain(drain_event, True) 230 # If we've got some objects that can't drain immediately, then simulate 231 if unready_objects > 0: 232 drain_event.setCount(unready_objects) 233 simulate() 234 else: 235 all_drained = True 236 cc_main.cleanupCountedDrain(drain_event) 237 return all_drained 238 239def resume(root): 240 root.resume() 241 242def checkpoint(root): 243 if not isinstance(root, objects.Root): 244 raise TypeError, "Object is not a root object. Checkpoint must be called on a root object." 245 doDrain(root) 246 print "Writing checkpoint" 247 cc_main.serializeAll() 248 resume(root) 249 250def restoreCheckpoint(root): 251 print "Restoring from checkpoint" 252 cc_main.unserializeAll() 253 resume(root) 254 255def changeToAtomic(system): 256 if not isinstance(system, objects.Root) and not isinstance(system, System): 257 raise TypeError, "Object is not a root or system object. Checkpoint must be " 258 "called on a root object." 259 doDrain(system) 260 print "Changing memory mode to atomic" 261 system.changeTiming(cc_main.SimObject.Atomic) 262 resume(system) 263 264def changeToTiming(system): 265 if not isinstance(system, objects.Root) and not isinstance(system, System): 266 raise TypeError, "Object is not a root or system object. Checkpoint must be " 267 "called on a root object." 268 doDrain(system) 269 print "Changing memory mode to timing" 270 system.changeTiming(cc_main.SimObject.Timing) 271 resume(system) 272 273def switchCpus(cpuList): 274 if not isinstance(cpuList, list): 275 raise RuntimeError, "Must pass a list to this function" 276 for i in cpuList: 277 if not isinstance(i, tuple): 278 raise RuntimeError, "List must have tuples of (oldCPU,newCPU)" 279 280 [old_cpus, new_cpus] = zip(*cpuList) 281 282 for cpu in old_cpus: 283 if not isinstance(cpu, objects.BaseCPU): 284 raise TypeError, "%s is not of type BaseCPU", cpu 285 for cpu in new_cpus: 286 if not isinstance(cpu, objects.BaseCPU): 287 raise TypeError, "%s is not of type BaseCPU", cpu 288 289 # Drain all of the individual CPUs 290 drain_event = cc_main.createCountedDrain() 291 unready_cpus = 0 292 for old_cpu in old_cpus: 293 unready_cpus += old_cpu.startDrain(drain_event, False) 294 # If we've got some objects that can't drain immediately, then simulate 295 if unready_cpus > 0: 296 drain_event.setCount(unready_cpus) 297 simulate() 298 cc_main.cleanupCountedDrain(drain_event) 299 # Now all of the CPUs are ready to be switched out 300 for old_cpu in old_cpus: 301 old_cpu._ccObject.switchOut() 302 index = 0 303 print "Switching CPUs" 304 for new_cpu in new_cpus: 305 new_cpu.takeOverFrom(old_cpus[index]) 306 new_cpu._ccObject.resume() 307 index += 1 308