sim_object.hh revision 9554
1/*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
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.
16 *
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.
28 *
29 * Authors: Steve Reinhardt
30 *          Nathan Binkert
31 */
32
33/* @file
34 * User Console Definitions
35 */
36
37#ifndef __SIM_OBJECT_HH__
38#define __SIM_OBJECT_HH__
39
40#include <iostream>
41#include <list>
42#include <map>
43#include <string>
44#include <vector>
45
46#include "enums/MemoryMode.hh"
47#include "params/SimObject.hh"
48#include "sim/drain.hh"
49#include "sim/eventq_impl.hh"
50#include "sim/serialize.hh"
51
52class BaseCPU;
53class Event;
54
55/**
56 * Abstract superclass for simulation objects.  Represents things that
57 * correspond to physical components and can be specified via the
58 * config file (CPUs, caches, etc.).
59 *
60 * SimObject initialization is controlled by the instantiate method in
61 * src/python/m5/simulate.py. There are slightly different
62 * initialization paths when starting the simulation afresh and when
63 * loading from a checkpoint.  After instantiation and connecting
64 * ports, simulate.py initializes the object using the following call
65 * sequence:
66 *
67 * <ol>
68 * <li>SimObject::init()
69 * <li>SimObject::regStats()
70 * <li><ul>
71 *     <li>SimObject::initState() if starting afresh.
72 *     <li>SimObject::loadState() if restoring from a checkpoint.
73 *     </ul>
74 * <li>SimObject::resetStats()
75 * <li>SimObject::startup()
76 * <li>Drainable::drainResume() if resuming from a checkpoint.
77 * </ol>
78 *
79 * @note Whenever a method is called on all objects in the simulator's
80 * object tree (e.g., init(), startup(), or loadState()), a pre-order
81 * depth-first traversal is performed (see descendants() in
82 * SimObject.py). This has the effect of calling the method on the
83 * parent node <i>before</i> its children.
84 */
85class SimObject : public EventManager, public Serializable, public Drainable
86{
87  private:
88    typedef std::vector<SimObject *> SimObjectList;
89
90    /** List of all instantiated simulation objects. */
91    static SimObjectList simObjectList;
92
93  protected:
94    /** Cached copy of the object parameters. */
95    const SimObjectParams *_params;
96
97  public:
98    typedef SimObjectParams Params;
99    const Params *params() const { return _params; }
100    SimObject(const Params *_params);
101    virtual ~SimObject() {}
102
103  public:
104
105    virtual const std::string name() const { return params()->name; }
106
107    /**
108     * init() is called after all C++ SimObjects have been created and
109     * all ports are connected.  Initializations that are independent
110     * of unserialization but rely on a fully instantiated and
111     * connected SimObject graph should be done here.
112     */
113    virtual void init();
114
115    /**
116     * loadState() is called on each SimObject when restoring from a
117     * checkpoint.  The default implementation simply calls
118     * unserialize() if there is a corresponding section in the
119     * checkpoint.  However, objects can override loadState() to get
120     * other behaviors, e.g., doing other programmed initializations
121     * after unserialize(), or complaining if no checkpoint section is
122     * found.
123     *
124     * @param cp Checkpoint to restore the state from.
125     */
126    virtual void loadState(Checkpoint *cp);
127
128    /**
129     * initState() is called on each SimObject when *not* restoring
130     * from a checkpoint.  This provides a hook for state
131     * initializations that are only required for a "cold start".
132     */
133    virtual void initState();
134
135    /**
136     * Register statistics for this object.
137     */
138    virtual void regStats();
139
140    /**
141     * Reset statistics associated with this object.
142     */
143    virtual void resetStats();
144
145    /**
146     * startup() is the final initialization call before simulation.
147     * All state is initialized (including unserialized state, if any,
148     * such as the curTick() value), so this is the appropriate place to
149     * schedule initial event(s) for objects that need them.
150     */
151    virtual void startup();
152
153    /**
154     * Provide a default implementation of the drain interface that
155     * simply returns 0 (draining completed) and sets the drain state
156     * to Drained.
157     */
158    unsigned int drain(DrainManager *drainManger);
159
160    /**
161     * Serialize all SimObjects in the system.
162     */
163    static void serializeAll(std::ostream &os);
164
165#ifdef DEBUG
166  public:
167    bool doDebugBreak;
168    static void debugObjectBreak(const std::string &objs);
169#endif
170
171    /**
172     * Find the SimObject with the given name and return a pointer to
173     * it.  Primarily used for interactive debugging.  Argument is
174     * char* rather than std::string to make it callable from gdb.
175     */
176    static SimObject *find(const char *name);
177};
178
179#ifdef DEBUG
180void debugObjectBreak(const char *objs);
181#endif
182
183#endif // __SIM_OBJECT_HH__
184