simulate.py revision 8998:c8bf5a20bc07
12155SN/A# Copyright (c) 2005 The Regents of The University of Michigan
22155SN/A# Copyright (c) 2010 Advanced Micro Devices, Inc.
32155SN/A# All rights reserved.
42155SN/A#
52155SN/A# Redistribution and use in source and binary forms, with or without
62155SN/A# modification, are permitted provided that the following conditions are
72155SN/A# met: redistributions of source code must retain the above copyright
82155SN/A# notice, this list of conditions and the following disclaimer;
92155SN/A# redistributions in binary form must reproduce the above copyright
102155SN/A# notice, this list of conditions and the following disclaimer in the
112155SN/A# documentation and/or other materials provided with the distribution;
122155SN/A# neither the name of the copyright holders nor the names of its
132155SN/A# contributors may be used to endorse or promote products derived from
142155SN/A# this software without specific prior written permission.
152155SN/A#
162155SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172155SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182155SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192155SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202155SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212155SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222155SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232155SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242155SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252155SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262155SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272155SN/A#
282665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert
292665Ssaidi@eecs.umich.edu#          Steve Reinhardt
302155SN/A
314202Sbinkertn@umich.eduimport atexit
322155SN/Aimport os
332178SN/Aimport sys
342178SN/A
352178SN/Atry:
362178SN/A    import pydot
372178SN/Aexcept:
382178SN/A    pydot = False
392178SN/A
402178SN/A
412178SN/A# import the SWIG-wrapped main C++ functions
422178SN/Aimport internal
432178SN/Aimport core
442178SN/Aimport stats
452155SN/Aimport SimObject
462178SN/Aimport ticks
472155SN/Aimport objects
482155SN/Afrom util import fatal
492178SN/Afrom util import attrdict
502155SN/A
515865Sksewell@umich.edu# define a MaxTick parameter
526181Sksewell@umich.eduMaxTick = 2**63 - 1
536181Sksewell@umich.edu
545865Sksewell@umich.edu# The final hook to generate .ini files.  Called from the user script
553918Ssaidi@eecs.umich.edu# once the config is built.
565865Sksewell@umich.edudef instantiate(ckpt_dir=None):
572623SN/A    from m5 import options
583918Ssaidi@eecs.umich.edu
595865Sksewell@umich.edu    root = objects.Root.getInstance()
605865Sksewell@umich.edu
612155SN/A    if not root:
622155SN/A        fatal("Need to instantiate Root() before calling instantiate()")
632292SN/A
646181Sksewell@umich.edu    # we need to fix the global frequency
656181Sksewell@umich.edu    ticks.fixGlobalFrequency()
663918Ssaidi@eecs.umich.edu
672292SN/A    # Make sure SimObject-valued params are in the configuration
682292SN/A    # hierarchy so we catch them with future descendants() walks
692292SN/A    for obj in root.descendants(): obj.adoptOrphanParams()
703918Ssaidi@eecs.umich.edu
712292SN/A    # Unproxy in sorted order for determinism
722292SN/A    for obj in root.descendants(): obj.unproxyParams()
732766Sktlim@umich.edu
742766Sktlim@umich.edu    if options.dump_config:
752766Sktlim@umich.edu        ini_file = file(os.path.join(options.outdir, options.dump_config), 'w')
762921Sktlim@umich.edu        # Print ini sections in sorted order for easier diffing
772921Sktlim@umich.edu        for obj in sorted(root.descendants(), key=lambda o: o.path()):
782766Sktlim@umich.edu            obj.print_ini(ini_file)
792766Sktlim@umich.edu        ini_file.close()
805529Snate@binkert.org
812766Sktlim@umich.edu    if options.json_config:
824762Snate@binkert.org        try:
832155SN/A            import json
842155SN/A            json_file = file(os.path.join(options.outdir, options.json_config), 'w')
852155SN/A            d = root.get_config_as_dict()
862155SN/A            json.dump(d, json_file, indent=4)
872155SN/A            json_file.close()
882155SN/A        except ImportError:
892766Sktlim@umich.edu            pass
902155SN/A
915865Sksewell@umich.edu    if pydot:
922155SN/A        doDot(root)
932155SN/A
942155SN/A    # Initialize the global statistics
952155SN/A    stats.initSimStats()
962178SN/A
972178SN/A    # Create the C++ sim objects and connect ports
982178SN/A    for obj in root.descendants(): obj.createCCObject()
992766Sktlim@umich.edu    for obj in root.descendants(): obj.connectPorts()
1002178SN/A
1012178SN/A    # Do a second pass to finish initializing the sim objects
1022178SN/A    for obj in root.descendants(): obj.init()
1032178SN/A
1042766Sktlim@umich.edu    # Do a third pass to initialize statistics
1052766Sktlim@umich.edu    for obj in root.descendants(): obj.regStats()
1062766Sktlim@umich.edu    for obj in root.descendants(): obj.regFormulas()
1072788Sktlim@umich.edu
1082178SN/A    # We're done registering statistics.  Enable the stats package now.
1092733Sktlim@umich.edu    stats.enable()
1102733Sktlim@umich.edu
1112817Sksewell@umich.edu    # Restore checkpoint (if any)
1122733Sktlim@umich.edu    if ckpt_dir:
1134486Sbinkertn@umich.edu        ckpt = internal.core.getCheckpoint(ckpt_dir)
1144486Sbinkertn@umich.edu        internal.core.unserializeGlobals(ckpt);
1154776Sgblack@eecs.umich.edu        for obj in root.descendants(): obj.loadState(ckpt)
1164776Sgblack@eecs.umich.edu        need_resume.append(root)
1174486Sbinkertn@umich.edu    else:
1184202Sbinkertn@umich.edu        for obj in root.descendants(): obj.initState()
1194202Sbinkertn@umich.edu
1204202Sbinkertn@umich.edu    # Reset to put the stats in a consistent state.
1214202Sbinkertn@umich.edu    stats.reset()
1224202Sbinkertn@umich.edu
1234776Sgblack@eecs.umich.edudef doDot(root):
1244202Sbinkertn@umich.edu    from m5 import options
1254202Sbinkertn@umich.edu    dot = pydot.Dot()
1264202Sbinkertn@umich.edu    root.outputDot(dot)
1274202Sbinkertn@umich.edu    dot.orientation = "portrait"
1285217Ssaidi@eecs.umich.edu    dot.size = "8.5,11"
1294202Sbinkertn@umich.edu    dot.ranksep="equally"
1302155SN/A    dot.rank="samerank"
1314202Sbinkertn@umich.edu    dot_filename = os.path.join(options.outdir, options.dot_config)
1324486Sbinkertn@umich.edu    dot.write(dot_filename)
1334486Sbinkertn@umich.edu    dot.write_pdf(dot_filename + ".pdf")
1344202Sbinkertn@umich.edu
1354202Sbinkertn@umich.eduneed_resume = []
1362821Sktlim@umich.eduneed_startup = True
1374776Sgblack@eecs.umich.edudef simulate(*args, **kwargs):
1384776Sgblack@eecs.umich.edu    global need_resume, need_startup
1394776Sgblack@eecs.umich.edu
1404776Sgblack@eecs.umich.edu    if need_startup:
1414776Sgblack@eecs.umich.edu        root = objects.Root.getInstance()
1424776Sgblack@eecs.umich.edu        for obj in root.descendants(): obj.startup()
1434776Sgblack@eecs.umich.edu        need_startup = False
1444776Sgblack@eecs.umich.edu
1452766Sktlim@umich.edu    for root in need_resume:
1464202Sbinkertn@umich.edu        resume(root)
1475192Ssaidi@eecs.umich.edu    need_resume = []
1482733Sktlim@umich.edu
1492733Sktlim@umich.edu    return internal.event.simulate(*args, **kwargs)
1502733Sktlim@umich.edu
1512733Sktlim@umich.edu# Export curTick to user script.
1522733Sktlim@umich.edudef curTick():
1532874Sktlim@umich.edu    return internal.core.curTick()
1542874Sktlim@umich.edu
1552874Sktlim@umich.edu# Python exit handlers happen in reverse order.  We want to dump stats last.
1564202Sbinkertn@umich.eduatexit.register(stats.dump)
1572733Sktlim@umich.edu
1585192Ssaidi@eecs.umich.edu# register our C++ exit callback function with Python
1595192Ssaidi@eecs.umich.eduatexit.register(internal.core.doExitCleanup)
1605192Ssaidi@eecs.umich.edu
1615217Ssaidi@eecs.umich.edu# This loops until all objects have been fully drained.
1625192Ssaidi@eecs.umich.edudef doDrain(root):
1635192Ssaidi@eecs.umich.edu    all_drained = drain(root)
1645192Ssaidi@eecs.umich.edu    while (not all_drained):
1655192Ssaidi@eecs.umich.edu        all_drained = drain(root)
1665192Ssaidi@eecs.umich.edu
1675192Ssaidi@eecs.umich.edu# Tries to drain all objects.  Draining might not be completed unless
1685192Ssaidi@eecs.umich.edu# all objects return that they are drained on the first call.  This is
1695192Ssaidi@eecs.umich.edu# because as objects drain they may cause other objects to no longer
1705192Ssaidi@eecs.umich.edu# be drained.
1715192Ssaidi@eecs.umich.edudef drain(root):
1725192Ssaidi@eecs.umich.edu    all_drained = False
1735192Ssaidi@eecs.umich.edu    drain_event = internal.event.createCountedDrain()
1745192Ssaidi@eecs.umich.edu    unready_objs = sum(obj.drain(drain_event) for obj in root.descendants())
1755784Sgblack@eecs.umich.edu    # If we've got some objects that can't drain immediately, then simulate
1765784Sgblack@eecs.umich.edu    if unready_objs > 0:
1775192Ssaidi@eecs.umich.edu        drain_event.setCount(unready_objs)
1785192Ssaidi@eecs.umich.edu        simulate()
1795192Ssaidi@eecs.umich.edu    else:
1805192Ssaidi@eecs.umich.edu        all_drained = True
1815192Ssaidi@eecs.umich.edu    internal.event.cleanupCountedDrain(drain_event)
1825192Ssaidi@eecs.umich.edu    return all_drained
1835784Sgblack@eecs.umich.edu
1846036Sksewell@umich.edudef resume(root):
1856036Sksewell@umich.edu    for obj in root.descendants(): obj.resume()
186
187def checkpoint(dir):
188    root = objects.Root.getInstance()
189    if not isinstance(root, objects.Root):
190        raise TypeError, "Checkpoint must be called on a root object."
191    doDrain(root)
192    print "Writing checkpoint"
193    internal.core.serializeAll(dir)
194    resume(root)
195
196def changeToAtomic(system):
197    if not isinstance(system, (objects.Root, objects.System)):
198        raise TypeError, "Parameter of type '%s'.  Must be type %s or %s." % \
199              (type(system), objects.Root, objects.System)
200    if system.getMemoryMode() != objects.params.atomic:
201        doDrain(system)
202        print "Changing memory mode to atomic"
203        for obj in system.descendants():
204            obj.changeTiming(objects.params.atomic)
205
206def changeToTiming(system):
207    if not isinstance(system, (objects.Root, objects.System)):
208        raise TypeError, "Parameter of type '%s'.  Must be type %s or %s." % \
209              (type(system), objects.Root, objects.System)
210
211    if system.getMemoryMode() != objects.params.timing:
212        doDrain(system)
213        print "Changing memory mode to timing"
214        for obj in system.descendants():
215            obj.changeTiming(objects.params.timing)
216
217def switchCpus(cpuList):
218    print "switching cpus"
219    if not isinstance(cpuList, list):
220        raise RuntimeError, "Must pass a list to this function"
221    for item in cpuList:
222        if not isinstance(item, tuple) or len(item) != 2:
223            raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
224
225    for old_cpu, new_cpu in cpuList:
226        if not isinstance(old_cpu, objects.BaseCPU):
227            raise TypeError, "%s is not of type BaseCPU" % old_cpu
228        if not isinstance(new_cpu, objects.BaseCPU):
229            raise TypeError, "%s is not of type BaseCPU" % new_cpu
230
231    # Now all of the CPUs are ready to be switched out
232    for old_cpu, new_cpu in cpuList:
233        old_cpu._ccObject.switchOut()
234
235    for old_cpu, new_cpu in cpuList:
236        new_cpu.takeOverFrom(old_cpu)
237
238from internal.core import disableAllListeners
239