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