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 "cpu/base.hh"
60 #include "sim/async.hh"
61 #include "sim/builder.hh"
62 #include "sim/configfile.hh"
63 #include "sim/host.hh"
64 #include "sim/sim_events.hh"
65 #include "sim/sim_exit.hh"
66 #include "sim/sim_object.hh"
67 #include "sim/stat_control.hh"
68 #include "sim/stats.hh"
69 #include "sim/root.hh"
74 volatile bool async_event
= false;
75 volatile bool async_dump
= false;
76 volatile bool async_dumpreset
= false;
77 volatile bool async_exit
= false;
78 volatile bool async_io
= false;
79 volatile bool async_alarm
= false;
81 /// Stats signal handler.
83 dumpStatsHandler(int sigtype
)
90 dumprstStatsHandler(int sigtype
)
93 async_dumpreset
= true;
96 /// Exit signal handler.
98 exitNowHandler(int sigtype
)
104 /// Abort signal handler.
106 abortHandler(int sigtype
)
108 cerr
<< "Program aborted at cycle " << curTick
<< endl
;
111 // dump trace buffer, if there is one
112 Trace::theLog
.dump(cerr
);
116 /// Simulator executable name
117 char *myProgName
= "";
119 /// Show brief help message.
121 showBriefHelp(ostream
&out
)
123 char *prog
= basename(myProgName
);
125 ccprintf(out
, "Usage:\n");
127 "%s [-p <path>] [-i ] [-h] <config file>\n"
129 " -p, --path <path> prepends <path> to PYTHONPATH instead of using\n"
130 " built-in zip archive. Useful when developing/debugging\n"
131 " changes to built-in Python libraries, as the new Python\n"
132 " can be tested without building a new m5 binary.\n\n"
133 " -i, --interactive forces entry into interactive mode after the supplied\n"
134 " script is executed (just like the -i option to the\n"
135 " Python interpreter).\n\n"
136 " -h Prints this help\n\n"
137 " <configfile> config file name (ends in .py)\n\n",
142 const char *briefCopyright
=
143 "Copyright (c) 2001-2006\n"
144 "The Regents of The University of Michigan\n"
145 "All Rights Reserved\n";
147 /// Print welcome message.
149 sayHello(ostream
&out
)
151 extern const char *compileDate
; // from date.cc
153 ccprintf(out
, "M5 Simulator System\n");
155 ccprintf(out
, "%s\n", briefCopyright
);
156 ccprintf(out
, "M5 compiled %d\n", compileDate
);
157 ccprintf(out
, "M5 started %s\n", Time::start
);
159 char *host
= getenv("HOSTNAME");
161 host
= getenv("HOST");
164 ccprintf(out
, "M5 executing on %s\n", host
);
168 extern "C" { void init_main(); }
171 main(int argc
, char **argv
)
173 // Saze off program name
174 myProgName
= argv
[0];
178 signal(SIGFPE
, SIG_IGN
); // may occur on misspeculated paths
179 signal(SIGTRAP
, SIG_IGN
);
180 signal(SIGUSR1
, dumpStatsHandler
); // dump intermediate stats
181 signal(SIGUSR2
, dumprstStatsHandler
); // dump and reset stats
182 signal(SIGINT
, exitNowHandler
); // dump final stats and exit
183 signal(SIGABRT
, abortHandler
);
185 Py_SetProgramName(argv
[0]);
187 // default path to m5 python code is the currently executing
188 // file... Python ZipImporter will find embedded zip archive
189 char *pythonpath
= argv
[0];
191 bool interactive
= false;
192 bool show_help
= false;
193 bool getopt_done
= false;
196 static struct option long_options
[] = {
197 {"python", 1, 0, 'p'},
198 {"interactive", 0, 0, 'i'},
204 switch (getopt_long(argc
, argv
, "+p:ih", long_options
, &opt_index
)) {
205 // -p <path> prepends <path> to PYTHONPATH instead of
206 // using built-in zip archive. Useful when
207 // developing/debugging changes to built-in Python
208 // libraries, as the new Python can be tested without
209 // building a new m5 binary.
214 // -i forces entry into interactive mode after the
215 // supplied script is executed (just like the -i option to
216 // the Python interpreter).
229 fatal("Unrecognized option %c\n", optopt
);
231 } while (!getopt_done
);
238 // Fix up argc & argv to hide arguments we just processed.
239 // getopt() sets optind to the index of the first non-processed
244 // Set up PYTHONPATH to make sure the m5 module is found
245 string
newpath(pythonpath
);
247 char *oldpath
= getenv("PYTHONPATH");
248 if (oldpath
!= NULL
) {
253 if (setenv("PYTHONPATH", newpath
.c_str(), true) == -1)
254 fatal("setenv: %s\n", strerror(errno
));
256 // initialize embedded Python interpreter
258 PySys_SetArgv(argc
, argv
);
260 // initialize SWIG 'main' module
264 // extra arg(s): first is script file, remaining ones are args
266 char *filename
= argv
[0];
267 FILE *fp
= fopen(filename
, "r");
269 fatal("cannot open file '%s'\n", filename
);
272 PyRun_AnyFile(fp
, filename
);
274 // no script file argument... force interactive prompt
279 // The following code to import readline was copied from Python
280 // 2.4.3's Modules/main.c.
281 // Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006
282 // Python Software Foundation; All Rights Reserved
283 // We should only enable this if we're actually using an
284 // interactive prompt.
286 v
= PyImport_ImportModule("readline");
292 PyRun_InteractiveLoop(stdin
, "stdin");
295 // clean up Python intepreter.
300 /// Initialize C++ configuration. Exported to Python via SWIG; invoked
301 /// from m5.instantiate().
305 configStream
= simout
.find("config.out");
307 // The configuration database is now complete; start processing it.
309 inifile
.load("config.ini");
311 // Initialize statistics database
312 Stats::InitSimStats();
314 // Now process the configuration hierarchy and create the SimObjects.
315 ConfigHierarchy
configHierarchy(inifile
);
316 configHierarchy
.build();
317 configHierarchy
.createSimObjects();
319 // Parse and check all non-config-hierarchy parameters.
320 ParamContext::parseAllContexts(inifile
);
321 ParamContext::checkAllContexts();
323 // Echo all parameter settings to stats file as well.
324 ParamContext::showAllContexts(*configStream
);
326 // Any objects that can't connect themselves until after construction should
328 SimObject::connectAll();
330 // Do a second pass to finish initializing the sim objects
331 SimObject::initAll();
333 // Restore checkpointed state, if any.
334 configHierarchy
.unserializeSimObjects();
336 // Done processing the configuration database.
337 // Check for unreferenced entries.
338 if (inifile
.printUnreferenced())
339 panic("unreferenced sections/entries in the intermediate ini file");
341 SimObject::regAllStats();
343 // uncomment the following to get PC-based execution-time profile
345 init_profile((char *)&_init
, (char *)&_fini
);
348 // Check to make sure that the stats package is properly initialized
351 // Reset to put the stats in a consistent state.
358 /** Simulate for num_cycles additional cycles. If num_cycles is -1
359 * (the default), do not limit simulation; some other event must
360 * terminate the loop. Exported to Python via SWIG.
361 * @return The SimLoopExitEvent that caused the loop to exit.
364 simulate(Tick num_cycles
= -1)
366 warn("Entering event queue @ %d. Starting simulation...\n", curTick
);
368 // Fix up num_cycles. Special default value -1 means simulate
369 // "forever"... schedule event at MaxTick just to be safe.
370 // Otherwise it's a delta for additional cycles to simulate past
371 // curTick, and thus must be non-negative.
372 if (num_cycles
== -1)
373 num_cycles
= MaxTick
;
374 else if (num_cycles
< 0)
375 fatal("simulate: num_cycles must be >= 0 (was %d)\n", num_cycles
);
377 num_cycles
= curTick
+ num_cycles
;
379 Event
*limit_event
= new SimLoopExitEvent(num_cycles
,
380 "simulate() limit reached");
383 // there should always be at least one event (the SimLoopExitEvent
384 // we just scheduled) in the queue
385 assert(!mainEventQueue
.empty());
386 assert(curTick
<= mainEventQueue
.nextTick() &&
387 "event scheduled in the past");
389 // forward current cycle to the time of the first event on the
391 curTick
= mainEventQueue
.nextTick();
392 Event
*exit_event
= mainEventQueue
.serviceOne();
393 if (exit_event
!= NULL
) {
394 // hit some kind of exit event; return to Python
395 // event must be subclass of SimLoopExitEvent...
396 SimLoopExitEvent
*se_event
= dynamic_cast<SimLoopExitEvent
*>(exit_event
);
397 if (se_event
== NULL
)
398 panic("Bogus exit event class!");
400 // if we didn't hit limit_event, delete it
401 if (se_event
!= limit_event
) {
402 assert(limit_event
->scheduled());
403 limit_event
->deschedule();
415 using namespace Stats
;
416 SetupEvent(Dump
, curTick
);
419 if (async_dumpreset
) {
420 async_dumpreset
= false;
422 using namespace Stats
;
423 SetupEvent(Dump
| Reset
, curTick
);
428 exitSimLoop("user interrupt received");
431 if (async_io
|| async_alarm
) {
439 // not reached... only exit is return on SimLoopExitEvent
443 * Queue of C++ callbacks to invoke on simulator exit.
445 CallbackQueue exitCallbacks
;
448 * Register an exit callback.
451 registerExitCallback(Callback
*callback
)
453 exitCallbacks
.add(callback
);
457 * Do C++ simulator exit processing. Exported to SWIG to be invoked
458 * when simulator terminates via Python's atexit mechanism.
463 exitCallbacks
.process();
464 exitCallbacks
.clear();
468 ParamContext::cleanupAllContexts();
470 // print simulation stats