sim_object.cc revision 2499
1/*
2 * Copyright (c) 2001-2005 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/match.hh"
34#include "base/misc.hh"
35#include "base/trace.hh"
36#include "base/stats/events.hh"
37#include "sim/configfile.hh"
38#include "sim/host.hh"
39#include "sim/sim_object.hh"
40#include "sim/stats.hh"
41#include "sim/param.hh"
42
43using namespace std;
44
45
46////////////////////////////////////////////////////////////////////////
47//
48// SimObject member definitions
49//
50////////////////////////////////////////////////////////////////////////
51
52//
53// static list of all SimObjects, used for initialization etc.
54//
55SimObject::SimObjectList SimObject::simObjectList;
56
57namespace Stats {
58    extern ObjectMatch event_ignore;
59}
60
61//
62// SimObject constructor: used to maintain static simObjectList
63//
64SimObject::SimObject(Params *p)
65    : _params(p)
66{
67#ifdef DEBUG
68    doDebugBreak = false;
69#endif
70
71    doRecordEvent = !Stats::event_ignore.match(name());
72    simObjectList.push_back(this);
73}
74
75//
76// SimObject constructor: used to maintain static simObjectList
77//
78SimObject::SimObject(const string &_name)
79    : _params(new Params)
80{
81    _params->name = _name;
82#ifdef DEBUG
83    doDebugBreak = false;
84#endif
85
86    doRecordEvent = !Stats::event_ignore.match(name());
87    simObjectList.push_back(this);
88}
89
90void
91SimObject::connect()
92{
93}
94
95void
96SimObject::init()
97{
98}
99
100//
101// no default statistics, so nothing to do in base implementation
102//
103void
104SimObject::regStats()
105{
106}
107
108void
109SimObject::regFormulas()
110{
111}
112
113void
114SimObject::resetStats()
115{
116}
117
118//
119// static function:
120//   call regStats() on all SimObjects and then regFormulas() on all
121//   SimObjects.
122//
123struct SimObjectResetCB : public Callback
124{
125    virtual void process() { SimObject::resetAllStats(); }
126};
127
128namespace {
129    static SimObjectResetCB StatResetCB;
130}
131
132void
133SimObject::regAllStats()
134{
135    SimObjectList::iterator i;
136    SimObjectList::iterator end = simObjectList.end();
137
138    /**
139     * @todo change cprintfs to DPRINTFs
140     */
141    for (i = simObjectList.begin(); i != end; ++i) {
142#ifdef STAT_DEBUG
143        cprintf("registering stats for %s\n", (*i)->name());
144#endif
145        (*i)->regStats();
146    }
147
148    for (i = simObjectList.begin(); i != end; ++i) {
149#ifdef STAT_DEBUG
150        cprintf("registering formulas for %s\n", (*i)->name());
151#endif
152        (*i)->regFormulas();
153    }
154
155    Stats::registerResetCallback(&StatResetCB);
156}
157
158//
159// static function: call connect() on all SimObjects.
160//
161void
162SimObject::connectAll()
163{
164    SimObjectList::iterator i = simObjectList.begin();
165    SimObjectList::iterator end = simObjectList.end();
166
167    for (; i != end; ++i) {
168        SimObject *obj = *i;
169        obj->connect();
170    }
171}
172
173//
174// static function: call init() on all SimObjects.
175//
176void
177SimObject::initAll()
178{
179    SimObjectList::iterator i = simObjectList.begin();
180    SimObjectList::iterator end = simObjectList.end();
181
182    for (; i != end; ++i) {
183        SimObject *obj = *i;
184        obj->init();
185    }
186}
187
188//
189// static function: call resetStats() on all SimObjects.
190//
191void
192SimObject::resetAllStats()
193{
194    SimObjectList::iterator i = simObjectList.begin();
195    SimObjectList::iterator end = simObjectList.end();
196
197    for (; i != end; ++i) {
198        SimObject *obj = *i;
199        obj->resetStats();
200    }
201}
202
203//
204// static function: serialize all SimObjects.
205//
206void
207SimObject::serializeAll(ostream &os)
208{
209    SimObjectList::reverse_iterator ri = simObjectList.rbegin();
210    SimObjectList::reverse_iterator rend = simObjectList.rend();
211
212    for (; ri != rend; ++ri) {
213        SimObject *obj = *ri;
214        obj->nameOut(os);
215        obj->serialize(os);
216   }
217}
218
219#ifdef DEBUG
220//
221// static function: flag which objects should have the debugger break
222//
223void
224SimObject::debugObjectBreak(const string &objs)
225{
226    SimObjectList::const_iterator i = simObjectList.begin();
227    SimObjectList::const_iterator end = simObjectList.end();
228
229    ObjectMatch match(objs);
230    for (; i != end; ++i) {
231        SimObject *obj = *i;
232        obj->doDebugBreak = match.match(obj->name());
233   }
234}
235
236extern "C"
237void
238debugObjectBreak(const char *objs)
239{
240    SimObject::debugObjectBreak(string(objs));
241}
242#endif
243
244void
245SimObject::recordEvent(const std::string &stat)
246{
247    if (doRecordEvent)
248        Stats::recordEvent(stat);
249}
250
251DEFINE_SIM_OBJECT_CLASS_NAME("SimObject", SimObject)
252