sim_object.hh revision 9554
12SN/A/*
21762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
37534Ssteve.reinhardt@amd.com * Copyright (c) 2010 Advanced Micro Devices, Inc.
42SN/A * All rights reserved.
52SN/A *
62SN/A * Redistribution and use in source and binary forms, with or without
72SN/A * modification, are permitted provided that the following conditions are
82SN/A * met: redistributions of source code must retain the above copyright
92SN/A * notice, this list of conditions and the following disclaimer;
102SN/A * redistributions in binary form must reproduce the above copyright
112SN/A * notice, this list of conditions and the following disclaimer in the
122SN/A * documentation and/or other materials provided with the distribution;
132SN/A * neither the name of the copyright holders nor the names of its
142SN/A * contributors may be used to endorse or promote products derived from
152SN/A * this software without specific prior written permission.
162SN/A *
172SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu *
292665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
302665Ssaidi@eecs.umich.edu *          Nathan Binkert
312SN/A */
322SN/A
332SN/A/* @file
342SN/A * User Console Definitions
352SN/A */
362SN/A
372SN/A#ifndef __SIM_OBJECT_HH__
382SN/A#define __SIM_OBJECT_HH__
392SN/A
405491Sgblack@eecs.umich.edu#include <iostream>
415491Sgblack@eecs.umich.edu#include <list>
422SN/A#include <map>
435491Sgblack@eecs.umich.edu#include <string>
442SN/A#include <vector>
452SN/A
468737Skoansin.tan@gmail.com#include "enums/MemoryMode.hh"
474762Snate@binkert.org#include "params/SimObject.hh"
489342SAndreas.Sandberg@arm.com#include "sim/drain.hh"
499356Snilay@cs.wisc.edu#include "sim/eventq_impl.hh"
5056SN/A#include "sim/serialize.hh"
512SN/A
522797Sktlim@umich.educlass BaseCPU;
532797Sktlim@umich.educlass Event;
542609SN/A
559196SAndreas.Sandberg@arm.com/**
562SN/A * Abstract superclass for simulation objects.  Represents things that
572SN/A * correspond to physical components and can be specified via the
582SN/A * config file (CPUs, caches, etc.).
599196SAndreas.Sandberg@arm.com *
609196SAndreas.Sandberg@arm.com * SimObject initialization is controlled by the instantiate method in
619196SAndreas.Sandberg@arm.com * src/python/m5/simulate.py. There are slightly different
629196SAndreas.Sandberg@arm.com * initialization paths when starting the simulation afresh and when
639196SAndreas.Sandberg@arm.com * loading from a checkpoint.  After instantiation and connecting
649196SAndreas.Sandberg@arm.com * ports, simulate.py initializes the object using the following call
659196SAndreas.Sandberg@arm.com * sequence:
669196SAndreas.Sandberg@arm.com *
679196SAndreas.Sandberg@arm.com * <ol>
689196SAndreas.Sandberg@arm.com * <li>SimObject::init()
699196SAndreas.Sandberg@arm.com * <li>SimObject::regStats()
709196SAndreas.Sandberg@arm.com * <li><ul>
719196SAndreas.Sandberg@arm.com *     <li>SimObject::initState() if starting afresh.
729196SAndreas.Sandberg@arm.com *     <li>SimObject::loadState() if restoring from a checkpoint.
739196SAndreas.Sandberg@arm.com *     </ul>
749196SAndreas.Sandberg@arm.com * <li>SimObject::resetStats()
759196SAndreas.Sandberg@arm.com * <li>SimObject::startup()
769342SAndreas.Sandberg@arm.com * <li>Drainable::drainResume() if resuming from a checkpoint.
779196SAndreas.Sandberg@arm.com * </ol>
789196SAndreas.Sandberg@arm.com *
799196SAndreas.Sandberg@arm.com * @note Whenever a method is called on all objects in the simulator's
809196SAndreas.Sandberg@arm.com * object tree (e.g., init(), startup(), or loadState()), a pre-order
819196SAndreas.Sandberg@arm.com * depth-first traversal is performed (see descendants() in
829196SAndreas.Sandberg@arm.com * SimObject.py). This has the effect of calling the method on the
839196SAndreas.Sandberg@arm.com * parent node <i>before</i> its children.
842SN/A */
859342SAndreas.Sandberg@arm.comclass SimObject : public EventManager, public Serializable, public Drainable
862SN/A{
872SN/A  private:
882SN/A    typedef std::vector<SimObject *> SimObjectList;
892SN/A
909196SAndreas.Sandberg@arm.com    /** List of all instantiated simulation objects. */
912SN/A    static SimObjectList simObjectList;
922SN/A
934762Snate@binkert.org  protected:
949196SAndreas.Sandberg@arm.com    /** Cached copy of the object parameters. */
954762Snate@binkert.org    const SimObjectParams *_params;
964762Snate@binkert.org
972SN/A  public:
984762Snate@binkert.org    typedef SimObjectParams Params;
994762Snate@binkert.org    const Params *params() const { return _params; }
1004762Snate@binkert.org    SimObject(const Params *_params);
1012SN/A    virtual ~SimObject() {}
1022SN/A
1035034Smilesck@eecs.umich.edu  public:
1045034Smilesck@eecs.umich.edu
1051553SN/A    virtual const std::string name() const { return params()->name; }
106265SN/A
1077532Ssteve.reinhardt@amd.com    /**
1087532Ssteve.reinhardt@amd.com     * init() is called after all C++ SimObjects have been created and
1097532Ssteve.reinhardt@amd.com     * all ports are connected.  Initializations that are independent
1107532Ssteve.reinhardt@amd.com     * of unserialization but rely on a fully instantiated and
1117532Ssteve.reinhardt@amd.com     * connected SimObject graph should be done here.
1127532Ssteve.reinhardt@amd.com     */
113465SN/A    virtual void init();
114465SN/A
1157532Ssteve.reinhardt@amd.com    /**
1167532Ssteve.reinhardt@amd.com     * loadState() is called on each SimObject when restoring from a
1177532Ssteve.reinhardt@amd.com     * checkpoint.  The default implementation simply calls
1187532Ssteve.reinhardt@amd.com     * unserialize() if there is a corresponding section in the
1197532Ssteve.reinhardt@amd.com     * checkpoint.  However, objects can override loadState() to get
1207532Ssteve.reinhardt@amd.com     * other behaviors, e.g., doing other programmed initializations
1217532Ssteve.reinhardt@amd.com     * after unserialize(), or complaining if no checkpoint section is
1227532Ssteve.reinhardt@amd.com     * found.
1239196SAndreas.Sandberg@arm.com     *
1249196SAndreas.Sandberg@arm.com     * @param cp Checkpoint to restore the state from.
1257532Ssteve.reinhardt@amd.com     */
1267532Ssteve.reinhardt@amd.com    virtual void loadState(Checkpoint *cp);
1277532Ssteve.reinhardt@amd.com
1287532Ssteve.reinhardt@amd.com    /**
1297532Ssteve.reinhardt@amd.com     * initState() is called on each SimObject when *not* restoring
1307532Ssteve.reinhardt@amd.com     * from a checkpoint.  This provides a hook for state
1317532Ssteve.reinhardt@amd.com     * initializations that are only required for a "cold start".
1327532Ssteve.reinhardt@amd.com     */
1337532Ssteve.reinhardt@amd.com    virtual void initState();
1347532Ssteve.reinhardt@amd.com
1359196SAndreas.Sandberg@arm.com    /**
1369196SAndreas.Sandberg@arm.com     * Register statistics for this object.
1379196SAndreas.Sandberg@arm.com     */
1382SN/A    virtual void regStats();
1399196SAndreas.Sandberg@arm.com
1409196SAndreas.Sandberg@arm.com    /**
1419196SAndreas.Sandberg@arm.com     * Reset statistics associated with this object.
1429196SAndreas.Sandberg@arm.com     */
143330SN/A    virtual void resetStats();
1442SN/A
1457532Ssteve.reinhardt@amd.com    /**
1467532Ssteve.reinhardt@amd.com     * startup() is the final initialization call before simulation.
1477532Ssteve.reinhardt@amd.com     * All state is initialized (including unserialized state, if any,
1487823Ssteve.reinhardt@amd.com     * such as the curTick() value), so this is the appropriate place to
1497532Ssteve.reinhardt@amd.com     * schedule initial event(s) for objects that need them.
1507532Ssteve.reinhardt@amd.com     */
1517492Ssteve.reinhardt@amd.com    virtual void startup();
152330SN/A
1539196SAndreas.Sandberg@arm.com    /**
1549342SAndreas.Sandberg@arm.com     * Provide a default implementation of the drain interface that
1559342SAndreas.Sandberg@arm.com     * simply returns 0 (draining completed) and sets the drain state
1569342SAndreas.Sandberg@arm.com     * to Drained.
1579342SAndreas.Sandberg@arm.com     */
1589342SAndreas.Sandberg@arm.com    unsigned int drain(DrainManager *drainManger);
1599342SAndreas.Sandberg@arm.com
1609342SAndreas.Sandberg@arm.com    /**
1619196SAndreas.Sandberg@arm.com     * Serialize all SimObjects in the system.
1629196SAndreas.Sandberg@arm.com     */
1639196SAndreas.Sandberg@arm.com    static void serializeAll(std::ostream &os);
164938SN/A
1651031SN/A#ifdef DEBUG
1661031SN/A  public:
1671031SN/A    bool doDebugBreak;
1681031SN/A    static void debugObjectBreak(const std::string &objs);
1691031SN/A#endif
1701031SN/A
1715314Sstever@gmail.com    /**
1725314Sstever@gmail.com     * Find the SimObject with the given name and return a pointer to
1735315Sstever@gmail.com     * it.  Primarily used for interactive debugging.  Argument is
1745314Sstever@gmail.com     * char* rather than std::string to make it callable from gdb.
1755314Sstever@gmail.com     */
1765314Sstever@gmail.com    static SimObject *find(const char *name);
1772SN/A};
1782SN/A
1799554Sandreas.hansson@arm.com#ifdef DEBUG
1809554Sandreas.hansson@arm.comvoid debugObjectBreak(const char *objs);
1819554Sandreas.hansson@arm.com#endif
1829554Sandreas.hansson@arm.com
1832SN/A#endif // __SIM_OBJECT_HH__
184