simulate.py revision 7492:acc1fbbef239
1# Copyright (c) 2005 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28#          Steve Reinhardt
29
30import atexit
31import os
32import sys
33
34# import the SWIG-wrapped main C++ functions
35import internal
36import core
37import stats
38from main import options
39import SimObject
40import ticks
41import objects
42
43# define a MaxTick parameter
44MaxTick = 2**63 - 1
45
46# The final hook to generate .ini files.  Called from the user script
47# once the config is built.
48def instantiate(root):
49    # we need to fix the global frequency
50    ticks.fixGlobalFrequency()
51
52    root.unproxy_all()
53
54    if options.dump_config:
55        ini_file = file(os.path.join(options.outdir, options.dump_config), 'w')
56        root.print_ini(ini_file)
57        ini_file.close()
58
59    # Initialize the global statistics
60    stats.initSimStats()
61
62    # Create the C++ sim objects and connect ports
63    root.createCCObject()
64    root.connectPorts()
65
66    # Do a second pass to finish initializing the sim objects
67    core.initAll()
68
69    # Do a third pass to initialize statistics
70    core.regAllStats()
71
72    # We're done registering statistics.  Enable the stats package now.
73    stats.enable()
74
75    # Reset to put the stats in a consistent state.
76    stats.reset()
77
78def doDot(root):
79    dot = pydot.Dot()
80    instance.outputDot(dot)
81    dot.orientation = "portrait"
82    dot.size = "8.5,11"
83    dot.ranksep="equally"
84    dot.rank="samerank"
85    dot.write("config.dot")
86    dot.write_ps("config.ps")
87
88need_resume = []
89need_startup = True
90def simulate(*args, **kwargs):
91    global need_resume, need_startup
92
93    if need_startup:
94        internal.core.startupAll()
95        need_startup = False
96
97    for root in need_resume:
98        resume(root)
99    need_resume = []
100
101    return internal.event.simulate(*args, **kwargs)
102
103# Export curTick to user script.
104def curTick():
105    return internal.core.cvar.curTick
106
107# Python exit handlers happen in reverse order.  We want to dump stats last.
108atexit.register(internal.stats.dump)
109
110# register our C++ exit callback function with Python
111atexit.register(internal.core.doExitCleanup)
112
113# This loops until all objects have been fully drained.
114def doDrain(root):
115    all_drained = drain(root)
116    while (not all_drained):
117        all_drained = drain(root)
118
119# Tries to drain all objects.  Draining might not be completed unless
120# all objects return that they are drained on the first call.  This is
121# because as objects drain they may cause other objects to no longer
122# be drained.
123def drain(root):
124    all_drained = False
125    drain_event = internal.event.createCountedDrain()
126    unready_objects = root.startDrain(drain_event, True)
127    # If we've got some objects that can't drain immediately, then simulate
128    if unready_objects > 0:
129        drain_event.setCount(unready_objects)
130        simulate()
131    else:
132        all_drained = True
133    internal.event.cleanupCountedDrain(drain_event)
134    return all_drained
135
136def resume(root):
137    root.resume()
138
139def checkpoint(root, dir):
140    if not isinstance(root, objects.Root):
141        raise TypeError, "Checkpoint must be called on a root object."
142    doDrain(root)
143    print "Writing checkpoint"
144    internal.core.serializeAll(dir)
145    resume(root)
146
147def restoreCheckpoint(root, dir):
148    print "Restoring from checkpoint"
149    internal.core.unserializeAll(dir)
150    need_resume.append(root)
151    stats.reset()
152
153def changeToAtomic(system):
154    if not isinstance(system, (objects.Root, objects.System)):
155        raise TypeError, "Parameter of type '%s'.  Must be type %s or %s." % \
156              (type(system), objects.Root, objects.System)
157    if system.getMemoryMode() != objects.params.atomic:
158        doDrain(system)
159        print "Changing memory mode to atomic"
160        system.changeTiming(objects.params.atomic)
161
162def changeToTiming(system):
163    if not isinstance(system, (objects.Root, objects.System)):
164        raise TypeError, "Parameter of type '%s'.  Must be type %s or %s." % \
165              (type(system), objects.Root, objects.System)
166
167    if system.getMemoryMode() != objects.params.timing:
168        doDrain(system)
169        print "Changing memory mode to timing"
170        system.changeTiming(objects.params.timing)
171
172def switchCpus(cpuList):
173    print "switching cpus"
174    if not isinstance(cpuList, list):
175        raise RuntimeError, "Must pass a list to this function"
176    for item in cpuList:
177        if not isinstance(item, tuple) or len(item) != 2:
178            raise RuntimeError, "List must have tuples of (oldCPU,newCPU)"
179
180    for old_cpu, new_cpu in cpuList:
181        if not isinstance(old_cpu, objects.BaseCPU):
182            raise TypeError, "%s is not of type BaseCPU" % old_cpu
183        if not isinstance(new_cpu, objects.BaseCPU):
184            raise TypeError, "%s is not of type BaseCPU" % new_cpu
185
186    # Now all of the CPUs are ready to be switched out
187    for old_cpu, new_cpu in cpuList:
188        old_cpu._ccObject.switchOut()
189
190    for old_cpu, new_cpu in cpuList:
191        new_cpu.takeOverFrom(old_cpu)
192
193from internal.core import disableAllListeners
194