2 * Copyright (c) 2003 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 "sim/host.hh"
41 #include "base/misc.hh"
43 #include "base/copyright.hh"
44 #include "base/inifile.hh"
45 #include "sim/configfile.hh"
46 #include "base/pollevent.hh"
47 #include "base/statistics.hh"
48 #include "sim/sim_events.hh"
49 #include "sim/sim_exit.hh"
50 #include "sim/sim_object.hh"
51 #include "sim/sim_stats.hh"
52 #include "sim/sim_time.hh"
53 #include "cpu/full_cpu/smt.hh"
55 #include "cpu/base_cpu.hh"
56 #include "sim/async.hh"
61 volatile bool async_event
= false;
62 volatile bool async_dump
= false;
63 volatile bool async_exit
= false;
64 volatile bool async_io
= false;
65 volatile bool async_alarm
= false;
67 /// Stats signal handler.
69 dumpStatsHandler(int sigtype
)
75 /// Exit signal handler.
77 exitNowHandler(int sigtype
)
83 /// Simulator executable name
84 const char *myProgName
= "";
86 /// Show brief help message.
88 showBriefHelp(ostream
&out
)
90 out
<< "Usage: " << myProgName
91 << " [-hn] [-Dname[=def]] [-Uname] [-I[dir]] "
92 << "[--<section>:<param>=<value>] [<config file> ...]" << endl
93 << " -h: print long help (including parameter listing)" << endl
94 << " -n: don't load default.ini" << endl
95 << " -u: don't quit on unreferenced parameters" << endl
96 << " -D,-U,-I: passed to cpp for preprocessing .ini files" << endl
;
99 /// Show verbose help message. Includes parameter listing from
100 /// showBriefHelp(), plus an exhaustive list of ini-file parameters
101 /// and SimObjects (with their parameters).
103 showLongHelp(ostream
&out
)
109 << "-----------------" << endl
110 << "Global Parameters" << endl
111 << "-----------------" << endl
114 ParamContext::describeAllContexts(out
);
118 << "-----------------" << endl
119 << "Simulator Objects" << endl
120 << "-----------------" << endl
123 SimObjectClass::describeAllClasses(out
);
126 /// Print welcome message.
128 sayHello(ostream
&out
)
130 extern const char *compileDate
; // from date.cc
132 ccprintf(out
, "M5 Simulator System\n");
134 ccprintf(out
, "%s\n", briefCopyright
);
135 ccprintf(out
, "M5 compiled on %d\n", compileDate
);
137 char *host
= getenv("HOSTNAME");
139 host
= getenv("HOST");
142 ccprintf(out
, "M5 executing on %s\n", host
);
144 ccprintf(out
, "M5 simulation started %s\n", Time::start
);
148 /// Echo the command line for posterity in such a way that it can be
149 /// used to rerun the same simulation (given the same .ini files).
152 echoCommandLine(int argc
, char **argv
, ostream
&out
)
154 out
<< "command line: " << argv
[0];
155 for (int i
= 1; i
< argc
; i
++) {
160 // If the arg contains spaces, we need to quote it.
161 // The rest of this is overkill to make it look purty.
163 // print dashes first outside quotes
164 int non_dash_pos
= arg
.find_first_not_of("-");
165 out
<< arg
.substr(0, non_dash_pos
); // print dashes
166 string body
= arg
.substr(non_dash_pos
); // the rest
168 // if it's an assignment, handle the lhs & rhs separately
169 int eq_pos
= body
.find("=");
170 if (eq_pos
== string::npos
) {
174 string
lhs(body
.substr(0, eq_pos
));
175 string
rhs(body
.substr(eq_pos
+ 1));
177 out
<< quote(lhs
) << "=" << quote(rhs
);
185 /// The simulator configuration database. This is the union of all
186 /// specified .ini files. This shouldn't need to be visible outside
187 /// this file, as it is passed as a parameter to all the param-parsing
190 static IniFile simConfigDB
;
192 /// Check for a default.ini file and load it if necessary.
194 handleDefaultIni(bool &loadIt
, vector
<char *> &cppArgs
)
199 if (stat("default.ini", &sb
) == 0) {
200 if (!simConfigDB
.loadCPP("default.ini", cppArgs
)) {
201 cout
<< "Error processing file default.ini" << endl
;
206 // set this whether it actually was found or not, so we don't
207 // bother to check again next time
215 main(int argc
, char **argv
)
217 // Save off program name
218 myProgName
= argv
[0];
220 signal(SIGFPE
, SIG_IGN
); // may occur on misspeculated paths
221 signal(SIGPIPE
, SIG_IGN
);
222 signal(SIGTRAP
, SIG_IGN
);
223 signal(SIGUSR1
, dumpStatsHandler
); // dump intermediate stats
224 signal(SIGINT
, exitNowHandler
); // dump final stats and exit
228 // Initialize statistics database
231 vector
<char *> cppArgs
;
233 // Should we use default.ini if it exists? By default, yes. (Use
235 bool loadDefaultIni
= true;
237 // Should we quit if there are unreferenced parameters? By
238 // default, yes... it's a good way of catching typos in
239 // section/parameter names (which otherwise go by silently). Use
241 bool quitOnUnreferenced
= true;
243 // Parse command-line options. The tricky part here is figuring
244 // out whether to look for & load default.ini, and if needed,
245 // doing so at the right time w.r.t. processing the other
248 // Since most of the complex options are handled through the
249 // config database, we don't mess with getopts, and just parse
251 for (int i
= 1; i
< argc
; ++i
) {
252 char *arg_str
= argv
[i
];
254 // if arg starts with '-', parse as option,
255 // else treat it as a configuration file name and load it
256 if (arg_str
[0] == '-') {
258 // switch on second char
259 switch (arg_str
[1]) {
266 // -n: don't load default.ini
267 if (!loadDefaultIni
) {
268 cerr
<< "Warning: -n option needs to precede any "
269 << "explicit configuration file name " << endl
270 << " or command-line configuration parameter."
273 loadDefaultIni
= false;
277 // -u: don't quit on unreferenced parameters
278 quitOnUnreferenced
= false;
284 // cpp options: record & pass to cpp. Note that these
285 // cannot have spaces, i.e., '-Dname=val' is OK, but
286 // '-D name=val' is not. I don't consider this a
287 // problem, since even though gnu cpp accepts the
288 // latter, other cpp implementations do not (Tru64,
290 cppArgs
.push_back(arg_str
);
294 // command-line configuration parameter:
295 // '--<section>:<parameter>=<value>'
297 // Load default.ini if necessary -- see comment in
298 // else clause below.
299 handleDefaultIni(loadDefaultIni
, cppArgs
);
301 if (!simConfigDB
.add(arg_str
+ 2)) {
304 "Could not parse configuration argument '%s'\n"
305 "Expecting --<section>:<parameter>=<value>\n",
313 ccprintf(cerr
, "Fatal: invalid argument '%s'\n", arg_str
);
318 // no '-', treat as config file name
320 // If we haven't loaded default.ini yet, and we want to,
321 // now is the time. Can't do it sooner because we need to
322 // look for '-n', can't do it later since we want
323 // default.ini loaded first (so that any other settings
325 handleDefaultIni(loadDefaultIni
, cppArgs
);
327 if (!simConfigDB
.loadCPP(arg_str
, cppArgs
)) {
328 cprintf("Error processing file %s\n", arg_str
);
334 // Final check for default.ini, in case no config files or
335 // command-line config parameters were given.
336 handleDefaultIni(loadDefaultIni
, cppArgs
);
338 // The configuration database is now complete; start processing it.
340 // Parse and check all non-config-hierarchy parameters.
341 ParamContext::parseAllContexts(simConfigDB
);
342 ParamContext::checkAllContexts();
344 // Print header info into stats file. Can't do this sooner since
345 // the stat file name is set via a .ini param... thus it just got
346 // opened above during ParamContext::checkAllContexts().
348 // Print hello message to stats file if it's actually a file. If
349 // it's not (i.e. it's cout or cerr) then we already did it above.
350 if (statStreamIsFile
)
351 sayHello(*statStream
);
353 // Echo command line and all parameter settings to stats file as well.
354 echoCommandLine(argc
, argv
, *statStream
);
355 ParamContext::showAllContexts(*statStream
);
357 // Now process the configuration hierarchy and create the SimObjects.
358 ConfigHierarchy
configHierarchy(simConfigDB
);
359 configHierarchy
.build();
360 configHierarchy
.createSimObjects();
362 // Restore checkpointed state, if any.
363 configHierarchy
.unserializeSimObjects();
365 // Done processing the configuration database.
366 // Check for unreferenced entries.
367 if (simConfigDB
.printUnreferenced() && quitOnUnreferenced
) {
368 cerr
<< "Fatal: unreferenced .ini sections/entries." << endl
369 << "If this is not an error, add 'unref_section_ok=y' or "
370 << "'unref_entries_ok=y' to the appropriate sections "
371 << "to suppress this message." << endl
;
375 SimObject::regAllStats();
377 // uncomment the following to get PC-based execution-time profile
379 init_profile((char *)&_init
, (char *)&_fini
);
382 // Check to make sure that the stats package is properly initialized
385 // Nothing to simulate if we don't have at least one CPU somewhere.
386 if (BaseCPU::numSimulatedCPUs() == 0) {
387 cerr
<< "Fatal: no CPUs to simulate." << endl
;
391 while (!mainEventQueue
.empty()) {
392 assert(curTick
<= mainEventQueue
.nextTick() &&
393 "event scheduled in the past");
395 // forward current cycle to the time of the first event on the
397 curTick
= mainEventQueue
.nextTick();
398 mainEventQueue
.serviceOne();
404 new DumpStatsEvent();
409 new SimExitEvent("User requested STOP");
412 if (async_io
|| async_alarm
) {
420 // This should never happen... every conceivable way for the
421 // simulation to terminate (hit max cycles/insts, signal,
422 // simulated system halts/exits) generates an exit event, so we
423 // should never run out of events on the queue.
424 exitNow("improperly exited event loop!", 1);