sim_object.cc revision 4076:e1c56b6b3072
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 * Authors: Steve Reinhardt
29 *          Nathan Binkert
30 */
31
32#include <assert.h>
33
34#include "base/callback.hh"
35#include "base/inifile.hh"
36#include "base/match.hh"
37#include "base/misc.hh"
38#include "base/trace.hh"
39#include "base/stats/events.hh"
40#include "sim/host.hh"
41#include "sim/sim_object.hh"
42#include "sim/stats.hh"
43#include "sim/param.hh"
44
45using namespace std;
46
47
48////////////////////////////////////////////////////////////////////////
49//
50// SimObject member definitions
51//
52////////////////////////////////////////////////////////////////////////
53
54//
55// static list of all SimObjects, used for initialization etc.
56//
57SimObject::SimObjectList SimObject::simObjectList;
58
59//
60// SimObject constructor: used to maintain static simObjectList
61//
62SimObject::SimObject(Params *p)
63    : _params(p)
64{
65#ifdef DEBUG
66    doDebugBreak = false;
67#endif
68
69    simObjectList.push_back(this);
70    state = Running;
71}
72
73//
74// SimObject constructor: used to maintain static simObjectList
75//
76SimObject::SimObject(const string &_name)
77    : _params(new Params)
78{
79    _params->name = _name;
80#ifdef DEBUG
81    doDebugBreak = false;
82#endif
83
84    simObjectList.push_back(this);
85    state = Running;
86}
87
88void
89SimObject::init()
90{
91}
92
93//
94// no default statistics, so nothing to do in base implementation
95//
96void
97SimObject::regStats()
98{
99}
100
101void
102SimObject::regFormulas()
103{
104}
105
106void
107SimObject::resetStats()
108{
109}
110
111//
112// static function:
113//   call regStats() on all SimObjects and then regFormulas() on all
114//   SimObjects.
115//
116struct SimObjectResetCB : public Callback
117{
118    virtual void process() { SimObject::resetAllStats(); }
119};
120
121namespace {
122    static SimObjectResetCB StatResetCB;
123}
124
125void
126SimObject::regAllStats()
127{
128    SimObjectList::iterator i;
129    SimObjectList::iterator end = simObjectList.end();
130
131    /**
132     * @todo change cprintfs to DPRINTFs
133     */
134    for (i = simObjectList.begin(); i != end; ++i) {
135#ifdef STAT_DEBUG
136        cprintf("registering stats for %s\n", (*i)->name());
137#endif
138        (*i)->regStats();
139    }
140
141    for (i = simObjectList.begin(); i != end; ++i) {
142#ifdef STAT_DEBUG
143        cprintf("registering formulas for %s\n", (*i)->name());
144#endif
145        (*i)->regFormulas();
146    }
147
148    Stats::registerResetCallback(&StatResetCB);
149}
150
151//
152// static function: call init() on all SimObjects.
153//
154void
155SimObject::initAll()
156{
157    SimObjectList::iterator i = simObjectList.begin();
158    SimObjectList::iterator end = simObjectList.end();
159
160    for (; i != end; ++i) {
161        SimObject *obj = *i;
162        obj->init();
163    }
164}
165
166//
167// static function: call resetStats() on all SimObjects.
168//
169void
170SimObject::resetAllStats()
171{
172    SimObjectList::iterator i = simObjectList.begin();
173    SimObjectList::iterator end = simObjectList.end();
174
175    for (; i != end; ++i) {
176        SimObject *obj = *i;
177        obj->resetStats();
178    }
179}
180
181//
182// static function: serialize all SimObjects.
183//
184void
185SimObject::serializeAll(ostream &os)
186{
187    SimObjectList::reverse_iterator ri = simObjectList.rbegin();
188    SimObjectList::reverse_iterator rend = simObjectList.rend();
189
190    for (; ri != rend; ++ri) {
191        SimObject *obj = *ri;
192        obj->nameOut(os);
193        obj->serialize(os);
194   }
195}
196
197void
198SimObject::unserializeAll(Checkpoint *cp)
199{
200    SimObjectList::reverse_iterator ri = simObjectList.rbegin();
201    SimObjectList::reverse_iterator rend = simObjectList.rend();
202
203    for (; ri != rend; ++ri) {
204        SimObject *obj = *ri;
205        DPRINTFR(Config, "Unserializing '%s'\n",
206                 obj->name());
207        if(cp->sectionExists(obj->name()))
208            obj->unserialize(cp, obj->name());
209        else
210            warn("Not unserializing '%s': no section found in checkpoint.\n",
211                 obj->name());
212   }
213}
214
215#ifdef DEBUG
216//
217// static function: flag which objects should have the debugger break
218//
219void
220SimObject::debugObjectBreak(const string &objs)
221{
222    SimObjectList::const_iterator i = simObjectList.begin();
223    SimObjectList::const_iterator end = simObjectList.end();
224
225    ObjectMatch match(objs);
226    for (; i != end; ++i) {
227        SimObject *obj = *i;
228        obj->doDebugBreak = match.match(obj->name());
229   }
230}
231
232void
233debugObjectBreak(const char *objs)
234{
235    SimObject::debugObjectBreak(string(objs));
236}
237#endif
238
239void
240SimObject::recordEvent(const std::string &stat)
241{
242    Stats::recordEvent(stat);
243}
244
245unsigned int
246SimObject::drain(Event *drain_event)
247{
248    state = Drained;
249    return 0;
250}
251
252void
253SimObject::resume()
254{
255    state = Running;
256}
257
258void
259SimObject::setMemoryMode(State new_mode)
260{
261    panic("setMemoryMode() should only be called on systems");
262}
263
264void
265SimObject::switchOut()
266{
267    panic("Unimplemented!");
268}
269
270void
271SimObject::takeOverFrom(BaseCPU *cpu)
272{
273    panic("Unimplemented!");
274}
275
276DEFINE_SIM_OBJECT_CLASS_NAME("SimObject", SimObject)
277