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 "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_init.hh"
55 #include "sim/sim_object.hh"
56 #include "sim/sim_stats.hh"
57 #include "sim/universe.hh"
62 volatile bool async_event
= false;
63 volatile bool async_dump
= false;
64 volatile bool async_dumpreset
= false;
65 volatile bool async_exit
= false;
66 volatile bool async_io
= false;
67 volatile bool async_alarm
= false;
69 /// Stats signal handler.
71 dumpStatsHandler(int sigtype
)
78 dumprstStatsHandler(int sigtype
)
81 async_dumpreset
= true;
84 /// Exit signal handler.
86 exitNowHandler(int sigtype
)
92 /// Simulator executable name
93 const char *myProgName
= "";
95 /// Show brief help message.
97 showBriefHelp(ostream
&out
)
99 out
<< "Usage: " << myProgName
100 << " [-hn] [-Dname[=def]] [-Uname] [-I[dir]] "
101 << "[--<section>:<param>=<value>] [<config file> ...]" << endl
102 << " -h: print long help (including parameter listing)" << endl
103 << " -n: don't load default.ini" << endl
104 << " -u: don't quit on unreferenced parameters" << endl
105 << " -D,-U,-I: passed to cpp for preprocessing .ini files" << endl
;
108 /// Show verbose help message. Includes parameter listing from
109 /// showBriefHelp(), plus an exhaustive list of ini-file parameters
110 /// and SimObjects (with their parameters).
112 showLongHelp(ostream
&out
)
118 << "-----------------" << endl
119 << "Global Parameters" << endl
120 << "-----------------" << endl
123 ParamContext::describeAllContexts(out
);
127 << "-----------------" << endl
128 << "Simulator Objects" << endl
129 << "-----------------" << endl
132 SimObjectClass::describeAllClasses(out
);
135 /// Print welcome message.
137 sayHello(ostream
&out
)
139 extern const char *compileDate
; // from date.cc
141 ccprintf(out
, "M5 Simulator System\n");
143 ccprintf(out
, "%s\n", briefCopyright
);
144 ccprintf(out
, "M5 compiled on %d\n", compileDate
);
146 char *host
= getenv("HOSTNAME");
148 host
= getenv("HOST");
151 ccprintf(out
, "M5 executing on %s\n", host
);
153 ccprintf(out
, "M5 simulation started %s\n", Time::start
);
157 /// Echo the command line for posterity in such a way that it can be
158 /// used to rerun the same simulation (given the same .ini files).
161 echoCommandLine(int argc
, char **argv
, ostream
&out
)
163 out
<< "command line: " << argv
[0];
164 for (int i
= 1; i
< argc
; i
++) {
169 // If the arg contains spaces, we need to quote it.
170 // The rest of this is overkill to make it look purty.
172 // print dashes first outside quotes
173 int non_dash_pos
= arg
.find_first_not_of("-");
174 out
<< arg
.substr(0, non_dash_pos
); // print dashes
175 string body
= arg
.substr(non_dash_pos
); // the rest
177 // if it's an assignment, handle the lhs & rhs separately
178 int eq_pos
= body
.find("=");
179 if (eq_pos
== string::npos
) {
183 string
lhs(body
.substr(0, eq_pos
));
184 string
rhs(body
.substr(eq_pos
+ 1));
186 out
<< quote(lhs
) << "=" << quote(rhs
);
194 /// The simulator configuration database. This is the union of all
195 /// specified .ini files. This shouldn't need to be visible outside
196 /// this file, as it is passed as a parameter to all the param-parsing
199 static IniFile simConfigDB
;
201 /// Check for a default.ini file and load it if necessary.
203 handleDefaultIni(bool &loadIt
, vector
<char *> &cppArgs
)
208 if (stat("default.ini", &sb
) == 0) {
209 if (!simConfigDB
.loadCPP("default.ini", cppArgs
)) {
210 cout
<< "Error processing file default.ini" << endl
;
215 // set this whether it actually was found or not, so we don't
216 // bother to check again next time
224 main(int argc
, char **argv
)
226 // Save off program name
227 myProgName
= argv
[0];
229 signal(SIGFPE
, SIG_IGN
); // may occur on misspeculated paths
230 signal(SIGTRAP
, SIG_IGN
);
231 signal(SIGUSR1
, dumpStatsHandler
); // dump intermediate stats
232 signal(SIGUSR2
, dumprstStatsHandler
); // dump and reset stats
233 signal(SIGINT
, exitNowHandler
); // dump final stats and exit
237 // Initialize statistics database
240 vector
<char *> cppArgs
;
242 // Should we use default.ini if it exists? By default, yes. (Use
244 bool loadDefaultIni
= true;
246 // Should we quit if there are unreferenced parameters? By
247 // default, yes... it's a good way of catching typos in
248 // section/parameter names (which otherwise go by silently). Use
250 bool quitOnUnreferenced
= true;
252 // Parse command-line options. The tricky part here is figuring
253 // out whether to look for & load default.ini, and if needed,
254 // doing so at the right time w.r.t. processing the other
257 // Since most of the complex options are handled through the
258 // config database, we don't mess with getopts, and just parse
260 for (int i
= 1; i
< argc
; ++i
) {
261 char *arg_str
= argv
[i
];
263 // if arg starts with '-', parse as option,
264 // else treat it as a configuration file name and load it
265 if (arg_str
[0] == '-') {
267 // switch on second char
268 switch (arg_str
[1]) {
275 // -n: don't load default.ini
276 if (!loadDefaultIni
) {
277 cerr
<< "Warning: -n option needs to precede any "
278 << "explicit configuration file name " << endl
279 << " or command-line configuration parameter."
282 loadDefaultIni
= false;
286 // -u: don't quit on unreferenced parameters
287 quitOnUnreferenced
= false;
293 // cpp options: record & pass to cpp. Note that these
294 // cannot have spaces, i.e., '-Dname=val' is OK, but
295 // '-D name=val' is not. I don't consider this a
296 // problem, since even though gnu cpp accepts the
297 // latter, other cpp implementations do not (Tru64,
299 cppArgs
.push_back(arg_str
);
303 // command-line configuration parameter:
304 // '--<section>:<parameter>=<value>'
306 // Load default.ini if necessary -- see comment in
307 // else clause below.
308 handleDefaultIni(loadDefaultIni
, cppArgs
);
310 if (!simConfigDB
.add(arg_str
+ 2)) {
313 "Could not parse configuration argument '%s'\n"
314 "Expecting --<section>:<parameter>=<value>\n",
322 ccprintf(cerr
, "Fatal: invalid argument '%s'\n", arg_str
);
327 // no '-', treat as config file name
329 // If we haven't loaded default.ini yet, and we want to,
330 // now is the time. Can't do it sooner because we need to
331 // look for '-n', can't do it later since we want
332 // default.ini loaded first (so that any other settings
334 handleDefaultIni(loadDefaultIni
, cppArgs
);
336 if (!simConfigDB
.loadCPP(arg_str
, cppArgs
)) {
337 cprintf("Error processing file %s\n", arg_str
);
343 // Final check for default.ini, in case no config files or
344 // command-line config parameters were given.
345 handleDefaultIni(loadDefaultIni
, cppArgs
);
347 // The configuration database is now complete; start processing it.
349 // Parse and check all non-config-hierarchy parameters.
350 ParamContext::parseAllContexts(simConfigDB
);
351 ParamContext::checkAllContexts();
353 // Print header info into stats file. Can't do this sooner since
354 // the stat file name is set via a .ini param... thus it just got
355 // opened above during ParamContext::checkAllContexts().
357 // Print hello message to stats file if it's actually a file. If
358 // it's not (i.e. it's cout or cerr) then we already did it above.
359 if (outputStream
!= &cout
&& outputStream
!= &cerr
)
360 sayHello(*outputStream
);
362 // Echo command line and all parameter settings to stats file as well.
363 echoCommandLine(argc
, argv
, *outputStream
);
364 ParamContext::showAllContexts(builderStream());
366 // Now process the configuration hierarchy and create the SimObjects.
367 ConfigHierarchy
configHierarchy(simConfigDB
);
368 configHierarchy
.build();
369 configHierarchy
.createSimObjects();
371 // Restore checkpointed state, if any.
372 configHierarchy
.unserializeSimObjects();
374 // Done processing the configuration database.
375 // Check for unreferenced entries.
376 if (simConfigDB
.printUnreferenced() && quitOnUnreferenced
) {
377 cerr
<< "Fatal: unreferenced .ini sections/entries." << endl
378 << "If this is not an error, add 'unref_section_ok=y' or "
379 << "'unref_entries_ok=y' to the appropriate sections "
380 << "to suppress this message." << endl
;
384 SimObject::regAllStats();
386 // uncomment the following to get PC-based execution-time profile
388 init_profile((char *)&_init
, (char *)&_fini
);
391 // Check to make sure that the stats package is properly initialized
394 // Reset to put the stats in a consistent state.
397 // Nothing to simulate if we don't have at least one CPU somewhere.
398 if (BaseCPU::numSimulatedCPUs() == 0) {
399 cerr
<< "Fatal: no CPUs to simulate." << endl
;
404 warn("Entering event queue. Starting simulation...\n");
406 while (!mainEventQueue
.empty()) {
407 assert(curTick
<= mainEventQueue
.nextTick() &&
408 "event scheduled in the past");
410 // forward current cycle to the time of the first event on the
412 curTick
= mainEventQueue
.nextTick();
413 mainEventQueue
.serviceOne();
420 using namespace Statistics
;
421 SetupEvent(Dump
, curTick
);
424 if (async_dumpreset
) {
425 async_dumpreset
= false;
427 using namespace Statistics
;
428 SetupEvent(Dump
| Reset
, curTick
);
433 new SimExitEvent("User requested STOP");
436 if (async_io
|| async_alarm
) {
444 // This should never happen... every conceivable way for the
445 // simulation to terminate (hit max cycles/insts, signal,
446 // simulated system halts/exits) generates an exit event, so we
447 // should never run out of events on the queue.
448 exitNow("no events on event loop! All CPUs must be idle.", 1);