__init__.py revision 2759
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 612667Sstever@eecs.umich.edu# Callback to set trace flags. Not necessarily the best way to do 622667Sstever@eecs.umich.edu# things in the long run (particularly if we change how these global 632667Sstever@eecs.umich.edu# options are handled). 642667Sstever@eecs.umich.edudef setTraceFlags(option, opt_str, value, parser): 652667Sstever@eecs.umich.edu objects.Trace.flags = value 662667Sstever@eecs.umich.edu 672728Sktlim@umich.edudef setTraceStart(option, opt_str, value, parser): 682728Sktlim@umich.edu objects.Trace.start = value 692728Sktlim@umich.edu 702759Sktlim@umich.edudef setTraceFile(option, opt_str, value, parser): 712759Sktlim@umich.edu objects.Trace.file = value 722728Sktlim@umich.edu 732759Sktlim@umich.edudef usePCSymbol(option, opt_str, value, parser): 742759Sktlim@umich.edu objects.ExecutionTrace.pc_symbol = value 752759Sktlim@umich.edu 762759Sktlim@umich.edudef printCycle(option, opt_str, value, parser): 772759Sktlim@umich.edu objects.ExecutionTrace.print_cycle = value 782759Sktlim@umich.edu 792759Sktlim@umich.edudef printOp(option, opt_str, value, parser): 802759Sktlim@umich.edu objects.ExecutionTrace.print_opclass = value 812759Sktlim@umich.edu 822759Sktlim@umich.edudef printThread(option, opt_str, value, parser): 832759Sktlim@umich.edu objects.ExecutionTrace.print_thread = value 842759Sktlim@umich.edu 852759Sktlim@umich.edudef printEA(option, opt_str, value, parser): 862759Sktlim@umich.edu objects.ExecutionTrace.print_effaddr = value 872759Sktlim@umich.edu 882759Sktlim@umich.edudef printData(option, opt_str, value, parser): 892759Sktlim@umich.edu objects.ExecutionTrace.print_data = value 902759Sktlim@umich.edu 912759Sktlim@umich.edudef printFetchseq(option, opt_str, value, parser): 922759Sktlim@umich.edu objects.ExecutionTrace.print_fetchseq = value 932759Sktlim@umich.edu 942759Sktlim@umich.edudef printCpseq(option, opt_str, value, parser): 952759Sktlim@umich.edu objects.ExecutionTrace.print_cpseq = value 962759Sktlim@umich.edu 972759Sktlim@umich.edudef dumpOnExit(option, opt_str, value, parser): 982759Sktlim@umich.edu objects.Trace.dump_on_exit = value 992759Sktlim@umich.edu 1002759Sktlim@umich.edudef debugBreak(option, opt_str, value, parser): 1012759Sktlim@umich.edu objects.Debug.break_cycles = value 1022728Sktlim@umich.edu 1032728Sktlim@umich.edudef statsTextFile(option, opt_str, value, parser): 1042728Sktlim@umich.edu objects.Statistics.text_file = value 1052728Sktlim@umich.edu 1062759Sktlim@umich.edu# Extra list to help for options that are true or false 1072759Sktlim@umich.eduTrueOrFalse = ['True', 'False'] 1082759Sktlim@umich.eduTorF = "True | False" 1092759Sktlim@umich.edu 1102667Sstever@eecs.umich.edu# Standard optparse options. Need to be explicitly included by the 1112667Sstever@eecs.umich.edu# user script when it calls optparse.OptionParser(). 1122667Sstever@eecs.umich.edustandardOptions = [ 1132667Sstever@eecs.umich.edu optparse.make_option("--traceflags", type="string", action="callback", 1142728Sktlim@umich.edu callback=setTraceFlags), 1152728Sktlim@umich.edu optparse.make_option("--tracestart", type="int", action="callback", 1162728Sktlim@umich.edu callback=setTraceStart), 1172759Sktlim@umich.edu optparse.make_option("--tracefile", type="string", action="callback", 1182759Sktlim@umich.edu callback=setTraceFile), 1192759Sktlim@umich.edu optparse.make_option("--pcsymbol", type="choice", choices=TrueOrFalse, 1202759Sktlim@umich.edu default="True", metavar=TorF, 1212759Sktlim@umich.edu action="callback", callback=usePCSymbol, 1222759Sktlim@umich.edu help="Use PC symbols in trace output"), 1232759Sktlim@umich.edu optparse.make_option("--printcycle", type="choice", choices=TrueOrFalse, 1242759Sktlim@umich.edu default="True", metavar=TorF, 1252759Sktlim@umich.edu action="callback", callback=printCycle, 1262759Sktlim@umich.edu help="Print cycle numbers in trace output"), 1272759Sktlim@umich.edu optparse.make_option("--printopclass", type="choice", 1282759Sktlim@umich.edu choices=TrueOrFalse, 1292759Sktlim@umich.edu default="True", metavar=TorF, 1302759Sktlim@umich.edu action="callback", callback=printOp, 1312759Sktlim@umich.edu help="Print cycle numbers in trace output"), 1322759Sktlim@umich.edu optparse.make_option("--printthread", type="choice", 1332759Sktlim@umich.edu choices=TrueOrFalse, 1342759Sktlim@umich.edu default="True", metavar=TorF, 1352759Sktlim@umich.edu action="callback", callback=printThread, 1362759Sktlim@umich.edu help="Print thread number in trace output"), 1372759Sktlim@umich.edu optparse.make_option("--printeffaddr", type="choice", 1382759Sktlim@umich.edu choices=TrueOrFalse, 1392759Sktlim@umich.edu default="True", metavar=TorF, 1402759Sktlim@umich.edu action="callback", callback=printEA, 1412759Sktlim@umich.edu help="Print effective address in trace output"), 1422759Sktlim@umich.edu optparse.make_option("--printdata", type="choice", 1432759Sktlim@umich.edu choices=TrueOrFalse, 1442759Sktlim@umich.edu default="True", metavar=TorF, 1452759Sktlim@umich.edu action="callback", callback=printData, 1462759Sktlim@umich.edu help="Print result data in trace output"), 1472759Sktlim@umich.edu optparse.make_option("--printfetchseq", type="choice", 1482759Sktlim@umich.edu choices=TrueOrFalse, 1492759Sktlim@umich.edu default="True", metavar=TorF, 1502759Sktlim@umich.edu action="callback", callback=printFetchseq, 1512759Sktlim@umich.edu help="Print fetch sequence numbers in trace output"), 1522759Sktlim@umich.edu optparse.make_option("--printcpseq", type="choice", 1532759Sktlim@umich.edu choices=TrueOrFalse, 1542759Sktlim@umich.edu default="True", metavar=TorF, 1552759Sktlim@umich.edu action="callback", callback=printCpseq, 1562759Sktlim@umich.edu help="Print correct path sequence numbers in trace output"), 1572759Sktlim@umich.edu optparse.make_option("--dumponexit", type="choice", 1582759Sktlim@umich.edu choices=TrueOrFalse, 1592759Sktlim@umich.edu default="True", metavar=TorF, 1602759Sktlim@umich.edu action="callback", callback=dumpOnExit, 1612759Sktlim@umich.edu help="Dump trace buffer on exit"), 1622759Sktlim@umich.edu optparse.make_option("--debugbreak", type="int", metavar="CYCLE", 1632759Sktlim@umich.edu action="callback", callback=debugBreak, 1642759Sktlim@umich.edu help="Cycle to create a breakpoint"), 1652728Sktlim@umich.edu optparse.make_option("--statsfile", type="string", action="callback", 1662728Sktlim@umich.edu callback=statsTextFile, metavar="FILE", 1672728Sktlim@umich.edu help="Sets the output file for the statistics") 1682667Sstever@eecs.umich.edu ] 1691530SN/A 1701530SN/A# make a SmartDict out of the build options for our local use 1711530SN/Aimport smartdict 1721530SN/Abuild_env = smartdict.SmartDict() 1732667Sstever@eecs.umich.edubuild_env.update(defines.m5_build_env) 1741530SN/A 1751530SN/A# make a SmartDict out of the OS environment too 1761530SN/Aenv = smartdict.SmartDict() 1771530SN/Aenv.update(os.environ) 1781530SN/A 1792738Sstever@eecs.umich.edu 1802738Sstever@eecs.umich.edu# Function to provide to C++ so it can look up instances based on paths 1812738Sstever@eecs.umich.edudef resolveSimObject(name): 1822738Sstever@eecs.umich.edu obj = config.instanceDict[name] 1832740Sstever@eecs.umich.edu return obj.getCCObject() 1842738Sstever@eecs.umich.edu 1852667Sstever@eecs.umich.edu# The final hook to generate .ini files. Called from the user script 1862667Sstever@eecs.umich.edu# once the config is built. 1872667Sstever@eecs.umich.edudef instantiate(root): 1882667Sstever@eecs.umich.edu config.ticks_per_sec = float(root.clock.frequency) 1892667Sstever@eecs.umich.edu # ugly temporary hack to get output to config.ini 1902667Sstever@eecs.umich.edu sys.stdout = file('config.ini', 'w') 1912667Sstever@eecs.umich.edu root.print_ini() 1922667Sstever@eecs.umich.edu sys.stdout.close() # close config.ini 1932667Sstever@eecs.umich.edu sys.stdout = sys.__stdout__ # restore to original 1942738Sstever@eecs.umich.edu main.loadIniFile(resolveSimObject) # load config.ini into C++ 1952738Sstever@eecs.umich.edu root.createCCObject() 1962738Sstever@eecs.umich.edu root.connectPorts() 1972738Sstever@eecs.umich.edu main.finalInit() 1982667Sstever@eecs.umich.edu noDot = True # temporary until we fix dot 1992667Sstever@eecs.umich.edu if not noDot: 2002667Sstever@eecs.umich.edu dot = pydot.Dot() 2012667Sstever@eecs.umich.edu instance.outputDot(dot) 2022667Sstever@eecs.umich.edu dot.orientation = "portrait" 2032667Sstever@eecs.umich.edu dot.size = "8.5,11" 2042667Sstever@eecs.umich.edu dot.ranksep="equally" 2052667Sstever@eecs.umich.edu dot.rank="samerank" 2062667Sstever@eecs.umich.edu dot.write("config.dot") 2072667Sstever@eecs.umich.edu dot.write_ps("config.ps") 2081527SN/A 2092667Sstever@eecs.umich.edu# Export curTick to user script. 2102667Sstever@eecs.umich.edudef curTick(): 2112667Sstever@eecs.umich.edu return main.cvar.curTick 2121511SN/A 2132667Sstever@eecs.umich.edu# register our C++ exit callback function with Python 2142667Sstever@eecs.umich.eduatexit.register(main.doExitCleanup) 2152655Sstever@eecs.umich.edu 2162667Sstever@eecs.umich.edu# This import allows user scripts to reference 'm5.objects.Foo' after 2172667Sstever@eecs.umich.edu# just doing an 'import m5' (without an 'import m5.objects'). May not 2182667Sstever@eecs.umich.edu# matter since most scripts will probably 'from m5.objects import *'. 2192667Sstever@eecs.umich.eduimport objects 220