__init__.py revision 2969
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
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
472969Sktlim@umich.edudef makeList(objOrList):
482969Sktlim@umich.edu    if isinstance(objOrList, list):
492969Sktlim@umich.edu        return objOrList
502969Sktlim@umich.edu    return [objOrList]
512969Sktlim@umich.edu
522667Sstever@eecs.umich.edu# Prepend given directory to system module search path.  We may not
532667Sstever@eecs.umich.edu# need this anymore if we can structure our config library more like a
542667Sstever@eecs.umich.edu# Python package.
551692SN/Adef AddToPath(path):
561869SN/A    # if it's a relative path and we know what directory the current
571869SN/A    # python script is in, make the path relative to that directory.
581869SN/A    if not os.path.isabs(path) and sys.path[0]:
591869SN/A        path = os.path.join(sys.path[0], path)
601692SN/A    path = os.path.realpath(path)
611869SN/A    # sys.path[0] should always refer to the current script's directory,
621869SN/A    # so place the new dir right after that.
631869SN/A    sys.path.insert(1, path)
641581SN/A
651530SN/A# make a SmartDict out of the build options for our local use
661530SN/Aimport smartdict
671530SN/Abuild_env = smartdict.SmartDict()
682667Sstever@eecs.umich.edubuild_env.update(defines.m5_build_env)
691530SN/A
701530SN/A# make a SmartDict out of the OS environment too
711530SN/Aenv = smartdict.SmartDict()
721530SN/Aenv.update(os.environ)
731530SN/A
742738Sstever@eecs.umich.edu# Function to provide to C++ so it can look up instances based on paths
752738Sstever@eecs.umich.edudef resolveSimObject(name):
762738Sstever@eecs.umich.edu    obj = config.instanceDict[name]
772740Sstever@eecs.umich.edu    return obj.getCCObject()
782738Sstever@eecs.umich.edu
792889Sbinkertn@umich.edufrom main import options, arguments, main
802889Sbinkertn@umich.edu
812667Sstever@eecs.umich.edu# The final hook to generate .ini files.  Called from the user script
822667Sstever@eecs.umich.edu# once the config is built.
832667Sstever@eecs.umich.edudef instantiate(root):
842667Sstever@eecs.umich.edu    config.ticks_per_sec = float(root.clock.frequency)
852667Sstever@eecs.umich.edu    # ugly temporary hack to get output to config.ini
862762Sstever@eecs.umich.edu    sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w')
872667Sstever@eecs.umich.edu    root.print_ini()
882667Sstever@eecs.umich.edu    sys.stdout.close() # close config.ini
892667Sstever@eecs.umich.edu    sys.stdout = sys.__stdout__ # restore to original
902763Sstever@eecs.umich.edu    cc_main.loadIniFile(resolveSimObject)  # load config.ini into C++
912738Sstever@eecs.umich.edu    root.createCCObject()
922738Sstever@eecs.umich.edu    root.connectPorts()
932763Sstever@eecs.umich.edu    cc_main.finalInit()
942667Sstever@eecs.umich.edu    noDot = True # temporary until we fix dot
952667Sstever@eecs.umich.edu    if not noDot:
962667Sstever@eecs.umich.edu       dot = pydot.Dot()
972667Sstever@eecs.umich.edu       instance.outputDot(dot)
982667Sstever@eecs.umich.edu       dot.orientation = "portrait"
992667Sstever@eecs.umich.edu       dot.size = "8.5,11"
1002667Sstever@eecs.umich.edu       dot.ranksep="equally"
1012667Sstever@eecs.umich.edu       dot.rank="samerank"
1022667Sstever@eecs.umich.edu       dot.write("config.dot")
1032667Sstever@eecs.umich.edu       dot.write_ps("config.ps")
1041527SN/A
1052667Sstever@eecs.umich.edu# Export curTick to user script.
1062667Sstever@eecs.umich.edudef curTick():
1072763Sstever@eecs.umich.edu    return cc_main.cvar.curTick
1081511SN/A
1092667Sstever@eecs.umich.edu# register our C++ exit callback function with Python
1102763Sstever@eecs.umich.eduatexit.register(cc_main.doExitCleanup)
1112655Sstever@eecs.umich.edu
1122667Sstever@eecs.umich.edu# This import allows user scripts to reference 'm5.objects.Foo' after
1132667Sstever@eecs.umich.edu# just doing an 'import m5' (without an 'import m5.objects').  May not
1142667Sstever@eecs.umich.edu# matter since most scripts will probably 'from m5.objects import *'.
1152667Sstever@eecs.umich.eduimport objects
1162797Sktlim@umich.edu
1172860Sktlim@umich.edu# This loops until all objects have been fully drained.
1182839Sktlim@umich.edudef doDrain(root):
1192860Sktlim@umich.edu    all_drained = drain(root)
1202860Sktlim@umich.edu    while (not all_drained):
1212860Sktlim@umich.edu        all_drained = drain(root)
1222860Sktlim@umich.edu
1232860Sktlim@umich.edu# Tries to drain all objects.  Draining might not be completed unless
1242860Sktlim@umich.edu# all objects return that they are drained on the first call.  This is
1252860Sktlim@umich.edu# because as objects drain they may cause other objects to no longer
1262860Sktlim@umich.edu# be drained.
1272860Sktlim@umich.edudef drain(root):
1282860Sktlim@umich.edu    all_drained = False
1292839Sktlim@umich.edu    drain_event = cc_main.createCountedDrain()
1302839Sktlim@umich.edu    unready_objects = root.startDrain(drain_event, True)
1312839Sktlim@umich.edu    # If we've got some objects that can't drain immediately, then simulate
1322797Sktlim@umich.edu    if unready_objects > 0:
1332839Sktlim@umich.edu        drain_event.setCount(unready_objects)
1342797Sktlim@umich.edu        simulate()
1352860Sktlim@umich.edu    else:
1362860Sktlim@umich.edu        all_drained = True
1372839Sktlim@umich.edu    cc_main.cleanupCountedDrain(drain_event)
1382860Sktlim@umich.edu    return all_drained
1392797Sktlim@umich.edu
1402797Sktlim@umich.edudef resume(root):
1412797Sktlim@umich.edu    root.resume()
1422797Sktlim@umich.edu
1432868Sktlim@umich.edudef checkpoint(root, dir):
1442797Sktlim@umich.edu    if not isinstance(root, objects.Root):
1452797Sktlim@umich.edu        raise TypeError, "Object is not a root object. Checkpoint must be called on a root object."
1462839Sktlim@umich.edu    doDrain(root)
1472797Sktlim@umich.edu    print "Writing checkpoint"
1482868Sktlim@umich.edu    cc_main.serializeAll(dir)
1492797Sktlim@umich.edu    resume(root)
1502797Sktlim@umich.edu
1512868Sktlim@umich.edudef restoreCheckpoint(root, dir):
1522797Sktlim@umich.edu    print "Restoring from checkpoint"
1532868Sktlim@umich.edu    cc_main.unserializeAll(dir)
1542865Sktlim@umich.edu    resume(root)
1552797Sktlim@umich.edu
1562797Sktlim@umich.edudef changeToAtomic(system):
1572797Sktlim@umich.edu    if not isinstance(system, objects.Root) and not isinstance(system, System):
1582797Sktlim@umich.edu        raise TypeError, "Object is not a root or system object.  Checkpoint must be "
1592797Sktlim@umich.edu        "called on a root object."
1602839Sktlim@umich.edu    doDrain(system)
1612797Sktlim@umich.edu    print "Changing memory mode to atomic"
1622797Sktlim@umich.edu    system.changeTiming(cc_main.SimObject.Atomic)
1632797Sktlim@umich.edu    resume(system)
1642797Sktlim@umich.edu
1652797Sktlim@umich.edudef changeToTiming(system):
1662797Sktlim@umich.edu    if not isinstance(system, objects.Root) and not isinstance(system, System):
1672797Sktlim@umich.edu        raise TypeError, "Object is not a root or system object.  Checkpoint must be "
1682797Sktlim@umich.edu        "called on a root object."
1692839Sktlim@umich.edu    doDrain(system)
1702797Sktlim@umich.edu    print "Changing memory mode to timing"
1712797Sktlim@umich.edu    system.changeTiming(cc_main.SimObject.Timing)
1722797Sktlim@umich.edu    resume(system)
1732797Sktlim@umich.edu
1742797Sktlim@umich.edudef switchCpus(cpuList):
1752797Sktlim@umich.edu    if not isinstance(cpuList, list):
1762797Sktlim@umich.edu        raise RuntimeError, "Must pass a list to this function"
1772797Sktlim@umich.edu    for i in cpuList:
1782797Sktlim@umich.edu        if not isinstance(i, tuple):
1792797Sktlim@umich.edu            raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
1802797Sktlim@umich.edu
1812797Sktlim@umich.edu    [old_cpus, new_cpus] = zip(*cpuList)
1822797Sktlim@umich.edu
1832797Sktlim@umich.edu    for cpu in old_cpus:
1842797Sktlim@umich.edu        if not isinstance(cpu, objects.BaseCPU):
1852797Sktlim@umich.edu            raise TypeError, "%s is not of type BaseCPU", cpu
1862797Sktlim@umich.edu    for cpu in new_cpus:
1872797Sktlim@umich.edu        if not isinstance(cpu, objects.BaseCPU):
1882797Sktlim@umich.edu            raise TypeError, "%s is not of type BaseCPU", cpu
1892797Sktlim@umich.edu
1902839Sktlim@umich.edu    # Drain all of the individual CPUs
1912839Sktlim@umich.edu    drain_event = cc_main.createCountedDrain()
1922797Sktlim@umich.edu    unready_cpus = 0
1932797Sktlim@umich.edu    for old_cpu in old_cpus:
1942839Sktlim@umich.edu        unready_cpus += old_cpu.startDrain(drain_event, False)
1952839Sktlim@umich.edu    # If we've got some objects that can't drain immediately, then simulate
1962797Sktlim@umich.edu    if unready_cpus > 0:
1972839Sktlim@umich.edu        drain_event.setCount(unready_cpus)
1982797Sktlim@umich.edu        simulate()
1992839Sktlim@umich.edu    cc_main.cleanupCountedDrain(drain_event)
2002797Sktlim@umich.edu    # Now all of the CPUs are ready to be switched out
2012797Sktlim@umich.edu    for old_cpu in old_cpus:
2022797Sktlim@umich.edu        old_cpu._ccObject.switchOut()
2032797Sktlim@umich.edu    index = 0
2042797Sktlim@umich.edu    print "Switching CPUs"
2052797Sktlim@umich.edu    for new_cpu in new_cpus:
2062797Sktlim@umich.edu        new_cpu.takeOverFrom(old_cpus[index])
2072797Sktlim@umich.edu        new_cpu._ccObject.resume()
2082797Sktlim@umich.edu        index += 1
209