__init__.py revision 2767:d2e5e9a18fe5
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 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 main import simulate, SimLoopExitEvent 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# Callback to set trace flags. Not necessarily the best way to do 62# things in the long run (particularly if we change how these global 63# options are handled). 64def setTraceFlags(option, opt_str, value, parser): 65 objects.Trace.flags = value 66 67def setTraceStart(option, opt_str, value, parser): 68 objects.Trace.start = value 69 70def setTraceFile(option, opt_str, value, parser): 71 objects.Trace.file = value 72 73def noPCSymbol(option, opt_str, value, parser): 74 objects.ExecutionTrace.pc_symbol = False 75 76def noPrintCycle(option, opt_str, value, parser): 77 objects.ExecutionTrace.print_cycle = False 78 79def noPrintOpclass(option, opt_str, value, parser): 80 objects.ExecutionTrace.print_opclass = False 81 82def noPrintThread(option, opt_str, value, parser): 83 objects.ExecutionTrace.print_thread = False 84 85def noPrintEA(option, opt_str, value, parser): 86 objects.ExecutionTrace.print_effaddr = False 87 88def noPrintData(option, opt_str, value, parser): 89 objects.ExecutionTrace.print_data = False 90 91def printFetchseq(option, opt_str, value, parser): 92 objects.ExecutionTrace.print_fetchseq = True 93 94def printCpseq(option, opt_str, value, parser): 95 objects.ExecutionTrace.print_cpseq = True 96 97def dumpOnExit(option, opt_str, value, parser): 98 objects.Trace.dump_on_exit = True 99 100def debugBreak(option, opt_str, value, parser): 101 objects.Debug.break_cycles = value 102 103def statsTextFile(option, opt_str, value, parser): 104 objects.Statistics.text_file = value 105 106# Extra list to help for options that are true or false 107TrueOrFalse = ['True', 'False'] 108TorF = "True | False" 109 110# Standard optparse options. Need to be explicitly included by the 111# user script when it calls optparse.OptionParser(). 112standardOptions = [ 113 optparse.make_option("--traceflags", type="string", action="callback", 114 callback=setTraceFlags), 115 optparse.make_option("--tracestart", type="int", action="callback", 116 callback=setTraceStart), 117 optparse.make_option("--tracefile", type="string", action="callback", 118 callback=setTraceFile), 119 optparse.make_option("--nopcsymbol", 120 action="callback", callback=noPCSymbol, 121 help="Disable PC symbols in trace output"), 122 optparse.make_option("--noprintcycle", 123 action="callback", callback=noPrintCycle, 124 help="Don't print cycle numbers in trace output"), 125 optparse.make_option("--noprintopclass", 126 action="callback", callback=noPrintOpclass, 127 help="Don't print op class type in trace output"), 128 optparse.make_option("--noprintthread", 129 action="callback", callback=noPrintThread, 130 help="Don't print thread number in trace output"), 131 optparse.make_option("--noprinteffaddr", 132 action="callback", callback=noPrintEA, 133 help="Don't print effective address in trace output"), 134 optparse.make_option("--noprintdata", 135 action="callback", callback=noPrintData, 136 help="Don't print result data in trace output"), 137 optparse.make_option("--printfetchseq", 138 action="callback", callback=printFetchseq, 139 help="Print fetch sequence numbers in trace output"), 140 optparse.make_option("--printcpseq", 141 action="callback", callback=printCpseq, 142 help="Print correct path sequence numbers in trace output"), 143 optparse.make_option("--dumponexit", 144 action="callback", callback=dumpOnExit, 145 help="Dump trace buffer on exit"), 146 optparse.make_option("--debugbreak", type="int", metavar="CYCLE", 147 action="callback", callback=debugBreak, 148 help="Cycle to create a breakpoint"), 149 optparse.make_option("--statsfile", type="string", action="callback", 150 callback=statsTextFile, metavar="FILE", 151 help="Sets the output file for the statistics") 152 ] 153 154# make a SmartDict out of the build options for our local use 155import smartdict 156build_env = smartdict.SmartDict() 157build_env.update(defines.m5_build_env) 158 159# make a SmartDict out of the OS environment too 160env = smartdict.SmartDict() 161env.update(os.environ) 162 163 164# Function to provide to C++ so it can look up instances based on paths 165def resolveSimObject(name): 166 obj = config.instanceDict[name] 167 return obj.getCCObject() 168 169# The final hook to generate .ini files. Called from the user script 170# once the config is built. 171def instantiate(root): 172 config.ticks_per_sec = float(root.clock.frequency) 173 # ugly temporary hack to get output to config.ini 174 sys.stdout = file('config.ini', 'w') 175 root.print_ini() 176 sys.stdout.close() # close config.ini 177 sys.stdout = sys.__stdout__ # restore to original 178 main.loadIniFile(resolveSimObject) # load config.ini into C++ 179 root.createCCObject() 180 root.connectPorts() 181 main.finalInit() 182 noDot = True # temporary until we fix dot 183 if not noDot: 184 dot = pydot.Dot() 185 instance.outputDot(dot) 186 dot.orientation = "portrait" 187 dot.size = "8.5,11" 188 dot.ranksep="equally" 189 dot.rank="samerank" 190 dot.write("config.dot") 191 dot.write_ps("config.ps") 192 193# Export curTick to user script. 194def curTick(): 195 return main.cvar.curTick 196 197# register our C++ exit callback function with Python 198atexit.register(main.doExitCleanup) 199 200# This import allows user scripts to reference 'm5.objects.Foo' after 201# just doing an 'import m5' (without an 'import m5.objects'). May not 202# matter since most scripts will probably 'from m5.objects import *'. 203import objects 204