sim_object.cc revision 843
1/*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <assert.h>
30
31#include "base/callback.hh"
32#include "base/inifile.hh"
33#include "base/misc.hh"
34#include "base/trace.hh"
35#include "sim/configfile.hh"
36#include "sim/host.hh"
37#include "sim/sim_object.hh"
38#include "sim/sim_stats.hh"
39#include "sim/param.hh"
40
41using namespace std;
42
43
44////////////////////////////////////////////////////////////////////////
45//
46// SimObject member definitions
47//
48////////////////////////////////////////////////////////////////////////
49
50//
51// static list of all SimObjects, used for initialization etc.
52//
53SimObject::SimObjectList SimObject::simObjectList;
54
55//
56// SimObject constructor: used to maintain static simObjectList
57//
58SimObject::SimObject(const string &_name)
59    : objName(_name)
60{
61    simObjectList.push_back(this);
62}
63
64void
65SimObject::init()
66{
67}
68
69//
70// no default statistics, so nothing to do in base implementation
71//
72void
73SimObject::regStats()
74{
75}
76
77void
78SimObject::regFormulas()
79{
80}
81
82void
83SimObject::resetStats()
84{
85}
86
87//
88// no default extra output
89//
90void
91SimObject::printExtraOutput(ostream &os)
92{
93}
94
95//
96// static function:
97//   call regStats() on all SimObjects and then regFormulas() on all
98//   SimObjects.
99//
100struct SimObjectResetCB : public Callback
101{
102    virtual void process() { SimObject::resetAllStats(); }
103};
104
105namespace {
106    static SimObjectResetCB StatResetCB;
107}
108
109void
110SimObject::regAllStats()
111{
112    SimObjectList::iterator i;
113    SimObjectList::iterator end = simObjectList.end();
114
115    /**
116     * @todo change cprintfs to DPRINTFs
117     */
118    for (i = simObjectList.begin(); i != end; ++i) {
119#ifdef STAT_DEBUG
120        cprintf("registering stats for %s\n", (*i)->name());
121#endif
122        (*i)->regStats();
123    }
124
125    for (i = simObjectList.begin(); i != end; ++i) {
126#ifdef STAT_DEBUG
127        cprintf("registering formulas for %s\n", (*i)->name());
128#endif
129        (*i)->regFormulas();
130    }
131
132    Statistics::registerResetCallback(&StatResetCB);
133}
134
135//
136// static function: call init() on all SimObjects.
137//
138void
139SimObject::initAll()
140{
141    SimObjectList::iterator i = simObjectList.begin();
142    SimObjectList::iterator end = simObjectList.end();
143
144    for (; i != end; ++i) {
145        SimObject *obj = *i;
146        obj->init();
147    }
148}
149
150//
151// static function: call resetStats() on all SimObjects.
152//
153void
154SimObject::resetAllStats()
155{
156    SimObjectList::iterator i = simObjectList.begin();
157    SimObjectList::iterator end = simObjectList.end();
158
159    for (; i != end; ++i) {
160        SimObject *obj = *i;
161        obj->resetStats();
162    }
163}
164
165//
166// static function: call printExtraOutput() on all SimObjects.
167//
168void
169SimObject::printAllExtraOutput(ostream &os)
170{
171    SimObjectList::iterator i = simObjectList.begin();
172    SimObjectList::iterator end = simObjectList.end();
173
174    for (; i != end; ++i) {
175        SimObject *obj = *i;
176        obj->printExtraOutput(os);
177   }
178}
179
180//
181// static function: serialize all SimObjects.
182//
183void
184SimObject::serializeAll(ostream &os)
185{
186    SimObjectList::reverse_iterator ri = simObjectList.rbegin();
187    SimObjectList::reverse_iterator rend = simObjectList.rend();
188
189    for (; ri != rend; ++ri) {
190        SimObject *obj = *ri;
191        obj->nameOut(os);
192        obj->serialize(os);
193   }
194}
195
196DEFINE_SIM_OBJECT_CLASS_NAME("SimObject", SimObject)
197