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.
32 #include <Python.h> // must be before system headers... see Python docs
34 #include <sys/types.h>
45 #include "base/inifile.hh"
46 #include "base/misc.hh"
47 #include "base/output.hh"
48 #include "base/pollevent.hh"
49 #include "base/statistics.hh"
50 #include "base/str.hh"
51 #include "base/time.hh"
52 #include "cpu/base.hh"
54 #include "sim/async.hh"
55 #include "sim/builder.hh"
56 #include "sim/configfile.hh"
57 #include "sim/host.hh"
58 #include "sim/sim_events.hh"
59 #include "sim/sim_exit.hh"
60 #include "sim/sim_object.hh"
61 #include "sim/stat_control.hh"
62 #include "sim/stats.hh"
63 #include "sim/root.hh"
68 volatile bool async_event
= false;
69 volatile bool async_dump
= false;
70 volatile bool async_dumpreset
= false;
71 volatile bool async_exit
= false;
72 volatile bool async_io
= false;
73 volatile bool async_alarm
= false;
75 /// Stats signal handler.
77 dumpStatsHandler(int sigtype
)
84 dumprstStatsHandler(int sigtype
)
87 async_dumpreset
= true;
90 /// Exit signal handler.
92 exitNowHandler(int sigtype
)
98 /// Abort signal handler.
100 abortHandler(int sigtype
)
102 cerr
<< "Program aborted at cycle " << curTick
<< endl
;
105 // dump trace buffer, if there is one
106 Trace::theLog
.dump(cerr
);
110 /// Simulator executable name
111 char *myProgName
= "";
114 /// Echo the command line for posterity in such a way that it can be
115 /// used to rerun the same simulation (given the same .ini files).
118 echoCommandLine(int argc
, char **argv
, ostream
&out
)
120 out
<< "command line: " << argv
[0];
121 for (int i
= 1; i
< argc
; i
++) {
126 // If the arg contains spaces, we need to quote it.
127 // The rest of this is overkill to make it look purty.
129 // print dashes first outside quotes
130 int non_dash_pos
= arg
.find_first_not_of("-");
131 out
<< arg
.substr(0, non_dash_pos
); // print dashes
132 string body
= arg
.substr(non_dash_pos
); // the rest
134 // if it's an assignment, handle the lhs & rhs separately
135 int eq_pos
= body
.find("=");
136 if (eq_pos
== string::npos
) {
140 string
lhs(body
.substr(0, eq_pos
));
141 string
rhs(body
.substr(eq_pos
+ 1));
143 out
<< quote(lhs
) << "=" << quote(rhs
);
150 main(int argc
, char **argv
)
152 // Save off program name
153 myProgName
= argv
[0];
155 signal(SIGFPE
, SIG_IGN
); // may occur on misspeculated paths
156 signal(SIGTRAP
, SIG_IGN
);
157 signal(SIGUSR1
, dumpStatsHandler
); // dump intermediate stats
158 signal(SIGUSR2
, dumprstStatsHandler
); // dump and reset stats
159 signal(SIGINT
, exitNowHandler
); // dump final stats and exit
160 signal(SIGABRT
, abortHandler
);
162 // Python embedded interpreter invocation
163 Py_SetProgramName(argv
[0]);
164 const char *fileName
= Py_GetProgramFullPath();
166 PySys_SetArgv(argc
, argv
);
168 // loadSwigModules();
170 // Set Python module path to include current file to find embedded
172 if (PyRun_SimpleString("import sys") != 0)
173 panic("Python error importing 'sys' module\n");
174 string pathCmd
= csprintf("sys.path[1:1] = ['%s']", fileName
);
175 if (PyRun_SimpleString(pathCmd
.c_str()) != 0)
176 panic("Python error setting sys.path\n");
178 // Pass compile timestamp string to Python
179 extern const char *compileDate
; // from date.cc
180 string setCompileDate
= csprintf("compileDate = '%s'", compileDate
);
181 if (PyRun_SimpleString(setCompileDate
.c_str()) != 0)
182 panic("Python error setting compileDate\n");
184 // PyRun_InteractiveLoop(stdin, "stdin");
185 // m5/__init__.py currently contains main argv parsing loop etc.,
186 // and will write out config.ini file before returning.
187 if (PyImport_ImportModule("defines") == NULL
)
188 panic("Python error importing 'defines.py'\n");
189 if (PyImport_ImportModule("m5") == NULL
)
190 panic("Python error importing 'm5' module\n");
193 configStream
= simout
.find("config.out");
195 // The configuration database is now complete; start processing it.
197 inifile
.load("config.ini");
199 // Initialize statistics database
200 Stats::InitSimStats();
202 // Now process the configuration hierarchy and create the SimObjects.
203 ConfigHierarchy
configHierarchy(inifile
);
204 configHierarchy
.build();
205 configHierarchy
.createSimObjects();
207 // Parse and check all non-config-hierarchy parameters.
208 ParamContext::parseAllContexts(inifile
);
209 ParamContext::checkAllContexts();
211 // Echo command line and all parameter settings to stats file as well.
212 echoCommandLine(argc
, argv
, *outputStream
);
213 ParamContext::showAllContexts(*configStream
);
215 // Any objects that can't connect themselves until after construction should
217 SimObject::connectAll();
219 // Do a second pass to finish initializing the sim objects
220 SimObject::initAll();
222 // Restore checkpointed state, if any.
223 configHierarchy
.unserializeSimObjects();
225 // Done processing the configuration database.
226 // Check for unreferenced entries.
227 if (inifile
.printUnreferenced())
228 panic("unreferenced sections/entries in the intermediate ini file");
230 SimObject::regAllStats();
232 // uncomment the following to get PC-based execution-time profile
234 init_profile((char *)&_init
, (char *)&_fini
);
237 // Check to make sure that the stats package is properly initialized
240 // Reset to put the stats in a consistent state.
243 warn("Entering event queue. Starting simulation...\n");
245 while (!mainEventQueue
.empty()) {
246 assert(curTick
<= mainEventQueue
.nextTick() &&
247 "event scheduled in the past");
249 // forward current cycle to the time of the first event on the
251 curTick
= mainEventQueue
.nextTick();
252 mainEventQueue
.serviceOne();
259 using namespace Stats
;
260 SetupEvent(Dump
, curTick
);
263 if (async_dumpreset
) {
264 async_dumpreset
= false;
266 using namespace Stats
;
267 SetupEvent(Dump
| Reset
, curTick
);
272 new SimExitEvent("User requested STOP");
275 if (async_io
|| async_alarm
) {
283 // This should never happen... every conceivable way for the
284 // simulation to terminate (hit max cycles/insts, signal,
285 // simulated system halts/exits) generates an exit event, so we
286 // should never run out of events on the queue.
287 exitNow("no events on event loop! All CPUs must be idle.", 1);