__init__.py revision 3105
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
473105Sstever@eecs.umich.edu# force scalars to one-element lists for uniformity
482969Sktlim@umich.edudef makeList(objOrList):
492969Sktlim@umich.edu    if isinstance(objOrList, list):
502969Sktlim@umich.edu        return objOrList
512969Sktlim@umich.edu    return [objOrList]
522969Sktlim@umich.edu
532667Sstever@eecs.umich.edu# Prepend given directory to system module search path.  We may not
542667Sstever@eecs.umich.edu# need this anymore if we can structure our config library more like a
552667Sstever@eecs.umich.edu# Python package.
561692SN/Adef AddToPath(path):
571869SN/A    # if it's a relative path and we know what directory the current
581869SN/A    # python script is in, make the path relative to that directory.
591869SN/A    if not os.path.isabs(path) and sys.path[0]:
601869SN/A        path = os.path.join(sys.path[0], path)
611692SN/A    path = os.path.realpath(path)
621869SN/A    # sys.path[0] should always refer to the current script's directory,
631869SN/A    # so place the new dir right after that.
641869SN/A    sys.path.insert(1, path)
651581SN/A
661530SN/A# make a SmartDict out of the build options for our local use
671530SN/Aimport smartdict
681530SN/Abuild_env = smartdict.SmartDict()
692667Sstever@eecs.umich.edubuild_env.update(defines.m5_build_env)
701530SN/A
711530SN/A# make a SmartDict out of the OS environment too
721530SN/Aenv = smartdict.SmartDict()
731530SN/Aenv.update(os.environ)
741530SN/A
752667Sstever@eecs.umich.edu# The final hook to generate .ini files.  Called from the user script
762667Sstever@eecs.umich.edu# once the config is built.
772667Sstever@eecs.umich.edudef instantiate(root):
783101Sstever@eecs.umich.edu    params.ticks_per_sec = float(root.clock.frequency)
793105Sstever@eecs.umich.edu    root.unproxy_all()
802667Sstever@eecs.umich.edu    # ugly temporary hack to get output to config.ini
812762Sstever@eecs.umich.edu    sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w')
822667Sstever@eecs.umich.edu    root.print_ini()
832667Sstever@eecs.umich.edu    sys.stdout.close() # close config.ini
842667Sstever@eecs.umich.edu    sys.stdout = sys.__stdout__ # restore to original
852763Sstever@eecs.umich.edu    cc_main.loadIniFile(resolveSimObject)  # load config.ini into C++
862738Sstever@eecs.umich.edu    root.createCCObject()
872738Sstever@eecs.umich.edu    root.connectPorts()
882763Sstever@eecs.umich.edu    cc_main.finalInit()
892667Sstever@eecs.umich.edu    noDot = True # temporary until we fix dot
902667Sstever@eecs.umich.edu    if not noDot:
912667Sstever@eecs.umich.edu       dot = pydot.Dot()
922667Sstever@eecs.umich.edu       instance.outputDot(dot)
932667Sstever@eecs.umich.edu       dot.orientation = "portrait"
942667Sstever@eecs.umich.edu       dot.size = "8.5,11"
952667Sstever@eecs.umich.edu       dot.ranksep="equally"
962667Sstever@eecs.umich.edu       dot.rank="samerank"
972667Sstever@eecs.umich.edu       dot.write("config.dot")
982667Sstever@eecs.umich.edu       dot.write_ps("config.ps")
991527SN/A
1002667Sstever@eecs.umich.edu# Export curTick to user script.
1012667Sstever@eecs.umich.edudef curTick():
1022763Sstever@eecs.umich.edu    return cc_main.cvar.curTick
1031511SN/A
1042667Sstever@eecs.umich.edu# register our C++ exit callback function with Python
1052763Sstever@eecs.umich.eduatexit.register(cc_main.doExitCleanup)
1062655Sstever@eecs.umich.edu
1072860Sktlim@umich.edu# This loops until all objects have been fully drained.
1082839Sktlim@umich.edudef doDrain(root):
1092860Sktlim@umich.edu    all_drained = drain(root)
1102860Sktlim@umich.edu    while (not all_drained):
1112860Sktlim@umich.edu        all_drained = drain(root)
1122860Sktlim@umich.edu
1132860Sktlim@umich.edu# Tries to drain all objects.  Draining might not be completed unless
1142860Sktlim@umich.edu# all objects return that they are drained on the first call.  This is
1152860Sktlim@umich.edu# because as objects drain they may cause other objects to no longer
1162860Sktlim@umich.edu# be drained.
1172860Sktlim@umich.edudef drain(root):
1182860Sktlim@umich.edu    all_drained = False
1192839Sktlim@umich.edu    drain_event = cc_main.createCountedDrain()
1202839Sktlim@umich.edu    unready_objects = root.startDrain(drain_event, True)
1212839Sktlim@umich.edu    # If we've got some objects that can't drain immediately, then simulate
1222797Sktlim@umich.edu    if unready_objects > 0:
1232839Sktlim@umich.edu        drain_event.setCount(unready_objects)
1242797Sktlim@umich.edu        simulate()
1252860Sktlim@umich.edu    else:
1262860Sktlim@umich.edu        all_drained = True
1272839Sktlim@umich.edu    cc_main.cleanupCountedDrain(drain_event)
1282860Sktlim@umich.edu    return all_drained
1292797Sktlim@umich.edu
1302797Sktlim@umich.edudef resume(root):
1312797Sktlim@umich.edu    root.resume()
1322797Sktlim@umich.edu
1332868Sktlim@umich.edudef checkpoint(root, dir):
1342797Sktlim@umich.edu    if not isinstance(root, objects.Root):
1352797Sktlim@umich.edu        raise TypeError, "Object is not a root object. Checkpoint must be called on a root object."
1362839Sktlim@umich.edu    doDrain(root)
1372797Sktlim@umich.edu    print "Writing checkpoint"
1382868Sktlim@umich.edu    cc_main.serializeAll(dir)
1392797Sktlim@umich.edu    resume(root)
1402797Sktlim@umich.edu
1412868Sktlim@umich.edudef restoreCheckpoint(root, dir):
1422797Sktlim@umich.edu    print "Restoring from checkpoint"
1432868Sktlim@umich.edu    cc_main.unserializeAll(dir)
1442865Sktlim@umich.edu    resume(root)
1452797Sktlim@umich.edu
1462797Sktlim@umich.edudef changeToAtomic(system):
1472797Sktlim@umich.edu    if not isinstance(system, objects.Root) and not isinstance(system, System):
1482797Sktlim@umich.edu        raise TypeError, "Object is not a root or system object.  Checkpoint must be "
1492797Sktlim@umich.edu        "called on a root object."
1502839Sktlim@umich.edu    doDrain(system)
1512797Sktlim@umich.edu    print "Changing memory mode to atomic"
1522797Sktlim@umich.edu    system.changeTiming(cc_main.SimObject.Atomic)
1532797Sktlim@umich.edu    resume(system)
1542797Sktlim@umich.edu
1552797Sktlim@umich.edudef changeToTiming(system):
1562797Sktlim@umich.edu    if not isinstance(system, objects.Root) and not isinstance(system, System):
1572797Sktlim@umich.edu        raise TypeError, "Object is not a root or system object.  Checkpoint must be "
1582797Sktlim@umich.edu        "called on a root object."
1592839Sktlim@umich.edu    doDrain(system)
1602797Sktlim@umich.edu    print "Changing memory mode to timing"
1612797Sktlim@umich.edu    system.changeTiming(cc_main.SimObject.Timing)
1622797Sktlim@umich.edu    resume(system)
1632797Sktlim@umich.edu
1642797Sktlim@umich.edudef switchCpus(cpuList):
1652797Sktlim@umich.edu    if not isinstance(cpuList, list):
1662797Sktlim@umich.edu        raise RuntimeError, "Must pass a list to this function"
1672797Sktlim@umich.edu    for i in cpuList:
1682797Sktlim@umich.edu        if not isinstance(i, tuple):
1692797Sktlim@umich.edu            raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
1702797Sktlim@umich.edu
1712797Sktlim@umich.edu    [old_cpus, new_cpus] = zip(*cpuList)
1722797Sktlim@umich.edu
1732797Sktlim@umich.edu    for cpu in old_cpus:
1742797Sktlim@umich.edu        if not isinstance(cpu, objects.BaseCPU):
1752797Sktlim@umich.edu            raise TypeError, "%s is not of type BaseCPU", cpu
1762797Sktlim@umich.edu    for cpu in new_cpus:
1772797Sktlim@umich.edu        if not isinstance(cpu, objects.BaseCPU):
1782797Sktlim@umich.edu            raise TypeError, "%s is not of type BaseCPU", cpu
1792797Sktlim@umich.edu
1802839Sktlim@umich.edu    # Drain all of the individual CPUs
1812839Sktlim@umich.edu    drain_event = cc_main.createCountedDrain()
1822797Sktlim@umich.edu    unready_cpus = 0
1832797Sktlim@umich.edu    for old_cpu in old_cpus:
1842839Sktlim@umich.edu        unready_cpus += old_cpu.startDrain(drain_event, False)
1852839Sktlim@umich.edu    # If we've got some objects that can't drain immediately, then simulate
1862797Sktlim@umich.edu    if unready_cpus > 0:
1872839Sktlim@umich.edu        drain_event.setCount(unready_cpus)
1882797Sktlim@umich.edu        simulate()
1892839Sktlim@umich.edu    cc_main.cleanupCountedDrain(drain_event)
1902797Sktlim@umich.edu    # Now all of the CPUs are ready to be switched out
1912797Sktlim@umich.edu    for old_cpu in old_cpus:
1922797Sktlim@umich.edu        old_cpu._ccObject.switchOut()
1932797Sktlim@umich.edu    index = 0
1942797Sktlim@umich.edu    print "Switching CPUs"
1952797Sktlim@umich.edu    for new_cpu in new_cpus:
1962797Sktlim@umich.edu        new_cpu.takeOverFrom(old_cpus[index])
1972797Sktlim@umich.edu        new_cpu._ccObject.resume()
1982797Sktlim@umich.edu        index += 1
1993101Sstever@eecs.umich.edu
2003101Sstever@eecs.umich.edu# Since we have so many mutual imports in this package, we should:
2013101Sstever@eecs.umich.edu# 1. Put all intra-package imports at the *bottom* of the file, unless
2023101Sstever@eecs.umich.edu#    they're absolutely needed before that (for top-level statements
2033101Sstever@eecs.umich.edu#    or class attributes).  Imports of "trivial" packages that don't
2043101Sstever@eecs.umich.edu#    import other packages (e.g., 'smartdict') can be at the top.
2053101Sstever@eecs.umich.edu# 2. Never use 'from foo import *' on an intra-package import since
2063101Sstever@eecs.umich.edu#    you can get the wrong result if foo is only partially imported
2073101Sstever@eecs.umich.edu#    at the point you do that (i.e., because foo is in the middle of
2083101Sstever@eecs.umich.edu#    importing *you*).
2093102Sstever@eecs.umich.edufrom main import options
2103101Sstever@eecs.umich.eduimport objects
2113101Sstever@eecs.umich.eduimport params
2123102Sstever@eecs.umich.edufrom SimObject import resolveSimObject
213