__init__.py revision 3511
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
302889Sbinkertn@umich.eduimport atexit, os, sys
312655Sstever@eecs.umich.edu
322667Sstever@eecs.umich.edu# import the SWIG-wrapped main C++ functions
332763Sstever@eecs.umich.eduimport cc_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
372868Sktlim@umich.edufrom cc_main import simulate, SimLoopExitEvent
382655Sstever@eecs.umich.edu
392667Sstever@eecs.umich.edu# import the m5 compile options
402667Sstever@eecs.umich.eduimport defines
411530SN/A
423511Shsul@eecs.umich.edu# define a MaxTick parameter
433511Shsul@eecs.umich.eduMaxTick = 2**63 - 1
443511Shsul@eecs.umich.edu
451530SN/A# define this here so we can use it right away if necessary
461530SN/Adef panic(string):
471530SN/A    print >>sys.stderr, 'panic:', string
481530SN/A    sys.exit(1)
491530SN/A
503105Sstever@eecs.umich.edu# force scalars to one-element lists for uniformity
512969Sktlim@umich.edudef makeList(objOrList):
522969Sktlim@umich.edu    if isinstance(objOrList, list):
532969Sktlim@umich.edu        return objOrList
542969Sktlim@umich.edu    return [objOrList]
552969Sktlim@umich.edu
562667Sstever@eecs.umich.edu# Prepend given directory to system module search path.  We may not
572667Sstever@eecs.umich.edu# need this anymore if we can structure our config library more like a
582667Sstever@eecs.umich.edu# Python package.
591692SN/Adef AddToPath(path):
601869SN/A    # if it's a relative path and we know what directory the current
611869SN/A    # python script is in, make the path relative to that directory.
621869SN/A    if not os.path.isabs(path) and sys.path[0]:
631869SN/A        path = os.path.join(sys.path[0], path)
641692SN/A    path = os.path.realpath(path)
651869SN/A    # sys.path[0] should always refer to the current script's directory,
661869SN/A    # so place the new dir right after that.
671869SN/A    sys.path.insert(1, path)
681581SN/A
691530SN/A# make a SmartDict out of the build options for our local use
701530SN/Aimport smartdict
711530SN/Abuild_env = smartdict.SmartDict()
722667Sstever@eecs.umich.edubuild_env.update(defines.m5_build_env)
731530SN/A
741530SN/A# make a SmartDict out of the OS environment too
751530SN/Aenv = smartdict.SmartDict()
761530SN/Aenv.update(os.environ)
771530SN/A
782667Sstever@eecs.umich.edu# The final hook to generate .ini files.  Called from the user script
792667Sstever@eecs.umich.edu# once the config is built.
802667Sstever@eecs.umich.edudef instantiate(root):
813101Sstever@eecs.umich.edu    params.ticks_per_sec = float(root.clock.frequency)
823105Sstever@eecs.umich.edu    root.unproxy_all()
832667Sstever@eecs.umich.edu    # ugly temporary hack to get output to config.ini
842762Sstever@eecs.umich.edu    sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w')
852667Sstever@eecs.umich.edu    root.print_ini()
862667Sstever@eecs.umich.edu    sys.stdout.close() # close config.ini
872667Sstever@eecs.umich.edu    sys.stdout = sys.__stdout__ # restore to original
882763Sstever@eecs.umich.edu    cc_main.loadIniFile(resolveSimObject)  # load config.ini into C++
892738Sstever@eecs.umich.edu    root.createCCObject()
902738Sstever@eecs.umich.edu    root.connectPorts()
912763Sstever@eecs.umich.edu    cc_main.finalInit()
922667Sstever@eecs.umich.edu    noDot = True # temporary until we fix dot
932667Sstever@eecs.umich.edu    if not noDot:
942667Sstever@eecs.umich.edu       dot = pydot.Dot()
952667Sstever@eecs.umich.edu       instance.outputDot(dot)
962667Sstever@eecs.umich.edu       dot.orientation = "portrait"
972667Sstever@eecs.umich.edu       dot.size = "8.5,11"
982667Sstever@eecs.umich.edu       dot.ranksep="equally"
992667Sstever@eecs.umich.edu       dot.rank="samerank"
1002667Sstever@eecs.umich.edu       dot.write("config.dot")
1012667Sstever@eecs.umich.edu       dot.write_ps("config.ps")
1021527SN/A
1032667Sstever@eecs.umich.edu# Export curTick to user script.
1042667Sstever@eecs.umich.edudef curTick():
1052763Sstever@eecs.umich.edu    return cc_main.cvar.curTick
1061511SN/A
1072667Sstever@eecs.umich.edu# register our C++ exit callback function with Python
1082763Sstever@eecs.umich.eduatexit.register(cc_main.doExitCleanup)
1092655Sstever@eecs.umich.edu
1102860Sktlim@umich.edu# This loops until all objects have been fully drained.
1112839Sktlim@umich.edudef doDrain(root):
1122860Sktlim@umich.edu    all_drained = drain(root)
1132860Sktlim@umich.edu    while (not all_drained):
1142860Sktlim@umich.edu        all_drained = drain(root)
1152860Sktlim@umich.edu
1162860Sktlim@umich.edu# Tries to drain all objects.  Draining might not be completed unless
1172860Sktlim@umich.edu# all objects return that they are drained on the first call.  This is
1182860Sktlim@umich.edu# because as objects drain they may cause other objects to no longer
1192860Sktlim@umich.edu# be drained.
1202860Sktlim@umich.edudef drain(root):
1212860Sktlim@umich.edu    all_drained = False
1222839Sktlim@umich.edu    drain_event = cc_main.createCountedDrain()
1232839Sktlim@umich.edu    unready_objects = root.startDrain(drain_event, True)
1242839Sktlim@umich.edu    # If we've got some objects that can't drain immediately, then simulate
1252797Sktlim@umich.edu    if unready_objects > 0:
1262839Sktlim@umich.edu        drain_event.setCount(unready_objects)
1272797Sktlim@umich.edu        simulate()
1282860Sktlim@umich.edu    else:
1292860Sktlim@umich.edu        all_drained = True
1302839Sktlim@umich.edu    cc_main.cleanupCountedDrain(drain_event)
1312860Sktlim@umich.edu    return all_drained
1322797Sktlim@umich.edu
1332797Sktlim@umich.edudef resume(root):
1342797Sktlim@umich.edu    root.resume()
1352797Sktlim@umich.edu
1362868Sktlim@umich.edudef checkpoint(root, dir):
1372797Sktlim@umich.edu    if not isinstance(root, objects.Root):
1382797Sktlim@umich.edu        raise TypeError, "Object is not a root object. Checkpoint must be called on a root object."
1392839Sktlim@umich.edu    doDrain(root)
1402797Sktlim@umich.edu    print "Writing checkpoint"
1412868Sktlim@umich.edu    cc_main.serializeAll(dir)
1422797Sktlim@umich.edu    resume(root)
1432797Sktlim@umich.edu
1442868Sktlim@umich.edudef restoreCheckpoint(root, dir):
1452797Sktlim@umich.edu    print "Restoring from checkpoint"
1462868Sktlim@umich.edu    cc_main.unserializeAll(dir)
1472865Sktlim@umich.edu    resume(root)
1482797Sktlim@umich.edu
1492797Sktlim@umich.edudef changeToAtomic(system):
1503203Shsul@eecs.umich.edu    if not isinstance(system, objects.Root) and not isinstance(system, objects.System):
1512797Sktlim@umich.edu        raise TypeError, "Object is not a root or system object.  Checkpoint must be "
1522797Sktlim@umich.edu        "called on a root object."
1532839Sktlim@umich.edu    doDrain(system)
1542797Sktlim@umich.edu    print "Changing memory mode to atomic"
1552797Sktlim@umich.edu    system.changeTiming(cc_main.SimObject.Atomic)
1562797Sktlim@umich.edu
1572797Sktlim@umich.edudef changeToTiming(system):
1583203Shsul@eecs.umich.edu    if not isinstance(system, objects.Root) and not isinstance(system, objects.System):
1592797Sktlim@umich.edu        raise TypeError, "Object is not a root or system object.  Checkpoint must be "
1602797Sktlim@umich.edu        "called on a root object."
1612839Sktlim@umich.edu    doDrain(system)
1622797Sktlim@umich.edu    print "Changing memory mode to timing"
1632797Sktlim@umich.edu    system.changeTiming(cc_main.SimObject.Timing)
1642797Sktlim@umich.edu
1652797Sktlim@umich.edudef switchCpus(cpuList):
1663203Shsul@eecs.umich.edu    print "switching cpus"
1672797Sktlim@umich.edu    if not isinstance(cpuList, list):
1682797Sktlim@umich.edu        raise RuntimeError, "Must pass a list to this function"
1692797Sktlim@umich.edu    for i in cpuList:
1702797Sktlim@umich.edu        if not isinstance(i, tuple):
1712797Sktlim@umich.edu            raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
1722797Sktlim@umich.edu
1732797Sktlim@umich.edu    [old_cpus, new_cpus] = zip(*cpuList)
1742797Sktlim@umich.edu
1752797Sktlim@umich.edu    for cpu in old_cpus:
1762797Sktlim@umich.edu        if not isinstance(cpu, objects.BaseCPU):
1773504Sktlim@umich.edu            raise TypeError, "%s is not of type BaseCPU" % cpu
1782797Sktlim@umich.edu    for cpu in new_cpus:
1792797Sktlim@umich.edu        if not isinstance(cpu, objects.BaseCPU):
1803504Sktlim@umich.edu            raise TypeError, "%s is not of type BaseCPU" % cpu
1812797Sktlim@umich.edu
1822839Sktlim@umich.edu    # Drain all of the individual CPUs
1832839Sktlim@umich.edu    drain_event = cc_main.createCountedDrain()
1842797Sktlim@umich.edu    unready_cpus = 0
1852797Sktlim@umich.edu    for old_cpu in old_cpus:
1862839Sktlim@umich.edu        unready_cpus += old_cpu.startDrain(drain_event, False)
1872839Sktlim@umich.edu    # If we've got some objects that can't drain immediately, then simulate
1882797Sktlim@umich.edu    if unready_cpus > 0:
1892839Sktlim@umich.edu        drain_event.setCount(unready_cpus)
1902797Sktlim@umich.edu        simulate()
1912839Sktlim@umich.edu    cc_main.cleanupCountedDrain(drain_event)
1922797Sktlim@umich.edu    # Now all of the CPUs are ready to be switched out
1932797Sktlim@umich.edu    for old_cpu in old_cpus:
1942797Sktlim@umich.edu        old_cpu._ccObject.switchOut()
1952797Sktlim@umich.edu    index = 0
1962797Sktlim@umich.edu    for new_cpu in new_cpus:
1972797Sktlim@umich.edu        new_cpu.takeOverFrom(old_cpus[index])
1982797Sktlim@umich.edu        new_cpu._ccObject.resume()
1992797Sktlim@umich.edu        index += 1
2003101Sstever@eecs.umich.edu
2013101Sstever@eecs.umich.edu# Since we have so many mutual imports in this package, we should:
2023101Sstever@eecs.umich.edu# 1. Put all intra-package imports at the *bottom* of the file, unless
2033101Sstever@eecs.umich.edu#    they're absolutely needed before that (for top-level statements
2043101Sstever@eecs.umich.edu#    or class attributes).  Imports of "trivial" packages that don't
2053101Sstever@eecs.umich.edu#    import other packages (e.g., 'smartdict') can be at the top.
2063101Sstever@eecs.umich.edu# 2. Never use 'from foo import *' on an intra-package import since
2073101Sstever@eecs.umich.edu#    you can get the wrong result if foo is only partially imported
2083101Sstever@eecs.umich.edu#    at the point you do that (i.e., because foo is in the middle of
2093101Sstever@eecs.umich.edu#    importing *you*).
2103102Sstever@eecs.umich.edufrom main import options
2113101Sstever@eecs.umich.eduimport objects
2123101Sstever@eecs.umich.eduimport params
2133102Sstever@eecs.umich.edufrom SimObject import resolveSimObject
214