2 * Copyright (c) 2000-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Steve Raasch
36 #include <Python.h> // must be before system headers... see Python docs
38 #include <sys/types.h>
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"
61 #include "mem/mem_object.hh"
62 #include "mem/port.hh"
63 #include "sim/async.hh"
64 #include "sim/builder.hh"
65 #include "sim/host.hh"
66 #include "sim/serialize.hh"
67 #include "sim/sim_events.hh"
68 #include "sim/sim_exit.hh"
69 #include "sim/sim_object.hh"
70 #include "sim/system.hh"
71 #include "sim/stat_control.hh"
72 #include "sim/stats.hh"
73 #include "sim/root.hh"
78 volatile bool async_event
= false;
79 volatile bool async_dump
= false;
80 volatile bool async_dumpreset
= false;
81 volatile bool async_exit
= false;
82 volatile bool async_io
= false;
83 volatile bool async_alarm
= false;
85 /// Stats signal handler.
87 dumpStatsHandler(int sigtype
)
94 dumprstStatsHandler(int sigtype
)
97 async_dumpreset
= true;
100 /// Exit signal handler.
102 exitNowHandler(int sigtype
)
108 /// Abort signal handler.
110 abortHandler(int sigtype
)
112 cerr
<< "Program aborted at cycle " << curTick
<< endl
;
115 // dump trace buffer, if there is one
116 Trace::theLog
.dump(cerr
);
125 main(int argc
, char **argv
)
127 signal(SIGFPE
, SIG_IGN
); // may occur on misspeculated paths
128 signal(SIGTRAP
, SIG_IGN
);
129 signal(SIGUSR1
, dumpStatsHandler
); // dump intermediate stats
130 signal(SIGUSR2
, dumprstStatsHandler
); // dump and reset stats
131 signal(SIGINT
, exitNowHandler
); // dump final stats and exit
132 signal(SIGABRT
, abortHandler
);
134 Py_SetProgramName(argv
[0]);
136 // default path to m5 python code is the currently executing
137 // file... Python ZipImporter will find embedded zip archive.
138 // The M5_ARCHIVE environment variable can be used to override this.
139 char *m5_archive
= getenv("M5_ARCHIVE");
140 string pythonpath
= m5_archive
? m5_archive
: argv
[0];
142 char *oldpath
= getenv("PYTHONPATH");
143 if (oldpath
!= NULL
) {
145 pythonpath
+= oldpath
;
148 if (setenv("PYTHONPATH", pythonpath
.c_str(), true) == -1)
149 fatal("setenv: %s\n", strerror(errno
));
151 char *python_home
= getenv("PYTHONHOME");
153 python_home
= PYTHONHOME
;
154 Py_SetPythonHome(python_home
);
156 // initialize embedded Python interpreter
158 PySys_SetArgv(argc
, argv
);
160 // initialize SWIG 'm5.internal.main' module
163 PyRun_SimpleString("import m5.main");
164 PyRun_SimpleString("m5.main.main()");
166 // clean up Python intepreter.
172 setOutputDir(const string
&dir
)
174 simout
.setDirectory(dir
);
181 createSimObject(const string
&name
)
183 return SimObjectClass::createObject(inifile
, name
);
188 * Pointer to the Python function that maps names to SimObjects.
190 PyObject
*resolveFunc
= NULL
;
193 * Convert a pointer to the Python object that SWIG wraps around a C++
194 * SimObject pointer back to the actual C++ pointer. See main.i.
196 extern "C" SimObject
*convertSwigSimObjectPtr(PyObject
*);
200 resolveSimObject(const string
&name
)
202 PyObject
*pyPtr
= PyEval_CallFunction(resolveFunc
, "(s)", name
.c_str());
205 panic("resolveSimObject: failure on call to Python for %s", name
);
208 SimObject
*simObj
= convertSwigSimObjectPtr(pyPtr
);
210 panic("resolveSimObject: failure on pointer conversion for %s", name
);
217 * Load config.ini into C++ database. Exported to Python via SWIG;
218 * invoked from m5.instantiate().
221 loadIniFile(PyObject
*_resolveFunc
)
223 resolveFunc
= _resolveFunc
;
224 configStream
= simout
.find("config.out");
226 // The configuration database is now complete; start processing it.
227 inifile
.load(simout
.resolve("config.ini"));
229 // Initialize statistics database
230 Stats::InitSimStats();
235 * Look up a MemObject port. Helper function for connectPorts().
238 lookupPort(SimObject
*so
, const std::string
&name
, int i
)
240 MemObject
*mo
= dynamic_cast<MemObject
*>(so
);
242 warn("error casting SimObject %s to MemObject", so
->name());
246 Port
*p
= mo
->getPort(name
, i
);
248 warn("error looking up port %s on object %s", name
, so
->name());
254 * Connect the described MemObject ports. Called from Python via SWIG.
257 connectPorts(SimObject
*o1
, const std::string
&name1
, int i1
,
258 SimObject
*o2
, const std::string
&name2
, int i2
)
260 Port
*p1
= lookupPort(o1
, name1
, i1
);
261 Port
*p2
= lookupPort(o2
, name2
, i2
);
263 if (p1
== NULL
|| p2
== NULL
) {
264 warn("connectPorts: port lookup error");
275 * Do final initialization steps after object construction but before
276 * start of simulation.
281 // Parse and check all non-config-hierarchy parameters.
282 ParamContext::parseAllContexts(inifile
);
283 ParamContext::checkAllContexts();
285 // Echo all parameter settings to stats file as well.
286 ParamContext::showAllContexts(*configStream
);
288 // Do a second pass to finish initializing the sim objects
289 SimObject::initAll();
291 // Restore checkpointed state, if any.
293 configHierarchy
.unserializeSimObjects();
296 SimObject::regAllStats();
298 // Check to make sure that the stats package is properly initialized
301 // Reset to put the stats in a consistent state.
308 /** Simulate for num_cycles additional cycles. If num_cycles is -1
309 * (the default), do not limit simulation; some other event must
310 * terminate the loop. Exported to Python via SWIG.
311 * @return The SimLoopExitEvent that caused the loop to exit.
314 simulate(Tick num_cycles
= MaxTick
)
316 warn("Entering event queue @ %d. Starting simulation...\n", curTick
);
319 fatal("simulate: num_cycles must be >= 0 (was %d)\n", num_cycles
);
320 else if (curTick
+ num_cycles
< 0) //Overflow
321 num_cycles
= MaxTick
;
323 num_cycles
= curTick
+ num_cycles
;
325 Event
*limit_event
= schedExitSimLoop("simulate() limit reached",
329 // there should always be at least one event (the SimLoopExitEvent
330 // we just scheduled) in the queue
331 assert(!mainEventQueue
.empty());
332 assert(curTick
<= mainEventQueue
.nextTick() &&
333 "event scheduled in the past");
335 // forward current cycle to the time of the first event on the
337 curTick
= mainEventQueue
.nextTick();
338 Event
*exit_event
= mainEventQueue
.serviceOne();
339 if (exit_event
!= NULL
) {
340 // hit some kind of exit event; return to Python
341 // event must be subclass of SimLoopExitEvent...
342 SimLoopExitEvent
*se_event
= dynamic_cast<SimLoopExitEvent
*>(exit_event
);
343 if (se_event
== NULL
)
344 panic("Bogus exit event class!");
346 // if we didn't hit limit_event, delete it
347 if (se_event
!= limit_event
) {
348 assert(limit_event
->scheduled());
349 limit_event
->deschedule();
361 using namespace Stats
;
362 SetupEvent(Dump
, curTick
);
365 if (async_dumpreset
) {
366 async_dumpreset
= false;
368 using namespace Stats
;
369 SetupEvent(Dump
| Reset
, curTick
);
374 exitSimLoop("user interrupt received");
377 if (async_io
|| async_alarm
) {
385 // not reached... only exit is return on SimLoopExitEvent
391 return new CountedDrainEvent();
395 cleanupCountedDrain(Event
*counted_drain
)
397 CountedDrainEvent
*event
=
398 dynamic_cast<CountedDrainEvent
*>(counted_drain
);
400 fatal("Called cleanupCountedDrain() on an event that was not "
401 "a CountedDrainEvent.");
403 assert(event
->getCount() == 0);
408 serializeAll(const std::string
&cpt_dir
)
410 Serializable::serializeAll(cpt_dir
);
414 unserializeAll(const std::string
&cpt_dir
)
416 Serializable::unserializeAll(cpt_dir
);
420 * Queue of C++ callbacks to invoke on simulator exit.
425 static CallbackQueue theQueue
;
430 * Register an exit callback.
433 registerExitCallback(Callback
*callback
)
435 exitCallbacks().add(callback
);
439 convertToBaseCPUPtr(SimObject
*obj
)
441 BaseCPU
*ptr
= dynamic_cast<BaseCPU
*>(obj
);
444 warn("Casting to BaseCPU pointer failed");
449 convertToSystemPtr(SimObject
*obj
)
451 System
*ptr
= dynamic_cast<System
*>(obj
);
454 warn("Casting to System pointer failed");
460 * Do C++ simulator exit processing. Exported to SWIG to be invoked
461 * when simulator terminates via Python's atexit mechanism.
466 exitCallbacks().process();
467 exitCallbacks().clear();
471 ParamContext::cleanupAllContexts();
473 // print simulation stats