2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Authors: Steve Reinhardt
34 * User Console Definitions
37 #ifndef __SIM_OBJECT_HH__
38 #define __SIM_OBJECT_HH__
46 #include "enums/MemoryMode.hh"
47 #include "params/SimObject.hh"
48 #include "sim/eventq.hh"
49 #include "sim/serialize.hh"
55 * Abstract superclass for simulation objects. Represents things that
56 * correspond to physical components and can be specified via the
57 * config file (CPUs, caches, etc.).
59 * SimObject initialization is controlled by the instantiate method in
60 * src/python/m5/simulate.py. There are slightly different
61 * initialization paths when starting the simulation afresh and when
62 * loading from a checkpoint. After instantiation and connecting
63 * ports, simulate.py initializes the object using the following call
67 * <li>SimObject::init()
68 * <li>SimObject::regStats()
70 * <li>SimObject::initState() if starting afresh.
71 * <li>SimObject::loadState() if restoring from a checkpoint.
73 * <li>SimObject::resetStats()
74 * <li>SimObject::startup()
75 * <li>SimObject::resume() if resuming from a checkpoint.
78 * An object's internal state needs to be drained when creating a
79 * checkpoint, switching between CPU models, or switching between
80 * timing models. Once the internal state has been drained from
81 * <i>all</i> objects in the system, the objects are serialized to
82 * disc or the configuration change takes place. The process works as
83 * follows (see simulate.py for details):
86 * <li>An instance of a CountedDrainEvent is created to keep track of
87 * how many objects need to be drained. The object maintains an
88 * internal counter that is decreased every time its
89 * CountedDrainEvent::process() method is called. When the counter
90 * reaches zero, the simulation is stopped.
92 * <li>Call SimObject::drain() for every object in the
93 * system. Draining has completed if all of them return
94 * zero. Otherwise, the sum of the return values is loaded into
95 * the counter of the CountedDrainEvent. A pointer of the drain
96 * event is passed as an argument to the drain() method.
98 * <li>Continue simulation. When an object has finished draining its
99 * internal state, it calls CountedDrainEvent::process() on the
100 * CountedDrainEvent. When counter in the CountedDrainEvent reaches
101 * zero, the simulation stops.
103 * <li>Check if any object still needs draining, if so repeat the
106 * <li>Serialize objects, switch CPU model, or change timing model.
108 * <li>Call SimObject::resume() and continue the simulation.
111 * @note Whenever a method is called on all objects in the simulator's
112 * object tree (e.g., init(), startup(), or loadState()), a pre-order
113 * depth-first traversal is performed (see descendants() in
114 * SimObject.py). This has the effect of calling the method on the
115 * parent node <i>before</i> its children.
117 class SimObject : public EventManager, public Serializable
121 * Object drain/handover states
123 * An object starts out in the Running state. When the simulator
124 * prepares to take a snapshot or prepares a CPU for handover, it
125 * calls the drain() method to transfer the object into the
126 * Draining or Drained state. If any object enters the Draining
127 * state (drain() returning >0), simulation continues until it all
128 * objects have entered the Drained.
130 * The before resuming simulation, the simulator calls resume() to
131 * transfer the object to the Running state.
133 * \note Even though the state of an object (visible to the rest
134 * of the world through getState()) could be used to determine if
135 * all objects have entered the Drained state, the protocol is
136 * actually a bit more elaborate. See drain() for details.
139 Running, /** Running normally */
140 Draining, /** Draining buffers pending serialization/handover */
141 Drained /** Buffers drained, ready for serialization/handover */
148 void changeState(State new_state) { state = new_state; }
151 State getState() { return state; }
154 typedef std::vector<SimObject *> SimObjectList;
156 /** List of all instantiated simulation objects. */
157 static SimObjectList simObjectList;
160 /** Cached copy of the object parameters. */
161 const SimObjectParams *_params;
164 typedef SimObjectParams Params;
165 const Params *params() const { return _params; }
166 SimObject(const Params *_params);
167 virtual ~SimObject() {}
171 virtual const std::string name() const { return params()->name; }
174 * init() is called after all C++ SimObjects have been created and
175 * all ports are connected. Initializations that are independent
176 * of unserialization but rely on a fully instantiated and
177 * connected SimObject graph should be done here.
182 * loadState() is called on each SimObject when restoring from a
183 * checkpoint. The default implementation simply calls
184 * unserialize() if there is a corresponding section in the
185 * checkpoint. However, objects can override loadState() to get
186 * other behaviors, e.g., doing other programmed initializations
187 * after unserialize(), or complaining if no checkpoint section is
190 * @param cp Checkpoint to restore the state from.
192 virtual void loadState(Checkpoint *cp);
195 * initState() is called on each SimObject when *not* restoring
196 * from a checkpoint. This provides a hook for state
197 * initializations that are only required for a "cold start".
199 virtual void initState();
202 * Register statistics for this object.
204 virtual void regStats();
207 * Reset statistics associated with this object.
209 virtual void resetStats();
212 * startup() is the final initialization call before simulation.
213 * All state is initialized (including unserialized state, if any,
214 * such as the curTick() value), so this is the appropriate place to
215 * schedule initial event(s) for objects that need them.
217 virtual void startup();
220 * Serialize all SimObjects in the system.
222 static void serializeAll(std::ostream &os);
225 * Determine if an object needs draining and register a drain
228 * When draining the state of an object, the simulator calls drain
229 * with a pointer to a drain event. If the object does not need
230 * further simulation to drain internal buffers, it switched to
231 * the Drained state and returns 0, otherwise it switches to the
232 * Draining state and returns the number of times that it will
233 * call Event::process() on the drain event. Most objects are
234 * expected to return either 0 or 1.
236 * The default implementation simply switches to the Drained state
239 * @note An object that has entered the Drained state can be
240 * disturbed by other objects in the system and consequently be
241 * forced to enter the Draining state again. The simulator
242 * therefore repeats the draining process until all objects return
243 * 0 on the first call to drain().
245 * @param drain_event Event to use to inform the simulator when
246 * the draining has completed.
248 * @return 0 if the object is ready for serialization now, >0 if
249 * it needs further simulation.
251 virtual unsigned int drain(Event *drain_event);
254 * Switch an object in the Drained stated into the Running state.
256 virtual void resume();
259 * Change the memory mode the simulator operates in.
261 * @note Should only be implemented in the System object.
263 virtual void setMemoryMode(Enums::MemoryMode new_mode);
266 * Prepare a CPU model to be switched out, invoked on active CPUs
267 * that are about to be replaced.
269 * @note This should only be implemented in CPU models.
271 virtual void switchOut();
274 * Load the state of a CPU from the previous CPU object, invoked
275 * on all new CPUs that are about to be switched in.
277 * A CPU model implementing this method is expected to initialize
278 * its state from the old CPU and connect its memory (unless they
279 * are already connected) to the memories connected to the old
282 * @note This should only be implemented in CPU models.
284 * @param cpu CPU to initialize read state from.
286 virtual void takeOverFrom(BaseCPU *cpu);
291 static void debugObjectBreak(const std::string &objs);
295 * Find the SimObject with the given name and return a pointer to
296 * it. Primarily used for interactive debugging. Argument is
297 * char* rather than std::string to make it callable from gdb.
299 static SimObject *find(const char *name);
302 #endif // __SIM_OBJECT_HH__