__init__.py revision 2781:b689ee340f27
16019Shines@cs.fsu.edu# Copyright (c) 2005 The Regents of The University of Michigan
212509Schuan.zhu@arm.com# All rights reserved.
37093Sgblack@eecs.umich.edu#
47093Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
57093Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are
67093Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright
77093Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
87093Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
97093Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
107093Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution;
117093Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its
127093Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from
137093Sgblack@eecs.umich.edu# this software without specific prior written permission.
146019Shines@cs.fsu.edu#
156019Shines@cs.fsu.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166019Shines@cs.fsu.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176019Shines@cs.fsu.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
186019Shines@cs.fsu.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
196019Shines@cs.fsu.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206019Shines@cs.fsu.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
216019Shines@cs.fsu.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226019Shines@cs.fsu.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236019Shines@cs.fsu.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246019Shines@cs.fsu.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256019Shines@cs.fsu.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266019Shines@cs.fsu.edu#
276019Shines@cs.fsu.edu# Authors: Nathan Binkert
286019Shines@cs.fsu.edu#          Steve Reinhardt
296019Shines@cs.fsu.edu
306019Shines@cs.fsu.eduimport sys, os, time, atexit, optparse
316019Shines@cs.fsu.edu
326019Shines@cs.fsu.edu# import the SWIG-wrapped main C++ functions
336019Shines@cs.fsu.eduimport cc_main
346019Shines@cs.fsu.edu# import a few SWIG-wrapped items (those that are likely to be used
356019Shines@cs.fsu.edu# directly by user scripts) completely into this module for
366019Shines@cs.fsu.edu# convenience
376019Shines@cs.fsu.edufrom cc_main import simulate, SimLoopExitEvent
386019Shines@cs.fsu.edu
396019Shines@cs.fsu.edu# import the m5 compile options
406019Shines@cs.fsu.eduimport defines
416735Sgblack@eecs.umich.edu
426735Sgblack@eecs.umich.edu# define this here so we can use it right away if necessary
4310037SARM gem5 Developersdef panic(string):
4410037SARM gem5 Developers    print >>sys.stderr, 'panic:', string
456019Shines@cs.fsu.edu    sys.exit(1)
466019Shines@cs.fsu.edu
476019Shines@cs.fsu.edu# Prepend given directory to system module search path.  We may not
4811793Sbrandon.potter@amd.com# need this anymore if we can structure our config library more like a
4911793Sbrandon.potter@amd.com# Python package.
5010037SARM gem5 Developersdef AddToPath(path):
5110037SARM gem5 Developers    # if it's a relative path and we know what directory the current
5210037SARM gem5 Developers    # python script is in, make the path relative to that directory.
538229Snate@binkert.org    if not os.path.isabs(path) and sys.path[0]:
548229Snate@binkert.org        path = os.path.join(sys.path[0], path)
556019Shines@cs.fsu.edu    path = os.path.realpath(path)
568232Snate@binkert.org    # sys.path[0] should always refer to the current script's directory,
578782Sgblack@eecs.umich.edu    # so place the new dir right after that.
586019Shines@cs.fsu.edu    sys.path.insert(1, path)
596019Shines@cs.fsu.edu
606019Shines@cs.fsu.edu
616019Shines@cs.fsu.edu# The m5 module's pointer to the parsed options object
6210037SARM gem5 Developersoptions = None
6310037SARM gem5 Developers
6410037SARM gem5 Developers
6510037SARM gem5 Developers# User should call this function after calling parse_args() to pass
6610037SARM gem5 Developers# parsed standard option values back into the m5 module for
6710037SARM gem5 Developers# processing.
6810037SARM gem5 Developersdef setStandardOptions(_options):
6910037SARM gem5 Developers    # Set module global var
7010037SARM gem5 Developers    global options
7110037SARM gem5 Developers    options = _options
7210037SARM gem5 Developers    # tell C++ about output directory
7310037SARM gem5 Developers    cc_main.setOutputDir(options.outdir)
7410037SARM gem5 Developers
7510037SARM gem5 Developers# Callback to set trace flags.  Not necessarily the best way to do
7610037SARM gem5 Developers# things in the long run (particularly if we change how these global
7710037SARM gem5 Developers# options are handled).
7810037SARM gem5 Developersdef setTraceFlags(option, opt_str, value, parser):
7910037SARM gem5 Developers    objects.Trace.flags = value
8010037SARM gem5 Developers
8110037SARM gem5 Developersdef setTraceStart(option, opt_str, value, parser):
8210037SARM gem5 Developers    objects.Trace.start = value
8310037SARM gem5 Developers
8410037SARM gem5 Developersdef setTraceFile(option, opt_str, value, parser):
8510037SARM gem5 Developers    objects.Trace.file = value
8610037SARM gem5 Developers
8710037SARM gem5 Developersdef noPCSymbol(option, opt_str, value, parser):
8810037SARM gem5 Developers    objects.ExecutionTrace.pc_symbol = False
8910037SARM gem5 Developers
9010037SARM gem5 Developersdef noPrintCycle(option, opt_str, value, parser):
9110037SARM gem5 Developers    objects.ExecutionTrace.print_cycle = False
9210037SARM gem5 Developers
9310037SARM gem5 Developersdef noPrintOpclass(option, opt_str, value, parser):
9410037SARM gem5 Developers    objects.ExecutionTrace.print_opclass = False
9510037SARM gem5 Developers
9610037SARM gem5 Developersdef noPrintThread(option, opt_str, value, parser):
9710037SARM gem5 Developers    objects.ExecutionTrace.print_thread = False
9810037SARM gem5 Developers
9910037SARM gem5 Developersdef noPrintEA(option, opt_str, value, parser):
10010037SARM gem5 Developers    objects.ExecutionTrace.print_effaddr = False
10110037SARM gem5 Developers
1026019Shines@cs.fsu.edudef noPrintData(option, opt_str, value, parser):
10310037SARM gem5 Developers    objects.ExecutionTrace.print_data = False
10410037SARM gem5 Developers
10510037SARM gem5 Developersdef printFetchseq(option, opt_str, value, parser):
1066019Shines@cs.fsu.edu    objects.ExecutionTrace.print_fetchseq = True
10710037SARM gem5 Developers
10810037SARM gem5 Developersdef printCpseq(option, opt_str, value, parser):
10910037SARM gem5 Developers    objects.ExecutionTrace.print_cpseq = True
11010037SARM gem5 Developers
11110037SARM gem5 Developersdef dumpOnExit(option, opt_str, value, parser):
11210037SARM gem5 Developers    objects.Trace.dump_on_exit = True
11310037SARM gem5 Developers
11410037SARM gem5 Developersdef debugBreak(option, opt_str, value, parser):
11510037SARM gem5 Developers    objects.Debug.break_cycles = value
11610037SARM gem5 Developers
11710037SARM gem5 Developersdef statsTextFile(option, opt_str, value, parser):
11810037SARM gem5 Developers    objects.Statistics.text_file = value
11910037SARM gem5 Developers
12010037SARM gem5 Developers# Extra list to help for options that are true or false
12110037SARM gem5 DevelopersTrueOrFalse = ['True', 'False']
12210037SARM gem5 DevelopersTorF = "True | False"
12310037SARM gem5 Developers
12410037SARM gem5 Developers# Standard optparse options.  Need to be explicitly included by the
12510037SARM gem5 Developers# user script when it calls optparse.OptionParser().
12610037SARM gem5 DevelopersstandardOptions = [
12710037SARM gem5 Developers    optparse.make_option("--outdir", type="string", default="."),
12810037SARM gem5 Developers    optparse.make_option("--traceflags", type="string", action="callback",
12910037SARM gem5 Developers                         callback=setTraceFlags),
13010037SARM gem5 Developers    optparse.make_option("--tracestart", type="int", action="callback",
13110037SARM gem5 Developers                         callback=setTraceStart),
13210037SARM gem5 Developers    optparse.make_option("--tracefile", type="string", action="callback",
13310037SARM gem5 Developers                         callback=setTraceFile),
13410037SARM gem5 Developers    optparse.make_option("--nopcsymbol",
13510037SARM gem5 Developers                         action="callback", callback=noPCSymbol,
13610037SARM gem5 Developers                         help="Disable PC symbols in trace output"),
13710037SARM gem5 Developers    optparse.make_option("--noprintcycle",
13810037SARM gem5 Developers                         action="callback", callback=noPrintCycle,
13910037SARM gem5 Developers                         help="Don't print cycle numbers in trace output"),
14010037SARM gem5 Developers    optparse.make_option("--noprintopclass",
14110037SARM gem5 Developers                         action="callback", callback=noPrintOpclass,
14210037SARM gem5 Developers                         help="Don't print op class type in trace output"),
14310037SARM gem5 Developers    optparse.make_option("--noprintthread",
14410037SARM gem5 Developers                         action="callback", callback=noPrintThread,
14510037SARM gem5 Developers                         help="Don't print thread number in trace output"),
14610037SARM gem5 Developers    optparse.make_option("--noprinteffaddr",
1476019Shines@cs.fsu.edu                         action="callback", callback=noPrintEA,
14810037SARM gem5 Developers                         help="Don't print effective address in trace output"),
14910037SARM gem5 Developers    optparse.make_option("--noprintdata",
15010037SARM gem5 Developers                         action="callback", callback=noPrintData,
1516019Shines@cs.fsu.edu                         help="Don't print result data in trace output"),
15210037SARM gem5 Developers    optparse.make_option("--printfetchseq",
15310037SARM gem5 Developers                         action="callback", callback=printFetchseq,
15410037SARM gem5 Developers                         help="Print fetch sequence numbers in trace output"),
15510037SARM gem5 Developers    optparse.make_option("--printcpseq",
15610037SARM gem5 Developers                         action="callback", callback=printCpseq,
15710037SARM gem5 Developers                         help="Print correct path sequence numbers in trace output"),
15810037SARM gem5 Developers    optparse.make_option("--dumponexit",
15910037SARM gem5 Developers                         action="callback", callback=dumpOnExit,
16010037SARM gem5 Developers                         help="Dump trace buffer on exit"),
16110037SARM gem5 Developers    optparse.make_option("--debugbreak", type="int", metavar="CYCLE",
16210037SARM gem5 Developers                         action="callback", callback=debugBreak,
16310037SARM gem5 Developers                         help="Cycle to create a breakpoint"),
16410037SARM gem5 Developers    optparse.make_option("--statsfile", type="string", action="callback",
16510037SARM gem5 Developers                         callback=statsTextFile, metavar="FILE",
16610037SARM gem5 Developers                         help="Sets the output file for the statistics")
16710037SARM gem5 Developers    ]
16810037SARM gem5 Developers
16910037SARM gem5 Developers# make a SmartDict out of the build options for our local use
17010037SARM gem5 Developersimport smartdict
17110037SARM gem5 Developersbuild_env = smartdict.SmartDict()
17210037SARM gem5 Developersbuild_env.update(defines.m5_build_env)
17310037SARM gem5 Developers
17410037SARM gem5 Developers# make a SmartDict out of the OS environment too
17510037SARM gem5 Developersenv = smartdict.SmartDict()
17610037SARM gem5 Developersenv.update(os.environ)
17710037SARM gem5 Developers
17810037SARM gem5 Developers
17910037SARM gem5 Developers# Function to provide to C++ so it can look up instances based on paths
18010037SARM gem5 Developersdef resolveSimObject(name):
18110037SARM gem5 Developers    obj = config.instanceDict[name]
18210037SARM gem5 Developers    return obj.getCCObject()
18310037SARM gem5 Developers
18410037SARM gem5 Developers# The final hook to generate .ini files.  Called from the user script
18510037SARM gem5 Developers# once the config is built.
18610037SARM gem5 Developersdef instantiate(root):
18710037SARM gem5 Developers    config.ticks_per_sec = float(root.clock.frequency)
18810037SARM gem5 Developers    # ugly temporary hack to get output to config.ini
18910037SARM gem5 Developers    sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w')
19010037SARM gem5 Developers    root.print_ini()
19110037SARM gem5 Developers    sys.stdout.close() # close config.ini
19210037SARM gem5 Developers    sys.stdout = sys.__stdout__ # restore to original
1936019Shines@cs.fsu.edu    cc_main.loadIniFile(resolveSimObject)  # load config.ini into C++
19410037SARM gem5 Developers    root.createCCObject()
19510037SARM gem5 Developers    root.connectPorts()
19610037SARM gem5 Developers    cc_main.finalInit()
1976019Shines@cs.fsu.edu    noDot = True # temporary until we fix dot
19810037SARM gem5 Developers    if not noDot:
19910037SARM gem5 Developers       dot = pydot.Dot()
20010037SARM gem5 Developers       instance.outputDot(dot)
20110037SARM gem5 Developers       dot.orientation = "portrait"
20210037SARM gem5 Developers       dot.size = "8.5,11"
20310037SARM gem5 Developers       dot.ranksep="equally"
20410037SARM gem5 Developers       dot.rank="samerank"
20510037SARM gem5 Developers       dot.write("config.dot")
20610037SARM gem5 Developers       dot.write_ps("config.ps")
20710037SARM gem5 Developers
20810037SARM gem5 Developers# Export curTick to user script.
20910037SARM gem5 Developersdef curTick():
21010037SARM gem5 Developers    return cc_main.cvar.curTick
21110037SARM gem5 Developers
21210037SARM gem5 Developers# register our C++ exit callback function with Python
21310037SARM gem5 Developersatexit.register(cc_main.doExitCleanup)
21410037SARM gem5 Developers
21510037SARM gem5 Developers# This import allows user scripts to reference 'm5.objects.Foo' after
21610037SARM gem5 Developers# just doing an 'import m5' (without an 'import m5.objects').  May not
21710037SARM gem5 Developers# matter since most scripts will probably 'from m5.objects import *'.
21810037SARM gem5 Developersimport objects
21910037SARM gem5 Developers