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 "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 "cpu/base_cpu.hh"
46 #include "cpu/full_cpu/smt.hh"
47 #include "sim/async.hh"
48 #include "sim/builder.hh"
49 #include "sim/configfile.hh"
50 #include "sim/host.hh"
51 #include "sim/sim_events.hh"
52 #include "sim/sim_exit.hh"
53 #include "sim/sim_object.hh"
54 #include "sim/sim_stats.hh"
55 #include "sim/sim_time.hh"
60 volatile bool async_event
= false;
61 volatile bool async_dump
= false;
62 volatile bool async_exit
= false;
63 volatile bool async_io
= false;
64 volatile bool async_alarm
= false;
66 /// Stats signal handler.
68 dumpStatsHandler(int sigtype
)
74 /// Exit signal handler.
76 exitNowHandler(int sigtype
)
82 /// Simulator executable name
83 const char *myProgName
= "";
85 /// Show brief help message.
87 showBriefHelp(ostream
&out
)
89 out
<< "Usage: " << myProgName
90 << " [-hn] [-Dname[=def]] [-Uname] [-I[dir]] "
91 << "[--<section>:<param>=<value>] [<config file> ...]" << endl
92 << " -h: print long help (including parameter listing)" << endl
93 << " -n: don't load default.ini" << endl
94 << " -u: don't quit on unreferenced parameters" << endl
95 << " -D,-U,-I: passed to cpp for preprocessing .ini files" << endl
;
98 /// Show verbose help message. Includes parameter listing from
99 /// showBriefHelp(), plus an exhaustive list of ini-file parameters
100 /// and SimObjects (with their parameters).
102 showLongHelp(ostream
&out
)
108 << "-----------------" << endl
109 << "Global Parameters" << endl
110 << "-----------------" << endl
113 ParamContext::describeAllContexts(out
);
117 << "-----------------" << endl
118 << "Simulator Objects" << endl
119 << "-----------------" << endl
122 SimObjectClass::describeAllClasses(out
);
125 /// Print welcome message.
127 sayHello(ostream
&out
)
129 extern const char *compileDate
; // from date.cc
131 ccprintf(out
, "M5 Simulator System\n");
133 ccprintf(out
, "%s\n", briefCopyright
);
134 ccprintf(out
, "M5 compiled on %d\n", compileDate
);
136 char *host
= getenv("HOSTNAME");
138 host
= getenv("HOST");
141 ccprintf(out
, "M5 executing on %s\n", host
);
143 ccprintf(out
, "M5 simulation started %s\n", Time::start
);
147 /// Echo the command line for posterity in such a way that it can be
148 /// used to rerun the same simulation (given the same .ini files).
151 echoCommandLine(int argc
, char **argv
, ostream
&out
)
153 out
<< "command line: " << argv
[0];
154 for (int i
= 1; i
< argc
; i
++) {
159 // If the arg contains spaces, we need to quote it.
160 // The rest of this is overkill to make it look purty.
162 // print dashes first outside quotes
163 int non_dash_pos
= arg
.find_first_not_of("-");
164 out
<< arg
.substr(0, non_dash_pos
); // print dashes
165 string body
= arg
.substr(non_dash_pos
); // the rest
167 // if it's an assignment, handle the lhs & rhs separately
168 int eq_pos
= body
.find("=");
169 if (eq_pos
== string::npos
) {
173 string
lhs(body
.substr(0, eq_pos
));
174 string
rhs(body
.substr(eq_pos
+ 1));
176 out
<< quote(lhs
) << "=" << quote(rhs
);
184 /// The simulator configuration database. This is the union of all
185 /// specified .ini files. This shouldn't need to be visible outside
186 /// this file, as it is passed as a parameter to all the param-parsing
189 static IniFile simConfigDB
;
191 /// Check for a default.ini file and load it if necessary.
193 handleDefaultIni(bool &loadIt
, vector
<char *> &cppArgs
)
198 if (stat("default.ini", &sb
) == 0) {
199 if (!simConfigDB
.loadCPP("default.ini", cppArgs
)) {
200 cout
<< "Error processing file default.ini" << endl
;
205 // set this whether it actually was found or not, so we don't
206 // bother to check again next time
214 main(int argc
, char **argv
)
216 // Save off program name
217 myProgName
= argv
[0];
219 signal(SIGFPE
, SIG_IGN
); // may occur on misspeculated paths
220 signal(SIGPIPE
, SIG_IGN
);
221 signal(SIGTRAP
, SIG_IGN
);
222 signal(SIGUSR1
, dumpStatsHandler
); // dump intermediate stats
223 signal(SIGINT
, exitNowHandler
); // dump final stats and exit
227 // Initialize statistics database
230 vector
<char *> cppArgs
;
232 // Should we use default.ini if it exists? By default, yes. (Use
234 bool loadDefaultIni
= true;
236 // Should we quit if there are unreferenced parameters? By
237 // default, yes... it's a good way of catching typos in
238 // section/parameter names (which otherwise go by silently). Use
240 bool quitOnUnreferenced
= true;
242 // Parse command-line options. The tricky part here is figuring
243 // out whether to look for & load default.ini, and if needed,
244 // doing so at the right time w.r.t. processing the other
247 // Since most of the complex options are handled through the
248 // config database, we don't mess with getopts, and just parse
250 for (int i
= 1; i
< argc
; ++i
) {
251 char *arg_str
= argv
[i
];
253 // if arg starts with '-', parse as option,
254 // else treat it as a configuration file name and load it
255 if (arg_str
[0] == '-') {
257 // switch on second char
258 switch (arg_str
[1]) {
265 // -n: don't load default.ini
266 if (!loadDefaultIni
) {
267 cerr
<< "Warning: -n option needs to precede any "
268 << "explicit configuration file name " << endl
269 << " or command-line configuration parameter."
272 loadDefaultIni
= false;
276 // -u: don't quit on unreferenced parameters
277 quitOnUnreferenced
= false;
283 // cpp options: record & pass to cpp. Note that these
284 // cannot have spaces, i.e., '-Dname=val' is OK, but
285 // '-D name=val' is not. I don't consider this a
286 // problem, since even though gnu cpp accepts the
287 // latter, other cpp implementations do not (Tru64,
289 cppArgs
.push_back(arg_str
);
293 // command-line configuration parameter:
294 // '--<section>:<parameter>=<value>'
296 // Load default.ini if necessary -- see comment in
297 // else clause below.
298 handleDefaultIni(loadDefaultIni
, cppArgs
);
300 if (!simConfigDB
.add(arg_str
+ 2)) {
303 "Could not parse configuration argument '%s'\n"
304 "Expecting --<section>:<parameter>=<value>\n",
312 ccprintf(cerr
, "Fatal: invalid argument '%s'\n", arg_str
);
317 // no '-', treat as config file name
319 // If we haven't loaded default.ini yet, and we want to,
320 // now is the time. Can't do it sooner because we need to
321 // look for '-n', can't do it later since we want
322 // default.ini loaded first (so that any other settings
324 handleDefaultIni(loadDefaultIni
, cppArgs
);
326 if (!simConfigDB
.loadCPP(arg_str
, cppArgs
)) {
327 cprintf("Error processing file %s\n", arg_str
);
333 // Final check for default.ini, in case no config files or
334 // command-line config parameters were given.
335 handleDefaultIni(loadDefaultIni
, cppArgs
);
337 // The configuration database is now complete; start processing it.
339 // Parse and check all non-config-hierarchy parameters.
340 ParamContext::parseAllContexts(simConfigDB
);
341 ParamContext::checkAllContexts();
343 // Print header info into stats file. Can't do this sooner since
344 // the stat file name is set via a .ini param... thus it just got
345 // opened above during ParamContext::checkAllContexts().
347 // Print hello message to stats file if it's actually a file. If
348 // it's not (i.e. it's cout or cerr) then we already did it above.
349 if (statStreamIsFile
)
350 sayHello(*statStream
);
352 // Echo command line and all parameter settings to stats file as well.
353 echoCommandLine(argc
, argv
, *statStream
);
354 ParamContext::showAllContexts(*statStream
);
356 // Now process the configuration hierarchy and create the SimObjects.
357 ConfigHierarchy
configHierarchy(simConfigDB
);
358 configHierarchy
.build();
359 configHierarchy
.createSimObjects();
361 // Restore checkpointed state, if any.
362 configHierarchy
.unserializeSimObjects();
364 // Done processing the configuration database.
365 // Check for unreferenced entries.
366 if (simConfigDB
.printUnreferenced() && quitOnUnreferenced
) {
367 cerr
<< "Fatal: unreferenced .ini sections/entries." << endl
368 << "If this is not an error, add 'unref_section_ok=y' or "
369 << "'unref_entries_ok=y' to the appropriate sections "
370 << "to suppress this message." << endl
;
374 SimObject::regAllStats();
376 // uncomment the following to get PC-based execution-time profile
378 init_profile((char *)&_init
, (char *)&_fini
);
381 // Check to make sure that the stats package is properly initialized
384 // Nothing to simulate if we don't have at least one CPU somewhere.
385 if (BaseCPU::numSimulatedCPUs() == 0) {
386 cerr
<< "Fatal: no CPUs to simulate." << endl
;
390 while (!mainEventQueue
.empty()) {
391 assert(curTick
<= mainEventQueue
.nextTick() &&
392 "event scheduled in the past");
394 // forward current cycle to the time of the first event on the
396 curTick
= mainEventQueue
.nextTick();
397 mainEventQueue
.serviceOne();
403 new DumpStatsEvent();
408 new SimExitEvent("User requested STOP");
411 if (async_io
|| async_alarm
) {
419 // This should never happen... every conceivable way for the
420 // simulation to terminate (hit max cycles/insts, signal,
421 // simulated system halts/exits) generates an exit event, so we
422 // should never run out of events on the queue.
423 exitNow("improperly exited event loop!", 1);