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