simulate.py revision 8675
14762Snate@binkert.org# Copyright (c) 2005 The Regents of The University of Michigan 27534Ssteve.reinhardt@amd.com# Copyright (c) 2010 Advanced Micro Devices, Inc. 34762Snate@binkert.org# All rights reserved. 44762Snate@binkert.org# 54762Snate@binkert.org# Redistribution and use in source and binary forms, with or without 64762Snate@binkert.org# modification, are permitted provided that the following conditions are 74762Snate@binkert.org# met: redistributions of source code must retain the above copyright 84762Snate@binkert.org# notice, this list of conditions and the following disclaimer; 94762Snate@binkert.org# redistributions in binary form must reproduce the above copyright 104762Snate@binkert.org# notice, this list of conditions and the following disclaimer in the 114762Snate@binkert.org# documentation and/or other materials provided with the distribution; 124762Snate@binkert.org# neither the name of the copyright holders nor the names of its 134762Snate@binkert.org# contributors may be used to endorse or promote products derived from 144762Snate@binkert.org# this software without specific prior written permission. 154762Snate@binkert.org# 164762Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 174762Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 184762Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 194762Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 204762Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 214762Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 224762Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 234762Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 244762Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 254762Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 264762Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 274762Snate@binkert.org# 284762Snate@binkert.org# Authors: Nathan Binkert 294762Snate@binkert.org# Steve Reinhardt 304762Snate@binkert.org 314762Snate@binkert.orgimport atexit 324762Snate@binkert.orgimport os 334762Snate@binkert.orgimport sys 344762Snate@binkert.org 354762Snate@binkert.org# import the SWIG-wrapped main C++ functions 364762Snate@binkert.orgimport internal 376001Snate@binkert.orgimport core 386001Snate@binkert.orgimport stats 394762Snate@binkert.orgimport SimObject 404762Snate@binkert.orgimport ticks 414851Snate@binkert.orgimport objects 427525Ssteve.reinhardt@amd.comfrom util import fatal 438664SAli.Saidi@ARM.comfrom util import attrdict 444762Snate@binkert.org 456654Snate@binkert.org# define a MaxTick parameter 466654Snate@binkert.orgMaxTick = 2**63 - 1 476654Snate@binkert.org 484762Snate@binkert.org# The final hook to generate .ini files. Called from the user script 494762Snate@binkert.org# once the config is built. 507531Ssteve.reinhardt@amd.comdef instantiate(ckpt_dir=None): 518245Snate@binkert.org from m5 import options 528234Snate@binkert.org 537525Ssteve.reinhardt@amd.com root = objects.Root.getInstance() 547525Ssteve.reinhardt@amd.com 557525Ssteve.reinhardt@amd.com if not root: 567525Ssteve.reinhardt@amd.com fatal("Need to instantiate Root() before calling instantiate()") 577525Ssteve.reinhardt@amd.com 584762Snate@binkert.org # we need to fix the global frequency 594762Snate@binkert.org ticks.fixGlobalFrequency() 604762Snate@binkert.org 617528Ssteve.reinhardt@amd.com # Make sure SimObject-valued params are in the configuration 627528Ssteve.reinhardt@amd.com # hierarchy so we catch them with future descendants() walks 637528Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.adoptOrphanParams() 647528Ssteve.reinhardt@amd.com 657527Ssteve.reinhardt@amd.com # Unproxy in sorted order for determinism 667527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.unproxyParams() 675037Smilesck@eecs.umich.edu 685773Snate@binkert.org if options.dump_config: 695773Snate@binkert.org ini_file = file(os.path.join(options.outdir, options.dump_config), 'w') 707527Ssteve.reinhardt@amd.com # Print ini sections in sorted order for easier diffing 717527Ssteve.reinhardt@amd.com for obj in sorted(root.descendants(), key=lambda o: o.path()): 727527Ssteve.reinhardt@amd.com obj.print_ini(ini_file) 735773Snate@binkert.org ini_file.close() 744762Snate@binkert.org 758664SAli.Saidi@ARM.com if options.json_config: 768675SAli.Saidi@ARM.com try: 778675SAli.Saidi@ARM.com import json 788675SAli.Saidi@ARM.com json_file = file(os.path.join(options.outdir, options.json_config), 'w') 798675SAli.Saidi@ARM.com d = root.get_config_as_dict() 808675SAli.Saidi@ARM.com json.dump(d, json_file, indent=4) 818675SAli.Saidi@ARM.com json_file.close() 828675SAli.Saidi@ARM.com except ImportError: 838675SAli.Saidi@ARM.com pass 848664SAli.Saidi@ARM.com 858664SAli.Saidi@ARM.com 864762Snate@binkert.org # Initialize the global statistics 876001Snate@binkert.org stats.initSimStats() 884762Snate@binkert.org 894762Snate@binkert.org # Create the C++ sim objects and connect ports 907527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.createCCObject() 917527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.connectPorts() 924762Snate@binkert.org 934762Snate@binkert.org # Do a second pass to finish initializing the sim objects 947527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.init() 954762Snate@binkert.org 964762Snate@binkert.org # Do a third pass to initialize statistics 977527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.regStats() 987527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.regFormulas() 994762Snate@binkert.org 1006001Snate@binkert.org # We're done registering statistics. Enable the stats package now. 1016001Snate@binkert.org stats.enable() 1024762Snate@binkert.org 1037531Ssteve.reinhardt@amd.com # Restore checkpoint (if any) 1047531Ssteve.reinhardt@amd.com if ckpt_dir: 1057532Ssteve.reinhardt@amd.com ckpt = internal.core.getCheckpoint(ckpt_dir) 1067532Ssteve.reinhardt@amd.com internal.core.unserializeGlobals(ckpt); 1077532Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.loadState(ckpt) 1087531Ssteve.reinhardt@amd.com need_resume.append(root) 1097532Ssteve.reinhardt@amd.com else: 1107532Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.initState() 1117531Ssteve.reinhardt@amd.com 1124762Snate@binkert.org # Reset to put the stats in a consistent state. 1136001Snate@binkert.org stats.reset() 1144762Snate@binkert.org 1154762Snate@binkert.orgdef doDot(root): 1164762Snate@binkert.org dot = pydot.Dot() 1174762Snate@binkert.org instance.outputDot(dot) 1184762Snate@binkert.org dot.orientation = "portrait" 1194762Snate@binkert.org dot.size = "8.5,11" 1204762Snate@binkert.org dot.ranksep="equally" 1214762Snate@binkert.org dot.rank="samerank" 1224762Snate@binkert.org dot.write("config.dot") 1234762Snate@binkert.org dot.write_ps("config.ps") 1244762Snate@binkert.org 1254762Snate@binkert.orgneed_resume = [] 1264762Snate@binkert.orgneed_startup = True 1274762Snate@binkert.orgdef simulate(*args, **kwargs): 1284762Snate@binkert.org global need_resume, need_startup 1294762Snate@binkert.org 1304762Snate@binkert.org if need_startup: 1317527Ssteve.reinhardt@amd.com root = objects.Root.getInstance() 1327527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.startup() 1334762Snate@binkert.org need_startup = False 1344762Snate@binkert.org 1354762Snate@binkert.org for root in need_resume: 1364762Snate@binkert.org resume(root) 1374762Snate@binkert.org need_resume = [] 1384762Snate@binkert.org 1394762Snate@binkert.org return internal.event.simulate(*args, **kwargs) 1404762Snate@binkert.org 1414762Snate@binkert.org# Export curTick to user script. 1424762Snate@binkert.orgdef curTick(): 1437823Ssteve.reinhardt@amd.com return internal.core.curTick() 1444762Snate@binkert.org 1454762Snate@binkert.org# Python exit handlers happen in reverse order. We want to dump stats last. 1468296Snate@binkert.orgatexit.register(stats.dump) 1474762Snate@binkert.org 1484762Snate@binkert.org# register our C++ exit callback function with Python 1494762Snate@binkert.orgatexit.register(internal.core.doExitCleanup) 1504762Snate@binkert.org 1514762Snate@binkert.org# This loops until all objects have been fully drained. 1524762Snate@binkert.orgdef doDrain(root): 1534762Snate@binkert.org all_drained = drain(root) 1544762Snate@binkert.org while (not all_drained): 1554762Snate@binkert.org all_drained = drain(root) 1564762Snate@binkert.org 1574762Snate@binkert.org# Tries to drain all objects. Draining might not be completed unless 1584762Snate@binkert.org# all objects return that they are drained on the first call. This is 1594762Snate@binkert.org# because as objects drain they may cause other objects to no longer 1604762Snate@binkert.org# be drained. 1614762Snate@binkert.orgdef drain(root): 1624762Snate@binkert.org all_drained = False 1634762Snate@binkert.org drain_event = internal.event.createCountedDrain() 1647527Ssteve.reinhardt@amd.com unready_objs = sum(obj.drain(drain_event) for obj in root.descendants()) 1654762Snate@binkert.org # If we've got some objects that can't drain immediately, then simulate 1667527Ssteve.reinhardt@amd.com if unready_objs > 0: 1677527Ssteve.reinhardt@amd.com drain_event.setCount(unready_objs) 1684762Snate@binkert.org simulate() 1694762Snate@binkert.org else: 1704762Snate@binkert.org all_drained = True 1714762Snate@binkert.org internal.event.cleanupCountedDrain(drain_event) 1724762Snate@binkert.org return all_drained 1734762Snate@binkert.org 1744762Snate@binkert.orgdef resume(root): 1757527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.resume() 1764762Snate@binkert.org 1777525Ssteve.reinhardt@amd.comdef checkpoint(dir): 1787525Ssteve.reinhardt@amd.com root = objects.Root.getInstance() 1794762Snate@binkert.org if not isinstance(root, objects.Root): 1804762Snate@binkert.org raise TypeError, "Checkpoint must be called on a root object." 1814762Snate@binkert.org doDrain(root) 1824762Snate@binkert.org print "Writing checkpoint" 1834859Snate@binkert.org internal.core.serializeAll(dir) 1844762Snate@binkert.org resume(root) 1854762Snate@binkert.org 1864762Snate@binkert.orgdef changeToAtomic(system): 1874762Snate@binkert.org if not isinstance(system, (objects.Root, objects.System)): 1884762Snate@binkert.org raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \ 1894762Snate@binkert.org (type(system), objects.Root, objects.System) 1904945Snate@binkert.org if system.getMemoryMode() != objects.params.atomic: 1914762Snate@binkert.org doDrain(system) 1924762Snate@binkert.org print "Changing memory mode to atomic" 1937527Ssteve.reinhardt@amd.com for obj in system.descendants(): 1947527Ssteve.reinhardt@amd.com obj.changeTiming(objects.params.atomic) 1954762Snate@binkert.org 1964762Snate@binkert.orgdef changeToTiming(system): 1974762Snate@binkert.org if not isinstance(system, (objects.Root, objects.System)): 1984762Snate@binkert.org raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \ 1994762Snate@binkert.org (type(system), objects.Root, objects.System) 2004762Snate@binkert.org 2014945Snate@binkert.org if system.getMemoryMode() != objects.params.timing: 2024762Snate@binkert.org doDrain(system) 2034762Snate@binkert.org print "Changing memory mode to timing" 2047527Ssteve.reinhardt@amd.com for obj in system.descendants(): 2057527Ssteve.reinhardt@amd.com obj.changeTiming(objects.params.timing) 2064762Snate@binkert.org 2074762Snate@binkert.orgdef switchCpus(cpuList): 2084762Snate@binkert.org print "switching cpus" 2094762Snate@binkert.org if not isinstance(cpuList, list): 2104762Snate@binkert.org raise RuntimeError, "Must pass a list to this function" 2114946Snate@binkert.org for item in cpuList: 2124946Snate@binkert.org if not isinstance(item, tuple) or len(item) != 2: 2134762Snate@binkert.org raise RuntimeError, "List must have tuples of (oldCPU,newCPU)" 2144762Snate@binkert.org 2154946Snate@binkert.org for old_cpu, new_cpu in cpuList: 2164946Snate@binkert.org if not isinstance(old_cpu, objects.BaseCPU): 2174946Snate@binkert.org raise TypeError, "%s is not of type BaseCPU" % old_cpu 2184946Snate@binkert.org if not isinstance(new_cpu, objects.BaseCPU): 2194946Snate@binkert.org raise TypeError, "%s is not of type BaseCPU" % new_cpu 2204762Snate@binkert.org 2214946Snate@binkert.org # Now all of the CPUs are ready to be switched out 2224946Snate@binkert.org for old_cpu, new_cpu in cpuList: 2234946Snate@binkert.org old_cpu._ccObject.switchOut() 2244762Snate@binkert.org 2254946Snate@binkert.org for old_cpu, new_cpu in cpuList: 2264946Snate@binkert.org new_cpu.takeOverFrom(old_cpu) 2275523Snate@binkert.org 2285523Snate@binkert.orgfrom internal.core import disableAllListeners 229