simulate.py revision 4946
14762Snate@binkert.org# Copyright (c) 2005 The Regents of The University of Michigan
24762Snate@binkert.org# All rights reserved.
34762Snate@binkert.org#
44762Snate@binkert.org# Redistribution and use in source and binary forms, with or without
54762Snate@binkert.org# modification, are permitted provided that the following conditions are
64762Snate@binkert.org# met: redistributions of source code must retain the above copyright
74762Snate@binkert.org# notice, this list of conditions and the following disclaimer;
84762Snate@binkert.org# redistributions in binary form must reproduce the above copyright
94762Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
104762Snate@binkert.org# documentation and/or other materials provided with the distribution;
114762Snate@binkert.org# neither the name of the copyright holders nor the names of its
124762Snate@binkert.org# contributors may be used to endorse or promote products derived from
134762Snate@binkert.org# this software without specific prior written permission.
144762Snate@binkert.org#
154762Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
164762Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
174762Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
184762Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
194762Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
204762Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
214762Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
224762Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
234762Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
244762Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
254762Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
264762Snate@binkert.org#
274762Snate@binkert.org# Authors: Nathan Binkert
284762Snate@binkert.org#          Steve Reinhardt
294762Snate@binkert.org
304762Snate@binkert.orgimport atexit
314762Snate@binkert.orgimport os
324762Snate@binkert.orgimport sys
334762Snate@binkert.org
344762Snate@binkert.org# import the SWIG-wrapped main C++ functions
354762Snate@binkert.orgimport internal
364762Snate@binkert.orgfrom main import options
374762Snate@binkert.orgimport SimObject
384762Snate@binkert.orgimport ticks
394851Snate@binkert.orgimport objects
404762Snate@binkert.org
414762Snate@binkert.org# The final hook to generate .ini files.  Called from the user script
424762Snate@binkert.org# once the config is built.
434762Snate@binkert.orgdef instantiate(root):
444762Snate@binkert.org    # we need to fix the global frequency
454762Snate@binkert.org    ticks.fixGlobalFrequency()
464762Snate@binkert.org
474762Snate@binkert.org    root.unproxy_all()
484762Snate@binkert.org    # ugly temporary hack to get output to config.ini
494762Snate@binkert.org    sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w')
504762Snate@binkert.org    root.print_ini()
514762Snate@binkert.org    sys.stdout.close() # close config.ini
524762Snate@binkert.org    sys.stdout = sys.__stdout__ # restore to original
534762Snate@binkert.org
544762Snate@binkert.org    # Initialize the global statistics
554762Snate@binkert.org    internal.stats.initSimStats()
564762Snate@binkert.org
574762Snate@binkert.org    # Create the C++ sim objects and connect ports
584762Snate@binkert.org    root.createCCObject()
594762Snate@binkert.org    root.connectPorts()
604762Snate@binkert.org
614762Snate@binkert.org    # Do a second pass to finish initializing the sim objects
624859Snate@binkert.org    internal.core.initAll()
634762Snate@binkert.org
644762Snate@binkert.org    # Do a third pass to initialize statistics
654859Snate@binkert.org    internal.core.regAllStats()
664762Snate@binkert.org
674762Snate@binkert.org    # Check to make sure that the stats package is properly initialized
684762Snate@binkert.org    internal.stats.check()
694762Snate@binkert.org
704762Snate@binkert.org    # Reset to put the stats in a consistent state.
714762Snate@binkert.org    internal.stats.reset()
724762Snate@binkert.org
734762Snate@binkert.orgdef doDot(root):
744762Snate@binkert.org    dot = pydot.Dot()
754762Snate@binkert.org    instance.outputDot(dot)
764762Snate@binkert.org    dot.orientation = "portrait"
774762Snate@binkert.org    dot.size = "8.5,11"
784762Snate@binkert.org    dot.ranksep="equally"
794762Snate@binkert.org    dot.rank="samerank"
804762Snate@binkert.org    dot.write("config.dot")
814762Snate@binkert.org    dot.write_ps("config.ps")
824762Snate@binkert.org
834762Snate@binkert.orgneed_resume = []
844762Snate@binkert.orgneed_startup = True
854762Snate@binkert.orgdef simulate(*args, **kwargs):
864762Snate@binkert.org    global need_resume, need_startup
874762Snate@binkert.org
884762Snate@binkert.org    if need_startup:
894762Snate@binkert.org        internal.core.SimStartup()
904762Snate@binkert.org        need_startup = False
914762Snate@binkert.org
924762Snate@binkert.org    for root in need_resume:
934762Snate@binkert.org        resume(root)
944762Snate@binkert.org    need_resume = []
954762Snate@binkert.org
964762Snate@binkert.org    return internal.event.simulate(*args, **kwargs)
974762Snate@binkert.org
984762Snate@binkert.org# Export curTick to user script.
994762Snate@binkert.orgdef curTick():
1004762Snate@binkert.org    return internal.core.cvar.curTick
1014762Snate@binkert.org
1024762Snate@binkert.org# Python exit handlers happen in reverse order.  We want to dump stats last.
1034762Snate@binkert.orgatexit.register(internal.stats.dump)
1044762Snate@binkert.org
1054762Snate@binkert.org# register our C++ exit callback function with Python
1064762Snate@binkert.orgatexit.register(internal.core.doExitCleanup)
1074762Snate@binkert.org
1084762Snate@binkert.org# This loops until all objects have been fully drained.
1094762Snate@binkert.orgdef doDrain(root):
1104762Snate@binkert.org    all_drained = drain(root)
1114762Snate@binkert.org    while (not all_drained):
1124762Snate@binkert.org        all_drained = drain(root)
1134762Snate@binkert.org
1144762Snate@binkert.org# Tries to drain all objects.  Draining might not be completed unless
1154762Snate@binkert.org# all objects return that they are drained on the first call.  This is
1164762Snate@binkert.org# because as objects drain they may cause other objects to no longer
1174762Snate@binkert.org# be drained.
1184762Snate@binkert.orgdef drain(root):
1194762Snate@binkert.org    all_drained = False
1204762Snate@binkert.org    drain_event = internal.event.createCountedDrain()
1214762Snate@binkert.org    unready_objects = root.startDrain(drain_event, True)
1224762Snate@binkert.org    # If we've got some objects that can't drain immediately, then simulate
1234762Snate@binkert.org    if unready_objects > 0:
1244762Snate@binkert.org        drain_event.setCount(unready_objects)
1254762Snate@binkert.org        simulate()
1264762Snate@binkert.org    else:
1274762Snate@binkert.org        all_drained = True
1284762Snate@binkert.org    internal.event.cleanupCountedDrain(drain_event)
1294762Snate@binkert.org    return all_drained
1304762Snate@binkert.org
1314762Snate@binkert.orgdef resume(root):
1324762Snate@binkert.org    root.resume()
1334762Snate@binkert.org
1344762Snate@binkert.orgdef checkpoint(root, dir):
1354762Snate@binkert.org    if not isinstance(root, objects.Root):
1364762Snate@binkert.org        raise TypeError, "Checkpoint must be called on a root object."
1374762Snate@binkert.org    doDrain(root)
1384762Snate@binkert.org    print "Writing checkpoint"
1394859Snate@binkert.org    internal.core.serializeAll(dir)
1404762Snate@binkert.org    resume(root)
1414762Snate@binkert.org
1424762Snate@binkert.orgdef restoreCheckpoint(root, dir):
1434762Snate@binkert.org    print "Restoring from checkpoint"
1444859Snate@binkert.org    internal.core.unserializeAll(dir)
1454762Snate@binkert.org    need_resume.append(root)
1464762Snate@binkert.org
1474762Snate@binkert.orgdef changeToAtomic(system):
1484762Snate@binkert.org    if not isinstance(system, (objects.Root, objects.System)):
1494762Snate@binkert.org        raise TypeError, "Parameter of type '%s'.  Must be type %s or %s." % \
1504762Snate@binkert.org              (type(system), objects.Root, objects.System)
1514945Snate@binkert.org    if system.getMemoryMode() != objects.params.atomic:
1524762Snate@binkert.org        doDrain(system)
1534762Snate@binkert.org        print "Changing memory mode to atomic"
1544945Snate@binkert.org        system.changeTiming(objects.params.atomic)
1554762Snate@binkert.org
1564762Snate@binkert.orgdef changeToTiming(system):
1574762Snate@binkert.org    if not isinstance(system, (objects.Root, objects.System)):
1584762Snate@binkert.org        raise TypeError, "Parameter of type '%s'.  Must be type %s or %s." % \
1594762Snate@binkert.org              (type(system), objects.Root, objects.System)
1604762Snate@binkert.org
1614945Snate@binkert.org    if system.getMemoryMode() != objects.params.timing:
1624762Snate@binkert.org        doDrain(system)
1634762Snate@binkert.org        print "Changing memory mode to timing"
1644945Snate@binkert.org        system.changeTiming(objects.params.timing)
1654762Snate@binkert.org
1664762Snate@binkert.orgdef switchCpus(cpuList):
1674762Snate@binkert.org    print "switching cpus"
1684762Snate@binkert.org    if not isinstance(cpuList, list):
1694762Snate@binkert.org        raise RuntimeError, "Must pass a list to this function"
1704946Snate@binkert.org    for item in cpuList:
1714946Snate@binkert.org        if not isinstance(item, tuple) or len(item) != 2:
1724762Snate@binkert.org            raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
1734762Snate@binkert.org
1744946Snate@binkert.org    for old_cpu, new_cpu in cpuList:
1754946Snate@binkert.org        if not isinstance(old_cpu, objects.BaseCPU):
1764946Snate@binkert.org            raise TypeError, "%s is not of type BaseCPU" % old_cpu
1774946Snate@binkert.org        if not isinstance(new_cpu, objects.BaseCPU):
1784946Snate@binkert.org            raise TypeError, "%s is not of type BaseCPU" % new_cpu
1794762Snate@binkert.org
1804946Snate@binkert.org    # Now all of the CPUs are ready to be switched out
1814946Snate@binkert.org    for old_cpu, new_cpu in cpuList:
1824946Snate@binkert.org        old_cpu._ccObject.switchOut()
1834762Snate@binkert.org
1844946Snate@binkert.org    for old_cpu, new_cpu in cpuList:
1854946Snate@binkert.org        new_cpu.takeOverFrom(old_cpu)
186