main.cc (3144:b6e9e1811d71) main.cc (3202:c095cfd0da96)
1/*
2 * Copyright (c) 2000-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 Raasch
29 * Nathan Binkert
30 * Steve Reinhardt
31 */
32
33///
34/// @file sim/main.cc
35///
36#include <Python.h> // must be before system headers... see Python docs
37
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <errno.h>
41#include <libgen.h>
42#include <stdlib.h>
43#include <signal.h>
44#include <getopt.h>
45
46#include <list>
47#include <string>
48#include <vector>
49
50#include "base/callback.hh"
51#include "base/inifile.hh"
52#include "base/misc.hh"
53#include "base/output.hh"
54#include "base/pollevent.hh"
55#include "base/statistics.hh"
56#include "base/str.hh"
57#include "base/time.hh"
58#include "cpu/base.hh"
59#include "cpu/smt.hh"
60#include "mem/mem_object.hh"
61#include "mem/port.hh"
62#include "sim/async.hh"
63#include "sim/builder.hh"
64#include "sim/host.hh"
65#include "sim/serialize.hh"
66#include "sim/sim_events.hh"
67#include "sim/sim_exit.hh"
68#include "sim/sim_object.hh"
1/*
2 * Copyright (c) 2000-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 Raasch
29 * Nathan Binkert
30 * Steve Reinhardt
31 */
32
33///
34/// @file sim/main.cc
35///
36#include <Python.h> // must be before system headers... see Python docs
37
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <errno.h>
41#include <libgen.h>
42#include <stdlib.h>
43#include <signal.h>
44#include <getopt.h>
45
46#include <list>
47#include <string>
48#include <vector>
49
50#include "base/callback.hh"
51#include "base/inifile.hh"
52#include "base/misc.hh"
53#include "base/output.hh"
54#include "base/pollevent.hh"
55#include "base/statistics.hh"
56#include "base/str.hh"
57#include "base/time.hh"
58#include "cpu/base.hh"
59#include "cpu/smt.hh"
60#include "mem/mem_object.hh"
61#include "mem/port.hh"
62#include "sim/async.hh"
63#include "sim/builder.hh"
64#include "sim/host.hh"
65#include "sim/serialize.hh"
66#include "sim/sim_events.hh"
67#include "sim/sim_exit.hh"
68#include "sim/sim_object.hh"
69#include "sim/system.hh"
69#include "sim/stat_control.hh"
70#include "sim/stats.hh"
71#include "sim/root.hh"
72
73using namespace std;
74
75// See async.h.
76volatile bool async_event = false;
77volatile bool async_dump = false;
78volatile bool async_dumpreset = false;
79volatile bool async_exit = false;
80volatile bool async_io = false;
81volatile bool async_alarm = false;
82
83/// Stats signal handler.
84void
85dumpStatsHandler(int sigtype)
86{
87 async_event = true;
88 async_dump = true;
89}
90
91void
92dumprstStatsHandler(int sigtype)
93{
94 async_event = true;
95 async_dumpreset = true;
96}
97
98/// Exit signal handler.
99void
100exitNowHandler(int sigtype)
101{
102 async_event = true;
103 async_exit = true;
104}
105
106/// Abort signal handler.
107void
108abortHandler(int sigtype)
109{
110 cerr << "Program aborted at cycle " << curTick << endl;
111
112#if TRACING_ON
113 // dump trace buffer, if there is one
114 Trace::theLog.dump(cerr);
115#endif
116}
117
118extern "C" { void init_cc_main(); }
119
120int
121main(int argc, char **argv)
122{
123 signal(SIGFPE, SIG_IGN); // may occur on misspeculated paths
124 signal(SIGTRAP, SIG_IGN);
125 signal(SIGUSR1, dumpStatsHandler); // dump intermediate stats
126 signal(SIGUSR2, dumprstStatsHandler); // dump and reset stats
127 signal(SIGINT, exitNowHandler); // dump final stats and exit
128 signal(SIGABRT, abortHandler);
129
130 Py_SetProgramName(argv[0]);
131
132 // default path to m5 python code is the currently executing
133 // file... Python ZipImporter will find embedded zip archive.
134 // The M5_ARCHIVE environment variable can be used to override this.
135 char *m5_archive = getenv("M5_ARCHIVE");
136 string pythonpath = m5_archive ? m5_archive : argv[0];
137
138 char *oldpath = getenv("PYTHONPATH");
139 if (oldpath != NULL) {
140 pythonpath += ":";
141 pythonpath += oldpath;
142 }
143
144 if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1)
145 fatal("setenv: %s\n", strerror(errno));
146
147 // initialize embedded Python interpreter
148 Py_Initialize();
149 PySys_SetArgv(argc, argv);
150
151 // initialize SWIG 'cc_main' module
152 init_cc_main();
153
154 PyRun_SimpleString("import m5.main");
155 PyRun_SimpleString("m5.main.main()");
156
157 // clean up Python intepreter.
158 Py_Finalize();
159}
160
161
162void
163setOutputDir(const string &dir)
164{
165 simout.setDirectory(dir);
166}
167
168
169IniFile inifile;
170
171SimObject *
172createSimObject(const string &name)
173{
174 return SimObjectClass::createObject(inifile, name);
175}
176
177
178/**
179 * Pointer to the Python function that maps names to SimObjects.
180 */
181PyObject *resolveFunc = NULL;
182
183/**
184 * Convert a pointer to the Python object that SWIG wraps around a C++
185 * SimObject pointer back to the actual C++ pointer. See main.i.
186 */
187extern "C" SimObject *convertSwigSimObjectPtr(PyObject *);
188
189
190SimObject *
191resolveSimObject(const string &name)
192{
193 PyObject *pyPtr = PyEval_CallFunction(resolveFunc, "(s)", name.c_str());
194 if (pyPtr == NULL) {
195 PyErr_Print();
196 panic("resolveSimObject: failure on call to Python for %s", name);
197 }
198
199 SimObject *simObj = convertSwigSimObjectPtr(pyPtr);
200 if (simObj == NULL)
201 panic("resolveSimObject: failure on pointer conversion for %s", name);
202
203 return simObj;
204}
205
206
207/**
208 * Load config.ini into C++ database. Exported to Python via SWIG;
209 * invoked from m5.instantiate().
210 */
211void
212loadIniFile(PyObject *_resolveFunc)
213{
214 resolveFunc = _resolveFunc;
215 configStream = simout.find("config.out");
216
217 // The configuration database is now complete; start processing it.
218 inifile.load(simout.resolve("config.ini"));
219
220 // Initialize statistics database
221 Stats::InitSimStats();
222}
223
224
225/**
226 * Look up a MemObject port. Helper function for connectPorts().
227 */
228Port *
229lookupPort(SimObject *so, const std::string &name, int i)
230{
231 MemObject *mo = dynamic_cast<MemObject *>(so);
232 if (mo == NULL) {
233 warn("error casting SimObject %s to MemObject", so->name());
234 return NULL;
235 }
236
237 Port *p = mo->getPort(name, i);
238 if (p == NULL)
239 warn("error looking up port %s on object %s", name, so->name());
240 return p;
241}
242
243
244/**
245 * Connect the described MemObject ports. Called from Python via SWIG.
246 */
247int
248connectPorts(SimObject *o1, const std::string &name1, int i1,
249 SimObject *o2, const std::string &name2, int i2)
250{
251 Port *p1 = lookupPort(o1, name1, i1);
252 Port *p2 = lookupPort(o2, name2, i2);
253
254 if (p1 == NULL || p2 == NULL) {
255 warn("connectPorts: port lookup error");
256 return 0;
257 }
258
259 p1->setPeer(p2);
260 p2->setPeer(p1);
261
262 return 1;
263}
264
265/**
266 * Do final initialization steps after object construction but before
267 * start of simulation.
268 */
269void
270finalInit()
271{
272 // Parse and check all non-config-hierarchy parameters.
273 ParamContext::parseAllContexts(inifile);
274 ParamContext::checkAllContexts();
275
276 // Echo all parameter settings to stats file as well.
277 ParamContext::showAllContexts(*configStream);
278
279 // Do a second pass to finish initializing the sim objects
280 SimObject::initAll();
281
282 // Restore checkpointed state, if any.
283#if 0
284 configHierarchy.unserializeSimObjects();
285#endif
286
287 SimObject::regAllStats();
288
289 // Check to make sure that the stats package is properly initialized
290 Stats::check();
291
292 // Reset to put the stats in a consistent state.
293 Stats::reset();
294
295 SimStartup();
296}
297
298
299/** Simulate for num_cycles additional cycles. If num_cycles is -1
300 * (the default), do not limit simulation; some other event must
301 * terminate the loop. Exported to Python via SWIG.
302 * @return The SimLoopExitEvent that caused the loop to exit.
303 */
304SimLoopExitEvent *
305simulate(Tick num_cycles = -1)
306{
307 warn("Entering event queue @ %d. Starting simulation...\n", curTick);
308
309 // Fix up num_cycles. Special default value -1 means simulate
310 // "forever"... schedule event at MaxTick just to be safe.
311 // Otherwise it's a delta for additional cycles to simulate past
312 // curTick, and thus must be non-negative.
313 if (num_cycles == -1)
314 num_cycles = MaxTick;
315 else if (num_cycles < 0)
316 fatal("simulate: num_cycles must be >= 0 (was %d)\n", num_cycles);
317 else
318 num_cycles = curTick + num_cycles;
319
320 Event *limit_event = schedExitSimLoop("simulate() limit reached",
321 num_cycles);
322
323 while (1) {
324 // there should always be at least one event (the SimLoopExitEvent
325 // we just scheduled) in the queue
326 assert(!mainEventQueue.empty());
327 assert(curTick <= mainEventQueue.nextTick() &&
328 "event scheduled in the past");
329
330 // forward current cycle to the time of the first event on the
331 // queue
332 curTick = mainEventQueue.nextTick();
333 Event *exit_event = mainEventQueue.serviceOne();
334 if (exit_event != NULL) {
335 // hit some kind of exit event; return to Python
336 // event must be subclass of SimLoopExitEvent...
337 SimLoopExitEvent *se_event = dynamic_cast<SimLoopExitEvent *>(exit_event);
338 if (se_event == NULL)
339 panic("Bogus exit event class!");
340
341 // if we didn't hit limit_event, delete it
342 if (se_event != limit_event) {
343 assert(limit_event->scheduled());
344 limit_event->deschedule();
345 delete limit_event;
346 }
347
348 return se_event;
349 }
350
351 if (async_event) {
352 async_event = false;
353 if (async_dump) {
354 async_dump = false;
355
356 using namespace Stats;
357 SetupEvent(Dump, curTick);
358 }
359
360 if (async_dumpreset) {
361 async_dumpreset = false;
362
363 using namespace Stats;
364 SetupEvent(Dump | Reset, curTick);
365 }
366
367 if (async_exit) {
368 async_exit = false;
369 exitSimLoop("user interrupt received");
370 }
371
372 if (async_io || async_alarm) {
373 async_io = false;
374 async_alarm = false;
375 pollQueue.service();
376 }
377 }
378 }
379
380 // not reached... only exit is return on SimLoopExitEvent
381}
382
383Event *
384createCountedDrain()
385{
386 return new CountedDrainEvent();
387}
388
389void
390cleanupCountedDrain(Event *counted_drain)
391{
392 CountedDrainEvent *event =
393 dynamic_cast<CountedDrainEvent *>(counted_drain);
394 if (event == NULL) {
395 fatal("Called cleanupCountedDrain() on an event that was not "
396 "a CountedDrainEvent.");
397 }
398 assert(event->getCount() == 0);
399 delete event;
400}
401
402void
403serializeAll(const std::string &cpt_dir)
404{
405 Serializable::serializeAll(cpt_dir);
406}
407
408void
409unserializeAll(const std::string &cpt_dir)
410{
411 Serializable::unserializeAll(cpt_dir);
412}
413
414/**
415 * Queue of C++ callbacks to invoke on simulator exit.
416 */
417CallbackQueue&
418exitCallbacks()
419{
420 static CallbackQueue theQueue;
421 return theQueue;
422}
423
424/**
425 * Register an exit callback.
426 */
427void
428registerExitCallback(Callback *callback)
429{
430 exitCallbacks().add(callback);
431}
432
433BaseCPU *
434convertToBaseCPUPtr(SimObject *obj)
435{
436 BaseCPU *ptr = dynamic_cast<BaseCPU *>(obj);
437
438 if (ptr == NULL)
439 warn("Casting to BaseCPU pointer failed");
440 return ptr;
441}
442
70#include "sim/stat_control.hh"
71#include "sim/stats.hh"
72#include "sim/root.hh"
73
74using namespace std;
75
76// See async.h.
77volatile bool async_event = false;
78volatile bool async_dump = false;
79volatile bool async_dumpreset = false;
80volatile bool async_exit = false;
81volatile bool async_io = false;
82volatile bool async_alarm = false;
83
84/// Stats signal handler.
85void
86dumpStatsHandler(int sigtype)
87{
88 async_event = true;
89 async_dump = true;
90}
91
92void
93dumprstStatsHandler(int sigtype)
94{
95 async_event = true;
96 async_dumpreset = true;
97}
98
99/// Exit signal handler.
100void
101exitNowHandler(int sigtype)
102{
103 async_event = true;
104 async_exit = true;
105}
106
107/// Abort signal handler.
108void
109abortHandler(int sigtype)
110{
111 cerr << "Program aborted at cycle " << curTick << endl;
112
113#if TRACING_ON
114 // dump trace buffer, if there is one
115 Trace::theLog.dump(cerr);
116#endif
117}
118
119extern "C" { void init_cc_main(); }
120
121int
122main(int argc, char **argv)
123{
124 signal(SIGFPE, SIG_IGN); // may occur on misspeculated paths
125 signal(SIGTRAP, SIG_IGN);
126 signal(SIGUSR1, dumpStatsHandler); // dump intermediate stats
127 signal(SIGUSR2, dumprstStatsHandler); // dump and reset stats
128 signal(SIGINT, exitNowHandler); // dump final stats and exit
129 signal(SIGABRT, abortHandler);
130
131 Py_SetProgramName(argv[0]);
132
133 // default path to m5 python code is the currently executing
134 // file... Python ZipImporter will find embedded zip archive.
135 // The M5_ARCHIVE environment variable can be used to override this.
136 char *m5_archive = getenv("M5_ARCHIVE");
137 string pythonpath = m5_archive ? m5_archive : argv[0];
138
139 char *oldpath = getenv("PYTHONPATH");
140 if (oldpath != NULL) {
141 pythonpath += ":";
142 pythonpath += oldpath;
143 }
144
145 if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1)
146 fatal("setenv: %s\n", strerror(errno));
147
148 // initialize embedded Python interpreter
149 Py_Initialize();
150 PySys_SetArgv(argc, argv);
151
152 // initialize SWIG 'cc_main' module
153 init_cc_main();
154
155 PyRun_SimpleString("import m5.main");
156 PyRun_SimpleString("m5.main.main()");
157
158 // clean up Python intepreter.
159 Py_Finalize();
160}
161
162
163void
164setOutputDir(const string &dir)
165{
166 simout.setDirectory(dir);
167}
168
169
170IniFile inifile;
171
172SimObject *
173createSimObject(const string &name)
174{
175 return SimObjectClass::createObject(inifile, name);
176}
177
178
179/**
180 * Pointer to the Python function that maps names to SimObjects.
181 */
182PyObject *resolveFunc = NULL;
183
184/**
185 * Convert a pointer to the Python object that SWIG wraps around a C++
186 * SimObject pointer back to the actual C++ pointer. See main.i.
187 */
188extern "C" SimObject *convertSwigSimObjectPtr(PyObject *);
189
190
191SimObject *
192resolveSimObject(const string &name)
193{
194 PyObject *pyPtr = PyEval_CallFunction(resolveFunc, "(s)", name.c_str());
195 if (pyPtr == NULL) {
196 PyErr_Print();
197 panic("resolveSimObject: failure on call to Python for %s", name);
198 }
199
200 SimObject *simObj = convertSwigSimObjectPtr(pyPtr);
201 if (simObj == NULL)
202 panic("resolveSimObject: failure on pointer conversion for %s", name);
203
204 return simObj;
205}
206
207
208/**
209 * Load config.ini into C++ database. Exported to Python via SWIG;
210 * invoked from m5.instantiate().
211 */
212void
213loadIniFile(PyObject *_resolveFunc)
214{
215 resolveFunc = _resolveFunc;
216 configStream = simout.find("config.out");
217
218 // The configuration database is now complete; start processing it.
219 inifile.load(simout.resolve("config.ini"));
220
221 // Initialize statistics database
222 Stats::InitSimStats();
223}
224
225
226/**
227 * Look up a MemObject port. Helper function for connectPorts().
228 */
229Port *
230lookupPort(SimObject *so, const std::string &name, int i)
231{
232 MemObject *mo = dynamic_cast<MemObject *>(so);
233 if (mo == NULL) {
234 warn("error casting SimObject %s to MemObject", so->name());
235 return NULL;
236 }
237
238 Port *p = mo->getPort(name, i);
239 if (p == NULL)
240 warn("error looking up port %s on object %s", name, so->name());
241 return p;
242}
243
244
245/**
246 * Connect the described MemObject ports. Called from Python via SWIG.
247 */
248int
249connectPorts(SimObject *o1, const std::string &name1, int i1,
250 SimObject *o2, const std::string &name2, int i2)
251{
252 Port *p1 = lookupPort(o1, name1, i1);
253 Port *p2 = lookupPort(o2, name2, i2);
254
255 if (p1 == NULL || p2 == NULL) {
256 warn("connectPorts: port lookup error");
257 return 0;
258 }
259
260 p1->setPeer(p2);
261 p2->setPeer(p1);
262
263 return 1;
264}
265
266/**
267 * Do final initialization steps after object construction but before
268 * start of simulation.
269 */
270void
271finalInit()
272{
273 // Parse and check all non-config-hierarchy parameters.
274 ParamContext::parseAllContexts(inifile);
275 ParamContext::checkAllContexts();
276
277 // Echo all parameter settings to stats file as well.
278 ParamContext::showAllContexts(*configStream);
279
280 // Do a second pass to finish initializing the sim objects
281 SimObject::initAll();
282
283 // Restore checkpointed state, if any.
284#if 0
285 configHierarchy.unserializeSimObjects();
286#endif
287
288 SimObject::regAllStats();
289
290 // Check to make sure that the stats package is properly initialized
291 Stats::check();
292
293 // Reset to put the stats in a consistent state.
294 Stats::reset();
295
296 SimStartup();
297}
298
299
300/** Simulate for num_cycles additional cycles. If num_cycles is -1
301 * (the default), do not limit simulation; some other event must
302 * terminate the loop. Exported to Python via SWIG.
303 * @return The SimLoopExitEvent that caused the loop to exit.
304 */
305SimLoopExitEvent *
306simulate(Tick num_cycles = -1)
307{
308 warn("Entering event queue @ %d. Starting simulation...\n", curTick);
309
310 // Fix up num_cycles. Special default value -1 means simulate
311 // "forever"... schedule event at MaxTick just to be safe.
312 // Otherwise it's a delta for additional cycles to simulate past
313 // curTick, and thus must be non-negative.
314 if (num_cycles == -1)
315 num_cycles = MaxTick;
316 else if (num_cycles < 0)
317 fatal("simulate: num_cycles must be >= 0 (was %d)\n", num_cycles);
318 else
319 num_cycles = curTick + num_cycles;
320
321 Event *limit_event = schedExitSimLoop("simulate() limit reached",
322 num_cycles);
323
324 while (1) {
325 // there should always be at least one event (the SimLoopExitEvent
326 // we just scheduled) in the queue
327 assert(!mainEventQueue.empty());
328 assert(curTick <= mainEventQueue.nextTick() &&
329 "event scheduled in the past");
330
331 // forward current cycle to the time of the first event on the
332 // queue
333 curTick = mainEventQueue.nextTick();
334 Event *exit_event = mainEventQueue.serviceOne();
335 if (exit_event != NULL) {
336 // hit some kind of exit event; return to Python
337 // event must be subclass of SimLoopExitEvent...
338 SimLoopExitEvent *se_event = dynamic_cast<SimLoopExitEvent *>(exit_event);
339 if (se_event == NULL)
340 panic("Bogus exit event class!");
341
342 // if we didn't hit limit_event, delete it
343 if (se_event != limit_event) {
344 assert(limit_event->scheduled());
345 limit_event->deschedule();
346 delete limit_event;
347 }
348
349 return se_event;
350 }
351
352 if (async_event) {
353 async_event = false;
354 if (async_dump) {
355 async_dump = false;
356
357 using namespace Stats;
358 SetupEvent(Dump, curTick);
359 }
360
361 if (async_dumpreset) {
362 async_dumpreset = false;
363
364 using namespace Stats;
365 SetupEvent(Dump | Reset, curTick);
366 }
367
368 if (async_exit) {
369 async_exit = false;
370 exitSimLoop("user interrupt received");
371 }
372
373 if (async_io || async_alarm) {
374 async_io = false;
375 async_alarm = false;
376 pollQueue.service();
377 }
378 }
379 }
380
381 // not reached... only exit is return on SimLoopExitEvent
382}
383
384Event *
385createCountedDrain()
386{
387 return new CountedDrainEvent();
388}
389
390void
391cleanupCountedDrain(Event *counted_drain)
392{
393 CountedDrainEvent *event =
394 dynamic_cast<CountedDrainEvent *>(counted_drain);
395 if (event == NULL) {
396 fatal("Called cleanupCountedDrain() on an event that was not "
397 "a CountedDrainEvent.");
398 }
399 assert(event->getCount() == 0);
400 delete event;
401}
402
403void
404serializeAll(const std::string &cpt_dir)
405{
406 Serializable::serializeAll(cpt_dir);
407}
408
409void
410unserializeAll(const std::string &cpt_dir)
411{
412 Serializable::unserializeAll(cpt_dir);
413}
414
415/**
416 * Queue of C++ callbacks to invoke on simulator exit.
417 */
418CallbackQueue&
419exitCallbacks()
420{
421 static CallbackQueue theQueue;
422 return theQueue;
423}
424
425/**
426 * Register an exit callback.
427 */
428void
429registerExitCallback(Callback *callback)
430{
431 exitCallbacks().add(callback);
432}
433
434BaseCPU *
435convertToBaseCPUPtr(SimObject *obj)
436{
437 BaseCPU *ptr = dynamic_cast<BaseCPU *>(obj);
438
439 if (ptr == NULL)
440 warn("Casting to BaseCPU pointer failed");
441 return ptr;
442}
443
444System *
445convertToSystemPtr(SimObject *obj)
446{
447 System *ptr = dynamic_cast<System *>(obj);
448
449 if (ptr == NULL)
450 warn("Casting to System pointer failed");
451 return ptr;
452}
453
454
443/**
444 * Do C++ simulator exit processing. Exported to SWIG to be invoked
445 * when simulator terminates via Python's atexit mechanism.
446 */
447void
448doExitCleanup()
449{
450 exitCallbacks().process();
451 exitCallbacks().clear();
452
453 cout.flush();
454
455 ParamContext::cleanupAllContexts();
456
457 // print simulation stats
458 Stats::DumpNow();
459}
455/**
456 * Do C++ simulator exit processing. Exported to SWIG to be invoked
457 * when simulator terminates via Python's atexit mechanism.
458 */
459void
460doExitCleanup()
461{
462 exitCallbacks().process();
463 exitCallbacks().clear();
464
465 cout.flush();
466
467 ParamContext::cleanupAllContexts();
468
469 // print simulation stats
470 Stats::DumpNow();
471}