__init__.py revision 3102
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 472969Sktlim@umich.edudef makeList(objOrList): 482969Sktlim@umich.edu if isinstance(objOrList, list): 492969Sktlim@umich.edu return objOrList 502969Sktlim@umich.edu return [objOrList] 512969Sktlim@umich.edu 522667Sstever@eecs.umich.edu# Prepend given directory to system module search path. We may not 532667Sstever@eecs.umich.edu# need this anymore if we can structure our config library more like a 542667Sstever@eecs.umich.edu# Python package. 551692SN/Adef AddToPath(path): 561869SN/A # if it's a relative path and we know what directory the current 571869SN/A # python script is in, make the path relative to that directory. 581869SN/A if not os.path.isabs(path) and sys.path[0]: 591869SN/A path = os.path.join(sys.path[0], path) 601692SN/A path = os.path.realpath(path) 611869SN/A # sys.path[0] should always refer to the current script's directory, 621869SN/A # so place the new dir right after that. 631869SN/A sys.path.insert(1, path) 641581SN/A 651530SN/A# make a SmartDict out of the build options for our local use 661530SN/Aimport smartdict 671530SN/Abuild_env = smartdict.SmartDict() 682667Sstever@eecs.umich.edubuild_env.update(defines.m5_build_env) 691530SN/A 701530SN/A# make a SmartDict out of the OS environment too 711530SN/Aenv = smartdict.SmartDict() 721530SN/Aenv.update(os.environ) 731530SN/A 742667Sstever@eecs.umich.edu# The final hook to generate .ini files. Called from the user script 752667Sstever@eecs.umich.edu# once the config is built. 762667Sstever@eecs.umich.edudef instantiate(root): 773101Sstever@eecs.umich.edu params.ticks_per_sec = float(root.clock.frequency) 782667Sstever@eecs.umich.edu # ugly temporary hack to get output to config.ini 792762Sstever@eecs.umich.edu sys.stdout = file(os.path.join(options.outdir, 'config.ini'), 'w') 802667Sstever@eecs.umich.edu root.print_ini() 812667Sstever@eecs.umich.edu sys.stdout.close() # close config.ini 822667Sstever@eecs.umich.edu sys.stdout = sys.__stdout__ # restore to original 832763Sstever@eecs.umich.edu cc_main.loadIniFile(resolveSimObject) # load config.ini into C++ 842738Sstever@eecs.umich.edu root.createCCObject() 852738Sstever@eecs.umich.edu root.connectPorts() 862763Sstever@eecs.umich.edu cc_main.finalInit() 872667Sstever@eecs.umich.edu noDot = True # temporary until we fix dot 882667Sstever@eecs.umich.edu if not noDot: 892667Sstever@eecs.umich.edu dot = pydot.Dot() 902667Sstever@eecs.umich.edu instance.outputDot(dot) 912667Sstever@eecs.umich.edu dot.orientation = "portrait" 922667Sstever@eecs.umich.edu dot.size = "8.5,11" 932667Sstever@eecs.umich.edu dot.ranksep="equally" 942667Sstever@eecs.umich.edu dot.rank="samerank" 952667Sstever@eecs.umich.edu dot.write("config.dot") 962667Sstever@eecs.umich.edu dot.write_ps("config.ps") 971527SN/A 982667Sstever@eecs.umich.edu# Export curTick to user script. 992667Sstever@eecs.umich.edudef curTick(): 1002763Sstever@eecs.umich.edu return cc_main.cvar.curTick 1011511SN/A 1022667Sstever@eecs.umich.edu# register our C++ exit callback function with Python 1032763Sstever@eecs.umich.eduatexit.register(cc_main.doExitCleanup) 1042655Sstever@eecs.umich.edu 1052860Sktlim@umich.edu# This loops until all objects have been fully drained. 1062839Sktlim@umich.edudef doDrain(root): 1072860Sktlim@umich.edu all_drained = drain(root) 1082860Sktlim@umich.edu while (not all_drained): 1092860Sktlim@umich.edu all_drained = drain(root) 1102860Sktlim@umich.edu 1112860Sktlim@umich.edu# Tries to drain all objects. Draining might not be completed unless 1122860Sktlim@umich.edu# all objects return that they are drained on the first call. This is 1132860Sktlim@umich.edu# because as objects drain they may cause other objects to no longer 1142860Sktlim@umich.edu# be drained. 1152860Sktlim@umich.edudef drain(root): 1162860Sktlim@umich.edu all_drained = False 1172839Sktlim@umich.edu drain_event = cc_main.createCountedDrain() 1182839Sktlim@umich.edu unready_objects = root.startDrain(drain_event, True) 1192839Sktlim@umich.edu # If we've got some objects that can't drain immediately, then simulate 1202797Sktlim@umich.edu if unready_objects > 0: 1212839Sktlim@umich.edu drain_event.setCount(unready_objects) 1222797Sktlim@umich.edu simulate() 1232860Sktlim@umich.edu else: 1242860Sktlim@umich.edu all_drained = True 1252839Sktlim@umich.edu cc_main.cleanupCountedDrain(drain_event) 1262860Sktlim@umich.edu return all_drained 1272797Sktlim@umich.edu 1282797Sktlim@umich.edudef resume(root): 1292797Sktlim@umich.edu root.resume() 1302797Sktlim@umich.edu 1312868Sktlim@umich.edudef checkpoint(root, dir): 1322797Sktlim@umich.edu if not isinstance(root, objects.Root): 1332797Sktlim@umich.edu raise TypeError, "Object is not a root object. Checkpoint must be called on a root object." 1342839Sktlim@umich.edu doDrain(root) 1352797Sktlim@umich.edu print "Writing checkpoint" 1362868Sktlim@umich.edu cc_main.serializeAll(dir) 1372797Sktlim@umich.edu resume(root) 1382797Sktlim@umich.edu 1392868Sktlim@umich.edudef restoreCheckpoint(root, dir): 1402797Sktlim@umich.edu print "Restoring from checkpoint" 1412868Sktlim@umich.edu cc_main.unserializeAll(dir) 1422865Sktlim@umich.edu resume(root) 1432797Sktlim@umich.edu 1442797Sktlim@umich.edudef changeToAtomic(system): 1452797Sktlim@umich.edu if not isinstance(system, objects.Root) and not isinstance(system, System): 1462797Sktlim@umich.edu raise TypeError, "Object is not a root or system object. Checkpoint must be " 1472797Sktlim@umich.edu "called on a root object." 1482839Sktlim@umich.edu doDrain(system) 1492797Sktlim@umich.edu print "Changing memory mode to atomic" 1502797Sktlim@umich.edu system.changeTiming(cc_main.SimObject.Atomic) 1512797Sktlim@umich.edu resume(system) 1522797Sktlim@umich.edu 1532797Sktlim@umich.edudef changeToTiming(system): 1542797Sktlim@umich.edu if not isinstance(system, objects.Root) and not isinstance(system, System): 1552797Sktlim@umich.edu raise TypeError, "Object is not a root or system object. Checkpoint must be " 1562797Sktlim@umich.edu "called on a root object." 1572839Sktlim@umich.edu doDrain(system) 1582797Sktlim@umich.edu print "Changing memory mode to timing" 1592797Sktlim@umich.edu system.changeTiming(cc_main.SimObject.Timing) 1602797Sktlim@umich.edu resume(system) 1612797Sktlim@umich.edu 1622797Sktlim@umich.edudef switchCpus(cpuList): 1632797Sktlim@umich.edu if not isinstance(cpuList, list): 1642797Sktlim@umich.edu raise RuntimeError, "Must pass a list to this function" 1652797Sktlim@umich.edu for i in cpuList: 1662797Sktlim@umich.edu if not isinstance(i, tuple): 1672797Sktlim@umich.edu raise RuntimeError, "List must have tuples of (oldCPU,newCPU)" 1682797Sktlim@umich.edu 1692797Sktlim@umich.edu [old_cpus, new_cpus] = zip(*cpuList) 1702797Sktlim@umich.edu 1712797Sktlim@umich.edu for cpu in old_cpus: 1722797Sktlim@umich.edu if not isinstance(cpu, objects.BaseCPU): 1732797Sktlim@umich.edu raise TypeError, "%s is not of type BaseCPU", cpu 1742797Sktlim@umich.edu for cpu in new_cpus: 1752797Sktlim@umich.edu if not isinstance(cpu, objects.BaseCPU): 1762797Sktlim@umich.edu raise TypeError, "%s is not of type BaseCPU", cpu 1772797Sktlim@umich.edu 1782839Sktlim@umich.edu # Drain all of the individual CPUs 1792839Sktlim@umich.edu drain_event = cc_main.createCountedDrain() 1802797Sktlim@umich.edu unready_cpus = 0 1812797Sktlim@umich.edu for old_cpu in old_cpus: 1822839Sktlim@umich.edu unready_cpus += old_cpu.startDrain(drain_event, False) 1832839Sktlim@umich.edu # If we've got some objects that can't drain immediately, then simulate 1842797Sktlim@umich.edu if unready_cpus > 0: 1852839Sktlim@umich.edu drain_event.setCount(unready_cpus) 1862797Sktlim@umich.edu simulate() 1872839Sktlim@umich.edu cc_main.cleanupCountedDrain(drain_event) 1882797Sktlim@umich.edu # Now all of the CPUs are ready to be switched out 1892797Sktlim@umich.edu for old_cpu in old_cpus: 1902797Sktlim@umich.edu old_cpu._ccObject.switchOut() 1912797Sktlim@umich.edu index = 0 1922797Sktlim@umich.edu print "Switching CPUs" 1932797Sktlim@umich.edu for new_cpu in new_cpus: 1942797Sktlim@umich.edu new_cpu.takeOverFrom(old_cpus[index]) 1952797Sktlim@umich.edu new_cpu._ccObject.resume() 1962797Sktlim@umich.edu index += 1 1973101Sstever@eecs.umich.edu 1983101Sstever@eecs.umich.edu# Since we have so many mutual imports in this package, we should: 1993101Sstever@eecs.umich.edu# 1. Put all intra-package imports at the *bottom* of the file, unless 2003101Sstever@eecs.umich.edu# they're absolutely needed before that (for top-level statements 2013101Sstever@eecs.umich.edu# or class attributes). Imports of "trivial" packages that don't 2023101Sstever@eecs.umich.edu# import other packages (e.g., 'smartdict') can be at the top. 2033101Sstever@eecs.umich.edu# 2. Never use 'from foo import *' on an intra-package import since 2043101Sstever@eecs.umich.edu# you can get the wrong result if foo is only partially imported 2053101Sstever@eecs.umich.edu# at the point you do that (i.e., because foo is in the middle of 2063101Sstever@eecs.umich.edu# importing *you*). 2073102Sstever@eecs.umich.edufrom main import options 2083101Sstever@eecs.umich.eduimport objects 2093101Sstever@eecs.umich.eduimport params 2103102Sstever@eecs.umich.edufrom SimObject import resolveSimObject 211