__init__.py revision 2762
11736SN/A# Copyright (c) 2005 The Regents of The University of Michigan 21736SN/A# All rights reserved. 31736SN/A# 41736SN/A# Redistribution and use in source and binary forms, with or without 51736SN/A# modification, are permitted provided that the following conditions are 61736SN/A# met: redistributions of source code must retain the above copyright 71736SN/A# notice, this list of conditions and the following disclaimer; 81736SN/A# redistributions in binary form must reproduce the above copyright 91736SN/A# notice, this list of conditions and the following disclaimer in the 101736SN/A# documentation and/or other materials provided with the distribution; 111736SN/A# neither the name of the copyright holders nor the names of its 121736SN/A# contributors may be used to endorse or promote products derived from 131736SN/A# this software without specific prior written permission. 141736SN/A# 151736SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 161736SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 171736SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 181736SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 191736SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 201736SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 211736SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 221736SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 231736SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 241736SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 251736SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 262665Ssaidi@eecs.umich.edu# 272665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert 282665Ssaidi@eecs.umich.edu# Steve Reinhardt 291736SN/A 302667Sstever@eecs.umich.eduimport sys, os, time, atexit, optparse 312655Sstever@eecs.umich.edu 322667Sstever@eecs.umich.edu# import the SWIG-wrapped main C++ functions 332667Sstever@eecs.umich.eduimport main 342667Sstever@eecs.umich.edu# import a few SWIG-wrapped items (those that are likely to be used 352667Sstever@eecs.umich.edu# directly by user scripts) completely into this module for 362667Sstever@eecs.umich.edu# convenience 372667Sstever@eecs.umich.edufrom main import simulate, SimLoopExitEvent 382655Sstever@eecs.umich.edu 392667Sstever@eecs.umich.edu# import the m5 compile options 402667Sstever@eecs.umich.eduimport defines 411530SN/A 421530SN/A# define this here so we can use it right away if necessary 431530SN/Adef panic(string): 441530SN/A print >>sys.stderr, 'panic:', string 451530SN/A sys.exit(1) 461530SN/A 472667Sstever@eecs.umich.edu# Prepend given directory to system module search path. We may not 482667Sstever@eecs.umich.edu# need this anymore if we can structure our config library more like a 492667Sstever@eecs.umich.edu# Python package. 501692SN/Adef AddToPath(path): 511869SN/A # if it's a relative path and we know what directory the current 521869SN/A # python script is in, make the path relative to that directory. 531869SN/A if not os.path.isabs(path) and sys.path[0]: 541869SN/A path = os.path.join(sys.path[0], path) 551692SN/A path = os.path.realpath(path) 561869SN/A # sys.path[0] should always refer to the current script's directory, 571869SN/A # so place the new dir right after that. 581869SN/A sys.path.insert(1, path) 591581SN/A 602667Sstever@eecs.umich.edu 612762Sstever@eecs.umich.edu# The m5 module's pointer to the parsed options object 622762Sstever@eecs.umich.eduoptions = None 632762Sstever@eecs.umich.edu 642762Sstever@eecs.umich.edu 652762Sstever@eecs.umich.edu# User should call this function after calling parse_args() to pass 662762Sstever@eecs.umich.edu# parsed standard option values back into the m5 module for 672762Sstever@eecs.umich.edu# processing. 682762Sstever@eecs.umich.edudef setStandardOptions(_options): 692762Sstever@eecs.umich.edu # Set module global var 702762Sstever@eecs.umich.edu global options 712762Sstever@eecs.umich.edu options = _options 722762Sstever@eecs.umich.edu # tell C++ about output directory 732762Sstever@eecs.umich.edu main.setOutputDir(options.outdir) 742762Sstever@eecs.umich.edu 752667Sstever@eecs.umich.edu# Callback to set trace flags. Not necessarily the best way to do 762667Sstever@eecs.umich.edu# things in the long run (particularly if we change how these global 772667Sstever@eecs.umich.edu# options are handled). 782667Sstever@eecs.umich.edudef setTraceFlags(option, opt_str, value, parser): 792667Sstever@eecs.umich.edu objects.Trace.flags = value 802667Sstever@eecs.umich.edu 812728Sktlim@umich.edudef setTraceStart(option, opt_str, value, parser): 822728Sktlim@umich.edu objects.Trace.start = value 832728Sktlim@umich.edu 842759Sktlim@umich.edudef setTraceFile(option, opt_str, value, parser): 852759Sktlim@umich.edu objects.Trace.file = value 862728Sktlim@umich.edu 872759Sktlim@umich.edudef usePCSymbol(option, opt_str, value, parser): 882759Sktlim@umich.edu objects.ExecutionTrace.pc_symbol = value 892759Sktlim@umich.edu 902759Sktlim@umich.edudef printCycle(option, opt_str, value, parser): 912759Sktlim@umich.edu objects.ExecutionTrace.print_cycle = value 922759Sktlim@umich.edu 932759Sktlim@umich.edudef printOp(option, opt_str, value, parser): 942759Sktlim@umich.edu objects.ExecutionTrace.print_opclass = value 952759Sktlim@umich.edu 962759Sktlim@umich.edudef printThread(option, opt_str, value, parser): 972759Sktlim@umich.edu objects.ExecutionTrace.print_thread = value 982759Sktlim@umich.edu 992759Sktlim@umich.edudef printEA(option, opt_str, value, parser): 1002759Sktlim@umich.edu objects.ExecutionTrace.print_effaddr = value 1012759Sktlim@umich.edu 1022759Sktlim@umich.edudef printData(option, opt_str, value, parser): 1032759Sktlim@umich.edu objects.ExecutionTrace.print_data = value 1042759Sktlim@umich.edu 1052759Sktlim@umich.edudef printFetchseq(option, opt_str, value, parser): 1062759Sktlim@umich.edu objects.ExecutionTrace.print_fetchseq = value 1072759Sktlim@umich.edu 1082759Sktlim@umich.edudef printCpseq(option, opt_str, value, parser): 1092759Sktlim@umich.edu objects.ExecutionTrace.print_cpseq = value 1102759Sktlim@umich.edu 1112759Sktlim@umich.edudef dumpOnExit(option, opt_str, value, parser): 1122759Sktlim@umich.edu objects.Trace.dump_on_exit = value 1132759Sktlim@umich.edu 1142759Sktlim@umich.edudef debugBreak(option, opt_str, value, parser): 1152759Sktlim@umich.edu objects.Debug.break_cycles = value 1162728Sktlim@umich.edu 1172728Sktlim@umich.edudef statsTextFile(option, opt_str, value, parser): 1182728Sktlim@umich.edu objects.Statistics.text_file = value 1192728Sktlim@umich.edu 1202759Sktlim@umich.edu# Extra list to help for options that are true or false 1212759Sktlim@umich.eduTrueOrFalse = ['True', 'False'] 1222759Sktlim@umich.eduTorF = "True | False" 1232759Sktlim@umich.edu 1242667Sstever@eecs.umich.edu# Standard optparse options. Need to be explicitly included by the 1252667Sstever@eecs.umich.edu# user script when it calls optparse.OptionParser(). 1262667Sstever@eecs.umich.edustandardOptions = [ 1272762Sstever@eecs.umich.edu optparse.make_option("--outdir", type="string", default="."), 1282667Sstever@eecs.umich.edu optparse.make_option("--traceflags", type="string", action="callback", 1292728Sktlim@umich.edu callback=setTraceFlags), 1302728Sktlim@umich.edu optparse.make_option("--tracestart", type="int", action="callback", 1312728Sktlim@umich.edu callback=setTraceStart), 1322759Sktlim@umich.edu optparse.make_option("--tracefile", type="string", action="callback", 1332759Sktlim@umich.edu callback=setTraceFile), 1342759Sktlim@umich.edu optparse.make_option("--pcsymbol", type="choice", choices=TrueOrFalse, 1352759Sktlim@umich.edu default="True", metavar=TorF, 1362759Sktlim@umich.edu action="callback", callback=usePCSymbol, 1372759Sktlim@umich.edu help="Use PC symbols in trace output"), 1382759Sktlim@umich.edu optparse.make_option("--printcycle", type="choice", choices=TrueOrFalse, 1392759Sktlim@umich.edu default="True", metavar=TorF, 1402759Sktlim@umich.edu action="callback", callback=printCycle, 1412759Sktlim@umich.edu help="Print cycle numbers in trace output"), 1422759Sktlim@umich.edu optparse.make_option("--printopclass", type="choice", 1432759Sktlim@umich.edu choices=TrueOrFalse, 1442759Sktlim@umich.edu default="True", metavar=TorF, 1452759Sktlim@umich.edu action="callback", callback=printOp, 1462759Sktlim@umich.edu help="Print cycle numbers in trace output"), 1472759Sktlim@umich.edu optparse.make_option("--printthread", type="choice", 1482759Sktlim@umich.edu choices=TrueOrFalse, 1492759Sktlim@umich.edu default="True", metavar=TorF, 1502759Sktlim@umich.edu action="callback", callback=printThread, 1512759Sktlim@umich.edu help="Print thread number in trace output"), 1522759Sktlim@umich.edu optparse.make_option("--printeffaddr", type="choice", 1532759Sktlim@umich.edu choices=TrueOrFalse, 1542759Sktlim@umich.edu default="True", metavar=TorF, 1552759Sktlim@umich.edu action="callback", callback=printEA, 1562759Sktlim@umich.edu help="Print effective address in trace output"), 1572759Sktlim@umich.edu optparse.make_option("--printdata", type="choice", 1582759Sktlim@umich.edu choices=TrueOrFalse, 1592759Sktlim@umich.edu default="True", metavar=TorF, 1602759Sktlim@umich.edu action="callback", callback=printData, 1612759Sktlim@umich.edu help="Print result data in trace output"), 1622759Sktlim@umich.edu optparse.make_option("--printfetchseq", type="choice", 1632759Sktlim@umich.edu choices=TrueOrFalse, 1642759Sktlim@umich.edu default="True", metavar=TorF, 1652759Sktlim@umich.edu action="callback", callback=printFetchseq, 1662759Sktlim@umich.edu help="Print fetch sequence numbers in trace output"), 1672759Sktlim@umich.edu optparse.make_option("--printcpseq", type="choice", 1682759Sktlim@umich.edu choices=TrueOrFalse, 1692759Sktlim@umich.edu default="True", metavar=TorF, 1702759Sktlim@umich.edu action="callback", callback=printCpseq, 1712759Sktlim@umich.edu help="Print correct path sequence numbers in trace output"), 1722759Sktlim@umich.edu optparse.make_option("--dumponexit", type="choice", 1732759Sktlim@umich.edu choices=TrueOrFalse, 1742759Sktlim@umich.edu default="True", metavar=TorF, 1752759Sktlim@umich.edu action="callback", callback=dumpOnExit, 1762759Sktlim@umich.edu help="Dump trace buffer on exit"), 1772759Sktlim@umich.edu optparse.make_option("--debugbreak", type="int", metavar="CYCLE", 1782759Sktlim@umich.edu action="callback", callback=debugBreak, 1792759Sktlim@umich.edu help="Cycle to create a breakpoint"), 1802728Sktlim@umich.edu optparse.make_option("--statsfile", type="string", action="callback", 1812728Sktlim@umich.edu callback=statsTextFile, metavar="FILE", 1822728Sktlim@umich.edu help="Sets the output file for the statistics") 1832667Sstever@eecs.umich.edu ] 1841530SN/A 1851530SN/A# make a SmartDict out of the build options for our local use 1861530SN/Aimport smartdict 1871530SN/Abuild_env = smartdict.SmartDict() 1882667Sstever@eecs.umich.edubuild_env.update(defines.m5_build_env) 1891530SN/A 1901530SN/A# make a SmartDict out of the OS environment too 1911530SN/Aenv = smartdict.SmartDict() 1921530SN/Aenv.update(os.environ) 1931530SN/A 1942738Sstever@eecs.umich.edu 1952738Sstever@eecs.umich.edu# Function to provide to C++ so it can look up instances based on paths 1962738Sstever@eecs.umich.edudef resolveSimObject(name): 1972738Sstever@eecs.umich.edu obj = config.instanceDict[name] 1982740Sstever@eecs.umich.edu return obj.getCCObject() 1992738Sstever@eecs.umich.edu 2002667Sstever@eecs.umich.edu# The final hook to generate .ini files. Called from the user script 2012667Sstever@eecs.umich.edu# once the config is built. 2022667Sstever@eecs.umich.edudef instantiate(root): 2032667Sstever@eecs.umich.edu config.ticks_per_sec = float(root.clock.frequency) 2042667Sstever@eecs.umich.edu # ugly temporary hack to get output to config.ini 2052762Sstever@eecs.umich.edu sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w') 2062667Sstever@eecs.umich.edu root.print_ini() 2072667Sstever@eecs.umich.edu sys.stdout.close() # close config.ini 2082667Sstever@eecs.umich.edu sys.stdout = sys.__stdout__ # restore to original 2092738Sstever@eecs.umich.edu main.loadIniFile(resolveSimObject) # load config.ini into C++ 2102738Sstever@eecs.umich.edu root.createCCObject() 2112738Sstever@eecs.umich.edu root.connectPorts() 2122738Sstever@eecs.umich.edu main.finalInit() 2132667Sstever@eecs.umich.edu noDot = True # temporary until we fix dot 2142667Sstever@eecs.umich.edu if not noDot: 2152667Sstever@eecs.umich.edu dot = pydot.Dot() 2162667Sstever@eecs.umich.edu instance.outputDot(dot) 2172667Sstever@eecs.umich.edu dot.orientation = "portrait" 2182667Sstever@eecs.umich.edu dot.size = "8.5,11" 2192667Sstever@eecs.umich.edu dot.ranksep="equally" 2202667Sstever@eecs.umich.edu dot.rank="samerank" 2212667Sstever@eecs.umich.edu dot.write("config.dot") 2222667Sstever@eecs.umich.edu dot.write_ps("config.ps") 2231527SN/A 2242667Sstever@eecs.umich.edu# Export curTick to user script. 2252667Sstever@eecs.umich.edudef curTick(): 2262667Sstever@eecs.umich.edu return main.cvar.curTick 2271511SN/A 2282667Sstever@eecs.umich.edu# register our C++ exit callback function with Python 2292667Sstever@eecs.umich.eduatexit.register(main.doExitCleanup) 2302655Sstever@eecs.umich.edu 2312667Sstever@eecs.umich.edu# This import allows user scripts to reference 'm5.objects.Foo' after 2322667Sstever@eecs.umich.edu# just doing an 'import m5' (without an 'import m5.objects'). May not 2332667Sstever@eecs.umich.edu# matter since most scripts will probably 'from m5.objects import *'. 2342667Sstever@eecs.umich.eduimport objects 235