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