sim_object.hh revision 10422
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; 54class ProbeManager; 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 /** Manager coordinates hooking up probe points with listeners. */ 94 ProbeManager *probeManager; 95 96 protected: 97 /** Cached copy of the object parameters. */ 98 const SimObjectParams *_params; 99 100 public: 101 typedef SimObjectParams Params; 102 const Params *params() const { return _params; } 103 SimObject(const Params *_params); 104 virtual ~SimObject(); 105 106 public: 107 108 virtual const std::string name() const { return params()->name; } 109 110 /** 111 * init() is called after all C++ SimObjects have been created and 112 * all ports are connected. Initializations that are independent 113 * of unserialization but rely on a fully instantiated and 114 * connected SimObject graph should be done here. 115 */ 116 virtual void init(); 117 118 /** 119 * loadState() is called on each SimObject when restoring from a 120 * checkpoint. The default implementation simply calls 121 * unserialize() if there is a corresponding section in the 122 * checkpoint. However, objects can override loadState() to get 123 * other behaviors, e.g., doing other programmed initializations 124 * after unserialize(), or complaining if no checkpoint section is 125 * found. 126 * 127 * @param cp Checkpoint to restore the state from. 128 */ 129 virtual void loadState(Checkpoint *cp); 130 131 /** 132 * initState() is called on each SimObject when *not* restoring 133 * from a checkpoint. This provides a hook for state 134 * initializations that are only required for a "cold start". 135 */ 136 virtual void initState(); 137 138 /** 139 * Register statistics for this object. 140 */ 141 virtual void regStats(); 142 143 /** 144 * Reset statistics associated with this object. 145 */ 146 virtual void resetStats(); 147 148 /** 149 * Register probe points for this object. 150 */ 151 virtual void regProbePoints(); 152 153 /** 154 * Register probe listeners for this object. 155 */ 156 virtual void regProbeListeners(); 157 158 /** 159 * Get the probe manager for this object. 160 */ 161 ProbeManager *getProbeManager(); 162 163 /** 164 * startup() is the final initialization call before simulation. 165 * All state is initialized (including unserialized state, if any, 166 * such as the curTick() value), so this is the appropriate place to 167 * schedule initial event(s) for objects that need them. 168 */ 169 virtual void startup(); 170 171 /** 172 * Provide a default implementation of the drain interface that 173 * simply returns 0 (draining completed) and sets the drain state 174 * to Drained. 175 */ 176 unsigned int drain(DrainManager *drainManger); 177 178 /** 179 * Serialize all SimObjects in the system. 180 */ 181 static void serializeAll(std::ostream &os); 182 183#ifdef DEBUG 184 public: 185 bool doDebugBreak; 186 static void debugObjectBreak(const std::string &objs); 187#endif 188 189 /** 190 * Find the SimObject with the given name and return a pointer to 191 * it. Primarily used for interactive debugging. Argument is 192 * char* rather than std::string to make it callable from gdb. 193 */ 194 static SimObject *find(const char *name); 195}; 196 197#ifdef DEBUG 198void debugObjectBreak(const char *objs); 199#endif 200 201#endif // __SIM_OBJECT_HH__ 202