2 * Copyright (c) 2000-2004 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 <sys/types.h>
40 #include "base/copyright.hh"
41 #include "base/inifile.hh"
42 #include "base/misc.hh"
43 #include "base/pollevent.hh"
44 #include "base/statistics.hh"
45 #include "base/time.hh"
46 #include "cpu/base_cpu.hh"
47 #include "cpu/full_cpu/smt.hh"
48 #include "sim/async.hh"
49 #include "sim/builder.hh"
50 #include "sim/configfile.hh"
51 #include "sim/host.hh"
52 #include "sim/sim_events.hh"
53 #include "sim/sim_exit.hh"
54 #include "sim/sim_object.hh"
55 #include "sim/stat_control.hh"
56 #include "sim/stats.hh"
57 #include "sim/universe.hh"
58 #include "sim/pyconfig/pyconfig.hh"
63 volatile bool async_event
= false;
64 volatile bool async_dump
= false;
65 volatile bool async_dumpreset
= false;
66 volatile bool async_exit
= false;
67 volatile bool async_io
= false;
68 volatile bool async_alarm
= false;
70 /// Stats signal handler.
72 dumpStatsHandler(int sigtype
)
79 dumprstStatsHandler(int sigtype
)
82 async_dumpreset
= true;
85 /// Exit signal handler.
87 exitNowHandler(int sigtype
)
93 /// Abort signal handler.
95 abortHandler(int sigtype
)
97 cerr
<< "Program aborted at cycle " << curTick
<< endl
;
100 // dump trace buffer, if there is one
101 Trace::theLog
.dump(cerr
);
105 /// Simulator executable name
106 const char *myProgName
= "";
108 /// Show brief help message.
110 showBriefHelp(ostream
&out
)
112 out
<< "Usage: " << myProgName
113 << " [-hnu] [-Dname[=def]] [-Uname] [-I[dir]] "
114 << "<config-spec> [<config-spec> ...]\n"
115 << "[] [<config file> ...]\n"
116 << " -h: print long help (including parameter listing)\n"
117 << " -u: don't quit on unreferenced parameters\n"
118 << " -D,-U,-I: passed to cpp for preprocessing .ini files\n"
119 << " <config-spec>: config file name (.ini or .py) or\n"
120 << " single param (--<section>:<param>=<value>)"
124 /// Show verbose help message. Includes parameter listing from
125 /// showBriefHelp(), plus an exhaustive list of ini-file parameters
126 /// and SimObjects (with their parameters).
128 showLongHelp(ostream
&out
)
134 << "-----------------" << endl
135 << "Global Parameters" << endl
136 << "-----------------" << endl
139 ParamContext::describeAllContexts(out
);
143 << "-----------------" << endl
144 << "Simulator Objects" << endl
145 << "-----------------" << endl
148 SimObjectClass::describeAllClasses(out
);
151 /// Print welcome message.
153 sayHello(ostream
&out
)
155 extern const char *compileDate
; // from date.cc
157 ccprintf(out
, "M5 Simulator System\n");
159 ccprintf(out
, "%s\n", briefCopyright
);
160 ccprintf(out
, "M5 compiled on %d\n", compileDate
);
162 char *host
= getenv("HOSTNAME");
164 host
= getenv("HOST");
167 ccprintf(out
, "M5 executing on %s\n", host
);
169 ccprintf(out
, "M5 simulation started %s\n", Time::start
);
173 /// Echo the command line for posterity in such a way that it can be
174 /// used to rerun the same simulation (given the same .ini files).
177 echoCommandLine(int argc
, char **argv
, ostream
&out
)
179 out
<< "command line: " << argv
[0];
180 for (int i
= 1; i
< argc
; i
++) {
185 // If the arg contains spaces, we need to quote it.
186 // The rest of this is overkill to make it look purty.
188 // print dashes first outside quotes
189 int non_dash_pos
= arg
.find_first_not_of("-");
190 out
<< arg
.substr(0, non_dash_pos
); // print dashes
191 string body
= arg
.substr(non_dash_pos
); // the rest
193 // if it's an assignment, handle the lhs & rhs separately
194 int eq_pos
= body
.find("=");
195 if (eq_pos
== string::npos
) {
199 string
lhs(body
.substr(0, eq_pos
));
200 string
rhs(body
.substr(eq_pos
+ 1));
202 out
<< quote(lhs
) << "=" << quote(rhs
);
210 /// The simulator configuration database. This is the union of all
211 /// specified .ini files. This shouldn't need to be visible outside
212 /// this file, as it is passed as a parameter to all the param-parsing
215 static IniFile simConfigDB
;
219 main(int argc
, char **argv
)
221 // Save off program name
222 myProgName
= argv
[0];
224 signal(SIGFPE
, SIG_IGN
); // may occur on misspeculated paths
225 signal(SIGTRAP
, SIG_IGN
);
226 signal(SIGUSR1
, dumpStatsHandler
); // dump intermediate stats
227 signal(SIGUSR2
, dumprstStatsHandler
); // dump and reset stats
228 signal(SIGINT
, exitNowHandler
); // dump final stats and exit
229 signal(SIGABRT
, abortHandler
);
233 // Initialize statistics database
234 Stats::InitSimStats();
236 vector
<char *> cppArgs
;
238 // Should we quit if there are unreferenced parameters? By
239 // default, yes... it's a good way of catching typos in
240 // section/parameter names (which otherwise go by silently). Use
242 bool quitOnUnreferenced
= true;
244 // Parse command-line options.
245 // Since most of the complex options are handled through the
246 // config database, we don't mess with getopts, and just parse
248 for (int i
= 1; i
< argc
; ++i
) {
249 char *arg_str
= argv
[i
];
251 // if arg starts with '-', parse as option,
252 // else treat it as a configuration file name and load it
253 if (arg_str
[0] == '-') {
255 // switch on second char
256 switch (arg_str
[1]) {
263 // -u: don't quit on unreferenced parameters
264 quitOnUnreferenced
= false;
270 // cpp options: record & pass to cpp. Note that these
271 // cannot have spaces, i.e., '-Dname=val' is OK, but
272 // '-D name=val' is not. I don't consider this a
273 // problem, since even though gnu cpp accepts the
274 // latter, other cpp implementations do not (Tru64,
276 cppArgs
.push_back(arg_str
);
280 // command-line configuration parameter:
281 // '--<section>:<parameter>=<value>'
282 if (!simConfigDB
.add(arg_str
+ 2)) {
285 "Could not parse configuration argument '%s'\n"
286 "Expecting --<section>:<parameter>=<value>\n",
294 ccprintf(cerr
, "Fatal: invalid argument '%s'\n", arg_str
);
299 // no '-', treat as config file name
301 // make STL string out of file name
302 string
filename(arg_str
);
304 int ext_loc
= filename
.rfind(".");
307 (ext_loc
!= string::npos
) ? filename
.substr(ext_loc
) : "";
310 if (!simConfigDB
.loadCPP(filename
, cppArgs
)) {
311 cprintf("Error processing file %s\n", filename
);
314 } else if (ext
== ".py") {
315 if (!loadPythonConfig(filename
, &simConfigDB
)) {
316 cprintf("Error processing file %s\n", filename
);
321 cprintf("Config file name '%s' must end in '.py' or '.ini'.\n",
328 // The configuration database is now complete; start processing it.
330 // Parse and check all non-config-hierarchy parameters.
331 ParamContext::parseAllContexts(simConfigDB
);
332 ParamContext::checkAllContexts();
334 // Print header info into stats file. Can't do this sooner since
335 // the stat file name is set via a .ini param... thus it just got
336 // opened above during ParamContext::checkAllContexts().
338 // Print hello message to stats file if it's actually a file. If
339 // it's not (i.e. it's cout or cerr) then we already did it above.
340 if (outputStream
!= &cout
&& outputStream
!= &cerr
)
341 sayHello(*outputStream
);
343 // Echo command line and all parameter settings to stats file as well.
344 echoCommandLine(argc
, argv
, *outputStream
);
345 ParamContext::showAllContexts(*configStream
);
347 // Now process the configuration hierarchy and create the SimObjects.
348 ConfigHierarchy
configHierarchy(simConfigDB
);
349 configHierarchy
.build();
350 configHierarchy
.createSimObjects();
352 // Do a second pass to finish initializing the sim objects
353 SimObject::initAll();
355 // Restore checkpointed state, if any.
356 configHierarchy
.unserializeSimObjects();
358 // Done processing the configuration database.
359 // Check for unreferenced entries.
360 if (simConfigDB
.printUnreferenced() && quitOnUnreferenced
) {
361 cerr
<< "Fatal: unreferenced .ini sections/entries." << endl
362 << "If this is not an error, add 'unref_section_ok=y' or "
363 << "'unref_entries_ok=y' to the appropriate sections "
364 << "to suppress this message." << endl
;
368 SimObject::regAllStats();
370 // uncomment the following to get PC-based execution-time profile
372 init_profile((char *)&_init
, (char *)&_fini
);
375 // Check to make sure that the stats package is properly initialized
378 // Reset to put the stats in a consistent state.
381 // Nothing to simulate if we don't have at least one CPU somewhere.
382 if (BaseCPU::numSimulatedCPUs() == 0) {
383 cerr
<< "Fatal: no CPUs to simulate." << endl
;
387 warn("Entering event queue. Starting simulation...\n");
389 while (!mainEventQueue
.empty()) {
390 assert(curTick
<= mainEventQueue
.nextTick() &&
391 "event scheduled in the past");
393 // forward current cycle to the time of the first event on the
395 curTick
= mainEventQueue
.nextTick();
396 mainEventQueue
.serviceOne();
403 using namespace Stats
;
404 SetupEvent(Dump
, curTick
);
407 if (async_dumpreset
) {
408 async_dumpreset
= false;
410 using namespace Stats
;
411 SetupEvent(Dump
| Reset
, curTick
);
416 new SimExitEvent("User requested STOP");
419 if (async_io
|| async_alarm
) {
427 // This should never happen... every conceivable way for the
428 // simulation to terminate (hit max cycles/insts, signal,
429 // simulated system halts/exits) generates an exit event, so we
430 // should never run out of events on the queue.
431 exitNow("no events on event loop! All CPUs must be idle.", 1);