simulate.py revision 7527
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 366001Snate@binkert.orgimport core 376001Snate@binkert.orgimport stats 384762Snate@binkert.orgfrom main import options 394762Snate@binkert.orgimport SimObject 404762Snate@binkert.orgimport ticks 414851Snate@binkert.orgimport objects 427525Ssteve.reinhardt@amd.comfrom util import fatal 434762Snate@binkert.org 446654Snate@binkert.org# define a MaxTick parameter 456654Snate@binkert.orgMaxTick = 2**63 - 1 466654Snate@binkert.org 474762Snate@binkert.org# The final hook to generate .ini files. Called from the user script 484762Snate@binkert.org# once the config is built. 497525Ssteve.reinhardt@amd.comdef instantiate(): 507525Ssteve.reinhardt@amd.com root = objects.Root.getInstance() 517525Ssteve.reinhardt@amd.com 527525Ssteve.reinhardt@amd.com if not root: 537525Ssteve.reinhardt@amd.com fatal("Need to instantiate Root() before calling instantiate()") 547525Ssteve.reinhardt@amd.com 554762Snate@binkert.org # we need to fix the global frequency 564762Snate@binkert.org ticks.fixGlobalFrequency() 574762Snate@binkert.org 587527Ssteve.reinhardt@amd.com # Unproxy in sorted order for determinism 597527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.unproxyParams() 605037Smilesck@eecs.umich.edu 615773Snate@binkert.org if options.dump_config: 625773Snate@binkert.org ini_file = file(os.path.join(options.outdir, options.dump_config), 'w') 637527Ssteve.reinhardt@amd.com # Print ini sections in sorted order for easier diffing 647527Ssteve.reinhardt@amd.com for obj in sorted(root.descendants(), key=lambda o: o.path()): 657527Ssteve.reinhardt@amd.com obj.print_ini(ini_file) 665773Snate@binkert.org ini_file.close() 674762Snate@binkert.org 684762Snate@binkert.org # Initialize the global statistics 696001Snate@binkert.org stats.initSimStats() 704762Snate@binkert.org 714762Snate@binkert.org # Create the C++ sim objects and connect ports 727527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.createCCObject() 737527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.connectPorts() 744762Snate@binkert.org 754762Snate@binkert.org # Do a second pass to finish initializing the sim objects 767527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.init() 774762Snate@binkert.org 784762Snate@binkert.org # Do a third pass to initialize statistics 797527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.regStats() 807527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.regFormulas() 814762Snate@binkert.org 826001Snate@binkert.org # We're done registering statistics. Enable the stats package now. 836001Snate@binkert.org stats.enable() 844762Snate@binkert.org 854762Snate@binkert.org # Reset to put the stats in a consistent state. 866001Snate@binkert.org stats.reset() 874762Snate@binkert.org 884762Snate@binkert.orgdef doDot(root): 894762Snate@binkert.org dot = pydot.Dot() 904762Snate@binkert.org instance.outputDot(dot) 914762Snate@binkert.org dot.orientation = "portrait" 924762Snate@binkert.org dot.size = "8.5,11" 934762Snate@binkert.org dot.ranksep="equally" 944762Snate@binkert.org dot.rank="samerank" 954762Snate@binkert.org dot.write("config.dot") 964762Snate@binkert.org dot.write_ps("config.ps") 974762Snate@binkert.org 984762Snate@binkert.orgneed_resume = [] 994762Snate@binkert.orgneed_startup = True 1004762Snate@binkert.orgdef simulate(*args, **kwargs): 1014762Snate@binkert.org global need_resume, need_startup 1024762Snate@binkert.org 1034762Snate@binkert.org if need_startup: 1047527Ssteve.reinhardt@amd.com root = objects.Root.getInstance() 1057527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.startup() 1064762Snate@binkert.org need_startup = False 1074762Snate@binkert.org 1084762Snate@binkert.org for root in need_resume: 1094762Snate@binkert.org resume(root) 1104762Snate@binkert.org need_resume = [] 1114762Snate@binkert.org 1124762Snate@binkert.org return internal.event.simulate(*args, **kwargs) 1134762Snate@binkert.org 1144762Snate@binkert.org# Export curTick to user script. 1154762Snate@binkert.orgdef curTick(): 1164762Snate@binkert.org return internal.core.cvar.curTick 1174762Snate@binkert.org 1184762Snate@binkert.org# Python exit handlers happen in reverse order. We want to dump stats last. 1194762Snate@binkert.orgatexit.register(internal.stats.dump) 1204762Snate@binkert.org 1214762Snate@binkert.org# register our C++ exit callback function with Python 1224762Snate@binkert.orgatexit.register(internal.core.doExitCleanup) 1234762Snate@binkert.org 1244762Snate@binkert.org# This loops until all objects have been fully drained. 1254762Snate@binkert.orgdef doDrain(root): 1264762Snate@binkert.org all_drained = drain(root) 1274762Snate@binkert.org while (not all_drained): 1284762Snate@binkert.org all_drained = drain(root) 1294762Snate@binkert.org 1304762Snate@binkert.org# Tries to drain all objects. Draining might not be completed unless 1314762Snate@binkert.org# all objects return that they are drained on the first call. This is 1324762Snate@binkert.org# because as objects drain they may cause other objects to no longer 1334762Snate@binkert.org# be drained. 1344762Snate@binkert.orgdef drain(root): 1354762Snate@binkert.org all_drained = False 1364762Snate@binkert.org drain_event = internal.event.createCountedDrain() 1377527Ssteve.reinhardt@amd.com unready_objs = sum(obj.drain(drain_event) for obj in root.descendants()) 1384762Snate@binkert.org # If we've got some objects that can't drain immediately, then simulate 1397527Ssteve.reinhardt@amd.com if unready_objs > 0: 1407527Ssteve.reinhardt@amd.com drain_event.setCount(unready_objs) 1414762Snate@binkert.org simulate() 1424762Snate@binkert.org else: 1434762Snate@binkert.org all_drained = True 1444762Snate@binkert.org internal.event.cleanupCountedDrain(drain_event) 1454762Snate@binkert.org return all_drained 1464762Snate@binkert.org 1474762Snate@binkert.orgdef resume(root): 1487527Ssteve.reinhardt@amd.com for obj in root.descendants(): obj.resume() 1494762Snate@binkert.org 1507525Ssteve.reinhardt@amd.comdef checkpoint(dir): 1517525Ssteve.reinhardt@amd.com root = objects.Root.getInstance() 1524762Snate@binkert.org if not isinstance(root, objects.Root): 1534762Snate@binkert.org raise TypeError, "Checkpoint must be called on a root object." 1544762Snate@binkert.org doDrain(root) 1554762Snate@binkert.org print "Writing checkpoint" 1564859Snate@binkert.org internal.core.serializeAll(dir) 1574762Snate@binkert.org resume(root) 1584762Snate@binkert.org 1597525Ssteve.reinhardt@amd.comdef restoreCheckpoint(dir): 1607525Ssteve.reinhardt@amd.com root = objects.Root.getInstance() 1614762Snate@binkert.org print "Restoring from checkpoint" 1624859Snate@binkert.org internal.core.unserializeAll(dir) 1634762Snate@binkert.org need_resume.append(root) 1646977SLisa.Hsu@amd.com stats.reset() 1654762Snate@binkert.org 1664762Snate@binkert.orgdef changeToAtomic(system): 1674762Snate@binkert.org if not isinstance(system, (objects.Root, objects.System)): 1684762Snate@binkert.org raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \ 1694762Snate@binkert.org (type(system), objects.Root, objects.System) 1704945Snate@binkert.org if system.getMemoryMode() != objects.params.atomic: 1714762Snate@binkert.org doDrain(system) 1724762Snate@binkert.org print "Changing memory mode to atomic" 1737527Ssteve.reinhardt@amd.com for obj in system.descendants(): 1747527Ssteve.reinhardt@amd.com obj.changeTiming(objects.params.atomic) 1754762Snate@binkert.org 1764762Snate@binkert.orgdef changeToTiming(system): 1774762Snate@binkert.org if not isinstance(system, (objects.Root, objects.System)): 1784762Snate@binkert.org raise TypeError, "Parameter of type '%s'. Must be type %s or %s." % \ 1794762Snate@binkert.org (type(system), objects.Root, objects.System) 1804762Snate@binkert.org 1814945Snate@binkert.org if system.getMemoryMode() != objects.params.timing: 1824762Snate@binkert.org doDrain(system) 1834762Snate@binkert.org print "Changing memory mode to timing" 1847527Ssteve.reinhardt@amd.com for obj in system.descendants(): 1857527Ssteve.reinhardt@amd.com obj.changeTiming(objects.params.timing) 1864762Snate@binkert.org 1874762Snate@binkert.orgdef switchCpus(cpuList): 1884762Snate@binkert.org print "switching cpus" 1894762Snate@binkert.org if not isinstance(cpuList, list): 1904762Snate@binkert.org raise RuntimeError, "Must pass a list to this function" 1914946Snate@binkert.org for item in cpuList: 1924946Snate@binkert.org if not isinstance(item, tuple) or len(item) != 2: 1934762Snate@binkert.org raise RuntimeError, "List must have tuples of (oldCPU,newCPU)" 1944762Snate@binkert.org 1954946Snate@binkert.org for old_cpu, new_cpu in cpuList: 1964946Snate@binkert.org if not isinstance(old_cpu, objects.BaseCPU): 1974946Snate@binkert.org raise TypeError, "%s is not of type BaseCPU" % old_cpu 1984946Snate@binkert.org if not isinstance(new_cpu, objects.BaseCPU): 1994946Snate@binkert.org raise TypeError, "%s is not of type BaseCPU" % new_cpu 2004762Snate@binkert.org 2014946Snate@binkert.org # Now all of the CPUs are ready to be switched out 2024946Snate@binkert.org for old_cpu, new_cpu in cpuList: 2034946Snate@binkert.org old_cpu._ccObject.switchOut() 2044762Snate@binkert.org 2054946Snate@binkert.org for old_cpu, new_cpu in cpuList: 2064946Snate@binkert.org new_cpu.takeOverFrom(old_cpu) 2075523Snate@binkert.org 2085523Snate@binkert.orgfrom internal.core import disableAllListeners 209